-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmpEngineProdCons.py
executable file
·447 lines (352 loc) · 16.4 KB
/
mpEngineProdCons.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import settings
import logging
import multiprocessing
import Queue
import time
from datetime import timedelta, datetime
from appAux import psutil_phymem_usage
import gc
logger = logging.getLogger(__name__)
dying_workers = []
# Auto-balancing Producer-Consumer class
def rate_limited(period, damping = 1.0):
'''
Prevent a method from being called
if it was previously called before
a time widows has elapsed.
:param period: The time window after which method invocations can continue.
:param damping: A factor by which to dampen the time window.
:return function: Decorated function that will forward method invocations if the time window has elapsed.
'''
frequency = damping / float(period)
def decorate(func):
last_called = [0.0]
def func_wrapper(*args, **kargs):
elapsed = time.time() - last_called[0]
left_to_wait = frequency - elapsed
if left_to_wait > 0:
# time.sleep(left_to_wait)
# print left_to_wait
return None
ret = func(*args, **kargs)
last_called[0] = time.time()
return ret
return func_wrapper
return decorate
class MPEngineProdCons(object):
def __init__(self, maxCores, producer_Class, consumer_Class, governorOffFlag = False):
logger.debug("mpEngine initializing")
self.governorOffFlag = governorOffFlag
self.maxCores = maxCores
self.__deleting__ = False
self.__internalLock__ = multiprocessing.Lock()
self.killed_event = multiprocessing.Event()
# Producers
self.num_producers = 0
self.next_worker_num = 0
self.producer_Class = producer_Class
self.producer_pool = []
self.producer_pool_exitEvent = []
self.producer_task_queue = multiprocessing.JoinableQueue()
self.producer_results_queue = multiprocessing.JoinableQueue()
self.producer_pool_progress = multiprocessing.Value('i', 0)
# Consumers
self.num_consumers = 0
self.next_consumer_num = 0
self.consumer_Class = consumer_Class
self.consumer_pool = []
# Note: consumer_pool_exitEvent is used both to notify a worker it should end and for the worker to notify it has dones so
self.consumer_pool_exitEvent = []
self.consumer_task_queue = self.producer_results_queue
self.consumer_results_queue = multiprocessing.JoinableQueue()
self.consumer_pool_progress = multiprocessing.Value('i', 0)
# Tasks
self.num_tasks = multiprocessing.Value('i', 0)
self.tasks_added = False
# Rebalance checks
self._rebalance_last_kick = datetime.now()
self.rebalance_backoff_timer = 60 * 1
self._rebalance_mem_last_kick = datetime.now()
self.rebalance_mem_backoff_timer = 60 * 2
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is not None:
print exc_type, exc_value, traceback
self.__del__
return self
def __del__(self):
# Lock internal
self.__internalLock__.acquire()
if not self.__deleting__:
logger.debug("Bringing down mpEngine")
self.__deleting__ = True
while self.num_producers > 0: self.removeProducer(True)
while self.num_consumers > 0: self.removeConsumer(True)
logger.debug("mpEngine down")
# Release internal
self.__internalLock__.release()
def check_mpEngineStatus(self):
status_ok = True
assert(len(self.producer_pool) == self.num_producers)
assert(len(self.producer_pool) == len(self.producer_pool_exitEvent))
assert(len(self.consumer_pool) == self.num_consumers)
assert(len(self.consumer_pool) == len(self.consumer_pool_exitEvent))
# Check all the processes we believe we have are really alive
for (worker_num, worker, extra_arg_list) in self.producer_pool:
if not worker.is_alive():
logger.error("check_mpEngineStatus error, dead producer process: %s / %s" % (worker_num, worker.name))
status_ok = False
for (worker_num, worker, extra_arg_list) in self.consumer_pool:
if not worker.is_alive():
logger.error("check_mpEngineStatus error, dead consumer process: %s / %s" % (worker_num, worker.name))
status_ok = False
if self.killed_event.is_set(): status_ok = False
return status_ok
def working(self):
# Internal check
if self.killed_event.is_set() or not self.check_mpEngineStatus():
print("\n\n\n==>Tearing down mpEngine, we've been killed! out of memory? (give me 60 secs to try to shutdown cleanly)")
logger.error("Tearing down mpEngine, we've been killed!")
self.endConsumers()
time.sleep(2)
self.endProducers()
time.sleep(2)
return False
# Check if we still have work to do
if (self.get_num_tasks() != self.getProgressConsumers()): return True
else:
logger.debug("mpEngine work finished!")
# Wait until all our workers have exited
while self.num_producers > 0: self.removeProducer(True)
while self.num_consumers > 0: self.removeConsumer(True)
return False
def addTaskList(self, task_list):
if not self.tasks_added:
with self.num_tasks.get_lock():
for new_task in task_list:
self.num_tasks.value += 1
self.producer_task_queue.put(new_task)
return True
else:
logger.error("Can only add tasks once!")
return False
def getProgress(self):
return (self.num_producers, self.num_consumers, self.get_num_tasks(), self.getProgressProducers(), self.getProgressConsumers())
def get_num_tasks(self):
with self.num_tasks.get_lock():
return self.num_tasks.value
def getProgressProducers(self):
return self.producer_pool_progress.value
def getProgressConsumers(self):
return self.consumer_pool_progress.value
def getProducerCount(self):
return self.num_producers
def addProducer(self, extra_arg_list = []):
if self.num_producers < self.maxCores:
# Lock internal
self.__internalLock__.acquire()
new_worker_num = self.next_worker_num
logger.debug("Adding Producer-%d" % (new_worker_num))
self.producer_pool_exitEvent.append(multiprocessing.Event())
self.producer_pool.append((new_worker_num, self.producer_Class(
self.producer_task_queue, self.producer_results_queue, self.get_num_tasks(), self.get_num_tasks(), self.producer_pool_progress,
self.producer_pool_exitEvent[-1], self.killed_event, extra_arg_list), extra_arg_list))
self.producer_pool[-1][1].daemon = False # Remove for debugging
self.producer_pool[-1][1].start()
# Update worker count
self.num_producers += 1
# Update next worker num
self.next_worker_num += 1
# Release internal
self.__internalLock__.release()
logger.debug("Producer-%d added" % new_worker_num)
else:
logger.error("Attempted to start workers beyond the maxCores setting")
def removeProducer(self, noLock = False):
if self.num_producers > 0:
# Lock internal
if not noLock: self.__internalLock__.acquire()
# Remove last worker from worker pool
(worker_num, producer, extra_arg_list) = self.producer_pool.pop()
logger.debug("Removing Producer-%d" % worker_num)
# Remove last worker's exitFlag
producer_exitEvent = self.producer_pool_exitEvent.pop()
# Set the worker's exit event
if not producer_exitEvent.is_set():
logger.debug("Producer-%d exitEvent SET" % worker_num)
producer_exitEvent.set()
# Update producer count
self.num_producers -= 1
# Release internal
if not noLock: self.__internalLock__.release()
else:
logger.error("Attempted to remove producer from empty pool.")
def startProducers(self, num_producers, extra_arg_list = []):
logger.debug("Starting producers")
if num_producers is None:
for i in xrange(self.maxCores - 1): self.addProducer(extra_arg_list)
else:
for i in xrange(num_producers): self.addProducer(extra_arg_list)
def restartProducers(self):
logger.debug("Restarting producers")
extra_arg_list_list = []
current_num_producers = self.num_producers
# Shut them all down
for i in xrange(current_num_producers):
# Grab extra_arg_list
(worker_num, producer, extra_arg_list) = self.producer_pool[-1]
extra_arg_list_list.append(extra_arg_list)
self.removeProducer(True)
# Start them all up again
for i in xrange(current_num_producers):
self.addProducer(extra_arg_list_list.pop())
logger.debug("Restarting producers - done")
def endProducers(self):
logger.debug("Ending all producers")
for i in xrange(self.num_producers): self.removeProducer()
def getConsumerCount(self):
return self.num_consumers
def addConsumer(self, extra_arg_list = []):
if self.num_consumers < self.maxCores:
# Lock internal
self.__internalLock__.acquire()
new_worker_num = self.next_worker_num
logger.debug("Adding Consumer-%d" % (new_worker_num))
self.consumer_pool_exitEvent.append(multiprocessing.Event())
self.consumer_pool.append((new_worker_num, self.consumer_Class(
self.consumer_task_queue, self.consumer_results_queue, self.get_num_tasks(), self.producer_pool_progress, self.consumer_pool_progress,
self.consumer_pool_exitEvent[-1], self.killed_event, extra_arg_list), extra_arg_list))
self.consumer_pool[-1][1].daemon = False # Remove for debugging
self.consumer_pool[-1][1].start()
# Update consumer count
self.num_consumers += 1
# Update next worker num
self.next_worker_num += 1
# Release internal
self.__internalLock__.release()
logger.debug("Consumer-%d added" % new_worker_num)
else:
logger.error("Attempted to start workers beyond the maxCores setting")
def removeConsumer(self, noLock = True):
if self.num_consumers > 0:
# Lock internal
if not noLock: self.__internalLock__.acquire()
# Remove last worker from worker pool
(worker_num, consumer, extra_arg_list) = self.consumer_pool.pop()
logger.debug("Removing Consumer-%d" % worker_num)
# Remove last worker's exitFlag
consumer_exitEvent = self.consumer_pool_exitEvent.pop()
# Set the worker's exit event
if not consumer_exitEvent.is_set():
logger.debug("Consumer-%d exitEvent SET" % worker_num)
consumer_exitEvent.set()
# Wait for the worker to acknowledge he has shutdown:
while consumer_exitEvent.is_set():
logger.debug("Waiting for Consumer-%d to shutdown" % worker_num)
time.sleep(1)
# Update consumer count
self.num_consumers -= 1
# Release internal
if not noLock: self.__internalLock__.release()
else:
logger.error("Attempted to remove consumer from empty pool.")
def startConsumers(self, num_consumers, extra_arg_list = []):
logger.debug("Starting consumers")
if num_consumers is None:
for i in xrange(self.maxCores - 1): self.addConsumer(extra_arg_list)
else:
for i in xrange(num_consumers): self.addConsumer(extra_arg_list)
def restartConsumers(self):
logger.debug("Restarting consumers")
extra_arg_list_list = []
current_num_consumers = self.num_consumers
# Shut them all down
for i in xrange(current_num_consumers):
# Grab extra_arg_list
(worker_num, consumer, extra_arg_list) = self.consumer_pool[-1]
extra_arg_list_list.append(extra_arg_list)
self.removeConsumer(True)
# Give them time to actually shutdown
time.sleep(1)
# Start them all up again
for i in xrange(current_num_consumers):
self.addConsumer(extra_arg_list_list.pop())
logger.debug("Restarting consumers - done")
def endConsumers(self):
logger.debug("Ending all consumers")
for i in xrange(self.num_consumers): self.removeConsumer()
def grabResults(self):
results = []
try:
while True:
next_result = self.consumer_results_queue.get_nowait()
results.append(next_result)
except Queue.Empty:
pass
return results
@rate_limited(1.0 / 10.0)
def rebalance(self):
if self.governorOffFlag:
return
progProducers = self.getProgressProducers()
progConsumers = self.getProgressConsumers()
num_tasks = self.get_num_tasks()
elapsed_backoff_time = (datetime.now() - self._rebalance_last_kick).seconds
logger.debug("Starting balancing (timer: %d/%d)" % (elapsed_backoff_time, self.rebalance_backoff_timer))
# Kill producers if all tasks have been served
if num_tasks == progProducers and progProducers > 0:
self.endProducers()
return
# Restart paused production on backoff timer or if we have at least 20% memory available
if self.num_producers == 0 and ((elapsed_backoff_time > self.rebalance_backoff_timer) or psutil_phymem_usage() < 80):
logger.debug("Rebalancing, restarting production")
self.addProducer()
return
# Memory governor
# Pause production if we're over 90%
if psutil_phymem_usage() > 90:
logger.debug("Rebalancing, mem > 90%, pausing production")
self.endProducers()
self._rebalance_last_kick = datetime.now()
return
# Reduce production if we're over 75%
if psutil_phymem_usage() > 75 and self.num_producers > 1:
if (datetime.now() - self._rebalance_mem_last_kick).seconds > self.rebalance_mem_backoff_timer:
logger.debug("Rebalancing, mem > 75%")
self.removeProducer()
self._rebalance_mem_last_kick = datetime.now()
return
# Memory pressure check
if psutil_phymem_usage() > 70:
if (datetime.now() - self._rebalance_mem_last_kick).seconds > self.rebalance_mem_backoff_timer:
logger.debug("Rebalancing mem, recycling processes")
self.restartConsumers()
self.restartProducers()
gc.collect()
self._rebalance_mem_last_kick = datetime.now()
logger.debug("Rebalancing mem, recycling processes - done")
return
else:
logger.debug("Rebalance (Memory pressure check) postponed, waiting for rebalance_backoff_timer")
# We wait until tasks are moving along to start rebalancing stuff
if progProducers < (num_tasks / 10):
return
# Auto-balancing
if progProducers > progConsumers * 2:
if self.num_producers > 1:
if elapsed_backoff_time > self.rebalance_backoff_timer:
logger.debug("Rebalancing, too many producers")
self.removeProducer()
self._rebalance_last_kick = datetime.now()
return
else: logger.debug("Rebalance postponed, waiting for rebalance_backoff_timer")
elif progProducers < progConsumers * 1.20:
if num_tasks > progProducers * 1.20:
if psutil_phymem_usage() < 70 and elapsed_backoff_time > self.rebalance_backoff_timer:
logger.debug("Rebalancing")
self.addProducer()
self._rebalance_last_kick = datetime.now()
return
else: logger.debug("Rebalance (Auto-balancing) postponed, waiting for rebalance_backoff_timer")
logger.debug("Balancing done")