From 7355c8ba3a4fd650889e34176b0971451537b1e4 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 6 May 2020 20:48:05 -0700 Subject: [PATCH] lncfg: add new GetBackends which returns active DB backends In this commit, we modify the existing `GetBackend` method to now be called `GetBackends`. This new method will populate a new `RemoteDB` attribute based on if the replicated backend is active or not. As is, the local backend is used everywhere. An upcoming commit will once again re-enable the remote backend, in a hybrid manner. --- lncfg/db.go | 46 +++++++++++++++++++++++++++++++++++++++------- lnd.go | 7 ++++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lncfg/db.go b/lncfg/db.go index 6bc1e9a3..cb6bcb24 100644 --- a/lncfg/db.go +++ b/lncfg/db.go @@ -48,18 +48,50 @@ func (db *DB) Validate() error { return nil } -// GetBackend returns a kvdb.Backend as set in the DB config. -func (db *DB) GetBackend(ctx context.Context, dbPath string, - networkName string) (kvdb.Backend, error) { +// DatabaseBackends is a two-tuple that holds the set of active database +// backends for the daemon. The two backends we expose are the local database +// backend, and the remote backend. The LocalDB attribute will always be +// populated. However, the remote DB will only be set if a replicated database +// is active. +type DatabaseBackends struct { + // LocalDB points to the local non-replicated backend. + LocalDB kvdb.Backend + + // RemoteDB points to a possibly networked replicated backend. If no + // replicated backend is active, then this pointer will be nil. + RemoteDB kvdb.Backend +} + +// GetBackends returns a set of kvdb.Backends as set in the DB config. The +// local database will ALWAYS be non-nil, while the remote database will only +// be populated if etcd is specified. +func (db *DB) GetBackends(ctx context.Context, dbPath string, + networkName string) (*DatabaseBackends, error) { + + var ( + localDB, remoteDB kvdb.Backend + err error + ) if db.Backend == EtcdBackend { // Prefix will separate key/values in the db. - return kvdb.GetEtcdBackend(ctx, networkName, db.Etcd) + remoteDB, err = kvdb.GetEtcdBackend(ctx, networkName, db.Etcd) + if err != nil { + return nil, err + } } - // The implementation by walletdb accepts "noFreelistSync" as the - // second parameter, so we negate here. - return kvdb.GetBoltBackend(dbPath, dbName, !db.Bolt.SyncFreelist) + localDB, err = kvdb.GetBoltBackend( + dbPath, dbName, !db.Bolt.SyncFreelist, + ) + if err != nil { + return nil, err + } + + return &DatabaseBackends{ + LocalDB: localDB, + RemoteDB: remoteDB, + }, nil } // Compile-time constraint to ensure Workers implements the Validator interface. diff --git a/lnd.go b/lnd.go index 7cfae4c3..bf121275 100644 --- a/lnd.go +++ b/lnd.go @@ -251,6 +251,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { "minutes...") startOpenTime := time.Now() + ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -260,8 +261,8 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { cfg.DB.Bolt.SyncFreelist) } - chanDbBackend, err := cfg.DB.GetBackend(ctx, - cfg.localDatabaseDir(), cfg.networkName(), + databaseBackends, err := cfg.DB.GetBackends( + ctx, cfg.localDatabaseDir(), cfg.networkName(), ) if err != nil { ltndLog.Error(err) @@ -271,7 +272,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { // Open the channeldb, which is dedicated to storing channel, and // network related metadata. chanDB, err := channeldb.CreateWithBackend( - chanDbBackend, + databaseBackends.LocalDB, channeldb.OptionSetRejectCacheSize(cfg.Caches.RejectCacheSize), channeldb.OptionSetChannelCacheSize(cfg.Caches.ChannelCacheSize), channeldb.OptionDryRunMigration(cfg.DryRunMigration),