forked from weka/export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_api.py
393 lines (312 loc) · 14.4 KB
/
async_api.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
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
387
388
389
"""
async_api.py - subprocesses to execute multithreaded API calls.
"""
import pickle
import multiprocessing
from logging import debug, info, warning, error, critical, getLogger, DEBUG
import threading
import wekalib
import time
import queue
import math
import traceback
import json
import sys
# initialize logger - configured in main routine
log = getLogger(__name__)
# stupid macos doesn't fork by default
from multiprocessing import set_start_method
set_start_method("fork")
# this is a hidden class
class Job(object):
def __init__(self, hostname, method, parms):
self.hostname = hostname
self.method = method
self.parms = parms
self.result = dict()
self.exception = False
self.times_in_q = 1
def __str__(self):
return f"{self.hostname},{json.dumps(self.result,indent=2)}"
die_mf = Job(None, None, None)
# this is a hidden class
class SlaveThread(object):
def __init__(self, cluster, outputq):
self.cluster = cluster
self.outputq = outputq
self.inputq = queue.Queue();
self.thread = threading.Thread(target=self.slave_thread, daemon=True)
self.thread.start()
def __str__(self):
return self.thread.name
def slave_thread(self):
while True:
try:
job = self.inputq.get()
except EOFError:
del self.inputq
return # just silently die - this happens when the parent exits
log.debug(f"slave thread {self} received job hostname={job.hostname}")
if job.hostname is None:
# time to die
log.debug(f"slave thread {self} told to die")
del self.inputq
return
#else:
# log.debug(f"slave thread {self} received job")
for retries in range(1,3):
hostobj = self.cluster.get_hostobj_byname(job.hostname)
try:
job.result = hostobj.call_api(job.method, job.parms)
job.exception = False
break
except wekalib.exceptions.HTTPError as exc:
if exc.code == 502: # Bad Gateway - a transient error
log.error(f"slave thread {self} received Bad Gateway on host {job.hostname}")
# retry a few times
log.debug(f"{self} retrying after Bad Gateway")
job.times_in_q += 1
#self.submit(job)
time.sleep(0.5) # make it yield so maybe the server will recover
#self.inputq.task_done()
job.result = wekalib.exceptions.APIError(f"{exc.host}: ({exc.code}) {exc.message}") # send as APIError
job.exception = True
continue # go back to the inputq.get()
except wekalib.exceptions.TimeoutError as exc:
job.result = exc
job.exception = True
log.error(f"{exc}")
break
except Exception as exc:
job.result = exc
job.exception = True
log.info(f"Exception recieved on host {job.hostname}:{exc}")
log.info(traceback.format_exc())
break
# else, give up and return the error - note: multiprocessing.queue module hates HTTPErrors - can't unpickle correctly
#job.result = wekalib.exceptions.APIError(f"{exc.host}: ({exc.code}) {exc.message}") # send as APIError
#job.exception = True
if job.exception and type(job.result) is wekalib.exceptions.APIError:
log.debug(f"{self} noting Bad Gateway exception, returning exception")
# this will send back the above exeptions as well as good results
#log.info(f"job.result={json.dumps(job.result, indent=2)}")
log.debug(f"slave thread {self} queuing output.. Is exception = {job.exception}")
self.outputq.put(job)
#self.inputq.task_done()
# slave thread submit
def submit(self, job):
""" submit an job to this slave for processing """
self.inputq.put(job)
# Start a process that will have lots of threads
class SlaveProcess(object):
def __init__(self, cluster, num_threads, outputq):
self.cluster = cluster
self.outputq = outputq
self.queuesize = 0
#self.inputq = multiprocessing.JoinableQueue(500) # 50,000 max entries?
try:
self.inputq = multiprocessing.Queue(500) # 50,000 max entries?
except OSError as exc:
log.critical(f"Cannot create Queue, {exc}, exiting")
sys.exit(1)
self.slavethreads = list()
self.num_threads = num_threads
# actually start the process
self.proc = multiprocessing.Process(target=self.slave_process, args=(cluster,), daemon=True)
self.proc.start()
# process submit
def submit(self, job):
""" submit an job to this slave for processing """
self.inputq.put(job)
self.queuesize += 1
def __str__(self):
return self.proc.name
def flush(self):
self.inputq.close()
self.inputq.join_thread() # needed?
# this is the main loop of the process created above
def slave_process(self, cluster):
""" processes API call requests asychronously - runs in a sub-process (not thread) """
self.slavethreads = list()
self.bucket_array = list()
#log.info(f"starting threads {time.asctime()}")
log.info(f"starting {self.num_threads} threads")
for i in range(0, self.num_threads):
self.slavethreads.append(None) # reserve spots so we can start them on demand below
#self.slavethreads.append(SlaveThread(self.cluster, self.outputq))
#log.info(f"starting threads complete {time.asctime()}")
slavestats = dict()
hostname_tracker = dict()
while True:
#log.debug(f"waiting on queue")
job = self.inputq.get()
#log.debug(f"got job from queue, {job.hostname}")
log.debug(f"slave process {self} received job hostname={job.hostname}")
if job.hostname is None:
# we were told to die... shut it down
log.debug(f"slave process {self} told to die {self.slavethreads}")
for slave in self.slavethreads:
if slave is not None:
log.debug(f"sending die to {slave}")
slave.submit(die_mf) # slave THREAD
# do we need to wait for the queue to drain? Is that even a good idea?
log.debug(f"{self} done telling threads")
#while threading.active_count() > 0:
# log.debug(f"{threading.active_count()} threads are alive still")
# have to leave a lot of time in case it has a full inputq?
for slave in self.slavethreads:
if slave is not None:
log.debug(f"{self} joining thread {slave}")
slave.thread.join(timeout=30.0) # wait for it to die
if slave.thread.is_alive():
log.error(f"a thread didn't die!")
del self.inputq
self.outputq.close() # flush data to the ouputq
self.outputq.join_thread() # not sure if this is required or a bad idea
return # Goodbye, cruel world!
# all are daemon threads, so when this process dies, so do all the threads
#log.debug(f"slave process {self} received job")
# check here to make sure we can get the host object; if not, toss the job - we won't be able to call the api anyway
hostobj = cluster.get_hostobj_byname(job.hostname)
if hostobj is None:
log.debug(f"error on hostname {job.hostname}")
job.result = wekalib.exceptions.APIError(f"{job.hostname}: (NOHOST) Host object not found") # send as APIError
job.exception = True
self.outputq.put(job) # say it didn't work
#self.inputq.task_done() # complete the item on the inputq so parent doesn't hang
continue
# new stuff
try:
this_hash = self.bucket_array.index(job.hostname)
except ValueError:
self.bucket_array.append(job.hostname)
this_hash = self.bucket_array.index(job.hostname)
bucket = this_hash % len(self.slavethreads)
if bucket not in slavestats:
slavestats[bucket] = 1
else:
slavestats[bucket] += 1
if job.hostname not in hostname_tracker:
hostname_tracker[job.hostname] = bucket
elif hostname_tracker[job.hostname] != bucket:
log.info(f"bucket changed for {job.hostname} from {hostname_tracker[job.hostname]} to {bucket}")
# create a thread for the bucket, if needed
if self.slavethreads[bucket] is None:
log.debug(f"creating slave thread {bucket}")
self.slavethreads[bucket] = SlaveThread(self.cluster, self.outputq) # start them on demand
# submit the job to a thread
self.slavethreads[bucket].submit(job)
#self.inputq.task_done()
# process join
def join(self):
self.inputq.join() # wait for the queue to be completed
# exposed class - distribute calls to SlaveProcess processes via input queues
class Async():
def __init__(self, cluster, max_procs=8, max_threads_per_proc=100):
self.cluster = cluster
self.outputq = multiprocessing.Queue()
self.slaves = list()
self.num_outstanding = 0
self.stats = dict()
self.slaves = list()
self.bucket_array = list()
#self.num_slaves = max_procs
self.max_threads_per_proc = max_threads_per_proc
# # of processes and threads to run... (self-tuning)
self.num_slaves = math.ceil(self.cluster.sizeof() / self.max_threads_per_proc)
if self.num_slaves > max_procs:
self.num_slaves = max_procs # limit the number of slave processes we start
#log.info(f"starting processes {time.asctime()}")
# create the slave processes
for i in range(0, self.num_slaves):
self.slaves.append(SlaveProcess(self.cluster, self.max_threads_per_proc, self.outputq))
#log.info(f"starting processes complete {time.asctime()}")
# kill the slave processes
def __del__(self):
#for slave in self.slaves:
# slave.submit(die_mf)
# slave.proc.join(5.0) # wait for it to die (proc join)
del self.outputq
# submit a job
def submit(self, hostname, method, parms):
job = Job(hostname, method, parms) # wekahost? Object? decisions, decisions
log.debug(f"submitting job {job}")
try:
this_hash = self.bucket_array.index(hostname)
except ValueError:
self.bucket_array.append(hostname)
this_hash = self.bucket_array.index(hostname)
bucket = this_hash % len(self.slaves)
#log.debug(f"{hostname}/{this_hash}/{bucket}")
if bucket not in self.stats:
self.stats[bucket] = 1
else:
self.stats[bucket] += 1
#log.info(f"process bucket distribution: {self.stats}")
self.slaves[bucket].submit(job)
self.num_outstanding += 1
def log_stats(self):
log.info(f"process bucket distribution: {dict(sorted(self.stats.items()))}")
# wait for all the API calls to return
def wait(self):
"""
input q needs to be empty
output q needs to be empty
track in-flight api calls?
"""
#self.log_stats()
log.debug(f"{threading.active_count()} threads active")
#for slave in self.slaves:
# slave.proc.terminate() # testing
for slave in self.slaves:
log.debug(f"sending die to {slave}")
slave.submit(die_mf) # tell the subprocess that we're done queueing tasks
slave.flush()
# inputq is a multiprocessing.JoinableQueue
# outputq is a multiprocessing.Queue()
# wait for inputq's to drain
timed_out=False
timeout_period = 30.0
#timeout_period = 5.0 # testing
while self.num_outstanding > 0:
try:
log.debug(f"outputq size is {self.outputq.qsize()}, num_outstanding is {self.num_outstanding}")
except NotImplementedError:
pass # stupid macos returns error because qsize is broken in the OS
try:
result = self.outputq.get(True, timeout=timeout_period) # don't block because they should be dead
except queue.Empty as exc:
# timed out - if timeout is specified, it either returns an item or queue.Empty on timeout
log.error(f"outputq timeout!") # should never happen because they're dead
if not timed_out:
# we've timed out once already
timed_out = True
timeout_period = 0.01 # changing timeout period limits our max wait time
self.num_outstanding -= 1 # just get rid of them
continue
self.num_outstanding -= 1
if not result.exception:
if len(result.result) != 0:
yield result # yield so it is an iterator
else:
log.debug(f"API sent error: {result.result}")
for slave in self.slaves:
# wait for proc to finish processing inputq and die; timeout is 30s?
log.debug(f"joining {slave}")
slave.proc.join(0.1) # don't wait for him, just kill him below
log.debug(f"{slave} joined")
if slave.proc.exitcode is None:
# timed out - hung process!?
log.debug(f"killing {slave}")
if sys.version_info.minor < 7:
slave.proc.terminate() # can't be rude
else:
slave.proc.kill() # we would rather be rude about it
# queue should be empty now
return
if __name__ == "__main__":
import time
testme = Async()
#testme.start()
time.sleep(5)
#testme.stop()