From 624f113edb460dc309dec01eda9d12bd59e9ef67 Mon Sep 17 00:00:00 2001 From: Andrey Cheptsov Date: Fri, 27 Nov 2026 13:15:39 +0100 Subject: [PATCH 1/2] Preserve job processing times when unlocking jobs --- .../pipeline_tasks/runs/__init__.py | 3 +- .../pipeline_tasks/runs/test_active.py | 44 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) 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 713828d27b..d7d199f047 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 a289e1a5f0..93a1e36ba9 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 @@ -31,6 +32,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 +128,41 @@ async def test_transitions_provisioning_to_running( assert run.status == RunStatus.RUNNING assert run.lock_token is None + async def test_does_not_postpone_running_job_processing( + self, test_db, session: AsyncSession, worker: RunWorker + ) -> 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, + ) + job = await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + last_processed_at=get_current_datetime() - timedelta(minutes=1), + ) + 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=1) + assert [item.id for item in items] == [job.id] + async def test_terminates_run_when_all_jobs_done( self, test_db, session: AsyncSession, worker: RunWorker ) -> None: @@ -1059,17 +1096,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 +1131,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 From 5b0600ff29fd6b7c54c1b8d9cfa23c7618eec2c1 Mon Sep 17 00:00:00 2001 From: Andrey Cheptsov Date: Fri, 27 Nov 2026 13:52:55 +0100 Subject: [PATCH 2/2] Cover job scheduling across service configurations --- .../pipeline_tasks/runs/test_active.py | 74 ++++++++++++++++--- 1 file changed, 64 insertions(+), 10 deletions(-) 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 93a1e36ba9..1e32df3549 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 @@ -11,6 +11,8 @@ from sqlalchemy.ext.asyncio import AsyncSession from dstack._internal.core.models.configurations import ( + AnyRunConfiguration, + DevEnvironmentConfiguration, ScalingSpec, ServiceConfiguration, TaskConfiguration, @@ -128,8 +130,46 @@ 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 + self, + test_db, + session: AsyncSession, + worker: RunWorker, + configuration: AnyRunConfiguration, ) -> None: project = await create_project(session=session) user = await create_user(session=session) @@ -140,13 +180,26 @@ async def test_does_not_postpone_running_job_processing( repo=repo, user=user, status=RunStatus.RUNNING, - ) - job = await create_job( - session=session, - run=run, - status=JobStatus.RUNNING, - last_processed_at=get_current_datetime() - timedelta(minutes=1), - ) + 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() @@ -160,8 +213,8 @@ async def test_does_not_postpone_running_job_processing( lock_timeout=timedelta(seconds=30), heartbeater=Mock(), ) - items = await fetcher.fetch(limit=1) - assert [item.id for item in items] == [job.id] + 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 @@ -1395,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