/
constraint.go
156 lines (136 loc) · 4.31 KB
/
constraint.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2021 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package placement
import (
"fmt"
"strings"
)
// ConstraintOp defines how a Constraint matches a store.
type ConstraintOp string
const (
// In restricts the store label value should in the value list.
// If label does not exist, `in` is always false.
In ConstraintOp = "in"
// NotIn restricts the store label value should not in the value list.
// If label does not exist, `notIn` is always true.
NotIn ConstraintOp = "notIn"
// Exists restricts the store should have the label.
Exists ConstraintOp = "exists"
// NotExists restricts the store should not have the label.
NotExists ConstraintOp = "notExists"
)
// Constraint is used to filter store when trying to place peer of a region.
type Constraint struct {
Key string `json:"key,omitempty"`
Op ConstraintOp `json:"op,omitempty"`
Values []string `json:"values,omitempty"`
}
// NewConstraint will create a Constraint from a string.
func NewConstraint(label string) (Constraint, error) {
r := Constraint{}
if len(label) < 4 {
return r, fmt.Errorf("%w: %s", ErrInvalidConstraintFormat, label)
}
var op ConstraintOp
switch label[0] {
case '+':
op = In
case '-':
op = NotIn
default:
return r, fmt.Errorf("%w: %s", ErrInvalidConstraintFormat, label)
}
kv := strings.Split(label[1:], "=")
if len(kv) != 2 {
return r, fmt.Errorf("%w: %s", ErrInvalidConstraintFormat, label)
}
key := strings.TrimSpace(kv[0])
if key == "" {
return r, fmt.Errorf("%w: %s", ErrInvalidConstraintFormat, label)
}
val := strings.TrimSpace(kv[1])
if val == "" {
return r, fmt.Errorf("%w: %s", ErrInvalidConstraintFormat, label)
}
if op == In && key == EngineLabelKey && strings.ToLower(val) == EngineLabelTiFlash {
return r, fmt.Errorf("%w: %s", ErrUnsupportedConstraint, label)
}
r.Key = key
r.Op = op
r.Values = strings.Split(val, ",")
return r, nil
}
// NewConstraintDirect will create a Constraint from argument directly.
func NewConstraintDirect(key string, op ConstraintOp, val ...string) Constraint {
return Constraint{
Key: key,
Op: op,
Values: val,
}
}
// Restore converts a Constraint to a string.
func (c *Constraint) Restore() (string, error) {
var sb strings.Builder
if len(c.Values) != 1 {
return "", fmt.Errorf("%w: constraint should have exactly one label value, got %v", ErrInvalidConstraintFormat, c.Values)
}
switch c.Op {
case In:
sb.WriteString("+")
case NotIn:
sb.WriteString("-")
default:
return "", fmt.Errorf("%w: disallowed operation '%s'", ErrInvalidConstraintFormat, c.Op)
}
sb.WriteString(c.Key)
sb.WriteString("=")
sb.WriteString(c.Values[0])
return sb.String(), nil
}
// ConstraintCompatibility is the return type of CompatibleWith.
type ConstraintCompatibility byte
const (
// ConstraintCompatible indicates two constraints are compatible.
ConstraintCompatible ConstraintCompatibility = iota
// ConstraintIncompatible indicates two constraints are incompatible.
ConstraintIncompatible
// ConstraintDuplicated indicates two constraints are duplicated.
ConstraintDuplicated
)
// CompatibleWith will check if two constraints are compatible.
// Return (compatible, duplicated).
func (c *Constraint) CompatibleWith(o *Constraint) ConstraintCompatibility {
sameKey := c.Key == o.Key
if !sameKey {
return ConstraintCompatible
}
sameOp := c.Op == o.Op
sameVal := true
for i := range c.Values {
if i < len(o.Values) && c.Values[i] != o.Values[i] {
sameVal = false
break
}
}
// no following cases:
// 1. duplicated constraint, skip it
// 2. no instance can meet: +dc=sh, -dc=sh
// 3. can not match multiple instances: +dc=sh, +dc=bj
if sameOp && sameVal {
return ConstraintDuplicated
} else if (!sameOp && sameVal) || (sameOp && !sameVal && c.Op == In) {
return ConstraintIncompatible
}
return ConstraintCompatible
}