Skip to content

· 7 min read · Engineering

Designed for Flexibility, Part 5: Three Regions and a Stockout

Designed for Flexibility, Part 5: Three Regions and a Stockout

The advisor is capacity-advisor, an open-source Go program I wrote for this series. It ranks Spot VM options (discounted spare capacity Google can take back at short notice) using the obtainability score from Compute Engine’s capacity-advice API. The score goes from zero to one and estimates your odds of getting the machines you asked for. My fine-tune job needed one NVIDIA L4 GPU, and the advisor put a g2-standard-4 in us-east1-c at the top of its list with a 0.90. For the next day the GKE autoscaler (the part that adds a node if a Pod won’t fit) turned down each L4 I asked for in that zone, and the score didn’t budge. What should a capacity selector do if a confident score and the cluster disagree, and how would it even find out that they do?

Part 3 turns the advisor’s numbers into an ordered fallback list, and Part 4 shows how a job survives when Google takes its Spot VM back.

The Plan: A Fine-Tune on a Spot-Then-Flex-Start Ladder

The job was a LoRA fine-tune of Gemma-2B, an open model from Google, on one L4. LoRA only trains a thin set of new weights on top of the base model. It wrote checkpoints to a GCS-FUSE mount (a Cloud Storage bucket the Pod sees as a local directory), and a preemption would cost me only the steps since the last one. Kueue, a Kubernetes job queue, holds the job until quota clears so it doesn’t thrash against a cluster that can’t fit it. After admission the job climbs a Spot-then-flex-start ladder, the ordered list of machine options (rungs) the cluster tries in turn. Spot comes first for the discount. If Spot L4s vanish mid-run, it drops to flex-start (it holds the job until GPU capacity frees up) and carries on.

On paper nothing was missing. The advisor ranked the rungs, Kueue held the job until it fit, and checkpoints covered any reclaim.

Thirty-Six Hours of Refusals in Three Regions

There were no L4s in us-east1-c. The job sat Pending for about a day, and the autoscaler logged the same reason each time: no.scale.up.nap.pod.zonal.resources.exceeded. Quota wasn’t it. I had 16 L4s of quota and was using none. I checked us-east4 next. It said 0.90 at 19:21 UTC and 0.10 at 23:44. I tore the stack down and rebuilt it in us-central1, where zones a, b and c were dry for another half day. Over about thirty-six hours and three regions, the advisor kept reading 0.90 for zones that had refused me.

The stockout ended the moment a live attempt found a g2-standard-8 in us-central1-a. It has the same single L4 as the g2-standard-4 I had asked for, in a machine one size larger. I got it by trying to create one.

The Score Never Heard About the Refusals

The advisor’s composite score, from Part 3, blends obtainability with uptime, preemption history and evidence from the cluster, and drops anything below 0.4. The math was fine, but the score had no way to hear “no.” To the advisor, us-east1-c still looked like my best bet.

The function is in score.go. This time I’ve kept the doc comment Part 3 left out:

advisor/internal/score/score.go
// Composite folds every factor into one number. evidenceFactor is a
// multiplicand rather than a gate: a candidate crushed by evidence must stay in
// the analysis so the renderer can widen around it, whereas a dropped candidate
// disappears entirely. Pass 1.0 when there is no evidence.
func Composite(obtainability, uptime, preemption, price, priceExponent, evidenceFactor float64) (float64, bool) {
if obtainability < dropBelow {
return 0, true
}
return obtainability * obtainability * uptime * preemption *
math.Pow(price, priceExponent) * evidenceFactor, false
}

I had been refused in us-east1-c all day, and none of it was written down anywhere the advisor could read. The one-shot analyze command I ran passes nil as its evidence source. With nothing to go on, analyze.go gave each candidate a factor of 1.0 and the refusals had nowhere to go. In Composite, evidenceFactor can only scale a score. Taking a refused zone off a rung is a separate check that comes up below.

