-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
errors.go
1737 lines (1488 loc) · 58 KB
/
errors.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"
"reflect"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/caller"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
_ "github.com/cockroachdb/errors/extgrpc" // register EncodeError support for gRPC Status
"github.com/cockroachdb/redact"
"github.com/gogo/protobuf/proto"
)
// Printer is an interface that lets us use what's common between the
// errors.Printer interface and redact.SafePrinter so we can write functions
// that both SafeFormatError and SafeFormat can share.
type Printer interface {
// Print appends args to the message output.
Print(args ...interface{})
// Printf writes a formatted string.
Printf(format string, args ...interface{})
}
// ClientVisibleRetryError is to be implemented by errors visible by
// layers above and that can be handled by retrying the transaction.
type ClientVisibleRetryError interface {
ClientVisibleRetryError()
}
// ClientVisibleAmbiguousError is to be implemented by errors visible
// by layers above and that indicate uncertainty.
type ClientVisibleAmbiguousError interface {
ClientVisibleAmbiguousError()
}
func (e *UnhandledRetryableError) Error() string {
return e.String()
}
var _ error = &UnhandledRetryableError{}
func (e *UnhandledRetryableError) SafeFormatError(p errors.Printer) (next error) {
p.Print(e.PErr)
return nil
}
// SafeFormat implements redact.SafeFormatter.
func (e *UnhandledRetryableError) SafeFormat(s redact.SafePrinter, r rune) {
e.PErr.SafeFormat(s, r)
}
func (e *UnhandledRetryableError) String() string {
return redact.StringWithoutMarkers(e)
}
// transactionRestartError is an interface implemented by errors that cause
// a transaction to be restarted.
type transactionRestartError interface {
canRestartTransaction() TransactionRestart
}
// ErrorUnexpectedlySet creates a string to panic with when a response (typically
// a kvpb.BatchResponse) unexpectedly has Error set in its response header.
func ErrorUnexpectedlySet(culprit, response interface{}) error {
return errors.AssertionFailedf("error is unexpectedly set, culprit is %T:\n%+v", culprit, response)
}
// ErrorPriority is used to rank errors such that the "best" one is chosen to be
// presented as the batch result when a batch is split up and observes multiple
// errors. Higher values correspond to higher priorities.
type ErrorPriority int
const (
_ ErrorPriority = iota
// ErrorScoreTxnRestart indicates that the transaction should be restarted
// with an incremented epoch.
ErrorScoreTxnRestart
// ErrorScoreUnambiguousError is used for errors which are known to return a
// transaction reflecting the highest timestamp of any intent that was
// written. We allow the transaction to continue after such errors; we also
// allow RollbackToSavepoint() to be called after such errors. In particular,
// this is useful for SQL which wants to allow rolling back to a savepoint
// after ConditionFailedErrors (uniqueness violations) and LockConflictError
// (lock not available errors). With continuing after errors it's important
// for the coordinator to track the timestamp at which intents might have been
// written.
//
// Note that all the lower scores also are unambiguous in this sense, so this
// score can be seen as an upper-bound for unambiguous errors.
ErrorScoreUnambiguousError
// ErrorScoreNonRetriable indicates that the transaction performed an
// operation that does not warrant a retry. The error should be propagated to
// the client and the transaction should terminate immediately.
ErrorScoreNonRetriable
// ErrorScoreTxnAbort indicates that the transaction is aborted. The
// operation can only try again under the purview of a new transaction.
//
// This error has the highest priority because, as far as KV is concerned, a
// TransactionAbortedError is impossible to recover from (whereas
// non-retriable errors could conceivably be recovered if the client wanted to
// ignore them). Also, the TxnCoordSender likes to assume that a
// TransactionAbortedError is the only way it finds about an aborted
// transaction, and so it benefits from all other errors being merged into a
// TransactionAbortedError instead of the other way around.
ErrorScoreTxnAbort
)
// ErrPriority computes the priority of the given error.
func ErrPriority(err error) ErrorPriority {
// TODO(tbg): this method could take an `*Error` if it weren't for SQL
// propagating these as an `error`. See `DistSQLReceiver.Push`.
var detail ErrorDetailInterface
switch tErr := err.(type) {
case nil:
return 0
case ErrorDetailInterface:
detail = tErr
case *internalError:
detail = (*Error)(tErr).GetDetail()
case *UnhandledRetryableError:
if _, ok := tErr.PErr.GetDetail().(*TransactionAbortedError); ok {
return ErrorScoreTxnAbort
}
return ErrorScoreTxnRestart
}
switch v := detail.(type) {
case *TransactionRetryWithProtoRefreshError:
if v.PrevTxnAborted() {
return ErrorScoreTxnAbort
}
return ErrorScoreTxnRestart
case *ConditionFailedError, *WriteIntentError:
// We particularly care about returning the low ErrorScoreUnambiguousError
// because we don't want to transition a transaction that encounters a
// ConditionFailedError or a WriteIntentError to an error state. More
// specifically, we want to allow rollbacks to savepoint after one of these
// errors.
return ErrorScoreUnambiguousError
}
return ErrorScoreNonRetriable
}
// NewError creates an Error from the given error.
func NewError(err error) *Error {
if err == nil {
return nil
}
e := &Error{
EncodedError: errors.EncodeError(context.Background(), err),
}
return e
}
// NewErrorWithTxn creates an Error from the given error and a transaction.
//
// txn is cloned before being stored in Error.
func NewErrorWithTxn(err error, txn *roachpb.Transaction) *Error {
e := NewError(err)
e.SetTxn(txn)
return e
}
// NewErrorf creates an Error from the given error message. It is a
// passthrough to fmt.Errorf, with an additional prefix containing the
// filename and line number.
func NewErrorf(format string, a ...interface{}) *Error {
err := errors.Newf(format, a...)
file, line, _ := caller.Lookup(1)
err = errors.Wrapf(err, "%s:%d", file, line)
return NewError(err)
}
// SafeFormat implements redact.SafeFormatter.
func (e *Error) SafeFormat(s redact.SafePrinter, _ rune) {
if e == nil {
s.Print(nil)
return
}
s.Print(errors.DecodeError(context.Background(), e.EncodedError))
if txn := e.GetTxn(); txn != nil {
s.SafeString(": ")
s.Print(txn)
}
}
func (e *Error) SafeFormatError(p errors.Printer) (next error) {
if e == nil {
p.Print(nil)
return
}
p.Print(errors.DecodeError(context.Background(), e.EncodedError))
if txn := e.GetTxn(); txn != nil {
p.Printf(": %v", txn)
}
return nil
}
// String implements fmt.Stringer.
func (e *Error) String() string {
return redact.StringWithoutMarkers(e)
}
// TransactionRestart returns the TransactionRestart for this Error.
func (e *Error) TransactionRestart() TransactionRestart {
if e.EncodedError.IsSet() {
var iface transactionRestartError
if errors.As(errors.DecodeError(context.Background(), e.EncodedError), &iface) {
return iface.canRestartTransaction()
}
}
return TransactionRestart_NONE
}
type internalError Error
func (e *internalError) Error() string {
return (*Error)(e).String()
}
// ErrorDetailInterface is an interface for each error detail.
// These must not be implemented by anything other than our protobuf-backed error details
// as we rely on a 1:1 correspondence between the interface and what can be stored via
// `Error.DeprecatedSetDetail`.
type ErrorDetailInterface interface {
error
protoutil.Message
// Type returns the error's type.
Type() ErrorDetailType
}
// ErrorDetailType identifies the type of KV error.
type ErrorDetailType int
// This lists all ErrorDetail types. The numeric values in this list are used to
// identify corresponding timeseries.
//
//go:generate stringer -type=ErrorDetailType
const (
NotLeaseHolderErrType ErrorDetailType = 1
RangeNotFoundErrType ErrorDetailType = 2
RangeKeyMismatchErrType ErrorDetailType = 3
ReadWithinUncertaintyIntervalErrType ErrorDetailType = 4
TransactionAbortedErrType ErrorDetailType = 5
TransactionPushErrType ErrorDetailType = 6
TransactionRetryErrType ErrorDetailType = 7
TransactionStatusErrType ErrorDetailType = 8
WriteIntentErrType ErrorDetailType = 9
WriteTooOldErrType ErrorDetailType = 10
OpRequiresTxnErrType ErrorDetailType = 11
ConditionFailedErrType ErrorDetailType = 12
LeaseRejectedErrType ErrorDetailType = 13
NodeUnavailableErrType ErrorDetailType = 14
RaftGroupDeletedErrType ErrorDetailType = 16
ReplicaCorruptionErrType ErrorDetailType = 17
ReplicaTooOldErrType ErrorDetailType = 18
AmbiguousResultErrType ErrorDetailType = 26
StoreNotFoundErrType ErrorDetailType = 27
TransactionRetryWithProtoRefreshErrType ErrorDetailType = 28
IntegerOverflowErrType ErrorDetailType = 31
UnsupportedRequestErrType ErrorDetailType = 32
BatchTimestampBeforeGCErrType ErrorDetailType = 34
TxnAlreadyEncounteredErrType ErrorDetailType = 35
IntentMissingErrType ErrorDetailType = 36
MergeInProgressErrType ErrorDetailType = 37
RangeFeedRetryErrType ErrorDetailType = 38
IndeterminateCommitErrType ErrorDetailType = 39
InvalidLeaseErrType ErrorDetailType = 40
OptimisticEvalConflictsErrType ErrorDetailType = 41
MinTimestampBoundUnsatisfiableErrType ErrorDetailType = 42
RefreshFailedErrType ErrorDetailType = 43
MVCCHistoryMutationErrType ErrorDetailType = 44
LockConflictErrType ErrorDetailType = 45
// When adding new error types, don't forget to update NumErrors below.
// CommunicationErrType indicates a gRPC error; this is not an ErrorDetail.
// The value 22 is chosen because it's reserved in the errors proto.
CommunicationErrType ErrorDetailType = 22
// InternalErrType indicates a pErr that doesn't contain a recognized error
// detail. The value 25 is chosen because it's reserved in the errors proto.
InternalErrType ErrorDetailType = 25
NumErrors int = 46
)
// Register the migration of all errors that used to be in the roachpb package
// and are now in the kv/kvpb package.
func init() {
roachpbPath := reflect.TypeOf(roachpb.Key("")).PkgPath()
errors.RegisterTypeMigration(roachpbPath, "*roachpb.UnhandledRetryableError", &UnhandledRetryableError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.internalError", &internalError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.NotLeaseHolderError", &NotLeaseHolderError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.RangeNotFoundError", &RangeNotFoundError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.RangeKeyMismatchError", &RangeKeyMismatchError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.ReadWithinUncertaintyIntervalError", &ReadWithinUncertaintyIntervalError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.TransactionAbortedError", &TransactionAbortedError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.TransactionPushError", &TransactionPushError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.TransactionRetryError", &TransactionRetryError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.TransactionStatusError", &TransactionStatusError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.WriteIntentError", &WriteIntentError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.WriteTooOldError", &WriteTooOldError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.OpRequiresTxnError", &OpRequiresTxnError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.ConditionFailedError", &ConditionFailedError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.LeaseRejectedError", &LeaseRejectedError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.NodeUnavailableError", &NodeUnavailableError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.RaftGroupDeletedError", &RaftGroupDeletedError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.ReplicaCorruptionError", &ReplicaCorruptionError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.ReplicaTooOldError", &ReplicaTooOldError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.StoreNotFoundError", &StoreNotFoundError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.TransactionRetryWithProtoRefreshError", &TransactionRetryWithProtoRefreshError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.IntegerOverflowError", &IntegerOverflowError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.UnsupportedRequestError", &UnsupportedRequestError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.BatchTimestampBeforeGCError", &BatchTimestampBeforeGCError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.TxnAlreadyEncounteredErrorError", &TxnAlreadyEncounteredErrorError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.IntentMissingError", &IntentMissingError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.MergeInProgressError", &MergeInProgressError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.RangeFeedRetryError", &RangeFeedRetryError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.IndeterminateCommitError", &IndeterminateCommitError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.InvalidLeaseError", &InvalidLeaseError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.OptimisticEvalConflictsError", &OptimisticEvalConflictsError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.MinTimestampBoundUnsatisfiableError", &MinTimestampBoundUnsatisfiableError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.RefreshFailedError", &RefreshFailedError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.MVCCHistoryMutationError", &MVCCHistoryMutationError{})
errors.RegisterTypeMigration(roachpbPath, "*roachpb.InsufficientSpaceError", &InsufficientSpaceError{})
}
// GoError returns a Go error converted from Error. If the error is a transaction
// retry error, it returns the error itself wrapped in an UnhandledRetryableError.
// Otherwise, if an error detail is present, is is returned (i.e. the result will
// match GetDetail()). Otherwise, returns the error itself masqueraded as an `error`.
func (e *Error) GoError() error {
if e == nil {
return nil
}
if e.EncodedError.IsSet() {
err := errors.DecodeError(context.Background(), e.EncodedError)
var iface transactionRestartError
if errors.As(err, &iface) {
if txnRestart := iface.canRestartTransaction(); txnRestart != TransactionRestart_NONE {
// TODO(tbg): revisit this unintuitive error wrapping here and see if
// a better solution can be found.
return &UnhandledRetryableError{
PErr: *e,
}
}
}
return err
}
// Everything below is legacy behavior that can be deleted in 21.2.
if e.TransactionRestart() != TransactionRestart_NONE {
return &UnhandledRetryableError{
PErr: *e,
}
}
if detail := e.GetDetail(); detail != nil {
return detail
}
return (*internalError)(e)
}
// GetDetail returns an error detail associated with the error, or nil otherwise.
func (e *Error) GetDetail() ErrorDetailInterface {
if e == nil || !e.EncodedError.IsSet() {
return nil
}
var detail ErrorDetailInterface
errors.As(errors.DecodeError(context.Background(), e.EncodedError), &detail)
return detail
}
// SetTxn sets the error transaction and resets the error message.
// The argument is cloned before being stored in the Error.
func (e *Error) SetTxn(txn *roachpb.Transaction) {
e.UnexposedTxn = nil
e.UpdateTxn(txn)
}
// UpdateTxn updates the error transaction and resets the error message.
// The argument is cloned before being stored in the Error.
func (e *Error) UpdateTxn(o *roachpb.Transaction) {
if o == nil {
return
}
if e.UnexposedTxn == nil {
e.UnexposedTxn = o.Clone()
} else {
e.UnexposedTxn.Update(o)
}
e.checkTxnStatusValid()
}
// checkTxnStatusValid verifies that the transaction status is in-sync with the
// error detail.
func (e *Error) checkTxnStatusValid() {
txn := e.UnexposedTxn
err := e.GetDetail()
if txn == nil {
return
}
if errors.HasType(err, (*TransactionAbortedError)(nil)) {
return
}
if e.TransactionRestart() == TransactionRestart_NONE {
return
}
if txn.Status.IsFinalized() {
log.Fatalf(context.TODO(), "transaction unexpectedly finalized in (%T): %v", err, e)
}
}
// GetTxn returns the txn.
func (e *Error) GetTxn() *roachpb.Transaction {
if e == nil {
return nil
}
return e.UnexposedTxn
}
// SetErrorIndex sets the index of the error.
func (e *Error) SetErrorIndex(index int32) {
e.Index = &ErrPosition{Index: index}
}
func (e *NodeUnavailableError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *NodeUnavailableError) SafeFormatError(p errors.Printer) (next error) {
p.Printf("node unavailable; try another peer")
return nil
}
// Type is part of the ErrorDetailInterface.
func (e *NodeUnavailableError) Type() ErrorDetailType {
return NodeUnavailableErrType
}
var _ ErrorDetailInterface = &NodeUnavailableError{}
func (e *NotLeaseHolderError) Error() string {
return redact.Sprint(e).StripMarkers()
}
// Type is part of the ErrorDetailInterface.
func (e *NotLeaseHolderError) Type() ErrorDetailType {
return NotLeaseHolderErrType
}
func (e *NotLeaseHolderError) printError(s Printer) {
s.Printf("[NotLeaseHolderError] ")
if e.CustomMsg != "" {
s.Print(e.CustomMsg)
s.Printf("; ")
}
s.Printf("r%d: ", e.RangeID)
if e.Replica != (roachpb.ReplicaDescriptor{}) {
s.Printf("replica %s not lease holder; ", e.Replica)
} else {
s.Printf("replica not lease holder; ")
}
if e.Lease != nil {
s.Printf("current lease is %s", e.Lease)
} else if e.DeprecatedLeaseHolder != nil {
s.Printf("replica %s is", *e.DeprecatedLeaseHolder)
} else {
s.Printf("lease holder unknown")
}
}
func (e *NotLeaseHolderError) SafeFormatError(p errors.Printer) (next error) {
e.printError(p)
return nil
}
var _ ErrorDetailInterface = &NotLeaseHolderError{}
// Type is part of the ErrorDetailInterface.
func (e *LeaseRejectedError) Type() ErrorDetailType {
return LeaseRejectedErrType
}
func (e *LeaseRejectedError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *LeaseRejectedError) SafeFormatError(p errors.Printer) (next error) {
p.Printf("cannot replace lease %s with %s: %s", e.Existing, e.Requested, e.Message)
return nil
}
var _ ErrorDetailInterface = &LeaseRejectedError{}
// NewRangeNotFoundError initializes a new RangeNotFoundError for the given RangeID and, optionally,
// a StoreID.
func NewRangeNotFoundError(rangeID roachpb.RangeID, storeID roachpb.StoreID) *RangeNotFoundError {
return &RangeNotFoundError{
RangeID: rangeID,
StoreID: storeID,
}
}
func (e *RangeNotFoundError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *RangeNotFoundError) printError(s Printer) {
s.Printf("r%d was not found", e.RangeID)
if e.StoreID != 0 {
s.Printf(" on s%d", e.StoreID)
}
}
func (e *RangeNotFoundError) SafeFormatError(p errors.Printer) (next error) {
e.printError(p)
return nil
}
// Type is part of the ErrorDetailInterface.
func (e *RangeNotFoundError) Type() ErrorDetailType {
return RangeNotFoundErrType
}
var _ ErrorDetailInterface = &RangeNotFoundError{}
// IsRangeNotFoundError returns true if err contains a *RangeNotFoundError.
func IsRangeNotFoundError(err error) bool {
return errors.HasType(err, (*RangeNotFoundError)(nil))
}
// NewRangeKeyMismatchErrorWithCTPolicy initializes a new RangeKeyMismatchError.
// identical to NewRangeKeyMismatchError, with the given ClosedTimestampPolicy.
func NewRangeKeyMismatchErrorWithCTPolicy(
ctx context.Context,
start, end roachpb.Key,
desc *roachpb.RangeDescriptor,
lease *roachpb.Lease,
ctPolicy roachpb.RangeClosedTimestampPolicy,
) *RangeKeyMismatchError {
if desc == nil {
panic("NewRangeKeyMismatchError with nil descriptor")
}
if !desc.IsInitialized() {
// We must never send uninitialized ranges back to the client guard against
// regressions of #6027.
panic(fmt.Sprintf("descriptor is not initialized: %+v", desc))
}
var l roachpb.Lease
if lease != nil {
// We ignore leases that are not part of the descriptor.
_, ok := desc.GetReplicaDescriptorByID(lease.Replica.ReplicaID)
if ok {
l = *lease
}
}
e := &RangeKeyMismatchError{
RequestStartKey: start,
RequestEndKey: end,
}
ri := roachpb.RangeInfo{
Desc: *desc,
Lease: l,
ClosedTimestampPolicy: ctPolicy,
}
// More ranges are sometimes added to rangesInternal later.
e.AppendRangeInfo(ctx, ri)
return e
}
// NewRangeKeyMismatchError initializes a new RangeKeyMismatchError.
//
// desc and lease represent info about the range that the request was
// erroneously routed to. lease can be nil. If it's not nil but the leaseholder
// is not part of desc, it is ignored. This allows callers to read the
// descriptor and lease non-atomically without worrying about incoherence.
//
// Note that more range info is commonly added to the error after the error is
// created.
func NewRangeKeyMismatchError(
ctx context.Context, start, end roachpb.Key, desc *roachpb.RangeDescriptor, lease *roachpb.Lease,
) *RangeKeyMismatchError {
return NewRangeKeyMismatchErrorWithCTPolicy(ctx,
start,
end,
desc,
lease,
roachpb.LAG_BY_CLUSTER_SETTING, /* default closed timestsamp policy*/
)
}
func (e *RangeKeyMismatchError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *RangeKeyMismatchError) printError(s Printer) {
mr, err := e.MismatchedRange()
if err != nil {
s.Print(err)
}
s.Printf("key range %s-%s outside of bounds of range %s-%s; suggested ranges: %s",
e.RequestStartKey, e.RequestEndKey, mr.Desc.StartKey, mr.Desc.EndKey, e.Ranges)
}
func (e *RangeKeyMismatchError) SafeFormatError(p errors.Printer) (next error) {
e.printError(p)
return nil
}
// Type is part of the ErrorDetailInterface.
func (e *RangeKeyMismatchError) Type() ErrorDetailType {
return RangeKeyMismatchErrType
}
// MismatchedRange returns the range info for the range that the request was
// erroneously routed to, or an error if the Ranges slice is empty.
func (e *RangeKeyMismatchError) MismatchedRange() (roachpb.RangeInfo, error) {
if len(e.Ranges) == 0 {
return roachpb.RangeInfo{}, errors.AssertionFailedf(
"RangeKeyMismatchError (key range %s-%s) with empty RangeInfo slice", e.RequestStartKey, e.RequestEndKey,
)
}
return e.Ranges[0], nil
}
// AppendRangeInfo appends info about a group of ranges to the set returned to the
// kvclient.
//
// l can be empty. Otherwise, the leaseholder is asserted to be a replica in
// desc.
func (e *RangeKeyMismatchError) AppendRangeInfo(ctx context.Context, ris ...roachpb.RangeInfo) {
for _, ri := range ris {
if !ri.Lease.Empty() {
if _, ok := ri.Desc.GetReplicaDescriptorByID(ri.Lease.Replica.ReplicaID); !ok {
log.Fatalf(ctx, "lease names missing replica; lease: %s, desc: %s", ri.Lease, ri.Desc)
}
}
e.Ranges = append(e.Ranges, ri)
}
}
var _ ErrorDetailInterface = &RangeKeyMismatchError{}
// ClientVisibleAmbiguousError implements the ClientVisibleAmbiguousError interface.
func (e *AmbiguousResultError) ClientVisibleAmbiguousError() {}
var _ ErrorDetailInterface = &AmbiguousResultError{}
var _ ClientVisibleAmbiguousError = &AmbiguousResultError{}
func (e *TransactionAbortedError) Error() string {
return fmt.Sprintf("TransactionAbortedError(%s)", e.Reason)
}
func (*TransactionAbortedError) canRestartTransaction() TransactionRestart {
return TransactionRestart_IMMEDIATE
}
// Type is part of the ErrorDetailInterface.
func (e *TransactionAbortedError) Type() ErrorDetailType {
return TransactionAbortedErrType
}
var _ ErrorDetailInterface = &TransactionAbortedError{}
var _ transactionRestartError = &TransactionAbortedError{}
// ClientVisibleRetryError implements the ClientVisibleRetryError interface.
func (e *TransactionRetryWithProtoRefreshError) ClientVisibleRetryError() {}
func (e *TransactionRetryWithProtoRefreshError) Error() string {
return redact.Sprint(e).StripMarkers()
}
// Type is part of the ErrorDetailInterface.
func (e *TransactionRetryWithProtoRefreshError) Type() ErrorDetailType {
return TransactionRetryWithProtoRefreshErrType
}
var _ ClientVisibleRetryError = &TransactionRetryWithProtoRefreshError{}
var _ ErrorDetailInterface = &TransactionRetryWithProtoRefreshError{}
// NewTransactionAbortedError initializes a new TransactionAbortedError.
func NewTransactionAbortedError(reason TransactionAbortedReason) *TransactionAbortedError {
return &TransactionAbortedError{
Reason: reason,
}
}
func (e *TransactionAbortedError) SafeFormatError(p errors.Printer) (next error) {
p.Printf("TransactionAbortedError(%s)", redact.SafeString(TransactionAbortedReason_name[int32(e.Reason)]))
return nil
}
type retryErrOptions struct {
conflictingTxn *enginepb.TxnMeta
}
// RetryErrOption is used to annotate optional fields in retry related errors.
type RetryErrOption interface {
apply(*retryErrOptions)
}
type retryErrOptionFunc func(*retryErrOptions)
func (f retryErrOptionFunc) apply(o *retryErrOptions) {
f(o)
}
// WithConflictingTxn is used to annotate a retry error with the conflicting
// transaction metadata (optional). This is only used for cases where a refresh
// fails with `REASON_INTENT`.
func WithConflictingTxn(txn *enginepb.TxnMeta) RetryErrOption {
return retryErrOptionFunc(func(o *retryErrOptions) {
o.conflictingTxn = txn
})
}
// NewTransactionRetryWithProtoRefreshError initializes a new
// TransactionRetryWithProtoRefreshError.
//
// prevTxnID is the ID of the transaction being retried.
// prevTxnEpoch is the epoch of the transaction being retried.
// nextTxn is the transaction that the client should use for the next attempts.
//
// TODO(tbg): the message passed here is usually pErr.String(), which is a bad
// pattern (loses structure, thus redaction). We can leverage error chaining
// to improve this: wrap `pErr.GoError()` with a barrier and then with the
// TransactionRetryWithProtoRefreshError.
func NewTransactionRetryWithProtoRefreshError(
msg redact.RedactableString,
prevTxnID uuid.UUID,
prevTxnEpoch enginepb.TxnEpoch,
nextTxn roachpb.Transaction,
opts ...RetryErrOption,
) *TransactionRetryWithProtoRefreshError {
options := retryErrOptions{}
for _, o := range opts {
o.apply(&options)
}
return &TransactionRetryWithProtoRefreshError{
Msg: msg.StripMarkers(),
MsgRedactable: msg,
PrevTxnID: prevTxnID,
PrevTxnEpoch: prevTxnEpoch,
NextTransaction: nextTxn,
ConflictingTxn: options.conflictingTxn,
}
}
func (e *TransactionRetryWithProtoRefreshError) SafeFormatError(p errors.Printer) (next error) {
if e.MsgRedactable != "" {
p.Printf("TransactionRetryWithProtoRefreshError: %s", e.MsgRedactable)
} else {
p.Printf("TransactionRetryWithProtoRefreshError: %s", e.Msg)
}
return nil
}
// PrevTxnAborted returns true if this error originated from a
// TransactionAbortedError. If true, the client will need to create a new
// transaction, as opposed to continuing with the existing one at a bumped
// epoch.
func (e *TransactionRetryWithProtoRefreshError) PrevTxnAborted() bool {
return !e.PrevTxnID.Equal(e.NextTransaction.ID)
}
// PrevTxnEpochBumped returns true if the previous transaction was not aborted
// but its epoch was bumped. In this case, the client can continue with the
// existing transaction, but must restart from the beginning because its writes
// were discarded.
//
// NOTE: the method panics if the previous transaction was aborted and the next
// transaction has a different identity. Callers must first check PrevTxnAborted.
func (e *TransactionRetryWithProtoRefreshError) PrevTxnEpochBumped() bool {
if e.PrevTxnAborted() {
panic("PrevTxnEpochBumped called on aborted txn")
}
return e.PrevTxnEpoch != e.NextTransaction.Epoch
}
// TxnMustRestartFromBeginning returns true if the previous transaction's writes
// were discarded due to the retry error, meaning that it must restart from the
// beginning.
func (e *TransactionRetryWithProtoRefreshError) TxnMustRestartFromBeginning() bool {
return e.PrevTxnAborted() || e.PrevTxnEpochBumped()
}
// NewTransactionPushError initializes a new TransactionPushError.
func NewTransactionPushError(pusheeTxn roachpb.Transaction) *TransactionPushError {
// Note: this error will cause a txn restart. The error that the client
// receives contains a txn that might have a modified priority.
return &TransactionPushError{PusheeTxn: pusheeTxn}
}
func (e *TransactionPushError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *TransactionPushError) SafeFormatError(p errors.Printer) (next error) {
p.Printf("failed to push %v", e.PusheeTxn)
return nil
}
func (*TransactionPushError) canRestartTransaction() TransactionRestart {
return TransactionRestart_IMMEDIATE
}
// Type is part of the ErrorDetailInterface.
func (e *TransactionPushError) Type() ErrorDetailType {
return TransactionPushErrType
}
var _ ErrorDetailInterface = &TransactionPushError{}
var _ transactionRestartError = &TransactionPushError{}
// NewTransactionRetryError initializes a new TransactionRetryError.
func NewTransactionRetryError(
reason TransactionRetryReason, extraMsg redact.RedactableString, opts ...RetryErrOption,
) *TransactionRetryError {
options := retryErrOptions{}
for _, o := range opts {
o.apply(&options)
}
return &TransactionRetryError{
Reason: reason,
ExtraMsg: extraMsg.StripMarkers(),
ExtraMsgRedactable: extraMsg,
ConflictingTxn: options.conflictingTxn,
}
}
func (e *TransactionRetryError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *TransactionRetryError) SafeFormatError(p errors.Printer) (next error) {
var msg redact.RedactableString = ""
if e.ExtraMsgRedactable != "" {
msg = redact.Sprintf(" - %s", e.ExtraMsgRedactable)
} else if e.ExtraMsg != "" {
msg = redact.Sprintf(" - %s", e.ExtraMsg)
}
if e.ConflictingTxn != nil {
msg = redact.Sprintf(" %s - conflicting txn: meta={%s}", msg, e.ConflictingTxn.String())
}
p.Printf("TransactionRetryError: retry txn (%s%s)", redact.SafeString(TransactionRetryReason_name[int32(e.Reason)]), msg)
return nil
}
// Type is part of the ErrorDetailInterface.
func (e *TransactionRetryError) Type() ErrorDetailType {
return TransactionRetryErrType
}
func (*TransactionRetryError) canRestartTransaction() TransactionRestart {
return TransactionRestart_IMMEDIATE
}
var _ ErrorDetailInterface = &TransactionRetryError{}
var _ transactionRestartError = &TransactionRetryError{}
// NewTransactionStatusError initializes a new TransactionStatusError with
// the given message and reason.
func NewTransactionStatusError(
reason TransactionStatusError_Reason, msg redact.RedactableString,
) *TransactionStatusError {
return &TransactionStatusError{
Msg: msg.StripMarkers(),
MsgRedactable: msg,
Reason: reason,
}
}
func (e *TransactionStatusError) Error() string {
return redact.Sprint(e).StripMarkers()
}
// Type is part of the ErrorDetailInterface.
func (e *TransactionStatusError) Type() ErrorDetailType {
return TransactionStatusErrType
}
func (e *TransactionStatusError) SafeFormatError(p errors.Printer) (next error) {
if e.MsgRedactable != "" {
p.Printf("TransactionStatusError: %s (%s)", e.MsgRedactable, redact.Safe(e.Reason))
} else {
p.Printf("TransactionStatusError: %s (%s)", e.Msg, redact.Safe(e.Reason))
}
return nil
}
var _ ErrorDetailInterface = &TransactionStatusError{}
func (e *LockConflictError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *LockConflictError) SafeFormatError(p errors.Printer) (next error) {
e.printError(p)
return nil
}
func (e *LockConflictError) printError(buf Printer) {
printConflictingLocks(buf, e.Locks)
}
// Type is part of the ErrorDetailInterface.
func (e *LockConflictError) Type() ErrorDetailType {
return LockConflictErrType
}
var _ ErrorDetailInterface = &LockConflictError{}
func (e *WriteIntentError) Error() string {
return redact.Sprint(e).StripMarkers()
}
func (e *WriteIntentError) SafeFormatError(p errors.Printer) (next error) {
e.printError(p)
return nil
}
func (e *WriteIntentError) printError(buf Printer) {
printConflictingLocks(buf, e.Locks)
switch e.Reason {
case WriteIntentError_REASON_UNSPECIFIED:
// Nothing to say.
case WriteIntentError_REASON_WAIT_POLICY:
buf.Printf(" [reason=wait_policy]")
case WriteIntentError_REASON_LOCK_TIMEOUT:
buf.Printf(" [reason=lock_timeout]")
case WriteIntentError_REASON_LOCK_WAIT_QUEUE_MAX_LENGTH_EXCEEDED:
buf.Printf(" [reason=lock_wait_queue_max_length_exceeded]")
default:
// Could panic, better to silently ignore in case new reasons are added.
}
}
// Type is part of the ErrorDetailInterface.
func (e *WriteIntentError) Type() ErrorDetailType {
return WriteIntentErrType
}
var _ ErrorDetailInterface = &WriteIntentError{}
func printConflictingLocks(buf Printer, locks []roachpb.Lock) {
buf.Printf("conflicting locks on ")
// If we have a lot of locks, we only want to show the first and the last.
const maxBegin = 5
const maxEnd = 5
var begin, end []roachpb.Lock
if len(locks) <= maxBegin+maxEnd {
begin = locks
} else {
begin = locks[0:maxBegin]
end = locks[len(locks)-maxEnd:]
}
for i := range begin {
if i > 0 {
buf.Printf(", ")
}
buf.Print(begin[i].Key)
}
if end != nil {
buf.Printf(" ... ")
for i := range end {
if i > 0 {
buf.Printf(", ")
}
buf.Print(end[i].Key)
}
}