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

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


The following commit(s) were added to refs/heads/master by this push:
     new c2d103ef9 create parent directory (#802)
c2d103ef9 is described below

commit c2d103ef9d3d9d7d2c199b8e28c7709f64ea71e9
Author: William Song <[email protected]>
AuthorDate: Thu Jan 5 13:45:42 2023 +0800

    create parent directory (#802)
---
 .../src/main/java/org/apache/ratis/util/FileUtils.java    | 15 +++++++++++++++
 .../java/org/apache/ratis/server/impl/ServerState.java    |  2 +-
 .../org/apache/ratis/server/storage/SnapshotManager.java  |  5 ++---
 3 files changed, 18 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 15ec6ea94..be736b09f 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
@@ -71,6 +71,21 @@ public interface FileUtils {
         () -> "Files.createDirectories " + dir);
   }
 
+  static void createDirectoriesDeleteExistingNonDirectory(File dir) throws 
IOException {
+    createDirectoriesDeleteExistingNonDirectory(dir.toPath());
+  }
+
+  static void createDirectoriesDeleteExistingNonDirectory(Path dir) throws 
IOException {
+    try {
+      createDirectories(dir);
+    } catch (FileAlreadyExistsException e) {
+      LOG.warn("Failed to create directory " + dir
+          + " since it already exists as a non-directory.  Trying to delete it 
...", e);
+      delete(dir);
+      createDirectories(dir);
+    }
+  }
+
   static void move(File src, File dst) throws IOException {
     move(src.toPath(), dst.toPath());
   }
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
index 826c4cff8..e0832c9be 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
@@ -441,7 +441,7 @@ class ServerState {
     // TODO: verify that we need to install the snapshot
     StateMachine sm = server.getStateMachine();
     sm.pause(); // pause the SM to prepare for install snapshot
-    snapshotManager.installSnapshot(request, sm, getStorage().getStorageDir());
+    snapshotManager.installSnapshot(request, sm);
     
updateInstalledSnapshotIndex(TermIndex.valueOf(request.getSnapshotChunk().getTermIndex()));
   }
 
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 dbde86940..1e2f70a68 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
@@ -77,8 +77,7 @@ public class SnapshotManager {
         new File(dir.get().getRoot(), c.getFilename()).toPath()).toString();
   }
 
-  public void installSnapshot(InstallSnapshotRequestProto request, 
StateMachine stateMachine, RaftStorageDirectory dir)
-      throws IOException {
+  public void installSnapshot(InstallSnapshotRequestProto request, 
StateMachine stateMachine) throws IOException {
     final InstallSnapshotRequestProto.SnapshotChunkProto snapshotChunkRequest 
= request.getSnapshotChunk();
     final long lastIncludedIndex = 
snapshotChunkRequest.getTermIndex().getIndex();
 
@@ -102,7 +101,7 @@ public class SnapshotManager {
       }
 
       final File tmpSnapshotFile = new File(tmpDir, 
getRelativePath.apply(chunk));
-      FileUtils.createDirectories(tmpSnapshotFile);
+      
FileUtils.createDirectoriesDeleteExistingNonDirectory(tmpSnapshotFile.getParentFile());
 
       FileOutputStream out = null;
       try {

Reply via email to