diff --git a/dvc/repo/experiments/queue/local.py b/dvc/repo/experiments/queue/local.py index 081e61848..c8e556fdd 100644 --- a/dvc/repo/experiments/queue/local.py +++ b/dvc/repo/experiments/queue/local.py @@ -1,7 +1,6 @@ import hashlib import logging import os -import time from collections import defaultdict from typing import ( TYPE_CHECKING, @@ -15,8 +14,6 @@ from typing import ( Set, ) -from celery import chain -from celery.canvas import Signature from funcy import cached_property, first from kombu.message import Message @@ -35,7 +32,7 @@ from ..executor.local import WorkspaceExecutor from ..refs import EXEC_BRANCH from ..stash import ExpStashEntry from .base import BaseStashQueue, QueueEntry, QueueGetResult -from .tasks import cleanup_exp, collect_exp, setup_exp +from .tasks import run_exp if TYPE_CHECKING: from dvc.repo.experiments import Experiments @@ -136,26 +133,9 @@ class LocalCeleryQueue(BaseStashQueue): def put(self, *args, **kwargs) -> QueueEntry: """Stash an experiment and add it to the queue.""" entry = self._stash_exp(*args, **kwargs) - task_chain = self._chain_exp(entry) - task_chain.freeze() - task_chain.delay() + self.celery.signature(run_exp.s(entry.asdict())).delay() return entry - def _chain_exp(self, entry: QueueEntry) -> Signature: - """Return a complete experiment chain for this queue entry.""" - infofile = self.get_infofile_path(entry.stash_rev) - cmd = ["dvc", "exp", "exec-run", "--infofile", infofile] - return self.celery.signature( - chain( - setup_exp.s(entry.asdict()), - self.proc.run_signature( - cmd, name=entry.stash_rev, immutable=True - ), - collect_exp.s(entry.asdict()), - cleanup_exp.s(entry.asdict()), - ) - ) - # NOTE: Queue consumption should not be done directly. Celery worker(s) # will automatically consume available experiments. def get(self) -> QueueGetResult: @@ -180,7 +160,7 @@ class LocalCeleryQueue(BaseStashQueue): def _iter_queued(self) -> Generator[_MessageEntry, None, None]: for msg in self.celery.iter_queued(): - if msg.headers.get("task") != setup_exp.name: + if msg.headers.get("task") != run_exp.name: continue args, kwargs, _embed = msg.decode() entry_dict = kwargs.get("entry_dict", args[0]) @@ -188,44 +168,20 @@ class LocalCeleryQueue(BaseStashQueue): def _iter_processed(self) -> Generator[_MessageEntry, None, None]: for msg in self.celery.iter_processed(): - if msg.headers.get("task") != setup_exp.name: + if msg.headers.get("task") != run_exp.name: continue args, kwargs, _embed = msg.decode() entry_dict = kwargs.get("entry_dict", args[0]) yield _MessageEntry(msg, QueueEntry.from_dict(entry_dict)) def _iter_active_tasks(self) -> Generator[_TaskEntry, None, None]: - from celery.exceptions import TimeoutError as _CeleryTimeout - - for msg, entry in self._iter_processed(): - setup_id = msg.headers["id"] - try: - self._get_cleanup_result(setup_id, timeout=0.0) - except _CeleryTimeout: - yield _TaskEntry(setup_id, entry) - - @staticmethod - def _get_cleanup_result(setup_id: str, timeout: Optional[float] = None): - from celery.exceptions import TimeoutError as _CeleryTimeout from celery.result import AsyncResult - def _get(result: AsyncResult, expires: Optional[float]): - timeout = None if expires is None else expires - time.time() - if timeout is not None and timeout <= 0.0 and not result.ready(): - raise _CeleryTimeout - return result.get(timeout=timeout) - - expires: Optional[float] = ( - None if timeout is None else time.time() + timeout - ) - setup_result = AsyncResult(setup_id) - _get(setup_result, expires) - run_result = setup_result.children[0] - _get(run_result, expires) - collect_result = run_result.children[0] - _get(collect_result, expires) - cleanup_result = collect_result.children[0] - _get(cleanup_result, expires) + for msg, entry in self._iter_processed(): + task_id = msg.headers["id"] + result = AsyncResult(task_id) + if not result.ready(): + yield _TaskEntry(task_id, entry) def iter_active(self) -> Generator[QueueEntry, None, None]: for _, entry in self._iter_active_tasks(): @@ -238,6 +194,7 @@ class LocalCeleryQueue(BaseStashQueue): self, entry: QueueEntry, timeout: Optional[float] = None ) -> Optional[ExecutorResult]: from celery.exceptions import TimeoutError as _CeleryTimeout + from celery.result import AsyncResult def _load_info(rev: str) -> ExecutorInfo: infofile = self.get_infofile_path(rev) @@ -254,11 +211,12 @@ class LocalCeleryQueue(BaseStashQueue): for queue_entry in self.iter_queued(): if entry.stash_rev == queue_entry.stash_rev: raise DvcException("Experiment has not been started.") - for setup_id, active_entry in self._iter_active_tasks(): + for task_id, active_entry in self._iter_active_tasks(): if entry.stash_rev == active_entry.stash_rev: - logger.debug("Waiting for exp task '%s' to complete", setup_id) + logger.debug("Waiting for exp task '%s' to complete", task_id) try: - self._get_cleanup_result(setup_id, timeout=timeout) + result = AsyncResult(task_id) + result.get(timeout=timeout) except _CeleryTimeout as exc: raise DvcException( "Timed out waiting for exp to finish." diff --git a/dvc/repo/experiments/queue/tasks.py b/dvc/repo/experiments/queue/tasks.py index c2912bb64..e384ecf9a 100644 --- a/dvc/repo/experiments/queue/tasks.py +++ b/dvc/repo/experiments/queue/tasks.py @@ -1,7 +1,6 @@ -from typing import Any, Dict, List +from typing import Any, Dict from celery import shared_task -from celery.signals import task_postrun from celery.utils.log import get_task_logger from dvc.utils.fs import remove @@ -14,11 +13,14 @@ logger = get_task_logger(__name__) @shared_task -def setup_exp(entry_dict: Dict[str, Any]) -> None: +def setup_exp(entry_dict: Dict[str, Any]) -> str: """Setup an experiment. Arguments: entry_dict: Serialized QueueEntry for this experiment. + + Returns: + Root executor (temp) directory for this experiment. """ from dvc.repo import Repo @@ -31,6 +33,7 @@ def setup_exp(entry_dict: Dict[str, Any]) -> None: ) infofile = repo.experiments.celery_queue.get_infofile_path(entry.stash_rev) executor.info.dump_json(infofile) + return executor.root_dir @shared_task @@ -91,18 +94,35 @@ def cleanup_exp( # pylint: disable=unused-argument remove(tmp_dir) -@task_postrun.connect(sender=cleanup_exp) -def _cleanup_postrun_handler( - args: List[Any] = None, - **kwargs, -): +def _set_collected(infofile: str): + try: + executor_info = ExecutorInfo.load_json(infofile) + executor_info.collected = True + executor_info.dump_json(infofile) + except FileNotFoundError: + pass + + +@shared_task +def run_exp(entry_dict: Dict[str, Any]) -> None: + """Run a full experiment. + + Experiment subtasks are executed inline as one atomic operation. + + Arguments: + entry_dict: Serialized QueueEntry for this experiment. + """ from dvc.repo import Repo - assert args - (_, entry_dict) = args entry = QueueEntry.from_dict(entry_dict) repo = Repo(entry.dvc_root) - infofile = repo.experiments.celery_queue.get_infofile_path(entry.stash_rev) - executor_info = ExecutorInfo.load_json(infofile) - executor_info.collected = True - executor_info.dump_json(infofile) + queue = repo.experiments.celery_queue + infofile = queue.get_infofile_path(entry.stash_rev) + root_dir = setup_exp.s(entry_dict)() + try: + cmd = ["dvc", "exp", "exec-run", "--infofile", infofile] + proc_dict = queue.proc.run_signature(cmd, name=entry.stash_rev)() + collect_exp.s(proc_dict, entry_dict)() + finally: + cleanup_exp.s(root_dir, entry_dict)() + _set_collected(infofile)