Skip to content

Commit 555b88c

Browse files
committed
lncfg: use configurable global lock for postgres backends
Replace hardcoded WithGlobalLock assignment with configurable options wallet postgres backends. Also add the WithGlobalLock option to the channeldb table for postgres backends. Defaults: - channeldb: false (allow concurrent access) - wallet: true (maintain safe single-writer behavior) Users can now override these defaults via: - db.postgres.channeldb-with-global-lock - db.postgres.walletdb-with-global-lock This gives operators flexibility while maintaining safe defaults until full native SQL migration is complete.
1 parent 7cf79cd commit 555b88c

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lncfg/db.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@ func DefaultDB() *DB {
115115
},
116116
Postgres: &sqldb.PostgresConfig{
117117
MaxConnections: defaultPostgresMaxConnections,
118-
QueryConfig: *sqldb.DefaultPostgresConfig(),
118+
// Normally we don't use a global lock for channeldb
119+
// access, but if a user encounters huge concurrency
120+
// issues, they can enable this to use a global lock.
121+
ChannelDBWithGlobalLock: false,
122+
// Default to true to maintain safe single-writer
123+
// behavior until the wallet subsystem is fully
124+
// concurrent-safe.
125+
WalletDBWithGlobalLock: true,
126+
QueryConfig: *sqldb.DefaultPostgresConfig(),
119127
},
120128
Sqlite: &sqldb.SqliteConfig{
121129
MaxConnections: defaultSqliteMaxConnections,
@@ -400,9 +408,15 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
400408
// users to native SQL.
401409
postgresConfig := GetPostgresConfigKVDB(db.Postgres)
402410

411+
// Create a separate config for channeldb with the global lock
412+
// setting if configured.
413+
postgresConfigChannelDB := GetPostgresConfigKVDB(db.Postgres)
414+
postgresConfigChannelDB.WithGlobalLock = db.Postgres.
415+
ChannelDBWithGlobalLock
416+
403417
postgresBackend, err := kvdb.Open(
404418
kvdb.PostgresBackendName, ctx,
405-
postgresConfig, NSChannelDB,
419+
postgresConfigChannelDB, NSChannelDB,
406420
)
407421
if err != nil {
408422
return nil, fmt.Errorf("error opening postgres graph "+
@@ -450,14 +464,11 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
450464
}
451465
closeFuncs[NSTowerServerDB] = postgresTowerServerBackend.Close
452466

453-
// The wallet subsystem is still not robust enough to run it
454-
// without a single writer in postgres therefore we create a
455-
// new config with the global lock enabled.
456-
//
457-
// NOTE: This is a temporary measure and should be removed as
458-
// soon as the wallet code is more robust.
467+
// Create a separate config for wallet with the global lock
468+
// setting if configured.
459469
postgresConfigWalletDB := GetPostgresConfigKVDB(db.Postgres)
460-
postgresConfigWalletDB.WithGlobalLock = true
470+
postgresConfigWalletDB.WithGlobalLock = db.Postgres.
471+
WalletDBWithGlobalLock
461472

462473
postgresWalletBackend, err := kvdb.Open(
463474
kvdb.PostgresBackendName, ctx,

sample-lnd.conf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,22 @@
16161616
; Whether to skip executing schema migrations.
16171617
; db.postgres.skipmigrations=false
16181618

1619+
; Use a global lock for channeldb access. This ensures only a single writer at
1620+
; a time but reduces concurrency. This is a temporary workaround until the
1621+
; revocation log is migrated to native SQL.
1622+
; Default:
1623+
; db.postgres.channeldb-with-global-lock=false
1624+
; Example:
1625+
; db.postgres.channeldb-with-global-lock=true
1626+
1627+
; Use a global lock for wallet database access. This ensures only a single
1628+
; writer at a time but reduces concurrency. This is a temporary workaround
1629+
; until the wallet is fully concurrent-safe.
1630+
; Default:
1631+
; db.postgres.walletdb-with-global-lock=true
1632+
; Example:
1633+
; db.postgres.walletdb-with-global-lock=false
1634+
16191635
; The maximum number of elements to use in a native-SQL batch query IN clause.
16201636
; db.postgres.query.max-batch-size=5000
16211637

0 commit comments

Comments
 (0)