|
108 | 108 | public class IoTConsensusServerImpl { |
109 | 109 |
|
110 | 110 | public static final String SNAPSHOT_DIR_NAME = "snapshot"; |
| 111 | + private static final String SNAPSHOT_LOG_NAME = "snapshot.log"; |
111 | 112 | private static final Pattern SNAPSHOT_INDEX_PATTEN = Pattern.compile(".*[^\\d](?=(\\d+))"); |
112 | 113 | private static final PerformanceOverviewMetrics PERFORMANCE_OVERVIEW_METRICS = |
113 | 114 | PerformanceOverviewMetrics.getInstance(); |
@@ -378,25 +379,48 @@ public void receiveSnapshotFragment( |
378 | 379 | throws ConsensusGroupModifyPeerException { |
379 | 380 | try { |
380 | 381 | String targetFilePath = calculateSnapshotPath(snapshotId, originalFilePath); |
| 382 | + File existingFile = getExistingSnapshotFile(targetFilePath); |
| 383 | + if (existingFile != null) { |
| 384 | + writeSnapshotFragment(existingFile, fileChunk, fileOffset); |
| 385 | + return; |
| 386 | + } |
| 387 | + |
381 | 388 | recvFolderManager.getNextWithRetry( |
382 | 389 | 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); |
392 | 391 | return null; |
393 | 392 | }); |
| 393 | + } catch (IOException e) { |
| 394 | + throw new ConsensusGroupModifyPeerException( |
| 395 | + String.format(IoTConsensusMessages.ERROR_RECEIVING_SNAPSHOT, snapshotId), e); |
394 | 396 | } catch (DiskSpaceInsufficientException e) { |
395 | 397 | throw new ConsensusGroupModifyPeerException( |
396 | 398 | String.format(IoTConsensusMessages.ERROR_RECEIVING_SNAPSHOT, snapshotId), e); |
397 | 399 | } |
398 | 400 | } |
399 | 401 |
|
| 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 | + |
400 | 424 | private String calculateSnapshotPath(String snapshotId, String originalFilePath) |
401 | 425 | throws ConsensusGroupModifyPeerException { |
402 | 426 | if (!originalFilePath.contains(snapshotId)) { |
|
0 commit comments