This is an automated email from the ASF dual-hosted git repository.

markrmiller pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new bafbdb2  SOLR-15653: Fix collection creation race that assumes a local 
clusterstate when the collection has only just been created.
bafbdb2 is described below

commit bafbdb23bd3ea991f33d048805bf31ee7b78954b
Author: Mark Miller <[email protected]>
AuthorDate: Fri Oct 1 15:34:42 2021 -0500

    SOLR-15653: Fix collection creation race that assumes a local clusterstate 
when the collection has only just been created.
---
 solr/CHANGES.txt                                       |  2 ++
 .../org/apache/solr/cloud/api/collections/Assign.java  | 18 +++++++++---------
 .../cloud/api/collections/CreateCollectionCmd.java     |  4 ++--
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 6d6819a..fb0b6fa 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -384,6 +384,8 @@ Bug Fixes
 
 * SOLR-15628: The SolrException.log() helper method has been fixed to 
correctly passes the Throwable to the Logger w/o stringification (hossman)
 
+* SOLR-15653: Fix collection creation race that assumes a local clusterstate 
when the collection has only just been created. (Mark Miller)
+
 ==================  8.10.0 ==================
 
 Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this 
release.
diff --git 
a/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java 
b/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java
index 87d1dc13..b399ed4 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java
@@ -177,7 +177,7 @@ public class Assign {
   }
 
   private static int defaultCounterValue(DocCollection collection, boolean 
newCollection, String shard) {
-    if (newCollection) return 0;
+    if (newCollection || collection == null) return 0;
 
     int defaultValue;
     if (collection.getSlice(shard) != null && 
collection.getSlice(shard).getReplicas().isEmpty()) {
@@ -201,20 +201,20 @@ public class Assign {
     return defaultValue;
   }
 
-  public static String buildSolrCoreName(DistribStateManager stateManager, 
DocCollection collection, String shard, Replica.Type type, boolean 
newCollection) {
-    Slice slice = collection.getSlice(shard);
+  public static String buildSolrCoreName(DistribStateManager stateManager, 
String collectionName, DocCollection collection, String shard, Replica.Type 
type, boolean newCollection) {
+
     int defaultValue = defaultCounterValue(collection, newCollection, shard);
-    int replicaNum = incAndGetId(stateManager, collection.getName(), 
defaultValue);
-    String coreName = buildSolrCoreName(collection.getName(), shard, type, 
replicaNum);
-    while (existCoreName(coreName, slice)) {
-      replicaNum = incAndGetId(stateManager, collection.getName(), 
defaultValue);
-      coreName = buildSolrCoreName(collection.getName(), shard, type, 
replicaNum);
+    int replicaNum = incAndGetId(stateManager, collectionName, defaultValue);
+    String coreName = buildSolrCoreName(collectionName, shard, type, 
replicaNum);
+    while (collection != null && existCoreName(coreName, 
collection.getSlice(shard))) {
+      replicaNum = incAndGetId(stateManager, collectionName, defaultValue);
+      coreName = buildSolrCoreName(collectionName, shard, type, replicaNum);
     }
     return coreName;
   }
 
   public static String buildSolrCoreName(DistribStateManager stateManager, 
DocCollection collection, String shard, Replica.Type type) {
-    return buildSolrCoreName(stateManager, collection, shard, type, false);
+    return buildSolrCoreName(stateManager, collection.getName(), collection, 
shard, type, false);
   }
 
   private static boolean existCoreName(String coreName, Slice slice) {
diff --git 
a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java
 
b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java
index 04cb7b8..45211d4 100644
--- 
a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java
+++ 
b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java
@@ -231,8 +231,8 @@ public class CreateCollectionCmd implements 
CollApiCmds.CollectionApiCommand {
       for (ReplicaPosition replicaPosition : replicaPositions) {
         String nodeName = replicaPosition.node;
 
-        String coreName = 
Assign.buildSolrCoreName(ccc.getSolrCloudManager().getDistribStateManager(),
-            
ccc.getSolrCloudManager().getClusterStateProvider().getClusterState().getCollection(collectionName),
+        String coreName = 
Assign.buildSolrCoreName(ccc.getSolrCloudManager().getDistribStateManager(), 
collectionName,
+            
ccc.getSolrCloudManager().getClusterStateProvider().getClusterState().getCollectionOrNull(collectionName),
             replicaPosition.shard, replicaPosition.type, true);
         if (log.isDebugEnabled()) {
           log.debug(formatString("Creating core {0} as part of shard {1} of 
collection {2} on {3}"

Reply via email to