From 83187225e493ee6e2a231cd833eb6c33ad722bc8 Mon Sep 17 00:00:00 2001 From: Ted H Date: Sat, 14 Nov 2026 13:16:59 -0500 Subject: [PATCH 1/4] node: hold the failed handoff sample on the rig clock Agent: grok/grok-4.7 --- node/agent/followup39_test.go | 56 ++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/node/agent/followup39_test.go b/node/agent/followup39_test.go index eeea1a43f..3e8673fb6 100644 --- a/node/agent/followup39_test.go +++ b/node/agent/followup39_test.go @@ -68,17 +68,33 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { r.srv.SetUsageUnavailable(1000) serveBoundGeneration(t, r) r.manualUsage(t) + var fail atomic.Bool + var offset atomic.Int64 + if kind == "hold" { + offset.Store(int64(1500 * time.Millisecond)) + } + // This meter reads the rig clock. The production meter has + // already checkpointed time.Now, so catch the rig clock up + // before sampling: a stamp behind that checkpoint is held, + // and one a full sample interval behind is a clock gap. + // A hold also keeps the new checkpoint one second inside a + // sample interval ahead of the wall clock. After a restart + // the meter is the production one and reads time.Now again; + // the checkpoint then stays held through Boot, and it is not + // far enough ahead to be recorded as a clock gap. + lead := time.Duration(0) + if kind == "hold" { + lead = meter.DefaultSampleInterval - time.Second + } + if wait := time.Now().Add(lead - time.Duration(offset.Load())).Sub(r.clock.Wall()); wait > 0 { + r.clock.Advance(wait) + } a := oneBinding(r.clock.Wall()) b := a b.DeviceId, b.MemberId = uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()) b.PeerId, b.BindingRevision, b.AllowedIpv4 = "peer-b", 2, []string{"100.64.1.2/32"} upA, upB := meter.CounterName(a.DeviceId, meter.DirUpload), meter.CounterName(b.DeviceId, meter.DirUpload) src := &r20DynamicSource{boot: r.eng.bootID.String(), bindings: r.eng.meterBindings, counters: map[string]enforce.CounterValue{upA: {Packets: 1, Bytes: 1000}, upB: {}}} - var fail atomic.Bool - var offset atomic.Int64 - if kind == "hold" { - offset.Store(int64(1500 * time.Millisecond)) - } source := r21ReadFunc(func(ctx context.Context) (meter.Observation, error) { if fail.Load() { return meter.Observation{}, errors.New("TEST ONLY counter read failure") @@ -87,7 +103,7 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { }) r.eng.meter.Close() var err error - r.eng.meter, err = meter.New(meter.Config{NodeID: r.eng.nodeID, IncarnationID: r.eng.incID, Store: r.eng.usage, Source: source, Now: func() time.Time { return time.Now().Add(time.Duration(offset.Load())) }}) + r.eng.meter, err = meter.New(meter.Config{NodeID: r.eng.nodeID, IncarnationID: r.eng.incID, Store: r.eng.usage, Source: source, Now: func() time.Time { return r.clock.Wall().Add(time.Duration(offset.Load())) }}) if err != nil { t.Fatal(err) } @@ -146,7 +162,23 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { if err != nil { t.Fatal(err) } - time.Sleep(max(0, time.Until(head.LastSample)+10*time.Millisecond)) + if restart { + // The restarted meter stamps with time.Now. The + // checkpoint was taken from the rig clock and still + // leads the wall clock by less than one sample + // interval, so the sample is held rather than stored + // as a clock gap. Wait until the wall clock passes + // it. A slower host reaches here later and waits less. + time.Sleep(max(0, time.Until(head.LastSample)+10*time.Millisecond)) + } else if wait := head.LastSample.Sub(r.clock.Wall()) + 10*time.Millisecond; wait > 0 { + // This meter still reads the rig clock. Sleeping + // would not release the sample. + r.clock.Advance(wait) + } + } else if !restart { + // A changed counter read at the checkpoint time is held. + // This meter reads the rig clock, so move it forward. + r.clock.Advance(time.Millisecond) } fail.Store(false) r.eng.leaseRefreshPending = true @@ -162,13 +194,21 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { // Exercise Run itself, not just Sample: a paused sampler would silently // skip this delta. Stop the fixture's old loop before starting this one. r.manualUsage(t) + if !restart { + // Run reads this same clock. Settlement just checkpointed + // the open epoch at the current reading, and a sample at + // that reading is held. + r.clock.Advance(time.Millisecond) + } src.setCounter(upA, enforce.CounterValue{Packets: 3, Bytes: 4000}) src.setCounter(upB, enforce.CounterValue{Packets: 2, Bytes: 5120}) ctx, cancel := context.WithCancel(t.Context()) done := make(chan struct{}) go func() { defer close(done); _ = r.eng.meter.Run(ctx) }() r.eng.meter.ResumeSampling() - deadline := time.Now().Add(time.Second) + // A loaded host can take longer than a second to publish the + // sample. Leave as soon as the ledger shows the delta. + deadline := time.Now().Add(30 * time.Second) for time.Now().Before(deadline) { u = r21ReadLedger(t, r.eng.cfg, r.eng.nodeID, r.eng.incID) if u.byRevision[3] == 1024 { From 1c86bb0749f05ff782a5184eb8880bda9af81880 Mon Sep 17 00:00:00 2001 From: Ted H Date: Sat, 14 Nov 2026 16:00:15 -0500 Subject: [PATCH 2/4] node: keep a failed handoff pending without a checkpoint ahead of real time Agent: grok/grok-4.7 --- node/agent/followup39_test.go | 69 +++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/node/agent/followup39_test.go b/node/agent/followup39_test.go index 3e8673fb6..436c3b58e 100644 --- a/node/agent/followup39_test.go +++ b/node/agent/followup39_test.go @@ -59,6 +59,24 @@ func r22CrashDuringStep(t *testing.T, r *rig) { r20Crash(r.eng) } +// r22WaitPastCheckpoint sleeps until real time is 10ms past the newest +// usage checkpoint. A production meter reads time.Now, so callers run +// this before one samples. The sleep is a lower bound: a slower host +// arrives later and waits less. +func r22WaitPastCheckpoint(t *testing.T, e *Engine) { + t.Helper() + st, err := usagestore.Open(e.cfg.UsageDBPath(), e.nodeID, e.incID, usagestore.Options{}) + if err != nil { + t.Fatal(err) + } + head, err := st.Head(t.Context()) + _ = st.Close() + if err != nil { + t.Fatal(err) + } + time.Sleep(max(0, time.Until(head.LastSample)+10*time.Millisecond)) +} + func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { for _, kind := range []string{"hold", "read_error"} { for _, restart := range []bool{false, true} { @@ -77,17 +95,16 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { // already checkpointed time.Now, so catch the rig clock up // before sampling: a stamp behind that checkpoint is held, // and one a full sample interval behind is a clock gap. - // A hold also keeps the new checkpoint one second inside a - // sample interval ahead of the wall clock. After a restart - // the meter is the production one and reads time.Now again; - // the checkpoint then stays held through Boot, and it is not - // far enough ahead to be recorded as a clock gap. - lead := time.Duration(0) - if kind == "hold" { - lead = meter.DefaultSampleInterval - time.Second - } - if wait := time.Now().Add(lead - time.Duration(offset.Load())).Sub(r.clock.Wall()); wait > 0 { - r.clock.Advance(wait) + // The first reading is the rig clock plus offset. The rig + // clock starts at real time, so a hold steps the wall clock + // back by that offset and the reading is real time, not + // ahead of it. The handoff then reads with offset 0, 1.5s + // behind the checkpoint, and stays held whatever real time does. + delta := time.Now().Add(-time.Duration(offset.Load())).Sub(r.clock.Wall()) + if delta > 0 { + r.clock.Advance(delta) + } else if delta < 0 { + r.clock.JumpWall(delta) } a := oneBinding(r.clock.Wall()) b := a @@ -127,7 +144,15 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { t.Fatal("failed final sample was not left pending") } if restart { + // The restarted meter reads real time, so only a failing + // read keeps the final sample pending without depending + // on elapsed time. + fail.Store(true) r20Crash(r.eng) + // A production meter reads time.Now. Wait until real + // time is past the newest checkpoint before it samples. + // A slower host reaches here later and waits less. + r22WaitPastCheckpoint(t, r.eng) next, err := Start(Options{Config: r20ChangedSets(r), Clock: r.clock, Plane: r.plane, MeterSource: source, Prober: passProbe{}}) if err != nil { t.Fatal(err) @@ -152,7 +177,7 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { if r.eng.desired != nil && r.eng.desired.ConfigGeneration == 4 { t.Fatal("pending interval allowed desired refresh") } - if kind == "hold" { + if kind == "hold" && !restart { st, err := usagestore.Open(r.eng.cfg.UsageDBPath(), r.eng.nodeID, r.eng.incID, usagestore.Options{}) if err != nil { t.Fatal(err) @@ -162,17 +187,11 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { if err != nil { t.Fatal(err) } - if restart { - // The restarted meter stamps with time.Now. The - // checkpoint was taken from the rig clock and still - // leads the wall clock by less than one sample - // interval, so the sample is held rather than stored - // as a clock gap. Wait until the wall clock passes - // it. A slower host reaches here later and waits less. - time.Sleep(max(0, time.Until(head.LastSample)+10*time.Millisecond)) - } else if wait := head.LastSample.Sub(r.clock.Wall()) + 10*time.Millisecond; wait > 0 { - // This meter still reads the rig clock. Sleeping - // would not release the sample. + // This meter still reads the rig clock. The restarted + // meter does not reach here: its reads fail until below, + // and real time is already past the checkpoint. Sleeping + // would not release this sample. + if wait := head.LastSample.Sub(r.clock.Wall()) + 10*time.Millisecond; wait > 0 { r.clock.Advance(wait) } } else if !restart { @@ -225,6 +244,10 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { cfg := r.eng.cfg cfg.ExtraDeniedV4 = append(append([]string(nil), cfg.ExtraDeniedV4...), "198.51.100.0/24") next := r20Successor(t, r, cfg, src, r.plane) + // The successor's meter reads time.Now. Wait until real + // time is past the newest checkpoint before it samples. + // A slower host reaches here later and waits less. + r22WaitPastCheckpoint(t, next) if err := next.Boot(t.Context()); err != nil { t.Fatal(err) } From 6bbace5c0e35c1ea16e9368d6280c2ec10628d31 Mon Sep 17 00:00:00 2001 From: Ted H Date: Sat, 14 Nov 2026 16:58:42 -0500 Subject: [PATCH 3/4] node: keep hold-store checks on restart and bound the catch-up step Open and head checks run for both hold subtests. Only the rig-clock advance stays on the meter that reads that clock. The catch-up aligns the first reading with real time and will not step the wall further back than the anchor tolerance minus 250ms. Agent: grok/grok-4.7 --- node/agent/followup39_test.go | 48 +++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/node/agent/followup39_test.go b/node/agent/followup39_test.go index 436c3b58e..aecfeb230 100644 --- a/node/agent/followup39_test.go +++ b/node/agent/followup39_test.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "github.com/TED-H-com/Freedom-VPN/node/internal/meter" + "github.com/TED-H-com/Freedom-VPN/node/internal/timeanchor" "github.com/TED-H-com/Freedom-VPN/node/internal/usagestore" "github.com/TED-H-com/Freedom-VPN/node/watchdog" "github.com/TED-H-com/Freedom-VPN/pkg/api" @@ -95,12 +96,31 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { // already checkpointed time.Now, so catch the rig clock up // before sampling: a stamp behind that checkpoint is held, // and one a full sample interval behind is a clock gap. - // The first reading is the rig clock plus offset. The rig - // clock starts at real time, so a hold steps the wall clock - // back by that offset and the reading is real time, not - // ahead of it. The handoff then reads with offset 0, 1.5s - // behind the checkpoint, and stays held whatever real time does. - delta := time.Now().Add(-time.Duration(offset.Load())).Sub(r.clock.Wall()) + // The first reading is the rig clock plus offset. The catch-up + // aligns that reading with the real time just read. The step can + // be forward or backward, and it is never further back than the + // anchor's backward skew minus 250ms. The handoff then reads with + // offset 0, 1.5s behind the checkpoint, and stays held whatever + // real time does. + r22WaitPastCheckpoint(t, r.eng) + limit := -(timeanchor.DefaultBackwardSkew - 250*time.Millisecond) + started := time.Now() + var delta time.Duration + for { + delta = time.Now().Add(-time.Duration(offset.Load())).Sub(r.clock.Wall()) + if delta >= limit { + break + } + remain := 10*time.Second - time.Since(started) + if remain <= 0 { + t.Fatalf("catch-up still behind the anchor tolerance: delta=%s", delta) + } + gap := limit - delta + if gap > remain { + gap = remain + } + time.Sleep(gap) + } if delta > 0 { r.clock.Advance(delta) } else if delta < 0 { @@ -177,7 +197,7 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { if r.eng.desired != nil && r.eng.desired.ConfigGeneration == 4 { t.Fatal("pending interval allowed desired refresh") } - if kind == "hold" && !restart { + if kind == "hold" { st, err := usagestore.Open(r.eng.cfg.UsageDBPath(), r.eng.nodeID, r.eng.incID, usagestore.Options{}) if err != nil { t.Fatal(err) @@ -187,12 +207,14 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { if err != nil { t.Fatal(err) } - // This meter still reads the rig clock. The restarted - // meter does not reach here: its reads fail until below, - // and real time is already past the checkpoint. Sleeping - // would not release this sample. - if wait := head.LastSample.Sub(r.clock.Wall()) + 10*time.Millisecond; wait > 0 { - r.clock.Advance(wait) + // Only the rig-clock meter needs this advance. The restarted + // meter reads real time, the wait before Start already put + // real time past this checkpoint, and its reads fail until + // below, so moving the rig clock would not release the sample. + if !restart { + if wait := head.LastSample.Sub(r.clock.Wall()) + 10*time.Millisecond; wait > 0 { + r.clock.Advance(wait) + } } } else if !restart { // A changed counter read at the checkpoint time is held. From 61e77fe5c2327ce17f60149b23a1e64289cc8adb Mon Sep 17 00:00:00 2001 From: Ted H Date: Sat, 14 Nov 2026 18:14:47 -0500 Subject: [PATCH 4/4] node: correct three comments in the restart hold test Agent: grok/grok-4.7 --- node/agent/followup39_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/node/agent/followup39_test.go b/node/agent/followup39_test.go index aecfeb230..e679e53ec 100644 --- a/node/agent/followup39_test.go +++ b/node/agent/followup39_test.go @@ -63,7 +63,9 @@ func r22CrashDuringStep(t *testing.T, r *rig) { // r22WaitPastCheckpoint sleeps until real time is 10ms past the newest // usage checkpoint. A production meter reads time.Now, so callers run // this before one samples. The sleep is a lower bound: a slower host -// arrives later and waits less. +// arrives later and waits less. A host wall-clock step back during the +// sleep is not handled: real time can then still be behind the +// checkpoint, and a sample taken then can be held. func r22WaitPastCheckpoint(t *testing.T, e *Engine) { t.Helper() st, err := usagestore.Open(e.cfg.UsageDBPath(), e.nodeID, e.incID, usagestore.Options{}) @@ -95,7 +97,7 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { // This meter reads the rig clock. The production meter has // already checkpointed time.Now, so catch the rig clock up // before sampling: a stamp behind that checkpoint is held, - // and one a full sample interval behind is a clock gap. + // and one more than a full sample interval behind is a clock gap. // The first reading is the rig clock plus offset. The catch-up // aligns that reading with the real time just read. The step can // be forward or backward, and it is never further back than the @@ -210,7 +212,8 @@ func TestR22_H1B_FailedHandoffSettlementAndSampling(t *testing.T) { // Only the rig-clock meter needs this advance. The restarted // meter reads real time, the wait before Start already put // real time past this checkpoint, and its reads fail until - // below, so moving the rig clock would not release the sample. + // `fail` is cleared below, so moving the rig clock would not + // release the sample. if !restart { if wait := head.LastSample.Sub(r.clock.Wall()) + 10*time.Millisecond; wait > 0 { r.clock.Advance(wait)