You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "mumrah (via GitHub)" <gi...@apache.org> on 2024/09/10 01:38:21 UTC
[PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
mumrah opened a new pull request, #17147:
URL: https://github.com/apache/kafka/pull/17147
There is a race condition between KRaftMigrationDriver running its first poll() and being notified by Raft about a leader change. If `onControllerChange` is called before RecoverMigrationStateFromZKEvent is run, we will end up getting stuck in the INACTIVE state.
This patch fixes the race by enqueuing a RecoverMigrationStateFromZKEvent from onControllerChange if the drive has not yet initialized. If another RecoverMigrationStateFromZKEvent was already enqueued, the second one to run will just be ignored.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on code in PR #17147:
URL: https://github.com/apache/kafka/pull/17147#discussion_r1751851734
##########
metadata/src/main/java/org/apache/kafka/metadata/migration/KRaftMigrationDriver.java:
##########
@@ -519,7 +522,7 @@ public void run() throws Exception {
KRaftMigrationDriver.this.image = image;
String metadataType = isSnapshot ? "snapshot" : "delta";
- if (migrationState.equals(MigrationDriverState.INACTIVE)) {
+ if (EnumSet.of(MigrationDriverState.UNINITIALIZED, MigrationDriverState.INACTIVE).contains(migrationState)) {
Review Comment:
Yea, this race is harmless since `onMetadataChange` does not alter the state machine state and most of its logic is guarded behind `if (!migrationState.allowDualWrite()) {`. But it's good to have this for completeness.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #17147:
URL: https://github.com/apache/kafka/pull/17147#discussion_r1753049138
##########
metadata/src/test/java/org/apache/kafka/metadata/migration/KRaftMigrationDriverTest.java:
##########
@@ -232,6 +232,32 @@ CompletableFuture<Void> enqueueMetadataChangeEventWithFuture(
return future;
}
+ @Test
+ public void testOnControllerChangeWhenUninitialized() throws InterruptedException {
+ CountingMetadataPropagator metadataPropagator = new CountingMetadataPropagator();
+ CapturingMigrationClient.newBuilder().build();
+ CapturingMigrationClient migrationClient = CapturingMigrationClient.newBuilder().build();
+ MockFaultHandler faultHandler = new MockFaultHandler("testBecomeLeaderUninitialized");
+ KRaftMigrationDriver.Builder builder = defaultTestBuilder()
+ .setZkMigrationClient(migrationClient)
+ .setPropagator(metadataPropagator)
+ .setFaultHandler(faultHandler);
+ try (KRaftMigrationDriver driver = builder.build()) {
+ // Fake a complete migration with ZK client
+ migrationClient.setMigrationRecoveryState(
+ ZkMigrationLeadershipState.EMPTY.withKRaftMetadataOffsetAndEpoch(100, 1));
+
+ // enqueue a poll event. once run, this will enqueue a recovery event
+ driver.start();
Review Comment:
nit: I think we can invoke `driver.start` after `driver.onControllerChange`, so that we can reliably make sure the test goes with what we expected, that is: UNINITIALIZED state -> `onControllerChange` -> `RecoverMigrationStateFromZKEvent`.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #17147:
URL: https://github.com/apache/kafka/pull/17147#discussion_r1751666612
##########
metadata/src/main/java/org/apache/kafka/metadata/migration/KRaftMigrationDriver.java:
##########
@@ -519,7 +522,7 @@ public void run() throws Exception {
KRaftMigrationDriver.this.image = image;
String metadataType = isSnapshot ? "snapshot" : "delta";
- if (migrationState.equals(MigrationDriverState.INACTIVE)) {
+ if (EnumSet.of(MigrationDriverState.UNINITIALIZED, MigrationDriverState.INACTIVE).contains(migrationState)) {
// No need to log anything if this node is not the active controller
Review Comment:
Please update this comment.
##########
metadata/src/main/java/org/apache/kafka/metadata/migration/KRaftMigrationDriver.java:
##########
@@ -519,7 +522,7 @@ public void run() throws Exception {
KRaftMigrationDriver.this.image = image;
String metadataType = isSnapshot ? "snapshot" : "delta";
- if (migrationState.equals(MigrationDriverState.INACTIVE)) {
+ if (EnumSet.of(MigrationDriverState.UNINITIALIZED, MigrationDriverState.INACTIVE).contains(migrationState)) {
Review Comment:
Ah, nice! Not only `onControllerChange`, but all `OnMetadataChange` will cause race condition.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on PR #17147:
URL: https://github.com/apache/kafka/pull/17147#issuecomment-2340759007
Thanks for taking a look @showuon! I've added a unit test for the fixed race condition. Without the fix, this test reliably fails for me locally.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on PR #17147:
URL: https://github.com/apache/kafka/pull/17147#issuecomment-2339441796
This was found by investigating flaky ZkMigrationFailoverTest tests. Here is an example of a flaky failure where the driver never finished initializing https://ge.apache.org/s/6ymwuwglpcrza/tests/task/:core:test/details/kafka.zk.ZkMigrationFailoverTest/testDriverSkipsEventsFromOlderEpoch()%5B8%5D?top-execution=1
Here is a 40x run of the test with the changes in this PR https://github.com/apache/kafka/actions/runs/10783928121/job/29906701114
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on code in PR #17147:
URL: https://github.com/apache/kafka/pull/17147#discussion_r1754786855
##########
metadata/src/test/java/org/apache/kafka/metadata/migration/KRaftMigrationDriverTest.java:
##########
@@ -232,6 +232,32 @@ CompletableFuture<Void> enqueueMetadataChangeEventWithFuture(
return future;
}
+ @Test
+ public void testOnControllerChangeWhenUninitialized() throws InterruptedException {
+ CountingMetadataPropagator metadataPropagator = new CountingMetadataPropagator();
+ CapturingMigrationClient.newBuilder().build();
+ CapturingMigrationClient migrationClient = CapturingMigrationClient.newBuilder().build();
+ MockFaultHandler faultHandler = new MockFaultHandler("testBecomeLeaderUninitialized");
+ KRaftMigrationDriver.Builder builder = defaultTestBuilder()
+ .setZkMigrationClient(migrationClient)
+ .setPropagator(metadataPropagator)
+ .setFaultHandler(faultHandler);
+ try (KRaftMigrationDriver driver = builder.build()) {
+ // Fake a complete migration with ZK client
+ migrationClient.setMigrationRecoveryState(
+ ZkMigrationLeadershipState.EMPTY.withKRaftMetadataOffsetAndEpoch(100, 1));
+
+ // enqueue a poll event. once run, this will enqueue a recovery event
+ driver.start();
Review Comment:
Good point. Let's consider the possibilities:
Case 1: If the PollEvent runs quickly, we'll see
* start(), enqueue PollEvent
* run PollEvent, enqueue RecoverMigrationStateFromZKEvent (from PollEvent)
* onControllerChange(), enqueue RecoverMigrationStateFromZKEvent and KRaftLeaderEvent
* run RecoverMigrationStateFromZKEvent, state = INACTIVE
* run RecoverMigrationStateFromZKEvent, no-op
* run KRaftLeaderEvent, state is INACTIVE ✅
Case 2: If the RecoverMigrationStateFromZKEvent from PollEvent runs before onControllerChange:
* start(), enqueue PollEvent
* run PollEvent, enqueue RecoverMigrationStateFromZKEvent (from PollEvent)
* run RecoverMigrationStateFromZKEvent, state = INACTIVE
* onControllerChange(), enqueue KRaftLeaderEvent
* run KRaftLeaderEvent, state is INACTIVE ✅
Only Case 1 tests the race condition and the fix.
If we reverse the operations, we'll have
Case 3:
* onControllerChange(), enqueue RecoverMigrationStateFromZKEvent and KRaftLeaderEvent
* start(), enqueue PollEvent
* run RecoverMigrationStateFromZKEvent, state = INACTIVE
* run KRaftLeaderEvent, state is INACTIVE ✅
* run PollEvent, no-op
So yea, it makes sense to use Case 3 for the test since it contrives the race condition.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on code in PR #17147:
URL: https://github.com/apache/kafka/pull/17147#discussion_r1754786855
##########
metadata/src/test/java/org/apache/kafka/metadata/migration/KRaftMigrationDriverTest.java:
##########
@@ -232,6 +232,32 @@ CompletableFuture<Void> enqueueMetadataChangeEventWithFuture(
return future;
}
+ @Test
+ public void testOnControllerChangeWhenUninitialized() throws InterruptedException {
+ CountingMetadataPropagator metadataPropagator = new CountingMetadataPropagator();
+ CapturingMigrationClient.newBuilder().build();
+ CapturingMigrationClient migrationClient = CapturingMigrationClient.newBuilder().build();
+ MockFaultHandler faultHandler = new MockFaultHandler("testBecomeLeaderUninitialized");
+ KRaftMigrationDriver.Builder builder = defaultTestBuilder()
+ .setZkMigrationClient(migrationClient)
+ .setPropagator(metadataPropagator)
+ .setFaultHandler(faultHandler);
+ try (KRaftMigrationDriver driver = builder.build()) {
+ // Fake a complete migration with ZK client
+ migrationClient.setMigrationRecoveryState(
+ ZkMigrationLeadershipState.EMPTY.withKRaftMetadataOffsetAndEpoch(100, 1));
+
+ // enqueue a poll event. once run, this will enqueue a recovery event
+ driver.start();
Review Comment:
Good point. Let's consider the possibilities:
Case 1: If the PollEvent runs quickly, we'll see
* start(), enqueue PollEvent
* run PollEvent, enqueue RecoverMigrationStateFromZKEvent (from PollEvent)
* onControllerChange(), enqueue RecoverMigrationStateFromZKEvent and KRaftLeaderEvent
* run RecoverMigrationStateFromZKEvent, state = INACTIVE
* run RecoverMigrationStateFromZKEvent, no-op
* run KRaftLeaderEvent, state is INACTIVE ✅
Case 2: If the RecoverMigrationStateFromZKEvent from PollEvent runs before onControllerChange:
* start(), enqueue PollEvent
* run PollEvent, enqueue RecoverMigrationStateFromZKEvent (from PollEvent)
* run RecoverMigrationStateFromZKEvent, state = INACTIVE
* onControllerChange(), enqueue KRaftLeaderEvent
* run KRaftLeaderEvent, state is INACTIVE ✅
Only Case 1 tests the race condition and the fix.
If we reverse the operations, we'll have
Case 3:
* onControllerChange(), enqueue RecoverMigrationStateFromZKEvent and KRaftLeaderEvent
* start(), enqueue PollEvent
* run RecoverMigrationStateFromZKEvent, state = INACTIVE
* run KRaftLeaderEvent, state is INACTIVE ✅
* run PollEvent, no-op
So yea, it makes sense to use Case 3 for the test since it always contrives the race condition.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17506 KRaftMigrationDriver initialization race [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah merged PR #17147:
URL: https://github.com/apache/kafka/pull/17147
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org