Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

election: fix the keep alive worker (#6925) #6940

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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():

Check warning on line 156 in server/election/lease.go

View check run for this annotation

Codecov / codecov/patch

server/election/lease.go#L156

Added line #L156 was not covered by tests
}
} else {
log.Error("keep alive response ttl is zero", zap.String("purpose", l.Purpose))

Check warning on line 159 in server/election/lease.go

View check run for this annotation

Codecov / codecov/patch

server/election/lease.go#L158-L159

Added lines #L158 - L159 were not covered by tests
}
}()

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())
}
Loading