-
Notifications
You must be signed in to change notification settings - Fork 0
/
RMINode.java
309 lines (266 loc) · 8.68 KB
/
RMINode.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/**
* Implements a node in the Chord network.
*
* @author Daniel McDonald
*/
public class RMINode implements RMINodeServer {
/**
* The key for the node.
*/
private long nodeKey;
/**
* The length of the hash.
*/
private int hashLength;
/**
* The finger table for this node.
*/
private FingerTable fingerTable;
/**
* The node's predecessor.
*/
private RMINodeServer predecessor;
/**
* The file logger.
*/
private NodeFileLogger logger;
/**
* A periodic task for updating the finger table.
*/
private final ScheduledExecutorService periodicTask = Executors.newScheduledThreadPool(1);
/**
* Log the current state to the output file.
* @param message
*/
private void logState(String message){
String s = getNodeKey() + ": p=";
try{
s += predecessor.getNodeKey();
}
catch(Throwable t) {
s += "-";
}
logger.logOutput(s + " " + fingerTable.toString() + " --> " + message);
}
/**
* Constructs an RMINode.
*
* @param hashLength Length of the hash.
* @param key The key to use.
* @throws RemoteException
*/
public RMINode(int hashLength, long key) throws RemoteException {
double keySpace = Math.pow(2, hashLength);
if(key > keySpace)
throw new IllegalArgumentException("key (" + key + ") must be less than the keyspace provided by hashLength (" + keySpace + ")");
this.hashLength = hashLength;
this.nodeKey = key;
fingerTable = new FingerTable(this);
logger = new NodeFileLogger(key);
}
/**
* Join the given network.
*
* @param fromNetwork The node to join.
* @throws RemoteException
*/
public void join(RMINodeServer fromNetwork) throws RemoteException {
if(fromNetwork != null) {
RMINodeServer successor = fromNetwork.findSuccessor(getNodeKey());
if(successor.getNodeKey() == getNodeKey())
throw new IllegalArgumentException("A node with this key already exists in the network");
fingerTable.getSuccessor().setNode(successor);
predecessor = successor.findPredecessor(successor.getNodeKey());
successor.checkPredecessor(this);
for(Finger f: fingerTable)
f.setNode(fromNetwork.findSuccessor(f.getStart()));
logState("node " + getNodeKey() + " added to network by " + fromNetwork.getNodeKey());
}
else {
//this is currently the only node in the network, so set all fingers and predecessor to self
for(Finger f: fingerTable)
f.setNode(this);
predecessor = this;
logState("new network created by " + getNodeKey());
}
periodicTask.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try{
stabilize();
fixFinger(fingerTable.getRandomFinger());
} catch(Throwable t){
Log.err("error running periodic task: " + t.getClass());
t.printStackTrace();
}
}
}, 5, 1, TimeUnit.SECONDS);
}
/**
* Tests whether the key is within the given interval.
* @param leftInclusive <code>true</code> if this should be left inclusive.
* @param left The left edge of the interval.
* @param x The key to test.
* @param right The right edge of the interval.
* @param rightInclusive <code>true</code> if this should be right inclusive.
* @return <code>true</code> if the key is within the interval.
*/
private boolean isWithinInterval(boolean leftInclusive, long left, long x, long right, boolean rightInclusive) {
//if the bounds have been violated, it's not within the interval
if((!leftInclusive && left == x) || (!rightInclusive && right == x))
return false;
//if left and right are the same, the entire key space is contained in the set. And since we know the bounds are correct, we know that x is in the interval
if(left == right)
return true;
//if left > right, then we're in a situation where the interval spans the end and beginning of the ring. The logic is a bit different there
if(left > right)
return left <= x || x <= right;
return left <= x && x <= right;
}
/**
* Tests whether the key is in range.
* @param key The key to test.
* @return <code>true</code> if the key is in range
* @throws RemoteException
*/
private boolean isInRange(long key) throws RemoteException {
if(predecessor == null)
predecessor = findPredecessor(getNodeKey());
try {
//special case: if we're the only node in the network, then we're our own predecessor. That means we own the whole god-damned thing
if(predecessor == null || predecessor.getNodeKey() == getNodeKey())
return true;
return isWithinInterval(false, predecessor.getNodeKey(), key, getNodeKey(), true);
}
catch (RemoteException e){
predecessor = null; //if we got a remote exception, then the predecessor is no longer online
throw e;
}
}
/**
* {@inheritDoc}
*/
@Override
public long getNodeKey() { return nodeKey; }
/**
* {@inheritDoc}
*/
@Override
public int getHashLength() { return hashLength; }
/**
* {@inheritDoc}
*/
@Override
public Serializable get(String key) throws RemoteException {
long hash = new KeyHash<String>(key, getHashLength()).getHash();
if(isInRange(hash))
throw new NotImplementedException();
return findSuccessor(hash).get(key);
}
/**
* {@inheritDoc}
*/
@Override
public void put(String key, Serializable value) throws RemoteException {
long hash = new KeyHash<String>(key, getHashLength()).getHash();
if(isInRange(hash))
throw new NotImplementedException();
findSuccessor(hash).put(key, value);
}
/**
* {@inheritDoc}
*/
@Override
public void delete(String key) throws RemoteException {
long hash = new KeyHash<String>(key, getHashLength()).getHash();
if(isInRange(hash))
throw new NotImplementedException();
findSuccessor(hash).delete(key);
}
/**
* {@inheritDoc}
*/
@Override
public RMINodeServer findSuccessor(long key) throws RemoteException {
//if the key belongs to our interval, then we're the successor
if(isInRange(key))
return this;
RMINodeServer predecessor = findPredecessor(key);
if(predecessor.getNodeKey() == getNodeKey())
return fingerTable.getSuccessor().getNode();
return predecessor.findSuccessor(key);
}
/**
* {@inheritDoc}
*/
@Override
public RMINodeServer findPredecessor(long key) throws RemoteException {
//if the key belongs to our interval, then return our predecessor
if(isInRange(key))
return predecessor;
//if the key belongs to the interval of our successor, then we're the predecessor
if(isWithinInterval(false, getNodeKey(), key, fingerTable.getSuccessor().getNode().getNodeKey(), true))
return this;
for(Finger f : fingerTable.reverse())
try {
if(f.getNode() != null && isWithinInterval(false, getNodeKey(), f.getNode().getNodeKey(), key, false))
return f.getNode().findPredecessor(key);
}
catch (RemoteException e) {
fixFinger(f);
}
return predecessor.findPredecessor(key);
}
/**
* {@inheritDoc}
*/
@Override
public void checkPredecessor(RMINodeServer potentialPredecessor) throws RemoteException {
if(predecessor == null || isWithinInterval(false, predecessor.getNodeKey(), potentialPredecessor.getNodeKey(), getNodeKey(), false)) {
this.predecessor = potentialPredecessor;
logState(getNodeKey() + ".predecessor = " + potentialPredecessor.getNodeKey());
//TODO: update range, reassign values, etc
}
}
/**
* Fix the given finger in the finger table.
*
* @param finger The finger to fix.
*/
private void fixFinger(Finger finger) {
try {
RMINodeServer successor = findSuccessor(finger.getStart());
if(finger.getNode() != null && finger.getNode().getNodeKey() != successor.getNodeKey()) {
finger.setNode(successor);
logState(getNodeKey() + ".finger[" + finger.getStart() + "] = " + successor.getNodeKey());
}
} catch (RemoteException e) {
finger.setNode(null);
}
}
/**
* Stabilize the finger table.
*/
private void stabilize() {
RMINodeServer successor = fingerTable.getSuccessor().getNode();
if(successor != null) {
try {
RMINodeServer successor_predecessor = successor.findPredecessor(successor.getNodeKey());
if(isWithinInterval(false, getNodeKey(), successor_predecessor.getNodeKey(), successor.getNodeKey(), false) && successor_predecessor.getNodeKey() != fingerTable.getSuccessor().getNode().getNodeKey()) {
fingerTable.getSuccessor().setNode(successor_predecessor);
logState(getNodeKey() + ".successor = " + successor_predecessor.getNodeKey());
}
fingerTable.getSuccessor().getNode().checkPredecessor(this);
} catch (RemoteException e) {
fixFinger(fingerTable.getSuccessor());
return;
}
}
}
}