-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
rule_solver.go
249 lines (220 loc) · 6.92 KB
/
rule_solver.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2016 The Cockroach Authors.
//
// 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.
//
// Author: Tristan Rice ([email protected])
package storage
import (
"math"
"sort"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/pkg/errors"
)
type candidate struct {
store roachpb.StoreDescriptor
score float64
}
// rule is a generic rule that can be used to solve a constraint problem.
// Returning false will remove the store from the list of candidate stores.
// The score will be summed together with the other rule scores to create a
// store ranking (higher is better).
type rule func(
c config.Constraints,
store roachpb.StoreDescriptor,
existing []roachpb.ReplicaDescriptor,
sl StoreList,
) (candidate bool, score float64)
// defaultRules is the default rule set to use.
var defaultRules = []rule{
ruleReplicasUniqueNodes,
ruleConstraints,
ruleCapacity,
ruleDiversity,
}
// makeDefaultRuleSolver returns a ruleSolver with defaultRules.
func makeDefaultRuleSolver(storePool *StorePool) *ruleSolver {
return makeRuleSolver(storePool, defaultRules)
}
// makeRuleSolver makes a new ruleSolver. The order of the rules is the order in
// which they are run. For optimization purposes, less computationally intense
// rules should run first to eliminate candidates.
func makeRuleSolver(storePool *StorePool, rules []rule) *ruleSolver {
return &ruleSolver{
storePool: storePool,
rules: rules,
}
}
// ruleSolver solves a set of rules for a store.
type ruleSolver struct {
storePool *StorePool
rules []rule
}
// solve solves given constraints. See (*ruleSolver).solveInternal.
func (rs *ruleSolver) solve(
c config.Constraints, existing []roachpb.ReplicaDescriptor,
) ([]roachpb.StoreDescriptor, error) {
candidates, err := rs.solveScores(c, existing)
if err != nil {
return nil, err
}
candidateStores := make([]roachpb.StoreDescriptor, len(candidates))
for i, candidate := range candidates {
candidateStores[i] = candidate.store
}
return candidateStores, nil
}
// solveScores solves given constraints and returns the score.
func (rs *ruleSolver) solveScores(
c config.Constraints, existing []roachpb.ReplicaDescriptor,
) ([]candidate, error) {
sl, _, throttledStoreCount := rs.storePool.getStoreList(config.Constraints{}, false)
// When there are throttled stores that do match, we shouldn't send
// the replica to purgatory or even consider relaxing the constraints.
if throttledStoreCount > 0 {
return nil, errors.Errorf("%d matching stores are currently throttled", throttledStoreCount)
}
candidates := make([]candidate, 0, len(sl.stores))
for _, store := range sl.stores {
if candidate, ok := rs.computeCandidate(c, store, existing, sl); ok {
candidates = append(candidates, candidate)
}
}
sort.Sort(byScore(candidates))
return candidates, nil
}
func (rs *ruleSolver) computeCandidate(
constraints config.Constraints,
store roachpb.StoreDescriptor,
existing []roachpb.ReplicaDescriptor,
sl StoreList,
) (candidate, bool) {
var totalScore float64
for _, rule := range rs.rules {
isCandidate, score := rule(constraints, store, existing, sl)
if !isCandidate {
return candidate{}, false
}
if !math.IsNaN(score) {
totalScore += score
}
}
return candidate{store: store, score: totalScore}, true
}
// ruleReplicasUniqueNodes ensures that no two replicas are put on the same
// node.
func ruleReplicasUniqueNodes(
_ config.Constraints,
store roachpb.StoreDescriptor,
existing []roachpb.ReplicaDescriptor,
_ StoreList,
) (candidate bool, score float64) {
for _, r := range existing {
if r.NodeID == store.Node.NodeID {
return false, 0
}
}
return true, 0
}
func storeHasConstraint(store roachpb.StoreDescriptor, c config.Constraint) bool {
var found bool
if c.Key == "" {
for _, attrs := range []roachpb.Attributes{store.Attrs, store.Node.Attrs} {
for _, attr := range attrs.Attrs {
if attr == c.Value {
return true
}
}
}
} else {
for _, tier := range store.Locality.Tiers {
if c.Key == tier.Key && c.Value == tier.Value {
return true
}
}
}
return found
}
// ruleConstraints enforces that required and prohibited constraints are
// followed, and that stores with more positive constraints are ranked higher.
func ruleConstraints(
constraints config.Constraints,
store roachpb.StoreDescriptor,
_ []roachpb.ReplicaDescriptor,
_ StoreList,
) (candidate bool, score float64) {
const weight = 1.0
matched := 0
for _, c := range constraints.Constraints {
hasConstraint := storeHasConstraint(store, c)
switch {
case c.Type == config.Constraint_POSITIVE && hasConstraint:
matched++
case c.Type == config.Constraint_REQUIRED && !hasConstraint:
return false, 0
case c.Type == config.Constraint_PROHIBITED && hasConstraint:
return false, 0
}
}
return true, weight * float64(matched) / float64(len(constraints.Constraints))
}
// ruleDiversity ensures that nodes that have the fewest locality tiers in
// common are given higher priority.
func ruleDiversity(
_ config.Constraints,
store roachpb.StoreDescriptor,
existing []roachpb.ReplicaDescriptor,
sl StoreList,
) (candidate bool, score float64) {
const weight = 0.1
stores := map[roachpb.StoreID]roachpb.StoreDescriptor{}
for _, store := range sl.stores {
stores[store.StoreID] = store
}
var maxScore float64
tiers := store.Locality.Tiers
for i, tier := range tiers {
tierScore := float64(int(1) << uint(len(tiers)-i-1))
for _, existing := range existing {
store := stores[existing.StoreID]
st := store.Locality.Tiers
if len(st) < i || st[i].Key != tier.Key {
panic("TODO(d4l3k): Node locality configurations are not equivalent")
}
if st[i].Value != tier.Value {
score += tierScore
}
maxScore += tierScore
}
}
return true, weight * score / maxScore
}
// ruleCapacity prioritizes placing data on empty nodes when the choice is
// available and prevents data from going onto mostly full nodes.
func ruleCapacity(
_ config.Constraints,
store roachpb.StoreDescriptor,
_ []roachpb.ReplicaDescriptor,
_ StoreList,
) (candidate bool, score float64) {
const weight = 0.01
// Don't overfill stores.
if store.Capacity.FractionUsed() > maxFractionUsedThreshold {
return false, 0
}
return true, weight / float64(store.Capacity.RangeCount+1)
}
type byScore []candidate
func (c byScore) Len() int { return len(c) }
func (c byScore) Less(i, j int) bool { return c[i].score > c[j].score }
func (c byScore) Swap(i, j int) { c[i], c[j] = c[j], c[i] }