-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-9212; Ensure LeaderAndIsr state updated in controller context during reassignment #7795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,14 @@ | |
| import org.apache.kafka.common.errors.TopicAuthorizationException; | ||
| import org.apache.kafka.common.internals.ClusterResourceListeners; | ||
| import org.apache.kafka.common.internals.Topic; | ||
| import org.apache.kafka.common.message.MetadataResponseData; | ||
| import org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBrokerCollection; | ||
| import org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition; | ||
| import org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic; | ||
| import org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection; | ||
| import org.apache.kafka.common.protocol.ApiKeys; | ||
| import org.apache.kafka.common.protocol.Errors; | ||
| import org.apache.kafka.common.protocol.types.Struct; | ||
| import org.apache.kafka.common.requests.MetadataResponse; | ||
| import org.apache.kafka.common.utils.LogContext; | ||
| import org.apache.kafka.common.utils.MockTime; | ||
|
|
@@ -33,9 +40,13 @@ | |
| import org.junit.Test; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.kafka.test.TestUtils.assertOptional; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
@@ -147,6 +158,112 @@ public void testTimeToNextUpdate_RetryBackoff() { | |
| assertEquals(0, metadata.timeToNextUpdate(now + 1)); | ||
| } | ||
|
|
||
| /** | ||
| * Prior to Kafka version 2.4 (which coincides with Metadata version 9), the broker does not propagate leader epoch | ||
| * information accurately while a reassignment is in progress, so we cannot rely on it. This is explained in more | ||
| * detail in MetadataResponse's constructor. | ||
| */ | ||
| @Test | ||
| public void testIgnoreLeaderEpochInOlderMetadataResponse() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Could we also briefly explain the issue in the tests? Personally, I tend to read tests to understand the expected behavior and the issue with versions earlier than 9 is not immediately apparent
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it OK now? |
||
| TopicPartition tp = new TopicPartition("topic", 0); | ||
|
|
||
| MetadataResponsePartition partitionMetadata = new MetadataResponsePartition() | ||
| .setPartitionIndex(tp.partition()) | ||
| .setLeaderId(5) | ||
| .setLeaderEpoch(10) | ||
| .setReplicaNodes(Arrays.asList(1, 2, 3)) | ||
| .setIsrNodes(Arrays.asList(1, 2, 3)) | ||
| .setOfflineReplicas(Collections.emptyList()) | ||
| .setErrorCode(Errors.NONE.code()); | ||
|
|
||
| MetadataResponseTopic topicMetadata = new MetadataResponseTopic() | ||
| .setName(tp.topic()) | ||
| .setErrorCode(Errors.NONE.code()) | ||
| .setPartitions(Collections.singletonList(partitionMetadata)) | ||
| .setIsInternal(false); | ||
|
|
||
| MetadataResponseTopicCollection topics = new MetadataResponseTopicCollection(); | ||
| topics.add(topicMetadata); | ||
|
|
||
| MetadataResponseData data = new MetadataResponseData() | ||
| .setClusterId("clusterId") | ||
| .setControllerId(0) | ||
| .setTopics(topics) | ||
| .setBrokers(new MetadataResponseBrokerCollection()); | ||
|
|
||
| for (short version = ApiKeys.METADATA.oldestVersion(); version < 9; version++) { | ||
| Struct struct = data.toStruct(version); | ||
| MetadataResponse response = new MetadataResponse(struct, version); | ||
| assertFalse(response.hasReliableLeaderEpochs()); | ||
| metadata.update(response, 100); | ||
| assertTrue(metadata.partitionInfoIfCurrent(tp).isPresent()); | ||
| MetadataCache.PartitionInfoAndEpoch info = metadata.partitionInfoIfCurrent(tp).get(); | ||
| assertEquals(-1, info.epoch()); | ||
| } | ||
|
|
||
| for (short version = 9; version <= ApiKeys.METADATA.latestVersion(); version++) { | ||
| Struct struct = data.toStruct(version); | ||
| MetadataResponse response = new MetadataResponse(struct, version); | ||
| assertTrue(response.hasReliableLeaderEpochs()); | ||
| metadata.update(response, 100); | ||
| assertTrue(metadata.partitionInfoIfCurrent(tp).isPresent()); | ||
| MetadataCache.PartitionInfoAndEpoch info = metadata.partitionInfoIfCurrent(tp).get(); | ||
| assertEquals(10, info.epoch()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testStaleMetadata() { | ||
| TopicPartition tp = new TopicPartition("topic", 0); | ||
|
|
||
| MetadataResponsePartition partitionMetadata = new MetadataResponsePartition() | ||
| .setPartitionIndex(tp.partition()) | ||
| .setLeaderId(1) | ||
| .setLeaderEpoch(10) | ||
| .setReplicaNodes(Arrays.asList(1, 2, 3)) | ||
| .setIsrNodes(Arrays.asList(1, 2, 3)) | ||
| .setOfflineReplicas(Collections.emptyList()) | ||
| .setErrorCode(Errors.NONE.code()); | ||
|
|
||
| MetadataResponseTopic topicMetadata = new MetadataResponseTopic() | ||
| .setName(tp.topic()) | ||
| .setErrorCode(Errors.NONE.code()) | ||
| .setPartitions(Collections.singletonList(partitionMetadata)) | ||
| .setIsInternal(false); | ||
|
|
||
| MetadataResponseTopicCollection topics = new MetadataResponseTopicCollection(); | ||
| topics.add(topicMetadata); | ||
|
|
||
| MetadataResponseData data = new MetadataResponseData() | ||
| .setClusterId("clusterId") | ||
| .setControllerId(0) | ||
| .setTopics(topics) | ||
| .setBrokers(new MetadataResponseBrokerCollection()); | ||
|
|
||
| metadata.update(new MetadataResponse(data), 100); | ||
|
|
||
| // Older epoch with changed ISR should be ignored | ||
| partitionMetadata | ||
| .setPartitionIndex(tp.partition()) | ||
| .setLeaderId(1) | ||
| .setLeaderEpoch(9) | ||
| .setReplicaNodes(Arrays.asList(1, 2, 3)) | ||
| .setIsrNodes(Arrays.asList(1, 2)) | ||
| .setOfflineReplicas(Collections.emptyList()) | ||
| .setErrorCode(Errors.NONE.code()); | ||
|
|
||
| metadata.update(new MetadataResponse(data), 101); | ||
| assertEquals(Optional.of(10), metadata.lastSeenLeaderEpoch(tp)); | ||
|
|
||
| assertTrue(metadata.partitionInfoIfCurrent(tp).isPresent()); | ||
| MetadataCache.PartitionInfoAndEpoch info = metadata.partitionInfoIfCurrent(tp).get(); | ||
|
|
||
| List<Integer> cachedIsr = Arrays.stream(info.partitionInfo().inSyncReplicas()) | ||
| .map(Node::id).collect(Collectors.toList()); | ||
| assertEquals(Arrays.asList(1, 2, 3), cachedIsr); | ||
| assertEquals(10, info.epoch()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailedUpdate() { | ||
| long time = 100; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -750,6 +750,41 @@ class ReassignPartitionsClusterTest extends ZooKeeperTestHarness with Logging { | |
| assertEquals(Seq(101), zkClient.getReplicasForPartition(tp0)) | ||
| } | ||
|
|
||
| @Test | ||
| def testProduceAndConsumeWithReassignmentInProgress(): Unit = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice test! |
||
| startBrokers(Seq(100, 101)) | ||
| adminClient = createAdminClient(servers) | ||
| createTopic(zkClient, topicName, Map(tp0.partition() -> Seq(100)), servers = servers) | ||
|
|
||
| produceMessages(tp0.topic, 500, acks = -1, valueLength = 100 * 1000) | ||
|
|
||
| TestUtils.throttleAllBrokersReplication(adminClient, Seq(101), throttleBytes = 1) | ||
| TestUtils.assignThrottledPartitionReplicas(adminClient, Map(tp0 -> Seq(101))) | ||
|
|
||
| adminClient.alterPartitionReassignments( | ||
| Map(reassignmentEntry(tp0, Seq(100, 101))).asJava | ||
| ).all().get() | ||
|
|
||
| awaitReassignmentInProgress(tp0) | ||
|
|
||
| produceMessages(tp0.topic, 500, acks = -1, valueLength = 64) | ||
| val consumer = TestUtils.createConsumer(TestUtils.getBrokerListStrFromServers(servers)) | ||
| try { | ||
| consumer.assign(Seq(tp0).asJava) | ||
| pollUntilAtLeastNumRecords(consumer, numRecords = 1000) | ||
| } finally { | ||
| consumer.close() | ||
| } | ||
|
|
||
| assertTrue(isAssignmentInProgress(tp0)) | ||
|
|
||
| TestUtils.resetBrokersThrottle(adminClient, Seq(101)) | ||
| TestUtils.removePartitionReplicaThrottles(adminClient, Set(tp0)) | ||
|
|
||
| waitForAllReassignmentsToComplete() | ||
| assertEquals(Seq(100, 101), zkClient.getReplicasForPartition(tp0)) | ||
| } | ||
|
|
||
| @Test | ||
| def shouldListMovingPartitionsThroughApi(): Unit = { | ||
| startBrokers(Seq(100, 101)) | ||
|
|
@@ -1228,6 +1263,16 @@ class ReassignPartitionsClusterTest extends ZooKeeperTestHarness with Logging { | |
| s"Znode ${ReassignPartitionsZNode.path} wasn't deleted", pause = pause) | ||
| } | ||
|
|
||
| def awaitReassignmentInProgress(topicPartition: TopicPartition): Unit = { | ||
| waitUntilTrue(() => isAssignmentInProgress(topicPartition), | ||
| "Timed out waiting for expected reassignment to begin") | ||
| } | ||
|
|
||
| def isAssignmentInProgress(topicPartition: TopicPartition): Boolean = { | ||
| val reassignments = adminClient.listPartitionReassignments().reassignments().get() | ||
| reassignments.asScala.get(topicPartition).isDefined | ||
| } | ||
|
|
||
| def waitForAllReassignmentsToComplete(pause: Long = 100L): Unit = { | ||
| waitUntilTrue(() => adminClient.listPartitionReassignments().reassignments().get().isEmpty, | ||
| s"There still are ongoing reassignments", pause = pause) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note this was a bug. We were using the leader epoch from the response even if it was stale and we had taken the
PartitionInfofrom the previous update.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a test that covers this bug too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
testStaleMetadata()does that