Skip to content

Commit

Permalink
bbolt sync-freelist ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEk committed Sep 27, 2021
1 parent 3800cd9 commit d546381
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
39 changes: 32 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"os/user"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/release-notes/release-notes-0.14.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion htlcswitch/decayedlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion kvdb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`

Expand Down
13 changes: 7 additions & 6 deletions lncfg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func DefaultDB() *DB {
Backend: BoltBackend,
BatchCommitInterval: DefaultBatchCommitInterval,
Bolt: &kvdb.BoltConfig{
NoFreelistSync: true,
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout,
},
Expand Down Expand Up @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lncfg/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 1 addition & 1 deletion lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion watchtower/wtdb/client_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d546381

Please sign in to comment.