|
| 1 | +package com.xqbase.java.hash; |
| 2 | + |
| 3 | +import com.google.common.util.concurrent.AtomicLongMap; |
| 4 | +import junit.framework.TestCase; |
| 5 | + |
| 6 | +import java.util.Random; |
| 7 | + |
| 8 | +/** |
| 9 | + * Consistent Hash Test Case. |
| 10 | + */ |
| 11 | +public class HashingTest extends TestCase { |
| 12 | + |
| 13 | + private static final int ITERS = 10000; |
| 14 | + private static final int MAX_SHARDS = 500; |
| 15 | + |
| 16 | + public void testConsistentHash_correctness() { |
| 17 | + long[] interestingValues = { -1, 0, 1, 2, Long.MAX_VALUE, Long.MIN_VALUE }; |
| 18 | + for (long h : interestingValues) { |
| 19 | + checkConsistentHashCorrectness(h); |
| 20 | + } |
| 21 | + Random r = new Random(7); |
| 22 | + for (int i = 0; i < 20; i++) { |
| 23 | + checkConsistentHashCorrectness(r.nextLong()); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private void checkConsistentHashCorrectness(long hashCode) { |
| 28 | + int last = 0; |
| 29 | + for (int shards = 1; shards <= 100000; shards++) { |
| 30 | + int b = Hashing.consistentHash(hashCode, shards); |
| 31 | + if (b != last) { |
| 32 | + assertEquals(shards - 1, b); |
| 33 | + last = b; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public void testConsistentHash_probabilities() { |
| 39 | + AtomicLongMap<Integer> map = AtomicLongMap.create(); |
| 40 | + Random r = new Random(9); |
| 41 | + for (int i = 0; i < ITERS; i++) { |
| 42 | + countRemaps(r.nextLong(), map); |
| 43 | + } |
| 44 | + for (int shard = 2; shard <= MAX_SHARDS; shard++) { |
| 45 | + // Rough: don't exceed 1.2x the expected number of remaps by more than 20 |
| 46 | + assertTrue(map.get(shard) <= 1.2 * ITERS / shard + 20); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void countRemaps(long h, AtomicLongMap<Integer> map) { |
| 51 | + int last = 0; |
| 52 | + for (int shards = 2; shards <= MAX_SHARDS; shards++) { |
| 53 | + int chosen = Hashing.consistentHash(h, shards); |
| 54 | + if (chosen != last) { |
| 55 | + map.incrementAndGet(shards); |
| 56 | + last = chosen; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check a few "golden" values to see that implementations across languages |
| 63 | + * are equivalent. |
| 64 | + */ |
| 65 | + public void testConsistentHash_linearCongruentialGeneratorCompatibility() { |
| 66 | + int[] golden100 = |
| 67 | + { 0, 55, 62, 8, 45, 59, 86, 97, 82, 59, |
| 68 | + 73, 37, 17, 56, 86, 21, 90, 37, 38, 83 }; |
| 69 | + for (int i = 0; i < golden100.length; i++) { |
| 70 | + assertEquals(golden100[i], Hashing.consistentHash(i, 100)); |
| 71 | + } |
| 72 | + assertEquals(6, Hashing.consistentHash(10863919174838991L, 11)); |
| 73 | + assertEquals(3, Hashing.consistentHash(2016238256797177309L, 11)); |
| 74 | + assertEquals(5, Hashing.consistentHash(1673758223894951030L, 11)); |
| 75 | + assertEquals(80343, Hashing.consistentHash(2, 100001)); |
| 76 | + assertEquals(22152, Hashing.consistentHash(2201, 100001)); |
| 77 | + assertEquals(15018, Hashing.consistentHash(2202, 100001)); |
| 78 | + } |
| 79 | +} |
0 commit comments