oficina / home Public

roster: a publication keeps a lease whose subject and role it still lists (#97)

Every successful refresh bumped the cache version and so ended every lease
issued before it, giving a request that decided just before a refresh a
spurious retryable 503. A Lease now carries the subject and role it was
decided for, and Valid/Remaining accept a newer version while the cache is
usable, the subject still holds that role, and every publication since the
lease has listed it so (a per-member "since" version: a removal or role
change ends the lease for good, and a re-add does not revive it). The
deadline stays the original one, so the 60 s removal contract is unchanged.
A fence (record update, generation block, clock discrepancy) still ends
every earlier lease, even after a later publication.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 17db191e1c4baf6b4b4eb429173a66c8768c3414 parent 0cee9ba
2 files changed, +166 −8
  • roster/cache.go +43 −6
  • roster/lease_test.go +123 −2
diff --git a/roster/cache.go b/roster/cache.go
index e521bc4..33659b7 100644
--- a/roster/cache.go
+++ b/roster/cache.go
@@ -70,10 +70,36 @@ type Decision struct {
// from a new decision after renewal, or close when renewal cannot authorize it.
// Both accessors refuse immediately on cache contention: a caller may hold the
// database writer that an in-flight durable publication needs to acquire.
+//
+// A lease names the subject and role it was decided for. A later successful
+// publication does not end it while every publication since the lease has
+// listed that subject with that role (oficina/home #97): the deadline stays
+// the one the lease was issued with, so a publication never extends standing
+// and the 60-second removal contract is untouched. A removal, or a role
+// change, ends it for good: re-adding the subject does not revive it. A fence (a record update, a
+// generation block, a clock discrepancy) still ends every earlier lease, even
+// once a later publication makes the cache usable again: a fence can mean the
+// clock the deadline was measured on is no longer trustworthy.
type Lease struct {
cache *Cache
version uint64
+ fences uint64
deadline time.Duration
+ subject string
+ role string
+}
+
+// current reports whether the lease still holds against the cache state. The
+// caller holds c.mu and has called observe.
+func (l Lease) current(c *Cache, n time.Duration) bool {
+ if !c.usable || n >= l.deadline || c.fences != l.fences || c.version < l.version {
+ return false
+ }
+ if c.version == l.version {
+ return true
+ }
+ role, ok := c.members[l.subject]
+ return ok && role == l.role && c.since[l.subject] <= l.version
}
func (l Lease) Valid() bool {
@@ -86,7 +112,7 @@ func (l Lease) Valid() bool {
}
defer c.mu.Unlock()
_, n := c.observe()
- return c.version == l.version && c.usable && n < l.deadline
+ return l.current(c, n)
}
func (l Lease) Remaining() time.Duration {
if l.cache == nil {
@@ -98,7 +124,7 @@ func (l Lease) Remaining() time.Duration {
}
defer c.mu.Unlock()
_, n := c.observe()
- if c.version != l.version || !c.usable || n >= l.deadline {
+ if !l.current(c, n) {
return 0
}
return l.deadline - n
@@ -139,9 +165,11 @@ type Cache struct {
wall time.Time
mono time.Duration
version uint64
+ fences uint64
usable bool
start time.Duration
members map[string]string
+ since map[string]uint64 // version at which each member's current role was first published
evidence evidence
flight *flight
attempted bool
@@ -286,6 +314,7 @@ func (c *Cache) UpdateRecord(ctx context.Context, r VerifiedRecord) error {
func (c *Cache) fence() {
c.usable = false
c.version++
+ c.fences++
if c.flight != nil {
c.flight.cancel()
}
@@ -341,7 +370,7 @@ func (c *Cache) decision(subject string) Decision {
if !ok {
return Decision{State: NotMember}
}
- return Decision{State: Member, Role: role, Lease: Lease{c, c.version, c.start + MaximumAge}}
+ return Decision{State: Member, Role: role, Lease: Lease{cache: c, version: c.version, fences: c.fences, deadline: c.start + MaximumAge, subject: subject, role: role}}
}
// Refresh joins the same bounded flight as Check. Caller cancellation stops
@@ -487,13 +516,21 @@ func (c *Cache) publish(ctx context.Context, r VerifiedRecord, version uint64, s
if ctx.Err() != nil || c.version != version || now-start >= FetchTimeout {
return ErrUnavailable
}
- c.members = make(map[string]string, len(members))
+ c.version++
+ next := make(map[string]string, len(members))
+ since := make(map[string]uint64, len(members))
for _, m := range members {
- c.members[m.Subject] = m.Role
+ next[m.Subject] = m.Role
+ if old, ok := c.members[m.Subject]; ok && old == m.Role {
+ since[m.Subject] = c.since[m.Subject]
+ } else {
+ since[m.Subject] = c.version
+ }
}
+ c.members = next
+ c.since = since
c.start = start
c.usable = true
- c.version++
return nil
}
func (c *Cache) evidenceSnapshot() bool {
diff --git a/roster/lease_test.go b/roster/lease_test.go
index ff742b4..4018a94 100644
--- a/roster/lease_test.go
+++ b/roster/lease_test.go
@@ -3,6 +3,7 @@ package roster
import (
"context"
"database/sql"
+ "net/http"
"path/filepath"
"sync"
"testing"
@@ -114,8 +115,10 @@ func TestLeaseDoesNotWaitForSQLiteWriterPublication(t *testing.T) {
if !next.Lease.Valid() {
t.Fatal("retry could not use published lease")
}
- if d.Lease.Valid() {
- t.Fatal("old lease survived publication version")
+ // The publication listed the subject with the same role, so the earlier
+ // lease survives it (#97) — on its own, earlier deadline.
+ if !d.Lease.Valid() || d.Lease.Remaining() >= next.Lease.Remaining() {
+ t.Fatal("unchanged publication ended or extended the earlier lease")
}
}
@@ -205,3 +208,121 @@ func TestUnknownSigningKeyRefused(t *testing.T) {
t.Fatalf("unknown roster key result %v", e)
}
}
+
+// refreshTo publishes members at epoch after the refresh interval.
+func (f *fixture) refreshTo(c *Cache, epoch int64, members []MemberEntry) {
+ f.t.Helper()
+ f.set(func(r *http.Request) (*http.Response, error) { return f.response(r, epoch, members, true, ""), nil })
+ if e := c.Refresh(context.Background()); e != nil {
+ f.t.Fatal(e)
+ }
+}
+
+// #97: a refresh that still lists the subject with the same role keeps an
+// earlier lease, and keeps its deadline: the 60 s ceiling is measured from the
+// fetch that decided it, not from the publication that merely confirmed it.
+func TestLeaseSurvivesUnchangedPublicationOnItsOwnDeadline(t *testing.T) {
+ f := setup(t)
+ c := f.cache()
+ d := f.check(c, 1)
+ state(t, d, Member)
+ f.clock.step(30 * time.Second)
+ f.refreshTo(c, 2, []MemberEntry{{person(1), "member"}, {person(2), "member"}})
+ if !d.Lease.Valid() {
+ t.Fatal("unchanged publication ended the lease")
+ }
+ if got := d.Lease.Remaining(); got != 30*time.Second {
+ t.Fatalf("remaining %v, want the original deadline's 30s", got)
+ }
+ fresh := f.check(c, 1)
+ f.clock.step(29 * time.Second)
+ if !d.Lease.Valid() {
+ t.Fatal("early expiry")
+ }
+ f.clock.step(time.Second)
+ if d.Lease.Valid() || d.Lease.Remaining() != 0 {
+ t.Fatal("publication extended the earlier lease past its deadline")
+ }
+ if !fresh.Lease.Valid() {
+ t.Fatal("the confirming publication's own lease ended early")
+ }
+}
+
+func TestLeaseEndsOnRoleChange(t *testing.T) {
+ for _, tc := range []struct{ from, to string }{{"member", "owner"}, {"owner", "member"}} {
+ f := setup(t)
+ f.set(func(r *http.Request) (*http.Response, error) {
+ return f.response(r, 1, []MemberEntry{{person(1), tc.from}}, true, ""), nil
+ })
+ c := f.cache()
+ d := f.check(c, 1)
+ if d.State != Member || d.Role != tc.from {
+ t.Fatalf("decision %+v", d)
+ }
+ f.clock.step(30 * time.Second)
+ f.refreshTo(c, 2, []MemberEntry{{person(1), tc.to}})
+ if d.Lease.Valid() || d.Lease.Remaining() != 0 {
+ t.Fatalf("%s lease survived a change to %s", tc.from, tc.to)
+ }
+ if n := f.check(c, 1); n.Role != tc.to || !n.Lease.Valid() {
+ t.Fatal("new role not decided")
+ }
+ }
+}
+
+func TestLeaseEndsOnRemovalAndReAddDoesNotReviveIt(t *testing.T) {
+ f := setup(t)
+ c := f.cache()
+ d := f.check(c, 1)
+ other := f.check(c, 1)
+ f.clock.step(30 * time.Second)
+ f.refreshTo(c, 2, []MemberEntry{{person(2), "member"}})
+ if d.Lease.Valid() || d.Lease.Remaining() != 0 {
+ t.Fatal("lease survived removal")
+ }
+ state(t, f.check(c, 1), NotMember)
+ // A forced refresh after RetryAfter re-adds the subject inside the
+ // original 60 s: the earlier lease stays ended.
+ f.clock.step(RetryAfter)
+ f.refreshTo(c, 3, []MemberEntry{{person(1), "member"}, {person(2), "member"}})
+ if d.Lease.Valid() || other.Lease.Remaining() != 0 {
+ t.Fatal("re-add revived a lease that removal ended")
+ }
+ if !f.check(c, 1).Lease.Valid() {
+ t.Fatal("re-added member not decided afresh")
+ }
+}
+
+func TestLeaseSurvivesAnotherMembersChange(t *testing.T) {
+ f := setup(t)
+ f.set(func(r *http.Request) (*http.Response, error) {
+ return f.response(r, 1, []MemberEntry{{person(1), "owner"}, {person(2), "member"}}, true, ""), nil
+ })
+ c := f.cache()
+ d := f.check(c, 1)
+ f.clock.step(30 * time.Second)
+ f.refreshTo(c, 2, []MemberEntry{{person(1), "owner"}, {person(3), "owner"}})
+ if !d.Lease.Valid() {
+ t.Fatal("another member's change ended this lease")
+ }
+}
+
+// A fence ends every earlier lease even when a later publication lists the
+// subject unchanged: the deadline was measured on a clock the fence distrusts.
+func TestLeaseEndedByFenceStaysEndedAfterPublication(t *testing.T) {
+ f := setup(t)
+ c := f.cache()
+ d := f.check(c, 1)
+ f.clock.wallStep(-ClockTolerance - time.Nanosecond)
+ if d.Lease.Valid() {
+ t.Fatal("fence missed")
+ }
+ f.clock.step(RetryAfter)
+ f.refreshTo(c, 2, []MemberEntry{{person(1), "member"}})
+ if d.Lease.Valid() || d.Lease.Remaining() != 0 {
+ t.Fatal("publication revived a fenced lease")
+ }
+ if !f.check(c, 1).Lease.Valid() {
+ t.Fatal("no fresh decision after the fence")
+ }
+}