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

shashikant pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new a59b571  RATIS-1023.During installing snapshot, when snapshot file is 
corrup… (#168)
a59b571 is described below

commit a59b571be196969cb0f0a1b8f6362df525245c7b
Author: Rui Wang <[email protected]>
AuthorDate: Wed Aug 12 03:13:37 2020 -0700

    RATIS-1023.During installing snapshot, when snapshot file is corrup… (#168)
---
 .../main/java/org/apache/ratis/util/FileUtils.java |  7 ++++
 .../java/org/apache/ratis/util/FileUtilsTest.java  | 41 ++++++++++++++++++++++
 .../ratis/logservice/api/LogServiceClient.java     |  2 +-
 .../ratis/logservice/server/TestMetaServer.java    |  2 +-
 .../ratis/server/storage/SnapshotManager.java      |  7 ++--
 5 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/ratis-common/src/main/java/org/apache/ratis/util/FileUtils.java 
b/ratis-common/src/main/java/org/apache/ratis/util/FileUtils.java
index 76c110e..eea2ea1 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/FileUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/FileUtils.java
@@ -183,4 +183,11 @@ public interface FileUtils {
       }
     });
   }
+
+  // Rename a file by appending .corrupt to file name. This function does not 
guarantee
+  // that the rename operation is successful.
+  static void renameFileToCorrupt(File tmpSnapshotFile) {
+    File corruptedTempFile = new File(tmpSnapshotFile.getPath() + ".corrupt");
+    tmpSnapshotFile.renameTo(corruptedTempFile);
+  }
 }
diff --git 
a/ratis-common/src/test/java/org/apache/ratis/util/FileUtilsTest.java 
b/ratis-common/src/test/java/org/apache/ratis/util/FileUtilsTest.java
new file mode 100644
index 0000000..7196a44
--- /dev/null
+++ b/ratis-common/src/test/java/org/apache/ratis/util/FileUtilsTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.ratis.util;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import org.junit.Test;
+
+/** Test methods of SnapshotManager. */
+public class FileUtilsTest {
+
+  @Test
+  public void testRenameToCorrupt() throws IOException {
+    File srcFile = File.createTempFile("snapshot.1_20", null);
+    File corruptFile = new File(srcFile.getPath() + ".corrupt");
+    if (corruptFile.exists()) {
+      FileUtils.deleteFully(corruptFile);
+    }
+    FileUtils.renameFileToCorrupt(srcFile);
+    srcFile.deleteOnExit();
+    corruptFile.deleteOnExit();
+    assertTrue(corruptFile.exists());
+  }
+}
diff --git 
a/ratis-logservice/src/main/java/org/apache/ratis/logservice/api/LogServiceClient.java
 
b/ratis-logservice/src/main/java/org/apache/ratis/logservice/api/LogServiceClient.java
index 5752e53..dd25967 100644
--- 
a/ratis-logservice/src/main/java/org/apache/ratis/logservice/api/LogServiceClient.java
+++ 
b/ratis-logservice/src/main/java/org/apache/ratis/logservice/api/LogServiceClient.java
@@ -72,7 +72,7 @@ public class LogServiceClient implements AutoCloseable {
     /**
      * Constuctor (with configuration). Build raft client for meta quorum
      * @param metaQuorum
-     * @param config log serice configuration
+     * @param config log service configuration
      * @param properties
      */
     public LogServiceClient(String metaQuorum, LogServiceConfiguration config, 
RaftProperties properties) {
diff --git 
a/ratis-logservice/src/test/java/org/apache/ratis/logservice/server/TestMetaServer.java
 
b/ratis-logservice/src/test/java/org/apache/ratis/logservice/server/TestMetaServer.java
index c911fad..34f32ea 100644
--- 
a/ratis-logservice/src/test/java/org/apache/ratis/logservice/server/TestMetaServer.java
+++ 
b/ratis-logservice/src/test/java/org/apache/ratis/logservice/server/TestMetaServer.java
@@ -120,7 +120,7 @@ public class TestMetaServer {
 
     @Before
     public void before() {
-        //ensure workers before each test
+        // ensure workers before each test
         if (workers.size() < 3) {
             cluster.createWorkers(3 - workers.size());
         }
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
index 69ab2c7..63985aa 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java
@@ -22,6 +22,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.channels.FileChannel;
 
+import org.apache.ratis.io.CorruptedFileException;
 import org.apache.ratis.io.MD5Hash;
 import org.apache.ratis.protocol.RaftPeerId;
 import org.apache.ratis.proto.RaftProtos.FileChunkProto;
@@ -115,8 +116,10 @@ public class SnapshotManager {
         if (!digest.equals(expectedDigest)) {
           LOG.warn("The snapshot md5 digest {} does not match expected {}",
               digest, expectedDigest);
-          //TODO: rename the temp snapshot file to .corrupt
-          throw new IOException("MD5 mismatch for snapshot-" + 
lastIncludedIndex
+          // rename the temp snapshot file to .corrupt
+          FileUtils.renameFileToCorrupt(tmpSnapshotFile);
+          throw new CorruptedFileException(
+              tmpSnapshotFile, "MD5 mismatch for snapshot-" + lastIncludedIndex
               + " installation");
         } else {
           MD5FileUtil.saveMD5File(tmpSnapshotFile, digest);

Reply via email to