From ad4463b6bec34f654c4cd09c7c7a1831d3621061 Mon Sep 17 00:00:00 2001 From: hebelala Date: Mon, 17 Jan 2022 17:27:49 +0800 Subject: [PATCH] RoundRobinLoadBalancer enhancement (#357) * send yield command as ping, for fail fast * use ConcurrentHashMap instead of HashMap, to avoid volatile problem --- .../nebula/client/graph/net/RoundRobinLoadBalancer.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/com/vesoft/nebula/client/graph/net/RoundRobinLoadBalancer.java b/client/src/main/java/com/vesoft/nebula/client/graph/net/RoundRobinLoadBalancer.java index 9dd055e58..a656624e6 100644 --- a/client/src/main/java/com/vesoft/nebula/client/graph/net/RoundRobinLoadBalancer.java +++ b/client/src/main/java/com/vesoft/nebula/client/graph/net/RoundRobinLoadBalancer.java @@ -5,9 +5,9 @@ import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException; import com.vesoft.nebula.client.graph.exception.IOErrorException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -20,7 +20,7 @@ public class RoundRobinLoadBalancer implements LoadBalancer { private static final int S_OK = 0; private static final int S_BAD = 1; private final List addresses = new ArrayList<>(); - private final Map serversStatus = new HashMap<>(); + private final Map serversStatus = new ConcurrentHashMap<>(); private final int timeout; private final AtomicInteger pos = new AtomicInteger(0); private final int delayTime = 60; // unit seconds @@ -80,8 +80,9 @@ public boolean ping(HostAddress addr) { } else { connection.open(addr, this.timeout); } + boolean pong = connection.ping(); connection.close(); - return true; + return pong; } catch (IOErrorException | ClientServerIncompatibleException e) { return false; }