Skip to content

Commit ac4afd2

Browse files
authored
Merge pull request #187 from ydb-platform/remove-context-trace
Remove context trace
2 parents bb2565c + 0e01831 commit ac4afd2

File tree

12 files changed

+63
-176
lines changed

12 files changed

+63
-176
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
* Refactored gtrace tool for generate `Compose` options
1+
* Removed redundant `trace.With{Table,Driver,Retry}` and `trace.Context{Table,Driver,Retry}` funcs
2+
* Refactored tool `gtrace` for generate `Compose` options
23
* Added panic recover on trace calls in `Compose` call step
34
* Added `trace.With{Discovery,Driver,Coordination,Ratelimiter,Table,Scheme,Scripting}PanicCallback` options
45
* Added `ydb.WithPanicCallback` option

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func defaultConfig() (c *config) {
342342
return builder.New(
343343
ctx,
344344
address,
345-
trace.ContextDriver(ctx).Compose(c.trace),
345+
c.trace,
346346
)
347347
},
348348
),

internal/conn/conn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (c *conn) GetState() (s State) {
176176

177177
func (c *conn) take(ctx context.Context) (cc *grpc.ClientConn, err error) {
178178
onDone := trace.DriverOnConnTake(
179-
trace.ContextDriver(ctx).Compose(c.config.Trace()),
179+
c.config.Trace(),
180180
&ctx,
181181
c.endpoint.Copy(),
182182
)
@@ -328,7 +328,7 @@ func (c *conn) Invoke(
328328
issues []trace.Issue
329329
wrapping = needWrapping(ctx)
330330
onDone = trace.DriverOnConnInvoke(
331-
trace.ContextDriver(ctx).Compose(c.config.Trace()),
331+
c.config.Trace(),
332332
&ctx,
333333
c.endpoint,
334334
trace.Method(method),
@@ -395,7 +395,7 @@ func (c *conn) NewStream(
395395
) (_ grpc.ClientStream, err error) {
396396
var (
397397
streamRecv = trace.DriverOnConnNewStream(
398-
trace.ContextDriver(ctx).Compose(c.config.Trace()),
398+
c.config.Trace(),
399399
&ctx,
400400
c.endpoint.Copy(),
401401
trace.Method(method),

internal/conn/pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (p *pool) Pessimize(ctx context.Context, cc Conn, cause error) {
9191
}
9292

9393
trace.DriverOnPessimizeNode(
94-
trace.ContextDriver(ctx).Compose(p.config.Trace()),
94+
p.config.Trace(),
9595
&ctx,
9696
e,
9797
cc.GetState(),

internal/db/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func New(
5151
opts ...discoveryConfig.Option,
5252
) (_ Connection, err error) {
5353
onDone := trace.DriverOnInit(
54-
trace.ContextDriver(ctx).Compose(c.Trace()),
54+
c.Trace(),
5555
&ctx,
5656
c.Endpoint(),
5757
c.Database(),

internal/meta/meta.go

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

121121
var token string
122122

123-
getCredentialsDone := trace.DriverOnGetCredentials(trace.ContextDriver(ctx).Compose(m.trace), &ctx)
123+
getCredentialsDone := trace.DriverOnGetCredentials(m.trace, &ctx)
124124
defer func() {
125125
getCredentialsDone(token, err)
126126
}()

internal/table/client.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func newClient(
6262
builder SessionBuilder,
6363
config config.Config,
6464
) *client {
65-
onDone := trace.TableOnInit(config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
65+
onDone := trace.TableOnInit(config.Trace(), &ctx)
6666
if builder == nil {
6767
builder = func(ctx context.Context) (s Session, err error) {
6868
return newSession(ctx, cc, config)
@@ -213,7 +213,7 @@ func (c *client) createSession(ctx context.Context) (s Session, err error) {
213213
c.config.CreateSessionTimeout(),
214214
)
215215

216-
onDone := trace.TableOnPoolSessionNew(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
216+
onDone := trace.TableOnPoolSessionNew(c.config.Trace(), &ctx)
217217

218218
defer func() {
219219
onDone(s, err)
@@ -285,15 +285,28 @@ func (c *client) createSession(ctx context.Context) (s Session, err error) {
285285
}
286286
}
287287

288-
// Get returns first idle session from the client and removes it from
289-
// there. If no items stored in client it creates new one returns it.
290-
func (c *client) Get(ctx context.Context) (s Session, err error) {
288+
type getOptions struct {
289+
t trace.Table
290+
}
291+
292+
type getOption func(o *getOptions)
293+
294+
func withTrace(t trace.Table) getOption {
295+
return func(o *getOptions) {
296+
o.t = o.t.Compose(t)
297+
}
298+
}
299+
300+
func (c *client) get(ctx context.Context, opts ...getOption) (s Session, err error) {
291301
var (
292302
i = 0
293-
t = c.config.Trace().Compose(trace.ContextTable(ctx))
303+
o = getOptions{t: c.config.Trace()}
294304
)
305+
for _, opt := range opts {
306+
opt(&o)
307+
}
295308

296-
onDone := trace.TableOnPoolGet(t, &ctx)
309+
onDone := trace.TableOnPoolGet(o.t, &ctx)
297310
defer func() {
298311
onDone(s, i, err)
299312
}()
@@ -329,7 +342,7 @@ func (c *client) Get(ctx context.Context) (s Session, err error) {
329342
// are less than maximum amount of touched session. That is, we want to
330343
// be fair here and not to lock more goroutines than we could ship
331344
// session to.
332-
s, err = c.waitFromCh(ctx, t)
345+
s, err = c.waitFromCh(ctx, o.t)
333346
if err != nil {
334347
err = errors.WithStackTrace(err)
335348
}
@@ -345,6 +358,12 @@ func (c *client) Get(ctx context.Context) (s Session, err error) {
345358
return s, nil
346359
}
347360

361+
// Get returns first idle session from the client and removes it from
362+
// there. If no items stored in client it creates new one returns it.
363+
func (c *client) Get(ctx context.Context) (s Session, err error) {
364+
return c.get(ctx)
365+
}
366+
348367
func (c *client) waitFromCh(ctx context.Context, t trace.Table) (s Session, err error) {
349368
var (
350369
ch *chan Session
@@ -403,7 +422,7 @@ func (c *client) waitFromCh(ctx context.Context, t trace.Table) (s Session, err
403422
// Get() or Take() calls. In other way it will produce unexpected behavior or
404423
// panic.
405424
func (c *client) Put(ctx context.Context, s Session) (err error) {
406-
onDone := trace.TableOnPoolPut(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx, s)
425+
onDone := trace.TableOnPoolPut(c.config.Trace(), &ctx, s)
407426
defer func() {
408427
onDone(err)
409428
}()
@@ -447,7 +466,7 @@ func (c *client) Put(ctx context.Context, s Session) (err error) {
447466
// It returns first error occurred during stale sessions' deletion.
448467
// Note that even on error it calls Close() on each session.
449468
func (c *client) Close(ctx context.Context) (err error) {
450-
onDone := trace.TableOnClose(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
469+
onDone := trace.TableOnClose(c.config.Trace(), &ctx)
451470
defer func() {
452471
onDone(err)
453472
}()
@@ -851,7 +870,7 @@ func (c *client) closeSession(ctx context.Context, s Session, opts ...closeSessi
851870
}
852871

853872
if h.withTrace {
854-
onDone := trace.TableOnPoolSessionClose(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx, s)
873+
onDone := trace.TableOnPoolSessionClose(c.config.Trace(), &ctx, s)
855874
defer onDone()
856875
}
857876

internal/table/client_test.go

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,14 @@ func TestSessionPoolCloseWhenWaiting(t *testing.T) {
195195
mustGetSession(t, p)
196196

197197
go func() {
198-
_, err := p.Get(
199-
trace.WithTable(
200-
context.Background(),
201-
trace.Table{
202-
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
203-
get <- struct{}{}
204-
return nil
205-
},
198+
_, err := p.get(
199+
context.Background(),
200+
withTrace(trace.Table{
201+
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
202+
get <- struct{}{}
203+
return nil
206204
},
207-
),
205+
}),
208206
)
209207
got <- err
210208
}()
@@ -326,7 +324,7 @@ func TestSessionPoolClose(t *testing.T) {
326324
}
327325

328326
func TestRaceWgClosed(t *testing.T) {
329-
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
327+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
330328
defer cancel()
331329

332330
defer func() {
@@ -622,20 +620,18 @@ func TestSessionPoolSizeLimitOverflow(t *testing.T) {
622620
}
623621
}
624622
go func() {
625-
session, err := p.Get(
626-
trace.WithTable(
627-
context.Background(),
628-
trace.Table{
629-
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
630-
get <- struct{}{}
631-
return nil
632-
},
633-
OnPoolWait: func(trace.TablePoolWaitStartInfo) func(trace.TablePoolWaitDoneInfo) {
634-
wait <- struct{}{}
635-
return nil
636-
},
623+
session, err := p.get(
624+
context.Background(),
625+
withTrace(trace.Table{
626+
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
627+
get <- struct{}{}
628+
return nil
629+
},
630+
OnPoolWait: func(trace.TablePoolWaitStartInfo) func(trace.TablePoolWaitDoneInfo) {
631+
wait <- struct{}{}
632+
return nil
637633
},
638-
),
634+
}),
639635
)
640636
got <- sessionAndError{session, err}
641637
}()
@@ -1239,17 +1235,7 @@ func mustResetTimer(t *testing.T, ch <-chan time.Duration, exp time.Duration) {
12391235
func mustCreateSession(t *testing.T, p *client) Session {
12401236
wg := sync.WaitGroup{}
12411237
defer wg.Wait()
1242-
s, err := p.createSession(trace.WithTable(
1243-
context.Background(),
1244-
trace.Table{
1245-
OnPoolSessionNew: func(info trace.TablePoolSessionNewStartInfo) func(trace.TablePoolSessionNewDoneInfo) {
1246-
wg.Add(1)
1247-
return func(info trace.TablePoolSessionNewDoneInfo) {
1248-
wg.Done()
1249-
}
1250-
},
1251-
},
1252-
))
1238+
s, err := p.createSession(context.Background())
12531239
if err != nil {
12541240
t.Helper()
12551241
t.Fatalf("%s: %v", caller(), err)
@@ -1272,27 +1258,7 @@ func mustPutSession(t *testing.T, p *client, s Session) {
12721258
wg := sync.WaitGroup{}
12731259
defer wg.Wait()
12741260
if err := p.Put(
1275-
trace.WithTable(
1276-
context.Background(),
1277-
trace.Table{
1278-
OnPoolPut: func(info trace.TablePoolPutStartInfo) func(trace.TablePoolPutDoneInfo) {
1279-
wg.Add(1)
1280-
return func(info trace.TablePoolPutDoneInfo) {
1281-
wg.Done()
1282-
}
1283-
},
1284-
OnPoolSessionClose: func(
1285-
info trace.TablePoolSessionCloseStartInfo,
1286-
) func(
1287-
doneInfo trace.TablePoolSessionCloseDoneInfo,
1288-
) {
1289-
wg.Add(1)
1290-
return func(info trace.TablePoolSessionCloseDoneInfo) {
1291-
wg.Done()
1292-
}
1293-
},
1294-
},
1295-
),
1261+
context.Background(),
12961262
s,
12971263
); err != nil {
12981264
t.Helper()
@@ -1303,23 +1269,7 @@ func mustPutSession(t *testing.T, p *client, s Session) {
13031269
func mustClose(t *testing.T, p *client) {
13041270
wg := sync.WaitGroup{}
13051271
defer wg.Wait()
1306-
if err := p.Close(
1307-
trace.WithTable(
1308-
context.Background(),
1309-
trace.Table{
1310-
OnPoolSessionClose: func(
1311-
info trace.TablePoolSessionCloseStartInfo,
1312-
) func(
1313-
doneInfo trace.TablePoolSessionCloseDoneInfo,
1314-
) {
1315-
wg.Add(1)
1316-
return func(info trace.TablePoolSessionCloseDoneInfo) {
1317-
wg.Done()
1318-
}
1319-
},
1320-
},
1321-
),
1322-
); err != nil {
1272+
if err := p.Close(context.Background()); err != nil {
13231273
t.Helper()
13241274
t.Fatalf("%s: %v", caller(), err)
13251275
}

internal/table/retry_test.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/ydb-platform/ydb-go-sdk/v3/table"
1818
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
1919
"github.com/ydb-platform/ydb-go-sdk/v3/testutil"
20-
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
2120
)
2221

2322
func TestRetryerBackoffRetryCancelation(t *testing.T) {
@@ -212,7 +211,6 @@ func TestRetryerImmediateReturn(t *testing.T) {
212211
// We are testing all suspentions of custom operation func against to all deadline
213212
// timeouts - all sub-tests must have latency less than timeouts (+tolerance)
214213
func TestRetryContextDeadline(t *testing.T) {
215-
tolerance := 10 * time.Millisecond
216214
timeouts := []time.Duration{
217215
50 * time.Millisecond,
218216
100 * time.Millisecond,
@@ -318,31 +316,8 @@ func TestRetryContextDeadline(t *testing.T) {
318316
t.Run(fmt.Sprintf("Timeout=%v,Sleep=%v", timeout, sleep), func(t *testing.T) {
319317
ctx, cancel := context.WithTimeout(context.Background(), timeout)
320318
defer cancel()
321-
start := time.Now()
322319
_ = do(
323-
trace.WithRetry(
324-
ctx,
325-
trace.Retry{
326-
OnRetry: func(
327-
info trace.RetryLoopStartInfo,
328-
) func(
329-
intermediateInfo trace.RetryLoopIntermediateInfo,
330-
) func(
331-
trace.RetryLoopDoneInfo,
332-
) {
333-
return func(
334-
info trace.RetryLoopIntermediateInfo,
335-
) func(trace.RetryLoopDoneInfo) {
336-
return func(info trace.RetryLoopDoneInfo) {
337-
latency := time.Since(start)
338-
if latency-timeouts[i] > tolerance {
339-
t.Errorf("unexpected latency: %v", latency)
340-
}
341-
}
342-
}
343-
},
344-
},
345-
),
320+
ctx,
346321
p,
347322
func(ctx context.Context, _ table.Session) error {
348323
select {

internal/table/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s *session) isClosing() bool {
8989
}
9090

9191
func newSession(ctx context.Context, cc grpc.ClientConnInterface, config config.Config) (s Session, err error) {
92-
onDone := trace.TableOnSessionNew(config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
92+
onDone := trace.TableOnSessionNew(config.Trace(), &ctx)
9393
defer func() {
9494
onDone(s, err)
9595
}()

0 commit comments

Comments
 (0)