parallel.py 9.1 KB

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