@@ -12,10 +12,12 @@ 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/stack"
1516 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1617 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1718 "github.com/ydb-platform/ydb-go-sdk/v3/query"
1819 "github.com/ydb-platform/ydb-go-sdk/v3/retry"
20+ "github.com/ydb-platform/ydb-go-sdk/v3/trace"
1921)
2022
2123//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 +29,7 @@ type balancer interface {
2729var _ query.Client = (* Client )(nil )
2830
2931type Client struct {
32+ config * config.Config
3033 grpcClient Ydb_Query_V1.QueryServiceClient
3134 pool * pool.Pool [Session ]
3235}
@@ -40,39 +43,63 @@ func (c Client) Close(ctx context.Context) error {
4043 return nil
4144}
4245
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- })
46+ func do (
47+ ctx context.Context ,
48+ pool * pool.Pool [Session ],
49+ op query.Operation ,
50+ t * trace.Query ,
51+ opts ... query.DoOption ,
52+ ) (finalErr error ) {
53+ doOpts := query .ParseDoOpts (t , opts ... )
54+
55+ err := pool .With (ctx , func (ctx context.Context , s * Session ) error {
56+ err := op (ctx , s )
5357 if err != nil {
5458 return xerrors .WithStackTrace (err )
5559 }
5660
5761 return nil
58- }, opts .RetryOptions ... )
62+ }, append (doOpts .RetryOpts (), retry .WithTrace (& trace.Retry {
63+ OnRetry : func (
64+ info trace.RetryLoopStartInfo ,
65+ ) func (
66+ trace.RetryLoopIntermediateInfo ,
67+ ) func (
68+ trace.RetryLoopDoneInfo ,
69+ ) {
70+ onIntermediate := trace .QueryOnDo (doOpts .Trace (), & ctx , stack .FunctionID ("" ))
71+
72+ return func (info trace.RetryLoopIntermediateInfo ) func (trace.RetryLoopDoneInfo ) {
73+ onDone := onIntermediate (info .Error )
74+
75+ return func (info trace.RetryLoopDoneInfo ) {
76+ onDone (info .Attempts , info .Error )
77+ }
78+ }
79+ },
80+ }))... )
81+ if err != nil {
82+ return xerrors .WithStackTrace (err )
83+ }
84+
85+ return nil
5986}
6087
6188func (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 ))
68- }
69-
70- return do (ctx , c .pool , op , & doOptions )
89+ return do (ctx , c .pool , op , c .config .Trace (), opts ... )
7190}
7291
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 )
92+ func doTx (
93+ ctx context.Context ,
94+ pool * pool.Pool [Session ],
95+ op query.TxOperation ,
96+ t * trace.Query ,
97+ opts ... query.DoTxOption ,
98+ ) error {
99+ doTxOpts := query .ParseDoTxOpts (t , opts ... )
100+
101+ err := do (ctx , pool , func (ctx context.Context , s query.Session ) error {
102+ tx , err := s .Begin (ctx , doTxOpts .TxSettings ())
76103 if err != nil {
77104 return xerrors .WithStackTrace (err )
78105 }
@@ -96,19 +123,16 @@ func doTx(ctx context.Context, pool *pool.Pool[Session], op query.TxOperation, o
96123 }
97124
98125 return nil
99- }, & opts .DoOptions )
126+ }, t , doTxOpts .DoOpts ()... )
127+ if err != nil {
128+ return xerrors .WithStackTrace (err )
129+ }
130+
131+ return nil
100132}
101133
102134func (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 ))
109- }
110-
111- return doTx (ctx , c .pool , op , & doTxOptions )
135+ return doTx (ctx , c .pool , op , c .config .Trace (), opts ... )
112136}
113137
114138func deleteSession (ctx context.Context , client Ydb_Query_V1.QueryServiceClient , sessionID string ) error {
@@ -231,6 +255,7 @@ func createSession(
231255
232256func New (ctx context.Context , balancer balancer , config * config.Config ) (* Client , error ) {
233257 client := & Client {
258+ config : config ,
234259 grpcClient : Ydb_Query_V1 .NewQueryServiceClient (balancer ),
235260 }
236261
0 commit comments