parallel.py 7.2 KB

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