Skip to content

Commit 41cf01f

Browse files
authored
Merge pull request #180 from ydb-platform/remode-transport-error-code
simplify internal operation and transport errors
2 parents d3e278d + d5cdd04 commit 41cf01f

22 files changed

+471
-639
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Refactored internal operation and transport errors
2+
13
## v3.16.8
24
* Added `config.ExcludeGRPCCodesForPessimization()` opttion for exclude some grpc codes from pessimization rules
35
* Refactored pessimization node conditions

config/config.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
"time"
99

1010
"google.golang.org/grpc"
11-
"google.golang.org/grpc/codes"
11+
grpcCodes "google.golang.org/grpc/codes"
1212

1313
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer"
16-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
1716
"github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
1817
builder "github.com/ydb-platform/ydb-go-sdk/v3/internal/net"
1918
"github.com/ydb-platform/ydb-go-sdk/v3/internal/resolver"
@@ -80,7 +79,7 @@ type Config interface {
8079
UseDNSResolver() bool
8180

8281
// ExcludeGRPCCodesForPessimization defines grpc codes for exclude its from pessimization trigger
83-
ExcludeGRPCCodesForPessimization() []errors.TransportErrorCode
82+
ExcludeGRPCCodesForPessimization() []grpcCodes.Code
8483
}
8584

8685
// Config contains driver configuration options.
@@ -97,14 +96,14 @@ type config struct {
9796
database string
9897
requestsType string
9998
userAgent string
100-
excludeGRPCCodesForPessimization []errors.TransportErrorCode
99+
excludeGRPCCodesForPessimization []grpcCodes.Code
101100
grpcOptions []grpc.DialOption
102101
credentials credentials.Credentials
103102
tlsConfig *tls.Config
104103
meta meta.Meta
105104
}
106105

107-
func (c *config) ExcludeGRPCCodesForPessimization() []errors.TransportErrorCode {
106+
func (c *config) ExcludeGRPCCodesForPessimization() []grpcCodes.Code {
108107
return c.excludeGRPCCodesForPessimization
109108
}
110109

@@ -275,16 +274,11 @@ func WithGrpcOptions(option ...grpc.DialOption) Option {
275274
}
276275
}
277276

