kvdb+lncfg: fully move etcd behind build tag

This commit separates all etcd related sources (sans a few stubs and
config) from the rest of the source tree and makes compilation conditional
depending on whether the kvdb_etcd build tag is specified.
This commit is contained in:
Andras Banki-Horvath 2020-05-15 16:59:37 +02:00
parent 3ef331e016
commit 85aee9b064
26 changed files with 145 additions and 77 deletions

View File

@ -5,7 +5,7 @@ import (
"os"
"path/filepath"
"github.com/lightningnetwork/lnd/channeldb/kvdb/etcd"
_ "github.com/btcsuite/btcwallet/walletdb/bdb" // Import to register backend.
)
// fileExists returns true if the file exists, and false otherwise.
@ -63,16 +63,7 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
}
return db, empty, nil
} else if TestBackend == EtcdBackendName {
config, cleanup, err := etcd.NewEmbeddedEtcdInstance(path)
if err != nil {
return nil, nil, err
}
backend, err := Open(EtcdBackendName, *config)
if err != nil {
cleanup()
return nil, nil, err
}
return backend, cleanup, nil
return GetEtcdTestBackend(path, name)
}
return nil, nil, fmt.Errorf("unknown backend")

View File

@ -1,10 +0,0 @@
package kvdb
import (
_ "github.com/btcsuite/btcwallet/walletdb/bdb" // Import to register backend.
)
// BoltBackendName is the name of the backend that should be passed into
// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live
// instance of bbolt.
const BoltBackendName = "bdb"

33
channeldb/kvdb/config.go Normal file
View File

@ -0,0 +1,33 @@
package kvdb
// BoltBackendName is the name of the backend that should be passed into
// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live
// instance of bbolt.
const BoltBackendName = "bdb"
// EtcdBackendName is the name of the backend that should be passed into
// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live
// instance of etcd.
const EtcdBackendName = "etcd"
// BoltConfig holds bolt configuration.
type BoltConfig struct {
NoFreeListSync bool `long:"nofreelistsync" description:"If true, prevents the database from syncing its freelist to disk"`
}
// EtcdConfig holds etcd configuration.
type EtcdConfig struct {
Host string `long:"host" description:"Etcd database host."`
User string `long:"user" description:"Etcd database user."`
Pass string `long:"pass" description:"Password for the database user."`
CertFile string `long:"cert_file" description:"Path to the TLS certificate for etcd RPC."`
KeyFile string `long:"key_file" description:"Path to the TLS private key for etcd RPC."`
InsecureSkipVerify bool `long:"insecure_skip_verify" description:"Whether we intend to skip TLS verification"`
CollectStats bool `long:"collect_stats" description:"Whether to collect etcd commit stats."`
}

View File

