Skip to content

Commit 4f051d9

Browse files
committed
lncfg+scripts: 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. Moreover exclude db.postgres.walletdb-with-global-lock check in the sample config file script. We cannot easily check the correct default because we set it later in the LND startup sequence so we exclude it.
1 parent 2e16efe commit 4f051d9

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

docs/postgres.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ db.postgres.timeout=0
4242
Connection timeout is disabled, to account for situations where the database
4343
might be slow for unexpected reasons.
4444

45+
Moreover for particular kv tables we also add the option to access the
46+
tables via a global lock (single wirter). This is a temorpary measure until
47+
these particular tables have a native sql schema. This helps to mitigate
48+
resource exhaustion in case LND experiencing high concurrent load:
49+
50+
* `db.postgres.walletdb-with-global-lock=true` to run LND with a single writer
51+
for the walletdb_kv table (default is true).
52+
* `db.postgres.channeldb-with-global-lock=false` to run the channeldb_kv table
53+
with a single writer (default is false).
54+
4555
## Important note about replication
4656

4757
In case a replication architecture is planned, streaming replication should be avoided, as the master does not verify the replica is indeed identical, but it will only forward the edits queue, and let the slave catch up autonomously; synchronous mode, albeit slower, is paramount for `lnd` data integrity across the copies, as it will finalize writes only after the slave confirmed successful replication.

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 upgraded to
124+
// a native sql schema.
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,17 @@
16251625
; Whether to skip executing schema migrations.
16261626
; db.postgres.skipmigrations=false
16271627

1628+
; Use a global lock for channeldb access. This ensures only a single writer at
1629+
; a time but reduces concurrency. This is a temporary workaround until the
1630+
; revocation log is migrated to native SQL.
1631+
; db.postgres.channeldb-with-global-lock=false
1632+
1633+
1634+
; Use a global lock for wallet database access. This is a temporary workaround
1635+
; until the wallet subsystem is upgraded to a native sql schema.
1636+
; db.postgres.walletdb-with-global-lock=true
1637+
1638+
16281639
; The maximum number of elements to use in a native-SQL batch query IN clause.
16291640
; db.postgres.query.max-batch-size=5000
16301641

scripts/check-sample-lnd-conf.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ OPTIONS_NO_LND_DEFAULT_VALUE_CHECK="channel-max-fee-exposure adminmacaroonpath \
5959
backupfilepath maxchansize bitcoin.chaindir bitcoin.defaultchanconfs \
6060
bitcoin.defaultremotedelay bitcoin.dnsseed signrpc.signermacaroonpath \
6161
walletrpc.walletkitmacaroonpath chainrpc.notifiermacaroonpath \
62-
routerrpc.routermacaroonpath"
62+
routerrpc.routermacaroonpath db.postgres.walletdb-with-global-lock"
6363

6464

6565
# EXITCODE is returned at the end after all checks are performed and set to 1

0 commit comments

Comments
 (0)