forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operator_kind.go
88 lines (78 loc) · 2.48 KB
/
operator_kind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package schedule
import (
"strings"
"github.com/pkg/errors"
)
// OperatorKind is a bit field to identify operator types.
type OperatorKind uint32
// Flags for operators.
const (
OpLeader OperatorKind = 1 << iota // Include leader transfer.
OpRegion // Include peer movement.
OpAdmin // Initiated by admin.
OpHotRegion // Initiated by hot region scheduler.
OpAdjacent // Initiated by adjacent region scheduler.
OpReplica // Initiated by replica checkers.
OpBalance // Initiated by balancers.
OpMerge // Initiated by merge checkers or merge schedulers.
OpRange // Initiated by range scheduler.
opMax
)
var flagToName = map[OperatorKind]string{
OpLeader: "leader",
OpRegion: "region",
OpAdmin: "admin",
OpHotRegion: "hot-region",
OpAdjacent: "adjacent",
OpReplica: "replica",
OpBalance: "balance",
OpMerge: "merge",
OpRange: "range",
}
var nameToFlag = map[string]OperatorKind{
"leader": OpLeader,
"region": OpRegion,
"admin": OpAdmin,
"hot-region": OpHotRegion,
"adjacent": OpAdjacent,
"replica": OpReplica,
"balance": OpBalance,
"merge": OpMerge,
"range": OpRange,
}
func (k OperatorKind) String() string {
var flagNames []string
for flag := OperatorKind(1); flag < opMax; flag <<= 1 {
if k&flag != 0 {
flagNames = append(flagNames, flagToName[flag])
}
}
if len(flagNames) == 0 {
return "unknown"
}
return strings.Join(flagNames, ",")
}
// ParseOperatorKind converts string (flag name list concat by ',') to OperatorKind.
func ParseOperatorKind(str string) (OperatorKind, error) {
var k OperatorKind
for _, flagName := range strings.Split(str, ",") {
flag, ok := nameToFlag[flagName]
if !ok {
return 0, errors.Errorf("unknown flag name: %s", flagName)
}
k |= flag
}
return k, nil
}