2021-08-23 10:35:48 +02:00
|
|
|
//go:build kvdb_etcd
|
2021-02-08 21:55:52 +01:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb/etcd"
|
2021-02-08 21:55:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// makeEtcdElector will construct a new etcdLeaderElector. It expects a cancel
|
|
|
|
// context a unique (in the cluster) LND id and a *etcd.Config as arguments.
|
|
|
|
func makeEtcdElector(ctx context.Context, args ...interface{}) (LeaderElector,
|
|
|
|
error) {
|
|
|
|
|
2022-03-16 20:09:55 +01:00
|
|
|
if len(args) != 4 {
|
2021-02-08 21:55:52 +01:00
|
|
|
return nil, fmt.Errorf("invalid number of arguments to "+
|
2022-03-16 20:09:55 +01:00
|
|
|
"cluster.makeEtcdElector(): expected 4, got %v",
|
2021-02-08 21:55:52 +01:00
|
|
|
len(args))
|
|
|
|
}
|
|
|
|
|
|
|
|
id, ok := args[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid argument (0) to " +
|
|
|
|
"cluster.makeEtcdElector(), expected: string")
|
|
|
|
}
|
|
|
|
|
|
|
|
electionPrefix, ok := args[1].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid argument (1) to " +
|
|
|
|
"cluster.makeEtcdElector(), expected: string")
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:09:55 +01:00
|
|
|
leaderSessionTTL, ok := args[2].(int)
|
2021-02-08 21:55:52 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid argument (2) to " +
|
2022-03-16 20:09:55 +01:00
|
|
|
"cluster.makeEtcdElector(), expected: int")
|
|
|
|
}
|
|
|
|
|
|
|
|
etcdCfg, ok := args[3].(*etcd.Config)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid argument (3) to " +
|
2021-02-08 21:55:52 +01:00
|
|
|
"cluster.makeEtcdElector(), expected: *etcd.Config")
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:09:55 +01:00
|
|
|
return newEtcdLeaderElector(
|
|
|
|
ctx, id, electionPrefix, leaderSessionTTL, etcdCfg,
|
|
|
|
)
|
2021-02-08 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterLeaderElectorFactory(EtcdLeaderElector, makeEtcdElector)
|
|
|
|
}
|