Skip to content

Kafka Streams Threading: Exception handling#13957

Merged
cadonna merged 4 commits into
apache:trunkfrom
lucasbru:poll_thread_except
Jul 13, 2023
Merged

Kafka Streams Threading: Exception handling#13957
cadonna merged 4 commits into
apache:trunkfrom
lucasbru:poll_thread_except

Conversation

@lucasbru

@lucasbru lucasbru commented Jul 4, 2023

Copy link
Copy Markdown
Member

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.

@cadonna cadonna left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member Author

Test failures are unrelated

@cadonna cadonna left a comment

Copy link
Copy Markdown
Member

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

cadonna commented Jul 13, 2023

Copy link
Copy Markdown
Member

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
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants