parallel.py 11 KB

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