You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ab...@apache.org on 2021/04/12 15:53:33 UTC
[solr] 01/01: SOLR-15300: Initial patch.
This is an automated email from the ASF dual-hosted git repository.
ab pushed a commit to branch jira/solr-15300
in repository https://gitbox.apache.org/repos/asf/solr.git
commit fbb87b6faaf2b476c997ee3dc3c7b3c8fdd9eb09
Author: Andrzej Bialecki <ab...@apache.org>
AuthorDate: Mon Apr 12 17:53:10 2021 +0200
SOLR-15300: Initial patch.
---
.../apache/solr/handler/admin/ClusterStatus.java | 78 ++++++++++++++++++++--
.../cloud/api/collections/TestCollectionAPI.java | 66 ++++++++++++++++++
2 files changed, 138 insertions(+), 6 deletions(-)
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ClusterStatus.java b/solr/core/src/java/org/apache/solr/handler/admin/ClusterStatus.java
index f95c3fd..b0c51cb 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/ClusterStatus.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/ClusterStatus.java
@@ -48,6 +48,49 @@ public class ClusterStatus {
private final ZkNodeProps message;
private final String collection; // maybe null
+ /** Shard / collection health state. */
+ public enum Health {
+ /** All replicas up, leader exists. */
+ GREEN,
+ /** Some replicas down, leader exists. */
+ YELLOW,
+ /** Many replicas down, leader exists. */
+ ORANGE,
+ /** No leader or most replicas down. */
+ RED;
+
+ public static final float ORANGE_LEVEL = 0.66f;
+ public static final float RED_LEVEL = 0.33f;
+
+ public static Health calcShardState(float fractionReplicasUp, boolean hasLeader) {
+ if (hasLeader) {
+ if (fractionReplicasUp == 1.0f) {
+ return GREEN;
+ } else if (fractionReplicasUp > ORANGE_LEVEL) {
+ return YELLOW;
+ } else if (fractionReplicasUp > RED_LEVEL) {
+ return ORANGE;
+ } else {
+ return RED;
+ }
+ } else {
+ return RED;
+ }
+ }
+
+ /** Combine multiple states into one. Always reports as the worst state. */
+ public static Health combine(Collection<Health> states) {
+ Health res = GREEN;
+ for (Health state : states) {
+ if (state.ordinal() > res.ordinal()) {
+ res = state;
+ }
+ }
+ return res;
+ }
+ }
+
+
public ClusterStatus(ZkStateReader zkStateReader, ZkNodeProps props) {
this.zkStateReader = zkStateReader;
this.message = props;
@@ -131,11 +174,12 @@ public class ClusterStatus {
requestedShards.addAll(Arrays.asList(paramShards));
}
- byte[] bytes = Utils.toJSON(clusterStateCollection);
- Map<String, Object> docCollection = (Map<String, Object>) Utils.fromJSON(bytes);
- collectionStatus = getCollectionStatus(docCollection, name, requestedShards);
+ byte[] bytes = Utils.toJSON(clusterStateCollection);
+ Map<String, Object> docCollection = (Map<String, Object>) Utils.fromJSON(bytes);
+ collectionStatus = getCollectionStatus(docCollection, name, requestedShards);
collectionStatus.put("znodeVersion", clusterStateCollection.getZNodeVersion());
+
if (collectionVsAliases.containsKey(name) && !collectionVsAliases.get(name).isEmpty()) {
collectionStatus.put("aliases", collectionVsAliases.get(name));
}
@@ -248,17 +292,39 @@ public class ClusterStatus {
final Map<String, Map<String,Object>> shards = collection != null
? (Map<String, Map<String,Object>>)collection.getOrDefault("shards", Collections.emptyMap())
: Collections.emptyMap();
- shards.values().forEach(s -> {
+ List<Health> healthStates = new ArrayList<>(shards.size());
+ shards.forEach((shardName, s) -> {
final Map<String, Map<String,Object>> replicas =
(Map<String, Map<String,Object>>)s.getOrDefault("replicas", Collections.emptyMap());
- replicas.values().forEach(r -> {
+ int[] totalVsActive = new int[2];
+ boolean hasLeader = false;
+ for (Map<String, Object> r : replicas.values()) {
+ totalVsActive[0]++;
+ boolean active = false;
+ if (Replica.State.ACTIVE.toString().equals(r.get("state"))) {
+ totalVsActive[1]++;
+ active = true;
+ }
+ if ("true".equals(r.get("leader")) && active) {
+ hasLeader = true;
+ }
String nodeName = (String)r.get(ZkStateReader.NODE_NAME_PROP);
if (nodeName != null) {
// UI needs the base_url set
r.put(ZkStateReader.BASE_URL_PROP, UrlScheme.INSTANCE.getBaseUrlForNodeName(nodeName));
}
- });
+ }
+ float ratioActive;
+ if (totalVsActive[0] == 0) {
+ ratioActive = 0.0f;
+ } else {
+ ratioActive = (float) totalVsActive[1] / totalVsActive[0];
+ }
+ Health health = Health.calcShardState(ratioActive, hasLeader);
+ s.put("health", health.toString());
+ healthStates.add(health);
});
+ collection.put("health", Health.combine(healthStates).toString());
return collection;
}
}
diff --git a/solr/core/src/test/org/apache/solr/cloud/api/collections/TestCollectionAPI.java b/solr/core/src/test/org/apache/solr/cloud/api/collections/TestCollectionAPI.java
index 884ea22..f8f5635 100644
--- a/solr/core/src/test/org/apache/solr/cloud/api/collections/TestCollectionAPI.java
+++ b/solr/core/src/test/org/apache/solr/cloud/api/collections/TestCollectionAPI.java
@@ -22,17 +22,22 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import com.google.common.collect.Lists;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.cloud.SolrCloudManager;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
+import org.apache.solr.client.solrj.impl.SolrClientCloudManager;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.V2Request;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
+import org.apache.solr.cloud.CloudUtil;
import org.apache.solr.cloud.ZkConfigSetService;
import org.apache.solr.cloud.ZkTestServer;
import org.apache.solr.common.SolrException;
@@ -42,10 +47,12 @@ import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.SolrZkClient;
+import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CollectionParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.Utils;
import org.apache.zookeeper.KeeperException;
import org.junit.Test;
@@ -83,6 +90,7 @@ public class TestCollectionAPI extends ReplicaPropertiesBase {
clusterStatusWithCollection();
clusterStatusWithCollectionAndShard();
clusterStatusWithCollectionAndMultipleShards();
+ clusterStatusWithCollectionHealthState();
clusterStatusWithRouteKey();
clusterStatusAliasTest();
clusterStatusRolesTest();
@@ -336,6 +344,64 @@ public class TestCollectionAPI extends ReplicaPropertiesBase {
}
}
+ @SuppressWarnings({"unchecked"})
+ private void clusterStatusWithCollectionHealthState() throws Exception {
+ try (CloudSolrClient client = createCloudClient(null)) {
+ final CollectionAdminRequest.ClusterStatus request = new CollectionAdminRequest.ClusterStatus();
+ request.setCollectionName(COLLECTION_NAME);
+ NamedList<Object> rsp = request.process(client).getResponse();
+ NamedList<Object> cluster = (NamedList<Object>) rsp.get("cluster");
+ assertNotNull("Cluster state should not be null", cluster);
+ Map<String, Object> collection = (Map<String, Object>) Utils.getObjectByPath(cluster, false, "collections/" + COLLECTION_NAME);
+ assertEquals("collection health", "GREEN", collection.get("health"));
+ Map<String, Object> shardStatus = (Map<String, Object>) collection.get("shards");
+ assertEquals(2, shardStatus.size());
+ String health = (String) Utils.getObjectByPath(shardStatus, false, "shard1/health");
+ assertEquals("shard1 health", "GREEN", health);
+ health = (String) Utils.getObjectByPath(shardStatus, false, "shard2/health");
+ assertEquals("shard2 health", "GREEN", health);
+
+ // bring some replicas down
+ JettySolrRunner jetty = chaosMonkey.getShard("shard1", 0);
+ String nodeName = jetty.getNodeName();
+ jetty.stop();
+ ZkStateReader zkStateReader = client.getZkStateReader();
+ zkStateReader.waitForLiveNodes(30, TimeUnit.SECONDS, (o, n) -> n != null && !n.contains(nodeName));
+
+ rsp = request.process(client).getResponse();
+ collection = (Map<String, Object>) Utils.getObjectByPath(rsp, false, "cluster/collections/" + COLLECTION_NAME);
+ assertFalse("collection health should not be GREEN", "GREEN".equals(collection.get("health")));
+ shardStatus = (Map<String, Object>) collection.get("shards");
+ assertEquals(2, shardStatus.size());
+ String health1 = (String) Utils.getObjectByPath(shardStatus, false, "shard1/health");
+ String health2 = (String) Utils.getObjectByPath(shardStatus, false, "shard2/health");
+ assertTrue("shard1=" + health1 + ", shard2=" + health2, !"GREEN".equals(health1) || !"GREEN".equals(health2));
+
+ // bring them up again
+ jetty.start();
+ SolrCloudManager cloudManager = new SolrClientCloudManager(null, client);
+ zkStateReader.waitForLiveNodes(30, TimeUnit.SECONDS, (o, n) -> n != null && n.contains(nodeName));
+ CloudUtil.waitForState(cloudManager, COLLECTION_NAME, 30, TimeUnit.SECONDS, (liveNodes, coll) -> {
+ for (Replica r : coll.getReplicas()) {
+ if (!r.isActive(liveNodes)) {
+ return false;
+ }
+ }
+ return true;
+ });
+ rsp = request.process(client).getResponse();
+ collection = (Map<String, Object>) Utils.getObjectByPath(rsp, false, "cluster/collections/" + COLLECTION_NAME);
+ assertEquals("collection health", "GREEN", collection.get("health"));
+ shardStatus = (Map<String, Object>) collection.get("shards");
+ assertEquals(2, shardStatus.size());
+ health = (String) Utils.getObjectByPath(shardStatus, false, "shard1/health");
+ assertEquals("shard1 health", "GREEN", health);
+ health = (String) Utils.getObjectByPath(shardStatus, false, "shard2/health");
+ assertEquals("shard2 health", "GREEN", health);
+
+ }
+ }
+
private void listCollection() throws IOException, SolrServerException {
try (CloudSolrClient client = createCloudClient(null)) {