-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
219 lines (177 loc) · 5.83 KB
/
test.py
File metadata and controls
219 lines (177 loc) · 5.83 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
#!/usr/bin/env python3
import sys
import os
import random
import time
import threading
import subprocess
import http.client
import configurations as configs
from hashing import consistent_hashing
import json
import re
from address import networkAddress
class Response(object): pass
class Request(object):
def _describe_exception(self, e):
return "%s: %s" % (type(e).__name__, e)
def _search_header_tuple(self, headers, header_name):
header_name = header_name.lower()
for key, value in headers:
if key.lower() == header_name:
return value
return None
def _determine_charset(self, content_type):
cmatch = re.match("text/plain; ?charset=(\\S*)", content_type)
if cmatch:
return cmatch.group(1)
else:
return "latin_1"
def send_request(self, host_port, method, url, body=None, accept_statuses=[200]):
def describe_request():
return "%s %s%s" % (method, host_port, url)
conn = None
try:
conn = http.client.HTTPConnection(host_port)
try:
conn.request(method, url, body)
r = conn.getresponse()
except Exception as e:
raise Exception(describe_request()
+ " --- "
+ self._describe_exception(e))
status = r.status
if status not in accept_statuses:
raise Exception(describe_request() + " --- unexpected status %d" % (r.status))
headers = r.getheaders()
body = r.read()
finally:
if conn:
conn.close()
content_type = self._search_header_tuple(headers, "Content-type")
if content_type == "application/json":
try:
body = json.loads(body)
except Exception as e:
raise Exception(describe_request()
+ " --- "
+ self._describe_exception(e)
+ " --- Body start: "
+ body[:30])
if content_type != None and content_type.startswith("text/plain") \
and sys.version_info[0] >= 3:
charset = self._determine_charset(content_type)
body = body.decode(charset)
r2 = Response()
r2.status = status
r2.headers = headers
r2.body = body
return r2
class TestNodes:
def __init__(self):
self._threads = {}
self.generated_keys = {}
self.default_port = configs.START_PORT_FROM
self.unstable_nodes = {}
self.host_address = networkAddress().get_host_address()
def generate_key(self, default):
port = configs.START_PORT_FROM if default == 0 else (random.randrange(configs.START_PORT_FROM + 1, configs.PORT_NUMBER_RANGE))
address = "{}:{}".format(self.host_address, port)
key = consistent_hashing(address)
if (key in self.generated_keys.keys()):
key, port = self.generate_key(default)
return key, port
def generate_ports(self, default):
key, port = self.generate_key(default)
self.generated_keys[key] = port
return port
def start_stand_alone(self):
i = 0
while i < configs.DHT_SIZE:
port = self.generate_ports(default = 0 if i == 0 else 1)
self._threads[i] = threading.Thread(target=self._run_stand_alone, args=[port])
print("Port number: {} - counter {}".format(port, i))
i = i + 1
for key in self._threads:
self._threads[key].start()
def start_joining(self):
for key in self.generated_keys:
port= self.generated_keys[key]
if port == configs.START_PORT_FROM:
continue
nodeA = "{}:{}".format(self.host_address, port)
nodeB = "{}:{}".format(self.host_address, configs.START_PORT_FROM)
r = Request()
r.send_request(nodeA, "POST", "/join?nprime="+nodeB)
def stop(self):
for key in self._threads:
self._threads[key].join()
def get_python_name(self):
py = "python3"
if os.name == 'nt':
py = "python"
return py
def _run_stand_alone(self, port):
subprocess.call([self.get_python_name(), 'storageNode.py', "-p {}".format(port)])
def run(self, port, node=None):
if node is None:
subprocess.call([self.get_python_name(), 'storageNode.py', "-p {}".format(port)])
else:
subprocess.call([self.get_python_name(), 'storageNode.py', "-p {}".format(port), node])
def check_join_statbility(self):
running = True
self.unstable_nodes = self.generated_keys.copy()
r = Request()
start_time = time.time()
while running:
for key in self.generated_keys:
port= self.generated_keys[key]
nodeA = "{}:{}".format(self.host_address, port)
result = r.send_request(nodeA, "GET", "/isStable")
if result.body == True:
if key in self.unstable_nodes: del self.unstable_nodes[key]
if len(self.unstable_nodes)<=0:
running = False
continue
end_time = time.time()
total = end_time - start_time
print()
print("Total time to become stable: ", total)
print()
def check_leave_statbility(self):
keys = random.sample(list(self.generated_keys), 1 if configs.DHT_SIZE==1 else int(configs.DHT_SIZE/2))
left_keys = { k : self.generated_keys[k] for k in set(self.generated_keys) - set(keys) }
running = True
r = Request()
print(left_keys)
start_time = time.time()
for key in keys:
port= self.generated_keys[key]
nodeA = "{}:{}".format(self.host_address, port)
result = r.send_request(nodeA, "POST", "/leave")
self.unstable_nodes = left_keys.copy()
time.sleep(2)
while running:
for key in left_keys:
port= self.generated_keys[key]
nodeA = "{}:{}".format(self.host_address, port)
result = r.send_request(nodeA, "GET", "/isStable")
if result.body == True:
if key in self.unstable_nodes: del self.unstable_nodes[key]
if len(self.unstable_nodes)<=0:
running = False
continue
end_time = time.time()
total = end_time - start_time
print()
print("Total time to become stable: ", total)
print()
if __name__ == "__main__":
nodes = TestNodes()
nodes.start_stand_alone()
time.sleep(30.0)
nodes.start_joining()
# nodes.check_join_statbility()
# nodes.check_leave_statbility()
nodes.stop()
# exit()