diff --git a/pkg/utils/etcdutil/etcdutil.go b/pkg/utils/etcdutil/etcdutil.go index ab18ca018591..4b60327a3324 100644 --- a/pkg/utils/etcdutil/etcdutil.go +++ b/pkg/utils/etcdutil/etcdutil.go @@ -33,6 +33,7 @@ import ( "github.com/tikv/pd/pkg/utils/logutil" "github.com/tikv/pd/pkg/utils/syncutil" "github.com/tikv/pd/pkg/utils/typeutil" + "go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/api/v3/mvccpb" "go.etcd.io/etcd/api/v3/v3rpc/rpctypes" etcdtypes "go.etcd.io/etcd/client/pkg/v3/types" @@ -119,12 +120,13 @@ func ListEtcdMembers(ctx context.Context, client *clientv3.Client) (*clientv3.Me time.Sleep(time.Duration(d) * time.Second) }) newCtx, cancel := context.WithTimeout(ctx, DefaultRequestTimeout) - listResp, err := client.MemberList(newCtx) + c := clientv3.RetryClusterClient(client) + resp, err := c.MemberList(newCtx, &etcdserverpb.MemberListRequest{Linearizable: false}) cancel() if err != nil { - return listResp, errs.ErrEtcdMemberList.Wrap(err).GenWithStackByCause() + return (*clientv3.MemberListResponse)(resp), errs.ErrEtcdMemberList.Wrap(err).GenWithStackByCause() } - return listResp, nil + return (*clientv3.MemberListResponse)(resp), nil } // RemoveEtcdMember removes a member by the given id. diff --git a/pkg/utils/etcdutil/etcdutil_test.go b/pkg/utils/etcdutil/etcdutil_test.go index 7fe17de74b55..99f71ffde05a 100644 --- a/pkg/utils/etcdutil/etcdutil_test.go +++ b/pkg/utils/etcdutil/etcdutil_test.go @@ -182,7 +182,6 @@ func TestEtcdClientSync(t *testing.T) { servers, client1, clean := NewTestEtcdCluster(t, 1) defer clean() etcd1, cfg1 := servers[0], servers[0].Config() - defer etcd1.Close() // Add a new member. etcd2 := MustAddEtcdMember(t, &cfg1, client1) @@ -781,7 +780,16 @@ func (suite *loopWatcherTestSuite) startEtcd(re *require.Assertions) { suite.etcd = etcd1 <-etcd1.Server.ReadyNotify() suite.cleans = append(suite.cleans, func() { - suite.etcd.Close() + if suite.etcd.Server != nil { + select { + case _, ok := <-suite.etcd.Err(): + if !ok { + return + } + default: + } + suite.etcd.Close() + } }) } diff --git a/pkg/utils/etcdutil/testutil.go b/pkg/utils/etcdutil/testutil.go index 3f95016ecd8e..e67ae415736a 100644 --- a/pkg/utils/etcdutil/testutil.go +++ b/pkg/utils/etcdutil/testutil.go @@ -86,13 +86,15 @@ func NewTestEtcdCluster(t *testing.T, count int) (servers []*embed.Etcd, etcdCli clean = func() { etcdClient.Close() for _, server := range servers { - if server != nil { - notifier := server.Server.StopNotify() + if server.Server != nil { select { - case <-notifier: + case _, ok := <-server.Err(): + if !ok { + return + } default: - server.Close() } + server.Close() } } } diff --git a/tests/integrations/mcs/scheduling/server_test.go b/tests/integrations/mcs/scheduling/server_test.go index 35dc78d0bee6..ded304d50209 100644 --- a/tests/integrations/mcs/scheduling/server_test.go +++ b/tests/integrations/mcs/scheduling/server_test.go @@ -675,11 +675,14 @@ func (suite *multipleServerTestSuite) TestReElectLeader() { newLeaderName := suite.cluster.WaitLeader() re.NotEqual(originLeaderName, newLeaderName) + suite.pdLeader = suite.cluster.GetServer(newLeaderName) suite.pdLeader.ResignLeader() newLeaderName = suite.cluster.WaitLeader() re.Equal(originLeaderName, newLeaderName) + suite.pdLeader = suite.cluster.GetServer(newLeaderName) rc = suite.pdLeader.GetServer().GetRaftCluster() + re.NotNil(rc) rc.IsPrepared() } diff --git a/tests/integrations/mcs/tso/server_test.go b/tests/integrations/mcs/tso/server_test.go index 6e412706225c..db2716c23bf0 100644 --- a/tests/integrations/mcs/tso/server_test.go +++ b/tests/integrations/mcs/tso/server_test.go @@ -622,9 +622,15 @@ func (suite *CommonTestSuite) TestBootstrapDefaultKeyspaceGroup() { re.NoError(s.Run()) // transfer leader to the new server + originLeaderName := suite.pdLeader.GetLeader().GetName() suite.pdLeader.ResignLeader() - suite.pdLeader = suite.cluster.GetServer(suite.cluster.WaitLeader()) + newLeaderName := suite.cluster.WaitLeader() + suite.pdLeader = suite.cluster.GetServer(newLeaderName) + re.NotEqual(originLeaderName, newLeaderName) check() + originLeaderName = suite.pdLeader.GetLeader().GetName() suite.pdLeader.ResignLeader() - suite.pdLeader = suite.cluster.GetServer(suite.cluster.WaitLeader()) + newLeaderName = suite.cluster.WaitLeader() + suite.pdLeader = suite.cluster.GetServer(newLeaderName) + re.NotEqual(originLeaderName, newLeaderName) }