Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add engine filter to exclude special stores #2426

Merged
merged 10 commits into from
May 22, 2020
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.
nolouch marked this conversation as resolved.
Show resolved Hide resolved
// 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