Skip to content
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

Kafka Streams Threading: Exception handling #13957

Merged
merged 4 commits into from Jul 13, 2023

Conversation

lucasbru
Copy link
Member

@lucasbru lucasbru commented Jul 4, 2023

Catch any exceptions that escape the processing logic inside TaskExecutors and record them in the TaskManager. Make sure the TaskExecutor survives, but the task is unassigned. Add a method to TaskManager to drain the exceptions. The aim here is that the polling thread will drain the exceptions to be able to execute the
uncaught exception handler, abort transactions, etc.

The logic is tested by basic unit tests.

Catch any exceptions that escape the processing logic
inside TaskExecutors and record them in the TaskManager.
Make sure the TaskExecutor survives, but the task is
unassigned. Add a method to TaskManager to drain the
exceptions. The aim here is that the polling thread will
drain the exceptions to be able to execute the
uncaught exception handler, abort transactions, etc.
Copy link
Contributor

@cadonna cadonna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @lucasbru !

Here my feedback!

public void shouldNotSetUncaughtExceptionsForUnassignedTasks() {
taskManager.add(Collections.singleton(task));

assertThrows(IllegalArgumentException.class, () -> taskManager.setUncaughtException(exception, task.id()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also verify the error message? Otherwise, we actually do not know which IllegalArgumentException was thrown.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

taskManager.assignNextTask(taskExecutor);
taskManager.setUncaughtException(exception, task.id());

assertThrows(IllegalArgumentException.class, () -> taskManager.setUncaughtException(exception, task.id()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also verify the error message? Otherwise, we actually do not know which IllegalArgumentException was thrown.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 188 to 207
@Test
public void shouldReturnExceptionsOnDrainExceptions() {
taskManager.add(Collections.singleton(task));
when(tasks.activeTasks()).thenReturn(Collections.singleton(task));
taskManager.assignNextTask(taskExecutor);
taskManager.setUncaughtException(exception, task.id());

assertEquals(taskManager.drainUncaughtExceptions(), Collections.singletonMap(task.id(), exception));
}

@Test
public void shouldClearExceptionsOnDrainExceptions() {
taskManager.add(Collections.singleton(task));
when(tasks.activeTasks()).thenReturn(Collections.singleton(task));
taskManager.assignNextTask(taskExecutor);
taskManager.setUncaughtException(exception, task.id());
taskManager.drainUncaughtExceptions();

assertEquals(taskManager.drainUncaughtExceptions(), Collections.emptyMap());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it would be OK to merge these two tests. Just add the assertEquals() of the second test to the end of the first test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

uncaughtExceptions.put(taskId, exception);
});

log.info("Set an uncaught exception for task {}", taskId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log some more information about the exception, like the type and the error message? Just to be able to better reason about the error case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +107 to +108
* Setting an uncaught exception for a task prevents it from being reassigned until the
* corresponding exception has been handled in the polling thread.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That part is not yet implemented, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

@lucasbru
Copy link
Member Author

Test failures are unrelated

Copy link
Contributor

@cadonna cadonna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @lucasbru !

LGTM!

@cadonna
Copy link
Contributor

cadonna commented Jul 13, 2023

Build failures are unrelated:

Build / JDK 17 and Scala 2.13 / integration.kafka.server.FetchFromFollowerIntegrationTest.testFollowerCompleteDelayedFetchesOnReplication(String).quorum=kraft
Build / JDK 17 and Scala 2.13 / org.apache.kafka.controller.QuorumControllerTest.testBalancePartitionLeaders()
Build / JDK 17 and Scala 2.13 / org.apache.kafka.trogdor.coordinator.CoordinatorTest.testTaskRequestWithOldStartMsGetsUpdated()
Build / JDK 20 and Scala 2.13 / org.apache.kafka.connect.mirror.integration.MirrorConnectorsIntegrationExactlyOnceTest.testOffsetTranslationBehindReplicationFlow()
Build / JDK 20 and Scala 2.13 / kafka.api.TransactionsTest.testBumpTransactionalEpoch(String).quorum=kraft
Build / JDK 20 and Scala 2.13 / org.apache.kafka.controller.QuorumControllerTest.testBalancePartitionLeaders()
Build / JDK 11 and Scala 2.13 / org.apache.kafka.controller.QuorumControllerTest.testBalancePartitionLeaders()
Build / JDK 8 and Scala 2.12 / integration.kafka.server.FetchFromFollowerIntegrationTest.testRackAwareRangeAssignor()

@cadonna cadonna merged commit 5f20750 into apache:trunk Jul 13, 2023
1 check failed
Cerchie pushed a commit to Cerchie/kafka that referenced this pull request Jul 25, 2023
Catch any exceptions that escape the processing logic
inside TaskExecutors and record them in the TaskManager.
Make sure the TaskExecutor survives, but the task is
unassigned. Add a method to TaskManager to drain the
exceptions. The aim here is that the polling thread will
drain the exceptions to be able to execute the
uncaught exception handler, abort transactions, etc.

Reviewer: Bruno Cadonna <cadonna@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants