mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
11ec2458f0
commit
15253151b5
2 files changed
+63
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user