fvaleri commented on code in PR #24320:
URL: https://github.com/apache/kafka/pull/24320#discussion_r4684017842
##########
metadata/src/test/java/org/apache/kafka/image/TopicsImageTest.java:
##########
@@ -703,6 +728,520 @@ public void testLocalReassignmentChanges() {
testToImage(finalImage, Optional.of(imageRecords));
}
+ private static ApiMessageAndVersion newMirrorTopicStateChangeRecord(String
topicName, String mirrorName, MirrorPartitionState state) {
+ return new ApiMessageAndVersion(
+ new MirrorTopicStateChangeRecord()
+ .setTopicName(topicName)
+ .setMirrorName(mirrorName)
+ .setDesiredState(state.value()),
+ MIRROR_TOPIC_STATE_CHANGE_RECORD.highestSupportedVersion()
+ );
+ }
+
+ @Test
+ public void testMirrorTopicStateChangeOnExistingTopic() {
+ int localId = 3;
+ Uuid topicId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
+
+ List<TopicImage> topics = new ArrayList<>();
+ topics.add(
+ newTopicImage(
+ "topic",
+ topicId,
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ TopicsImage image = new TopicsImage(newTopicsByIdMap(topics),
newTopicsByNameMap(topics));
+
+ List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic",
"test-mirror", MirrorPartitionState.MIRRORING));
+
+ TopicsDelta delta = new TopicsDelta(image);
+ RecordTestUtils.replayAll(delta, topicRecords);
+
+ LocalReplicaChanges changes = delta.localChanges(localId);
+ assertEquals(Set.of(), changes.deletes());
+ assertEquals(Map.of(), changes.electedLeaders());
+ assertEquals(Map.of(), changes.leaders());
+ assertEquals(Map.of(), changes.followers());
+ assertEquals(
+ Map.of(topicId, new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.MIRRORING.value())),
+ changes.mirrorTopicStates()
+ );
+
+ TopicsImage finalImage = delta.apply();
+ TopicImage topic = finalImage.getTopic(topicId);
+ assertEquals(new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.MIRRORING.value()), topic.mirror());
+ assertEquals(image.getTopic(topicId).partitions(), topic.partitions());
+
+ List<ApiMessageAndVersion> imageRecords = getImageRecords(image);
+ imageRecords.addAll(topicRecords);
+ testToImage(finalImage, Optional.of(imageRecords));
+ }
+
+ @Test
+ public void testMirrorTopicStateChangeOnNewTopic() {
+ int localId = 3;
+ Uuid topicId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
+
+ List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
+ topicRecords.add(
+ new ApiMessageAndVersion(
+ new TopicRecord().setName("topic").setTopicId(topicId),
+ TOPIC_RECORD.highestSupportedVersion()
+ )
+ );
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic",
"test-mirror", MirrorPartitionState.LOG_ALIGNMENT));
+ topicRecords.add(newPartitionRecord(topicId, 0, List.of(localId, 1,
2)));
+
+ TopicsDelta delta = new TopicsDelta(TopicsImage.EMPTY);
+ RecordTestUtils.replayAll(delta, topicRecords);
+
+ LocalReplicaChanges changes = delta.localChanges(localId);
+ assertEquals(Set.of(new TopicPartition("topic", 0)),
changes.electedLeaders().keySet());
+ assertEquals(
+ Map.of(topicId, new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.LOG_ALIGNMENT.value())),
+ changes.mirrorTopicStates()
+ );
+
+ TopicsImage finalImage = delta.apply();
+ TopicImage topic = finalImage.getTopic(topicId);
+ assertEquals(new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.LOG_ALIGNMENT.value()), topic.mirror());
+ assertEquals(1, topic.partitions().size());
+
+ testToImage(finalImage, Optional.of(topicRecords));
+ }
+
+ @Test
+ public void testMirrorTopicDesiredStateChange() {
+ int localId = 3;
+ Uuid topicId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
+
+ List<TopicImage> topics = new ArrayList<>();
+ topics.add(
+ newTopicImage(
+ "topic",
+ topicId,
+ new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.MIRRORING.value()),
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ TopicsImage image = new TopicsImage(newTopicsByIdMap(topics),
newTopicsByNameMap(topics));
+
+ List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic",
"test-mirror", MirrorPartitionState.PAUSED));
+
+ TopicsDelta delta = new TopicsDelta(image);
+ RecordTestUtils.replayAll(delta, topicRecords);
+
+ LocalReplicaChanges changes = delta.localChanges(localId);
+ assertEquals(
+ Map.of(topicId, new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.PAUSED.value())),
+ changes.mirrorTopicStates()
+ );
+
+ TopicsImage finalImage = delta.apply();
+ TopicImage topic = finalImage.getTopic(topicId);
+ assertEquals(new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.PAUSED.value()), topic.mirror());
+
+ List<ApiMessageAndVersion> imageRecords = getImageRecords(image,
MetadataVersion.IBP_4_5_IV1);
+ imageRecords.addAll(topicRecords);
+ testToImage(finalImage, Optional.of(imageRecords));
+ }
+
+ @Test
+ public void testMirrorTopicStateChangeLastRecordWins() {
+ int localId = 3;
+ Uuid topicId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
+
+ List<TopicImage> topics = new ArrayList<>();
+ topics.add(
+ newTopicImage(
+ "topic",
+ topicId,
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ TopicsImage image = new TopicsImage(newTopicsByIdMap(topics),
newTopicsByNameMap(topics));
+
+ List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic",
"test-mirror", MirrorPartitionState.MIRRORING));
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic",
"test-mirror", MirrorPartitionState.PAUSED));
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic",
"test-mirror", MirrorPartitionState.STOPPED));
+
+ TopicsDelta delta = new TopicsDelta(image);
+ RecordTestUtils.replayAll(delta, topicRecords);
+
+ assertEquals(
+ Map.of(topicId, new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.STOPPED.value())),
+ delta.localChanges(localId).mirrorTopicStates()
+ );
+
+ TopicsImage finalImage = delta.apply();
+ assertEquals(MirrorPartitionState.STOPPED.value(),
((TopicImage.ActiveMirror)
finalImage.getTopic(topicId).mirror()).desiredState());
+
+ List<ApiMessageAndVersion> imageRecords = getImageRecords(image);
+ imageRecords.addAll(topicRecords);
+ testToImage(finalImage, Optional.of(imageRecords));
+ }
+
+ @Test
+ public void testMirrorTopicDisassociation() {
+ int localId = 3;
+ Uuid topicId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
+
+ List<TopicImage> topics = new ArrayList<>();
+ topics.add(
+ newTopicImage(
+ "topic",
+ topicId,
+ new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.STOPPED.value()),
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ TopicsImage image = new TopicsImage(newTopicsByIdMap(topics),
newTopicsByNameMap(topics));
+
+ {
+ List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
+ topicRecords.add(newMirrorTopicStateChangeRecord("topic", null,
MirrorPartitionState.LOG_ALIGNMENT));
+ TopicsDelta delta = new TopicsDelta(image);
+ RuntimeException ex = assertThrows(RuntimeException.class, () ->
RecordTestUtils.replayAll(delta, topicRecords));
+ assertEquals(IllegalArgumentException.class,
ex.getCause().getClass());
+ assertEquals("desiredState must be UNKNOWN when mirrorName is
null, got LOG_ALIGNMENT", ex.getCause().getMessage());
+ }
Review Comment:
Can we move the validation block into a dedicated test?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]