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

haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 1840cd1a78 [IOTDB-3344] Fix dataRegion snapshot may meet file not 
found due to compaction (#6303)
1840cd1a78 is described below

commit 1840cd1a782461dc2c56ecf3ee2d9cc5446d48c0
Author: Liu Xuxin <[email protected]>
AuthorDate: Fri Jun 17 13:53:35 2022 +0800

    [IOTDB-3344] Fix dataRegion snapshot may meet file not found due to 
compaction (#6303)
---
 .../iotdb/db/engine/snapshot/SnapshotTaker.java    | 56 +++++++++++++---------
 1 file changed, 34 insertions(+), 22 deletions(-)

diff --git 
a/server/src/main/java/org/apache/iotdb/db/engine/snapshot/SnapshotTaker.java 
b/server/src/main/java/org/apache/iotdb/db/engine/snapshot/SnapshotTaker.java
index 1842b1b830..a05f8f7182 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/engine/snapshot/SnapshotTaker.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/engine/snapshot/SnapshotTaker.java
@@ -24,6 +24,7 @@ import 
org.apache.iotdb.db.engine.compaction.log.CompactionLogger;
 import org.apache.iotdb.db.engine.modification.ModificationFile;
 import 
org.apache.iotdb.db.engine.snapshot.exception.DirectoryNotLegalException;
 import org.apache.iotdb.db.engine.storagegroup.DataRegion;
+import org.apache.iotdb.db.engine.storagegroup.TsFileManager;
 import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
 
 import org.slf4j.Logger;
@@ -70,32 +71,32 @@ public class SnapshotTaker {
     }
 
     List<Long> timePartitions = dataRegion.getTimePartitions();
-    for (Long timePartition : timePartitions) {
-      List<String> seqDataDirs = getAllDataDirOfOnePartition(true, 
timePartition);
-
-      try {
-        createFileSnapshot(seqDataDirs, snapshotDir, true, timePartition);
-      } catch (IOException e) {
-        LOGGER.error("Fail to create snapshot", e);
-        File[] files = snapshotDir.listFiles();
-        if (files != null) {
-          for (File file : files) {
-            if (!file.delete()) {
-              LOGGER.error("Failed to delete link file {} after failing to 
create snapshot", file);
-            }
-          }
+    TsFileManager manager = dataRegion.getTsFileManager();
+    manager.readLock();
+    try {
+      for (Long timePartition : timePartitions) {
+        List<String> seqDataDirs = getAllDataDirOfOnePartition(true, 
timePartition);
+
+        try {
+          createFileSnapshot(seqDataDirs, snapshotDir, true, timePartition);
+        } catch (IOException e) {
+          LOGGER.error("Fail to create snapshot", e);
+          cleanUpWhenFail(snapshotDir);
+          return false;
         }
-        return false;
-      }
 
-      List<String> unseqDataDirs = getAllDataDirOfOnePartition(false, 
timePartition);
+        List<String> unseqDataDirs = getAllDataDirOfOnePartition(false, 
timePartition);
 
-      try {
-        createFileSnapshot(unseqDataDirs, snapshotDir, false, timePartition);
-      } catch (IOException e) {
-        LOGGER.error("Fail to create snapshot", e);
-        return false;
+        try {
+          createFileSnapshot(unseqDataDirs, snapshotDir, false, timePartition);
+        } catch (IOException e) {
+          LOGGER.error("Fail to create snapshot", e);
+          cleanUpWhenFail(snapshotDir);
+          return false;
+        }
       }
+    } finally {
+      manager.readUnlock();
     }
 
     return true;
@@ -163,4 +164,15 @@ public class SnapshotTaker {
       }
     }
   }
+
+  private void cleanUpWhenFail(File snapshotDir) {
+    File[] files = snapshotDir.listFiles();
+    if (files != null) {
+      for (File file : files) {
+        if (!file.delete()) {
+          LOGGER.error("Failed to delete link file {} after failing to create 
snapshot", file);
+        }
+      }
+    }
+  }
 }

Reply via email to