public void testConsistentHash_probabilities() { AtomicLongMap<Integer> map = AtomicLongMap.create(); Random r = new Random(9); for (int i = 0; i < ITERS; i++) { countRemaps(r.nextLong(), map); } for (int shard = 2; shard <= MAX_SHARDS; shard++) { // Rough: don't exceed 1.2x the expected number of remaps by more than 20 assertTrue(map.get(shard) <= 1.2 * ITERS / shard + 20); } }
private void countRemaps(long h, AtomicLongMap<Integer> map) { int last = 0; for (int shards = 2; shards <= MAX_SHARDS; shards++) { int chosen = Hashing.consistentHash(h, shards); if (chosen != last) { map.incrementAndGet(shards); last = chosen; } } }