You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "frankvicky (via GitHub)" <gi...@apache.org> on 2025/04/07 07:40:58 UTC

[PR] KAFKA-16758: remove internal.leave.group.on.close config [kafka]

frankvicky opened a new pull request, #19400:
URL: https://github.com/apache/kafka/pull/19400

   This is a follow-up of #17614 
   The patch is to remove the `internal.leave.group.on.close` config.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2173773230


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1205,45 +1200,9 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                         final long cacheSizePerThread = cacheSizePerThread(numLiveStreamThreads());
                         log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                         resizeThreadCache(cacheSizePerThread);
-                        if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
-                            final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
-                            final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
-                            final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = 
-                                adminClient.removeMembersFromConsumerGroup(
-                                    applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG),
-                                    new RemoveMembersFromConsumerGroupOptions(membersToRemove)
-                                );
-                            try {
-                                final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
-                                removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
-                            } catch (final java.util.concurrent.TimeoutException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to a timeout:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new TimeoutException(exception.getMessage(), exception);
-                            } catch (final InterruptedException e) {
-                                Thread.currentThread().interrupt();
-                            } catch (final ExecutionException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new StreamsException(
-                                        "Could not remove static member " + groupInstanceID.get()
-                                            + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                            + " for the following reason: ",
-                                        exception.getCause()
-                                );
-                            }

