-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_single_q.py
253 lines (206 loc) · 6.61 KB
/
master_single_q.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
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
import threading, time, sys, getopt, json, queue, nslookup
from collections import deque
from config import *
from priority_dict import *
from debug_utils import *
from commer import CommerOnMaster, LISTEN_IP, send_msg
from msg import Msg, InfoType, UpdateType, Update
from plot import plot_master
def get_wip_l(domain):
query = nslookup.Nslookup()
record = query.dns_lookup(domain)
log(DEBUG, "", dns_response=record.response_full, dns_answer=record.answer)
return record.answer
class RRQueue(): # Round Robin
def __init__(self, max_qlen):
self.max_qlen = max_qlen
self.cid_q_m = {}
self.next_cid_to_pop_q = deque()
self.num_dropped = 0
def reg(self, cid):
if cid not in self.cid_q_m:
self.cid_q_m[cid] = deque()
self.next_cid_to_pop_q.append(cid)
log(DEBUG, "reged", cid=cid)
def unreg(self, cid):
if cid in self.cid_q_m:
self.cid_q_m.pop(cid)
self.next_cid_to_pop_q.remove(cid)
log(DEBUG, "unreged", cid=cid)
def push(self, msg):
if msg.payload.cid not in self.cid_q_m:
self.reg(msg.payload.cid)
r = True
q = self.cid_q_m[msg.payload.cid]
if len(q) == self.max_qlen:
msg_popped = q.popleft()
log(DEBUG, "Was full, popped the oldest req", msg_popped=msg_popped)
self.num_dropped += 1
r = False
q.append(msg)
log(DEBUG, "pushed", msg=msg)
return r
def pop(self):
for _ in range(len(self.cid_q_m)):
q = self.cid_q_m[self.next_cid_to_pop_q[0]]
self.next_cid_to_pop_q.rotate(-1)
if len(q) > 0:
return q.popleft()
return None
class WQueue(): # Worker
def __init__(self, wip_l, w_token_q, max_qlen):
self.w_token_q = w_token_q
self.max_qlen = max_qlen
self.wip_qlen_heap_m = priority_dict()
for wip in wip_l:
self.wip_qlen_heap_m[wip] = 0
for _ in range(self.max_qlen):
self.w_token_q.put(1)
self.lock = threading.Lock()
log(DEBUG, "WQueue constructed", w_token_q_len=self.w_token_q.qsize(), wip_l=wip_l)
def inc_qlen(self, wip):
log(DEBUG, "started", wip=wip)
with self.lock:
self.wip_qlen_heap_m[wip] += 1
check(self.wip_qlen_heap_m[wip] <= self.max_qlen, "Q-len cannot be greater than max_qlen= {}".format(self.max_qlen))
log(DEBUG, "done", wip=wip, qlen=self.wip_qlen_heap_m[wip])
def dec_qlen(self, wip):
log(DEBUG, "started", wip=wip)
with self.lock:
self.wip_qlen_heap_m[wip] -= 1
check(self.wip_qlen_heap_m[wip] >= 0, "Q-len cannot be negative")
self.w_token_q.put(1)
log(DEBUG, "done", wip=wip, qlen=self.wip_qlen_heap_m[wip])
def pop(self):
with self.lock:
wip = self.wip_qlen_heap_m.smallest()
qlen = self.wip_qlen_heap_m[wip]
if qlen >= self.max_qlen:
log(WARNING, "Attempted to return a full worker", qlen=qlen)
return None
return wip
class Master():
def __init__(self, _id, wip_l, dashboard_server_ip):
self._id = _id
self.wip_l = wip_l
self.dashboard_server_ip = dashboard_server_ip
self.commer = CommerOnMaster(self._id, self.handle_msg)
self.msg_token_q = queue.Queue()
self.msg_q = RRQueue(max_qlen=5)
self.w_token_q = queue.Queue()
self.w_q = WQueue(self.wip_l, self.w_token_q, max_qlen=30)
self.num_updates_sent = 0
t_update = threading.Thread(target=self.run_dashboard_updates, daemon=True)
t_update.start()
self.on = True
t = threading.Thread(target=self.run, daemon=True)
t.start()
t.join()
def __repr__(self):
return "Master(id= {}, wip_l= {})".format(self._id, self.wip_l)
def close(self):
self.commer.close()
self.on = False
log(DEBUG, "done")
def send_update_to_dashboard(self):
if self.dashboard_server_ip is None:
return
# log(DEBUG, "started")
self.num_updates_sent += 1
m = {'mid': self._id,
'epoch': time.time(),
'w_qlen_l': [qlen for wip, qlen in self.w_q.wip_qlen_heap_m.items()]}
msg = Msg(_id = self.num_updates_sent,
payload = Update(_id=self.num_updates_sent, typ=UpdateType.from_master, m=m),
src_id = self._id,
src_ip = LISTEN_IP,
dst_id = 'd',
dst_ip = self.dashboard_server_ip)
send_msg(msg)
log(DEBUG, "done", m=m)
def run_dashboard_updates(self):
while True:
time.sleep(1)
self.send_update_to_dashboard()
def handle_msg(self, msg):
log(DEBUG, "handling", msg=msg)
p = msg.payload
if p.is_req():
p.epoch_arrived_cluster = time.time()
if self.msg_q.push(msg):
self.msg_token_q.put(1)
elif p.is_info():
if p.typ == InfoType.client_disconn:
# self.msg_q.unreg(msg.src_id)
pass
elif p.typ == InfoType.worker_req_completion:
self.w_q.dec_qlen(msg.src_ip)
elif p.typ == InfoType.close:
for wip in self.wip_l:
self.commer.send_to_worker(wip, msg)
self.close()
else:
log(ERROR, "Unexpected payload type", payload=p)
def run(self):
while self.on:
log(DEBUG, "Waiting for msg")
self.msg_token_q.get(block=True)
msg = self.msg_q.pop()
check(msg is not None, "Msg must have arrived")
log(DEBUG, "Waiting for worker")
self.w_token_q.get(block=True)
log(DEBUG, "", w_token_q_len=self.w_token_q.qsize())
wip = self.w_q.pop()
check(wip is not None, "There should have been an available worker")
log(DEBUG, "Will send_to_worker", wip=wip)
self.commer.send_to_worker(wip, msg)
if msg.payload.is_req():
log(DEBUG, "Will inc_qlen", wip=wip)
self.w_q.inc_qlen(wip)
def parse_argv(argv):
m = {}
try:
opts, args = getopt.getopt(argv, '', ['log_to_std=', 'i=', 'wip_l=', 'worker_service=', 'dashboard_server_ip='])
except getopt.GetoptError:
assert_("Wrong args;", opts=opts, args=args)
for opt, arg in opts:
if opt == '--log_to_std':
m['log_to_std'] = bool(int(arg))
elif opt == '--i':
m['i'] = arg
elif opt == '--wip_l':
m['wip_l'] = json.loads(arg)
elif opt == '--worker_service':
m['worker_service'] = arg
elif opt == '--dashboard_server_ip':
m['dashboard_server_ip'] = arg
else:
assert_("Unexpected opt= {}, arg= {}".format(opt, arg))
if 'log_to_std' not in m:
m['log_to_std'] = True
if 'i' not in m:
m['i'] = LISTEN_IP
if 'wip_l' not in m:
check('worker_service' in m, 'wip_l not set, worker_service should have been set')
wip_l = get_wip_l(m['worker_service'])
while len(wip_l) == 0:
log(DEBUG, "get_wip_l returned empty...retrying", worker_service=m['worker_service'])
wip_l = get_wip_l(m['worker_service'])
m['wip_l'] = wip_l
if 'dashboard_server_ip' not in m:
m['dashboard_server_ip'] = 'dashboard-service'
log(DEBUG, "", m=m)
return m
def run(argv):
m = parse_argv(argv)
_id = 'm_' + m['i']
log_to_file('{}.log'.format(_id))
if m['log_to_std']:
log_to_std()
log(DEBUG, "", m=m)
mr = Master(_id, m['wip_l'],
dashboard_server_ip=m['dashboard_server_ip'])
# input("Enter to finish...\n")
# sys.exit()
if __name__ == '__main__':
run(sys.argv[1:])