Apache Lucene open-source search software
SOLR-5320: Added support for tri-level compositeId routing
Shalin Shekhar Mangarf61d23263064
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 92188b0e279..9a4df220116 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -78,6 +78,9 @@ New Features* SOLR-5441: Expose number of transaction log files and their size via JMX.(Rafał Kuć via shalin)+* SOLR-5320: Added support for tri-level compositeId routing.+ (Anshum Gupta via shalin)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/update/SolrIndexSplitter.java b/solr/core/src/java/org/apache/solr/update/SolrIndexSplitter.javaindex 168906501f3..094103b77c5 100644--- a/solr/core/src/java/org/apache/solr/update/SolrIndexSplitter.java+++ b/solr/core/src/java/org/apache/solr/update/SolrIndexSplitter.java@@ -215,7 +215,7 @@ public class SolrIndexSplitter {}public static String getRouteKey(String idString) {- int idx = idString.indexOf(CompositeIdRouter.separator);+ int idx = idString.indexOf(CompositeIdRouter.SEPARATOR);if (idx <= 0) return null;String part1 = idString.substring(0, idx);int commaIdx = part1.indexOf(CompositeIdRouter.bitsSeparator);diff --git a/solr/core/src/test/org/apache/solr/cloud/ShardRoutingTest.java b/solr/core/src/test/org/apache/solr/cloud/ShardRoutingTest.javaindex ff0819f5e99..679b1dd9c78 100644--- a/solr/core/src/test/org/apache/solr/cloud/ShardRoutingTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ShardRoutingTest.java@@ -102,6 +102,10 @@ public class ShardRoutingTest extends AbstractFullDistribZkTestBase {hash of x is 3e9a9b1b high bits=0 shard=shard3hash of y is 477d9216 high bits=1 shard=shard4hash of z is c1f69a17 high bits=3 shard=shard2++ hash of f1 is 313bf6b1+ hash of f2 is ff143f8+***/}@@ -143,28 +147,31 @@ public class ShardRoutingTest extends AbstractFullDistribZkTestBase {doAddDoc("c!doc2");doAddDoc("d!doc3");doAddDoc("e!doc4");+ doAddDoc("f1!f2!doc5");doRTG("b!doc1");doRTG("c!doc2");doRTG("d!doc3");doRTG("e!doc4");+ doRTG("f1!f2!doc5");doRTG("b!doc1,c!doc2");doRTG("d!doc3,e!doc4");commit();- doQuery("b!doc1,c!doc2,d!doc3,e!doc4", "q","*:*");- doQuery("b!doc1,c!doc2,d!doc3,e!doc4", "q","*:*", "shards","shard1,shard2,shard3,shard4");- doQuery("b!doc1,c!doc2,d!doc3,e!doc4", "q","*:*", shardKeys,"b!,c!,d!,e!");+ doQuery("b!doc1,c!doc2,d!doc3,e!doc4,f1!f2!doc5", "q","*:*");+ doQuery("b!doc1,c!doc2,d!doc3,e!doc4,f1!f2!doc5", "q","*:*", "shards","shard1,shard2,shard3,shard4");+ doQuery("b!doc1,c!doc2,d!doc3,e!doc4,f1!f2!doc5", "q","*:*", shardKeys,"b!,c!,d!,e!,f1!f2!");doQuery("b!doc1", "q","*:*", shardKeys,"b!");doQuery("c!doc2", "q","*:*", shardKeys,"c!");- doQuery("d!doc3", "q","*:*", shardKeys,"d!");+ doQuery("d!doc3,f1!f2!doc5", "q","*:*", shardKeys,"d!");doQuery("e!doc4", "q","*:*", shardKeys,"e!");+ doQuery("f1!f2!doc5,d!doc3", "q","*:*", shardKeys,"f1/8!");// try using shards parameterdoQuery("b!doc1", "q","*:*", "shards",bucket1);doQuery("c!doc2", "q","*:*", "shards",bucket2);- doQuery("d!doc3", "q","*:*", "shards",bucket3);+ doQuery("d!doc3,f1!f2!doc5", "q","*:*", "shards",bucket3);doQuery("e!doc4", "q","*:*", "shards",bucket4);@@ -174,26 +181,27 @@ public class ShardRoutingTest extends AbstractFullDistribZkTestBase {doQuery("b!doc1,c!doc2", "q","*:*", shardKeys,"b,c"); // query shards that would contain *documents* "b" and "c" (i.e. not prefixes). The upper bits are the same, so the shards should be the same.doQuery("b!doc1,c!doc2", "q","*:*", shardKeys,"b/1!"); // top bit of hash(b)==1, so shard1 and shard2- doQuery("d!doc3,e!doc4", "q","*:*", shardKeys,"d/1!"); // top bit of hash(b)==0, so shard3 and shard4+ doQuery("d!doc3,e!doc4,f1!f2!doc5", "q","*:*", shardKeys,"d/1!"); // top bit of hash(b)==0, so shard3 and shard4doQuery("b!doc1,c!doc2", "q","*:*", shardKeys,"b!,c!");- doQuery("b!doc1,c!doc2,d!doc3,e!doc4", "q","*:*", shardKeys,"foo/0!");+ doQuery("b!doc1,f1!f2!doc5,c!doc2,d!doc3,e!doc4", "q","*:*", shardKeys,"foo/0!");// test targeting deleteByQuery at only certain shardsdoDBQ("*:*", shardKeys,"b!");commit();- doQuery("c!doc2,d!doc3,e!doc4", "q","*:*");+ doQuery("c!doc2,d!doc3,e!doc4,f1!f2!doc5", "q","*:*");doAddDoc("b!doc1");- doDBQ("*:*", shardKeys,"c!");+ doDBQ("*:*", shardKeys,"f1!");commit();- doQuery("b!doc1,d!doc3,e!doc4", "q","*:*");- doAddDoc("c!doc2");+ doQuery("b!doc1,c!doc2,e!doc4", "q","*:*");+ doAddDoc("f1!f2!doc5");+ doAddDoc("d!doc3");doDBQ("*:*", shardKeys,"c!");commit();- doQuery("b!doc1,d!doc3,e!doc4", "q","*:*");+ doQuery("b!doc1,f1!f2!doc5,d!doc3,e!doc4", "q","*:*");doAddDoc("c!doc2");doDBQ("*:*", shardKeys,"d!,e!");@@ -201,15 +209,12 @@ public class ShardRoutingTest extends AbstractFullDistribZkTestBase {doQuery("b!doc1,c!doc2", "q","*:*");doAddDoc("d!doc3");doAddDoc("e!doc4");+ doAddDoc("f1!f2!doc5");commit();}----public void doTestNumRequests() throws Exception {log.info("### STARTING doTestNumRequests");@@ -263,6 +268,11 @@ public class ShardRoutingTest extends AbstractFullDistribZkTestBase {leader2.client.solrClient.query( params("q","*:*", "shard.keys","b!,d!") );nEnd = getNumRequests();assertEquals(5, nEnd - nStart); // original + 2 phase distrib search * 2 shards.++ nStart = getNumRequests();+ leader2.client.solrClient.query( params("q","*:*", "shard.keys","b!,f1!f2!") );+ nEnd = getNumRequests();+ assertEquals(5, nEnd - nStart);}public void doAtomicUpdate() throws Exception {@@ -279,9 +289,10 @@ public class ShardRoutingTest extends AbstractFullDistribZkTestBase {Object val = ((Map)rsp.getResponse().get("doc")).get("foo_i");assertEquals((Integer)expectedVal, val);}+}- long getNumRequests() {+ long getNumRequests() {long n = controlJetty.getDebugFilter().getTotalRequests();for (JettySolrRunner jetty : jettys) {n += jetty.getDebugFilter().getTotalRequests();diff --git a/solr/core/src/test/org/apache/solr/cloud/TriLevelCompositeIdRoutingTest.java b/solr/core/src/test/org/apache/solr/cloud/TriLevelCompositeIdRoutingTest.javanew file mode 100644index 00000000000..7d5aa129222--- /dev/null+++ b/solr/core/src/test/org/apache/solr/cloud/TriLevelCompositeIdRoutingTest.java@@ -0,0 +1,167 @@+package org.apache.solr.cloud;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.solr.client.solrj.response.QueryResponse;+import org.apache.solr.common.SolrDocument;+import org.junit.BeforeClass;++import java.util.HashMap;+import java.util.HashSet;+import java.util.Random;+import java.util.Set;+++public class TriLevelCompositeIdRoutingTest extends ShardRoutingTest {++ int NUM_APPS = 5;+ int NUM_USERS = 10;+ int NUM_DOCS = 100;+++ @BeforeClass+ public static void beforeTriLevelCompositeIdRoutingTest() throws Exception {+ // TODO: we use an fs based dir because something+ // like a ram dir will not recover correctly right now+ // because tran log will still exist on restart and ram+ // dir will not persist - perhaps translog can empty on+ // start if using an EphemeralDirectoryFactory+ useFactory(null);+ }++ public TriLevelCompositeIdRoutingTest() {+ schemaString = "schema15.xml"; // we need a string id+ super.sliceCount = 12; // a lot of slices for more ranges and buckets+ super.shardCount = 24;+ super.fixShardCount = true;++ }++ @Override+ public void doTest() throws Exception {+ boolean testFinished = false;+ try {+ handle.clear();+ handle.put("QTime", SKIPVAL);+ handle.put("timestamp", SKIPVAL);++ // todo: do I have to do this here?+ waitForRecoveriesToFinish(true);++ doTriLevelHashingTest();+ doTriLevelHashingTestWithBitMask();++ testFinished = true;+ } finally {+ if (!testFinished) {+ printLayoutOnTearDown = true;+ }+ }+ }+++ private void doTriLevelHashingTest() throws Exception {+ log.info("### STARTING doTriLevelHashingTest");+ // for now, we know how ranges will be distributed to shards.+ // may have to look it up in clusterstate if that assumption changes.++ Random r = new Random();++ for (int i=0;i < NUM_DOCS;i++) {+ int appId = r.nextInt(NUM_APPS) + 1;+ int userId = r.nextInt(NUM_USERS) + 1;++ String id = "app" + appId + "!" + "user" + userId + "!" + "doc" + r.nextInt(100);+ doAddDoc(id);++ }++ commit();++ HashMap<String, Integer> idMap = new HashMap<String, Integer>();++ for(int i=1;i<=sliceCount;i++) {++ Set<String> ids = doQueryGetUniqueIdKeys("q", "*:*", "shards", "shard" + i);+ for(String id:ids) {+ assertFalse("Found the same route key [" + id + "] in 2 shards.", idMap.containsKey(id));+ idMap.put(getKey(id), i);+ }+ }++ }+++ private void doTriLevelHashingTestWithBitMask() throws Exception {+ log.info("### STARTING doTriLevelHashingTestWithBitMask");+ // for now, we know how ranges will be distributed to shards.+ // may have to look it up in clusterstate if that assumption changes.+ del("*:*");++ Random r = new Random();++ for (int i=0;i < NUM_DOCS;i++) {+ int appId = r.nextInt(NUM_APPS) + 1;+ int userId = r.nextInt(NUM_USERS) + 1;+ int bitMask = r.nextInt(16) + 1;++ String id = "app" + appId + "/" + bitMask + "!" + "user" + userId + "!" + "doc" + r.nextInt(100);+ doAddDoc(id);++ }++ commit();++ HashMap<String, Integer> idMap = new HashMap<String, Integer>();++ for(int i=1;i<=sliceCount;i++) {++ Set<String> ids = doQueryGetUniqueIdKeys("q", "*:*", "shards", "shard" + i);+ for(String id:ids) {+ assertFalse("Found the same route key [" + id + "] in 2 shards.", idMap.containsKey(id));+ idMap.put(getKey(id), i);+ }+ }++ }++ void doAddDoc(String id) throws Exception {+ index("id",id);+ // todo - target diff servers and use cloud clients as well as non-cloud clients+ }++ Set<String> doQueryGetUniqueIdKeys(String... queryParams) throws Exception {+ QueryResponse rsp = cloudClient.query(params(queryParams));+ Set<String> obtainedIdKeys = new HashSet<String>();+ Set<String> obtainedIdKeys2 = new HashSet<String>();+ for (SolrDocument doc : rsp.getResults()) {+ obtainedIdKeys.add(getKey((String) doc.get("id")));+ }+ return obtainedIdKeys;+ }++ private String getKey(String id) {+ return id.substring(0, id.lastIndexOf('!'));+ }++ @Override+ public void tearDown() throws Exception {+ super.tearDown();+ }++}diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.java b/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.javaindex 06892f47a4d..53411791b39 100644--- a/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.java+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.java@@ -29,35 +29,18 @@ import java.util.List;//// user!uniqueid+// app!user!uniqueid// user/4!uniqueid+// app/2!user/4!uniqueid//public class CompositeIdRouter extends HashBasedRouter {public static final String NAME = "compositeId";- public static final int separator = '!';+ public static final String SEPARATOR = "!";// separator used to optionally specify number of bits to allocate toward first part.public static final int bitsSeparator = '/';private int bits = 16;- private int mask1 = 0xffff0000;- private int mask2 = 0x0000ffff;-- protected void setBits(int firstBits) {- this.bits = firstBits;- // java can't shift 32 bits- mask1 = firstBits==0 ? 0 : (-1 << (32-firstBits));- mask2 = firstBits==32 ? 0 : (-1 >>> firstBits);- }-- protected int getBits(String firstPart, int commaIdx) {- int v = 0;- for (int idx = commaIdx + 1; idx<firstPart.length(); idx++) {- char ch = firstPart.charAt(idx);- if (ch < '0' || ch > '9') return -1;- v = v * 10 + (ch - '0');- }- return v > 32 ? -1 : v;- }@Overridepublic int sliceHash(String id, SolrInputDocument doc, SolrParams params, DocCollection collection) {@@ -68,55 +51,26 @@ public class CompositeIdRouter extends HashBasedRouter {throw new SolrException (SolrException.ErrorCode.BAD_REQUEST, "No value for :"+shardFieldName + ". Unable to identify shard");id = o.toString();}- int idx = id.indexOf(separator);- if (idx < 0) {+ if (id.indexOf(SEPARATOR) < 0) {return Hash.murmurhash3_x86_32(id, 0, id.length(), 0);}- String part1 = id.substring(0, idx);- int commaIdx = part1.indexOf(bitsSeparator);- int m1 = mask1;- int m2 = mask2;-- if (commaIdx > 0) {- int firstBits = getBits(part1, commaIdx);- if (firstBits >= 0) {- m1 = firstBits==0 ? 0 : (-1 << (32-firstBits));- m2 = firstBits==32 ? 0 : (-1 >>> firstBits);- part1 = part1.substring(0, commaIdx);- }- }- String part2 = id.substring(idx+1);-- int hash1 = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);- int hash2 = Hash.murmurhash3_x86_32(part2, 0, part2.length(), 0);- return (hash1 & m1) | (hash2 & m2);+ return new KeyParser(id).getHash();}++ /**+ * Get Range for a given CompositeId based route key+ * @param routeKey+ * @return+ */public Range keyHashRange(String routeKey) {- int idx = routeKey.indexOf(separator);- if (idx < 0) {+ if (routeKey.indexOf(SEPARATOR) < 0) {int hash = sliceHash(routeKey, null, null, null);return new Range(hash, hash);}- String part1 = routeKey.substring(0, idx);- int commaIdx = part1.indexOf(bitsSeparator);- int m1 = mask1;- int m2 = mask2;-- if (commaIdx > 0) {- int firstBits = getBits(part1, commaIdx);- if (firstBits >= 0) {- m1 = firstBits==0 ? 0 : (-1 << (32-firstBits));- m2 = firstBits==32 ? 0 : (-1 >>> firstBits);- part1 = part1.substring(0, commaIdx);- }- }- int hash = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);- int min = hash & m1;- int max = min | m2;- return new Range(min, max);+ return new KeyParser(routeKey).getRange();}@Override@@ -128,41 +82,12 @@ public class CompositeIdRouter extends HashBasedRouter {}String id = shardKey;- int idx = shardKey.indexOf(separator);- if (idx < 0) {+ if (shardKey.indexOf(SEPARATOR) < 0) {// shardKey is a simple id, so don't do a rangereturn Collections.singletonList(hashToSlice(Hash.murmurhash3_x86_32(id, 0, id.length(), 0), collection));}- int m1 = mask1;- int m2 = mask2;-- String part1 = id.substring(0,idx);- int bitsSepIdx = part1.indexOf(bitsSeparator);- if (bitsSepIdx > 0) {- int firstBits = getBits(part1, bitsSepIdx);- if (firstBits >= 0) {- m1 = firstBits==0 ? 0 : (-1 << (32-firstBits));- m2 = firstBits==32 ? 0 : (-1 >>> firstBits);- part1 = part1.substring(0, bitsSepIdx);- }- }-- // If the upper bits are 0xF0000000, the range we want to cover is- // 0xF0000000 0xFfffffff-- int hash1 = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);- int upperBits = hash1 & m1;- int lowerBound = upperBits;- int upperBound = upperBits | m2;-- if (m1 == 0) {- // no bits used from first part of key.. the code above will produce 0x000000000->0xffffffff which only works on unsigned space, but we're using signed space.- lowerBound = Integer.MIN_VALUE;- upperBound = Integer.MAX_VALUE;- }-- Range completeRange = new Range(lowerBound, upperBound);+ Range completeRange = new KeyParser(id).getRange();List<Slice> targetSlices = new ArrayList<Slice>(1);for (Slice slice : collection.getActiveSlices()) {@@ -219,17 +144,18 @@ public class CompositeIdRouter extends HashBasedRouter {// Round to avoid splitting hash domains across ranges if such rounding is not significant.// With default bits==16, one would need to create more than 4000 shards before this// becomes false by default.+ int mask = 0x0000ffff;boolean round = rangeStep >= (1<<bits)*16;while (end < max) {targetEnd = targetStart + rangeStep;end = targetEnd;- if (round && ((end & mask2) != mask2)) {+ if (round && ((end & mask) != mask)) {// round up or down?int increment = 1 << bits; // 0x00010000- long roundDown = (end | mask2) - increment ;- long roundUp = (end | mask2) + increment;+ long roundDown = (end | mask) - increment ;+ long roundUp = (end | mask) + increment;if (end - roundDown < roundUp - end && roundDown > start) {end = roundDown;} else {@@ -249,4 +175,121 @@ public class CompositeIdRouter extends HashBasedRouter {return ranges;}+ /**+ * Helper class to calculate parts, masks etc for an id.+ */+ static class KeyParser {+ String key;+ int[] numBits;+ int[] hashes;+ int[] masks;+ boolean triLevel;+ int pieces;++ public KeyParser(String key) {+ String[] parts = key.split(SEPARATOR);+ this.key = key;+ pieces = parts.length;+ hashes = new int[pieces];+ numBits = new int[2];+ if(key.endsWith("!"))+ pieces++;+ if(pieces == 3) {+ numBits[0] = 8;+ numBits[1] = 8;+ triLevel = true;+ } else {+ numBits[0] = 16;+ triLevel = false;++ }++ for(int i=0;i<parts.length;i++) {+ int commaIdx = parts[i].indexOf(bitsSeparator);++ if(commaIdx > 0) {+ numBits[i] = getNumBits(parts[i], commaIdx);+ parts[i] = parts[i].substring(0, commaIdx);+ }+ hashes[i] = Hash.murmurhash3_x86_32(parts[i], 0, parts[i].length(), 0);+ }+ masks = getMasks();+ }++ Range getRange() {+ int lowerBound;+ int upperBound;++ if(triLevel) {+ lowerBound = hashes[0] & masks[0] | hashes[1] & masks[1];+ upperBound = lowerBound | masks[2];+ } else {+ lowerBound = hashes[0] & masks[0];+ upperBound = lowerBound | masks[1];+ }+ // If the upper bits are 0xF0000000, the range we want to cover is+ // 0xF0000000 0xFfffffff++ if ((masks[0] == 0 && !triLevel) || (masks[0] == 0 && masks[1] == 0 && triLevel)) {+ // no bits used from first part of key.. the code above will produce 0x000000000->0xffffffff+ // which only works on unsigned space, but we're using signed space.+ lowerBound = Integer.MIN_VALUE;+ upperBound = Integer.MAX_VALUE;+ }+ Range r = new Range(lowerBound, upperBound);+ return r;+ }++ /**+ * Get bit masks for routing based on routing level+ * @return+ */+ private int[] getMasks() {+ int[] masks;+ if(triLevel)+ masks = getBitMasks(numBits[0], numBits[1]);+ else+ masks = getBitMasks(numBits[0]);++ return masks;+ }++ private int[] getBitMasks(int firstBits, int secondBits) {+ // java can't shift 32 bits+ int[] masks = new int[3];+ masks[0] = firstBits==0 ? 0 : (-1 << (32-firstBits));+ masks[1] = (firstBits + secondBits)==0 ? 0 : (-1 << (32 - firstBits - secondBits));+ masks[1] = masks[0] ^ masks[1];+ masks[2] = (firstBits + secondBits) == 32 ? 0 : ~(masks[0] | masks[1]);+ return masks;+ }++ private int getNumBits(String firstPart, int commaIdx) {+ int v = 0;+ for (int idx = commaIdx + 1; idx<firstPart.length(); idx++) {+ char ch = firstPart.charAt(idx);+ if (ch < '0' || ch > '9') return -1;+ v = v * 10 + (ch - '0');+ }+ return v > 32 ? -1 : v;+ }++ private int[] getBitMasks(int firstBits) {+ // java can't shift 32 bits+ int[] masks;+ masks = new int[2];+ masks[0] = firstBits==0 ? 0 : (-1 << (32-firstBits));+ masks[1] = firstBits==32 ? 0 : (-1 >>> firstBits);+ return masks;+ }++ int getHash() {+ int result = hashes[0] & masks[0];++ for(int i=1;i<pieces;i++)+ result = result | (hashes[i] & masks[i]);+ return result;+ }++ }}