From 7eeb49ea514fef5776adf56ac83cfa74f83495c8 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 22 Oct 2026 03:23:16 -0700 Subject: [PATCH] filer: evaluate a write condition on DeleteEntry under the path lock A conditional delete is now a check-then-write like UpdateEntry's: the condition is evaluated against this filer's store while the exclusive path lock is held, the same lock replays take, so it serializes against local writes and replicated events on this filer. A failed condition reports PRECONDITION_FAILED and deletes nothing; a write still in flight from a peer can slip past the check, the same window if_not_modified_after already leaves. Older filers ignore the field and keep today's floor. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- weed/server/filer_grpc_server.go | 18 +++++ .../filer_grpc_server_condition_test.go | 80 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index c338e051c5..34fb629e15 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -1049,6 +1049,24 @@ func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntr pathLock := fs.entryLockTable.AcquireLock("DeleteEntry", fullpath, util.ExclusiveLock) defer fs.entryLockTable.ReleaseLock(fullpath, pathLock) + // A condition makes the delete a check-then-write like UpdateEntry's: the + // lock serializes it against local writes and replays applied to this + // store, so only a write still in flight from a peer can slip past the + // check — the same window if_not_modified_after leaves. + if conditionIsSet(req.Condition) { + current, findErr := fs.filer.FindEntry(ctx, fullpath) + switch { + case errors.Is(findErr, filer_pb.ErrNotFound): + current = nil + case findErr != nil: + return &filer_pb.DeleteEntryResponse{}, fmt.Errorf("DeleteEntry condition check %s: %w", fullpath, findErr) + } + if !writeConditionSatisfied(req.Condition, current) { + glog.V(3).InfofCtx(ctx, "DeleteEntry %s: precondition failed: %v", fullpath, req.Condition) + return &filer_pb.DeleteEntryResponse{Error: "precondition failed", ErrorCode: filer_pb.FilerError_PRECONDITION_FAILED}, nil + } + } + if req.KeepRemoteObject { ctx = filer.WithKeepRemoteObject(ctx) } diff --git a/weed/server/filer_grpc_server_condition_test.go b/weed/server/filer_grpc_server_condition_test.go index 89086a5645..9c8f9a9970 100644 --- a/weed/server/filer_grpc_server_condition_test.go +++ b/weed/server/filer_grpc_server_condition_test.go @@ -382,3 +382,83 @@ func TestUpdateEntryConditionEnforced(t *testing.T) { t.Fatalf("update not applied, mtime %d", got) } } + +// The delete condition turns verify-then-delete into a check-then-write: the +// filer drops the entry only while it still matches the version the caller +// verified, and a stale expectation answers PRECONDITION_FAILED without +// deleting. +func TestDeleteEntryConditionEnforced(t *testing.T) { + newServer := func() (*FilerServer, *renameTestStore) { + store := newRenameTestStore() + store.entries["/test/obj"] = &filer.Entry{ + FullPath: "/test/obj", + Attr: filer.Attr{Inode: 1, Mtime: time.Unix(1700000000, 0), Mode: 0644, FileSize: 100}, + Chunks: []*filer_pb.FileChunk{chunkFid("3,01637037d6")}, + } + f := newRenameTestFiler(t, store) + return &FilerServer{filer: f, option: &FilerOption{}, entryLockTable: util.NewLockTable[util.FullPath]()}, store + } + + del := func(expected *filer_pb.Entry) *filer_pb.DeleteEntryRequest { + return &filer_pb.DeleteEntryRequest{ + Directory: "/test", + Name: "obj", + Condition: one(&filer_pb.WriteCondition_Clause{ + Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, + ExpectedEntry: expected, + }), + } + } + + t.Run("matching version deletes", func(t *testing.T) { + fs, store := newServer() + expected := store.entries["/test/obj"].ToProtoEntry() + resp, err := fs.DeleteEntry(context.Background(), del(expected)) + if err != nil || resp.Error != "" { + t.Fatalf("delete failed: %v %s", err, resp.Error) + } + if _, ok := store.entries["/test/obj"]; ok { + t.Fatal("entry must be deleted when the verified version still matches") + } + }) + + t.Run("stale version keeps the entry", func(t *testing.T) { + fs, store := newServer() + expected := store.entries["/test/obj"].ToProtoEntry() + store.entries["/test/obj"].Chunks = append(store.entries["/test/obj"].Chunks, chunkFid("3,09")) + resp, err := fs.DeleteEntry(context.Background(), del(expected)) + if err != nil { + t.Fatalf("DeleteEntry: %v", err) + } + if resp.ErrorCode != filer_pb.FilerError_PRECONDITION_FAILED { + t.Fatalf("stale version must report PRECONDITION_FAILED, got %v %q", resp.ErrorCode, resp.Error) + } + if _, ok := store.entries["/test/obj"]; !ok { + t.Fatal("stale version must leave the entry alone") + } + }) + + t.Run("vanished entry reports precondition failed", func(t *testing.T) { + fs, store := newServer() + expected := store.entries["/test/obj"].ToProtoEntry() + delete(store.entries, "/test/obj") + resp, err := fs.DeleteEntry(context.Background(), del(expected)) + if err != nil { + t.Fatalf("DeleteEntry: %v", err) + } + if resp.ErrorCode != filer_pb.FilerError_PRECONDITION_FAILED { + t.Fatalf("gone entry must report PRECONDITION_FAILED, got %v %q", resp.ErrorCode, resp.Error) + } + }) + + t.Run("unconditional delete still applies", func(t *testing.T) { + fs, store := newServer() + resp, err := fs.DeleteEntry(context.Background(), &filer_pb.DeleteEntryRequest{Directory: "/test", Name: "obj"}) + if err != nil || resp.Error != "" { + t.Fatalf("delete failed: %v %s", err, resp.Error) + } + if _, ok := store.entries["/test/obj"]; ok { + t.Fatal("unconditional delete must drop the entry") + } + }) +}