-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_old.py
More file actions
executable file
·386 lines (326 loc) · 15.9 KB
/
node_old.py
File metadata and controls
executable file
·386 lines (326 loc) · 15.9 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
#!/usr/bin/env python3
import time
import socket
import argparse
import threading
import queue
import logging
logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s')
from common import encode
from common import decode
from common import load_config
from common import send_msg
class Receiver(threading.Thread):
'''Separate thread to open port to listen
1. Recieves the message
2. Decodes - messages are binary coded json strings, but we work with dictionaries
3. If it is a heartbeat message - respnonds
4. If it is not - puts dictionary into incoming queue to be fetched and processed by the node
'''
def __init__(self, host, port, queue):
super().__init__()
self.setName('Receiver')
# Queue for incoming messages
self._queue = queue
# Open a socket and bind to the port
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock.bind((host, port))
self._sock.listen()
def run(self):
while True:
# recieve messages and put them into queue
conn, addr = self._sock.accept()
conn.settimeout(1)
data = conn.recv(1024)
msg = decode(data)
logging.debug('msg: {}'.format(repr(msg)))
if not msg:
continue
if msg['type'] == 'ping':
# respond with pong to heartbeat messages
# no need to put it into queue
logging.debug('Reacting to heartbeat from {}'.format(msg['id']))
conn.sendall(encode('{"type": "pong"}'))
else:
incoming_queue.put(msg)
conn.close()
class Sender(threading.Thread):
'''Sender thread. Fetches messages from the outgoing queue and send them one by one.
Also initiates 'failure mode' if unable to send failure message.
'''
def __init__(self, incoming=None, outgoing=None):
super().__init__()
self.setName('Sender')
self._incoming = incoming
self._outgoing = outgoing
self._sock = None
def run(self):
while True:
if not self._outgoing.empty():
msg = self._outgoing.get()
logging.debug('msg: {}'.format(repr(msg)))
# TODO: Is it possible to reuse the socket???
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self._sock.connect((msg['host'], msg['port']))
self._sock.sendall(encode(msg))
except ConnectionRefusedError:
if msg['type'] == 'ping':
logging.debug('failure: {}'.format(msg['to_id']))
failure_msg = dict(type="fail", id=msg['to_id'])
self._incoming.put(failure_msg)
finally:
self._sock.close()
class Communicator(object):
'''Proxy class. Translates network-wide ids to actual ip-addresses and ports'''
def __init__(self, net_config=None, incoming=None, outgoing=None, id=None):
# network configuration as a dictionary node_id -> ('port', 'host')
self._config = net_config
self._incoming = incoming
self._outgoing = outgoing
self._id = id
def send(self, id, msg):
# translate id to addr:port and put in outgoing queue
msg['host'] = self._config[id]['host']
msg['port'] = self._config[id]['port']
# add to_id and from_id to the message
msg['to_id'] = id
msg['id'] = self._id
# schedule message for sending
self._outgoing.put(msg)
def ready(self):
# check if queue contains any messages
return not self._incoming.empty()
def receive(self):
# get message from incoming queue, one at the time
return self._incoming.get()
class Node(threading.Thread):
'''Implements main reconfiguration functionality'''
def __init__(self, id, links, edges, communicator):
super().__init__()
self.setName('Node')
self.id = id
# hardware link id -> networkwide logical node id
self._ports = {i: nb_id for i, nb_id in enumerate(links)}
self._edges = [self.port_to(edge) for edge in edges]
self._con = communicator
# state variables
self.coord_so_far = None
self.port_to_coord = None
self.status = 'idle'
self.recd_reply = {}
def _heartbeat(self):
threading.Timer(10.0, self._heartbeat).start()
for port_id in self._edges:
node_id = self._ports[port_id]
logging.debug('sending heartbeat to {}'.format(node_id))
self._con.send(node_id, dict(type="ping"))
def _on_reconfig(self, node_list, frag_id, from_id):
sender_id = node_list[-1]
if self.status == 'idle':
logging.debug('Reconfig, was in idle state!!')
# TODO: This is wrong. Should check not the number of ports, but 'available' ports. But this probably not the problem.
if len(self._ports) == 1:
self._con.send(sender_id, dict(type='no_contention'))
self.coord_so_far = frag_id
self.status = 'wait'
self.port_to_coord = self.port_to(sender_id)
for port in set(self._ports.keys()).difference([self.port_to(sender_id)]):
self._con.send(self._ports[port], dict(type='reconfig',
node_list=node_list+[self.id],
frag_id=frag_id))
elif self.status == 'wait':
logging.debug('Reconfig, was in wait state!!')
e = self.port_to(sender_id)
if (frag_id == self.coord_so_far) and (e not in self.get_port()):
logging.debug('This node is the initiator of the reconfig: no_contention!!')
self._con.send(self._ports[e], dict(type='no_contention'))
return
if self.id in node_list:
logging.debug('Already in the node_list: no_contention!!')
self._con.send(self._ports[e], dict(type='no_contention'))
return
# Resolve contention
if (self.coord_so_far > frag_id) or ((self.coord_so_far == frag_id) and (self.id > sender_id)):
logging.debug('This node is the boss: stop with our frag_id!!')
self._con.send(sender_id, dict(type='stop', frag_id=self.coord_so_far))
else:
logging.debug('This node is the slave: change coord_so_far!!')
logging.debug('self._ports: {}'.format(repr(self._ports)))
logging.debug('self.port_to_coord: {}'.format(self.port_to_coord))
self.coord_so_far = frag_id
if self.port_to_coord is not None:
self._con.send(self._ports[self.port_to_coord], dict(type='stop', frag_id=frag_id))
self.port_to_coord = self.port_to(sender_id)
def _on_stop(self, frag_id, from_id):
p = self.port_to(from_id)
if frag_id > self.coord_so_far:
self.coord_so_far = frag_id
if self.port_to_coord is not None:
self._con.send(self._ports[self.port_to_coord], dict(type='stop', frag_id=frag_id))
self.port_to_coord = p
if frag_id == self.coord_so_far:
if self.port_to_coord not in self.get_port():
if self.port_to_coord is not None:
self._con.send(self._ports[self.port_to_coord], dict(type='no_contention'))
self.recd_reply[self.port_to_coord] = 'no_contention'
else:
self._con.send(self._ports[self.port_to_coord], dict(type='stop', frag_id=frag_id))
self.port_to_coord = p
if frag_id < self.coord_so_far:
self._con.send(self._ports[p], dict(type='stop', frag_id=self.coord_so_far))
def _on_everybody_responded(self):
logging.debug('Everybody responded was actually called!!')
logging.debug('Everybody responded: self.port_to_coord {} {}'.format(self.port_to_coord, self.status))
if 'accepted' in self.recd_reply.values():
self._con.send(self._ports[self.port_to_coord], dict(type='accepted'))
if self.port_to_coord not in self.get_port():
self.assign_edge(self.port_to_coord)
else:
# TODO: There might be an issue here
a = self.port_to_coord
b = self.get_port()
c = (self.port_to_coord not in self.get_port())
d = self.set_of_ports().difference([self.port_to_coord])
e = self.set_of_ports().difference([self.port_to_coord]).intersection(self.get_port())
f = len(self.set_of_ports().difference([self.port_to_coord]).intersection(self.get_port()))
g = len(self.set_of_ports().difference([self.port_to_coord]).intersection(self.get_port()))!=0
logging.debug('a: {}'.format(a))
logging.debug('b: {}'.format(b))
logging.debug('c: {}'.format(c))
logging.debug('d: {}'.format(d))
logging.debug('e: {}'.format(e))
logging.debug('f: {}'.format(f))
logging.debug('g: {}'.format(g))
if (self.port_to_coord not in self.get_port()) and len(self.set_of_ports().difference([self.port_to_coord]).intersection(self.get_port()))!=0:
logging.debug('Send accept message!!!!!!!!!!!!!!!!!!!!!!!!!!!')
self._con.send(self._ports[self.port_to_coord], dict(type='accepted'))
self.assign_edge(self.port_to_coord)
else:
if self.port_to_coord is not None:
self._con.send(self._ports[self.port_to_coord], dict(type='no_contention'))
# Go back to the idle state
self.recd_reply = {}
self.status = 'idle'
self.coord_so_far = None
self.port_to_coord = None
def port_to(self, node_id):
for port_idx, cur_id in self._ports.items():
if cur_id == node_id:
return port_idx
return None
def set_of_ports(self):
return set(self._ports.keys())
def assign_edge(self, port_id):
logging.debug('Assign new edge to port: {}')
logging.debug('Edges before: {}'.format(repr(self._edges)))
self._edges.append(port_id)
logging.debug('Edges after: {}'.format(repr(self._edges)))
def remove_edge(self, node_id):
port_id = self.port_to(node_id)
logging.debug('Removing edge to node {} (port {})'.format(node_id, port_id))
logging.debug('Edges before: {}'.format(repr(self._edges)))
if port_id in self._edges:
logging.debug('Removing edge to node {} (port {})'.format(node_id, port_id))
self._edges = list(set(self._edges).difference([port_id]))
logging.debug('Edges after: {}'.format(repr(self._edges)))
def get_port(self):
return self._edges
def run(self):
while True:
if not incoming_queue.empty():
msg = incoming_queue.get()
logging.debug('Processing message: {}\nCurrent recvd: {}\nCurrent status: {}\nCoord so far: {}\nPort to coord: {}'.format(repr(msg),
self.recd_reply,
self.status,
self.coord_so_far,
self.port_to_coord))
logging.debug(''.format(self.recd_reply))
if msg['type'] == 'start':
print('Activating in a second')
time.sleep(2.0)
self._heartbeat()
elif msg['type'] == 'reconfig':
self.recd_reply[self.port_to(int(msg['id']))] = 'no_contention'
self._on_reconfig(msg['node_list'], int(msg['frag_id']), int(msg['id']))
elif msg['type'] == 'no_contention':
if self.status == 'wait':
self.recd_reply[self.port_to(int(msg['id']))] = 'no_contention'
if len(self.recd_reply) == len(self._ports):
self._on_everybody_responded()
elif msg['type'] == 'accept':
if self.status == 'wait':
self.recd_reply[self.port_to(int(msg['id']))] = 'accepted'
if len(self.recd_reply) == len(self._ports):
self._on_everybody_responded()
elif msg['type'] == 'stop':
if self.status == 'wait':
self._on_stop(int(msg['frag_id']), int(msg['id']))
elif msg['type'] == 'fail':
# set status of the node
# TODO: This might be an issue
self.status = 'wait'
self.coord_so_far = self.id
self.port_to_coord = None # What to put here???
# remove the failed edge from our MST
self.remove_edge(int(msg['id']))
# send reconfiguration request through all the ports
for dest_id in set(self._ports.values()).difference([int(msg['id'])]):
self._con.send(id=dest_id,
msg=dict(type='reconfig',
node_list=[self.id],
frag_id=self.id))
elif msg['type'] == 'extract':
host = msg['host']
port = msg['port']
graph_msg = encode(dict(id=self.id,
links=list(self._ports.values()),
edges=[self._ports[port_id] for port_id in self._edges]))
send_msg(msg=graph_msg, host=host, port=port)
def opt_parser():
parser = argparse.ArgumentParser(description='Network reconfiguration node')
parser.add_argument('--id',
default=13,
type=int)
parser.add_argument('--wait',
default=11,
type=int,
help='Time to wait before we start to send heartbit')
parser.add_argument('--net_config', default='config/sample_graph2.json', type=str)
return parser
if __name__ == '__main__':
# Parse command line arguments
parser = opt_parser()
opt = parser.parse_args()
# Load network configuration
net_config = load_config(opt.net_config)
# Get configuration of this node
print(net_config[opt.id])
my_id = opt.id
host = net_config[my_id]['host']
port = net_config[my_id]['port']
links = net_config[my_id]['links']
edges = net_config[my_id]['edges']
# create queues for incoming and outgoig messages
incoming_queue = queue.Queue()
outgoing_queue = queue.Queue()
communicator = Communicator(net_config=net_config,
incoming=incoming_queue,
outgoing=outgoing_queue,
id=my_id)
# thread to receive incoming messages
receiver = Receiver(host=host,
port=port,
queue=incoming_queue)
sender = Sender(incoming=incoming_queue,
outgoing=outgoing_queue)
node = Node(id=my_id,
links=links,
edges=edges,
communicator=communicator)
receiver.start()
sender.start()
node.start()
receiver.join()
sender.join()
node.join()