diff --git a/src/dstack/_internal/server/services/runs/replicas.py b/src/dstack/_internal/server/services/runs/replicas.py index df29a65b9..8b88b89ad 100644 --- a/src/dstack/_internal/server/services/runs/replicas.py +++ b/src/dstack/_internal/server/services/runs/replicas.py @@ -1,4 +1,5 @@ from dataclasses import dataclass +from enum import Enum from typing import List, Optional, Tuple from dstack._internal.core.models.configurations import ReplicaGroup @@ -19,7 +20,11 @@ class GroupRolloutState: has_out_of_date_replicas: bool non_terminated_replica_count: int not_receiving_traffic_out_of_date_replica_count: int + """Out-of-date replicas that are not terminating and receive no requests. A replica that only + some gateway replicas route requests to is not counted.""" receiving_traffic_non_terminating_replica_count: int + """Replicas that are not terminating and receive requests through every running gateway + replica, if the service uses a gateway.""" def build_replica_lists( @@ -51,11 +56,12 @@ def build_replica_lists( elif {JobStatus.PROVISIONING, JobStatus.PULLING} & statuses: # if there are any provisioning or pulling jobs, the replica is active and has the importance of 1 active_replicas.append((1, is_out_of_date, replica_num, replica_jobs)) - elif not is_replica_receiving_traffic(run_model, replica_jobs): + elif get_replica_traffic(run_model, replica_jobs) == ReplicaTraffic.NONE: # all jobs are running, but not receiving traffic, the replica is active and has the importance of 2 active_replicas.append((2, is_out_of_date, replica_num, replica_jobs)) else: - # all jobs are running and receiving traffic, the replica is active and has the importance of 3 + # all jobs are running and receiving traffic (possibly not through every gateway + # replica yet), the replica is active and has the importance of 3 active_replicas.append((3, is_out_of_date, replica_num, replica_jobs)) # Sort by is_out_of_date (up-to-date first), importance (desc), and replica_num (asc) @@ -82,7 +88,7 @@ def get_group_rollout_state(run_model: RunModel, group: ReplicaGroup) -> GroupRo if any(not j.status.is_finished() for j in jobs): non_terminated_replica_nums.add(jobs[0].replica_num) - receiving_traffic = is_replica_receiving_traffic(run_model, jobs) + traffic = get_replica_traffic(run_model, jobs) if ( any(j.deployment_num < run_model.deployment_num for j in jobs) @@ -90,11 +96,11 @@ def get_group_rollout_state(run_model: RunModel, group: ReplicaGroup) -> GroupRo j.status not in [JobStatus.TERMINATING] + JobStatus.finished_statuses() for j in jobs ) - and not receiving_traffic + and traffic == ReplicaTraffic.NONE ): not_receiving_traffic_out_of_date_replica_count += 1 - if receiving_traffic and all(j.status != JobStatus.TERMINATING for j in jobs): + if traffic == ReplicaTraffic.FULL and all(j.status != JobStatus.TERMINATING for j in jobs): receiving_traffic_non_terminating_replica_count += 1 return GroupRolloutState( @@ -129,29 +135,40 @@ def has_out_of_date_replicas(run: RunModel, group_filter: Optional[str] = None) return False -def is_replica_receiving_traffic(run_model: RunModel, jobs: list[JobModel]) -> bool: +class ReplicaTraffic(Enum): + NONE = "none" + """The replica receives no requests.""" + PARTIAL = "partial" + """Some running gateway replicas route requests to the replica, others don't yet.""" + FULL = "full" + """The replica receives requests through every running gateway replica, if the service uses + a gateway.""" + + +def get_replica_traffic(run_model: RunModel, jobs: list[JobModel]) -> ReplicaTraffic: # Only job_num=0 is supposed to receive service requests job = jobs[0] if not job.ready or job.status != JobStatus.RUNNING: - return False + return ReplicaTraffic.NONE if not job.registered: # served by the service's router replica - return True + return ReplicaTraffic.FULL if run_model.gateway is None: # served by the in-server proxy - return True + return ReplicaTraffic.FULL running_gateway_replica_ids = { replica.id for replica in get_gateway_replica_models(run_model.gateway) if replica.status == GatewayReplicaStatus.RUNNING } - if not running_gateway_replica_ids: - return False - registration_by_replica_id = { - r.gateway_replica_id: r for r in job.service_replica_registrations + registered_gateway_replica_ids = { + r.gateway_replica_id + for r in job.service_replica_registrations + if r.is_registered and r.gateway_replica_id in running_gateway_replica_ids } - for replica_id in running_gateway_replica_ids: - registration = registration_by_replica_id.get(replica_id) - if registration is None or not registration.is_registered: - return False - return True + if not registered_gateway_replica_ids: + return ReplicaTraffic.NONE + if registered_gateway_replica_ids != running_gateway_replica_ids: + # e.g., a gateway replica has just become running and hasn't registered the replica yet + return ReplicaTraffic.PARTIAL + return ReplicaTraffic.FULL 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..aab764341 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 @@ -1748,6 +1748,191 @@ async def test_service_rolling_deployment_scales_down_old_replica_once_new_repli assert old_job.termination_reason == JobTerminationReason.SCALED_DOWN assert new_job.status == JobStatus.RUNNING + async def test_service_rolling_deployment_keeps_old_replica_when_new_gateway_replica_not_synced( + 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) + backend = await create_backend(session=session, project_id=project.id) + gateway = await create_gateway( + session=session, + project_id=project.id, + backend_id=backend.id, + status=GatewayStatus.RUNNING, + ) + gateway_replica_1 = await create_gateway_replica( + session=session, gateway_id=gateway.id, backend=backend, replica_num=0 + ) + # The second gateway replica has just become RUNNING and has not been synced yet, + # so no service replica is registered on it. + await create_gateway_replica( + session=session, gateway_id=gateway.id, backend=backend, replica_num=1 + ) + run_spec = get_run_spec( + repo_id=repo.name, + run_name="service-run", + configuration=ServiceConfiguration( + port=8080, + commands=["echo new!"], + ), + ) + run = await create_run( + session=session, + project=project, + repo=repo, + user=user, + run_name="service-run", + run_spec=run_spec, + status=RunStatus.RUNNING, + deployment_num=1, + gateway=gateway, + ) + old_job = await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + deployment_num=0, + registered=True, + ready=True, + replica_num=0, + ) + old_spec = get_job_spec(old_job) + old_spec.commands = ["echo old!"] + old_job.job_spec_data = old_spec.model_dump_json() + # The replacement is not ready yet, so the old replica is the only one serving. + new_job = await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + deployment_num=1, + registered=False, + ready=False, + replica_num=1, + ) + session.add( + ServiceReplicaRegistrationModel( + job_id=old_job.id, + gateway_replica_id=gateway_replica_1.id, + is_registered=True, + register_attempt=0, + ) + ) + await session.commit() + + lock_run(run) + await session.commit() + + await worker.process(run_to_pipeline_item(run)) + + await session.refresh(run) + assert run.status == RunStatus.RUNNING + + await session.refresh(old_job) + await session.refresh(new_job) + assert old_job.status == JobStatus.RUNNING + assert new_job.status == JobStatus.RUNNING + + async def test_service_rolling_deployment_stops_old_replica_without_traffic_not_partially_registered_one( + 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) + backend = await create_backend(session=session, project_id=project.id) + gateway = await create_gateway( + session=session, + project_id=project.id, + backend_id=backend.id, + status=GatewayStatus.RUNNING, + ) + gateway_replica_1 = await create_gateway_replica( + session=session, gateway_id=gateway.id, backend=backend, replica_num=0 + ) + # The second gateway replica has not been synced yet + await create_gateway_replica( + session=session, gateway_id=gateway.id, backend=backend, replica_num=1 + ) + run_spec = get_run_spec( + repo_id=repo.name, + run_name="service-run", + configuration=ServiceConfiguration( + port=8080, + commands=["echo new!"], + replicas=Range[int](min=2, max=2), + ), + ) + run = await create_run( + session=session, + project=project, + repo=repo, + user=user, + run_name="service-run", + run_spec=run_spec, + status=RunStatus.RUNNING, + deployment_num=1, + gateway=gateway, + ) + # Its probes never passed, so it receives no traffic + old_job_without_traffic = await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + deployment_num=0, + registered=False, + ready=False, + replica_num=0, + ) + # Receives traffic through the first gateway replica + old_job_partially_registered = await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + deployment_num=0, + registered=True, + ready=True, + replica_num=1, + ) + for old_job in (old_job_without_traffic, old_job_partially_registered): + old_spec = get_job_spec(old_job) + old_spec.commands = ["echo old!"] + old_job.job_spec_data = old_spec.model_dump_json() + # The surge replica is not ready yet + new_job = await create_job( + session=session, + run=run, + status=JobStatus.RUNNING, + deployment_num=1, + registered=False, + ready=False, + replica_num=2, + ) + session.add( + ServiceReplicaRegistrationModel( + job_id=old_job_partially_registered.id, + gateway_replica_id=gateway_replica_1.id, + is_registered=True, + register_attempt=0, + ) + ) + await session.commit() + + lock_run(run) + await session.commit() + + await worker.process(run_to_pipeline_item(run)) + + await session.refresh(run) + assert run.status == RunStatus.RUNNING + + await session.refresh(old_job_without_traffic) + await session.refresh(old_job_partially_registered) + await session.refresh(new_job) + assert old_job_without_traffic.status == JobStatus.TERMINATING + assert old_job_without_traffic.termination_reason == JobTerminationReason.SCALED_DOWN + assert old_job_partially_registered.status == JobStatus.RUNNING + assert new_job.status == JobStatus.RUNNING + async def test_service_router_rolling_deployment_surges_ready_worker_replica( self, test_db, session: AsyncSession, worker: RunWorker ) -> None: