Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <admin@liudos.us>
  • Loading branch information
lhy1024 committed Aug 16, 2023
1 parent e2c17b2 commit f8ae8e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/election/leadership.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ func (ls *Leadership) Watch(serverCtx context.Context, revision int64) {
return
}

unhealthyTimeout := watchLoopUnhealthyTimeout
failpoint.Inject("fastTick", func() {
unhealthyTimeout = 5 * time.Second
})
ticker := time.NewTicker(etcdutil.RequestProgressInterval)
defer ticker.Stop()
lastReceivedResponseTime := time.Now()
Expand All @@ -213,7 +217,7 @@ func (ls *Leadership) Watch(serverCtx context.Context, revision int64) {
// When etcd is not available, the watcher.Watch will block,
// so we check the etcd availability first.
if !etcdutil.IsHealthy(serverCtx, ls.client) {
if time.Since(lastReceivedResponseTime) > watchLoopUnhealthyTimeout {
if time.Since(lastReceivedResponseTime) > unhealthyTimeout {
log.Error("the connect of leadership watcher is unhealthy",
zap.Int64("revision", revision),
zap.String("leader-key", ls.leaderKey),
Expand Down
24 changes: 24 additions & 0 deletions pkg/election/leadership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package election

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -156,6 +157,29 @@ func TestExitWatch(t *testing.T) {
time.Sleep(500 * time.Millisecond)
server.Close()
})
// Case6: transfer leader without client reconnection.
checkExitWatch(t, leaderKey, func(server *embed.Etcd, client *clientv3.Client) {
cfg1 := server.Config()
cfg2 := etcdutil.NewTestSingleConfig(t)
cfg2.InitialCluster = cfg1.InitialCluster + fmt.Sprintf(",%s=%s", cfg2.Name, &cfg2.LPUrls[0])
cfg2.ClusterState = embed.ClusterStateFlagExisting
peerURL := cfg2.LPUrls[0].String()
addResp, err := etcdutil.AddEtcdMember(client, []string{peerURL})
re.NoError(err)
etcd2, err := embed.StartEtcd(cfg2)
re.NoError(err)
re.Equal(uint64(etcd2.Server.ID()), addResp.Member.ID)
<-etcd2.Server.ReadyNotify()
ep := cfg2.LCUrls[0].String()
client1, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
})
re.NoError(err)

server.Server.HardStop()
client1.Delete(context.Background(), leaderKey)
})
// TODO: add test to simulate the case that pd leader is io hang.
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/election/fastTick"))
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/utils/etcdutil/fastTick"))
}
Expand Down

0 comments on commit f8ae8e9

Please sign in to comment.