You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2017/10/16 08:09:51 UTC

lucene-solr:master: SOLR-11491: Support retrieval of cluster properties in HttpClusterStateProvider.

Repository: lucene-solr
Updated Branches:
  refs/heads/master 55de39bb7 -> fc88e225d


SOLR-11491: Support retrieval of cluster properties in HttpClusterStateProvider.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/fc88e225
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/fc88e225
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/fc88e225

Branch: refs/heads/master
Commit: fc88e225d29f343a3028c43a076fa58318879c48
Parents: 55de39b
Author: Andrzej Bialecki <ab...@apache.org>
Authored: Mon Oct 16 10:09:43 2017 +0200
Committer: Andrzej Bialecki <ab...@apache.org>
Committed: Mon Oct 16 10:09:58 2017 +0200

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  2 +
 .../solrj/impl/HttpClusterStateProvider.java    | 53 +++++++++++++++-----
 2 files changed, 43 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc88e225/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 5405839..0ba1575 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -52,6 +52,8 @@ Bug Fixes
 
 * SOLR-11447: ZkStateWriter should process commands in atomic. (Cao Manh Dat, shalin)
 
+* SOLR-11491: Support retrieval of cluster properties in HttpClusterStateProvider. (ab)
+
 Optimizations
 ----------------------
 * SOLR-11285: Refactor autoscaling framework to avoid direct references to Zookeeper and Solr

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc88e225/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java
index 4ab8671..06518f1 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java
@@ -19,6 +19,7 @@ package org.apache.solr.client.solrj.impl;
 import java.io.IOException;
 import java.lang.invoke.MethodHandles;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -90,7 +91,7 @@ public class HttpClusterStateProvider implements ClusterStateProvider {
       try (HttpSolrClient client = new HttpSolrClient.Builder().
           withBaseSolrUrl(ZkStateReader.getBaseUrlForNodeName(nodeName, urlScheme)).
           withHttpClient(httpClient).build()) {
-        ClusterState cs = fetchClusterState(client, collection);
+        ClusterState cs = fetchClusterState(client, collection, null);
         return cs.getCollectionRef(collection);
       } catch (SolrServerException | RemoteSolrException | IOException e) {
         if (e.getMessage().contains(collection + " not found")) {
@@ -111,7 +112,7 @@ public class HttpClusterStateProvider implements ClusterStateProvider {
   }
 
   @SuppressWarnings({"rawtypes", "unchecked"})
-  private ClusterState fetchClusterState(SolrClient client, String collection) throws SolrServerException, IOException {
+  private ClusterState fetchClusterState(SolrClient client, String collection, Map<String, Object> clusterProperties) throws SolrServerException, IOException {
     ModifiableSolrParams params = new ModifiableSolrParams();
     if (collection != null) {
       params.set("collection", collection);
@@ -120,13 +121,29 @@ public class HttpClusterStateProvider implements ClusterStateProvider {
     QueryRequest request = new QueryRequest(params);
     request.setPath("/admin/collections");
     NamedList cluster = (SimpleOrderedMap) client.request(request).get("cluster");
-    Map<String, Object> collectionsMap = Collections.singletonMap(collection,
-        ((NamedList) cluster.get("collections")).get(collection));
-    int znodeVersion = (int)((Map<String, Object>)(collectionsMap).get(collection)).get("znodeVersion");
+    Map<String, Object> collectionsMap;
+    if (collection != null) {
+      collectionsMap = Collections.singletonMap(collection,
+          ((NamedList) cluster.get("collections")).get(collection));
+    } else {
+      collectionsMap = ((NamedList)cluster.get("collections")).asMap(10);
+    }
+    int znodeVersion;
+    if (collection != null) {
+      znodeVersion =  (int)((Map<String, Object>)(collectionsMap).get(collection)).get("znodeVersion");
+    } else {
+      znodeVersion = -1;
+    }
     Set<String> liveNodes = new HashSet((List<String>)(cluster.get("live_nodes")));
     this.liveNodes = liveNodes;
     liveNodesTimestamp = System.nanoTime();
     ClusterState cs = ClusterState.load(znodeVersion, collectionsMap, liveNodes, ZkStateReader.CLUSTER_STATE);
+    if (clusterProperties != null) {
+      NamedList properties = (NamedList) cluster.get("properties");
+      if (properties != null) {
+        clusterProperties.putAll(properties.asMap(10));
+      }
+    }
     return cs;
   }
 
@@ -235,7 +252,7 @@ public class HttpClusterStateProvider implements ClusterStateProvider {
       try (HttpSolrClient client = new HttpSolrClient.Builder().
           withBaseSolrUrl(ZkStateReader.getBaseUrlForNodeName(nodeName, urlScheme)).
           withHttpClient(httpClient).build()) {
-        ClusterState cs = fetchClusterState(client, null);
+        ClusterState cs = fetchClusterState(client, null, null);
         return cs;
       } catch (SolrServerException | RemoteSolrException | IOException e) {
         log.warn("Attempt to fetch cluster state from " +
@@ -251,9 +268,23 @@ public class HttpClusterStateProvider implements ClusterStateProvider {
 
   @Override
   public Map<String, Object> getClusterProperties() {
-    throw new UnsupportedOperationException("Fetching cluster properties not supported"
-        + " using the HttpClusterStateProvider. "
-        + "ZkClientClusterStateProvider can be used for this."); // TODO
+    for (String nodeName: liveNodes) {
+      try (HttpSolrClient client = new HttpSolrClient.Builder().
+          withBaseSolrUrl(ZkStateReader.getBaseUrlForNodeName(nodeName, urlScheme)).
+          withHttpClient(httpClient).build()) {
+        Map<String, Object> clusterProperties = new HashMap<>();
+        fetchClusterState(client, null, clusterProperties);
+        return clusterProperties;
+      } catch (SolrServerException | RemoteSolrException | IOException e) {
+        log.warn("Attempt to fetch cluster state from " +
+            ZkStateReader.getBaseUrlForNodeName(nodeName, urlScheme) + " failed.", e);
+      }
+    }
+    throw new RuntimeException("Tried fetching cluster state using the node names we knew of, i.e. " + liveNodes +". However, "
+        + "succeeded in obtaining the cluster state from none of them."
+        + "If you think your Solr cluster is up and is accessible,"
+        + " you could try re-creating a new CloudSolrClient using working"
+        + " solrUrl(s) or zkHost(s).");
   }
 
   @Override
@@ -268,9 +299,7 @@ public class HttpClusterStateProvider implements ClusterStateProvider {
     if (propertyName.equals(ZkStateReader.URL_SCHEME)) {
       return this.urlScheme;
     }
-    throw new UnsupportedOperationException("Fetching cluster properties not supported"
-        + " using the HttpClusterStateProvider. "
-        + "ZkClientClusterStateProvider can be used for this."); // TODO
+    return getClusterProperties().get(propertyName);
   }
 
   @Override