Apache Solr open-source search software
SOLR-4034: Check if a collection already exists before trying to create a new one.
Mark Robert Miller3005330649c9
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 1e0e67a99b4..7bbb77352f7 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -226,6 +226,9 @@ Bug Fixes* SOLR-4075: A logical shard that has had all of it's SolrCores unloaded shouldbe removed from the cluster state. (Mark Miller, Gilles Comeau)++* SOLR-4034: Check if a collection already exists before trying to create a+ new one. (Po Rui, Mark Miller)Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.java b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.javaindex e6294941b19..62cc98299ea 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionProcessor.java@@ -158,7 +158,12 @@ public class OverseerCollectionProcessor implements Runnable {}private boolean createCollection(ClusterState clusterState, ZkNodeProps message) {-+ String collectionName = message.getStr("name");+ if(clusterState.getCollections().contains(collectionName)) {+ SolrException.log(log, "collection already exists: " + collectionName);+ return false;+ }+// look at the replication factor and see if it matches reality// if it does not, find best nodes to create more cores@@ -179,6 +184,17 @@ public class OverseerCollectionProcessor implements Runnable {return false;}+ if (numReplicas < 0) {+ SolrException.log(log, REPLICATION_FACTOR + " must be > 0");+ return false;+ }++ if (numShards < 0) {+ SolrException.log(log, "numShards must be > 0");+ return false;+ }++String name = message.getStr("name");String configName = message.getStr("collection.configName");@@ -198,6 +214,14 @@ public class OverseerCollectionProcessor implements Runnable {Collections.shuffle(nodeList);int numNodes = numShards * (numReplicas + 1);+ if (nodeList.size() < numNodes) {+ log.warn("Not enough nodes available to satisfy create collection request for collection:"+ + collectionName+ + " nodes needed:"+ + numNodes+ + " nodes available:" + nodeList.size() + " - using nodes available");+ }+List<String> createOnNodes = nodeList.subList(0, Math.min(nodeList.size(), numNodes));log.info("Create collection " + name + " on " + createOnNodes);diff --git a/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java b/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.javaindex 31a4bf8e2f1..a97629840dd 100644--- a/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java@@ -739,7 +739,7 @@ public class BasicDistributedZkTest extends AbstractFullDistribZkTestBase {for (int i = 0; i < cnt; i++) {createCollection(collectionInfos, i,_TestUtil.nextInt(random(), 0, shardCount) + 1,- _TestUtil.nextInt(random(), 0, 5) + 1);+ _TestUtil.nextInt(random(), 0, 3) + 1);}Set<Entry<String,List<Integer>>> collectionInfosEntrySet = collectionInfos.entrySet();