From 040ff9049c20ae541baf78299997a30ee0cc5b0b Mon Sep 17 00:00:00 2001 From: Yip Leo Date: Mon, 23 Nov 2026 02:18:25 +0800 Subject: [PATCH] filer: roll back a stream rename refused for a non-empty target (#12886) * filer: roll back a stream rename refused for a non-empty target StreamRenameEntry refuses to move a directory onto a non-empty one, but returned without ending the transaction it had begun. On a SQL store (MySQL, Postgres and the stores built on them) that transaction keeps its pooled connection: it was begun under context.Background(), so database/sql never rolls it back by itself. Every such refusal holds one more connection for the life of the filer, and once connection_max_open (50 in the scaffold) are held, store calls wait for a connection that never comes back. Roll the transaction back before returning, as the function's other refusals already do. Co-Authored-By: Claude Opus 5.5 * filer: check a stream rename's target directory inside its transaction The not-empty check listed the target under context.Background(), outside the transaction the rename had already begun. The SQLite store's main pool has a single connection, which the transaction holds, so every directory rename onto an existing directory waited for that connection forever and the filer's store stopped answering. On a bounded SQL pool, concurrent renames that each hold a transaction can wedge the same way. List through the transaction's context, as the child move already does. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Chris Lu --- weed/server/filer_grpc_server_rename.go | 3 +- .../filer_grpc_server_rename_sqlite_test.go | 50 ++++++++++ .../filer_grpc_server_rename_stream_test.go | 94 +++++++++++++++++++ weed/server/filer_grpc_server_rename_test.go | 31 ++++-- 4 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 weed/server/filer_grpc_server_rename_sqlite_test.go create mode 100644 weed/server/filer_grpc_server_rename_stream_test.go diff --git a/weed/server/filer_grpc_server_rename.go b/weed/server/filer_grpc_server_rename.go index 1de4a9c687..9992391172 100644 --- a/weed/server/filer_grpc_server_rename.go +++ b/weed/server/filer_grpc_server_rename.go @@ -140,7 +140,8 @@ func (fs *FilerServer) StreamRenameEntry(req *filer_pb.StreamRenameEntryRequest, fs.filer.RollbackTransaction(ctx) return fmt.Errorf("%s is not directory", targetDir) } - if entries, _, _ := fs.filer.ListDirectoryEntries(context.Background(), targetDir, "", false, 1, "", "", ""); len(entries) > 0 { + if entries, _, _ := fs.filer.ListDirectoryEntries(ctx, targetDir, "", false, 1, "", "", ""); len(entries) > 0 { + fs.filer.RollbackTransaction(ctx) return fmt.Errorf("%s is not empty", targetDir) } } diff --git a/weed/server/filer_grpc_server_rename_sqlite_test.go b/weed/server/filer_grpc_server_rename_sqlite_test.go new file mode 100644 index 0000000000..d454dc28d3 --- /dev/null +++ b/weed/server/filer_grpc_server_rename_sqlite_test.go @@ -0,0 +1,50 @@ +//go:build (linux || darwin || windows) && sqlite + +package weed_server + +import ( + "context" + "path/filepath" + "testing" + "time" + + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/filer/sqlite" +) + +// The SQLite store has one connection and the rename's transaction holds it, +// so a check that reads outside the transaction waits for it forever. +func TestStreamRenameOntoDirectoryOnSqlite(t *testing.T) { + store := &sqlite.SqliteStore{} + if err := store.Initialize(testConfig{"dbFile": filepath.Join(t.TempDir(), "filer.db")}, ""); err != nil { + t.Fatal(err) + } + defer store.Shutdown() + ctx := context.Background() + for _, entry := range []*filer.Entry{ + newDirectoryEntry("/a", 101), + newFileEntry("/a/y.txt", 102), + newDirectoryEntry("/e", 103), + } { + if err := store.InsertEntry(ctx, entry); err != nil { + t.Fatal(err) + } + } + + done := make(chan error, 1) + go func() { + _, err := streamRename(t, store, "a", "e") + done <- err + }() + select { + case err := <-done: + if err != nil { + t.Fatalf("StreamRenameEntry: %v", err) + } + case <-time.After(10 * time.Second): + t.Fatalf("StreamRenameEntry onto an existing directory never returned: %+v", store.DB.Stats()) + } + if _, err := store.FindEntry(ctx, "/e/y.txt"); err != nil { + t.Fatalf("moved child: %v", err) + } +} diff --git a/weed/server/filer_grpc_server_rename_stream_test.go b/weed/server/filer_grpc_server_rename_stream_test.go new file mode 100644 index 0000000000..67f487c8fe --- /dev/null +++ b/weed/server/filer_grpc_server_rename_stream_test.go @@ -0,0 +1,94 @@ +package weed_server + +import ( + "context" + "strings" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" + "google.golang.org/grpc" +) + +// renameTestStream collects what StreamRenameEntry sends. +type renameTestStream struct { + grpc.ServerStream + sent []*filer_pb.StreamRenameEntryResponse +} + +func (s *renameTestStream) Context() context.Context { return context.Background() } +func (s *renameTestStream) Send(resp *filer_pb.StreamRenameEntryResponse) error { + s.sent = append(s.sent, resp) + return nil +} + +func streamRename(t *testing.T, store filer.FilerStore, oldName, newName string) (*renameTestStream, error) { + t.Helper() + server := &FilerServer{filer: newRenameTestFiler(t, store), entryLockTable: util.NewLockTable[util.FullPath]()} + stream := &renameTestStream{} + err := server.StreamRenameEntry(&filer_pb.StreamRenameEntryRequest{ + OldDirectory: "/", + OldName: oldName, + NewDirectory: "/", + NewName: newName, + }, stream) + return stream, err +} + +// A directory renamed onto a non-empty directory is refused. The refusal has +// to end the rename's transaction: a SQL store otherwise keeps it open, and +// with it one of its pool's connections, for the life of the filer. +func TestStreamRenameOntoNonEmptyDirectoryEndsItsTransaction(t *testing.T) { + store := newRenameTestStore() + store.strictTx = true + store.entries["/a"] = newDirectoryEntry("/a", 101) + store.entries["/b"] = newDirectoryEntry("/b", 102) + store.entries["/b/x.txt"] = newFileEntry("/b/x.txt", 103) + + stream, err := streamRename(t, store, "a", "b") + if err == nil || !strings.Contains(err.Error(), "is not empty") { + t.Fatalf("StreamRenameEntry error = %v, want \"is not empty\"", err) + } + if n := store.openTransactions(); n != 0 { + t.Fatalf("%d transaction(s) left open after the refusal", n) + } + if len(stream.sent) != 0 { + t.Fatalf("sent %d responses for a refused rename", len(stream.sent)) + } + if _, err := store.FindEntry(context.Background(), "/a"); err != nil { + t.Fatalf("source after the refusal: %v", err) + } +} + +// The other ways out of StreamRenameEntry end the transaction too. +func TestStreamRenameEndsItsTransaction(t *testing.T) { + t.Run("onto a file", func(t *testing.T) { + store := newRenameTestStore() + store.strictTx = true + store.entries["/a"] = newDirectoryEntry("/a", 101) + store.entries["/c.txt"] = newFileEntry("/c.txt", 102) + if _, err := streamRename(t, store, "a", "c.txt"); err == nil || !strings.Contains(err.Error(), "is not directory") { + t.Fatalf("StreamRenameEntry error = %v, want \"is not directory\"", err) + } + if n := store.openTransactions(); n != 0 { + t.Fatalf("%d transaction(s) left open", n) + } + }) + t.Run("onto an empty directory", func(t *testing.T) { + store := newRenameTestStore() + store.strictTx = true + store.entries["/a"] = newDirectoryEntry("/a", 101) + store.entries["/a/y.txt"] = newFileEntry("/a/y.txt", 102) + store.entries["/e"] = newDirectoryEntry("/e", 103) + if _, err := streamRename(t, store, "a", "e"); err != nil { + t.Fatalf("StreamRenameEntry: %v", err) + } + if n := store.openTransactions(); n != 0 { + t.Fatalf("%d transaction(s) left open", n) + } + if _, err := store.FindEntry(context.Background(), "/e/y.txt"); err != nil { + t.Fatalf("moved child: %v", err) + } + }) +} diff --git a/weed/server/filer_grpc_server_rename_test.go b/weed/server/filer_grpc_server_rename_test.go index 5e7fbda0e9..e0dc7e3527 100644 --- a/weed/server/filer_grpc_server_rename_test.go +++ b/weed/server/filer_grpc_server_rename_test.go @@ -34,6 +34,7 @@ type renameTestStore struct { findDelay time.Duration // optional: widen check-then-act windows in tests findErr error // simulates a failing entry lookup strictTx bool // like a SQL store: calls through a committed transaction fail + openTx int // strictTx transactions begun and not yet committed or rolled back kv map[string][]byte // optional KV (hard links); nil: every key is absent kvGetErrs int // fail this many KvGet calls before answering @@ -64,20 +65,38 @@ func (s *renameTestStore) Initialize(util.Configuration, string) error { return func (s *renameTestStore) Shutdown() {} func (s *renameTestStore) BeginTransaction(ctx context.Context) (context.Context, error) { if s.strictTx { + s.mu.Lock() + s.openTx++ + s.mu.Unlock() return context.WithValue(ctx, renameTestTxKey{}, &renameTestTx{}), nil } return ctx, nil } -func (s *renameTestStore) CommitTransaction(ctx context.Context) error { + +// endTx marks the transaction in ctx committed or rolled back. +func (s *renameTestStore) endTx(ctx context.Context) { if tx, ok := ctx.Value(renameTestTxKey{}).(*renameTestTx); ok { - tx.done = true + s.mu.Lock() + if !tx.done { + s.openTx-- + tx.done = true + } + s.mu.Unlock() } +} + +func (s *renameTestStore) openTransactions() int { + s.mu.Lock() + defer s.mu.Unlock() + return s.openTx +} + +func (s *renameTestStore) CommitTransaction(ctx context.Context) error { + s.endTx(ctx) return s.commitErr } func (s *renameTestStore) RollbackTransaction(ctx context.Context) error { - if tx, ok := ctx.Value(renameTestTxKey{}).(*renameTestTx); ok { - tx.done = true - } + s.endTx(ctx) return nil } func (s *renameTestStore) KvPut(_ context.Context, key []byte, value []byte) error { @@ -300,7 +319,7 @@ func swapNotificationQueue(t *testing.T, q notification.MessageQueue) { // each, and its loop goroutines keep the whole filer reachable, so the tens of // filers this package builds would otherwise pin gigabytes for the rest of the // run - fatal on 32-bit, where that exhausts the address space. -func newRenameTestFiler(t *testing.T, store *renameTestStore) *filer.Filer { +func newRenameTestFiler(t *testing.T, store filer.FilerStore) *filer.Filer { dialOption := grpc.WithTransportCredentials(insecure.NewCredentials()) masterClient := wdclient.NewMasterClient( dialOption,