mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 22:21:18 +02:00
kvdb: add timeout options for bbolt (#4787)
* mod: bump btcwallet version to accept db timeout * btcwallet: add DBTimeOut in config * kvdb: add database timeout option for bbolt This commit adds a DBTimeout option in bbolt config. The relevant functions walletdb.Open/Create are updated to use this config. In addition, the bolt compacter also applies the new timeout option. * channeldb: add DBTimeout in db options This commit adds the DBTimeout option for channeldb. A new unit test file is created to test the default options. In addition, the params used in kvdb.Create inside channeldb_test is updated with a DefaultDBTimeout value. * contractcourt+routing: use DBTimeout in kvdb This commit touches multiple test files in contractcourt and routing. The call of function kvdb.Create and kvdb.Open are now updated with the new param DBTimeout, using the default value kvdb.DefaultDBTimeout. * lncfg: add DBTimeout option in db config The DBTimeout option is added to db config. A new unit test is added to check the default DB config is created as expected. * migration: add DBTimeout param in kvdb.Create/kvdb.Open * keychain: update tests to use DBTimeout param * htlcswitch+chainreg: add DBTimeout option * macaroons: support DBTimeout config in creation This commit adds the DBTimeout during the creation of macaroons.db. The usage of kvdb.Create and kvdb.Open in its tests are updated with a timeout value using kvdb.DefaultDBTimeout. * walletunlocker: add dbTimeout option in UnlockerService This commit adds a new param, dbTimeout, during the creation of UnlockerService. This param is then passed to wallet.NewLoader inside various service calls, specifying a timeout value to be used when opening the bbolt. In addition, the macaroonService is also called with this dbTimeout param. * watchtower/wtdb: add dbTimeout param during creation This commit adds the dbTimeout param for the creation of both watchtower.db and wtclient.db. * multi: add db timeout param for walletdb.Create This commit adds the db timeout param for the function call walletdb.Create. It touches only the test files found in chainntnfs, lnwallet, and routing. * lnd: pass DBTimeout config to relevant services This commit enables lnd to pass the DBTimeout config to the following services/config/functions, - chainControlConfig - walletunlocker - wallet.NewLoader - macaroons - watchtower In addition, the usage of wallet.Create is updated too. * sample-config: add dbtimeout option
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
||||
@@ -129,8 +130,10 @@ type ClientDB struct {
|
||||
// migrations will be applied before returning. Any attempt to open a database
|
||||
// with a version number higher that the latest version will fail to prevent
|
||||
// accidental reversion.
|
||||
func OpenClientDB(dbPath string) (*ClientDB, error) {
|
||||
bdb, firstInit, err := createDBIfNotExist(dbPath, clientDBName)
|
||||
func OpenClientDB(dbPath string, dbTimeout time.Duration) (*ClientDB, error) {
|
||||
bdb, firstInit, err := createDBIfNotExist(
|
||||
dbPath, clientDBName, dbTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/watchtower/blob"
|
||||
"github.com/lightningnetwork/lnd/watchtower/wtclient"
|
||||
@@ -783,7 +784,9 @@ func TestClientDB(t *testing.T) {
|
||||
err)
|
||||
}
|
||||
|
||||
db, err := wtdb.OpenClientDB(path)
|
||||
db, err := wtdb.OpenClientDB(
|
||||
path, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
os.RemoveAll(path)
|
||||
t.Fatalf("unable to open db: %v", err)
|
||||
@@ -806,14 +809,18 @@ func TestClientDB(t *testing.T) {
|
||||
err)
|
||||
}
|
||||
|
||||
db, err := wtdb.OpenClientDB(path)
|
||||
db, err := wtdb.OpenClientDB(
|
||||
path, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
os.RemoveAll(path)
|
||||
t.Fatalf("unable to open db: %v", err)
|
||||
}
|
||||
db.Close()
|
||||
|
||||
db, err = wtdb.OpenClientDB(path)
|
||||
db, err = wtdb.OpenClientDB(
|
||||
path, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
os.RemoveAll(path)
|
||||
t.Fatalf("unable to reopen db: %v", err)
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
||||
)
|
||||
@@ -49,7 +50,9 @@ func fileExists(path string) bool {
|
||||
// one doesn't exist. The boolean returned indicates if the database did not
|
||||
// exist before, or if it has been created but no version metadata exists within
|
||||
// it.
|
||||
func createDBIfNotExist(dbPath, name string) (kvdb.Backend, bool, error) {
|
||||
func createDBIfNotExist(dbPath, name string,
|
||||
dbTimeout time.Duration) (kvdb.Backend, bool, error) {
|
||||
|
||||
path := filepath.Join(dbPath, name)
|
||||
|
||||
// If the database file doesn't exist, this indicates we much initialize
|
||||
@@ -65,7 +68,9 @@ func createDBIfNotExist(dbPath, name string) (kvdb.Backend, bool, error) {
|
||||
|
||||
// Specify bbolt freelist options to reduce heap pressure in case the
|
||||
// freelist grows to be very large.
|
||||
bdb, err := kvdb.Create(kvdb.BoltBackendName, path, true)
|
||||
bdb, err := kvdb.Create(
|
||||
kvdb.BoltBackendName, path, true, dbTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package wtdb
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
@@ -66,8 +67,10 @@ type TowerDB struct {
|
||||
// migrations will be applied before returning. Any attempt to open a database
|
||||
// with a version number higher that the latest version will fail to prevent
|
||||
// accidental reversion.
|
||||
func OpenTowerDB(dbPath string) (*TowerDB, error) {
|
||||
bdb, firstInit, err := createDBIfNotExist(dbPath, towerDBName)
|
||||
func OpenTowerDB(dbPath string, dbTimeout time.Duration) (*TowerDB, error) {
|
||||
bdb, firstInit, err := createDBIfNotExist(
|
||||
dbPath, towerDBName, dbTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
||||
"github.com/lightningnetwork/lnd/watchtower"
|
||||
"github.com/lightningnetwork/lnd/watchtower/blob"
|
||||
"github.com/lightningnetwork/lnd/watchtower/wtdb"
|
||||
@@ -642,7 +643,9 @@ func TestTowerDB(t *testing.T) {
|
||||
err)
|
||||
}
|
||||
|
||||
db, err := wtdb.OpenTowerDB(path)
|
||||
db, err := wtdb.OpenTowerDB(
|
||||
path, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
os.RemoveAll(path)
|
||||
t.Fatalf("unable to open db: %v", err)
|
||||
@@ -665,7 +668,9 @@ func TestTowerDB(t *testing.T) {
|
||||
err)
|
||||
}
|
||||
|
||||
db, err := wtdb.OpenTowerDB(path)
|
||||
db, err := wtdb.OpenTowerDB(
|
||||
path, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
os.RemoveAll(path)
|
||||
t.Fatalf("unable to open db: %v", err)
|
||||
@@ -675,7 +680,9 @@ func TestTowerDB(t *testing.T) {
|
||||
// Open the db again, ensuring we test a
|
||||
// different path during open and that all
|
||||
// buckets remain initialized.
|
||||
db, err = wtdb.OpenTowerDB(path)
|
||||
db, err = wtdb.OpenTowerDB(
|
||||
path, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
os.RemoveAll(path)
|
||||
t.Fatalf("unable to open db: %v", err)
|
||||
|
Reference in New Issue
Block a user