2021-02-08 23:55:52 +03:00
|
|
|
package lncfg
|
|
|
|
|
|
|
|
import (
|
2021-02-08 23:56:27 +03:00
|
|
|
"context"
|
2021-02-08 23:55:52 +03:00
|
|
|
"fmt"
|
2021-02-08 23:56:27 +03:00
|
|
|
"os"
|
2021-02-08 23:55:52 +03:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/cluster"
|
|
|
|
)
|
|
|
|
|
2021-02-08 23:56:27 +03:00
|
|
|
const (
|
|
|
|
// DefaultEtcdElectionPrefix is used as election prefix if none is provided
|
|
|
|
// through the config.
|
|
|
|
DefaultEtcdElectionPrefix = "/leader/"
|
|
|
|
)
|
|
|
|
|
2021-02-08 23:55:52 +03:00
|
|
|
// Cluster holds configuration for clustered LND.
|
|
|
|
type Cluster struct {
|
2021-02-08 23:56:27 +03:00
|
|
|
EnableLeaderElection bool `long:"enable-leader-election" description:"Enables leader election if set."`
|
|
|
|
|
2021-02-08 23:55:52 +03:00
|
|
|
LeaderElector string `long:"leader-elector" choice:"etcd" description:"Leader elector to use. Valid values: \"etcd\"."`
|
|
|
|
|
2021-02-08 23:56:27 +03:00
|
|
|
EtcdElectionPrefix string `long:"etcd-election-prefix" description:"Election key prefix when using etcd leader elector. Defaults to \"/leader/\"."`
|
|
|
|
|
|
|
|
ID string `long:"id" description:"Identifier for this node inside the cluster (used in leader election). Defaults to the hostname."`
|
2021-02-08 23:55:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultCluster creates and returns a new default DB config.
|
|
|
|
func DefaultCluster() *Cluster {
|
2021-02-08 23:56:27 +03:00
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
return &Cluster{
|
|
|
|
LeaderElector: cluster.EtcdLeaderElector,
|
|
|
|
EtcdElectionPrefix: DefaultEtcdElectionPrefix,
|
|
|
|
ID: hostname,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeLeaderElector is a helper method to construct the concrete leader elector
|
|
|
|
// based on the current configuration.
|
|
|
|
func (c *Cluster) MakeLeaderElector(electionCtx context.Context, db *DB) (
|
|
|
|
cluster.LeaderElector, error) {
|
|
|
|
|
|
|
|
if c.LeaderElector == cluster.EtcdLeaderElector {
|
|
|
|
return cluster.MakeLeaderElector(
|
|
|
|
electionCtx, c.LeaderElector, c.ID,
|
|
|
|
c.EtcdElectionPrefix, db.Etcd,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unsupported leader elector")
|
2021-02-08 23:55:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the Cluster config.
|
|
|
|
func (c *Cluster) Validate() error {
|
2021-02-08 23:56:27 +03:00
|
|
|
if !c.EnableLeaderElection {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:55:52 +03:00
|
|
|
switch c.LeaderElector {
|
2021-02-08 23:56:27 +03:00
|
|
|
|
2021-02-08 23:55:52 +03:00
|
|
|
case cluster.EtcdLeaderElector:
|
|
|
|
if c.EtcdElectionPrefix == "" {
|
|
|
|
return fmt.Errorf("etcd-election-prefix must be set")
|
|
|
|
}
|
2021-02-08 23:56:27 +03:00
|
|
|
return nil
|
2021-02-08 23:55:52 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown leader elector, valid values are: "+
|
|
|
|
"\"%v\"", cluster.EtcdLeaderElector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile-time constraint to ensure Workers implements the Validator interface.
|
|
|
|
var _ Validator = (*Cluster)(nil)
|