forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_pool.go
613 lines (549 loc) · 20.3 KB
/
store_pool.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright 2015 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: Bram Gruneir (bram+code@cockroachlabs.com)
package storage
import (
"bytes"
"fmt"
"sort"
"time"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/shuffle"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
)
const (
// TestTimeUntilStoreDead is the test value for TimeUntilStoreDead to
// quickly mark stores as dead.
TestTimeUntilStoreDead = 5 * time.Millisecond
// TestTimeUntilStoreDeadOff is the test value for TimeUntilStoreDead that
// prevents the store pool from marking stores as dead.
TestTimeUntilStoreDeadOff = 24 * time.Hour
)
// declinedReservationsTimeout needs to be non-zero to prevent useless retries
// in the replicateQueue.process() retry loop.
var declinedReservationsTimeout = settings.RegisterPositiveDurationSetting(
"server.declined_reservation_timeout",
"the amount of time to consider the store throttled for up-replication after a reservation was declined",
1*time.Second,
)
var failedReservationsTimeout = settings.RegisterPositiveDurationSetting(
"server.failed_reservation_timeout",
"the amount of time to consider the store throttled for up-replication after a failed reservation call",
5*time.Second,
)
type nodeStatus int
const (
_ nodeStatus = iota
// The node has not heartbeat the node liveness for longer than the
// time until store dead threshold.
nodeStatusDead
// The node liveness either couldn't be consulted or the node is draining.
nodeStatusUnknown
// The node is considered live.
nodeStatusLive
)
// A NodeLivenessFunc accepts a node ID, current time and threshold before
// a node is considered dead and returns whether or not the node is live.
type NodeLivenessFunc func(roachpb.NodeID, time.Time, time.Duration) nodeStatus
// MakeStorePoolNodeLivenessFunc returns a function which determines
// the status of a node based on information provided by the specified
// NodeLiveness.
func MakeStorePoolNodeLivenessFunc(nodeLiveness *NodeLiveness) NodeLivenessFunc {
return func(nodeID roachpb.NodeID, now time.Time, threshold time.Duration) nodeStatus {
liveness, err := nodeLiveness.GetLiveness(nodeID)
if err == nil && !liveness.Draining {
if liveness.isLive(hlc.Timestamp{WallTime: now.UnixNano()}, nodeLiveness.clock.MaxOffset()) {
return nodeStatusLive
}
deadAsOf := liveness.Expiration.GoTime().Add(threshold)
if !now.Before(deadAsOf) {
return nodeStatusDead
}
}
return nodeStatusUnknown
}
}
type storeDetail struct {
desc *roachpb.StoreDescriptor
// throttledUntil is when a throttled store can be considered available again
// due to a failed or declined snapshot.
throttledUntil time.Time
// lastUpdatedTime is set when a store is first consulted and every time
// gossip arrives for a store.
lastUpdatedTime time.Time
deadReplicas map[roachpb.RangeID][]roachpb.ReplicaDescriptor
}
// isThrottled returns whether the store is currently throttled.
func (sd storeDetail) isThrottled(now time.Time) bool {
return sd.throttledUntil.After(now)
}
// storeStatus is the current status of a store.
type storeStatus int
// These are the possible values for a storeStatus.
const (
_ storeStatus = iota
// The store's node is not live or no gossip has been received from
// the store for more than the timeUntilStoreDead threshold.
storeStatusDead
// The store isn't available because it hasn't gossiped yet. This
// status lasts until either gossip is received from the store or
// the timeUntilStoreDead threshold has passed, at which point its
// status will change to dead.
storeStatusUnknown
// The store is alive but it is throttled.
storeStatusThrottled
// The store is alive but a replica for the same rangeID was recently
// discovered to be corrupt.
storeStatusReplicaCorrupted
// The store is alive and available.
storeStatusAvailable
)
// status returns the current status of the store, including whether
// any of the replicas for the specified rangeID are corrupted.
func (sd *storeDetail) status(
now time.Time, threshold time.Duration, rangeID roachpb.RangeID, nl NodeLivenessFunc,
) storeStatus {
// The store is considered dead if it hasn't been updated via gossip
// within the liveness threshold. Note that lastUpdatedTime is set
// when the store detail is created and will have a non-zero value
// even before the first gossip arrives for a store.
deadAsOf := sd.lastUpdatedTime.Add(threshold)
if now.After(deadAsOf) {
return storeStatusDead
}
// If there's no descriptor (meaning no gossip ever arrived for this
// store), return unavailable.
if sd.desc == nil {
return storeStatusUnknown
}
// Even if the store has been updated via gossip, we still rely on
// the node liveness to determine whether it is considered live.
switch nl(sd.desc.Node.NodeID, now, threshold) {
case nodeStatusDead:
return storeStatusDead
case nodeStatusUnknown:
return storeStatusUnknown
}
if sd.isThrottled(now) {
return storeStatusThrottled
}
if len(sd.deadReplicas[rangeID]) > 0 {
return storeStatusReplicaCorrupted
}
return storeStatusAvailable
}
// localityWithString maintains a string representation of each locality along
// with its protocol buffer implementation. This is for the sake of optimizing
// memory usage by allocating a single copy of each that can be returned to
// callers of getNodeLocalityString rather than each caller (which is currently
// each replica in the local store) making its own copy.
type localityWithString struct {
locality roachpb.Locality
str string
}
// StorePool maintains a list of all known stores in the cluster and
// information on their health.
type StorePool struct {
log.AmbientContext
clock *hlc.Clock
gossip *gossip.Gossip
nodeLivenessFn NodeLivenessFunc
timeUntilStoreDead *settings.DurationSetting
startTime time.Time
deterministic bool
// We use separate mutexes for storeDetails and nodeLocalities because the
// nodeLocalities map is used in the critical code path of Replica.Send()
// and we'd rather not block that on something less important accessing
// storeDetails.
detailsMu struct {
syncutil.RWMutex
storeDetails map[roachpb.StoreID]*storeDetail
}
localitiesMu struct {
syncutil.RWMutex
nodeLocalities map[roachpb.NodeID]localityWithString
}
}
// NewStorePool creates a StorePool and registers the store updating callback
// with gossip.
func NewStorePool(
ambient log.AmbientContext,
g *gossip.Gossip,
clock *hlc.Clock,
nodeLivenessFn NodeLivenessFunc,
timeUntilStoreDead *settings.DurationSetting,
deterministic bool,
) *StorePool {
sp := &StorePool{
AmbientContext: ambient,
clock: clock,
gossip: g,
nodeLivenessFn: nodeLivenessFn,
timeUntilStoreDead: timeUntilStoreDead,
startTime: clock.PhysicalTime(),
deterministic: deterministic,
}
sp.detailsMu.storeDetails = make(map[roachpb.StoreID]*storeDetail)
sp.localitiesMu.nodeLocalities = make(map[roachpb.NodeID]localityWithString)
storeRegex := gossip.MakePrefixPattern(gossip.KeyStorePrefix)
g.RegisterCallback(storeRegex, sp.storeGossipUpdate)
deadReplicasRegex := gossip.MakePrefixPattern(gossip.KeyDeadReplicasPrefix)
g.RegisterCallback(deadReplicasRegex, sp.deadReplicasGossipUpdate)
return sp
}
func (sp *StorePool) String() string {
sp.detailsMu.RLock()
defer sp.detailsMu.RUnlock()
ids := make(roachpb.StoreIDSlice, 0, len(sp.detailsMu.storeDetails))
for id := range sp.detailsMu.storeDetails {
ids = append(ids, id)
}
sort.Sort(ids)
var buf bytes.Buffer
now := sp.clock.PhysicalTime()
for _, id := range ids {
detail := sp.detailsMu.storeDetails[id]
fmt.Fprintf(&buf, "%d", id)
status := detail.status(now, sp.timeUntilStoreDead.Get(), 0, sp.nodeLivenessFn)
if status != storeStatusAvailable {
fmt.Fprintf(&buf, " (status=%d)", status)
}
fmt.Fprintf(&buf, ": range-count=%d fraction-used=%.2f",
detail.desc.Capacity.RangeCount, detail.desc.Capacity.FractionUsed())
throttled := detail.throttledUntil.Sub(now)
if throttled > 0 {
fmt.Fprintf(&buf, " [throttled=%.1fs]", throttled.Seconds())
}
_, _ = buf.WriteString("\n")
}
return buf.String()
}
// storeGossipUpdate is the gossip callback used to keep the StorePool up to date.
func (sp *StorePool) storeGossipUpdate(_ string, content roachpb.Value) {
var storeDesc roachpb.StoreDescriptor
if err := content.GetProto(&storeDesc); err != nil {
ctx := sp.AnnotateCtx(context.TODO())
log.Error(ctx, err)
return
}
sp.detailsMu.Lock()
detail := sp.getStoreDetailLocked(storeDesc.StoreID)
detail.desc = &storeDesc
detail.lastUpdatedTime = sp.clock.PhysicalTime()
sp.detailsMu.Unlock()
sp.localitiesMu.Lock()
sp.localitiesMu.nodeLocalities[storeDesc.Node.NodeID] =
localityWithString{storeDesc.Node.Locality, storeDesc.Node.Locality.String()}
sp.localitiesMu.Unlock()
}
// deadReplicasGossipUpdate is the gossip callback used to keep the StorePool up to date.
func (sp *StorePool) deadReplicasGossipUpdate(_ string, content roachpb.Value) {
var replicas roachpb.StoreDeadReplicas
if err := content.GetProto(&replicas); err != nil {
ctx := sp.AnnotateCtx(context.TODO())
log.Error(ctx, err)
return
}
sp.detailsMu.Lock()
defer sp.detailsMu.Unlock()
detail := sp.getStoreDetailLocked(replicas.StoreID)
deadReplicas := make(map[roachpb.RangeID][]roachpb.ReplicaDescriptor)
for _, r := range replicas.Replicas {
deadReplicas[r.RangeID] = append(deadReplicas[r.RangeID], r.Replica)
}
detail.deadReplicas = deadReplicas
}
// newStoreDetail makes a new storeDetail struct. It sets index to be -1 to
// ensure that it will be processed by a queue immediately.
func newStoreDetail() *storeDetail {
return &storeDetail{
deadReplicas: make(map[roachpb.RangeID][]roachpb.ReplicaDescriptor),
}
}
// getStoreDetailLocked returns the store detail for the given storeID.
// The lock must be held *in write mode* even though this looks like a
// read-only method.
func (sp *StorePool) getStoreDetailLocked(storeID roachpb.StoreID) *storeDetail {
detail, ok := sp.detailsMu.storeDetails[storeID]
if !ok {
// We don't have this store yet (this is normal when we're
// starting up and don't have full information from the gossip
// network). The first time this occurs, presume the store is
// alive, but start the clock so it will become dead if enough
// time passes without updates from gossip.
detail = newStoreDetail()
detail.lastUpdatedTime = sp.startTime
sp.detailsMu.storeDetails[storeID] = detail
}
return detail
}
// getStoreDescriptor returns the latest store descriptor for the given
// storeID.
func (sp *StorePool) getStoreDescriptor(storeID roachpb.StoreID) (roachpb.StoreDescriptor, bool) {
sp.detailsMu.RLock()
defer sp.detailsMu.RUnlock()
if detail, ok := sp.detailsMu.storeDetails[storeID]; ok && detail.desc != nil {
return *detail.desc, true
}
return roachpb.StoreDescriptor{}, false
}
// liveAndDeadReplicas divides the provided repls slice into two
// slices: the first for live replicas, and the second for dead
// replicas. Replicas for which liveness or deadness cannot be
// ascertained are excluded from the returned slices.
func (sp *StorePool) liveAndDeadReplicas(
rangeID roachpb.RangeID, repls []roachpb.ReplicaDescriptor,
) (liveReplicas, deadReplicas []roachpb.ReplicaDescriptor) {
sp.detailsMu.Lock()
defer sp.detailsMu.Unlock()
now := sp.clock.PhysicalTime()
for _, repl := range repls {
detail := sp.getStoreDetailLocked(repl.StoreID)
// Mark replica as dead if store is dead.
switch detail.status(now, sp.timeUntilStoreDead.Get(), rangeID, sp.nodeLivenessFn) {
case storeStatusDead:
deadReplicas = append(deadReplicas, repl)
case storeStatusReplicaCorrupted:
// Check whether the replica we're examining has been marked as dead.
var corrupt bool
for _, deadRepl := range detail.deadReplicas[rangeID] {
if deadRepl.ReplicaID == repl.ReplicaID {
corrupt = true
break
}
}
// This replica is the corrupt replica, so consider the store dead.
if corrupt {
deadReplicas = append(deadReplicas, repl)
} else {
// Otherwise, consider the store live.
liveReplicas = append(liveReplicas, repl)
}
case storeStatusAvailable, storeStatusThrottled:
// We count both available and throttled stores to be live for the
// purpose of computing quorum.
liveReplicas = append(liveReplicas, repl)
}
}
return
}
// stat provides a running sample size and running stats.
type stat struct {
n, mean, s float64
}
// Update adds the specified value to the stat, augmenting the running stats.
func (s *stat) update(x float64) {
s.n++
oldMean := s.mean
s.mean += (x - s.mean) / s.n
// Update variable used to calculate running standard deviation. See: Knuth
// TAOCP, vol 2, 3rd ed, page 232.
s.s = s.s + (x-oldMean)*(x-s.mean)
}
// StoreList holds a list of store descriptors and associated count and used
// stats for those stores.
type StoreList struct {
stores []roachpb.StoreDescriptor
// candidateCount tracks range count stats for stores that are eligible to
// be rebalance targets (their used capacity percentage must be lower than
// maxFractionUsedThreshold).
candidateCount stat
// candidateLeases tracks range lease stats for stores that are eligible to
// be rebalance targets.
candidateLeases stat
}
// Generates a new store list based on the passed in descriptors. It will
// maintain the order of those descriptors.
func makeStoreList(descriptors []roachpb.StoreDescriptor) StoreList {
sl := StoreList{stores: descriptors}
for _, desc := range descriptors {
if desc.Capacity.FractionUsed() <= maxFractionUsedThreshold {
sl.candidateCount.update(float64(desc.Capacity.RangeCount))
}
sl.candidateLeases.update(float64(desc.Capacity.LeaseCount))
}
return sl
}
func (sl StoreList) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, " candidate: avg-ranges=%v avg-leases=%v\n",
sl.candidateCount.mean, sl.candidateLeases.mean)
for _, desc := range sl.stores {
fmt.Fprintf(&buf, " %d: ranges=%d leases=%d fraction-used=%.2f\n",
desc.StoreID, desc.Capacity.RangeCount,
desc.Capacity.LeaseCount, desc.Capacity.FractionUsed())
}
return buf.String()
}
// filter takes a store list and filters it using the passed in constraints. It
// maintains the original order of the passed in store list.
func (sl StoreList) filter(constraints config.Constraints) StoreList {
if len(constraints.Constraints) == 0 {
return sl
}
var filteredDescs []roachpb.StoreDescriptor
storeLoop:
for _, store := range sl.stores {
m := map[string]struct{}{}
for _, s := range store.CombinedAttrs().Attrs {
m[s] = struct{}{}
}
for _, c := range constraints.Constraints {
if _, ok := m[c.Value]; !ok {
continue storeLoop
}
}
filteredDescs = append(filteredDescs, store)
}
return makeStoreList(filteredDescs)
}
// GetStores returns a list of known stores in the cluster.
func (sp *StorePool) GetStores() []roachpb.ReplicationTarget {
sl, _, _ := sp.getStoreList(roachpb.RangeID(0))
result := make([]roachpb.ReplicationTarget, len(sl.stores))
for i, sd := range sl.stores {
result[i].StoreID = sd.StoreID
result[i].NodeID = sd.Node.NodeID
}
return result
}
// getStoreList returns a storeList that contains all active stores that
// contain the required attributes and their associated stats. It also returns
// the total number of alive and throttled stores. The passed in rangeID is used
// to check for corrupted replicas.
func (sp *StorePool) getStoreList(rangeID roachpb.RangeID) (StoreList, int, int) {
sp.detailsMu.RLock()
defer sp.detailsMu.RUnlock()
var storeIDs roachpb.StoreIDSlice
for storeID := range sp.detailsMu.storeDetails {
storeIDs = append(storeIDs, storeID)
}
return sp.getStoreListFromIDsRLocked(storeIDs, rangeID)
}
// getStoreListFromIDs is the same function as getStoreList but only returns stores
// from the subset of passed in store IDs.
func (sp *StorePool) getStoreListFromIDs(
storeIDs roachpb.StoreIDSlice, rangeID roachpb.RangeID,
) (StoreList, int, int) {
sp.detailsMu.RLock()
defer sp.detailsMu.RUnlock()
return sp.getStoreListFromIDsRLocked(storeIDs, rangeID)
}
// getStoreListFromIDsRLocked is the same function as getStoreList but requires
// that the detailsMU read lock is held.
func (sp *StorePool) getStoreListFromIDsRLocked(
storeIDs roachpb.StoreIDSlice, rangeID roachpb.RangeID,
) (StoreList, int, int) {
if sp.deterministic {
sort.Sort(storeIDs)
} else {
shuffle.Shuffle(storeIDs)
}
var aliveStoreCount int
var throttledStoreCount int
var storeDescriptors []roachpb.StoreDescriptor
now := sp.clock.PhysicalTime()
for _, storeID := range storeIDs {
detail := sp.detailsMu.storeDetails[storeID]
switch s := detail.status(now, sp.timeUntilStoreDead.Get(), rangeID, sp.nodeLivenessFn); s {
case storeStatusThrottled:
aliveStoreCount++
throttledStoreCount++
case storeStatusReplicaCorrupted:
aliveStoreCount++
case storeStatusAvailable:
aliveStoreCount++
storeDescriptors = append(storeDescriptors, *detail.desc)
case storeStatusDead, storeStatusUnknown:
// Do nothing; this node cannot be used.
default:
panic(fmt.Sprintf("unknown store status: %d", s))
}
}
return makeStoreList(storeDescriptors), aliveStoreCount, throttledStoreCount
}
type throttleReason int
const (
_ throttleReason = iota
throttleDeclined
throttleFailed
)
// throttle informs the store pool that the given remote store declined a
// snapshot or failed to apply one, ensuring that it will not be considered
// for up-replication or rebalancing until after the configured timeout period
// has elapsed. Declined being true indicates that the remote store explicitly
// declined a snapshot.
func (sp *StorePool) throttle(reason throttleReason, storeID roachpb.StoreID) {
sp.detailsMu.Lock()
defer sp.detailsMu.Unlock()
detail := sp.getStoreDetailLocked(storeID)
// If a snapshot is declined, be it due to an error or because it was
// rejected, we mark the store detail as having been declined so it won't
// be considered as a candidate for new replicas until after the configured
// timeout period has passed.
switch reason {
case throttleDeclined:
timeout := declinedReservationsTimeout.Get()
detail.throttledUntil = sp.clock.PhysicalTime().Add(timeout)
if log.V(2) {
ctx := sp.AnnotateCtx(context.TODO())
log.Infof(ctx, "snapshot declined, s%d will be throttled for %s until %s",
storeID, timeout, detail.throttledUntil)
}
case throttleFailed:
timeout := failedReservationsTimeout.Get()
detail.throttledUntil = sp.clock.PhysicalTime().Add(timeout)
if log.V(2) {
ctx := sp.AnnotateCtx(context.TODO())
log.Infof(ctx, "snapshot failed, s%d will be throttled for %s until %s",
storeID, timeout, detail.throttledUntil)
}
}
}
// getLocalities returns the localities for the provided replicas.
// TODO(bram): consider storing a full list of all node to node diversity
// scores for faster lookups.
func (sp *StorePool) getLocalities(
replicas []roachpb.ReplicaDescriptor,
) map[roachpb.NodeID]roachpb.Locality {
sp.localitiesMu.RLock()
defer sp.localitiesMu.RUnlock()
localities := make(map[roachpb.NodeID]roachpb.Locality)
for _, replica := range replicas {
if locality, ok := sp.localitiesMu.nodeLocalities[replica.NodeID]; ok {
localities[replica.NodeID] = locality.locality
} else {
localities[replica.NodeID] = roachpb.Locality{}
}
}
return localities
}
// getNodeLocalityString returns the locality information for the given node
// in its string format.
func (sp *StorePool) getNodeLocalityString(nodeID roachpb.NodeID) string {
sp.localitiesMu.RLock()
defer sp.localitiesMu.RUnlock()
locality, ok := sp.localitiesMu.nodeLocalities[nodeID]
if !ok {
return ""
}
return locality.str
}