gaurav-narula commented on code in PR #24320:
URL: https://github.com/apache/kafka/pull/24320#discussion_r4719204264
##########
metadata/src/test/java/org/apache/kafka/image/TopicsImageTest.java:
##########
@@ -703,6 +728,534 @@ 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.UNKNOWN));
+ TopicsDelta delta = new TopicsDelta(image);
+ RecordTestUtils.replayAll(delta, topicRecords);
+ assertEquals(
+ Map.of(topicId, new TopicImage.NoMirror()),
+ delta.localChanges(localId).mirrorTopicStates()
+ );
+
+ TopicsImage finalImage = delta.apply();
+ assertEquals(new TopicImage.NoMirror(),
finalImage.getTopic(topicId).mirror());
+
+ List<ApiMessageAndVersion> imageRecords = getImageRecords(image,
MetadataVersion.IBP_4_5_IV1);
+ imageRecords.addAll(topicRecords);
+ testToImage(finalImage, Optional.of(imageRecords));
+ }
+
+ @Test
+ public void testMirrorTopicDisassociationWithInvalidDesiredState() {
+ 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[] {3, 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());
+ }
+
+ @Test
+ public void testSnapshotClearsMirrorTopicAssociation() {
+ int localId = 3;
+ Uuid topicId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
+ Uuid otherTopicId = Uuid.fromString("f62ptyETTjet8SL5ZeREiw");
+
+ List<TopicImage> topics = new ArrayList<>();
+ topics.add(
+ newTopicImage(
+ "topic",
+ topicId,
+ new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.MIRRORING.value()),
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ topics.add(
+ newTopicImage(
+ "other-topic",
+ otherTopicId,
+ new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.PAUSED.value()),
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ TopicsImage image = new TopicsImage(newTopicsByIdMap(topics),
newTopicsByNameMap(topics));
+
+ // The snapshot contains both topics, but "topic" is no longer
associated with the mirror.
+ List<TopicImage> snapshotTopics = new ArrayList<>();
+ snapshotTopics.add(
+ newTopicImage(
+ "topic",
+ topicId,
+ newPartition(new int[] {localId, 1, 2})
+ )
+ );
+ snapshotTopics.add(topics.get(1));
+ TopicsImage snapshotImage = new
TopicsImage(newTopicsByIdMap(snapshotTopics),
newTopicsByNameMap(snapshotTopics));
+ List<ApiMessageAndVersion> snapshotRecords =
getImageRecords(snapshotImage, MetadataVersion.IBP_4_5_IV1);
+
+ TopicsDelta delta = new TopicsDelta(image);
+ RecordTestUtils.replayAll(delta, snapshotRecords);
+ delta.finishSnapshot();
+
+ // The topic whose mirror association was removed is reported even
though the snapshot has no
+ // MirrorTopicStateChangeRecord for it. The other topic is reported
because its record is replayed.
+ assertEquals(
+ Map.of(
+ topicId, new TopicImage.NoMirror(),
+ otherTopicId, new TopicImage.ActiveMirror("test-mirror",
MirrorPartitionState.PAUSED.value())
+ ),
+ delta.localChanges(localId).mirrorTopicStates()
Review Comment:
> The KIP says "the delta layer detects the same-value replay and sets a
recover flag on the change", but mirrorTopicStates only carries the resulting
Mirror, so a state change and a same-value rewrite are indistinguishable to the
consumer. Is adding the recover flag planned for a follow-up PR?
I think the KIP needs to be updated. We initially thought of a recover-flag
but later decided against it. MirrorMetadataManager would handle both scenarios
and in certain cases, same-value rewrites would be re-applied to ensure the
state machine makes progress.
> When a broker loads a snapshot, every mirrored topic's record is replayed,
so unchanged topics are also reported (e.g. other-topic in
testSnapshotClearsMirrorTopicAssociation). By the KIP's definition, this is
also a same-value replay. Should snapshot replays count as recoveries?
I don't think we'd be disambiguating between snapshot replays.
--
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]