sunm2n commented on code in PR #24240:
URL: https://github.com/apache/kafka/pull/24240#discussion_r4594230564
##########
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:
It wasn't hard, so I added it here. `closeStateConsumer()` now clears the
status before closing, which covers all three catch blocks in `initialize()`,
including the first close in the inner `InvalidOffsetException` catch. There is
a new test for it
(`shouldClearInterruptStatusBeforeClosingGlobalConsumerWhenInitializationFails`),
and I updated the description.
##########
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:
I rewrote it with the concrete effects and split it between the handler and
shutdown paths: the comment in the catch block names `addStreamThread()` on the
`SHUTDOWN_APPLICATION` path, and the one in `finally` says that closing the
consumer with the status set can leave fetch sessions open on the brokers. The
part about the restore is gone together with the restore.
##########
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:
I followed the pattern from #23549 here, but you're right that there's no
need to restore the flag at the end of `run()`, so I removed the restore, and
the shutdown block is no longer re-indented. The same seems true for
`StreamThread.run()`; happy to drop it there as well if you prefer.
##########
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:
It turned out to be easy with a poll task that sets the status and then
throws the `InvalidOffsetException`, so I added that test. It checks the status
inside the handler, and it fails if I remove the `Thread.interrupted()` from
that catch, so I kept the clear. I also added a test where the handler sets the
status and then throws.
##########
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:
Agreed. With the restore gone there is no status left to assert after
`run()`, so the `finally` now only clears the status, since JUnit may reuse the
thread, and the assertions come after the try.
##########
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();
Review Comment:
No longer applies: the variable is gone together with the restore.
##########
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());
+ }
+ }
+
+ @Test
+ public void
shouldClearInterruptStatusDuringShutdownAndRestoreItAfterwards() {
+ final AtomicBoolean interruptedOnClose = new AtomicBoolean(true);
+ final MockConsumer<byte[], byte[]> consumer =
consumerRecordingInterruptStatusOnClose(interruptedOnClose);
+ interruptOnNextPoll(consumer);
+ globalStreamThread = globalStreamThread(consumer);
Review Comment:
Renamed to `newGlobalStreamThread`.
--
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]