filer: refuse a non-recursive delete of a bucket that has children (#13122)

* filer: refuse a non-recursive delete of a bucket that has children

A non-recursive delete of a folder that has children fails with
ErrNonEmptyFolder: doBatchDeleteFolderMetaAndData lists the folder's
first page and refuses. For a bucket on a store that keeps each bucket
apart (leveldb3, the SQL stores with bucket tables, ArangoDB, YDB with
bucket tables), it skipped that listing, because such a store drops a
deleted bucket's children in one call and a recursive delete needs no
walk. The non-recursive delete then went through: DeleteFolderChildren
dropped the bucket's objects, the bucket entry was deleted, and
DeleteEntryMetaAndData deleted the bucket's collection, so the objects'
volumes went with them.

rmdir on a weed mount, fs.rm without -r and a filer HTTP DELETE without
recursive=true each send such a delete. On the other stores the same
request is refused.

A non-recursive delete now lists a bucket on these stores the way it
lists any other folder, and is refused when the bucket has children. An
empty bucket is still dropped by the store, which frees what the store
keeps per bucket, and a recursive delete still drops a bucket without a
walk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: say why a non-recursive bucket delete still sweeps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-30 11:50:00 -08:00
1 parent 8bb5c069ec
commit a2d6b07d3c
3 files changed
+185 -4

No files matched your search

+7 -4
View File
@@ -200,7 +200,10 @@ func (f *Filer) doBatchDeleteFolderMetaAndData(ctx context.Context, entry *Entry
var chunksToDelete, fsyncChunksToDelete []*filer_pb.FileChunk
lastFileName := ""
includeLastFile := false
listedChildren := !isDeletingBucket || !f.Store.CanDropWholeBucket()
// A store that keeps each bucket apart drops a deleted bucket whole, so
// only a non-recursive delete lists it, to refuse one that has children.
dropsWholeBucket := isDeletingBucket && f.Store.CanDropWholeBucket()
listedChildren := !dropsWholeBucket || !isRecursive
if listedChildren {
for {
entries, _, err := f.ListDirectoryEntries(ctx, entry.FullPath, lastFileName, includeLastFile, PaginationSize, "", "", "")
@@ -265,9 +268,9 @@ func (f *Filer) doBatchDeleteFolderMetaAndData(ctx context.Context, entry *Entry
glog.V(3).InfofCtx(ctx, "deleting directory %v delete chunks: %v", entry.FullPath, shouldDeleteChunks)
// a non-recursive delete already proved the folder empty above, so sweeping the
// children now can only remove entries that raced in after that listing
if isRecursive || !listedChildren {
// a non-recursive delete proved the folder empty above and sweeps only to
// free an empty bucket the store keeps apart
if isRecursive || dropsWholeBucket {
if storeDeletionErr := f.Store.DeleteFolderChildren(ctx, entry.FullPath); storeDeletionErr != nil {
return fmt.Errorf("filer store delete: %w", storeDeletionErr)
}
@@ -0,0 +1,150 @@
package filer
import (
"context"
"errors"
"os"
"reflect"
"sync"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// wholeBucketStore drops a deleted bucket's entries in one call, the way
// leveldb3 and the SQL stores with a table per bucket do.
type wholeBucketStore struct {
*hookedStore
mu sync.Mutex
dropped []util.FullPath
}
func (s *wholeBucketStore) OnBucketCreation(string) {}
func (s *wholeBucketStore) OnBucketDeletion(string) {}
func (s *wholeBucketStore) CanDropWholeBucket() bool {
return true
}
func (s *wholeBucketStore) DeleteFolderChildren(ctx context.Context, p util.FullPath) error {
s.mu.Lock()
s.dropped = append(s.dropped, p)
s.mu.Unlock()
return s.hookedStore.DeleteFolderChildren(ctx, p)
}
func (s *wholeBucketStore) droppedFolders() []util.FullPath {
s.mu.Lock()
defer s.mu.Unlock()
return append([]util.FullPath(nil), s.dropped...)
}
func newFilerOnWholeBucketStore(t *testing.T) (*Filer, *wholeBucketStore, *collectionDeleteMaster) {
t.Helper()
f, store, master := newFilerWithFakeMaster(t)
bucketStore := &wholeBucketStore{hookedStore: store}
f.Store = NewFilerStoreWrapper(bucketStore)
return f, bucketStore, master
}
func seedObject(t *testing.T, store *wholeBucketStore, path util.FullPath) {
t.Helper()
if err := store.InsertEntry(context.Background(), &Entry{
FullPath: path,
Attr: Attr{Mode: 0644},
}); err != nil {
t.Fatalf("seed object %s: %v", path, err)
}
}
// A non-recursive delete of a bucket that still holds objects is refused on a
// store that drops whole buckets, as on every other store. rmdir on a mount,
// fs.rm without -r and an HTTP DELETE without recursive=true all send one.
// The store's whole-bucket drop used to skip the listing that refuses it, so
// the delete went through: the objects' entries went with the bucket, then the
// bucket's collection was deleted, volumes and all.
func TestNonRecursiveDeleteKeepsNonEmptyBucketOnWholeBucketStore(t *testing.T) {
f, store, master := newFilerOnWholeBucketStore(t)
seedBucket(t, store.hookedStore, "/buckets/photos")
seedObject(t, store, "/buckets/photos/cat.jpg")
err := f.DeleteEntryMetaAndData(context.Background(), "/buckets/photos", false, false, true, false, nil, time.Time{})
if !errors.Is(err, ErrNonEmptyFolder) {
t.Fatalf("non-recursive delete of a non-empty bucket = %v, want ErrNonEmptyFolder", err)
}
for _, p := range []string{"/buckets/photos", "/buckets/photos/cat.jpg"} {
if store.getEntry(p) == nil {
t.Errorf("%s is gone after the refused delete", p)
}
}
if dropped := store.droppedFolders(); len(dropped) != 0 {
t.Errorf("the refused delete dropped %v", dropped)
}
select {
case call := <-master.calls:
t.Errorf("the refused delete deleted collection %q", call.name)
default:
}
}
// An empty bucket still goes the store's way: one drop of the bucket, which
// also frees whatever the store keeps per bucket, then its collection.
func TestNonRecursiveDeleteDropsEmptyBucketOnWholeBucketStore(t *testing.T) {
f, store, master := newFilerOnWholeBucketStore(t)
seedBucket(t, store.hookedStore, "/buckets/empty")
if err := f.DeleteEntryMetaAndData(context.Background(), "/buckets/empty", false, false, true, false, nil, time.Time{}); err != nil {
t.Fatalf("non-recursive delete of an empty bucket: %v", err)
}
if store.getEntry("/buckets/empty") != nil {
t.Error("the bucket entry is still there")
}
if got, want := store.droppedFolders(), []util.FullPath{"/buckets/empty"}; !reflect.DeepEqual(got, want) {
t.Errorf("dropped %v, want %v", got, want)
}
select {
case call := <-master.calls:
if call.name != "empty" {
t.Errorf("CollectionDelete = %q, want %q", call.name, "empty")
}
case <-time.After(20 * time.Second):
t.Fatal("CollectionDelete never reached the master")
}
}
// A recursive delete keeps the shortcut: the store drops the bucket with its
// objects, without the filer walking them first.
func TestRecursiveDeleteDropsNonEmptyBucketOnWholeBucketStore(t *testing.T) {
f, store, master := newFilerOnWholeBucketStore(t)
seedBucket(t, store.hookedStore, "/buckets/logs")
if err := store.InsertEntry(context.Background(), &Entry{
FullPath: "/buckets/logs/2026",
Attr: Attr{Mode: os.ModeDir | 0755},
}); err != nil {
t.Fatalf("seed directory: %v", err)
}
seedObject(t, store, "/buckets/logs/2026/app.log")
if err := f.DeleteEntryMetaAndData(context.Background(), "/buckets/logs", true, false, true, false, nil, time.Time{}); err != nil {
t.Fatalf("recursive delete of a non-empty bucket: %v", err)
}
for _, p := range []string{"/buckets/logs", "/buckets/logs/2026", "/buckets/logs/2026/app.log"} {
if store.getEntry(p) != nil {
t.Errorf("%s is still there", p)
}
}
if got, want := store.droppedFolders(), []util.FullPath{"/buckets/logs"}; !reflect.DeepEqual(got, want) {
t.Errorf("dropped %v, want the bucket alone, without a walk of its children: %v", got, want)
}
select {
case call := <-master.calls:
if call.name != "logs" {
t.Errorf("CollectionDelete = %q, want %q", call.name, "logs")
}
case <-time.After(20 * time.Second):
t.Fatal("CollectionDelete never reached the master")
}
}
@@ -2,11 +2,13 @@ package leveldb
import (
"context"
"errors"
"math"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb"
@@ -275,3 +277,29 @@ func TestUnavailableDatabaseFailsListAndDelete(t *testing.T) {
t.Fatal("delete children on a closed database succeeded")
}
}
// leveldb3 keeps each bucket in a database of its own and drops it whole when
// the bucket is deleted. A non-recursive delete of a bucket that still holds
// objects must be refused all the same, as on the other stores, and leave the
// objects where they are.
func TestNonRecursiveDeleteOfNonEmptyBucketIsRefused(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
testFiler.DirBucketsPath = "/buckets"
store := &LevelDB3Store{}
store.initialize(t.TempDir())
testFiler.SetStore(store)
ctx := context.Background()
object := util.FullPath("/buckets/photos/2026/cat.jpg")
if err := testFiler.CreateEntry(ctx, &filer.Entry{FullPath: object, Attr: filer.Attr{Mode: 0644}}, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create %s: %v", object, err)
}
err := testFiler.DeleteEntryMetaAndData(ctx, "/buckets/photos", false, false, true, false, nil, time.Time{})
if !errors.Is(err, filer.ErrNonEmptyFolder) {
t.Fatalf("non-recursive delete of a non-empty bucket = %v, want ErrNonEmptyFolder", err)
}
if _, err := testFiler.FindEntry(ctx, object); err != nil {
t.Fatalf("%s after the refused delete: %v", object, err)
}
}