-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
api.go
1987 lines (1717 loc) · 65.6 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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, 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.
package kvpb
import (
"context"
"fmt"
"strings"
"unicode/utf8"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
_ "github.com/cockroachdb/cockroach/pkg/kv/kvnemesis/kvnemesisutil" // see RequestHeader
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
"github.com/dustin/go-humanize"
)
//go:generate mockgen -package=kvpbmock -destination=kvpbmock/mocks_generated.go . InternalClient,Internal_RangeFeedClient,Internal_MuxRangeFeedClient
// SupportsBatch determines whether the methods in the provided batch
// are supported by the ReadConsistencyType, returning an error if not.
func (rc ReadConsistencyType) SupportsBatch(ba *BatchRequest) error {
switch rc {
case CONSISTENT:
return nil
case READ_UNCOMMITTED, INCONSISTENT:
for _, ru := range ba.Requests {
m := ru.GetInner().Method()
switch m {
case Get, Scan, ReverseScan, QueryResolvedTimestamp:
default:
return errors.Errorf("method %s not allowed with %s batch", m, rc)
}
}
return nil
}
panic("unreachable")
}
type flag int
const (
isAdmin flag = 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
isLocking // locking cmds acquire locks for their transaction
isIntentWrite // intent write cmds leave intents when they succeed
isRange // range commands may span multiple keys
isReverse // reverse commands traverse ranges in descending direction
isAlone // requests which must be alone in a batch
isPrefix // requests which, in a batch, must not be split from the following request
isUnsplittable // range command that must not be split during sending
skipsLeaseCheck // commands which skip the check that the evaluating replica has a valid lease
appliesTSCache // commands which apply the timestamp cache and closed timestamp
updatesTSCache // commands which update the timestamp cache
updatesTSCacheOnErr // commands which make read data available on errors
needsRefresh // commands which require refreshes to avoid serializable retries
canBackpressure // commands which deserve backpressure when a Range grows too large
canSkipLocked // commands which can evaluate under the SkipLocked wait policy
bypassesReplicaCircuitBreaker // commands which bypass the replica circuit breaker, i.e. opt out of fail-fast
requiresClosedTSOlderThanStorageSnapshot // commands which read a replica's closed timestamp that is older than the state of the storage engine
)
// flagDependencies specifies flag dependencies, asserted by TestFlagCombinations.
var flagDependencies = map[flag][]flag{
isAdmin: {isAlone},
isLocking: {isTxn},
isIntentWrite: {isWrite, isLocking},
appliesTSCache: {isWrite},
skipsLeaseCheck: {isAlone},
}
// flagExclusions specifies flag incompatibilities, asserted by TestFlagCombinations.
var flagExclusions = map[flag][]flag{
skipsLeaseCheck: {isIntentWrite},
}
// IsReadOnly returns true iff the request is read-only. A request is
// read-only if it does not go through raft, meaning that it cannot
// change any replicated state. However, read-only requests may still acquire
// locks, but only with unreplicated durability.
func IsReadOnly(args Request) bool {
flags := args.flags()
return (flags&isRead) != 0 && (flags&isWrite) == 0
}
// IsTransactional returns true if the request may be part of a
// transaction.
func IsTransactional(args Request) bool {
return (args.flags() & isTxn) != 0
}
// IsLocking returns true if the request acquires locks when used within
// a transaction.
func IsLocking(args Request) bool {
return (args.flags() & isLocking) != 0
}
// LockingDurability returns the durability of the locks acquired by the
// request. The function assumes that IsLocking(args).
func LockingDurability(args Request) lock.Durability {
if IsReadOnly(args) {
return lock.Unreplicated
}
return lock.Replicated
}
// IsIntentWrite returns true if the request produces write intents at
// the request's sequence number when used within a transaction.
func IsIntentWrite(args Request) bool {
return (args.flags() & isIntentWrite) != 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
}
// AppliesTimestampCache returns whether the command is a write that applies the
// timestamp cache (and closed timestamp), possibly pushing its write timestamp
// into the future to avoid re-writing history.
func AppliesTimestampCache(args Request) bool {
return (args.flags() & appliesTSCache) != 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
}
// UpdatesTimestampCacheOnError returns whether the command must
// update the timestamp cache even on error, as in some cases the data
// which was read is returned (e.g. ConditionalPut ConditionFailedError).
func UpdatesTimestampCacheOnError(args Request) bool {
return (args.flags() & updatesTSCacheOnErr) != 0
}
// NeedsRefresh returns whether the command must be refreshed in
// order to avoid client-side retries on serializable transactions.
func NeedsRefresh(args Request) bool {
return (args.flags() & needsRefresh) != 0
}
// CanBackpressure returns whether the command can be backpressured
// when waiting for a Range to split after it has grown too large.
func CanBackpressure(args Request) bool {
return (args.flags() & canBackpressure) != 0
}
// CanSkipLocked returns whether the command can evaluate under the
// SkipLocked wait policy.
func CanSkipLocked(args Request) bool {
return (args.flags() & canSkipLocked) != 0
}
// BypassesReplicaCircuitBreaker returns whether the command bypasses
// the per-Replica circuit breakers. These requests will thus hang when
// addressed to an unavailable range (instead of failing fast).
func BypassesReplicaCircuitBreaker(args Request) bool {
return (args.flags() & bypassesReplicaCircuitBreaker) != 0
}
// Request is an interface for RPC requests.
type Request interface {
protoutil.Message
// Header returns the request header.
Header() RequestHeader
// SetHeader sets the request header.
SetHeader(RequestHeader)
// Method returns the request method.
Method() Method
// ShallowCopy returns a shallow copy of the receiver.
ShallowCopy() Request
flags() flag
}
// LockingReadRequest is an interface used to expose the key-level locking
// strength of a read-only request.
type LockingReadRequest interface {
Request
KeyLocking() (lock.Strength, lock.Durability)
}
var _ LockingReadRequest = (*GetRequest)(nil)
// KeyLocking implements the LockingReadRequest interface.
func (gr *GetRequest) KeyLocking() (lock.Strength, lock.Durability) {
return gr.KeyLockingStrength, gr.KeyLockingDurability
}
var _ LockingReadRequest = (*ScanRequest)(nil)
// KeyLocking implements the LockingReadRequest interface.
func (sr *ScanRequest) KeyLocking() (lock.Strength, lock.Durability) {
return sr.KeyLockingStrength, sr.KeyLockingDurability
}
var _ LockingReadRequest = (*ReverseScanRequest)(nil)
// KeyLocking implements the LockingReadRequest interface.
func (rsr *ReverseScanRequest) KeyLocking() (lock.Strength, lock.Durability) {
return rsr.KeyLockingStrength, rsr.KeyLockingDurability
}
// SizedWriteRequest is an interface used to expose the number of bytes a
// request might write.
type SizedWriteRequest interface {
Request
WriteBytes() int64
}
var _ SizedWriteRequest = (*PutRequest)(nil)
// WriteBytes makes PutRequest implement SizedWriteRequest.
func (pr *PutRequest) WriteBytes() int64 {
return int64(len(pr.Key)) + int64(pr.Value.Size())
}
var _ SizedWriteRequest = (*ConditionalPutRequest)(nil)
// WriteBytes makes ConditionalPutRequest implement SizedWriteRequest.
func (cpr *ConditionalPutRequest) WriteBytes() int64 {
return int64(len(cpr.Key)) + int64(cpr.Value.Size())
}
var _ SizedWriteRequest = (*InitPutRequest)(nil)
// WriteBytes makes InitPutRequest implement SizedWriteRequest.
func (pr *InitPutRequest) WriteBytes() int64 {
return int64(len(pr.Key)) + int64(pr.Value.Size())
}
var _ SizedWriteRequest = (*IncrementRequest)(nil)
// WriteBytes makes IncrementRequest implement SizedWriteRequest.
func (ir *IncrementRequest) WriteBytes() int64 {
return int64(len(ir.Key)) + 8 // assume 8 bytes for the int64
}
var _ SizedWriteRequest = (*DeleteRequest)(nil)
// WriteBytes makes DeleteRequest implement SizedWriteRequest.
func (dr *DeleteRequest) WriteBytes() int64 {
return int64(len(dr.Key))
}
var _ SizedWriteRequest = (*AddSSTableRequest)(nil)
// WriteBytes makes AddSSTableRequest implement SizedWriteRequest.
func (r *AddSSTableRequest) WriteBytes() int64 {
return int64(len(r.Data))
}
// Response is an interface for RPC responses.
type Response interface {
protoutil.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(context.Context, combinable, *BatchRequest) error
}
// CombineResponses attempts to combine the two provided responses. If both of
// the responses are combinable, they will be combined. If neither are
// combinable, the function is a no-op and returns a nil error. If one of the
// responses is combinable and the other isn't, the function returns an error.
func CombineResponses(ctx context.Context, left, right Response, ba *BatchRequest) error {
cLeft, lOK := left.(combinable)
cRight, rOK := right.(combinable)
if lOK && rOK {
return cLeft.combine(ctx, cRight, ba)
} else if lOK != rOK {
return errors.Errorf("can not combine %T and %T", left, right)
}
return nil
}
// 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 {
return errors.Errorf("combining %+v with %+v", rh.ResumeSpan, otherRH.ResumeSpan)
}
rh.ResumeSpan = otherRH.ResumeSpan
rh.ResumeReason = otherRH.ResumeReason
rh.NumKeys += otherRH.NumKeys
rh.NumBytes += otherRH.NumBytes
return nil
}
// DeserializeColumnarBatchesFromArrow takes in a slice of serialized (in the
// Apache Arrow format) coldata.Batch'es and deserializes them. This function is
// necessary in order to combine responses for Scans and ReverseScans that used
// COL_BATCH_RESPONSE scan format where some requests were executed locally
// (and, thus, responses are passed as is, not serialized) and other requests
// were executed remotely (thus, responses are serialized). The responses to
// remotely-executed requests will be deserialized and all responses will be
// combined in such a fashion as to maintain the correct ordering between parts
// of a single original request (that was split into multiple at the range
// boundaries).
//
// In order to avoid circular dependencies, the implementation is injected from
// SQL.
var DeserializeColumnarBatchesFromArrow func(
_ context.Context, serializedColBatches [][]byte, _ *BatchRequest,
) ([]coldata.Batch, error)
// combine implements the combinable interface.
func (sr *ScanResponse) combine(ctx context.Context, c combinable, ba *BatchRequest) error {
otherSR := c.(*ScanResponse)
if sr != nil {
sr.Rows = append(sr.Rows, otherSR.Rows...)
sr.IntentRows = append(sr.IntentRows, otherSR.IntentRows...)
if ba.IndexFetchSpec != nil {
// If IndexFetchSpec is set, then we might have used the
// COL_BATCH_RESPONSE scan format. If so, we need to be careful when
// combining responses for locally-executed and remotely-executed
// requests. Namely, if we have such a mix, then we always
// deserialize the responses to the remotely-executed requests.
if len(sr.ColBatches.ColBatches) > 0 && len(otherSR.BatchResponses) > 0 {
otherColBatches, err := DeserializeColumnarBatchesFromArrow(ctx, otherSR.BatchResponses, ba)
if err != nil {
return err
}
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, otherColBatches...)
} else if len(sr.BatchResponses) > 0 && len(otherSR.ColBatches.ColBatches) > 0 {
colBatches, err := DeserializeColumnarBatchesFromArrow(ctx, sr.BatchResponses, ba)
if err != nil {
return err
}
sr.BatchResponses = nil
sr.ColBatches.ColBatches = make([]coldata.Batch, 0, len(sr.BatchResponses)+len(otherSR.ColBatches.ColBatches))
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, colBatches...)
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, otherSR.ColBatches.ColBatches...)
} else {
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, otherSR.ColBatches.ColBatches...)
sr.BatchResponses = append(sr.BatchResponses, otherSR.BatchResponses...)
}
} else {
sr.BatchResponses = append(sr.BatchResponses, otherSR.BatchResponses...)
if buildutil.CrdbTestBuild {
if len(sr.ColBatches.ColBatches) > 0 {
return errors.AssertionFailedf(
"unexpectedly non-empty ColBatches when IndexFetchSpec is not set on BatchRequest",
)
}
}
}
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(ctx context.Context, c combinable, ba *BatchRequest) error {
otherSR := c.(*ReverseScanResponse)
if sr != nil {
sr.Rows = append(sr.Rows, otherSR.Rows...)
sr.IntentRows = append(sr.IntentRows, otherSR.IntentRows...)
if ba.IndexFetchSpec != nil {
// If IndexFetchSpec is set, then we might have used the
// COL_BATCH_RESPONSE scan format. If so, we need to be careful when
// combining responses for locally-executed and remotely-executed
// requests. Namely, if we have such a mix, then we always
// deserialize the responses to the remotely-executed requests.
if len(sr.ColBatches.ColBatches) > 0 && len(otherSR.BatchResponses) > 0 {
otherColBatches, err := DeserializeColumnarBatchesFromArrow(ctx, otherSR.BatchResponses, ba)
if err != nil {
return err
}
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, otherColBatches...)
} else if len(sr.BatchResponses) > 0 && len(otherSR.ColBatches.ColBatches) > 0 {
colBatches, err := DeserializeColumnarBatchesFromArrow(ctx, sr.BatchResponses, ba)
if err != nil {
return err
}
sr.BatchResponses = nil
sr.ColBatches.ColBatches = make([]coldata.Batch, 0, len(sr.BatchResponses)+len(otherSR.ColBatches.ColBatches))
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, colBatches...)
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, otherSR.ColBatches.ColBatches...)
} else {
sr.ColBatches.ColBatches = append(sr.ColBatches.ColBatches, otherSR.ColBatches.ColBatches...)
sr.BatchResponses = append(sr.BatchResponses, otherSR.BatchResponses...)
}
} else {
sr.BatchResponses = append(sr.BatchResponses, otherSR.BatchResponses...)
if buildutil.CrdbTestBuild {
if len(sr.ColBatches.ColBatches) > 0 {
return errors.AssertionFailedf(
"unexpectedly non-empty ColBatches when IndexFetchSpec is not set on BatchRequest",
)
}
}
}
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(_ context.Context, c combinable, _ *BatchRequest) 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
}
var _ combinable = &DeleteRangeResponse{}
// combine implements the combinable interface.
func (dr *RevertRangeResponse) combine(_ context.Context, c combinable, _ *BatchRequest) error {
otherDR := c.(*RevertRangeResponse)
if dr != nil {
if err := dr.ResponseHeader.combine(otherDR.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &RevertRangeResponse{}
// combine implements the combinable interface.
func (rr *ResolveIntentResponse) combine(_ context.Context, c combinable, _ *BatchRequest) error {
otherRR := c.(*ResolveIntentResponse)
if rr != nil {
if err := rr.ResponseHeader.combine(otherRR.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &ResolveIntentResponse{}
// combine implements the combinable interface.
func (rr *ResolveIntentRangeResponse) combine(
_ context.Context, c combinable, _ *BatchRequest,
) 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(
_ context.Context, c combinable, _ *BatchRequest,
) error {
if cc != nil {
otherCC := c.(*CheckConsistencyResponse)
cc.Result = append(cc.Result, otherCC.Result...)
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(_ context.Context, c combinable, _ *BatchRequest) 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(_ context.Context, c combinable, _ *BatchRequest) error {
if r != nil {
otherR := c.(*AdminScatterResponse)
if err := r.ResponseHeader.combine(otherR.Header()); err != nil {
return err
}
r.RangeInfos = append(r.RangeInfos, otherR.RangeInfos...)
r.MVCCStats.Add(otherR.MVCCStats)
r.ReplicasScatteredBytes += otherR.ReplicasScatteredBytes
}
return nil
}
var _ combinable = &AdminScatterResponse{}
func (avptr *AdminVerifyProtectedTimestampResponse) combine(
_ context.Context, c combinable, _ *BatchRequest,
) error {
other := c.(*AdminVerifyProtectedTimestampResponse)
if avptr != nil {
avptr.DeprecatedFailedRanges = append(avptr.DeprecatedFailedRanges,
other.DeprecatedFailedRanges...)
avptr.VerificationFailedRanges = append(avptr.VerificationFailedRanges,
other.VerificationFailedRanges...)
if err := avptr.ResponseHeader.combine(other.Header()); err != nil {
return err
}
}
return nil
}
var _ combinable = &AdminVerifyProtectedTimestampResponse{}
// combine implements the combinable interface.
func (r *QueryResolvedTimestampResponse) combine(
_ context.Context, c combinable, _ *BatchRequest,
) error {
if r != nil {
otherR := c.(*QueryResolvedTimestampResponse)
if err := r.ResponseHeader.combine(otherR.Header()); err != nil {
return err
}
r.ResolvedTS.Backward(otherR.ResolvedTS)
}
return nil
}
var _ combinable = &QueryResolvedTimestampResponse{}
// combine implements the combinable interface.
func (r *BarrierResponse) combine(_ context.Context, c combinable, _ *BatchRequest) error {
otherR := c.(*BarrierResponse)
if r != nil {
if err := r.ResponseHeader.combine(otherR.Header()); err != nil {
return err
}
r.Timestamp.Forward(otherR.Timestamp)
}
return nil
}
var _ combinable = &BarrierResponse{}
// combine implements the combinable interface.
func (r *QueryLocksResponse) combine(_ context.Context, c combinable, _ *BatchRequest) error {
otherR := c.(*QueryLocksResponse)
if r != nil {
if err := r.ResponseHeader.combine(otherR.Header()); err != nil {
return err
}
r.Locks = append(r.Locks, otherR.Locks...)
}
return nil
}
var _ combinable = &QueryLocksResponse{}
// combine implements the combinable interface.
func (r *IsSpanEmptyResponse) combine(_ context.Context, c combinable, _ *BatchRequest) error {
otherR := c.(*IsSpanEmptyResponse)
if r != nil {
if err := r.ResponseHeader.combine(otherR.Header()); err != nil {
return err
}
// Given the request doesn't actually count anything, and instead
// hijacks NumKeys to indicate whether there is any data, there's
// no good reason to have it take on a value greater than 1.
if r.ResponseHeader.NumKeys > 1 {
r.ResponseHeader.NumKeys = 1
}
}
return nil
}
var _ combinable = &IsSpanEmptyResponse{}
// Header implements the Request interface.
func (rh RequestHeader) Header() RequestHeader {
return rh
}
// SetHeader implements the Request interface.
func (rh *RequestHeader) SetHeader(other RequestHeader) {
*rh = other
}
// Span returns the key range that the Request operates over.
func (rh RequestHeader) Span() roachpb.Span {
return roachpb.Span{Key: rh.Key, EndKey: rh.EndKey}
}
// SetSpan addresses the RequestHeader to the specified key span.
func (rh *RequestHeader) SetSpan(s roachpb.Span) {
rh.Key = s.Key
rh.EndKey = s.EndKey
}
// RequestHeaderFromSpan creates a RequestHeader addressed at the specified key
// span.
func RequestHeaderFromSpan(s roachpb.Span) RequestHeader {
return RequestHeader{Key: s.Key, EndKey: s.EndKey}
}
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 txn := o.Txn; txn != nil {
if h.Txn == nil {
h.Txn = txn.Clone()
} else {
h.Txn.Update(txn)
}
}
h.Now.Forward(o.Now)
h.RangeInfos = append(h.RangeInfos, o.RangeInfos...)
h.CollectedSpans = append(h.CollectedSpans, o.CollectedSpans...)
return nil
}
// 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
}
// Verify implements the Response interface for ResponseHeader 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
}
// TruncatedRequestsString formats a slice of RequestUnions for printing,
// limited to maxBytes bytes.
func TruncatedRequestsString(reqs []RequestUnion, maxBytes int) string {
if maxBytes < len("<nil>") {
panic(errors.AssertionFailedf("maxBytes too low: %d", maxBytes))
}
if reqs == nil {
return "<nil>"
}
if len(reqs) == 0 {
return "[]"
}
var b strings.Builder
b.WriteRune('[')
b.WriteString(reqs[0].String())
for i := 1; i < len(reqs); i++ {
if b.Len() > maxBytes {
break
}
b.WriteRune(' ')
b.WriteString(reqs[i].String())
}
b.WriteRune(']')
str := b.String()
if len(str) > maxBytes {
str = str[:maxBytes-len("…]")]
// Check whether we truncated in the middle of a rune.
for len(str) > 1 {
if r, _ := utf8.DecodeLastRuneInString(str); r == utf8.RuneError {
// Shave off another byte and check again.
str = str[:len(str)-1]
} else {
break
}
}
return str + "…]"
}
return str
}
// 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 (*ClearRangeRequest) Method() Method { return ClearRange }
// Method implements the Request interface.
func (*RevertRangeRequest) Method() Method { return RevertRange }
// 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 (*EndTxnRequest) Method() Method { return EndTxn }
// Method implements the Request interface.
func (*AdminSplitRequest) Method() Method { return AdminSplit }
// Method implements the Request interface.
func (*AdminUnsplitRequest) Method() Method { return AdminUnsplit }
// 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 (*AdminRelocateRangeRequest) Method() Method { return AdminRelocateRange }
// 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 (*RecoverTxnRequest) Method() Method { return RecoverTxn }
// Method implements the Request interface.
func (*QueryTxnRequest) Method() Method { return QueryTxn }
// Method implements the Request interface.
func (*QueryIntentRequest) Method() Method { return QueryIntent }
// Method implements the Request interface.
func (*QueryLocksRequest) Method() Method { return QueryLocks }
// 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 (*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 (*ProbeRequest) Method() Method { return Probe }
// 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 (*ExportRequest) Method() Method { return Export }
// Method implements the Request interface.
func (*AdminScatterRequest) Method() Method { return AdminScatter }
// Method implements the Request interface.
func (*AddSSTableRequest) Method() Method { return AddSSTable }
// Method implements the Request interface.
func (*MigrateRequest) Method() Method { return Migrate }
// Method implements the Request interface.
func (*RecomputeStatsRequest) Method() Method { return RecomputeStats }
// Method implements the Request interface.
func (*RefreshRequest) Method() Method { return Refresh }
// Method implements the Request interface.
func (*RefreshRangeRequest) Method() Method { return RefreshRange }
// Method implements the Request interface.
func (*SubsumeRequest) Method() Method { return Subsume }
// Method implements the Request interface.
func (*RangeStatsRequest) Method() Method { return RangeStats }
// Method implements the Request interface.
func (*AdminVerifyProtectedTimestampRequest) Method() Method {
return AdminVerifyProtectedTimestamp
}
// Method implements the Request interface.
func (*QueryResolvedTimestampRequest) Method() Method { return QueryResolvedTimestamp }
// Method implements the Request interface.
func (*BarrierRequest) Method() Method { return Barrier }
// Method implements the Request interface.
func (*IsSpanEmptyRequest) Method() Method { return IsSpanEmpty }
// 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 (crr *ClearRangeRequest) ShallowCopy() Request {
shallowCopy := *crr
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (crr *RevertRangeRequest) ShallowCopy() Request {
shallowCopy := *crr
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 (etr *EndTxnRequest) 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 (aur *AdminUnsplitRequest) ShallowCopy() Request {
shallowCopy := *aur
return &shallowCopy
}
// ShallowCopy implements the Request interface.
func (amr *AdminMergeRequest) ShallowCopy() Request {
shallowCopy := *amr
return &shallowCopy
}
// ShallowCopy implements the Request interface.