Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: HunDunDM <hundundm@gmail.com>
  • Loading branch information
HunDunDM committed Oct 26, 2022
1 parent 85a3a5b commit ecb7e49
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 45 deletions.
4 changes: 2 additions & 2 deletions server/api/trend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTrend(t *testing.T) {
newPeerID := op.Step(0).(operator.AddLearner).PeerID
region5 = region5.Clone(core.WithAddPeer(&metapb.Peer{Id: newPeerID, StoreId: 3, Role: metapb.PeerRole_Learner}), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region5)
region5 = region5.Clone(core.WithPromoteLearner(newPeerID), core.WithRemoveStorePeer(2), core.WithIncConfVer())
region5 = region5.Clone(core.WithRole(newPeerID, metapb.PeerRole_Voter), core.WithRemoveStorePeer(2), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region5)

op, err = svr.GetHandler().GetOperator(6)
Expand All @@ -71,7 +71,7 @@ func TestTrend(t *testing.T) {
newPeerID = op.Step(0).(operator.AddLearner).PeerID
region6 = region6.Clone(core.WithAddPeer(&metapb.Peer{Id: newPeerID, StoreId: 3, Role: metapb.PeerRole_Learner}), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region6)
region6 = region6.Clone(core.WithPromoteLearner(newPeerID), core.WithLeader(region6.GetStorePeer(2)), core.WithRemoveStorePeer(1), core.WithIncConfVer())
region6 = region6.Clone(core.WithRole(newPeerID, metapb.PeerRole_Voter), core.WithLeader(region6.GetStorePeer(2)), core.WithRemoveStorePeer(1), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region6)

var trend Trend
Expand Down
39 changes: 3 additions & 36 deletions server/core/region_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,45 +325,12 @@ func WithAddPeer(peer *metapb.Peer) RegionCreateOption {
}
}

// WithPromoteLearner promotes the learner.
func WithPromoteLearner(peerID uint64) RegionCreateOption {
// WithRole changes the role.
func WithRole(peerID uint64, role metapb.PeerRole) RegionCreateOption {
return func(region *RegionInfo) {
for _, p := range region.GetPeers() {
if p.GetId() == peerID {
p.Role = metapb.PeerRole_Voter
}
}
}
}

// WithPromoteLearnerEnter promotes the learner to the joint enter stage.
func WithPromoteLearnerEnter(peerID uint64) RegionCreateOption {
return func(region *RegionInfo) {
for _, p := range region.GetPeers() {
if p.GetId() == peerID {
p.Role = metapb.PeerRole_IncomingVoter
}
}
}
}

// WithDemoteVoter demotes the voter.
func WithDemoteVoter(peerID uint64) RegionCreateOption {
return func(region *RegionInfo) {
for _, p := range region.GetPeers() {
if p.GetId() == peerID {
p.Role = metapb.PeerRole_Learner
}
}
}
}

// WithDemoteVoterEnter demotes the voter to the joint enter stage.
func WithDemoteVoterEnter(peerID uint64) RegionCreateOption {
return func(region *RegionInfo) {
for _, p := range region.GetPeers() {
if p.GetId() == peerID {
p.Role = metapb.PeerRole_DemotingVoter
p.Role = role
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/schedule/operator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func (suite *operatorControllerTestSuite) TestDispatchUnfinishedStep() {
suite.Equal(2, stream.MsgLength())

region4 := region3.Clone(
core.WithPromoteLearner(3),
core.WithRole(3, metapb.PeerRole_Voter),
core.WithIncConfVer(),
)
suite.True(steps[1].IsFinish(region4))
Expand Down
8 changes: 2 additions & 6 deletions tools/pd-simulator/simulator/task.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 TiKV Project Authors.
// Copyright 2017 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -295,21 +295,17 @@ func checkAndCreateChangePeerOption(engine *RaftEngine, region *core.RegionInfo,
return nil
}
// create option
opts := []core.RegionCreateOption{core.WithIncConfVer()}
switch to {
case metapb.PeerRole_Voter: // Learner/IncomingVoter -> Voter
engine.schedulerStats.taskStats.incPromoteLearner(region.GetID())
return append(opts, core.WithPromoteLearner(peer.GetId()))
case metapb.PeerRole_Learner: // Voter/DemotingVoter -> Learner
engine.schedulerStats.taskStats.incDemoteVoter(region.GetID())
return append(opts, core.WithDemoteVoter(peer.GetId()))
case metapb.PeerRole_IncomingVoter: // Learner -> IncomingVoter, only in joint state
return append(opts, core.WithPromoteLearnerEnter(peer.GetId()))
case metapb.PeerRole_DemotingVoter: // Voter -> DemotingVoter, only in joint state
return append(opts, core.WithDemoteVoterEnter(peer.GetId()))
default:
return nil
}
return []core.RegionCreateOption{core.WithRole(peer.GetId(), to), core.WithIncConfVer()}
}

type promoteLearner struct {
Expand Down

0 comments on commit ecb7e49

Please sign in to comment.