mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-02 18:02:29 +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:
@@ -244,6 +244,7 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) {
|
||||
NoFreelistSync: opts.NoFreelistSync,
|
||||
AutoCompact: opts.AutoCompact,
|
||||
AutoCompactMinAge: opts.AutoCompactMinAge,
|
||||
DBTimeout: opts.DBTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@@ -808,7 +808,9 @@ func makeFwdPkgDB(t *testing.T, path string) kvdb.Backend { // nolint:unparam
|
||||
path = filepath.Join(path, "fwdpkg.db")
|
||||
}
|
||||
|
||||
bdb, err := kvdb.Create(kvdb.BoltBackendName, path, true)
|
||||
bdb, err := kvdb.Create(
|
||||
kvdb.BoltBackendName, path, true, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to open boltdb: %v", err)
|
||||
}
|
||||
|
@@ -64,6 +64,10 @@ type BoltBackendConfig struct {
|
||||
// since a bolt database file was last compacted for the compaction to
|
||||
// be considered again.
|
||||
AutoCompactMinAge time.Duration
|
||||
|
||||
// DBTimeout specifies the timeout value to use when opening the wallet
|
||||
// database.
|
||||
DBTimeout time.Duration
|
||||
}
|
||||
|
||||
// GetBoltBackend opens (or creates if doesn't exits) a bbolt backed database
|
||||
@@ -79,7 +83,10 @@ func GetBoltBackend(cfg *BoltBackendConfig) (Backend, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return Create(BoltBackendName, dbFilePath, cfg.NoFreelistSync)
|
||||
return Create(
|
||||
BoltBackendName, dbFilePath,
|
||||
cfg.NoFreelistSync, cfg.DBTimeout,
|
||||
)
|
||||
}
|
||||
|
||||
// This is an existing database. We might want to compact it on startup
|
||||
@@ -90,7 +97,10 @@ func GetBoltBackend(cfg *BoltBackendConfig) (Backend, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return Open(BoltBackendName, dbFilePath, cfg.NoFreelistSync)
|
||||
return Open(
|
||||
BoltBackendName, dbFilePath,
|
||||
cfg.NoFreelistSync, cfg.DBTimeout,
|
||||
)
|
||||
}
|
||||
|
||||
// compactAndSwap will attempt to write a new temporary DB file to disk with
|
||||
@@ -156,8 +166,9 @@ func compactAndSwap(cfg *BoltBackendConfig) error {
|
||||
_ = os.Remove(tempDestFilePath)
|
||||
}()
|
||||
c := &compacter{
|
||||
srcPath: sourceFilePath,
|
||||
dstPath: tempDestFilePath,
|
||||
srcPath: sourceFilePath,
|
||||
dstPath: tempDestFilePath,
|
||||
dbTimeout: cfg.DBTimeout,
|
||||
}
|
||||
initialSize, newSize, err := c.execute()
|
||||
if err != nil {
|
||||
@@ -235,6 +246,7 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
|
||||
DBPath: path,
|
||||
DBFileName: name,
|
||||
NoFreelistSync: true,
|
||||
DBTimeout: DefaultDBTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/lightningnetwork/lnd/healthcheck"
|
||||
"go.etcd.io/bbolt"
|
||||
@@ -38,6 +39,9 @@ type compacter struct {
|
||||
srcPath string
|
||||
dstPath string
|
||||
txMaxSize int64
|
||||
|
||||
// dbTimeout specifies the timeout value used when opening the db.
|
||||
dbTimeout time.Duration
|
||||
}
|
||||
|
||||
// execute opens the source and destination databases and then compacts the
|
||||
@@ -79,6 +83,7 @@ func (cmd *compacter) execute() (int64, int64, error) {
|
||||
// possible freelist sync problems.
|
||||
src, err := bbolt.Open(cmd.srcPath, 0444, &bbolt.Options{
|
||||
ReadOnly: true,
|
||||
Timeout: cmd.dbTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("error opening source database: %v",
|
||||
@@ -91,7 +96,9 @@ func (cmd *compacter) execute() (int64, int64, error) {
|
||||
}()
|
||||
|
||||
// Open destination database.
|
||||
dst, err := bbolt.Open(cmd.dstPath, fi.Mode(), nil)
|
||||
dst, err := bbolt.Open(cmd.dstPath, fi.Mode(), &bbolt.Options{
|
||||
Timeout: cmd.dbTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("error opening destination database: "+
|
||||
"%v", err)
|
||||
|
@@ -17,6 +17,10 @@ const (
|
||||
// have passed since a bolt database file was last compacted for the
|
||||
// compaction to be considered again.
|
||||
DefaultBoltAutoCompactMinAge = time.Hour * 24 * 7
|
||||
|
||||
// DefaultDBTimeout specifies the default timeout value when opening
|
||||
// the bbolt database.
|
||||
DefaultDBTimeout = time.Second * 60
|
||||
)
|
||||
|
||||
// BoltConfig holds bolt configuration.
|
||||
@@ -26,6 +30,8 @@ type BoltConfig struct {
|
||||
AutoCompact bool `long:"auto-compact" description:"Whether the databases used within lnd should automatically be compacted on every startup (and if the database has the configured minimum age). This is disabled by default because it requires additional disk space to be available during the compaction that is freed afterwards. In general compaction leads to smaller database files."`
|
||||
|
||||
AutoCompactMinAge time.Duration `long:"auto-compact-min-age" description:"How long ago the last compaction of a database file must be for it to be considered for auto compaction again. Can be set to 0 to compact on every startup."`
|
||||
|
||||
DBTimeout time.Duration `long:"dbtimeout" description:"Specify the timeout value used when opening the database."`
|
||||
}
|
||||
|
||||
// EtcdConfig holds etcd configuration.
|
||||
|
@@ -55,7 +55,10 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) {
|
||||
|
||||
// Specify bbolt freelist options to reduce heap pressure in case the
|
||||
// freelist grows to be very large.
|
||||
bdb, err := kvdb.Open(kvdb.BoltBackendName, path, opts.NoFreelistSync)
|
||||
bdb, err := kvdb.Open(
|
||||
kvdb.BoltBackendName, path,
|
||||
opts.NoFreelistSync, opts.DBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -84,7 +87,9 @@ func createChannelDB(dbPath string) error {
|
||||
}
|
||||
|
||||
path := filepath.Join(dbPath, dbName)
|
||||
bdb, err := kvdb.Create(kvdb.BoltBackendName, path, false)
|
||||
bdb, err := kvdb.Create(
|
||||
kvdb.BoltBackendName, path, false, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package migration_01_to_11
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
// DefaultRejectCacheSize is the default number of rejectCacheEntries to
|
||||
// cache for use in the rejection cache of incoming gossip traffic. This
|
||||
@@ -10,6 +12,10 @@ const (
|
||||
// in order to reply to gossip queries. This produces a cache size of
|
||||
// around 40MB.
|
||||
DefaultChannelCacheSize = 20000
|
||||
|
||||
// DefaultDBTimeout specifies the default timeout value when opening
|
||||
// the bbolt database.
|
||||
DefaultDBTimeout = time.Second * 60
|
||||
)
|
||||
|
||||
// Options holds parameters for tuning and customizing a channeldb.DB.
|
||||
@@ -26,6 +32,10 @@ type Options struct {
|
||||
// freelist to disk, resulting in improved performance at the expense of
|
||||
// increased startup time.
|
||||
NoFreelistSync bool
|
||||
|
||||
// DBTimeout specifies the timeout value to use when opening the wallet
|
||||
// database.
|
||||
DBTimeout time.Duration
|
||||
}
|
||||
|
||||
// DefaultOptions returns an Options populated with default values.
|
||||
@@ -34,6 +44,7 @@ func DefaultOptions() Options {
|
||||
RejectCacheSize: DefaultRejectCacheSize,
|
||||
ChannelCacheSize: DefaultChannelCacheSize,
|
||||
NoFreelistSync: true,
|
||||
DBTimeout: DefaultDBTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -20,7 +20,9 @@ func MakeDB() (kvdb.Backend, func(), error) {
|
||||
}
|
||||
|
||||
dbPath := file.Name()
|
||||
db, err := kvdb.Open(kvdb.BoltBackendName, dbPath, true)
|
||||
db, err := kvdb.Open(
|
||||
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@@ -50,6 +50,7 @@ func DefaultOptions() Options {
|
||||
NoFreelistSync: true,
|
||||
AutoCompact: false,
|
||||
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
|
||||
DBTimeout: kvdb.DefaultDBTimeout,
|
||||
},
|
||||
RejectCacheSize: DefaultRejectCacheSize,
|
||||
ChannelCacheSize: DefaultChannelCacheSize,
|
||||
|
28
channeldb/options_test.go
Normal file
28
channeldb/options_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package channeldb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestDefaultOptions tests the default options are created as intended.
|
||||
func TestDefaultOptions(t *testing.T) {
|
||||
|
||||
opts := channeldb.DefaultOptions()
|
||||
|
||||
require.True(t, opts.NoFreelistSync)
|
||||
require.False(t, opts.AutoCompact)
|
||||
require.Equal(
|
||||
t, kvdb.DefaultBoltAutoCompactMinAge, opts.AutoCompactMinAge,
|
||||
)
|
||||
require.Equal(t, kvdb.DefaultDBTimeout, opts.DBTimeout)
|
||||
require.Equal(
|
||||
t, channeldb.DefaultRejectCacheSize, opts.RejectCacheSize,
|
||||
)
|
||||
require.Equal(
|
||||
t, channeldb.DefaultChannelCacheSize, opts.ChannelCacheSize,
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user