Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jul 17, 2024
1 parent ecaef02 commit 66434af
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
14 changes: 12 additions & 2 deletions pkg/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ const (
// The timeout to wait transfer etcd leader to complete.
moveLeaderTimeout = 5 * time.Second
dcLocationConfigEtcdPrefix = "dc-location"
// If the campaign times is more than this value in `campaignTimesRecordTimeout`, the PD will resign and campaign again.
campaignLeaderFrequencyTimes = 3
)

// If the campaign times is more than this value in `campaignTimesRecordTimeout`, the PD will resign and campaign again.
var campaignLeaderFrequencyTimes = 3

// EmbeddedEtcdMember is used for the election related logic. It implements Member interface.
type EmbeddedEtcdMember struct {
leadership *election.Leadership
Expand Down Expand Up @@ -554,3 +555,12 @@ func (m *EmbeddedEtcdMember) SetMemberGitHash(id uint64, gitHash string) error {
func (m *EmbeddedEtcdMember) Close() {
m.Etcd().Close()
}

// ChangeFrequencyTimes changes the frequency check times of campaign leader.
// ONLY used for test to make the test more stable.
// PLEASE flash back this value after using.
func ChangeFrequencyTimes(times int) int {
before := campaignLeaderFrequencyTimes
campaignLeaderFrequencyTimes = times
return before
}
5 changes: 5 additions & 0 deletions tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/tikv/pd/pkg/member"
"math"
"path"
"reflect"
Expand Down Expand Up @@ -165,6 +166,10 @@ func TestClientLeaderChange(t *testing.T) {
}

func TestLeaderTransferAndMoveCluster(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
5 changes: 5 additions & 0 deletions tests/integrations/mcs/scheduling/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package scheduling
import (
"context"
"fmt"
"github.com/tikv/pd/pkg/member"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -651,6 +652,10 @@ func (suite *multipleServerTestSuite) TearDownSuite() {
}

func (suite *multipleServerTestSuite) TestReElectLeader() {
beforeTimes := member.ChangeFrequencyTimes(10)
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
}()
re := suite.Require()
tc, err := tests.NewTestSchedulingCluster(suite.ctx, 1, suite.backendEndpoints)
re.NoError(err)
Expand Down
15 changes: 12 additions & 3 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cluster_test
import (
"context"
"fmt"
"github.com/tikv/pd/pkg/member"
"math"
"strconv"
"sync"
Expand Down Expand Up @@ -183,6 +184,10 @@ func TestDamagedRegion(t *testing.T) {
}

func TestRegionStatistics(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -238,7 +243,7 @@ func TestRegionStatistics(t *testing.T) {
re.Len(regions, 1)

leaderServer.ResignLeader()
re.NotEqual(tc.WaitLeader(), leaderName)
re.NotEqual(leaderName, tc.WaitLeader())
leaderServer = tc.GetLeaderServer()
leaderName = leaderServer.GetServer().Name()
rc = leaderServer.GetRaftCluster()
Expand All @@ -255,11 +260,11 @@ func TestRegionStatistics(t *testing.T) {
re.False(r.LoadedFromStorage() && r.LoadedFromSync())

leaderServer.ResignLeader()
re.NotEqual(tc.WaitLeader(), leaderName)
re.NotEqual(leaderName, tc.WaitLeader())
leaderServer = tc.GetLeaderServer()
leaderName = leaderServer.GetServer().Name()
leaderServer.ResignLeader()
re.NotEqual(tc.WaitLeader(), leaderName)
re.NotEqual(leaderName, tc.WaitLeader())
rc = tc.GetLeaderServer().GetRaftCluster()
r = rc.GetRegion(region.Id)
re.NotNil(r)
Expand Down Expand Up @@ -1642,6 +1647,10 @@ func TestMinResolvedTS(t *testing.T) {

// See https://github.com/tikv/pd/issues/4941
func TestTransferLeaderBack(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
5 changes: 5 additions & 0 deletions tests/server/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package id_test

import (
"context"
"github.com/tikv/pd/pkg/member"
"sync"
"testing"

Expand Down Expand Up @@ -106,6 +107,10 @@ func TestCommand(t *testing.T) {
}

func TestMonotonicID(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
7 changes: 6 additions & 1 deletion tests/server/region_syncer/region_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package syncer_test

import (
"context"
"github.com/tikv/pd/pkg/member"
"testing"
"time"

Expand Down Expand Up @@ -254,6 +255,10 @@ func TestPrepareChecker(t *testing.T) {

// ref: https://github.com/tikv/pd/issues/6988
func TestPrepareCheckerWithTransferLeader(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -298,7 +303,7 @@ func TestPrepareCheckerWithTransferLeader(t *testing.T) {
leaderServer = cluster.GetLeaderServer()
err = cluster.ResignLeader()
re.NoError(err)
re.NotEqual(leaderServer.GetLeader().GetName(), cluster.WaitLeader())
re.NotEqual(leaderServer.GetServer().Name(), cluster.WaitLeader())
rc = cluster.GetLeaderServer().GetServer().GetRaftCluster()
re.True(rc.IsPrepared())
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker"))
Expand Down

0 comments on commit 66434af

Please sign in to comment.