-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ConsistentHashingSelector.java
99 lines (90 loc) · 3.22 KB
/
ConsistentHashingSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http:https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.net.endpoint.impl;
import io.vertx.core.net.endpoint.EndpointNode;
import io.vertx.core.net.endpoint.EndpointSelector;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* Consistent hashing selector.
*
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class ConsistentHashingSelector implements EndpointSelector {
private final List<? extends EndpointNode> endpoints;
private final SortedMap<Long, EndpointNode> nodes;
private final EndpointSelector fallbackSelector;
public ConsistentHashingSelector(List<? extends EndpointNode> endpoints, int numberOfVirtualNodes, EndpointSelector fallbackSelector) {
MessageDigest instance;
try {
instance = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedOperationException(e);
}
SortedMap<Long, EndpointNode> ring = new TreeMap<>();
for (EndpointNode node : endpoints) {
for (int idx = 0;idx < numberOfVirtualNodes;idx++) {
String nodeId = node.key() + "-" + idx;
long hash = hash(instance, nodeId.getBytes(StandardCharsets.UTF_8));
ring.put(hash, node);
}
}
this.nodes = ring;
this.endpoints = endpoints;
this.fallbackSelector = fallbackSelector;
}
// MD5 : 16 bytes
private static long hash(MessageDigest md, byte[] data) {
md.reset();
md.update(data);
byte[] digest = md.digest();
byte b0 = (byte)(digest[0] ^ digest[8]);
byte b1 = (byte)(digest[1] ^ digest[9]);
byte b2 = (byte)(digest[2] ^ digest[10]);
byte b3 = (byte)(digest[3] ^ digest[11]);
byte b4 = (byte)(digest[4] ^ digest[12]);
byte b5 = (byte)(digest[5] ^ digest[13]);
byte b6 = (byte)(digest[6] ^ digest[14]);
byte b7 = (byte)(digest[7] ^ digest[15]);
return ((long)b0 << 56) | ((long)b1 << 48) | ((long)b2 << 40) | ((long)b3 << 32)
| ((long)b4 << 24) | ((long)b5 << 16) | ((long)b6 << 8) | (long)b7;
}
@Override
public int select() {
return fallbackSelector.select();
}
@Override
public int select(String key) {
if (key == null) {
throw new NullPointerException("No null routing key accepted");
}
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedOperationException(e);
}
long hash = hash(md, key.getBytes(StandardCharsets.UTF_8));
SortedMap<Long, EndpointNode> map = nodes.tailMap(hash);
Long val;
if (map.isEmpty()) {
val = nodes.firstKey();
} else {
val = map.firstKey();
}
EndpointNode endpoint = nodes.get(val);
return endpoints.indexOf(endpoint); // TODO IMPROVE THAT
}
}