filer: a listing over a hard link no longer deadlocks a bounded SQL pool (#11118)

* filer: give the SQL stores' key-value reads their own connections

A listing holds the connection its rows are on for the whole iteration, and
FilerStoreWrapper calls maybeReadHardLink -> KvGet from inside that iteration,
so a hard-linked entry needs a second connection while the first is still busy.
Out of one bounded pool that is a deadlock: the listings fill the pool and then
wait for a connection none of them will release, and the wrapper's
context.WithoutCancel leaves the waiters without a deadline, so the filer stays
wedged rather than erroring.

The sqlite store shows it at its sharpest -- it allows a single connection, so
one listing over one hard-linked entry never returns. On postgres with
connection_max_open = 50, 60 concurrent listings over hard-linked entries made
no progress at all.

Key-value reads now run on their own pool, carved out of connection_max_open
rather than added to it, so the operator's cap still bounds what the store opens
against the database. An unbounded pool keeps a single pool: nothing can wait
there. sqlite's single connection becomes two, one per pool, and its writes get
a busy timeout so a write that meets the reader waits instead of failing.

Claude-Session: https://claude.ai/code/session_018DWwctzD4T2DmnczPRM47t

* sqlite: keep both pools on one database, whatever the dbFile spells

A dbFile that already carries URI options got a second "?" appended, which the
driver reads as part of the preceding option value, and a bare :memory: is
private to each connection, so the key-value pool would open its own empty
database and every key-value operation would fail on a missing filemeta.

Claude-Session: https://claude.ai/code/session_018DWwctzD4T2DmnczPRM47t

* sqlite: assert the busy timeout on the in-memory DSN too

Claude-Session: https://claude.ai/code/session_018DWwctzD4T2DmnczPRM47t
This commit is contained in:
Chris Lu authored and GitHub committed 2026-09-02 23:45:45 -07:00
1 parent 9b61289293
commit 9f6efc8b53
9 files changed
+228 -35

No files matched your search

@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"sync"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
@@ -29,6 +30,7 @@ type SqlGenerator interface {
type AbstractSqlStore struct {
SqlGenerator
DB *sql.DB
KvDB *sql.DB
SupportBucketTable bool
dbs map[string]bool
dbsLock sync.Mutex
@@ -97,6 +99,49 @@ func (store *AbstractSqlStore) RollbackTransaction(ctx context.Context) error {
return nil
}
// A listing holds its connection for the whole row iteration while its callback
// reads a hard link through KvGet, so both cannot come out of one bounded pool:
// the listings fill it and then wait for a connection none of them will release.
// Give the key-value reads their own slice of connection_max_open. A cap of 1 is
// the exception -- it has to become 2, or a single listing cannot finish.
func splitPoolForKv(maxOpen int) (mainOpen, kvOpen int) {
if maxOpen <= 0 {
return maxOpen, 0
}
kvOpen = min(max(maxOpen/4, 1), maxKvPoolSize)
return max(maxOpen-kvOpen, 1), kvOpen
}
const maxKvPoolSize = 8
// UseConnectionPools sizes the store's pool and, when it is bounded, opens the
// separate pool the key-value reads run on.
func (store *AbstractSqlStore) UseConnectionPools(db *sql.DB, openKv func() (*sql.DB, error), maxIdle, maxOpen, maxLifetimeSeconds int) error {
lifetime := time.Duration(maxLifetimeSeconds) * time.Second
mainOpen, kvOpen := splitPoolForKv(maxOpen)
db.SetMaxIdleConns(maxIdle)
db.SetMaxOpenConns(mainOpen)
db.SetConnMaxLifetime(lifetime)
store.DB = db
if kvOpen == 0 {
return nil
}
kvDB, err := openKv()
if err != nil {
return err
}
kvDB.SetMaxIdleConns(kvOpen)
kvDB.SetMaxOpenConns(kvOpen)
kvDB.SetConnMaxLifetime(lifetime)
store.KvDB = kvDB
return nil
}
func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.FullPath, isForChildren bool) (txOrDB TxOrDB, bucket string, shortPath util.FullPath, err error) {
shortPath = fullpath
@@ -400,6 +445,9 @@ func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, dirPath
func (store *AbstractSqlStore) Shutdown() {
store.DB.Close()
if store.KvDB != nil {
store.KvDB.Close()
}
}
func isValidBucket(bucket string) bool {