From a12688bc8bab700899af0a525a76c22d79aceab8 Mon Sep 17 00:00:00 2001 From: nishantmonu51 Date: Thu, 10 Jul 2014 12:21:27 +0530 Subject: [PATCH] fix partitionNum calculation & remove extra sorting --- pom.xml | 2 +- .../partition/HashBasedNumberedShardSpec.java | 16 +--------------- .../shard/HashBasedNumberedShardSpecTest.java | 12 ++++++++++++ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 7d270176674..dfa203ec905 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ UTF-8 0.26.5 2.5.0 - 0.2.5 + 0.2.5-SNAPSHOT diff --git a/server/src/main/java/io/druid/timeline/partition/HashBasedNumberedShardSpec.java b/server/src/main/java/io/druid/timeline/partition/HashBasedNumberedShardSpec.java index afcb2feb922..8f347ee6cfd 100644 --- a/server/src/main/java/io/druid/timeline/partition/HashBasedNumberedShardSpec.java +++ b/server/src/main/java/io/druid/timeline/partition/HashBasedNumberedShardSpec.java @@ -30,8 +30,6 @@ import com.google.common.hash.Hashing; import io.druid.data.input.InputRow; import io.druid.data.input.Rows; -import java.util.Collections; -import java.util.Comparator; import java.util.List; public class HashBasedNumberedShardSpec extends NumberedShardSpec @@ -79,24 +77,12 @@ public class HashBasedNumberedShardSpec extends NumberedShardSpec @Override public ShardSpecLookup getLookup(final List shardSpecs) { - // Sort on basis of partitionNumber - Collections.sort( - shardSpecs, new Comparator() - { - @Override - public int compare(ShardSpec o1, ShardSpec o2) - { - return Integer.compare(o1.getPartitionNum(), o2.getPartitionNum()); - } - } - ); - return new ShardSpecLookup() { @Override public ShardSpec getShardSpec(InputRow row) { - int index = (int) ((long) hash(row)) % getPartitions(); + int index = Math.abs(hash(row) % getPartitions()); return shardSpecs.get(index); } }; diff --git a/server/src/test/java/io/druid/server/shard/HashBasedNumberedShardSpecTest.java b/server/src/test/java/io/druid/server/shard/HashBasedNumberedShardSpecTest.java index bfd573dae89..5f176015c2a 100644 --- a/server/src/test/java/io/druid/server/shard/HashBasedNumberedShardSpecTest.java +++ b/server/src/test/java/io/druid/server/shard/HashBasedNumberedShardSpecTest.java @@ -194,4 +194,16 @@ public class HashBasedNumberedShardSpecTest return 0; } } + + @Test + public void testValidity(){ + for(int i=Integer.MIN_VALUE;i<=Integer.MAX_VALUE;i++){ + { + int partitionNum = Math.abs((int) ((long) i % 2)); + if(partitionNum != 0 && partitionNum != 1){ + throw new ISE("for i "+ i+ "partitionNum "+ partitionNum); + } + } + } + } }