Repository: searchbox-io/Jest
Pull request #459 (2017-02-01 12:26:24)

NodeChecker to support v5


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java b/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java
index ebf5a522..9d4322c9 100644
--- a/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java
+++ b/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java
@@ -1,5 +1,6 @@
 package io.searchbox.client.config.discovery;
 
+import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.util.concurrent.AbstractScheduledService;
 import com.google.common.util.concurrent.MoreExecutors;
@@ -34,6 +35,7 @@ public class NodeChecker extends AbstractScheduledService {
 
     private final static Logger log = LoggerFactory.getLogger(NodeChecker.class);
     private final static String PUBLISH_ADDRESS_KEY = "http_address";
+    private final static String PUBLISH_ADDRESS_KEY_V5 = "publish_address"; // The one that under "http" node
     private final static Pattern INETSOCKETADDRESS_PATTERN = Pattern.compile("(?:inet\\[)?(?:(?:[^:]+)?\\/)?([^:]+):(\\d+)\\]?");
 
     private final NodesInfo action;
@@ -87,17 +89,28 @@ public class NodeChecker extends AbstractScheduledService {
             JsonObject nodes = (JsonObject) jsonMap.get("nodes");
             if (nodes != null) {
                 for (Entry<String, JsonElement> entry : nodes.entrySet()) {
+
                     JsonObject host = entry.getValue().getAsJsonObject();
+                    JsonElement addressElement = null;
+                    if (host.has("version")) {
+                        int majorVersion = Integer.parseInt(Splitter.on('.').splitToList(host.get("version").getAsString()).get(0));
 
-                    // get as a JsonElement first as some nodes in the cluster may not have an http_address
-                    if (host.has(PUBLISH_ADDRESS_KEY)) {
-                        JsonElement addressElement = host.get(PUBLISH_ADDRESS_KEY);
-                        if (!addressElement.isJsonNull()) {
-                            String httpAddress = getHttpAddress(addressElement.getAsString());
-                            if(httpAddress != null) httpHosts.add(httpAddress);
+                        if (majorVersion >= 5) {
+                            JsonObject http = host.getAsJsonObject("http");
+                            if (http.has(PUBLISH_ADDRESS_KEY_V5)) addressElement = http.get(PUBLISH_ADDRESS_KEY_V5);
                         }
                     }
-                }
+
+                    if (addressElement == null) {
+                        // get as a JsonElement first as some nodes in the cluster may not have an http_address
+                        if (host.has(PUBLISH_ADDRESS_KEY)) addressElement = host.get(PUBLISH_ADDRESS_KEY);
+                    }
+
+                    if (addressElement != null && !addressElement.isJsonNull()) {
+                        String httpAddress = getHttpAddress(addressElement.getAsString());
+                        if(httpAddress != null) httpHosts.add(httpAddress);
+                    }
+              }
             }
             if (log.isDebugEnabled()) {
                 log.debug("Discovered {} HTTP hosts: {}", httpHosts.size(), StringUtils.join(httpHosts, ","));


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java b/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java
index 5b191b19..ad98e4a8 100644
--- a/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java
+++ b/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java
@@ -73,6 +73,35 @@ public class NodeCheckerTest {
         assertEquals("https://192.168.2.7:9200", servers.iterator().next());
     }
 
+    @Test
+    public void testEsVersion5() throws Exception {
+        NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig);
+
+        JestResult result = new JestResult(new Gson());
+        result.setJsonMap(ImmutableMap.<String, Object>of(
+                "ok", "true",
+                "nodes", ImmutableMap.of(
+                        "node_name", ImmutableMap.of(
+                                "version",      "5.0.1",
+                                "http",             ImmutableMap.of("publish_address",  "192.168.2.7:9200")
+                        )
+                )
+        ));
+        result.setSucceeded(true);
+        when(jestClient.execute(isA(Action.class))).thenReturn(result);
+
+        nodeChecker.runOneIteration();
+
+        verify(jestClient).execute(isA(Action.class));
+        ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class);
+        verify(jestClient).setServers(argument.capture());
+        verifyNoMoreInteractions(jestClient);
+
+        Set servers = argument.getValue();
+        assertEquals(1, servers.size());
+        assertEquals("http://192.168.2.7:9200", servers.iterator().next());
+    }
+
     @Test
     public void testWithResolvedWithoutHostnameAddress() throws Exception {
         NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig);