gaurav-narula commented on code in PR #24320:
URL: https://github.com/apache/kafka/pull/24320#discussion_r4720640150
##########
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:
Updated the KIP
##########
metadata/src/main/resources/common/metadata/MirrorTopicStateChangeRecord.json:
##########
@@ -0,0 +1,31 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+{
+ "apiKey": 30,
+ "type": "metadata",
+ "name": "MirrorTopicStateChangeRecord",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "TopicName", "type": "string", "versions": "0+", "entityType":
"topicName",
+ "about": "The topic name." },
+ { "name": "MirrorName", "type": "string", "versions": "0+",
"nullableVersions": "0+", "entityType": "mirrorName",
+ "about": "The cluster mirror name. Null when disassociating a topic from
a mirror." },
+ { "name": "DesiredState", "type": "int8", "versions": "0+", "default":
"-1",
+ "about": "The desired state for all mirror partitions. Valid values:
UNKNOWN(-1), LOG_ALIGNMENT(0), EPOCH_FENCING(1), ULE_RECOVERY(2), MIRRORING(3),
PAUSING(4), PAUSED(5), STOPPING(6), STOPPED(7), FAILED(8)." }
Review Comment:
Updated the KIP
--
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]