Review Comment:
   I have updated the notable changes and javadoc to reflect the behavior change.
   ![image](https://github.com/user-attachments/assets/c79cf355-b43c-4e7b-a586-e9e177b639e7)
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2071532836


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1776,22 +1780,28 @@ private long advanceNowAndComputeLatency() {
         return Math.max(now - previous, 0);
     }
 
+    public void shutdown() {

Review Comment:
   as mentioned above, let's just get rid of the overload without a `leaveGroup` parameter, this way the caller has to consciously decide whether it should be leaving the group or not



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3232351992

   @frankvicky: This makes sense, then. So remaining in group means sending the leave epoch in the static case. Then it is just the code comment that is misleading and could probably be updated to state what you said in the PR comment that you referenced.
   
   Will implement these changes also for KIP-1071 then? While static membership isn't fully implemented yet for KIP-1071, we should still pull through those changes.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [WIP] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-2791810133

   Hi @ableegoldman 
   Please take a look when you have a free moment.
   I have some questions since I'm not familiar with the stream module.
   Please bear with me if the code looks like it was written while drunk.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3238432137

   Just talked to @bbejeck about this, and he will prioritize KIP-1153 to make sure we get is merged.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3225700806

   I was just cycling back to the KIP. There is the following example:
   ```
   // Old usage
   consumer.close(Duration.ofSeconds(30));
    
   // New usage
   consumer.close(CloseOptions.timeout(Duration.ofSeconds(30))
       .withGroupMembershipOperation(GroupMembershipOperation.DEFAULT));
    
   // New usage (shorter version)
   consumer.close(CloseOptions.timeout(Duration.ofSeconds(30)));
   ```
   It seems this example is not correct? The behavior of the existing `close()` and `close(Duration)` should not change, and they would never send a leave-group-request (for both dynamic, and static case) from my understanding. Thus, for dynamic member, the new usage would need to pass `REMAIN_IN_GROUP` to get the same behavior?
   
   Thinking about this further, we could include a change of default behavior for `close()` and `close(Duration)` in the KIP, but only apply it in AK 5.0. We cannot apply it in a minor release... Thoughts?


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-2829083222

   looks like  KafkaStreamsCloseOptionsIntegrationTest is failing


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2072336376


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   Back to the topic.
   @ableegoldman 
   I have some thoughts about removing this method: https://github.com/apache/kafka/pull/19400#discussion_r2062833316
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3223821351

   Hey @frankvicky @chia7712 - what is missing from finishing this work?
   
   Also, I see that support for `CloseOptions` was implemented for KIP-848 and share groups, but it is missing from KIP-1071. Do we need a separate ticket to complete the work to also include KIP-1071? Should work analogous.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2053464025


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1205,45 +1200,9 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                         final long cacheSizePerThread = cacheSizePerThread(numLiveStreamThreads());
                         log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                         resizeThreadCache(cacheSizePerThread);
-                        if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
-                            final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
-                            final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
-                            final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = 
-                                adminClient.removeMembersFromConsumerGroup(
-                                    applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG),
-                                    new RemoveMembersFromConsumerGroupOptions(membersToRemove)
-                                );
-                            try {
-                                final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
-                                removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
-                            } catch (final java.util.concurrent.TimeoutException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to a timeout:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new TimeoutException(exception.getMessage(), exception);
-                            } catch (final InterruptedException e) {
-                                Thread.currentThread().interrupt();
-                            } catch (final ExecutionException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new StreamsException(
-                                        "Could not remove static member " + groupInstanceID.get()
-                                            + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                            + " for the following reason: ",
-                                        exception.getCause()
-                                );
-                            }

Review Comment:
   nice! love to see all this code get deleted for one line šŸ˜„ 



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1805,12 +1816,12 @@ private void completeShutdown(final boolean cleanRun) {
             log.error("Failed to unsubscribe due to the following error: ", e);
         }
         try {
-            mainConsumer.close();
+            mainConsumer.close(closeOptions);
         } catch (final Throwable e) {
             log.error("Failed to close consumer due to the following error:", e);
         }
         try {
-            restoreConsumer.close();
+            restoreConsumer.close(closeOptions);

Review Comment:
   the restore consumer direct assignment so it's not part of the consumer group and doesn't need the close options (I don't remember, what happens if you pass in LEAVE_GROUP or REMAIN_IN_GROUP for a consumer that doesn't use group subscription?)



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1994,6 +2005,13 @@ public Map<String, KafkaFuture<Uuid>> clientInstanceIds(final Duration timeout)
         return result;
     }
 
+    public void closeConsumer(final long timeoutMs, final boolean leaveGroup) {
+        final CloseOptions.GroupMembershipOperation operation = leaveGroup ? LEAVE_GROUP : REMAIN_IN_GROUP;
+        final CloseOptions closeOptions = CloseOptions.timeout(Duration.ofMillis(timeoutMs <= 0 ? 0 : timeoutMs))

Review Comment:
    it's definitely a bit questionable that we don't pass the timeout in to consumer#close in the current code (interestingly we also don't for producer#close but do use the timeout for admin#close) But, I don't want to change that in this PR, on the off chance there was an actual reason for it 
   
   I know we had some issues with the producer timeout on close in the past but I don't remember the specifics and I'm not personally aware of any similar issues for the consumer's close. It's worth following up on, but separately from this. Let's just use the default for now (ie don't set the timeout at all)



##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,9 +255,8 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured
         final boolean cleanResult = tryCleanGlobal(false, null, null, appID);
-        assertFalse(cleanResult);
+        assertTrue(cleanResult);

Review Comment:
   not 100% sure i follow, why does this change?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059383062


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1805,12 +1816,12 @@ private void completeShutdown(final boolean cleanRun) {
             log.error("Failed to unsubscribe due to the following error: ", e);
         }
         try {
-            mainConsumer.close();
+            mainConsumer.close(closeOptions);
         } catch (final Throwable e) {
             log.error("Failed to close consumer due to the following error:", e);
         }
         try {
-            restoreConsumer.close();
+            restoreConsumer.close(closeOptions);

Review Comment:
   I think we should pass either DEFAULT or REMAIN_IN_GROUP in for the restore consumer so it doesn't try to leave the group. If I'm remembering correctly, when LEAVE_GROUP is used it always tries to send a leave group even if it's not in a group, whereas with DEFAULT it will skip the leaveGroup if there's no member id or the coordinator is unknown



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2135830769


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1205,45 +1200,9 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                         final long cacheSizePerThread = cacheSizePerThread(numLiveStreamThreads());
                         log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                         resizeThreadCache(cacheSizePerThread);
-                        if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
-                            final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
-                            final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
-                            final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = 
-                                adminClient.removeMembersFromConsumerGroup(
-                                    applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG),
-                                    new RemoveMembersFromConsumerGroupOptions(membersToRemove)
-                                );
-                            try {
-                                final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
-                                removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
-                            } catch (final java.util.concurrent.TimeoutException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to a timeout:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new TimeoutException(exception.getMessage(), exception);
-                            } catch (final InterruptedException e) {
-                                Thread.currentThread().interrupt();
-                            } catch (final ExecutionException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new StreamsException(
-                                        "Could not remove static member " + groupInstanceID.get()
-                                            + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                            + " for the following reason: ",
-                                        exception.getCause()
-                                );
-                            }

Review Comment:
   @lucasbru @mjsax WDYT?
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2071526999


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1181,7 +1180,6 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                     final boolean callingThreadIsNotCurrentStreamThread = !streamThread.getName().equals(Thread.currentThread().getName());
                     if (streamThread.isThreadAlive() && (callingThreadIsNotCurrentStreamThread || numLiveStreamThreads() == 1)) {
                         log.info("Removing StreamThread {}", streamThread.getName());
-                        final Optional<String> groupInstanceID = streamThread.groupInstanceID();
                         streamThread.requestLeaveGroupDuringShutdown();
                         streamThread.shutdown();

Review Comment:
   I think we can just call the `streamThread.shutdown(boolean leaveGroup)` method here (with leaveGroup = true). Then we won't need the `streamThread.closeConsumer()` down below



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2155089524


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1823,14 +1826,7 @@ private void completeShutdown(final boolean cleanRun, final boolean leaveGroup)
             log.error("Failed to close changelog reader due to the following error:", e);
         }
         try {
-            if (leaveGroupRequested.get()) {
-                mainConsumer.unsubscribe();

Review Comment:
   mm yeah that's correct



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2076789736


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1791,13 +1791,16 @@ private long advanceNowAndComputeLatency() {
     public void shutdown(final boolean leaveGroup) {
         log.info("Informed to shut down");
         final State oldState = setState(State.PENDING_SHUTDOWN);
+        if (leaveGroup) {
+            requestLeaveGroupDuringShutdown();
+        }
         if (oldState == State.CREATED) {
             // The thread may not have been started. Take responsibility for shutting down
-            completeShutdown(true, leaveGroup);
+            completeShutdown(true);
         }
     }
 
-    private void completeShutdown(final boolean cleanRun, final boolean leaveGroup) {

Review Comment:
   We could leverage the `leaveGroupRequested` and don't need a new argument.



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1823,14 +1826,7 @@ private void completeShutdown(final boolean cleanRun, final boolean leaveGroup)
             log.error("Failed to close changelog reader due to the following error:", e);
         }
         try {
-            if (leaveGroupRequested.get()) {
-                mainConsumer.unsubscribe();

Review Comment:
   `Consumer# unsubscribe()` will make consumers leave the group. 
   I believe this is a workaround, and now we could rely onthe  new close API



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1454,10 +1450,6 @@ private Thread shutdownHelper(final boolean error, final long timeoutMs, final b
                 }
             });
 
-            if (leaveGroup) {
-                processStreamThread(streamThreadLeaveConsumerGroup(timeoutMs));
-            }

Review Comment:
   As previous discussion, we should only close the consumer in `StreamThread#completeShutdown`



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2062833316


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   Hi @ableegoldman 
   Thanks for the insight!
   
   I have dug into the code and found that the reason might be the calling of `consumer#close` twice.
   During the close process, we will first enter `StreamThread#completeShutdown`[0] with `leaveGroup=false`, which will invoke close for the first time. This is the key point: after the first invoke, the close flag of the consumer has been set to true.[1][2]
   The second close invoke is `KafkaStreams#shutdownHelper`.[3] At this time, we will pass `leaveGroup=true` to the method.[4] Unfortunately, the close flag of the consumer is set to true[5], so the consumer will not execute the leave group operation as expected. 
   
   [0] 
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java#L865
   
   [1]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L1129
   
   [2]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L1135
   
   [3]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java#L1536
   
   [4] Note that here, we want to call `consumer#close(CloseOptions)` instead of `processStreamThread(streamThreadLeaveConsumerGroup(timeoutMs));`, so it will be as follows.
   ```java
                   processStreamThread(t -> t.closeConsumer(true));
   ```
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java#L1462
   
   [5]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L1129-L1133



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2060908295


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   this method is only ever called from `KafkaStreams#close`, there's no separate method KafkaStreams method that can be used to remove consumer from the group via the admin client (side note: maybe there should be?). There's a cli tool for static member removal so users who don't set `leaveGroup=true` in the CloseOptions can do so through this. 
   
   So the new `consumer#close(CloseOptions.groupMembershipOperation(LEAVE_GROUP))` is essentially replacing this method entirely so in theory we should be able to remove it. That said, it's definitely concerning if KafkaStreamsCloseOptionsIntegrationTest is failing because we removed this. That suggests maybe our hack/workaround for removing static members in `consumer#close` via intentionally getting fenced doesn't work?
   
   If that's the case then we should go back to using the admin's `#removeMembersFromConsumerGroup` for Streams for now, and can follow up with that KIP to do a proper fix for allowing static members to permanently leave via LeaveGroup. 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059683265


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   Hi @ableegoldman 
   Do you think we should keep this method?
   Before KIP-1092, the consumer relied on `admin#removeMembersFromConsumerGroup` to leave the group.
   As https://github.com/apache/kafka/pull/19400#discussion_r2059386588 states, if we want to pass `leaveGroup=false` and maintain the default behavior, I think we should retain this method. Once a consumer is shut down and does not leave the group, they must rely on the admin to remove them from the consumer group.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-2831729304

   @frankvicky this isn't strictly related to this PR but there's a KIP (1153) out to fix up the API for the Streams version of CloseOptions, would be great if you could take a look and give a +1 once it moves to voting


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2057856462


##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,9 +255,8 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured
         final boolean cleanResult = tryCleanGlobal(false, null, null, appID);
-        assertFalse(cleanResult);
+        assertTrue(cleanResult);

Review Comment:
   In https://github.com/apache/kafka/pull/19400/files#r2036678414
   We have passed `leaveGroup=true` to the close method, which will make all consumers leave the group.
   To keep the original behavior, we should pass `leaveGroup=false` to the close method.
   
   I think we should maintain the original behavior for now; I will change it to false in the next commit.



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1805,12 +1816,12 @@ private void completeShutdown(final boolean cleanRun) {
             log.error("Failed to unsubscribe due to the following error: ", e);
         }
         try {
-            mainConsumer.close();
+            mainConsumer.close(closeOptions);
         } catch (final Throwable e) {
             log.error("Failed to close consumer due to the following error:", e);
         }
         try {
-            restoreConsumer.close();
+            restoreConsumer.close(closeOptions);

Review Comment:
   If the consumer doesn't subscribe, it will not join a group.
   In this case, it will get an unknown member ID in the response.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2072333954


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1776,22 +1780,28 @@ private long advanceNowAndComputeLatency() {
         return Math.max(now - previous, 0);
     }
 
+    public void shutdown() {

Review Comment:
   Make sense!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3227552232

   @mjsax Not sure - the default for the consumer was to leave the group for dynamic members, so I think the example is correct? We just need to make sure that Kafka Streams will use `REMAIN_IN_GROUP` by default, which I suppose this PR is doing.
   
   But I'm also confused. In KIP-1092, I see this:
   
   ```
            * {@code LEAVE_GROUP} means the consumer will leave the group.
            * {@code REMAIN_IN_GROUP} means the consumer will remain in the group.
            * {@code DEFAULT} applies the default behavior, which may depend on whether the consumer is static or dynamic.
   ```
   
   This seems clear. But in the new consumer code, a different behavior seems to be documented, see https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerMembershipManager.java#L431-L436
   
   ```
    - Default operation: both static and dynamic consumers will send a leave heartbeat
    - Leave operation: both static and dynamic consumers will send a leave heartbeat
    - Remain in group: only static consumers will send a leave heartbeat, while dynamic members will not
   ```
   
   ???
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3231880905

   @lucasbru: I have reviewed #17614. I suppose the difference you mentioned is due to https://github.com/apache/kafka/pull/17614#discussion_r2029841125.
   
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2060897554


##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,7 +256,6 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured

Review Comment:
   it happens šŸ˜„ 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2062833316


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   Hi @ableegoldman 
   Thanks for the insight!
   
   I have dug into the code and found that the reason might be the calling of `consumer#close` twice.
   During the close process, we will first enter `StreamThread#completeShutdown`[0] with `leaveGroup=false`, which will invoke close for the first time. This is the key point: after the first invoke, the close flag of the consumer has been set to true.[1][2]
   The second close invoke is `KafkaStreams#shutdownHelper`.[3] At this time, we will pass `leaveGroup=true` to the method.[4] Unfortunately, the close flag of the consumer is set to true[5], so the consumer will not execute the leave group operation as expected. 
   
   [0] 
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java#L865
   
   [1]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L1129
   
   [2]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L1135
   
   [3]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java#L1536
   
   [4] Note that here, we want to call `consumer#close(CloseOptions)` instead of `processStreamThread(streamThreadLeaveConsumerGroup(timeoutMs));`, so it will be as follows.
   ```java
                   processStreamThread(t -> t.closeConsumer(true));
   ```
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java#L1462
   
   [5]
   https://github.com/frankvicky/kafka/blob/ffb74cb5a4673696c4b2517d898e4e06e8223b73/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L1129



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059385075


##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,9 +255,8 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured
         final boolean cleanResult = tryCleanGlobal(false, null, null, appID);
-        assertFalse(cleanResult);
+        assertTrue(cleanResult);

Review Comment:
   oh i see, I missed that we were passing `leaveGroup=true` to the consumer before. Glad this test caught that, though maybe we should try to have a test specifically to make sure the default leave group behavior isn't ever changed by accident in the future?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2076630475


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   yeah, so we should basically just leave everything up to the StreamThread's `completeShutdown` method. It's true that right now the shutdown helper calls this `#streamThreadLeaveConsumerGroup` after the thread has shut down which makes it seem like we want the shutdown helper to be responsible for managing the group membership after the thread has died, but that's really just an artifact of the lack of a `consumer#close(CloseOptions)` before now. We had to wait for the thread to shut down and close the consumer, and then we could use the admin client to explicitly remove it from the group. But that's really not ideal for a number of reasons (a big one being that it only works for static members) and was essentially just a workaround since the consumer couldn't do this itself.
   
   Now that we do have consumers which can handle their own group membership via `#close`, we don't need this workaround and can leave everything up to the consumer which the StreamThread owns. In short, instead of refactoring the way this `#streamThreadLeaveConsumerGroup` method works, we should just remove this call entirely, and leave it up to the StreamThread to properly close the consumer with the right leavegroup option in its `#completeShutdown`
   
   Lmk if that makes sense!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2071529203


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1181,7 +1180,6 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                     final boolean callingThreadIsNotCurrentStreamThread = !streamThread.getName().equals(Thread.currentThread().getName());
                     if (streamThread.isThreadAlive() && (callingThreadIsNotCurrentStreamThread || numLiveStreamThreads() == 1)) {
                         log.info("Removing StreamThread {}", streamThread.getName());
-                        final Optional<String> groupInstanceID = streamThread.groupInstanceID();
                         streamThread.requestLeaveGroupDuringShutdown();
                         streamThread.shutdown();

Review Comment:
   In fact, we should probably never call the plain `stream#shutdown` method anymore and can get rid of it altogether to make sure we always use the right method and `leaveGroup` operation. 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2134956959


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1205,45 +1200,9 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                         final long cacheSizePerThread = cacheSizePerThread(numLiveStreamThreads());
                         log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                         resizeThreadCache(cacheSizePerThread);
-                        if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
-                            final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
-                            final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
-                            final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = 
-                                adminClient.removeMembersFromConsumerGroup(
-                                    applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG),
-                                    new RemoveMembersFromConsumerGroupOptions(membersToRemove)
-                                );
-                            try {
-                                final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
-                                removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
-                            } catch (final java.util.concurrent.TimeoutException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to a timeout:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new TimeoutException(exception.getMessage(), exception);
-                            } catch (final InterruptedException e) {
-                                Thread.currentThread().interrupt();
-                            } catch (final ExecutionException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new StreamsException(
-                                        "Could not remove static member " + groupInstanceID.get()
-                                            + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                            + " for the following reason: ",
-                                        exception.getCause()
-                                );
-                            }

Review Comment:
   Nice catch!
   Let's wait for more feedback on it.
   I hope we can reach a consensus on this change.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059380545


##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,7 +256,6 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured

Review Comment:
   why remove this comment?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059686017


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   This is also the reason that KafkaStreamsCloseOptionsIntegrationTest is failing.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059386588


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -875,7 +878,7 @@ public void run() {
             streamsUncaughtExceptionHandler.accept(e, false);
             // Note: the above call currently rethrows the exception, so nothing below this line will be executed
         } finally {
-            completeShutdown(cleanRun);
+            completeShutdown(cleanRun, true);

Review Comment:
   ah sorry i totally missed this question on my first pass šŸ¤¦ā€ā™€ļø No, we actually do want to stay in the group and retain the existing default behavior for Streams
   
   if you're curious why the default behavior for Streams is the opposite of the plain consumer client, it's because Streams is stateful which means rebalancing and moving partitions can be a much heavier operation and involve restoring many records from the changelog topics when partitions are shuffled around. By skipping the leave group, we can often avoid an unnecessary extra rebalance that would result in partitions being moved, since the assignment of Streams is heavily dependent on the current group members. If an error gets thrown and the StreamThread dies and gets restarted within the session timeout, we can avoid any rebalances where the dead thread is missing, so the rebalance when the new thread restarts should result in the same assignment of partitions to threads as we had before the thread died.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059384302


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1805,12 +1816,12 @@ private void completeShutdown(final boolean cleanRun) {
             log.error("Failed to unsubscribe due to the following error: ", e);
         }
         try {
-            mainConsumer.close();
+            mainConsumer.close(closeOptions);
         } catch (final Throwable e) {
             log.error("Failed to close consumer due to the following error:", e);
         }
         try {
-            restoreConsumer.close();
+            restoreConsumer.close(closeOptions);

Review Comment:
   Hm actually how could it even send a LeaveGroup if LEAVE_GROUP is selected for a consumer that isn't part of a group? That is, by definition the coordinator is unknown, so where would the request even be sent?
   
   Don't remember of the top of my head how we handled this in the consumer client CloseOptions PR but it seems like the consumer should be agnostic to the GroupMembershipOperation and never send anything if it's not part of a group to begin with, right?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2071546806


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -2013,6 +2024,12 @@ public Map<String, KafkaFuture<Uuid>> clientInstanceIds(final Duration timeout)
         return result;
     }
 
+    public void closeConsumer(final boolean leaveGroup) {
+        final GroupMembershipOperation operation = leaveGroup ? LEAVE_GROUP : REMAIN_IN_GROUP;
+        final CloseOptions closeOptions = CloseOptions.groupMembershipOperation(operation);
+        mainConsumer.close(closeOptions);

Review Comment:
   we should be able to get rid of this method so it doesn't matter, but just fyi this wouldn't be thread safe. For future reference, the way we invoke things on the StreamThread from outside (eg from `KafkaStreams`) is usually to just set a flag and then check it in the main loop which starts from the `runLoop()` method šŸ™‚ 



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   leftover debugging code I'm guessing? šŸ˜„ 



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1824,12 +1834,13 @@ private void completeShutdown(final boolean cleanRun) {
             log.error("Failed to unsubscribe due to the following error: ", e);
         }
         try {
-            mainConsumer.close();
+            final GroupMembershipOperation membershipOperation = leaveGroup ? LEAVE_GROUP : REMAIN_IN_GROUP;
+            mainConsumer.close(CloseOptions.groupMembershipOperation(membershipOperation));
         } catch (final Throwable e) {
             log.error("Failed to close consumer due to the following error:", e);
         }
         try {
-            restoreConsumer.close();
+            restoreConsumer.close(CloseOptions.groupMembershipOperation(REMAIN_IN_GROUP));

Review Comment:
   can you just add a comment here to explain why we use this for the restore consumer, eg
   ```
   // restore consumer isn't part of a group so we use REMAIN_IN_GROUP to skip any leaveGroup checks
   ```



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   actually we should be able to get rid of this entire method now, right? the consumer#close should take care of leaving the group so we don't have to rely on the admin client anymore



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2076862882


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   The test of close consumer we already have a test: `org.apache.kafka.clients.consumer.KafkaConsumerTest#testGracefulClose`
   
   It would give us enough protection after we replace it with the new close API.
   I would like to do it in a separate PR.
   https://issues.apache.org/jira/browse/KAFKA-19249
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [WIP] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2036675899


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1205,45 +1200,9 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                         final long cacheSizePerThread = cacheSizePerThread(numLiveStreamThreads());
                         log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                         resizeThreadCache(cacheSizePerThread);
-                        if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
-                            final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
-                            final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
-                            final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = 
-                                adminClient.removeMembersFromConsumerGroup(
-                                    applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG),
-                                    new RemoveMembersFromConsumerGroupOptions(membersToRemove)
-                                );
-                            try {
-                                final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
-                                removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
-                            } catch (final java.util.concurrent.TimeoutException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to a timeout:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new TimeoutException(exception.getMessage(), exception);
-                            } catch (final InterruptedException e) {
-                                Thread.currentThread().interrupt();
-                            } catch (final ExecutionException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new StreamsException(
-                                        "Could not remove static member " + groupInstanceID.get()
-                                            + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                            + " for the following reason: ",
-                                        exception.getCause()
-                                );
-                            }

Review Comment:
   IMHO, this chunk of code could be replaced with the new close api.



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -875,7 +878,7 @@ public void run() {
             streamsUncaughtExceptionHandler.accept(e, false);
             // Note: the above call currently rethrows the exception, so nothing below this line will be executed
         } finally {
-            completeShutdown(cleanRun);
+            completeShutdown(cleanRun, true);

Review Comment:
   If we close the `StreamThread`,  we should ensure consumers leave their group.
   Does it make sense?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3366217068

   @frankvicky I'm going to make a pass regarding [this comment ](https://github.com/apache/kafka/pull/19400#issuecomment-3242118581) from @lucasbru - I want to confirm if we are changing the default behavior, which is something I don't think we want to do.  Either way we'll get this merged soon


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2072334931


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   > leftover debugging code I'm guessing? šŸ˜„
   
   Ahhhhhh... looks like someone left their debugging breadcrumbs 😬 
   
   ![image](https://github.com/user-attachments/assets/65a02f40-aa08-4ac0-8f16-292a8a644259)
   
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [WIP] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2036681668


##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,9 +255,8 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured
         final boolean cleanResult = tryCleanGlobal(false, null, null, appID);
-        assertFalse(cleanResult);
+        assertTrue(cleanResult);

Review Comment:
   Regarding https://github.com/apache/kafka/pull/19400#discussion_r2036675899
   This assertion is no longer false.
   Make sense?



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1640,33 +1599,6 @@ public synchronized boolean close(final CloseOptions options) throws IllegalArgu
         return close(Optional.of(timeoutMs), options.leaveGroup);
     }
 
-    private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remainingTimeMs) {

Review Comment:
   Same as https://github.com/apache/kafka/pull/19400/files#r2036675899



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2072336376


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   Back to the topic.
   @ableegoldman 
   I have some thoughts about removing this method. 
   https://github.com/apache/kafka/pull/19400#discussion_r2062833316
   
   I will handle the following comment after we have a conclusion.
   https://github.com/apache/kafka/pull/19400#discussion_r2059385075
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3223862580

   > what is missing from finishing this work?
   
   I will take a look later :rofl: 
   
   > Also, I see that support for CloseOptions was implemented for KIP-848 and share groups, but it is missing from KIP-1071. Do we need a separate ticket to complete the work to also include KIP-1071? Should work analogous.
   
   I’m not sure it makes sense that the shared consumer does not leave the group when closing. The main motivation for introducing `CloseOptions` is to improve Kafka Streams.
   
   By the way, it would be good to have a separatet KIP to implement `CloseOptions` for shared consumer if needs. @AndrewJSchofield @apoorvmittal10 FYI


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2400156975


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1874,10 +1878,15 @@ private long advanceNowAndComputeLatency() {
      * <p>
      * Note that there is nothing to prevent this function from being called multiple times
      * (e.g., in testing), hence the state is set only the first time
+     *
+     * @param leaveGroup this flag will control whether the consumer will leave the group on close or not
      */
-    public void shutdown() {
+    public void shutdown(final boolean leaveGroup) {
         log.info("Informed to shut down");
         final State oldState = setState(State.PENDING_SHUTDOWN);
+        if (leaveGroup) {

Review Comment:
   couldn't this be simplified to `leaveGroupRequested.set(leaveGroup)`



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3242118581

   @frankvicky It looks like that integration test was broken by https://github.com/apache/kafka/pull/20284 . I can see that it passes on CI only because it is first run with the old protocol, and then run with the new protocol, and in the new protocol it shuts down only because it tries to reuse the group ID from the first run. At this time, the member from the first run is still in the group, so we get an error that the group is of the incorrect protocol type.
   
   I created https://issues.apache.org/jira/browse/KAFKA-19660 for that.
   
   This indeed shines a light on your (this) PR though: It seems to changes the default for Kafka Streams: it leaves the group when `streams.close()` is called, while this wasn't the case before.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3241614781

   @frankvicky What I actually meant to ask in my previous comment: Are you planning to implement these changes also for KIP-1071? While static membership isn't fully implemented yet for KIP-1071, we should still pull through those changes.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2401027088


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1874,10 +1878,15 @@ private long advanceNowAndComputeLatency() {
      * <p>
      * Note that there is nothing to prevent this function from being called multiple times
      * (e.g., in testing), hence the state is set only the first time
+     *
+     * @param leaveGroup this flag will control whether the consumer will leave the group on close or not
      */
-    public void shutdown() {
+    public void shutdown(final boolean leaveGroup) {
         log.info("Informed to shut down");
         final State oldState = setState(State.PENDING_SHUTDOWN);
+        if (leaveGroup) {

Review Comment:
   Nice catch!
   I have amended it.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2134045418


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1816,10 +1820,15 @@ private long advanceNowAndComputeLatency() {
      * <p>
      * Note that there is nothing to prevent this function from being called multiple times
      * (e.g., in testing), hence the state is set only the first time
+     *
+     * @param leaveGroup this flag will control whether the consumer will leave the group on close or not
      */
-    public void shutdown() {
+    public void shutdown(final boolean leaveGroup) {
         log.info("Informed to shut down");
         final State oldState = setState(State.PENDING_SHUTDOWN);
+        if (leaveGroup) {
+            requestLeaveGroupDuringShutdown();

Review Comment:
   `requestLeaveGroupDuringShutdown` is not used by other classes now, so we can remove `requestLeaveGroupDuringShutdown` by calling `leaveGroupRequested.set(true);`



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1181,9 +1177,7 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                     final boolean callingThreadIsNotCurrentStreamThread = !streamThread.getName().equals(Thread.currentThread().getName());
                     if (streamThread.isThreadAlive() && (callingThreadIsNotCurrentStreamThread || numLiveStreamThreads() == 1)) {
                         log.info("Removing StreamThread {}", streamThread.getName());
-                        final Optional<String> groupInstanceID = streamThread.groupInstanceID();
-                        streamThread.requestLeaveGroupDuringShutdown();
-                        streamThread.shutdown();
+                        streamThread.shutdown(true);
                         if (!streamThread.getName().equals(Thread.currentThread().getName())) {

Review Comment:
   As you are updating this file, please replace `!streamThread.getName().equals(Thread.currentThread().getName())` with `callingThreadIsNotCurrentStreamThread`.



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -1824,12 +1834,13 @@ private void completeShutdown(final boolean cleanRun) {
             log.error("Failed to unsubscribe due to the following error: ", e);
         }
         try {
-            mainConsumer.close();
+            final GroupMembershipOperation membershipOperation = leaveGroup ? LEAVE_GROUP : REMAIN_IN_GROUP;
+            mainConsumer.close(CloseOptions.groupMembershipOperation(membershipOperation));
         } catch (final Throwable e) {
             log.error("Failed to close consumer due to the following error:", e);
         }
         try {
-            restoreConsumer.close();
+            restoreConsumer.close(CloseOptions.groupMembershipOperation(REMAIN_IN_GROUP));

Review Comment:
   The restore consumer does not use a consumer group, so it should be fine to call  `restoreConsumer.close();`, right?



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1205,45 +1200,9 @@ private Optional<String> removeStreamThread(final long timeoutMs) throws Timeout
                         final long cacheSizePerThread = cacheSizePerThread(numLiveStreamThreads());
                         log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                         resizeThreadCache(cacheSizePerThread);
-                        if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
-                            final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
-                            final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
-                            final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = 
-                                adminClient.removeMembersFromConsumerGroup(
-                                    applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG),
-                                    new RemoveMembersFromConsumerGroupOptions(membersToRemove)
-                                );
-                            try {
-                                final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
-                                removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
-                            } catch (final java.util.concurrent.TimeoutException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to a timeout:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new TimeoutException(exception.getMessage(), exception);
-                            } catch (final InterruptedException e) {
-                                Thread.currentThread().interrupt();
-                            } catch (final ExecutionException exception) {
-                                log.error(
-                                    String.format(
-                                        "Could not remove static member %s from consumer group %s due to:",
-                                        groupInstanceID.get(),
-                                        applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                    ),
-                                        exception
-                                );
-                                throw new StreamsException(
-                                        "Could not remove static member " + groupInstanceID.get()
-                                            + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG)
-                                            + " for the following reason: ",
-                                        exception.getCause()
-                                );
-                            }

Review Comment:
   It seems the behavior of `KafkaStreams#removeStreamThread` has changed. Without an explicit removal, the consumer is kicked off from the group after stream thread completes its `run` function. This means users CANNOT assume `KafkaStreams#removeStreamThread` will remove the member from the group. 



##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -490,7 +487,7 @@ private void replaceStreamThread(final Throwable throwable) {
             closeToError();
         }
         final StreamThread deadThread = (StreamThread) Thread.currentThread();
-        deadThread.shutdown();
+        deadThread.shutdown(true);

Review Comment:
   It seems the origin behavior does not want to leave group, right? otherwise, it will trigger the extra rebalance.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059685193


##########
tools/src/test/java/org/apache/kafka/tools/ResetIntegrationTest.java:
##########
@@ -256,7 +256,6 @@ public void testResetWhenLongSessionTimeoutConfiguredWithForceOption(final TestI
         streams = new KafkaStreams(setupTopologyWithoutIntermediateUserTopic(), streamsConfig);
         streams.cleanUp();
 
-        // Reset would fail since long session timeout has been configured

Review Comment:
   It was unintentionally deleted when I was tweaking this test.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059787354


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -875,7 +878,7 @@ public void run() {
             streamsUncaughtExceptionHandler.accept(e, false);
             // Note: the above call currently rethrows the exception, so nothing below this line will be executed
         } finally {
-            completeShutdown(cleanRun);
+            completeShutdown(cleanRun, true);

Review Comment:
   Thanks for the explanation! 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "ableegoldman (via GitHub)" <gi...@apache.org>.
ableegoldman commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2059386588


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java:
##########
@@ -875,7 +878,7 @@ public void run() {
             streamsUncaughtExceptionHandler.accept(e, false);
             // Note: the above call currently rethrows the exception, so nothing below this line will be executed
         } finally {
-            completeShutdown(cleanRun);
+            completeShutdown(cleanRun, true);

Review Comment:
   ah sorry i totally missed this question on my first pass šŸ¤¦ā€ā™€ļø No, we actually do want to stay in the group and retain the existing default behavior for Streams
   
   if you're curious why the default behavior for Streams is the opposite of the plain consumer client, it's because Streams is stateful which means rebalancing and moving partitions can be a much heavier operation and involve restoring many records from the changelog topics when partitions are shuffled around. By skipping the leave group, we can often avoid an unnecessary extra rebalance



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #19400:
URL: https://github.com/apache/kafka/pull/19400#discussion_r2072336376


##########
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java:
##########
@@ -1645,6 +1606,7 @@ private Consumer<StreamThread> streamThreadLeaveConsumerGroup(final long remaini
             if (groupInstanceId.isPresent()) {
                 log.debug("Sending leave group trigger to removing instance from consumer group: {}.",
                     groupInstanceId.get());
+                System.err.println(groupInstanceId.get());

Review Comment:
   Back to the topic.
   @ableegoldman 
   I have some thoughts about removing this method. 
   https://github.com/apache/kafka/pull/19400#discussion_r2062833316
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3238419918

   High level comment: Is it wise to complete this PR before https://github.com/apache/kafka/pull/19955 ? -- It seems we should only remove the internal config, after we got the public API change for KS merged? Otherwise, even if not publicly user facing, we are loosing the ability to change KS's behavior on `KafkaStreams#close()` ? -- I know that we have some users, how actually don't like how KS behalves, and actually use this internal config to change the behavior. If we merge this PR before we complete KIP-1153, be would introduce a "regression" for these users.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3229679525

   Oh sorry. I mixed up this work with the corresponding KS work... https://cwiki.apache.org/confluence/display/KAFKA/KIP-1153%3A+Refactor+Kafka+Streams+CloseOptions+to+Fluent+API+Style 


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3241650294

   @lucasbru: Sorry that I missed responding to your question.
   IMO, we should implement these changes also for KIP-1071, but I'm not sure we can do it at this moment.
   I'm investigating the root cause of the failed test `JoinWithIncompleteMetadataIntegrationTest#testShouldAutoShutdownOnJoinWithIncompleteMetadata`, and it appears that the root cause is that the new protocol doesn't trigger rebalance when the topic is missing.
   Would you happen to have any insight?


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck commented on PR #19400:
URL: https://github.com/apache/kafka/pull/19400#issuecomment-3366559636

   Merged #19400 into trunk


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-18185: remove internal.leave.group.on.close config [kafka]

Posted by "bbejeck (via GitHub)" <gi...@apache.org>.
bbejeck merged PR #19400:
URL: https://github.com/apache/kafka/pull/19400


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org