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.
This commit is contained in:
Olaoluwa Osuntokun 2020-05-06 20:48:05 -07:00
parent 6a6521bba0
commit 7355c8ba3a
No known key found for this signature in database
GPG Key ID: BC13F65E2DC84465
2 changed files with 43 additions and 10 deletions

View File

@ -48,18 +48,50 @@ func (db *DB) Validate() error {
return nil return nil
} }
// GetBackend returns a kvdb.Backend as set in the DB config. // DatabaseBackends is a two-tuple that holds the set of active database
func (db *DB) GetBackend(ctx context.Context, dbPath string, // backends for the daemon. The two backends we expose are the local database
networkName string) (kvdb.Backend, error) { // 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 { if db.Backend == EtcdBackend {
// Prefix will separate key/values in the db. // 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 localDB, err = kvdb.GetBoltBackend(
// second parameter, so we negate here. dbPath, dbName, !db.Bolt.SyncFreelist,
return 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. // Compile-time constraint to ensure Workers implements the Validator interface.

7
lnd.go
View File

@ -251,6 +251,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
"minutes...") "minutes...")
startOpenTime := time.Now() startOpenTime := time.Now()
ctx := context.Background() ctx := context.Background()
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
@ -260,8 +261,8 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
cfg.DB.Bolt.SyncFreelist) cfg.DB.Bolt.SyncFreelist)
} }
chanDbBackend, err := cfg.DB.GetBackend(ctx, databaseBackends, err := cfg.DB.GetBackends(
cfg.localDatabaseDir(), cfg.networkName(), ctx, cfg.localDatabaseDir(), cfg.networkName(),
) )
if err != nil { if err != nil {
ltndLog.Error(err) 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 // Open the channeldb, which is dedicated to storing channel, and
// network related metadata. // network related metadata.
chanDB, err := channeldb.CreateWithBackend( chanDB, err := channeldb.CreateWithBackend(
chanDbBackend, databaseBackends.LocalDB,
channeldb.OptionSetRejectCacheSize(cfg.Caches.RejectCacheSize), channeldb.OptionSetRejectCacheSize(cfg.Caches.RejectCacheSize),
channeldb.OptionSetChannelCacheSize(cfg.Caches.ChannelCacheSize), channeldb.OptionSetChannelCacheSize(cfg.Caches.ChannelCacheSize),
channeldb.OptionDryRunMigration(cfg.DryRunMigration), channeldb.OptionDryRunMigration(cfg.DryRunMigration),