From 9c47892d4ea8f70beff0f5a8357d749e843c2ca2 Mon Sep 17 00:00:00 2001 From: Mark Robert Miller Date: Wed, 23 Oct 2013 17:11:44 +0000 Subject: [PATCH] SOLR-5380: Using cloudSolrServer.setDefaultCollection(collectionId) does not work as intended for an alias spanning more than 1 collection. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1535076 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 4 +++ .../solr/cloud/AliasIntegrationTest.java | 6 ++++ .../client/solrj/impl/CloudSolrServer.java | 30 +++++++++++-------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 8d7d42f700c..2140a9d1ad7 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -122,6 +122,10 @@ Bug Fixes * SOLR-5363: Solr doesn't start up properly with Log4J2 (Petar Tahchiev via Alan Woodward) +* SOLR-5380: Using cloudSolrServer.setDefaultCollection(collectionId) does not + work as intended for an alias spanning more than 1 collection. + (Thomas Egense, Shawn Heisey, Mark Miller) + Optimizations ---------------------- 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 0815bf699c7..16d9b48467d 100644 --- a/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java @@ -157,6 +157,12 @@ public class AliasIntegrationTest extends AbstractFullDistribZkTestBase { query = new SolrQuery("*:*"); query.set("collection", "testalias"); res = cloudSolrServer.query(query); + assertEquals(5, res.getResults().getNumFound()); + + // Try with setDefaultCollection + query = new SolrQuery("*:*"); + cloudSolrServer.setDefaultCollection("testalias"); + res = cloudSolrServer.query(query); cloudSolrServer.shutdown(); assertEquals(5, res.getResults().getNumFound()); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java index b6537be818b..1130e15cd0b 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrServer.java @@ -546,17 +546,7 @@ public class CloudSolrServer extends SolrServer { throw new SolrException(ErrorCode.BAD_REQUEST, "Could not find collection: " + collection); } - collection = collectionsList.iterator().next(); - - StringBuilder collectionString = new StringBuilder(); - Iterator it = collectionsList.iterator(); - for (int i = 0; i < collectionsList.size(); i++) { - String col = it.next(); - collectionString.append(col); - if (i < collectionsList.size() - 1) { - collectionString.append(","); - } - } + // TODO: not a big deal because of the caching, but we could avoid looking // at every shard // when getting leaders if we tweaked some things @@ -592,10 +582,24 @@ public class CloudSolrServer extends SolrServer { || !coreNodeProps.getState().equals(ZkStateReader.ACTIVE)) continue; if (nodes.put(node, nodeProps) == null) { if (!sendToLeaders || (sendToLeaders && coreNodeProps.isLeader())) { - String url = coreNodeProps.getCoreUrl(); + String url; + if (reqParams.get("collection") == null) { + url = ZkCoreNodeProps.getCoreUrl( + nodeProps.getStr(ZkStateReader.BASE_URL_PROP), + defaultCollection); + } else { + url = coreNodeProps.getCoreUrl(); + } urlList2.add(url); } else if (sendToLeaders) { - String url = coreNodeProps.getCoreUrl(); + String url; + if (reqParams.get("collection") == null) { + url = ZkCoreNodeProps.getCoreUrl( + nodeProps.getStr(ZkStateReader.BASE_URL_PROP), + defaultCollection); + } else { + url = coreNodeProps.getCoreUrl(); + } replicas.add(url); } }