|
21 | 21 | import java.io.FileOutputStream;
|
22 | 22 | import java.io.IOException;
|
23 | 23 | import java.nio.channels.FileChannel;
|
24 |
| -import java.util.UUID; |
25 | 24 |
|
26 | 25 | import org.apache.ratis.io.CorruptedFileException;
|
27 | 26 | import org.apache.ratis.io.MD5Hash;
|
@@ -61,22 +60,25 @@ public void installSnapshot(StateMachine stateMachine,
|
61 | 60 | final long lastIncludedIndex = snapshotChunkRequest.getTermIndex().getIndex();
|
62 | 61 | final RaftStorageDirectory dir = storage.getStorageDir();
|
63 | 62 |
|
64 |
| - // create a unique temporary directory |
65 |
| - final File tmpDir = new File(dir.getTmpDir(), UUID.randomUUID().toString()); |
| 63 | + // create a unique temporary directory based on the request id |
| 64 | + final File tmpDir = new File(dir.getTmpDir(), snapshotChunkRequest.getRequestId()); |
66 | 65 | FileUtils.createDirectories(tmpDir);
|
67 | 66 | tmpDir.deleteOnExit();
|
68 | 67 |
|
69 |
| - LOG.info("Installing snapshot:{}, to tmp dir:{}", request, tmpDir); |
| 68 | + LOG.info("Installing snapshot:{}, to tmp dir:{}", snapshotChunkRequest.getRequestId(), tmpDir); |
70 | 69 |
|
71 | 70 | // TODO: Make sure that subsequent requests for the same installSnapshot are coming in order,
|
72 | 71 | // and are not lost when whole request cycle is done. Check requestId and requestIndex here
|
73 | 72 |
|
74 | 73 | for (FileChunkProto chunk : snapshotChunkRequest.getFileChunksList()) {
|
| 74 | + LOG.info("Installing chunk :{} with offset{}, to tmp dir:{} for file {}", |
| 75 | + chunk.getChunkIndex(), chunk.getOffset(), tmpDir, chunk.getFilename()); |
75 | 76 | SnapshotInfo pi = stateMachine.getLatestSnapshot();
|
76 | 77 | if (pi != null && pi.getTermIndex().getIndex() >= lastIncludedIndex) {
|
77 | 78 | throw new IOException("There exists snapshot file "
|
78 | 79 | + pi.getFiles() + " in " + selfId
|
79 |
| - + " with endIndex >= lastIncludedIndex " + lastIncludedIndex); |
| 80 | + + " with endIndex (" + pi.getTermIndex().getIndex() |
| 81 | + + ") >= lastIncludedIndex (" + lastIncludedIndex + ")"); |
80 | 82 | }
|
81 | 83 |
|
82 | 84 | String fileName = chunk.getFilename(); // this is relative to the root dir
|
@@ -130,10 +132,10 @@ public void installSnapshot(StateMachine stateMachine,
|
130 | 132 | }
|
131 | 133 |
|
132 | 134 | if (snapshotChunkRequest.getDone()) {
|
133 |
| - LOG.info("Install snapshot is done, renaming tnp dir:{} to:{}", |
134 |
| - tmpDir, dir.getStateMachineDir()); |
135 |
| - dir.getStateMachineDir().delete(); |
136 |
| - tmpDir.renameTo(dir.getStateMachineDir()); |
| 135 | + LOG.info("Install snapshot is done, moving files from dir:{} to:{}", |
| 136 | + tmpDir, dir.getStateMachineDir()); |
| 137 | + FileUtils.moveDirectory(tmpDir.toPath(), dir.getStateMachineDir().toPath()); |
| 138 | + FileUtils.deleteFully(tmpDir); |
137 | 139 | }
|
138 | 140 | }
|
139 | 141 | }
|
0 commit comments