sequenceDiagram
    participant Advisor
    participant Autoscaler
    participant Fine-tune Job

    Advisor->>Fine-tune Job: rank us-east1-c at 0.90
    Fine-tune Job->>Autoscaler: request an L4 in us-east1-c
    Autoscaler-->>Fine-tune Job: refused, no capacity
    Note over Advisor: score still reads 0.90
    Fine-tune Job->>Autoscaler: retry, same zone
    Autoscaler-->>Fine-tune Job: refused again
    Note over Advisor,Autoscaler: ~36 hours, 3 regions, same pattern
    Fine-tune Job->>Autoscaler: live attempt, us-central1-a, g2-standard-8
    Autoscaler-->>Fine-tune Job: granted

The record of all this is in cwest/gke-spot-playbook, the series’ companion GitHub repo. demo/act3/runbook.md (“spot survives, on the ladder”) has the Kueue admission, the Spot-then-flex-start climb, and the stockout itself, region by region. demo/act7/runbook.md (“obtainability under GPU scarcity”) goes through the stockout-and-failover path later on A100s, with the probe in place. The trainer and its manifests are in workloads/03-finetune, with the checkpoint logic in checkpoints.py and the training loop in train.py. The score that stayed at 0.90 came out of the same dropBelow and evidence logic from Part 3, in advisor/internal/score/score.go.

What Was Missing: A Ledger and a Probe

The 0.90 was the advisor’s best guess at the moment it ran, and by the time I read it that moment had passed. I needed something that remembered which zones had just turned me down and quit sending me back to them.

Later parts of the series build that in two pieces. One is a ledger. When the autoscaler refuses a zone, the refusal gets written into the score, and the selector sees it the next time it ranks. The other is a probe that makes a cheap live attempt on a rung ahead of a scarce job.

The ledger’s math sits in the repo too, in evidence.go. Factor is what would have replaced that 1.0:

advisor/internal/evidence/evidence.go
// Factor returns the multiplier for k: 1.0 when there is no usable evidence,
// p.Floor at the instant of a failure, recovering exponentially toward 1.0.
func (l *Ledger) Factor(k Key, now time.Time, p Params) float64 {
o, ok := l.Latest[l.id(k)]
if !ok {
return 1.0
}
age := now.Sub(o.At)
if age < 0 {
age = 0
}
if p.MaxAge > 0 && age >= p.MaxAge {
return 1.0
}
if p.HalfLife <= 0 {
return p.Floor
}
decayed := math.Pow(0.5, age.Seconds()/p.HalfLife.Seconds())
return p.Floor + (1-p.Floor)*(1-decayed)
}

The ledger holds one entry per machine type per zone. Each refusal drops it to the floor, and the half-life is how long it takes to win back half the distance to 1.0.

My advisor.yaml sets them:

advisor.yaml
evidence:
halfLifeMinutes: 30
floor: 0.05
dryBelow: 0.5
maxAgeHours: 6

With those values a refusal in us-east1-c would have cut that rung’s score to a twentieth (the 0.05 floor). The factor stays under 0.5 for about 28 minutes, and for that half hour the zone counts as dry and stays off the rung. After six hours the refusal is forgotten.

The refusal times are made up. The fall to 0.05 and the climb back on a 30-minute half-life are what I measured on a live cluster.

Neither idea is new. The ledger is negative caching. DNS resolvers have done it to the spec in RFC 2308 since 1998. When one hears a name doesn’t exist, it holds on to that no for a set time. The probe is a health check, like a Kubernetes readiness probe.

I had neither. For thirty-six hours I was the ledger and the probe, re-scanning scores by hand and hauling the cluster to another region, until one live attempt turned up the node.

Part 6 automates the ledger so I’m not hand-editing the live ComputeClass (the GKE object that holds the ladder’s rungs) each time a zone goes dry, and getting it running turned up something worse than the stockout: automation that looks like it’s working.