host: retire HA members promptly with sparse lock scans
List the departed member's data-bucket locks once per walk so unrelated records do not consume the 32-read page budget. Preserve per-record catch-up, lease waits, known-duty reads and the independent rescan; fall back to the full walk if listing fails. Exercise three departures over 260 records under 30-second write churn. Both expired and released leases finish in 2m30s, while the previous scanner misses the five-minute bound. Cover listing pagination, failures and seeded safety/liveness simulation.
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
internal/host/hadepartures_g3_test.go+10 −0internal/host/hadepartures_listing_test.go+285 −0internal/host/hadepartures_scan.go+31 −2internal/host/hadepartures_scan_test.go+6 −4internal/lease/ha.go+26 −2internal/lease/ha_list_test.go+49 −0internal/registry/ha_scan.go+3 −0
Intent
<task-notification> <task-id>b0mra5ob7</task-id> <tool-use-id>toolu_01AMmi7WJxnneyNhR5WGUok2</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/f72ef775-81b3-4d2e-a18d-c8edb14aa192/tasks/b0mra5ob7.output</output-file> <status>completed</status> <summary>Background command "sleep 15; .superpowers/sdd/2026-11-07-region-ha-g3-fixes/wait.sh t9b" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 24.5 g CO₂e (2,980,436 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-g3-fixes, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/host/hadepartures_g3_test.go b/internal/host/hadepartures_g3_test.go| index c468c7977..a7e428fbf 100644 |
| --- a/internal/host/hadepartures_g3_test.go |
| +++ b/internal/host/hadepartures_g3_test.go |
| @@ -37,6 +37,7 @@ type g3Departure struct { |
| id, edgesKey, edgesTag string |
| records []provision.Record |
| pages int |
| + signer *boxidtest.Signer |
| } |
| func newG3Departure(t *testing.T, n int) *g3Departure { |
| @@ -62,6 +63,7 @@ func newG3Departure(t *testing.T, n int) *g3Departure { |
| x.peer = hadeparture.Box{Account: a.Self.Account, InstanceID: "i-2123456789abcdef0", Scope: a.Self.Scope} |
| x.peer.Ref = groupReferenceOf(x.peer.Account, x.peer.InstanceID) |
| signer := boxidtest.NewSigner(t) |
| + x.signer = signer |
| a.Verifier = boxid.Verifier{Account: a.Self.Account, Certs: signer.Certs()} |
| for _, b := range []hadeparture.Box{a.Self, x.dead, x.peer} { |
| _, raw := signer.Record(t, b.Ref, b.Account, b.Scope.CloudRegion, b.InstanceID) |
| @@ -318,6 +320,11 @@ func TestDepartureG3Simulation(t *testing.T) { |
| rng := rand.New(rand.NewSource(seed)) |
| n := 1 + rng.Intn(300) |
| x := newScanRig(t, n) |
| + listing := &listedScanReader{scanReader: x.locks} |
| + if seed%7 == 0 { |
| + listing.err = errors.New("AccessDenied") |
| + } |
| + x.c.cfg.NewReader = func(context.Context, string) (DepartureLockReader, error) { return listing, nil } |
| rng.Shuffle(n, func(i, j int) { x.inv.Records[i], x.inv.Records[j] = x.inv.Records[j], x.inv.Records[i] }) |
| start := x.now |
| lastExpiry := start |
| @@ -734,6 +741,9 @@ func TestDepartureG3MissingPendingRecord(t *testing.T) { |
| func TestDepartureG3RollbackCheckpoint(t *testing.T) { |
| x := newG3Departure(t, 100) |
| + for _, rec := range x.records[1:] { |
| + x.fx.PutLock("instances/"+rec.Host, x.fx.LockBody("instances/"+x.records[0].Host)) |
| + } |
| if err := x.c.step(t.Context()); err != nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/host/hadepartures_listing_test.go b/internal/host/hadepartures_listing_test.go| new file mode 100644 |
| index 000000000..e4863b40b |
| --- /dev/null |
| +++ b/internal/host/hadepartures_listing_test.go |
| @@ -0,0 +1,285 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "net/http" |
| + "os" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/boxid" |
| + "github.com/carlosframework/platform/internal/hadeparture" |
| + "github.com/carlosframework/platform/internal/hafresh" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +type listedScanReader struct { |
| + *scanReader |
| + lists int |
| + err error |
| + omit bool |
| +} |
| + |
| +func (r *listedScanReader) ListLocks(context.Context, string) (map[string]bool, error) { |
| + r.lists++ |
| + keys := map[string]bool{} |
| + if !r.omit { |
| + for key := range r.views { |
| + keys[key] = true |
| + } |
| + } |
| + return keys, r.err |
| +} |
| + |
| +func TestDepartureLockListingPages(t *testing.T) { |
| + x := newScanRig(t, 260) |
| + for _, h := range x.inv.Records[32:259] { |
| + delete(x.locks.views, h.Host) |
| + } |
| + l := &listedScanReader{scanReader: x.locks} |
| + x.c.cfg.NewReader = func(context.Context, string) (DepartureLockReader, error) { return l, nil } |
| + for i, want := range []int{32, 33, 65, 66} { |
| + x.pass(t) |
| + if x.locks.reads != want || l.lists != i/2+1 { |
| + t.Fatalf("page %d: reads=%d lists=%d", i, x.locks.reads, l.lists) |
| + } |
| + } |
| + row, err := x.r.Departure(t.Context(), x.d.ID) |
| + if err != nil || row.Phase != "complete" { |
| + t.Fatal(row, err) |
| + } |
| +} |
| + |
| +func TestDepartureLockListing(t *testing.T) { |
| + for _, mode := range []string{"sparse", "empty", "failed", "seen", "catchup"} { |
| + t.Run(mode, func(t *testing.T) { |
| + x := newScanRig(t, 260) |
| + for _, h := range x.inv.Records[1:] { |
| + delete(x.locks.views, h.Host) |
| + } |
| + if mode == "empty" { |
| + delete(x.locks.views, x.inv.Records[0].Host) |
| + } |
| + l := &listedScanReader{scanReader: x.locks} |
| + x.c.cfg.NewReader = func(context.Context, string) (DepartureLockReader, error) { return l, nil } |
| + logs := 0 |
| + x.c.cfg.Logf = func(format string, _ ...any) { |
| + if strings.Contains(format, "lock_list_fallback") { |
| + logs++ |
| + } |
| + } |
| + if mode == "failed" { |
| + l.err = errors.New("AccessDenied") |
| + } |
| + x.pass(t) |
| + if l.lists != 1 { |
| + t.Fatal("listing not taken once", l.lists) |
| + } |
| + if mode == "seen" { |
| + l.omit = true |
| + } |
| + if mode == "catchup" { |
| + h := x.inv.Records[259] |
| + x.inv.Records[259].Revision = "changed" |
| + marker := hafresh.Marker{V: 1, Box: x.d.Box.Ref, IID: x.d.Box.InstanceID, Lifetime: h.Lifetime} |
| + x.locks.views[h.Host] = hafresh.View{ETag: "late", Lock: hafresh.Lock{Generation: 1, Owner: hafresh.MarkerOwner(marker), Release: &marker, ExpiresAt: time.Unix(0, 0)}} |
| + } |
| + for i := 0; i < 24; i++ { |
| + x.pass(t) |
| + row, err := x.r.Departure(t.Context(), x.d.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if row.Phase == "complete" { |
| + break |
| + } |
| + } |
| + row, err := x.r.Departure(t.Context(), x.d.ID) |
| + if err != nil || row.Phase != "complete" { |
| + t.Fatal(row, err) |
| + } |
| + switch mode { |
| + case "failed": |
| + if x.locks.reads != 520 || l.lists != 2 || logs != 1 { |
| + t.Fatalf("fallback reads=%d lists=%d logs=%d", x.locks.reads, l.lists, logs) |
| + } |
| + case "empty": |
| + if x.locks.reads != 0 || l.lists != 2 { |
| + t.Fatal(x.locks.reads, l.lists) |
| + } |
| + case "catchup": |
| + if x.imports != 2 { |
| + t.Fatal("new lock missed on rescan", x.imports) |
| + } |
| + default: |
| + if x.locks.reads != 2 || l.lists != 2 { |
| + t.Fatal(x.locks.reads, l.lists) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDepartureG3FiveMinutes(t *testing.T) { |
| + for _, released := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("released=%v", released), func(t *testing.T) { |
| + x := newG3Departure(t, 260) |
| + for i := 1; i < len(x.records); i++ { |
| + rec := x.records[i] |
| + rec.HA = false |
| + _, tag, err := x.store.Get(t.Context(), provision.RecordKey(rec.Account, rec.App, rec.Host)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err = provision.UpdateRecord(t.Context(), x.store, rec, tag); err != nil { |
| + t.Fatal(err) |
| + } |
| + x.records[i] = rec |
| + } |
| + x.expiry = x.start.Add(-time.Second) |
| + x.putLock(t, x.dead, 5, x.expiry) |
| + if released { |
| + entry, _ := provision.LifetimeEntryOf(x.records[0]) |
| + raw, _ := hafresh.MarkerBody(5, hafresh.Marker{V: 1, Box: x.dead.Ref, IID: x.dead.InstanceID, Lifetime: entry.ID}) |
| + x.fx.PutLock("instances/"+x.records[0].Host, raw) |
| + } |
| + ids := []string{x.id} |
| + keys := []string{x.edgesKey} |
| + for i := 3; i <= 4; i++ { |
| + b := hadeparture.Box{Account: x.dead.Account, Scope: x.dead.Scope, InstanceID: fmt.Sprintf("i-%d123456789abcdef0", i)} |
| + b.Ref = groupReferenceOf(b.Account, b.InstanceID) |
| + _, raw := x.signer.Record(t, b.Ref, b.Account, b.Scope.CloudRegion, b.InstanceID) |
| + if _, err := x.store.Put(t.Context(), boxid.Key(b.Ref), raw, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + key := "control/edges/" + b.Ref + ".json" |
| + // Unchanged, enrolled revisions use the real completion read/delete. |
| + tag, err := x.store.Put(t.Context(), key, []byte(`{"box":"`+b.Ref+`"}`), "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + obs, err := x.c.cfg.Identities.Observe(t.Context(), hadeparture.BoxObservation{Known: true, Box: b, EdgesKey: key, EdgesETag: tag}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err = x.c.Enqueue(t.Context(), obs); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err = x.c.record(t.Context(), hadeparture.Message{ID: b.InstanceID, Body: terminationHint(b.InstanceID), Receipt: x.queue.queue}); err != nil { |
| + t.Fatal(err) |
| + } |
| + ids = append(ids, departureID(x.c.cfg.DeploymentBucket, obs.Box)) |
| + keys = append(keys, key) |
| + } |
| + rows, err := x.a.Registry.PendingDepartures(t.Context(), x.now, 10) |
| + if err != nil || len(rows) != 3 { |
| + t.Fatalf("pending=%d %v", len(rows), err) |
| + } |
| + for _, row := range rows { |
| + if row.ProofDigest == "" || row.ProofKey == "" { |
| + t.Fatal("missing termination proof") |
| + } |
| + } |
| + requestStart := len(x.fx.Requests()) |
| + for !x.now.After(x.start.Add(5 * time.Minute)) { |
| + // One unrelated write every 30 seconds, including between scan slots. |
| + rec := x.records[1] |
| + key := provision.RecordKey(rec.Account, rec.App, rec.Host) |
| + raw, tag, err := x.store.Get(t.Context(), key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = x.store.Put(t.Context(), key, append(raw, '\n'), tag); err != nil { |
| + t.Fatal(err) |
| + } |
| + x.reconcile(t) |
| + if err = x.c.step(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + complete := true |
| + for _, id := range ids { |
| + row, err := x.a.Registry.Departure(t.Context(), id) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if row.LastError == "blocked" || row.RetryCount != 0 { |
| + t.Fatalf("churn blocked: %+v", row) |
| + } |
| + complete = complete && row.Phase == "complete" |
| + } |
| + if complete { |
| + break |
| + } |
| + x.now = x.now.Add(30 * time.Second) |
| + } |
| + if x.now.After(x.start.Add(5 * time.Minute)) { |
| + t.Fatal("departures missed five-minute deadline") |
| + } |
| + for _, key := range keys { |
| + if _, _, err := x.store.Get(t.Context(), key); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal("edges retained", key, err) |
| + } |
| + } |
| + if _, _, acks := x.queue.counts(); acks != 3 { |
| + t.Fatal("receipts not acked", acks) |
| + } |
| + if x.pages != 0 { |
| + t.Fatal("churn paged", x.pages) |
| + } |
| + lists, unrelatedReads := 0, 0 |
| + for _, req := range x.fx.Requests()[requestStart:] { |
| + if req == "LIST instances/" { |
| + lists++ |
| + } |
| + if strings.HasPrefix(req, "GET instances/g3-") && strings.HasSuffix(req, "/lock.json") && req != "GET instances/"+x.records[0].Host+"/lock.json" { |
| + unrelatedReads++ |
| + } |
| + } |
| + if lists != 6 || unrelatedReads != 0 { |
| + t.Fatalf("lists=%d unrelated lock reads=%d", lists, unrelatedReads) |
| + } |
| + timer, err := x.a.Registry.GetTimer(x.records[0].Host, "reminder") |
| + if err != nil || timer == nil || timer.Registration != "original" { |
| + t.Fatal("wake not adopted", timer, err) |
| + } |
| + t.Logf("three departures completed and acked in %s; %d listings, %d unrelated lock reads", x.now.Sub(x.start), lists, unrelatedReads) |
| + }) |
| + } |
| +} |
| + |
| +func TestDepartureG3ListingDenied(t *testing.T) { |
| + x := newG3Departure(t, 65) |
| + x.expiry = x.start.Add(-time.Second) |
| + x.putLock(t, x.dead, 5, x.expiry) |
| + logs := 0 |
| + x.c.cfg.Logf = func(format string, _ ...any) { |
| + if strings.Contains(format, "lock_list_fallback") { |
| + logs++ |
| + } |
| + } |
| + for range 8 { |
| + x.fx.FailNext("LIST", http.StatusForbidden) |
| + if err := x.c.step(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if x.row(t).Phase == "complete" { |
| + break |
| + } |
| + x.now = x.row(t).NextAttempt |
| + } |
| + if x.row(t).Phase != "complete" || logs != 1 || x.pages != 0 { |
| + t.Fatalf("fallback: phase=%s logs=%d pages=%d", x.row(t).Phase, logs, x.pages) |
| + } |
| + reads := 0 |
| + for _, req := range x.fx.Requests() { |
| + if req == "GET instances/"+x.records[64].Host+"/lock.json" { |
| + reads++ |
| + } |
| + } |
| + if reads != 2 { |
| + t.Fatal("fallback did not walk complete inventory twice", reads) |
| + } |
| +} |
diff --git a/internal/host/hadepartures_scan.go b/internal/host/hadepartures_scan.go| index eda9e1cfb..d8d821069 100644 |
| --- a/internal/host/hadepartures_scan.go |
| +++ b/internal/host/hadepartures_scan.go |
| @@ -26,6 +26,10 @@ type DepartureLockReader interface { |
| ReadLock(context.Context, string, string) (hafresh.View, error) |
| } |
| +type departureLockLister interface { |
| + ListLocks(context.Context, string) (map[string]bool, error) |
| +} |
| + |
| var errDeparturePending = errors.New("ha_departure_pending") |
| // MatchesDeparture never treats an unparsed owner or a partial marker as evidence. |
| @@ -137,6 +141,7 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| s.Waiting = 0 |
| s.NotBefore = time.Time{} |
| s.Visited = map[string]registry.HADepartureVisit{} |
| + s.LockPrefixes, s.LocksListed = nil, false |
| if s.Hosts == nil { |
| s.Hosts = map[string]registry.HADepartureHostScan{} |
| } |
| @@ -153,6 +158,20 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| if err != nil { |
| return false, errors.Join(err, c.retryScan(ctx, d, nil, "reader", true)) |
| } |
| + if !s.LocksListed { |
| + s.LocksListed = true |
| + if lister, ok := reader.(departureLockLister); ok { |
| + call, cancel := context.WithTimeout(ctx, 5*time.Second) |
| + prefixes, err := lister.ListLocks(call, d.Box.Scope.DataBucket) |
| + cancel() |
| + if err == nil { |
| + s.LockPrefixes = prefixes |
| + } else if !s.LockListAlerted { |
| + c.logf("ha_departure_lock_list_fallback departure=%s bucket=%s error=%v", d.ID, d.Box.Scope.DataBucket, err) |
| + s.LockListAlerted = true |
| + } |
| + } |
| + } |
| blockers := []provision.DepartureHost{} |
| count := 0 |
| for _, h := range inv.Records { |
| @@ -160,10 +179,16 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| if visit, ok := s.Visited[h.Host]; ok && visit.RecordDigest == digest { |
| continue |
| } |
| - if count == 32 || ctx.Err() != nil { |
| + _, previouslySeen := s.Hosts[h.Host] |
| + // After termination proof, the dead member cannot create another lock. |
| + // Keep reading known duties even if their lock disappears from the list. |
| + absent := s.LockPrefixes != nil && !s.LockPrefixes[c.cfg.Prefix(h.Host)] && !previouslySeen |
| + if count == 32 && !absent || ctx.Err() != nil { |
| break |
| } |
| - count++ |
| + if !absent { |
| + count++ |
| + } |
| visit := registry.HADepartureVisit{RecordDigest: digest} |
| s.Visited[h.Host] = visit |
| block := func() { |
| @@ -185,6 +210,9 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| blockers = append(blockers, h) |
| continue |
| } |
| + if absent { |
| + continue |
| + } |
| if !s.Rescan && seen && old.RecordDigest == scanDigest(h) && !old.NotBefore.IsZero() && !c.now().After(old.NotBefore) { |
| wait(old.NotBefore) |
| continue |
| @@ -333,6 +361,7 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| s.Waiting = 0 |
| s.NotBefore = time.Time{} |
| s.Visited = map[string]registry.HADepartureVisit{} |
| + s.LockPrefixes, s.LocksListed = nil, false |
| } |
| } |
| if err = c.cfg.Registry.SaveDepartureScan(ctx, d.ID, phase, s); err != nil { |
diff --git a/internal/host/hadepartures_scan_test.go b/internal/host/hadepartures_scan_test.go| index d85696040..c95022fdc 100644 |
| --- a/internal/host/hadepartures_scan_test.go |
| +++ b/internal/host/hadepartures_scan_test.go |
| @@ -671,23 +671,25 @@ func TestDepartureProductionImporter(t *testing.T) { |
| constructors++ |
| return reader(ctx, region) |
| } |
| - for page, want := range []struct{ gets, puts int }{{193, 64}, {6, 2}, {32, 0}, {1, 0}} { |
| + for page, want := range []struct{ gets, puts, lists int }{{193, 64, 1}, {6, 2, 0}, {32, 0, 1}, {1, 0, 0}} { |
| before := len(fx.Requests()) |
| if _, err = c.ScanPass(t.Context()); err != nil { |
| t.Fatal(err) |
| } |
| - gets, puts := 0, 0 |
| + gets, puts, lists := 0, 0, 0 |
| for _, request := range fx.Requests()[before:] { |
| if strings.HasPrefix(request, "GET ") { |
| gets++ |
| } else if strings.HasPrefix(request, "PUT ") { |
| puts++ |
| + } else if request == "LIST instances/" { |
| + lists++ |
| } else { |
| t.Fatal("unexpected request", request) |
| } |
| } |
| - if gets != want.gets || puts != want.puts { |
| - t.Fatalf("page %d: GET=%d PUT=%d want %+v", page, gets, puts, want) |
| + if gets != want.gets || puts != want.puts || lists != want.lists { |
| + t.Fatalf("page %d: GET=%d PUT=%d LIST=%d want %+v", page, gets, puts, lists, want) |
| } |
| now = now.Add(31 * time.Second) |
| } |
diff --git a/internal/lease/ha.go b/internal/lease/ha.go| index 77bdc0837..eb5ffcf33 100644 |
| --- a/internal/lease/ha.go |
| +++ b/internal/lease/ha.go |
| @@ -193,14 +193,17 @@ func (h *Held) View() hafresh.View { return h.view } |
| // ReadLock reads s3://bucket/<prefix>/lock.json with its ETag, through the |
| // client Acquire builds. Only S3's NoSuchKey is Absent. |
| -type LockReader struct{ client lss3.S3API } |
| +type LockReader struct { |
| + client lss3.S3API |
| + lister lister |
| +} |
| func NewLockReader(ctx context.Context, region string) (*LockReader, error) { |
| client, _, err := newLeaserClient(ctx, region) |
| if err != nil { |
| return nil, err |
| } |
| - return &LockReader{client: client}, nil |
| + return &LockReader{client: client, lister: client}, nil |
| } |
| func (r *LockReader) ReadLock(ctx context.Context, bucket, prefix string) (hafresh.View, error) { |
| key, err := lockKey(bucket, prefix) |
| @@ -211,6 +214,27 @@ func (r *LockReader) ReadLock(ctx context.Context, bucket, prefix string) (hafre |
| defer cancel() |
| return readView(ctx, r.client, bucket, key) |
| } |
| + |
| +// ListLocks returns instance prefixes with a lock in this bucket. A partial |
| +// listing is never evidence of absence, so any page failure discards the set. |
| +func (r *LockReader) ListLocks(ctx context.Context, bucket string) (map[string]bool, error) { |
| + locks := map[string]bool{} |
| + pager := awss3.NewListObjectsV2Paginator(r.lister, &awss3.ListObjectsV2Input{Bucket: aws.String(bucket), Prefix: aws.String("instances/")}) |
| + for pager.HasMorePages() { |
| + page, err := pager.NextPage(ctx) |
| + if err != nil { |
| + return nil, err |
| + } |
| + for _, object := range page.Contents { |
| + key := aws.ToString(object.Key) |
| + host := strings.TrimSuffix(strings.TrimPrefix(key, "instances/"), "/lock.json") |
| + if host != "" && !strings.Contains(host, "/") && key == "instances/"+host+"/lock.json" { |
| + locks["instances/"+host] = true |
| + } |
| + } |
| + } |
| + return locks, nil |
| +} |
| func ReadLock(ctx context.Context, bucket, prefix, region string) (hafresh.View, error) { |
| if _, err := lockKey(bucket, prefix); err != nil { |
| return hafresh.View{}, err |
diff --git a/internal/lease/ha_list_test.go b/internal/lease/ha_list_test.go| new file mode 100644 |
| index 000000000..1dd3436ce |
| --- /dev/null |
| +++ b/internal/lease/ha_list_test.go |
| @@ -0,0 +1,49 @@ |
| +package lease_test |
| + |
| +import ( |
| + "fmt" |
| + "net/http" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/lease/locktest" |
| +) |
| + |
| +func TestListDepartureLocks(t *testing.T) { |
| + for _, failPage := range []int{0, 1, 2} { |
| + t.Run(fmt.Sprint(failPage), func(t *testing.T) { |
| + f := locktest.NewLockFixture(t) |
| + for i := range 1001 { |
| + f.PutLock(fmt.Sprintf("instances/h-%04d.example.com", i), []byte("lock")) |
| + } |
| + for _, key := range []string{"instances/h/files/0/lock.json", "instances/h/lock.json.bak", "instances/lock.json", "other/h/lock.json"} { |
| + f.PutObject(key, []byte("unrelated")) |
| + } |
| + r, err := lease.NewLockReader(t.Context(), "eu-west-1") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if failPage != 0 { |
| + f.FailNth("LIST", failPage, http.StatusForbidden) |
| + } |
| + keys, err := r.ListLocks(t.Context(), f.Bucket) |
| + if failPage != 0 { |
| + if err == nil || keys != nil { |
| + t.Fatalf("partial listing escaped: keys=%d err=%v", len(keys), err) |
| + } |
| + return |
| + } |
| + if err != nil || len(keys) != 1001 { |
| + t.Fatalf("listing keys=%d err=%v", len(keys), err) |
| + } |
| + for i := range 1001 { |
| + if !keys[fmt.Sprintf("instances/h-%04d.example.com", i)] { |
| + t.Fatal("missing lock", i) |
| + } |
| + } |
| + if len(f.Requests()) != 2 { |
| + t.Fatal("pagination", f.Requests()) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/registry/ha_scan.go b/internal/registry/ha_scan.go| index 8f2523914..855834e80 100644 |
| --- a/internal/registry/ha_scan.go |
| +++ b/internal/registry/ha_scan.go |
| @@ -32,6 +32,9 @@ type HADepartureScan struct { |
| Waiting int |
| NotBefore time.Time |
| Visited map[string]HADepartureVisit |
| + LockPrefixes map[string]bool |
| + LocksListed bool |
| + LockListAlerted bool |
| Hosts map[string]HADepartureHostScan |
| DeleteUncertain bool |
| EdgesETag string |