Skip to content

Commit

Permalink
change frequentcy check time
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 cc99e73
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 @@ -45,6 +45,7 @@ import (
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/member"
"github.com/tikv/pd/pkg/mock/mockid"
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/tso"
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 @@ -30,6 +30,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core/storelimit"
mcs "github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/member"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/schedulers"
"github.com/tikv/pd/pkg/utils/testutil"
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 @@ -34,6 +34,7 @@ import (
"github.com/tikv/pd/pkg/core/storelimit"
"github.com/tikv/pd/pkg/dashboard"
"github.com/tikv/pd/pkg/id"
"github.com/tikv/pd/pkg/member"
"github.com/tikv/pd/pkg/mock/mockid"
sc "github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/schedule/operator"
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 @@ -21,6 +21,7 @@ import (

"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/member"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/tests"
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 @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/member"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/tests"
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 cc99e73

Please sign in to comment.