-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpastry.py
157 lines (131 loc) · 4.2 KB
/
pastry.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
"""
Run Experiments for the Pastry DHT Protocol
Uses the PastryNode and Network classes defined in this repo.
"""
import math
import sys
import random
import hashlib
import matplotlib.pyplot as plt
from pastry_node import PastryNode
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 = []
nodes_hash = []
l = 6
b = 4
num_points = 10000
num_queries = 1000000
def plot_histogram(dict):
plt.bar(dict.keys(), dict.values())
plt.xlim(0, 12)
plt.ylim(0, 0.8)
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 = PastryNode(i, node_hash, network, l, b)
is_added = network.add_node(pn)
if is_added:
pn.join()
num_added += 1
nodes.append(i)
nodes_hash.append(int(node_hash, 16))
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 range(num_queries // 100):
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, found = node.search(q)
# Add in histogram
hops = 10 if hops > 10 else hops
if hops in hops_hist:
hops_hist[hops] += 1
else:
hops_hist[hops] = 1
q_hash = int(hash_int(q), 16)
in_list = q_hash in nodes_hash
if (in_list and found != -1) or (not in_list and found == -1):
continue
flag = 1
print(in_list, found)
print('Couldn\'t find node ' + str(q) + ' correctly')
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 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 = int(hash_int(chosen_node), 16)
removed = network.remove_node(del_node)
if removed:
num_deleted += 1
nodes.remove(chosen_node)
nodes_hash.remove(int(hash_int(chosen_node), 16))
# 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)
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))