Skip to content

ateapi: select highest-fidelity worker in a single Schedule pass - #3777

Open
Shrutiya Mohan (shrutiyam-glitch) wants to merge 5 commits into
agent-substrate:mainfrom
shrutiyam-glitch:scheduler-update
Open

Shrutiya Mohan (shrutiyam-glitch) wants to merge 5 commits into
agent-substrate:mainfrom
shrutiyam-glitch:scheduler-update

Conversation

@shrutiyam-glitch

@shrutiyam-glitch Shrutiya Mohan (shrutiyam-glitch) commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3779

Resuming an actor from a memory snapshot could scan the worker fleet twice. The scheduler first looked only for workers able to restore the snapshot's memory state. If none had room, it dropped that requirement and scanned again, accepting any worker of the same sandbox class for a cold boot from the snapshot's files. Each scan locks the worker cache and copies the fleet, and the retry had to be mirrored wherever an existing assignment is re-checked.

The scheduler now does this in one pass. It rates each worker by how much of the snapshot it can restore and keeps only the best-rated workers that have room. A worker that can restore memory state always wins over one that can only cold boot, and the existing random choice applies within that group. Callers state up front whether a cold boot is acceptable.

The set of workers an actor can land on and the choice among them are unchanged. Only the number of scans changes.

AI use: AI-assisted, I reviewed and tested the changes.

  • Tests pass
  • Docs updated (none needed; the existing docs already describe the cold-boot fallback)

@shrutiyam-glitch

Shrutiya Mohan (shrutiyam-glitch) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Benchmark results from #4000 (BenchmarkSchedule, 3-run medians at n = 10,000 workers, excluding workercache copy and Postgres):

go test -run='^$' -bench='BenchmarkSchedule/.+/n=10000$' -benchmem -count=3 ./cmd/ateapi/internal/scheduling
Scenario main #3777 Change
cold-boot 3,930.7 µs 3,822.2 µs -2.8%
memory-hit 17,250.4 µs 9,938.1 µs -42.4%
memory-fallback 20,377.3 µs 10,476.5 µs -48.6%
memory-mixed 9,988.2 µs 7,764.2 µs -22.3%
memory-rollout 20,588.3 µs 19,450.0 µs -5.5%
pinned 657.9 µs 521.5 µs -20.7%

What this shows:

  • memory-fallback (-48.6%): Drops from two full Schedule scans to one pass.
  • memory-hit (-42.4%): Evaluates each worker's runtime once (BestRuntime) instead of twice (Applies + Booking), halving cpu_features parsing on matching workers.
  • memory-mixed (-22.3%): Both branches finish in one pass; the 10% matching workers run half as many cpu_features parses, while both still parse cpu_features on the 90% mismatched workers before sandbox_bundle_hash rejects them.
  • memory-rollout (-5.5%): Every worker carries two non-matching restorable runtimes ahead of the matching one, so both branches spend most of their time checking those non-matching runtimes on the 90% non-matching workers, while #3777 avoids the duplicate runtime scan on the 10% matching workers.
  • cold-boot (-2.8%): Already ran in one pass without cpu_features parsing; remaining time is dominated by selector checks and per-candidate bookingFor allocations.
  • pinned (-20.7%): Both branches scan all 10,000 workers to filter RequiredNodes, saving the duplicate runtime check on the 4 workers on the pinned node.

