diff --git a/docs/compute/scheduler.md b/docs/compute/scheduler.md index 110b8a3..98eaa50 100644 --- a/docs/compute/scheduler.md +++ b/docs/compute/scheduler.md @@ -95,7 +95,9 @@ while outbid; moving with nothing free to move to would throw that away and leav Every price limit is also capped by the project's hourly cap. A rental needs budget for at least one hour, under the project's row lock; it then holds the cost of its max runtime (or whatever is left), so two jobs can't spend -the same dollars. +the same dollars. A stopped box holds only what it spent plus its disk until it is destroyed (`box_destroy_hours`), so +the project's other jobs can use the rest. Resuming it needs an hour's budget free again and takes its run's hold +back; when the others hold too much, the resume is refused with what is free. ### Data transfer diff --git a/krater/compute/scheduler.py b/krater/compute/scheduler.py index b9f2dc2..eb543fc 100644 --- a/krater/compute/scheduler.py +++ b/krater/compute/scheduler.py @@ -1471,6 +1471,12 @@ def _watch_stopped( *, previously_seen: datetime | None, ) -> None: + if row.start_requested_at is None: + # Stopped and not resuming: it holds only its disk, so the project's other jobs can use the rest. + job.hold_cents = min( + job.hold_cents, + compute_jobs.stopped_box_hold_cents(job, row, destroy_hours=policy.box_destroy_hours), + ) if row.start_requested_at is not None: if seen.phase is Phase.RUNNING: row.state = ComputeInstanceState.RUNNING diff --git a/krater/services/compute_jobs.py b/krater/services/compute_jobs.py index 161db0a..ae7a1f3 100644 --- a/krater/services/compute_jobs.py +++ b/krater/services/compute_jobs.py @@ -592,6 +592,18 @@ def resume_box(session: Session, actor: Actor, *, job: ComputeJob, now: datetime f"The project already has {limit} machines at once, its limit. Resume this box once one of them ends." ) clearance.require_team_cleared(session, weave_client, job.project) + # Stopped, it held only its disk (`stopped_box_hold_cents`): running again needs a run's hold, as a new job does, + # under the project's row lock so two starts can't hold the same dollars. + session.execute(sa.select(Project.id).where(Project.id == job.project_id).with_for_update()) + free = budget.remaining_cents(session, job.project) - _others_held(session, job) + hour_cents = math.ceil(row.full_microusd / MICROUSD_PER_CENT) + if free < hour_cents: + raise InvalidState( + f"Resuming needs ${hour_cents / 100:,.2f} of the project's budget for its first hour, and " + f"${max(free, 0) / 100:,.2f} is free: the project's other jobs hold the rest. Resume it once one ends." + ) + wanted = math.ceil(row.full_microusd * smoke.runtime_cap_minutes(job) / 60 / MICROUSD_PER_CENT) + job.hold_cents = max(job.hold_cents, (job.spend_cents or 0) + min(wanted, free)) row.start_requested_at = now job.state_reason = "Resuming on the same machine." session.add( @@ -672,6 +684,14 @@ def _rate_microusd(job: ComputeJob) -> int | None: return live[-1].full_microusd if live else None +def stopped_box_hold_cents(job: ComputeJob, row: ComputeInstance, *, destroy_hours: int) -> int: + """What a stopped box holds of its project's budget: what it spent, plus its disk until the scheduler destroys it. + Not a GPU run: it isn't running. Holding its whole run kept every other job of the project waiting (production, + 2026-11-20: a box stopped by the network outage held $23.77, all its project had left, for hours). Resuming takes a + run's hold again (`resume_box`).""" + return (job.spend_cents or 0) + math.ceil(row.disk_microusd * destroy_hours / MICROUSD_PER_CENT) + + def _others_held(session: Session, job: ComputeJob) -> int: rows = session.execute( sa.select(ComputeJob.hold_cents, ComputeJob.spend_cents).where( diff --git a/tests/compute/test_boxes.py b/tests/compute/test_boxes.py index 18417fc..8215f64 100644 --- a/tests/compute/test_boxes.py +++ b/tests/compute/test_boxes.py @@ -460,3 +460,66 @@ def test_a_box_whose_runner_really_didnt_start_again_is_still_stopped( assert job.state is ComputeJobState.STOPPED and "didn't start again" in job.state_reason assert "runner_kept_running" not in _events(db_session, job) + + +def _stopped(db_session, member, project, client, fake): # noqa: F811 + job, row = _running_box(db_session, member, project, client, fake) + compute_jobs.stop_box(db_session, member, job=job, now=NOW + timedelta(minutes=3)) + runner_api.synced(db_session, row, now=NOW + timedelta(minutes=3, seconds=10)) + _pass(db_session, client, NOW + timedelta(minutes=4)) # stops it + _pass(db_session, client, NOW + timedelta(minutes=5)) # watches it stopped + return job, row + + +def test_a_stopped_box_holds_only_its_disk_so_the_projects_other_jobs_can_start( + db_session, member, project, client, fake, monkeypatch +) -> None: # noqa: F811 + # Production, 2026-11-20: a box stopped by the network outage kept its week's hold, all its project had left, and + # the project's next job and box waited with "$0.00 is free". + from krater.services import compute_settings + + monkeypatch.setattr("krater.services.budget.remaining_cents", lambda session, project: 1000) + job, row = _running_box(db_session, member, project, client, fake) + assert job.hold_cents == 1000 # a week's run costs more than the budget: it holds all of it + waiting = _submit(db_session, member, project, now=NOW + timedelta(minutes=2, seconds=30)) + _pass(db_session, client, NOW + timedelta(minutes=3)) + assert waiting.state is ComputeJobState.QUEUED and waiting.state_reason.startswith("Waiting for budget") + + compute_jobs.stop_box(db_session, member, job=job, now=NOW + timedelta(minutes=3, seconds=30)) + runner_api.synced(db_session, row, now=NOW + timedelta(minutes=3, seconds=40)) + _pass(db_session, client, NOW + timedelta(minutes=4)) + _pass(db_session, client, NOW + timedelta(minutes=5)) + + destroy_hours = compute_settings.effective_policy(db_session, project).box_destroy_hours + assert job.state is ComputeJobState.STOPPED + assert job.hold_cents == job.spend_cents + -(-row.disk_microusd * destroy_hours // 10_000) + _pass(db_session, client, NOW + timedelta(minutes=6)) + assert waiting.state is not ComputeJobState.QUEUED + + +def test_resume_takes_a_runs_hold_again_and_waits_while_other_jobs_hold_the_budget( + db_session, member, project, client, fake, monkeypatch +) -> None: # noqa: F811 + from krater.services.errors import InvalidState + + monkeypatch.setattr("krater.services.budget.remaining_cents", lambda session, project: 1000) + job, row = _stopped(db_session, member, project, client, fake) + other = _submit(db_session, member, project, max_runtime_minutes=7 * 24 * 60, now=NOW + timedelta(minutes=5)) + _pass(db_session, client, NOW + timedelta(minutes=6)) + assert other.state is not ComputeJobState.QUEUED # it holds everything the stopped box let go + + # Spend since leaves less free than the box's first hour, counting the disk hold it keeps for itself. + left = other.hold_cents + 6 + monkeypatch.setattr("krater.services.budget.remaining_cents", lambda session, project: left) + with pytest.raises(InvalidState, match="Resuming needs"): + compute_jobs.resume_box( + db_session, member, job=job, now=NOW + timedelta(minutes=7), weave_client=current_weave[0] + ) + assert row.start_requested_at is None and job.state is ComputeJobState.STOPPED + + compute_jobs.cancel_job(db_session, member, job=other, now=NOW + timedelta(minutes=8)) + compute_jobs.resume_box(db_session, member, job=job, now=NOW + timedelta(minutes=9), weave_client=current_weave[0]) + assert row.start_requested_at is not None + assert job.hold_cents > 500 # a run's hold again, not just its disk + _pass(db_session, client, NOW + timedelta(minutes=10)) # resuming: the hold stays + assert job.hold_cents > 500