Files
seaweedfs/weed/server/filer_grpc_server_rename_stream_test.go
T
040ff9049c 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-22 10:18:25 -08:00

95 lines
3.3 KiB
Go

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)
}
})
}