-
Notifications
You must be signed in to change notification settings - Fork 0
/
chord.py
189 lines (151 loc) · 5.64 KB
/
chord.py
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
from genericpath import exists
import property
import random
import math
__author__ = 'gaurav'
# #########################################
# Main Simulator Module #
# #########################################
from node import Node
from idGenerator import IdGenerator
from operation import Operation
from stats import Stats
from datacollector import DataCollector
from graphplotter import GraphPlotter
nodeIdGenerator = IdGenerator()
nodeIdGenerator.lastAllottedId = property.INITID
nodeIdGenerator.randomRange = property.RANGEID
data_collector = DataCollector()
maxId = 0
########## NODES IN RING OVERLAY #######################
nodes = []
########################################################
############## OPERATIONS AND EXPERIMENTATION ##########
def add_node(newNode, existingnode):
newNode.join(existingnode)
newNode.update_others(Operation.INSERT)
def print_ring(start):
trav = start
trav.node_info()
while trav.successor is not None and trav.successor == start:
trav.node_info()
trav = trav.successor
#################EXPERIMENTATIOn START###################
n = property.N
for j in range(0, property.EXP):
print "\n####################################################EXPERIMENT No."+str(j+1)+" ###############################################################"
nodeIdGenerator.flush()
nodes = []
# Max number of bits = M
M = int(math.log(n, 2)) + 1
#Add first node
maxId = nodeIdGenerator.generate()
head = Node(maxId, M)
head.successor = head
head.predecessor = head
head.init_finger_table_start(M, maxId, head)
for i in range(1, M + 1):
head.fingerTable[i].node = head
nodes.append(head)
print head.node_info()
if property.SIM:
print "EXPERIMENT 1: Adding new nodes in the ring overlay..."
for i in range(1, n):
next_id = nodeIdGenerator.generate()
if next_id > maxId:
maxId = next_id
node = Node(next_id, M)
existing_node = nodes[random.randint(0, len(nodes)-1)]
node.init_finger_table_start(M, maxId, existing_node)
#add_node(node, head)
add_node(node, existing_node)
nodes.append(node)
if property.SIM:
print "Intermediate Stabilization..."
for i in range(1, n):
#choose a random starting node from the list of existing nodes
#existing_node = nodes[random.randint(0, len(nodes)-1)]
existing_node = nodes[i]
existing_node.stabilize()
if property.SIM:
print "EXPERIMENT 2: Looking up keys..."
for i in range(1, n):
#choose a random starting node from the list of existing nodes
existing_node = nodes[random.randint(0, len(nodes)-1)]
key = random.randint(1, n)
key = 2**M - key
successor = existing_node.lookup(key)
if property.SIM:
print "Intermediate Stabilization..."
for i in range(1, n):
#choose a random starting node from the list of existing nodes
#existing_node = nodes[random.randint(0, len(nodes)-1)]
existing_node = nodes[i]
existing_node.stabilize()
if property.SIM:
print "EXPERIMENT 3: Deleting existing nodes..."
for i in range(1, n):
#choose a random starting node from the list of existing nodes
nodes[i].depart()
if property.SIM:
print "Printing Experiment Results..."
print "\n\n**********************OBSERVATIONS***********************"
print "INPUT PARAMETERS:"
print "NUMBER OF NODES, N="+str(n)
print "NUMBER OF BITS USED IN NODEID/KEY HASHES, M="+str(M)
print "\nSTATS:"
Stats().print_stats()
#print_ring(head)
data_collector.collect(n)
Stats().reset()
n += property.N
################################################## FAULT INTRODUCTION AND SIMULATION ##################################################
#1. Simulate a lookup just after deletion of few nodes
nodeIdGenerator.flush()
nodes = []
# Max number of bits = M
M = int(math.log(n, 2)) + 1
#Add first node
maxId = nodeIdGenerator.generate()
head = Node(maxId, M)
head.successor = head
head.predecessor = head
head.init_finger_table_start(M, maxId, head)
for i in range(1, M + 1):
head.fingerTable[i].node = head
nodes.append(head)
for i in range(1, n):
next_id = nodeIdGenerator.generate()
if next_id > maxId:
maxId = next_id
node = Node(next_id, M)
existing_node = nodes[random.randint(0, len(nodes)-1)]
node.init_finger_table_start(M, maxId, existing_node)
#add_node(node, head)
add_node(node, existing_node)
nodes.append(node)
for i in range(1, n):
#choose a random starting node from the list of existing nodes
#existing_node = nodes[random.randint(0, len(nodes)-1)]
existing_node = nodes[i]
existing_node.stabilize()
if property.SIM:
print 'Deleting a few nodes now...'
for i in range(1, 3):
existing_node = nodes[random.randint(0, len(nodes)-1)]
existing_node.depart()
if property.SIM:
print 'Looking up keys immediately after some node failures...'
for i in range(1, n):
#choose a random starting node from the list of existing nodes
existing_node = nodes[random.randint(0, len(nodes)-1)]
key = random.randint(1, n)
key = 2**M - key
successor = existing_node.lookup(key)
if property.SIM:
print 'Look up successful after node failures'
##################################################### PLOT GRAPHS ########################################################
GraphPlotter().plotForInserts()
GraphPlotter().plotForDeletes()
GraphPlotter().plotForLookups()
GraphPlotter().plotForFingerTableEfficacy()