Skip to content

Commit 1b5075f

Browse files
committed
Refactored retry.doOption and retry.doTxOption types
Marked as deprecated `retry.WithDoRetryOptions` and `retry.WithDoTxRetryOptions`
1 parent aebceb5 commit 1b5075f

30 files changed

+365
-238
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Marked as deprecated `retry.WithDoRetryOptions` and `retry.WithDoTxRetryOptions`
12
* Added receiving first result set on construct `internal/table/scanner.NewStream()`
23
* Added experimental package `metrics` with SDK metrics
34
* Fixed redundant trace call for finished `database/sql` transactions

SQL.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ err := retry.Do(context.TODO(), db, func(ctx context.Context, cc *sql.Conn) erro
270270
}
271271
...
272272
return nil // good final of retry operation
273-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
273+
}, retry.WithIdempotent(true))
274274

275275
```
276276

@@ -296,9 +296,7 @@ err := retry.DoTx(context.TODO(), db, func(ctx context.Context, tx *sql.Tx) erro
296296
}
297297
...
298298
return nil // good final of retry tx operation
299-
}, retry.WithDoTxRetryOptions(
300-
retry.WithIdempotent(true),
301-
), retry.WithTxOptions(&sql.TxOptions{
299+
}, retry.WithIdempotent(true), retry.WithTxOptions(&sql.TxOptions{
302300
Isolation: sql.LevelSnapshot,
303301
ReadOnly: true,
304302
}))
@@ -568,7 +566,7 @@ err := retry.Do(context.TODO(), db, func(ctx context.Context, cc *sql.Conn) erro
568566
}
569567
...
570568
return nil // good final of retry operation
571-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
569+
}, retry.WithIdempotent(true))
572570
```
573571
574572
## Troubleshooting <a name="troubleshooting"></a>

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func Example_databaseSQL() {
8686
}
8787
log.Printf("id=%v, myStr='%s'\n", id, myStr)
8888
return nil
89-
}, retry.WithDoTxRetryOptions(retry.WithIdempotent(true)))
89+
}, retry.WithIdempotent(true))
9090
if err != nil {
9191
log.Printf("query failed: %v", err)
9292
}

examples/basic/database_sql/series.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"path"
1010
"time"
1111

12-
"github.com/ydb-platform/ydb-go-sdk/v3"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
1413
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
1514
"github.com/ydb-platform/ydb-go-sdk/v3/table"
@@ -31,7 +30,7 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
3130
}
3231
log.Printf("AST = %s\n\nPlan = %s", ast, plan)
3332
return nil
34-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
33+
}, retry.WithIdempotent(true))
3534
if err != nil {
3635
return fmt.Errorf("explain query failed: %w", err)
3736
}
@@ -62,7 +61,7 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
6261
)
6362
}
6463
return rows.Err()
65-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
64+
}, retry.WithIdempotent(true))
6665
if err != nil {
6766
return fmt.Errorf("execute data query failed: %w", err)
6867
}
@@ -155,7 +154,7 @@ func selectScan(ctx context.Context, db *sql.DB) (err error) {
155154
)
156155
}
157156
return rows.Err()
158-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
157+
}, retry.WithIdempotent(true))
159158
if err != nil {
160159
return fmt.Errorf("scan query failed: %w", err)
161160
}
@@ -202,7 +201,7 @@ func fillTablesWithData(ctx context.Context, db *sql.DB) (err error) {
202201
return err
203202
}
204203
return nil
205-
}, retry.WithDoTxRetryOptions(retry.WithIdempotent(true)))
204+
}, retry.WithIdempotent(true))
206205
if err != nil {
207206
return fmt.Errorf("upsert query failed: %w", err)
208207
}
@@ -235,7 +234,7 @@ func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
235234
return err
236235
}
237236
return nil
238-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
237+
}, retry.WithIdempotent(true))
239238
if err != nil {
240239
return fmt.Errorf("create table failed: %w", err)
241240
}
@@ -266,7 +265,7 @@ func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
266265
return err
267266
}
268267
return nil
269-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
268+
}, retry.WithIdempotent(true))
270269
if err != nil {
271270
return fmt.Errorf("create table failed: %w", err)
272271
}
@@ -299,7 +298,7 @@ func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
299298
return err
300299
}
301300
return nil
302-
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
301+
}, retry.WithIdempotent(true))
303302
if err != nil {
304303
return fmt.Errorf("create table failed: %w", err)
305304
}

