From d21bbd467ce2968b6d762a649fa6dc9e58960a27 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 22 Oct 2026 03:23:16 -0700 Subject: [PATCH] volume.fsck: hold the verified entry version on empty-directory deletes deleteEmptyDirectory carries the looked-up entry as an IF_ENTRY_EQUAL clause, so a directory recreated or rewritten after the fsck lookup is kept even when the change fits inside the same mtime second. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- weed/shell/command_volume_fsck.go | 21 ++++++++++++++++----- weed/shell/command_volume_fsck_test.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/weed/shell/command_volume_fsck.go b/weed/shell/command_volume_fsck.go index 7578c71823..f2d3286b8e 100644 --- a/weed/shell/command_volume_fsck.go +++ b/weed/shell/command_volume_fsck.go @@ -838,8 +838,8 @@ func (c *commandVolumeFsck) purgeEmptyDirectories() { if mtime <= 0 || mtime >= time.Now().Add(-directoryQuietPeriod).Unix() { continue } - if err := c.deleteEmptyDirectory(dir, mtime); err != nil { - if !errors.Is(err, filer.ErrNonEmptyFolder) { + if err := c.deleteEmptyDirectory(dir, entry); err != nil { + if !errors.Is(err, filer.ErrNonEmptyFolder) && !errors.Is(err, errDeleteEntryChanged) { fmt.Fprintf(c.writer, "delete empty directory %s: %v\n", dir, err) } continue @@ -848,20 +848,31 @@ func (c *commandVolumeFsck) purgeEmptyDirectories() { } } -// deleteEmptyDirectory deletes dir unless it changed since it was looked up at mtime, +// errDeleteEntryChanged reports a delete skipped because the entry no longer +// matches the version the caller verified. +var errDeleteEntryChanged = errors.New("entry changed since verification") + +// deleteEmptyDirectory deletes dir unless it changed since it was looked up, // so a directory promoted to an S3 object meanwhile survives. The delete is not // recursive, leaving the filer to reject a directory that is not empty. -func (c *commandVolumeFsck) deleteEmptyDirectory(dir util.FullPath, mtime int64) error { +func (c *commandVolumeFsck) deleteEmptyDirectory(dir util.FullPath, expected *filer_pb.Entry) error { parent, name := dir.DirAndName() return c.env.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { resp, err := client.DeleteEntry(context.Background(), &filer_pb.DeleteEntryRequest{ Directory: parent, Name: name, - IfNotModifiedAfter: mtime, + IfNotModifiedAfter: expected.Attributes.GetMtime(), + Condition: &filer_pb.WriteCondition{Clauses: []*filer_pb.WriteCondition_Clause{{ + Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, + ExpectedEntry: expected, + }}}, }) if err != nil { return err } + if resp.ErrorCode == filer_pb.FilerError_PRECONDITION_FAILED { + return errDeleteEntryChanged + } if resp.Error != "" { return filer.DeleteEntryError(resp.Error) } diff --git a/weed/shell/command_volume_fsck_test.go b/weed/shell/command_volume_fsck_test.go index 50bdd0f8fa..d36d6d9e5c 100644 --- a/weed/shell/command_volume_fsck_test.go +++ b/weed/shell/command_volume_fsck_test.go @@ -19,6 +19,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/proto" ) func TestVolumeFsckCanPurgeDirectory(t *testing.T) { @@ -100,6 +101,21 @@ func (s *stubFilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_p func (s *stubFilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (*filer_pb.DeleteEntryResponse, error) { fullPath := string(util.NewFullPath(req.Directory, req.Name)) entry, found := s.entries[fullPath] + // mirror the real filer: the condition is checked before the delete runs, + // so a stale expectation answers PRECONDITION_FAILED — even for a path + // that is already gone + for _, cl := range req.GetCondition().GetClauses() { + if cl.GetKind() != filer_pb.WriteCondition_IF_ENTRY_EQUAL { + continue + } + expected := proto.Clone(cl.GetExpectedEntry()).(*filer_pb.Entry) + filer_pb.BeforeEntrySerialization(expected.GetChunks()) + stored := proto.Clone(entry).(*filer_pb.Entry) + filer_pb.BeforeEntrySerialization(stored.GetChunks()) + if !proto.Equal(stored, expected) { + return &filer_pb.DeleteEntryResponse{Error: "precondition failed", ErrorCode: filer_pb.FilerError_PRECONDITION_FAILED}, nil + } + } if !found { return &filer_pb.DeleteEntryResponse{Error: filer_pb.ErrNotFound.Error()}, nil }