-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathtikvrpc.go
More file actions
1570 lines (1440 loc) · 50.9 KB
/
Copy pathtikvrpc.go
File metadata and controls
1570 lines (1440 loc) · 50.9 KB
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 2021 TiKV 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.
// NOTE: The code in this file is based on code from the
// TiDB project, licensed under the Apache License v 2.0
//
// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/tikvrpc/tikvrpc.go
//
// Copyright 2017 PingCAP, Inc.
//
// 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 tikvrpc
import (
"context"
"sync/atomic"
"time"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/kvproto/pkg/debugpb"
"github.com/pingcap/kvproto/pkg/errorpb"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/mpp"
"github.com/pingcap/kvproto/pkg/tikvpb"
"github.com/pkg/errors"
"github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/kv"
"github.com/tikv/client-go/v2/oracle"
)
// CmdType represents the concrete request type in Request or response type in Response.
type CmdType uint16
// CmdType values.
const (
CmdGet CmdType = 1 + iota
CmdScan
CmdPrewrite
CmdCommit
CmdCleanup
CmdBatchGet
CmdBatchRollback
CmdScanLock
CmdResolveLock
CmdGC
CmdDeleteRange
CmdPessimisticLock
CmdPessimisticRollback
CmdTxnHeartBeat
CmdCheckTxnStatus
CmdCheckSecondaryLocks
CmdFlashbackToVersion
CmdPrepareFlashbackToVersion
CmdFlush
CmdBufferBatchGet
CmdRawGet CmdType = 256 + iota
CmdRawBatchGet
CmdRawPut
CmdRawBatchPut
CmdRawDelete
CmdRawBatchDelete
CmdRawDeleteRange
CmdRawScan
CmdRawGetKeyTTL
CmdRawCompareAndSwap
CmdRawChecksum
CmdUnsafeDestroyRange
CmdRegisterLockObserver
CmdCheckLockObserver
CmdRemoveLockObserver
CmdPhysicalScanLock
CmdStoreSafeTS
CmdLockWaitInfo
CmdGetHealthFeedback
CmdBroadcastTxnStatus
CmdCop CmdType = 512 + iota
CmdCopStream
CmdBatchCop
CmdMPPTask // TODO: These non TiKV RPCs should be moved out of TiKV client
CmdMPPConn // TODO: These non TiKV RPCs should be moved out of TiKV client
CmdMPPCancel // TODO: These non TiKV RPCs should be moved out of TiKV client
CmdMPPAlive // TODO: These non TiKV RPCs should be moved out of TiKV client
CmdMvccGetByKey CmdType = 1024 + iota
CmdMvccGetByStartTs
CmdSplitRegion
CmdDebugGetRegionProperties CmdType = 2048 + iota
CmdCompact // TODO: These non TiKV RPCs should be moved out of TiKV client
CmdGetTiFlashSystemTable // TODO: These non TiKV RPCs should be moved out of TiKV client
CmdEmpty CmdType = 3072 + iota
)
// CmdType aliases.
const (
CmdGetKeyTTL = CmdRawGetKeyTTL
)
func (t CmdType) String() string {
switch t {
case CmdGet:
return "Get"
case CmdScan:
return "Scan"
case CmdPrewrite:
return "Prewrite"
case CmdPessimisticLock:
return "PessimisticLock"
case CmdPessimisticRollback:
return "PessimisticRollback"
case CmdCommit:
return "Commit"
case CmdCleanup:
return "Cleanup"
case CmdBatchGet:
return "BatchGet"
case CmdBatchRollback:
return "BatchRollback"
case CmdScanLock:
return "ScanLock"
case CmdResolveLock:
return "ResolveLock"
case CmdGC:
return "GC"
case CmdDeleteRange:
return "DeleteRange"
case CmdRawGet:
return "RawGet"
case CmdRawBatchGet:
return "RawBatchGet"
case CmdRawPut:
return "RawPut"
case CmdRawBatchPut:
return "RawBatchPut"
case CmdRawDelete:
return "RawDelete"
case CmdRawBatchDelete:
return "RawBatchDelete"
case CmdRawDeleteRange:
return "RawDeleteRange"
case CmdRawScan:
return "RawScan"
case CmdRawChecksum:
return "RawChecksum"
case CmdRawGetKeyTTL:
return "RawGetKeyTTL"
case CmdRawCompareAndSwap:
return "RawCompareAndSwap"
case CmdUnsafeDestroyRange:
return "UnsafeDestroyRange"
case CmdRegisterLockObserver:
return "RegisterLockObserver"
case CmdCheckLockObserver:
return "CheckLockObserver"
case CmdRemoveLockObserver:
return "RemoveLockObserver"
case CmdPhysicalScanLock:
return "PhysicalScanLock"
case CmdCop:
return "Cop"
case CmdCopStream:
return "CopStream"
case CmdBatchCop:
return "BatchCop"
case CmdMPPTask:
return "DispatchMPPTask"
case CmdMPPConn:
return "EstablishMPPConnection"
case CmdMPPCancel:
return "CancelMPPTask"
case CmdMPPAlive:
return "MPPAlive"
case CmdMvccGetByKey:
return "MvccGetByKey"
case CmdMvccGetByStartTs:
return "MvccGetByStartTS"
case CmdSplitRegion:
return "SplitRegion"
case CmdCheckTxnStatus:
return "CheckTxnStatus"
case CmdCheckSecondaryLocks:
return "CheckSecondaryLocks"
case CmdDebugGetRegionProperties:
return "DebugGetRegionProperties"
case CmdCompact:
return "Compact"
case CmdTxnHeartBeat:
return "TxnHeartBeat"
case CmdStoreSafeTS:
return "StoreSafeTS"
case CmdLockWaitInfo:
return "LockWaitInfo"
case CmdGetHealthFeedback:
return "GetHealthFeedback"
case CmdBroadcastTxnStatus:
return "BroadcastTxnStatus"
case CmdFlashbackToVersion:
return "FlashbackToVersion"
case CmdPrepareFlashbackToVersion:
return "PrepareFlashbackToVersion"
case CmdGetTiFlashSystemTable:
return "GetTiFlashSystemTable"
case CmdFlush:
return "Flush"
case CmdBufferBatchGet:
return "BufferBatchGet"
}
return "Unknown"
}
// Request wraps all kv/coprocessor requests.
type Request struct {
Type CmdType
// Req is one of the request type defined in kvrpcpb.
//
// WARN: It may be read concurrently in batch-send-loop, so you should ONLY modify it via `AttachContext`,
// otherwise there could be a risk of data race.
Req interface{}
kvrpcpb.Context
ReadReplicaScope string
// remove txnScope after tidb removed txnScope
TxnScope string
ReplicaReadType kv.ReplicaReadType // different from `kvrpcpb.Context.ReplicaRead`
ReplicaReadSeed *uint32 // pointer to follower read seed in snapshot/coprocessor
StoreTp EndpointType
// ForwardedHost is the address of a store which will handle the request. It's different from
// the address the request sent to.
// If it's not empty, the store which receive the request will forward it to
// the forwarded host. It's useful when network partition occurs.
ForwardedHost string
// ReplicaNumber is the number of current replicas, which is used to calculate the RU cost.
ReplicaNumber int64
// The initial read type, note this will be assigned in the first try, no need to set it outside the client.
ReadType string
// InputRequestSource is the input source of the request, if it's not empty, the final RequestSource sent to store will be attached with the retry info.
InputRequestSource string
// AccessLocationAttr indicates the request is sent to a different zone.
AccessLocation kv.AccessLocationType
// PredictedReadBytes is an optional caller-supplied estimate of this
// request's read bytes. The resource-control layer passes it to PD's
// controller, which decides whether the request type is eligible for
// paging pre-charge; non-cop hints may be ignored.
PredictedReadBytes uint64
// rev represents the revision of the request, it's increased when `Req.Context` gets patched.
rev uint32
}
var defaultRequestOrigin atomic.Int32
// SetDefaultRequestOrigin sets the process-wide request origin used when an RPC
// context does not carry an explicit origin.
func SetDefaultRequestOrigin(origin kvrpcpb.RequestOrigin) {
defaultRequestOrigin.Store(int32(origin))
}
// GetDefaultRequestOrigin returns the process-wide default request origin.
func GetDefaultRequestOrigin() kvrpcpb.RequestOrigin {
return kvrpcpb.RequestOrigin(defaultRequestOrigin.Load())
}
func fillDefaultRequestOrigin(ctx *kvrpcpb.Context) {
if ctx.GetRequestOrigin() == kvrpcpb.RequestOrigin_RequestOriginUnknown {
ctx.RequestOrigin = GetDefaultRequestOrigin()
}
}
// NewRequest returns new kv rpc request.
func NewRequest(typ CmdType, pointer interface{}, ctxs ...kvrpcpb.Context) *Request {
if len(ctxs) > 0 {
ctx := ctxs[0]
fillDefaultRequestOrigin(&ctx)
return &Request{
Type: typ,
Req: pointer,
Context: ctx,
}
}
return &Request{
Type: typ,
Req: pointer,
}
}
// NewReplicaReadRequest returns new kv rpc request with replica read.
func NewReplicaReadRequest(typ CmdType, pointer interface{}, replicaReadType kv.ReplicaReadType, replicaReadSeed *uint32, ctxs ...kvrpcpb.Context) *Request {
req := NewRequest(typ, pointer, ctxs...)
req.ReplicaRead = replicaReadType.IsFollowerRead()
req.ReplicaReadType = replicaReadType
req.ReplicaReadSeed = replicaReadSeed
return req
}
func (req *Request) SetReplicaReadType(replicaReadType kv.ReplicaReadType) {
if req == nil {
return
}
req.ReplicaRead = replicaReadType.IsFollowerRead()
req.ReplicaReadType = replicaReadType
}
// GetReplicaReadSeed returns ReplicaReadSeed pointer.
func (req *Request) GetReplicaReadSeed() *uint32 {
if req != nil {
return req.ReplicaReadSeed
}
return nil
}
// EnableStaleWithMixedReplicaRead enables stale read and set replica read type to mixed.
func (req *Request) EnableStaleWithMixedReplicaRead() {
req.StaleRead = true
req.ReplicaReadType = kv.ReplicaReadMixed
req.ReplicaRead = false
}
// DisableStaleReadMeetLock is called when stale-read fallbacks to leader read after meeting key-is-locked error.
func (req *Request) DisableStaleReadMeetLock() {
req.StaleRead = false
req.ReplicaReadType = kv.ReplicaReadLeader
}
// IsGlobalStaleRead checks if the request is a global stale read request.
func (req *Request) IsGlobalStaleRead() bool {
return req.ReadReplicaScope == oracle.GlobalTxnScope &&
// remove txnScope after tidb remove it
req.TxnScope == oracle.GlobalTxnScope &&
req.GetStaleRead()
}
// IsDebugReq check whether the req is debug req.
func (req *Request) IsDebugReq() bool {
switch req.Type {
case CmdDebugGetRegionProperties:
return true
}
return false
}
// IsInterruptible checks if the request can be interrupted when the query is killed.
func (req *Request) IsInterruptible() bool {
switch req.Type {
case CmdPessimisticRollback, CmdBatchRollback, CmdCommit:
return false
default:
return true
}
}
// Get returns GetRequest in request.
func (req *Request) Get() *kvrpcpb.GetRequest {
return req.Req.(*kvrpcpb.GetRequest)
}
// Scan returns ScanRequest in request.
func (req *Request) Scan() *kvrpcpb.ScanRequest {
return req.Req.(*kvrpcpb.ScanRequest)
}
// Prewrite returns PrewriteRequest in request.
func (req *Request) Prewrite() *kvrpcpb.PrewriteRequest {
return req.Req.(*kvrpcpb.PrewriteRequest)
}
// Commit returns CommitRequest in request.
func (req *Request) Commit() *kvrpcpb.CommitRequest {
return req.Req.(*kvrpcpb.CommitRequest)
}
// Cleanup returns CleanupRequest in request.
func (req *Request) Cleanup() *kvrpcpb.CleanupRequest {
return req.Req.(*kvrpcpb.CleanupRequest)
}
// BatchGet returns BatchGetRequest in request.
func (req *Request) BatchGet() *kvrpcpb.BatchGetRequest {
return req.Req.(*kvrpcpb.BatchGetRequest)
}
// BatchRollback returns BatchRollbackRequest in request.
func (req *Request) BatchRollback() *kvrpcpb.BatchRollbackRequest {
return req.Req.(*kvrpcpb.BatchRollbackRequest)
}
// ScanLock returns ScanLockRequest in request.
func (req *Request) ScanLock() *kvrpcpb.ScanLockRequest {
return req.Req.(*kvrpcpb.ScanLockRequest)
}
// ResolveLock returns ResolveLockRequest in request.
func (req *Request) ResolveLock() *kvrpcpb.ResolveLockRequest {
return req.Req.(*kvrpcpb.ResolveLockRequest)
}
// GC returns GCRequest in request.
func (req *Request) GC() *kvrpcpb.GCRequest {
return req.Req.(*kvrpcpb.GCRequest)
}
// DeleteRange returns DeleteRangeRequest in request.
func (req *Request) DeleteRange() *kvrpcpb.DeleteRangeRequest {
return req.Req.(*kvrpcpb.DeleteRangeRequest)
}
// RawGet returns RawGetRequest in request.
func (req *Request) RawGet() *kvrpcpb.RawGetRequest {
return req.Req.(*kvrpcpb.RawGetRequest)
}
// RawBatchGet returns RawBatchGetRequest in request.
func (req *Request) RawBatchGet() *kvrpcpb.RawBatchGetRequest {
return req.Req.(*kvrpcpb.RawBatchGetRequest)
}
// RawPut returns RawPutRequest in request.
func (req *Request) RawPut() *kvrpcpb.RawPutRequest {
return req.Req.(*kvrpcpb.RawPutRequest)
}
// RawBatchPut returns RawBatchPutRequest in request.
func (req *Request) RawBatchPut() *kvrpcpb.RawBatchPutRequest {
return req.Req.(*kvrpcpb.RawBatchPutRequest)
}
// RawDelete returns PrewriteRequest in request.
func (req *Request) RawDelete() *kvrpcpb.RawDeleteRequest {
return req.Req.(*kvrpcpb.RawDeleteRequest)
}
// RawBatchDelete returns RawBatchDeleteRequest in request.
func (req *Request) RawBatchDelete() *kvrpcpb.RawBatchDeleteRequest {
return req.Req.(*kvrpcpb.RawBatchDeleteRequest)
}
// RawDeleteRange returns RawDeleteRangeRequest in request.
func (req *Request) RawDeleteRange() *kvrpcpb.RawDeleteRangeRequest {
return req.Req.(*kvrpcpb.RawDeleteRangeRequest)
}
// RawScan returns RawScanRequest in request.
func (req *Request) RawScan() *kvrpcpb.RawScanRequest {
return req.Req.(*kvrpcpb.RawScanRequest)
}
// UnsafeDestroyRange returns UnsafeDestroyRangeRequest in request.
func (req *Request) UnsafeDestroyRange() *kvrpcpb.UnsafeDestroyRangeRequest {
return req.Req.(*kvrpcpb.UnsafeDestroyRangeRequest)
}
// RawGetKeyTTL returns RawGetKeyTTLRequest in request.
func (req *Request) RawGetKeyTTL() *kvrpcpb.RawGetKeyTTLRequest {
return req.Req.(*kvrpcpb.RawGetKeyTTLRequest)
}
// RawCompareAndSwap returns RawCASRequest in request.
func (req *Request) RawCompareAndSwap() *kvrpcpb.RawCASRequest {
return req.Req.(*kvrpcpb.RawCASRequest)
}
// RawChecksum returns RawChecksumRequest in request.
func (req *Request) RawChecksum() *kvrpcpb.RawChecksumRequest {
return req.Req.(*kvrpcpb.RawChecksumRequest)
}
// RegisterLockObserver returns RegisterLockObserverRequest in request.
func (req *Request) RegisterLockObserver() *kvrpcpb.RegisterLockObserverRequest {
return req.Req.(*kvrpcpb.RegisterLockObserverRequest)
}
// CheckLockObserver returns CheckLockObserverRequest in request.
func (req *Request) CheckLockObserver() *kvrpcpb.CheckLockObserverRequest {
return req.Req.(*kvrpcpb.CheckLockObserverRequest)
}
// RemoveLockObserver returns RemoveLockObserverRequest in request.
func (req *Request) RemoveLockObserver() *kvrpcpb.RemoveLockObserverRequest {
return req.Req.(*kvrpcpb.RemoveLockObserverRequest)
}
// PhysicalScanLock returns PhysicalScanLockRequest in request.
func (req *Request) PhysicalScanLock() *kvrpcpb.PhysicalScanLockRequest {
return req.Req.(*kvrpcpb.PhysicalScanLockRequest)
}
// Cop returns coprocessor request in request.
func (req *Request) Cop() *coprocessor.Request {
return req.Req.(*coprocessor.Request)
}
// BatchCop returns BatchCop request in request.
func (req *Request) BatchCop() *coprocessor.BatchRequest {
return req.Req.(*coprocessor.BatchRequest)
}
// DispatchMPPTask returns dispatch task request in request.
func (req *Request) DispatchMPPTask() *mpp.DispatchTaskRequest {
return req.Req.(*mpp.DispatchTaskRequest)
}
// IsMPPAlive returns IsAlive request in request.
func (req *Request) IsMPPAlive() *mpp.IsAliveRequest {
return req.Req.(*mpp.IsAliveRequest)
}
// EstablishMPPConn returns EstablishMPPConnectionRequest in request.
func (req *Request) EstablishMPPConn() *mpp.EstablishMPPConnectionRequest {
return req.Req.(*mpp.EstablishMPPConnectionRequest)
}
// CancelMPPTask returns canceling task in request
func (req *Request) CancelMPPTask() *mpp.CancelTaskRequest {
return req.Req.(*mpp.CancelTaskRequest)
}
// MvccGetByKey returns MvccGetByKeyRequest in request.
func (req *Request) MvccGetByKey() *kvrpcpb.MvccGetByKeyRequest {
return req.Req.(*kvrpcpb.MvccGetByKeyRequest)
}
// MvccGetByStartTs returns MvccGetByStartTsRequest in request.
func (req *Request) MvccGetByStartTs() *kvrpcpb.MvccGetByStartTsRequest {
return req.Req.(*kvrpcpb.MvccGetByStartTsRequest)
}
// SplitRegion returns SplitRegionRequest in request.
func (req *Request) SplitRegion() *kvrpcpb.SplitRegionRequest {
return req.Req.(*kvrpcpb.SplitRegionRequest)
}
// PessimisticLock returns PessimisticLockRequest in request.
func (req *Request) PessimisticLock() *kvrpcpb.PessimisticLockRequest {
return req.Req.(*kvrpcpb.PessimisticLockRequest)
}
// PessimisticRollback returns PessimisticRollbackRequest in request.
func (req *Request) PessimisticRollback() *kvrpcpb.PessimisticRollbackRequest {
return req.Req.(*kvrpcpb.PessimisticRollbackRequest)
}
// DebugGetRegionProperties returns GetRegionPropertiesRequest in request.
func (req *Request) DebugGetRegionProperties() *debugpb.GetRegionPropertiesRequest {
return req.Req.(*debugpb.GetRegionPropertiesRequest)
}
// Compact returns CompactRequest in request.
func (req *Request) Compact() *kvrpcpb.CompactRequest {
return req.Req.(*kvrpcpb.CompactRequest)
}
// GetTiFlashSystemTable returns TiFlashSystemTableRequest in request.
func (req *Request) GetTiFlashSystemTable() *kvrpcpb.TiFlashSystemTableRequest {
return req.Req.(*kvrpcpb.TiFlashSystemTableRequest)
}
// Empty returns BatchCommandsEmptyRequest in request.
func (req *Request) Empty() *tikvpb.BatchCommandsEmptyRequest {
return req.Req.(*tikvpb.BatchCommandsEmptyRequest)
}
// CheckTxnStatus returns CheckTxnStatusRequest in request.
func (req *Request) CheckTxnStatus() *kvrpcpb.CheckTxnStatusRequest {
return req.Req.(*kvrpcpb.CheckTxnStatusRequest)
}
// CheckSecondaryLocks returns CheckSecondaryLocksRequest in request.
func (req *Request) CheckSecondaryLocks() *kvrpcpb.CheckSecondaryLocksRequest {
return req.Req.(*kvrpcpb.CheckSecondaryLocksRequest)
}
// TxnHeartBeat returns TxnHeartBeatRequest in request.
func (req *Request) TxnHeartBeat() *kvrpcpb.TxnHeartBeatRequest {
return req.Req.(*kvrpcpb.TxnHeartBeatRequest)
}
// StoreSafeTS returns StoreSafeTSRequest in request.
func (req *Request) StoreSafeTS() *kvrpcpb.StoreSafeTSRequest {
return req.Req.(*kvrpcpb.StoreSafeTSRequest)
}
// LockWaitInfo returns GetLockWaitInfoRequest in request.
func (req *Request) LockWaitInfo() *kvrpcpb.GetLockWaitInfoRequest {
return req.Req.(*kvrpcpb.GetLockWaitInfoRequest)
}
// GetHealthFeedback returns GetHealthFeedbackRequest in request.
func (req *Request) GetHealthFeedback() *kvrpcpb.GetHealthFeedbackRequest {
return req.Req.(*kvrpcpb.GetHealthFeedbackRequest)
}
func (req *Request) BroadcastTxnStatus() *kvrpcpb.BroadcastTxnStatusRequest {
return req.Req.(*kvrpcpb.BroadcastTxnStatusRequest)
}
// FlashbackToVersion returns FlashbackToVersionRequest in request.
func (req *Request) FlashbackToVersion() *kvrpcpb.FlashbackToVersionRequest {
return req.Req.(*kvrpcpb.FlashbackToVersionRequest)
}
// PrepareFlashbackToVersion returns PrepareFlashbackToVersionRequest in request.
func (req *Request) PrepareFlashbackToVersion() *kvrpcpb.PrepareFlashbackToVersionRequest {
return req.Req.(*kvrpcpb.PrepareFlashbackToVersionRequest)
}
// Flush returns FlushRequest in request.
func (req *Request) Flush() *kvrpcpb.FlushRequest {
return req.Req.(*kvrpcpb.FlushRequest)
}
// BufferBatchGet returns BufferBatchGetRequest in request.
func (req *Request) BufferBatchGet() *kvrpcpb.BufferBatchGetRequest {
return req.Req.(*kvrpcpb.BufferBatchGetRequest)
}
// ToBatchCommandsRequest converts the request to an entry in BatchCommands request.
func (req *Request) ToBatchCommandsRequest() *tikvpb.BatchCommandsRequest_Request {
switch req.Type {
case CmdGet:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Get{Get: req.Get()}}
case CmdScan:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Scan{Scan: req.Scan()}}
case CmdPrewrite:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Prewrite{Prewrite: req.Prewrite()}}
case CmdCommit:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Commit{Commit: req.Commit()}}
case CmdCleanup:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Cleanup{Cleanup: req.Cleanup()}}
case CmdBatchGet:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_BatchGet{BatchGet: req.BatchGet()}}
case CmdBatchRollback:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_BatchRollback{BatchRollback: req.BatchRollback()}}
case CmdScanLock:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_ScanLock{ScanLock: req.ScanLock()}}
case CmdResolveLock:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_ResolveLock{ResolveLock: req.ResolveLock()}}
case CmdGC:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_GC{GC: req.GC()}}
case CmdDeleteRange:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_DeleteRange{DeleteRange: req.DeleteRange()}}
case CmdRawGet:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawGet{RawGet: req.RawGet()}}
case CmdRawBatchGet:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawBatchGet{RawBatchGet: req.RawBatchGet()}}
case CmdRawPut:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawPut{RawPut: req.RawPut()}}
case CmdRawBatchPut:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawBatchPut{RawBatchPut: req.RawBatchPut()}}
case CmdRawDelete:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawDelete{RawDelete: req.RawDelete()}}
case CmdRawBatchDelete:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawBatchDelete{RawBatchDelete: req.RawBatchDelete()}}
case CmdRawDeleteRange:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawDeleteRange{RawDeleteRange: req.RawDeleteRange()}}
case CmdRawScan:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_RawScan{RawScan: req.RawScan()}}
case CmdCop:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Coprocessor{Coprocessor: req.Cop()}}
case CmdPessimisticLock:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_PessimisticLock{PessimisticLock: req.PessimisticLock()}}
case CmdPessimisticRollback:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_PessimisticRollback{PessimisticRollback: req.PessimisticRollback()}}
case CmdEmpty:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Empty{Empty: req.Empty()}}
case CmdCheckTxnStatus:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_CheckTxnStatus{CheckTxnStatus: req.CheckTxnStatus()}}
case CmdCheckSecondaryLocks:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_CheckSecondaryLocks{CheckSecondaryLocks: req.CheckSecondaryLocks()}}
case CmdTxnHeartBeat:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_TxnHeartBeat{TxnHeartBeat: req.TxnHeartBeat()}}
case CmdFlashbackToVersion:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_FlashbackToVersion{FlashbackToVersion: req.FlashbackToVersion()}}
case CmdPrepareFlashbackToVersion:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_PrepareFlashbackToVersion{PrepareFlashbackToVersion: req.PrepareFlashbackToVersion()}}
case CmdFlush:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_Flush{Flush: req.Flush()}}
case CmdBufferBatchGet:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_BufferBatchGet{BufferBatchGet: req.BufferBatchGet()}}
case CmdGetHealthFeedback:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_GetHealthFeedback{GetHealthFeedback: req.GetHealthFeedback()}}
case CmdBroadcastTxnStatus:
return &tikvpb.BatchCommandsRequest_Request{Cmd: &tikvpb.BatchCommandsRequest_Request_BroadcastTxnStatus{BroadcastTxnStatus: req.BroadcastTxnStatus()}}
}
return nil
}
// GetSize return the data size of the request.
func (req *Request) GetSize() int {
size := 0
switch req.Type {
case CmdGet:
size = req.Get().Size()
case CmdBatchGet:
size = req.BatchGet().Size()
case CmdScan:
size = req.Scan().Size()
case CmdCop:
size = req.Cop().Size()
case CmdPrewrite:
size = req.Prewrite().Size()
case CmdCommit:
size = req.Commit().Size()
case CmdPessimisticLock:
size = req.PessimisticLock().Size()
case CmdPessimisticRollback:
size = req.PessimisticRollback().Size()
case CmdBatchRollback:
size = req.BatchRollback().Size()
case CmdCheckSecondaryLocks:
size = req.CheckSecondaryLocks().Size()
case CmdScanLock:
size = req.ScanLock().Size()
case CmdResolveLock:
size = req.ResolveLock().Size()
case CmdFlush:
size = req.Flush().Size()
case CmdCheckTxnStatus:
size = req.CheckTxnStatus().Size()
case CmdMPPTask:
size = req.DispatchMPPTask().Size()
default:
// ignore others
}
return size
}
// Response wraps all kv/coprocessor responses.
type Response struct {
Resp interface{}
}
// ResponseExt likes Response but contains extra information.
type ResponseExt struct {
Response
// The address of the target store. When forwarding is enabled, it points to the target node handling the request
// rather than the node forwarding the message.
Addr string
}
// FromBatchCommandsResponse converts a BatchCommands response to Response.
func FromBatchCommandsResponse(res *tikvpb.BatchCommandsResponse_Response) (*Response, error) {
if res.GetCmd() == nil {
return nil, errors.New("Unknown command response")
}
switch res := res.GetCmd().(type) {
case *tikvpb.BatchCommandsResponse_Response_Get:
return &Response{Resp: res.Get}, nil
case *tikvpb.BatchCommandsResponse_Response_Scan:
return &Response{Resp: res.Scan}, nil
case *tikvpb.BatchCommandsResponse_Response_Prewrite:
return &Response{Resp: res.Prewrite}, nil
case *tikvpb.BatchCommandsResponse_Response_Commit:
return &Response{Resp: res.Commit}, nil
case *tikvpb.BatchCommandsResponse_Response_Cleanup:
return &Response{Resp: res.Cleanup}, nil
case *tikvpb.BatchCommandsResponse_Response_BatchGet:
return &Response{Resp: res.BatchGet}, nil
case *tikvpb.BatchCommandsResponse_Response_BatchRollback:
return &Response{Resp: res.BatchRollback}, nil
case *tikvpb.BatchCommandsResponse_Response_ScanLock:
return &Response{Resp: res.ScanLock}, nil
case *tikvpb.BatchCommandsResponse_Response_ResolveLock:
return &Response{Resp: res.ResolveLock}, nil
case *tikvpb.BatchCommandsResponse_Response_GC:
return &Response{Resp: res.GC}, nil
case *tikvpb.BatchCommandsResponse_Response_DeleteRange:
return &Response{Resp: res.DeleteRange}, nil
case *tikvpb.BatchCommandsResponse_Response_FlashbackToVersion:
return &Response{Resp: res.FlashbackToVersion}, nil
case *tikvpb.BatchCommandsResponse_Response_PrepareFlashbackToVersion:
return &Response{Resp: res.PrepareFlashbackToVersion}, nil
case *tikvpb.BatchCommandsResponse_Response_RawGet:
return &Response{Resp: res.RawGet}, nil
case *tikvpb.BatchCommandsResponse_Response_RawBatchGet:
return &Response{Resp: res.RawBatchGet}, nil
case *tikvpb.BatchCommandsResponse_Response_RawPut:
return &Response{Resp: res.RawPut}, nil
case *tikvpb.BatchCommandsResponse_Response_RawBatchPut:
return &Response{Resp: res.RawBatchPut}, nil
case *tikvpb.BatchCommandsResponse_Response_RawDelete:
return &Response{Resp: res.RawDelete}, nil
case *tikvpb.BatchCommandsResponse_Response_RawBatchDelete:
return &Response{Resp: res.RawBatchDelete}, nil
case *tikvpb.BatchCommandsResponse_Response_RawDeleteRange:
return &Response{Resp: res.RawDeleteRange}, nil
case *tikvpb.BatchCommandsResponse_Response_RawScan:
return &Response{Resp: res.RawScan}, nil
case *tikvpb.BatchCommandsResponse_Response_Coprocessor:
return &Response{Resp: res.Coprocessor}, nil
case *tikvpb.BatchCommandsResponse_Response_PessimisticLock:
return &Response{Resp: res.PessimisticLock}, nil
case *tikvpb.BatchCommandsResponse_Response_PessimisticRollback:
return &Response{Resp: res.PessimisticRollback}, nil
case *tikvpb.BatchCommandsResponse_Response_Empty:
return &Response{Resp: res.Empty}, nil
case *tikvpb.BatchCommandsResponse_Response_TxnHeartBeat:
return &Response{Resp: res.TxnHeartBeat}, nil
case *tikvpb.BatchCommandsResponse_Response_CheckTxnStatus:
return &Response{Resp: res.CheckTxnStatus}, nil
case *tikvpb.BatchCommandsResponse_Response_CheckSecondaryLocks:
return &Response{Resp: res.CheckSecondaryLocks}, nil
case *tikvpb.BatchCommandsResponse_Response_Flush:
return &Response{Resp: res.Flush}, nil
case *tikvpb.BatchCommandsResponse_Response_BufferBatchGet:
return &Response{Resp: res.BufferBatchGet}, nil
case *tikvpb.BatchCommandsResponse_Response_GetHealthFeedback:
return &Response{Resp: res.GetHealthFeedback}, nil
case *tikvpb.BatchCommandsResponse_Response_BroadcastTxnStatus:
return &Response{Resp: res.BroadcastTxnStatus}, nil
}
panic("unreachable")
}
// CopStreamResponse combines tikvpb.Tikv_CoprocessorStreamClient and the first Recv() result together.
// In streaming API, get grpc stream client may not involve any network packet, then region error have
// to be handled in Recv() function. This struct facilitates the error handling.
type CopStreamResponse struct {
tikvpb.Tikv_CoprocessorStreamClient
*coprocessor.Response // The first result of Recv()
Timeout time.Duration
Lease // Shared by this object and a background goroutine.
Ctx context.Context
Bypass bool
CountRPC bool
}
// BatchCopStreamResponse comprises the BatchCoprocessorClient , the first result and timeout detector.
type BatchCopStreamResponse struct {
tikvpb.Tikv_BatchCoprocessorClient
*coprocessor.BatchResponse
Timeout time.Duration
Lease // Shared by this object and a background goroutine.
Ctx context.Context
CountRPC bool
}
// MPPStreamResponse is indeed a wrapped client that can receive data packet from tiflash mpp server.
type MPPStreamResponse struct {
tikvpb.Tikv_EstablishMPPConnectionClient
*mpp.MPPDataPacket
Timeout time.Duration
Lease
}
//go:generate bash gen.sh
// AttachContext sets the request context to the request,
// return false if encounter unknown request type.
// Parameter `rpcCtx` use `kvrpcpb.Context` instead of `*kvrpcpb.Context` to avoid concurrent modification by shallow copy.
func AttachContext(req *Request, rpcCtx kvrpcpb.Context) bool {
fillDefaultRequestOrigin(&rpcCtx)
req.Context = rpcCtx
ctx := &rpcCtx
cmd := req.Type
// CmdCopStream and CmdCop share the same request type.
if cmd == CmdCopStream {
cmd = CmdCop
}
if cmd == CmdCop {
// Only NextGen's RU-v1 controller can price the factual remote read
// subset separately. Legacy clients must keep the worker's pre-priced
// compatibility response.
ctx.ClientPricesRemoteReads = config.NextGen
}
if patchCmdCtx(req, cmd, ctx) {
return true
}
switch req.Type {
// Dispatching MPP tasks don't need a region context, because it's a request for store but not region.
case CmdMPPTask:
case CmdMPPConn:
case CmdMPPCancel:
case CmdMPPAlive:
// Empty command doesn't need a region context.
case CmdEmpty:
default:
return false
}
return true
}
// SetContext set the Context field for the given req to the specified ctx.
//
// Deprecated: use SetContextNoAttach instead, RPCClient will call AttachContext(req, req.Context).
func SetContext(req *Request, region *metapb.Region, peer *metapb.Peer) error {
if region != nil {
req.RegionId = region.Id
req.RegionEpoch = region.RegionEpoch
}
req.Peer = peer
// Shallow copy the context to avoid concurrent modification.
if !AttachContext(req, req.Context) {
return errors.Errorf("invalid request type %v", req.Type)
}
return nil
}
// SetContextNoAttach likes SetContext, but it doesn't attach the context to the underlying request.
func SetContextNoAttach(req *Request, region *metapb.Region, peer *metapb.Peer) error {
if !isValidReqType(req.Type) {
return errors.Errorf("invalid request type %v", req.Type)
}
if region != nil {
req.RegionId = region.Id
req.RegionEpoch = region.RegionEpoch
}
req.Peer = peer
return nil
}
// GenRegionErrorResp returns corresponding Response with specified RegionError
// according to the given req.
func GenRegionErrorResp(req *Request, e *errorpb.Error) (*Response, error) {
var p interface{}
resp := &Response{}
switch req.Type {
case CmdGet:
p = &kvrpcpb.GetResponse{
RegionError: e,
}
case CmdScan:
p = &kvrpcpb.ScanResponse{
RegionError: e,
}
case CmdPrewrite:
p = &kvrpcpb.PrewriteResponse{
RegionError: e,
}
case CmdPessimisticLock:
p = &kvrpcpb.PessimisticLockResponse{
RegionError: e,
}
case CmdPessimisticRollback:
p = &kvrpcpb.PessimisticRollbackResponse{
RegionError: e,
}
case CmdCommit:
p = &kvrpcpb.CommitResponse{
RegionError: e,
}
case CmdCleanup:
p = &kvrpcpb.CleanupResponse{
RegionError: e,
}
case CmdBatchGet:
p = &kvrpcpb.BatchGetResponse{
RegionError: e,
}
case CmdBatchRollback:
p = &kvrpcpb.BatchRollbackResponse{
RegionError: e,
}
case CmdScanLock:
p = &kvrpcpb.ScanLockResponse{
RegionError: e,
}
case CmdResolveLock:
p = &kvrpcpb.ResolveLockResponse{
RegionError: e,
}
case CmdGC:
p = &kvrpcpb.GCResponse{
RegionError: e,
}
case CmdDeleteRange:
p = &kvrpcpb.DeleteRangeResponse{
RegionError: e,
}
case CmdRawGet: