forked from cockroachdb/cockroach
/
data.go
735 lines (651 loc) · 21.7 KB
/
data.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package roachpb
import (
"bytes"
"fmt"
"hash"
"hash/crc32"
"math"
"math/rand"
"sort"
"strconv"
"sync"
"time"
"github.com/biogo/store/interval"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/encoding"
"github.com/cockroachdb/cockroach/util/uuid"
"github.com/gogo/protobuf/proto"
)
const (
// MaxPriority is the maximum allowed priority.
MaxPriority = math.MaxInt32
)
var (
// RKeyMin is a minimum key value which sorts before all other keys.
RKeyMin = RKey("")
// KeyMin is a minimum key value which sorts before all other keys.
KeyMin = Key(RKeyMin)
// RKeyMax is a maximum key value which sorts after all other keys.
RKeyMax = RKey{0xff, 0xff}
// KeyMax is a maximum key value which sorts after all other keys.
KeyMax = Key(RKeyMax)
)
// RKey denotes a Key whose local addressing has been accounted for.
type RKey Key
// AsRawKey returns the RKey as a Key. This is to be used only in select
// situations in which an RKey is known to not contain a wrapped locally-
// addressed Key. Whenever the Key which created the RKey is still available,
// it should be used instead.
func (rk RKey) AsRawKey() Key {
return Key(rk)
}
// Less compares two RKeys.
func (rk RKey) Less(otherRK RKey) bool {
return bytes.Compare(rk, otherRK) < 0
}
// Equal checks for byte-wise equality.
func (rk RKey) Equal(other []byte) bool {
return bytes.Equal(rk, other)
}
// Next returns the RKey that sorts immediately after the given one.
func (rk RKey) Next() RKey {
return RKey(BytesNext(rk))
}
// PrefixEnd determines the end key given key as a prefix, that is the
// key that sorts precisely behind all keys starting with prefix: "1"
// is added to the final byte and the carry propagated. The special
// cases of nil and KeyMin always returns KeyMax.
func (rk RKey) PrefixEnd() RKey {
if len(rk) == 0 {
return RKeyMax
}
return RKey(bytesPrefixEnd(rk))
}
func (rk RKey) String() string {
return Key(rk).String()
}
// Key is a custom type for a byte string in proto
// messages which refer to Cockroach keys.
type Key []byte
// MakeKey makes a new key which is the concatenation of the
// given inputs, in order.
func MakeKey(keys ...[]byte) []byte {
byteSlices := make([][]byte, len(keys))
for i, k := range keys {
byteSlices[i] = []byte(k)
}
return bytes.Join(byteSlices, nil)
}
// BytesNext returns the next possible byte by appending an \x00.
func BytesNext(b []byte) []byte {
// TODO(spencer): Do we need to enforce KeyMaxLength here?
return append(append([]byte(nil), b...), 0)
}
func bytesPrefixEnd(b []byte) []byte {
end := append([]byte(nil), b...)
for i := len(end) - 1; i >= 0; i-- {
end[i] = end[i] + 1
if end[i] != 0 {
return end
}
}
// This statement will only be reached if the key is already a
// maximal byte string (i.e. already \xff...).
return b
}
// Next returns the next key in lexicographic sort order.
func (k Key) Next() Key {
return Key(BytesNext(k))
}
// IsPrev is a more efficient version of k.Next().Equal(m).
func (k Key) IsPrev(m Key) bool {
l := len(m) - 1
return l == len(k) && m[l] == 0 && k.Equal(m[:l])
}
// PrefixEnd determines the end key given key as a prefix, that is the
// key that sorts precisely behind all keys starting with prefix: "1"
// is added to the final byte and the carry propagated. The special
// cases of nil and KeyMin always returns KeyMax.
func (k Key) PrefixEnd() Key {
if len(k) == 0 {
return Key(RKeyMax)
}
return Key(bytesPrefixEnd(k))
}
// Equal returns whether two keys are identical.
func (k Key) Equal(l Key) bool {
return bytes.Equal(k, l)
}
// Compare implements the interval.Comparable interface for tree nodes.
func (k Key) Compare(b interval.Comparable) int {
return bytes.Compare(k, b.(Key))
}
// String returns a string-formatted version of the key.
func (k Key) String() string {
return fmt.Sprintf("%q", []byte(k))
}
// Format implements the fmt.Formatter interface.
func (k Key) Format(f fmt.State, verb rune) {
// Note: this implementation doesn't handle the width and precision
// specifiers such as "%20.10s".
fmt.Fprint(f, strconv.Quote(string(k)))
}
// Timestamp constant values.
var (
// MaxTimestamp is the max value allowed for Timestamp.
MaxTimestamp = Timestamp{WallTime: math.MaxInt64, Logical: math.MaxInt32}
// MinTimestamp is the min value allowed for Timestamp.
MinTimestamp = Timestamp{WallTime: 0, Logical: 1}
// ZeroTimestamp is an empty timestamp.
ZeroTimestamp = Timestamp{WallTime: 0, Logical: 0}
)
// Less compares two timestamps.
func (t Timestamp) Less(s Timestamp) bool {
return t.WallTime < s.WallTime || (t.WallTime == s.WallTime && t.Logical < s.Logical)
}
// Equal returns whether two timestamps are the same.
func (t Timestamp) Equal(s Timestamp) bool {
return t.WallTime == s.WallTime && t.Logical == s.Logical
}
func (t Timestamp) String() string {
return fmt.Sprintf("%.09f,%d", float64(t.WallTime)/1E9, t.Logical)
}
// Add returns a timestamp with the WallTime and Logical components increased.
func (t Timestamp) Add(wallTime int64, logical int32) Timestamp {
return Timestamp{
WallTime: t.WallTime + wallTime,
Logical: t.Logical + logical,
}
}
// Next returns the timestamp with the next later timestamp.
func (t *Timestamp) Next() Timestamp {
if t.Logical == math.MaxInt32 {
if t.WallTime == math.MaxInt32 {
panic("cannot take the next value to a max timestamp")
}
return Timestamp{
WallTime: t.WallTime + 1,
}
}
return Timestamp{
WallTime: t.WallTime,
Logical: t.Logical + 1,
}
}
// Prev returns the next earliest timestamp.
func (t *Timestamp) Prev() Timestamp {
if t.Logical > 0 {
return Timestamp{
WallTime: t.WallTime,
Logical: t.Logical - 1,
}
} else if t.WallTime > 0 {
return Timestamp{
WallTime: t.WallTime - 1,
Logical: math.MaxInt32,
}
}
panic("cannot take the previous value to a zero timestamp")
}
// Forward updates the timestamp from the one given, if that moves it
// forwards in time.
func (t *Timestamp) Forward(s Timestamp) {
if t.Less(s) {
*t = s
}
}
// Backward updates the timestamp from the one given, if that moves it
// backwards in time.
func (t *Timestamp) Backward(s Timestamp) {
if s.Less(*t) {
*t = s
}
}
// GoTime converts the timestamp to a time.Time.
func (t Timestamp) GoTime() time.Time {
return time.Unix(0, t.WallTime)
}
// InitChecksum initializes a checksum based on the provided key and
// the contents of the value. If the value contains a byte slice, the
// checksum includes it directly.
func (v *Value) InitChecksum(key []byte) {
if v.Checksum == nil {
v.Checksum = proto.Uint32(v.computeChecksum(key))
}
}
// Verify verifies the value's Checksum matches a newly-computed
// checksum of the value's contents. If the value's Checksum is not
// set the verification is a noop.
func (v Value) Verify(key []byte) error {
if v.Checksum != nil {
if cksum := v.computeChecksum(key); cksum != *v.Checksum {
return fmt.Errorf("invalid checksum (%d) for key %s, value [% x]",
cksum, Key(key), v)
}
}
return nil
}
// MakeValueFromString returns a value with bytes and tag set.
func MakeValueFromString(s string) Value {
v := Value{}
v.SetBytes([]byte(s))
return v
}
// MakeValueFromBytes returns a value with bytes and tag set.
func MakeValueFromBytes(bs []byte) Value {
v := Value{}
v.SetBytes(bs)
return v
}
// MakeValueFromBytesAndTimestamp returns a value with bytes, timestamp and
// tag set.
func MakeValueFromBytesAndTimestamp(bs []byte, t Timestamp) Value {
v := Value{Timestamp: &t}
v.SetBytes(bs)
return v
}
// SetBytes sets the bytes and tag field of the receiver.
func (v *Value) SetBytes(b []byte) {
v.RawBytes = b
v.Tag = ValueType_BYTES
}
// SetFloat encodes the specified float64 value into the bytes field of the
// receiver and sets the tag.
func (v *Value) SetFloat(f float64) {
v.RawBytes = encoding.EncodeUint64(nil, math.Float64bits(f))
v.Tag = ValueType_FLOAT
}
// SetInt encodes the specified int64 value into the bytes field of the
// receiver and sets the tag.
func (v *Value) SetInt(i int64) {
v.RawBytes = encoding.EncodeUint64(nil, uint64(i))
v.Tag = ValueType_INT
}
// SetProto encodes the specified proto message into the bytes field of
// the receiver. If the proto message is an InternalTimeSeriesData,
// the tag will be set to TIMESERIES rather than BYTES.
func (v *Value) SetProto(msg proto.Message) error {
data, err := proto.Marshal(msg)
if err != nil {
return err
}
v.SetBytes(data)
// Special handling for ts data.
if _, ok := msg.(*InternalTimeSeriesData); ok {
v.Tag = ValueType_TIMESERIES
}
return nil
}
// SetTime encodes the specified time value into the bytes field of the
// receiver and sets the tag.
func (v *Value) SetTime(t time.Time) {
v.RawBytes = encoding.EncodeTime(nil, t)
v.Tag = ValueType_TIME
}
// GetBytes returns the bytes field of the receiver. If the tag is not
// BYTES an error will be returned.
func (v Value) GetBytes() ([]byte, error) {
if tag := v.Tag; tag != ValueType_BYTES {
return nil, fmt.Errorf("value type is not %s: %s", ValueType_BYTES, tag)
}
return v.RawBytes, nil
}
// GetFloat decodes a float64 value from the bytes field of the receiver. If
// the bytes field is not 8 bytes in length or the tag is not FLOAT an error
// will be returned.
func (v Value) GetFloat() (float64, error) {
if tag := v.Tag; tag != ValueType_FLOAT {
return 0, fmt.Errorf("value type is not %s: %s", ValueType_FLOAT, tag)
}
if len(v.RawBytes) != 8 {
return 0, fmt.Errorf("float64 value should be exactly 8 bytes: %d", len(v.RawBytes))
}
_, u, err := encoding.DecodeUint64(v.RawBytes)
if err != nil {
return 0, err
}
return math.Float64frombits(u), nil
}
// GetInt decodes an int64 value from the bytes field of the receiver. If the
// bytes field is not 8 bytes in length or the tag is not INT an error will be
// returned.
func (v Value) GetInt() (int64, error) {
if tag := v.Tag; tag != ValueType_INT {
return 0, fmt.Errorf("value type is not %s: %s", ValueType_INT, tag)
}
if len(v.RawBytes) != 8 {
return 0, fmt.Errorf("uint64 value should be exactly 8 bytes: %d", len(v.RawBytes))
}
_, u, err := encoding.DecodeUint64(v.RawBytes)
if err != nil {
return 0, err
}
return int64(u), nil
}
// GetProto unmarshals the bytes field of the receiver into msg. If
// unmarshalling fails or the tag is not BYTES, an error will be
// returned.
func (v Value) GetProto(msg proto.Message) error {
expectedTag := ValueType_BYTES
// Special handling for ts data.
if _, ok := msg.(*InternalTimeSeriesData); ok {
expectedTag = ValueType_TIMESERIES
}
if tag := v.Tag; tag != expectedTag {
return fmt.Errorf("value type is not %s: %s", expectedTag, tag)
}
return proto.Unmarshal(v.RawBytes, msg)
}
// GetTime decodes a time value from the bytes field of the receiver. If the
// tag is not TIME an error will be returned.
func (v Value) GetTime() (time.Time, error) {
if tag := v.Tag; tag != ValueType_TIME {
return time.Time{}, fmt.Errorf("value type is not %s: %s", ValueType_TIME, tag)
}
_, t, err := encoding.DecodeTime(v.RawBytes)
if err != nil {
return t, err
}
return t, nil
}
var crc32Pool = sync.Pool{
New: func() interface{} {
return crc32.NewIEEE()
},
}
// GetTimeseries decodes an InternalTimeSeriesData value from the bytes
// field of the receiver. An error will be returned if the tag is not
// TIMESERIES or if decoding fails.
func (v Value) GetTimeseries() (InternalTimeSeriesData, error) {
ts := InternalTimeSeriesData{}
return ts, v.GetProto(&ts)
}
// computeChecksum computes a checksum based on the provided key and
// the contents of the value. If the value contains a byte slice, the
// checksum includes it directly.
func (v Value) computeChecksum(key []byte) uint32 {
crc := crc32Pool.Get().(hash.Hash32)
if _, err := crc.Write(key); err != nil {
panic(err)
}
if v.RawBytes != nil {
if _, err := crc.Write(v.RawBytes); err != nil {
panic(err)
}
}
sum := crc.Sum32()
crc.Reset()
crc32Pool.Put(crc)
return sum
}
// NewTransaction creates a new transaction. The transaction key is
// composed using the specified baseKey (for locality with data
// affected by the transaction) and a random ID to guarantee
// uniqueness. The specified user-level priority is combined with a
// randomly chosen value to yield a final priority, used to settle
// write conflicts in a way that avoids starvation of long-running
// transactions (see Replica.PushTxn).
func NewTransaction(name string, baseKey Key, userPriority int32,
isolation IsolationType, now Timestamp, maxOffset int64) *Transaction {
// Compute priority by adjusting based on userPriority factor.
priority := MakePriority(userPriority)
// Compute timestamp and max timestamp.
max := now
max.WallTime += maxOffset
return &Transaction{
Name: name,
Key: baseKey,
ID: uuid.NewUUID4(),
Priority: priority,
Isolation: isolation,
Timestamp: now,
OrigTimestamp: now,
MaxTimestamp: max,
Sequence: 1,
}
}
// Clone creates a deep copy of the given transaction.
func (t *Transaction) Clone() *Transaction {
return proto.Clone(t).(*Transaction)
}
// Equal tests two transactions for equality. They are equal if they are
// either simultaneously nil or their IDs match.
func (t *Transaction) Equal(s *Transaction) bool {
if t == nil && s == nil {
return true
}
if (t == nil && s != nil) || (t != nil && s == nil) {
return false
}
return TxnIDEqual(t.ID, s.ID)
}
// TraceID implements tracer.Traceable. For a nontrivial Transaction, it
// returns 't', followed by the transaction ID. Otherwise, the empty string is
// returned.
func (t *Transaction) TraceID() string {
if t == nil || len(t.ID) == 0 {
return ""
}
s := uuid.UUID(t.ID).String()
return "t" + s
}
// TraceName implements tracer.Traceable. It returns TraceID, but using the
// short version of the UUID.
func (t *Transaction) TraceName() string {
if t == nil || len(t.ID) == 0 {
return "(none)"
}
return "t" + t.Short()
}
// IsInitialized returns true if the transaction has been initialized.
func (t *Transaction) IsInitialized() bool {
return len(t.ID) > 0
}
// MakePriority generates a random priority value, biased by the
// specified userPriority. If userPriority=100, the resulting
// priority is 100x more likely to be probabilistically greater
// than a similar invocation with userPriority=1.
func MakePriority(userPriority int32) int32 {
// A currently undocumented feature allows an explicit priority to
// be set by specifying priority < 1. The explicit priority is
// simply -userPriority in this case. This is hacky, but currently
// used for unittesting. Perhaps this should be documented and allowed.
if userPriority < 0 {
return -userPriority
}
if userPriority == 0 {
userPriority = 1
}
// The idea here is to bias selection of a random priority from the
// range [1, 2^31-1) such that if userPriority=100, it's 100x more
// likely to be a higher int32 than if userPriority=1. The formula
// below chooses random values according to the following table:
// userPriority | range
// 1 | all positive int32s
// 10 | top 9/10ths of positive int32s
// 100 | top 99/100ths of positive int32s
// 1000 | top 999/1000ths of positive int32s
// ...etc
return math.MaxInt32 - rand.Int31n(math.MaxInt32/userPriority)
}
// TxnIDEqual returns whether the transaction IDs are equal.
func TxnIDEqual(a, b []byte) bool {
return bytes.Equal(a, b)
}
// Restart reconfigures a transaction for restart. The epoch is
// incremented for an in-place restart. The timestamp of the
// transaction on restart is set to the maximum of the transaction's
// timestamp and the specified timestamp.
func (t *Transaction) Restart(userPriority, upgradePriority int32, timestamp Timestamp) {
t.Epoch++
if t.Timestamp.Less(timestamp) {
t.Timestamp = timestamp
}
// Set original timestamp to current timestamp on restart.
t.OrigTimestamp = t.Timestamp
// Potentially upgrade priority both by creating a new random
// priority using userPriority and considering upgradePriority.
t.UpgradePriority(MakePriority(userPriority))
t.UpgradePriority(upgradePriority)
}
// Update ratchets priority, timestamp and original timestamp values (among
// others) for the transaction. If t.ID is empty, then the transaction is
// copied from o.
func (t *Transaction) Update(o *Transaction) {
if o == nil {
return
}
if len(t.ID) == 0 {
*t = *proto.Clone(o).(*Transaction)
return
}
if len(t.Key) == 0 {
t.Key = o.Key
}
if o.Status != PENDING {
t.Status = o.Status
}
if t.Epoch < o.Epoch {
t.Epoch = o.Epoch
}
t.Timestamp.Forward(o.Timestamp)
t.OrigTimestamp.Forward(o.OrigTimestamp)
t.MaxTimestamp.Forward(o.MaxTimestamp)
if o.LastHeartbeat != nil {
if t.LastHeartbeat == nil {
t.LastHeartbeat = &Timestamp{}
}
t.LastHeartbeat.Forward(*o.LastHeartbeat)
}
// Copy the list of nodes without time uncertainty.
t.CertainNodes = NodeList{Nodes: append(Int32Slice(nil),
o.CertainNodes.Nodes...)}
t.UpgradePriority(o.Priority)
// We can't assert against regression here since it can actually happen
// that we update from a transaction which isn't Writing.
t.Writing = t.Writing || o.Writing
if t.Sequence < o.Sequence {
t.Sequence = o.Sequence
}
}
// UpgradePriority sets transaction priority to the maximum of current
// priority and the specified minPriority.
func (t *Transaction) UpgradePriority(minPriority int32) {
if minPriority > t.Priority {
t.Priority = minPriority
}
}
// String formats transaction into human readable string.
func (t Transaction) String() string {
var buf bytes.Buffer
// Compute priority as a floating point number from 0-100 for readability.
floatPri := 100 * float64(t.Priority) / float64(math.MaxInt32)
if len(t.Name) > 0 {
fmt.Fprintf(&buf, "%q ", t.Name)
}
fmt.Fprintf(&buf, "id=%s key=%s rw=%t pri=%.8f iso=%s stat=%s epo=%d ts=%s orig=%s max=%s",
uuid.UUID(t.ID).Short(), t.Key, t.Writing, floatPri, t.Isolation, t.Status, t.Epoch, t.Timestamp, t.OrigTimestamp, t.MaxTimestamp)
return buf.String()
}
// Short returns the short form of the Transaction's UUID.
func (t Transaction) Short() string {
return uuid.UUID(t.ID).Short()
}
// NewGCMetadata returns a GCMetadata initialized to have a ByteCounts
// slice with ten byte count values set to zero. Now is specified as
// nanoseconds since the Unix epoch.
func NewGCMetadata(nowNanos int64) *GCMetadata {
return &GCMetadata{
LastScanNanos: nowNanos,
OldestIntentNanos: proto.Int64(nowNanos),
}
}
// Add adds the given NodeID to the interface (unless already present)
// and restores ordering.
func (s *NodeList) Add(nodeID NodeID) {
if !s.Contains(nodeID) {
(*s).Nodes = append(s.Nodes, int32(nodeID))
sort.Sort(Int32Slice(s.Nodes))
}
}
// Contains returns true if the underlying slice contains the given NodeID.
func (s NodeList) Contains(nodeID NodeID) bool {
ns := s.Nodes
i := sort.Search(len(ns), func(i int) bool { return NodeID(ns[i]) >= nodeID })
return i < len(ns) && NodeID(ns[i]) == nodeID
}
// Int32Slice implements sort.Interface.
type Int32Slice []int32
func (s Int32Slice) Len() int { return len(s) }
func (s Int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Int32Slice) Less(i, j int) bool { return s[i] < s[j] }
var _ fmt.Stringer = &Lease{}
func (l Lease) String() string {
t := time.Unix(l.Start.WallTime/1E9, 0).UTC()
return fmt.Sprintf("replica %s %s +%.3fs", l.Replica, t, float64(l.Expiration.WallTime-l.Start.WallTime)/1E9)
}
// Covers returns true if the given timestamp is strictly less than the
// Lease expiration, which indicates that the lease holder is authorized
// to carry out operations with that timestamp.
func (l Lease) Covers(timestamp Timestamp) bool {
return timestamp.Less(l.Expiration)
}
// OwnedBy returns whether the given store is the lease owner.
func (l Lease) OwnedBy(storeID StoreID) bool {
return l.Replica.StoreID == storeID
}
// RSpan is a key range with an inclusive start RKey and an exclusive end RKey.
type RSpan struct {
Key, EndKey RKey
}
// ContainsKey returns whether this span contains the specified key.
func (rs RSpan) ContainsKey(key RKey) bool {
return bytes.Compare(key, rs.Key) >= 0 && bytes.Compare(key, rs.EndKey) < 0
}
// ContainsKeyRange returns whether this span contains the specified
// key range from start (inclusive) to end (exclusive).
// If end is empty, returns ContainsKey(start).
func (rs RSpan) ContainsKeyRange(start, end RKey) bool {
if len(end) == 0 {
return rs.ContainsKey(start)
}
if comp := bytes.Compare(end, start); comp < 0 {
return false
} else if comp == 0 {
return rs.ContainsKey(start)
}
return bytes.Compare(start, rs.Key) >= 0 && bytes.Compare(rs.EndKey, end) >= 0
}
// Intersect returns the intersection of the current span and the
// descriptor's range. Returns an error if the span and the
// descriptor's range do not overlap.
func (rs RSpan) Intersect(desc *RangeDescriptor) (RSpan, error) {
if !rs.Key.Less(desc.EndKey) || !desc.StartKey.Less(rs.EndKey) {
return rs, util.Errorf("span and descriptor's range do not overlap")
}
key := rs.Key
if !desc.ContainsKey(key) {
key = desc.StartKey
}
endKey := rs.EndKey
if !desc.ContainsKeyRange(desc.StartKey, endKey) || endKey == nil {
endKey = desc.EndKey
}
return RSpan{key, endKey}, nil
}