-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNode.java
More file actions
572 lines (436 loc) · 14.7 KB
/
Node.java
File metadata and controls
572 lines (436 loc) · 14.7 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import java.net.InetSocketAddress;
import java.util.HashMap;
/**
* Node class that implements the node data structure
* and functionalities of a chord node includes update finger table and response table,
* find predecessor and successor, key query functions(based on closest node or lowest response time),
* and print finger table information.
*
*
*/
public class Node {
private long localId;
private InetSocketAddress localAddress;
private InetSocketAddress predecessor;
private HashMap<Integer, InetSocketAddress> finger;
private HashMap<Integer, Long> responseTime;
private Listener listener;
private Heartbeats heartBeat;
/**
* Constructor
* @param address: this node's local address
*/
public Node (InetSocketAddress address) {
localAddress = address;
localId = Helper.hashSocketAddress(localAddress);
// initialize an empty finge table
finger = new HashMap<Integer, InetSocketAddress>();
responseTime = new HashMap<Integer,Long>();
for (int i = 1; i <= 32; i++) {
updateIthFinger (i, null);
}
// initialize predecessor
predecessor = null;
// initialize threads
listener = new Listener(this);
heartBeat = new Heartbeats(this);
}
/**
* Create or join a ring
* @param contact
* @return true if successfully create a ring
* or join a ring via contact
*/
public boolean join (InetSocketAddress contact) {
// if contact is other node (join ring), try to contact that node
// (contact will never be null)
if (contact != null && !contact.equals(localAddress)) {
InetSocketAddress successor = Helper.requestAddress(contact, "FINDSUCC_" + localId);
if (successor == null) {
System.out.println("\nCannot find node you are trying to contact. Please exit.\n");
return false;
}
updateIthFinger(1, successor);
}
InetSocketAddress prevSock = null;
long prevTime = 0;
// build finger table and response time table//
for(int i=1;i<=32;i++){
long startTime = System.nanoTime();
InetSocketAddress ithFinger = findSuccessor(Helper.ithStart(getId(),i));
long elapsedTime = System.nanoTime() - startTime;
if(prevSock!=null && prevSock==ithFinger) elapsedTime = prevTime;
updateFingers(i,ithFinger);
responseTime.put(i,elapsedTime);
//updateResponse(i,elapsedTime);
prevSock = ithFinger;
prevTime = elapsedTime;
}
// start all threads
listener.start();
heartBeat.start();
return true;
}
/**
* update the response time of ith server in the finger table
* @param i int value ith key
* @param addr the socket address of the node we want to test response time
*/
private void updateResponse(int i, InetSocketAddress addr){
long Rtime = -1;
if(addr==null){
//responseTime.put(i,Rtime);
//return;
}
else if(addr.equals(localAddress)){
//responseTime.put(i,(long)0);
Rtime = 0;
}
else if(Rtime==-1){
long startTime = System.nanoTime();
String respon = Helper.sendRequest(addr,"KEEP");
//InetSocketAddress target = find_successor(Helper.hashSocketAddress(addr)-1);
//InetSocketAddress target = Helper.requestAddress(addr,"KEEP");
if(respon!=null) Rtime = System.nanoTime() - startTime;
}
for(int j=1;j<=32;j++){
InetSocketAddress cur = finger.get(j);
if(cur==null) responseTime.put(j,(long)-1);
else if(cur.equals(addr)) responseTime.put(j,Rtime);
else if(j==i) responseTime.put(j,Rtime);
}
}
/**
* Notify successor that this node should be its predecessor
* @param successor
* @return successor's response
*/
public String notify(InetSocketAddress successor) {
if (successor!=null && !successor.equals(localAddress))
return Helper.sendRequest(successor, "IAMPRE_"+localAddress.getAddress().toString()+":"+localAddress.getPort());
else
return null;
}
/**
* Being notified by another node, set it as my predecessor if it is.
* @param newpre
*/
public void notified (InetSocketAddress newpre) {
if (predecessor == null || predecessor.equals(localAddress)) {
this.setPredecessor(newpre);
}
else {
long oldpre_id = Helper.hashSocketAddress(predecessor);
long local_relative_id = Helper.computeRelativeId(localId, oldpre_id);
long newpre_relative_id = Helper.computeRelativeId(Helper.hashSocketAddress(newpre), oldpre_id);
if (newpre_relative_id > 0 && newpre_relative_id < local_relative_id)
this.setPredecessor(newpre);
}
}
/**
* From the finger table of the node, choose the closest node that small or equal target id
* @param id
* @return ip address and port
*/
public InetSocketAddress findClosestNode (long id){
long target = Helper.computeRelativeId(id,localId); // get distance
//InetSocketAddress targetAddr = null;
// from larget to small to check finger table
for(int i=32; i>=1;i--){
InetSocketAddress ithFinger = finger.get(i);
if (ithFinger == null) {
continue;
}
long ithFingerID = Helper.hashSocketAddress(ithFinger);
long ithFingerRela = Helper.computeRelativeId(ithFingerID,localId);
if(ithFingerRela>0 && ithFingerRela<target ){
String respon = Helper.sendRequest(ithFinger,"KEEP");
if(respon!=null) return ithFinger;
else updateFingers(-2,ithFinger);
}
}
return localAddress;
}
public InetSocketAddress findLeastResponseTime(long id){
long target = Helper.computeRelativeId(id,localId); // get distance
InetSocketAddress targetAddr = null;
long targetTime = Long.MAX_VALUE;
// from larget to small to check finger table
for(int i=1; i<=32;i++){
InetSocketAddress ithFinger = finger.get(i);
if (ithFinger == null) {
continue;
}
long ithFingerID = Helper.hashSocketAddress(ithFinger);
long ithFingerRela = Helper.computeRelativeId(ithFingerID,localId);
long ithTime = responseTime.get(i);
if(ithFingerRela > 0 && ithFingerRela < target && ithTime < targetTime){
//String respon = Helper.sendRequest(ithFinger,"KEEP");
//if(respon!=null){
targetAddr = ithFinger;
targetTime = ithTime;
//}
//else updateFingers(-2,ithFinger);
}
}
//System.out.println(targetTime);
if(targetTime!=Long.MAX_VALUE) return targetAddr;
return localAddress;
}
/**
* Ask current node to find id's successor.
* @param id
* @return id's successor's socket address
*/
public InetSocketAddress findSuccessor(long id) {
// initialize return value as this node's successor (might be null)
InetSocketAddress res = this.getSuccessor();
// find predecessor
InetSocketAddress pre = findPredecessor(id);
// if other node found, ask it for its successor
if (!pre.equals(localAddress))
res = Helper.requestAddress(pre, "YOURSUCC");
// if res is still null, set it as local node, return
if (res == null)
res = localAddress;
return res;
}
/**
* Calculate relative distance between current node and target id to find id's predecessor
* @param id
* @return id's successor's socket address
*/
private InetSocketAddress findPredecessor(long id) {
InetSocketAddress cur = this.localAddress;
InetSocketAddress mostRecLlive = this.localAddress;
InetSocketAddress curSuc = this.getSuccessor();
long curSucID = 0;
if (curSuc != null)
curSucID = Helper.computeRelativeId(Helper.hashSocketAddress(curSuc), Helper.hashSocketAddress(cur));
long idRelatID = Helper.computeRelativeId(id, Helper.hashSocketAddress(cur));
while (!(idRelatID > 0 && idRelatID <= curSucID)) {
// temporarily save current node
InetSocketAddress preCur = cur;
// if current node is local node, find my closest
if (cur.equals(this.localAddress)) {
cur = this.findClosestNode(id);
}
// else current node is remote node, sent request to it for its closest
else {
InetSocketAddress result = Helper.requestAddress(cur, "FINDCLOS_" + id);
// if fail to get response, set cur to most recently
if (result == null) {
cur = mostRecLlive;
curSuc = Helper.requestAddress(cur, "YOURSUCC");
if (curSuc==null) {
System.out.println("It's not possible.");
return localAddress;
}
continue;
}
// if cur's closest is itself, return cur
else if (result.equals(cur))
return result;
// else cur's closest is other node "result"
else {
// set cur as most recently alive
mostRecLlive = cur;
// ask "result" for its successor
curSuc = Helper.requestAddress(result, "YOURSUCC");
// if we can get its response, then "result" must be our next cur
if (curSuc!=null) {
cur = result;
}
// else cur sticks, ask cur's successor
else {
curSuc = Helper.requestAddress(cur, "YOURSUCC");
}
}
// compute relative ids for while loop judgement
curSucID = Helper.computeRelativeId(Helper.hashSocketAddress(curSuc), Helper.hashSocketAddress(cur));
idRelatID = Helper.computeRelativeId(id, Helper.hashSocketAddress(cur));
}
if (preCur.equals(cur))
break;
}
return cur;
}
/**
* Update the finger table based on parameters.
* Synchronize, all threads trying to modify
* finger table only through this method.
* @param i: index or command code
* @param value
*/
public synchronized void updateFingers(int i, InetSocketAddress value) {
// valid index in [1, 32], just update the ith finger
if (i >= 1 && i <= 32) {
updateIthFinger(i, value);
}
// caller wants to delete the successor
else if (i == -1) {
InetSocketAddress suc = getSuccessor();
if(suc==null) return;
// if predeccessor equals to successor, delete it
if(predecessor!=null && predecessor.equals(suc)) setPredecessor(null);
//delete the node from the finger table into null if it's equals to successor
for(int j=1;j<=32;j++){
InetSocketAddress ithFinger = finger.get(j);
if(ithFinger==null) continue;
else if(ithFinger.equals(suc)) updateIthFinger(j,null);
}
// try to update new successor base on current finger table
fillSuccessor();
suc = getSuccessor();
// fill the successor by the predecessor or its predecessor if the successor still null
if(suc==null && predecessor!=null && !predecessor.equals(localAddress)){
InetSocketAddress pre = predecessor;
InetSocketAddress prePre = null;
while(true){
prePre = Helper.requestAddress(pre,"YOURPRE");
if(prePre==null) break;
if(prePre.equals(pre) || prePre.equals(localAddress) || prePre.equals(suc)) break;
else pre = prePre;
}
updateIthFinger(1, pre);
}
}
// caller wants to delete a finger in table
else if (i == -2) {
for(int j=1;j<=32;j++){
InetSocketAddress ithFinger = finger.get(j);
if(ithFinger==null) continue;
else if(ithFinger.equals(value)) updateIthFinger(j,null);
}
}
// caller wants to fill successor
else if (i == -3) {
fillSuccessor();
}
}
/**
* Update ith finger in finger table using new value
* @param i: index
* @param value node value
*/
private void updateIthFinger(int i, InetSocketAddress value) {
finger.put(i, value);
updateResponse(i,value);
// if the updated one is successor, notify the new successor
if (i == 1 && value != null && !value.equals(localAddress)) {
notify(value);
}
}
/**
* Try to fill successor with candidates in finger table or even predecessor
*/
private void fillSuccessor() {
InetSocketAddress suc = this.getSuccessor();
if (suc == null || suc.equals(localAddress)) {
for (int i = 2; i <= 32; i++) {
InetSocketAddress ithFinger = finger.get(i);
if (ithFinger!=null && !ithFinger.equals(localAddress)) {
for (int j = i-1; j >=1; j--) {
updateIthFinger(j, ithFinger);
}
break;
}
}
}
suc = getSuccessor();
if ((suc == null || suc.equals(localAddress)) && predecessor!=null && !predecessor.equals(localAddress)) {
updateIthFinger(1, predecessor);
}
}
/**
* Set predecessor using a new value.
* @param pre
*/
public synchronized void setPredecessor(InetSocketAddress pre) {
predecessor = pre;
}
/**
* Get private variables
* @return the variable caller wants
*/
public InetSocketAddress getAddress() {
return localAddress;
}
public long getId() {
return localId;
}
public InetSocketAddress getSuccessor() {
if (finger != null && finger.size() > 0) {
return finger.get(1);
}
return null;
}
public InetSocketAddress getPredecessor() {
return predecessor;
}
/**
* Print the predecessor and the successor of the node
*/
public void printNeighbors () {
System.out.println("\nYou are listening on port "+localAddress.getPort()+"."
+ "\nYour position is "+Helper.hexIdAndPosition(localAddress)+".");
InetSocketAddress successor = finger.get(1);
// if it cannot find both predecessor and successor
if ((predecessor == null || predecessor.equals(localAddress)) && (successor == null || successor.equals(localAddress))) {
System.out.println("Your predecessor is yourself.");
System.out.println("Your successor is yourself.");
}
// else, it can find either predecessor or successor
else {
if (predecessor != null) {
System.out.println("Your predecessor is node "+predecessor.getAddress().toString()+", "
+ "port "+predecessor.getPort()+ ", position "+Helper.hexIdAndPosition(predecessor)+".");
}
else {
System.out.println("Your predecessor is updating.");
}
if (successor != null) {
System.out.println("Your successor is node "+successor.getAddress().toString()+", "
+ "port "+successor.getPort()+ ", position "+Helper.hexIdAndPosition(successor)+".");
}
else {
System.out.println("Your successor is updating.");
}
}
}
/**
* Print current node's Chord finger table and the response time
*/
public void printDataStructure () {
System.out.println("\n==============================================================");
System.out.println("\nLOCAL:\t\t\t\t"+localAddress.toString()+"\t"+Helper.hexIdAndPosition(localAddress));
if (predecessor != null)
System.out.println("\nPREDECESSOR:\t\t\t"+predecessor.toString()+"\t"+Helper.hexIdAndPosition(predecessor));
else
System.out.println("\nPREDECESSOR:\t\t\tNULL");
System.out.println("\nFINGER TABLE:\n");
for (int i = 1; i <= 32; i++) {
long ithstart = Helper.ithStart(Helper.hashSocketAddress(localAddress),i);
InetSocketAddress f = finger.get(i);
StringBuilder sb = new StringBuilder();
sb.append(i+"\t"+ Helper.longTo8DigitHex(ithstart)+"\t\t");
// node ip and hex code and percentage
if (f!= null)
sb.append(f.toString()+"\t"+Helper.hexIdAndPosition(f));
else
sb.append("NULL");
sb.append("\t"+responseTime.get(i).toString()+"ns");
System.out.println(sb.toString());
}
System.out.println("\n==============================================================\n");
}
/**
* Stop this node's all threads.
*/
public void stopAllThreads() {
if (listener != null)
listener.toDie();
if (heartBeat != null)
heartBeat.toDie();
}
}