Skip to content

Apache Lucene open-source search software

SOLR-11011: Fix AssignTest.testIdIsUnique() failure

Cao Manh Datf80f1c096201
diff --git a/solr/core/src/java/org/apache/solr/cloud/Assign.java b/solr/core/src/java/org/apache/solr/cloud/Assign.java
index 98dcfde0e58..116fce9e6dd 100644
--- a/solr/core/src/java/org/apache/solr/cloud/Assign.java
+++ b/solr/core/src/java/org/apache/solr/cloud/Assign.java
@@ -115,12 +115,12 @@ public class Assign {
public static String assignNode(SolrZkClient client, DocCollection collection) {
// for backward compatibility;
- int numReplicas = collection.getReplicas().size();
- String coreNodeName = "core_node" + incAndGetId(client, collection.getName(), numReplicas * 20);
+ int defaultValue = defaultCounterValue(collection, false);
+ String coreNodeName = "core_node" + incAndGetId(client, collection.getName(), defaultValue);
while (collection.getReplica(coreNodeName) != null) {
// there is wee chance that, the new coreNodeName id not totally unique,
// but this will be guaranteed unique for new collections
- coreNodeName = "core_node" + incAndGetId(client, collection.getName(), numReplicas * 20);
+ coreNodeName = "core_node" + incAndGetId(client, collection.getName(), defaultValue);
}
return coreNodeName;
}
@@ -174,18 +174,34 @@ public class Assign {
return String.format(Locale.ROOT, "%s_%s_replica_%s%s", collectionName, shard, type.name().substring(0,1).toLowerCase(Locale.ROOT), replicaNum);
}
- public static String buildCoreName(SolrZkClient zkClient, DocCollection collection, String shard, Replica.Type type) {
+ private static int defaultCounterValue(DocCollection collection, boolean newCollection) {
+ if (newCollection) return 0;
+ int defaultValue = collection.getReplicas().size();
+ if (collection.getReplicationFactor() != null) {
+ // numReplicas and replicationFactor * numSlices can be not equals,
+ // in case of many addReplicas or deleteReplicas are executed
+ defaultValue = Math.max(defaultValue,
+ collection.getReplicationFactor() * collection.getSlices().size());
+ }
+ return defaultValue * 20;
+ }
+
+ public static String buildCoreName(SolrZkClient zkClient, DocCollection collection, String shard, Replica.Type type, boolean newCollection) {
Slice slice = collection.getSlice(shard);
- int numReplicas = collection.getReplicas().size();
- int replicaNum = incAndGetId(zkClient, collection.getName(), numReplicas * 20);
+ int defaultValue = defaultCounterValue(collection, newCollection);
+ int replicaNum = incAndGetId(zkClient, collection.getName(), defaultValue);
String coreName = buildCoreName(collection.getName(), shard, type, replicaNum);
while (existCoreName(coreName, slice)) {
- replicaNum = incAndGetId(zkClient, collection.getName(), numReplicas * 20);
+ replicaNum = incAndGetId(zkClient, collection.getName(), defaultValue);
coreName = buildCoreName(collection.getName(), shard, type, replicaNum);
}
return coreName;
}
+ public static String buildCoreName(SolrZkClient zkClient, DocCollection collection, String shard, Replica.Type type) {
+ return buildCoreName(zkClient, collection, shard, type, false);
+ }
+
private static boolean existCoreName(String coreName, Slice slice) {
if (slice == null) return false;
for (Replica replica : slice.getReplicas()) {
diff --git a/solr/core/src/java/org/apache/solr/cloud/CreateCollectionCmd.java b/solr/core/src/java/org/apache/solr/cloud/CreateCollectionCmd.java
index 3a51ada8b20..fc96a61e13f 100644
--- a/solr/core/src/java/org/apache/solr/cloud/CreateCollectionCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/CreateCollectionCmd.java
@@ -212,7 +212,7 @@ public class CreateCollectionCmd implements Cmd {
for (ReplicaPosition replicaPosition : replicaPositions) {
String nodeName = replicaPosition.node;
String coreName = Assign.buildCoreName(ocmh.zkStateReader.getZkClient(), zkStateReader.getClusterState().getCollection(collectionName),
- replicaPosition.shard, replicaPosition.type);
+ replicaPosition.shard, replicaPosition.type, true);
log.debug(formatString("Creating core {0} as part of shard {1} of collection {2} on {3}"
, coreName, replicaPosition.shard, collectionName, nodeName));
diff --git a/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java b/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
index 1fbf98cf233..b921e39a242 100644
--- a/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
@@ -441,6 +441,7 @@ public class OverseerTest extends SolrTestCaseJ4 {
ZkStateReader.COLLECTION_PROP, COLLECTION,
ZkStateReader.SHARD_ID_PROP, "shard1",
ZkStateReader.CORE_NAME_PROP, "core1",
+ ZkStateReader.CORE_NODE_NAME_PROP, "core_node1",
ZkStateReader.ROLES_PROP, "",
ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());
@@ -1063,6 +1064,7 @@ public class OverseerTest extends SolrTestCaseJ4 {
ZkStateReader.NODE_NAME_PROP, "node1",
ZkStateReader.COLLECTION_PROP, "c1",
ZkStateReader.CORE_NAME_PROP, "core1",
+ ZkStateReader.CORE_NODE_NAME_PROP, "core_node1",
ZkStateReader.ROLES_PROP, "",
ZkStateReader.STATE_PROP, Replica.State.DOWN.toString());
@@ -1118,6 +1120,7 @@ public class OverseerTest extends SolrTestCaseJ4 {
"collection", "test",
ZkStateReader.SHARD_ID_PROP, "x",
ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
+ ZkStateReader.CORE_NODE_NAME_PROP, "core_node1",
ZkStateReader.NODE_NAME_PROP, "node1",
ZkStateReader.CORE_NAME_PROP, "core1",
ZkStateReader.STATE_PROP, Replica.State.DOWN.toString()
@@ -1231,6 +1234,7 @@ public class OverseerTest extends SolrTestCaseJ4 {
ZkStateReader.NODE_NAME_PROP, "node"+N,
ZkStateReader.COLLECTION_PROP, COLLECTION,
ZkStateReader.CORE_NAME_PROP, "core"+N,
+ ZkStateReader.CORE_NODE_NAME_PROP, "core_node"+N,
ZkStateReader.ROLES_PROP, "",
ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());