-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
metadata.go
455 lines (402 loc) · 13.6 KB
/
metadata.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
// Copyright 2014 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL.txt and at www.mariadb.com/bsl11.
//
// Change Date: 2022-10-01
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by the Apache License, Version 2.0,
// included in the file licenses/APL.txt and at
// https://www.apache.org/licenses/LICENSE-2.0
package roachpb
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
)
// NodeID is a custom type for a cockroach node ID. (not a raft node ID)
// 0 is not a valid NodeID.
type NodeID int32
// String implements the fmt.Stringer interface.
// It is used to format the ID for use in Gossip keys.
func (n NodeID) String() string {
return strconv.FormatInt(int64(n), 10)
}
// StoreID is a custom type for a cockroach store ID.
type StoreID int32
// StoreIDSlice implements sort.Interface.
type StoreIDSlice []StoreID
func (s StoreIDSlice) Len() int { return len(s) }
func (s StoreIDSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s StoreIDSlice) Less(i, j int) bool { return s[i] < s[j] }
// String implements the fmt.Stringer interface.
// It is used to format the ID for use in Gossip keys.
func (n StoreID) String() string {
return strconv.FormatInt(int64(n), 10)
}
// A RangeID is a unique ID associated to a Raft consensus group.
type RangeID int64
// String implements the fmt.Stringer interface.
func (r RangeID) String() string {
return strconv.FormatInt(int64(r), 10)
}
// RangeIDSlice implements sort.Interface.
type RangeIDSlice []RangeID
func (r RangeIDSlice) Len() int { return len(r) }
func (r RangeIDSlice) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r RangeIDSlice) Less(i, j int) bool { return r[i] < r[j] }
// ReplicaID is a custom type for a range replica ID.
type ReplicaID int32
// String implements the fmt.Stringer interface.
func (r ReplicaID) String() string {
return strconv.FormatInt(int64(r), 10)
}
// Equals returns whether the Attributes lists are equivalent. Attributes lists
// are treated as sets, meaning that ordering and duplicates are ignored.
func (a Attributes) Equals(b Attributes) bool {
// This is O(n^2), but Attribute lists should never be long enough for that
// to matter, and allocating memory every time this is called would be worse.
if len(a.Attrs) != len(b.Attrs) {
return false
}
for _, aAttr := range a.Attrs {
var found bool
for _, bAttr := range b.Attrs {
if aAttr == bAttr {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// String implements the fmt.Stringer interface.
func (a Attributes) String() string {
return strings.Join(a.Attrs, ",")
}
// RSpan returns the RangeDescriptor's resolved span.
func (r RangeDescriptor) RSpan() RSpan {
return RSpan{Key: r.StartKey, EndKey: r.EndKey}
}
// ContainsKey returns whether this RangeDescriptor contains the specified key.
func (r RangeDescriptor) ContainsKey(key RKey) bool {
return r.RSpan().ContainsKey(key)
}
// ContainsKeyInverted returns whether this RangeDescriptor contains the
// specified key using an inverted range. See RSpan.ContainsKeyInverted.
func (r RangeDescriptor) ContainsKeyInverted(key RKey) bool {
return r.RSpan().ContainsKeyInverted(key)
}
// ContainsKeyRange returns whether this RangeDescriptor contains the specified
// key range from start (inclusive) to end (exclusive).
// If end is empty, returns ContainsKey(start).
func (r RangeDescriptor) ContainsKeyRange(start, end RKey) bool {
return r.RSpan().ContainsKeyRange(start, end)
}
// Replicas returns the set of nodes/stores on which replicas of this range are
// stored.
func (r RangeDescriptor) Replicas() ReplicaDescriptors {
return MakeReplicaDescriptors(r.InternalReplicas)
}
// SetReplicas overwrites the set of nodes/stores on which replicas of this
// range are stored.
func (r *RangeDescriptor) SetReplicas(replicas ReplicaDescriptors) {
r.InternalReplicas = replicas.AsProto()
}
// AddReplica adds the given replica to this range's set.
func (r *RangeDescriptor) AddReplica(toAdd ReplicaDescriptor) {
rs := r.Replicas()
rs.AddReplica(toAdd)
r.SetReplicas(rs)
}
// RemoveReplica removes the given replica from this range's set. If it wasn't
// found to remove, false is returned.
func (r *RangeDescriptor) RemoveReplica(toRemove ReplicaDescriptor) bool {
rs := r.Replicas()
found := rs.RemoveReplica(toRemove)
if found {
r.SetReplicas(rs)
}
return found
}
// GetReplicaDescriptor returns the replica which matches the specified store
// ID.
func (r RangeDescriptor) GetReplicaDescriptor(storeID StoreID) (ReplicaDescriptor, bool) {
for _, repDesc := range r.Replicas().Unwrap() {
if repDesc.StoreID == storeID {
return repDesc, true
}
}
return ReplicaDescriptor{}, false
}
// GetReplicaDescriptorByID returns the replica which matches the specified store
// ID.
func (r RangeDescriptor) GetReplicaDescriptorByID(replicaID ReplicaID) (ReplicaDescriptor, bool) {
for _, repDesc := range r.Replicas().Unwrap() {
if repDesc.ReplicaID == replicaID {
return repDesc, true
}
}
return ReplicaDescriptor{}, false
}
// IsInitialized returns false if this descriptor represents an
// uninitialized range.
// TODO(bdarnell): unify this with Validate().
func (r RangeDescriptor) IsInitialized() bool {
return len(r.EndKey) != 0
}
// GetGeneration returns the generation of this RangeDescriptor.
func (r RangeDescriptor) GetGeneration() int64 {
if r.Generation != nil {
return *r.Generation
}
return 0
}
// IncrementGeneration increments the generation of this RangeDescriptor.
func (r *RangeDescriptor) IncrementGeneration() {
// Create a new *int64 for the new generation. We permit shallow copies of
// RangeDescriptors, so we need to be careful not to mutate the
// potentially-shared generation counter.
r.Generation = proto.Int64(r.GetGeneration() + 1)
}
// Validate performs some basic validation of the contents of a range descriptor.
func (r RangeDescriptor) Validate() error {
if r.NextReplicaID == 0 {
return errors.Errorf("NextReplicaID must be non-zero")
}
seen := map[ReplicaID]struct{}{}
for i, rep := range r.Replicas().Unwrap() {
if err := rep.Validate(); err != nil {
return errors.Errorf("replica %d is invalid: %s", i, err)
}
if _, ok := seen[rep.ReplicaID]; ok {
return errors.Errorf("ReplicaID %d was reused", rep.ReplicaID)
}
seen[rep.ReplicaID] = struct{}{}
if rep.ReplicaID >= r.NextReplicaID {
return errors.Errorf("ReplicaID %d must be less than NextReplicaID %d",
rep.ReplicaID, r.NextReplicaID)
}
}
return nil
}
func (r RangeDescriptor) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "r%d:", r.RangeID)
if !r.IsInitialized() {
buf.WriteString("{-}")
} else {
buf.WriteString(r.RSpan().String())
}
buf.WriteString(" [")
if len(r.Replicas().Unwrap()) > 0 {
for i, rep := range r.Replicas().Unwrap() {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(rep.String())
}
} else {
buf.WriteString("<no replicas>")
}
fmt.Fprintf(&buf, ", next=%d, gen=%d]", r.NextReplicaID, r.GetGeneration())
return buf.String()
}
func (r ReplicationTarget) String() string {
return fmt.Sprintf("n%d,s%d", r.NodeID, r.StoreID)
}
func (r ReplicaDescriptor) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "(n%d,s%d):", r.NodeID, r.StoreID)
if r.ReplicaID == 0 {
buf.WriteString("?")
} else {
fmt.Fprintf(&buf, "%d", r.ReplicaID)
}
if r.Type == ReplicaType_LEARNER {
buf.WriteString("LEARNER")
}
return buf.String()
}
// Validate performs some basic validation of the contents of a replica descriptor.
func (r ReplicaDescriptor) Validate() error {
if r.NodeID == 0 {
return errors.Errorf("NodeID must not be zero")
}
if r.StoreID == 0 {
return errors.Errorf("StoreID must not be zero")
}
if r.ReplicaID == 0 {
return errors.Errorf("ReplicaID must not be zero")
}
return nil
}
// PercentilesFromData derives percentiles from a slice of data points.
// Sorts the input data if it isn't already sorted.
func PercentilesFromData(data []float64) Percentiles {
sort.Float64s(data)
return Percentiles{
P10: percentileFromSortedData(data, 10),
P25: percentileFromSortedData(data, 25),
P50: percentileFromSortedData(data, 50),
P75: percentileFromSortedData(data, 75),
P90: percentileFromSortedData(data, 90),
PMax: percentileFromSortedData(data, 100),
}
}
func percentileFromSortedData(data []float64, percent float64) float64 {
if len(data) == 0 {
return 0
}
if percent < 0 {
percent = 0
}
if percent >= 100 {
return data[len(data)-1]
}
// TODO(a-robinson): Use go's rounding function once we're using 1.10.
idx := int(float64(len(data)) * percent / 100.0)
return data[idx]
}
// String returns a string representation of the Percentiles.
func (p Percentiles) String() string {
return fmt.Sprintf("p10=%.2f p25=%.2f p50=%.2f p75=%.2f p90=%.2f pMax=%.2f",
p.P10, p.P25, p.P50, p.P75, p.P90, p.PMax)
}
// String returns a string representation of the StoreCapacity.
func (sc StoreCapacity) String() string {
return fmt.Sprintf("disk (capacity=%s, available=%s, used=%s, logicalBytes=%s), "+
"ranges=%d, leases=%d, queries=%.2f, writes=%.2f, "+
"bytesPerReplica={%s}, writesPerReplica={%s}",
humanizeutil.IBytes(sc.Capacity), humanizeutil.IBytes(sc.Available),
humanizeutil.IBytes(sc.Used), humanizeutil.IBytes(sc.LogicalBytes),
sc.RangeCount, sc.LeaseCount, sc.QueriesPerSecond, sc.WritesPerSecond,
sc.BytesPerReplica, sc.WritesPerReplica)
}
// FractionUsed computes the fraction of storage capacity that is in use.
func (sc StoreCapacity) FractionUsed() float64 {
if sc.Capacity == 0 {
return 0
}
// Prefer computing the fraction of available disk space used by considering
// anything on the disk that isn't in the store's data directory just a sunk
// cost, not truly part of the disk's capacity. This means that the disk's
// capacity is really just the available space plus cockroach's usage.
//
// Fall back to a more pessimistic calcuation of disk usage if we don't know
// how much space the store's data is taking up.
if sc.Used == 0 {
return float64(sc.Capacity-sc.Available) / float64(sc.Capacity)
}
return float64(sc.Used) / float64(sc.Available+sc.Used)
}
// String returns a string representation of the Tier.
func (t Tier) String() string {
return fmt.Sprintf("%s=%s", t.Key, t.Value)
}
// FromString parses the string representation into the Tier.
func (t *Tier) FromString(tier string) error {
parts := strings.Split(tier, "=")
if len(parts) != 2 || len(parts[0]) == 0 || len(parts[1]) == 0 {
return errors.Errorf("tier must be in the form \"key=value\" not %q", tier)
}
t.Key = parts[0]
t.Value = parts[1]
return nil
}
// String returns a string representation of all the Tiers. This is part
// of pflag's value interface.
func (l Locality) String() string {
tiers := make([]string, len(l.Tiers))
for i, tier := range l.Tiers {
tiers[i] = tier.String()
}
return strings.Join(tiers, ",")
}
// Type returns the underlying type in string form. This is part of pflag's
// value interface.
func (Locality) Type() string {
return "Locality"
}
// Equals returns whether the two Localities are equivalent.
//
// Because Locality Tiers are hierarchically ordered, if two Localities contain
// the same Tiers in different orders, they are not considered equal.
func (l Locality) Equals(r Locality) bool {
if len(l.Tiers) != len(r.Tiers) {
return false
}
for i := range l.Tiers {
if l.Tiers[i] != r.Tiers[i] {
return false
}
}
return true
}
// MaxDiversityScore is the largest possible diversity score, indicating that
// two localities are as different from each other as possible.
const MaxDiversityScore = 1.0
// DiversityScore returns a score comparing the two localities which ranges from
// 1, meaning completely diverse, to 0 which means not diverse at all (that
// their localities match). This function ignores the locality tier key names
// and only considers differences in their values.
//
// All localities are sorted from most global to most local so any localities
// after any differing values are irrelevant.
//
// While we recommend that all nodes have the same locality keys and same
// total number of keys, there's nothing wrong with having different locality
// keys as long as the immediately next keys are all the same for each value.
// For example:
// region:USA -> state:NY -> ...
// region:USA -> state:WA -> ...
// region:EUR -> country:UK -> ...
// region:EUR -> country:France -> ...
// is perfectly fine. This holds true at each level lower as well.
//
// There is also a need to consider the cases where the localities have
// different lengths. For these cases, we treat the missing key on one side as
// different.
func (l Locality) DiversityScore(other Locality) float64 {
length := len(l.Tiers)
if len(other.Tiers) < length {
length = len(other.Tiers)
}
for i := 0; i < length; i++ {
if l.Tiers[i].Value != other.Tiers[i].Value {
return float64(length-i) / float64(length)
}
}
if len(l.Tiers) != len(other.Tiers) {
return MaxDiversityScore / float64(length+1)
}
return 0
}
// Set sets the value of the Locality. It is the important part of
// pflag's value interface.
func (l *Locality) Set(value string) error {
if len(l.Tiers) > 0 {
return errors.New("can't set locality more than once")
}
if len(value) == 0 {
return errors.New("can't have empty locality")
}
tiersStr := strings.Split(value, ",")
tiers := make([]Tier, len(tiersStr))
for i, tier := range tiersStr {
if err := tiers[i].FromString(tier); err != nil {
return err
}
}
l.Tiers = tiers
return nil
}