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


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/operation/log/OperationLog.scala:
##########
@@ -51,17 +51,19 @@ object OperationLog extends Logging {
   /**
    * The operation log root directory, this directory will delete when JVM 
exit.

Review Comment:
   comments are outdated



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/operation/log/OperationLog.scala:
##########
@@ -51,17 +51,19 @@ object OperationLog extends Logging {
   /**
    * The operation log root directory, this directory will delete when JVM 
exit.
    */
-  def createOperationLogRootDirectory(session: Session): Unit = {
-    session.sessionManager.operationLogRoot.foreach { operationLogRoot =>
+  def createOperationLogRootDirectory(session: Session): Path = {
+    session.sessionManager.operationLogRoot.map { operationLogRoot =>
       val path = Paths.get(operationLogRoot, 
session.handle.identifier.toString)
       try {
         Files.createDirectories(path)
-        path.toFile.deleteOnExit()
+        FileExpirationUtils.deleteFileOnExit(path)
+        path
       } catch {
         case e: IOException =>
           error(s"Failed to create operation log root directory: $path", e)
+          null
       }
-    }
+    }.orNull

Review Comment:
   why not just return a `Option[Path]`?



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/util/FileExpirationUtils.scala:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.util
+
+import java.nio.file.{Path, Paths}
+import java.util
+import java.util.Collections
+import java.util.concurrent.atomic.AtomicBoolean
+
+import scala.collection.convert.ImplicitConversions.`collection asJava`
+
+import org.apache.kyuubi.Utils
+
+object FileExpirationUtils {
+  private lazy val isCleanupShutdownHookInstalled = {
+    addFilesCleanupOnExitShutdownHook()
+    new AtomicBoolean(true)
+  }
+
+  private lazy val files: util.Set[String] =
+    Collections.synchronizedSet(new util.LinkedHashSet[String]())

Review Comment:
   it's weird to maintain state in a Utils class, can we merge it into the 
Service class? BTW, we don't need to use an external collection to track those 
files, a Cache instance is sufficient, see 
https://github.com/apache/kyuubi/pull/6319/files



##########
kyuubi-server/src/main/scala/org/apache/kyuubi/server/FileCleanupService.scala:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.server
+
+import java.nio.file.Path
+import java.util.concurrent.{Executors, 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.AbstractService
+import org.apache.kyuubi.util.FileExpirationUtils
+
+class FileCleanupService(name: String) extends AbstractService(name) {
+  def this() = this(classOf[FileCleanupService].getSimpleName)
+
+  private lazy val fileCounter = new AtomicLong(0)
+  final private var expiringFiles: Cache[String, Path] = _
+  private lazy val cleanupScheduler = Executors.newScheduledThreadPool(1)
+
+  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, Path]) => {
+        val path = notification.getValue
+        FileExpirationUtils.removeFromFileList(path.toString)
+        Utils.deleteDirectoryRecursively(path.toFile)
+      })
+      .build[String, Path]()
+
+    cleanupScheduler.scheduleAtFixedRate(
+      () => expiringFiles.cleanUp(),
+      0,
+      Math.max(interval / 10, 100),
+      TimeUnit.MILLISECONDS)
+  }
+
+  override def stop(): Unit = {
+    expiringFiles.invalidateAll()

Review Comment:
   Do we guarantee it will be called after SessionManager stops?



##########
kyuubi-server/src/main/scala/org/apache/kyuubi/server/FileCleanupService.scala:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.server
+
+import java.nio.file.Path
+import java.util.concurrent.{Executors, 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.AbstractService
+import org.apache.kyuubi.util.FileExpirationUtils
+
+class FileCleanupService(name: String) extends AbstractService(name) {

Review Comment:
   maybe we can give it a more generic name, like `TempFileManager`



##########
kyuubi-server/src/main/scala/org/apache/kyuubi/server/FileCleanupService.scala:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.server
+
+import java.nio.file.Path
+import java.util.concurrent.{Executors, 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.AbstractService
+import org.apache.kyuubi.util.FileExpirationUtils
+
+class FileCleanupService(name: String) extends AbstractService(name) {

Review Comment:
   seems we also need such a service for engine.



-- 
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: notifications-unsubscr...@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@kyuubi.apache.org
For additional commands, e-mail: notifications-h...@kyuubi.apache.org

Reply via email to