From 15253151b5db113f6599d93f03c6e752e35bebca Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 27 Nov 2026 19:03:21 -0800 Subject: [PATCH] filer: delete an expired entry in a conflict-checked transaction where the store has one A write that another filer on the same store makes between the re-read and the delete is not on this filer's entry lock. On stores that refuse a commit over another writer's change (TiKV, FoundationDB) the re-read and the delete now share a transaction, so such a replacement keeps its row. Co-Authored-By: Claude Opus 5.5 --- weed/filer/filer_ttl_sweep.go | 23 +++++++++++++++++ weed/filer/filer_ttl_sweep_test.go | 40 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/weed/filer/filer_ttl_sweep.go b/weed/filer/filer_ttl_sweep.go index 516cd120f8..5ec528d57d 100644 --- a/weed/filer/filer_ttl_sweep.go +++ b/weed/filer/filer_ttl_sweep.go @@ -171,6 +171,9 @@ func (f *Filer) sweepExpiredEntriesIn(ctx context.Context, dir util.FullPath, na // deleteExpiredEntry removes the row at p only while it still holds an // expired entry, under the lock writers take, so a write that replaced the // listed entry keeps its row. A contended path is left to a later listing. +// Where the store refuses a commit over another writer's change, the re-read +// and the delete share a transaction, which keeps a replacement written +// through another filer too. func (f *Filer) deleteExpiredEntry(ctx context.Context, p util.FullPath) error { if lt := f.EntryLockTable; lt != nil { lock, acquired := lt.TryAcquireLock("deleteExpiredEntry", p, util.ExclusiveLock) @@ -179,6 +182,26 @@ func (f *Filer) deleteExpiredEntry(ctx context.Context, p util.FullPath) error { } defer lt.ReleaseLock(p, lock) } + if !f.RefusesConflictingCommits(p) || ctx.Value(storeTransactionKey{}) != nil { + return f.deleteIfExpired(ctx, p) + } + txCtx, err := f.BeginTransaction(ctx) + if err != nil { + return err + } + if err = f.deleteIfExpired(txCtx, p); err == nil { + err = f.CommitTransaction(txCtx) + } + if err != nil { + f.RollbackTransaction(txCtx) + if errors.Is(err, ErrTransactionConflict) { + return nil + } + } + return err +} + +func (f *Filer) deleteIfExpired(ctx context.Context, p util.FullPath) error { current, err := f.Store.FindEntry(ctx, p) if err != nil { if errors.Is(err, filer_pb.ErrNotFound) { diff --git a/weed/filer/filer_ttl_sweep_test.go b/weed/filer/filer_ttl_sweep_test.go index ba13d5d037..fad21f305e 100644 --- a/weed/filer/filer_ttl_sweep_test.go +++ b/weed/filer/filer_ttl_sweep_test.go @@ -220,6 +220,46 @@ func TestDeleteExpiredEntryKeepsAReplacement(t *testing.T) { } } +// Another filer on the same store does not take this filer's lock: a write it +// makes between the re-read and the delete gets the delete's commit refused. +func TestDeleteExpiredEntryKeepsAReplacementFromAnotherFiler(t *testing.T) { + f, _ := newTestFilerWithStubStore(t) + store := newVersionedStore() + f.Store = NewFilerStoreWrapper(store) + other := NewFilerStoreWrapper(store) + ctx := context.Background() + p := util.FullPath("/logs/current") + old := time.Now().Add(-time.Hour) + expired := &Entry{FullPath: p, Attr: Attr{Mode: 0o644, Crtime: old, Mtime: old, TtlSec: 120}} + + if err := other.InsertEntry(ctx, expired); err != nil { + t.Fatal(err) + } + ran := store.once(t, "r"+string(p), func() error { + return other.InsertEntry(ctx, &Entry{FullPath: p, Attr: Attr{Mode: 0o644, Crtime: time.Now(), Mtime: time.Now(), TtlSec: 120}}) + }) + if err := f.deleteExpiredEntry(ctx, p); err != nil { + t.Fatal(err) + } + if !*ran { + t.Fatal("the expired entry was not re-read in a transaction") + } + if store.getEntry(string(p)) == nil { + t.Fatal("replacement written through another filer removed") + } + + store.afterRead = nil + if err := other.InsertEntry(ctx, expired); err != nil { + t.Fatal(err) + } + if err := f.deleteExpiredEntry(ctx, p); err != nil { + t.Fatal(err) + } + if store.getEntry(string(p)) != nil { + t.Fatal("expired entry kept") + } +} + func TestSweepExpiredEntriesKeepsADirectoryAWriterHolds(t *testing.T) { f, store := newTestFilerWithStubStore(t) f.DirBucketsPath = "/buckets"