etcd: decouple embedded etcd instance startup

This refactor changes how we instantiate the etcd based Backend to allow
separate initialization and startup for the embedded instance.
This commit is contained in:
Andras Banki-Horvath committed 2021-05-04 17:33:05 +02:00
1 parent 6757e14998
commit 9992e5c0b5
5 files changed
+46 -47

No files matched your search

+9 -1
View File
@@ -1,6 +1,7 @@
package kvdb
import (
"context"
"encoding/binary"
"fmt"
"io/ioutil"
@@ -253,7 +254,14 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
}
return db, empty, nil
} else if TestBackend == EtcdBackendName {
return GetEtcdTestBackend(path, 0, 0)
etcdConfig, cancel, err := StartEtcdTestBackend(path, 0, 0)
if err != nil {
return nil, empty, err
}
backend, err := Open(
EtcdBackendName, context.TODO(), etcdConfig,
)
return backend, cancel, err
}
return nil, nil, fmt.Errorf("unknown backend")
+3 -26
View File
@@ -3,8 +3,6 @@
package kvdb
import (
"context"
"github.com/lightningnetwork/lnd/channeldb/kvdb/etcd"
)
@@ -12,33 +10,12 @@ import (
// 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(ctx context.Context, etcdConfig *etcd.Config) (
Backend, error) {
return Open(EtcdBackendName, etcdConfig)
}
// GetEtcdTestBackend creates an embedded etcd backend for testing
// storig the database at the passed path.
func GetEtcdTestBackend(path string, clientPort, peerPort uint16) (
Backend, func(), error) {
func StartEtcdTestBackend(path string, clientPort, peerPort uint16) (
*etcd.Config, func(), error) {
empty := func() {}
config, cleanup, err := etcd.NewEmbeddedEtcdInstance(
return etcd.NewEmbeddedEtcdInstance(
path, clientPort, peerPort,
)
if err != nil {
return nil, empty, err
}
backend, err := Open(EtcdBackendName, context.Background(), config)
if err != nil {
cleanup()
return nil, empty, err
}
return backend, cleanup, nil
}
+3 -12
View File
@@ -3,7 +3,6 @@
package kvdb
import (
"context"
"fmt"
"github.com/lightningnetwork/lnd/channeldb/kvdb/etcd"
@@ -15,17 +14,9 @@ const TestBackend = BoltBackendName
var errEtcdNotAvailable = fmt.Errorf("etcd backend not available")
// GetEtcdBackend is a stub returning nil and errEtcdNotAvailable error.
func GetEtcdBackend(ctx context.Context, etcdConfig *etcd.Config) (
Backend, error) {
return nil, errEtcdNotAvailable
}
// GetTestEtcdBackend is a stub returning nil, an empty closure and an
// errEtcdNotAvailable error.
func GetEtcdTestBackend(path string, clientPort, peerPort uint16) (
Backend, func(), error) {
// StartEtcdTestBackend is a stub returning nil, and errEtcdNotAvailable error.
func StartEtcdTestBackend(path string, clientPort, peerPort uint16) (
*etcd.Config, func(), error) {
return nil, func() {}, errEtcdNotAvailable
}