if constraints.RequiredRuntime.Runtime() == nil {
// canRelaxRuntime reports whether scheduling may accept a worker that
// restores snap without its memory state.
func canRelaxRuntime(actor *ateapi.Actor, snap *ateapi.Snapshot, required *ateapi.ActorSandboxRuntime) bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "canRelaxRuntime" function might be doing multiple jobs, for example why is "snapshot locality", localElsewhere, uploadCompleted etc. taken into account here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were the filters that were already there.
They indicate if the fidelity can be downgraded from MEMORY to ROOTFS.

For example,
localElsewhere (#3094): allows a snapshot whose node was deleted while its upload was still PENDING to fall back to ROOTFS from object storage.
"snapshot locality check" - probably a carryover from before local and durable restore were unified (#2967); today it is only reached on RESUMING re-entry in validateAssignedWorker while the actor is still on the snapshot's local node. 64e9448
I need to look into this more. will probably decouple if needed in a separate pr ?

Add Requirement.BestRuntime(status, floor) to return both the winning
WorkerSandboxRuntime and the SnapshotFidelity it achieves in one call
(DefaultRuntime or RestorableRuntimes at MEMORY, or DefaultRuntime at ROOTFS
when floor allows fallback), so the scheduler does not need to scan a
worker's runtimes twice for eligibility and overhead booking.

Signed-off-by: Shrutiya <[email protected]>
… pass

Replace Constraints.RequiredRuntime with Constraints.MinFidelity (defaulting
to SNAPSHOT_FIDELITY_MEMORY) and have Schedule evaluate the worker cache in a
single pass.
For each worker that passes matchesStatic, Schedule calls
SnapshotRuntime.BestRuntime(status, floor) once, computes booking from the
returned runtime's ActorOverhead, and resets candidates[:0] in place whenever
a candidate with room exceeds bestFidelity.

Signed-off-by: Shrutiya <[email protected]>
Set Constraints.MinFidelity in schedulingConstraints when canRelaxRuntime
allows fallback, so assignWorkerAttempt calls Scheduler.Schedule once without
mutating Constraints and validateAssignedWorker checks Scheduler.Applies
directly. Remove appliesWithFallback.

Signed-off-by: Shrutiya <[email protected]>
admit previously re-checked the row-locked worker against constraints.MinFidelity, so a worker picked at MEMORY that dropped the snapshot's runtime before bind could be accepted at ROOTFS when relaxable. Hold admit to at least the fidelity tier Schedule picked, returning errWorkerFilledUp so the retry can pick another worker.

Signed-off-by: Shrutiya <[email protected]>
Have Schedule return the picked worker, its fidelity tier, and its
resource booking in a Placement struct. assignWorkerAttempt previously
recomputed both on the cached worker to populate assignment.Resources
and to hold admit to the picked tier.

Unexport Constraints.effectiveMinFidelity now that controlapi no longer
calls it, and check the recorded assignment booking in
TestAssignWorkerAttempt_RefusesChangedOverhead.

Signed-off-by: Shrutiya <[email protected]>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

Comment on lines +180 to +183
if fidelity > bestFidelity {
bestFidelity = fidelity
floor = fidelity
candidates = candidates[:0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: raising floor here is what makes later lower-tier workers drop out in BestRuntime. Worth a one-line comment.

}

var candidates []candidate
floor := constraints.effectiveMinFidelity()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the floor be VOLUMES for all actors? we haven't introduced the minimumFidelity concept in the API yet, why does effectiveMinFidelity default to memory?

Limits: tmpl.GetResources().GetLimits(),
}
if canRelaxRuntime(actor, snap, required) {
c.MinFidelity = ateapi.SnapshotFidelity_SNAPSHOT_FIDELITY_ROOTFS

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// effectiveMinFidelity returns MinFidelity, or SNAPSHOT_FIDELITY_MEMORY when
// MinFidelity is unset.
func (c Constraints) effectiveMinFidelity() ateapi.SnapshotFidelity {
if c.MinFidelity == ateapi.SnapshotFidelity_SNAPSHOT_FIDELITY_UNSPECIFIED {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is the MinFidelity unset? Can we make sure each callsite always sets the minFidelity?

// MinFidelity is unset.
func (c Constraints) effectiveMinFidelity() ateapi.SnapshotFidelity {
if c.MinFidelity == ateapi.SnapshotFidelity_SNAPSHOT_FIDELITY_UNSPECIFIED {
return ateapi.SnapshotFidelity_SNAPSHOT_FIDELITY_MEMORY

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should default to VOLUMES, right?

candidates = append(candidates, cand)
if fidelity > bestFidelity {
bestFidelity = fidelity
floor = fidelity

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to update floor here? Shouldn't floor always be the minimumFidelity that an actor can survive (i.e. volumes)?

// runtime reaches floor. A zero Requirement imposes no runtime constraint: it
// returns DefaultRuntime at SNAPSHOT_FIDELITY_MEMORY, which meets any floor,
// or nil and SNAPSHOT_FIDELITY_UNSPECIFIED before the worker has registered.
func (r Requirement) BestRuntime(status *ateapi.WorkerStatus, floor ateapi.SnapshotFidelity) (*ateapi.WorkerSandboxRuntime, ateapi.SnapshotFidelity) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 should-fix 🟡 – floor only changes the result in one case: at MEMORY it turns (def, ROOTFS) into (nil, UNSPECIFIED). The scheduling callers (Schedule, doesNotFit, Applies) all check fidelity < floor (or >=) again, so the filter runs twice and "below floor" is signaled in-band through UNSPECIFIED.

The zero values also disagree. BestRuntime(status, UNSPECIFIED) accepts ROOTFS, while Constraints{MinFidelity: UNSPECIFIED} means MEMORY. The doc says a zero Requirement "meets any floor", which does not hold for a floor above MEMORY.

Could BestRuntime(status) return the runtime and the fidelity it reaches, and leave the floor comparison to callers? matchingRuntime and WorkerRuntimeFor become explicit about what they require instead of passing different constants, and Schedule can skip lower tiers directly with fidelity < floor.

// placementOf is the Placement the real Schedule returns when it picks worker
// under c.
func placementOf(worker *ateapi.Worker, c scheduling.Constraints) scheduling.Placement {
_, fidelity := c.SnapshotRuntime.BestRuntime(worker.GetStatus(), ateapi.SnapshotFidelity_SNAPSHOT_FIDELITY_ROOTFS)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ateapi.SnapshotFidelity_SNAPSHOT_FIDELITY_ROOTFS

This should test other possible values of c.minFidelity as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resume scans the worker fleet twice when no worker can restore a memory snapshot

3 participants