You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "apoorvmittal10 (via GitHub)" <gi...@apache.org> on 2024/10/22 22:29:53 UTC

[PR] KAFKA-17848: Fixing NPE in delayed share fetch [kafka]

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

   The test shows that with 16 topic partitions reading 1 million messages with 10 Share Consumers, the share fetch stucks.
   
   Though the code should execute `tryComplete` but in some scenario `onComplete` is called prior to the `tryComplete` which causes error. This PR fixes same.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] 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-17848: Fixing NPE in delayed share fetch [kafka]

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

   @junrao For my understanding, what can trigger `onComplete` without invoking `tryComplete`? Is it the number of requests that can be in purgatory? I was doing 10 share consumers paralled read with 5Million records already produced over 16 partitions.


-- 
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-17848: Fixing NPE in delayed share fetch [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -79,16 +79,16 @@ public void onExpiration() {
      */
     @Override
     public void onComplete() {
-        log.trace("Completing the delayed share fetch request for group {}, member {}, " +
-                        "topic partitions {}", shareFetchData.groupId(),
-            shareFetchData.memberId(), shareFetchData.partitionMaxBytes().keySet());
+        log.trace("Completing the delayed share fetch request for group {}, member {}, "
+            + "topic partitions {}", shareFetchData.groupId(), shareFetchData.memberId(),
+            topicPartitionDataFromTryComplete != null ? topicPartitionDataFromTryComplete.keySet() : "null");
 
         if (shareFetchData.future().isDone())
             return;
 
         Map<TopicIdPartition, FetchRequest.PartitionData> topicPartitionData;
         // tryComplete did not invoke forceComplete, so we need to check if we have any partitions to fetch.
-        if (topicPartitionDataFromTryComplete.isEmpty())
+        if (topicPartitionDataFromTryComplete == null || topicPartitionDataFromTryComplete.isEmpty())

Review Comment:
   For my understanding, how can this value be null? We initialize it in the `DelayedShareFetch` constructor and its updation always returns a map.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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

   @junrao Thanks for the suggestion, I have addressed the feedback.


-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   Interesting. `tryCompleteWatched` checks `isCompleted` without the lock. So, it's possible that multiple callers check `isCompleted` and get false. They all get queued up on the lock and will call `safeTryComplete` and `tryComplete` multiple times. Perhaps we could further add a `isCompleted` check inside `safeTryComplete` before making the `tryComplete` call.
   
   ```
           if (curr.isCompleted) {
             // another thread has completed this operation, just remove it
             iter.remove()
           } else if (curr.safeTryComplete()) {
   ```



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {
+            return true;
+        }
 
         topicPartitionDataFromTryComplete = acquirablePartitions();
 
-        if (!topicPartitionDataFromTryComplete.isEmpty())
-            return forceComplete();
-        log.info("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +
+        if (!topicPartitionDataFromTryComplete.isEmpty()) {
+            boolean result = forceComplete();
+            // If invocation of forceComplete is not successful, then that means the request is already completed
+            // hence release the acquired locks.
+            if (!result) {
+                releasePartitionLocks(shareFetchData.groupId(), topicPartitionDataFromTryComplete.keySet());
+            }
+            return result;
+        }
+        log.trace("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +

Review Comment:
   I have removed this log line, we can check the purgatory metric to see waiting requests.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {
+            return true;
+        }
 
         topicPartitionDataFromTryComplete = acquirablePartitions();
 
-        if (!topicPartitionDataFromTryComplete.isEmpty())
-            return forceComplete();
-        log.info("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +
+        if (!topicPartitionDataFromTryComplete.isEmpty()) {
+            boolean result = forceComplete();
+            // If invocation of forceComplete is not successful, then that means the request is already completed
+            // hence release the acquired locks.
+            if (!result) {
+                releasePartitionLocks(shareFetchData.groupId(), topicPartitionDataFromTryComplete.keySet());
+            }
+            return result;
+        }
+        log.trace("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +

Review Comment:
   yeah, I think I made it INFO at the beginning because I was testing the purgatory stuff, but going forward we will make the logs as trace/debug. I forgot to change it to DEBUG when it got 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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   Ohh, I added a log inside the method and can see a lot request lands 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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   So I rechecked and added log line to see the tryComplete is being called even when `completed` is true. 
   
   Here is my understanding, I can see in DelayedOperation.scala:
   
   1. tryComplete() is always executed safely i.e. from `safeTryComplete` or `safeTryCompleteOrElse` which takes a lock on DelayedOperation itself hence no 2 threads can execute `tryComplete()` simultaneously. Correct?
   2. You are right that `tryCompleteWatched` has `completed` check already. But the issue does exist.
   
   This triggers only when there are multiple share consumers for same group and same topic partition. I have traced the calls and can find following: the calls originates from `addToActionQueue` defined in `onCompleted` of DelayedShareFetch. Though the request goes through `tryCompletedWatch` but then again the `tryComplete` is called despite completed. The conditional variable in DelayedOperation etc. seems fine to me. Not sure how it triggers.
   
   ```
   [2024-10-25 17:34:32,670] INFO Share fetch request for group SG1, member hYDnbPjATHqM7uFXBGYKTw is already completed (kafka.server.share.DelayedShareFetch)
   [2024-10-25 17:34:32,703] INFO Share fetch request for group SG1, member hYDnbPjATHqM7uFXBGYKTw is already completed (kafka.server.share.DelayedShareFetch)
   [2024-10-25 17:34:32,754] INFO Share fetch request for group SG1, member hYDnbPjATHqM7uFXBGYKTw is already completed (kafka.server.share.DelayedShareFetch)
   [2024-10-25 17:34:33,191] INFO Share fetch request for group SG1, member hYDnbPjATHqM7uFXBGYKTw is already completed (kafka.server.share.DelayedShareFetch)
   [2024-10-25 17:34:33,391] INFO Share fetch request for group SG1, member OE4al-DNR6C8u3tiD05rGQ is already completed (kafka.server.share.DelayedShareFetch)
   [2024-10-25 17:34:33,682] INFO Share fetch request for group SG1, member OE4al-DNR6C8u3tiD05rGQ is already completed (kafka.server.share.DelayedShareFetch)
   [2024-10-25 17:34:34,363] INFO Share fetch request for group SG1, member OE4al-DNR6C8u3tiD05rGQ is already completed (kafka.server.share.DelayedShareFetch)



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   I have added tests.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,17 +149,28 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {
+            log.trace("Share fetch request for group {}, member {} is already completed",
+                shareFetchData.groupId(), shareFetchData.memberId());
+            return true;
+        }
 
         topicPartitionDataFromTryComplete = acquirablePartitions();
 
-        if (!topicPartitionDataFromTryComplete.isEmpty())
-            return forceComplete();
-        log.info("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +
-                "topic partitions {}", shareFetchData.groupId(),
-                shareFetchData.memberId(), shareFetchData.partitionMaxBytes().keySet());
+        if (!topicPartitionDataFromTryComplete.isEmpty()) {
+            boolean result = forceComplete();

Review Comment:
   Done.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   Yeah make more sense to move to DelayedOperation. Done.



-- 
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-17848: Fixing NPE in delayed share fetch [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -79,16 +79,16 @@ public void onExpiration() {
      */
     @Override
     public void onComplete() {
-        log.trace("Completing the delayed share fetch request for group {}, member {}, " +
-                        "topic partitions {}", shareFetchData.groupId(),
-            shareFetchData.memberId(), shareFetchData.partitionMaxBytes().keySet());
+        log.trace("Completing the delayed share fetch request for group {}, member {}, "
+            + "topic partitions {}", shareFetchData.groupId(), shareFetchData.memberId(),
+            topicPartitionDataFromTryComplete != null ? topicPartitionDataFromTryComplete.keySet() : "null");
 
         if (shareFetchData.future().isDone())
             return;
 
         Map<TopicIdPartition, FetchRequest.PartitionData> topicPartitionData;
         // tryComplete did not invoke forceComplete, so we need to check if we have any partitions to fetch.
-        if (topicPartitionDataFromTryComplete.isEmpty())
+        if (topicPartitionDataFromTryComplete == null || topicPartitionDataFromTryComplete.isEmpty())

Review Comment:
   I read one thing wrong and instead of null it'a always empty. Re-checking and verifying why this fixed consistently with this PR change, I can easily reproduce the issue without this change and can never with. I will update.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {
+            return true;
+        }
 
         topicPartitionDataFromTryComplete = acquirablePartitions();
 
-        if (!topicPartitionDataFromTryComplete.isEmpty())
-            return forceComplete();
-        log.info("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +
+        if (!topicPartitionDataFromTryComplete.isEmpty()) {
+            boolean result = forceComplete();
+            // If invocation of forceComplete is not successful, then that means the request is already completed
+            // hence release the acquired locks.
+            if (!result) {
+                releasePartitionLocks(shareFetchData.groupId(), topicPartitionDataFromTryComplete.keySet());
+            }
+            return result;
+        }
+        log.trace("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +

Review Comment:
   The frequency of this log line is phenomenal. I wonder whether it's really helpful or just likely to flood the logs to the extent that it's impossible to see anything else.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/scala/kafka/server/DelayedOperation.scala:
##########
@@ -119,7 +119,12 @@ abstract class DelayedOperation(delayMs: Long,
   /**
    * Thread-safe variant of tryComplete()
    */
-  private[server] def safeTryComplete(): Boolean = inLock(lock)(tryComplete())
+  private[server] def safeTryComplete(): Boolean = inLock(lock) {
+    if (isCompleted)
+      true

Review Comment:
   > If yes execute the completion logic by calling
   >  forceComplete() and return true iff forceComplete returns true; otherwise return false
   
   This is the return value definition. So if the request is completed, we should return `false`.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   Can we also add unit tests for the conditions to verify this line and line 164



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -182,22 +194,28 @@ Map<TopicIdPartition, FetchRequest.PartitionData> acquirablePartitions() {
             // Add the share partition to the list of partitions to be fetched only if we can
             // acquire the fetch lock on it.
             if (sharePartition.maybeAcquireFetchLock()) {
-                // If the share partition is already at capacity, we should not attempt to fetch.
-                if (sharePartition.canAcquireRecords()) {
-                    topicPartitionData.put(
+                try {
+                    // If the share partition is already at capacity, we should not attempt to fetch.
+                    if (sharePartition.canAcquireRecords()) {
+                        topicPartitionData.put(
                             topicIdPartition,
                             new FetchRequest.PartitionData(
-                                    topicIdPartition.topicId(),
-                                    sharePartition.nextFetchOffset(),
-                                    0,
-                                    partitionMaxBytes,
-                                    Optional.empty()
+                                topicIdPartition.topicId(),
+                                sharePartition.nextFetchOffset(),
+                                0,
+                                partitionMaxBytes,
+                                Optional.empty()
                             )
-                    );
-                } else {
-                    sharePartition.releaseFetchLock();
-                    log.trace("Record lock partition limit exceeded for SharePartition {}, " +
+                        );
+                    } else {
+                        sharePartition.releaseFetchLock();
+                        log.trace("Record lock partition limit exceeded for SharePartition {}, " +
                             "cannot acquire more records", sharePartition);
+                    }
+                } catch (Exception e) {

Review Comment:
   I have added the exception block so if there is any unrealized exception then atleast lock should be released.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -79,16 +79,16 @@ public void onExpiration() {
      */
     @Override
     public void onComplete() {
-        log.trace("Completing the delayed share fetch request for group {}, member {}, " +
-                        "topic partitions {}", shareFetchData.groupId(),
-            shareFetchData.memberId(), shareFetchData.partitionMaxBytes().keySet());
+        log.trace("Completing the delayed share fetch request for group {}, member {}, "
+            + "topic partitions {}", shareFetchData.groupId(), shareFetchData.memberId(),
+            topicPartitionDataFromTryComplete != null ? topicPartitionDataFromTryComplete.keySet() : "null");
 
         if (shareFetchData.future().isDone())
             return;
 
         Map<TopicIdPartition, FetchRequest.PartitionData> topicPartitionData;
         // tryComplete did not invoke forceComplete, so we need to check if we have any partitions to fetch.
-        if (topicPartitionDataFromTryComplete.isEmpty())
+        if (topicPartitionDataFromTryComplete == null || topicPartitionDataFromTryComplete.isEmpty())

Review Comment:
   I have added tests.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   But once it's in `tryComplete` with `isCompleted` as true of DelayedShareFetch then never again it arrives in `tryComplete` of that DelayedShareFetch.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/scala/kafka/server/DelayedOperation.scala:
##########
@@ -119,7 +119,12 @@ abstract class DelayedOperation(delayMs: Long,
   /**
    * Thread-safe variant of tryComplete()
    */
-  private[server] def safeTryComplete(): Boolean = inLock(lock)(tryComplete())
+  private[server] def safeTryComplete(): Boolean = inLock(lock) {
+    if (isCompleted)
+      true

Review Comment:
   Yeah, you are right. As the request is already completed then it should return false as other thread should have already bumped the `completed` count. And it should be removed from iteration once watched sees it completed. I made the 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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,17 +149,28 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {
+            log.trace("Share fetch request for group {}, member {} is already completed",
+                shareFetchData.groupId(), shareFetchData.memberId());
+            return true;
+        }
 
         topicPartitionDataFromTryComplete = acquirablePartitions();
 
-        if (!topicPartitionDataFromTryComplete.isEmpty())
-            return forceComplete();
-        log.info("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +
-                "topic partitions {}", shareFetchData.groupId(),
-                shareFetchData.memberId(), shareFetchData.partitionMaxBytes().keySet());
+        if (!topicPartitionDataFromTryComplete.isEmpty()) {
+            boolean result = forceComplete();

Review Comment:
   result => completedByMe?



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   Yeah, let me do that tomorrow. I have tested the runs manually and verified with 25 parallel share consumers and 25 million messages.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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

   @junrao @adixitconfluent @AndrewJSchofield Can I please get a re-review.


-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {

Review Comment:
   This is unnecessary since the caller `DelayedOperationPurgatory.Watchers.tryCompleteWatched` already does this.



##########
core/src/main/java/kafka/server/share/SharePartition.java:
##########
@@ -1807,16 +1807,15 @@ && checkForStartOffsetWithinBatch(inFlightBatch.firstOffset(), inFlightBatch.las
                     // Even if write share group state RPC call fails, we will still go ahead with the state transition.
                     // Update the cached state and start and end offsets after releasing the acquisition lock on timeout.
                     maybeUpdateCachedStateAndOffsets();
-
-                    // If we have an acquisition lock timeout for a share-partition, then we should check if
-                    // there is a pending share fetch request for the share-partition and complete it.
-                    DelayedShareFetchKey delayedShareFetchKey = new DelayedShareFetchGroupKey(groupId, topicIdPartition.topicId(), topicIdPartition.partition());
-                    replicaManager.completeDelayedShareFetchRequest(delayedShareFetchKey);
                 });
             }
         } finally {
             lock.writeLock().unlock();
         }
+        // If we have an acquisition lock timeout for a share-partition, then we should check if
+        // there is a pending share fetch request for the share-partition and complete it.
+        DelayedShareFetchKey delayedShareFetchKey = new DelayedShareFetchGroupKey(groupId, topicIdPartition.topicId(), topicIdPartition.partition());

Review Comment:
   Should we call this under `if (!stateBatches.isEmpty())`?



##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -182,22 +194,28 @@ Map<TopicIdPartition, FetchRequest.PartitionData> acquirablePartitions() {
             // Add the share partition to the list of partitions to be fetched only if we can
             // acquire the fetch lock on it.
             if (sharePartition.maybeAcquireFetchLock()) {
-                // If the share partition is already at capacity, we should not attempt to fetch.
-                if (sharePartition.canAcquireRecords()) {
-                    topicPartitionData.put(
+                try {
+                    // If the share partition is already at capacity, we should not attempt to fetch.
+                    if (sharePartition.canAcquireRecords()) {
+                        topicPartitionData.put(
                             topicIdPartition,
                             new FetchRequest.PartitionData(
-                                    topicIdPartition.topicId(),
-                                    sharePartition.nextFetchOffset(),
-                                    0,
-                                    partitionMaxBytes,
-                                    Optional.empty()
+                                topicIdPartition.topicId(),
+                                sharePartition.nextFetchOffset(),
+                                0,
+                                partitionMaxBytes,
+                                Optional.empty()
                             )
-                    );
-                } else {
-                    sharePartition.releaseFetchLock();
-                    log.trace("Record lock partition limit exceeded for SharePartition {}, " +
+                        );
+                    } else {
+                        sharePartition.releaseFetchLock();
+                        log.trace("Record lock partition limit exceeded for SharePartition {}, " +
                             "cannot acquire more records", sharePartition);
+                    }
+                } catch (Exception e) {

Review Comment:
   Where is the exception coming from?



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/SharePartition.java:
##########
@@ -1807,16 +1807,15 @@ && checkForStartOffsetWithinBatch(inFlightBatch.firstOffset(), inFlightBatch.las
                     // Even if write share group state RPC call fails, we will still go ahead with the state transition.
                     // Update the cached state and start and end offsets after releasing the acquisition lock on timeout.
                     maybeUpdateCachedStateAndOffsets();
-
-                    // If we have an acquisition lock timeout for a share-partition, then we should check if
-                    // there is a pending share fetch request for the share-partition and complete it.
-                    DelayedShareFetchKey delayedShareFetchKey = new DelayedShareFetchGroupKey(groupId, topicIdPartition.topicId(), topicIdPartition.partition());
-                    replicaManager.completeDelayedShareFetchRequest(delayedShareFetchKey);
                 });
             }
         } finally {
             lock.writeLock().unlock();
         }
+        // If we have an acquisition lock timeout for a share-partition, then we should check if
+        // there is a pending share fetch request for the share-partition and complete it.
+        DelayedShareFetchKey delayedShareFetchKey = new DelayedShareFetchGroupKey(groupId, topicIdPartition.topicId(), topicIdPartition.partition());

Review Comment:
   My bad, corrected 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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -79,16 +79,16 @@ public void onExpiration() {
      */
     @Override
     public void onComplete() {
-        log.trace("Completing the delayed share fetch request for group {}, member {}, " +
-                        "topic partitions {}", shareFetchData.groupId(),
-            shareFetchData.memberId(), shareFetchData.partitionMaxBytes().keySet());
+        log.trace("Completing the delayed share fetch request for group {}, member {}, "
+            + "topic partitions {}", shareFetchData.groupId(), shareFetchData.memberId(),
+            topicPartitionDataFromTryComplete != null ? topicPartitionDataFromTryComplete.keySet() : "null");
 
         if (shareFetchData.future().isDone())
             return;
 
         Map<TopicIdPartition, FetchRequest.PartitionData> topicPartitionData;
         // tryComplete did not invoke forceComplete, so we need to check if we have any partitions to fetch.
-        if (topicPartitionDataFromTryComplete.isEmpty())
+        if (topicPartitionDataFromTryComplete == null || topicPartitionDataFromTryComplete.isEmpty())

Review Comment:
   I have added tests.



-- 
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-17848: Fixing share purgatory request and locks handling [kafka]

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


##########
core/src/main/java/kafka/server/share/DelayedShareFetch.java:
##########
@@ -149,15 +149,27 @@ public void onComplete() {
      */
     @Override
     public boolean tryComplete() {
-        log.trace("Try to complete the delayed share fetch request for group {}, member {}, topic partitions {}",
-            shareFetchData.groupId(), shareFetchData.memberId(),
-            shareFetchData.partitionMaxBytes().keySet());
+        // There can be multiple threads which might invoke tryComplete for same share fetch request
+        // hence check if delay share fetch request is already completed. If yes, return true.
+        // However, this check alone cannot guarantee that request is really completed. It is possible that
+        // tryComplete is invoked by multiple threads and state has yet not updated. Hence, we need to check
+        // the forceComplete response as well.
+        if (isCompleted()) {
+            return true;
+        }
 
         topicPartitionDataFromTryComplete = acquirablePartitions();
 
-        if (!topicPartitionDataFromTryComplete.isEmpty())
-            return forceComplete();
-        log.info("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +
+        if (!topicPartitionDataFromTryComplete.isEmpty()) {
+            boolean result = forceComplete();
+            // If invocation of forceComplete is not successful, then that means the request is already completed
+            // hence release the acquired locks.
+            if (!result) {
+                releasePartitionLocks(shareFetchData.groupId(), topicPartitionDataFromTryComplete.keySet());
+            }
+            return result;
+        }
+        log.trace("Can't acquire records for any partition in the share fetch request for group {}, member {}, " +

Review Comment:
   Thanks. That seems much more appropriate.



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