/
chord.py
199 lines (170 loc) · 5.46 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
189
190
191
192
193
194
195
196
197
198
199
"""
Run Experiments for the Chord DHT Protocol
Uses the ChordNode and Network classes defined in this repo.
"""
import math
import sys
import random
import hashlib
import matplotlib.pyplot as plt
from chord_node import ChordNode
from modules.network import Network
if len(sys.argv) == 2:
print('Please enter required number of arguments')
sys.exit(0)
# Global Variables
num_nodes = int(sys.argv[1])
read_from_file = bool(int(sys.argv[2]))
nodes = []
data_store = {}
l = 6
m = 24
num_points = 10000
num_queries = 1000000
def plot_histogram(dict):
plt.bar(dict.keys(), dict.values())
plt.xlim(0, 14)
plt.ylim(0, 0.6)
plt.xlabel('Number of Hops')
plt.ylabel('Probability')
plt.show()
def hash_int(integer):
"""Hash the given integers and trim to l digits
Arguments:
integer {Integer}
Returns:
String -- string of l digits, hash of integer
"""
name = str(integer)
m = hashlib.sha1(name.encode('utf-8'))
node_hash = m.hexdigest()[:l]
return node_hash
def init_network(network, num_nodes):
"""Initialize network by adding nodes
Arguments:
network {Network}
num_nodes {Integer} -- Number of nodes
"""
num_added = 0
for i in range(2 * num_nodes):
print("Adding node " + str(i))
node_hash = hash_int(i)
print(node_hash)
pn = ChordNode(i, node_hash, network, m)
is_added = network.add_node(pn)
if is_added:
pn.join()
num_added += 1
nodes.append(i)
if num_added == num_nodes:
break
def search_queries(network, num_queries):
"""Run search queries for num_queries times
Arguments:
network {Network}
num_queries {Integer} -- Number of queries
"""
hops_hist = {}
num_epoch = 0
flag = 0
count = 0
for _ in range(100):
for q in data_store:
flag = 0
count += 1
if (count % 10000 == 0):
num_epoch += 1
print(str(num_epoch) + ' epochs completed')
hit_node = int(hash_int(random.choice(nodes)), 16)
node = network.get_node(hit_node)
hops, chord_value, path = node.search(q)
print('Lookup ' + str(q) + ': ' + str(path))
# Add in histogram
hops = 12 if hops > 12 else hops
if hops in hops_hist:
hops_hist[hops] += 1
else:
hops_hist[hops] = 1
if chord_value == -1:
try:
global_value = data_store[q]
flag = 1
print(
str(q) + ': Found ' + str(global_value) +
' when not stored')
except:
continue
else:
try:
global_value = data_store[q]
if (chord_value != global_value):
print(
str(q) + ': Found ' + str(global_value) +
' when ' + str(chord_value) + ' stored')
flag = 1
except:
flag = 1
if flag == 1:
print('Couldn\'t find node ' + str(q) + ' correctly')
if count >= num_queries:
break
if count >= num_queries:
break
if flag == 0:
print('All queries ran successfully')
new_dict = {}
avg_hops = 0
for k in hops_hist:
new_dict[k] = hops_hist[k] / num_queries
avg_hops += (new_dict[k] * k)
print(avg_hops)
plot_histogram(new_dict)
def store_keys(network, num_keys):
"""Store keys in the Chord Network
Arguments:
network {Network}
num_keys {Integer}
"""
global num_points
count = 0
for key in range(2 * num_keys):
value = random.randint(0, 2 * num_keys)
# Choose a random node and store in it
rand_node = int(hash_int(random.choice(nodes)), 16)
node = network.get_node(rand_node)
is_stored = node.store_key(key, value)
if is_stored == 0:
# Also store in the global array
count += 1
data_store[key] = value
if count == num_points:
break
def delete_nodes(network, del_nodes):
"""Simulate deletion of nodes from network
Arguments:
network {Network}
del_nodes {Integer} -- Number of nodes to be deleted
"""
num_deleted = 0
while num_deleted < del_nodes:
chosen_node = random.choice(nodes)
del_node = network.get_node(int(hash_int(chosen_node), 16))
removed = del_node.depart_network()
if removed:
num_deleted += 1
nodes.remove(chosen_node)
# Number of switches :- Max number of nodes that can be added onto the network
num_switches = num_nodes
network = Network(num_switches, read_from_file)
# Initialize network
init_network(network, num_nodes)
store_keys(network, num_points)
search_queries(network, num_queries)
delete_nodes(network, num_nodes // 2)
search_queries(network, num_queries)
print('Total number of nodes: ' + str(num_nodes))
print('Total number of data points: ' + str(num_points))
print('Total number of search queries: ' + str(num_queries))
print('Total number of node add queries: ' + str(num_nodes))
print('Total number of node delete queries: ' + str(num_nodes // 2))
print('Total number of data add queries: ' + str(num_queries))