You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2016/02/10 13:57:11 UTC

[4/7] incubator-tinkerpop git commit: Use an AtomicReference to protect the reconnect task, rather than synchronizing on it.

Use an AtomicReference<Boolean> to protect the reconnect task, rather than synchronizing on it.

TINKERPOP-1126


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/a3264648
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/a3264648
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/a3264648

Branch: refs/heads/TINKERPOP-1125-to-1127
Commit: a3264648a41e4db95cc95d5425a61ea8e07b7e64
Parents: cced242
Author: Kieran Sherlock <ki...@identitymind.com>
Authored: Tue Feb 9 17:10:59 2016 -0800
Committer: Kieran Sherlock <ki...@identitymind.com>
Committed: Tue Feb 9 20:42:41 2016 -0800

----------------------------------------------------------------------
 .../apache/tinkerpop/gremlin/driver/Host.java   | 24 +++++++++++---------
 1 file changed, 13 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/a3264648/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Host.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Host.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Host.java
index c060b18..34ee719 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Host.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Host.java
@@ -42,7 +42,8 @@ public final class Host {
     private final Cluster cluster;
     private final String hostLabel;
 
-    final AtomicReference<ScheduledFuture<?>> reconnectionAttempt = new AtomicReference<>(null);
+    final AtomicReference<Boolean> retryInProgress = new AtomicReference<>(Boolean.FALSE);
+    ScheduledFuture<?> retryThread = null;
 
     Host(final InetSocketAddress address, final Cluster cluster) {
         this.cluster = cluster;
@@ -71,20 +72,21 @@ public final class Host {
         isAvailable = false;
 
         // only do a connection re-attempt if one is not already in progress
-        synchronized (reconnectionAttempt) {
-            if (reconnectionAttempt.get() == null) {
-                reconnectionAttempt.set(this.cluster.executor().scheduleAtFixedRate(() -> {
-                    logger.debug("Trying to reconnect to dead host at {}", this);
-                    if (reconnect.apply(this))
-                        reconnected();
-                }, cluster.connectionPoolSettings().reconnectInitialDelay, cluster.connectionPoolSettings().reconnectInterval, TimeUnit.MILLISECONDS));
-            }
+        if (retryInProgress.compareAndSet(Boolean.FALSE, Boolean.TRUE)) {
+            retryThread = this.cluster.executor().scheduleAtFixedRate(() -> {
+                                                                          logger.debug("Trying to reconnect to dead host at {}", this);
+                                                                          if (reconnect.apply(this)) reconnected();
+                                                                      }, cluster.connectionPoolSettings().reconnectInitialDelay,
+                                                                      cluster.connectionPoolSettings().reconnectInterval, TimeUnit.MILLISECONDS);
         }
     }
 
     private void reconnected() {
-        reconnectionAttempt.get().cancel(false);
-        reconnectionAttempt.set(null);
+        // race condition!  retry boolean could be set to false, a new retryThread created above
+        // and then cancelled here.   But we're only executing this at all because we *have* reconnected
+        retryThread.cancel(false);
+        retryThread = null;
+        retryInProgress.set(Boolean.FALSE);
         makeAvailable();
     }