clolov commented on code in PR #24320:
URL: https://github.com/apache/kafka/pull/24320#discussion_r4696202310


##########
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:
   ```suggestion
   assertEquals(
       Map.of(topicId, new TopicImage.NoMirror()),
       delta.changedTopic(topicId).localChanges(localId).mirrorTopicStates()
   );
   ```



##########
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:
   I would expect the above assertion to pass, but it fails with
   ```
   Fails: expected: <{0hHJ3X5ZQ-CFfQ5xgpj90w=NoMirror[]}> but was: <{}>. 
   ```
   I this happens because TopicDelta.localChanges asks "did the mirror change?" 
while passing its own image as the "before", so it compares a value to itself 
and reports a change only when a mirror record happens to be in the delta.



-- 
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]

Reply via email to