-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsistentHashFactory.java
121 lines (93 loc) · 3.73 KB
/
ConsistentHashFactory.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ch.supsi.dti.isin.benchmark.adapter;
import java.util.Collection;
import java.util.function.Supplier;
import org.nerd4j.utils.lang.Require;
import ch.supsi.dti.isin.benchmark.config.AlgorithmConfig;
import ch.supsi.dti.isin.benchmark.config.InvalidConfigException;
import ch.supsi.dti.isin.benchmark.executor.InitTime;
import ch.supsi.dti.isin.cluster.Node;
import ch.supsi.dti.isin.consistenthash.ConsistentHash;
import ch.supsi.dti.isin.hashfunction.HashFunction;
/**
* The current factory aims to uniform the way consistent hashing algorithms are created.
* Authors who want to test a new consistent hashing algorithm must implement the current
* interface according to their algorithm and related configurations.
*
* @author Samuel De Babo Martins
* @author Massimo Coluzzi
*/
public abstract class ConsistentHashFactory
{
/** The configuration to use to create consistent hash algorithms. */
protected final AlgorithmConfig config;
/**
* Constructor with parameters.
*
* @param config the configuration to use
*/
protected ConsistentHashFactory( AlgorithmConfig config )
{
super();
this.config = validate(
Require.nonNull( config, "The algorithm configuration is mandatory" )
);
}
/* ******************* */
/* INTERFACE METHODS */
/* ******************* */
/**
* Creates a new instance of the consistent hashing algorithm.
*
* @param hash hash function to use inside the consistent hashing algorithm
* @param nodes list of initial nodes
* @param config additional configurations for the algorithm
* @return a new object implementing the {@link ConsistentHash} interface
*/
public abstract ConsistentHash createConsistentHash( HashFunction hash, Collection<? extends Node> nodes );
/**
* Returns a {@link Supplier} creating a new instance of the consistent hashing engine.
* <p>
* The {@link Supplier} returned by this method is intended to be used in the {@link InitTime}
* benchmark to test the algorithm's initialization time.
*
* @param hash hash function to use inside the consistent hashing algorithm
* @param nodes list of initial nodes
* @param config additional configurations for the algorithm
* @return a {@link Supplier} used to test initialization time
*/
public abstract Supplier<?> createEngineInitializer( HashFunction hash, Collection<? extends Node> nodes );
/**
* Creates an instance of {@link ConsistentHashEnginePilot}.
* <p>
* The engine pilot is used to uniform operations on engines without losing performance.
*
* @param hash hash function to use inside the consistent hashing algorithm
* @param nodes list of initial nodes
* @param config additional configurations for the algorithm
* @return a new object implementing the {@link ConsistentHashEnginePilot} interface
*/
public abstract ConsistentHashEnginePilot<?> createEnginePilot( ConsistentHash consistentHash );
/* **************** */
/* PUBLIC METHODS */
/* **************** */
/**
* Returns the {@link AlgorithmConfig} related to the current factory.
*
* @return the consistent hash algorithm configuration
*/
public AlgorithmConfig getConfig()
{
return config;
}
/* ***************** */
/* EXTENSION HOOKS */
/* ***************** */
/**
* Checks if the provided configuration is valid
* for the related consistent hash algorithm.
*
* @param config the configuration to validate
* @throws InvalidConfigException if the validation fails
*/
protected abstract AlgorithmConfig validate( AlgorithmConfig config );
}