hhughes commented on code in PR #1743:
URL: 
https://github.com/apache/cassandra-java-driver/pull/1743#discussion_r1393227394


##########
core/src/main/java/com/datastax/oss/driver/internal/core/loadbalancing/DefaultLoadBalancingPolicy.java:
##########
@@ -96,14 +100,39 @@ public class DefaultLoadBalancingPolicy extends 
BasicLoadBalancingPolicy impleme
   private static final int MAX_IN_FLIGHT_THRESHOLD = 10;
   private static final long RESPONSE_COUNT_RESET_INTERVAL_NANOS = 
MILLISECONDS.toNanos(200);
 
-  protected final Map<Node, AtomicLongArray> responseTimes = new 
ConcurrentHashMap<>();
+  protected final LoadingCache<Node, AtomicLongArray> responseTimes;
   protected final Map<Node, Long> upTimes = new ConcurrentHashMap<>();
   private final boolean avoidSlowReplicas;
 
   public DefaultLoadBalancingPolicy(@NonNull DriverContext context, @NonNull 
String profileName) {
     super(context, profileName);
     this.avoidSlowReplicas =
         
profile.getBoolean(DefaultDriverOption.LOAD_BALANCING_POLICY_SLOW_AVOIDANCE, 
true);
+    CacheLoader<Node, AtomicLongArray> cacheLoader =
+        new CacheLoader<Node, AtomicLongArray>() {
+          @Override
+          public AtomicLongArray load(Node key) throws Exception {
+            // The array stores at most two timestamps, since we don't need 
more;
+            // the first one is always the least recent one, and hence the one 
to inspect.
+            long now = nanoTime();
+            AtomicLongArray array =
+                responseTimes.asMap().containsKey(key) ? 
responseTimes.get(key) : null;
+            if (array == null) {
+              array = new AtomicLongArray(1);
+              array.set(0, now);
+            } else if (array.length() == 1) {
+              long previous = array.get(0);
+              array = new AtomicLongArray(2);
+              array.set(0, previous);
+              array.set(1, now);
+            } else {
+              array.set(0, array.get(1));
+              array.set(1, now);
+            }
+            return array;
+          }
+        };
+    this.responseTimes = 
CacheBuilder.newBuilder().weakValues().build(cacheLoader);

Review Comment:
   `responseTimes` should have `weakKeys` in order to drop entries when `Node` 
objects are finalized



-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to