cluster: configurable session TTL for the leader elector

This commit is contained in:
Andras Banki-Horvath
2022-03-16 20:09:55 +01:00
parent 57840bba36
commit 8eca46f142
4 changed files with 24 additions and 10 deletions

View File

@@ -35,7 +35,7 @@ type etcdLeaderElector struct {
// newEtcdLeaderElector constructs a new etcdLeaderElector.
func newEtcdLeaderElector(ctx context.Context, id, electionPrefix string,
cfg *etcd.Config) (*etcdLeaderElector, error) {
leaderSessionTTL int, cfg *etcd.Config) (*etcdLeaderElector, error) {
clientCfg := clientv3.Config{
Context: ctx,
@@ -72,7 +72,9 @@ func newEtcdLeaderElector(ctx context.Context, id, electionPrefix string,
cli.Lease = namespace.NewLease(cli.Lease, cfg.Namespace)
log.Infof("Applied namespace to leader elector: %v", cfg.Namespace)
session, err := concurrency.NewSession(cli)
session, err := concurrency.NewSession(
cli, concurrency.WithTTL(leaderSessionTTL),
)
if err != nil {
log.Errorf("Unable to start new leader election session: %v",
err)

View File

@@ -15,9 +15,9 @@ import (
func makeEtcdElector(ctx context.Context, args ...interface{}) (LeaderElector,
error) {
if len(args) != 3 {
if len(args) != 4 {
return nil, fmt.Errorf("invalid number of arguments to "+
"cluster.makeEtcdElector(): expected 3, got %v",
"cluster.makeEtcdElector(): expected 4, got %v",
len(args))
}
@@ -33,13 +33,21 @@ func makeEtcdElector(ctx context.Context, args ...interface{}) (LeaderElector,
"cluster.makeEtcdElector(), expected: string")
}
etcdCfg, ok := args[2].(*etcd.Config)
leaderSessionTTL, ok := args[2].(int)
if !ok {
return nil, fmt.Errorf("invalid argument (2) to " +
"cluster.makeEtcdElector(), expected: int")
}
etcdCfg, ok := args[3].(*etcd.Config)
if !ok {
return nil, fmt.Errorf("invalid argument (3) to " +
"cluster.makeEtcdElector(), expected: *etcd.Config")
}
return newEtcdLeaderElector(ctx, id, electionPrefix, etcdCfg)
return newEtcdLeaderElector(
ctx, id, electionPrefix, leaderSessionTTL, etcdCfg,
)
}
func init() {

View File

@@ -57,15 +57,16 @@ func TestEtcdElector(t *testing.T) {
election = "/election/"
id1 = "e1"
id2 = "e2"
ttl = 5
)
e1, err := newEtcdLeaderElector(
ctx, id1, election, etcdCfg,
ctx, id1, election, ttl, etcdCfg,
)
require.NoError(t, err)
e2, err := newEtcdLeaderElector(
ctx, id2, election, etcdCfg,
ctx, id2, election, ttl, etcdCfg,
)
require.NoError(t, err)