Skip to content

Commit

Permalink
election: fix the keep alive worker (#6925) (#6940)
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 Aug 14, 2023
1 parent df147b3 commit 3a19dec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion server/election/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,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 server/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 3a19dec

Please sign in to comment.