From 3688a2f696d17e19cfb11d05a74399c7d970a95f Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 15 May 2020 17:10:21 +0800 Subject: [PATCH 1/5] *: add engine filter Signed-off-by: nolouch --- server/schedule/filter/filters.go | 46 +++++++++++++++++++++++++++-- server/schedule/region_scatterer.go | 1 + server/schedulers/scheduler_test.go | 10 +++++-- server/util.go | 2 ++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/server/schedule/filter/filters.go b/server/schedule/filter/filters.go index bcac4c4aff7..cc02d8af91d 100644 --- a/server/schedule/filter/filters.go +++ b/server/schedule/filter/filters.go @@ -543,6 +543,41 @@ 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 store for special engine. +func NewEngineFilter(scope string, allowUses ...string) Filter { + var values []string + for _, v := range allEngineLabels { + if slice.NoneOf(allowUses, func(i int) bool { return allowUses[i] == v }) { + values = append(values, v) + } + } + return &engineFilter{ + scope: scope, + constraint: placement.LabelConstraint{Key: "engine", Op: "in", 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 @@ -558,7 +593,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}, } } @@ -582,11 +617,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 allEngineLabels = []string{EngineTiFlash} diff --git a/server/schedule/region_scatterer.go b/server/schedule/region_scatterer.go index d12aad0b904..a7759dbbf0c 100644 --- a/server/schedule/region_scatterer.go +++ b/server/schedule/region_scatterer.go @@ -82,6 +82,7 @@ func NewRegionScatterer(cluster opt.Cluster) *RegionScatterer { cluster: cluster, filters: []filter.Filter{ filter.StoreStateFilter{ActionScope: regionScatterName}, + filter.NewEngineFilter(regionScatterName), }, selected: newSelectedStores(), } diff --git a/server/schedulers/scheduler_test.go b/server/schedulers/scheduler_test.go index b6f50709acc..da54e478115 100644 --- a/server/schedulers/scheduler_test.go +++ b/server/schedulers/scheduler_test.go @@ -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) { @@ -209,7 +211,7 @@ 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) @@ -217,6 +219,8 @@ func (s *testScatterRegionSuite) scatter(c *C, numStores, numRegions uint64) { 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) diff --git a/server/util.go b/server/util.go index 564eb872d77..45e11a706ff 100644 --- a/server/util.go +++ b/server/util.go @@ -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" { From 19fd28218457a7b650971c70549b9d55f68ec36b Mon Sep 17 00:00:00 2001 From: nolouch Date: Thu, 21 May 2020 01:23:42 +0800 Subject: [PATCH 2/5] add comments Signed-off-by: nolouch --- server/schedule/filter/filters.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/server/schedule/filter/filters.go b/server/schedule/filter/filters.go index cc02d8af91d..298b2aa079e 100644 --- a/server/schedule/filter/filters.go +++ b/server/schedule/filter/filters.go @@ -548,11 +548,13 @@ type engineFilter struct { constraint placement.LabelConstraint } -// NewEngineFilter creates a filter that filters store for special engine. -func NewEngineFilter(scope string, allowUses ...string) Filter { +// NewEngineFilter creates a filter that filters stores to filter 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, allowSpeicalEngines ...string) Filter { var values []string - for _, v := range allEngineLabels { - if slice.NoneOf(allowUses, func(i int) bool { return allowUses[i] == v }) { + for _, v := range allSpeicalEngineLabels { + if slice.NoneOf(allowSpeicalEngines, func(i int) bool { return allowSpeicalEngines[i] == v }) { values = append(values, v) } } @@ -583,7 +585,9 @@ type specialUseFilter struct { constraint placement.LabelConstraint } -// NewSpecialUseFilter creates a filter that filters stores for special use. +// NewSpecialUseFilter creates a filter that filters 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 { @@ -631,4 +635,4 @@ const ( ) var allSpecialUses = []string{SpecialUseHotRegion, SpecialUseReserved} -var allEngineLabels = []string{EngineTiFlash} +var allSpeicalEngineLabels = []string{EngineTiFlash} From ec438be1fabbb84f353e34c647ed238726a4e393 Mon Sep 17 00:00:00 2001 From: nolouch Date: Thu, 21 May 2020 17:03:45 +0800 Subject: [PATCH 3/5] address comments Signed-off-by: nolouch --- server/schedule/filter/filters.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/schedule/filter/filters.go b/server/schedule/filter/filters.go index 298b2aa079e..2fe8747acbb 100644 --- a/server/schedule/filter/filters.go +++ b/server/schedule/filter/filters.go @@ -548,7 +548,7 @@ type engineFilter struct { constraint placement.LabelConstraint } -// NewEngineFilter creates a filter that filters stores to filter out default engine stores. +// 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, allowSpeicalEngines ...string) Filter { @@ -585,7 +585,7 @@ type specialUseFilter struct { constraint placement.LabelConstraint } -// NewSpecialUseFilter creates a filter that filters normal stores. +// 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 { From 44f19a4d87d1bad7414cbaf73973e2a46b282148 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 22 May 2020 16:05:19 +0800 Subject: [PATCH 4/5] address comments Signed-off-by: nolouch --- server/schedule/filter/filters.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/schedule/filter/filters.go b/server/schedule/filter/filters.go index 2fe8747acbb..ce53550c99e 100644 --- a/server/schedule/filter/filters.go +++ b/server/schedule/filter/filters.go @@ -560,7 +560,7 @@ func NewEngineFilter(scope string, allowSpeicalEngines ...string) Filter { } return &engineFilter{ scope: scope, - constraint: placement.LabelConstraint{Key: "engine", Op: "in", Values: values}, + constraint: placement.LabelConstraint{Key: "engine", Op: "notIn", Values: values}, } } @@ -573,11 +573,11 @@ func (f *engineFilter) Type() string { } func (f *engineFilter) Source(opt opt.Options, store *core.StoreInfo) bool { - return !f.constraint.MatchStore(store) + return f.constraint.MatchStore(store) } func (f *engineFilter) Target(opt opt.Options, store *core.StoreInfo) bool { - return !f.constraint.MatchStore(store) + return f.constraint.MatchStore(store) } type specialUseFilter struct { From 1dc252a66bbdb1f05f345658f1322812e5576ed4 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 22 May 2020 16:35:33 +0800 Subject: [PATCH 5/5] address comments Signed-off-by: nolouch --- server/schedule/filter/filters.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/schedule/filter/filters.go b/server/schedule/filter/filters.go index ce53550c99e..631b1338ef9 100644 --- a/server/schedule/filter/filters.go +++ b/server/schedule/filter/filters.go @@ -551,10 +551,10 @@ type engineFilter struct { // 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, allowSpeicalEngines ...string) Filter { +func NewEngineFilter(scope string, allowEngines ...string) Filter { var values []string - for _, v := range allSpeicalEngineLabels { - if slice.NoneOf(allowSpeicalEngines, func(i int) bool { return allowSpeicalEngines[i] == v }) { + for _, v := range allSpeicalEngines { + if slice.NoneOf(allowEngines, func(i int) bool { return allowEngines[i] == v }) { values = append(values, v) } } @@ -635,4 +635,4 @@ const ( ) var allSpecialUses = []string{SpecialUseHotRegion, SpecialUseReserved} -var allSpeicalEngineLabels = []string{EngineTiFlash} +var allSpeicalEngines = []string{EngineTiFlash}