parallel.py 7.0 KB

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