chia7712 commented on code in PR #17083:
URL: https://github.com/apache/kafka/pull/17083#discussion_r4694336130
##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerMetadataTest.java:
##########
@@ -184,4 +198,185 @@ private ConsumerMetadata newConsumerMetadata(boolean
includeInternalTopics) {
subscription, new LogContext(), new
ClusterResourceListeners());
}
+ @Test
+ public void testInvalidPartitionLeadershipUpdates() {
+ Metadata metadata = initializeMetadata();
+ List<Node> originalNodes = initializeNodes(metadata);
+ ClusterResourceListener mockListener =
initializeMockListener(metadata);
+
+ // Ensure invalid updates get ignored
+ Map<TopicPartition, Metadata.LeaderIdAndEpoch> invalidUpdates = new
HashMap<>();
+ // incomplete information
+ invalidUpdates.put(new TopicPartition("topic1", 999), new
Metadata.LeaderIdAndEpoch(Optional.empty(), Optional.empty()));
+ // non-existing leader ID
+ invalidUpdates.put(new TopicPartition("topic2", 0), new
Metadata.LeaderIdAndEpoch(Optional.of(99999), Optional.of(99999)));
+ // non-existing topicPartition
+ invalidUpdates.put(new
TopicPartition("topic_missing_from_existing_metadata", 1), new
Metadata.LeaderIdAndEpoch(Optional.of(0), Optional.of(99999)));
+ // stale epoch
+ invalidUpdates.put(new TopicPartition("topic1", 0), new
Metadata.LeaderIdAndEpoch(Optional.of(1), Optional.of(99)));
+
+ Set<TopicPartition> updatedTps =
metadata.updatePartitionLeadership(invalidUpdates, originalNodes);
+ assertTrue(updatedTps.isEmpty(), "Invalid updates should be ignored");
+
+ Cluster updatedCluster = metadata.fetch();
+ assertEquals(new HashSet<>(originalNodes), new
HashSet<>(updatedCluster.nodes()));
+ verify(mockListener, never()).onUpdate(any());
+ validateForUpdatePartitionLeadership(metadata,
+ metadataSupplier(Errors.NONE, new TopicPartition("topic1", 0),
Optional.of(1), Optional.of(100), Arrays.asList(1, 2), Arrays.asList(1, 2),
Collections.singletonList(3)),
+ metadataSupplier(Errors.NONE, new TopicPartition("topic2", 0),
Optional.of(2), Optional.of(200), Arrays.asList(2, 3), Arrays.asList(2, 3),
Collections.singletonList(1)),
+ metadataSupplier(Errors.NONE, new TopicPartition("topic1", 1),
Optional.of(2), Optional.of(200), Arrays.asList(2, 3), Arrays.asList(2, 3),
Collections.singletonList(1)),
+ metadataSupplier(Errors.NONE, new
TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, 0), Optional.of(2),
Optional.of(300), Arrays.asList(2, 3), Arrays.asList(2, 3),
Collections.singletonList(1)),
+ originalNodes,
+ "kafka-cluster",
+ Collections.singleton("topic4"),
+ Collections.singleton("topic3"),
+ Collections.singleton(Topic.GROUP_METADATA_TOPIC_NAME),
+ updatedCluster.controller(),
+ metadata.topicIds());
+ }
+
+
+ @Test
+ public void testValidPartitionLeadershipUpdate() {
+ Metadata metadata = initializeMetadata();
+ List<Node> originalNodes = initializeNodes(metadata);
+ ClusterResourceListener mockListener =
initializeMockListener(metadata);
+
+ // Ensure valid update to tp11 is applied
+ Map<TopicPartition, Metadata.LeaderIdAndEpoch> validUpdates = new
HashMap<>();
+ TopicPartition tp11 = new TopicPartition("topic1", 0);
+
+ Integer newLeaderId = 2; // New leader ID
+ Integer newLeaderEpoch = 101; // New leader epoch that is newer than
existing
+
+ validUpdates.put(tp11, new
Metadata.LeaderIdAndEpoch(Optional.of(newLeaderId),
Optional.of(newLeaderEpoch)));
+
+ Set<TopicPartition> updatedTps =
metadata.updatePartitionLeadership(validUpdates, originalNodes);
+
+ assertEquals(1, updatedTps.size());
+ assertEquals(tp11, updatedTps.iterator().next(), "tp11 should be
updated");
+
+ Cluster updatedCluster = metadata.fetch();
+ assertEquals(new HashSet<>(originalNodes), new
HashSet<>(updatedCluster.nodes()));
+ verify(mockListener, times(1)).onUpdate(any());
+ validateForUpdatePartitionLeadership(metadata,
+ new MetadataResponse.PartitionMetadata(Errors.NONE, tp11,
Optional.of(newLeaderId), Optional.of(newLeaderEpoch), Arrays.asList(1, 2),
Arrays.asList(1, 2), Collections.singletonList(3)),
+ metadataSupplier(Errors.NONE, new TopicPartition("topic2", 0),
Optional.of(2), Optional.of(200), Arrays.asList(2, 3), Arrays.asList(2, 3),
Collections.singletonList(1)),
+ metadataSupplier(Errors.NONE, new TopicPartition("topic1", 1),
Optional.of(2), Optional.of(200), Arrays.asList(2, 3), Arrays.asList(2, 3),
Collections.singletonList(1)),
+ metadataSupplier(Errors.NONE, new
TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, 0), Optional.of(2),
Optional.of(300), Arrays.asList(2, 3), Arrays.asList(2, 3),
Collections.singletonList(1)),
+ originalNodes,
+ "kafka-cluster",
+ Collections.singleton("topic4"),
+ Collections.singleton("topic3"),
+ Collections.singleton(Topic.GROUP_METADATA_TOPIC_NAME),
+ updatedCluster.controller(),
+ metadata.topicIds());
+ }
+
+ private Metadata initializeMetadata() {
+ Metadata metadata = new Metadata(100, 1000, 1000, new LogContext(),
new ClusterResourceListeners());
+
+ String topic1 = "topic1";
+ String topic2 = "topic2";
+ String topic3 = "topic3";
+ String topic4 = "topic4";
+ String clusterId = "kafka-cluster";
+ Uuid topic1Id = Uuid.randomUuid();
+ Uuid topic2Id = Uuid.randomUuid();
+ Uuid internalTopicId = Uuid.randomUuid();
+
+ Map<String, Uuid> topicIds = new HashMap<>();
+ topicIds.put(topic1, topic1Id);
+ topicIds.put(topic2, topic2Id);
+ topicIds.put(Topic.GROUP_METADATA_TOPIC_NAME, internalTopicId);
+
+ Map<String, Errors> errorCounts = new HashMap<>();
+ errorCounts.put(topic3, Errors.INVALID_TOPIC_EXCEPTION);
+ errorCounts.put(topic4, Errors.TOPIC_AUTHORIZATION_FAILED);
+
+ Map<String, Integer> topicPartitionCounts = new HashMap<>();
+ topicPartitionCounts.put(topic1, 2);
+ topicPartitionCounts.put(topic2, 1);
+ topicPartitionCounts.put(Topic.GROUP_METADATA_TOPIC_NAME, 1);
+
+ metadata.requestUpdate(true);
+ Metadata.MetadataRequestAndVersion versionAndBuilder =
metadata.newMetadataRequestAndVersion(time.milliseconds());
+ metadata.update(versionAndBuilder.requestVersion,
+ RequestTestUtils.metadataUpdateWith(clusterId,
+ 5,
+ errorCounts,
+ topicPartitionCounts,
+ tp -> null,
+ this::metadataSupplier,
+ ApiKeys.METADATA.latestVersion(),
+ topicIds),
+ false,
+ time.milliseconds());
+
+ return metadata;
+ }
+
+ private List<Node> initializeNodes(Metadata metadata) {
+ return new ArrayList<>(metadata.fetch().nodes());
+ }
+
+ private ClusterResourceListener initializeMockListener(Metadata metadata) {
+ ClusterResourceListener mockListener =
Mockito.mock(ClusterResourceListener.class);
+ metadata.addClusterUpdateListener(mockListener);
+ return mockListener;
+ }
+
+ private MetadataResponse.PartitionMetadata metadataSupplier(Errors error,
TopicPartition partition, Optional<Integer> leaderId, Optional<Integer>
leaderEpoch, List<Integer> replicas, List<Integer> isr, List<Integer>
offlineReplicas) {
+ if ("topic1".equals(partition.topic()) && partition.partition() == 0)
+ return new MetadataResponse.PartitionMetadata(Errors.NONE,
partition, Optional.of(1), Optional.of(100), Arrays.asList(1, 2),
Arrays.asList(1, 2), Collections.singletonList(3));
+ else if ("topic1".equals(partition.topic()) && partition.partition()
== 1)
+ return new MetadataResponse.PartitionMetadata(Errors.NONE,
partition, Optional.of(2), Optional.of(200), Arrays.asList(2, 3),
Arrays.asList(2, 3), Collections.singletonList(1));
+ else if ("topic2".equals(partition.topic()) && partition.partition()
== 0)
+ return new MetadataResponse.PartitionMetadata(Errors.NONE,
partition, Optional.of(2), Optional.of(200), Arrays.asList(2, 3),
Arrays.asList(2, 3), Collections.singletonList(1));
+ else if (Topic.GROUP_METADATA_TOPIC_NAME.equals(partition.topic()) &&
partition.partition() == 0)
+ return new MetadataResponse.PartitionMetadata(Errors.NONE,
partition, Optional.of(2), Optional.of(300), Arrays.asList(2, 3),
Arrays.asList(2, 3), Collections.singletonList(1));
+ else throw new RuntimeException("Unexpected partition " + partition);
+ }
+
+ private void validateForUpdatePartitionLeadership(Metadata updatedMetadata,
+
MetadataResponse.PartitionMetadata part1Metadata,
+
MetadataResponse.PartitionMetadata part2Metadata,
+
MetadataResponse.PartitionMetadata part12Metadata,
+
MetadataResponse.PartitionMetadata internalPartMetadata,
+ List<Node> expectedNodes,
+ String expectedClusterId,
+ Set<String>
expectedUnauthorisedTopics,
+ Set<String>
expectedInvalidTopics,
+ Set<String>
expectedInternalTopics,
+ Node expectedController,
+ Map<String, Uuid>
expectedTopicIds) {
+ Cluster updatedCluster = updatedMetadata.fetch();
+ assertEquals(updatedCluster.clusterResource().clusterId(),
expectedClusterId);
+ assertEquals(new HashSet<>(expectedNodes), new
HashSet<>(updatedCluster.nodes()));
+ assertEquals(3, updatedCluster.topics().size());
+ assertEquals(expectedInternalTopics, updatedCluster.internalTopics());
+ assertEquals(expectedInvalidTopics, updatedCluster.invalidTopics());
+ assertEquals(expectedUnauthorisedTopics,
updatedCluster.unauthorizedTopics());
+ assertEquals(expectedController, updatedCluster.controller());
+ assertEquals(expectedTopicIds, updatedMetadata.topicIds());
+
+ Map<Integer, Node> nodeMap =
expectedNodes.stream().collect(Collectors.toMap(Node::id, Function.identity()));
+ for (MetadataResponse.PartitionMetadata partitionMetadata :
Arrays.asList(part1Metadata, part2Metadata, part12Metadata,
internalPartMetadata)) {
+ TopicPartition tp = new TopicPartition(partitionMetadata.topic(),
partitionMetadata.partition());
+
+ Metadata.LeaderAndEpoch expectedLeaderInfo = new
Metadata.LeaderAndEpoch(Optional.of(nodeMap.get(partitionMetadata.leaderId.get())),
partitionMetadata.leaderEpoch);
+ assertEquals(expectedLeaderInfo,
updatedMetadata.currentLeader(tp));
+
+ Optional<MetadataResponse.PartitionMetadata>
optionalUpdatedMetadata = updatedMetadata.partitionMetadataIfCurrent(tp);
+ assertTrue(optionalUpdatedMetadata.isPresent());
+ MetadataResponse.PartitionMetadata updatedPartMetadata =
optionalUpdatedMetadata.get();
+ assertEquals(partitionMetadata.topicPartition,
updatedPartMetadata.topicPartition);
+ assertEquals(partitionMetadata.error, updatedPartMetadata.error);
+ assertEquals(partitionMetadata.leaderId,
updatedPartMetadata.leaderId);
+ assertEquals(partitionMetadata.leaderEpoch,
updatedPartMetadata.leaderEpoch);
+ assertEquals(partitionMetadata.replicaIds,
updatedPartMetadata.replicaIds);
+ assertEquals(partitionMetadata.inSyncReplicaIds,
updatedPartMetadata.inSyncReplicaIds);
+ assertEquals(partitionMetadata.offlineReplicaIds,
partitionMetadata.offlineReplicaIds);
Review Comment:
The second argument should be updatedPartMetadata rather than
partitionMetadata :)
--
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]