@@ -15,27 +15,34 @@ import (
1515 "github.com/ydb-platform/ydb-go-sdk/v3/coordination/options"
1616 "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination/config"
1717 "github.com/ydb-platform/ydb-go-sdk/v3/internal/operation"
18+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1819 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1920 "github.com/ydb-platform/ydb-go-sdk/v3/retry"
2021 "github.com/ydb-platform/ydb-go-sdk/v3/scheme"
22+ "github.com/ydb-platform/ydb-go-sdk/v3/trace"
2123)
2224
2325//go:generate mockgen -destination grpc_client_mock_test.go -package coordination -write_package_comment=false github.com/ydb-platform/ydb-go-genproto/Ydb_Coordination_V1 CoordinationServiceClient,CoordinationService_SessionClient
2426
2527var errNilClient = xerrors .Wrap (errors .New ("coordination client is not initialized" ))
2628
2729type Client struct {
28- config config.Config
29- service Ydb_Coordination_V1.CoordinationServiceClient
30+ config config.Config
31+ client Ydb_Coordination_V1.CoordinationServiceClient
3032
3133 mutex sync.Mutex // guards the fields below
3234 sessions map [* session ]struct {}
3335}
3436
3537func New (ctx context.Context , cc grpc.ClientConnInterface , config config.Config ) * Client {
38+ onDone := trace .CoordinationOnNew (config .Trace (), & ctx ,
39+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.New" ),
40+ )
41+ defer onDone ()
42+
3643 return & Client {
3744 config : config ,
38- service : Ydb_Coordination_V1 .NewCoordinationServiceClient (cc ),
45+ client : Ydb_Coordination_V1 .NewCoordinationServiceClient (cc ),
3946 sessions : make (map [* session ]struct {}),
4047 }
4148}
@@ -84,99 +91,128 @@ func createNode(
8491 return nil
8592}
8693
87- func (c * Client ) CreateNode (ctx context.Context , path string , config coordination.NodeConfig ) error {
94+ func (c * Client ) CreateNode (ctx context.Context , path string , config coordination.NodeConfig ) ( finalErr error ) {
8895 if c == nil {
8996 return xerrors .WithStackTrace (errNilClient )
9097 }
9198
99+ onDone := trace .CoordinationOnCreateNode (c .config .Trace (), & ctx ,
100+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.(*Client).CreateNode" ),
101+ path ,
102+ )
103+ defer func () {
104+ onDone (finalErr )
105+ }()
106+
92107 request := createNodeRequest (path , config , operationParams (ctx , & c .config , operation .ModeSync ))
93108
94109 if ! c .config .AutoRetry () {
95- return createNode (ctx , c .service , request )
110+ return createNode (ctx , c .client , request )
96111 }
97112
98113 return retry .Retry (ctx , func (ctx context.Context ) error {
99- return createNode (ctx , c .service , request )
114+ return createNode (ctx , c .client , request )
100115 }, retry .WithStackTrace (), retry .WithIdempotent (true ), retry .WithTrace (c .config .TraceRetry ()))
101116}
102117
103- func (c * Client ) AlterNode (ctx context.Context , path string , config coordination.NodeConfig ) error {
118+ func (c * Client ) AlterNode (ctx context.Context , path string , config coordination.NodeConfig ) ( finalErr error ) {
104119 if c == nil {
105120 return xerrors .WithStackTrace (errNilClient )
106121 }
122+
123+ onDone := trace .CoordinationOnAlterNode (c .config .Trace (), & ctx ,
124+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.(*Client).AlterNode" ),
125+ path ,
126+ )
127+ defer func () {
128+ onDone (finalErr )
129+ }()
130+
131+ request := alterNodeRequest (path , config , operationParams (ctx , & c .config , operation .ModeSync ))
132+
107133 call := func (ctx context.Context ) error {
108- return xerrors . WithStackTrace ( c . alterNode (ctx , path , config ) )
134+ return alterNode (ctx , c . client , request )
109135 }
110136 if ! c .config .AutoRetry () {
111137 return xerrors .WithStackTrace (call (ctx ))
112138 }
113139
114- return retry .Retry (ctx ,
115- call ,
116- retry .WithStackTrace (),
117- retry .WithIdempotent (true ),
118- retry .WithTrace (c .config .TraceRetry ()),
119- )
140+ return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
141+ return alterNode (ctx , c .client , request )
142+ }, retry .WithStackTrace (), retry .WithIdempotent (true ), retry .WithTrace (c .config .TraceRetry ()))
120143}
121144
122- func (c * Client ) alterNode (ctx context.Context , path string , config coordination.NodeConfig ) error {
123- _ , err := c .service .AlterNode (
124- ctx ,
125- & Ydb_Coordination.AlterNodeRequest {
126- Path : path ,
127- Config : & Ydb_Coordination.Config {
128- Path : config .Path ,
129- SelfCheckPeriodMillis : config .SelfCheckPeriodMillis ,
130- SessionGracePeriodMillis : config .SessionGracePeriodMillis ,
131- ReadConsistencyMode : config .ReadConsistencyMode .To (),
132- AttachConsistencyMode : config .AttachConsistencyMode .To (),
133- RateLimiterCountersMode : config .RatelimiterCountersMode .To (),
134- },
135- OperationParams : operation .Params (
136- ctx ,
137- c .config .OperationTimeout (),
138- c .config .OperationCancelAfter (),
139- operation .ModeSync ,
140- ),
145+ func alterNodeRequest (
146+ path string , config coordination.NodeConfig , operationParams * Ydb_Operations.OperationParams ,
147+ ) * Ydb_Coordination.AlterNodeRequest {
148+ return & Ydb_Coordination.AlterNodeRequest {
149+ Path : path ,
150+ Config : & Ydb_Coordination.Config {
151+ Path : config .Path ,
152+ SelfCheckPeriodMillis : config .SelfCheckPeriodMillis ,
153+ SessionGracePeriodMillis : config .SessionGracePeriodMillis ,
154+ ReadConsistencyMode : config .ReadConsistencyMode .To (),
155+ AttachConsistencyMode : config .AttachConsistencyMode .To (),
156+ RateLimiterCountersMode : config .RatelimiterCountersMode .To (),
141157 },
142- )
158+ OperationParams : operationParams ,
159+ }
160+ }
161+
162+ func alterNode (
163+ ctx context.Context , client Ydb_Coordination_V1.CoordinationServiceClient , request * Ydb_Coordination.AlterNodeRequest ,
164+ ) error {
165+ _ , err := client .AlterNode (ctx , request )
166+ if err != nil {
167+ return xerrors .WithStackTrace (err )
168+ }
143169
144- return xerrors . WithStackTrace ( err )
170+ return nil
145171}
146172
147- func (c * Client ) DropNode (ctx context.Context , path string ) error {
173+ func (c * Client ) DropNode (ctx context.Context , path string ) ( finalErr error ) {
148174 if c == nil {
149175 return xerrors .WithStackTrace (errNilClient )
150176 }
177+
178+ onDone := trace .CoordinationOnDropNode (c .config .Trace (), & ctx ,
179+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.(*Client).DropNode" ),
180+ path ,
181+ )
182+ defer func () {
183+ onDone (finalErr )
184+ }()
185+
186+ request := dropNodeRequest (path , operationParams (ctx , & c .config , operation .ModeSync ))
187+
151188 call := func (ctx context.Context ) error {
152- return xerrors . WithStackTrace ( c . dropNode (ctx , path ) )
189+ return dropNode (ctx , c . client , request )
153190 }
154191 if ! c .config .AutoRetry () {
155192 return xerrors .WithStackTrace (call (ctx ))
156193 }
157194
158- return retry .Retry (ctx , call ,
159- retry .WithStackTrace (),
160- retry .WithIdempotent (true ),
161- retry .WithTrace (c .config .TraceRetry ()),
162- )
195+ return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
196+ return dropNode (ctx , c .client , request )
197+ }, retry .WithStackTrace (), retry .WithIdempotent (true ), retry .WithTrace (c .config .TraceRetry ()))
163198}
164199
165- func (c * Client ) dropNode (ctx context.Context , path string ) error {
166- _ , err := c .service .DropNode (
167- ctx ,
168- & Ydb_Coordination.DropNodeRequest {
169- Path : path ,
170- OperationParams : operation .Params (
171- ctx ,
172- c .config .OperationTimeout (),
173- c .config .OperationCancelAfter (),
174- operation .ModeSync ,
175- ),
176- },
177- )
200+ func dropNodeRequest (path string , operationParams * Ydb_Operations.OperationParams ) * Ydb_Coordination.DropNodeRequest {
201+ return & Ydb_Coordination.DropNodeRequest {
202+ Path : path ,
203+ OperationParams : operationParams ,
204+ }
205+ }
178206
179- return xerrors .WithStackTrace (err )
207+ func dropNode (
208+ ctx context.Context , client Ydb_Coordination_V1.CoordinationServiceClient , request * Ydb_Coordination.DropNodeRequest ,
209+ ) error {
210+ _ , err := client .DropNode (ctx , request )
211+ if err != nil {
212+ return xerrors .WithStackTrace (err )
213+ }
214+
215+ return nil
180216}
181217
182218func (c * Client ) DescribeNode (
@@ -185,31 +221,34 @@ func (c *Client) DescribeNode(
185221) (
186222 entry * scheme.Entry ,
187223 config * coordination.NodeConfig ,
188- _ error ,
224+ finalErr error ,
189225) {
190226 if c == nil {
191227 return nil , nil , xerrors .WithStackTrace (errNilClient )
192228 }
193229
230+ onDone := trace .CoordinationOnDescribeNode (c .config .Trace (), & ctx ,
231+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.(*Client).DescribeNode" ),
232+ path ,
233+ )
234+ defer func () {
235+ onDone (finalErr )
236+ }()
237+
194238 request := describeNodeRequest (path , operationParams (ctx , & c .config , operation .ModeSync ))
195239
196240 if ! c .config .AutoRetry () {
197- return describeNode (ctx , c .service , request )
241+ return describeNode (ctx , c .client , request )
198242 }
199243
200- err := retry .Retry (ctx ,
201- func (ctx context.Context ) (err error ) {
202- entry , config , err = describeNode (ctx , c .service , request )
203- if err != nil {
204- return xerrors .WithStackTrace (err )
205- }
244+ err := retry .Retry (ctx , func (ctx context.Context ) (err error ) {
245+ entry , config , err = describeNode (ctx , c .client , request )
246+ if err != nil {
247+ return xerrors .WithStackTrace (err )
248+ }
206249
207- return nil
208- },
209- retry .WithStackTrace (),
210- retry .WithIdempotent (true ),
211- retry .WithTrace (c .config .TraceRetry ()),
212- )
250+ return nil
251+ }, retry .WithStackTrace (), retry .WithIdempotent (true ), retry .WithTrace (c .config .TraceRetry ()))
213252 if err != nil {
214253 return nil , nil , xerrors .WithStackTrace (err )
215254 }
@@ -309,11 +348,19 @@ func (c *Client) CreateSession(
309348 ctx context.Context ,
310349 path string ,
311350 opts ... options.CreateSessionOption ,
312- ) (coordination.Session , error ) {
351+ ) (_ coordination.Session , finalErr error ) {
313352 if c == nil {
314353 return nil , xerrors .WithStackTrace (errNilClient )
315354 }
316355
356+ onDone := trace .CoordinationOnCreateSession (c .config .Trace (), & ctx ,
357+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.(*Client).CreateSession" ),
358+ path ,
359+ )
360+ defer func () {
361+ onDone (finalErr )
362+ }()
363+
317364 return createSession (ctx , c , path , newCreateSessionConfig (opts ... ))
318365}
319366
0 commit comments