Skip to content

Commit 3c08fa3

Browse files
authored
Merge pull request #894 from ydb-platform/function-id
stack.FunctionID fix for codegen in the future
2 parents 66f5d69 + 79f4d13 commit 3c08fa3

File tree

28 files changed

+509
-258
lines changed

28 files changed

+509
-258
lines changed

driver.go

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,16 @@ type Driver struct { //nolint:maligned
9393
panicCallback func(e interface{})
9494
}
9595

96+
func (d *Driver) trace() *trace.Driver {
97+
if d.config != nil {
98+
return d.config.Trace()
99+
}
100+
return &trace.Driver{}
101+
}
102+
96103
// Close closes Driver and clear resources
97104
func (d *Driver) Close(ctx context.Context) (finalErr error) {
98-
onDone := trace.DriverOnClose(d.config.Trace(), &ctx, stack.FunctionID(0))
105+
onDone := trace.DriverOnClose(d.trace(), &ctx, stack.FunctionID(""))
99106
defer func() {
100107
onDone(finalErr)
101108
}()
@@ -201,15 +208,30 @@ func (d *Driver) Topic() topic.Client {
201208
//
202209
// See sugar.DSN helper for make dsn from endpoint and database
203210
func Open(ctx context.Context, dsn string, opts ...Option) (_ *Driver, err error) {
204-
return open(
205-
ctx,
206-
append(
207-
[]Option{
208-
WithConnectionString(dsn),
209-
},
210-
opts...,
211-
)...,
211+
d, err := newConnectionFromOptions(ctx, append(
212+
[]Option{
213+
WithConnectionString(dsn),
214+
},
215+
opts...,
216+
)...)
217+
if err != nil {
218+
return nil, xerrors.WithStackTrace(err)
219+
}
220+
221+
onDone := trace.DriverOnInit(
222+
d.trace(), &ctx,
223+
stack.FunctionID(""),
224+
d.config.Endpoint(), d.config.Database(), d.config.Secure(),
212225
)
226+
defer func() {
227+
onDone(err)
228+
}()
229+
230+
if err = d.connect(ctx); err != nil {
231+
return nil, xerrors.WithStackTrace(err)
232+
}
233+
234+
return d, nil
213235
}
214236

215237
func MustOpen(ctx context.Context, dsn string, opts ...Option) *Driver {
@@ -224,7 +246,25 @@ func MustOpen(ctx context.Context, dsn string, opts ...Option) *Driver {
224246
//
225247
// Deprecated: use Open with required param connectionString instead
226248
func New(ctx context.Context, opts ...Option) (_ *Driver, err error) {
227-
return open(ctx, opts...)
249+
d, err := newConnectionFromOptions(ctx, opts...)
250+
if err != nil {
251+
return nil, xerrors.WithStackTrace(err)
252+
}
253+
254+
onDone := trace.DriverOnInit(
255+
d.trace(), &ctx,
256+
stack.FunctionID(""),
257+
d.config.Endpoint(), d.config.Database(), d.config.Secure(),
258+
)
259+
defer func() {
260+
onDone(err)
261+
}()
262+
263+
if err = d.connect(ctx); err != nil {
264+
return nil, xerrors.WithStackTrace(err)
265+
}
266+
267+
return d, nil
228268
}
229269

230270
func newConnectionFromOptions(ctx context.Context, opts ...Option) (_ *Driver, err error) {
@@ -287,23 +327,15 @@ func newConnectionFromOptions(ctx context.Context, opts ...Option) (_ *Driver, e
287327
return d, nil
288328
}
289329

290-
func connect(ctx context.Context, d *Driver) error {
291-
var err error
292-
330+
func (d *Driver) connect(ctx context.Context) (err error) {
293331
if d.config.Endpoint() == "" {
294332
return xerrors.WithStackTrace(errors.New("configuration: empty dial address"))
295333
}
334+
296335
if d.config.Database() == "" {
297336
return xerrors.WithStackTrace(errors.New("configuration: empty database"))
298337
}
299338

300-
onDone := trace.DriverOnInit(
301-
d.config.Trace(), &ctx, stack.FunctionID(2), d.config.Endpoint(), d.config.Database(), d.config.Secure(),
302-
)
303-
defer func() {
304-
onDone(err)
305-
}()
306-
307339
if d.userInfo != nil {
308340
d.config = d.config.With(config.WithCredentials(
309341
credentials.NewStaticCredentials(
@@ -443,18 +475,6 @@ func connect(ctx context.Context, d *Driver) error {
443475
return nil
444476
}
445477

446-
func open(ctx context.Context, opts ...Option) (_ *Driver, err error) {
447-
d, err := newConnectionFromOptions(ctx, opts...)
448-
if err != nil {
449-
return nil, xerrors.WithStackTrace(err)
450-
}
451-
err = connect(ctx, d)
452-
if err != nil {
453-
return nil, xerrors.WithStackTrace(err)
454-
}
455-
return d, nil
456-
}
457-
458478
// GRPCConn casts *ydb.Driver to grpc.ClientConnInterface for executing
459479
// unary and streaming RPC over internal driver balancer.
460480
//

internal/balancer/balancer.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context) (err error) {
9393
var (
9494
address = "ydb:///" + b.driverConfig.Endpoint()
9595
onDone = trace.DriverOnBalancerClusterDiscoveryAttempt(
96-
b.driverConfig.Trace(), &ctx, stack.FunctionID(0), address,
96+
b.driverConfig.Trace(), &ctx,
97+
stack.FunctionID(""),
98+
address,
9799
)
98100
endpoints []endpoint.Endpoint
99101
localDC string
@@ -166,7 +168,9 @@ func endpointsDiff(newestEndpoints []endpoint.Endpoint, previousConns []conn.Con
166168
func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, endpoints []endpoint.Endpoint, localDC string) {
167169
var (
168170
onDone = trace.DriverOnBalancerUpdate(
169-
b.driverConfig.Trace(), &ctx, stack.FunctionID(0), b.config.DetectLocalDC,
171+
b.driverConfig.Trace(), &ctx,
172+
stack.FunctionID(""),
173+
b.config.DetectLocalDC,
170174
)
171175
previousConns []conn.Conn
172176
)
@@ -202,7 +206,8 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, endpoints []end
202206

203207
func (b *Balancer) Close(ctx context.Context) (err error) {
204208
onDone := trace.DriverOnBalancerClose(
205-
b.driverConfig.Trace(), &ctx, stack.FunctionID(0),
209+
b.driverConfig.Trace(), &ctx,
210+
stack.FunctionID(""),
206211
)
207212
defer func() {
208213
onDone(err)
@@ -227,7 +232,9 @@ func New(
227232
) (b *Balancer, finalErr error) {
228233
var (
229234
onDone = trace.DriverOnBalancerInit(
230-
driverConfig.Trace(), &ctx, stack.FunctionID(0), driverConfig.Balancer().String(),
235+
driverConfig.Trace(), &ctx,
236+
stack.FunctionID(""),
237+
driverConfig.Balancer().String(),
231238
)
232239
discoveryConfig = discoveryConfig.New(append(opts,
233240
discoveryConfig.With(driverConfig.Common),
@@ -358,7 +365,8 @@ func (b *Balancer) connections() *connectionsState {
358365

359366
func (b *Balancer) getConn(ctx context.Context) (c conn.Conn, err error) {
360367
onDone := trace.DriverOnBalancerChooseEndpoint(
361-
b.driverConfig.Trace(), &ctx, stack.FunctionID(0),
368+
b.driverConfig.Trace(), &ctx,
369+
stack.FunctionID(""),
362370
)
363371
defer func() {
364372
if err == nil {

internal/conn/conn.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ func (c *conn) IsState(states ...State) bool {
9494

9595
func (c *conn) park(ctx context.Context) (err error) {
9696
onDone := trace.DriverOnConnPark(
97-
c.config.Trace(), &ctx, stack.FunctionID(0), c.Endpoint(),
97+
c.config.Trace(), &ctx,
98+
stack.FunctionID(""),
99+
c.Endpoint(),
98100
)
99101
defer func() {
100102
onDone(err)
@@ -141,7 +143,9 @@ func (c *conn) SetState(ctx context.Context, s State) State {
141143
func (c *conn) setState(ctx context.Context, s State) State {
142144
if state := State(c.state.Swap(uint32(s))); state != s {
143145
trace.DriverOnConnStateChange(
144-
c.config.Trace(), &ctx, stack.FunctionID(0), c.endpoint.Copy(), state,
146+
c.config.Trace(), &ctx,
147+
stack.FunctionID(""),
148+
c.endpoint.Copy(), state,
145149
)(s)
146150
}
147151
return s
@@ -185,9 +189,10 @@ func (c *conn) realConn(ctx context.Context) (cc *grpc.ClientConn, err error) {
185189
}
186190

187191
onDone := trace.DriverOnConnDial(
188-
c.config.Trace(), &ctx, stack.FunctionID(0), c.endpoint.Copy(),
192+
c.config.Trace(), &ctx,
193+
stack.FunctionID(""),
194+
c.endpoint.Copy(),
189195
)
190-
191196
defer func() {
192197
onDone(err)
193198
}()
@@ -265,7 +270,9 @@ func (c *conn) Close(ctx context.Context) (err error) {
265270
}
266271

267272
onDone := trace.DriverOnConnClose(
268-
c.config.Trace(), &ctx, stack.FunctionID(0), c.Endpoint(),
273+
c.config.Trace(), &ctx,
274+
stack.FunctionID(""),
275+
c.Endpoint(),
269276
)
270277
defer func() {
271278
onDone(err)
@@ -296,18 +303,16 @@ func (c *conn) Invoke(
296303
issues []trace.Issue
297304
useWrapping = UseWrapping(ctx)
298305
onDone = trace.DriverOnConnInvoke(
299-
c.config.Trace(), &ctx, stack.FunctionID(0), c.endpoint, trace.Method(method),
306+
c.config.Trace(), &ctx,
307+
stack.FunctionID(""),
308+
c.endpoint, trace.Method(method),
300309
)
301310
cc *grpc.ClientConn
302311
md = metadata.MD{}
303312
)
304-
305-
defer func() {
306-
onDone(err, issues, opID, c.GetState(), md)
307-
}()
308-
309313
defer func() {
310314
meta.CallTrailerCallback(ctx, md)
315+
onDone(err, issues, opID, c.GetState(), md)
311316
}()
312317

313318
cc, err = c.realConn(ctx)
@@ -378,7 +383,9 @@ func (c *conn) NewStream(
378383
) (_ grpc.ClientStream, err error) {
379384
var (
380385
streamRecv = trace.DriverOnConnNewStream(
381-
c.config.Trace(), &ctx, stack.FunctionID(0), c.endpoint.Copy(), trace.Method(method),
386+
c.config.Trace(), &ctx,
387+
stack.FunctionID(""),
388+
c.endpoint.Copy(), trace.Method(method),
382389
)
383390
useWrapping = UseWrapping(ctx)
384391
cc *grpc.ClientConn

internal/conn/pool.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func (p *Pool) Ban(ctx context.Context, cc Conn, cause error) {
9090
}
9191

9292
trace.DriverOnConnBan(
93-
p.config.Trace(), &ctx, stack.FunctionID(0), e, cc.GetState(), cause,
93+
p.config.Trace(), &ctx,
94+
stack.FunctionID(""),
95+
e, cc.GetState(), cause,
9496
)(cc.SetState(ctx, Banned))
9597
}
9698

@@ -110,7 +112,9 @@ func (p *Pool) Allow(ctx context.Context, cc Conn) {
110112
}
111113

112114
trace.DriverOnConnAllow(
113-
p.config.Trace(), &ctx, stack.FunctionID(0), e, cc.GetState(),
115+
p.config.Trace(), &ctx,
116+
stack.FunctionID(""),
117+
e, cc.GetState(),
114118
)(cc.Unban(ctx))
115119
}
116120

@@ -120,7 +124,7 @@ func (p *Pool) Take(context.Context) error {
120124
}
121125

122126
func (p *Pool) Release(ctx context.Context) (finalErr error) {
123-
onDone := trace.DriverOnPoolRelease(p.config.Trace(), &ctx, stack.FunctionID(0))
127+
onDone := trace.DriverOnPoolRelease(p.config.Trace(), &ctx, stack.FunctionID(""))
124128
defer func() {
125129
onDone(finalErr)
126130
}()
@@ -201,7 +205,7 @@ func (p *Pool) collectConns() []*conn {
201205
}
202206

203207
func NewPool(ctx context.Context, config Config) *Pool {
204-
onDone := trace.DriverOnPoolNew(config.Trace(), &ctx, stack.FunctionID(0))
208+
onDone := trace.DriverOnPoolNew(config.Trace(), &ctx, stack.FunctionID(""))
205209
defer onDone()
206210

207211
p := &Pool{

internal/discovery/discovery.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ type Client struct {
3939
func (c *Client) Discover(ctx context.Context) (endpoints []endpoint.Endpoint, err error) {
4040
var (
4141
onDone = trace.DiscoveryOnDiscover(
42-
c.config.Trace(), &ctx, stack.FunctionID(0), c.config.Endpoint(), c.config.Database(),
42+
c.config.Trace(), &ctx,
43+
stack.FunctionID(""),
44+
c.config.Endpoint(), c.config.Database(),
4345
)
4446
request = Ydb_Discovery.ListEndpointsRequest{
4547
Database: c.config.Database(),
4648
}
4749
response *Ydb_Discovery.ListEndpointsResponse
4850
result Ydb_Discovery.ListEndpointsResult
51+
location string
4952
)
50-
51-
var location string
5253
defer func() {
5354
nodes := make([]trace.EndpointInfo, 0, len(endpoints))
5455
for _, e := range endpoints {
@@ -100,7 +101,7 @@ func (c *Client) Discover(ctx context.Context) (endpoints []endpoint.Endpoint, e
100101

101102
func (c *Client) WhoAmI(ctx context.Context) (whoAmI *discovery.WhoAmI, err error) {
102103
var (
103-
onDone = trace.DiscoveryOnWhoAmI(c.config.Trace(), &ctx, stack.FunctionID(0))
104+
onDone = trace.DiscoveryOnWhoAmI(c.config.Trace(), &ctx, stack.FunctionID(""))
104105
request = Ydb_Discovery.WhoAmIRequest{}
105106
response *Ydb_Discovery.WhoAmIResponse
106107
whoAmIResultResult Ydb_Discovery.WhoAmIResult

internal/meta/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (m *Meta) meta(ctx context.Context) (_ metadata.MD, err error) {
108108

109109
var token string
110110

111-
done := trace.DriverOnGetCredentials(m.trace, &ctx, stack.FunctionID(0))
111+
done := trace.DriverOnGetCredentials(m.trace, &ctx, stack.FunctionID(""))
112112
defer func() {
113113
done(token, err)
114114
}()

internal/repeater/repeater.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ func (r *repeater) wakeUp(ctx context.Context, e Event) (err error) {
145145

146146
ctx = WithEvent(ctx, e)
147147

148-
onDone := trace.DriverOnRepeaterWakeUp(r.trace, &ctx, stack.FunctionID(0), r.name, e)
148+
onDone := trace.DriverOnRepeaterWakeUp(r.trace, &ctx,
149+
stack.FunctionID(""),
150+
r.name, e,
151+
)
149152
defer func() {
150153
onDone(err)
151154

0 commit comments

Comments
 (0)