lnd+lncfg: make etcd fully remote

This commit gets rid of the concept of a local and remote database when
etcd is used. Instead the same backend is now used for both the
(previously renamed from local and remote DB) graph and channel state
databases.
This will make path finding extremely slow on etcd and will require
further optimizations and possibly a write-through cache for the graph
DB. But this is a requirement for making lnd itself fully stateless.
This commit is contained in:
Oliver Gugger
2021-08-03 09:57:28 +02:00
parent c4917ae7fc
commit 0d3647d715
2 changed files with 100 additions and 116 deletions

View File

@@ -10,10 +10,15 @@ import (
)
const (
dbName = "channel.db"
dbName = "channel.db"
BoltBackend = "bolt"
EtcdBackend = "etcd"
DefaultBatchCommitInterval = 500 * time.Millisecond
// NSChannelDB is the namespace name that we use for the combined graph
// and channel state DB.
NSChannelDB = "channeldb"
)
// DB holds database configuration for LND.
@@ -81,7 +86,6 @@ func (db *DB) Init(ctx context.Context, dbPath string) error {
// DatabaseBackends is a two-tuple that holds the set of active database
// backends for the daemon. The two backends we expose are the graph database
// backend, and the channel state backend.
// TODO(guggero): Actually make fully remote.
type DatabaseBackends struct {
// GraphDB points to the database backend that contains the less
// critical data that is accessed often, such as the channel graph and
@@ -95,27 +99,45 @@ type DatabaseBackends struct {
// HeightHintDB points to a possibly networked replicated backend that
// contains the chain height hint related data.
HeightHintDB kvdb.Backend
// Remote indicates whether the database backends are remote, possibly
// replicated instances or local bbolt backed databases.
Remote bool
// CloseFuncs is a map of close functions for each of the initialized
// DB backends keyed by their namespace name.
CloseFuncs map[string]func() error
}
// GetBackends returns a set of kvdb.Backends as set in the DB config.
func (db *DB) GetBackends(ctx context.Context, dbPath string) (
*DatabaseBackends, error) {
var (
localDB, remoteDB kvdb.Backend
err error
)
// We keep track of all the kvdb backends we actually open and return a
// reference to their close function so they can be cleaned up properly
// on error or shutdown.
closeFuncs := make(map[string]func() error)
if db.Backend == EtcdBackend {
remoteDB, err = kvdb.Open(
etcdBackend, err := kvdb.Open(
kvdb.EtcdBackendName, ctx, db.Etcd,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("error opening etcd DB: %v", err)
}
closeFuncs[NSChannelDB] = etcdBackend.Close
return &DatabaseBackends{
GraphDB: etcdBackend,
ChanStateDB: etcdBackend,
HeightHintDB: etcdBackend,
Remote: true,
CloseFuncs: closeFuncs,
}, nil
}
localDB, err = kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
// We're using all bbolt based databases by default.
boltBackend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
DBPath: dbPath,
DBFileName: dbName,
DBTimeout: db.Bolt.DBTimeout,
@@ -124,13 +146,15 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string) (
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
})
if err != nil {
return nil, err
return nil, fmt.Errorf("error opening bolt DB: %v", err)
}
closeFuncs[NSChannelDB] = boltBackend.Close
return &DatabaseBackends{
GraphDB: localDB,
ChanStateDB: remoteDB,
HeightHintDB: localDB,
GraphDB: boltBackend,
ChanStateDB: boltBackend,
HeightHintDB: boltBackend,
CloseFuncs: closeFuncs,
}, nil
}