|
41 | 41 | import java.io.PrintStream; |
42 | 42 | import java.nio.file.Files; |
43 | 43 | import java.nio.file.Paths; |
| 44 | +import java.nio.file.StandardOpenOption; |
44 | 45 | import java.util.ArrayList; |
45 | 46 | import java.util.Collection; |
46 | 47 | import java.util.EnumSet; |
@@ -437,14 +438,40 @@ void doFormat(BootstrapMetadata bootstrapMetadata) throws Exception { |
437 | 438 | directoryTypes.get(writeLogDir).description(), writeLogDir, |
438 | 439 | MetadataVersion.FEATURE_NAME, releaseVersion); |
439 | 440 | Files.createDirectories(Paths.get(writeLogDir)); |
440 | | - BootstrapDirectory bootstrapDirectory = new BootstrapDirectory(writeLogDir); |
| 441 | + File parentDir = new File(writeLogDir); |
| 442 | + File clusterMetadataDirectory = new File(parentDir, String.format("%s-%d", |
| 443 | + CLUSTER_METADATA_TOPIC_PARTITION.topic(), |
| 444 | + CLUSTER_METADATA_TOPIC_PARTITION.partition())); |
| 445 | + Files.createDirectories(clusterMetadataDirectory.toPath()); |
| 446 | + BootstrapDirectory bootstrapDirectory = new BootstrapDirectory(clusterMetadataDirectory.getPath()); |
441 | 447 | bootstrapDirectory.writeBinaryFile(bootstrapMetadata); |
442 | 448 | if (directoryTypes.get(writeLogDir).isDynamicMetadataDirectory()) { |
443 | | - writeDynamicQuorumSnapshot(writeLogDir, |
| 449 | + writeDynamicQuorumSnapshot(clusterMetadataDirectory.getPath(), |
444 | 450 | initialControllers.get(), |
445 | 451 | featureLevels.get(KRaftVersion.FEATURE_NAME), |
446 | 452 | controllerListenerName); |
| 453 | + File createdBoostrapCheckpoint = new File(clusterMetadataDirectory.getPath() + "/" + BootstrapDirectory.BINARY_BOOTSTRAP_FILENAME); |
| 454 | + File created000Checkpoint = new File(clusterMetadataDirectory.getPath() + "/" + BootstrapDirectory.BINARY_CHECKPOINT_FILENAME); |
| 455 | + Files.write( |
| 456 | + createdBoostrapCheckpoint.toPath(), |
| 457 | + Files.readAllBytes(created000Checkpoint.toPath()), |
| 458 | + StandardOpenOption.APPEND); |
| 459 | + try { |
| 460 | + created000Checkpoint.delete(); |
| 461 | + createdBoostrapCheckpoint.renameTo(created000Checkpoint); |
| 462 | + } catch (Exception ex) { |
| 463 | + throw new RuntimeException("Failed operation to combine metadata and kraft records: ", ex); |
| 464 | + } |
| 465 | + } else { |
| 466 | + File createdBoostrapCheckpoint = new File(clusterMetadataDirectory.getPath() + "/" + BootstrapDirectory.BINARY_BOOTSTRAP_FILENAME); |
| 467 | + File created000Checkpoint = new File(clusterMetadataDirectory.getPath() + "/" + BootstrapDirectory.BINARY_CHECKPOINT_FILENAME); |
| 468 | + try { |
| 469 | + createdBoostrapCheckpoint.renameTo(created000Checkpoint); |
| 470 | + } catch (Exception ex) { |
| 471 | + throw new RuntimeException("Failed to rename file: ", ex); |
| 472 | + } |
447 | 473 | } |
| 474 | + |
448 | 475 | }); |
449 | 476 | copier.setWriteErrorHandler((errorLogDir, e) -> { |
450 | 477 | throw new FormatterException("Error while writing meta.properties file " + |
@@ -498,16 +525,12 @@ static void writeDynamicQuorumSnapshot( |
498 | 525 | short kraftVersion, |
499 | 526 | String controllerListenerName |
500 | 527 | ) { |
501 | | - File parentDir = new File(writeLogDir); |
502 | | - File clusterMetadataDirectory = new File(parentDir, String.format("%s-%d", |
503 | | - CLUSTER_METADATA_TOPIC_PARTITION.topic(), |
504 | | - CLUSTER_METADATA_TOPIC_PARTITION.partition())); |
505 | 528 | VoterSet voterSet = initialControllers.toVoterSet(controllerListenerName); |
506 | 529 | RecordsSnapshotWriter.Builder builder = new RecordsSnapshotWriter.Builder(). |
507 | 530 | setLastContainedLogTimestamp(Time.SYSTEM.milliseconds()). |
508 | 531 | setMaxBatchSizeBytes(KafkaRaftClient.MAX_BATCH_SIZE_BYTES). |
509 | 532 | setRawSnapshotWriter(FileRawSnapshotWriter.create( |
510 | | - clusterMetadataDirectory.toPath(), |
| 533 | + Paths.get(writeLogDir), |
511 | 534 | Snapshots.BOOTSTRAP_SNAPSHOT_ID)). |
512 | 535 | setKraftVersion(KRaftVersion.fromFeatureLevel(kraftVersion)). |
513 | 536 | setVoterSet(Optional.of(voterSet)); |
|
0 commit comments