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 2024/05/21 14:05:21 UTC

[PR] KAFKA-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Fix the bug where the heartbeat is not sent when a newly created consumer is immediately closed.
   
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Hi @lianetm 
   I agree with your perspective. Reproducing this bug at the integration level is indeed complex and challenging. Therefore, I have removed the ReproduceHeartbeatFailTest. Additionally, I have refactored the test case by leveraging the utility function and extended the PR description. Please take a look. :grinning:


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +209,8 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+//        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+        boolean heartbeatNow = membershipManager.state() == MemberState.LEAVING || membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();

Review Comment:
   Could we add comments to list the cases about `heartbeatNow=true`? the condition check is a bit complicated to me :laughing: 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -880,4 +880,25 @@ private HeartbeatRequestManager createHeartbeatRequestManager(
                 backgroundEventHandler,
                 metrics);
     }
+
+
+    @Test
+    public void reproduceHeartbeatFail() throws InterruptedException {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.transitionToSendingLeaveGroup(true);

Review Comment:
   harmless but confusing to pass `true` here, since that indicates that the leave group is due to an expired poll timer (which is not the case we dealing with). I would suggest we either pass false, or I would even prefer to use the `membershipManager.leaveGroup()`, which is the higher level call that is used from the consumer `close`



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -880,4 +880,25 @@ private HeartbeatRequestManager createHeartbeatRequestManager(
                 backgroundEventHandler,
                 metrics);
     }
+
+
+    @Test
+    public void reproduceHeartbeatFail() throws InterruptedException {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.transitionToSendingLeaveGroup(true);
+        assertTrue(membershipManager.isLeavingGroup());
+
+        NetworkClientDelegate.PollResult resultAfterLeaving = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, resultAfterLeaving.unsentRequests.size());

