You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by qi...@apache.org on 2022/08/22 13:53:35 UTC

[iotdb] branch master updated: guarantee atomicity for latest Csnapshot (#7070)

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

qiaojialin 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 e3dbb4ae44 guarantee atomicity for latest Csnapshot (#7070)
e3dbb4ae44 is described below

commit e3dbb4ae4483fb3a91af7cefcdf08feaf2446b80
Author: William Song <48...@users.noreply.github.com>
AuthorDate: Mon Aug 22 21:53:29 2022 +0800

    guarantee atomicity for latest Csnapshot (#7070)
---
 .../consensus/ratis/ApplicationStateMachineProxy.java    |  2 +-
 .../apache/iotdb/consensus/ratis/SnapshotStorage.java    | 16 +++++++++++++++-
 .../org/apache/iotdb/consensus/ratis/SnapshotTest.java   |  6 ++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
index 601791f257..be98aac33b 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/ApplicationStateMachineProxy.java
@@ -174,7 +174,7 @@ public class ApplicationStateMachineProxy extends BaseStateMachine {
     // delete snapshotDir fully in case of last takeSnapshot() crashed
     FileUtils.deleteFully(snapshotDir);
 
-    snapshotDir.mkdir();
+    snapshotDir.mkdirs();
     if (!snapshotDir.isDirectory()) {
       logger.error("Unable to create snapshotDir at {}", snapshotDir);
       return RaftLog.INVALID_LOG_INDEX;
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java
index ecbc4ae628..de0f70a427 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/SnapshotStorage.java
@@ -90,7 +90,21 @@ public class SnapshotStorage implements StateMachineStorage {
     if (snapshots == null || snapshots.length == 0) {
       return null;
     }
-    return snapshots[snapshots.length - 1].toFile();
+    int i = snapshots.length - 1;
+    for (; i >= 0; i--) {
+      String metafilePath =
+          getMetafilePath(snapshots[i].toFile(), snapshots[i].getFileName().toString());
+      if (new File(metafilePath).exists()) {
+        break;
+      } else {
+        try {
+          FileUtils.deleteFully(snapshots[i]);
+        } catch (IOException e) {
+          logger.warn("delete incomplete snapshot directory {} failed due to {}", snapshots[i], e);
+        }
+      }
+    }
+    return i < 0 ? null : snapshots[i].toFile();
   }
 
   private List<Path> getAllFilesUnder(File rootDir) {
diff --git a/consensus/src/test/java/org/apache/iotdb/consensus/ratis/SnapshotTest.java b/consensus/src/test/java/org/apache/iotdb/consensus/ratis/SnapshotTest.java
index 45b4d6ee6b..204a6725c4 100644
--- a/consensus/src/test/java/org/apache/iotdb/consensus/ratis/SnapshotTest.java
+++ b/consensus/src/test/java/org/apache/iotdb/consensus/ratis/SnapshotTest.java
@@ -116,6 +116,12 @@ public class SnapshotTest {
     proxy.getStateMachineStorage().cleanupOldSnapshots(null);
     Assert.assertFalse(new File(snapshotFilename).exists());
     Assert.assertTrue(new File(snapshotFilenameLatest).exists());
+
+    // delete meta file, then the proxy will consider the latest snapshotInfo incomplete
+    Assert.assertTrue(new File(getSnapshotMetaFilename("616_4217")).delete());
+    info = proxy.getLatestSnapshot();
+    Assert.assertNull(info);
+    Assert.assertFalse(new File(snapshotFilenameLatest).exists());
   }
 
   private String getSnapshotMetaFilename(String termIndexMeta) {