drain: the status op names the hosts the move waits on (#1062)
The drain socket's status answer gains waiting: the step (quiesce or settle), since when, whether it gave up, the counts, and the hosts behind each (at most three per count, the counts whole), as the log and the refusal name them. group-maintenance status prints it as drain.waiting, null when the move is not waiting. It clears when the box settles, at a new step and on drain --cancel. Existing fields keep their meaning.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+138
−4
internal/host/group_maintenance_drain.go+1 −0internal/host/group_maintenance_verbs.go+3 −0internal/host/hadrain.go+23 −3internal/host/hadrain_quiet_linux_test.go+55 −0internal/host/hadrain_quiet_test.go+56 −1
diff --git a/internal/host/group_maintenance_drain.go b/internal/host/group_maintenance_drain.go| index 8f8e15d1c..24f3f533f 100644 |
| --- a/internal/host/group_maintenance_drain.go |
| +++ b/internal/host/group_maintenance_drain.go |
| @@ -213,5 +213,6 @@ func (cmd *groupMaintenanceCommand) handoverStatus(ctx context.Context) (mainten |
| if s.Step != "" { |
| ds.Step = &s.Step |
| } |
| + ds.Waiting = s.Waiting |
| return fs, ds |
| } |
diff --git a/internal/host/group_maintenance_verbs.go b/internal/host/group_maintenance_verbs.go| index 173785bd0..7497409f6 100644 |
| --- a/internal/host/group_maintenance_verbs.go |
| +++ b/internal/host/group_maintenance_verbs.go |
| @@ -284,6 +284,9 @@ type maintenanceDrainStatus struct { |
| Step *string `json:"step"` |
| Result *string `json:"result"` |
| Error *string `json:"error"` |
| + // Waiting is what the agent's move step waits on to settle, or gave |
| + // up waiting on, by host (#1062); null when it is not waiting. |
| + Waiting *drainWaiting `json:"waiting"` |
| } |
| type maintenanceProcessStatus struct { |
| State string `json:"state"` |
diff --git a/internal/host/hadrain.go b/internal/host/hadrain.go| index f19cf0bd8..e6688c3a3 100644 |
| --- a/internal/host/hadrain.go |
| +++ b/internal/host/hadrain.go |
| @@ -211,6 +211,9 @@ type drainDriver struct { |
| withdrawn bool |
| // cancelling is set while cancel joins and clears: no step starts. |
| cancelling bool |
| + // waiting is what quiesce or settle waits on, while it waits and after |
| + // it gave up; nil once the box settled (#1062). |
| + waiting *drainWaiting |
| } |
| type drainRun struct { |
| @@ -302,7 +305,7 @@ func (d *drainDriver) start(txn, step string, peers []string) (<-chan struct{}, |
| r := &drainRun{step: step, peers: append([]string{}, peers...), cancel: cancel, done: make(chan struct{})} |
| rep := &drainReport{V: 1, Transaction: txn, Incarnation: agentIncarnation, Step: step, Result: "running", StartedAt: now, |
| Reasons: []string{}, Acked: []string{}, Moved: []drainMoved{}, Released: []drainLeft{}, Unclean: []string{}, Left: []drainLeft{}} |
| - d.run, d.report = r, rep |
| + d.run, d.report, d.waiting = r, rep, nil |
| go func() { |
| defer close(r.done) |
| defer cancel() |
| @@ -394,7 +397,7 @@ func (d *drainDriver) cancel(ctx context.Context, txn string) error { |
| d.edge.EndConnectionDrain() |
| d.marks.clear() |
| d.mu.Lock() |
| - d.txn, d.run, d.report, d.withdrawn, d.deadline = "", nil, nil, false, time.Time{} |
| + d.txn, d.run, d.report, d.withdrawn, d.deadline, d.waiting = "", nil, nil, false, time.Time{}, nil |
| d.mu.Unlock() |
| return nil |
| } |
| @@ -407,6 +410,9 @@ type drainStatus struct { |
| Step string `json:"step"` |
| Result string `json:"result"` |
| Reasons int `json:"reasons"` |
| + // Waiting is what the move's quiesce or settle step waits on, or gave |
| + // up waiting on (#1062); absent when it is not waiting. |
| + Waiting *drainWaiting `json:"waiting,omitempty"` |
| } |
| // drainWaiting is what quiesce or settle is waiting on (#1062): the |
| @@ -463,10 +469,21 @@ func (w *drainWaiting) describe() string { |
| return strings.Join(parts, "; ") |
| } |
| +func (w *drainWaiting) copy() *drainWaiting { |
| + if w == nil { |
| + return nil |
| + } |
| + c := *w |
| + c.Held = append([]string{}, w.Held...) |
| + c.Hosts = activator.DrainQuietHosts{Waking: append([]string{}, w.Hosts.Waking...), Stopping: append([]string{}, w.Hosts.Stopping...), |
| + Recovering: append([]string{}, w.Hosts.Recovering...), Owed: append([]string{}, w.Hosts.Owed...)} |
| + return &c |
| +} |
| + |
| func (d *drainDriver) status() drainStatus { |
| d.mu.Lock() |
| defer d.mu.Unlock() |
| - s := drainStatus{Incarnation: agentIncarnation, Transaction: d.txn, Result: "none"} |
| + s := drainStatus{Incarnation: agentIncarnation, Transaction: d.txn, Result: "none", Waiting: d.waiting.copy()} |
| if d.report != nil { |
| s.Step, s.Result, s.Reasons = d.report.Step, d.report.Result, len(d.report.Reasons) |
| } |
| @@ -793,6 +810,7 @@ func (d *drainDriver) waitQuiet(ctx context.Context, step string, held bool) err |
| } |
| q, names := d.act.DrainQuietNamed() |
| if len(h) == 0 && q.Zero() { |
| + d.note(func() { d.waiting = nil }) |
| return nil |
| } |
| w := &drainWaiting{Step: step, Since: start, Quiet: q, Held: firstNames(h), HeldCount: len(h), |
| @@ -802,12 +820,14 @@ func (d *drainDriver) waitQuiet(ctx context.Context, step string, held bool) err |
| waited := now.Sub(start) |
| if waited >= drainQuietBound { |
| w.GaveUp = true |
| + d.note(func() { d.waiting = w }) |
| // draft for copy review (D1062-2) |
| err := fmt.Errorf("%w after %s: %s", errDrainQuietBound, drainQuietBound, w.describe()) |
| // draft for copy review (D1062-4) |
| d.logf("agent: drain %s: %v", step, err) |
| return err |
| } |
| + d.note(func() { d.waiting = w }) |
| if now.Sub(logged) >= drainQuietLogEvery { |
| logged = now |
| // draft for copy review (D1062-1) |
diff --git a/internal/host/hadrain_quiet_linux_test.go b/internal/host/hadrain_quiet_linux_test.go| new file mode 100644 |
| index 000000000..ef76a27a7 |
| --- /dev/null |
| +++ b/internal/host/hadrain_quiet_linux_test.go |
| @@ -0,0 +1,55 @@ |
| +package host |
| + |
| +import ( |
| + "encoding/json" |
| + "errors" |
| + "slices" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/activator" |
| +) |
| + |
| +// The verb's side of #1062: a box whose recovery never clears fails the |
| +// move at the bound, so the drain verb returns at once with the failed |
| +// report naming the host; group-maintenance status names it too, under |
| +// drain.waiting; and drain --cancel ends the drain. |
| +func TestDrainVerbQuiesceGivesUpAndStatusNamesTheHosts(t *testing.T) { |
| + act, ed, marks := healthyDrain() |
| + d, clk, _ := quietDriver(act, ed, marks) |
| + act.quiet = func() activator.DrainQuiet { |
| + clk.poll() |
| + return activator.DrainQuiet{Recovering: 1} |
| + } |
| + act.named = func() activator.DrainQuietHosts { |
| + return activator.DrainQuietHosts{Recovering: []string{"lost.example.com"}} |
| + } |
| + cmd, l := verbDrain(t, d) |
| + d.fence = l.read |
| + rep, err := runDrainVerb(t, cmd, "drain", "t1") |
| + if !errors.Is(err, errDrainFailed) || rep == nil || rep.Result != "failed" || !strings.Contains(strings.Join(rep.Reasons, ";"), "recovering 1 (lost.example.com)") { |
| + t.Fatalf("drain: %+v %v", rep, err) |
| + } |
| + out, err := runMaintenanceVerb(cmd, "status", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var st groupMaintenanceStatus |
| + must(t, json.Unmarshal(out, &st)) |
| + w := st.Drain.Waiting |
| + if st.Drain.State != "known" || *st.Drain.Result != "failed" || w == nil || w.Step != "quiesce" || !w.GaveUp || w.Quiet.Recovering != 1 || !slices.Equal(w.Hosts.Recovering, []string{"lost.example.com"}) { |
| + t.Fatalf("status: %s", out) |
| + } |
| + if _, err := runDrainVerb(t, cmd, "drain-cancel", "t1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + out, err = runMaintenanceVerb(cmd, "status", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + st = groupMaintenanceStatus{} |
| + must(t, json.Unmarshal(out, &st)) |
| + if st.Drain.Waiting != nil || l.read().Present { |
| + t.Fatalf("status after cancel: %s", out) |
| + } |
| +} |
diff --git a/internal/host/hadrain_quiet_test.go b/internal/host/hadrain_quiet_test.go| index f5da5ca8e..b2c0e9849 100644 |
| --- a/internal/host/hadrain_quiet_test.go |
| +++ b/internal/host/hadrain_quiet_test.go |
| @@ -111,6 +111,9 @@ func TestDrainQuiesceGivesUpNamingTheHosts(t *testing.T) { |
| t.Fatalf("log lines %q", lines) |
| } |
| st := d.status() |
| + if w := st.Waiting; w == nil || w.Step != "quiesce" || !w.GaveUp || w.Quiet.Recovering != 1 || !slices.Equal(w.Hosts.Recovering, []string{"lost.example.com"}) { |
| + t.Fatalf("status %+v waiting %+v", st, st.Waiting) |
| + } |
| if st.Result != "failed" || st.Step != "move" || st.Reasons != 1 { |
| t.Fatalf("status %+v", st) |
| } |
| @@ -119,11 +122,60 @@ func TestDrainQuiesceGivesUpNamingTheHosts(t *testing.T) { |
| if err := d.cancel(ctx, "t1"); err != nil { |
| t.Fatal(err) |
| } |
| - if st := d.status(); st.Result != "none" { |
| + if st := d.status(); st.Waiting != nil || st.Result != "none" { |
| t.Fatalf("status after cancel %+v", st) |
| } |
| } |
| +// While it waits, the status op names what quiesce waits on; a box that |
| +// settles inside the bound moves as before, logging nothing, and the |
| +// status op then names nothing. |
| +func TestDrainQuiesceStatusWhileWaiting(t *testing.T) { |
| + act, ed, marks := healthyDrain() |
| + d, clk, logs := quietDriver(act, ed, marks) |
| + release := make(chan struct{}) |
| + seen := make(chan drainStatus, 1) |
| + polls := 0 |
| + act.quiet = func() activator.DrainQuiet { |
| + select { |
| + case <-release: |
| + return activator.DrainQuiet{} |
| + default: |
| + } |
| + clk.poll() |
| + polls++ |
| + if polls == 2 { |
| + // The first poll's answer is the status now (a poll later, so |
| + // the driver has recorded it). |
| + seen <- d.status() |
| + close(release) |
| + } |
| + return activator.DrainQuiet{Waking: 1, Owed: 2} |
| + } |
| + act.named = func() activator.DrainQuietHosts { |
| + return activator.DrainQuietHosts{Waking: []string{"wake.example.com"}, Owed: []string{"a.example.com", "b.example.com"}} |
| + } |
| + rep := runDrain(t, d, "t1") |
| + if rep.Result != "ok" || len(rep.Moved) != 2 { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + st := <-seen |
| + w := st.Waiting |
| + if w == nil || w.Step != "quiesce" || w.GaveUp || w.Quiet.Waking != 1 || w.Quiet.Owed != 2 || |
| + !slices.Equal(w.Hosts.Waking, []string{"wake.example.com"}) || !slices.Equal(w.Hosts.Owed, []string{"a.example.com", "b.example.com"}) { |
| + t.Fatalf("status while waiting %+v", w) |
| + } |
| + if got := w.describe(); got != "waking 1 (wake.example.com); owed 2 (a.example.com, b.example.com)" { |
| + t.Fatalf("describe %q", got) |
| + } |
| + if lines := logs.all(); len(lines) != 0 { |
| + t.Fatalf("a short wait logged %q", lines) |
| + } |
| + if st := d.status(); st.Waiting != nil { |
| + t.Fatalf("status after the box settled %+v", st.Waiting) |
| + } |
| +} |
| + |
| // settle, after the moves, has the same bound: an entry that stays held |
| // fails the move at drainQuietBound, naming it, long before the drain |
| // deadline; at most drainNamesMax hosts are named per count. |
| @@ -161,6 +213,9 @@ func TestDrainSettleGivesUpNamingTheHosts(t *testing.T) { |
| if _, drained, _ := marks.counts(); rep.Result != "failed" || drained != 0 || !slices.Equal(rep.Reasons, []string{want}) { |
| t.Fatalf("report %+v", rep) |
| } |
| + if w := d.status().Waiting; w == nil || w.Step != "settle" || !w.GaveUp || w.HeldCount != 4 || len(w.Held) != drainNamesMax { |
| + t.Fatalf("status %+v", w) |
| + } |
| if len(logs.all()) != 5 { |
| t.Fatalf("log lines %q", logs.all()) |
| } |