region HA: drain asks every filed box, whatever its file's age
Astra r1 F1.1: a rolled-back binary keeps the newer binary's edges file (its writer drops ha_handover and skips the PUT), so the file is only a hint. The live acknowledgement is the proof: DrainAckPeers now returns every box with a file in the fold, by its key, with no age test, and an error when the fold has not been read in full. publishAndAck retries an unread fold on the same ticks within the same bound, then fails. A box that cannot answer (an old binary's 404, a dead box's leftover file) fails the drain before any move, by name. Witnesses: the edge scene's rolled-back box (old-schema writer skips the PUT, file keeps ha_handover: 1, ask 404s), quiet six-hour-old box that answers, and dead box's capable leftover; host driver tests for the named failure and the unread fold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+259
−38
internal/edge/hadrain_test.go+96 −2internal/edge/hadrainconns.go+27 −17internal/edge/hadrainconns_test.go+51 −11internal/host/hadrain.go+17 −6internal/host/hadrain_test.go+68 −2
diff --git a/internal/edge/hadrain_test.go b/internal/edge/hadrain_test.go| index ffdc6e2ce..204a3efce 100644 |
| --- a/internal/edge/hadrain_test.go |
| +++ b/internal/edge/hadrain_test.go |
| @@ -8,6 +8,7 @@ import ( |
| "io" |
| "net/http" |
| "net/http/httptest" |
| + "slices" |
| "strings" |
| "sync" |
| "sync/atomic" |
| @@ -78,8 +79,11 @@ type drainBox struct { |
| // door counts requests reaching this box's 443; app counts those its |
| // app saw, by path. |
| door atomic.Int64 |
| - mu sync.Mutex |
| - app []string |
| + // old makes the box run a binary from before held requests: nothing |
| + // answers the control paths (404), whatever its stored file says. |
| + old atomic.Bool |
| + mu sync.Mutex |
| + app []string |
| } |
| func (x *drainBox) edge() *Edge { return x.be.live.Load().edge } |
| @@ -137,6 +141,10 @@ func (s *drainScene) addWith(t *testing.T, box, ip, host, region string, act Act |
| x.be = be |
| srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| x.door.Add(1) |
| + if x.old.Load() && haControlPath(r.URL.Path) { |
| + http.NotFound(w, r) |
| + return |
| + } |
| be.live.Load().edge.ServeHTTP(w, r) |
| })) |
| srv.Listener.Close() |
| @@ -703,3 +711,89 @@ func TestHAControlSignatureBindsEachPart(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// oldSchemaFile is an edges file as a writer from before ha_handover |
| +// decodes it: the field is not in its struct. |
| +type oldSchemaFile struct { |
| + V int `json:"v"` |
| + Box string `json:"box"` |
| + EIP string `json:"eip"` |
| + Region string `json:"region"` |
| + Generation string `json:"generation"` |
| + Hosts map[string]any `json:"hosts"` |
| +} |
| + |
| +// oldWriterPass is an older writer's restart against x's stored file, the |
| +// shape of seedEdgesFileGate then the pass's write gate: it seeds its gate |
| +// by decoding the stored object into its own struct (dropping |
| +// ha_handover), builds the same file from its unchanged state, and skips |
| +// the PUT when the two match. It reports whether it wrote. |
| +func (s *drainScene) oldWriterPass(t *testing.T, x *drainBox) bool { |
| + t.Helper() |
| + stored, _, err := release.NewDirStore(s.bucket).Get(context.Background(), edgesPrefix+x.box+".json") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var seed oldSchemaFile |
| + if err := json.Unmarshal(stored, &seed); err != nil { |
| + t.Fatal(err) |
| + } |
| + seed.Generation = "" |
| + gate, _ := json.Marshal(seed) |
| + next, _ := json.Marshal(oldSchemaFile{V: 1, Box: x.box, EIP: x.ip, Region: x.region, |
| + Hosts: map[string]any{x.host: map[string]any{"role": "home"}}}) |
| + if string(gate) == string(next) { |
| + return false |
| + } |
| + putJSON(t, s.seed, edgesPrefix+x.box+".json", string(next)) |
| + return true |
| +} |
| + |
| +// The withdrawal's acknowledgement is the proof the floor's file cannot be |
| +// (held floor capability ruling, 2026-11-10). Every box with a file is |
| +// asked, whatever the file's age: |
| +// - a box rolled back to a binary from before held requests keeps the |
| +// newer binary's capable file (its writer drops the field it does not |
| +// know and skips the PUT), but cannot answer: the ask fails, naming it; |
| +// - a quiet live box whose file is hours old answers, so the drain can |
| +// go on; |
| +// - a dead box's capable leftover file is asked and cannot answer. |
| +func TestHADrainAckEveryFiledBox(t *testing.T) { |
| + s := newDrainScene(t) |
| + s.extra = map[string]map[string]any{"olden": {"ha_handover": 1}, "gone": {"ha_handover": 1}, "quiet": {"ha_handover": 1}} |
| + a := s.add(t, "annie", "127.0.0.11", "shop.example.com", "eu-west-1") |
| + olden := s.add(t, "olden", "127.0.0.12", "blog.example.com", "eu-west-1") |
| + quiet := s.add(t, "quiet", "127.0.0.13", "news.example.com", "us-east-1") |
| + old := time.Now().Add(-6 * time.Hour) |
| + s.file(t, a, drainGen0, notDrain) |
| + s.file(t, olden, old, notDrain) |
| + s.file(t, quiet, old, notDrain) |
| + // gone: a terminated box's leftover file, nothing listening. |
| + gone := &drainBox{box: "gone", ip: "127.0.0.14", host: "gone.example.com", region: "eu-west-1"} |
| + s.file(t, gone, old, notDrain) |
| + s.sync(t) |
| + |
| + olden.old.Store(true) |
| + if s.oldWriterPass(t, olden) { |
| + t.Fatal("the old writer wrote: an unchanged restart skips the PUT") |
| + } |
| + stored, _, err := release.NewDirStore(s.bucket).Get(context.Background(), edgesPrefix+"olden.json") |
| + if err != nil || !strings.Contains(string(stored), `"ha_handover":1`) { |
| + t.Fatalf("the rolled-back box's stored file lost the capability: %s %v", stored, err) |
| + } |
| + |
| + peers, err := a.edge().DrainAckPeers() |
| + if want := []string{"gone", "olden", "quiet"}; err != nil || !slices.Equal(peers, want) { |
| + t.Fatalf("peers %v, %v; want %v", peers, err, want) |
| + } |
| + s.file(t, a, drainGen0.Add(time.Minute), draining1) |
| + if seen, err := askDrain1(a, "quiet"); err != nil || !seen { |
| + t.Fatalf("the quiet live box: seen=%v err=%v, want seen", seen, err) |
| + } |
| + if seen, err := askDrain1(a, "olden"); err == nil || seen || !strings.Contains(err.Error(), "olden answered 404") { |
| + t.Fatalf("the rolled-back box: seen=%v err=%v, want its 404 named", seen, err) |
| + } |
| + if seen, err := askDrain1(a, "gone"); err == nil || seen || !strings.Contains(err.Error(), "gone") { |
| + t.Fatalf("the dead box: seen=%v err=%v, want a failed ask naming it", seen, err) |
| + } |
| +} |
diff --git a/internal/edge/hadrainconns.go b/internal/edge/hadrainconns.go| index fcbb6eda2..ec36059a1 100644 |
| --- a/internal/edge/hadrainconns.go |
| +++ b/internal/edge/hadrainconns.go |
| @@ -11,6 +11,7 @@ import ( |
| "net" |
| "net/http" |
| "slices" |
| + "strings" |
| "sync" |
| "sync/atomic" |
| "time" |
| @@ -19,32 +20,41 @@ import ( |
| "github.com/carlosframework/platform/internal/registry" |
| ) |
| -// DrainAckPeers is every other box of the deployment whose edges file is |
| -// live now (its generation fresh, boxchoice.HeadroomFresh): fixed boxes, |
| -// group members and boxes of other regions alike, since every live box |
| -// folds the whole deployment and so views this box's region (§25.4 step |
| -// 1). Memory only. Sorted. |
| -func (e *Edge) DrainAckPeers(now time.Time) []string { |
| +// DrainAckPeers is every other box of the deployment with an edges file in |
| +// this edge's fold, whatever the file's age: fixed boxes, group members |
| +// and boxes of other regions alike, since every box folds the whole |
| +// deployment and so views this box's region (§25.4 step 1). The file is a |
| +// hint, not proof (held floor capability ruling, 2026-11-10): a box that |
| +// publishes only on change may be hours quiet and still forward, and a |
| +// rolled-back binary leaves the newer binary's file in place. Only a live |
| +// acknowledgement proves a box holds requests, so a stale file's box is |
| +// asked like any other, and one that cannot answer (an old binary, a dead |
| +// box's leftover file) fails the drain by name. |
| +// |
| +// A peer is named by its file's key, so a file whose box field differs |
| +// from its key is asked by its key and its ask fails (controlTarget). An |
| +// error, and no peers, when the fold has not been read in full: a key the |
| +// last poll could not read or parse may be a box that must answer. Memory |
| +// only. Sorted. |
| +func (e *Edge) DrainAckPeers() ([]string, error) { |
| m := e.homes.Load() |
| if m == nil { |
| - return nil |
| + return nil, errors.New("ha-drain: this edge has no home map") |
| } |
| snap := m.snapshot() |
| - seen := map[string]bool{} |
| + if !snap.read || !snap.departureComplete { |
| + return nil, errors.New("ha-drain: the edges files have not all been read") |
| + } |
| var out []string |
| - for _, f := range snap.files { |
| - if f.Box == "" || f.Box == m.self || seen[f.Box] { |
| - continue |
| - } |
| - var gen string |
| - if !placementField(f.Generation, &gen) || !boxchoice.HeadroomFresh(gen, now) { |
| + for key := range snap.files { |
| + box := strings.TrimSuffix(strings.TrimPrefix(key, edgesPrefix), ".json") |
| + if box == "" || box == m.self { |
| continue |
| } |
| - seen[f.Box] = true |
| - out = append(out, f.Box) |
| + out = append(out, box) |
| } |
| slices.Sort(out) |
| - return out |
| + return out, nil |
| } |
| // DrainSnapshot is the wake snapshot a move chooses its successor from |
diff --git a/internal/edge/hadrainconns_test.go b/internal/edge/hadrainconns_test.go| index af23ecf1a..1de5aaab7 100644 |
| --- a/internal/edge/hadrainconns_test.go |
| +++ b/internal/edge/hadrainconns_test.go |
| @@ -17,27 +17,67 @@ import ( |
| "github.com/carlosframework/platform/internal/registry" |
| ) |
| -// The drain asks every other live box, whatever its region or kind, and |
| -// no box whose file is stale. |
| -func TestDrainAckPeersAreTheLiveBoxes(t *testing.T) { |
| +// ackMap is a home map read in full from files keyed by their box. |
| +func ackMap(t *testing.T, files ...map[string]any) *homeMap { |
| + t.Helper() |
| + m := newHomeMap(nil, "self") |
| + snap := &homeSnapshot{files: map[string]edgesFileView{}, read: true, departureComplete: true} |
| + for _, f := range files { |
| + key := edgesPrefix + f["box"].(string) + ".json" |
| + if k, ok := f["key"].(string); ok { |
| + key = k |
| + delete(f, "key") |
| + } |
| + parsed, err := parseEdgesFile(key, wakeBody(t, f)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + snap.files[key] = parsed |
| + } |
| + snap.hosts, _ = foldHomes(snap.files, m.self, false) |
| + m.snap = snap |
| + return m |
| +} |
| + |
| +// The drain asks every other box with an edges file, whatever its region, |
| +// kind or the file's age (held floor capability ruling, 2026-11-10): the |
| +// file is a hint and only a live answer proves the box runs a binary that |
| +// holds requests. A quiet box publishes hours apart, and a stale file may |
| +// be a rolled-back or dead box, which must fail the drain, not be skipped. |
| +func TestDrainAckPeersAreEveryFiledBox(t *testing.T) { |
| now := time.Now().UTC() |
| - fresh, stale := now.Format(time.RFC3339), now.Add(-time.Hour).Format(time.RFC3339) |
| - m := wakeMap(t, |
| + fresh, stale := now.Format(time.RFC3339), now.Add(-6*time.Hour).Format(time.RFC3339) |
| + m := ackMap(t, |
| map[string]any{"v": 1, "box": "self", "generation": fresh, "region": "ie"}, |
| map[string]any{"v": 1, "box": "b", "generation": fresh, "region": "ie"}, |
| map[string]any{"v": 1, "box": "g-0123456789abcdef0123", "generation": fresh, "region": "ie"}, |
| map[string]any{"v": 1, "box": "far", "generation": fresh, "region": "us-east-1"}, |
| - map[string]any{"v": 1, "box": "dead", "generation": stale, "region": "ie"}, |
| + map[string]any{"v": 1, "box": "dead", "generation": stale, "region": "ie", "ha_handover": 1}, |
| map[string]any{"v": 1, "box": "nogen", "region": "ie"}, |
| + // A file whose box is not its key's is asked by its key, so the |
| + // ask fails (controlTarget) and the drain names it. |
| + map[string]any{"v": 1, "box": "other", "key": edgesPrefix + "misfiled.json", "generation": fresh}, |
| ) |
| e := &Edge{} |
| e.homes.Store(m) |
| - got := e.DrainAckPeers(now) |
| - if want := []string{"b", "far", "g-0123456789abcdef0123"}; !slices.Equal(got, want) { |
| - t.Fatalf("peers %v, want %v", got, want) |
| + got, err := e.DrainAckPeers() |
| + if want := []string{"b", "dead", "far", "g-0123456789abcdef0123", "misfiled", "nogen"}; err != nil || !slices.Equal(got, want) { |
| + t.Fatalf("peers %v, %v; want %v", got, err, want) |
| + } |
| + // No fold, or a fold not read in full, is no answer: a file the poll |
| + // could not parse could be a box that must acknowledge. |
| + if p, err := (&Edge{}).DrainAckPeers(); err == nil || p != nil { |
| + t.Fatalf("peers without a fold: %v, %v", p, err) |
| } |
| - if (&Edge{}).DrainAckPeers(now) != nil { |
| - t.Fatal("peers without a fold") |
| + unread := newHomeMap(nil, "self") |
| + e.homes.Store(unread) |
| + if p, err := e.DrainAckPeers(); err == nil || p != nil { |
| + t.Fatalf("peers from an unread fold: %v, %v", p, err) |
| + } |
| + m.snap.departureComplete = false |
| + e.homes.Store(m) |
| + if p, err := e.DrainAckPeers(); err == nil || p != nil { |
| + t.Fatalf("peers from a partial fold: %v, %v", p, err) |
| } |
| } |
diff --git a/internal/host/hadrain.go b/internal/host/hadrain.go| index 4a3234cd4..03462a250 100644 |
| --- a/internal/host/hadrain.go |
| +++ b/internal/host/hadrain.go |
| @@ -98,7 +98,7 @@ type drainActivator interface { |
| // drainEdge is the edge's half (internal/edge hadrain.go, hatake.go, |
| // hadrainconns.go). |
| type drainEdge interface { |
| - DrainAckPeers(now time.Time) []string |
| + DrainAckPeers() ([]string, error) |
| AskDrainSeen(ctx context.Context, peer, txn, phase string, id uint64) (bool, error) |
| PeerTakes(ref string) bool |
| PeerDraining(ref string) bool |
| @@ -407,8 +407,11 @@ func (d *drainDriver) withdraw(ctx context.Context, txn string, rep *drainReport |
| } |
| // publishAndAck waits for the wanted drain state to be published, then |
| -// asks every live box until each has seen it: every 5 s, failing after 2 |
| -// minutes. |
| +// asks every box with an edges file, whatever the file's age, until each |
| +// has seen it: every 5 s, failing after 2 minutes, naming each box that |
| +// did not (held floor capability ruling, 2026-11-10: the file is a hint, |
| +// the live answer is the proof). A fold not yet read in full is retried on |
| +// the same ticks within the same bound. |
| func (d *drainDriver) publishAndAck(ctx context.Context, txn, phase string, rep *drainReport) []string { |
| pctx, cancel := context.WithTimeout(ctx, drainPublishTimeout) |
| mark, err := d.marks.waitPublished(pctx) |
| @@ -418,7 +421,17 @@ func (d *drainDriver) publishAndAck(ctx context.Context, txn, phase string, rep |
| } |
| actx, acancel := context.WithTimeout(ctx, drainAckBound) |
| defer acancel() |
| - peers := d.edge.DrainAckPeers(d.now()) |
| + tick := time.NewTicker(drainAckEvery) |
| + defer tick.Stop() |
| + peers, perr := d.edge.DrainAckPeers() |
| + for perr != nil { |
| + select { |
| + case <-actx.Done(): |
| + return []string{fmt.Sprintf("the %s state could not be acknowledged: %v", phase, perr)} |
| + case <-tick.C: |
| + } |
| + peers, perr = d.edge.DrainAckPeers() |
| + } |
| // Only this goroutine reads or writes seen and why; the askers send |
| // their answers back on a channel. |
| seen := map[string]bool{} |
| @@ -428,8 +441,6 @@ func (d *drainDriver) publishAndAck(ctx context.Context, txn, phase string, rep |
| ok bool |
| err error |
| } |
| - tick := time.NewTicker(drainAckEvery) |
| - defer tick.Stop() |
| for { |
| var pending []string |
| for _, p := range peers { |
diff --git a/internal/host/hadrain_test.go b/internal/host/hadrain_test.go| index f1f19bc7e..27655a8d4 100644 |
| --- a/internal/host/hadrain_test.go |
| +++ b/internal/host/hadrain_test.go |
| @@ -264,6 +264,7 @@ type fakeDrainEdge struct { |
| mu sync.Mutex |
| peers []string |
| + peersErr func() error |
| asks map[string]int |
| nottake map[string]bool |
| draining map[string]bool |
| @@ -274,10 +275,16 @@ type fakeDrainEdge struct { |
| heldShut []string |
| } |
| -func (f *fakeDrainEdge) DrainAckPeers(time.Time) []string { |
| +func (f *fakeDrainEdge) DrainAckPeers() ([]string, error) { |
| f.mu.Lock() |
| defer f.mu.Unlock() |
| - return append([]string(nil), f.peers...) |
| + if f.peersErr != nil { |
| + err := f.peersErr() |
| + if err != nil { |
| + return nil, err |
| + } |
| + } |
| + return append([]string(nil), f.peers...), nil |
| } |
| func (f *fakeDrainEdge) AskDrainSeen(_ context.Context, peer, txn, phase string, id uint64) (bool, error) { |
| f.mu.Lock() |
| @@ -902,6 +909,65 @@ func TestDrainMissingAcknowledgement(t *testing.T) { |
| } |
| } |
| +// A fold not read in full is no peer list: it is retried on the ack |
| +// ticks, and the drain fails within the same bound if it never is (held |
| +// floor capability ruling: every filed box must answer, so an unread file |
| +// cannot be skipped). |
| +func TestDrainAckPeersUnreadFold(t *testing.T) { |
| + defer shorten(&drainAckBound, 120*time.Millisecond)() |
| + defer shorten(&drainAckEvery, 20*time.Millisecond)() |
| + act, ed, marks := healthyDrain() |
| + ed.peersErr = func() error { return errors.New("ha-drain: the edges files have not all been read") } |
| + rep := runDrain(t, newDrainDriver(act, ed, marks, fenceOf("t1")), "t1") |
| + if rep.Result != "failed" || rep.Step != "withdraw" || !strings.Contains(rep.Reasons[0], "have not all been read") { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + if ed.askCount("b draining") != 0 { |
| + t.Fatal("asked with no peer list") |
| + } |
| + // Read on a later tick: the drain goes on. |
| + act, ed, marks = healthyDrain() |
| + var calls atomic.Int32 |
| + ed.peersErr = func() error { |
| + if calls.Add(1) < 3 { |
| + return errors.New("ha-drain: the edges files have not all been read") |
| + } |
| + return nil |
| + } |
| + rep = runDrain(t, newDrainDriver(act, ed, marks, fenceOf("t1")), "t1") |
| + if rep.Result != "ok" { |
| + t.Fatalf("report %+v", rep) |
| + } |
| +} |
| + |
| +// The drain fails before any move when a filed box cannot answer: a box |
| +// rolled back to an older binary (its capable file left in place) and a |
| +// dead box's leftover file are named; a quiet box that answers is not. |
| +func TestDrainFailsNamingFiledBoxesThatCannotAnswer(t *testing.T) { |
| + defer shorten(&drainAckBound, 120*time.Millisecond)() |
| + defer shorten(&drainAckEvery, 20*time.Millisecond)() |
| + act, ed, marks := healthyDrain() |
| + ed.peers = []string{"gone", "olden", "quiet"} |
| + ed.ack = func(peer string) (bool, error) { |
| + switch peer { |
| + case "olden": |
| + return false, errors.New("ha-drain: olden answered 404") |
| + case "gone": |
| + return false, errors.New("ha-drain: ask gone (127.0.0.14:443): connection refused") |
| + } |
| + return true, nil |
| + } |
| + rep := runDrain(t, newDrainDriver(act, ed, marks, fenceOf("t1")), "t1") |
| + why := strings.Join(rep.Reasons, ";") |
| + if rep.Result != "failed" || rep.Step != "withdraw" || !strings.Contains(why, "olden (ha-drain: olden answered 404)") || |
| + !strings.Contains(why, "gone (") || strings.Contains(why, "quiet") { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + if len(rep.Moved) != 0 || act.endedMoves() != 0 { |
| + t.Fatalf("moved before the acknowledgement: %+v", rep.Moved) |
| + } |
| +} |
| + |
| // Many peers answering at once (the acknowledgement map is the |
| // coordinator's alone): run under -race. |
| func TestDrainAcknowledgementsConcurrently(t *testing.T) { |