chia7712 commented on code in PR #16753:
URL: https://github.com/apache/kafka/pull/16753#discussion_r1703058596


##########
clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java:
##########
@@ -1196,16 +1196,31 @@ private long maybeDrainPendingCalls(long now) {
             long pollTimeout = Long.MAX_VALUE;
             log.trace("Trying to choose nodes for {} at {}", pendingCalls, 
now);
 
-            Iterator<Call> pendingIter = pendingCalls.iterator();
-            while (pendingIter.hasNext()) {
-                Call call = pendingIter.next();
+            List<Call> toRemove = new ArrayList<>();
+            // Using for-loop instead of iterator to avoid 
ConcurrentModificationException with following sequence:
+            // 1. In maybeDrainPendingCalls, pendingCalls.iterator() create a 
iterator.
+            // 2. In maybeDrainPendingCall, call.nodeProvider.provide() throws 
UnsupportedVersionException error.
+            // 3. In call.fail, runnable.pendingCalls.add modify the 
pendingCalls list.
+            // Using pendingCalls.size() to get the list size before the 
for-loop to avoid infinite loop.
+            // If call.fail keeps adding the call to pendingCalls,
+            // the loop like for (int i = 0; i < pendingCalls.size(); i++) 
can't stop.
+            int pendingSize = pendingCalls.size();
+            for (int i = 0; i < pendingSize; i++) {
+                Call call = pendingCalls.get(i);
                 // If the call is being retried, await the proper backoff 
before finding the node
                 if (now < call.nextAllowedTryMs) {
                     pollTimeout = Math.min(pollTimeout, call.nextAllowedTryMs 
- now);
-                } else if (maybeDrainPendingCall(call, now)) {
-                    pendingIter.remove();
+                    continue;
+                }
+                if (maybeDrainPendingCall(call, now)) {

Review Comment:
   If we change it from `else if` to `if`, it will drain the pending call even 
though the call is not being retried. why we need this change?



##########
clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java:
##########
@@ -1196,16 +1196,31 @@ private long maybeDrainPendingCalls(long now) {
             long pollTimeout = Long.MAX_VALUE;
             log.trace("Trying to choose nodes for {} at {}", pendingCalls, 
now);
 
-            Iterator<Call> pendingIter = pendingCalls.iterator();
-            while (pendingIter.hasNext()) {
-                Call call = pendingIter.next();
+            List<Call> toRemove = new ArrayList<>();
+            // Using for-loop instead of iterator to avoid 
ConcurrentModificationException with following sequence:
+            // 1. In maybeDrainPendingCalls, pendingCalls.iterator() create a 
iterator.

Review Comment:
   Maybe we can simply say "pendingCalls could be modified in this loop, hence 
...."?



##########
clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java:
##########
@@ -1196,16 +1196,31 @@ private long maybeDrainPendingCalls(long now) {
             long pollTimeout = Long.MAX_VALUE;
             log.trace("Trying to choose nodes for {} at {}", pendingCalls, 
now);
 
-            Iterator<Call> pendingIter = pendingCalls.iterator();
-            while (pendingIter.hasNext()) {
-                Call call = pendingIter.next();
+            List<Call> toRemove = new ArrayList<>();
+            // Using for-loop instead of iterator to avoid 
ConcurrentModificationException with following sequence:
+            // 1. In maybeDrainPendingCalls, pendingCalls.iterator() create a 
iterator.
+            // 2. In maybeDrainPendingCall, call.nodeProvider.provide() throws 
UnsupportedVersionException error.
+            // 3. In call.fail, runnable.pendingCalls.add modify the 
pendingCalls list.
+            // Using pendingCalls.size() to get the list size before the 
for-loop to avoid infinite loop.
+            // If call.fail keeps adding the call to pendingCalls,
+            // the loop like for (int i = 0; i < pendingCalls.size(); i++) 
can't stop.

Review Comment:
   It seems to me the loop can stop but we don't want to verify the re-enqueue 
call right now, right?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to