This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch ty/cleanTmp
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit b3db566774a4caaba31907032b0f7930b8bc42da
Author: JackieTien97 <[email protected]>
AuthorDate: Fri Mar 27 16:12:57 2026 +0800

    Clean up tmp dirs of udf and sort while starting up
---
 .../java/org/apache/iotdb/db/service/DataNode.java   | 12 ++++++++++++
 .../db/service/TemporaryQueryDataFileService.java    | 20 ++++++++++++++------
 .../iotdb/commons/executable/ExecutableManager.java  | 17 ++++++++++-------
 3 files changed, 36 insertions(+), 13 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/DataNode.java 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/DataNode.java
index 0b13da60110..7004d09872d 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/DataNode.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/DataNode.java
@@ -765,6 +765,15 @@ public class DataNode extends ServerCommandLine implements 
DataNodeMBean {
     }
   }
 
+  private void cleanupSortTmpDir() {
+    String sortTmpDir = config.getSortTmpDir();
+    File tmpDir = new File(sortTmpDir);
+    if (tmpDir.exists()) {
+      FileUtils.deleteFileOrDirectory(tmpDir, true);
+      logger.info("Cleaned up stale sort temp directory: {}", sortTmpDir);
+    }
+  }
+
   private void prepareResources() throws StartupException {
     prepareUDFResources();
     prepareTriggerResources();
@@ -819,6 +828,9 @@ public class DataNode extends ServerCommandLine implements 
DataNodeMBean {
     registerManager.register(new JMXService());
     JMXService.registerMBean(getInstance(), mbeanName);
 
+    // Clean up stale sort temp files left from previous runs
+    cleanupSortTmpDir();
+
     // Get resources for trigger,udf,pipe...
     prepareResources();
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/TemporaryQueryDataFileService.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/TemporaryQueryDataFileService.java
index cfc56734d00..d21e0f134a3 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/TemporaryQueryDataFileService.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/TemporaryQueryDataFileService.java
@@ -33,6 +33,7 @@ import org.slf4j.LoggerFactory;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -59,10 +60,9 @@ public class TemporaryQueryDataFileService implements 
IService {
 
   public String register(SerializationRecorder recorder) throws IOException {
     String queryId = recorder.getQueryId();
-    if (!recorders.containsKey(queryId)) {
-      recorders.put(queryId, new ArrayList<>());
-    }
-    recorders.get(queryId).add(recorder);
+    recorders
+        .computeIfAbsent(queryId, k -> Collections.synchronizedList(new 
ArrayList<>()))
+        .add(recorder);
 
     String dirName = getDirName(queryId);
     makeDirIfNecessary(dirName);
@@ -109,6 +109,11 @@ public class TemporaryQueryDataFileService implements 
IService {
   @Override
   public void start() throws StartupException {
     try {
+      // Clean up stale temp directories left from previous runs (e.g., after 
a crash)
+      File tmpDir = SystemFileFactory.INSTANCE.getFile(TEMPORARY_FILE_DIR);
+      if (tmpDir.exists()) {
+        FileUtils.deleteDirectory(tmpDir);
+      }
       makeDirIfNecessary(TEMPORARY_FILE_DIR);
     } catch (IOException e) {
       throw new StartupException(e);
@@ -117,8 +122,11 @@ public class TemporaryQueryDataFileService implements 
IService {
 
   @Override
   public void stop() {
-    for (Object queryId : recorders.keySet().toArray()) {
-      deregister((String) queryId);
+    recorders.clear();
+    try {
+      
FileUtils.deleteDirectory(SystemFileFactory.INSTANCE.getFile(TEMPORARY_FILE_DIR));
+    } catch (IOException e) {
+      logger.warn("Failed to delete temp dir {}: {}", TEMPORARY_FILE_DIR, e);
     }
   }
 
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
index cb6ca1664ac..f0a3fb33976 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/executable/ExecutableManager.java
@@ -86,8 +86,7 @@ public class ExecutableManager {
       for (String uriString : uris) {
         final URL url = new URI(uriString).toURL();
         final String fileName = uriString.substring(uriString.lastIndexOf("/") 
+ 1);
-        final String destination =
-            temporaryLibRoot + File.separator + requestId + File.separator + 
fileName;
+        final String destination = temporaryLibRoot + File.separator + 
requestId + File.separator + fileName;
         FileUtils.copyURLToFile(url, 
FSFactoryProducer.getFSFactory().getFile(destination));
       }
     } catch (Exception e) {
@@ -211,7 +210,8 @@ public class ExecutableManager {
   }
 
   /**
-   * Create and save the file if the specified file does not exist, or this 
method will override the
+   * Create and save the file if the specified file does not exist, or this 
method
+   * will override the
    * existing file.
    */
   protected void saveToDir(ByteBuffer byteBuffer, String destination) throws 
IOException {
@@ -223,7 +223,8 @@ public class ExecutableManager {
         }
         Files.createFile(path);
       }
-      // FileOutPutStream is not in append mode by default, so the file will 
be overridden if it
+      // FileOutPutStream is not in append mode by default, so the file will be
+      // overridden if it
       // already exists.
       try (FileOutputStream outputStream = new FileOutputStream(destination)) {
         outputStream.getChannel().write(byteBuffer);
@@ -238,7 +239,8 @@ public class ExecutableManager {
 
   /**
    * @param byteBuffer file
-   * @param fileName The name of the file. Absolute Path will be libRoot + 
File_Separator + fileName
+   * @param fileName   The name of the file. Absolute Path will be libRoot +
+   *                   File_Separator + fileName
    */
   public void saveToLibDir(ByteBuffer byteBuffer, String fileName) throws 
IOException {
     String destination = this.libRoot + File.separator + fileName;
@@ -247,8 +249,9 @@ public class ExecutableManager {
 
   /**
    * @param byteBuffer file
-   * @param fileName Absolute Path will be libRoot + File_Separator + 
INSTALL_DIR + File.separator +
-   *     fileName
+   * @param fileName   Absolute Path will be libRoot + File_Separator +
+   *                   INSTALL_DIR + File.separator +
+   *                   fileName
    */
   public void saveToInstallDir(ByteBuffer byteBuffer, String fileName) throws 
IOException {
     String destination = this.libRoot + File.separator + INSTALL_DIR + 
File.separator + fileName;

Reply via email to