carlos / platform Public

grouproll: check the failed drain before every ExitStandby

An exit-intent row is written before dispatch, and dispatch can be cut or
refused (an expired observation). The script skipped the moving-drain
check once any exit-intent existed, so a resume with the drain status now
unknown or running could return the member. The return now runs the check
on the status it acts on immediately before each ExitStandby (r2 cut, a
departure from the script).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev f81929e90e212ab371a01a7484b0711bc0c1e59c parent a1f932f
2 files changed, +99 −1
  • internal/grouproll/depart_test.go +77 −0
  • internal/grouproll/roll.go +22 −1
diff --git a/internal/grouproll/depart_test.go b/internal/grouproll/depart_test.go
index baa9a5c3b..d80cd29a3 100644
--- a/internal/grouproll/depart_test.go
+++ b/internal/grouproll/depart_test.go
@@ -1575,3 +1575,80 @@ func TestDepartUnansweredRestoreRecovers(t *testing.T) {
})
}
}
+
+// Astra P1-2 (the r2 cut, departing from the script): an exit-intent row
+// is written before ExitStandby is sent, so it proves nothing about the
+// drain. A failed drain's return cut after that row, then resumed while
+// the drain status is unknown or running, stops drain-moving and sends no
+// ExitStandby.
+func TestDepartReturnCutAfterExitIntentRechecksDrain(t *testing.T) {
+ for _, status := range []string{"unknown", "running"} {
+ t.Run(status, func(t *testing.T) {
+ h := newHeldHarness(t, 2, nil)
+ h.f.held.drainMode = "drop-done"
+ h.f.before = func(op string, _ map[string]any) error {
+ if op == "exit" {
+ panic(fakeDeath{"exit"})
+ }
+ return nil
+ }
+ if !expectDeath(t, func() { h.depart("replace", heldTx) }) {
+ t.Fatal("no cut at ExitStandby")
+ }
+ if _, ok := h.txJournal(heldTx).Last("exit-intent", nil); !ok || h.f.states[fakeIID] != "Standby" {
+ t.Fatalf("exit-intent %v state %s", ok, h.f.states[fakeIID])
+ }
+ h.f.before = nil
+ h.f.held.statusDrain = unknownDrain
+ if status == "running" {
+ h.f.held.statusDrain = func(map[string]any) map[string]any {
+ return map[string]any{"state": "known", "incarnation": heldIncarnation,
+ "transaction": heldTx, "step": "move", "result": "running"}
+ }
+ }
+ calls := len(h.f.calls)
+ if err := h.resume(); !isStop(err, "drain-moving") {
+ t.Fatalf("resume = %v, want drain-moving", err)
+ }
+ for _, c := range h.f.calls[calls:] {
+ if c.Op == "exit" {
+ t.Fatal("ExitStandby sent while the drain may still move hosts")
+ }
+ }
+ if h.f.states[fakeIID] != "Standby" || h.activeTx() != heldTx {
+ t.Fatalf("state %s active %q", h.f.states[fakeIID], h.activeTx())
+ }
+ })
+ }
+}
+
+// Astra P1-2: an ExitStandby refused before dispatch because its
+// observation expired leaves an exit-intent row behind. The resumed
+// attempt, with the drain status now unknown, stops drain-moving with no
+// ExitStandby.
+func TestDepartExpiredExitAttemptRechecksDrain(t *testing.T) {
+ h := newHeldHarness(t, 2, nil)
+ h.f.held.drainMode = "drop-done"
+ expired := false
+ h.beforeEvent = func(kind string, _ map[string]any) {
+ if kind == "exit-intent" && !expired {
+ expired = true
+ h.f.now = h.f.now.Add(10 * time.Second)
+ h.f.held.statusDrain = unknownDrain
+ }
+ }
+ // The expired attempt stops the run as an operation error (admission).
+ if err := h.depart("replace", heldTx); refusalKey(err) != "admission" || !expired {
+ t.Fatalf("depart = %v (exit-intent %v), want admission", err, expired)
+ }
+ if err := h.resume(); !isStop(err, "drain-moving") {
+ t.Fatalf("resume = %v, want drain-moving", err)
+ }
+ j := h.txJournal(heldTx)
+ if r, _ := j.Last("error", map[string]any{"op": "exit"}); r["error"] != "ObservationExpired" || r["dispatched"] != false {
+ t.Fatalf("exit error row %v", r)
+ }
+ if countOf(h.verbs(), "exit") != 0 || h.f.states[fakeIID] != "Standby" {
+ t.Fatalf("verbs %v state %s", h.verbs(), h.f.states[fakeIID])
+ }
+}
diff --git a/internal/grouproll/roll.go b/internal/grouproll/roll.go
index 38d1a02a5..b8344dff1 100644
--- a/internal/grouproll/roll.go
+++ b/internal/grouproll/roll.go
@@ -1246,6 +1246,13 @@ func (e *Engine) returnMember(ctx context.Context) error {
if ready == nil {
return refuse("record")
}
+ // The r2 cut: every ExitStandby of a failed drain's
+ // member is preceded by the drain check on the status it
+ // acts on. An earlier exit-intent proves nothing: it is
+ // written before dispatch, which can be cut or refused.
+ if err := e.checkDrainBeforeExit(ready.Status); err != nil {
+ return err
+ }
edge, _ := ready.Status["edge"].(map[string]any)
if _, err := e.event("exit-intent", map[string]any{"transaction": tx, "iid": iid,
"boot_id": e.hIdentity()["boot_id"], "process": edge["identity"],
@@ -1369,8 +1376,22 @@ func (e *Engine) returnReadiness(ctx context.Context, iid string, group *snapsho
// agent shows that no move step of this drain runs. Unknown or malformed
// drain status proves nothing, so it refuses too; the operator cancels the
// drain, then returns the member.
+//
+// The readiness check skips a member that has an exit-intent (the
+// script's rule, so a member that has left Standby is not held back);
+// checkDrainBeforeExit is the check without that exemption.
func (e *Engine) refuseWhileMoving(status map[string]any) error {
- if !e.unchanged() || e.has("exit-intent", nil) {
+ if e.has("exit-intent", nil) {
+ return nil
+ }
+ return e.checkDrainBeforeExit(status)
+}
+
+// checkDrainBeforeExit is refuseWhileMoving with no exemption for an
+// earlier exit-intent: the gate in front of every ExitStandby (spec r2
+// cut; the script skipped it once any exit-intent existed).
+func (e *Engine) checkDrainBeforeExit(status map[string]any) error {
+ if !e.unchanged() {
return nil
}
d, ok := status["drain"].(map[string]any)