Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-16995: Add a ReplicaCount to keep track of replicas per type (#1928)

Vincent P92ca38e06a41
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index c8dad544cc7..638316542b0 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -65,6 +65,8 @@ Other Changes
* SOLR-16911: Establish /solr as the only host context supported by Solr, removing legacy ability to change this. Solr paths will only be either /solr or /api now. (Eric Pugh)
+* SOLR-16995: Add a ReplicaCount class to keep track of replicas per type (Vincent Primault)
+
================== 9.5.0 ==================
New Features
---------------------
diff --git a/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java b/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java
index 7d013f875ec..31be35cc752 100644
--- a/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java
+++ b/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java
@@ -60,12 +60,7 @@ public class CloudDescriptor {
this.nodeName = props.getProperty(CoreDescriptor.CORE_NODE_NAME);
if (StrUtils.isNullOrEmpty(nodeName)) this.nodeName = null;
this.numShards = PropertiesUtil.toInteger(props.getProperty(CloudDescriptor.NUM_SHARDS), null);
- String replicaTypeStr = props.getProperty(CloudDescriptor.REPLICA_TYPE);
- if (StrUtils.isNullOrEmpty(replicaTypeStr)) {
- this.replicaType = Replica.Type.NRT;
- } else {
- this.replicaType = Replica.Type.valueOf(replicaTypeStr);
- }
+ this.replicaType = Replica.Type.get(props.getProperty(CloudDescriptor.REPLICA_TYPE));
for (String propName : props.stringPropertyNames()) {
if (propName.startsWith(ZkController.COLLECTION_PARAM_PREFIX)) {
collectionParams.put(
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/AddReplicaCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/AddReplicaCmd.java
index f306fc3726c..08dc391293f 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/AddReplicaCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/AddReplicaCmd.java
@@ -37,10 +37,8 @@ import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.EnumMap;
import java.util.List;
import java.util.Locale;
-import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.solr.client.solrj.cloud.SolrCloudManager;
@@ -53,6 +51,7 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkNodeProps;
@@ -135,23 +134,21 @@ public class AddReplicaCmd implements CollApiCmds.CollectionApiCommand {
int timeout = message.getInt(TIMEOUT, 10 * 60); // 10 minutes
boolean parallel = message.getBool("parallel", false);
- Replica.Type replicaType =
- Replica.Type.valueOf(
- message
- .getStr(ZkStateReader.REPLICA_TYPE, Replica.Type.NRT.name())
- .toUpperCase(Locale.ROOT));
- EnumMap<Replica.Type, Integer> replicaTypesVsCount = new EnumMap<>(Replica.Type.class);
- replicaTypesVsCount.put(
- Replica.Type.NRT, message.getInt(NRT_REPLICAS, replicaType == Replica.Type.NRT ? 1 : 0));
- replicaTypesVsCount.put(
- Replica.Type.TLOG, message.getInt(TLOG_REPLICAS, replicaType == Replica.Type.TLOG ? 1 : 0));
- replicaTypesVsCount.put(
- Replica.Type.PULL, message.getInt(PULL_REPLICAS, replicaType == Replica.Type.PULL ? 1 : 0));
-
- int totalReplicas = 0;
- for (Map.Entry<Replica.Type, Integer> entry : replicaTypesVsCount.entrySet()) {
- totalReplicas += entry.getValue();
+ ReplicaCount numReplicas =
+ new ReplicaCount(
+ message.getInt(NRT_REPLICAS, 0),
+ message.getInt(TLOG_REPLICAS, 0),
+ message.getInt(PULL_REPLICAS, 0));
+ if (numReplicas.total() <= 0) {
+ Replica.Type replicaType =
+ Replica.Type.valueOf(
+ message
+ .getStr(ZkStateReader.REPLICA_TYPE, Replica.Type.NRT.name())
+ .toUpperCase(Locale.ROOT));
+ numReplicas.increment(replicaType);
}
+
+ int totalReplicas = numReplicas.total();
if (totalReplicas > 1) {
if (node != null) {
throw new SolrException(
@@ -173,7 +170,7 @@ public class AddReplicaCmd implements CollApiCmds.CollectionApiCommand {
clusterState,
collectionName,
message,
- replicaTypesVsCount,
+ numReplicas,
ccc.getCoreContainer())
.stream()
.map(
@@ -401,7 +398,7 @@ public class AddReplicaCmd implements CollApiCmds.CollectionApiCommand {
ClusterState clusterState,
String collectionName,
ZkNodeProps message,
- EnumMap<Replica.Type, Integer> replicaTypeVsCount,
+ ReplicaCount numReplicas,
CoreContainer coreContainer)
throws IOException, InterruptedException {
boolean skipCreateReplicaInClusterState =
@@ -409,11 +406,7 @@ public class AddReplicaCmd implements CollApiCmds.CollectionApiCommand {
boolean skipNodeAssignment = message.getBool(CollectionAdminParams.SKIP_NODE_ASSIGNMENT, false);
String sliceName = message.getStr(SHARD_ID_PROP);
DocCollection collection = clusterState.getCollection(collectionName);
-
- int numNrtReplicas = replicaTypeVsCount.get(Replica.Type.NRT);
- int numPullReplicas = replicaTypeVsCount.get(Replica.Type.PULL);
- int numTlogReplicas = replicaTypeVsCount.get(Replica.Type.TLOG);
- int totalReplicas = numNrtReplicas + numPullReplicas + numTlogReplicas;
+ int totalReplicas = numReplicas.total();
String node = message.getStr(CoreAdminParams.NODE);
Object createNodeSetStr = message.get(CollectionHandlingUtils.CREATE_NODE_SET);
@@ -432,9 +425,7 @@ public class AddReplicaCmd implements CollApiCmds.CollectionApiCommand {
clusterState,
collection.getName(),
sliceName,
- numNrtReplicas,
- numTlogReplicas,
- numPullReplicas,
+ numReplicas,
createNodeSetStr,
cloudManager,
coreContainer);
@@ -452,9 +443,9 @@ public class AddReplicaCmd implements CollApiCmds.CollectionApiCommand {
// the same node, but we've got to accommodate.
positions = new ArrayList<>(totalReplicas);
int i = 0;
- for (Map.Entry<Replica.Type, Integer> entry : replicaTypeVsCount.entrySet()) {
- for (int j = 0; j < entry.getValue(); j++) {
- positions.add(new ReplicaPosition(collectionName, sliceName, i++, entry.getKey(), node));
+ for (Replica.Type type : Replica.Type.values()) {
+ for (int j = 0; j < numReplicas.get(type); j++) {
+ positions.add(new ReplicaPosition(collectionName, sliceName, i++, type, node));
}
}
}
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 6304e522209..4f5f2482dcf 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
@@ -49,6 +49,7 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkNodeProps;
@@ -325,20 +326,18 @@ public class Assign {
ClusterState clusterState,
String collectionName,
String shard,
- int nrtReplicas,
- int tlogReplicas,
- int pullReplicas,
+ ReplicaCount numReplicas,
Object createNodeSet,
SolrCloudManager cloudManager,
CoreContainer coreContainer)
throws IOException, InterruptedException, AssignmentException {
- log.debug(
- "getNodesForNewReplicas() shard: {} , nrtReplicas : {} , tlogReplicas: {} , pullReplicas: {} , createNodeSet {}",
- shard,
- nrtReplicas,
- tlogReplicas,
- pullReplicas,
- createNodeSet);
+ if (log.isDebugEnabled()) {
+ log.debug(
+ "getNodesForNewReplicas() shard={}, {} , createNodeSet={}",
+ shard,
+ numReplicas,
+ createNodeSet);
+ }
List<String> createNodeList;
if (createNodeSet instanceof List) {
@@ -361,9 +360,7 @@ public class Assign {
new AssignRequestBuilder()
.forCollection(collectionName)
.forShard(Collections.singletonList(shard))
- .assignNrtReplicas(nrtReplicas)
- .assignTlogReplicas(tlogReplicas)
- .assignPullReplicas(pullReplicas)
+ .assignReplicas(numReplicas)
.onNodes(createNodeList)
.build();
AssignStrategy assignStrategy = createAssignStrategy(coreContainer);
@@ -501,23 +498,17 @@ public class Assign {
public final String collectionName;
public final List<String> shardNames;
public final List<String> nodes;
- public final int numNrtReplicas;
- public final int numTlogReplicas;
- public final int numPullReplicas;
+ public final ReplicaCount numReplicas;
public AssignRequest(
String collectionName,
List<String> shardNames,
List<String> nodes,
- int numNrtReplicas,
- int numTlogReplicas,
- int numPullReplicas) {
+ ReplicaCount numReplicas) {
this.collectionName = collectionName;
this.shardNames = shardNames;
this.nodes = nodes;
- this.numNrtReplicas = numNrtReplicas;
- this.numTlogReplicas = numTlogReplicas;
- this.numPullReplicas = numPullReplicas;
+ this.numReplicas = numReplicas;
}
}
@@ -525,9 +516,11 @@ public class Assign {
private String collectionName;
private List<String> shardNames;
private List<String> nodes;
- private int numNrtReplicas;
- private int numTlogReplicas;
- private int numPullReplicas;
+ private ReplicaCount numReplicas;
+
+ public AssignRequestBuilder() {
+ this.numReplicas = ReplicaCount.empty();
+ }
public AssignRequestBuilder forCollection(String collectionName) {
this.collectionName = collectionName;
@@ -544,26 +537,15 @@ public class Assign {
return this;
}
- public AssignRequestBuilder assignNrtReplicas(int numNrtReplicas) {
- this.numNrtReplicas = numNrtReplicas;
- return this;
- }
-
- public AssignRequestBuilder assignTlogReplicas(int numTlogReplicas) {
- this.numTlogReplicas = numTlogReplicas;
- return this;
- }
-
- public AssignRequestBuilder assignPullReplicas(int numPullReplicas) {
- this.numPullReplicas = numPullReplicas;
+ public AssignRequestBuilder assignReplicas(ReplicaCount numReplicas) {
+ this.numReplicas = numReplicas;
return this;
}
public AssignRequest build() {
Objects.requireNonNull(collectionName, "The collectionName cannot be null");
Objects.requireNonNull(shardNames, "The shard names cannot be null");
- return new AssignRequest(
- collectionName, shardNames, nodes, numNrtReplicas, numTlogReplicas, numPullReplicas);
+ return new AssignRequest(collectionName, shardNames, nodes, numReplicas);
}
}
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 6a07970953e..a00b01b2370 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
@@ -71,6 +71,7 @@ import org.apache.solr.common.cloud.ImplicitDocRouter;
import org.apache.solr.common.cloud.PerReplicaStates;
import org.apache.solr.common.cloud.PerReplicaStatesOps;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
@@ -137,7 +138,9 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
// fail fast if parameters are wrong or incomplete
List<String> shardNames = populateShardNames(message, router);
- checkReplicaTypes(message);
+ ReplicaCount numReplicas = getNumReplicas(message);
+ numReplicas.validate();
+
DocCollection newColl = null;
final String collectionPath = DocCollection.getCollectionPath(collectionName);
@@ -239,7 +242,8 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
ccc.getSolrCloudManager(),
clusterState,
message,
- shardNames);
+ shardNames,
+ numReplicas);
} catch (Assign.AssignmentException e) {
ZkNodeProps deleteMessage = new ZkNodeProps("name", collectionName);
new DeleteCollectionCmd(ccc).call(clusterState, deleteMessage, results);
@@ -486,18 +490,13 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
SolrCloudManager cloudManager,
ClusterState clusterState,
ZkNodeProps message,
- List<String> shardNames)
+ List<String> shardNames,
+ ReplicaCount numReplicas)
throws IOException, InterruptedException, Assign.AssignmentException {
final String collectionName = message.getStr(NAME);
// look at the replication factor and see if it matches reality
// if it does not, find best nodes to create more cores
- int numTlogReplicas = message.getInt(TLOG_REPLICAS, 0);
- int numNrtReplicas =
- message.getInt(
- NRT_REPLICAS, message.getInt(REPLICATION_FACTOR, numTlogReplicas > 0 ? 0 : 1));
- int numPullReplicas = message.getInt(PULL_REPLICAS, 0);
- int numSlices = shardNames.size();
cloudManager = wrapCloudManager(clusterState, cloudManager);
// we need to look at every node and see how many cores it serves
@@ -516,7 +515,7 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
replicaPositions = new ArrayList<>();
} else {
- int totalNumReplicas = numNrtReplicas + numTlogReplicas + numPullReplicas;
+ int totalNumReplicas = numReplicas.total();
if (totalNumReplicas > nodeList.size()) {
log.warn(
"Specified number of replicas of {} on collection {} is higher than the number of Solr instances currently live or live and part of your {}({}). {}",
@@ -531,9 +530,7 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
new Assign.AssignRequestBuilder()
.forCollection(collectionName)
.forShard(shardNames)
- .assignNrtReplicas(numNrtReplicas)
- .assignTlogReplicas(numTlogReplicas)
- .assignPullReplicas(numPullReplicas)
+ .assignReplicas(numReplicas)
.onNodes(nodeList)
.build();
Assign.AssignStrategy assignStrategy = Assign.createAssignStrategy(coreContainer);
@@ -571,18 +568,6 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
};
}
- public static void checkReplicaTypes(ZkNodeProps message) {
- int numTlogReplicas = message.getInt(TLOG_REPLICAS, 0);
- int numNrtReplicas =
- message.getInt(
- NRT_REPLICAS, message.getInt(REPLICATION_FACTOR, numTlogReplicas > 0 ? 0 : 1));
-
- if (numNrtReplicas + numTlogReplicas <= 0) {
- throw new SolrException(
- ErrorCode.BAD_REQUEST, NRT_REPLICAS + " + " + TLOG_REPLICAS + " must be greater than 0");
- }
- }
-
public static List<String> populateShardNames(ZkNodeProps message, String router) {
List<String> shardNames = new ArrayList<>();
Integer numSlices = message.getInt(CollectionHandlingUtils.NUM_SLICES, null);
@@ -605,6 +590,14 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {
return shardNames;
}
+ private static ReplicaCount getNumReplicas(ZkNodeProps message) {
+ int numTlogReplicas = message.getInt(TLOG_REPLICAS, 0);
+ int numNrtReplicas =
+ message.getInt(
+ NRT_REPLICAS, message.getInt(REPLICATION_FACTOR, numTlogReplicas > 0 ? 0 : 1));
+ return new ReplicaCount(numNrtReplicas, numTlogReplicas, message.getInt(PULL_REPLICAS, 0));
+ }
+
String getConfigName(String coll, ZkNodeProps message) throws IOException {
String configName = message.getStr(COLL_CONF);
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateShardCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateShardCmd.java
index f0878e39df6..495f6856bc7 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateShardCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateShardCmd.java
@@ -32,6 +32,8 @@ import org.apache.solr.cloud.DistributedClusterStateUpdater;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CommonAdminParams;
@@ -70,20 +72,8 @@ public class CreateShardCmd implements CollApiCmds.CollectionApiCommand {
}
DocCollection collection = clusterState.getCollection(collectionName);
- int numNrtReplicas =
- message.getInt(
- NRT_REPLICAS,
- message.getInt(
- REPLICATION_FACTOR,
- collection.getInt(NRT_REPLICAS, collection.getInt(REPLICATION_FACTOR, 1))));
- int numPullReplicas = message.getInt(PULL_REPLICAS, collection.getInt(PULL_REPLICAS, 0));
- int numTlogReplicas = message.getInt(TLOG_REPLICAS, collection.getInt(TLOG_REPLICAS, 0));
-
- if (numNrtReplicas + numTlogReplicas <= 0) {
- throw new SolrException(
- SolrException.ErrorCode.BAD_REQUEST,
- NRT_REPLICAS + " + " + TLOG_REPLICAS + " must be greater than 0");
- }
+ ReplicaCount numReplicas = getNumReplicas(collection, message);
+ numReplicas.validate();
if (ccc.getDistributedClusterStateUpdater().isDistributedStateUpdate()) {
// The message has been crafted by CollectionsHandler.CollectionOperation.CREATESHARD_OP and
@@ -114,11 +104,11 @@ public class CreateShardCmd implements CollApiCmds.CollectionApiCommand {
SHARD_ID_PROP,
sliceName,
ZkStateReader.NRT_REPLICAS,
- String.valueOf(numNrtReplicas),
+ String.valueOf(numReplicas.get(Replica.Type.NRT)),
ZkStateReader.TLOG_REPLICAS,
- String.valueOf(numTlogReplicas),
+ String.valueOf(numReplicas.get(Replica.Type.TLOG)),
ZkStateReader.PULL_REPLICAS,
- String.valueOf(numPullReplicas),
+ String.valueOf(numReplicas.get(Replica.Type.PULL)),
CollectionHandlingUtils.CREATE_NODE_SET,
message.getStr(CollectionHandlingUtils.CREATE_NODE_SET),
CommonAdminParams.WAIT_FOR_FINAL_STATE,
@@ -170,4 +160,15 @@ public class CreateShardCmd implements CollApiCmds.CollectionApiCommand {
log.info("Finished create command on all shards for collection: {}", collectionName);
}
+
+ private static ReplicaCount getNumReplicas(DocCollection collection, ZkNodeProps message) {
+ return new ReplicaCount(
+ message.getInt(
+ NRT_REPLICAS,
+ message.getInt(
+ REPLICATION_FACTOR,
+ collection.getInt(NRT_REPLICAS, collection.getInt(REPLICATION_FACTOR, 1)))),
+ message.getInt(PULL_REPLICAS, collection.getInt(PULL_REPLICAS, 0)),
+ message.getInt(TLOG_REPLICAS, collection.getInt(TLOG_REPLICAS, 0)));
+ }
}
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/MigrateReplicasCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/MigrateReplicasCmd.java
index 29616737574..3912e096cb6 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/MigrateReplicasCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/MigrateReplicasCmd.java
@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
@@ -95,14 +96,11 @@ public class MigrateReplicasCmd implements CollApiCmds.CollectionApiCommand {
List<Assign.AssignRequest> assignRequests = new ArrayList<>(sourceReplicas.size());
List<String> targetNodeList = new ArrayList<>(targetNodes);
for (Replica sourceReplica : sourceReplicas) {
- Replica.Type replicaType = sourceReplica.getType();
Assign.AssignRequest assignRequest =
new Assign.AssignRequestBuilder()
.forCollection(sourceReplica.getCollection())
.forShard(Collections.singletonList(sourceReplica.getShard()))
- .assignNrtReplicas(replicaType == Replica.Type.NRT ? 1 : 0)
- .assignTlogReplicas(replicaType == Replica.Type.TLOG ? 1 : 0)
- .assignPullReplicas(replicaType == Replica.Type.PULL ? 1 : 0)
+ .assignReplicas(ReplicaCount.of(sourceReplica.getType(), 1))
.onNodes(targetNodeList)
.build();
assignRequests.add(assignRequest);
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/ReplaceNodeCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/ReplaceNodeCmd.java
index fe47f96cc6f..ee3324f628f 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/ReplaceNodeCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/ReplaceNodeCmd.java
@@ -27,6 +27,7 @@ import java.util.stream.Collectors;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
@@ -79,17 +80,11 @@ public class ReplaceNodeCmd implements CollApiCmds.CollectionApiCommand {
if (target == null || target.isEmpty()) {
List<Assign.AssignRequest> assignRequests = new ArrayList<>(sourceReplicas.size());
for (Replica sourceReplica : sourceReplicas) {
- Replica.Type replicaType = sourceReplica.getType();
- int numNrtReplicas = replicaType == Replica.Type.NRT ? 1 : 0;
- int numTlogReplicas = replicaType == Replica.Type.TLOG ? 1 : 0;
- int numPullReplicas = replicaType == Replica.Type.PULL ? 1 : 0;
Assign.AssignRequest assignRequest =
new Assign.AssignRequestBuilder()
.forCollection(sourceReplica.getCollection())
.forShard(Collections.singletonList(sourceReplica.getShard()))
- .assignNrtReplicas(numNrtReplicas)
- .assignTlogReplicas(numTlogReplicas)
- .assignPullReplicas(numPullReplicas)
+ .assignReplicas(ReplicaCount.of(sourceReplica.getType(), 1))
.onNodes(
ccc.getSolrCloudManager().getClusterStateProvider().getLiveNodes().stream()
.filter(node -> !node.equals(source))
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/RestoreCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/RestoreCmd.java
index 7621750a5f5..c3bc1a19de0 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/RestoreCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/RestoreCmd.java
@@ -60,6 +60,7 @@ import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.DocCollection.CollectionStateProps;
import org.apache.solr.common.cloud.ImplicitDocRouter;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkNodeProps;
@@ -103,8 +104,7 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
} else {
RestoreOnANewCollection restoreOnANewCollection =
new RestoreOnANewCollection(message, restoreContext.backupCollectionState);
- restoreOnANewCollection.validate(
- restoreContext.backupCollectionState, restoreContext.nodeList.size());
+ restoreOnANewCollection.validate();
restoreOnANewCollection.process(results, restoreContext);
}
}
@@ -221,27 +221,27 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
* @see RestoreOnExistingCollection
*/
private class RestoreOnANewCollection {
- private int numNrtReplicas;
- private int numTlogReplicas;
- private int numPullReplicas;
- private ZkNodeProps message;
+ private final ReplicaCount numReplicas;
+ private final ZkNodeProps message;
private RestoreOnANewCollection(ZkNodeProps message, DocCollection backupCollectionState) {
this.message = message;
+ int numNrtReplicas;
if (message.get(REPLICATION_FACTOR) != null) {
- this.numNrtReplicas = message.getInt(REPLICATION_FACTOR, 0);
+ numNrtReplicas = message.getInt(REPLICATION_FACTOR, 0);
} else if (message.get(NRT_REPLICAS) != null) {
- this.numNrtReplicas = message.getInt(NRT_REPLICAS, 0);
+ numNrtReplicas = message.getInt(NRT_REPLICAS, 0);
} else {
// replicationFactor and nrtReplicas is always in sync after SOLR-11676
// pick from cluster state of the backed up collection
- this.numNrtReplicas = backupCollectionState.getReplicationFactor();
+ numNrtReplicas = backupCollectionState.getReplicationFactor();
}
- this.numTlogReplicas =
- getInt(message, TLOG_REPLICAS, backupCollectionState.getNumTlogReplicas(), 0);
- this.numPullReplicas =
- getInt(message, PULL_REPLICAS, backupCollectionState.getNumPullReplicas(), 0);
+ this.numReplicas =
+ new ReplicaCount(
+ numNrtReplicas,
+ getInt(message, TLOG_REPLICAS, backupCollectionState.getNumTlogReplicas(), 0),
+ getInt(message, PULL_REPLICAS, backupCollectionState.getNumPullReplicas(), 0));
}
public void process(NamedList<Object> results, RestoreContext rc) throws Exception {
@@ -307,10 +307,8 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
log.info("Completed restoring collection={} backupName={}", restoreCollection, rc.backupName);
}
- private void validate(DocCollection backupCollectionState, int availableNodeCount) {
- int numShards = backupCollectionState.getActiveSlices().size();
- int totalReplicasPerShard = numNrtReplicas + numTlogReplicas + numPullReplicas;
- assert totalReplicasPerShard > 0;
+ private void validate() {
+ assert numReplicas.total() > 0;
}
private void uploadConfig(
@@ -343,10 +341,10 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
propMap.put(Overseer.QUEUE_OPERATION, CREATE.toString());
// mostly true. Prevents autoCreated=true in the collection state.
propMap.put("fromApi", "true");
- propMap.put(REPLICATION_FACTOR, numNrtReplicas);
- propMap.put(NRT_REPLICAS, numNrtReplicas);
- propMap.put(TLOG_REPLICAS, numTlogReplicas);
- propMap.put(PULL_REPLICAS, numPullReplicas);
+ propMap.put(REPLICATION_FACTOR, numReplicas.get(Replica.Type.NRT));
+ propMap.put(NRT_REPLICAS, numReplicas.get(Replica.Type.NRT));
+ propMap.put(TLOG_REPLICAS, numReplicas.get(Replica.Type.TLOG));
+ propMap.put(PULL_REPLICAS, numReplicas.get(Replica.Type.PULL));
// inherit settings from input API, defaulting to the backup's setting. Ex: replicationFactor
for (String collProp : CollectionHandlingUtils.COLLECTION_PROPS_AND_DEFAULTS.keySet()) {
@@ -427,9 +425,7 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
new Assign.AssignRequestBuilder()
.forCollection(restoreCollection)
.forShard(sliceNames)
- .assignNrtReplicas(numNrtReplicas)
- .assignTlogReplicas(numTlogReplicas)
- .assignPullReplicas(numPullReplicas)
+ .assignReplicas(numReplicas)
.onNodes(nodeList)
.build();
Assign.AssignStrategy assignStrategy = Assign.createAssignStrategy(ccc.getCoreContainer());
@@ -453,20 +449,7 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
propMap.put(Overseer.QUEUE_OPERATION, CREATESHARD);
propMap.put(COLLECTION_PROP, restoreCollection.getName());
propMap.put(SHARD_ID_PROP, sliceName);
-
- if (numNrtReplicas >= 1) {
- propMap.put(REPLICA_TYPE, Replica.Type.NRT.name());
- } else if (numTlogReplicas >= 1) {
- propMap.put(REPLICA_TYPE, Replica.Type.TLOG.name());
- } else {
- throw new SolrException(
- ErrorCode.BAD_REQUEST,
- "Unexpected number of replicas, replicationFactor, "
- + Replica.Type.NRT
- + " or "
- + Replica.Type.TLOG
- + " must be greater than 0");
- }
+ propMap.put(REPLICA_TYPE, numReplicas.getLeaderType().name());
// Get the first node matching the shard to restore in
String node;
@@ -554,7 +537,7 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
List<ReplicaPosition> replicaPositions,
String asyncId)
throws Exception {
- int totalReplicasPerShard = numNrtReplicas + numTlogReplicas + numPullReplicas;
+ int totalReplicasPerShard = numReplicas.total();
if (totalReplicasPerShard > 1) {
if (log.isInfoEnabled()) {
log.info("Adding replicas to restored collection={}", restoreCollection.getName());
@@ -562,28 +545,19 @@ public class RestoreCmd implements CollApiCmds.CollectionApiCommand {
for (Slice slice : restoreCollection.getSlices()) {
// Add the remaining replicas for each shard, considering it's type
- int createdNrtReplicas = 0, createdTlogReplicas = 0, createdPullReplicas = 0;
-
- // We already created either a NRT or an TLOG replica as leader
- if (numNrtReplicas > 0) {
- createdNrtReplicas++;
- } else if (numTlogReplicas > 0) {
- createdTlogReplicas++;
- }
+ // We already created either a replica as leader.
+ ReplicaCount createdReplicas = ReplicaCount.of(numReplicas.getLeaderType(), 1);
for (int i = 1; i < totalReplicasPerShard; i++) {
- Replica.Type typeToCreate;
- if (createdNrtReplicas < numNrtReplicas) {
- createdNrtReplicas++;
- typeToCreate = Replica.Type.NRT;
- } else if (createdTlogReplicas < numTlogReplicas) {
- createdTlogReplicas++;
- typeToCreate = Replica.Type.TLOG;
- } else {
- createdPullReplicas++;
- typeToCreate = Replica.Type.PULL;
- assert createdPullReplicas <= numPullReplicas : "Unexpected number of replicas";
+ Replica.Type typeToCreate = null;
+ for (Replica.Type type : Replica.Type.values()) {
+ if (createdReplicas.get(type) < numReplicas.get(type)) {
+ createdReplicas.increment(type);
+ typeToCreate = type;
+ break;
+ }
}
+ assert null != typeToCreate : "Unexpected number of replicas";
if (log.isDebugEnabled()) {
log.debug(
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/SplitShardCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/SplitShardCmd.java
index 0060eb676af..92c93e18b0e 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/SplitShardCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/SplitShardCmd.java
@@ -40,7 +40,6 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrResponse;
@@ -63,6 +62,7 @@ import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.DocRouter;
import org.apache.solr.common.cloud.PlainIdRouter;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkNodeProps;
@@ -228,44 +228,24 @@ public class SplitShardCmd implements CollApiCmds.CollectionApiCommand {
List<String> subShardNames = new ArrayList<>();
// reproduce the currently existing number of replicas per type
- AtomicInteger numNrt = new AtomicInteger();
- AtomicInteger numTlog = new AtomicInteger();
- AtomicInteger numPull = new AtomicInteger();
- parentSlice
- .getReplicas()
- .forEach(
- r -> {
- switch (r.getType()) {
- case NRT:
- numNrt.incrementAndGet();
- break;
- case TLOG:
- numTlog.incrementAndGet();
- break;
- case PULL:
- numPull.incrementAndGet();
- }
- });
- int repFactor = numNrt.get() + numTlog.get() + numPull.get();
+ ReplicaCount numReplicas = ReplicaCount.empty();
+ parentSlice.getReplicas().forEach(r -> numReplicas.increment(r.getType()));
+ int repFactor = numReplicas.total();
boolean success = false;
try {
// type of the first subreplica will be the same as leader
- boolean firstNrtReplica = parentShardLeader.getType() == Replica.Type.NRT;
+ Replica.Type leaderReplicaType = parentShardLeader.getType();
// verify that we indeed have the right number of correct replica types
- if ((firstNrtReplica && numNrt.get() < 1) || (!firstNrtReplica && numTlog.get() < 1)) {
+ if (numReplicas.get(leaderReplicaType) < 1) {
throw new SolrException(
SolrException.ErrorCode.SERVER_ERROR,
"aborting split - inconsistent replica types in collection "
+ collectionName
- + ": nrt="
- + numNrt.get()
- + ", tlog="
- + numTlog.get()
- + ", pull="
- + numPull.get()
+ + ": "
+ + numReplicas
+ ", shard leader type is "
- + parentShardLeader.getType());
+ + leaderReplicaType);
}
// check for the lock
@@ -340,7 +320,7 @@ public class SplitShardCmd implements CollApiCmds.CollectionApiCommand {
subRanges,
subSlices,
subShardNames,
- firstNrtReplica);
+ leaderReplicaType);
t.stop();
// 3. if this shard has attempted a split before and failed, there will be lingering INACTIVE
@@ -431,9 +411,7 @@ public class SplitShardCmd implements CollApiCmds.CollectionApiCommand {
propMap.put(Overseer.QUEUE_OPERATION, ADDREPLICA.toLower());
propMap.put(COLLECTION_PROP, collectionName);
propMap.put(SHARD_ID_PROP, subSlice);
- propMap.put(
- REPLICA_TYPE,
- firstNrtReplica ? Replica.Type.NRT.toString() : Replica.Type.TLOG.toString());
+ propMap.put(REPLICA_TYPE, leaderReplicaType.toString());
propMap.put("node", nodeName);
propMap.put(CoreAdminParams.NAME, subShardName);
propMap.put(CommonAdminParams.WAIT_FOR_FINAL_STATE, Boolean.toString(waitForFinalState));
@@ -565,20 +543,14 @@ public class SplitShardCmd implements CollApiCmds.CollectionApiCommand {
// we have already created one subReplica for each subShard on the parent node.
// identify locations for the remaining replicas
- if (firstNrtReplica) {
- numNrt.decrementAndGet();
- } else {
- numTlog.decrementAndGet();
- }
+ numReplicas.decrement(leaderReplicaType);
t = timings.sub("identifyNodesForReplicas");
Assign.AssignRequest assignRequest =
new Assign.AssignRequestBuilder()
.forCollection(collectionName)
.forShard(subSlices)
- .assignNrtReplicas(numNrt.get())
- .assignTlogReplicas(numTlog.get())
- .assignPullReplicas(numPull.get())
+ .assignReplicas(numReplicas)
.onNodes(
Assign.getLiveOrLiveAndCreateNodeSetList(
clusterState.getLiveNodes(),
@@ -1089,7 +1061,7 @@ public class SplitShardCmd implements CollApiCmds.CollectionApiCommand {
List<DocRouter.Range> subRanges,
List<String> subSlices,
List<String> subShardNames,
- boolean firstReplicaNrt) {
+ Replica.Type replicaType) {
String splitKey = message.getStr("split.key");
String rangesStr = message.getStr(CoreAdminParams.RANGES);
String fuzzStr = message.getStr(CommonAdminParams.SPLIT_FUZZ, "0");
@@ -1211,10 +1183,7 @@ public class SplitShardCmd implements CollApiCmds.CollectionApiCommand {
subSlices.add(subSlice);
String subShardName =
Assign.buildSolrCoreName(
- cloudManager.getDistribStateManager(),
- collection,
- subSlice,
- firstReplicaNrt ? Replica.Type.NRT : Replica.Type.TLOG);
+ cloudManager.getDistribStateManager(), collection, subSlice, replicaType);
subShardNames.add(subShardName);
}
return rangesStr;
diff --git a/solr/core/src/java/org/apache/solr/cluster/events/impl/CollectionsRepairEventListener.java b/solr/core/src/java/org/apache/solr/cluster/events/impl/CollectionsRepairEventListener.java
index 2c621f090ba..05a243f38ee 100644
--- a/solr/core/src/java/org/apache/solr/cluster/events/impl/CollectionsRepairEventListener.java
+++ b/solr/core/src/java/org/apache/solr/cluster/events/impl/CollectionsRepairEventListener.java
@@ -41,7 +41,7 @@ import org.apache.solr.cluster.events.ClusterEvent;
import org.apache.solr.cluster.events.ClusterEventListener;
import org.apache.solr.cluster.events.NodesDownEvent;
import org.apache.solr.common.cloud.ClusterState;
-import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.util.SolrNamedThreadFactory;
import org.apache.solr.core.CoreContainer;
@@ -171,39 +171,25 @@ public class CollectionsRepairEventListener
ClusterState clusterState = solrCloudManager.getClusterState();
clusterState.forEachCollection(
coll -> {
- // shard / type / count
- Map<String, Map<Replica.Type, AtomicInteger>> lostReplicas = new HashMap<>();
+ // shard / number of replicas per type
+ Map<String, ReplicaCount> lostReplicas = new HashMap<>();
coll.forEachReplica(
(shard, replica) -> {
if (reallyLostNodes.contains(replica.getNodeName())) {
lostReplicas
- .computeIfAbsent(shard, s -> new HashMap<>())
- .computeIfAbsent(replica.type, t -> new AtomicInteger())
- .incrementAndGet();
+ .computeIfAbsent(shard, s -> ReplicaCount.empty())
+ .increment(replica.type);
}
});
Assign.AssignStrategy assignStrategy = Assign.createAssignStrategy(cc);
lostReplicas.forEach(
(shard, types) -> {
- Assign.AssignRequestBuilder assignRequestBuilder =
+ Assign.AssignRequest assignRequest =
new Assign.AssignRequestBuilder()
.forCollection(coll.getName())
- .forShard(Collections.singletonList(shard));
- types.forEach(
- (type, count) -> {
- switch (type) {
- case NRT:
- assignRequestBuilder.assignNrtReplicas(count.get());
- break;
- case PULL:
- assignRequestBuilder.assignPullReplicas(count.get());
- break;
- case TLOG:
- assignRequestBuilder.assignTlogReplicas(count.get());
- break;
- }
- });
- Assign.AssignRequest assignRequest = assignRequestBuilder.build();
+ .forShard(Collections.singletonList(shard))
+ .assignReplicas(types)
+ .build();
try {
List<ReplicaPosition> positions =
assignStrategy.assign(solrCloudManager, assignRequest);
diff --git a/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementPluginAssignStrategy.java b/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementPluginAssignStrategy.java
index 5fab7b2fe9c..3b933400c65 100644
--- a/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementPluginAssignStrategy.java
+++ b/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementPluginAssignStrategy.java
@@ -193,9 +193,7 @@ public class PlacementPluginAssignStrategy implements Assign.AssignStrategy {
placementContext.getCluster().getCollection(assignRequest.collectionName),
new HashSet<>(assignRequest.shardNames),
nodes,
- assignRequest.numNrtReplicas,
- assignRequest.numTlogReplicas,
- assignRequest.numPullReplicas);
+ assignRequest.numReplicas);
final List<ReplicaPosition> replicaPositions = new ArrayList<>();
ArrayList<Node> nodeList = new ArrayList<>(request.getTargetNodes());
for (String shard : request.getShardNames()) {
diff --git a/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementRequestImpl.java b/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementRequestImpl.java
index bf1fa0dbccf..f101046803d 100644
--- a/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementRequestImpl.java
+++ b/solr/core/src/java/org/apache/solr/cluster/placement/impl/PlacementRequestImpl.java
@@ -17,7 +17,6 @@
package org.apache.solr.cluster.placement.impl;
-import java.util.EnumMap;
import java.util.HashSet;
import java.util.Set;
import org.apache.solr.cloud.api.collections.Assign;
@@ -26,14 +25,15 @@ import org.apache.solr.cluster.Node;
import org.apache.solr.cluster.Replica;
import org.apache.solr.cluster.SolrCollection;
import org.apache.solr.cluster.placement.PlacementRequest;
+import org.apache.solr.common.cloud.ReplicaCount;
public class PlacementRequestImpl implements PlacementRequest {
private final SolrCollection solrCollection;
private final Set<String> shardNames;
private final Set<Node> targetNodes;
- private final EnumMap<Replica.ReplicaType, Integer> countReplicas =
- new EnumMap<>(Replica.ReplicaType.class);
+ private final ReplicaCount numReplicas;
+ @Deprecated
public PlacementRequestImpl(
SolrCollection solrCollection,
Set<String> shardNames,
@@ -41,14 +41,22 @@ public class PlacementRequestImpl implements PlacementRequest {
int countNrtReplicas,
int countTlogReplicas,
int countPullReplicas) {
+ this(
+ solrCollection,
+ shardNames,
+ targetNodes,
+ new ReplicaCount(countNrtReplicas, countTlogReplicas, countPullReplicas));
+ }
+
+ public PlacementRequestImpl(
+ SolrCollection solrCollection,
+ Set<String> shardNames,
+ Set<Node> targetNodes,
+ ReplicaCount numReplicas) {
this.solrCollection = solrCollection;
this.shardNames = shardNames;
this.targetNodes = targetNodes;
- // Initializing map for all values of enum, so unboxing always possible later without checking
- // for null
- countReplicas.put(Replica.ReplicaType.NRT, countNrtReplicas);
- countReplicas.put(Replica.ReplicaType.TLOG, countTlogReplicas);
- countReplicas.put(Replica.ReplicaType.PULL, countPullReplicas);
+ this.numReplicas = numReplicas;
}
@Override
@@ -68,7 +76,8 @@ public class PlacementRequestImpl implements PlacementRequest {
@Override
public int getCountReplicasToCreate(Replica.ReplicaType replicaType) {
- return countReplicas.get(replicaType);
+ return numReplicas.get(
+ SimpleClusterAbstractionsImpl.ReplicaImpl.toCloudReplicaType(replicaType));
}
/**
@@ -111,12 +120,6 @@ public class PlacementRequestImpl implements PlacementRequest {
}
}
- return new PlacementRequestImpl(
- solrCollection,
- shardNames,
- nodes,
- assignRequest.numNrtReplicas,
- assignRequest.numTlogReplicas,
- assignRequest.numPullReplicas);
+ return new PlacementRequestImpl(solrCollection, shardNames, nodes, assignRequest.numReplicas);
}
}
diff --git a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/AffinityPlacementFactoryTest.java b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/AffinityPlacementFactoryTest.java
index 424161dafa4..14113386b04 100644
--- a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/AffinityPlacementFactoryTest.java
+++ b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/AffinityPlacementFactoryTest.java
@@ -53,6 +53,7 @@ import org.apache.solr.cluster.placement.impl.BalanceRequestImpl;
import org.apache.solr.cluster.placement.impl.ModificationRequestImpl;
import org.apache.solr.cluster.placement.impl.PlacementRequestImpl;
import org.apache.solr.common.SolrException;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.util.Pair;
import org.apache.solr.common.util.StrUtils;
import org.junit.Before;
@@ -134,9 +135,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 1,
- 0,
- 0);
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -178,7 +177,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
// Place two replicas of each type for each shard
PlacementRequestImpl placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 2, 2, 2);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(2, 2, 2));
PlacementPlan pp =
plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
@@ -202,7 +204,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
// Verify that if we ask for 7 replicas, the placement will use the low free space node
placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 7, 0, 0);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(7, 0, 0));
pp = plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
assertEquals(21, pp.getReplicaPlacements().size()); // 3 shards, 7 replicas each
placements = new HashSet<>();
@@ -222,7 +227,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
try {
placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 8, 0, 0);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(8, 0, 0));
plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
fail("Placing 8 replicas should not be possible given only 7 nodes have enough space");
} catch (PlacementException e) {
@@ -265,7 +273,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
// Place an additional NRT and an additional TLOG replica for each shard
PlacementRequestImpl placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 1, 1, 0);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(1, 1, 0));
// The replicas must be placed on the most appropriate nodes, i.e. those that do not already
// have a replica for the shard and then on the node with the lowest number of cores. NRT are
@@ -374,7 +385,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
// Add 2 NRT and one TLOG to each shard.
PlacementRequestImpl placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 2, 1, 0);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(2, 1, 0));
PlacementPlan pp =
plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
// Shard 1: The NRT's should go to the med cores node on AZ2 and low core on az3 (even though a
@@ -397,7 +411,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
// If we add instead 2 PULL replicas to each shard
placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 0, 0, 2);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(0, 0, 2));
pp = plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
// Shard 1: Given node AZ3_TLOGPULL is taken by the TLOG replica, the PULL should go to
// AZ1_TLOGPULL_LOWFREEDISK and AZ2_TLOGPULL
@@ -462,9 +479,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
solrCollection.getShardNames(),
new HashSet<>(liveNodes),
- countNrtToPlace,
- 0,
- 0);
+ new ReplicaCount(countNrtToPlace, 0, 0));
PlacementPlan pp =
plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
verifyPlacements(
@@ -509,9 +524,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 0,
- 0,
- 1);
+ new ReplicaCount(0, 0, 1));
PlacementPlan pp =
plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
@@ -527,7 +540,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
it.next(); // skip first shard to do placement for the second one...
placementRequest =
new PlacementRequestImpl(
- solrCollection, Set.of(it.next().getShardName()), new HashSet<>(liveNodes), 0, 0, 1);
+ solrCollection,
+ Set.of(it.next().getShardName()),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(0, 0, 1));
pp = plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
expectedPlacements = Set.of("2 PULL 0");
verifyPlacements(expectedPlacements, pp, collectionBuilder.getShardBuilders(), liveNodes);
@@ -566,9 +582,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
.map(Shard::getShardName)
.collect(Collectors.toSet()),
cluster.getLiveNodes(),
- 2,
- 2,
- 2);
+ new ReplicaCount(2, 2, 2));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
// 2 shards, 6 replicas
@@ -640,9 +654,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
.map(Shard::getShardName)
.collect(Collectors.toSet()),
cluster.getLiveNodes(),
- 2,
- 2,
- 2);
+ new ReplicaCount(2, 2, 2));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
// 2 shards, 6 replicas
@@ -721,9 +733,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
.map(Shard::getShardName)
.collect(Collectors.toSet()),
cluster.getLiveNodes(),
- 1,
- 0,
- 1);
+ new ReplicaCount(1, 0, 1));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
assertEquals(4, pp.getReplicaPlacements().size());
@@ -772,9 +782,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
.map(Set::of)
.orElseGet(Collections::emptySet),
cluster.getLiveNodes(),
- 0,
- 1,
- 1);
+ new ReplicaCount(0, 1, 1));
assertThrows(
PlacementException.class,
@@ -789,9 +797,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
.map(Shard::getShardName)
.collect(Collectors.toSet()),
cluster.getLiveNodes(),
- 0,
- 1,
- 1);
+ new ReplicaCount(0, 1, 1));
assertThrows(
PlacementException.class,
@@ -845,7 +851,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
PlacementRequestImpl placementRequest =
new PlacementRequestImpl(
- primaryCollection, Set.of("shard1", "shard2"), cluster.getLiveNodes(), 1, 0, 0);
+ primaryCollection,
+ Set.of("shard1", "shard2"),
+ cluster.getLiveNodes(),
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
assertEquals(2, pp.getReplicaPlacements().size());
@@ -859,7 +868,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
placementRequest =
new PlacementRequestImpl(
- primaryCollection, Set.of("shard1"), cluster.getLiveNodes(), 3, 0, 0);
+ primaryCollection, Set.of("shard1"), cluster.getLiveNodes(), new ReplicaCount(3, 0, 0));
try {
pp = plugin.computePlacement(placementRequest, placementContext);
fail("should generate 'Not enough eligible nodes' failure here");
@@ -909,9 +918,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
primaryCollection,
shuffle(Arrays.asList("shard2", "shard1")),
shuffle(cluster.getLiveNodes()),
- 1,
- 0,
- 0);
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
assertEquals(2, pp.getReplicaPlacements().size());
@@ -939,7 +946,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
placementRequest =
new PlacementRequestImpl(
- primaryCollection, Set.of("shard3"), cluster.getLiveNodes(), 1, 0, 0);
+ primaryCollection, Set.of("shard3"), cluster.getLiveNodes(), new ReplicaCount(1, 0, 0));
try {
pp = plugin.computePlacement(placementRequest, placementContext);
fail("should generate 'has no replicas on eligible nodes' failure here");
@@ -1171,7 +1178,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
SolrCollection collection = placementContext.getCluster().getCollection(collectionName);
PlacementRequestImpl placementRequest =
new PlacementRequestImpl(
- collection, Set.of("shard1"), placementContext.getCluster().getLiveNodes(), 3, 0, 0);
+ collection,
+ Set.of("shard1"),
+ placementContext.getCluster().getLiveNodes(),
+ new ReplicaCount(3, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
assertEquals("expected 3 placements: " + pp, 3, pp.getReplicaPlacements().size());
@@ -1196,7 +1206,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
collection = placementContext.getCluster().getCollection(collectionName);
placementRequest =
new PlacementRequestImpl(
- collection, Set.of("shard1"), placementContext.getCluster().getLiveNodes(), 6, 0, 0);
+ collection,
+ Set.of("shard1"),
+ placementContext.getCluster().getLiveNodes(),
+ new ReplicaCount(6, 0, 0));
pp = plugin.computePlacement(placementRequest, placementContext);
assertEquals("expected 6 placements: " + pp, 6, pp.getReplicaPlacements().size());
@@ -1220,7 +1233,10 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
collection = placementContext.getCluster().getCollection(collectionName);
placementRequest =
new PlacementRequestImpl(
- collection, Set.of("shard1"), placementContext.getCluster().getLiveNodes(), 6, 0, 0);
+ collection,
+ Set.of("shard1"),
+ placementContext.getCluster().getLiveNodes(),
+ new ReplicaCount(6, 0, 0));
pp = plugin.computePlacement(placementRequest, placementContext);
assertEquals("expected 6 placements: " + pp, 6, pp.getReplicaPlacements().size());
nodeNamesByType.clear();
@@ -1337,9 +1353,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
solrCollection.getShardNames(),
new HashSet<>(liveNodes),
- nrtReplicas,
- tlogReplicas,
- pullReplicas);
+ new ReplicaCount(nrtReplicas, tlogReplicas, pullReplicas));
long start = System.nanoTime();
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -1430,9 +1444,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 1,
- 0,
- 0);
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -1447,9 +1459,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 2,
- 0,
- 0);
+ new ReplicaCount(2, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -1519,9 +1529,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 1,
- 0,
- 0);
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -1536,9 +1544,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 2,
- 0,
- 0);
+ new ReplicaCount(2, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -1582,9 +1588,7 @@ public class AffinityPlacementFactoryTest extends AbstractPlacementFactoryTest {
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 1,
- 0,
- 0);
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
diff --git a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java
index 6afd24fafd1..03a272110ac 100644
--- a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java
+++ b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java
@@ -33,6 +33,7 @@ import org.apache.solr.cluster.placement.PlacementPlugin;
import org.apache.solr.cluster.placement.ReplicaPlacement;
import org.apache.solr.cluster.placement.impl.BalanceRequestImpl;
import org.apache.solr.cluster.placement.impl.PlacementRequestImpl;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
@@ -101,9 +102,7 @@ public class MinimizeCoresPlacementFactoryTest extends AbstractPlacementFactoryT
solrCollection,
Set.of(solrCollection.shards().iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 1,
- 0,
- 0);
+ new ReplicaCount(1, 0, 0));
PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);
@@ -147,7 +146,10 @@ public class MinimizeCoresPlacementFactoryTest extends AbstractPlacementFactoryT
// Place an additional NRT and an additional TLOG replica for each shard
PlacementRequestImpl placementRequest =
new PlacementRequestImpl(
- solrCollection, solrCollection.getShardNames(), new HashSet<>(liveNodes), 1, 1, 0);
+ solrCollection,
+ solrCollection.getShardNames(),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(1, 1, 0));
// The replicas must be placed on the most appropriate nodes, i.e. those that do not already
// have a replica for the shard and then on the node with the lowest number of cores. NRT are
@@ -198,9 +200,7 @@ public class MinimizeCoresPlacementFactoryTest extends AbstractPlacementFactoryT
solrCollection,
Set.of(solrCollection.iterator().next().getShardName()),
new HashSet<>(liveNodes),
- 0,
- 0,
- 1);
+ new ReplicaCount(0, 0, 1));
PlacementPlan pp =
plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
@@ -216,7 +216,10 @@ public class MinimizeCoresPlacementFactoryTest extends AbstractPlacementFactoryT
it.next(); // skip first shard to do placement for the second one...
placementRequest =
new PlacementRequestImpl(
- solrCollection, Set.of(it.next().getShardName()), new HashSet<>(liveNodes), 0, 0, 1);
+ solrCollection,
+ Set.of(it.next().getShardName()),
+ new HashSet<>(liveNodes),
+ new ReplicaCount(0, 0, 1));
pp = plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());
expectedPlacements = Set.of("2 PULL 0");
verifyPlacements(expectedPlacements, pp, collectionBuilder.getShardBuilders(), liveNodes);
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java
index e619ae716c4..5f77eae75ee 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java
@@ -47,6 +47,7 @@ import org.apache.solr.common.MapWriter;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ImplicitDocRouter;
import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.params.CollectionAdminParams;
import org.apache.solr.common.params.CollectionParams;
import org.apache.solr.common.params.CollectionParams.CollectionAction;
@@ -127,6 +128,21 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
return SolrRequestType.ADMIN.toString();
}
+ private static void writeNumReplicas(ReplicaCount numReplicas, ModifiableSolrParams params) {
+ if (numReplicas.contains(Replica.Type.NRT)) {
+ params.add(
+ CollectionAdminParams.NRT_REPLICAS, String.valueOf(numReplicas.get(Replica.Type.NRT)));
+ }
+ if (numReplicas.contains(Replica.Type.TLOG)) {
+ params.add(
+ CollectionAdminParams.TLOG_REPLICAS, String.valueOf(numReplicas.get(Replica.Type.TLOG)));
+ }
+ if (numReplicas.contains(Replica.Type.PULL)) {
+ params.add(
+ CollectionAdminParams.PULL_REPLICAS, String.valueOf(numReplicas.get(Replica.Type.PULL)));
+ }
+ }
+
/**
* Take the request specific basic auth creds on this admin request and propagate them to a
* related request if does not already have credentials set, such as a
@@ -445,9 +461,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
ImplicitDocRouter.NAME,
null,
checkNotNull("shards", shards),
- numNrtReplicas,
- numTlogReplicas,
- numPullReplicas);
+ new ReplicaCount(numNrtReplicas, numTlogReplicas, numPullReplicas));
}
/**
@@ -470,9 +484,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
protected String shards;
protected String routerField;
protected Integer numShards;
- protected Integer nrtReplicas;
- protected Integer pullReplicas;
- protected Integer tlogReplicas;
+ protected ReplicaCount numReplicas;
protected Boolean perReplicaState;
protected Properties properties;
@@ -493,9 +505,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
null,
numShards,
null,
- numNrtReplicas,
- numTlogReplicas,
- numPullReplicas);
+ new ReplicaCount(numNrtReplicas, numTlogReplicas, numPullReplicas));
}
/**
@@ -509,9 +519,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
ImplicitDocRouter.NAME,
null,
checkNotNull("shards", shards),
- numNrtReplicas,
- null,
- null);
+ ReplicaCount.of(Replica.Type.NRT, numNrtReplicas));
}
private Create(
@@ -520,9 +528,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
String routerName,
Integer numShards,
String shards,
- Integer numNrtReplicas,
- Integer numTlogReplicas,
- Integer numPullReplicas) {
+ ReplicaCount numReplicas) {
super(CollectionAction.CREATE, SolrIdentifierValidator.validateCollectionName(collection));
// NOTE: there's very little we can assert about the args because nothing but "collection" is
// required by the server
@@ -533,9 +539,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
this.routerName = routerName;
this.numShards = numShards;
this.setShards(shards);
- this.nrtReplicas = numNrtReplicas;
- this.tlogReplicas = numTlogReplicas;
- this.pullReplicas = numPullReplicas;
+ this.numReplicas = numReplicas;
}
public Create setCreateNodeSet(String nodeSet) {
@@ -554,23 +558,24 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
}
public Create setNrtReplicas(Integer nrtReplicas) {
- this.nrtReplicas = nrtReplicas;
- return this;
+ return setNumReplicas(Replica.Type.NRT, nrtReplicas);
}
public Create setTlogReplicas(Integer tlogReplicas) {
- this.tlogReplicas = tlogReplicas;
- return this;
+ return setNumReplicas(Replica.Type.TLOG, tlogReplicas);
}
public Create setPullReplicas(Integer pullReplicas) {
- this.pullReplicas = pullReplicas;
+ return setNumReplicas(Replica.Type.PULL, pullReplicas);
+ }
+
+ public Create setNumReplicas(Replica.Type type, Integer count) {
+ this.numReplicas.put(type, count);
return this;
}
public Create setReplicationFactor(Integer repl) {
- this.nrtReplicas = repl;
- return this;
+ return setNrtReplicas(repl);
}
public Create setRule(String... s) {
@@ -614,19 +619,23 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
}
public Integer getReplicationFactor() {
- return getNumNrtReplicas();
+ return getNumReplicas(Replica.Type.NRT);
}
public Integer getNumNrtReplicas() {
- return nrtReplicas;
+ return getNumReplicas(Replica.Type.NRT);
}
public Integer getNumTlogReplicas() {
- return tlogReplicas;
+ return getNumReplicas(Replica.Type.TLOG);
}
public Integer getNumPullReplicas() {
- return pullReplicas;
+ return getNumReplicas(Replica.Type.PULL);
+ }
+
+ public Integer getNumReplicas(Replica.Type type) {
+ return numReplicas.get(type);
}
public Boolean getPerReplicaState() {
@@ -686,18 +695,10 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
if (routerField != null) {
params.set("router.field", routerField);
}
- if (nrtReplicas != null) {
- params.set(CollectionAdminParams.NRT_REPLICAS, nrtReplicas);
- }
if (properties != null) {
addProperties(params, properties);
}
- if (pullReplicas != null) {
- params.set(CollectionAdminParams.PULL_REPLICAS, pullReplicas);
- }
- if (tlogReplicas != null) {
- params.set(CollectionAdminParams.TLOG_REPLICAS, tlogReplicas);
- }
+ writeNumReplicas(numReplicas, params);
if (Boolean.TRUE.equals(perReplicaState)) {
params.set(CollectionAdminParams.PER_REPLICA_STATE, perReplicaState);
}
@@ -1251,9 +1252,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
// in common with collection creation:
protected String configName;
protected Integer replicationFactor;
- protected Integer nrtReplicas;
- protected Integer tlogReplicas;
- protected Integer pullReplicas;
+ protected ReplicaCount numReplicas;
protected Optional<String> createNodeSet = Optional.empty();
protected Optional<Boolean> createNodeSetShuffle = Optional.empty();
protected Properties properties;
@@ -1262,6 +1261,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
public Restore(String collection, String backupName) {
super(CollectionAction.RESTORE, collection);
this.backupName = backupName;
+ this.numReplicas = ReplicaCount.empty();
}
public String getLocation() {
@@ -1318,30 +1318,35 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
}
public Integer getNrtReplicas() {
- return nrtReplicas;
+ return getNumReplicas(Replica.Type.NRT);
}
public Restore setNrtReplicas(Integer nrtReplicas) {
- this.nrtReplicas = nrtReplicas;
- return this;
+ return setNumReplicas(Replica.Type.NRT, nrtReplicas);
}
- ;
public Integer getTlogReplicas() {
- return tlogReplicas;
+ return getNumReplicas(Replica.Type.TLOG);
}
public Restore setTlogReplicas(Integer tlogReplicas) {
- this.tlogReplicas = tlogReplicas;
- return this;
+ return setNumReplicas(Replica.Type.TLOG, tlogReplicas);
}
public Integer getPullReplicas() {
- return pullReplicas;
+ return getNumReplicas(Replica.Type.PULL);
}
public Restore setPullReplicas(Integer pullReplicas) {
- this.pullReplicas = pullReplicas;
+ return setNumReplicas(Replica.Type.PULL, pullReplicas);
+ }
+
+ public int getNumReplicas(Replica.Type type) {
+ return this.numReplicas.get(type);
+ }
+
+ public Restore setNumReplicas(Replica.Type type, Integer count) {
+ this.numReplicas.put(type, count);
return this;
}
@@ -1377,7 +1382,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
params.set(CoreAdminParams.NAME, backupName);
params.set(CoreAdminParams.BACKUP_LOCATION, location); // note: optional
params.set("collection.configName", configName); // note: optional
- if (replicationFactor != null && nrtReplicas != null) {
+ if (replicationFactor != null && numReplicas.contains(Replica.Type.NRT)) {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST,
"Cannot set both replicationFactor and nrtReplicas as they mean the same thing");
@@ -1385,15 +1390,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
if (replicationFactor != null) {
params.set(CollectionAdminParams.REPLICATION_FACTOR, replicationFactor);
}
- if (nrtReplicas != null) {
- params.set(CollectionAdminParams.NRT_REPLICAS, nrtReplicas);
- }
- if (pullReplicas != null) {
- params.set(CollectionAdminParams.PULL_REPLICAS, pullReplicas);
- }
- if (tlogReplicas != null) {
- params.set(CollectionAdminParams.TLOG_REPLICAS, tlogReplicas);
- }
+ writeNumReplicas(numReplicas, params);
if (properties != null) {
addProperties(params, properties);
}
@@ -2441,7 +2438,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
protected String ulogDir;
protected Properties properties;
protected Replica.Type type;
- protected Integer nrtReplicas, tlogReplicas, pullReplicas;
+ protected ReplicaCount numReplicas;
protected Boolean skipNodeAssignment;
protected String createNodeSet;
@@ -2451,6 +2448,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
this.shard = shard;
this.routeKey = routeKey;
this.type = type;
+ this.numReplicas = ReplicaCount.empty();
}
public Properties getProperties() {
@@ -2528,29 +2526,35 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
}
public Integer getNrtReplicas() {
- return nrtReplicas;
+ return getNumReplicas(Replica.Type.NRT);
}
public AddReplica setNrtReplicas(Integer nrtReplicas) {
- this.nrtReplicas = nrtReplicas;
- return this;
+ return setNumReplicas(Replica.Type.NRT, nrtReplicas);
}
public Integer getTlogReplicas() {
- return tlogReplicas;
+ return getNumReplicas(Replica.Type.TLOG);
}
public AddReplica setTlogReplicas(Integer tlogReplicas) {
- this.tlogReplicas = tlogReplicas;
- return this;
+ return setNumReplicas(Replica.Type.TLOG, tlogReplicas);
}
public Integer getPullReplicas() {
- return pullReplicas;
+ return getNumReplicas(Replica.Type.PULL);
}
public AddReplica setPullReplicas(Integer pullReplicas) {
- this.pullReplicas = pullReplicas;
+ return setNumReplicas(Replica.Type.PULL, pullReplicas);
+ }
+
+ public int getNumReplicas(Replica.Type type) {
+ return this.numReplicas.get(type);
+ }
+
+ public AddReplica setNumReplicas(Replica.Type type, Integer count) {
+ this.numReplicas.put(type, count);
return this;
}
@@ -2598,15 +2602,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>
if (properties != null) {
addProperties(params, properties);
}
- if (nrtReplicas != null) {
- params.add(CollectionAdminParams.NRT_REPLICAS, String.valueOf(nrtReplicas));
- }
- if (tlogReplicas != null) {
- params.add(CollectionAdminParams.TLOG_REPLICAS, String.valueOf(tlogReplicas));
- }
- if (pullReplicas != null) {
- params.add(CollectionAdminParams.PULL_REPLICAS, String.valueOf(pullReplicas));
- }
+ writeNumReplicas(numReplicas, params);
if (createNodeSet != null) {
params.add(CREATE_NODE_SET_PARAM, createNodeSet);
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/DocCollection.java b/solr/solrj/src/java/org/apache/solr/common/cloud/DocCollection.java
index 0d23ca58ab0..c4fe5c67907 100644
--- a/solr/solrj/src/java/org/apache/solr/common/cloud/DocCollection.java
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/DocCollection.java
@@ -58,9 +58,7 @@ public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
private final String znode;
private final Integer replicationFactor;
- private final Integer numNrtReplicas;
- private final Integer numTlogReplicas;
- private final Integer numPullReplicas;
+ private final ReplicaCount numReplicas;
private final Boolean readOnly;
private final Boolean perReplicaState;
private final Map<String, Replica> replicaMap = new HashMap<>();
@@ -115,9 +113,11 @@ public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
this.nodeNameLeaderReplicas = new HashMap<>();
this.nodeNameReplicas = new HashMap<>();
this.replicationFactor = (Integer) verifyProp(props, CollectionStateProps.REPLICATION_FACTOR);
- this.numNrtReplicas = (Integer) verifyProp(props, CollectionStateProps.NRT_REPLICAS, 0);
- this.numTlogReplicas = (Integer) verifyProp(props, CollectionStateProps.TLOG_REPLICAS, 0);
- this.numPullReplicas = (Integer) verifyProp(props, CollectionStateProps.PULL_REPLICAS, 0);
+ this.numReplicas =
+ new ReplicaCount(
+ (Integer) verifyProp(props, CollectionStateProps.NRT_REPLICAS, 0),
+ (Integer) verifyProp(props, CollectionStateProps.TLOG_REPLICAS, 0),
+ (Integer) verifyProp(props, CollectionStateProps.PULL_REPLICAS, 0));
this.perReplicaState =
(Boolean) verifyProp(props, CollectionStateProps.PER_REPLICA_STATE, Boolean.FALSE);
if (this.perReplicaState) {
@@ -513,7 +513,7 @@ public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
* this collection was created with
*/
public Integer getNumNrtReplicas() {
- return numNrtReplicas;
+ return getNumReplicas(Replica.Type.NRT);
}
/**
@@ -521,7 +521,7 @@ public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
* this collection was created with
*/
public Integer getNumTlogReplicas() {
- return numTlogReplicas;
+ return getNumReplicas(Replica.Type.TLOG);
}
/**
@@ -529,7 +529,14 @@ public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
* this collection was created with
*/
public Integer getNumPullReplicas() {
- return numPullReplicas;
+ return getNumReplicas(Replica.Type.PULL);
+ }
+
+ /**
+ * @return the number of replicas of a given type this collection was created with
+ */
+ public Integer getNumReplicas(Replica.Type type) {
+ return numReplicas.get(type);
}
public boolean isPerReplicaState() {
@@ -540,12 +547,14 @@ public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
return perReplicaStatesRef != null ? perReplicaStatesRef.get() : null;
}
+ @Deprecated
public int getExpectedReplicaCount(Replica.Type type, int def) {
- Integer result = null;
- if (type == Replica.Type.NRT) result = numNrtReplicas;
- if (type == Replica.Type.PULL) result = numPullReplicas;
- if (type == Replica.Type.TLOG) result = numTlogReplicas;
- return result == null ? def : result;
+ // def is kept for backwards compatibility.
+ return numReplicas.get(type);
+ }
+
+ public int getExpectedReplicaCount(Replica.Type type) {
+ return numReplicas.get(type);
}
/** JSON properties related to a collection's state. */
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/Replica.java b/solr/solrj/src/java/org/apache/solr/common/cloud/Replica.java
index f05ad7078f5..854b20fa815 100644
--- a/solr/solrj/src/java/org/apache/solr/common/cloud/Replica.java
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/Replica.java
@@ -26,6 +26,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.solr.common.MapWriter;
+import org.apache.solr.common.util.StrUtils;
import org.apache.solr.common.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -125,7 +126,7 @@ public class Replica extends ZkNodeProps implements MapWriter {
}
public static Type get(String name) {
- return name == null ? Type.NRT : Type.valueOf(name.toUpperCase(Locale.ROOT));
+ return StrUtils.isNullOrEmpty(name) ? NRT : Type.valueOf(name.toUpperCase(Locale.ROOT));
}
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ReplicaCount.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ReplicaCount.java
new file mode 100644
index 00000000000..35c35c3ba76
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ReplicaCount.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.common.cloud;
+
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Locale;
+import java.util.stream.Collectors;
+import org.apache.solr.common.SolrException;
+
+/** Tracks the number of replicas per replica type. This class is mutable. */
+public final class ReplicaCount {
+ private final EnumMap<Replica.Type, Integer> countByType;
+
+ public ReplicaCount(Integer nrtReplicas, Integer tlogReplicas, Integer pullReplicas) {
+ this.countByType = new EnumMap<>(Replica.Type.class);
+ put(Replica.Type.NRT, nrtReplicas);
+ put(Replica.Type.TLOG, tlogReplicas);
+ put(Replica.Type.PULL, pullReplicas);
+ }
+
+ public static ReplicaCount empty() {
+ return new ReplicaCount(null, null, null);
+ }
+
+ public static ReplicaCount of(Replica.Type type, Integer count) {
+ ReplicaCount replicaCount = empty();
+ replicaCount.put(type, count);
+ return replicaCount;
+ }
+
+ /**
+ * Returns the number of replicas for a given type.
+ *
+ * @param type a replica type
+ */
+ public int get(Replica.Type type) {
+ return countByType.getOrDefault(type, 0);
+ }
+
+ /**
+ * Returns whether the number of replicas for a given type was explicitly defined.
+ *
+ * @param type a replica type
+ */
+ public boolean contains(Replica.Type type) {
+ return countByType.containsKey(type);
+ }
+
+ /**
+ * Defines the number of replicas for a given type.
+ *
+ * @param type a replica type
+ * @param count a number of replicas (if null, mark the value as missing)
+ */
+ public void put(Replica.Type type, Integer count) {
+ if (null != count && count >= 0) {
+ // If it considered as an error to put a negative value. In this case,
+ // we choose to fail gracefully by not changing anything.
+ countByType.put(type, count);
+ } else if (null == count) {
+ countByType.remove(type);
+ }
+ }
+
+ /**
+ * Increment the number of replicas for a given type.
+ *
+ * @param type a replica type
+ */
+ public void increment(Replica.Type type) {
+ put(type, get(type) + 1);
+ }
+
+ /**
+ * Decrement the number of replicas for a given type.
+ *
+ * @param type a replica type
+ */
+ public void decrement(Replica.Type type) {
+ put(type, get(type) - 1);
+ }
+
+ /** Returns the total number of replicas. */
+ public int total() {
+ return countByType.values().stream().reduce(Integer::sum).orElse(0);
+ }
+
+ /**
+ * Returns the first non-zero replica type that can be a leader.
+ *
+ * @throws SolrException if no such replica exists
+ */
+ public Replica.Type getLeaderType() {
+ for (Replica.Type type : Replica.Type.values()) {
+ if (type.leaderEligible && get(type) >= 1) {
+ return type;
+ }
+ }
+ throw new SolrException(
+ SolrException.ErrorCode.BAD_REQUEST,
+ "Unexpected number of replicas ("
+ + this
+ + "), there must be at least one leader-eligible replica");
+ }
+
+ /** Validate that there is at least one replica that can be a leader. */
+ public void validate() {
+ getLeaderType();
+ }
+
+ /** Returns a representation of this class which can be used for debugging. */
+ @Override
+ public String toString() {
+ return Arrays.stream(Replica.Type.values())
+ .map(t -> t.name().toLowerCase(Locale.ROOT) + "=" + get(t))
+ .collect(Collectors.joining(", "));
+ }
+}
diff --git a/solr/solrj/src/test/org/apache/solr/common/cloud/ReplicaCountTest.java b/solr/solrj/src/test/org/apache/solr/common/cloud/ReplicaCountTest.java
new file mode 100644
index 00000000000..44c371debad
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/common/cloud/ReplicaCountTest.java
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.common.cloud;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.solr.common.SolrException;
+import org.junit.Test;
+
+/** Unit tests for {@link ReplicaCount}. */
+public class ReplicaCountTest {
+
+ @Test
+ public void testPutAndGet() {
+ ReplicaCount replicaCount = ReplicaCount.empty();
+ replicaCount.put(Replica.Type.NRT, 1);
+ replicaCount.put(Replica.Type.TLOG, 2);
+
+ assertTrue(replicaCount.contains(Replica.Type.NRT));
+ assertTrue(replicaCount.contains(Replica.Type.TLOG));
+ assertFalse(replicaCount.contains(Replica.Type.PULL));
+
+ assertEquals(1, replicaCount.get(Replica.Type.NRT));
+ assertEquals(2, replicaCount.get(Replica.Type.TLOG));
+ assertEquals(0, replicaCount.get(Replica.Type.PULL));
+ }
+
+ @Test
+ public void testPutNullValue() {
+ ReplicaCount replicaCount = ReplicaCount.of(Replica.Type.NRT, 1);
+ replicaCount.put(Replica.Type.NRT, null);
+
+ assertFalse(replicaCount.contains(Replica.Type.NRT));
+ assertEquals(0, replicaCount.get(Replica.Type.NRT));
+ }
+
+ @Test
+ public void testPutNegativeValue() {
+ ReplicaCount replicaCount = ReplicaCount.of(Replica.Type.NRT, -1);
+
+ assertFalse(replicaCount.contains(Replica.Type.NRT));
+ assertEquals(0, replicaCount.get(Replica.Type.NRT));
+ }
+
+ @Test
+ public void testIncrement() {
+ ReplicaCount replicaCount = ReplicaCount.empty();
+ replicaCount.put(Replica.Type.NRT, 1);
+ replicaCount.increment(Replica.Type.NRT);
+ replicaCount.increment(Replica.Type.TLOG);
+
+ assertTrue(replicaCount.contains(Replica.Type.NRT));
+ assertTrue(replicaCount.contains(Replica.Type.TLOG));
+ assertFalse(replicaCount.contains(Replica.Type.PULL));
+
+ assertEquals(2, replicaCount.get(Replica.Type.NRT));
+ assertEquals(1, replicaCount.get(Replica.Type.TLOG));
+ }
+
+ @Test
+ public void testDecrement() {
+ ReplicaCount replicaCount = ReplicaCount.empty();
+ replicaCount.put(Replica.Type.NRT, 1);
+ replicaCount.put(Replica.Type.PULL, 2);
+ replicaCount.decrement(Replica.Type.NRT);
+ replicaCount.decrement(Replica.Type.PULL);
+
+ assertTrue(replicaCount.contains(Replica.Type.NRT));
+ assertTrue(replicaCount.contains(Replica.Type.PULL));
+
+ assertEquals(0, replicaCount.get(Replica.Type.NRT));
+ assertEquals(1, replicaCount.get(Replica.Type.PULL));
+ }
+
+ @Test
+ public void testDecrementZero() {
+ ReplicaCount replicaCount = ReplicaCount.empty();
+ replicaCount.decrement(Replica.Type.NRT);
+
+ assertFalse(replicaCount.contains(Replica.Type.NRT));
+ assertEquals(0, replicaCount.get(Replica.Type.NRT));
+ }
+
+ @Test
+ public void testTotal() {
+ ReplicaCount replicaCount = ReplicaCount.empty();
+ replicaCount.put(Replica.Type.NRT, 1);
+ replicaCount.put(Replica.Type.TLOG, 2);
+
+ assertEquals(3, replicaCount.total());
+ }
+
+ @Test
+ public void testToString() {
+ ReplicaCount replicaCount = ReplicaCount.empty();
+ replicaCount.put(Replica.Type.NRT, 1);
+ replicaCount.put(Replica.Type.TLOG, 2);
+
+ assertEquals("nrt=1, tlog=2, pull=0", replicaCount.toString());
+ }
+
+ @Test
+ public void testValidate() {
+ ReplicaCount replicaCountNrtOnly = ReplicaCount.of(Replica.Type.NRT, 1);
+ replicaCountNrtOnly.validate(); // Should not throw
+
+ ReplicaCount replicaCountPullOnly = ReplicaCount.of(Replica.Type.PULL, 1);
+ Throwable t = assertThrows(SolrException.class, replicaCountPullOnly::validate);
+ assertEquals(
+ "Unexpected number of replicas (nrt=0, tlog=0, pull=1), there must "
+ + "be at least one leader-eligible replica",
+ t.getMessage());
+ }
+
+ @Test
+ public void testGetLeaderType() {
+ // With only 1 NRT, NRT should be returned.
+ ReplicaCount replicaCount = ReplicaCount.of(Replica.Type.NRT, 1);
+ assertEquals(Replica.Type.NRT, replicaCount.getLeaderType());
+
+ // With 1 NRT and 1 TLOG, NRT is still returned as the leader type, as it comes
+ // first in the order of preference.
+ replicaCount.increment(Replica.Type.TLOG);
+ assertEquals(Replica.Type.NRT, replicaCount.getLeaderType());
+
+ // With only 1 TLOG, TLOG should now be returned.
+ replicaCount.decrement(Replica.Type.NRT);
+ assertEquals(Replica.Type.TLOG, replicaCount.getLeaderType());
+ }
+}