-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
allocator.go
372 lines (332 loc) · 12.4 KB
/
allocator.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2014 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: Spencer Kimball ([email protected])
// Author: Kathy Spradlin ([email protected])
// Author: Matt Tracy ([email protected])
package storage
import (
"fmt"
"math"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
const (
// maxFractionUsedThreshold: if the fraction used of a store descriptor
// capacity is greater than this value, it will never be used as a rebalance
// target and it will always be eligible to rebalance replicas to other
// stores.
maxFractionUsedThreshold = 0.95
// priorities for various repair operations.
removeDeadReplicaPriority float64 = 10000
addMissingReplicaPriority float64 = 1000
removeExtraReplicaPriority float64 = 100
)
// AllocatorAction enumerates the various replication adjustments that may be
// recommended by the allocator.
type AllocatorAction int
// These are the possible allocator actions.
const (
_ AllocatorAction = iota
AllocatorNoop
AllocatorRemove
AllocatorAdd
AllocatorRemoveDead
)
var allocatorActionNames = map[AllocatorAction]string{
AllocatorNoop: "noop",
AllocatorRemove: "remove",
AllocatorAdd: "add",
AllocatorRemoveDead: "remove dead",
}
func (a AllocatorAction) String() string {
return allocatorActionNames[a]
}
// allocatorError indicates a retryable error condition which sends replicas
// being processed through the replicate_queue into purgatory so that they
// can be retried quickly as soon as new stores come online, or additional
// space frees up.
type allocatorError struct {
required []config.Constraint
relaxConstraints bool
aliveStoreCount int
}
func (ae *allocatorError) Error() string {
anyAll := "all attributes"
if ae.relaxConstraints {
anyAll = "an attribute"
}
var auxInfo string
// Whenever the likely problem is not having enough nodes up, make the
// message really clear.
if ae.relaxConstraints || len(ae.required) == 0 {
auxInfo = "; likely not enough nodes in cluster"
}
return fmt.Sprintf("0 of %d store%s with %s matching %s%s",
ae.aliveStoreCount, util.Pluralize(int64(ae.aliveStoreCount)),
anyAll, ae.required, auxInfo)
}
func (*allocatorError) purgatoryErrorMarker() {}
var _ purgatoryError = &allocatorError{}
// AllocatorOptions are configurable options which effect the way that the
// replicate queue will handle rebalancing opportunities.
type AllocatorOptions struct {
// AllowRebalance allows this store to attempt to rebalance its own
// replicas to other stores.
AllowRebalance bool
}
// Allocator tries to spread replicas as evenly as possible across the stores
// in the cluster.
type Allocator struct {
storePool *StorePool
options AllocatorOptions
ruleSolver *ruleSolver
}
// MakeAllocator creates a new allocator using the specified StorePool.
func MakeAllocator(storePool *StorePool, options AllocatorOptions) Allocator {
return Allocator{
storePool: storePool,
options: options,
ruleSolver: makeDefaultRuleSolver(storePool),
}
}
// ComputeAction determines the exact operation needed to repair the supplied
// range, as governed by the supplied zone configuration. It returns the
// required action that should be taken and a replica on which the action should
// be performed.
func (a *Allocator) ComputeAction(
zone config.ZoneConfig, desc *roachpb.RangeDescriptor,
) (AllocatorAction, float64) {
if a.storePool == nil {
// Do nothing if storePool is nil for some unittests.
return AllocatorNoop, 0
}
deadReplicas := a.storePool.deadReplicas(desc.RangeID, desc.Replicas)
if len(deadReplicas) > 0 {
// The range has dead replicas, which should be removed immediately.
// Adjust the priority by the number of dead replicas the range has.
quorum := computeQuorum(len(desc.Replicas))
liveReplicas := len(desc.Replicas) - len(deadReplicas)
return AllocatorRemoveDead, removeDeadReplicaPriority + float64(quorum-liveReplicas)
}
// TODO(mrtracy): Handle non-homogeneous and mismatched attribute sets.
need := int(zone.NumReplicas)
have := len(desc.Replicas)
if have < need {
// Range is under-replicated, and should add an additional replica.
// Priority is adjusted by the difference between the current replica
// count and the quorum of the desired replica count.
neededQuorum := computeQuorum(need)
return AllocatorAdd, addMissingReplicaPriority + float64(neededQuorum-have)
}
if have > need {
// Range is over-replicated, and should remove a replica.
// Ranges with an even number of replicas get extra priority because
// they have a more fragile quorum.
return AllocatorRemove, removeExtraReplicaPriority - float64(have%2)
}
// Nothing to do.
return AllocatorNoop, 0
}
// AllocateTarget returns a suitable store for a new allocation with the
// required attributes. Nodes already accommodating existing or dead replicas
// are ruled out as targets.
func (a *Allocator) AllocateTarget(
constraints config.Constraints, existing []roachpb.ReplicaDescriptor, rangeID roachpb.RangeID,
) (*roachpb.StoreDescriptor, error) {
candidates, err := a.ruleSolver.Solve(constraints, existing, rangeID)
if err != nil {
return nil, err
}
if len(candidates) == 0 {
return nil, &allocatorError{
required: constraints.Constraints,
}
}
return &candidates[0].store, nil
}
// RemoveTarget returns a suitable replica to remove from the provided replica
// set. It attempts to consider which of the provided replicas would be the best
// candidate for removal. It also will exclude any replica that belongs to the
// range lease holder's store ID.
func (a Allocator) RemoveTarget(
constraints config.Constraints,
existing []roachpb.ReplicaDescriptor,
leaseStoreID roachpb.StoreID,
) (roachpb.ReplicaDescriptor, error) {
if len(existing) == 0 {
return roachpb.ReplicaDescriptor{}, errors.Errorf("must supply at least one replica to allocator.RemoveTarget()")
}
// TODO(bram): Is this getStoreList call required?
// Use an invalid range ID as we don't care about a corrupt ranges.
sl, _, _ := a.storePool.getStoreList(roachpb.RangeID(0))
found := false
var worst roachpb.ReplicaDescriptor
var worstScore float64
for _, exist := range existing {
if exist.StoreID == leaseStoreID {
continue
}
desc, ok := a.storePool.getStoreDescriptor(exist.StoreID)
if !ok {
continue
}
// If it's not a valid candidate, score will be zero.
candidate, _ := a.ruleSolver.computeCandidate(solveState{
constraints: constraints,
store: desc,
existing: nil,
sl: sl,
tierOrder: canonicalTierOrder(sl),
tiers: storeTierMap(sl),
})
if !found || candidate.score < worstScore {
worstScore = candidate.score
worst = exist
found = true
}
}
if found {
return worst, nil
}
return roachpb.ReplicaDescriptor{}, errors.Errorf("RemoveTarget() could not select an appropriate replica to be remove")
}
// RebalanceTarget returns a suitable store for a rebalance target with
// required attributes. Rebalance targets are selected via the same mechanism
// as AllocateTarget(), except the chosen target must follow some additional
// criteria. Namely, if chosen, it must further the goal of balancing the
// cluster.
//
// The supplied parameters are the required attributes for the range, a list of
// the existing replicas of the range, the store ID of the lease-holder
// replica and the range ID of the replica being allocated.
//
// The existing replicas modulo the lease-holder replica and any store with
// dead replicas are candidates for rebalancing. Note that rebalancing is
// accomplished by first adding a new replica to the range, then removing the
// most undesirable replica.
//
// Simply ignoring a rebalance opportunity in the event that the target chosen
// by AllocateTarget() doesn't fit balancing criteria is perfectly fine, as
// other stores in the cluster will also be doing their probabilistic best to
// rebalance. This helps prevent a stampeding herd targeting an abnormally
// under-utilized store.
func (a Allocator) RebalanceTarget(
constraints config.Constraints,
existing []roachpb.ReplicaDescriptor,
leaseStoreID roachpb.StoreID,
rangeID roachpb.RangeID,
) (*roachpb.StoreDescriptor, error) {
if !a.options.AllowRebalance {
return nil, nil
}
// TODO(bram): getStoreList is called twice, once now, and again in solve,
// fix this.
sl, _, _ := a.storePool.getStoreList(rangeID)
if log.V(3) {
log.Infof(context.TODO(), "rebalance-target (lease-holder=%d):\n%s", leaseStoreID, sl)
}
var shouldRebalance bool
for _, repl := range existing {
if leaseStoreID == repl.StoreID {
continue
}
storeDesc, ok := a.storePool.getStoreDescriptor(repl.StoreID)
if ok && a.shouldRebalance(storeDesc, sl) {
shouldRebalance = true
break
}
}
if !shouldRebalance {
return nil, nil
}
// Get candidate stores.
candidates, err := a.ruleSolver.Solve(constraints, nil, rangeID)
if err != nil {
return nil, err
}
// Find a candidate that is better than one of the existing stores, otherwise
// return nil.
candidatesFound := 0
for _, candidate := range candidates {
store := candidate.store
found := false
for _, repl := range existing {
if repl.StoreID == store.StoreID {
found = true
break
}
}
if !found {
return &store, nil
}
candidatesFound++
if candidatesFound > len(existing) {
break
}
}
return nil, nil
}
// RebalanceThreshold is the minimum ratio of a store's range surplus to the
// mean range count that permits rebalances away from that store.
var RebalanceThreshold = envutil.EnvOrDefaultFloat("COCKROACH_REBALANCE_THRESHOLD", 0.05)
// shouldRebalance returns whether the specified store is a candidate for
// having a replica removed from it given the candidate store list.
func (a Allocator) shouldRebalance(store roachpb.StoreDescriptor, sl StoreList) bool {
// TODO(peter,bram,cuong): The FractionUsed check seems suspicious. When a
// node becomes fuller than maxFractionUsedThreshold we will always select it
// for rebalancing. This is currently utilized by tests.
maxCapacityUsed := store.Capacity.FractionUsed() >= maxFractionUsedThreshold
// Rebalance if we're above the rebalance target, which is
// mean*(1+RebalanceThreshold).
target := int32(math.Ceil(sl.candidateCount.mean * (1 + RebalanceThreshold)))
rangeCountAboveTarget := store.Capacity.RangeCount > target
// Rebalance if the candidate store has a range count above the mean, and
// there exists another store that is underfull: its range count is smaller
// than mean*(1-RebalanceThreshold).
var rebalanceToUnderfullStore bool
if float64(store.Capacity.RangeCount) > sl.candidateCount.mean {
underfullThreshold := int32(math.Floor(sl.candidateCount.mean * (1 - RebalanceThreshold)))
for _, desc := range sl.stores {
if desc.Capacity.RangeCount < underfullThreshold {
rebalanceToUnderfullStore = true
break
}
}
}
// Require that moving a replica from the given store makes its range count
// converge on the mean range count. This only affects clusters with a
// small number of ranges.
rebalanceConvergesOnMean := float64(store.Capacity.RangeCount) > sl.candidateCount.mean+0.5
shouldRebalance :=
(maxCapacityUsed || rangeCountAboveTarget || rebalanceToUnderfullStore) && rebalanceConvergesOnMean
if log.V(2) {
log.Infof(context.TODO(),
"%d: should-rebalance=%t: fraction-used=%.2f range-count=%d "+
"(mean=%.1f, target=%d, fraction-used=%t, above-target=%t, underfull=%t, converges=%t)",
store.StoreID, shouldRebalance, store.Capacity.FractionUsed(),
store.Capacity.RangeCount, sl.candidateCount.mean, target,
maxCapacityUsed, rangeCountAboveTarget, rebalanceToUnderfullStore, rebalanceConvergesOnMean)
}
return shouldRebalance
}
// computeQuorum computes the quorum value for the given number of nodes.
func computeQuorum(nodes int) int {
return (nodes / 2) + 1
}