@ -1,10 +0,0 @@
package kvdb
import (
_ "github.com/lightningnetwork/lnd/channeldb/kvdb/etcd" // Import to register backend.
)
// EtcdBackendName is the name of the backend that should be passed into
// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live
// instance of etcd.
const EtcdBackendName = "etcd"

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
// bkey is a helper functon used in tests to create a bucket key from passed

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
// readWriteCursor holds a reference to the cursors bucket, the value

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -1,3 +1,5 @@
// +build kvdb_etcd
package etcd
import (

View File

@ -0,0 +1,48 @@
// +build kvdb_etcd
package kvdb
import (
"github.com/lightningnetwork/lnd/channeldb/kvdb/etcd"
)
// TestBackend is conditionally set to etcd when the kvdb_etcd build tag is
// defined, allowing testing our database code with etcd backend.
const TestBackend = EtcdBackendName
// GetEtcdBackend returns an etcd backend configured according to the
// passed etcdConfig.
func GetEtcdBackend(etcdConfig *EtcdConfig) (Backend, error) {
// Config translation is needed here in order to keep the
// etcd package fully independent from the rest of the source tree.
backendConfig := etcd.BackendConfig{
Host: etcdConfig.Host,
User: etcdConfig.User,
Pass: etcdConfig.Pass,
CertFile: etcdConfig.CertFile,
KeyFile: etcdConfig.KeyFile,
InsecureSkipVerify: etcdConfig.InsecureSkipVerify,
CollectCommitStats: etcdConfig.CollectStats,
}
return Open(EtcdBackendName, backendConfig)
}
// GetEtcdTestBackend creates an embedded etcd backend for testing
// storig the database at the passed path.
func GetEtcdTestBackend(path, name string) (Backend, func(), error) {
empty := func() {}
config, cleanup, err := etcd.NewEmbeddedEtcdInstance(path)
if err != nil {
return nil, empty, err
}
backend, err := Open(EtcdBackendName, *config)
if err != nil {
cleanup()
return nil, empty, err
}
return backend, cleanup, nil
}

View File

@ -0,0 +1,24 @@
// +build !kvdb_etcd
package kvdb
import (
"fmt"
)
// TestBackend is conditionally set to bdb when the kvdb_etcd build tag is
// not defined, allowing testing our database code with bolt backend.
const TestBackend = BoltBackendName
var errEtcdNotAvailable = fmt.Errorf("etcd backend not available")
// GetEtcdBackend is a stub returning nil and errEtcdNotAvailable error.
func GetEtcdBackend(etcdConfig *EtcdConfig) (Backend, error) {
return nil, errEtcdNotAvailable
}
// GetTestEtcdBackend is a stub returning nil, an empty closure and an
// errEtcdNotAvailable error.
func GetEtcdTestBackend(path, name string) (Backend, func(), error) {
return nil, func() {}, errEtcdNotAvailable
}

View File

@ -1,5 +0,0 @@
// +build !kvdb_etcd
package kvdb
const TestBackend = "bdb"

View File

@ -1,5 +0,0 @@
// +build kvdb_etcd
package kvdb
const TestBackend = "etcd"

View File

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
"github.com/lightningnetwork/lnd/channeldb/kvdb/etcd"
)
const (
@ -13,42 +12,20 @@ const (
etcdBackend = "etcd"
)
// BoltDB holds bolt configuration.
type BoltDB struct {
NoFreeListSync bool `long:"nofreelistsync" description:"If true, prevents the database from syncing its freelist to disk"`
}
// EtcdDB hold etcd configuration.
type EtcdDB struct {
Host string `long:"host" description:"Etcd database host."`
User string `long:"user" description:"Etcd database user."`
Pass string `long:"pass" description:"Password for the database user."`
CertFile string `long:"cert_file" description:"Path to the TLS certificate for etcd RPC."`
KeyFile string `long:"key_file" description:"Path to the TLS private key for etcd RPC."`
InsecureSkipVerify bool `long:"insecure_skip_verify" description:"Whether we intend to skip TLS verification"`
CollectStats bool `long:"collect_stats" description:"Wheter to collect etcd commit stats."`
}
// DB holds database configuration for LND.
type DB struct {
Backend string `long:"backend" description:"The selected database backend."`
Etcd *EtcdDB `group:"etcd" namespace:"etcd" description:"Etcd settings."`
Etcd *kvdb.EtcdConfig `group:"etcd" namespace:"etcd" description:"Etcd settings."`
Bolt *BoltDB `group:"bolt" namespace:"bolt" description:"Bolt settings."`
Bolt *kvdb.BoltConfig `group:"bolt" namespace:"bolt" description:"Bolt settings."`
}
// NewDB creates and returns a new default DB config.
func DefaultDB() *DB {
return &DB{
Backend: boltBackend,
Bolt: &BoltDB{
Bolt: &kvdb.BoltConfig{
NoFreeListSync: true,
},
}
@ -75,16 +52,7 @@ func (db *DB) Validate() error {
// GetBackend returns a kvdb.Backend as set in the DB config.
func (db *DB) GetBackend(path string) (kvdb.Backend, error) {
if db.Backend == etcdBackend {
backendConfig := etcd.BackendConfig{
Host: db.Etcd.Host,
User: db.Etcd.User,
Pass: db.Etcd.Pass,
CertFile: db.Etcd.CertFile,
KeyFile: db.Etcd.KeyFile,
InsecureSkipVerify: db.Etcd.InsecureSkipVerify,
CollectCommitStats: db.Etcd.CollectStats,
}
return kvdb.Open(kvdb.EtcdBackendName, backendConfig)
return kvdb.GetEtcdBackend(db.Etcd)
}
return kvdb.GetBoltBackend(path, dbName, db.Bolt.NoFreeListSync)