lucasbru commented on code in PR #24240:
URL: https://github.com/apache/kafka/pull/24240#discussion_r4598172652


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStreamThread.java:
##########
@@ -474,6 +483,9 @@ private StateConsumer initialize() {
     }
 
     private void closeStateConsumer(final StateConsumer stateConsumer, final 
boolean wipeStateStore) {
+        // Clear the interrupt status before closing the consumer, since an 
interrupt
+        // can leave fetch sessions open on the brokers.
+        Thread.interrupted();

Review Comment:
   Would it be simpler to clear the status once at the top of 
`StateConsumer.close()`? Both the `run()` finally and this method go through 
it, so that would replace this clear and the one in the finally, and only the 
two clears before the handler would stay. It would also avoid clearing when 
`stateConsumer` is null, where nothing gets closed.



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStreamThread.java:
##########
@@ -365,9 +368,15 @@ public void run() {
             );
             this.streamsUncaughtExceptionHandler.accept(e);
         } catch (final Exception e) {
+            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 {
+            // Clear the interrupt status before closing the consumer, since 
an interrupt

Review Comment:
   I'm not sure the "fetch sessions left open on the brokers" part holds. With 
the status set, `ClassicKafkaConsumer.close()` still sends the session-close 
requests in its one network poll before `maybeThrowInterruptException` throws. 
It just doesn't wait for the responses, and `close()` ends with an 
`InterruptException`, which `StateConsumer.close` logs as "Failed to close 
global consumer". I also ran this against a real broker and the fetch sessions 
were released with and without the change. Could we word it as what actually 
happens, e.g. that close() gives up after one poll and throws 
`InterruptException`? The same comment is also in `closeStateConsumer()`, one 
copy would be enough, and the clear in `catch (Exception)` has no comment.



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStreamThreadTest.java:
##########
@@ -325,6 +328,132 @@ 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();
+        } finally {
+            // clear the interrupt status since this thread may be reused by 
JUnit
+            Thread.interrupted();

Review Comment:
   Nit: the same `try { run() } finally { Thread.interrupted(); }` with the 
same comment is repeated in six tests. An `@AfterEach` that clears the status 
would replace all of them.



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStreamThreadTest.java:
##########
@@ -325,6 +328,132 @@ 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();
+        } finally {
+            // clear the interrupt status since this thread may be reused by 
JUnit
+            Thread.interrupted();
+        }
+
+        assertFalse(interruptedInHandler.get());
+    }
+
+    @Test
+    public void 
shouldClearInterruptStatusBeforeInvokingUncaughtExceptionHandlerOnInvalidOffsetException()
 {
+        final AtomicBoolean interruptedInHandler = new AtomicBoolean(true);
+        initializeConsumer();
+        mockConsumer.schedulePollTask(() -> {
+            Thread.currentThread().interrupt();
+            throw new InvalidOffsetException("Try Again!") {

Review Comment:
   Nit: this is the third copy of the anonymous `InvalidOffsetException("Try 
Again!") { partitions() ... }` in this class (also at the two 
`setPollException` calls). Maybe a small `invalidOffsetException()` helper?



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

Reply via email to