m1a2st commented on code in PR #24320:
URL: https://github.com/apache/kafka/pull/24320#discussion_r4709336022
##########
metadata/src/main/java/org/apache/kafka/image/TopicImage.java:
##########
@@ -27,22 +28,70 @@
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
/**
* Represents a topic in the metadata image.
*
* This class is thread-safe.
*/
-public record TopicImage(String name, Uuid id, Map<Integer,
PartitionRegistration> partitions) {
+public record TopicImage(String name, Uuid id, Mirror mirror, Map<Integer,
PartitionRegistration> partitions) {
+
+ public sealed interface Mirror permits ActiveMirror, NoMirror {
+
+ }
+
+ /**
+ * The mirror state of a mirrored topic.
+ *
+ * @param name The name of the mirror. Never null.
+ * @param desiredState The desired mirror partition state.
+ */
+ public record ActiveMirror(String name, byte desiredState) implements
Mirror {
Review Comment:
Could we use `MirrorPartitionState` instead of `byte` for the `desiredState`
field?
##########
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",
Review Comment:
Just curious, why dont't we use `topicId` instead of `topicName`?
##########
metadata/src/main/java/org/apache/kafka/image/TopicDelta.java:
##########
@@ -180,6 +224,9 @@ public LocalReplicaChanges localChanges(int brokerId) {
Map<TopicPartition, LocalReplicaChanges.PartitionInfo> followers = new
HashMap<>();
Map<String, Uuid> topicIds = new HashMap<>();
Map<TopicIdPartition, Uuid> directoryIds = new HashMap<>();
+ Map<Uuid, TopicImage.Mirror> mirrorTopicChanges = new HashMap<>();
+
+ mirrorTopicStateChange(image).ifPresent(mirror ->
mirrorTopicChanges.put(id(), mirror));
Review Comment:
Is this computation still needed? `TopicsDelta.localChanges` never reads
`changes.mirrorTopicStates()`; it recomputes the changes against the previous
`TopicsImage` instead, so the value computed here is discarded.
It can also produce incorrect results in the snapshot path, where this
delta's image is the fresh `NoMirror` instance created by `TopicRecord`,
causing removed mirror associations to be missed.
Could we remove this computation and pass an empty map instead?
##########
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:
The KIP says:
> No state validation is performed because the desired state in
MetadataImage never reflects FAILED
So should we remove `FAILED(8)` from the valid values in the about text here
(and in the KIP)?
##########
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?
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?
--
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]