-
Notifications
You must be signed in to change notification settings - Fork 14.8k
KAFKA-19831: Improved error handling in DefaultStateUpdater. #20767
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
base: trunk
Are you sure you want to change the base?
Conversation
Nikita-Shupletsov
commented
Oct 24, 2025
- Improved error handling in DefaultStateUpdater to take potential failures in Task#maybeCheckpoint into account.
- Improved TaskManager#shutdownStateUpdater to not hang indefinitely if the State Updater thread is dead.
| return new TaskAndAction(null, taskId, Action.REMOVE, future); | ||
| } | ||
|
|
||
| public static TaskAndAction createRemoveTask(final TaskId taskId) { |
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.
the non future remove task isn't used anywhere anymore, so I deleted the code that handled it.
also it made the proposed solution a bit tidier
streams/src/main/java/org/apache/kafka/streams/processor/internals/DefaultStateUpdater.java
Show resolved
Hide resolved
| measureCheckpointLatency(() -> task.maybeCheckpoint(true)); | ||
| // we need to enforce a checkpoint that removes the corrupted partitions | ||
| measureCheckpointLatency(() -> task.maybeCheckpoint(true)); | ||
| } catch (final StreamsException e) { |
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.
In general, we treat StreamsException as fatal, and should never swallow it. What is the reason for this change?
Also: maybe rename e -> swallow to make clear that we swallow intentionally, and not by mistake.
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.
because it's for a corrupted task. corrupted tasks already have an exception, so to not overwrite it, I just log it here.
renaming e to swallow is a good idea, will do
streams/src/main/java/org/apache/kafka/streams/processor/internals/DefaultStateUpdater.java
Show resolved
Hide resolved
streams/src/main/java/org/apache/kafka/streams/processor/internals/DefaultStateUpdater.java
Show resolved
Hide resolved
streams/src/main/java/org/apache/kafka/streams/processor/internals/DefaultStateUpdater.java
Show resolved
Hide resolved
streams/src/main/java/org/apache/kafka/streams/processor/internals/TaskManager.java
Show resolved
Hide resolved
...s/src/test/java/org/apache/kafka/streams/integration/StateUpdaterFailureIntegrationTest.java
Outdated
Show resolved
Hide resolved
|
|
||
| @Override | ||
| public void flush() { | ||
| if (numberOfStoreInits.get() == NUM_PARTITIONS * 1.5) { |
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.
This could be brittle? Should we just hardcode 9? If NUM_PARTITIONS is changed it's not guaranteed we end up with "integer". Also, the == would be evaluated on double type right now -- don't think we would want this.
|
|
||
| streams.removeStreamThread(); | ||
|
|
||
| TestUtils.waitForCondition(() -> numberOfStoreInits.get() == NUM_PARTITIONS * 1.5, "Streams never reinitialized the store enough times"); |
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.
Similar comment.
| public void flush() { | ||
| if (numberOfStoreInits.get() == NUM_PARTITIONS * 1.5) { | ||
| try { | ||
| TestUtils.waitForCondition(() -> currentState.get() == KafkaStreams.State.PENDING_SHUTDOWN, "Streams never reached PENDING_SHUTDOWN state"); |
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.
Not sure if I understand this condition. Can you elaborate?
Also: if we fail here, how do we ensure that the test fails? This would be executed on the background StreamsThread not the actual "main" thread running the test.
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.
to reproduce the issue, we need to fail during a shutdown.
so what the test does:
- it creates a KS app with 2 threads.
- then kills one thread which causes a rebalance
- then the rebalance hangs here, because we can't flush a task.
- we call streams close
- this call gets unblocked and throws an exception
- that's where we reproduce the issue
in other words: the problem I am trying to fix here reproduces if: we are in rebalance, we have some unhandled tasks in the task updater which can throw an exception, which can kill the thread, and we are trying to close the app.
in this case the stream thread will not do its thing, which in turn will not call TaskManager, which will not call stateUpdater.drainExceptionsAndFailedTasks, which will not remove all failed tasks from the lists. which in turn will cause stateUpdater.tasks to return the failed tasks. and TaskManager will try to delete them.
but because the thread is dead, the delete will never succeed, as the dead thread will never complete the future
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 will add a comment to the test to explain what exactly we are trying to achieve here
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 understand that we do want to throw here to trigger an error on the state-updater thread. But it's not clear to me, why we would need to block/wait here until we go to "pending shutdown"?
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.
what needs to happen to reproduce the issue:
the stream thread is already down(so, we want this condition: https://github.com/apache/kafka/blob/trunk/streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java#L933 to be false), we also want to have a task in the state updater. and this task needs to throw an exception.
maybe there is an easier way to achieve these conditions, but that's what I was able to come up with
...s/src/test/java/org/apache/kafka/streams/integration/StateUpdaterFailureIntegrationTest.java
Show resolved
Hide resolved
| } | ||
|
|
||
| @Test | ||
| public void correctlyHandleFlushErrorsDuringRebalance() throws InterruptedException { |
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.
This test is somewhat complex. Might be good to add a longer comment to explain what it does.
If I read it correctly, we start up two thread, and each thread get 3 tasks each (and we init 6 stores). We stop on thread, re-trigger the init of 3 stores, and let it crash when we hit "store init no.9".
| final StateUpdater.RemovedTaskResult removedTaskResult; | ||
| try { | ||
| removedTaskResult = future.get(); | ||
| removedTaskResult = future.get(1, TimeUnit.MINUTES); |
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 I understand correctly, that this is the core fix, to avoid blocking forever?
(Plus the additional cleanup code add to shutdownStateUpdater in case we really hit the timeout here?)
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.
the core fix is the error handling. my idea is to never let the state updated thread die if we get a StreamsException. if we don't die and handle them correctly, we will not need to worry about hanging here.
I added a timeout here just an extra layer of protection. that's why I added BUG_ERROR_MESSAGE to the log message, because I don't think we should expect it ever happen under normal circumstances
Co-authored-by: Matthias J. Sax <[email protected]>
Conflicts: streams/src/main/java/org/apache/kafka/streams/processor/internals/DefaultStateUpdater.java
…ng tested. Small refactoring.