forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
978 lines (830 loc) · 29.8 KB
/
api.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// 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.
package roachpb
import (
"fmt"
"strconv"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/rlmcpherson/s3gof3r"
)
// UserPriority is a custom type for transaction's user priority.
type UserPriority float64
func (up UserPriority) String() string {
switch up {
case MinUserPriority:
return "LOW"
case UnspecifiedUserPriority, NormalUserPriority:
return "NORMAL"
case MaxUserPriority:
return "HIGH"
default:
return fmt.Sprintf("%g", float64(up))
}
}
const (
// MinUserPriority is the minimum allowed user priority.
MinUserPriority = 0.001
// UnspecifiedUserPriority means NormalUserPriority.
UnspecifiedUserPriority = 0
// NormalUserPriority is set to 1, meaning ops run through the database
// are all given equal weight when a random priority is chosen. This can
// be set specifically via client.NewDBWithPriority().
NormalUserPriority = 1
// MaxUserPriority is the maximum allowed user priority.
MaxUserPriority = 1000
)
// 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] }
const (
isAdmin = 1 << iota // admin cmds don't go through raft, but run on lease holder
isRead // read-only cmds don't go through raft, but may run on lease holder
isWrite // write cmds go through raft and must be proposed on lease holder
isTxn // txn commands may be part of a transaction
isTxnWrite // txn write cmds start heartbeat and are marked for intent resolution
isRange // range commands may span multiple keys
isReverse // reverse commands traverse ranges in descending direction
isAlone // requests which must be alone in a batch
// Requests for acquiring a lease skip the (proposal-time) check that the
// proposing replica has a valid lease.
skipLeaseCheck
consultsTSCache // mutating commands which write data at a timestamp
updatesTSCache // commands which read data at a timestamp
)
// IsReadOnly returns true iff the request is read-only.
func IsReadOnly(args Request) bool {
flags := args.flags()
return (flags&isRead) != 0 && (flags&isWrite) == 0
}
// IsTransactionWrite returns true if the request produces write
// intents when used within a transaction.
func IsTransactionWrite(args Request) bool {
return (args.flags() & isTxnWrite) != 0
}
// IsRange returns true if the command is range-based and must include
// a start and an end key.
func IsRange(args Request) bool {
return (args.flags() & isRange) != 0
}
// ConsultsTimestampCache returns whether the command must consult
// the timestamp cache to determine whether a mutation is safe at
// a proposed timestamp or needs to move to a higher timestamp to
// avoid re-writing history.
func ConsultsTimestampCache(args Request) bool {
return (args.flags() & consultsTSCache) != 0
}
// UpdatesTimestampCache returns whether the command must update
// the timestamp cache in order to set a low water mark for the
// timestamp at which mutations to overlapping key(s) can write
// such that they don't re-write history.
func UpdatesTimestampCache(args Request) bool {
return (args.flags() & updatesTSCache) != 0
}
// Request is an interface for RPC requests.
type Request interface {
proto.Message
// Header returns the request header.
Header() Span
// SetHeader sets the request header.
SetHeader(Span)
// Method returns the request method.
Method() Method
// ShallowCopy returns a shallow copy of the receiver.
ShallowCopy() Request
flags() int
}
// leaseRequestor is implemented by requests dealing with leases.
// Implementors return the previous lease at the time the request
// was proposed.
type leaseRequestor interface {
prevLease() Lease
}
var _ leaseRequestor = &RequestLeaseRequest{}
func (rlr *RequestLeaseRequest) prevLease() Lease {
return rlr.PrevLease
}
var _ leaseRequestor = &TransferLeaseRequest{}
func (tlr *TransferLeaseRequest) prevLease() Lease {
return tlr.PrevLease
}
// Response is an interface for RPC responses.
type Response interface {
proto.Message
// Header returns the response header.
Header() ResponseHeader
// SetHeader sets the response header.
SetHeader(ResponseHeader)
// Verify verifies response integrity, as applicable.
Verify(req Request) error
}
// combinable is implemented by response types whose corresponding
// requests may cross range boundaries, such as Scan or DeleteRange.
// combine() allows responses from individual ranges to be aggregated
// into a single one.
type combinable interface {
combine(combinable) error
}
// combine is used by range-spanning Response types (e.g. Scan or DeleteRange)
// to merge their headers.
func (rh *ResponseHeader) combine(otherRH ResponseHeader) error {
if rh.Txn != nil && otherRH.Txn == nil {
rh.Txn = nil
}
if rh.ResumeSpan != nil {
panic(fmt.Sprintf("combining %+v with %+v", rh.ResumeSpan, otherRH.ResumeSpan))
}
rh.ResumeSpan = otherRH.ResumeSpan
rh.NumKeys += otherRH.NumKeys
rh.RangeInfos = append(rh.RangeInfos, otherRH.RangeInfos...)
return nil
}
// combine implements the combinable interface.
func (sr *ScanResponse) combine(c combinable) error {
otherSR := c.(*ScanResponse)
if sr != nil {
sr.Rows = append(sr.Rows, otherSR.Rows...)
if err := sr.ResponseHeader.combine(otherSR.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &ScanResponse{}
// combine implements the combinable interface.
func (sr *ReverseScanResponse) combine(c combinable) error {
otherSR := c.(*ReverseScanResponse)
if sr != nil {
sr.Rows = append(sr.Rows, otherSR.Rows...)
if err := sr.ResponseHeader.combine(otherSR.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &ReverseScanResponse{}
// combine implements the combinable interface.
func (dr *DeleteRangeResponse) combine(c combinable) error {
otherDR := c.(*DeleteRangeResponse)
if dr != nil {
dr.Keys = append(dr.Keys, otherDR.Keys...)
if err := dr.ResponseHeader.combine(otherDR.Header()); err != nil {
return err
}
}
return nil
}
// combine implements the combinable interface.
func (rr *ResolveIntentRangeResponse) combine(c combinable) error {
otherRR := c.(*ResolveIntentRangeResponse)
if rr != nil {
if err := rr.ResponseHeader.combine(otherRR.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &ResolveIntentRangeResponse{}
// Combine implements the combinable interface.
func (cc *CheckConsistencyResponse) combine(c combinable) error {
if cc != nil {
otherCC := c.(*CheckConsistencyResponse)
if err := cc.ResponseHeader.combine(otherCC.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &CheckConsistencyResponse{}
// Combine implements the combinable interface.
func (er *ExportResponse) combine(c combinable) error {
if er != nil {
otherER := c.(*ExportResponse)
if err := er.ResponseHeader.combine(otherER.Header()); err != nil {
return err
}
er.Files = append(er.Files, otherER.Files...)
}
return nil
}
var _ combinable = &ExportResponse{}
// Combine implements the combinable interface.
func (r *AdminScatterResponse) combine(c combinable) error {
if r != nil {
otherR := c.(*AdminScatterResponse)
if err := r.ResponseHeader.combine(otherR.Header()); err != nil {
return err
}
r.Ranges = append(r.Ranges, otherR.Ranges...)
}
return nil
}
var _ combinable = &AdminScatterResponse{}
// Header implements the Request interface.
func (rh Span) Header() Span {
return rh
}
// SetHeader implements the Request interface.
func (rh *Span) SetHeader(other Span) {
*rh = other
}
func (h *BatchResponse_Header) combine(o BatchResponse_Header) error {
if h.Error != nil || o.Error != nil {
return errors.Errorf(
"can't combine batch responses with errors, have errors %q and %q",
h.Error, o.Error,
)
}
h.Timestamp.Forward(o.Timestamp)
if h.Txn == nil {
h.Txn = o.Txn
} else {
h.Txn.Update(o.Txn)
}
h.Now.Forward(o.Now)
h.CollectedSpans = append(h.CollectedSpans, o.CollectedSpans...)
return nil
}
// Header implements the Request interface.
func (*NoopRequest) Header() Span { panic("NoopRequest has no span") }
// SetHeader implements the Request interface.
func (*NoopRequest) SetHeader(_ Span) { panic("NoopRequest has no span") }
// SetHeader implements the Response interface.
func (rh *ResponseHeader) SetHeader(other ResponseHeader) {
*rh = other
}
// Header implements the Response interface for ResponseHeader.
func (rh ResponseHeader) Header() ResponseHeader {
return rh
}
// Header implements the Response interface.
func (*NoopResponse) Header() ResponseHeader { return ResponseHeader{} }
// SetHeader implements the Response interface.
func (*NoopResponse) SetHeader(_ ResponseHeader) {}
// Verify implements the Response interface for ResopnseHeader with a
// default noop. Individual response types should override this method
// if they contain checksummed data which can be verified.
func (rh *ResponseHeader) Verify(req Request) error {
return nil
}
// Verify verifies the integrity of the get response value.
func (gr *GetResponse) Verify(req Request) error {
if gr.Value != nil {
return gr.Value.Verify(req.Header().Key)
}
return nil
}
// Verify verifies the integrity of every value returned in the scan.
func (sr *ScanResponse) Verify(req Request) error {
for _, kv := range sr.Rows {
if err := kv.Value.Verify(kv.Key); err != nil {
return err
}
}
return nil
}
// Verify verifies the integrity of every value returned in the reverse scan.
func (sr *ReverseScanResponse) Verify(req Request) error {
for _, kv := range sr.Rows {
if err := kv.Value.Verify(kv.Key); err != nil {
return err
}
}
return nil
}
// Verify implements the Response interface.
func (*NoopResponse) Verify(_ Request) error {
return nil
}
// GetInner returns the Request contained in the union.
func (ru RequestUnion) GetInner() Request {
return ru.GetValue().(Request)
}
// GetInner returns the Response contained in the union.
func (ru ResponseUnion) GetInner() Response {
return ru.GetValue().(Response)
}
// MustSetInner sets the Request contained in the union. It panics if the
// request is not recognized by the union type. The RequestUnion is reset
// before being repopulated.
func (ru *RequestUnion) MustSetInner(args Request) {
ru.Reset()
if !ru.SetValue(args) {
panic(fmt.Sprintf("%T excludes %T", ru, args))
}
}
// MustSetInner sets the Response contained in the union. It panics if the
// response is not recognized by the union type. The ResponseUnion is reset
// before being repopulated.
func (ru *ResponseUnion) MustSetInner(reply Response) {
ru.Reset()
if !ru.SetValue(reply) {
panic(fmt.Sprintf("%T excludes %T", ru, reply))
}
}
// Method implements the Request interface.
func (*GetRequest) Method() Method { return Get }
// Method implements the Request interface.
func (*PutRequest) Method() Method { return Put }
// Method implements the Request interface.
func (*ConditionalPutRequest) Method() Method { return ConditionalPut }
// Method implements the Request interface.
func (*InitPutRequest) Method() Method { return InitPut }
// Method implements the Request interface.
func (*IncrementRequest) Method() Method { return Increment }
// Method implements the Request interface.
func (*DeleteRequest) Method() Method { return Delete }
// Method implements the Request interface.
func (*DeleteRangeRequest) Method() Method { return DeleteRange }
// Method implements the Request interface.
func (*ScanRequest) Method() Method { return Scan }
// Method implements the Request interface.
func (*ReverseScanRequest) Method() Method { return ReverseScan }
// Method implements the Request interface.
func (*CheckConsistencyRequest) Method() Method { return CheckConsistency }
// Method implements the Request interface.
func (*BeginTransactionRequest) Method() Method { return BeginTransaction }
// Method implements the Request interface.
func (*EndTransactionRequest) Method() Method { return EndTransaction }
// Method implements the Request interface.
func (*AdminSplitRequest) Method() Method { return AdminSplit }
// Method implements the Request interface.
func (*AdminMergeRequest) Method() Method { return AdminMerge }
// Method implements the Request interface.
func (*AdminTransferLeaseRequest) Method() Method { return AdminTransferLease }
// Method implements the Request interface.
func (*AdminChangeReplicasRequest) Method() Method { return AdminChangeReplicas }
// Method implements the Request interface.
func (*HeartbeatTxnRequest) Method() Method { return HeartbeatTxn }
// Method implements the Request interface.
func (*GCRequest) Method() Method { return GC }
// Method implements the Request interface.
func (*PushTxnRequest) Method() Method { return PushTxn }
// Method implements the Request interface.
func (*QueryTxnRequest) Method() Method { return QueryTxn }
// Method implements the Request interface.
func (*RangeLookupRequest) Method() Method { return RangeLookup }
// Method implements the Request interface.
func (*ResolveIntentRequest) Method() Method { return ResolveIntent }
// Method implements the Request interface.
func (*ResolveIntentRangeRequest) Method() Method { return ResolveIntentRange }
// Method implements the Request interface.
func (*NoopRequest) Method() Method { return Noop }
// Method implements the Request interface.
func (*MergeRequest) Method() Method { return Merge }
// Method implements the Request interface.
func (*TruncateLogRequest) Method() Method { return TruncateLog }
// Method implements the Request interface.
func (*RequestLeaseRequest) Method() Method { return RequestLease }
// Method implements the Request interface.
func (*TransferLeaseRequest) Method() Method { return TransferLease }
// Method implements the Request interface.
func (*LeaseInfoRequest) Method() Method { return LeaseInfo }
// Method implements the Request interface.
func (*ComputeChecksumRequest) Method() Method { return ComputeChecksum }
// Method implements the Request interface.
func (*DeprecatedVerifyChecksumRequest) Method() Method { return DeprecatedVerifyChecksum }
// Method implements the Request interface.
func (*WriteBatchRequest) Method() Method { return WriteBatch }
// Method implements the Request interface.
func (*ExportRequest) Method() Method { return Export }
// Method implements the Request interface.
func (*ImportRequest) Method() Method { return Import }
// Method implements the Request interface.
func (*AdminScatterRequest) Method() Method { return AdminScatter }
// Method implements the Request interface.
func (*AddSSTableRequest) Method() Method { return AddSSTable }
// ShallowCopy implements the Request interface.
func (gr *GetRequest) ShallowCopy() Request {
shallowCopy := *gr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (pr *PutRequest) ShallowCopy() Request {
shallowCopy := *pr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (cpr *ConditionalPutRequest) ShallowCopy() Request {
shallowCopy := *cpr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (pr *InitPutRequest) ShallowCopy() Request {
shallowCopy := *pr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (ir *IncrementRequest) ShallowCopy() Request {
shallowCopy := *ir
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (dr *DeleteRequest) ShallowCopy() Request {
shallowCopy := *dr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (drr *DeleteRangeRequest) ShallowCopy() Request {
shallowCopy := *drr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (sr *ScanRequest) ShallowCopy() Request {
shallowCopy := *sr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (rsr *ReverseScanRequest) ShallowCopy() Request {
shallowCopy := *rsr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (ccr *CheckConsistencyRequest) ShallowCopy() Request {
shallowCopy := *ccr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (btr *BeginTransactionRequest) ShallowCopy() Request {
shallowCopy := *btr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (etr *EndTransactionRequest) ShallowCopy() Request {
shallowCopy := *etr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (asr *AdminSplitRequest) ShallowCopy() Request {
shallowCopy := *asr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (amr *AdminMergeRequest) ShallowCopy() Request {
shallowCopy := *amr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (atlr *AdminTransferLeaseRequest) ShallowCopy() Request {
shallowCopy := *atlr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (acrr *AdminChangeReplicasRequest) ShallowCopy() Request {
shallowCopy := *acrr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (htr *HeartbeatTxnRequest) ShallowCopy() Request {
shallowCopy := *htr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (gcr *GCRequest) ShallowCopy() Request {
shallowCopy := *gcr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (ptr *PushTxnRequest) ShallowCopy() Request {
shallowCopy := *ptr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (qtr *QueryTxnRequest) ShallowCopy() Request {
shallowCopy := *qtr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (rlr *RangeLookupRequest) ShallowCopy() Request {
shallowCopy := *rlr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (rir *ResolveIntentRequest) ShallowCopy() Request {
shallowCopy := *rir
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (rirr *ResolveIntentRangeRequest) ShallowCopy() Request {
shallowCopy := *rirr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (nr *NoopRequest) ShallowCopy() Request {
shallowCopy := *nr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (mr *MergeRequest) ShallowCopy() Request {
shallowCopy := *mr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (tlr *TruncateLogRequest) ShallowCopy() Request {
shallowCopy := *tlr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (rlr *RequestLeaseRequest) ShallowCopy() Request {
shallowCopy := *rlr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (tlr *TransferLeaseRequest) ShallowCopy() Request {
shallowCopy := *tlr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (lt *LeaseInfoRequest) ShallowCopy() Request {
shallowCopy := *lt
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (ccr *ComputeChecksumRequest) ShallowCopy() Request {
shallowCopy := *ccr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (dvcr *DeprecatedVerifyChecksumRequest) ShallowCopy() Request {
shallowCopy := *dvcr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (r *WriteBatchRequest) ShallowCopy() Request {
shallowCopy := *r
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (ekr *ExportRequest) ShallowCopy() Request {
shallowCopy := *ekr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (r *ImportRequest) ShallowCopy() Request {
shallowCopy := *r
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (r *AdminScatterRequest) ShallowCopy() Request {
shallowCopy := *r
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (r *AddSSTableRequest) ShallowCopy() Request {
shallowCopy := *r
return &shallowCopy
}
// NewGet returns a Request initialized to get the value at key.
func NewGet(key Key) Request {
return &GetRequest{
Span: Span{
Key: key,
},
}
}
// NewIncrement returns a Request initialized to increment the value at
// key by increment.
func NewIncrement(key Key, increment int64) Request {
return &IncrementRequest{
Span: Span{
Key: key,
},
Increment: increment,
}
}
// NewPut returns a Request initialized to put the value at key.
func NewPut(key Key, value Value) Request {
value.InitChecksum(key)
return &PutRequest{
Span: Span{
Key: key,
},
Value: value,
}
}
// NewPutInline returns a Request initialized to put the value at key
// using an inline value.
func NewPutInline(key Key, value Value) Request {
value.InitChecksum(key)
return &PutRequest{
Span: Span{
Key: key,
},
Value: value,
Inline: true,
}
}
// NewConditionalPut returns a Request initialized to put value as a byte
// slice at key if the existing value at key equals expValueBytes.
func NewConditionalPut(key Key, value, expValue Value) Request {
value.InitChecksum(key)
var expValuePtr *Value
if expValue.RawBytes != nil {
// Make a copy to avoid forcing expValue itself on to the heap.
expValueTmp := expValue
expValuePtr = &expValueTmp
expValue.InitChecksum(key)
}
return &ConditionalPutRequest{
Span: Span{
Key: key,
},
Value: value,
ExpValue: expValuePtr,
}
}
// NewInitPut returns a Request initialized to put the value at key, as long as
// the key doesn't exist, returning a ConditionFailedError if the key exists and
// the existing value is different from value. If failOnTombstones is set to
// true, tombstones count as mismatched values and will cause a
// ConditionFailedError.
func NewInitPut(key Key, value Value, failOnTombstones bool) Request {
value.InitChecksum(key)
return &InitPutRequest{
Span: Span{
Key: key,
},
Value: value,
FailOnTombstones: failOnTombstones,
}
}
// NewDelete returns a Request initialized to delete the value at key.
func NewDelete(key Key) Request {
return &DeleteRequest{
Span: Span{
Key: key,
},
}
}
// NewDeleteRange returns a Request initialized to delete the values in
// the given key range (excluding the endpoint).
func NewDeleteRange(startKey, endKey Key, returnKeys bool) Request {
return &DeleteRangeRequest{
Span: Span{
Key: startKey,
EndKey: endKey,
},
ReturnKeys: returnKeys,
}
}
// NewScan returns a Request initialized to scan from start to end keys
// with max results.
func NewScan(key, endKey Key) Request {
return &ScanRequest{
Span: Span{
Key: key,
EndKey: endKey,
},
}
}
// NewCheckConsistency returns a Request initialized to scan from start to end keys.
func NewCheckConsistency(key, endKey Key, withDiff bool) Request {
return &CheckConsistencyRequest{
Span: Span{
Key: key,
EndKey: endKey,
},
WithDiff: withDiff,
}
}
// NewReverseScan returns a Request initialized to reverse scan from end to
// start keys with max results.
func NewReverseScan(key, endKey Key) Request {
return &ReverseScanRequest{
Span: Span{
Key: key,
EndKey: endKey,
},
}
}
func (*GetRequest) flags() int { return isRead | isTxn | updatesTSCache }
func (*PutRequest) flags() int { return isWrite | isTxn | isTxnWrite | consultsTSCache }
// ConditionalPut and InitPut effectively read and may not write,
// so must update the timestamp cache.
func (*ConditionalPutRequest) flags() int {
return isRead | isWrite | isTxn | isTxnWrite | updatesTSCache | consultsTSCache
}
func (*InitPutRequest) flags() int {
return isRead | isWrite | isTxn | isTxnWrite | updatesTSCache | consultsTSCache
}
func (*IncrementRequest) flags() int { return isRead | isWrite | isTxn | isTxnWrite | consultsTSCache }
func (*DeleteRequest) flags() int { return isWrite | isTxn | isTxnWrite | consultsTSCache }
func (drr *DeleteRangeRequest) flags() int {
// DeleteRangeRequest has different properties if the "inline" flag is set.
// This flag indicates that the request is deleting inline MVCC values,
// which cannot be deleted transactionally - inline DeleteRange will thus
// fail if executed as part of a transaction. This alternate flag set
// is needed to prevent the command from being automatically wrapped into a
// transaction by TxnCoordSender, which can occur if the command spans
// multiple ranges.
//
// TODO(mrtracy): The behavior of DeleteRangeRequest with "inline" set has
// likely diverged enough that it should be promoted into its own command.
// However, it is complicated to plumb a new command through the system,
// while this special case in flags() fixes all current issues succinctly.
// This workaround does not preclude us from creating a separate
// "DeleteInlineRange" command at a later date.
if drr.Inline {
return isWrite | isRange | isAlone
}
// DeleteRange updates the timestamp cache as it doesn't leave
// intents or tombstones for keys which don't yet exist. By updating
// the write timestamp cache, it forces subsequent writes to get a
// write-too-old error and avoids the phantom delete anomaly.
return isWrite | isTxn | isTxnWrite | isRange | updatesTSCache | consultsTSCache
}
func (*ScanRequest) flags() int { return isRead | isRange | isTxn | updatesTSCache }
func (*ReverseScanRequest) flags() int { return isRead | isRange | isReverse | isTxn | updatesTSCache }
func (*BeginTransactionRequest) flags() int { return isWrite | isTxn | consultsTSCache }
// EndTransaction updates the write timestamp cache to prevent
// replays. Replays for the same transaction key and timestamp will
// have Txn.WriteTooOld=true and must retry on EndTransaction.
func (*EndTransactionRequest) flags() int { return isWrite | isTxn | isAlone | updatesTSCache }
func (*AdminSplitRequest) flags() int { return isAdmin | isAlone }
func (*AdminMergeRequest) flags() int { return isAdmin | isAlone }
func (*AdminTransferLeaseRequest) flags() int { return isAdmin | isAlone }
func (*AdminChangeReplicasRequest) flags() int { return isAdmin | isAlone }
func (*HeartbeatTxnRequest) flags() int { return isWrite | isTxn }
func (*GCRequest) flags() int { return isWrite | isRange }
func (*PushTxnRequest) flags() int { return isWrite | isAlone }
func (*QueryTxnRequest) flags() int { return isRead | isAlone }
func (*RangeLookupRequest) flags() int { return isRead }
func (*ResolveIntentRequest) flags() int { return isWrite }
func (*ResolveIntentRangeRequest) flags() int { return isWrite | isRange }
func (*NoopRequest) flags() int { return isRead } // slightly special
func (*TruncateLogRequest) flags() int { return isWrite }
func (*MergeRequest) flags() int { return isWrite }
func (*RequestLeaseRequest) flags() int {
return isWrite | isAlone | skipLeaseCheck
}
// LeaseInfoRequest is usually executed in an INCONSISTENT batch, which has the
// effect of the `skipLeaseCheck` flag that lease write operations have.
func (*LeaseInfoRequest) flags() int { return isRead | isAlone }
func (*TransferLeaseRequest) flags() int {
// TransferLeaseRequest requires the lease, which is checked in
// `AdminTransferLease()` before the TransferLeaseRequest is created and sent
// for evaluation and in the usual way at application time (i.e.
// replica.processRaftCommand() checks that the lease hasn't changed since the
// command resulting from the evaluation of TransferLeaseRequest was
// proposed).
//
// But we're marking it with skipLeaseCheck because `redirectOnOrAcquireLease`
// can't be used before evaluation as, by the time that call would be made,
// the store has registered that a transfer is in progress and
// `redirectOnOrAcquireLease` would already tentatively redirect to the future
// lease holder.
return isWrite | isAlone | skipLeaseCheck
}
func (*ComputeChecksumRequest) flags() int { return isWrite | isRange }
func (*DeprecatedVerifyChecksumRequest) flags() int { return isWrite }
func (*CheckConsistencyRequest) flags() int { return isAdmin | isRange }
func (*WriteBatchRequest) flags() int { return isWrite | isRange }
func (*ExportRequest) flags() int { return isRead | isRange | updatesTSCache }
func (*ImportRequest) flags() int { return isAdmin | isAlone }
func (*AdminScatterRequest) flags() int { return isAdmin | isAlone | isRange }
func (*AddSSTableRequest) flags() int { return isWrite | isAlone | isRange }
// Keys returns credentials in an s3gof3r.Keys
func (b *ExportStorage_S3) Keys() s3gof3r.Keys {
return s3gof3r.Keys{
AccessKey: b.AccessKey,
SecretKey: b.Secret,
}
}
// InsertRangeInfo inserts ri into a slice of RangeInfo's if a descriptor for
// the same range is not already present. If it is present, it's overwritten;
// the rationale being that ri is newer information than what we had before.
func InsertRangeInfo(ris []RangeInfo, ri RangeInfo) []RangeInfo {
for i := range ris {
if ris[i].Desc.RangeID == ri.Desc.RangeID {
ris[i] = ri
return ris
}
}
return append(ris, ri)
}
// Add combines the values from other, for use on an accumulator BulkOpSummary.
func (b *BulkOpSummary) Add(other BulkOpSummary) {
b.DataSize += other.DataSize
b.Rows += other.Rows
b.IndexEntries += other.IndexEntries
b.SystemRecords += other.SystemRecords
}