@@ -12,10 +12,13 @@ import (
1212
1313 "github.com/ydb-platform/ydb-go-sdk/v3/internal/pool"
1414 "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/config"
15+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
16+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1517 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1618 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1719 "github.com/ydb-platform/ydb-go-sdk/v3/query"
1820 "github.com/ydb-platform/ydb-go-sdk/v3/retry"
21+ "github.com/ydb-platform/ydb-go-sdk/v3/trace"
1922)
2023
2124//go:generate mockgen -destination grpc_client_mock_test.go -package query -write_package_comment=false github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1 QueryServiceClient,QueryService_AttachSessionClient,QueryService_ExecuteQueryClient
@@ -27,6 +30,7 @@ type balancer interface {
2730var _ query.Client = (* Client )(nil )
2831
2932type Client struct {
33+ config * config.Config
3034 grpcClient Ydb_Query_V1.QueryServiceClient
3135 pool * pool.Pool [Session ]
3236}
@@ -40,39 +44,63 @@ func (c Client) Close(ctx context.Context) error {
4044 return nil
4145}
4246
43- func do (ctx context.Context , pool * pool.Pool [Session ], op query.Operation , opts * query.DoOptions ) error {
44- return retry .Retry (ctx , func (ctx context.Context ) error {
45- err := pool .With (ctx , func (ctx context.Context , s * Session ) error {
46- err := op (ctx , s )
47- if err != nil {
48- return xerrors .WithStackTrace (err )
49- }
50-
51- return nil
52- })
47+ func do (
48+ ctx context.Context ,
49+ pool * pool.Pool [Session ],
50+ op query.Operation ,
51+ t * trace.Query ,
52+ opts ... options.DoOption ,
53+ ) (finalErr error ) {
54+ doOpts := options .ParseDoOpts (t , opts ... )
55+
56+ err := pool .With (ctx , func (ctx context.Context , s * Session ) error {
57+ err := op (ctx , s )
5358 if err != nil {
5459 return xerrors .WithStackTrace (err )
5560 }
5661
5762 return nil
58- }, opts .RetryOptions ... )
59- }
60-
61- func (c Client ) Do (ctx context.Context , op query.Operation , opts ... query.DoOption ) error {
62- doOptions := query .NewDoOptions (opts ... )
63- if doOptions .Label != "" {
64- doOptions .RetryOptions = append (doOptions .RetryOptions , retry .WithLabel (doOptions .Label ))
65- }
66- if doOptions .Idempotent {
67- doOptions .RetryOptions = append (doOptions .RetryOptions , retry .WithIdempotent (doOptions .Idempotent ))
63+ }, append (doOpts .RetryOpts (), retry .WithTrace (& trace.Retry {
64+ OnRetry : func (
65+ info trace.RetryLoopStartInfo ,
66+ ) func (
67+ trace.RetryLoopIntermediateInfo ,
68+ ) func (
69+ trace.RetryLoopDoneInfo ,
70+ ) {
71+ onIntermediate := trace .QueryOnDo (doOpts .Trace (), & ctx , stack .FunctionID ("" ))
72+
73+ return func (info trace.RetryLoopIntermediateInfo ) func (trace.RetryLoopDoneInfo ) {
74+ onDone := onIntermediate (info .Error )
75+
76+ return func (info trace.RetryLoopDoneInfo ) {
77+ onDone (info .Attempts , info .Error )
78+ }
79+ }
80+ },
81+ }))... )
82+ if err != nil {
83+ return xerrors .WithStackTrace (err )
6884 }
6985
70- return do (ctx , c .pool , op , & doOptions )
86+ return nil
87+ }
88+
89+ func (c Client ) Do (ctx context.Context , op query.Operation , opts ... options.DoOption ) error {
90+ return do (ctx , c .pool , op , c .config .Trace (), opts ... )
7191}
7292
73- func doTx (ctx context.Context , pool * pool.Pool [Session ], op query.TxOperation , opts * query.DoTxOptions ) error {
74- return do (ctx , pool , func (ctx context.Context , s query.Session ) error {
75- tx , err := s .Begin (ctx , opts .TxSettings )
93+ func doTx (
94+ ctx context.Context ,
95+ pool * pool.Pool [Session ],
96+ op query.TxOperation ,
97+ t * trace.Query ,
98+ opts ... options.DoTxOption ,
99+ ) error {
100+ doTxOpts := options .ParseDoTxOpts (t , opts ... )
101+
102+ err := do (ctx , pool , func (ctx context.Context , s query.Session ) error {
103+ tx , err := s .Begin (ctx , doTxOpts .TxSettings ())
76104 if err != nil {
77105 return xerrors .WithStackTrace (err )
78106 }
@@ -96,19 +124,16 @@ func doTx(ctx context.Context, pool *pool.Pool[Session], op query.TxOperation, o
96124 }
97125
98126 return nil
99- }, & opts .DoOptions )
100- }
101-
102- func (c Client ) DoTx (ctx context.Context , op query.TxOperation , opts ... query.DoTxOption ) error {
103- doTxOptions := query .NewDoTxOptions (opts ... )
104- if doTxOptions .Label != "" {
105- doTxOptions .RetryOptions = append (doTxOptions .RetryOptions , retry .WithLabel (doTxOptions .Label ))
106- }
107- if doTxOptions .Idempotent {
108- doTxOptions .RetryOptions = append (doTxOptions .RetryOptions , retry .WithIdempotent (doTxOptions .Idempotent ))
127+ }, t , doTxOpts .DoOpts ()... )
128+ if err != nil {
129+ return xerrors .WithStackTrace (err )
109130 }
110131
111- return doTx (ctx , c .pool , op , & doTxOptions )
132+ return nil
133+ }
134+
135+ func (c Client ) DoTx (ctx context.Context , op query.TxOperation , opts ... options.DoTxOption ) error {
136+ return doTx (ctx , c .pool , op , c .config .Trace (), opts ... )
112137}
113138
114139func deleteSession (ctx context.Context , client Ydb_Query_V1.QueryServiceClient , sessionID string ) error {
@@ -231,6 +256,7 @@ func createSession(
231256
232257func New (ctx context.Context , balancer balancer , config * config.Config ) (* Client , error ) {
233258 client := & Client {
259+ config : config ,
234260 grpcClient : Ydb_Query_V1 .NewQueryServiceClient (balancer ),
235261 }
236262
0 commit comments