Managed fleets 1b task 6a, review round 1: HA keeps no records; one snapshot; no activator still counts
Controller ruling on findings 1, 2 and 5: HA hosts are outside 1b's move machinery, so they keep no replication records. An HA wake writes no awake record, an HA stop, failed wake or reaped wake records and clears nothing, the HA restore is untouched, and a record of a host the box classifies HA (lifetime index or registry row) is not counted. The HA error marking in releaseHAClean and closeHA is reverted. Finding 3: with no activator the agent counts the record directory under its data directory itself (activator.CountReplicationRecords): every awake and failed record, each host once, since no lease can be held; an unreadable directory or a data directory that is not absolute reports at least 1. Finding 4: both counters come from one snapshot (Service.DrainFacts, over one capture of the held leases), so a host with an awake record that takes its lease mid-read is counted once, never in neither. Tests: TestHAWakeAndStopKeepNoRecord, TestDrainSnapshotCountsAWakingHostOnce (deterministic hook at the capture, both orders, and the old two-sample false zero), TestHeartbeatWithoutAnActivatorReportsRecords.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+321
−96
internal/activator/activator.go+7 −6internal/activator/drainreport.go+146 −59internal/activator/drainreport_test.go+104 −4internal/activator/harelease.go+2 −5internal/activator/maintenance.go+3 −1internal/activator/opset.go+3 −1internal/host/agent.go+4 −3internal/host/drainreport.go+19 −14internal/host/drainreport_test.go+32 −2internal/lease/release.go+1 −1
CI failed — run details
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index 61b3840f3..ba4182817 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -2722,7 +2722,7 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro |
| // the host is awake leaves no final sync, and only this record tells |
| // the box's heartbeat that writes may be missing from the bucket. A |
| // record that cannot be written refuses the wake, nothing having run. |
| - if err := a.noteUnsynced(rt); err != nil { |
| + if err := a.noteUnsynced(rt, lease); err != nil { |
| w.stopOutcome.leaseReleased = a.giveBack(ctx, host, lease) |
| return w, fmt.Errorf("start %s: refusing to start: %w", host, err) |
| } |
| @@ -4894,11 +4894,12 @@ func (a *Service) finishStop(e *actEntry, outcome *stopObservation) { |
| if repErr != nil { |
| log.Printf("activator: final checkpoint %s: %v", e.host, repErr) |
| } |
| - // The release ran a final sync (an HA release, or a replica's Close): |
| - // its outcome is the host's replication failure record, written or |
| - // cleared before the entry goes (drainreport.go). No sync ran without |
| - // a replica, so nothing is recorded then. |
| - if _, ha := haEntryRep(e); ha || e.rep != nil { |
| + // A legacy release ran a final sync (the replica's Close): its outcome |
| + // is the host's replication records, written or cleared before the |
| + // entry goes (drainreport.go). No sync ran without a replica, so |
| + // nothing is recorded then. An HA host keeps no records at all |
| + // (controller ruling, Task 6a review: HA hosts are outside 1b's moves). |
| + if _, ha := haEntryRep(e); !ha && e.rep != nil { |
| a.noteFinalReplication(e.host, repErr) |
| } |
| a.mu.Lock() |
diff --git a/internal/activator/drainreport.go b/internal/activator/drainreport.go| index 915ad176e..c67899b61 100644 |
| --- a/internal/activator/drainreport.go |
| +++ b/internal/activator/drainreport.go |
| @@ -21,13 +21,23 @@ package activator |
| // LeasesHeld's). |
| // - <host>.json, written when a release reports a failed final sync |
| // while the lease was released anyway (lease.Replicator.Close releases |
| -// whatever the sync did; an HA release writes its marker whatever the |
| -// sync did). It counts whatever is held. |
| +// whatever the sync did). It counts whatever is held. |
| // A host counts once, whichever records it has. Both are removed when a |
| // later final sync of the host on this box lands or the host is |
| // restored fresh from the bucket here. They survive an agent restart; |
| // nothing else removes them. |
| // |
| +// Both counts come from one snapshot (drainSnapshot): one capture of the |
| +// held leases decides both, so a host taking its lease mid-read is counted |
| +// once, never in neither. Without an activator the agent counts the |
| +// directory itself (CountReplicationRecords): every record, since no lease |
| +// can be held. |
| +// |
| +// HA hosts keep no records (controller ruling, Task 6a review: HA hosts are |
| +// outside 1b's move machinery, and Task 6b refuses to label or drain them): |
| +// nothing is written for an HA wake, stop, failed wake or restore, and a |
| +// record of a host the box now classifies HA is not counted. |
| +// |
| // A wake whose unsynced record cannot be written is refused before anything |
| // runs, the lease given back. A failed-sync record that cannot be written |
| // is logged loudly and changes nothing else: the stop, the lease release |
| @@ -161,6 +171,106 @@ func writeRecordLocked(dir, name string, v any) error { |
| return writeDurable(dir, name, append(body, '\n')) |
| } |
| +// drainSnapshotHook, when set, runs at the snapshot's two points: "before" |
| +// the held leases are captured and "held" after, before the records are |
| +// read. Test-only: it makes the interleaving with a wake deterministic. |
| +var drainSnapshotHook func(point string) |
| + |
| +// drainSnapshot is both drain facts from one capture of the held leases: |
| +// how many are held, and how many hosts have records, an awake record |
| +// counting only for a host not in that capture. A record of a host this |
| +// box classifies HA is not counted (knownHA). The error is the records' |
| +// read: the leases are always known. |
| +func (b *ExecBackend) drainSnapshot() (leases, failures int, err error) { |
| + if h := drainSnapshotHook; h != nil { |
| + h("before") |
| + } |
| + b.drain.mu.Lock() |
| + heldHosts := b.pruneHeldLocked() |
| + b.drain.mu.Unlock() |
| + held := map[string]bool{} |
| + for _, h := range heldHosts { |
| + held[h] = true |
| + } |
| + if h := drainSnapshotHook; h != nil { |
| + h("held") |
| + } |
| + dir, err := b.failedSyncDir() |
| + if err != nil { |
| + return len(heldHosts), 0, err |
| + } |
| + hosts, err := readRecordHosts(dir, held, &b.drain.rec) |
| + if err != nil { |
| + return len(heldHosts), 0, err |
| + } |
| + n := 0 |
| + for h := range hosts { |
| + if !b.knownHA(h) { |
| + n++ |
| + } |
| + } |
| + return len(heldHosts), n, nil |
| +} |
| + |
| +// knownHA reports whether this box classifies host HA, by the sticky marks |
| +// only (the lifetime index, the registry row): no record read, no request. |
| +func (b *ExecBackend) knownHA(host string) bool { |
| + if b.Lifetimes != nil { |
| + if e, ok := b.Lifetimes.Lookup(host); ok && e.HA { |
| + return true |
| + } |
| + } |
| + if b.Registry != nil { |
| + if rt, err := b.Registry.Get(host); err == nil && rt != nil && rt.HA { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// readRecordHosts lists the hosts with records in dir: every <host>.json, |
| +// and every <host>.unsynced whose host is not in held; never a temp file |
| +// (WriteAtomic's start with a dot). No directory is none; any other read |
| +// failure is returned, never "none". mu, when set, is held for the read. |
| +func readRecordHosts(dir string, held map[string]bool, mu *sync.Mutex) (map[string]bool, error) { |
| + if mu != nil { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + } |
| + ents, err := os.ReadDir(dir) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return nil, nil |
| + } |
| + if err != nil { |
| + return nil, err |
| + } |
| + hosts := map[string]bool{} |
| + for _, e := range ents { |
| + name := e.Name() |
| + if strings.HasPrefix(name, ".") { |
| + continue |
| + } |
| + if host, ok := strings.CutSuffix(name, failedSyncSuffix); ok { |
| + hosts[host] = true |
| + } else if host, ok := strings.CutSuffix(name, unsyncedSuffix); ok && !held[host] { |
| + hosts[host] = true |
| + } |
| + } |
| + return hosts, nil |
| +} |
| + |
| +// CountReplicationRecords counts the hosts with a replication record under |
| +// dataDir, for an agent with no activator: no lease can be held, so every |
| +// awake and failed record counts, each host once. A dataDir that is not |
| +// absolute, or a directory that cannot be read, is an error, never "none". |
| +func CountReplicationRecords(dataDir string) (int, error) { |
| + if dataDir == "" || !filepath.IsAbs(dataDir) { |
| + return 0, fmt.Errorf("no absolute data directory to read replication records under (%q)", dataDir) |
| + } |
| + hosts, err := readRecordHosts(filepath.Join(filepath.Clean(dataDir), ReplicationFailuresDir), nil, nil) |
| + return len(hosts), err |
| +} |
| + |
| // noteUnsynced writes host's awake record durably, before anything of the |
| // host can write its database. A later wake replaces it. A backend with no |
| // absolute data directory keeps no records at all: the wake goes ahead as |
| @@ -239,44 +349,10 @@ func (b *ExecBackend) clearFailedFinalSync(host string) error { |
| return nil |
| } |
| -// failedFinalSyncs counts the hosts whose writes are not known to be in |
| -// the bucket: every <host>.json, and every <host>.unsynced whose host this |
| -// process holds no lease for, each host once; never a temp file |
| -// (WriteAtomic's start with a dot). No directory is none; any other read |
| -// failure is returned, never "none". |
| +// failedFinalSyncs is drainSnapshot's failure count. |
| func (b *ExecBackend) failedFinalSyncs() (int, error) { |
| - dir, err := b.failedSyncDir() |
| - if err != nil { |
| - return 0, err |
| - } |
| - b.drain.mu.Lock() |
| - held := map[string]bool{} |
| - for _, h := range b.pruneHeldLocked() { |
| - held[h] = true |
| - } |
| - b.drain.mu.Unlock() |
| - b.drain.rec.Lock() |
| - defer b.drain.rec.Unlock() |
| - ents, err := os.ReadDir(dir) |
| - if errors.Is(err, os.ErrNotExist) { |
| - return 0, nil |
| - } |
| - if err != nil { |
| - return 0, err |
| - } |
| - hosts := map[string]bool{} |
| - for _, e := range ents { |
| - name := e.Name() |
| - if strings.HasPrefix(name, ".") { |
| - continue |
| - } |
| - if host, ok := strings.CutSuffix(name, failedSyncSuffix); ok { |
| - hosts[host] = true |
| - } else if host, ok := strings.CutSuffix(name, unsyncedSuffix); ok && !held[host] { |
| - hosts[host] = true |
| - } |
| - } |
| - return len(hosts), nil |
| + _, n, err := b.drainSnapshot() |
| + return n, err |
| } |
| // drainFacts is the backend half the Service reads the drain facts |
| @@ -284,6 +360,7 @@ func (b *ExecBackend) failedFinalSyncs() (int, error) { |
| // (a unit-backed row takes no lease and replicates nothing). |
| type drainFacts interface { |
| heldLeases() int |
| + drainSnapshot() (leases, failures int, err error) |
| noteUnsynced(rt registry.Route) error |
| noteFailedFinalSync(host string, cause error) error |
| clearFailedFinalSync(host string) error |
| @@ -304,6 +381,13 @@ func (b *SplitBackend) execFacts() (drainFacts, bool) { |
| return f, ok |
| } |
| +func (b *SplitBackend) drainSnapshot() (int, int, error) { |
| + if f, ok := b.execFacts(); ok { |
| + return f.drainSnapshot() |
| + } |
| + return 0, 0, errNoDrainFacts |
| +} |
| + |
| func (b *SplitBackend) heldLeases() int { |
| if f, ok := b.execFacts(); ok { |
| return f.heldLeases() |
| @@ -347,9 +431,21 @@ func (b *SplitBackend) failedFinalSyncs() (int, error) { |
| return 0, errNoDrainFacts |
| } |
| +// DrainFacts is the heartbeat's two drain facts from one snapshot |
| +// (drainSnapshot): the instance leases this process holds right now, and |
| +// the hosts whose writes are not known to be in the bucket. err means the |
| +// failure count is unknown, never zero; leases is known either way. |
| +func (a *Service) DrainFacts() (leases, failures int, err error) { |
| + if f, ok := a.backend.(drainFacts); ok { |
| + return f.drainSnapshot() |
| + } |
| + return 0, 0, errNoDrainFacts |
| +} |
| + |
| // LeasesHeld is how many instance leases this process holds right now |
| -// (the heartbeat's leases_held). A backend that takes no leases of its |
| -// own holds none. |
| +// (the heartbeat's leases_held, which DrainFacts reads in the same |
| +// snapshot as the failures). A backend that takes no leases of its own |
| +// holds none. |
| func (a *Service) LeasesHeld() int { |
| if f, ok := a.backend.(drainFacts); ok { |
| return f.heldLeases() |
| @@ -357,9 +453,9 @@ func (a *Service) LeasesHeld() int { |
| return 0 |
| } |
| -// ReplicationFailures is how many hosts' final syncs failed on this box |
| -// and have not been replicated since (the heartbeat's |
| -// replication_failures). An error means the count is unknown, never zero. |
| +// ReplicationFailures is DrainFacts' failure count alone: the hosts whose |
| +// writes on this box are not known to be in the bucket. An error means |
| +// the count is unknown, never zero. |
| func (a *Service) ReplicationFailures() (int, error) { |
| if f, ok := a.backend.(drainFacts); ok { |
| return f.failedFinalSyncs() |
| @@ -367,30 +463,21 @@ func (a *Service) ReplicationFailures() (int, error) { |
| return 0, errNoDrainFacts |
| } |
| -// finalReplicationError marks an HA release's error as one that left the |
| -// host's last writes, database or files, short of the bucket |
| -// (releaseHAClean). Error is the wrapped text, unchanged. |
| -type finalReplicationError struct{ err error } |
| - |
| -func (e *finalReplicationError) Error() string { return e.err.Error() } |
| -func (e *finalReplicationError) Unwrap() error { return e.err } |
| - |
| // finalReplicationFailed reports whether a release's error says its final |
| -// replication failed: a legacy replicator's *lease.FinalSyncError, or an |
| -// HA release's finalReplicationError. |
| +// sync failed: a legacy replicator's *lease.FinalSyncError. |
| func finalReplicationFailed(err error) bool { |
| var fs *lease.FinalSyncError |
| - var fr *finalReplicationError |
| - return errors.As(err, &fs) || errors.As(err, &fr) |
| + return errors.As(err, &fs) |
| } |
| // noteUnsynced writes rt's awake record before anything of the host can |
| // write (doWake, after the lease and the restore, before the seed and the |
| // spawn). An error refuses the wake. A backend that keeps no records |
| -// (a test double) records nothing. |
| -func (a *Service) noteUnsynced(rt registry.Route) error { |
| +// (a test double) records nothing, and neither does an HA wake (its lease |
| +// or the row says HA): HA hosts keep no records. |
| +func (a *Service) noteUnsynced(rt registry.Route, l Lease) error { |
| f, ok := a.backend.(drainFacts) |
| - if !ok { |
| + if !ok || rt.HA || haHeld(l, nil) { |
| return nil |
| } |
| if err := f.noteUnsynced(rt); err != nil { |
diff --git a/internal/activator/drainreport_test.go b/internal/activator/drainreport_test.go| index ebcf9f065..3c5c4cda7 100644 |
| --- a/internal/activator/drainreport_test.go |
| +++ b/internal/activator/drainreport_test.go |
| @@ -255,8 +255,8 @@ func TestDrainFactsThroughTheSplitBackend(t *testing.T) { |
| } |
| // Which release errors are a failed final replication: a legacy |
| -// replicator's *lease.FinalSyncError and an HA release's database or files |
| -// failure, however joined; never a failed lease delete or marker alone. |
| +// replicator's *lease.FinalSyncError, however joined; never a failed lease |
| +// delete or marker alone. |
| func TestFinalReplicationFailedClassifies(t *testing.T) { |
| other := errors.New("marker PUT refused") |
| for _, c := range []struct { |
| @@ -268,8 +268,6 @@ func TestFinalReplicationFailedClassifies(t *testing.T) { |
| {ErrStopUnproven, false}, |
| {&lease.FinalSyncError{Err: errors.New("sync")}, true}, |
| {errors.Join(&lease.FinalSyncError{Err: errors.New("sync")}, other), true}, |
| - {errors.Join(nil, &finalReplicationError{errors.New("final sync: x")}, nil, other), true}, |
| - {errors.Join(&finalReplicationError{errors.New("files: x")}, nil, nil, nil), true}, |
| } { |
| if got := finalReplicationFailed(c.err); got != c.want { |
| t.Errorf("finalReplicationFailed(%v) = %v, want %v", c.err, got, c.want) |
| @@ -393,3 +391,105 @@ func TestUnwritableAwakeRecordRefusesTheWake(t *testing.T) { |
| func fleetlessRoute(host string) registry.Route { |
| return registry.Route{Host: host, Kind: "instance"} |
| } |
| + |
| +// HA hosts keep no records (controller ruling, Task 6a review): an HA |
| +// wake and stop write nothing and leave ReplicationFailures as it was, and |
| +// a record of a host the box classifies HA is not counted. |
| +func TestHAWakeAndStopKeepNoRecord(t *testing.T) { |
| + s := haService(t, "shop.example.com") |
| + dir := filepath.Join(s.b.DataDir, ReplicationFailuresDir) |
| + if err := s.b.noteFailedFinalSync("cafe.example.com", errors.New("a legacy host's failure")); err != nil { |
| + t.Fatal(err) |
| + } |
| + before := failures(t, s.a) |
| + if before != 1 { |
| + t.Fatalf("one legacy record: %d", before) |
| + } |
| + e, err := s.wakeForTest(context.Background()) |
| + if err != nil || e == nil { |
| + t.Fatalf("wake: %v", err) |
| + } |
| + if _, ha := s.b.haAcquired(s.rt.Host); !ha { |
| + t.Fatal("the host did not wake HA") |
| + } |
| + if _, err := os.Stat(filepath.Join(dir, s.rt.Host+unsyncedSuffix)); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("an HA wake wrote an awake record: %v", err) |
| + } |
| + s.stopForTest(e) |
| + if !s.stoppedFor(e) { |
| + t.Fatal("the stop went pending") |
| + } |
| + ents, err := os.ReadDir(dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(ents) != 1 || ents[0].Name() != "cafe.example.com.json" { |
| + t.Fatalf("an HA wake and stop changed the records: %v", ents) |
| + } |
| + if n := failures(t, s.a); n != before { |
| + t.Fatalf("ReplicationFailures %d after an HA wake and stop, want %d", n, before) |
| + } |
| + // A record of the HA host itself (left from before it was HA) is not |
| + // counted. |
| + if err := s.b.noteFailedFinalSync(s.rt.Host, errors.New("from its legacy days")); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n := failures(t, s.a); n != before { |
| + t.Fatalf("an HA host's record counted: %d, want %d", n, before) |
| + } |
| +} |
| + |
| +// Both counters come from one capture of the held leases: a host with an |
| +// awake record that takes its lease where the heartbeat used to sample |
| +// twice is counted once, as held when it took the lease before the |
| +// capture and as a failure when after; never in neither. The two separate |
| +// reads show the old false zero. |
| +func TestDrainSnapshotCountsAWakingHostOnce(t *testing.T) { |
| + s := legacyService(t, "shop.example.com") |
| + rt := s.rt |
| + acquire := func() Lease { |
| + t.Helper() |
| + l, err := s.b.Acquire(context.Background(), rt) |
| + if err != nil { |
| + t.Fatalf("acquire: %v", err) |
| + } |
| + return l |
| + } |
| + if err := s.b.noteUnsynced(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + // The old shape: leases sampled, the lease taken, failures sampled. |
| + leases := s.a.LeasesHeld() |
| + l := acquire() |
| + if f := failures(t, s.a); leases+f != 0 { |
| + t.Fatalf("the two-sample read was expected to miss the host (the bug): leases %d, failures %d", leases, f) |
| + } |
| + if err := l.Release(context.Background()); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + for _, at := range []struct{ point, as string }{{"before", "held"}, {"held", "failure"}} { |
| + var taken Lease |
| + drainSnapshotHook = func(p string) { |
| + if p == at.point && taken == nil { |
| + taken = acquire() |
| + } |
| + } |
| + leases, f, err := s.a.DrainFacts() |
| + drainSnapshotHook = nil |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + want := [2]int{1, 0} |
| + if at.as == "failure" { |
| + want = [2]int{0, 1} |
| + } |
| + if [2]int{leases, f} != want { |
| + t.Fatalf("lease taken at %q: leases %d, failures %d, want %v (once, as %s)", at.point, leases, f, want, at.as) |
| + } |
| + if err := taken.Release(context.Background()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
diff --git a/internal/activator/harelease.go b/internal/activator/harelease.go| index 9d4496bcb..f07e3d136 100644 |
| --- a/internal/activator/harelease.go |
| +++ b/internal/activator/harelease.go |
| @@ -215,14 +215,11 @@ func (a *Service) releaseHAClean(ctx context.Context, e *actEntry) error { |
| if merr == nil { |
| log.Printf("activator: %s: released with a marker (clean %v, txid %d, files %s)", rt.Host, m.Clean, txid, gen) |
| } |
| - // The files' and the database's final publish are the host's final |
| - // replication: either failing is recorded until a later one lands |
| - // (finalReplicationError, drainreport.go). |
| if ferr != nil { |
| - ferr = &finalReplicationError{fmt.Errorf("files: %w", ferr)} |
| + ferr = fmt.Errorf("files: %w", ferr) |
| } |
| if finErr != nil { |
| - finErr = &finalReplicationError{fmt.Errorf("final sync: %w", finErr)} |
| + finErr = fmt.Errorf("final sync: %w", finErr) |
| } |
| return errors.Join(ferr, finErr, rerr, merr) |
| } |
diff --git a/internal/activator/maintenance.go b/internal/activator/maintenance.go| index 0f42d8959..454761793 100644 |
| --- a/internal/activator/maintenance.go |
| +++ b/internal/activator/maintenance.go |
| @@ -476,7 +476,9 @@ func (a *Service) cleanupFailedWake(ctx context.Context, e *actEntry, rt registr |
| err := w.rep.Close(closeCtx) |
| w.stopOutcome.replicaClosed = err == nil |
| w.stopOutcome.leaseReleased = w.stopOutcome.replicaClosed |
| - a.noteFinalReplication(rt.Host, err) |
| + if !haHeld(lease, w.rep) { // an HA host keeps no records (drainreport.go) |
| + a.noteFinalReplication(rt.Host, err) |
| + } |
| return |
| } |
| w.stopOutcome.leaseReleased = a.giveBack(ctx, rt.Host, lease) |
diff --git a/internal/activator/opset.go b/internal/activator/opset.go| index 24f2fc1bb..f4fc9636c 100644 |
| --- a/internal/activator/opset.go |
| +++ b/internal/activator/opset.go |
| @@ -922,7 +922,9 @@ func (a *Service) reapUnprovenWake(host string, u *unprovenWake) { |
| if u.rep != nil { |
| err = u.rep.Close(ctx) |
| outcome.replicaClosed = err == nil |
| - a.noteFinalReplication(host, err) |
| + if !haHeld(u.lease, u.rep) { // an HA host keeps no records (drainreport.go) |
| + a.noteFinalReplication(host, err) |
| + } |
| } else if u.lease != nil { |
| err = releaseLease(ctx, u.lease) |
| if err != nil { |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index a69be1c9d..440b06312 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -4398,9 +4398,10 @@ func statusLoop(reg *registry.Registry, svc *activator.Service, buildStates func |
| Layout: layoutStatusReport(routes), |
| } |
| // The drain rule's facts (managed fleets 1b): the instance leases |
| - // this box holds and its unresolved failed final syncs, from the |
| - // activator's memory and its data directory. No store request. |
| - drainReport(&p, svc) |
| + // this box holds and its unresolved replication records, from the |
| + // activator's memory and the data directory (read directly when |
| + // there is no activator). No store request. |
| + drainReport(&p, svc, dataDir()) |
| // The hosts this box is refusing because it cannot provide their |
| // config (amadan #213): private Metrics only, nothing public. |
| claimReport(&p) |
diff --git a/internal/host/drainreport.go b/internal/host/drainreport.go| index a03889538..42ae57d32 100644 |
| --- a/internal/host/drainreport.go |
| +++ b/internal/host/drainreport.go |
| @@ -8,23 +8,28 @@ import ( |
| ) |
| // drainReport fills the heartbeat's drain rule facts (managed fleets 1b, |
| -// rule 5) from the box's activator: the instance leases it holds right |
| -// now, and the hosts whose final sync failed here with no later |
| -// replication landing since. Both are read from the process and the |
| -// box's disk, never the bucket. A count of failures the activator cannot |
| -// read is reported as at least 1, so a console never reads it as drained. |
| -// A box with no activator (svc nil) takes no instance leases and reports |
| -// 0 for both. |
| -func drainReport(p *status.Probes, svc *activator.Service) { |
| +// rule 5): the instance leases this process holds right now, and the hosts |
| +// whose writes on this box are not known to be in the bucket. Both come |
| +// from the activator's one snapshot (Service.DrainFacts), so a host taking |
| +// its lease mid-read is counted once, never in neither. With no activator |
| +// (svc nil) no lease can be held, and the durable records under dataDir |
| +// are counted directly (activator.CountReplicationRecords): records left |
| +// by an earlier run with an activator still count. Neither read touches |
| +// the bucket. A failure count that cannot be read is reported as at least |
| +// 1, so a console never reads it as drained. |
| +func drainReport(p *status.Probes, svc *activator.Service, dataDir string) { |
| + var ( |
| + leases, n int |
| + err error |
| + ) |
| if svc == nil { |
| - p.LeasesHeld, p.ReplicationFailures = 0, 0 |
| - return |
| + n, err = activator.CountReplicationRecords(dataDir) |
| + } else { |
| + leases, n, err = svc.DrainFacts() |
| } |
| - p.LeasesHeld = svc.LeasesHeld() |
| - n, err := svc.ReplicationFailures() |
| if err != nil { |
| - log.Printf("agent: status tick: replication failures unreadable: %v (reported as at least 1)", err) |
| + log.Printf("agent: status tick: replication records unreadable: %v (reported as at least 1)", err) |
| n = max(n, 1) |
| } |
| - p.ReplicationFailures = n |
| + p.LeasesHeld, p.ReplicationFailures = leases, n |
| } |
diff --git a/internal/host/drainreport_test.go b/internal/host/drainreport_test.go| index 1d6a1d9bf..05678533e 100644 |
| --- a/internal/host/drainreport_test.go |
| +++ b/internal/host/drainreport_test.go |
| @@ -43,7 +43,7 @@ func (d *drainBox) heartbeat(t *testing.T, svc *activator.Service) (string, stat |
| t.Helper() |
| ctx := context.Background() |
| p := status.Probes{MachineID: "test-machine"} |
| - drainReport(&p, svc) |
| + drainReport(&p, svc, d.eb.DataDir) |
| if err := statusTick(ctx, d.store, d.reg, p, false, "pi-1"); err != nil { |
| t.Fatalf("statusTick: %v", err) |
| } |
| @@ -131,7 +131,37 @@ func TestHeartbeatReportsReplicationFailures(t *testing.T) { |
| if _, box := d.heartbeat(t, d.svc); box.ReplicationFailures < 1 { |
| t.Fatalf("an unreadable record directory: replication_failures %d, want at least 1", box.ReplicationFailures) |
| } |
| + if _, box := d.heartbeat(t, nil); box.LeasesHeld != 0 || box.ReplicationFailures < 1 { |
| + t.Fatalf("no activator, unreadable records: leases_held %d, replication_failures %d, want 0 and at least 1", box.LeasesHeld, box.ReplicationFailures) |
| + } |
| +} |
| + |
| +// Review finding 3: no activator is not no failures. Records left by an |
| +// earlier run (a failed final sync of one host, another left awake by a |
| +// crash) are on disk under the data directory; the agent restarted without |
| +// an activator reports both, since no lease can be held without one. With |
| +// no records it reports 0. |
| +func TestHeartbeatWithoutAnActivatorReportsRecords(t *testing.T) { |
| + t.Parallel() |
| + d := newDrainBox(t, "test-bucket") |
| if _, box := d.heartbeat(t, nil); box.LeasesHeld != 0 || box.ReplicationFailures != 0 { |
| - t.Fatalf("no activator: leases_held %d, replication_failures %d", box.LeasesHeld, box.ReplicationFailures) |
| + t.Fatalf("no activator, no records: leases_held %d, replication_failures %d", box.LeasesHeld, box.ReplicationFailures) |
| + } |
| + dir := filepath.Join(d.eb.DataDir, activator.ReplicationFailuresDir) |
| + if err := os.MkdirAll(dir, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, name := range []string{"shop.example.com.json", "cafe.example.com.unsynced", "cafe.example.com.json", ".tmp-partial.json"} { |
| + if err := os.WriteFile(filepath.Join(dir, name), []byte("{}\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if _, box := d.heartbeat(t, nil); box.LeasesHeld != 0 || box.ReplicationFailures != 2 { |
| + t.Fatalf("no activator, two hosts' records: leases_held %d, replication_failures %d, want 0 and 2", box.LeasesHeld, box.ReplicationFailures) |
| + } |
| + p := status.Probes{} |
| + drainReport(&p, nil, "relative/data") |
| + if p.ReplicationFailures < 1 { |
| + t.Fatalf("no activator and no absolute data directory: replication_failures %d, want at least 1", p.ReplicationFailures) |
| } |
| } |
diff --git a/internal/lease/release.go b/internal/lease/release.go| index dbead2678..e50cfcfc6 100644 |
| --- a/internal/lease/release.go |
| +++ b/internal/lease/release.go |
| @@ -226,7 +226,7 @@ func (r *Replicator) closeHA(ctx context.Context) error { |
| lease := r.rn.stop() |
| relCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| defer cancel() |
| - return errors.Join(finalSyncErr(r.storeCloseErr()), releaseWithMarker(relCtx, r.leaser, r.region, lease, uncleanMarker(r.base, txid))) |
| + return errors.Join(r.storeCloseErr(), releaseWithMarker(relCtx, r.leaser, r.region, lease, uncleanMarker(r.base, txid))) |
| } |
| func (r *Replicator) storeCloseErr() error { |