Skip to content

Apache Lucene open-source search software

SOLR-8804: Fix a race condition in the ClusterStatus API call

Varun Thacker343d9c6fa4cc
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 4e969bfe71a..1d91a3e57ee 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -289,6 +289,9 @@ Bug Fixes
* SOLR-8790: Collections API responses contain node name in the core-level responses that are
returned. (Anshum Gupta)
+* SOLR-8804: Fix a race condition in the ClusterStatus API call whereby the call would fail when a concurrent delete
+ collection api command was executed (Alexey Serba, Varun Thacker)
+
Optimizations
----------------------
* SOLR-7876: Speed up queries and operations that use many terms when timeAllowed has not been
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 ff60adc465b..63044fa338f 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
@@ -89,7 +89,7 @@ public class ClusterStatus {
byte[] bytes = Utils.toJSON(clusterState);
Map<String, Object> stateMap = (Map<String,Object>) Utils.fromJSON(bytes);
- Set<String> collections = new HashSet<>();
+ Set<String> collections;
String routeKey = message.getStr(ShardParams._ROUTE_);
String shard = message.getStr(ZkStateReader.SHARD_ID_PROP);
if (collection == null) {
@@ -98,11 +98,19 @@ public class ClusterStatus {
collections = Collections.singleton(collection);
}
- NamedList<Object> collectionProps = new SimpleOrderedMap<Object>();
+ NamedList<Object> collectionProps = new SimpleOrderedMap<>();
for (String name : collections) {
- Map<String, Object> collectionStatus = null;
- DocCollection clusterStateCollection = clusterState.getCollection(name);
+ Map<String, Object> collectionStatus;
+ DocCollection clusterStateCollection = clusterState.getCollectionOrNull(name);
+ if (clusterStateCollection == null) {
+ if (collection != null) {
+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection: " + name + " not found");
+ } else {
+ //collection might have got deleted at the same time
+ continue;
+ }
+ }
Set<String> requestedShards = new HashSet<>();
if (routeKey != null) {
diff --git a/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java b/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java
index 45b6f733bec..dec2bf58031 100644
--- a/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java
+++ b/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java
@@ -77,6 +77,7 @@ public class TestCollectionAPI extends ReplicaPropertiesBase {
clusterStatusWithRouteKey();
clusterStatusAliasTest();
clusterStatusRolesTest();
+ clusterStatusBadCollectionTest();
replicaPropTest();
clusterStatusZNodeVersion();
testClusterStateMigration();
@@ -318,6 +319,24 @@ public class TestCollectionAPI extends ReplicaPropertiesBase {
}
}
+ private void clusterStatusBadCollectionTest() throws Exception {
+ try (CloudSolrClient client = createCloudClient(null)) {
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
+ params.set("collection", "bad_collection_name");
+ SolrRequest request = new QueryRequest(params);
+ request.setPath("/admin/collections");
+
+ try {
+ client.request(request);
+ fail("Collection does not exist. An exception should be thrown");
+ } catch (SolrException e) {
+ //expected
+ assertTrue(e.getMessage().contains("Collection: bad_collection_name not found"));
+ }
+ }
+ }
+
private void replicaPropTest() throws Exception {
try (CloudSolrClient client = createCloudClient(null)) {
client.connect();