Review Comment:
   Here it would be helpful to check that the HB request generated is indeed the one to leave the group. So what about checking that `ConsumerGroupHeartbeatRequest` contained in the resultAfterLeaving has actually the leave epoch? There is already a utility func [getHeartbeatRequest](https://github.com/apache/kafka/blob/4d04eb83ea2cf67484d81e30603e976f63616428/clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java#L423) in this class to help you with that. 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Thanks for the updates @frankvicky! Could we update the PR name to be accurate about the scope of this PR? (aligning with the description that was already updated too). We are ensuring that the HB manager generates leave request regardless of in-flights.


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +209,8 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+//        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+        boolean heartbeatNow = membershipManager.state() == MemberState.LEAVING || membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();

Review Comment:
   nit: I find that adding a line break after the || makes it easier to understand (splitting the 2 cases that come out of this)



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -752,6 +752,26 @@ public void testFencedMemberStopHeartbeatUntilItReleasesAssignmentToRejoin() {
         assertEquals(1, result.unsentRequests.size(), "Fenced member should resume heartbeat after transitioning to JOINING");
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.CONSUMER_GROUP_HEARTBEAT)
+    public void testSendingLeaveGroupHeartbeatWhenPreviousOneInFlight(final short version) {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.leaveGroup();
+        assertTrue(membershipManager.isLeavingGroup());

Review Comment:
   nit: I don't think this line brings value here. We have ln 769, which is the end result we're interested in asserting to make sure we're actually leaving the group (and this is in the end logic belonging to the other component to achieve that result) 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   hey @frankvicky , thanks for the changes, left a few last minor comments. 


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +209,8 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+//        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();

Review Comment:
   let's remove this pls



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +209,11 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+        // Case 1: The member is leaving
+        boolean heartbeatNow = membershipManager.state() == MemberState.LEAVING ||
+                // Case 2: The member should send a heartbeat now, and there is no heartbeat request currently in flight

Review Comment:
   ```suggestion
                   // Case 2: The member state indicates it should send a heartbeat without waiting for the interval, and there is no heartbeat request currently in-flight
   ```



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +209,11 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+        // Case 1: The member is leaving
+        boolean heartbeatNow = membershipManager.state() == MemberState.LEAVING ||
+                // Case 2: The member should send a heartbeat now, and there is no heartbeat request currently in flight

Review Comment:
   Thank you, this is more precisely and clear, I will modify the 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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -880,4 +880,25 @@ private HeartbeatRequestManager createHeartbeatRequestManager(
                 backgroundEventHandler,
                 metrics);
     }
+
+
+    @Test
+    public void reproduceHeartbeatFail() throws InterruptedException {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.transitionToSendingLeaveGroup(true);

Review Comment:
   +1 to `membershipManager.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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -752,6 +752,26 @@ public void testFencedMemberStopHeartbeatUntilItReleasesAssignmentToRejoin() {
         assertEquals(1, result.unsentRequests.size(), "Fenced member should resume heartbeat after transitioning to JOINING");
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.CONSUMER_GROUP_HEARTBEAT)
+    public void testSendingLeaveGroupHeartbeatWhenPreviousOneInFlight(final short version) {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.leaveGroup();
+        assertTrue(membershipManager.isLeavingGroup());

Review Comment:
   nit: I don't think this line brings value here. We have ln 769, which is the end result we're interested in, asserting to make sure we're actually leaving the group (and this is in the end logic belonging to the other component to achieve that result) 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Hi @lianetm 
   Sorry for late update.
   I tried the steps you provided to write unit tests, and it was very helpful! 
   And the suggestion regarding the adjustment of the 'heartbeatNow' parameter is indeed effective. Thank you very much.  :smile:
   I am currently still trying to fix this issue in the integration tests, which might take some more time.


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Hi @lianetm,
   I really appreciate your suggestion. These suggestions are very helpful to me. I will give them a try. 😸


-- 
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-16639: Ensure HeartbeatRequestManager generates leave request regardless of in-flight heartbeats. [kafka]

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

   Hi @lianetm, sure, I just renamed the PR. Please take a look. 😸 


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -880,4 +880,25 @@ private HeartbeatRequestManager createHeartbeatRequestManager(
                 backgroundEventHandler,
                 metrics);
     }
+
+
+    @Test
+    public void reproduceHeartbeatFail() throws InterruptedException {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.transitionToSendingLeaveGroup(true);
+        assertTrue(membershipManager.isLeavingGroup());
+
+        NetworkClientDelegate.PollResult resultAfterLeaving = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, resultAfterLeaving.unsentRequests.size());
+        NetworkClientDelegate.PollResult pollAgain = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, pollAgain.unsentRequests.size());
+        assertEquals(ConsumerGroupHeartbeatRequest.LEAVE_GROUP_MEMBER_EPOCH, membershipManager.memberEpoch());

Review Comment:
   I would remove this line if we agree on my comment above. We don't need to care about what this other component does, as long as the one we're unit testing ends up generating the request we are looking for. 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Hi @lianetm, thanks for your comments. I have made some changes based on them, please take a look. 😺


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -752,6 +752,26 @@ public void testFencedMemberStopHeartbeatUntilItReleasesAssignmentToRejoin() {
         assertEquals(1, result.unsentRequests.size(), "Fenced member should resume heartbeat after transitioning to JOINING");
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.CONSUMER_GROUP_HEARTBEAT)
+    public void testSendingLeaveGroupHeartbeatWhenPreviousOneInFlight(final short version) {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.leaveGroup();
+        assertTrue(membershipManager.isLeavingGroup());

Review Comment:
   This makes sense. In the unit test for 'HeartBratRequestManager,' we should focus solely on its functionality.
   I will remove this line. 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +209,8 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+//        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+        boolean heartbeatNow = membershipManager.state() == MemberState.LEAVING || membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();

Review Comment:
   This make sense, I will add the comments to clarify this variable.



-- 
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-16639: Ensure HeartbeatRequestManager generates leave request regardless of in-flight heartbeats. [kafka]

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


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Hey @frankvicky , thanks a lot for taking on this one, I took a look and left some comments. Related to testing, I would suggest we start with a simple unit test in the HeartbeatManager, where I think we can easily repro the situation:
   
   1. poll and assert a HB request is generated (leave it without response)
   2. poll and assert no HB is sent (just to be sure the logic for blocking on inflight is in place)
   2. mock a leave group operation (membershipMgr.leaveGroup, this is what's in the end called when the member leaves for any reason: unsubscribed or close)
   3. poll again  and assert a HB is indeed generated (this is what should fail without your PR)
   4. poll again just to make sure another HB to leave is not generated
   
   Hope it helps! Let me know. Thanks!


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -880,4 +880,25 @@ private HeartbeatRequestManager createHeartbeatRequestManager(
                 backgroundEventHandler,
                 metrics);
     }
+
+
+    @Test
+    public void reproduceHeartbeatFail() throws InterruptedException {
+        mockStableMember();
+        time.sleep(DEFAULT_HEARTBEAT_INTERVAL_MS);
+        NetworkClientDelegate.PollResult result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(1, result.unsentRequests.size());
+
+        result = heartbeatRequestManager.poll(time.milliseconds());
+        assertEquals(0, result.unsentRequests.size(), "No heartbeat should be sent while a previous one is in-flight");
+
+        membershipManager.transitionToSendingLeaveGroup(true);
+        assertTrue(membershipManager.isLeavingGroup());
+
+        NetworkClientDelegate.PollResult resultAfterLeaving = heartbeatRequestManager.poll(time.milliseconds());

Review Comment:
   maybe renaming this to `heartbeatToLeave` would be clearer? at this point we're only generating the request to leave



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -208,7 +210,7 @@ public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
             return new NetworkClientDelegate.PollResult(heartbeatRequestState.heartbeatIntervalMs, Collections.singletonList(leaveHeartbeat));
         }
 
-        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
+        boolean heartbeatNow = membershipManager.shouldHeartbeatNow() && (!heartbeatRequestState.requestInFlight() || membershipManager.isLeavingGroup());

Review Comment:
   I'm afraid this addition has an undesired side-effect: members with rebalance callbacks will be storming the broker with heartbeat requests without respecting the HB interval (ex. while `onPartitionsRevoked` runs to release assignment before leaving). The issue is that `isLeavingGroup` will be true while the member is in PREPARE_LEAVING (executing the rebalance callbacks to revoke the owned partitions, which runs on the user space out of our control). While the callbacks execute (might be long), we want to keep the member in the group, so we keep sending heartbeats, on the interval.
   
   That being said, I wonder if this small twist would be enough:
   ```suggestion
           boolean heartbeatNow = membershipManager.state() == MemberState.LEAVING ||
               membershipManager.shouldHeartbeatNow() && !heartbeatRequestState.requestInFlight();
   ```
   
   At the end of the day, when LEAVING (not preparing to leave), we shouldn't have to wait for an inflight because:
   1. the leave intention is not affected in any way by the response that may come for the inflight request (ex. getting fenced, new assignments, all will be ignored if member is LEAVING)
   2. the HB to leave is only sent once, so no risk of storming the broker with requests sent without waiting for inflight responses. 
   
   I'm still thinking in case I'm missing something, but saying that we don't wait for inflight responses to send a leave group seems conceptually right, and solves the issue we had since that was truly the root cause. 
   
   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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -422,7 +422,7 @@ public void onHeartbeatSuccess(ConsumerGroupHeartbeatResponseData response) {
         if (state == MemberState.LEAVING) {
             log.debug("Ignoring heartbeat response received from broker. Member {} with epoch {} is " +
                     "already leaving the group.", memberId, memberEpoch);
-            return;
+//            return;

Review Comment:
   Is there a need to process the leave group response now? We were intentionally avoiding processing the leave response just to avoid undesired effects on the client due to unexpected responses. Processing the HB response leads to updating memberId, epoch, reconciling assignments, state transitions, etc, depending on the content of the response. On the leave, we don't expect any, and we actually do the transitions that are needed the moment the leave request is sent out, so just short-circuiting here. 



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -880,4 +880,25 @@ private HeartbeatRequestManager createHeartbeatRequestManager(
                 backgroundEventHandler,
                 metrics);
     }
+
+
+    @Test
+    public void reproduceHeartbeatFail() throws InterruptedException {

Review Comment:
   I'm afraid in some time from now we won't know what heartbeat failure this test refers to (without reading the whole test). So what about renaming this to something like `testLeaveGroupSentOnCloseWhenPreviousOneInflight` or something along those lines



-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Also, could we update the PR description to extend on the root cause (leave not sent if another in-flight) and approach (send leave group without waiting for previous in-flight, based on the fact that leave intention is not affected by previous responses, and the leave is only sent out once, in the transient state LEAVING). Helpful to keep also what you currently have, that describes the case where the situation becomes an issue. 


-- 
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-16639: AsyncKafkaConsumer#close dose not send heartbeat to leave group. [kafka]

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

   Great news! Thanks for the updates @frankvicky, I'll take a look. 
   
   What exactly are you after regarding the integration test? I expect this bit would be tricky to test at that level, given it depends on many time situations from broker/client that need to align (HB response not received, trigger leave then, and checking that the request would actually happen would require some kind of validation of rebalance happening within the inflight time-frame). Maybe there's an easy and effective way I'm missing, but I would say the unit tests at the HB manager level should give us good confidence on this change. 
   
   Thanks!  


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