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