Skip to content

Commit 4620df6

Browse files
authored
executor,store/copr: trace the buildCopTask and limitExec close duration when it's slow (#40579)
ref #40441
1 parent b9fb22d commit 4620df6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

executor/executor.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,9 @@ type LimitExec struct {
13931393

13941394
// columnIdxsUsedByChild keep column indexes of child executor used for inline projection
13951395
columnIdxsUsedByChild []int
1396+
1397+
// Log the close time when opentracing is enabled.
1398+
span opentracing.Span
13961399
}
13971400

13981401
// Next implements the Executor Next interface.
@@ -1470,13 +1473,29 @@ func (e *LimitExec) Open(ctx context.Context) error {
14701473
e.childResult = tryNewCacheChunk(e.children[0])
14711474
e.cursor = 0
14721475
e.meetFirstBatch = e.begin == 0
1476+
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
1477+
e.span = span
1478+
}
14731479
return nil
14741480
}
14751481

14761482
// Close implements the Executor Close interface.
14771483
func (e *LimitExec) Close() error {
1484+
start := time.Now()
1485+
14781486
e.childResult = nil
1479-
return e.baseExecutor.Close()
1487+
err := e.baseExecutor.Close()
1488+
1489+
elapsed := time.Since(start)
1490+
if elapsed > time.Millisecond {
1491+
logutil.BgLogger().Info("limit executor close takes a long time",
1492+
zap.Duration("elapsed", elapsed))
1493+
if e.span != nil {
1494+
span1 := e.span.Tracer().StartSpan("limitExec.Close", opentracing.ChildOf(e.span.Context()), opentracing.StartTime(start))
1495+
defer span1.Finish()
1496+
}
1497+
}
1498+
return err
14801499
}
14811500

14821501
func (e *LimitExec) adjustRequiredRows(chk *chunk.Chunk) *chunk.Chunk {

store/copr/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ go_library(
3434
"//util/trxevents",
3535
"@com_github_dgraph_io_ristretto//:ristretto",
3636
"@com_github_gogo_protobuf//proto",
37+
"@com_github_opentracing_opentracing_go//:opentracing-go",
3738
"@com_github_pingcap_errors//:errors",
3839
"@com_github_pingcap_failpoint//:failpoint",
3940
"@com_github_pingcap_kvproto//pkg/coprocessor",

store/copr/coprocessor.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"unsafe"
2727

2828
"github.com/gogo/protobuf/proto"
29+
"github.com/opentracing/opentracing-go"
2930
"github.com/pingcap/errors"
3031
"github.com/pingcap/failpoint"
3132
"github.com/pingcap/kvproto/pkg/coprocessor"
@@ -384,12 +385,20 @@ func buildCopTasks(bo *Backoffer, cache *RegionCache, ranges *KeyRanges, req *kv
384385
builder.reverse()
385386
}
386387
tasks := builder.build()
387-
if elapsed := time.Since(start); elapsed > time.Millisecond*500 {
388+
elapsed := time.Since(start)
389+
if elapsed > time.Millisecond*500 {
388390
logutil.BgLogger().Warn("buildCopTasks takes too much time",
389391
zap.Duration("elapsed", elapsed),
390392
zap.Int("range len", rangesLen),
391393
zap.Int("task len", len(tasks)))
392394
}
395+
if elapsed > time.Millisecond {
396+
ctx := bo.GetCtx()
397+
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
398+
span1 := span.Tracer().StartSpan("copr.buildCopTasks", opentracing.ChildOf(span.Context()), opentracing.StartTime(start))
399+
defer span1.Finish()
400+
}
401+
}
393402
metrics.TxnRegionsNumHistogramWithCoprocessor.Observe(float64(builder.regionNum()))
394403
return tasks, nil
395404
}

0 commit comments

Comments
 (0)