lncfg+etcd: add namespace support for etcd databases

Since we're now storing the content of multiple previously distinct
database files in etcd, we want to properly namespace them as not to
provoke any key collisions. We append a sub namespace to the given
global namespace in order to still support multiple lnd nodes using the
same etcd database simultaneously.

Because the btcwallet code uses the legacy walletdb interface we must
assume it is not fully concurrency safe. Therefore we make sure only a
single writer can be active at any given time for the wallet DB backend
when using etcd.
This commit is contained in:
Oliver Gugger
2021-08-03 09:57:36 +02:00
parent 57c7862eeb
commit 1e84b52fee
2 changed files with 124 additions and 7 deletions

View File

@@ -1,5 +1,7 @@
package etcd
import "fmt"
// Config holds etcd configuration alongside with configuration related to our higher level interface.
type Config struct {
Embedded bool `long:"embedded" description:"Use embedded etcd instance instead of the external one. Note: use for testing only."`
@@ -30,3 +32,51 @@ type Config struct {
// single writer to the database at a time.
SingleWriter bool
}
// CloneWithSubNamespace clones the current configuration and returns a new
// instance with the given sub namespace applied by appending it to the main
// namespace.
func (c *Config) CloneWithSubNamespace(subNamespace string) *Config {
ns := c.Namespace
if len(ns) == 0 {
ns = subNamespace
} else {
ns = fmt.Sprintf("%s/%s", ns, subNamespace)
}
return &Config{
Embedded: c.Embedded,
EmbeddedClientPort: c.EmbeddedClientPort,
EmbeddedPeerPort: c.EmbeddedPeerPort,
Host: c.Host,
User: c.User,
Pass: c.Pass,
Namespace: ns,
DisableTLS: c.DisableTLS,
CertFile: c.CertFile,
KeyFile: c.KeyFile,
InsecureSkipVerify: c.InsecureSkipVerify,
CollectStats: c.CollectStats,
SingleWriter: c.SingleWriter,
}
}
// CloneWithSingleWriter clones the current configuration and returns a new
// instance with the single writer property set to true.
func (c *Config) CloneWithSingleWriter() *Config {
return &Config{
Embedded: c.Embedded,
EmbeddedClientPort: c.EmbeddedClientPort,
EmbeddedPeerPort: c.EmbeddedPeerPort,
Host: c.Host,
User: c.User,
Pass: c.Pass,
Namespace: c.Namespace,
DisableTLS: c.DisableTLS,
CertFile: c.CertFile,
KeyFile: c.KeyFile,
InsecureSkipVerify: c.InsecureSkipVerify,
CollectStats: c.CollectStats,
SingleWriter: true,
}
}