hostedops: retry an incomplete VM scan with backoff in the estimate
Every block runs the estimate, and one throttled ListVMs stopped the run. scanVMs retries with doubling waits (5 s to 1 min), each a FINDING, within a bound: 3 min per image for the estimate, --max for vms scan. A scan still incomplete stops, as before. Mutations S1-S4 recorded.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+131
−16
docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md+19 −0internal/hostedops/drill_test.go+66 −0internal/hostedops/estimate.go+9 −3internal/hostedops/vms.go+37 −13
diff --git a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md| index 50eb2c47..631fa161 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| @@ -3405,3 +3405,22 @@ Red: `go test ./internal/hostedops -run TestRebootWaitsForTheHostToGo -count=1`: |
| Mutation checks, each restored and green again: |
| - **R1** `away` starts true. `-run TestRebootWaitsForTheHostToGo`: FAIL as the red run (`up all along: reboot = 0, want 99`). |
| - **R2** only a failed read counts as away. Same test: FAIL `drill_test.go:100: old switch, then new: reboot = 99, want 0`. |
| + |
| +### Fix 6: one throttle no longer ends the drill (Fable 34, section 0) |
| + |
| +The estimate every block runs turned one failed `ListVMs` into a stop. New `scanVMs` lists an image's |
| +VMs and retries an incomplete scan with growing waits (5 s, doubling, at most 1 min), each retry a |
| +FINDING, while the next attempt can start within its bound; a scan still incomplete returns its |
| +error and the caller stops, as before. The estimate uses it with `EstimateScanMax` (3 min) per |
| +image; `vms scan --max` uses it in place of its own fixed 15 s loop (`TestVMsScanMax` unchanged and |
| +green). The AWS clients (`opsaws/clients.go`) still make one attempt per call; the retry is here. |
| + |
| +Red (after adding the `EstimateScanMax` constant, so the test compiles): |
| +`go test ./internal/hostedops -run TestEstimateRetriesTheScan -count=1`: FAIL `drill_test.go:143: ops block 5.1 -- true = 99, want 0`. |
| +Green: the whole package, ok (`TestEstimateIncompleteScanStops` still stops on a persistent throttle). |
| + |
| +Mutation checks, each restored and green again: |
| +- **S1** the estimate's bound set to 0 (one attempt). `-run TestEstimateRetriesTheScan`: FAIL `drill_test.go:143: ops block 5.1 -- true = 99, want 0`. |
| +- **S2** the wait no longer doubles. Same test: FAIL `drill_test.go:153: the waits did not grow: [10:00:00 10:00:05 10:00:10 10:00:15]`. |
| +- **S3** the bound ten times longer. Same test: FAIL `drill_test.go:173: a persistent failure: 33 scans over 29m15s, want several within 3m0s`. |
| +- **S4** the retry FINDING dropped. Same test: FAIL `drill_test.go:163: 0 retry findings, want 3`. |
diff --git a/internal/hostedops/drill_test.go b/internal/hostedops/drill_test.go| index 7dd59095..64204c51 100644 |
| --- a/internal/hostedops/drill_test.go |
| +++ b/internal/hostedops/drill_test.go |
| @@ -2,7 +2,9 @@ package hostedops |
| import ( |
| "errors" |
| + "strings" |
| "testing" |
| + "time" |
| "amadan.net/oficina/equipo/internal/hostedwire" |
| ) |
| @@ -107,3 +109,67 @@ func TestRebootWaitsForTheHostToGo(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// scanTimes are the fake clock's times of each ListVMs call. |
| +func scanTimes(t *testing.T, r *rig) []time.Time { |
| + t.Helper() |
| + var out []time.Time |
| + for _, c := range r.cloud.log() { |
| + if _, at, ok := strings.Cut(c, " at="); ok && strings.HasPrefix(c, "ListVMs ") { |
| + ts, err := time.Parse(time.TimeOnly, at) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + out = append(out, ts) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// TestEstimateRetriesTheScan (Fable 34, section 0): one throttled |
| +// ListVMs no longer ends the drill. The estimate every block runs retries |
| +// an incomplete scan with growing waits, each retry a FINDING, within |
| +// EstimateScanMax; a scan still incomplete at the bound stops, as before. |
| +func TestEstimateRetriesTheScan(t *testing.T) { |
| + r := newRig(t, "REC microvm-image "+image1) |
| + r.cloud.listVMs = func(n int, _ string) ([]VM, error) { |
| + if n <= 3 { |
| + return nil, errors.New("ThrottlingException: rate exceeded") |
| + } |
| + return []VM{{ID: "microvm-a", Image: image1, State: "RUNNING"}}, nil |
| + } |
| + ran := false |
| + r.deps.Exec = func(_ ctxT, _ []string, _ []string, _, _ wT) int { ran = true; return 0 } |
| + r.want(0, "block", "5.1", "--", "true") |
| + if !ran || !r.has("VM", "microvm-a") { |
| + t.Fatalf("a block did not run past three throttles (ran %t):\n%s", ran, journalText(r)) |
| + } |
| + at := scanTimes(t, r) |
| + if len(at) != 4 { |
| + t.Fatalf("%d scans, want 4", len(at)) |
| + } |
| + for i := 2; i < len(at); i++ { |
| + if at[i].Sub(at[i-1]) <= at[i-1].Sub(at[i-2]) { |
| + t.Fatalf("the waits did not grow: %v", at) |
| + } |
| + } |
| + findings := 0 |
| + for _, e := range r.ledger().Entries { |
| + if e.Verb == "FINDING" && strings.Contains(e.Text, "ThrottlingException") { |
| + findings++ |
| + } |
| + } |
| + if findings != 3 { |
| + t.Fatalf("%d retry findings, want 3:\n%s", findings, journalText(r)) |
| + } |
| + |
| + // Throttled throughout: a stop, within the bound. |
| + r = newRig(t, "REC microvm-image "+image1) |
| + r.cloud.listVMs = func(int, string) ([]VM, error) { return nil, errors.New("ThrottlingException: rate exceeded") } |
| + start := r.clock.Now() |
| + r.want(ExitStop, "estimate") |
| + at = scanTimes(t, r) |
| + if len(at) < 3 || r.clock.Now().Sub(start) > EstimateScanMax { |
| + t.Fatalf("a persistent failure: %d scans over %s, want several within %s", len(at), r.clock.Now().Sub(start), EstimateScanMax) |
| + } |
| +} |
diff --git a/internal/hostedops/estimate.go b/internal/hostedops/estimate.go| index 89360e83..036c6c90 100644 |
| --- a/internal/hostedops/estimate.go |
| +++ b/internal/hostedops/estimate.go |
| @@ -130,9 +130,15 @@ func images(l *Ledger) []imageRec { |
| return out |
| } |
| +// EstimateScanMax bounds the retries of one image's incomplete VM scan in |
| +// an estimate (Fable 34, section 0): every block runs the estimate, so one |
| +// throttled call must not stop the run. |
| +const EstimateScanMax = 3 * time.Minute |
| + |
| // estimate scans every recorded image's VMs (recording each), then |
| -// computes. An incomplete scan, or an image whose ARN is not known, is a |
| -// hard stop: the estimate depends on it. |
| +// computes. A scan is retried within EstimateScanMax; one still |
| +// incomplete, or an image whose ARN is not known, is a hard stop: the |
| +// estimate depends on it. |
| func (o *Ops) estimate() (Estimate, error) { |
| c, err := o.open() |
| if err != nil { |
| @@ -154,7 +160,7 @@ func (o *Ops) estimate() (Estimate, error) { |
| if l.Deleted["microvm-image "+r.Value] { |
| continue |
| } |
| - vms, err := c.ListVMs(o.ctx, r.Value) |
| + vms, err := o.scanVMs(c, r.Value, EstimateScanMax, "estimate") |
| if err != nil { |
| return Estimate{}, stopf("estimate: the VM scan of %s is incomplete: %v", r.Value, err) |
| } |
diff --git a/internal/hostedops/vms.go b/internal/hostedops/vms.go| index 92bb779c..6f5103ff 100644 |
| --- a/internal/hostedops/vms.go |
| +++ b/internal/hostedops/vms.go |
| @@ -4,6 +4,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "flag" |
| + "fmt" |
| "slices" |
| "strings" |
| "time" |
| @@ -66,19 +67,9 @@ func cmdVMsScan(o *Ops, args []string) error { |
| if err := o.runImage(l, *image); err != nil { |
| return err |
| } |
| - end := o.Clock.Now().Add(*maxD) |
| - vms, err := o.cloud.ListVMs(o.ctx, *image) |
| - for err != nil { |
| - if !o.Clock.Now().Add(15 * time.Second).Before(end) { |
| - return stopf("the VM scan is incomplete: %v", err) |
| - } |
| - if ferr := o.finding("vms scan: incomplete (%v); scanning again", err); ferr != nil { |
| - return ferr |
| - } |
| - if serr := o.Clock.Sleep(o.ctx, 15*time.Second); serr != nil { |
| - return stopf("the VM scan is incomplete: interrupted") |
| - } |
| - vms, err = o.cloud.ListVMs(o.ctx, *image) |
| + vms, err := o.scanVMs(o.cloud, *image, *maxD, "vms scan") |
| + if err != nil { |
| + return stopf("the VM scan is incomplete: %v", err) |
| } |
| if err := o.recordVMs(l, vms, "scan"); err != nil { |
| return err |
| @@ -88,6 +79,39 @@ func cmdVMsScan(o *Ops, args []string) error { |
| return o.jw("SCAN", "image=%s listed=%d live=%d", *image, len(vms), countLive(vms)) |
| } |
| +// The waits before an incomplete scan is tried again: the first, doubling |
| +// up to the most. |
| +const ( |
| + scanWait = 5 * time.Second |
| + scanWaitMax = time.Minute |
| +) |
| + |
| +// scanVMs lists image's VMs through c, retrying an incomplete scan (a |
| +// throttle, a failed page) with growing waits, each retry a FINDING, while |
| +// the next attempt can start within max of the first; max 0 is one |
| +// attempt. A scan still incomplete returns its last error: the callers |
| +// stop on it. |
| +func (o *Ops) scanVMs(c Cloud, image string, max time.Duration, what string) ([]VM, error) { |
| + end := o.Clock.Now().Add(max) |
| + wait := scanWait |
| + for { |
| + vms, err := c.ListVMs(o.ctx, image) |
| + if err == nil { |
| + return vms, nil |
| + } |
| + if !o.Clock.Now().Add(wait).Before(end) { |
| + return nil, err |
| + } |
| + if ferr := o.finding("%s: the scan of %s is incomplete (%v); scanning again in %s", what, image, err, wait); ferr != nil { |
| + return nil, ferr |
| + } |
| + if serr := o.Clock.Sleep(o.ctx, wait); serr != nil { |
| + return nil, fmt.Errorf("interrupted after %v", err) |
| + } |
| + wait = min(2*wait, scanWaitMax) |
| + } |
| +} |
| + |
| func countLive(vms []VM) int { |
| n := 0 |
| for _, v := range vms { |