Skip to content

Commit 8c4d18a

Browse files
committed
retry.WithStackTrace
1 parent fcdcdbb commit 8c4d18a

File tree

10 files changed

+57
-45
lines changed

10 files changed

+57
-45
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ jobs:
5353
GO: ${{ matrix.go-version }}
5454
YDB_CONNECTION_STRING: grpcs://localhost:2135/?database=/local
5555
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
56-
YDB_ANONYMOUS_CREDENTIALS: 1
5756
YDB_SHUTDOWN_URLS: http://localhost:8765/actors/kqp_proxy?force_shutdown=all
5857
HIDE_APPLICATION_OUTPUT: 1
5958
runs-on: ${{ matrix.os }}
@@ -97,7 +96,6 @@ jobs:
9796
GO: ${{ matrix.go-version }}
9897
YDB_CONNECTION_STRING: grpcs://localhost:2135/?database=/local
9998
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
100-
YDB_ANONYMOUS_CREDENTIALS: 1
10199
runs-on: ${{ matrix.os }}
102100
steps:
103101
- name: Install Go
@@ -140,7 +138,6 @@ jobs:
140138
GO: ${{ matrix.go-version }}
141139
YDB_CONNECTION_STRING: grpcs://localhost:2135/?database=/local
142140
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
143-
YDB_ANONYMOUS_CREDENTIALS: 1
144141
runs-on: ${{ matrix.os }}
145142
steps:
146143
- name: Install Go
@@ -182,7 +179,6 @@ jobs:
182179
GO: ${{ matrix.go-version }}
183180
YDB_CONNECTION_STRING: grpcs://localhost:2135/?database=/local
184181
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
185-
YDB_ANONYMOUS_CREDENTIALS: 1
186182
runs-on: ${{ matrix.os }}
187183
steps:
188184
- name: Install Go
@@ -224,7 +220,6 @@ jobs:
224220
GO: ${{ matrix.go-version }}
225221
YDB_CONNECTION_STRING: grpcs://localhost:2135/?database=/local
226222
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
227-
YDB_ANONYMOUS_CREDENTIALS: 1
228223
runs-on: ${{ matrix.os }}
229224
steps:
230225
- name: Install Go

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* Refactored initialization of coordination, ratelimiter, scheme, scripting and table clients from `internal/lazy` package to each client initialization with `sync.Once`
22
* Removed `internal/lazy` package
3+
* Added retry option `retry.WithStackTrace` for wrapping errors with stacktrace
34

45
## v3.24.0
56
* Fixed re-opening case after close lazy-initialized clients

internal/coordination/client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func (c *Client) CreateNode(ctx context.Context, path string, config coordinatio
4343
if !c.config.AutoRetry() {
4444
return xerrors.WithStackTrace(c.createNode(ctx, path, config))
4545
}
46-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
46+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
4747
return xerrors.WithStackTrace(c.createNode(ctx, path, config))
48-
}))
48+
}, retry.WithStackTrace())
4949
}
5050

5151
func (c *Client) createNode(ctx context.Context, path string, config coordination.NodeConfig) (err error) {
@@ -79,9 +79,9 @@ func (c *Client) AlterNode(ctx context.Context, path string, config coordination
7979
if !c.config.AutoRetry() {
8080
return xerrors.WithStackTrace(c.alterNode(ctx, path, config))
8181
}
82-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
82+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
8383
return xerrors.WithStackTrace(c.alterNode(ctx, path, config))
84-
}))
84+
}, retry.WithStackTrace())
8585
}
8686

8787
func (c *Client) alterNode(ctx context.Context, path string, config coordination.NodeConfig) (err error) {
@@ -115,9 +115,9 @@ func (c *Client) DropNode(ctx context.Context, path string) (err error) {
115115
if !c.config.AutoRetry() {
116116
return xerrors.WithStackTrace(c.dropNode(ctx, path))
117117
}
118-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
118+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
119119
return xerrors.WithStackTrace(c.dropNode(ctx, path))
120-
}))
120+
}, retry.WithStackTrace())
121121
}
122122

123123
func (c *Client) dropNode(ctx context.Context, path string) (err error) {
@@ -152,10 +152,10 @@ func (c *Client) DescribeNode(
152152
entry, config, err = c.describeNode(ctx, path)
153153
return entry, config, xerrors.WithStackTrace(err)
154154
}
155-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
155+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
156156
entry, config, err = c.describeNode(ctx, path)
157157
return xerrors.WithStackTrace(err)
158-
}))
158+
}, retry.WithStackTrace())
159159
return
160160
}
161161

internal/ratelimiter/client.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func (c *Client) CreateResource(
5353
if !c.config.AutoRetry() {
5454
return xerrors.WithStackTrace(c.createResource(ctx, coordinationNodePath, resource))
5555
}
56-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
56+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
5757
return c.createResource(ctx, coordinationNodePath, resource)
58-
}))
58+
}, retry.WithStackTrace())
5959
}
6060

