Skip to content

Commit

Permalink
election: fix the keep alive worker (#6925) (#6941)
Browse files Browse the repository at this point in the history
close #6926

Signed-off-by: Ryan Leung <[email protected]>

Co-authored-by: Ryan Leung <[email protected]>
Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 26, 2023
1 parent 115052d commit aeb2593
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/election/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ func (l *lease) keepAliveWorker(ctx context.Context, interval time.Duration) <-c
expire := start.Add(time.Duration(res.TTL) * time.Second)
select {
case ch <- expire:
case <-ctx1.Done():
// Here we don't use `ctx1.Done()` because we want to make sure if the keep alive success, we can update the expire time.
case <-ctx.Done():
}
} else {
log.Error("keep alive response ttl is zero", zap.String("purpose", l.Purpose))
}
}()

Expand Down
31 changes: 31 additions & 0 deletions pkg/election/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,34 @@ func TestLease(t *testing.T) {
time.Sleep((defaultLeaseTimeout + 1) * time.Second)
re.True(lease1.IsExpired())
}

func TestLeaseKeepAlive(t *testing.T) {
re := require.New(t)
cfg := etcdutil.NewTestSingleConfig(t)
etcd, err := embed.StartEtcd(cfg)
defer func() {
etcd.Close()
}()
re.NoError(err)

ep := cfg.LCUrls[0].String()
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
})
re.NoError(err)

<-etcd.Server.ReadyNotify()

// Create the lease.
lease := &lease{
Purpose: "test_lease",
client: client,
lease: clientv3.NewLease(client),
}

re.NoError(lease.Grant(defaultLeaseTimeout))
ch := lease.keepAliveWorker(context.Background(), 2*time.Second)
time.Sleep(2 * time.Second)
<-ch
re.NoError(lease.Close())
}

0 comments on commit aeb2593

Please sign in to comment.