Skip to content

Commit

Permalink
operator: allows to skip placement rules checks (#5458) (#5460)
Browse files Browse the repository at this point in the history
close #5401, ref #5458

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
Signed-off-by: HunDunDM <hundundm@gmail.com>

Co-authored-by: 混沌DM <hundundm@gmail.com>
Co-authored-by: HunDunDM <hundundm@gmail.com>
  • Loading branch information
ti-chi-bot and HunDunDM authored Sep 27, 2022
1 parent c7dcc3c commit ba7f7e8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
24 changes: 16 additions & 8 deletions server/schedule/operator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
)

// Builder is used to create operators. Usage:
// op, err := NewBuilder(desc, cluster, region).
// RemovePeer(store1).
// AddPeer(peer1).
// SetLeader(store2).
// Build(kind)
//
// op, err := NewBuilder(desc, cluster, region).
// RemovePeer(store1).
// AddPeer(peer1).
// SetLeader(store2).
// Build(kind)
//
// The generated Operator will choose the most appropriate execution order
// according to various constraints.
type Builder struct {
Expand All @@ -52,8 +54,9 @@ type Builder struct {
targetLeaderStoreID uint64
err error

// skip origin check flags
// skip check flags
skipOriginJointStateCheck bool
skipPlacementRulesCheck bool

// build flags
allowDemote bool
Expand All @@ -80,6 +83,11 @@ func SkipOriginJointStateCheck(b *Builder) {
b.skipOriginJointStateCheck = true
}

// SkipPlacementRulesCheck lets the builder skip the placement rules check for origin and target peers.
func SkipPlacementRulesCheck(b *Builder) {
b.skipPlacementRulesCheck = true
}

// NewBuilder creates a Builder.
func NewBuilder(desc string, cluster opt.Cluster, region *core.RegionInfo, opts ...BuilderOption) *Builder {
b := &Builder{
Expand Down Expand Up @@ -123,7 +131,7 @@ func NewBuilder(desc string, cluster opt.Cluster, region *core.RegionInfo, opts

// placement rules
var rules []*placement.Rule
if err == nil && cluster.GetOpts().IsPlacementRulesEnabled() {
if err == nil && !b.skipPlacementRulesCheck && cluster.GetOpts().IsPlacementRulesEnabled() {
fit := cluster.FitRegion(region)
for _, rf := range fit.RuleFits {
rules = append(rules, rf.Rule)
Expand Down Expand Up @@ -728,7 +736,7 @@ func (b *Builder) allowLeader(peer *metapb.Peer, ignoreClusterLimit bool) bool {
}

// placement rules
if len(b.rules) == 0 {
if b.skipPlacementRulesCheck || len(b.rules) == 0 {
return true
}
for _, r := range b.rules {
Expand Down
4 changes: 2 additions & 2 deletions server/schedule/operator/create_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func CreateTransferLeaderOperator(desc string, cluster opt.Cluster, region *core

// CreateForceTransferLeaderOperator creates an operator that transfers the leader from a source store to a target store forcible.
func CreateForceTransferLeaderOperator(desc string, cluster opt.Cluster, region *core.RegionInfo, sourceStoreID uint64, targetStoreID uint64, kind OpKind) (*Operator, error) {
return NewBuilder(desc, cluster, region, SkipOriginJointStateCheck).
return NewBuilder(desc, cluster, region, SkipOriginJointStateCheck, SkipPlacementRulesCheck).
SetLeader(targetStoreID).
EnableForceTargetLeader().
Build(kind)
Expand Down Expand Up @@ -211,7 +211,7 @@ func CreateScatterRegionOperator(desc string, cluster opt.Cluster, origin *core.

// CreateLeaveJointStateOperator creates an operator that let region leave joint state.
func CreateLeaveJointStateOperator(desc string, cluster opt.Cluster, origin *core.RegionInfo) (*Operator, error) {
b := NewBuilder(desc, cluster, origin, SkipOriginJointStateCheck)
b := NewBuilder(desc, cluster, origin, SkipOriginJointStateCheck, SkipPlacementRulesCheck)

if b.err == nil && !core.IsInJointState(origin.GetPeers()...) {
b.err = errors.Errorf("cannot build leave joint state operator for region which is not in joint state")
Expand Down
63 changes: 63 additions & 0 deletions server/schedule/operator/create_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package operator

import (
"context"
"encoding/hex"
"strings"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -1073,3 +1074,65 @@ func (s *testCreateOperatorSuite) TestMoveRegionWithoutJointConsensus(c *C) {
}
}
}

var _ = Suite(&testCreateOperatorWithRulesSuite{})

type testCreateOperatorWithRulesSuite struct{}

// Ref https://github.com/tikv/pd/issues/5401
func (s *testCreateOperatorWithRulesSuite) TestCreateLeaveJointStateOperatorWithoutFitRules(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

opts := config.NewTestOptions()
cluster := mockcluster.NewCluster(ctx, opts)
err := cluster.SetRules([]*placement.Rule{
{
GroupID: "pd",
ID: "default",
StartKeyHex: hex.EncodeToString([]byte("")),
EndKeyHex: hex.EncodeToString([]byte("")),
Role: placement.Voter,
Count: 1,
},
{
GroupID: "t1",
ID: "t1",
StartKeyHex: hex.EncodeToString([]byte("a")),
EndKeyHex: hex.EncodeToString([]byte("b")),
Role: placement.Voter,
Count: 1,
},
{
GroupID: "t2",
ID: "t2",
StartKeyHex: hex.EncodeToString([]byte("b")),
EndKeyHex: hex.EncodeToString([]byte("c")),
Role: placement.Voter,
Count: 1,
},
})
c.Assert(err, IsNil)
cluster.AddRegionStore(1, 1)
cluster.AddRegionStore(2, 1)
cluster.AddRegionStore(3, 1)
cluster.AddRegionStore(4, 1)
originPeers := []*metapb.Peer{
{Id: 3, StoreId: 3, Role: metapb.PeerRole_DemotingVoter},
{Id: 4, StoreId: 4, Role: metapb.PeerRole_IncomingVoter},
}

region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: originPeers, StartKey: []byte("a"), EndKey: []byte("c")}, originPeers[0])
op, err := CreateLeaveJointStateOperator("test", cluster, region)
c.Assert(err, IsNil)
c.Assert(op.kind, Equals, OpLeader)
c.Assert(op.steps, HasLen, 2)
step0 := op.steps[0].(TransferLeader)
c.Assert(step0.FromStore, Equals, uint64(3))
c.Assert(step0.ToStore, Equals, uint64(4))
step1 := op.steps[1].(ChangePeerV2Leave)
c.Assert(step1.PromoteLearners, HasLen, 1)
c.Assert(step1.DemoteVoters, HasLen, 1)
c.Assert(step1.PromoteLearners[0].ToStore, Equals, uint64(4))
c.Assert(step1.DemoteVoters[0].ToStore, Equals, uint64(3))
}

0 comments on commit ba7f7e8

Please sign in to comment.