Apache Lucene open-source search software
SOLR-9563: Collection creation can fail if a node doesn't update its state quickly enough
Alan Woodward58031fd449b9
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex bbcc42b9160..47e78cfdfa8 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -152,6 +152,10 @@ Bug Fixes* SOLR-9556: OverseerAutoFailoverReplicaThread was not exiting on interrupt(Alan Woodward)+* SOLR-9563: Collection creation could fail if an ADDREPLICA subrequest arrived+ at a node before its local state had updated with the new collection data+ (Alan Woodward)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.javaindex ea85a678d8c..b56d1c90dcd 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java@@ -43,6 +43,7 @@ import java.util.concurrent.ExecutorService;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;+import java.util.concurrent.atomic.AtomicReference;import com.google.common.base.Strings;import org.apache.commons.lang.StringUtils;@@ -1524,34 +1525,43 @@ public class ZkController {if (!Overseer.isLegacy(zkStateReader)) {CloudDescriptor cloudDesc = cd.getCloudDescriptor();String coreNodeName = cloudDesc.getCoreNodeName();- assert coreNodeName != null : "SolrCore: " + cd.getName() + " has no coreNodeName";+ if (coreNodeName == null)+ throw new SolrException(ErrorCode.SERVER_ERROR, "No coreNodeName for " + cd);if (cloudDesc.getShardId() == null) {- throw new SolrException(ErrorCode.SERVER_ERROR, "No shard id for :" + cd);- }- long endTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(3, TimeUnit.SECONDS);- String errMessage = null;- while (System.nanoTime() < endTime) {- Slice slice = zkStateReader.getClusterState().getSlice(cd.getCollectionName(), cloudDesc.getShardId());- if (slice == null) {- errMessage = "Invalid slice : " + cloudDesc.getShardId();- continue;- }- if (slice.getReplica(coreNodeName) != null) {+ throw new SolrException(ErrorCode.SERVER_ERROR, "No shard id for " + cd);+ }++ AtomicReference<String> errorMessage = new AtomicReference<>();+ AtomicReference<DocCollection> collectionState = new AtomicReference<>();+ try {+ zkStateReader.waitForState(cd.getCollectionName(), 3, TimeUnit.SECONDS, (n, c) -> {+ collectionState.set(c);+ if (c == null)+ return false;+ Slice slice = c.getSlice(cloudDesc.getShardId());+ if (slice == null) {+ errorMessage.set("Invalid shard: " + cloudDesc.getShardId());+ return false;+ }Replica replica = slice.getReplica(coreNodeName);+ if (replica == null) {+ errorMessage.set("coreNodeName " + coreNodeName + " does not exist in shard " + cloudDesc.getShardId());+ return false;+ }String baseUrl = replica.getStr(BASE_URL_PROP);String coreName = replica.getStr(CORE_NAME_PROP);if (baseUrl.equals(this.baseURL) && coreName.equals(cd.getName())) {- return;- } else {- errMessage = "replica with coreNodeName " + coreNodeName + " exists but with a different name or base_url";+ return true;}- }- Thread.sleep(100);- }- if (errMessage == null) {- errMessage = "replica " + coreNodeName + " is not present in cluster state";+ errorMessage.set("coreNodeName " + coreNodeName + " exists, but does not match expected node or core name");+ return false;+ });+ } catch (TimeoutException e) {+ String error = errorMessage.get();+ if (error == null)+ error = "Replica " + coreNodeName + " is not present in cluster state";+ throw new SolrException(ErrorCode.SERVER_ERROR, error + ": " + collectionState.get());}- throw new SolrException(ErrorCode.SERVER_ERROR, errMessage + ". state : " + zkStateReader.getClusterState().getCollection(cd.getCollectionName()));}}diff --git a/solr/core/src/test/org/apache/solr/cloud/DeleteInactiveReplicaTest.java b/solr/core/src/test/org/apache/solr/cloud/DeleteInactiveReplicaTest.javaindex 91608d52eb2..8f8731bf2f9 100644--- a/solr/core/src/test/org/apache/solr/cloud/DeleteInactiveReplicaTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/DeleteInactiveReplicaTest.java@@ -85,7 +85,7 @@ public class DeleteInactiveReplicaTest extends SolrCloudTestCase {CoreContainer.CoreLoadFailure loadFailure = cc.getCoreInitFailures().get(replica.getCoreName());assertNotNull("Deleted core was still loaded!", loadFailure);assertTrue("Unexpected load failure message: " + loadFailure.exception.getMessage(),- loadFailure.exception.getMessage().contains("not present in cluster state"));+ loadFailure.exception.getMessage().contains("does not exist in shard"));// Check that we can't create a core with no coreNodeNametry (SolrClient queryClient = getHttpSolrClient(jetty.getBaseUrl().toString())) {