Created
February 9, 2017 09:52
-
-
Save daschl/4e5148ddc8d296381c29b9d00e56097f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public String hash(final NodeInfo info, final int repetition) { | |
String hostname = info.hostname().getHostAddress(); | |
if (hostname.startsWith("/")) { | |
hostname = hostname.substring(1); | |
} | |
int port = info.services().get(ServiceType.BINARY); | |
return hostname + ":" + port + "-" + repetition; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void populateKetamaNodes() { | |
for (NodeInfo node : nodes()) { | |
if (!node.services().containsKey(ServiceType.BINARY)) { | |
continue; | |
} | |
for (int i = 0; i < 40; i++) { | |
MessageDigest md5; | |
try { | |
md5 = MessageDigest.getInstance("MD5"); | |
md5.update(env.memcachedHashingStrategy().hash(node, i).getBytes(CharsetUtil.UTF_8)); | |
byte[] digest = md5.digest(); | |
for (int j = 0; j < 4; j++) { | |
Long key = ((long) (digest[3 + j * 4] & 0xFF) << 24) | |
| ((long) (digest[2 + j * 4] & 0xFF) << 16) | |
| ((long) (digest[1 + j * 4] & 0xFF) << 8) | |
| (digest[j * 4] & 0xFF); | |
ketamaNodes.put(key, node); | |
} | |
} catch (NoSuchAlgorithmException e) { | |
throw new IllegalStateException("Could not populate ketama nodes.", e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment