Skip to content

Commit 5ec376a

Browse files
committed
Fix IoTConsensus snapshot fragment placement
1 parent 8ff057c commit 5ec376a

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerImpl.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
public class IoTConsensusServerImpl {
109109

110110
public static final String SNAPSHOT_DIR_NAME = "snapshot";
111+
private static final String SNAPSHOT_LOG_NAME = "snapshot.log";
111112
private static final Pattern SNAPSHOT_INDEX_PATTEN = Pattern.compile(".*[^\\d](?=(\\d+))");
112113
private static final PerformanceOverviewMetrics PERFORMANCE_OVERVIEW_METRICS =
113114
PerformanceOverviewMetrics.getInstance();
@@ -378,25 +379,48 @@ public void receiveSnapshotFragment(
378379
throws ConsensusGroupModifyPeerException {
379380
try {
380381
String targetFilePath = calculateSnapshotPath(snapshotId, originalFilePath);
382+
File existingFile = getExistingSnapshotFile(targetFilePath);
383+
if (existingFile != null) {
384+
writeSnapshotFragment(existingFile, fileChunk, fileOffset);
385+
return;
386+
}
387+
381388
recvFolderManager.getNextWithRetry(
382389
folder -> {
383-
File targetFile = getSnapshotPath(folder, targetFilePath);
384-
Path parentDir = Paths.get(targetFile.getParent());
385-
if (!Files.exists(parentDir)) {
386-
Files.createDirectories(parentDir);
387-
}
388-
try (FileOutputStream fos = new FileOutputStream(targetFile.getAbsolutePath(), true);
389-
FileChannel channel = fos.getChannel()) {
390-
channel.write(fileChunk.slice(), fileOffset);
391-
}
390+
writeSnapshotFragment(getSnapshotPath(folder, targetFilePath), fileChunk, fileOffset);
392391
return null;
393392
});
393+
} catch (IOException e) {
394+
throw new ConsensusGroupModifyPeerException(
395+
String.format(IoTConsensusMessages.ERROR_RECEIVING_SNAPSHOT, snapshotId), e);
394396
} catch (DiskSpaceInsufficientException e) {
395397
throw new ConsensusGroupModifyPeerException(
396398
String.format(IoTConsensusMessages.ERROR_RECEIVING_SNAPSHOT, snapshotId), e);
397399
}
398400
}
399401

402+
private File getExistingSnapshotFile(String targetFilePath) {
403+
for (String folder : recvFolderManager.getFolders()) {
404+
File targetFile = getSnapshotPath(folder, targetFilePath);
405+
if (targetFile.exists()) {
406+
return targetFile;
407+
}
408+
}
409+
return null;
410+
}
411+
412+
private void writeSnapshotFragment(File targetFile, ByteBuffer fileChunk, long fileOffset)
413+
throws IOException {
414+
Path parentDir = Paths.get(targetFile.getParent());
415+
if (!Files.exists(parentDir)) {
416+
Files.createDirectories(parentDir);
417+
}
418+
try (FileOutputStream fos = new FileOutputStream(targetFile.getAbsolutePath(), true);
419+
FileChannel channel = fos.getChannel()) {
420+
channel.write(fileChunk.slice(), fileOffset);
421+
}
422+
}
423+
400424
private String calculateSnapshotPath(String snapshotId, String originalFilePath)
401425
throws ConsensusGroupModifyPeerException {
402426
if (!originalFilePath.contains(snapshotId)) {

0 commit comments

Comments
 (0)