Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
SOLR-2592: realtime-get support
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1418762 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
yonik committed Dec 8, 2012
1 parent 09669e7 commit 761ff1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
Expand Up @@ -38,6 +38,8 @@
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
Expand Down Expand Up @@ -356,23 +358,23 @@ public int createSubRequests(ResponseBuilder rb) throws IOException {
CloudDescriptor cloudDescriptor = rb.req.getCore().getCoreDescriptor().getCloudDescriptor();

String collection = cloudDescriptor.getCollectionName();

ClusterState clusterState = zkController.getClusterState();

Map<String, List<String>> shardToId = new HashMap<String, List<String>>();
DocCollection coll = clusterState.getCollection(collection);


Map<String, List<String>> sliceToId = new HashMap<String, List<String>>();
for (String id : allIds) {
int hash = Hash.murmurhash3_x86_32(id, 0, id.length(), 0);
String shard = clusterState.getShard(hash, collection);
Slice slice = coll.getRouter().getTargetSlice(id, null, params, coll);

List<String> idsForShard = shardToId.get(shard);
List<String> idsForShard = sliceToId.get(slice.getName());
if (idsForShard == null) {
idsForShard = new ArrayList<String>(2);
shardToId.put(shard, idsForShard);
sliceToId.put(slice.getName(), idsForShard);
}
idsForShard.add(id);
}

for (Map.Entry<String,List<String>> entry : shardToId.entrySet()) {
for (Map.Entry<String,List<String>> entry : sliceToId.entrySet()) {
String shard = entry.getKey();
String shardIdList = StrUtils.join(entry.getValue(), ',');

Expand Down
22 changes: 22 additions & 0 deletions solr/core/src/test/org/apache/solr/cloud/ShardRoutingTest.java
Expand Up @@ -136,6 +136,14 @@ private void doHashingTest() throws Exception {
doAddDoc("c!doc2");
doAddDoc("d!doc3");
doAddDoc("e!doc4");

doRTG("b!doc1");
doRTG("c!doc2");
doRTG("d!doc3");
doRTG("e!doc4");
doRTG("b!doc1,c!doc2");
doRTG("d!doc3,e!doc4");

commit();

doQuery("b!doc1,c!doc2,d!doc3,e!doc4", "q","*:*");
Expand Down Expand Up @@ -178,6 +186,20 @@ void doQuery(String expectedDocs, String... queryParams) throws Exception {
assertEquals(expectedIds, obtainedIds);
}

void doRTG(String ids) throws Exception {
cloudClient.query(params("qt","/get", "ids",ids));

Set<String> expectedIds = new HashSet<String>( StrUtils.splitSmart(ids, ",", true) );

QueryResponse rsp = cloudClient.query(params("qt","/get", "ids",ids));
Set<String> obtainedIds = new HashSet<String>();
for (SolrDocument doc : rsp.getResults()) {
obtainedIds.add((String) doc.get("id"));
}

assertEquals(expectedIds, obtainedIds);
}

@Override
public void tearDown() throws Exception {
super.tearDown();
Expand Down
17 changes: 0 additions & 17 deletions solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java
Expand Up @@ -212,23 +212,6 @@ private RangeInfo addRangeInfo(String collection) {
return rangeInfo;
}

/**
* Get shard id for hash. This is used when determining which Slice the
* document is to be submitted to.
*/
public String getShard(int hash, String collection) {
RangeInfo rangInfo = getRanges(collection);

int cnt = 0;
for (Range range : rangInfo.ranges) {
if (range.includes(hash)) {
return rangInfo.shardList.get(cnt);
}
cnt++;
}

throw new IllegalStateException("The DocRouter failed");
}

@Override
public String toString() {
Expand Down

0 comments on commit 761ff1a

Please sign in to comment.