lease: tombstone writes on the exact version it checked
Astra R1 P1 on slice 0. Tombstone checked the lock's fields on its own read, then called litestream's AcquireLease, which read lock.json again and conditioned its write on that second read. A storage-generations release marker written between the two reads was replaced by the tombstone. The write is now conditional on the version Tombstone classified (If-None-Match * when absent, If-Match its ETag otherwise), through the package's putLock. A lost condition (412/409, or 404 on a vanished key) reads the winner back: its owner's HeldError, nil for this move's own tombstone, ErrUnknownLockField for a later release's lock, and ErrTombstoneChanged when nothing can be read back. Otherwise unchanged. TestTombstoneRefusesUnknownFieldWrittenBeforeItsWrite installs a generations marker just after the checked read and just before the PUT, on an absent and an expired lock; the marker stands and Tombstone refuses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+99
−25
internal/lease/tombstone.go+43 −25internal/lease/tombstone_test.go+56 −0
CI cancelled — superseded by a newer push — run details
diff --git a/internal/lease/tombstone.go b/internal/lease/tombstone.go| index 947e6f8cb..41422496d 100644 |
| --- a/internal/lease/tombstone.go |
| +++ b/internal/lease/tombstone.go |
| @@ -177,12 +177,15 @@ func heldErr(l *litestream.Lease) error { |
| // - live and held by owner: nil and no write; the caller re-asserts with |
| // Reassert; |
| // - live and held by anyone else: a *HeldError, nothing written; |
| -// - the lock changed under the write and cannot be read back: |
| -// ErrTombstoneChanged. |
| +// - the lock changed under the write: read back, and answered as the |
| +// winner is (its owner's *HeldError, nil for owner itself, |
| +// ErrUnknownLockField), or ErrTombstoneChanged when it cannot be read |
| +// back. |
| // |
| -// A live holder is never replaced: the replace is conditional on the ETag of |
| -// the lease that was read as expired, so a holder that renews in between |
| -// wins and Tombstone reports it as held. |
| +// A live holder is never replaced: the write is conditional on the exact |
| +// version that was read and classified (absent, or expired with known |
| +// fields only), so a holder that renews in between, or a later release's |
| +// lock written in between, wins and Tombstone refuses. |
| func Tombstone(ctx context.Context, bucket, location, region, owner string, ttl time.Duration) error { |
| if !tombstoneOwnerOK(owner) { |
| return ErrTombstoneOwner |
| @@ -199,8 +202,7 @@ func Tombstone(ctx context.Context, bucket, location, region, owner string, ttl |
| return err |
| } |
| if len(unknown) > 0 { |
| - // A later release's lock (storage generations, spec §9 slice 0). |
| - return fmt.Errorf("tombstone lease: %w (%s; owner %q)", ErrUnknownLockField, strings.Join(unknown, ", "), cur.Owner) |
| + return unknownFieldErr(unknown, cur.Owner) |
| } |
| if !absent && !leaseFree(cur.ExpiresAt, time.Now()) { |
| if cur.Owner == owner { |
| @@ -208,26 +210,42 @@ func Tombstone(ctx context.Context, bucket, location, region, owner string, ttl |
| } |
| return heldErr(cur) |
| } |
| - // Absent or expired. AcquireLease reads again and writes conditionally: |
| - // IfNoneMatch "*" when absent, IfMatch on the ETag it reads otherwise. |
| - // If a holder appeared since our read it answers LeaseExistsError. |
| - if _, err := leaser.AcquireLease(ctx); err != nil { |
| - var lexists *litestream.LeaseExistsError |
| - if errors.As(err, &lexists) { |
| - switch lexists.Owner { |
| - case "": |
| - // The write lost its condition and AcquireLease could not |
| - // read the winner back (it was removed again, or the read |
| - // failed). There is no holder to name. |
| - return fmt.Errorf("tombstone lease: %w", ErrTombstoneChanged) |
| - case owner: |
| - return nil |
| - } |
| - return fmt.Errorf("acquire lease: %w", &HeldError{Owner: lexists.Owner, ExpiresAt: lexists.ExpiresAt}) |
| - } |
| + // Absent or expired: the write is conditional on exactly the version |
| + // just classified (IfNoneMatch "*" when absent, IfMatch on its ETag), |
| + // never on a later read of its own, so nothing written in between (a |
| + // holder, or a lock with fields this binary does not know) is replaced. |
| + next := &litestream.Lease{Generation: 1, ExpiresAt: time.Now().Add(ttl), Owner: owner} |
| + seen := hafresh.View{Absent: true} |
| + if !absent { |
| + next.Generation = cur.Generation + 1 |
| + seen = hafresh.View{ETag: cur.ETag} |
| + } |
| + _, err = putLock(ctx, leaser.Client(), leaser.Bucket, key, next, seen) |
| + if err == nil { |
| + return nil |
| + } |
| + if !preconditionFailed(err) && (absent || !lockAbsent(err)) { |
| return fmt.Errorf("tombstone lease: %w", err) |
| } |
| - return nil |
| + // The write lost its condition: read the winner back to name it. |
| + won, unknown, gone, rerr := readLockFields(ctx, leaser, key) |
| + switch { |
| + case rerr != nil || gone: |
| + // Removed again, or unreadable: there is no holder to name. |
| + return fmt.Errorf("tombstone lease: %w", ErrTombstoneChanged) |
| + case len(unknown) > 0: |
| + return unknownFieldErr(unknown, won.Owner) |
| + case won.Owner == owner: |
| + return nil |
| + } |
| + return heldErr(won) |
| +} |
| + |
| +// unknownFieldErr is Tombstone's refusal of a lock carrying carlos_ fields |
| +// this binary does not know: a later release's lock (storage generations, |
| +// spec §9 slice 0). |
| +func unknownFieldErr(unknown []string, owner string) error { |
| + return fmt.Errorf("tombstone lease: %w (%s; owner %q)", ErrUnknownLockField, strings.Join(unknown, ", "), owner) |
| } |
| // Reassert moves the expiry of a tombstone owner already holds to now+ttl: |
diff --git a/internal/lease/tombstone_test.go b/internal/lease/tombstone_test.go| index 616f0ea76..7ff69f51e 100644 |
| --- a/internal/lease/tombstone_test.go |
| +++ b/internal/lease/tombstone_test.go |
| @@ -31,9 +31,18 @@ type tombStore struct { |
| *leasetest.VersionedBucket |
| mu sync.Mutex |
| before func(method string) |
| + after func() // runs once, after the next GET of lock.json is answered |
| writes []string // "PUT" and "DELETE" requests that reached lock.json, in order |
| } |
| +// afterRead arms hook to run once, after the next GET of lock.json has |
| +// been answered and before the code under test sends anything else. |
| +func (s *tombStore) afterRead(hook func()) { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + s.after = hook |
| +} |
| + |
| // beforeWrite arms hook for the next write to lock.json, once. |
| func (s *tombStore) beforeWrite(hook func(method string)) { |
| s.mu.Lock() |
| @@ -60,6 +69,15 @@ func tombFixture(t *testing.T) *tombStore { |
| } |
| } |
| inner.ServeHTTP(w, r) |
| + if strings.HasSuffix(key, "lock.json") && r.Method == http.MethodGet { |
| + s.mu.Lock() |
| + hook := s.after |
| + s.after = nil |
| + s.mu.Unlock() |
| + if hook != nil { |
| + hook() |
| + } |
| + } |
| })) |
| t.Cleanup(srv.Close) |
| t.Setenv("AWS_ACCESS_KEY_ID", "test-key") |
| @@ -255,6 +273,44 @@ func TestTombstoneRefusesUnknownField(t *testing.T) { |
| } |
| } |
| +// Slice 0, the window: the lock Tombstone classified is replaced by a |
| +// storage-generations release marker before its write lands, either just |
| +// after the read it checked (where litestream's AcquireLease read again and |
| +// took the marker over) or just before the PUT. The write is conditional on |
| +// the exact version it checked, so it loses; the read-back names the |
| +// unknown field, and the marker stands. |
| +func TestTombstoneRefusesUnknownFieldWrittenBeforeItsWrite(t *testing.T) { |
| + marker, err := json.Marshal(map[string]any{"generation": 4, "expires_at": time.Unix(0, 0).UTC(), "owner": "released/v1;ref=b;iid=i-b;gen=1", |
| + "carlos_release_g": map[string]any{"v": 1, "box": "b", "iid": "i-b", "clean": true}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + setups := map[string]func(s *tombStore, t *testing.T){ |
| + "absent": func(s *tombStore, t *testing.T) {}, |
| + "expired": func(s *tombStore, t *testing.T) { s.putLock(t, tombLoc, "box-a:1", 3, time.Now().Add(-time.Hour)) }, |
| + } |
| + arms := map[string]func(s *tombStore, install func()){ |
| + "after the checked read": func(s *tombStore, install func()) { s.afterRead(install) }, |
| + "before the write": func(s *tombStore, install func()) { s.beforeWrite(func(string) { install() }) }, |
| + } |
| + for sname, setup := range setups { |
| + for aname, arm := range arms { |
| + t.Run(sname+", "+aname, func(t *testing.T) { |
| + s := tombFixture(t) |
| + setup(s, t) |
| + arm(s, func() { s.Put("data", tombLoc+"/lock.json", marker) }) |
| + err := lease.Tombstone(context.Background(), "data", tombLoc, tombRegion, tombMove, time.Hour) |
| + if !errors.Is(err, lease.ErrUnknownLockField) || !strings.Contains(err.Error(), "carlos_release_g") { |
| + t.Fatalf("Tombstone = %v, want ErrUnknownLockField naming carlos_release_g", err) |
| + } |
| + if _, now, _ := s.lock(t, tombLoc); !bytes.Equal(now, marker) { |
| + t.Fatalf("the marker was overwritten: %s", now) |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| // An own tombstone that has expired is free like any other: Tombstone writes |
| // a fresh one rather than reporting the dead one held. |
| func TestTombstoneRewritesItsOwnExpiredTombstone(t *testing.T) { |