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:
parent
3ef331e016
commit
85aee9b064
@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"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.
|
// 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
|
return db, empty, nil
|
||||||
} else if TestBackend == EtcdBackendName {
|
} else if TestBackend == EtcdBackendName {
|
||||||
config, cleanup, err := etcd.NewEmbeddedEtcdInstance(path)
|
return GetEtcdTestBackend(path, name)
|
||||||
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 nil, nil, fmt.Errorf("unknown backend")
|
return nil, nil, fmt.Errorf("unknown backend")
|
||||||
|
@ -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
33
channeldb/kvdb/config.go
Normal 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."`
|
||||||
|
}
|
@ -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"
|
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
// bkey is a helper functon used in tests to create a bucket key from passed
|
// bkey is a helper functon used in tests to create a bucket key from passed
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
// readWriteCursor holds a reference to the cursors bucket, the value
|
// readWriteCursor holds a reference to the cursors bucket, the value
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build kvdb_etcd
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
48
channeldb/kvdb/kvdb_etcd.go
Normal file
48
channeldb/kvdb/kvdb_etcd.go
Normal 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
|
||||||
|
}
|
24
channeldb/kvdb/kvdb_no_etcd.go
Normal file
24
channeldb/kvdb/kvdb_no_etcd.go
Normal 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
|
||||||
|
}
|
@ -1,5 +0,0 @@
|
|||||||
// +build !kvdb_etcd
|
|
||||||
|
|
||||||
package kvdb
|
|
||||||
|
|
||||||
const TestBackend = "bdb"
|
|
@ -1,5 +0,0 @@
|
|||||||
// +build kvdb_etcd
|
|
||||||
|
|
||||||
package kvdb
|
|
||||||
|
|
||||||
const TestBackend = "etcd"
|
|
40
lncfg/db.go
40
lncfg/db.go
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
||||||
"github.com/lightningnetwork/lnd/channeldb/kvdb/etcd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -13,42 +12,20 @@ const (
|
|||||||
etcdBackend = "etcd"
|
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.
|
// DB holds database configuration for LND.
|
||||||
type DB struct {
|
type DB struct {
|
||||||
Backend string `long:"backend" description:"The selected database backend."`
|
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.
|
// NewDB creates and returns a new default DB config.
|
||||||
func DefaultDB() *DB {
|
func DefaultDB() *DB {
|
||||||
return &DB{
|
return &DB{
|
||||||
Backend: boltBackend,
|
Backend: boltBackend,
|
||||||
Bolt: &BoltDB{
|
Bolt: &kvdb.BoltConfig{
|
||||||
NoFreeListSync: true,
|
NoFreeListSync: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -75,16 +52,7 @@ func (db *DB) Validate() error {
|
|||||||
// GetBackend returns a kvdb.Backend as set in the DB config.
|
// GetBackend returns a kvdb.Backend as set in the DB config.
|
||||||
func (db *DB) GetBackend(path string) (kvdb.Backend, error) {
|
func (db *DB) GetBackend(path string) (kvdb.Backend, error) {
|
||||||
if db.Backend == etcdBackend {
|
if db.Backend == etcdBackend {
|
||||||
backendConfig := etcd.BackendConfig{
|
return kvdb.GetEtcdBackend(db.Etcd)
|
||||||
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.GetBoltBackend(path, dbName, db.Bolt.NoFreeListSync)
|
return kvdb.GetBoltBackend(path, dbName, db.Bolt.NoFreeListSync)
|
||||||
|
Loading…
Reference in New Issue
Block a user