You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by jq...@apache.org on 2018/01/30 18:20:19 UTC

[kafka] branch trunk updated: KAFKA-6345; Keep a separate count of in-flight requests to avoid ConcurrentModificationException.

This is an automated email from the ASF dual-hosted git repository.

jqin pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new d97e63b  KAFKA-6345; Keep a separate count of in-flight requests to avoid ConcurrentModificationException.
d97e63b is described below

commit d97e63b26063c8fe629005ff61c8150317c88503
Author: Sean McCauliff <sm...@linkedin.com>
AuthorDate: Tue Jan 30 10:20:08 2018 -0800

    KAFKA-6345; Keep a separate count of in-flight requests to avoid ConcurrentModificationException.
    
    This keeps a separate count of the number of in flight requests so that sensor threads will not need to deal with ConcurrentModfiicationException.
    
    This would probably still be correct with volatile rather than AtomicInteger, but FindBugs flags the use of volatile as the count is incremented and decremented.
    
    Author: Sean McCauliff <sm...@linkedin.com>
    
    Reviewers: Jiangjie (Becket) Qin <be...@gmail.com>
    
    Closes #4460 from smccauliff/KAFKA-6345
---
 .../org/apache/kafka/clients/InFlightRequests.java | 30 +++++++----
 .../apache/kafka/clients/InFlightRequestsTest.java | 60 ++++++++++++++++++++++
 2 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/clients/src/main/java/org/apache/kafka/clients/InFlightRequests.java b/clients/src/main/java/org/apache/kafka/clients/InFlightRequests.java
index 3689a09..a062818 100644
--- a/clients/src/main/java/org/apache/kafka/clients/InFlightRequests.java
+++ b/clients/src/main/java/org/apache/kafka/clients/InFlightRequests.java
@@ -23,6 +23,8 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
 
 /**
  * The set of requests which have been sent or are being sent but haven't yet received a response
@@ -31,6 +33,8 @@ final class InFlightRequests {
 
     private final int maxInFlightRequestsPerConnection;
     private final Map<String, Deque<NetworkClient.InFlightRequest>> requests = new HashMap<>();
+    /** Thread safe total number of in flight requests. */
+    private final AtomicInteger inFlightRequestCount = new AtomicInteger(0);
 
     public InFlightRequests(int maxInFlightRequestsPerConnection) {
         this.maxInFlightRequestsPerConnection = maxInFlightRequestsPerConnection;
@@ -47,6 +51,7 @@ final class InFlightRequests {
             this.requests.put(destination, reqs);
         }
         reqs.addFirst(request);
+        inFlightRequestCount.incrementAndGet();
     }
 
     /**
@@ -63,7 +68,9 @@ final class InFlightRequests {
      * Get the oldest request (the one that will be completed next) for the given node
      */
     public NetworkClient.InFlightRequest completeNext(String node) {
-        return requestQueue(node).pollLast();
+        NetworkClient.InFlightRequest inFlightRequest = requestQueue(node).pollLast();
+        inFlightRequestCount.decrementAndGet();
+        return inFlightRequest;
     }
 
     /**
@@ -80,7 +87,9 @@ final class InFlightRequests {
      * @return The request
      */
     public NetworkClient.InFlightRequest completeLastSent(String node) {
-        return requestQueue(node).pollFirst();
+        NetworkClient.InFlightRequest inFlightRequest = requestQueue(node).pollFirst();
+        inFlightRequestCount.decrementAndGet();
+        return inFlightRequest;
     }
 
     /**
@@ -114,13 +123,10 @@ final class InFlightRequests {
     }
 
     /**
-     * Count all in-flight requests for all nodes
+     * Count all in-flight requests for all nodes. This method is thread safe, but may lag the actual count.
      */
     public int count() {
-        int total = 0;
-        for (Deque<NetworkClient.InFlightRequest> deque : this.requests.values())
-            total += deque.size();
-        return total;
+        return inFlightRequestCount.get();
     }
 
     /**
@@ -141,8 +147,14 @@ final class InFlightRequests {
      * @return All the in-flight requests for that node that have been removed
      */
     public Iterable<NetworkClient.InFlightRequest> clearAll(String node) {
-        Deque<NetworkClient.InFlightRequest> reqs = requests.remove(node);
-        return (reqs == null) ? Collections.<NetworkClient.InFlightRequest>emptyList() : reqs;
+        Deque<NetworkClient.InFlightRequest> reqs = requests.get(node);
+        if (reqs == null) {
+            return Collections.emptyList();
+        } else {
+            Deque<NetworkClient.InFlightRequest> clearedRequests = requests.remove(node);
+            inFlightRequestCount.getAndAdd(-clearedRequests.size());
+            return clearedRequests;
+        }
     }
 
     /**
diff --git a/clients/src/test/java/org/apache/kafka/clients/InFlightRequestsTest.java b/clients/src/test/java/org/apache/kafka/clients/InFlightRequestsTest.java
new file mode 100644
index 0000000..e00ca08
--- /dev/null
+++ b/clients/src/test/java/org/apache/kafka/clients/InFlightRequestsTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.clients;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class InFlightRequestsTest {
+
+    private InFlightRequests inFlightRequests;
+    @Before
+    public void setup() {
+        inFlightRequests = new InFlightRequests(12);
+        NetworkClient.InFlightRequest ifr =
+                new NetworkClient.InFlightRequest(null, 0, "dest", null, false, false, null, null, 0);
+        inFlightRequests.add(ifr);
+    }
+
+    @Test
+    public void checkIncrementAndDecrementOnLastSent() {
+        assertEquals(1, inFlightRequests.count());
+
+        inFlightRequests.completeLastSent("dest");
+        assertEquals(0, inFlightRequests.count());
+    }
+
+    @Test
+    public void checkDecrementOnClear() {
+        inFlightRequests.clearAll("dest");
+        assertEquals(0, inFlightRequests.count());
+    }
+
+    @Test
+    public void checkDecrementOnCompleteNext() {
+        inFlightRequests.completeNext("dest");
+        assertEquals(0, inFlightRequests.count());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void throwExceptionOnNeverBeforeSeenNode() {
+        inFlightRequests.completeNext("not-added");
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
jqin@apache.org.