fix: tolerate small clock corrections without reducing charges
5 files changed,
+63
−3
internal/equipo/team/clock.go+29 −0internal/equipo/team/final_clock_test.go+26 −0internal/equipo/team/settletime_test.go+1 −1internal/equipo/team/shared.go+1 −1internal/equipo/team/store.go+6 −1
diff --git a/internal/equipo/team/clock.go b/internal/equipo/team/clock.go| index a224be0b..0474157e 100644 |
| --- a/internal/equipo/team/clock.go |
| +++ b/internal/equipo/team/clock.go |
| @@ -38,3 +38,32 @@ func (c *FakeClock) Advance(d time.Duration) { |
| defer c.mu.Unlock() |
| c.now = c.now.Add(d) |
| } |
| + |
| +// Corrections up to two seconds use the high-water time. This gives NTP room |
| +// without extending leases or reducing elapsed charges. Larger rollbacks remain |
| +// visible to observeClock and require containment and recovery. |
| +type storeClock struct { |
| + source Clock |
| + mu sync.Mutex |
| + high time.Time |
| +} |
| + |
| +func (c *storeClock) remember(high time.Time) { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if high.After(c.high) { |
| + c.high = high |
| + } |
| +} |
| +func (c *storeClock) Now() time.Time { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + now := c.source.Now() |
| + if now.Before(c.high) && c.high.Sub(now) <= 2*time.Second { |
| + return c.high |
| + } |
| + if now.After(c.high) { |
| + c.high = now |
| + } |
| + return now |
| +} |
diff --git a/internal/equipo/team/final_clock_test.go b/internal/equipo/team/final_clock_test.go| new file mode 100644 |
| index 00000000..819faec9 |
| --- /dev/null |
| +++ b/internal/equipo/team/final_clock_test.go |
| @@ -0,0 +1,26 @@ |
| +package team |
| + |
| +import ( |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestSmallClockRollbackPreservesLeaseAndCharge(t *testing.T) { |
| + e := newEnv(t) |
| + must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| + s, h := periodJob(e, 1) |
| + e.clock.Advance(10 * time.Second) |
| + before, err := e.store.Renew(e.ctx, s, h) |
| + must(t, err) |
| + e.clock.Advance(-time.Millisecond) |
| + after, err := e.store.Renew(e.ctx, s, h) |
| + must(t, err) |
| + if !after.Equal(before) { |
| + t.Fatal("clock correction moved lease", before, after) |
| + } |
| + must(t, e.store.StopAttempt(e.ctx, s, h, "test")) |
| + must(t, e.store.Contain(e.ctx, s, h.Attempt, "supervisor")) |
| + if u := periodUsage(e, 1); u.Spent.Seconds < 10 || u.Spent.Seconds > 11 { |
| + t.Fatal("small rollback changed charge", u) |
| + } |
| +} |
diff --git a/internal/equipo/team/settletime_test.go b/internal/equipo/team/settletime_test.go| index 291cbed9..463f5e29 100644 |
| --- a/internal/equipo/team/settletime_test.go |
| +++ b/internal/equipo/team/settletime_test.go |
| @@ -23,7 +23,7 @@ func TestContainTimeAfterClockStep(t *testing.T) { |
| must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| usage := periodUsage(e, 1) |
| reserved := int64(0) |
| - if tc.elapsed < 0 { |
| + if tc.elapsed < -2*time.Second { |
| tc.seconds = int64(testPolicy().AttemptSeconds) |
| } |
| if usage.Reserved.Seconds != reserved || usage.Spent.Seconds != tc.seconds { |
diff --git a/internal/equipo/team/shared.go b/internal/equipo/team/shared.go| index 3e8b39d6..d926f187 100644 |
| --- a/internal/equipo/team/shared.go |
| +++ b/internal/equipo/team/shared.go |
| @@ -23,7 +23,7 @@ func OpenShared(ctx context.Context, writer *sql.DB, path string, o Options) (*S |
| if err != nil { |
| return nil, err |
| } |
| - s := &Store{db: writer, path: path, marker: o.Marker, clock: o.Clock, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance, executorEvidence: o.ExecutorEvidence, observeWrite: o.ObserveWrite, commitHook: o.commitHook} |
| + s := &Store{db: writer, path: path, marker: o.Marker, clock: &storeClock{source: o.Clock}, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance, executorEvidence: o.ExecutorEvidence, observeWrite: o.ObserveWrite, observerFailed: o.ObserverFailed, commitHook: o.commitHook} |
| if s.adapters == nil { |
| s.adapters = map[Operation]Adapter{} |
| } |
diff --git a/internal/equipo/team/store.go b/internal/equipo/team/store.go| index 497c1bce..b0877a2c 100644 |
| --- a/internal/equipo/team/store.go |
| +++ b/internal/equipo/team/store.go |
| @@ -102,7 +102,7 @@ func Open(ctx context.Context, path string, o Options) (*Store, error) { |
| return nil, fail(CodeInvalid, "open: %v", err) |
| } |
| db.SetMaxOpenConns(4) |
| - s := &Store{db: db, ownsDB: true, path: path, marker: o.Marker, clock: o.Clock, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance, executorEvidence: o.ExecutorEvidence} |
| + s := &Store{db: db, ownsDB: true, path: path, marker: o.Marker, clock: &storeClock{source: o.Clock}, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance, executorEvidence: o.ExecutorEvidence} |
| if s.adapters == nil { |
| s.adapters = map[Operation]Adapter{} |
| } |
| @@ -400,6 +400,11 @@ func (s *Store) migrate(ctx context.Context) error { |
| func (s *Store) start(ctx context.Context) error { |
| var next int64 |
| err := s.transaction(ctx, false, true, func(tx *sql.Tx) error { |
| + var high string |
| + if err := tx.QueryRowContext(ctx, "SELECT clock_high_water FROM service WHERE id=1").Scan(&high); err != nil { |
| + return err |
| + } |
| + s.clock.(*storeClock).remember(parseTS(high)) |
| var inc, revision int64 |
| var previousRestore, previousQuarantine int |
| var previousWhy string |