Skip to content

Apache Lucene open-source search software

SOLR-2592: avoid splitting composite router hash domains

Yonik Seeley5b04be1195e8
diff --git a/solr/core/src/test/org/apache/solr/cloud/TestHashPartitioner.java b/solr/core/src/test/org/apache/solr/cloud/TestHashPartitioner.java
index d493ff44e00..37f9ca72110 100644
--- a/solr/core/src/test/org/apache/solr/cloud/TestHashPartitioner.java
+++ b/solr/core/src/test/org/apache/solr/cloud/TestHashPartitioner.java
@@ -47,13 +47,22 @@ public class TestHashPartitioner extends SolrTestCaseJ4 {
assertEquals(0x00000000, ranges.get(1).min);
assertEquals(0x7fffffff, ranges.get(1).max);
- ranges = hp.partitionRange(2, 0, 0x7fffffff);
+ ranges = hp.partitionRange(2, new DocRouter.Range(0, 0x7fffffff));
assertEquals(0x00000000, ranges.get(0).min);
assertEquals(0x3fffffff, ranges.get(0).max);
assertEquals(0x40000000, ranges.get(1).min);
assertEquals(0x7fffffff, ranges.get(1).max);
- for (int i = 1; i <= 30000; i += 13) {
+ int defaultLowerBits = 0x0000ffff;
+
+ for (int i = 1; i <= 30000; i++) {
+ // start skipping at higher numbers
+ if (i > 100) i+=13;
+ else if (i > 1000) i+=31;
+ else if (i > 5000) i+=101;
+
+ long rangeSize = 0x0000000100000000L / i;
+
ranges = hp.partitionRange(i, hp.fullRange());
assertEquals(i, ranges.size());
assertTrue("First range does not start before " + Integer.MIN_VALUE
@@ -69,6 +78,32 @@ public class TestHashPartitioner extends SolrTestCaseJ4 {
assertEquals(range, newRange);
}
+ // ensure that ranges are contiguous and that size deviations are not too large.
+ int lastEnd = Integer.MIN_VALUE - 1;
+ for (Range range : ranges) {
+ int currStart = range.min;
+ int currEnd = range.max;
+ assertEquals(lastEnd+1, currStart);
+
+ if (ranges.size() < 4000) {
+ // ranges should be rounded to avoid crossing hash domains
+ assertEquals(defaultLowerBits, currEnd & defaultLowerBits);
+
+ // given our rounding condition that domains should be less than 1/16 of the step size,
+ // this means that any sizing deviations should also be less than 1/16th of the idealized range size.
+ // boolean round = rangeStep >= (1<<bits)*16;
+
+ long currRangeSize = (long)currEnd - (long)currStart;
+ long error = Math.abs(rangeSize - currRangeSize);
+ assertTrue( error < rangeSize/16);
+ }
+
+
+ // String s = range.toString();
+ // Range newRange = hp.fromString(s);
+ // assertEquals(range, newRange);
+ lastEnd = currEnd;
+ }
}
}
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.java
index 6694d9e553a..ab98ed5c575 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
@@ -141,4 +141,58 @@ public class CompositeIdRouter extends HashBasedRouter {
return targetSlices;
}
+
+
+ @Override
+ public List<Range> partitionRange(int partitions, Range range) {
+ int min = range.min;
+ int max = range.max;
+
+ assert max >= min;
+ if (partitions == 0) return Collections.EMPTY_LIST;
+ long rangeSize = (long)max - (long)min;
+ long rangeStep = Math.max(1, rangeSize / partitions);
+
+ List<Range> ranges = new ArrayList<Range>(partitions);
+
+ long start = min;
+ long end = start;
+
+ // keep track of the idealized target to avoid accumulating rounding errors
+ long targetStart = min;
+ long targetEnd = targetStart;
+
+ // 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.
+ boolean round = rangeStep >= (1<<bits)*16;
+
+ while (end < max) {
+ targetEnd = targetStart + rangeStep;
+ end = targetEnd;
+
+ if (round && ((end & mask2) != mask2)) {
+ // round up or down?
+ int increment = 1 << bits; // 0x00010000
+ long roundDown = (end | mask2) - increment ;
+ long roundUp = (end | mask2) + increment;
+ if (end - roundDown < roundUp - end && roundDown > start) {
+ end = roundDown;
+ } else {
+ end = roundUp;
+ }
+ }
+
+ // make last range always end exactly on MAX_VALUE
+ if (ranges.size() == partitions - 1) {
+ end = max;
+ }
+ ranges.add(new Range((int)start, (int)end));
+ start = end + 1L;
+ targetStart = targetEnd + 1L;
+ }
+
+ return ranges;
+ }
+
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java b/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java
index d6ef17e71b1..85f301eb80f 100644
--- a/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java
@@ -124,18 +124,17 @@ public abstract class DocRouter {
return new Range(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
- public List<Range> partitionRange(int partitions, Range range) {
- return partitionRange(partitions, range.min, range.max);
- }
-
/**
* Returns the range for each partition
*/
- public List<Range> partitionRange(int partitions, int min, int max) {
+ public List<Range> partitionRange(int partitions, Range range) {
+ int min = range.min;
+ int max = range.max;
+
assert max >= min;
if (partitions == 0) return Collections.EMPTY_LIST;
- long range = (long)max - (long)min;
- long srange = Math.max(1, range / partitions);
+ long rangeSize = (long)max - (long)min;
+ long rangeStep = Math.max(1, rangeSize / partitions);
List<Range> ranges = new ArrayList<Range>(partitions);
@@ -143,7 +142,7 @@ public abstract class DocRouter {
long end = start;
while (end < max) {
- end = start + srange;
+ end = start + rangeStep;
// make last range always end exactly on MAX_VALUE
if (ranges.size() == partitions - 1) {
end = max;