278-
func ExcludeGRPCCodesForPessimization(codes ...codes.Code) Option {
277+
func ExcludeGRPCCodesForPessimization(codes ...grpcCodes.Code) Option {
279278
return func(c *config) {
280279
c.excludeGRPCCodesForPessimization = append(
281280
c.excludeGRPCCodesForPessimization,
282-
func() (teCodes []errors.TransportErrorCode) {
283-
for _, c := range codes {
284-
teCodes = append(teCodes, errors.FromGRPCCode(c))
285-
}
286-
return teCodes
287-
}()...,
281+
codes...,
288282
)
289283
}
290284
}

errors.go

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,62 @@
11
package ydb
22

33
import (
4-
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Issue"
4+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
5+
grpcCodes "google.golang.org/grpc/codes"
56

67
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
78
ratelimiterErrors "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/errors"
89
"github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
910
)
1011

11-
func IterateByIssues(err error, it func(message string, code uint32, severity uint32)) {
12-
var o *errors.OperationError
13-
if !errors.As(err, &o) {
14-
return
15-
}
16-
issues := o.Issues()
17-
iterate(issues, it)
18-
}
19-
20-
func iterate(issues []*Ydb_Issue.IssueMessage, it func(message string, code uint32, severity uint32)) {
21-
for _, issue := range issues {
22-
it(issue.GetMessage(), issue.GetIssueCode(), issue.GetSeverity())
23-
iterate(issue.GetIssues(), it)
24-
}
12+
func IterateByIssues(err error, it func(message string, code Ydb.StatusIds_StatusCode, severity uint32)) {
13+
errors.IterateByIssues(err, it)
2514
}
2615

2716
func IsTimeoutError(err error) bool {
2817
return errors.IsTimeoutError(err)
2918
}
3019

31-
func IsTransportError(err error, codes ...int32) bool {
32-
return errors.IsTransportError(
33-
err,
34-
func() (cc []errors.TransportErrorCode) {
35-
for _, code := range codes {
36-
cc = append(cc, errors.TransportErrorCode(code))
37-
}
38-
return cc
39-
}()...,
40-
)
41-
}
42-
43-
func IsTransportErrorCancelled(err error) bool {
44-
return IsTransportError(err, int32(errors.TransportErrorCanceled))
20+
func IsTransportError(err error, codes ...grpcCodes.Code) bool {
21+
return errors.IsTransportError(err, codes...)
4522
}
4623

47-
func IsTransportErrorResourceExhausted(err error) bool {
48-
return IsTransportError(err, int32(errors.TransportErrorResourceExhausted))
49-
}
50-
51-
type Error interface {
52-
error
53-
54-
Code() int32
55-
Name() string
56-
}
24+
type Error errors.Error
5725

58-
func TransportErrorDescription(err error) Error {
59-
var t *errors.TransportError
60-
if errors.As(err, &t) {
61-
return t
62-
}
63-
return nil
26+
func TransportError(err error) Error {
27+
return errors.TransportError(err)
6428
}
6529

6630
func IsYdbError(err error) bool {
67-
return IsTransportError(err) || IsOperationError(err)
31+
return errors.IsYdb(err)
6832
}
6933

70-
func IsOperationError(err error, codes ...int32) bool {
71-
return errors.IsOpError(
72-
err,
73-
func() (cc []errors.StatusCode) {
74-
for _, code := range codes {
75-
cc = append(cc, errors.StatusCode(code))
76-
}
77-
return cc
78-
}()...,
79-
)
34+
func IsOperationError(err error, codes ...Ydb.StatusIds_StatusCode) bool {
35+
return errors.IsOperationError(err, codes...)
8036
}
8137

82-
func OperationErrorDescription(err error) Error {
83-
var o *errors.OperationError
84-
if errors.As(err, &o) {
85-
return o
86-
}
87-
return nil
38+
func OperationError(err error) Error {
39+
return errors.OperationError(err)
8840
}
8941

9042
func IsOperationErrorOverloaded(err error) bool {
91-
return IsOperationError(err, int32(errors.StatusOverloaded))
43+
return IsOperationError(err, Ydb.StatusIds_OVERLOADED)
9244
}
9345

9446
func IsOperationErrorUnavailable(err error) bool {
95-
return IsOperationError(err, int32(errors.StatusUnavailable))
47+
return IsOperationError(err, Ydb.StatusIds_UNAVAILABLE)
9648
}
9749

9850
func IsOperationErrorAlreadyExistsError(err error) bool {
99-
return IsOperationError(err, int32(errors.StatusAlreadyExists))
51+
return IsOperationError(err, Ydb.StatusIds_ALREADY_EXISTS)
10052
}
10153

10254
func IsOperationErrorNotFoundError(err error) bool {
103-
return IsOperationError(err, int32(errors.StatusNotFound))
55+
return IsOperationError(err, Ydb.StatusIds_NOT_FOUND)
10456
}
10557

10658
func IsOperationErrorSchemeError(err error) bool {
107-
return IsOperationError(err, int32(errors.StatusSchemeError))
59+
return IsOperationError(err, Ydb.StatusIds_SCHEME_ERROR)
10860
}
10961

11062
func IsRatelimiterAcquireError(err error) bool {

internal/conn/conn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (c *conn) Invoke(
355355
return errors.WithStackTrace(
356356
errors.FromGRPCError(
357357
err,
358-
errors.WithTEAddress(c.Address()),
358+
errors.WithAddress(c.Address()),
359359
),
360360
)
361361
}
@@ -375,7 +375,7 @@ func (c *conn) Invoke(
375375
case o.GetOperation().GetStatus() != Ydb.StatusIds_SUCCESS:
376376
return errors.WithStackTrace(
377377
errors.NewOpError(
378-
errors.WithOEOperation(
378+
errors.FromOperation(
379379
o.GetOperation(),
380380
),
381381
),
@@ -435,7 +435,7 @@ func (c *conn) NewStream(
435435
return s, errors.WithStackTrace(
436436
errors.FromGRPCError(
437437
err,
438-
errors.WithTEAddress(c.Address()),
438+
errors.WithAddress(c.Address()),
439439
),
440440
)
441441
}

internal/conn/grpc_client_stream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (s *grpcClientStream) CloseSend() (err error) {
2727
return errors.WithStackTrace(
2828
errors.FromGRPCError(
2929
err,
30-
errors.WithTEAddress(s.c.Address()),
30+
errors.WithAddress(s.c.Address()),
3131
),
3232
)
3333
}
@@ -48,7 +48,7 @@ func (s *grpcClientStream) SendMsg(m interface{}) (err error) {
4848
return errors.WithStackTrace(
4949
errors.FromGRPCError(
5050
err,
51-
errors.WithTEAddress(s.c.Address()),
51+
errors.WithAddress(s.c.Address()),
5252
),
5353
)
5454
}
@@ -77,7 +77,7 @@ func (s *grpcClientStream) RecvMsg(m interface{}) (err error) {
7777
return errors.WithStackTrace(
7878
errors.FromGRPCError(
7979
err,
80-
errors.WithTEAddress(s.c.Address()),
80+
errors.WithAddress(s.c.Address()),
8181
),
8282
)
8383
}
@@ -89,7 +89,7 @@ func (s *grpcClientStream) RecvMsg(m interface{}) (err error) {
8989
if s := operation.GetStatus(); s != Ydb.StatusIds_SUCCESS {
9090
return errors.WithStackTrace(
9191
errors.NewOpError(
92-
errors.WithOEOperation(
92+
errors.FromOperation(
9393
operation,
9494
),
9595
),

internal/errors/check.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package errors
2+
3+
// Check returns retry mode for err.
4+
func Check(err error) (
5+
statusCode int64,
6+
operationStatus OperationStatus,
7+
backoff BackoffType,
8+
deleteSession bool,
9+
) {
10+
var te *transportError
11+
var oe *operationError
12+
var re *RetryableError
13+
switch {
14+
case As(err, &te):
15+
return int64(te.code),
16+
te.OperationStatus(),
17+
te.BackoffType(),
18+
te.MustDeleteSession()
19+
case As(err, &oe):
20+
return int64(oe.code),
21+
oe.OperationStatus(),
22+
oe.BackoffType(),
23+
oe.MustDeleteSession()
24+
case As(err, &re):
25+
return -1,
26+
OperationNotFinished,
27+
re.BackoffType,
28+
re.MustDeleteSession
29+
default:
30+
return -1,
31+
OperationFinished, // it's finished, not need any retry attempts
32+
BackoffTypeNoBackoff,
33+
false
34+
}
35+
}

internal/errors/errors.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
9+
grpcCodes "google.golang.org/grpc/codes"
10+
11+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
812
)
913

14+
type Error interface {
15+
error
16+
17+
Code() int32
18+
Name() string
19+
}
20+
1021
func IsTimeoutError(err error) bool {
1122
switch {
1223
case
13-
IsOpError(
24+
IsOperationError(
1425
err,
15-
StatusTimeout,
16-
StatusCancelled,
26+
Ydb.StatusIds_TIMEOUT,
27+
Ydb.StatusIds_CANCELLED,
1728
),
18-
IsTransportError(err, TransportErrorCanceled, TransportErrorDeadlineExceeded),
29+
IsTransportError(err, grpcCodes.Canceled, grpcCodes.DeadlineExceeded),
1930
Is(
2031
err,
2132
context.DeadlineExceeded,

internal/errors/issues.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package errors
22

33
import (
44
"bytes"
5+
"errors"
56

7+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
68
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Issue"
79
)
810

@@ -59,3 +61,21 @@ func (it IssueIterator) Get(i int) (issue Issue, nested IssueIterator) {
5961
Severity: x.GetSeverity(),
6062
}, nested
6163
}
64+
65+
func IterateByIssues(err error, it func(message string, code Ydb.StatusIds_StatusCode, severity uint32)) {
66+
var o *operationError
67+
if !errors.As(err, &o) {
68+
return
69+
}
70+
iterate(o.Issues(), it)
71+
}
72+
73+
func iterate(
74+
issues []*Ydb_Issue.IssueMessage,
75+
it func(message string, code Ydb.StatusIds_StatusCode, severity uint32),
76+
) {
77+
for _, issue := range issues {
78+
it(issue.GetMessage(), Ydb.StatusIds_StatusCode(issue.GetIssueCode()), issue.GetSeverity())
79+
iterate(issue.GetIssues(), it)
80+
}
81+
}

0 commit comments

Comments
 (0)