You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ds...@apache.org on 2017/12/07 15:58:24 UTC

lucene-solr:branch_7_2: SOLR-11691: Bug: V2 requests for create-alias didn't work when the collections param was an array.

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7_2 a78e4dcd4 -> d17d331ec


SOLR-11691: Bug: V2 requests for create-alias didn't work when the collections param was an array.

(cherry picked from commit fe8dca8)


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/d17d331e
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/d17d331e
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/d17d331e

Branch: refs/heads/branch_7_2
Commit: d17d331ec06f31c1abb625f53d3a0450e0c1c83c
Parents: a78e4dc
Author: David Smiley <ds...@apache.org>
Authored: Thu Dec 7 10:55:50 2017 -0500
Committer: David Smiley <ds...@apache.org>
Committed: Thu Dec 7 10:58:19 2017 -0500

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  2 ++
 .../org/apache/solr/cloud/CreateAliasCmd.java   | 36 ++++++++++++++------
 .../apache/solr/cloud/AliasIntegrationTest.java | 10 ++++--
 3 files changed, 35 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d17d331e/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index f9f4b07..5648727 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -181,6 +181,8 @@ Bug Fixes
 * SOLR-11664: JSON Facet API: range facets containing unique, hll, min, max aggregations over string fields
   produced incorrect results since 7.0 (Volodymyr Rudniev, yonik)
 
+* SOLR-11691: V2 requests for create-alias didn't work when the collections param was an array.
+  (Jason Gerlowski, Gus Heck, David Smiley, noble)
 
 Optimizations
 ----------------------

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d17d331e/solr/core/src/java/org/apache/solr/cloud/CreateAliasCmd.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/cloud/CreateAliasCmd.java b/solr/core/src/java/org/apache/solr/cloud/CreateAliasCmd.java
index 62b65f3..e10d53e 100644
--- a/solr/core/src/java/org/apache/solr/cloud/CreateAliasCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/CreateAliasCmd.java
@@ -21,6 +21,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.solr.cloud.OverseerCollectionMessageHandler.Cmd;
 import org.apache.solr.common.SolrException;
@@ -43,13 +44,14 @@ public class CreateAliasCmd implements Cmd {
   @Override
   public void call(ClusterState state, ZkNodeProps message, NamedList results)
       throws Exception {
-    String aliasName = message.getStr(NAME);
-    String collections = message.getStr("collections"); // could be comma delimited list
+    final String aliasName = message.getStr(NAME);
+    final List<String> canonicalCollectionList = parseCollectionsParameter(message.get("collections"));
+    final String canonicalCollectionsString = StrUtils.join(canonicalCollectionList, ',');
 
     ZkStateReader zkStateReader = ocmh.zkStateReader;
-    validateAllCollectionsExistAndNoDups(collections, zkStateReader);
+    validateAllCollectionsExistAndNoDups(canonicalCollectionList, zkStateReader);
 
-    zkStateReader.aliasesHolder.applyModificationAndExportToZk(aliases -> aliases.cloneWithCollectionAlias(aliasName, collections));
+    zkStateReader.aliasesHolder.applyModificationAndExportToZk(aliases -> aliases.cloneWithCollectionAlias(aliasName, canonicalCollectionsString));
 
     // Sleep a bit to allow ZooKeeper state propagation.
     //
@@ -66,20 +68,34 @@ public class CreateAliasCmd implements Cmd {
     Thread.sleep(100);
   }
 
-  private void validateAllCollectionsExistAndNoDups(String collections, ZkStateReader zkStateReader) {
-    List<String> collectionArr = StrUtils.splitSmart(collections, ",", true);
-    if (new HashSet<>(collectionArr).size() != collectionArr.size()) {
+  private void validateAllCollectionsExistAndNoDups(List<String> collectionList, ZkStateReader zkStateReader) {
+    final String collectionStr = StrUtils.join(collectionList, ',');
+
+    if (new HashSet<>(collectionList).size() != collectionList.size()) {
       throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
-          String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', since it contains duplicates", collections));
+          String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', since it contains duplicates", collectionStr));
     }
     ClusterState clusterState = zkStateReader.getClusterState();
     Set<String> aliasNames = zkStateReader.getAliases().getCollectionAliasListMap().keySet();
-    for (String collection : collectionArr) {
+    for (String collection : collectionList) {
       if (clusterState.getCollectionOrNull(collection) == null && !aliasNames.contains(collection)) {
         throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
-            String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', '%s' is not an existing collection or alias", collections, collection));
+            String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', '%s' is not an existing collection or alias", collectionStr, collection));
       }
     }
   }
+  
+  /**
+   * The v2 API directs that the 'collections' parameter be provided as a JSON array (e.g. ["a", "b"]).  We also
+   * maintain support for the legacy format, a comma-separated list (e.g. a,b).
+   */
+  @SuppressWarnings("unchecked")
+  private List<String> parseCollectionsParameter(Object colls) {
+    if (colls == null) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "missing collections param");
+    if (colls instanceof List) return (List<String>) colls;
+    return StrUtils.splitSmart(colls.toString(), ",", true).stream()
+        .map(String::trim)
+        .collect(Collectors.toList());
+  }
 
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d17d331e/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java b/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
index 25a850d..d0f0e80 100644
--- a/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
@@ -23,12 +23,14 @@ import java.util.function.Consumer;
 import java.util.function.UnaryOperator;
 
 import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.embedded.JettySolrRunner;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
 import org.apache.solr.client.solrj.impl.HttpSolrClient;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
 import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.request.V2Request;
 import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.cloud.Aliases;
@@ -177,7 +179,6 @@ public class AliasIntegrationTest extends SolrCloudTestCase {
   
   @Test
   public void test() throws Exception {
-
     CollectionAdminRequest.createCollection("collection1", "conf", 2, 1).process(cluster.getSolrClient());
     CollectionAdminRequest.createCollection("collection2", "conf", 1, 1).process(cluster.getSolrClient());
     waitForState("Expected collection1 to be created with 2 shards and 1 replica", "collection1", clusterShape(2, 1));
@@ -240,7 +241,11 @@ public class AliasIntegrationTest extends SolrCloudTestCase {
     //searchSeveralWays("testalias4,testalias5", new SolrQuery("*:*"), 3);
 
     ///////////////
-    CollectionAdminRequest.createAlias("testalias6", "collection2,collection1").process(cluster.getSolrClient());
+    // use v2 API
+    new V2Request.Builder("/collections")
+        .withMethod(SolrRequest.METHOD.POST)
+        .withPayload("{\"create-alias\": {\"name\": \"testalias6\", collections:[\"collection2\",\"collection1\"]}}")
+        .build().process(cluster.getSolrClient());
 
     searchSeveralWays("testalias6", new SolrQuery("*:*"), 6);
 
@@ -328,7 +333,6 @@ public class AliasIntegrationTest extends SolrCloudTestCase {
   }
 
   public void testErrorChecks() throws Exception {
-
     CollectionAdminRequest.createCollection("testErrorChecks-collection", "conf", 2, 1).process(cluster.getSolrClient());
     waitForState("Expected testErrorChecks-collection to be created with 2 shards and 1 replica", "testErrorChecks-collection", clusterShape(2, 1));