Apache Lucene open-source search software
SOLR-5295: The CREATESHARD collection API creates maxShardsPerNode number of replicas if replicationFactor is not specified
Shalin Shekhar Mangarb883f6f5b88f
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex da096434a1e..271dc64728b 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -82,6 +82,9 @@ Bug Fixes* SOLR-4590: Collections API should return a nice error when not in SolrCloud mode.(Anshum Gupta, Mark Miller)+* SOLR-5295: The CREATESHARD collection API creates maxShardsPerNode number of+ replicas if replicationFactor is not specified. (Brett Hoerner, shalin)+Security----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.java b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.javaindex 71b61e95f1c..43355c043d3 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.java@@ -386,7 +386,7 @@ public class OverseerCollectionProcessor implements Runnable, ClosableThread {}private boolean createShard(ClusterState clusterState, ZkNodeProps message, NamedList results) throws KeeperException, InterruptedException {- log.info("create shard invoked");+ log.info("Create shard invoked: {}", message);String collectionName = message.getStr(COLLECTION_PROP);String shard = message.getStr(SHARD_ID_PROP);if(collectionName == null || shard ==null)@@ -395,19 +395,18 @@ public class OverseerCollectionProcessor implements Runnable, ClosableThread {DocCollection collection = clusterState.getCollection(collectionName);int maxShardsPerNode = collection.getInt(MAX_SHARDS_PER_NODE, 1);- int repFactor = message.getInt(REPLICATION_FACTOR, collection.getInt(MAX_SHARDS_PER_NODE, 1));-// int minReplicas = message.getInt("minReplicas",repFactor);- String createNodeSetStr =message.getStr(CREATE_NODE_SET);+ int repFactor = message.getInt(REPLICATION_FACTOR, collection.getInt(REPLICATION_FACTOR, 1));+ String createNodeSetStr = message.getStr(CREATE_NODE_SET);ArrayList<Node> sortedNodeList = getNodesForNewShard(clusterState, collectionName, numSlices, maxShardsPerNode, repFactor, createNodeSetStr);Overseer.getInQueue(zkStateReader.getZkClient()).offer(ZkStateReader.toJSON(message));- // wait for a while until we don't see the collection+ // wait for a while until we see the shardlong waitUntil = System.currentTimeMillis() + 30000;boolean created = false;while (System.currentTimeMillis() < waitUntil) {Thread.sleep(100);- created = zkStateReader.getClusterState().getCollection(collectionName).getSlice(shard) !=null;+ created = zkStateReader.getClusterState().getCollection(collectionName).getSlice(shard) != null;if (created) break;}if (!created)diff --git a/solr/core/src/test/org/apache/solr/cloud/CustomCollectionTest.java b/solr/core/src/test/org/apache/solr/cloud/CustomCollectionTest.javaindex 9cad1f07c97..5a13681c2e8 100644--- a/solr/core/src/test/org/apache/solr/cloud/CustomCollectionTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/CustomCollectionTest.java@@ -135,6 +135,7 @@ public class CustomCollectionTest extends AbstractFullDistribZkTestBase {public void doTest() throws Exception {testCustomCollectionsAPI();testRouteFieldForHashRouter();+ testCreateShardRepFactor();if (DEBUG) {super.printLayout();}@@ -423,7 +424,49 @@ public class CustomCollectionTest extends AbstractFullDistribZkTestBase {}+ private void testCreateShardRepFactor() throws Exception {+ String collectionName = "testCreateShardRepFactor";+ HashMap<String, List<Integer>> collectionInfos = new HashMap<String, List<Integer>>();+ CloudSolrServer client = null;+ try {+ client = createCloudClient(null);+ Map<String, Object> props = ZkNodeProps.makeMap(+ REPLICATION_FACTOR, 1,+ MAX_SHARDS_PER_NODE, 5,+ NUM_SLICES, 2,+ "shards", "a,b",+ "router.name", "implicit");+ createCollection(collectionInfos, collectionName, props, client);+ } finally {+ if (client != null) client.shutdown();+ }+ ZkStateReader zkStateReader = getCommonCloudSolrServer().getZkStateReader();+ waitForRecoveriesToFinish(collectionName, zkStateReader, false);++ ModifiableSolrParams params = new ModifiableSolrParams();+ params.set("action", CollectionAction.CREATESHARD.toString());+ params.set("collection", collectionName);+ params.set("shard", "x");+ SolrRequest request = new QueryRequest(params);+ request.setPath("/admin/collections");+ createNewSolrServer("", getBaseUrl((HttpSolrServer) clients.get(0))).request(request);++ waitForRecoveriesToFinish(collectionName, zkStateReader, false);++ int replicaCount = 0;+ int attempts = 0;+ while (true) {+ if (attempts > 30) fail("Not enough active replicas in the shard 'x'");+ zkStateReader.updateClusterState(true);+ attempts++;+ replicaCount = zkStateReader.getClusterState().getSlice(collectionName, "x").getReplicas().size();+ if (replicaCount >= 1) break;+ Thread.sleep(500);+ }++ assertEquals("CREATESHARD API created more than replicationFactor number of replicas", 1, replicaCount);+ }public static String getUrlFromZk(ClusterState clusterState, String collection) {