This is an automated email from the ASF dual-hosted git repository. williamsong pushed a commit to branch snapshot-branch2 in repository https://gitbox.apache.org/repos/asf/ratis.git
commit b5f5b0c24d0186b98171ce8b9126d1d22a10e4a4 Author: William Song <[email protected]> AuthorDate: Thu Jan 5 13:45:42 2023 +0800 RATIS-1765. [GrpcLogAppender] Calculate streaming md5 file-wise when installSnapshot --- .../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 | 8 +++----- 3 files changed, 19 insertions(+), 6 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 40a51e9f9..e50edaf6d 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 b1422db4f..820f2a9f0 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 @@ -478,7 +478,7 @@ class ServerState implements Closeable { // 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(sm, request); + 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 294f0a205..2ce8f9143 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,10 +77,8 @@ public class SnapshotManager { c -> smDir.relativize(new File(dir.getRoot(), c.getFilename()).toPath()).toString(); } - public void installSnapshot(StateMachine stateMachine, - InstallSnapshotRequestProto request) throws IOException { - final InstallSnapshotRequestProto.SnapshotChunkProto snapshotChunkRequest = - request.getSnapshotChunk(); + public void installSnapshot(InstallSnapshotRequestProto request, StateMachine stateMachine) throws IOException { + final InstallSnapshotRequestProto.SnapshotChunkProto snapshotChunkRequest = request.getSnapshotChunk(); final long lastIncludedIndex = snapshotChunkRequest.getTermIndex().getIndex(); // create a unique temporary directory @@ -103,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 {
