carlos / platform Public

Managed fleets 1b task 6b, ruling: drainDone allows two minutes of clock skew

A heartbeat counts as composed after the drain only when it is at least
drainSkewMargin (2 minutes) past draining_since: the heartbeat's time is
the box's clock and the drain's start is the console's, and a box clock
running ahead must not pass a pre-drain heartbeat. Test first:
TestDrainDoneAllowsForClockSkew (1 minute after: not done; 3 minutes
after, empty, at the floor: done). The door tests' post-drain heartbeats
move past the margin.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev dd03259ed5053ffc837e677ecc45fc82f5773731 parent a7fc62e
2 files changed, +31 −9
  • internal/console/fleetrelabel.go +10 −3
  • internal/console/fleetrelabel_test.go +21 −6
diff --git a/internal/console/fleetrelabel.go b/internal/console/fleetrelabel.go
index fcdb83ccf..d616881dc 100644
--- a/internal/console/fleetrelabel.go
+++ b/internal/console/fleetrelabel.go
@@ -345,10 +345,17 @@ func (s *Server) readFleetBinding(ctx context.Context, fleet, h string) (fleetBi
return b, true, nil
}
+// drainSkewMargin is how long after a drain began a heartbeat must be
+// composed to count as composed after it. The heartbeat's time is the box's
+// clock and the drain's start is the console's. Boxes run NTP, but a box
+// clock running ahead must not pass a heartbeat composed before the drain
+// (controller ruling on Task 6b, concern 5).
+const drainSkewMargin = 2 * time.Minute
+
// drainDone is the drain rule (rule 5), the one test of whether a draining
// box holds nothing and may be detached: its newest heartbeat hb was
-// composed after the drain began (drainingSince, zero when the box is not
-// draining) and reports no routes for the fleet, no held leases and no
+// composed at least drainSkewMargin after the drain began (drainingSince,
+// zero when the box is not draining) and reports no routes for the fleet, no held leases and no
// failed final replications still on its disk. Static routes do not count:
// every box of the fleet serves them from the bucket, with no process, no
// lease and no local data. The heartbeat must also come from a release at
@@ -356,7 +363,7 @@ func (s *Server) readFleetBinding(ctx context.Context, fleet, h string) (fleetBi
// read as holding none. There is no exception for a silent box.
func drainDone(hb status.Box, drainingSince time.Time) bool {
return !drainingSince.IsZero() &&
- hb.GeneratedAt.After(drainingSince) &&
+ !hb.GeneratedAt.Before(drainingSince.Add(drainSkewMargin)) &&
boxAtLabelFloor(hb) &&
hb.Routes.Resident == 0 && hb.Routes.Hibernating == 0 && hb.Sidecars == 0 &&
hb.LeasesHeld == 0 && hb.ReplicationFailures == 0
diff --git a/internal/console/fleetrelabel_test.go b/internal/console/fleetrelabel_test.go
index be70abdde..a1eaa3479 100644
--- a/internal/console/fleetrelabel_test.go
+++ b/internal/console/fleetrelabel_test.go
@@ -362,7 +362,7 @@ func TestDrainDoneNeedsAPostDrainHeartbeat(t *testing.T) {
if drainDone(empty, since) {
t.Fatal("an empty heartbeat composed before the drain is done")
}
- empty.GeneratedAt = since.Add(time.Second)
+ empty.GeneratedAt = since.Add(3 * time.Minute)
if !drainDone(empty, since) {
t.Fatal("an empty heartbeat composed after the drain is not done")
}
@@ -392,9 +392,24 @@ func TestDrainDoneNeedsAPostDrainHeartbeat(t *testing.T) {
}
}
+// Controller ruling (concern 5): boxes run NTP, but a box clock running
+// ahead must not pass a heartbeat composed before the drain, so the
+// heartbeat must be composed drainSkewMargin after the drain began.
+func TestDrainDoneAllowsForClockSkew(t *testing.T) {
+ since := time.Now()
+ hb := status.Box{GeneratedAt: since.Add(time.Minute), Metrics: &status.BoxMetrics{Release: release.FleetBoxLabelFloor}}
+ if drainDone(hb, since) {
+ t.Fatal("an empty heartbeat composed 1 minute after the drain is done")
+ }
+ hb.GeneratedAt = since.Add(3 * time.Minute)
+ if !drainDone(hb, since) {
+ t.Fatal("an empty heartbeat composed 3 minutes after the drain is not done")
+ }
+}
+
func TestDrainDoneNeedsNoLeases(t *testing.T) {
since := time.Now()
- hb := status.Box{GeneratedAt: since.Add(time.Minute), LeasesHeld: 1, Metrics: &status.BoxMetrics{Release: release.FleetBoxLabelFloor}}
+ hb := status.Box{GeneratedAt: since.Add(3 * time.Minute), LeasesHeld: 1, Metrics: &status.BoxMetrics{Release: release.FleetBoxLabelFloor}}
if drainDone(hb, since) {
t.Fatal("no routes but one lease held is done")
}
@@ -446,12 +461,12 @@ func TestDrainDoneNeedsNoFailedReplication(t *testing.T) {
// The box stopped a.example.com and released its lease, but the final
// sync failed (the box half: TestFailedFinalSyncIsReportedUntilReplicated,
// internal/activator): it reports 1.
- beat(t, fx, "pi-1", time.Now().Add(time.Second), func(b *status.Box) { b.ReplicationFailures = 1 })
+ beat(t, fx, "pi-1", time.Now().Add(3*time.Minute), func(b *status.Box) { b.ReplicationFailures = 1 })
if code, raw := detachAPI(t, fx, token, "pi-1"); code != http.StatusConflict || errorOf(t, raw) != msgDetachNotDrained("pi", "pi-1") {
t.Fatalf("detach with a failed final sync = %d %s; want 409", code, raw)
}
// A later replication landed and cleared it.
- beat(t, fx, "pi-1", time.Now().Add(2*time.Second), nil)
+ beat(t, fx, "pi-1", time.Now().Add(4*time.Minute), nil)
if code, raw := detachAPI(t, fx, token, "pi-1"); code != http.StatusOK {
t.Fatalf("detach of a drained box = %d %s", code, raw)
}
@@ -501,14 +516,14 @@ func TestBothDetachDoorsUseDrainDone(t *testing.T) {
return strings.Contains(body, "Detached pi/pi-1."), body
}
// A post-drain heartbeat that still holds a lease.
- beat(t, fx, "pi-1", time.Now().Add(time.Second), func(b *status.Box) { b.LeasesHeld = 1 })
+ beat(t, fx, "pi-1", time.Now().Add(3*time.Minute), func(b *status.Box) { b.LeasesHeld = 1 })
if ok, said := detach(); ok || !strings.Contains(said, msgDetachNotDrained("pi", "pi-1")) {
t.Fatalf("detach while a lease is held: ok %v\n%s", ok, said)
}
if b := boxRow(t, reg, "pi-1"); b.Status != "active" {
t.Fatal("a refused detach detached the box")
}
- beat(t, fx, "pi-1", time.Now().Add(2*time.Second), nil)
+ beat(t, fx, "pi-1", time.Now().Add(4*time.Minute), nil)
if ok, said := detach(); !ok {
t.Fatalf("detach of a drained box refused:\n%s", said)
}