drain: quiesce and settle name what they wait on, then give up (#1062)
The move step's quiesce and settle polled DrainQuietNow with no log and no bound but the drain deadline. They now log once a minute which counts are not zero and the hosts behind each, and at drainQuietBound (5 minutes) give up: the move fails with a reason naming the counts that did not clear and their hosts, so the verb returns the failed report at once and drain --cancel ends it. The activator's DrainQuietNamed is DrainQuietNow with the hosts named (waking and stopping entries, recovering haLoss hosts, and each owed marker, release, retained wake and release command). DrainQuietNow keeps its counts and Zero.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+382
−37
internal/activator/hadrainmove.go+55 −9internal/activator/hadrainquiet_test.go+26 −0internal/host/hadrain.go+126 −28internal/host/hadrain_quiet_test.go+167 −0internal/host/hadrain_test.go+8 −0
diff --git a/internal/activator/hadrainmove.go b/internal/activator/hadrainmove.go| index 35f679080..54084a6c9 100644 |
| --- a/internal/activator/hadrainmove.go |
| +++ b/internal/activator/hadrainmove.go |
| @@ -9,6 +9,7 @@ package activator |
| import ( |
| "context" |
| "errors" |
| + "path" |
| "sort" |
| "time" |
| @@ -47,24 +48,69 @@ func (q DrainQuiet) Zero() bool { return q == DrainQuiet{} } |
| // DrainQuietNow counts what is still settling, memory only. |
| func (a *Service) DrainQuietNow() DrainQuiet { |
| - owed := len(lease.PendingObligations()) |
| + q, _ := a.DrainQuietNamed() |
| + return q |
| +} |
| + |
| +// DrainQuietHosts names the hosts behind each DrainQuiet count (#1062), |
| +// sorted, one name per count: a host can appear more than once in Owed |
| +// (an owed marker and an owed release, say). |
| +type DrainQuietHosts struct { |
| + Waking []string `json:"waking"` |
| + Stopping []string `json:"stopping"` |
| + Recovering []string `json:"recovering"` |
| + // Owed names each release obligation by its host: an owed marker by |
| + // its lock's host (the key itself when it is not a lock), then owed |
| + // legacy releases, retained failed wakes and open release commands. |
| + Owed []string `json:"owed"` |
| +} |
| + |
| +// DrainQuietNamed is DrainQuietNow with the hosts behind each count, read |
| +// in the same critical section, memory only. |
| +func (a *Service) DrainQuietNamed() (DrainQuiet, DrainQuietHosts) { |
| + obligations := lease.PendingObligations() |
| + n := DrainQuietHosts{Waking: []string{}, Stopping: []string{}, Recovering: []string{}, Owed: []string{}} |
| + for _, o := range obligations { |
| + n.Owed = append(n.Owed, obligationHost(o.Key)) |
| + } |
| a.mu.Lock() |
| - defer a.mu.Unlock() |
| - q := DrainQuiet{Owed: owed + len(a.unprovenWakes) + len(a.owedReleases) + len(a.releaseOps)} |
| - for _, e := range a.entries { |
| + for host := range a.unprovenWakes { |
| + n.Owed = append(n.Owed, host) |
| + } |
| + for o := range a.owedReleases { |
| + n.Owed = append(n.Owed, o.host) |
| + } |
| + for host := range a.releaseOps { |
| + n.Owed = append(n.Owed, host) |
| + } |
| + for host, e := range a.entries { |
| switch e.state { |
| case actWaking: |
| - q.Waking++ |
| + n.Waking = append(n.Waking, host) |
| case actStopping: |
| - q.Stopping++ |
| + n.Stopping = append(n.Stopping, host) |
| } |
| } |
| - for _, l := range a.haLoss.hosts { |
| + for host, l := range a.haLoss.hosts { |
| if l.running || l.recovery != nil { |
| - q.Recovering++ |
| + n.Recovering = append(n.Recovering, host) |
| } |
| } |
| - return q |
| + a.mu.Unlock() |
| + for _, s := range [][]string{n.Waking, n.Stopping, n.Recovering, n.Owed} { |
| + sort.Strings(s) |
| + } |
| + q := DrainQuiet{Waking: len(n.Waking), Stopping: len(n.Stopping), Recovering: len(n.Recovering), Owed: len(n.Owed)} |
| + return q, n |
| +} |
| + |
| +// obligationHost is the host of an owed marker's lock key |
| +// (<prefix>/<host>/lock.json, haOwedKey), or the key itself. |
| +func obligationHost(key string) string { |
| + if path.Base(key) == "lock.json" && path.Dir(key) != "." { |
| + return path.Base(path.Dir(key)) |
| + } |
| + return key |
| } |
| // DrainHost is one awake host the drain must release (§25.4 step 3). |
diff --git a/internal/activator/hadrainquiet_test.go b/internal/activator/hadrainquiet_test.go| index 8e4b53068..9935e6ccd 100644 |
| --- a/internal/activator/hadrainquiet_test.go |
| +++ b/internal/activator/hadrainquiet_test.go |
| @@ -140,3 +140,29 @@ func TestDrainQuietRecoveryFailedWakeClears(t *testing.T) { |
| } |
| assertRecoveryEnded(t, s) |
| } |
| + |
| +// DrainQuietNamed names the host behind each count, in step with the |
| +// counts (#1062): a recovery under way, then an owed marker. |
| +func TestDrainQuietNamedNamesTheHosts(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + s.failMarkerPuts(2) // the release, then the recovery's settlement |
| + finishLostTestEntry(t, s, e, r) |
| + clk := &fakeClock{t: time.Now()} |
| + s.a.now = clk.now |
| + var during DrainQuietHosts |
| + var duringQ DrainQuiet |
| + s.a.wakeEnded = func(string) { duringQ, during = s.a.DrainQuietNamed() } |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + host := s.rt.Host |
| + if duringQ.Recovering != 1 || len(during.Recovering) != 1 || during.Recovering[0] != host { |
| + t.Fatalf("during the recovery: %+v %+v", duringQ, during) |
| + } |
| + q, n := s.a.DrainQuietNamed() |
| + if q != s.a.DrainQuietNow() { |
| + t.Fatalf("counts differ: %+v %+v", q, s.a.DrainQuietNow()) |
| + } |
| + if q.Owed != 1 || len(n.Owed) != 1 || n.Owed[0] != host || len(n.Recovering) != 0 || len(n.Waking) != 0 || len(n.Stopping) != 0 { |
| + t.Fatalf("after the failed recovery: %+v %+v", q, n) |
| + } |
| +} |
diff --git a/internal/host/hadrain.go b/internal/host/hadrain.go| index 701404fd0..f19cf0bd8 100644 |
| --- a/internal/host/hadrain.go |
| +++ b/internal/host/hadrain.go |
| @@ -30,7 +30,9 @@ import ( |
| "encoding/hex" |
| "errors" |
| "fmt" |
| + "log" |
| "sort" |
| + "strings" |
| "sync" |
| "time" |
| @@ -42,14 +44,19 @@ import ( |
| // The drain's bounds (§25.4). |
| var ( |
| - drainAckEvery = 5 * time.Second |
| - drainAckBound = 2 * time.Minute |
| - drainInflightBound = 10 * time.Second |
| - drainConnBound = 60 * time.Second |
| - drainTakeBound = 40 * time.Second |
| - drainDeadlineBase = 15 * time.Minute |
| - drainDeadlinePer20 = time.Minute |
| - drainQuietPoll = 100 * time.Millisecond |
| + drainAckEvery = 5 * time.Second |
| + drainAckBound = 2 * time.Minute |
| + drainInflightBound = 10 * time.Second |
| + drainConnBound = 60 * time.Second |
| + drainTakeBound = 40 * time.Second |
| + drainDeadlineBase = 15 * time.Minute |
| + drainDeadlinePer20 = time.Minute |
| + drainQuietPoll = 100 * time.Millisecond |
| + // drainQuietBound is how long quiesce and settle wait for the box to |
| + // settle before the move gives up (#1062), well inside the verb's own |
| + // timeout, and drainQuietLogEvery how often they say what they wait on. |
| + drainQuietBound = 5 * time.Minute |
| + drainQuietLogEvery = time.Minute |
| drainMovesAtOnce = 4 |
| drainPublishTimeout = 2 * time.Minute |
| ) |
| @@ -76,6 +83,7 @@ func drainDeadline(awake int) time.Duration { |
| // and handover.go). |
| type drainActivator interface { |
| DrainQuietNow() activator.DrainQuiet |
| + DrainQuietNamed() (activator.DrainQuiet, activator.DrainQuietHosts) |
| DrainHosts() []activator.DrainHost |
| DrainHeld() []string |
| BeginMove(host string) error |
| @@ -187,6 +195,10 @@ type drainDriver struct { |
| marks drainMarks |
| fence func() fenceState |
| now func() time.Time |
| + // quietTick is the quiesce and settle poll (a ticker, and its stop), |
| + // and logf their log: seams for tests. |
| + quietTick func() (<-chan time.Time, func()) |
| + logf func(format string, args ...any) |
| mu sync.Mutex |
| // txn is the drain transaction this agent is in, "" for none. |
| @@ -220,7 +232,11 @@ var ( |
| ) |
| func newDrainDriver(act drainActivator, e drainEdge, marks drainMarks, fence func() fenceState) *drainDriver { |
| - return &drainDriver{act: act, edge: e, marks: marks, fence: fence, now: time.Now} |
| + return &drainDriver{act: act, edge: e, marks: marks, fence: fence, now: time.Now, logf: log.Printf, |
| + quietTick: func() (<-chan time.Time, func()) { |
| + t := time.NewTicker(drainQuietPoll) |
| + return t.C, t.Stop |
| + }} |
| } |
| // start runs step for txn, or joins the same step already running, and |
| @@ -393,6 +409,60 @@ type drainStatus struct { |
| Reasons int `json:"reasons"` |
| } |
| +// drainWaiting is what quiesce or settle is waiting on (#1062): the |
| +// counts, and the hosts behind each, at most drainNamesMax per count. |
| +type drainWaiting struct { |
| + // Step is "quiesce" (before the moves) or "settle" (after them). |
| + Step string `json:"step"` |
| + Since time.Time `json:"since"` |
| + // GaveUp is set once the wait passed drainQuietBound and the move |
| + // failed. |
| + GaveUp bool `json:"gave_up"` |
| + Quiet activator.DrainQuiet `json:"quiet"` |
| + Hosts activator.DrainQuietHosts `json:"hosts"` |
| + // Held is settle's entries still here, by host. |
| + Held []string `json:"held"` |
| + HeldCount int `json:"held_count"` |
| +} |
| + |
| +// drainNamesMax is how many hosts the log, the refusal and the status |
| +// name per count; the counts themselves are whole. |
| +const drainNamesMax = 3 |
| + |
| +func firstNames(names []string) []string { |
| + if len(names) > drainNamesMax { |
| + names = names[:drainNamesMax] |
| + } |
| + return append([]string{}, names...) |
| +} |
| + |
| +// describe is the waiting counts that are not zero, each with its hosts: |
| +// "recovering 1 (shop.example.com); owed 2 (a.example.com, b.example.com)". |
| +func (w *drainWaiting) describe() string { |
| + var parts []string |
| + add := func(name string, n int, hosts []string) { |
| + if n == 0 { |
| + return |
| + } |
| + named := strings.Join(hosts, ", ") |
| + if more := n - len(hosts); more > 0 && len(hosts) > 0 { |
| + // draft for copy review (D1062-3) |
| + named = fmt.Sprintf("%s and %d more", named, more) |
| + } |
| + if named == "" { |
| + parts = append(parts, fmt.Sprintf("%s %d", name, n)) |
| + return |
| + } |
| + parts = append(parts, fmt.Sprintf("%s %d (%s)", name, n, named)) |
| + } |
| + add("held", w.HeldCount, w.Held) |
| + add("waking", w.Quiet.Waking, w.Hosts.Waking) |
| + add("stopping", w.Quiet.Stopping, w.Hosts.Stopping) |
| + add("recovering", w.Quiet.Recovering, w.Hosts.Recovering) |
| + add("owed", w.Quiet.Owed, w.Hosts.Owed) |
| + return strings.Join(parts, "; ") |
| +} |
| + |
| func (d *drainDriver) status() drainStatus { |
| d.mu.Lock() |
| defer d.mu.Unlock() |
| @@ -693,35 +763,63 @@ func (d *drainDriver) releaseOne(ctx context.Context, h activator.DrainHost, rep |
| // quiesce waits, memory only, until nothing is settling: wakes restoring, |
| // acquires on their PUT, recoveries, pending stops and owed markers. |
| func (d *drainDriver) quiesce(ctx context.Context) error { |
| - t := time.NewTicker(drainQuietPoll) |
| - defer t.Stop() |
| - for { |
| - q := d.act.DrainQuietNow() |
| - if q.Zero() { |
| - return nil |
| - } |
| - select { |
| - case <-ctx.Done(): |
| - return fmt.Errorf("%w: %+v", ctx.Err(), q) |
| - case <-t.C: |
| - } |
| - } |
| + return d.waitQuiet(ctx, "quiesce", false) |
| } |
| // settle waits, memory only, until no entry is held here and nothing is |
| // settling. |
| func (d *drainDriver) settle(ctx context.Context) error { |
| - t := time.NewTicker(drainQuietPoll) |
| - defer t.Stop() |
| + return d.waitQuiet(ctx, "settle", true) |
| +} |
| + |
| +// errDrainQuietBound is a quiesce or settle that gave up at |
| +// drainQuietBound (#1062). |
| +var errDrainQuietBound = errors.New("gave up waiting") |
| + |
| +// waitQuiet is quiesce (held false) and settle (held true). While the box |
| +// is not quiet it logs, once a minute, what it waits on and for which |
| +// hosts, and keeps that for the status op; at drainQuietBound it gives up, |
| +// naming the counts that did not clear and their hosts, and the move |
| +// fails as any failed move does (#1062). |
| +func (d *drainDriver) waitQuiet(ctx context.Context, step string, held bool) error { |
| + tick, stop := d.quietTick() |
| + defer stop() |
| + start := d.now() |
| + logged := start |
| for { |
| - held, q := d.act.DrainHeld(), d.act.DrainQuietNow() |
| - if len(held) == 0 && q.Zero() { |
| + var h []string |
| + if held { |
| + h = d.act.DrainHeld() |
| + } |
| + q, names := d.act.DrainQuietNamed() |
| + if len(h) == 0 && q.Zero() { |
| return nil |
| } |
| + w := &drainWaiting{Step: step, Since: start, Quiet: q, Held: firstNames(h), HeldCount: len(h), |
| + Hosts: activator.DrainQuietHosts{Waking: firstNames(names.Waking), Stopping: firstNames(names.Stopping), |
| + Recovering: firstNames(names.Recovering), Owed: firstNames(names.Owed)}} |
| + now := d.now() |
| + waited := now.Sub(start) |
| + if waited >= drainQuietBound { |
| + w.GaveUp = true |
| + // 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 |
| + } |
| + if now.Sub(logged) >= drainQuietLogEvery { |
| + logged = now |
| + // draft for copy review (D1062-1) |
| + d.logf("agent: drain %s: waiting %s for the box to settle: %s", step, waited.Round(time.Second), w.describe()) |
| + } |
| select { |
| case <-ctx.Done(): |
| - return fmt.Errorf("%w: held %v, %+v", ctx.Err(), held, q) |
| - case <-t.C: |
| + if held { |
| + return fmt.Errorf("%w: held %v, %+v", ctx.Err(), h, q) |
| + } |
| + return fmt.Errorf("%w: %+v", ctx.Err(), q) |
| + case <-tick: |
| } |
| } |
| } |
diff --git a/internal/host/hadrain_quiet_test.go b/internal/host/hadrain_quiet_test.go| new file mode 100644 |
| index 000000000..f5da5ca8e |
| --- /dev/null |
| +++ b/internal/host/hadrain_quiet_test.go |
| @@ -0,0 +1,167 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "fmt" |
| + "slices" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/activator" |
| +) |
| + |
| +// quietClock is the drain's clock for #1062's witnesses: each poll of the |
| +// activator moves it on by step, so the waits are counted in polls, with |
| +// no real time and no goroutine to keep in step. |
| +type quietClock struct { |
| + mu sync.Mutex |
| + t time.Time |
| + step time.Duration |
| +} |
| + |
| +func (c *quietClock) now() time.Time { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + return c.t |
| +} |
| + |
| +func (c *quietClock) poll() { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + c.t = c.t.Add(c.step) |
| +} |
| + |
| +// drainLogLines collects the driver's log lines. |
| +type drainLogLines struct { |
| + mu sync.Mutex |
| + lines []string |
| +} |
| + |
| +func (l *drainLogLines) logf(format string, args ...any) { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + l.lines = append(l.lines, fmt.Sprintf(format, args...)) |
| +} |
| + |
| +func (l *drainLogLines) all() []string { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return append([]string(nil), l.lines...) |
| +} |
| + |
| +// quietDriver is a drain driver on the fakes whose poll never waits (a |
| +// closed tick), whose clock moves 20 s per poll, and whose log is kept. |
| +func quietDriver(act *fakeDrainAct, ed *fakeDrainEdge, marks *fakeMarks) (*drainDriver, *quietClock, *drainLogLines) { |
| + clk := &quietClock{t: time.Now(), step: 20 * time.Second} |
| + logs := &drainLogLines{} |
| + d := newDrainDriver(act, ed, marks, fenceOf("t1")) |
| + d.now = clk.now |
| + d.logf = logs.logf |
| + tick := make(chan time.Time) |
| + close(tick) |
| + d.quietTick = func() (<-chan time.Time, func()) { return tick, func() {} } |
| + return d, clk, logs |
| +} |
| + |
| +// A recovery that never clears (#1062's field case): quiesce says once a |
| +// minute what it waits on and for which host, gives up at |
| +// drainQuietBound naming the count and the host, and the move fails as |
| +// any failed move does: no move began, nothing published drained, the |
| +// status op names the host, and drain --cancel ends it at once. |
| +func TestDrainQuiesceGivesUpNamingTheHosts(t *testing.T) { |
| + if drainQuietBound != 5*time.Minute || drainQuietLogEvery != time.Minute { |
| + t.Fatalf("bounds %v %v, want 5m and 1m", drainQuietBound, drainQuietLogEvery) |
| + } |
| + act, ed, marks := healthyDrain() |
| + d, clk, logs := 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"}} |
| + } |
| + rep := runDrain(t, d, "t1") |
| + want := "the box did not settle: gave up waiting after 5m0s: recovering 1 (lost.example.com)" |
| + if rep.Result != "failed" || !slices.Equal(rep.Reasons, []string{want}) { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + act.mu.Lock() |
| + begun := len(act.begun) |
| + act.mu.Unlock() |
| + if _, drained, _ := marks.counts(); begun != 0 || drained != 0 || indexOf(act.rec.events(), "stop ") >= 0 { |
| + t.Fatal("a move began, or drained was published, on a box that never settled") |
| + } |
| + lines := logs.all() |
| + var waiting, gaveUp int |
| + for _, l := range lines { |
| + switch { |
| + case strings.HasPrefix(l, "agent: drain quiesce: waiting ") && strings.HasSuffix(l, "for the box to settle: recovering 1 (lost.example.com)"): |
| + waiting++ |
| + case l == "agent: drain quiesce: "+strings.TrimPrefix(want, "the box did not settle: "): |
| + gaveUp++ |
| + default: |
| + t.Errorf("unexpected log line %q", l) |
| + } |
| + } |
| + // One line a minute until the bound: at 1, 2, 3 and 4 minutes. |
| + if waiting != 4 || gaveUp != 1 { |
| + t.Fatalf("log lines %q", lines) |
| + } |
| + st := d.status() |
| + if st.Result != "failed" || st.Step != "move" || st.Reasons != 1 { |
| + t.Fatalf("status %+v", st) |
| + } |
| + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| + defer cancel() |
| + if err := d.cancel(ctx, "t1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if st := d.status(); st.Result != "none" { |
| + t.Fatalf("status after cancel %+v", st) |
| + } |
| +} |
| + |
| +// 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. |
| +func TestDrainSettleGivesUpNamingTheHosts(t *testing.T) { |
| + act, ed, marks := healthyDrain() |
| + d, clk, logs := quietDriver(act, ed, marks) |
| + var moved bool |
| + act.stop = func(ctx context.Context, host string) (activator.DrainRelease, error) { |
| + act.mu.Lock() |
| + act.awake[host] = false |
| + if host == "busy.example.com" { |
| + for _, h := range []string{"late1.example.com", "late2.example.com", "late3.example.com", "late4.example.com"} { |
| + act.awake[h] = true |
| + } |
| + } |
| + moved = true |
| + act.mu.Unlock() |
| + return activator.DrainRelease{HA: true, Marker: true, Clean: true}, nil |
| + } |
| + act.quiet = func() activator.DrainQuiet { |
| + act.mu.Lock() |
| + after := moved |
| + act.mu.Unlock() |
| + if !after { |
| + return activator.DrainQuiet{} |
| + } |
| + clk.poll() |
| + return activator.DrainQuiet{Stopping: 1} |
| + } |
| + act.named = func() activator.DrainQuietHosts { |
| + return activator.DrainQuietHosts{Stopping: []string{"late1.example.com"}} |
| + } |
| + rep := runDrain(t, d, "t1") |
| + want := "the box did not settle after the moves: gave up waiting after 5m0s: held 4 (late1.example.com, late2.example.com, late3.example.com and 1 more); stopping 1 (late1.example.com)" |
| + if _, drained, _ := marks.counts(); rep.Result != "failed" || drained != 0 || !slices.Equal(rep.Reasons, []string{want}) { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + if len(logs.all()) != 5 { |
| + t.Fatalf("log lines %q", logs.all()) |
| + } |
| +} |
diff --git a/internal/host/hadrain_test.go b/internal/host/hadrain_test.go| index 2a624eb52..df4dd9ae5 100644 |
| --- a/internal/host/hadrain_test.go |
| +++ b/internal/host/hadrain_test.go |
| @@ -48,6 +48,7 @@ func (r *recorder) events() []string { |
| type fakeDrainAct struct { |
| rec *recorder |
| quiet func() activator.DrainQuiet |
| + named func() activator.DrainQuietHosts |
| stop func(ctx context.Context, host string) (activator.DrainRelease, error) |
| owner func(rt registry.Route) (string, int64, bool) |
| @@ -83,6 +84,13 @@ func (f *fakeDrainAct) DrainQuietNow() activator.DrainQuiet { |
| } |
| return f.quiet() |
| } |
| +func (f *fakeDrainAct) DrainQuietNamed() (activator.DrainQuiet, activator.DrainQuietHosts) { |
| + q := f.DrainQuietNow() |
| + if f.named == nil { |
| + return q, activator.DrainQuietHosts{} |
| + } |
| + return q, f.named() |
| +} |
| func (f *fakeDrainAct) quietCalls() int { |
| f.mu.Lock() |
| defer f.mu.Unlock() |