6161
func (c *Client) createResource(
@@ -95,9 +95,9 @@ func (c *Client) AlterResource(
9595
if !c.config.AutoRetry() {
9696
return xerrors.WithStackTrace(c.alterResource(ctx, coordinationNodePath, resource))
9797
}
98-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
98+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
9999
return xerrors.WithStackTrace(c.alterResource(ctx, coordinationNodePath, resource))
100-
}))
100+
}, retry.WithStackTrace())
101101
}
102102

103103
func (c *Client) alterResource(
@@ -137,9 +137,9 @@ func (c *Client) DropResource(
137137
if !c.config.AutoRetry() {
138138
return xerrors.WithStackTrace(c.dropResource(ctx, coordinationNodePath, resourcePath))
139139
}
140-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
140+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
141141
return xerrors.WithStackTrace(c.dropResource(ctx, coordinationNodePath, resourcePath))
142-
}))
142+
}, retry.WithStackTrace())
143143
}
144144

145145
func (c *Client) dropResource(
@@ -173,10 +173,10 @@ func (c *Client) ListResource(
173173
list, err = c.listResource(ctx, coordinationNodePath, resourcePath, recursive)
174174
return list, xerrors.WithStackTrace(err)
175175
}
176-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
176+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
177177
list, err = c.listResource(ctx, coordinationNodePath, resourcePath, recursive)
178178
return xerrors.WithStackTrace(err)
179-
}, retry.WithIdempotent(true)))
179+
}, retry.WithIdempotent(true), retry.WithStackTrace())
180180
return
181181
}
182182

@@ -223,10 +223,10 @@ func (c *Client) DescribeResource(
223223
resource, err = c.describeResource(ctx, coordinationNodePath, resourcePath)
224224
return resource, xerrors.WithStackTrace(err)
225225
}
226-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
226+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
227227
resource, err = c.describeResource(ctx, coordinationNodePath, resourcePath)
228228
return xerrors.WithStackTrace(err)
229-
}, retry.WithIdempotent(true)))
229+
}, retry.WithIdempotent(true), retry.WithStackTrace())
230230
return
231231
}
232232

@@ -286,9 +286,9 @@ func (c *Client) AcquireResource(
286286
if !c.config.AutoRetry() {
287287
return xerrors.WithStackTrace(c.acquireResource(ctx, coordinationNodePath, resourcePath, amount, opts...))
288288
}
289-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
289+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
290290
return xerrors.WithStackTrace(c.acquireResource(ctx, coordinationNodePath, resourcePath, amount, opts...))
291-
}))
291+
}, retry.WithStackTrace())
292292
}
293293

294294
func (c *Client) acquireResource(

internal/scheme/client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func (c *Client) MakeDirectory(ctx context.Context, path string) (err error) {
4646
if !c.config.AutoRetry() {
4747
return xerrors.WithStackTrace(c.makeDirectory(ctx, path))
4848
}
49-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
49+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
5050
return xerrors.WithStackTrace(c.makeDirectory(ctx, path))
51-
}))
51+
}, retry.WithStackTrace())
5252
}
5353

5454
func (c *Client) makeDirectory(ctx context.Context, path string) (err error) {
@@ -74,9 +74,9 @@ func (c *Client) RemoveDirectory(ctx context.Context, path string) (err error) {
7474
if !c.config.AutoRetry() {
7575
return xerrors.WithStackTrace(c.removeDirectory(ctx, path))
7676
}
77-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
77+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
7878
return xerrors.WithStackTrace(c.removeDirectory(ctx, path))
79-
}))
79+
}, retry.WithStackTrace())
8080
}
8181

8282
func (c *Client) removeDirectory(ctx context.Context, path string) (err error) {
@@ -103,10 +103,10 @@ func (c *Client) ListDirectory(ctx context.Context, path string) (d scheme.Direc
103103
d, err = c.listDirectory(ctx, path)
104104
return d, xerrors.WithStackTrace(err)
105105
}
106-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
106+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
107107
d, err = c.listDirectory(ctx, path)
108108
return xerrors.WithStackTrace(err)
109-
}, retry.WithIdempotent(true)))
109+
}, retry.WithIdempotent(true), retry.WithStackTrace())
110110
return
111111
}
112112

@@ -150,10 +150,10 @@ func (c *Client) DescribePath(ctx context.Context, path string) (e scheme.Entry,
150150
e, err = c.describePath(ctx, path)
151151
return e, xerrors.WithStackTrace(err)
152152
}
153-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
153+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
154154
e, err = c.describePath(ctx, path)
155155
return xerrors.WithStackTrace(err)
156-
}, retry.WithIdempotent(true)))
156+
}, retry.WithIdempotent(true), retry.WithStackTrace())
157157
return
158158
}
159159

