lucasbru commented on code in PR #24240:
URL: https://github.com/apache/kafka/pull/24240#discussion_r4586013566
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStreamThread.java:
##########
@@ -311,6 +311,10 @@ public void run() {
}
boolean wipeStateStore = false;
+ // An interrupt that stops the run loop must not leak into the
exception handler or the shutdown:
+ // the handler may have to create a stream thread on this thread, and
closing the global consumer
+ // would be cut short. The flag is cleared before either runs and
restored once the shutdown completes.
+ boolean interrupted = false;
Review Comment:
`initialize()` has the same problem: if the thread is interrupted there, the
catch blocks call `closeStateConsumer(...)` with the status still set, and
`run()` then returns on the `stateConsumer == null` path. I see the description
calls this out as not covered. Would it be hard to clear it in `initialize()`
too, or do you prefer a follow-up?
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStreamThread.java:
##########
@@ -311,6 +311,10 @@ public void run() {
}
boolean wipeStateStore = false;
+ // An interrupt that stops the run loop must not leak into the
exception handler or the shutdown:
Review Comment:
Could this name the concrete effects? "leak into" and "cut short" are a bit
vague. E.g. that `addStreamThread()` on the SHUTDOWN_APPLICATION path would run
with the status set, and that the consumer close would skip closing its fetch
sessions. It also doesn't say why the status is restored at the end.
##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStreamThreadTest.java:
##########
@@ -325,6 +328,76 @@ public Set<TopicPartition> partitions() {
assertFalse(new File(baseDirectoryName + File.separator + "testAppId"
+ File.separator + "global").exists());
}
+ @Test
+ public void
shouldClearInterruptStatusBeforeInvokingUncaughtExceptionHandler() {
+ final AtomicBoolean interruptedInHandler = new AtomicBoolean(true);
+ initializeConsumer();
+ interruptOnNextPoll(mockConsumer);
+ globalStreamThread.setUncaughtExceptionHandler(
+ e ->
interruptedInHandler.set(Thread.currentThread().isInterrupted())
+ );
+
+ try {
+ globalStreamThread.run();
+
+ assertFalse(interruptedInHandler.get());
+ } finally {
+ assertTrue(Thread.interrupted());
Review Comment:
Nit: if the assertion in the `try` fails and the status wasn't restored
either, this `assertTrue` in the `finally` replaces the original failure. Could
capture `Thread.interrupted()` in the `finally` and assert after the try (same
in the other new tests).
##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStreamThread.java:
##########
@@ -365,28 +370,36 @@ public void run() {
);
this.streamsUncaughtExceptionHandler.accept(e);
} catch (final Exception e) {
+ interrupted = Thread.interrupted();
log.error("Error happened while maintaining global state store.
The streams application or client will now close to ERROR.", e);
this.streamsUncaughtExceptionHandler.accept(e);
} finally {
- // set the state to pending shutdown first as it may be called due
to error;
- // its state may already be PENDING_SHUTDOWN so it will return
false but we
- // intentionally do not check the returned flag
- setState(State.PENDING_SHUTDOWN);
+ interrupted |= Thread.interrupted();
+ try {
+ // set the state to pending shutdown first as it may be called
due to error;
+ // its state may already be PENDING_SHUTDOWN so it will return
false but we
+ // intentionally do not check the returned flag
+ setState(State.PENDING_SHUTDOWN);
- log.info("Shutting down");
+ log.info("Shutting down");
- try {
- stateConsumer.close(wipeStateStore);
- } catch (final IOException e) {
- log.error("Failed to close state maintainer due to the
following error:", e);
- }
+ try {
+ stateConsumer.close(wipeStateStore);
+ } catch (final IOException e) {
+ log.error("Failed to close state maintainer due to the
following error:", e);
+ }
- streamsMetrics.removeAllThreadLevelSensors(getName());
- streamsMetrics.removeAllThreadLevelMetrics(getName());
+ streamsMetrics.removeAllThreadLevelSensors(getName());
+ streamsMetrics.removeAllThreadLevelMetrics(getName());
- setState(DEAD);
+ setState(DEAD);
- log.info("Shutdown complete");
+ log.info("Shutdown complete");
+ } finally {
+ if (interrupted) {
Review Comment:
Is the restore needed? `run()` is the last thing this thread does, and as
far as I can tell nothing reads the status after it returns (`KafkaStreams`
uses `shutdown()` + `join()`). It seems to matter only for tests that call
`run()` directly, and it costs the extra try/finally and re-indenting the whole
shutdown block. If we keep it, a comment saying why would help.
##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStreamThreadTest.java:
##########
@@ -325,6 +328,76 @@ public Set<TopicPartition> partitions() {
assertFalse(new File(baseDirectoryName + File.separator + "testAppId"
+ File.separator + "global").exists());
}
+ @Test
+ public void
shouldClearInterruptStatusBeforeInvokingUncaughtExceptionHandler() {
Review Comment:
We have no test where the interrupt arrives together with an
`InvalidOffsetException` (the other branch that clears the status), or where
the handler throws. Probably hard to trigger the first one with the mock
consumer, so fine to skip, but then is the `Thread.interrupted()` in that catch
needed?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]