Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jul 18, 2024
1 parent cc99e73 commit 392ae60
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 36 deletions.
22 changes: 9 additions & 13 deletions pkg/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ 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 @@ -188,7 +187,13 @@ func (m *EmbeddedEtcdMember) CampaignLeader(ctx context.Context, leaseTimeout in
failpoint.Return(m.leadership.Campaign(leaseTimeout, m.MemberValue()))
})

if m.leadership.GetCampaignTimesNum() > campaignLeaderFrequencyTimes {
checkTimes := campaignLeaderFrequencyTimes
failpoint.Inject("changeFrequencyTimes", func(val failpoint.Value) {
if v, ok := val.(int); ok {
checkTimes = v
}
})
if m.leadership.GetCampaignTimesNum() > checkTimes {
if err := m.ResignEtcdLeader(ctx, m.Name(), ""); err != nil {
return err
}
Expand Down Expand Up @@ -555,12 +560,3 @@ 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
}
7 changes: 3 additions & 4 deletions tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ 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 @@ -166,11 +165,11 @@ func TestClientLeaderChange(t *testing.T) {
}

func TestLeaderTransferAndMoveCluster(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 3)
Expand Down
7 changes: 3 additions & 4 deletions tests/integrations/mcs/scheduling/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ 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 @@ -652,11 +651,11 @@ func (suite *multipleServerTestSuite) TearDownSuite() {
}

func (suite *multipleServerTestSuite) TestReElectLeader() {
beforeTimes := member.ChangeFrequencyTimes(10)
re := suite.Require()
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
re := suite.Require()
tc, err := tests.NewTestSchedulingCluster(suite.ctx, 1, suite.backendEndpoints)
re.NoError(err)
defer tc.Destroy()
Expand Down
13 changes: 6 additions & 7 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ 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 @@ -184,11 +183,11 @@ func TestDamagedRegion(t *testing.T) {
}

func TestRegionStatistics(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tc, err := tests.NewTestCluster(ctx, 3)
Expand Down Expand Up @@ -1647,11 +1646,11 @@ func TestMinResolvedTS(t *testing.T) {

// See https://github.com/tikv/pd/issues/4941
func TestTransferLeaderBack(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tc, err := tests.NewTestCluster(ctx, 2)
Expand Down
8 changes: 4 additions & 4 deletions tests/server/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"sync"
"testing"

"github.com/pingcap/failpoint"
"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 @@ -107,11 +107,11 @@ func TestCommand(t *testing.T) {
}

func TestMonotonicID(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 2)
Expand Down
7 changes: 3 additions & 4 deletions tests/server/region_syncer/region_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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 @@ -255,11 +254,11 @@ func TestPrepareChecker(t *testing.T) {

// ref: https://github.com/tikv/pd/issues/6988
func TestPrepareCheckerWithTransferLeader(t *testing.T) {
beforeTimes := member.ChangeFrequencyTimes(10)
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
member.ChangeFrequencyTimes(beforeTimes)
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker", `return(true)`))
Expand Down

0 comments on commit 392ae60

Please sign in to comment.