From b15183e4d387f9dd2e87f2efbcf50ab050fa4d47 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 27 Nov 2026 18:46:30 -0800 Subject: [PATCH] filer: keep a directory that holds an S3 object when its children expire An S3 key that also has keys below it is promoted to a directory row that keeps the object's data. When the last child expired, the empty-directory cleanup deleted that row and the object with it. Co-Authored-By: Claude Opus 5.5 --- weed/filer/filer.go | 4 ++++ weed/filer/filer_ttl_sweep_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/weed/filer/filer.go b/weed/filer/filer.go index e9cb5b393e..56349a7af3 100644 --- a/weed/filer/filer.go +++ b/weed/filer/filer.go @@ -1239,6 +1239,10 @@ func (f *Filer) DeleteEmptyParentDirectories(ctx context.Context, dirPath util.F return } + if isKeyObject, keyErr := f.IsDirectoryKeyObject(ctx, dirPath); keyErr != nil || isKeyObject { + return + } + // Directory is empty, try to delete it glog.V(2).InfofCtx(ctx, "DeleteEmptyParentDirectories: deleting empty directory %s", dirPath) parentDir, _ := dirPath.DirAndName() diff --git a/weed/filer/filer_ttl_sweep_test.go b/weed/filer/filer_ttl_sweep_test.go index e04dd31348..889fce1e1a 100644 --- a/weed/filer/filer_ttl_sweep_test.go +++ b/weed/filer/filer_ttl_sweep_test.go @@ -9,6 +9,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/util" ) @@ -147,3 +148,30 @@ func TestSweepsExpiredEntriesOnlyOnceForASharedStore(t *testing.T) { t.Errorf("%s: not swept here although its owner has a store of its own", ownedByPeer) } } + +func TestSweepExpiredEntriesKeepsADirectoryKeyObject(t *testing.T) { + f, store := newTestFilerWithStubStore(t) + f.DirBucketsPath = "/buckets" + f.FilerConf = NewFilerConf() + if err := f.FilerConf.SetLocationConf(&filer_pb.FilerConf_PathConf{LocationPrefix: "/buckets/b/", Ttl: "2m"}); err != nil { + t.Fatal(err) + } + old := time.Now().Add(-time.Hour) + putSweepTestEntry(t, store, "/buckets", true, 0, old) + putSweepTestEntry(t, store, "/buckets/b", true, 0, old) + reports := &Entry{FullPath: "/buckets/b/reports", Attr: Attr{Mode: os.ModeDir | 0o755, Crtime: old, Mtime: old, Mime: "text/plain"}, + Extended: map[string][]byte{s3_constants.SeaweedFSPrefixObject: []byte("true")}} + if err := store.InsertEntry(context.Background(), reports); err != nil { + t.Fatal(err) + } + putSweepTestEntry(t, store, "/buckets/b/reports/day1", false, 120, old) + + f.sweepExpiredEntries(context.Background(), "", make(map[string]time.Time), time.Now()) + + if store.getEntry("/buckets/b/reports/day1") != nil { + t.Fatal("expired child not removed") + } + if store.getEntry("/buckets/b/reports") == nil { + t.Fatal("directory holding an object removed with its last child") + } +}