internal/table/client.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,31 @@ func (c *Client) CreateSession(ctx context.Context, opts ...table.Option) (_ tab
241241
}
242242
return s, nil
243243
}
244-
options := retryOptions(c.config.Trace(), opts...)
245-
err = retry.Retry(
246-
ctx,
244+
err = retry.Retry(ctx,
247245
func(ctx context.Context) (err error) {
248246
s, err = createSession(ctx)
249247
if err != nil {
250248
return xerrors.WithStackTrace(err)
251249
}
252250
return nil
253251
},
254-
retry.WithIdempotent(true),
255-
retry.WithID("CreateSession"),
256-
retry.WithFastBackoff(options.FastBackoff),
257-
retry.WithSlowBackoff(options.SlowBackoff),
258-
retry.WithTrace(trace.Retry{
259-
OnRetry: func(info trace.RetryLoopStartInfo) func(trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
260-
onIntermediate := trace.TableOnCreateSession(c.config.Trace(), info.Context)
261-
return func(info trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
262-
onDone := onIntermediate(info.Error)
263-
return func(info trace.RetryLoopDoneInfo) {
264-
onDone(s, info.Attempts, info.Error)
265-
}
266-
}
267-
},
268-
}),
252+
append(
253+
[]retry.Option{
254+
retry.WithIdempotent(true),
255+
retry.WithID("CreateSession"),
256+
retry.WithTrace(trace.Retry{
257+
OnRetry: func(info trace.RetryLoopStartInfo) func(trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
258+
onIntermediate := trace.TableOnCreateSession(c.config.Trace(), info.Context)
259+
return func(info trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
260+
onDone := onIntermediate(info.Error)
261+
return func(info trace.RetryLoopDoneInfo) {
262+
onDone(s, info.Attempts, info.Error)
263+
}
264+
}
265+
},
266+
}),
267+
}, retryOptions(c.config.Trace(), opts...).RetryOptions...,
268+
)...,
269269
)
270270
if err != nil {
271271
return nil, xerrors.WithStackTrace(err)

internal/table/retry.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package table
33
import (
44
"context"
55

6-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/backoff"
76
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/config"
87
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
98
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
@@ -50,18 +49,14 @@ func doTx(
5049
attempts, onIntermediate := 0, trace.TableOnDoTx(
5150
opts.Trace,
5251
&ctx,
52+
opts.ID,
5353
opts.Idempotent,
5454
isRetryCalledAbove(ctx),
5555
)
5656
defer func() {
5757
onIntermediate(err)(attempts, err)
5858
}()
59-
err = retryBackoff(
60-
ctx,
61-
c,
62-
opts.FastBackoff,
63-
opts.SlowBackoff,
64-
opts.Idempotent,
59+
err = retryBackoff(ctx, c,
6560
func(ctx context.Context, s table.Session) (err error) {
6661
attempts++
6762

@@ -110,6 +105,7 @@ func doTx(
110105

111106
return nil
112107
},
108+
opts.RetryOptions...,
113109
)
114110
if err != nil {
115111
return xerrors.WithStackTrace(err)
@@ -127,11 +123,11 @@ func do(
127123
if opts.Trace == nil {
128124
opts.Trace = &trace.Table{}
129125
}
130-
attempts, onIntermediate := 0, trace.TableOnDo(opts.Trace, &ctx, opts.Idempotent, isRetryCalledAbove(ctx))
126+
attempts, onIntermediate := 0, trace.TableOnDo(opts.Trace, &ctx, opts.ID, opts.Idempotent, isRetryCalledAbove(ctx))
131127
defer func() {
132128
onIntermediate(err)(attempts, err)
133129
}()
134-
return retryBackoff(ctx, c, opts.FastBackoff, opts.SlowBackoff, opts.Idempotent,
130+
return retryBackoff(ctx, c,
135131
func(ctx context.Context, s table.Session) (err error) {
136132
attempts++
137133

@@ -156,16 +152,15 @@ func do(
156152

157153
return nil
158154
},
155+
opts.RetryOptions...,
159156
)
160157
}
161158

162159
func retryBackoff(
163160
ctx context.Context,
164161
p SessionProvider,
165-
fastBackoff backoff.Backoff,
166-
slowBackoff backoff.Backoff,
167-
isOperationIdempotent bool,
168162
op table.Operation,
163+
opts ...retry.Option,
169164
) (err error) {
170165
err = retry.Retry(markRetryCall(ctx),
171166
func(ctx context.Context) (err error) {
@@ -188,9 +183,7 @@ func retryBackoff(
188183

189184
return nil
190185
},
191-
retry.WithFastBackoff(fastBackoff),
192-
retry.WithSlowBackoff(slowBackoff),
193-
retry.WithIdempotent(isOperationIdempotent),
186+
opts...,
194187
)
195188
if err != nil {
196189
return xerrors.WithStackTrace(err)
@@ -200,9 +193,7 @@ func retryBackoff(
200193

201194
func retryOptions(trace *trace.Table, opts ...table.Option) *table.Options {
202195
options := &table.Options{
203-
Trace: trace,
204-
FastBackoff: backoff.Fast,
205-
SlowBackoff: backoff.Slow,
196+
Trace: trace,
206197
TxSettings: table.TxSettings(
207198
table.WithSerializableReadWrite(),
208199
),

internal/table/retry_test.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,22 @@ func TestRetryerBackoffRetryCancelation(t *testing.T) {
4949
return testErr
5050
},
5151
&table.Options{
52-
FastBackoff: testutil.BackoffFunc(func(n int) <-chan time.Time {
53-
ch := make(chan time.Time)
54-
backoff <- ch
55-
return ch
56-
}),
57-
SlowBackoff: testutil.BackoffFunc(func(n int) <-chan time.Time {
58-
ch := make(chan time.Time)
59-
backoff <- ch
60-
return ch
61-
}),
52+
RetryOptions: []retry.Option{
53+
retry.WithFastBackoff(
54+
testutil.BackoffFunc(func(n int) <-chan time.Time {
55+
ch := make(chan time.Time)
56+
backoff <- ch
57+
return ch
58+
}),
59+
),
60+
retry.WithSlowBackoff(
61+
testutil.BackoffFunc(func(n int) <-chan time.Time {
62+
ch := make(chan time.Time)
63+
backoff <- ch
64+
return ch
65+
}),
66+
),
67+
},
6268
},
6369
)
6470
results <- err
@@ -207,12 +213,18 @@ func TestRetryerImmediateReturn(t *testing.T) {
207213
return testErr
208214
},
209215
&table.Options{
210-
FastBackoff: testutil.BackoffFunc(func(n int) <-chan time.Time {
211-
panic("this code will not be called")
212-
}),
213-
SlowBackoff: testutil.BackoffFunc(func(n int) <-chan time.Time {
214-
panic("this code will not be called")
215-
}),
216+
RetryOptions: []retry.Option{
217+
retry.WithFastBackoff(
218+
testutil.BackoffFunc(func(n int) <-chan time.Time {
219+
panic("this code will not be called")
220+
}),
221+
),
222+
retry.WithSlowBackoff(
223+
testutil.BackoffFunc(func(n int) <-chan time.Time {
224+
panic("this code will not be called")
225+
}),
226+
),
227+
},
216228
},
217229
)
218230
if !xerrors.Is(err, testErr) {

metrics/table.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ func table(config Config) (t trace.Table) {
1717
wait := config.WithSystem("pool").GaugeVec("wait")
1818
waitLatency := config.WithSystem("pool").WithSystem("wait").TimerVec("latency")
1919
alive := config.GaugeVec("sessions", "node_id")
20-
doAttempts := config.WithSystem("do").HistogramVec("attempts", []float64{0, 1, 2, 5, 10})
21-
doErrors := config.WithSystem("do").WithSystem("intermediate").CounterVec("errors", "status")
22-
doTxAttempts := config.WithSystem("doTx").HistogramVec("attempts", []float64{0, 1, 2, 5, 10})
23-
doTxErrors := config.WithSystem("doTx").WithSystem("intermediate").CounterVec("errors", "status")
20+
doAttempts := config.WithSystem("do").HistogramVec("attempts", []float64{0, 1, 2, 5, 10}, "name")
21+
doErrors := config.WithSystem("do").CounterVec("errors", "status", "name")
22+
doIntermediateErrors := config.WithSystem("do").WithSystem("intermediate").CounterVec("errors", "status", "name")
23+
doLatency := config.WithSystem("do").TimerVec("latency", "status", "name")
24+
doTxAttempts := config.WithSystem("doTx").HistogramVec("attempts", []float64{0, 1, 2, 5, 10}, "name")
25+
doTxIntermediateErrors := config.WithSystem("doTx").WithSystem("intermediate").CounterVec("errors", "status", "name")
26+
doTxErrors := config.WithSystem("doTx").CounterVec("errors", "status", "name")
27+
doTxLatency := config.WithSystem("doTx").TimerVec("latency", "status", "name")
2428
t.OnInit = func(info trace.TableInitStartInfo) func(trace.TableInitDoneInfo) {
2529
return func(info trace.TableInitDoneInfo) {
2630
limit.With(nil).Set(float64(info.Limit))
@@ -31,15 +35,28 @@ func table(config Config) (t trace.Table) {
3135
) func(
3236
trace.TableDoDoneInfo,
3337
) {
38+
var (
39+
name = info.ID
40+
start = time.Now()
41+
)
3442
return func(info trace.TableDoIntermediateInfo) func(trace.TableDoDoneInfo) {
3543
if info.Error != nil && config.Details()&trace.TableEvents != 0 {
36-
doErrors.With(map[string]string{
44+
doIntermediateErrors.With(map[string]string{
3745
"status": errorBrief(info.Error),
46+
"name": name,
3847
}).Inc()
3948
}
4049
return func(info trace.TableDoDoneInfo) {
4150
if config.Details()&trace.TableEvents != 0 {
4251
doAttempts.With(nil).Record(float64(info.Attempts))
52+
doErrors.With(map[string]string{
53+
"status": errorBrief(info.Error),
54+
"name": name,
55+
}).Inc()
56+
doLatency.With(map[string]string{
57+
"status": errorBrief(info.Error),
58+
"name": name,
59+
}).Record(time.Since(start))
4360
}
4461
}
4562
}
@@ -49,15 +66,28 @@ func table(config Config) (t trace.Table) {
4966
) func(
5067
trace.TableDoTxDoneInfo,
5168
) {
69+
var (
70+
name = info.ID
71+
start = time.Now()
72+
)
5273
return func(info trace.TableDoTxIntermediateInfo) func(trace.TableDoTxDoneInfo) {
5374
if info.Error != nil && config.Details()&trace.TableEvents != 0 {
54-
doTxErrors.With(map[string]string{
75+
doTxIntermediateErrors.With(map[string]string{
5576
"status": errorBrief(info.Error),
77+
"name": name,
5678
}).Inc()
5779
}
5880
return func(info trace.TableDoTxDoneInfo) {
5981
if config.Details()&trace.TableEvents != 0 {
6082
doTxAttempts.With(nil).Record(float64(info.Attempts))
83+
doTxErrors.With(map[string]string{
84+
"status": errorBrief(info.Error),
85+
"name": name,
86+
}).Inc()
87+
doTxLatency.With(map[string]string{
88+
"status": errorBrief(info.Error),
89+
"name": name,
90+
}).Record(time.Since(start))
6191
}
6292
}
6393
}

0 commit comments

Comments
 (0)