cxzl25 commented on code in PR #6587:
URL: https://github.com/apache/kyuubi/pull/6587#discussion_r1728269982


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/service/TempFileService.scala:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.service
+
+import java.nio.file.{Path, Paths}
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.atomic.AtomicLong
+
+import com.google.common.cache.{Cache, CacheBuilder, RemovalNotification}
+
+import org.apache.kyuubi.Utils
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.service.TempFileService.tempFileCounter
+import org.apache.kyuubi.util.{TempFileCleanupUtils, ThreadUtils}
+
+class TempFileService(name: String) extends AbstractService(name) {
+  def this() = this(classOf[TempFileService].getSimpleName)
+
+  final private var expiringFiles: Cache[String, String] = _
+  private lazy val cleanupScheduler =
+    
ThreadUtils.newDaemonSingleThreadScheduledExecutor(s"$name-cleanup-scheduler")
+
+  override def initialize(conf: KyuubiConf): Unit = {
+    super.initialize(conf)
+    val interval = conf.get(KyuubiConf.SERVER_STALE_FILE_EXPIRATION_INTERVAL)
+    expiringFiles = CacheBuilder.newBuilder()
+      .expireAfterWrite(interval, TimeUnit.MILLISECONDS)
+      .removalListener((notification: RemovalNotification[String, String]) => {
+        val pathStr = notification.getValue
+        debug(s"Remove expired temp file: $pathStr")
+        cleanupFilePath(pathStr)
+      })
+      .build[String, String]()
+
+    cleanupScheduler.scheduleAtFixedRate(
+      () => expiringFiles.cleanUp(),
+      0,
+      Math.max(interval / 10, 100),
+      TimeUnit.MILLISECONDS)
+  }
+
+  override def stop(): Unit = {
+    expiringFiles.asMap().values().forEach(cleanupFilePath)
+    super.stop()
+  }
+
+  private def cleanupFilePath(pathStr: String): Unit = {
+    try {
+      val path = Paths.get(pathStr)
+      TempFileCleanupUtils.cancelDeleteOnExit(path)
+      Utils.deleteDirectoryRecursively(path.toFile)
+    } catch {
+      case e: Throwable => error(s"Failed to delete file $pathStr", e)
+    }
+  }
+
+  /**
+   * add the file path to the expiration list
+   * ensuring the path will be deleted
+   * either after duration
+   * or on the JVM exit
+   *
+   * @param path the path of file or directory
+   */
+  def addPathToExpiration(path: Path): Unit = {
+    require(path != null)
+    if (!expiringFiles.asMap().values().contains(path)) {

Review Comment:
   Why is the key value of excitingFiles designed like this id -> path?
   
   How efficient is the contains value method?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to