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>
This commit is contained in:
Chris LuandDevin committed 2026-10-22 03:23:16 -07:00
1 parent f11388a159
commit d21bbd467c
2 files changed
+32 -5

No files matched your search

+16 -5
View File
@@ -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)
}
+16
View File
@@ -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
}