-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathConsistentHash.java
More file actions
174 lines (126 loc) · 5.02 KB
/
ConsistentHash.java
File metadata and controls
174 lines (126 loc) · 5.02 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package ch.supsi.dti.isin.consistenthash;
import java.util.Collection;
import org.nerd4j.utils.lang.Require;
import org.nerd4j.utils.math.PrimeSieve;
import ch.supsi.dti.isin.cluster.Node;
import ch.supsi.dti.isin.consistenthash.anchor.AnchorHash;
import ch.supsi.dti.isin.consistenthash.dx.DxHash;
import ch.supsi.dti.isin.consistenthash.jump.JumpHash;
import ch.supsi.dti.isin.consistenthash.jumpback.JumpBackHash;
import ch.supsi.dti.isin.consistenthash.maglev.MaglevHash;
import ch.supsi.dti.isin.consistenthash.multiprobe.MultiProbeHash;
import ch.supsi.dti.isin.consistenthash.rendezvous.RendezvousHash;
import ch.supsi.dti.isin.consistenthash.ring.RingHash;
import ch.supsi.dti.isin.hashfunction.HashFunction;
/**
* Represents a Consistent hashing altoritm.
*
*
* @author Massimo Coluzzi
*/
public interface ConsistentHash
{
/** The {@link HashFunction.Algorithm} to be used by default in consistent hash algorithms. */
public static final HashFunction.Algorithm DEFAULT_HASH_ALGOTITHM = HashFunction.Algorithm.XX;
/** The {@link HashFunction} to be used by default in consistent hash algorithms. */
public static final HashFunction DEFAULT_HASH_FUNCTION = HashFunction.create( DEFAULT_HASH_ALGOTITHM );
/**
* Returns the node associated to the given key.
*
* @param key the key to check
* @return the related node
*/
Node getNode( String key );
/**
* Makes the algorithm aware of new nodes in the cluster.
*
* @param nodes the nodes to add
*/
void addNodes( Collection<? extends Node> nodes );
/**
* Makes the algorithm aware of the removal of nodes from the cluster.
*
* @param nodes the nodes to remove
*/
void removeNodes( Collection<? extends Node> nodes );
/**
* Tells if the algorithm supports only removals in LIFO order.
*
* @return {@code true} if nodes can be removed only in LIFO order.
*/
boolean supportsOnlyLifoRemovals();
/**
* Returns the number of nodes in the cluster.
*
* @return number of nodes in the cluster.
*/
int nodeCount();
/**
* Returns the actual implementation of the algorithm.
*
* @return the actual implementation of the algorithm
*/
Object engine();
/* ***************** */
/* FACTORY METHODS */
/* ***************** */
/**
* Creates a new consistent hash function that uses the given algorithm.
*
* @param algorithm the algorithm to use
* @return a new hash function
*/
public static ConsistentHash create(
ConsistentHash.Algorithm algorithm,
HashFunction.Algorithm function,
Collection<? extends Node> nodes
)
{
Require.nonNull( algorithm, "The algorithm to use is mandatory" );
Require.nonNull( function, "The hash function to use is mandatory" );
Require.nonEmpty( nodes, "The cluster must have at least one node" );
final HashFunction hash = HashFunction.create( function );
switch( algorithm )
{
case ANCHOR_HASH: return new AnchorHash( nodes, nodes.size() << 1, hash );
case DX_HASH: return new DxHash( nodes, nodes.size() << 1, hash );
case JUMP_HASH: return new JumpHash( nodes, hash );
case MAGLEV_HASH:
final int lookupSize = (int) PrimeSieve.get().getSmallestPrimeGreaterEqual( nodes.size() << 7 );
return new MaglevHash( nodes, lookupSize, hash );
case MULTIPROBE_HASH: return new MultiProbeHash( nodes, hash );
case RENDEZVOUS_HASH: return new RendezvousHash( nodes, hash );
case RING_HASH: return new RingHash( nodes, hash );
case JUMP_BACK_HASH: return new JumpBackHash( nodes, hash );
default:
throw new IllegalArgumentException( "Unknown algorithm " + algorithm );
}
}
/* *************** */
/* INNER CLASSES */
/* *************** */
/**
* Enumerates the available implementation algorithms.
*
* @author Massimo Coluzzi
*/
enum Algorithm
{
/** {@code https://scm.ti-edu.ch/attachments/download/2035/AnchorHash.pdf} */
ANCHOR_HASH,
/** {@code https://arxiv.org/pdf/2107.07930.pdf} */
DX_HASH,
/** {@code https://arxiv.org/pdf/1406.2294.pdf} */
JUMP_HASH,
/** {@code https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/44824.pdf} */
MAGLEV_HASH,
/** {@code https://arxiv.org/pdf/1505.00062.pdf} */
MULTIPROBE_HASH,
/** {@code https://ieeexplore.ieee.org/abstract/document/663936} */
RENDEZVOUS_HASH,
/** {@code https://www.cs.princeton.edu/courses/archive/fall09/cos518/papers/chash.pdf} */
RING_HASH,
/** {@code https://github.com/dynatrace-oss/hash4j/blob/v0.17.0/src/main/java/com/dynatrace/hash4j/consistent/ConsistentJumpBackBucketHasher.java} */
JUMP_BACK_HASH;
}
}