1
0

parallel.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import logging
  4. import operator
  5. import sys
  6. from threading import Lock
  7. from threading import Semaphore
  8. from threading import Thread
  9. from docker.errors import APIError
  10. from docker.errors import ImageNotFound
  11. from six.moves import _thread as thread
  12. from six.moves.queue import Empty
  13. from six.moves.queue import Queue
  14. from compose.cli.colors import green
  15. from compose.cli.colors import red
  16. from compose.cli.signals import ShutdownException
  17. from compose.const import PARALLEL_LIMIT
  18. from compose.errors import HealthCheckFailed
  19. from compose.errors import NoHealthCheckConfigured
  20. from compose.errors import OperationFailedError
  21. from compose.utils import get_output_stream
  22. log = logging.getLogger(__name__)
  23. STOP = object()
  24. class GlobalLimit(object):
  25. """Simple class to hold a global semaphore limiter for a project. This class
  26. should be treated as a singleton that is instantiated when the project is.
  27. """
  28. global_limiter = Semaphore(PARALLEL_LIMIT)
  29. @classmethod
  30. def set_global_limit(cls, value):
  31. if value is None:
  32. value = PARALLEL_LIMIT
  33. cls.global_limiter = Semaphore(value)
  34. def parallel_execute_watch(events, writer, errors, results, msg, get_name, func_name):
  35. """ Watch events from a parallel execution, update status and fill errors and results.
  36. Returns exception to re-raise.
  37. """
  38. error_to_reraise = None
  39. for obj, result, exception in events:
  40. if exception is None:
  41. if func_name == 'start_service' and (
  42. callable(getattr(obj, 'containers', None)) and not obj.containers()):
  43. # If service has no containers started
  44. writer.write(msg, get_name(obj), 'failed', red)
  45. else:
  46. writer.write(msg, get_name(obj), 'done', green)
  47. results.append(result)
  48. elif isinstance(exception, ImageNotFound):
  49. # This is to bubble up ImageNotFound exceptions to the client so we
  50. # can prompt the user if they want to rebuild.
  51. errors[get_name(obj)] = exception.explanation
  52. writer.write(msg, get_name(obj), 'error', red)
  53. error_to_reraise = exception
  54. elif isinstance(exception, APIError):
  55. errors[get_name(obj)] = exception.explanation
  56. writer.write(msg, get_name(obj), 'error', red)
  57. elif isinstance(exception, (OperationFailedError, HealthCheckFailed, NoHealthCheckConfigured)):
  58. errors[get_name(obj)] = exception.msg
  59. writer.write(msg, get_name(obj), 'error', red)
  60. elif isinstance(exception, UpstreamError):
  61. writer.write(msg, get_name(obj), 'error', red)
  62. else:
  63. errors[get_name(obj)] = exception
  64. error_to_reraise = exception
  65. return error_to_reraise
  66. def parallel_execute(objects, func, get_name, msg, get_deps=None, limit=None):
  67. """Runs func on objects in parallel while ensuring that func is
  68. ran on object only after it is ran on all its dependencies.
  69. get_deps called on object must return a collection with its dependencies.
  70. get_name called on object must return its name.
  71. """
  72. objects = list(objects)
  73. stream = get_output_stream(sys.stderr)
  74. if ParallelStreamWriter.instance:
  75. writer = ParallelStreamWriter.instance
  76. else:
  77. writer = ParallelStreamWriter(stream)
  78. for obj in objects:
  79. writer.add_object(msg, get_name(obj))
  80. for obj in objects:
  81. writer.write_initial(msg, get_name(obj))
  82. events = parallel_execute_iter(objects, func, get_deps, limit)
  83. errors = {}
  84. results = []
  85. error_to_reraise = parallel_execute_watch(
  86. events, writer, errors, results, msg, get_name, func.__name__)
  87. for obj_name, error in errors.items():
  88. stream.write("\nERROR: for {} {}\n".format(obj_name, error))
  89. if error_to_reraise:
  90. raise error_to_reraise
  91. return results, errors
  92. def _no_deps(x):
  93. return []
  94. class State(object):
  95. """
  96. Holds the state of a partially-complete parallel operation.
  97. state.started: objects being processed
  98. state.finished: objects which have been processed
  99. state.failed: objects which either failed or whose dependencies failed
  100. """
  101. def __init__(self, objects):
  102. self.objects = objects
  103. self.started = set()
  104. self.finished = set()
  105. self.failed = set()
  106. def is_done(self):
  107. return len(self.finished) + len(self.failed) >= len(self.objects)
  108. def pending(self):
  109. return set(self.objects) - self.started - self.finished - self.failed
  110. class NoLimit(object):
  111. def __enter__(self):
  112. pass
  113. def __exit__(self, *ex):
  114. pass
  115. def parallel_execute_iter(objects, func, get_deps, limit):
  116. """
  117. Runs func on objects in parallel while ensuring that func is
  118. ran on object only after it is ran on all its dependencies.
  119. Returns an iterator of tuples which look like:
  120. # if func returned normally when run on object
  121. (object, result, None)
  122. # if func raised an exception when run on object
  123. (object, None, exception)
  124. # if func raised an exception when run on one of object's dependencies
  125. (object, None, UpstreamError())
  126. """
  127. if get_deps is None:
  128. get_deps = _no_deps
  129. if limit is None:
  130. limiter = NoLimit()
  131. else:
  132. limiter = Semaphore(limit)
  133. results = Queue()
  134. state = State(objects)
  135. while True:
  136. feed_queue(objects, func, get_deps, results, state, limiter)
  137. try:
  138. event = results.get(timeout=0.1)
  139. except Empty:
  140. continue
  141. # See https://github.com/docker/compose/issues/189
  142. except thread.error:
  143. raise ShutdownException()
  144. if event is STOP:
  145. break
  146. obj, _, exception = event
  147. if exception is None:
  148. log.debug('Finished processing: {}'.format(obj))
  149. state.finished.add(obj)
  150. else:
  151. log.debug('Failed: {}'.format(obj))
  152. state.failed.add(obj)
  153. yield event
  154. def producer(obj, func, results, limiter):
  155. """
  156. The entry point for a producer thread which runs func on a single object.
  157. Places a tuple on the results queue once func has either returned or raised.
  158. """
  159. with limiter, GlobalLimit.global_limiter:
  160. try:
  161. result = func(obj)
  162. results.put((obj, result, None))
  163. except Exception as e:
  164. results.put((obj, None, e))
  165. def feed_queue(objects, func, get_deps, results, state, limiter):
  166. """
  167. Starts producer threads for any objects which are ready to be processed
  168. (i.e. they have no dependencies which haven't been successfully processed).
  169. Shortcuts any objects whose dependencies have failed and places an
  170. (object, None, UpstreamError()) tuple on the results queue.
  171. """
  172. pending = state.pending()
  173. log.debug('Pending: {}'.format(pending))
  174. for obj in pending:
  175. deps = get_deps(obj)
  176. try:
  177. if any(dep[0] in state.failed for dep in deps):
  178. log.debug('{} has upstream errors - not processing'.format(obj))
  179. results.put((obj, None, UpstreamError()))
  180. state.failed.add(obj)
  181. elif all(
  182. dep not in objects or (
  183. dep in state.finished and (not ready_check or ready_check(dep))
  184. ) for dep, ready_check in deps
  185. ):
  186. log.debug('Starting producer thread for {}'.format(obj))
  187. t = Thread(target=producer, args=(obj, func, results, limiter))
  188. t.daemon = True
  189. t.start()
  190. state.started.add(obj)
  191. except (HealthCheckFailed, NoHealthCheckConfigured) as e:
  192. log.debug(
  193. 'Healthcheck for service(s) upstream of {} failed - '
  194. 'not processing'.format(obj)
  195. )
  196. results.put((obj, None, e))
  197. if state.is_done():
  198. results.put(STOP)
  199. class UpstreamError(Exception):
  200. pass
  201. class ParallelStreamWriter(object):
  202. """Write out messages for operations happening in parallel.
  203. Each operation has its own line, and ANSI code characters are used
  204. to jump to the correct line, and write over the line.
  205. """
  206. noansi = False
  207. lock = Lock()
  208. instance = None
  209. @classmethod
  210. def set_noansi(cls, value=True):
  211. cls.noansi = value
  212. def __init__(self, stream):
  213. self.stream = stream
  214. self.lines = []
  215. self.width = 0
  216. ParallelStreamWriter.instance = self
  217. def add_object(self, msg, obj_index):
  218. if msg is None:
  219. return
  220. self.lines.append(msg + obj_index)
  221. self.width = max(self.width, len(msg + ' ' + obj_index))
  222. def write_initial(self, msg, obj_index):
  223. if msg is None:
  224. return
  225. return self._write_noansi(msg, obj_index, '')
  226. def _write_ansi(self, msg, obj_index, status):
  227. self.lock.acquire()
  228. position = self.lines.index(msg + obj_index)
  229. diff = len(self.lines) - position
  230. # move up
  231. self.stream.write("%c[%dA" % (27, diff))
  232. # erase
  233. self.stream.write("%c[2K\r" % 27)
  234. self.stream.write("{:<{width}} ... {}\r".format(msg + ' ' + obj_index,
  235. status, width=self.width))
  236. # move back down
  237. self.stream.write("%c[%dB" % (27, diff))
  238. self.stream.flush()
  239. self.lock.release()
  240. def _write_noansi(self, msg, obj_index, status):
  241. self.stream.write(
  242. "{:<{width}} ... {}\r\n".format(
  243. msg + ' ' + obj_index, status, width=self.width
  244. )
  245. )
  246. self.stream.flush()
  247. def write(self, msg, obj_index, status, color_func):
  248. if msg is None:
  249. return
  250. if self.noansi:
  251. self._write_noansi(msg, obj_index, status)
  252. else:
  253. self._write_ansi(msg, obj_index, color_func(status))
  254. def get_stream_writer():
  255. instance = ParallelStreamWriter.instance
  256. if instance is None:
  257. raise RuntimeError('ParallelStreamWriter has not yet been instantiated')
  258. return instance
  259. def parallel_operation(containers, operation, options, message):
  260. parallel_execute(
  261. containers,
  262. operator.methodcaller(operation, **options),
  263. operator.attrgetter('name'),
  264. message,
  265. )
  266. def parallel_remove(containers, options):
  267. stopped_containers = [c for c in containers if not c.is_running]
  268. parallel_operation(stopped_containers, 'remove', options, 'Removing')
  269. def parallel_pause(containers, options):
  270. parallel_operation(containers, 'pause', options, 'Pausing')
  271. def parallel_unpause(containers, options):
  272. parallel_operation(containers, 'unpause', options, 'Unpausing')
  273. def parallel_kill(containers, options):
  274. parallel_operation(containers, 'kill', options, 'Killing')