Skip to content

Commit

Permalink
*: add engine filter to exclude special stores (#2426) (#2447)
Browse files Browse the repository at this point in the history
* *: add engine filter

Signed-off-by: nolouch <nolouch@gmail.com>

* add comments

Signed-off-by: nolouch <nolouch@gmail.com>

* address comments

Signed-off-by: nolouch <nolouch@gmail.com>

* address comments

Signed-off-by: nolouch <nolouch@gmail.com>

* address comments

Signed-off-by: nolouch <nolouch@gmail.com>

Co-authored-by: nolouch <nolouch@gmail.com>
  • Loading branch information
sre-bot and nolouch authored May 25, 2020
1 parent 8deb1ee commit 0fb3adf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
52 changes: 49 additions & 3 deletions server/schedule/filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,51 @@ func (f *ruleFitFilter) Target(opt opt.Options, store *core.StoreInfo) bool {
return placement.CompareRegionFit(f.oldFit, newFit) <= 0
}

type engineFilter struct {
scope string
constraint placement.LabelConstraint
}

// NewEngineFilter creates a filter that filters out default engine stores.
// By default, all stores that are not marked with a special engine will be filtered out.
// Specify the special engine label if you want to include the special stores.
func NewEngineFilter(scope string, allowEngines ...string) Filter {
var values []string
for _, v := range allSpeicalEngines {
if slice.NoneOf(allowEngines, func(i int) bool { return allowEngines[i] == v }) {
values = append(values, v)
}
}
return &engineFilter{
scope: scope,
constraint: placement.LabelConstraint{Key: "engine", Op: "notIn", Values: values},
}
}

func (f *engineFilter) Scope() string {
return f.scope
}

func (f *engineFilter) Type() string {
return "engine-filter"
}

func (f *engineFilter) Source(opt opt.Options, store *core.StoreInfo) bool {
return f.constraint.MatchStore(store)
}

func (f *engineFilter) Target(opt opt.Options, store *core.StoreInfo) bool {
return f.constraint.MatchStore(store)
}

type specialUseFilter struct {
scope string
constraint placement.LabelConstraint
}

// NewSpecialUseFilter creates a filter that filters stores for special use.
// NewSpecialUseFilter creates a filter that filters out normal stores.
// By default, all stores that are not marked with a special use will be filtered out.
// Specify the special use label if you want to include the special stores.
func NewSpecialUseFilter(scope string, allowUses ...string) Filter {
var values []string
for _, v := range allSpecialUses {
Expand All @@ -558,7 +597,7 @@ func NewSpecialUseFilter(scope string, allowUses ...string) Filter {
}
return &specialUseFilter{
scope: scope,
constraint: placement.LabelConstraint{Key: specialUseKey, Op: "in", Values: values},
constraint: placement.LabelConstraint{Key: SpecialUseKey, Op: "in", Values: values},
}
}

Expand All @@ -582,11 +621,18 @@ func (f *specialUseFilter) Target(opt opt.Options, store *core.StoreInfo) bool {
}

const (
specialUseKey = "specialUse"
// SpecialUseKey is the label used to indicate special use storage.
SpecialUseKey = "specialUse"
// SpecialUseHotRegion is the hot region value of special use label
SpecialUseHotRegion = "hotRegion"
// SpecialUseReserved is the reserved value of special use label
SpecialUseReserved = "reserved"

// EngineKey is the label key used to indicate engine.
EngineKey = "engine"
// EngineTiFlash is the tiflash value of the engine label.
EngineTiFlash = "tiflash"
)

var allSpecialUses = []string{SpecialUseHotRegion, SpecialUseReserved}
var allSpeicalEngines = []string{EngineTiFlash}
1 change: 1 addition & 0 deletions server/schedule/region_scatterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewRegionScatterer(cluster opt.Cluster) *RegionScatterer {
cluster: cluster,
filters: []filter.Filter{
filter.StoreStateFilter{ActionScope: regionScatterName},
filter.NewEngineFilter(regionScatterName),
},
selected: newSelectedStores(),
}
Expand Down
10 changes: 7 additions & 3 deletions server/schedulers/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ var _ = Suite(&testScatterRegionSuite{})
type testScatterRegionSuite struct{}

func (s *testScatterRegionSuite) TestSixStores(c *C) {
s.scatter(c, 6, 4)
s.scatter(c, 6, 4, false)
s.scatter(c, 6, 4, true)
}

func (s *testScatterRegionSuite) TestFiveStores(c *C) {
s.scatter(c, 5, 5)
s.scatter(c, 5, 5, false)
s.scatter(c, 5, 5, true)
}

func (s *testScatterRegionSuite) checkOperator(op *operator.Operator, c *C) {
Expand All @@ -209,14 +211,16 @@ func (s *testScatterRegionSuite) checkOperator(op *operator.Operator, c *C) {
}
}

func (s *testScatterRegionSuite) scatter(c *C, numStores, numRegions uint64) {
func (s *testScatterRegionSuite) scatter(c *C, numStores, numRegions uint64, useRules bool) {
opt := mockoption.NewScheduleOptions()
tc := mockcluster.NewCluster(opt)

// Add stores 1~6.
for i := uint64(1); i <= numStores; i++ {
tc.AddRegionStore(i, 0)
}
tc.AddLabelsStore(numStores+1, 0, map[string]string{"engine": "tiflash"})
tc.EnablePlacementRules = useRules

// Add regions 1~4.
seq := newSequencer(3)
Expand Down
2 changes: 2 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ func checkBootstrapRequest(clusterID uint64, req *pdpb.BootstrapRequest) error {
return nil
}

// isTiFlashStore used to judge flash store.
// FIXME: remove the hack way
func isTiFlashStore(store *metapb.Store) bool {
for _, l := range store.GetLabels() {
if l.GetKey() == "engine" && l.GetValue() == "tiflash" {
Expand Down

0 comments on commit 0fb3adf

Please sign in to comment.