@@ -192,9 +192,9 @@ func (c *Client) ModifyPermissions(ctx context.Context, path string, opts ...sch
192192
if !c.config.AutoRetry() {
193193
return xerrors.WithStackTrace(c.modifyPermissions(ctx, path, opts...))
194194
}
195-
return xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
195+
return retry.Retry(ctx, func(ctx context.Context) (err error) {
196196
return xerrors.WithStackTrace(c.modifyPermissions(ctx, path, opts...))
197-
}))
197+
}, retry.WithStackTrace())
198198
}
199199

200200
func (c *Client) modifyPermissions(ctx context.Context, path string, opts ...scheme.PermissionsOption) (err error) {

internal/scripting/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func (c *Client) Execute(
4848
r, err = c.execute(ctx, query, params)
4949
return r, xerrors.WithStackTrace(err)
5050
}
51-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
51+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
5252
r, err = c.execute(ctx, query, params)
5353
return xerrors.WithStackTrace(err)
54-
}))
54+
}, retry.WithStackTrace())
5555
return
5656
}
5757

@@ -113,10 +113,10 @@ func (c *Client) Explain(
113113
e, err = c.explain(ctx, query, mode)
114114
return e, xerrors.WithStackTrace(err)
115115
}
116-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
116+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
117117
e, err = c.explain(ctx, query, mode)
118118
return xerrors.WithStackTrace(err)
119-
}))
119+
}, retry.WithStackTrace())
120120
return
121121
}
122122

@@ -176,10 +176,10 @@ func (c *Client) StreamExecute(
176176
r, err = c.streamExecute(ctx, query, params)
177177
return r, xerrors.WithStackTrace(err)
178178
}
179-
err = xerrors.WithStackTrace(retry.Retry(ctx, func(ctx context.Context) (err error) {
179+
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
180180
r, err = c.streamExecute(ctx, query, params)
181181
return xerrors.WithStackTrace(err)
182-
}))
182+
}, retry.WithStackTrace())
183183
return
184184
}
185185

retry/retry.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type retryOptions struct {
1919
id string
2020
trace trace.Retry
2121
idempotent bool
22+
stackTrace bool
2223
fastBackoff backoff.Backoff
2324
slowBackoff backoff.Backoff
2425

@@ -34,6 +35,13 @@ func WithID(id string) retryOption {
3435
}
3536
}
3637

38+
// WithStackTrace wraps errors with stacktrace from Retry call
39+
func WithStackTrace() retryOption {
40+
return func(h *retryOptions) {
41+
h.stackTrace = true
42+
}
43+
}
44+
3745
// WithTrace returns trace option
3846
func WithTrace(trace trace.Retry) retryOption {
3947
return func(h *retryOptions) {
@@ -89,6 +97,14 @@ func Retry(ctx context.Context, op retryOperation, opts ...retryOption) (err err
8997
for _, o := range opts {
9098
o(options)
9199
}
100+
defer func() {
101+
if err != nil && options.stackTrace {
102+
err = xerrors.WithStackTrace(
103+
err,
104+
xerrors.WithSkipDepth(2), // 1 - exit from defer, 1 - exit from Retry call
105+
)
106+
}
107+
}()
92108
var (
93109
i int
94110
attempts int

test/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestConnection(t *testing.T) {
6666
ctx,
6767
"", // corner case for check replacement of endpoint+database+secure
6868
ydb.WithConnectionString(os.Getenv("YDB_CONNECTION_STRING")),
69-
ydb.WithAnonymousCredentials(),
69+
ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
7070
ydb.With(
7171
config.WithOperationTimeout(time.Second*2),
7272
config.WithOperationCancelAfter(time.Second*2),

test/discovery_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestDiscovery(t *testing.T) {
5252
db, err := ydb.Open(
5353
ctx,
5454
os.Getenv("YDB_CONNECTION_STRING"),
55-
ydb.WithAnonymousCredentials(),
55+
ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
5656
ydb.With(
5757
config.WithOperationTimeout(time.Second*2),
5858
config.WithOperationCancelAfter(time.Second*2),

test/scripting_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestScripting(t *testing.T) {
2727
db, err := ydb.Open(
2828
ctx,
2929
os.Getenv("YDB_CONNECTION_STRING"),
30-
ydb.WithAnonymousCredentials(),
30+
ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
3131
ydb.With(
3232
config.WithOperationTimeout(time.Second*2),
3333
config.WithOperationCancelAfter(time.Second*2),
@@ -86,7 +86,7 @@ func TestScripting(t *testing.T) {
8686
return fmt.Errorf("unexpected sum: %v", sum)
8787
}
8888
return res.Err()
89-
}, retry.WithIdempotent(true)); err != nil {
89+
}, retry.WithIdempotent(true), retry.WithStackTrace()); err != nil {
9090
t.Fatalf("Execute failed: %v", err)
9191
}
9292
// StreamExecute

0 commit comments

Comments
 (0)