Skip to content

Instantly share code, notes, and snippets.

@Barakat
Created August 30, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Barakat/5eedfa481bc979463d7e to your computer and use it in GitHub Desktop.
Save Barakat/5eedfa481bc979463d7e to your computer and use it in GitHub Desktop.
Simple Java consistent hashing implementation, do not use in production
import java.util.Map;
import java.util.TreeMap;
import java.util.SortedMap;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
import java.io.UnsupportedEncodingException;
public class ConsistentHash {
private static int MAX_REPLICA = 100;
private SortedMap<BigInteger, String> entries = new TreeMap<>();
public void put(String node) throws NoSuchAlgorithmException,
UnsupportedEncodingException{
BigInteger key = hash(node);
entries.put(key, node);
for (int i = 0 ; i < MAX_REPLICA ; i++ ) {
key = hash(node + ":" + i);
entries.put(key, node);
}
}
private String get(String entry) throws NoSuchAlgorithmException,
UnsupportedEncodingException{
assert !entries.isEmpty() : "Handle this case properly";
BigInteger key = hash(entry);
if (!entries.containsKey(key)) {
SortedMap<BigInteger, String> tailMap = entries.tailMap(key);
key = tailMap.isEmpty() ? entries.firstKey() : tailMap.firstKey();
}
return entries.get(key);
}
private BigInteger hash(String node) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] checksum = md5.digest(node.getBytes("UTF-8"));
return new BigInteger(1, checksum);
}
public static void main(String[] args) throws Exception {
ConsistentHash s = new ConsistentHash();
for (char c = 'A' ; c <= 'Z' ; c++) {
s.put(String.valueOf(c));
}
for (int i = 0 ; i < 1000 ; i++) {
System.out.print(s.get("user-id-" + i + "-data"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment