SOLR-2957: collection URLs in a cluster

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1242800 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2012-02-10 14:52:44 +00:00
parent 8a50cefc6b
commit 76f0587508
2 changed files with 88 additions and 13 deletions

View File

@ -25,6 +25,8 @@ import java.io.OutputStreamWriter;
import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.WeakHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -42,6 +44,10 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.cloud.CloudState;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.FastWriter;
import org.apache.solr.common.util.ContentStreamBase;
@ -178,10 +184,24 @@ public class SolrDispatchFilter implements Filter
}
}
if (core == null) {
corename = "";
if (cores.isZooKeeperAware() && corename.length() == 0) {
core = cores.getCore("");
} else if (!cores.isZooKeeperAware()) {
core = cores.getCore("");
}
}
}
if (core == null && cores.isZooKeeperAware()) {
// we couldn't find the core - lets make sure a collection was not specified instead
core = getCoreByCollection(cores, core, corename, path);
if (core != null) {
// we found a core, update the path
path = path.substring( idx );
}
// TODO: if we couldn't find it locally, look on other nodes
}
// With a valid core...
if( core != null ) {
@ -280,6 +300,48 @@ public class SolrDispatchFilter implements Filter
chain.doFilter(request, response);
}
private SolrCore getCoreByCollection(CoreContainer cores, SolrCore core,
String corename, String path) {
String collection = corename;
ZkStateReader zkStateReader = cores.getZkController().getZkStateReader();
CloudState cloudState = zkStateReader.getCloudState();
Map<String,Slice> slices = cloudState.getSlices(collection);
// look for a core on this node
Set<Entry<String,Slice>> entries = slices.entrySet();
done:
for (Entry<String,Slice> entry : entries) {
// first see if we have the leader
ZkNodeProps leaderProps = cloudState.getLeader(collection, entry.getKey());
core = checkProps(cores, core, path, leaderProps);
if (core != null) {
break done;
}
// check everyone then
Map<String,ZkNodeProps> shards = entry.getValue().getShards();
Set<Entry<String,ZkNodeProps>> shardEntries = shards.entrySet();
for (Entry<String,ZkNodeProps> shardEntry : shardEntries) {
ZkNodeProps zkProps = shardEntry.getValue();
core = checkProps(cores, core, path, zkProps);
if (core != null) {
break done;
}
}
}
return core;
}
private SolrCore checkProps(CoreContainer cores, SolrCore core, String path,
ZkNodeProps zkProps) {
String corename;
if (cores.getZkController().getNodeName().equals(zkProps.get(ZkStateReader.NODE_NAME_PROP))) {
corename = zkProps.get(ZkStateReader.CORE_NAME_PROP);
core = cores.getCore(corename);
}
return core;
}
private void handleAdminRequest(HttpServletRequest req, ServletResponse response, SolrRequestHandler handler,
SolrQueryRequest solrReq) throws IOException {
SolrQueryResponse solrResp = new SolrQueryResponse();

View File

@ -62,6 +62,7 @@ public class BasicDistributedZkTest extends AbstractDistributedZkTestCase {
private Map<String,List<SolrServer>> otherCollectionClients = new HashMap<String,List<SolrServer>>();
private Map<String,List<SolrServer>> oneInstanceCollectionClients = new HashMap<String,List<SolrServer>>();
private String oneInstanceCollection = "oneInstanceCollection";;
public BasicDistributedZkTest() {
fixShardCount = true;
@ -245,27 +246,39 @@ public class BasicDistributedZkTest extends AbstractDistributedZkTestCase {
testMultipleCollections();
testANewCollectionInOneInstance();
testSearchByCollectionName();
// Thread.sleep(10000000000L);
if (DEBUG) {
super.printLayout();
}
}
private void testSearchByCollectionName() throws SolrServerException {
SolrServer client = clients.get(0);
String baseUrl = ((CommonsHttpSolrServer) client).getBaseURL();
// the cores each have different names, but if we add the collection name to the url
// we should get mapped to the right core
SolrServer client1 = createNewSolrServer(oneInstanceCollection, baseUrl);
SolrQuery query = new SolrQuery("*:*");
long oneDocs = client1.query(query).getResults().getNumFound();
assertEquals(3, oneDocs);
}
private void testANewCollectionInOneInstance() throws Exception {
String collection = "oneInstanceCollection";
List<SolrServer> collectionClients = new ArrayList<SolrServer>();
SolrServer client = clients.get(0);
oneInstanceCollectionClients.put(collection, collectionClients);
oneInstanceCollectionClients.put(oneInstanceCollection , collectionClients);
String baseUrl = ((CommonsHttpSolrServer) client).getBaseURL();
createCollection(collection, collectionClients, baseUrl, 1);
createCollection(collection, collectionClients, baseUrl, 2);
createCollection(collection, collectionClients, baseUrl, 3);
createCollection(collection, collectionClients, baseUrl, 4);
createCollection(oneInstanceCollection, collectionClients, baseUrl, 1);
createCollection(oneInstanceCollection, collectionClients, baseUrl, 2);
createCollection(oneInstanceCollection, collectionClients, baseUrl, 3);
createCollection(oneInstanceCollection, collectionClients, baseUrl, 4);
SolrServer client1 = createNewSolrServer(collection + "1", baseUrl);
SolrServer client2 = createNewSolrServer(collection + "2", baseUrl);
SolrServer client3 = createNewSolrServer(collection + "3", baseUrl);
SolrServer client4 = createNewSolrServer(collection + "4", baseUrl);
SolrServer client1 = createNewSolrServer(oneInstanceCollection + "1", baseUrl);
SolrServer client2 = createNewSolrServer(oneInstanceCollection + "2", baseUrl);
SolrServer client3 = createNewSolrServer(oneInstanceCollection + "3", baseUrl);
SolrServer client4 = createNewSolrServer(oneInstanceCollection + "4", baseUrl);
client2.add(getDoc(id, "1"));
client3.add(getDoc(id, "2"));
@ -279,7 +292,7 @@ public class BasicDistributedZkTest extends AbstractDistributedZkTestCase {
long threeDocs = client3.query(query).getResults().getNumFound();
long fourDocs = client4.query(query).getResults().getNumFound();
query.set("collection", collection);
query.set("collection", oneInstanceCollection);
query.set("distrib", true);
long allDocs = solrj.query(query).getResults().getNumFound();