@Test public void mappedServersTest() { Map<Integer, String> servers = new HashMap<>(); servers.put(0, "server0"); servers.put(1, "server1"); HashFunction md5 = Hashing.md5(); List<PartitionEntry> triggers = generateTriggers(3, 1000); Map<Integer, String> newPartition; Map<Integer, String> oldPartition; print("initial - test 2 servers " + servers.toString()); newPartition = new HashMap<>(); for (PartitionEntry trigger : triggers) { newPartition.put( trigger.hashCode(), servers.get(Hashing.consistentHash(md5.hashInt(trigger.hashCode()), 2))); } for (int buckets = 3; buckets < 10; buckets++) { servers.put(buckets - 1, "server" + (buckets - 1)); print("test " + buckets + " servers " + servers.toString()); oldPartition = newPartition; newPartition = new HashMap<>(); for (PartitionEntry trigger : triggers) { newPartition.put( trigger.hashCode(), servers.get(Hashing.consistentHash(md5.hashInt(trigger.hashCode()), buckets))); } int changes = comparePartitions(oldPartition, newPartition); print( "Changes from " + (buckets - 1) + " to " + buckets + " servers: " + changes + " of " + oldPartition.size()); print("" + (((float) changes / (float) oldPartition.size()) * 100) + " % moved"); print( "K(" + oldPartition.size() + ")/n(" + buckets + "): " + ((float) oldPartition.size() / (float) buckets)); } }
@Test public void consistentHashTest() { HashFunction md5 = Hashing.md5(); List<PartitionEntry> triggers = generateTriggers(3, 1000); Map<Integer, Integer> newPartition; Map<Integer, Integer> oldPartition; print("initial - test 2 servers"); newPartition = new HashMap<>(); for (PartitionEntry trigger : triggers) { newPartition.put( trigger.hashCode(), Hashing.consistentHash(md5.hashInt(trigger.hashCode()), 2)); } for (int buckets = 3; buckets < 10; buckets++) { print("test " + buckets + " servers"); oldPartition = newPartition; newPartition = new HashMap<>(); for (PartitionEntry trigger : triggers) { newPartition.put( trigger.hashCode(), Hashing.consistentHash(md5.hashInt(trigger.hashCode()), buckets)); } int changes = comparePartitions(oldPartition, newPartition); print( "Changes from " + (buckets - 1) + " to " + buckets + " servers: " + changes + " of " + oldPartition.size()); print("" + (((float) changes / (float) oldPartition.size()) * 100) + " % moved"); print( "K(" + oldPartition.size() + ")/n(" + buckets + "): " + ((float) oldPartition.size() / (float) buckets)); } for (int buckets = 10; buckets > 3; buckets--) { print("test " + buckets + " servers"); oldPartition = newPartition; newPartition = new HashMap<>(); for (PartitionEntry trigger : triggers) { newPartition.put( trigger.hashCode(), Hashing.consistentHash(md5.hashInt(trigger.hashCode()), buckets)); } int changes = comparePartitions(oldPartition, newPartition); print( "Changes from " + (buckets) + " to " + (buckets - 1) + " servers: " + changes + " of " + oldPartition.size()); print("" + (((float) changes / (float) oldPartition.size()) * 100) + " % moved"); print( "K(" + oldPartition.size() + ")/n(" + buckets + "): " + ((float) oldPartition.size() / (float) buckets)); } }