diff --git a/src/dstack/_internal/server/background/pipeline_tasks/runs/__init__.py b/src/dstack/_internal/server/background/pipeline_tasks/runs/__init__.py index 713828d27..d7d199f04 100644 --- a/src/dstack/_internal/server/background/pipeline_tasks/runs/__init__.py +++ b/src/dstack/_internal/server/background/pipeline_tasks/runs/__init__.py @@ -703,9 +703,10 @@ def _build_active_job_update_rows( if job_update_map is not None: for key, value in job_update_map.items(): update_row[key] = value + # Unlocking a job must not postpone its own pipeline. + set_processed_update_map_fields(update_row) if job_id in unlock_job_ids: set_unlock_update_map_fields(update_row) - set_processed_update_map_fields(update_row) job_update_rows.append(update_row) return job_update_rows diff --git a/src/tests/_internal/server/background/pipeline_tasks/runs/test_active.py b/src/tests/_internal/server/background/pipeline_tasks/runs/test_active.py index a289e1a5f..1e32df354 100644 --- a/src/tests/_internal/server/background/pipeline_tasks/runs/test_active.py +++ b/src/tests/_internal/server/background/pipeline_tasks/runs/test_active.py @@ -1,8 +1,9 @@ +import asyncio import json import uuid from datetime import timedelta from typing import Optional -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock, Mock, patch import gpuhunt import pytest @@ -10,6 +11,8 @@ from sqlalchemy.ext.asyncio import AsyncSession from dstack._internal.core.models.configurations import ( + AnyRunConfiguration, + DevEnvironmentConfiguration, ScalingSpec, ServiceConfiguration, TaskConfiguration, @@ -31,6 +34,7 @@ RunStatus, RunTerminationReason, ) +from dstack._internal.server.background.pipeline_tasks.jobs_running import JobRunningFetcher from dstack._internal.server.background.pipeline_tasks.runs import RunWorker from dstack._internal.server.models import ( JobModel, @@ -126,6 +130,92 @@ async def test_transitions_provisioning_to_running( assert run.status == RunStatus.RUNNING assert run.lock_token is None + @pytest.mark.parametrize( + "configuration", + [ + pytest.param(DevEnvironmentConfiguration(ide="vscode"), id="dev-environment"), + pytest.param(TaskConfiguration(commands=["echo task"]), id="task"), + pytest.param( + ServiceConfiguration( + port=8080, + commands=["echo service"], + replicas=Range[int](min=2, max=2), + ), + id="replicated-service", + ), + pytest.param( + ServiceConfiguration.model_validate( + { + "type": "service", + "port": 8080, + "model": "test-model", + "groups": [ + {"name": "worker", "replicas": 2, "commands": ["echo worker"]}, + { + "name": "router", + "replicas": 1, + "router": {"type": "smg"}, + "commands": ["echo router"], + }, + ], + } + ), + id="router-service", + ), + ], + ) + async def test_does_not_postpone_running_job_processing( + self, + test_db, + session: AsyncSession, + worker: RunWorker, + configuration: AnyRunConfiguration, + ) -> None: + project = await create_project(session=session) + user = await create_user(session=session) + repo = await create_repo(session=session, project_id=project.id) + run = await create_run( + session=session, + project=project, + repo=repo, + user=user, + status=RunStatus.RUNNING, + run_spec=get_run_spec(repo_id=repo.name, configuration=configuration), + ) + replica_groups: list[Optional[str]] = [None] + if isinstance(configuration, ServiceConfiguration): + replica_groups = [ + group.name + for group in configuration.replica_groups + for _ in range(group.replicas.min or 0) + ] + jobs = [ + await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + replica_num=replica_num, + replica_group_name=group_name, + last_processed_at=get_current_datetime() - timedelta(minutes=1), + ) + for replica_num, group_name in enumerate(replica_groups) + ] + lock_run(run) + await session.commit() + + await worker.process(run_to_pipeline_item(run)) + + # Run processing must leave the job eligible for its own pipeline. + fetcher = JobRunningFetcher( + queue=asyncio.Queue(), + queue_desired_minsize=1, + min_processing_interval=timedelta(seconds=5), + lock_timeout=timedelta(seconds=30), + heartbeater=Mock(), + ) + items = await fetcher.fetch(limit=len(jobs)) + assert {item.id for item in items} == {job.id for job in jobs} + async def test_terminates_run_when_all_jobs_done( self, test_db, session: AsyncSession, worker: RunWorker ) -> None: @@ -1059,17 +1149,20 @@ async def test_service_scale_down( ) run.desired_replica_count = 2 run.desired_replica_counts = json.dumps({"0": 2}) + old_time = get_current_datetime() - timedelta(minutes=1) await create_job( session=session, run=run, status=JobStatus.RUNNING, replica_num=0, + last_processed_at=old_time, ) await create_job( session=session, run=run, status=JobStatus.RUNNING, replica_num=1, + last_processed_at=old_time, ) lock_run(run) await session.commit() @@ -1091,6 +1184,8 @@ async def test_service_scale_down( assert len(running) == 1 assert len(terminating) == 1 assert terminating[0].termination_reason == JobTerminationReason.SCALED_DOWN + assert running[0].last_processed_at == old_time + assert terminating[0].last_processed_at == run.last_processed_at async def test_service_zero_scale_noop( self, test_db, session: AsyncSession, worker: RunWorker @@ -1353,6 +1448,7 @@ async def test_service_in_place_deployment_bump( await session.refresh(job) assert job.deployment_num == 1 + assert job.last_processed_at == run.last_processed_at async def test_service_in_place_deployment_bump_on_cpu_arch_widening( self, test_db, session: AsyncSession, worker: RunWorker, image_config_mock