bbolt sync-freelist ignored

This commit is contained in:
ErikEk 2021-07-15 21:42:05 +02:00
parent 3800cd9433
commit d5463818a8
8 changed files with 47 additions and 18 deletions

View file

@ -13,6 +13,7 @@ import (
"os"
"os/user"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
@ -604,7 +605,9 @@ func LoadConfig(interceptor signal.Interceptor) (*Config, error) {
// Next, load any additional configuration options from the file.
var configFileError error
cfg := preCfg
if err := flags.IniParse(configFilePath, &cfg); err != nil {
fileParser := flags.NewParser(&cfg, flags.Default)
err := flags.NewIniParser(fileParser).ParseFile(configFilePath)
if err != nil {
// If it's a parsing related error, then we'll return
// immediately, otherwise we can proceed as possibly the config
// file doesn't exist which is OK.
@ -617,12 +620,14 @@ func LoadConfig(interceptor signal.Interceptor) (*Config, error) {
// Finally, parse the remaining command line options again to ensure
// they take precedence.
if _, err := flags.Parse(&cfg); err != nil {
flagParser := flags.NewParser(&cfg, flags.Default)
if _, err := flagParser.Parse(); err != nil {
return nil, err
}
// Make sure everything we just loaded makes sense.
cleanCfg, err := ValidateConfig(cfg, usageMessage, interceptor)
cleanCfg, err := ValidateConfig(cfg, usageMessage, interceptor,
fileParser, flagParser)
if err != nil {
return nil, err
}
@ -641,8 +646,8 @@ func LoadConfig(interceptor signal.Interceptor) (*Config, error) {
// illegal values or combination of values are set. All file system paths are
// normalized. The cleaned up config is returned on success.
func ValidateConfig(cfg Config, usageMessage string,
interceptor signal.Interceptor) (*Config, error) {
interceptor signal.Interceptor, fileParser,
flagParser *flags.Parser) (*Config, error) {
// If the provided lnd directory is not the default, we'll modify the
// path to all of the files and directories that will live within it.
lndDir := CleanAndExpandPath(cfg.LndDir)
@ -688,6 +693,26 @@ func ValidateConfig(cfg Config, usageMessage string,
return nil
}
// IsSet returns true if an option has been set in either the config
// file or by a flag.
isSet := func(field string) bool {
fieldname, ok := reflect.TypeOf(Config{}).FieldByName(field)
if !ok {
fmt.Fprintf(os.Stderr, "could not find field %s\n", field)
return false
}
long, ok := fieldname.Tag.Lookup("long")
if !ok {
fmt.Fprintf(os.Stderr,
"field %s does not have a long tag\n", field)
return false
}
return fileParser.FindOptionByLongName(long).IsSet() ||
flagParser.FindOptionByLongName(long).IsSet()
}
// As soon as we're done parsing configuration options, ensure all paths
// to directories and files are cleaned and expanded before attempting
// to use them later on.
@ -1444,8 +1469,8 @@ func ValidateConfig(cfg Config, usageMessage string,
// parameters. However we want to also allow existing users to use the
// value on the top-level config. If the outer config value is set,
// then we'll use that directly.
if cfg.SyncFreelist {
cfg.DB.Bolt.SyncFreelist = cfg.SyncFreelist
if isSet("SyncFreelist") {
cfg.DB.Bolt.NoFreelistSync = !cfg.SyncFreelist
}
// Ensure that the user hasn't chosen a remote-max-htlc value greater

View file

@ -357,6 +357,9 @@ you.
* [Fix crash with empty AMP or MPP record in
invoice](https://github.com/lightningnetwork/lnd/pull/5743).
* [Config setting sync-freelist was ignored in certain
cases](https://github.com/lightningnetwork/lnd/pull/5527).
* The underlying gRPC connection of a WebSocket is now [properly closed when the
WebSocket end of a connection is
closed](https://github.com/lightningnetwork/lnd/pull/5683). A bug with the

View file

@ -50,7 +50,7 @@ func NewBoltBackendCreator(dbPath,
cfg := &kvdb.BoltBackendConfig{
DBPath: dbPath,
DBFileName: dbFileName,
NoFreelistSync: !boltCfg.SyncFreelist,
NoFreelistSync: boltCfg.NoFreelistSync,
AutoCompact: boltCfg.AutoCompact,
AutoCompactMinAge: boltCfg.AutoCompactMinAge,
DBTimeout: boltCfg.DBTimeout,

View file

@ -30,7 +30,7 @@ const (
// BoltConfig holds bolt configuration.
type BoltConfig struct {
SyncFreelist bool `long:"nofreelistsync" description:"Whether the databases used within lnd should sync their freelist to disk. This is disabled by default resulting in improved memory performance during operation, but with an increase in startup time."`
NoFreelistSync bool `long:"nofreelistsync" description:"Whether the databases used within lnd should sync their freelist to disk. This is set to true by default, meaning we don't sync the free-list resulting in imporved memory performance during operation, but with an increase in startup time."`
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."`

View file

@ -65,6 +65,7 @@ func DefaultDB() *DB {
Backend: BoltBackend,
BatchCommitInterval: DefaultBatchCommitInterval,
Bolt: &kvdb.BoltConfig{
NoFreelistSync: true,
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout,
},
@ -364,7 +365,7 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DBPath: chanDBPath,
DBFileName: channelDBName,
DBTimeout: db.Bolt.DBTimeout,
NoFreelistSync: !db.Bolt.SyncFreelist,
NoFreelistSync: db.Bolt.NoFreelistSync,
AutoCompact: db.Bolt.AutoCompact,
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
})
@ -377,7 +378,7 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DBPath: walletDBPath,
DBFileName: macaroonDBName,
DBTimeout: db.Bolt.DBTimeout,
NoFreelistSync: !db.Bolt.SyncFreelist,
NoFreelistSync: db.Bolt.NoFreelistSync,
AutoCompact: db.Bolt.AutoCompact,
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
})
@ -390,7 +391,7 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DBPath: chanDBPath,
DBFileName: decayedLogDbName,
DBTimeout: db.Bolt.DBTimeout,
NoFreelistSync: !db.Bolt.SyncFreelist,
NoFreelistSync: db.Bolt.NoFreelistSync,
AutoCompact: db.Bolt.AutoCompact,
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
})
@ -408,7 +409,7 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DBPath: chanDBPath,
DBFileName: towerClientDBName,
DBTimeout: db.Bolt.DBTimeout,
NoFreelistSync: !db.Bolt.SyncFreelist,
NoFreelistSync: db.Bolt.NoFreelistSync,
AutoCompact: db.Bolt.AutoCompact,
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
},
@ -429,7 +430,7 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DBPath: towerServerDBPath,
DBFileName: towerServerDBName,
DBTimeout: db.Bolt.DBTimeout,
NoFreelistSync: !db.Bolt.SyncFreelist,
NoFreelistSync: db.Bolt.NoFreelistSync,
AutoCompact: db.Bolt.AutoCompact,
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
},
@ -456,7 +457,7 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
// method so we don't need to add anything to our map (in fact
// nothing is opened just yet).
WalletDB: btcwallet.LoaderWithLocalWalletDB(
walletDBPath, !db.Bolt.SyncFreelist, db.Bolt.DBTimeout,
walletDBPath, db.Bolt.NoFreelistSync, db.Bolt.DBTimeout,
),
CloseFuncs: closeFuncs,
}, nil

View file

@ -20,5 +20,5 @@ func TestDBDefaultConfig(t *testing.T) {
require.Equal(t, kvdb.DefaultDBTimeout, defaultConfig.Bolt.DBTimeout)
// Implicitly, the following fields are default to false.
require.False(t, defaultConfig.Bolt.AutoCompact)
require.False(t, defaultConfig.Bolt.SyncFreelist)
require.True(t, defaultConfig.Bolt.NoFreelistSync)
}

2
lnd.go
View file

@ -1633,7 +1633,7 @@ func initializeDatabases(ctx context.Context,
if cfg.DB.Backend == lncfg.BoltBackend {
ltndLog.Infof("Opening bbolt database, sync_freelist=%v, "+
"auto_compact=%v", cfg.DB.Bolt.SyncFreelist,
"auto_compact=%v", !cfg.DB.Bolt.NoFreelistSync,
cfg.DB.Bolt.AutoCompact)
}

View file

@ -128,7 +128,7 @@ func NewBoltBackendCreator(active bool, dbPath,
cfg := &kvdb.BoltBackendConfig{
DBPath: dbPath,
DBFileName: dbFileName,
NoFreelistSync: !boltCfg.SyncFreelist,
NoFreelistSync: boltCfg.NoFreelistSync,
AutoCompact: boltCfg.AutoCompact,
AutoCompactMinAge: boltCfg.AutoCompactMinAge,
DBTimeout: boltCfg.DBTimeout,