ha: measure suspect owners from lease expiry and expose page errors
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
10 files changed,
+435
−46
internal/activator/hasamebox.go+0 −2internal/activator/hasuspectholder.go+49 −11internal/activator/hasuspectholder_test.go+215 −25internal/activator/hatakeover.go+0 −3internal/activator/hawake.go+6 −1internal/hafresh/acquire.go+2 −0internal/host/hadepartures.go+3 −3internal/host/hadepartures_page_followups_test.go+61 −1internal/lease/ha.go+3 −0internal/lease/ha_observe_test.go+96 −0
diff --git a/internal/activator/hasamebox.go b/internal/activator/hasamebox.go| index 2fa1050ec..92a03ba37 100644 |
| --- a/internal/activator/hasamebox.go |
| +++ b/internal/activator/hasamebox.go |
| @@ -10,7 +10,6 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/cgroup" |
| - "github.com/carlosframework/platform/internal/lease" |
| "github.com/carlosframework/platform/internal/registry" |
| ) |
| @@ -70,7 +69,6 @@ func (b *ExecBackend) sameBoxEmpty(ctx context.Context, rt registry.Route) func( |
| return func() (bool, error) { |
| if err := b.sameBoxProven(rt); err != nil { |
| b.sameBox.alarm(rt.Host, err) |
| - b.note(rt.Host, lease.CurrentSelf().IID, true, b.suspectHolders.clock()) |
| return false, err |
| } |
| log.Printf("activator: %s: same-box recovery: the startup sweep proved every unit stopped and the host's slice reads empty", rt.Host) |
diff --git a/internal/activator/hasuspectholder.go b/internal/activator/hasuspectholder.go| index 162d18b40..1beaba249 100644 |
| --- a/internal/activator/hasuspectholder.go |
| +++ b/internal/activator/hasuspectholder.go |
| @@ -10,16 +10,19 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/alerting" |
| + "github.com/carlosframework/platform/internal/hafresh" |
| "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/registry" |
| ) |
| const suspectHolderAfter = 10 * time.Minute |
| const suspectHolderFresh = 5 * time.Minute |
| type suspectHolder struct { |
| - first, last time.Time |
| - ownerIID string |
| - crossed bool |
| + expiresAt, last time.Time |
| + ownerIID string |
| + generation int64 |
| + crossed bool |
| } |
| type suspectHolderState struct { |
| @@ -42,7 +45,37 @@ func (s *suspectHolderState) clear(host string) { |
| delete(s.hosts, host) |
| } |
| -func (b *ExecBackend) note(host, ownerIID string, sameBox bool, now time.Time) { |
| +func (b *ExecBackend) observeHARefusal(ctx context.Context, rt registry.Route) func(hafresh.View, hafresh.Decision) { |
| + if _, finalizer := ctx.Value(adoptionCauseKey{}).(string); finalizer { |
| + return nil |
| + } |
| + return func(v hafresh.View, d hafresh.Decision) { |
| + owner, ok := hafresh.ParseOwner(v.Lock.Owner) |
| + if !ok || !owner.HA || owner.IID == "" || v.Absent || v.Lock.Release != nil { |
| + return |
| + } |
| + if d.Action == hafresh.ActHeld { |
| + b.suspectHolders.clear(rt.Host) |
| + return |
| + } |
| + if d.Action != hafresh.ActRefuse { |
| + return |
| + } |
| + sameBox := owner.IID == lease.CurrentSelf().IID |
| + reason := hafresh.ReasonOtherExpired |
| + if sameBox { |
| + reason = hafresh.ReasonSameBoxUnsure |
| + } else if b.OwnerTerminated == nil && b.TerminationProofs == nil { |
| + return |
| + } |
| + if d.Reason != reason && !strings.HasPrefix(d.Reason, reason+": ") { |
| + return |
| + } |
| + b.note(rt.Host, owner.IID, sameBox, v.Lock.ExpiresAt, v.Lock.Generation, b.suspectHolders.clock()) |
| + } |
| +} |
| + |
| +func (b *ExecBackend) note(host, ownerIID string, sameBox bool, expiresAt time.Time, generation int64, now time.Time) { |
| if ownerIID == "" { |
| return |
| } |
| @@ -62,12 +95,17 @@ func (b *ExecBackend) note(host, ownerIID string, sameBox bool, now time.Time) { |
| delete(s.paged, iid) |
| } |
| } |
| + if expiresAt.IsZero() || !expiresAt.Before(now) { |
| + delete(s.hosts, host) |
| + s.mu.Unlock() |
| + return |
| + } |
| entry, seen := s.hosts[host] |
| - if !seen || entry.ownerIID != ownerIID { |
| - entry = suspectHolder{first: now, ownerIID: ownerIID} |
| + if !seen || entry.ownerIID != ownerIID || entry.generation != generation || !entry.expiresAt.Equal(expiresAt) { |
| + entry = suspectHolder{expiresAt: expiresAt, ownerIID: ownerIID, generation: generation} |
| } |
| entry.last = now |
| - eligible := now.Sub(entry.first) >= suspectHolderAfter |
| + eligible := now.Sub(entry.expiresAt) >= suspectHolderAfter |
| crossing := eligible && !entry.crossed |
| entry.crossed = entry.crossed || crossing |
| s.hosts[host] = entry |
| @@ -79,7 +117,7 @@ func (b *ExecBackend) note(host, ownerIID string, sameBox bool, now time.Time) { |
| } |
| s.paged[ownerIID] = now |
| for h, candidate := range s.hosts { |
| - if candidate.ownerIID == ownerIID && now.Sub(candidate.first) >= suspectHolderAfter && now.Sub(candidate.last) < suspectHolderFresh { |
| + if candidate.ownerIID == ownerIID && now.Sub(candidate.expiresAt) >= suspectHolderAfter && now.Sub(candidate.last) < suspectHolderFresh { |
| hosts = append(hosts, h) |
| } |
| } |
| @@ -120,7 +158,7 @@ func (b *ExecBackend) SuspectHolderHosts(now time.Time) int { |
| defer s.mu.Unlock() |
| n := 0 |
| for _, entry := range s.hosts { |
| - if now.Sub(entry.first) >= suspectHolderAfter && now.Sub(entry.last) < suspectHolderFresh { |
| + if now.Sub(entry.expiresAt) >= suspectHolderAfter && now.Sub(entry.last) < suspectHolderFresh { |
| n++ |
| } |
| } |
| @@ -130,6 +168,6 @@ func (b *ExecBackend) SuspectHolderHosts(now time.Time) int { |
| const suspectHolderLog = "activator: ALERT %s: the host has been refused for over 10 minutes. Its lease belongs to instance %s, which is not confirmed terminated. (ha_suspect_holder)" |
| const suspectHolderSameBoxLog = "activator: ALERT %s: the host has been refused for over 10 minutes. This box could not prove its earlier processes for it have stopped. (ha_suspect_holder_same_box)" |
| const suspectHolderSubject = "Instance %s is holding hosts it does not serve." |
| -const suspectHolderBody = "Instance %s let its leases expire and is not confirmed terminated. These hosts have been down for over 10 minutes: %s. If the instance is in an Auto Scaling group, terminate it in its group. The hosts then move to a surviving box. If it is a fixed box, check its storage access and its agent first. This page repeats every hour while the hosts stay down. (ha_suspect_holder)" |
| +const suspectHolderBody = "Instance %s has an expired lease and is not confirmed terminated. These hosts have leases that expired at least 10 minutes ago: %s. If the instance is in an Auto Scaling group, terminate it in its group. If it is a fixed box, check its storage access and agent first. This page repeats every hour while the hosts remain refused. (ha_suspect_holder)" |
| const suspectHolderSameBoxSubject = "Box %s cannot recover hosts after a restart." |
| -const suspectHolderSameBoxBody = "This box could not prove that its earlier processes for these hosts have stopped: %s. No other box may take them, so they have been down for over 10 minutes. If the box is in an Auto Scaling group, terminate it in its group. If it is a fixed box, check the agent's startup sweep and cgroup delegation. This page repeats every hour while the hosts stay down. (ha_suspect_holder_same_box)" |
| +const suspectHolderSameBoxBody = "This box could not prove that its earlier processes for these hosts have stopped: %s. Their leases expired at least 10 minutes ago. No other box may take them without proof. If this box is in an Auto Scaling group, terminate it in its group. If it is a fixed box, check the agent's startup sweep and cgroup delegation. This page repeats every hour while recovery remains refused. (ha_suspect_holder_same_box)" |
diff --git a/internal/activator/hasuspectholder_test.go b/internal/activator/hasuspectholder_test.go| index 28a1fe4f2..5f20c21b3 100644 |
| --- a/internal/activator/hasuspectholder_test.go |
| +++ b/internal/activator/hasuspectholder_test.go |
| @@ -61,9 +61,10 @@ func suspectRefuse(t *testing.T, b *ExecBackend, rt registry.Route) { |
| func TestSuspectHolderThresholdAndHourly(t *testing.T) { |
| logs := captureLog(t) |
| - b, rt, _ := takeoverBackend(t, time.Now().Add(-time.Minute), &terminatedOracle{}) |
| - now := time.Now() |
| - start := now |
| + startExpiry := time.Now().Add(-time.Hour).Truncate(time.Second) |
| + b, rt, _ := takeoverBackend(t, startExpiry, &terminatedOracle{}) |
| + now := startExpiry.Add(time.Second) |
| + start := startExpiry |
| b.suspectHolders.now = func() time.Time { return now } |
| var pages suspectTestPages |
| pages.wire(t, b) |
| @@ -115,7 +116,7 @@ func TestSuspectHolderGroupingAndLimit(t *testing.T) { |
| var pages suspectTestPages |
| pages.wire(t, b) |
| report := func(host, owner string) { |
| - b.reportTakeover(context.Background(), registry.Route{Host: host}, owner, false, nil) |
| + b.note(host, owner, false, start, 1, now) |
| } |
| for i := 11; i >= 0; i-- { |
| report(fmt.Sprintf("host-%02d.example.com", i), "i-b") |
| @@ -169,9 +170,10 @@ func TestSuspectHolderRecoveryClears(t *testing.T) { |
| if n := b.SuspectHolderHosts(now); n != 0 { |
| t.Fatalf("successful acquire retained %d suspects", n) |
| } |
| - b.reportTakeover(context.Background(), rt, "i-c", false, nil) |
| + expiry := now |
| + b.note(rt.Host, "i-c", false, expiry, 1, now) |
| now = now.Add(9*time.Minute + 59*time.Second) |
| - b.reportTakeover(context.Background(), rt, "i-c", false, nil) |
| + b.note(rt.Host, "i-c", false, expiry, 1, now) |
| if got := len(pages.snapshot()); got != 1 { |
| t.Fatalf("new owner paged early: %d", got) |
| } |
| @@ -189,21 +191,22 @@ func TestSuspectHolderResetOwnerAndGap(t *testing.T) { |
| var pages suspectTestPages |
| pages.wire(t, b) |
| rt := registry.Route{Host: "host.example.com"} |
| - b.reportTakeover(context.Background(), rt, "i-b", false, nil) |
| + b.note(rt.Host, "i-b", false, start.Add(-time.Second), 1, now) |
| owner := "i-b" |
| now = start.Add(30 * time.Minute) |
| if changeOwner { |
| owner = "i-c" |
| now = start.Add(9 * time.Minute) |
| } |
| - b.reportTakeover(context.Background(), rt, owner, false, nil) |
| + expiry := now |
| + b.note(rt.Host, owner, false, expiry, 2, now) |
| now = now.Add(9*time.Minute + 59*time.Second) |
| - b.reportTakeover(context.Background(), rt, owner, false, nil) |
| + b.note(rt.Host, owner, false, expiry, 2, now) |
| if len(pages.snapshot()) != 0 { |
| t.Fatal("reset clock paged early") |
| } |
| now = now.Add(time.Second) |
| - b.reportTakeover(context.Background(), rt, owner, false, nil) |
| + b.note(rt.Host, owner, false, expiry, 2, now) |
| if len(pages.snapshot()) != 1 { |
| t.Fatal("reset clock failed to page") |
| } |
| @@ -228,9 +231,10 @@ func TestSuspectHolderFinalizerExcludedAndConfirmedClear(t *testing.T) { |
| if b.SuspectHolderHosts(now) != 0 { |
| t.Fatal("finalizer tracked as suspect") |
| } |
| - b.reportTakeover(context.Background(), rt, "i-b", false, nil) |
| + expiry := now |
| + b.note(rt.Host, "i-b", false, expiry, 1, now) |
| now = now.Add(10 * time.Minute) |
| - b.reportTakeover(context.Background(), rt, "i-b", false, nil) |
| + b.note(rt.Host, "i-b", false, expiry, 1, now) |
| if b.SuspectHolderHosts(now) != 1 { |
| t.Fatal("visitor not tracked") |
| } |
| @@ -244,21 +248,21 @@ func TestSuspectHolderSameBox(t *testing.T) { |
| logs := captureLog(t) |
| b, rt := haBackend(t, "host.example.com") |
| b.StartupSwept = func() bool { return false } |
| - now := time.Now() |
| + expiry := time.Now().Add(-time.Hour).Truncate(time.Second) |
| + now := expiry.Add(time.Second) |
| + putLockFor(t, b, rt, leaseBody(t, hafresh.OwnerFor(lease.CurrentSelf(), 7, true), expiry)) |
| b.suspectHolders.now = func() time.Time { return now } |
| var pages suspectTestPages |
| pages.wire(t, b) |
| before := len(haFixOf(b).fx.Requests()) |
| for _, d := range []time.Duration{0, 10 * time.Minute} { |
| now = now.Add(d) |
| - if ok, err := b.sameBoxEmpty(context.Background(), rt)(); ok || err == nil { |
| - t.Fatal("same-box refusal changed") |
| - } |
| + suspectRefuse(t, b, rt) |
| } |
| if got := pages.snapshot(); len(got) != 1 || got[0].subject != "Box i-a cannot recover hosts after a restart." || !strings.Contains(got[0].body, "ha_suspect_holder_same_box") { |
| t.Fatalf("same-box pages=%+v", got) |
| } |
| - if len(haFixOf(b).fx.Requests()) != before || lockWrites(t, b, rt) != 0 { |
| + if len(haFixOf(b).fx.Requests()) != before+2 || lockWrites(t, b, rt) != 0 { |
| t.Fatal("same-box tracking added storage requests") |
| } |
| if strings.Count(logs.String(), "(ha_suspect_holder_same_box)") != 1 { |
| @@ -275,11 +279,17 @@ func TestSuspectHolderConcurrentSpentContext(t *testing.T) { |
| rt := registry.Route{Host: "host.example.com"} |
| ctx, cancel := context.WithCancel(context.Background()) |
| cancel() |
| - b.reportTakeover(ctx, rt, "i-b", false, context.Canceled) |
| + b.OwnerTerminated = func(context.Context, string) (bool, error) { return false, context.Canceled } |
| + view := hafresh.View{Lock: hafresh.Lock{Owner: heldByB, Generation: 1, ExpiresAt: now}} |
| + observe := b.observeHARefusal(ctx, rt) |
| + observe(view, hafresh.Decision{Action: hafresh.ActRefuse, Reason: hafresh.ReasonOtherExpired}) |
| now = now.Add(10 * time.Minute) |
| var wg sync.WaitGroup |
| for range 20 { |
| - wg.Go(func() { b.reportTakeover(ctx, rt, "i-b", false, context.Canceled); b.SuspectHolderHosts(now) }) |
| + wg.Go(func() { |
| + observe(view, hafresh.Decision{Action: hafresh.ActRefuse, Reason: hafresh.ReasonOtherExpired}) |
| + b.SuspectHolderHosts(now) |
| + }) |
| } |
| wg.Wait() |
| if got := len(pages.snapshot()); got != 1 { |
| @@ -290,19 +300,21 @@ func TestSuspectHolderConcurrentSpentContext(t *testing.T) { |
| func TestSuspectHolderNilPageStillCounts(t *testing.T) { |
| b := &ExecBackend{ServedPools: eligibleSharedPools} |
| now := time.Now() |
| + expiry := now |
| b.suspectHolders.now = func() time.Time { return now } |
| rt := registry.Route{Host: "host.example.com"} |
| - b.reportTakeover(context.Background(), rt, "i-b", false, nil) |
| + b.note(rt.Host, "i-b", false, expiry, 1, now) |
| now = now.Add(10 * time.Minute) |
| - b.reportTakeover(context.Background(), rt, "i-b", false, nil) |
| + b.note(rt.Host, "i-b", false, expiry, 1, now) |
| if b.SuspectHolderHosts(now) != 1 { |
| t.Fatal("nil page lost suspect") |
| } |
| } |
| func TestSuspectHolderAcquireBetweenRefusals(t *testing.T) { |
| - b, rt, _ := takeoverBackend(t, time.Now().Add(-time.Minute), &terminatedOracle{}) |
| - now := time.Now() |
| + startExpiry := time.Now().Add(-time.Hour).Truncate(time.Second) |
| + b, rt, _ := takeoverBackend(t, startExpiry, &terminatedOracle{}) |
| + now := startExpiry.Add(time.Second) |
| start := now |
| b.suspectHolders.now = func() time.Time { return now } |
| var pages suspectTestPages |
| @@ -316,7 +328,7 @@ func TestSuspectHolderAcquireBetweenRefusals(t *testing.T) { |
| t.Fatal(err) |
| } |
| releaseHALease(t, l) |
| - putLockFor(t, b, rt, leaseBody(t, heldByB, time.Now().Add(-time.Minute))) |
| + putLockFor(t, b, rt, leaseBody(t, heldByB, start.Add(5*time.Minute))) |
| writes := lockWrites(t, b, rt) |
| for _, d := range []time.Duration{5 * time.Minute, 10 * time.Minute} { |
| now = start.Add(d) |
| @@ -347,7 +359,11 @@ func TestSuspectHolderPageListsOnlySustainedRecentRefusals(t *testing.T) { |
| var pages suspectTestPages |
| pages.wire(t, b) |
| report := func(host string) { |
| - b.reportTakeover(context.Background(), registry.Route{Host: host}, "i-b", false, nil) |
| + expiry := start.Add(-time.Nanosecond) |
| + if host == "young.example.com" { |
| + expiry = now.Add(-time.Nanosecond) |
| + } |
| + b.note(host, "i-b", false, expiry, 1, now) |
| } |
| report("stale.example.com") |
| report("active.example.com") |
| @@ -360,3 +376,177 @@ func TestSuspectHolderPageListsOnlySustainedRecentRefusals(t *testing.T) { |
| t.Fatalf("page hosts=%+v", got) |
| } |
| } |
| + |
| +func TestSuspectHolderUsesObservedExpiry(t *testing.T) { |
| + for _, adapter := range []bool{false, true} { |
| + for _, late := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("adapter=%t/late=%t", adapter, late), func(t *testing.T) { |
| + expiry := time.Now().Add(-time.Hour).Truncate(time.Second) |
| + oracle := &terminatedOracle{} |
| + b, rt, _ := takeoverBackend(t, expiry, oracle) |
| + if adapter { |
| + b.TerminationProofs = &lease.TerminationProofAdapter{} |
| + } |
| + before := len(haFixOf(b).fx.Requests()) |
| + now := expiry.Add(4*time.Minute + 13*time.Second) |
| + b.suspectHolders.now = func() time.Time { return now } |
| + var pages suspectTestPages |
| + pages.wire(t, b) |
| + if late { |
| + now = expiry.Add(14 * time.Minute) |
| + } else { |
| + suspectRefuse(t, b, rt) |
| + now = expiry.Add(9*time.Minute + 59*time.Second) |
| + suspectRefuse(t, b, rt) |
| + if len(pages.snapshot()) != 0 || b.SuspectHolderHosts(now) != 0 { |
| + t.Fatal("paged before expiry + 10 minutes") |
| + } |
| + now = expiry.Add(10 * time.Minute) |
| + } |
| + suspectRefuse(t, b, rt) |
| + if len(pages.snapshot()) != 1 || b.SuspectHolderHosts(now) != 1 { |
| + t.Fatalf("expiry-based threshold: pages=%d count=%d", len(pages.snapshot()), b.SuspectHolderHosts(now)) |
| + } |
| + probes := 3 |
| + if late { |
| + probes = 1 |
| + } |
| + if len(haFixOf(b).fx.Requests())-before != probes { |
| + t.Fatal("tracker added storage reads") |
| + } |
| + if !adapter && len(oracle.calls()) != probes { |
| + t.Fatal("tracker added termination reads") |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| +func TestSuspectHolderUsesObservedExpiryChanges(t *testing.T) { |
| + for _, change := range []string{"owner", "generation", "renewal", "zero", "future"} { |
| + t.Run(change, func(t *testing.T) { |
| + b := &ExecBackend{} |
| + var pages suspectTestPages |
| + pages.wire(t, b) |
| + expiry := time.Now().Add(-time.Hour) |
| + now := expiry.Add(9 * time.Minute) |
| + b.note("host", "i-b", false, expiry, 1, now) |
| + owner, gen, next := "i-b", int64(1), expiry.Add(8*time.Minute) |
| + switch change { |
| + case "owner": |
| + owner = "i-c" |
| + case "generation": |
| + gen = 2 |
| + case "zero": |
| + next = time.Time{} |
| + case "future": |
| + next = now.Add(time.Hour) |
| + } |
| + b.note("host", owner, false, next, gen, now) |
| + now = expiry.Add(10 * time.Minute) |
| + b.note("host", owner, false, next, gen, now) |
| + if len(pages.snapshot()) != 0 || b.SuspectHolderHosts(now) != 0 { |
| + t.Fatal("earlier lease deadline survived") |
| + } |
| + if change == "zero" || change == "future" { |
| + if len(b.suspectHolders.hosts) != 0 { |
| + t.Fatal("invalid/future expiry retained") |
| + } |
| + return |
| + } |
| + now = next.Add(10 * time.Minute) |
| + b.note("host", owner, false, next, gen, now) |
| + if len(pages.snapshot()) != 1 || b.SuspectHolderHosts(now) != 1 { |
| + t.Fatal("replacement lease did not page at its expiry + 10m") |
| + } |
| + if entry := b.suspectHolders.hosts["host"]; entry.ownerIID != owner || entry.generation != gen || !entry.expiresAt.Equal(next) { |
| + t.Fatalf("entry=%+v", entry) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestSuspectHolderUsesObservedExpiryExclusions(t *testing.T) { |
| + for _, mode := range []string{"pool", "capacity", "legacy", "malformed-marker", "missing-iid", "zero-expiry", "held", "no-proof", "finalizer"} { |
| + t.Run(mode, func(t *testing.T) { |
| + expiry := time.Now().Add(-time.Hour).Truncate(time.Second) |
| + oracle := &terminatedOracle{} |
| + b, rt, _ := takeoverBackend(t, expiry, oracle) |
| + now := expiry.Add(14 * time.Minute) |
| + b.suspectHolders.now = func() time.Time { return now } |
| + var pages suspectTestPages |
| + pages.wire(t, b) |
| + ctx := context.Background() |
| + switch mode { |
| + case "pool": |
| + b.ServedPools = nil |
| + rt.Pool = "hapair" |
| + case "capacity": |
| + ctx = context.WithValue(ctx, dutyContextKey{}, dutyRestriction{allow: func(hafresh.View) error { return errors.New("capacity refused") }}) |
| + case "legacy": |
| + putLockFor(t, b, rt, leaseBody(t, "old:123", expiry)) |
| + case "malformed-marker": |
| + putLockFor(t, b, rt, leaseBody(t, "released/v1;ref=b;iid=i-b", expiry)) |
| + case "missing-iid": |
| + putLockFor(t, b, rt, leaseBody(t, "box/v1;ref=b;pid=7;ha=1", expiry)) |
| + case "zero-expiry": |
| + putLockFor(t, b, rt, leaseBody(t, heldByB, time.Time{})) |
| + case "held": |
| + putLockFor(t, b, rt, leaseBody(t, heldByB, time.Now().Add(time.Hour))) |
| + case "no-proof": |
| + b.OwnerTerminated = nil |
| + case "finalizer": |
| + ctx = context.WithValue(ctx, adoptionCauseKey{}, "b") |
| + } |
| + for range 2 { |
| + if l, err := b.Acquire(ctx, rt); err == nil { |
| + releaseHALease(t, l) |
| + t.Fatal("excluded refusal acquired") |
| + } |
| + now = now.Add(11 * time.Minute) |
| + } |
| + if len(b.suspectHolders.hosts) != 0 || b.SuspectHolderHosts(now) != 0 { |
| + t.Fatal("excluded refusal entered visitor tracker") |
| + } |
| + wantPages := 0 |
| + if mode == "finalizer" { |
| + wantPages = 1 |
| + } |
| + if len(pages.snapshot()) != wantPages { |
| + t.Fatalf("pages=%+v", pages.snapshot()) |
| + } |
| + if lockWrites(t, b, rt) != 0 { |
| + t.Fatal("refusal wrote lease") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestSuspectHolderIdleEvictionAndHeldRenewal(t *testing.T) { |
| + b := &ExecBackend{} |
| + expiry := time.Now().Add(-time.Hour) |
| + b.note("idle", "i-b", false, expiry, 1, expiry.Add(time.Second)) |
| + b.note("active", "i-c", false, expiry, 1, expiry.Add(30*time.Minute+time.Second)) |
| + if _, ok := b.suspectHolders.hosts["idle"]; ok { |
| + t.Fatal("idle entry retained") |
| + } |
| + view := hafresh.View{Lock: hafresh.Lock{Owner: heldByB, Generation: 2, ExpiresAt: time.Now().Add(time.Hour)}} |
| + b.observeHARefusal(context.Background(), registry.Route{Host: "active"})(view, hafresh.Decision{Action: hafresh.ActHeld, Reason: heldByB}) |
| + if len(b.suspectHolders.hosts) != 0 { |
| + t.Fatal("renewed held lease retained expired suspicion") |
| + } |
| +} |
| + |
| +func TestSuspectHolderGenerationChangesWithoutExpiryChange(t *testing.T) { |
| + b := &ExecBackend{} |
| + expiry := time.Now().Add(-time.Hour) |
| + now := expiry.Add(9 * time.Minute) |
| + b.note("host", "i-b", false, expiry, 1, now) |
| + b.note("host", "i-b", false, expiry, 2, now) |
| + if entry := b.suspectHolders.hosts["host"]; entry.generation != 2 || !entry.expiresAt.Equal(expiry) { |
| + t.Fatalf("entry=%+v", entry) |
| + } |
| + if b.SuspectHolderHosts(now) != 0 { |
| + t.Fatal("generation change caused early count") |
| + } |
| +} |
diff --git a/internal/activator/hatakeover.go b/internal/activator/hatakeover.go| index cf7ecfacf..fca98fef8 100644 |
| --- a/internal/activator/hatakeover.go |
| +++ b/internal/activator/hatakeover.go |
| @@ -47,9 +47,6 @@ func (b *ExecBackend) reportTakeover(ctx context.Context, rt registry.Route, iid |
| } |
| if !gone || err != nil { |
| b.takeover.alarm(rt.Host, iid, cause, err) |
| - if cause == "visitor" { |
| - b.note(rt.Host, iid, false, b.suspectHolders.clock()) |
| - } |
| if finalizer && b.Page != nil && b.takeover.claimPage(rt.Host, time.Now()) { |
| if ref == "" { |
| ref = iid |
diff --git a/internal/activator/hawake.go b/internal/activator/hawake.go| index 5026c4ae7..aeb8f7a37 100644 |
| --- a/internal/activator/hawake.go |
| +++ b/internal/activator/hawake.go |
| @@ -399,7 +399,12 @@ func (b *ExecBackend) acquireClassified(ctx context.Context, rt registry.Route) |
| } |
| } |
| - proofs := hafresh.Proofs{LocalEligible: func() bool { return b.poolEligible(rt) }, SameBoxEmpty: b.sameBoxEmpty(ctx, rt), OwnerTerminated: b.ownerTerminated(ctx, rt)} |
| + proofs := hafresh.Proofs{ |
| + LocalEligible: func() bool { return b.poolEligible(rt) }, |
| + SameBoxEmpty: b.sameBoxEmpty(ctx, rt), |
| + OwnerTerminated: b.ownerTerminated(ctx, rt), |
| + ObserveRefusal: b.observeHARefusal(ctx, rt), |
| + } |
| if b.TerminationProofs != nil { |
| adapter := *b.TerminationProofs |
| adapter.Observe = func(iid string, gone bool, err error) { b.reportTakeover(ctx, rt, iid, gone, err) } |
diff --git a/internal/hafresh/acquire.go b/internal/hafresh/acquire.go| index 3a224994d..5f1a0cdb9 100644 |
| --- a/internal/hafresh/acquire.go |
| +++ b/internal/hafresh/acquire.go |
| @@ -44,6 +44,8 @@ type Proofs struct { |
| LocalEligible func() bool |
| SameBoxEmpty func() (bool, error) |
| OwnerTerminated func(iid string) (bool, error) |
| + // ObserveRefusal receives the classified view after an acquire round refuses. |
| + ObserveRefusal func(View, Decision) |
| } |
| // Refusal reasons, matched by callers and tests. |
diff --git a/internal/host/hadepartures.go b/internal/host/hadepartures.go| index 4191ebee4..730b7f20d 100644 |
| --- a/internal/host/hadepartures.go |
| +++ b/internal/host/hadepartures.go |
| @@ -449,7 +449,7 @@ func (c *Controller) step(ctx context.Context) error { |
| c.logf("ha_departure_pending code=%s sample_iid=%s", code, h.IID) |
| if c.cfg.PageIdentity != nil { |
| if e = c.cfg.PageIdentity(ctx, h, code); e != nil { |
| - c.logf("ha_departure_page code=failed") |
| + c.logf("ha_departure_page code=failed iid=%s error=%v", h.IID, e) |
| } |
| } |
| } |
| @@ -465,7 +465,7 @@ func (c *Controller) step(ctx context.Context) error { |
| c.logf("ha_departure_pending code=%s sample_iid=%s", code, iid) |
| if c.cfg.PageIdentity != nil { |
| if e = c.cfg.PageIdentity(ctx, h, code); e != nil { |
| - c.logf("ha_departure_page code=failed") |
| + c.logf("ha_departure_page code=failed ref=%s iid=%s error=%v", ref, iid, e) |
| } |
| } |
| } |
| @@ -527,7 +527,7 @@ func (c *Controller) step(ctx context.Context) error { |
| c.logf("ha_departure_blocked id=%s code=legacy_sidecar", d.ID) |
| if c.cfg.Page != nil { |
| if err = c.cfg.Page(ctx, d, blockers); err != nil { |
| - c.logf("ha_departure_page code=failed") |
| + c.logf("ha_departure_page code=failed id=%s iid=%s error=%v", d.ID, d.Box.InstanceID, err) |
| } |
| } |
| } |
diff --git a/internal/host/hadepartures_page_followups_test.go b/internal/host/hadepartures_page_followups_test.go| index f6d1b87ab..17efdbab5 100644 |
| --- a/internal/host/hadepartures_page_followups_test.go |
| +++ b/internal/host/hadepartures_page_followups_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "database/sql" |
| "encoding/json" |
| "errors" |
| + "fmt" |
| "path/filepath" |
| "strings" |
| "testing" |
| @@ -107,7 +108,7 @@ func TestTerminationFinalizerPagesThroughWiring(t *testing.T) { |
| return nil, errors.New("unavailable") |
| } |
| a.Backend.OwnerTerminated = a.Confirmer.Terminated |
| - body, _ := json.Marshal(hafresh.Lock{Generation: 5, Owner: hafresh.OwnerFor(hafresh.Self{Ref: "dead", IID: "i-1123456789abcdef0"}, 1, true), ExpiresAt: time.Now().Add(-time.Hour)}) |
| + body, _ := json.Marshal(hafresh.Lock{Generation: 5, Owner: hafresh.OwnerFor(hafresh.Self{Ref: "dead", IID: "i-1123456789abcdef0"}, 1, true), ExpiresAt: time.Now().Add(-time.Minute)}) |
| fx.PutLock("instances/"+rt.Host, body) |
| type page struct{ Level, Subject, Body string } |
| var pages []page |
| @@ -159,3 +160,62 @@ func TestTerminationFinalizerPagesThroughWiring(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +func TestDeparturePageFailureIncludesCause(t *testing.T) { |
| + for _, site := range []string{"pending-identity", "replacement", "departure"} { |
| + t.Run(site, func(t *testing.T) { |
| + x := newScanRig(t, 0) |
| + repo := bindScanIdentity(t, x, "fixed", x.d.Box.InstanceID) |
| + repo.Now = func() time.Time { return x.now } |
| + x.c.cfg.Identities = repo |
| + x.c.cfg.NewReader = nil |
| + var lines []string |
| + x.c.cfg.Logf = func(format string, args ...any) { lines = append(lines, fmt.Sprintf(format, args...)) } |
| + cause := errors.New("operation error SNS: Publish, AuthorizationError: no identity-based policy allows SNS:Publish") |
| + calls := 0 |
| + x.c.cfg.PageIdentity = func(context.Context, registry.HAPendingTermination, string) error { calls++; return cause } |
| + x.c.cfg.Page = func(context.Context, registry.HADeparture, []provision.DepartureHost) error { calls++; return cause } |
| + iid := "i-2123456789abcdef0" |
| + if site == "departure" { |
| + iid = x.d.Box.InstanceID |
| + x.inv.Records = []provision.DepartureHost{{Host: "side.example.com", Kind: "sidecar"}} |
| + } else { |
| + repo.Fixed["fixed"] = iid |
| + obs, ok, err := repo.Resolve(t.Context(), "fixed", "") |
| + if err != nil || !ok { |
| + t.Fatalf("identity: %v %v", ok, err) |
| + } |
| + obs.PublishedAt = time.Time{} |
| + x.c.view.Peers = []hadeparture.BoxObservation{obs} |
| + if site == "pending-identity" { |
| + h := registry.HAPendingTermination{Account: x.d.Box.Account, Region: repo.Region, IID: iid, ReceivedAt: x.now} |
| + if err := x.r.ParkTermination(t.Context(), h, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + } |
| + if err := x.c.step(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if calls != 1 { |
| + t.Fatalf("page calls=%d logs=%v", calls, lines) |
| + } |
| + var failures []string |
| + for _, line := range lines { |
| + if strings.HasPrefix(line, "ha_departure_page code=failed") { |
| + failures = append(failures, line) |
| + } |
| + } |
| + if len(failures) != 1 || !strings.Contains(failures[0], "error="+cause.Error()) || !strings.Contains(failures[0], "iid="+iid) || site == "departure" && !strings.Contains(failures[0], "id="+x.d.ID) { |
| + t.Fatalf("failure lost cause or identity: %v", failures) |
| + } |
| + x.now = x.now.Add(30 * time.Second) |
| + if err := x.c.step(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if calls != 1 { |
| + t.Fatalf("page failure changed throttle: calls=%d", calls) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/lease/ha.go b/internal/lease/ha.go| index 82901415c..77bdc0837 100644 |
| --- a/internal/lease/ha.go |
| +++ b/internal/lease/ha.go |
| @@ -120,6 +120,9 @@ func acquireHARestricted(ctx context.Context, bucket, prefix, region string, bas |
| proofs := bindTerminationProof(ctx, client, bucket, loc, region, base, me, v, p) |
| d := hafresh.DecideAcquire(v, me, time.Now(), proofs) |
| won = d |
| + if p.ObserveRefusal != nil && (d.Action == hafresh.ActRefuse || d.Action == hafresh.ActHeld) { |
| + p.ObserveRefusal(v, d) |
| + } |
| switch d.Action { |
| case hafresh.ActCreate: |
| empty, err := prefixEmpty(ctx, client, bucket, loc) |
diff --git a/internal/lease/ha_observe_test.go b/internal/lease/ha_observe_test.go| new file mode 100644 |
| index 000000000..547c45ae1 |
| --- /dev/null |
| +++ b/internal/lease/ha_observe_test.go |
| @@ -0,0 +1,96 @@ |
| +package lease_test |
| + |
| +import ( |
| + "errors" |
| + "reflect" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/hafresh" |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/lease/locktest" |
| +) |
| + |
| +func TestAcquireHAObserveRefusal(t *testing.T) { |
| + for _, mode := range []string{"foreign", "same-box", "held", "legacy", "pool", "capacity", "cas"} { |
| + t.Run(mode, func(t *testing.T) { |
| + haBox(t) |
| + fx := locktest.NewLockFixture(t) |
| + owner := hafresh.OwnerFor(hafresh.Self{Ref: "b", IID: "i-b"}, 1, true) |
| + expiry := time.Now().Add(-time.Hour).Truncate(time.Second) |
| + want := hafresh.Decision{Action: hafresh.ActRefuse, Reason: hafresh.ReasonOtherExpired} |
| + switch mode { |
| + case "same-box": |
| + owner = hafresh.OwnerFor(lease.CurrentSelf(), 1, true) |
| + want.Reason = hafresh.ReasonSameBoxUnsure |
| + case "held": |
| + expiry = time.Now().Add(time.Hour).Truncate(time.Second) |
| + want = hafresh.Decision{Action: hafresh.ActHeld, Reason: owner} |
| + case "legacy": |
| + owner = "old:123" |
| + want.Reason = hafresh.ReasonLeftoverLock |
| + } |
| + body := leaseBody(owner, expiry) |
| + expected, err := hafresh.DecodeLock(body, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.PutLock("instances/h", body) |
| + if mode == "cas" { |
| + marker, _ := hafresh.MarkerBody(3, hafresh.Marker{V: 1, Box: "b", IID: "i-b"}) |
| + fx.PutLock("instances/h", marker) |
| + fx.BeforeWrite(func(string, string) { fx.PutLock("instances/h", body) }) |
| + } |
| + calls, proofs := 0, 0 |
| + proof := func() (bool, error) { |
| + proofs++ |
| + // Storage changes during the proof. The callback must still receive |
| + // the view classified in this round, with no additional GET. |
| + fx.PutLock("instances/h", leaseBody(owner, expiry.Add(time.Minute))) |
| + return false, nil |
| + } |
| + p := hafresh.Proofs{ |
| + LocalEligible: func() bool { return mode != "pool" }, |
| + SameBoxEmpty: proof, |
| + OwnerTerminated: func(string) (bool, error) { return proof() }, |
| + ObserveRefusal: func(v hafresh.View, d hafresh.Decision) { |
| + calls++ |
| + if d != want || !reflect.DeepEqual(v.Lock, expected.Lock) || v.ETag == "" || v.Absent { |
| + t.Errorf("observation=%+v %+v want=%+v %+v", v, d, expected, want) |
| + } |
| + if (mode == "foreign" || mode == "same-box" || mode == "cas") && proofs != 1 { |
| + t.Errorf("observer before proof: %d", proofs) |
| + } |
| + }, |
| + } |
| + _, err = lease.AcquireHAForDuty(ctx, fx.Bucket, "instances/h", "", hafresh.Marker{}, p, func(hafresh.View) error { |
| + if mode == "capacity" { |
| + return errors.New("capacity refused") |
| + } |
| + return nil |
| + }) |
| + if err == nil { |
| + t.Fatal("expected refusal") |
| + } |
| + wantCalls, wantGets := 1, 1 |
| + if mode == "pool" { |
| + wantCalls, wantGets = 0, 0 |
| + } |
| + if mode == "capacity" { |
| + wantCalls = 0 |
| + } |
| + if mode == "cas" { |
| + wantGets = 2 |
| + } |
| + gets := 0 |
| + for _, req := range fx.Requests() { |
| + if req == "GET instances/h/lock.json" { |
| + gets++ |
| + } |
| + } |
| + if calls != wantCalls || gets != wantGets { |
| + t.Fatalf("calls=%d gets=%d want=%d,%d requests=%v", calls, gets, wantCalls, wantGets, fx.Requests()) |
| + } |
| + }) |
| + } |
| +} |