Improve AbstractLeader tracker removal 58/14458/3
authorRobert Varga <rovarga@cisco.com>
Fri, 23 Jan 2015 18:57:47 +0000 (19:57 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 23 Jan 2015 22:36:26 +0000 (23:36 +0100)
The remove operation performs a linear lookup by index and perform a
second iteration to find the object again. Instead of that, instantiate
an iterator and use its remove() method to elide the second lookup.

Change-Id: Ida2a5c327115d2da787e397311c0a0c5a46a0d43
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java

index eba5e7b069769fb337e44d4096619a29430d057d..ab11c5bd845d5926fb143f6703c8e7f2df7caad6 100644 (file)
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -84,7 +85,7 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior {
 
     private Cancellable heartbeatSchedule = null;
 
-    private List<ClientRequestTracker> trackerList = new ArrayList<>();
+    private final Collection<ClientRequestTracker> trackerList = new ArrayList<>();
 
     protected final int minReplicationCount;
 
@@ -230,13 +231,16 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior {
 
     @Override
     protected ClientRequestTracker removeClientRequestTracker(long logIndex) {
-
-        ClientRequestTracker toRemove = findClientRequestTracker(logIndex);
-        if(toRemove != null) {
-            trackerList.remove(toRemove);
+        final Iterator<ClientRequestTracker> it = trackerList.iterator();
+        while (it.hasNext()) {
+            final ClientRequestTracker t = it.next();
+            if (t.getIndex() == logIndex) {
+                it.remove();
+                return t;
+            }
         }
 
-        return toRemove;
+        return null;
     }
 
     @Override