Skip to content

Commit a7e3c2c

Browse files
committed
* Added config.AutoRetry() flag with true value by default. config.AutoRetry() affects how to errors handle in sub-clients calls.
* Added `config.WithNoAutoRetry` for disabling auto-retry on errors in sub-clients calls * Refactored `internal/lazy` package (supported check `config.AutoRetry()`, removed all error wrappings with stacktrace)
1 parent e063da0 commit a7e3c2c

File tree

13 files changed

+116
-91
lines changed

13 files changed

+116
-91
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
* Fixed re-opening case after close lazy-initialized clients
22
* Removed dependency of call context for initializing lazy table client
3+
* Added `config.AutoRetry()` flag with `true` value by default. `config.AutoRetry()` affects how to errors handle in sub-clients calls.
4+
* Added `config.WithNoAutoRetry` for disabling auto-retry on errors in sub-clients calls
5+
* Refactored `internal/lazy` package (supported check `config.AutoRetry()`, removed all error wrappings with stacktrace)
36

47
## v3.23.0
58
* Added `WithTLSConfig` option for redefine TLS config

config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ func WithOperationCancelAfter(operationCancelAfter time.Duration) Option {
208208
}
209209
}
210210

211+
// WithNoAutoRetry disable auto-retry calls from YDB sub-clients
212+
func WithNoAutoRetry() Option {
213+
return func(c *Config) {
214+
config.SetAutoRetry(&c.Common, false)
215+
}
216+
}
217+
211218
// WithPanicCallback applies panic callback to config
212219
func WithPanicCallback(panicCallback func(e interface{})) Option {
213220
return func(c *Config) {

internal/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import (
77
type Common struct {
88
operationTimeout time.Duration
99
operationCancelAfter time.Duration
10+
disableAutoRetry bool
1011

1112
panicCallback func(e interface{})
1213
}
1314

15+
// AutoRetry defines auto-retry flag
16+
func (c *Common) AutoRetry() bool {
17+
return !c.disableAutoRetry
18+
}
19+
1420
// PanicCallback returns user-defined panic callback
1521
// If nil - panic callback not defined
1622
func (c *Common) PanicCallback() func(e interface{}) {
@@ -59,3 +65,8 @@ func SetOperationCancelAfter(c *Common, operationCancelAfter time.Duration) {
5965
func SetPanicCallback(c *Common, panicCallback func(e interface{})) {
6066
c.panicCallback = panicCallback
6167
}
68+
69+
// SetAutoRetry affects on AutoRetry() flag
70+
func SetAutoRetry(c *Common, autoRetry bool) {
71+
c.disableAutoRetry = !autoRetry
72+
}

internal/coordination/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ type client struct {
2121
service Ydb_Coordination_V1.CoordinationServiceClient
2222
}
2323

24-
func New(cc grpc.ClientConnInterface, options []config.Option) coordination.Client {
24+
func New(cc grpc.ClientConnInterface, config config.Config) coordination.Client {
2525
return &client{
26-
config: config.New(options...),
26+
config: config,
2727
service: Ydb_Coordination_V1.NewCoordinationServiceClient(cc),
2828
}
2929
}

internal/lazy/coordiantion.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,46 @@ import (
88
builder "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination"
99
"github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination/config"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/database"
11-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1211
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
1413
)
1514

1615
type lazyCoordination struct {
17-
db database.Connection
18-
options []config.Option
19-
c coordination.Client
20-
m sync.Mutex
16+
db database.Connection
17+
config config.Config
18+
c coordination.Client
19+
m sync.Mutex
2120
}
2221

2322
func Coordination(db database.Connection, options []config.Option) coordination.Client {
2423
return &lazyCoordination{
25-
db: db,
26-
options: options,
24+
db: db,
25+
config: config.New(options...),
2726
}
2827
}
2928

3029
func (c *lazyCoordination) CreateNode(ctx context.Context, path string, config coordination.NodeConfig) (err error) {
30+
if !c.config.AutoRetry() {
31+
return c.client().CreateNode(ctx, path, config)
32+
}
3133
return retry.Retry(ctx, func(ctx context.Context) (err error) {
3234
return c.client().CreateNode(ctx, path, config)
3335
})
3436
}
3537

3638
func (c *lazyCoordination) AlterNode(ctx context.Context, path string, config coordination.NodeConfig) (err error) {
39+
if !c.config.AutoRetry() {
40+
return c.client().AlterNode(ctx, path, config)
41+
}
3742
return retry.Retry(ctx, func(ctx context.Context) (err error) {
3843
return c.client().AlterNode(ctx, path, config)
3944
})
4045
}
4146

4247
func (c *lazyCoordination) DropNode(ctx context.Context, path string) (err error) {
48+
if !c.config.AutoRetry() {
49+
return c.client().DropNode(ctx, path)
50+
}
4351
return retry.Retry(ctx, func(ctx context.Context) (err error) {
4452
return c.client().DropNode(ctx, path)
4553
})
@@ -53,11 +61,14 @@ func (c *lazyCoordination) DescribeNode(
5361
config *coordination.NodeConfig,
5462
err error,
5563
) {
64+
if !c.config.AutoRetry() {
65+
return c.client().DescribeNode(ctx, path)
66+
}
5667
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
5768
entry, config, err = c.client().DescribeNode(ctx, path)
58-
return xerrors.WithStackTrace(err)
69+
return err
5970
})
60-
return entry, config, xerrors.WithStackTrace(err)
71+
return entry, config, err
6172
}
6273

6374
func (c *lazyCoordination) Close(ctx context.Context) (err error) {
@@ -66,18 +77,14 @@ func (c *lazyCoordination) Close(ctx context.Context) (err error) {
6677
if c.c == nil {
6778
return nil
6879
}
69-
err = c.c.Close(ctx)
70-
if err != nil {
71-
return xerrors.WithStackTrace(err)
72-
}
73-
return nil
80+
return c.c.Close(ctx)
7481
}
7582

7683
func (c *lazyCoordination) client() coordination.Client {
7784
c.m.Lock()
7885
defer c.m.Unlock()
7986
if c.c == nil {
80-
c.c = builder.New(c.db, c.options)
87+
c.c = builder.New(c.db, c.config)
8188
}
8289
return c.c
8390
}

internal/lazy/ratelimiter.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ import (
88
builder "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter"
99
"github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/config"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/options"
11-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1211
"github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
1413
)
1514

1615
type lazyRatelimiter struct {
17-
db database.Connection
18-
options []config.Option
19-
c ratelimiter.Client
20-
m sync.Mutex
16+
db database.Connection
17+
config config.Config
18+
c ratelimiter.Client
19+
m sync.Mutex
2120
}
2221

2322
func Ratelimiter(db database.Connection, options []config.Option) ratelimiter.Client {
2423
return &lazyRatelimiter{
25-
db: db,
26-
options: options,
24+
db: db,
25+
config: config.New(options...),
2726
}
2827
}
2928

@@ -33,18 +32,17 @@ func (r *lazyRatelimiter) Close(ctx context.Context) (err error) {
3332
if r.c == nil {
3433
return nil
3534
}
36-
err = r.c.Close(ctx)
37-
if err != nil {
38-
return xerrors.WithStackTrace(err)
39-
}
40-
return nil
35+
return r.c.Close(ctx)
4136
}
4237

4338
func (r *lazyRatelimiter) CreateResource(
4439
ctx context.Context,
4540
coordinationNodePath string,
4641
resource ratelimiter.Resource,
4742
) (err error) {
43+
if !r.config.AutoRetry() {
44+
return r.client().CreateResource(ctx, coordinationNodePath, resource)
45+
}
4846
return retry.Retry(ctx, func(ctx context.Context) (err error) {
4947
return r.client().CreateResource(ctx, coordinationNodePath, resource)
5048
})
@@ -55,6 +53,9 @@ func (r *lazyRatelimiter) AlterResource(
5553
coordinationNodePath string,
5654
resource ratelimiter.Resource,
5755
) (err error) {
56+
if !r.config.AutoRetry() {
57+
return r.client().AlterResource(ctx, coordinationNodePath, resource)
58+
}
5859
return retry.Retry(ctx, func(ctx context.Context) (err error) {
5960
return r.client().AlterResource(ctx, coordinationNodePath, resource)
6061
})
@@ -65,6 +66,9 @@ func (r *lazyRatelimiter) DropResource(
6566
coordinationNodePath string,
6667
resourcePath string,
6768
) (err error) {
69+
if !r.config.AutoRetry() {
70+
return r.client().DropResource(ctx, coordinationNodePath, resourcePath)
71+
}
6872
return retry.Retry(ctx, func(ctx context.Context) (err error) {
6973
return r.client().DropResource(ctx, coordinationNodePath, resourcePath)
7074
})
@@ -76,23 +80,29 @@ func (r *lazyRatelimiter) ListResource(
7680
resourcePath string,
7781
recursive bool,
7882
) (paths []string, err error) {
83+
if !r.config.AutoRetry() {
84+
return r.client().ListResource(ctx, coordinationNodePath, resourcePath, recursive)
85+
}
7986
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
8087
paths, err = r.client().ListResource(ctx, coordinationNodePath, resourcePath, recursive)
81-
return xerrors.WithStackTrace(err)
88+
return err
8289
})
83-
return paths, xerrors.WithStackTrace(err)
90+
return paths, err
8491
}
8592

8693
func (r *lazyRatelimiter) DescribeResource(
8794
ctx context.Context,
8895
coordinationNodePath string,
8996
resourcePath string,
9097
) (resource *ratelimiter.Resource, err error) {
98+
if !r.config.AutoRetry() {
99+
return r.client().DescribeResource(ctx, coordinationNodePath, resourcePath)
100+
}
91101
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
92102
resource, err = r.client().DescribeResource(ctx, coordinationNodePath, resourcePath)
93-
return xerrors.WithStackTrace(err)
103+
return err
94104
})
95-
return resource, xerrors.WithStackTrace(err)
105+
return resource, err
96106
}
97107

98108
func (r *lazyRatelimiter) AcquireResource(
@@ -102,6 +112,9 @@ func (r *lazyRatelimiter) AcquireResource(
102112
amount uint64,
103113
opts ...options.AcquireOption,
104114
) (err error) {
115+
if !r.config.AutoRetry() {
116+
return r.client().AcquireResource(ctx, coordinationNodePath, resourcePath, amount, opts...)
117+
}
105118
return retry.Retry(ctx, func(ctx context.Context) (err error) {
106119
return r.client().AcquireResource(ctx, coordinationNodePath, resourcePath, amount, opts...)
107120
})
@@ -111,7 +124,7 @@ func (r *lazyRatelimiter) client() ratelimiter.Client {
111124
r.m.Lock()
112125
defer r.m.Unlock()
113126
if r.c == nil {
114-
r.c = builder.New(r.db, r.options)
127+
r.c = builder.New(r.db, r.config)
115128
}
116129
return r.c
117130
}

internal/lazy/scheme.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ import (
77
"github.com/ydb-platform/ydb-go-sdk/v3/internal/database"
88
builder "github.com/ydb-platform/ydb-go-sdk/v3/internal/scheme"
99
"github.com/ydb-platform/ydb-go-sdk/v3/internal/scheme/config"
10-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1110
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
1211
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
1312
)
1413

1514
type lazyScheme struct {
16-
db database.Connection
17-
options []config.Option
18-
c scheme.Client
19-
m sync.Mutex
15+
db database.Connection
16+
config config.Config
17+
c scheme.Client
18+
m sync.Mutex
2019
}
2120

2221
func Scheme(db database.Connection, options []config.Option) scheme.Client {
2322
return &lazyScheme{
24-
db: db,
25-
options: options,
23+
db: db,
24+
config: config.New(options...),
2625
}
2726
}
2827

@@ -42,19 +41,15 @@ func (s *lazyScheme) Close(ctx context.Context) (err error) {
4241
if s.c == nil {
4342
return nil
4443
}
45-
err = s.c.Close(ctx)
46-
if err != nil {
47-
return xerrors.WithStackTrace(err)
48-
}
49-
return nil
44+
return s.c.Close(ctx)
5045
}
5146

5247
func (s *lazyScheme) DescribePath(ctx context.Context, path string) (e scheme.Entry, err error) {
5348
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
5449
e, err = s.client().DescribePath(ctx, path)
55-
return xerrors.WithStackTrace(err)
50+
return err
5651
}, retry.WithIdempotent(true))
57-
return e, xerrors.WithStackTrace(err)
52+
return e, err
5853
}
5954

6055
func (s *lazyScheme) MakeDirectory(ctx context.Context, path string) (err error) {
@@ -66,9 +61,9 @@ func (s *lazyScheme) MakeDirectory(ctx context.Context, path string) (err error)
6661
func (s *lazyScheme) ListDirectory(ctx context.Context, path string) (d scheme.Directory, err error) {
6762
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
6863
d, err = s.client().ListDirectory(ctx, path)
69-
return xerrors.WithStackTrace(err)
64+
return err
7065
}, retry.WithIdempotent(true))
71-
return d, xerrors.WithStackTrace(err)
66+
return d, err
7267
}
7368

7469
func (s *lazyScheme) RemoveDirectory(ctx context.Context, path string) (err error) {
@@ -81,7 +76,7 @@ func (s *lazyScheme) client() scheme.Client {
8176
s.m.Lock()
8277
defer s.m.Unlock()
8378
if s.c == nil {
84-
s.c = builder.New(s.db, s.options)
79+
s.c = builder.New(s.db, s.config)
8580
}
8681
return s.c
8782
}

0 commit comments

Comments
 (0)