Skip to content

Commit d9aa756

Browse files
rleungxsre-bot
authored andcommitted
schedule: add more dimension for filter metrics (#1746) (#1911)
Signed-off-by: Ryan Leung <rleungx@gmail.com>
1 parent 6571002 commit d9aa756

17 files changed

+204
-151
lines changed

server/schedule/filters.go

Lines changed: 79 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525

2626
// Filter is an interface to filter source and target store.
2727
type Filter interface {
28+
// Scope is used to indicate where the filter will act on.
29+
Scope() string
2830
Type() string
2931
// Return true if the store should not be used as a source store.
3032
FilterSource(opt Options, store *core.StoreInfo) bool
@@ -38,7 +40,7 @@ func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool {
3840
storeID := fmt.Sprintf("%d", store.GetID())
3941
for _, filter := range filters {
4042
if filter.FilterSource(opt, store) {
41-
filterCounter.WithLabelValues("filter-source", storeAddress, storeID, filter.Type()).Inc()
43+
filterCounter.WithLabelValues("filter-source", storeAddress, storeID, filter.Scope(), filter.Type()).Inc()
4244
return true
4345
}
4446
}
@@ -51,26 +53,32 @@ func FilterTarget(opt Options, store *core.StoreInfo, filters []Filter) bool {
5153
storeID := fmt.Sprintf("%d", store.GetID())
5254
for _, filter := range filters {
5355
if filter.FilterTarget(opt, store) {
54-
filterCounter.WithLabelValues("filter-target", storeAddress, storeID, filter.Type()).Inc()
56+
filterCounter.WithLabelValues("filter-target", storeAddress, storeID, filter.Scope(), filter.Type()).Inc()
5557
return true
5658
}
5759
}
5860
return false
5961
}
6062

6163
type excludedFilter struct {
64+
scope string
6265
sources map[uint64]struct{}
6366
targets map[uint64]struct{}
6467
}
6568

6669
// NewExcludedFilter creates a Filter that filters all specified stores.
67-
func NewExcludedFilter(sources, targets map[uint64]struct{}) Filter {
70+
func NewExcludedFilter(scope string, sources, targets map[uint64]struct{}) Filter {
6871
return &excludedFilter{
72+
scope: scope,
6973
sources: sources,
7074
targets: targets,
7175
}
7276
}
7377

78+
func (f *excludedFilter) Scope() string {
79+
return f.scope
80+
}
81+
7482
func (f *excludedFilter) Type() string {
7583
return "exclude-filter"
7684
}
@@ -85,30 +93,15 @@ func (f *excludedFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
8593
return ok
8694
}
8795

88-
type blockFilter struct{}
89-
90-
// NewBlockFilter creates a Filter that filters all stores that are blocked from balance.
91-
func NewBlockFilter() Filter {
92-
return &blockFilter{}
93-
}
94-
95-
func (f *blockFilter) Type() string {
96-
return "block-filter"
97-
}
98-
99-
func (f *blockFilter) FilterSource(opt Options, store *core.StoreInfo) bool {
100-
return store.IsBlocked()
101-
}
96+
type overloadFilter struct{ scope string }
10297

103-
func (f *blockFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
104-
return store.IsBlocked()
98+
// NewOverloadFilter creates a Filter that filters all stores that are overloaded from balance.
99+
func NewOverloadFilter(scope string) Filter {
100+
return &overloadFilter{scope: scope}
105101
}
106102

107-
type overloadFilter struct{}
108-
109-
// NewOverloadFilter creates a Filter that filters all stores that are overloaded from balance.
110-
func NewOverloadFilter() Filter {
111-
return &overloadFilter{}
103+
func (f *overloadFilter) Scope() string {
104+
return f.scope
112105
}
113106

114107
func (f *overloadFilter) Type() string {
@@ -123,11 +116,15 @@ func (f *overloadFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
123116
return store.IsOverloaded()
124117
}
125118

126-
type stateFilter struct{}
119+
type stateFilter struct{ scope string }
127120

128121
// NewStateFilter creates a Filter that filters all stores that are not UP.
129-
func NewStateFilter() Filter {
130-
return &stateFilter{}
122+
func NewStateFilter(scope string) Filter {
123+
return &stateFilter{scope: scope}
124+
}
125+
126+
func (f *stateFilter) Scope() string {
127+
return f.scope
131128
}
132129

133130
func (f *stateFilter) Type() string {
@@ -142,11 +139,15 @@ func (f *stateFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
142139
return !store.IsUp()
143140
}
144141

145-
type healthFilter struct{}
142+
type healthFilter struct{ scope string }
146143

147144
// NewHealthFilter creates a Filter that filters all stores that are Busy or Down.
148-
func NewHealthFilter() Filter {
149-
return &healthFilter{}
145+
func NewHealthFilter(scope string) Filter {
146+
return &healthFilter{scope: scope}
147+
}
148+
149+
func (f *healthFilter) Scope() string {
150+
return f.scope
150151
}
151152

152153
func (f *healthFilter) Type() string {
@@ -168,31 +169,16 @@ func (f *healthFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
168169
return f.filter(opt, store)
169170
}
170171

171-
type disconnectFilter struct{}
172-
173-
// NewDisconnectFilter creates a Filter that filters all stores that are disconnected.
174-
func NewDisconnectFilter() Filter {
175-
return &disconnectFilter{}
176-
}
177-
178-
func (f *disconnectFilter) Type() string {
179-
return "disconnect-filter"
180-
}
181-
182-
func (f *disconnectFilter) FilterSource(opt Options, store *core.StoreInfo) bool {
183-
return store.IsDisconnected()
184-
}
185-
186-
func (f *disconnectFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
187-
return store.IsDisconnected()
188-
}
189-
190-
type pendingPeerCountFilter struct{}
172+
type pendingPeerCountFilter struct{ scope string }
191173

192174
// NewPendingPeerCountFilter creates a Filter that filters all stores that are
193175
// currently handling too many pending peers.
194-
func NewPendingPeerCountFilter() Filter {
195-
return &pendingPeerCountFilter{}
176+
func NewPendingPeerCountFilter(scope string) Filter {
177+
return &pendingPeerCountFilter{scope: scope}
178+
}
179+
180+
func (p *pendingPeerCountFilter) Scope() string {
181+
return p.scope
196182
}
197183

198184
func (p *pendingPeerCountFilter) Type() string {
@@ -214,12 +200,16 @@ func (p *pendingPeerCountFilter) FilterTarget(opt Options, store *core.StoreInfo
214200
return p.filter(opt, store)
215201
}
216202

217-
type snapshotCountFilter struct{}
203+
type snapshotCountFilter struct{ scope string }
218204

219205
// NewSnapshotCountFilter creates a Filter that filters all stores that are
220206
// currently handling too many snapshots.
221-
func NewSnapshotCountFilter() Filter {
222-
return &snapshotCountFilter{}
207+
func NewSnapshotCountFilter(scope string) Filter {
208+
return &snapshotCountFilter{scope: scope}
209+
}
210+
211+
func (f *snapshotCountFilter) Scope() string {
212+
return f.scope
223213
}
224214

225215
func (f *snapshotCountFilter) Type() string {
@@ -241,12 +231,17 @@ func (f *snapshotCountFilter) FilterTarget(opt Options, store *core.StoreInfo) b
241231
}
242232

243233
type cacheFilter struct {
234+
scope string
244235
cache *cache.TTLUint64
245236
}
246237

247238
// NewCacheFilter creates a Filter that filters all stores that are in the cache.
248-
func NewCacheFilter(cache *cache.TTLUint64) Filter {
249-
return &cacheFilter{cache: cache}
239+
func NewCacheFilter(scope string, cache *cache.TTLUint64) Filter {
240+
return &cacheFilter{scope: scope, cache: cache}
241+
}
242+
243+
func (f *cacheFilter) Scope() string {
244+
return f.scope
250245
}
251246

252247
func (f *cacheFilter) Type() string {
@@ -261,12 +256,16 @@ func (f *cacheFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
261256
return false
262257
}
263258

264-
type storageThresholdFilter struct{}
259+
type storageThresholdFilter struct{ scope string }
265260

266261
// NewStorageThresholdFilter creates a Filter that filters all stores that are
267262
// almost full.
268-
func NewStorageThresholdFilter() Filter {
269-
return &storageThresholdFilter{}
263+
func NewStorageThresholdFilter(scope string) Filter {
264+
return &storageThresholdFilter{scope: scope}
265+
}
266+
267+
func (f *storageThresholdFilter) Scope() string {
268+
return f.scope
270269
}
271270

272271
func (f *storageThresholdFilter) Type() string {
@@ -283,14 +282,15 @@ func (f *storageThresholdFilter) FilterTarget(opt Options, store *core.StoreInfo
283282

284283
// distinctScoreFilter ensures that distinct score will not decrease.
285284
type distinctScoreFilter struct {
285+
scope string
286286
labels []string
287287
stores []*core.StoreInfo
288288
safeScore float64
289289
}
290290

291291
// NewDistinctScoreFilter creates a filter that filters all stores that have
292292
// lower distinct score than specified store.
293-
func NewDistinctScoreFilter(labels []string, stores []*core.StoreInfo, source *core.StoreInfo) Filter {
293+
func NewDistinctScoreFilter(scope string, labels []string, stores []*core.StoreInfo, source *core.StoreInfo) Filter {
294294
newStores := make([]*core.StoreInfo, 0, len(stores)-1)
295295
for _, s := range stores {
296296
if s.GetID() == source.GetID() {
@@ -300,12 +300,17 @@ func NewDistinctScoreFilter(labels []string, stores []*core.StoreInfo, source *c
300300
}
301301

302302
return &distinctScoreFilter{
303+
scope: scope,
303304
labels: labels,
304305
stores: newStores,
305306
safeScore: DistinctScore(labels, newStores, source),
306307
}
307308
}
308309

310+
func (f *distinctScoreFilter) Scope() string {
311+
return f.scope
312+
}
313+
309314
func (f *distinctScoreFilter) Type() string {
310315
return "distinct-filter"
311316
}
@@ -319,19 +324,25 @@ func (f *distinctScoreFilter) FilterTarget(opt Options, store *core.StoreInfo) b
319324
}
320325

321326
type namespaceFilter struct {
327+
scope string
322328
classifier namespace.Classifier
323329
namespace string
324330
}
325331

326332
// NewNamespaceFilter creates a Filter that filters all stores that are not
327333
// belong to a namespace.
328-
func NewNamespaceFilter(classifier namespace.Classifier, namespace string) Filter {
334+
func NewNamespaceFilter(scope string, classifier namespace.Classifier, namespace string) Filter {
329335
return &namespaceFilter{
336+
scope: scope,
330337
classifier: classifier,
331338
namespace: namespace,
332339
}
333340
}
334341

342+
func (f *namespaceFilter) Scope() string {
343+
return f.scope
344+
}
345+
335346
func (f *namespaceFilter) Type() string {
336347
return "namespace-filter"
337348
}
@@ -348,35 +359,21 @@ func (f *namespaceFilter) FilterTarget(opt Options, store *core.StoreInfo) bool
348359
return f.filter(store)
349360
}
350361

351-
type rejectLeaderFilter struct{}
352-
353-
// NewRejectLeaderFilter creates a Filter that filters stores that marked as
354-
// rejectLeader from being the target of leader transfer.
355-
func NewRejectLeaderFilter() Filter {
356-
return rejectLeaderFilter{}
357-
}
358-
359-
func (f rejectLeaderFilter) Type() string {
360-
return "reject-leader-filter"
361-
}
362-
363-
func (f rejectLeaderFilter) FilterSource(opt Options, store *core.StoreInfo) bool {
364-
return false
365-
}
366-
367-
func (f rejectLeaderFilter) FilterTarget(opt Options, store *core.StoreInfo) bool {
368-
return opt.CheckLabelProperty(RejectLeader, store.GetLabels())
369-
}
370-
371362
// StoreStateFilter is used to determine whether a store can be selected as the
372363
// source or target of the schedule based on the store's state.
373364
type StoreStateFilter struct {
365+
ActionScope string
374366
// Set true if the schedule involves any transfer leader operation.
375367
TransferLeader bool
376368
// Set true if the schedule involves any move region operation.
377369
MoveRegion bool
378370
}
379371

372+
// Scope returns the scheduler or the checker which the filter acts on.
373+
func (f StoreStateFilter) Scope() string {
374+
return f.ActionScope
375+
}
376+
380377
// Type returns the type of the Filter.
381378
func (f StoreStateFilter) Type() string {
382379
return "store-state-filter"

server/schedule/filters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var _ = Suite(&testFiltersSuite{})
2525
type testFiltersSuite struct{}
2626

2727
func (s *testReplicationSuite) TestPendingPeerFilter(c *C) {
28-
filter := NewPendingPeerCountFilter()
28+
filter := NewPendingPeerCountFilter("")
2929
opt := mockoption.NewScheduleOptions()
3030
tc := mockcluster.NewCluster(opt)
3131
store := core.NewStoreInfo(&metapb.Store{Id: 1})

server/schedule/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
Subsystem: "schedule",
4040
Name: "filter",
4141
Help: "Counter of the filter",
42-
}, []string{"action", "address", "store", "type"})
42+
}, []string{"action", "address", "store", "scope", "type"})
4343

4444
operatorCounter = prometheus.NewCounterVec(
4545
prometheus.CounterOpts{

server/schedule/namespace_checker.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ import (
2121
"go.uber.org/zap"
2222
)
2323

24+
const namespaceCheckerName = "namespace-checker"
25+
2426
// NamespaceChecker ensures region to go to the right place.
2527
type NamespaceChecker struct {
28+
name string
2629
cluster Cluster
2730
filters []Filter
2831
classifier namespace.Classifier
@@ -31,10 +34,11 @@ type NamespaceChecker struct {
3134
// NewNamespaceChecker creates a namespace checker.
3235
func NewNamespaceChecker(cluster Cluster, classifier namespace.Classifier) *NamespaceChecker {
3336
filters := []Filter{
34-
StoreStateFilter{MoveRegion: true},
37+
StoreStateFilter{ActionScope: namespaceCheckerName, MoveRegion: true},
3538
}
3639

3740
return &NamespaceChecker{
41+
name: namespaceCheckerName,
3842
cluster: cluster,
3943
filters: filters,
4044
classifier: classifier,
@@ -102,7 +106,7 @@ func (n *NamespaceChecker) SelectBestPeerToRelocate(region *core.RegionInfo, tar
102106
// SelectBestStoreToRelocate randomly returns the store to relocate
103107
func (n *NamespaceChecker) SelectBestStoreToRelocate(region *core.RegionInfo, targets []*core.StoreInfo) uint64 {
104108
selector := NewRandomSelector(n.filters)
105-
target := selector.SelectTarget(n.cluster, targets, NewExcludedFilter(nil, region.GetStoreIds()))
109+
target := selector.SelectTarget(n.cluster, targets, NewExcludedFilter(n.name, nil, region.GetStoreIds()))
106110
if target == nil {
107111
return 0
108112
}
@@ -120,7 +124,7 @@ func (n *NamespaceChecker) isExists(stores []*core.StoreInfo, storeID uint64) bo
120124

121125
func (n *NamespaceChecker) getNamespaceStores(region *core.RegionInfo) []*core.StoreInfo {
122126
ns := n.classifier.GetRegionNamespace(region)
123-
filteredStores := n.filter(n.cluster.GetStores(), NewNamespaceFilter(n.classifier, ns))
127+
filteredStores := n.filter(n.cluster.GetStores(), NewNamespaceFilter(n.name, n.classifier, ns))
124128

125129
return filteredStores
126130
}

0 commit comments

Comments
 (0)