-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: tidb tracing prototype #7016
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a8821c8
tidb tracing prototype
zhexuany 92d1764
add benchmark for noop
zhexuany 3e61f1b
remove old span system
zhexuany 93b420a
rename file and add tree relationship tesdt
zhexuany b8abe7e
tree format
zhexuany 7a9d6db
remvoe useless code
zhexuany b46a727
fix fmt issue
zhexuany 84b9d78
iterate all data until chk's row count is zero
zhexuany 3fe6951
chk has 0 row count, it will not enter for loop
zhexuany 31961e6
undo format by go1.11
zhexuany c757655
Merge branch 'master' into tidb_tracing_prototype
zhexuany 70f0d9b
address comments
zhexuany 6181ca2
Merge branch 'tidb_tracing_prototype' of github.com:zhexuany/tidb int…
zhexuany cb366aa
remove unecessary import
zhexuany 46e681e
add more tests
zhexuany e0d7cba
add a todo
zhexuany 7657e5e
Merge branch 'master' into tidb_tracing_prototype
zhexuany 227626b
address comments
zhexuany 03c3f7a
remvoe extra line
zhexuany 507b567
move buildTrace to builder.go
zhexuany 0ccae95
remove extra imports from trace.go
zhexuany e2cb6ff
record optimize time in traceExec's Next
zhexuany 9f5b5bf
open child in next rather in open
zhexuany e49d58a
fix optimize logic mistake
zhexuany da0e06b
address comment
zhexuany 2c6aa09
use == not !=
zhexuany 1468f03
Merge branch 'master' into tidb_tracing_prototype
zz-jason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,9 +57,6 @@ type TableReaderExecutor struct { | |
|
||
// Open initialzes necessary variables for using this executor. | ||
func (e *TableReaderExecutor) Open(ctx context.Context) error { | ||
span, ctx := startSpanFollowsContext(ctx, "executor.TableReader.Open") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove this? |
||
defer span.Finish() | ||
|
||
var err error | ||
if e.corColInFilter { | ||
e.dagPB.Executors, _, err = constructDistExec(e.ctx, e.plans) | ||
|
@@ -101,11 +98,11 @@ func (e *TableReaderExecutor) Open(ctx context.Context) error { | |
// Next fills data into the chunk passed by its caller. | ||
// The task was actually done by tableReaderHandler. | ||
func (e *TableReaderExecutor) Next(ctx context.Context, chk *chunk.Chunk) error { | ||
err := e.resultHandler.nextChunk(ctx, chk) | ||
if err != nil { | ||
if err := e.resultHandler.nextChunk(ctx, chk); err != nil { | ||
e.feedback.Invalidate() | ||
return err | ||
} | ||
return errors.Trace(err) | ||
return errors.Trace(nil) | ||
} | ||
|
||
// Close implements the Executor Close interface. | ||
|
@@ -115,7 +112,7 @@ func (e *TableReaderExecutor) Close() error { | |
return errors.Trace(err) | ||
} | ||
|
||
// buildResp first build request and send it to tikv using distsql.Select. It uses SelectResut returned by the callee | ||
// buildResp first builds request and sends it to tikv using distsql.Select. It uses SelectResut returned by the callee | ||
// to fetch all results. | ||
func (e *TableReaderExecutor) buildResp(ctx context.Context, ranges []*ranger.Range) (distsql.SelectResult, error) { | ||
var builder distsql.RequestBuilder | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/juju/errors" | ||
"github.com/opentracing/basictracer-go" | ||
opentracing "github.com/opentracing/opentracing-go" | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/plan" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pingcap/tidb/util/tracing" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// TraceExec represents a root executor of trace query. | ||
type TraceExec struct { | ||
baseExecutor | ||
// CollectedSpans collects all span during execution. Span is appended via | ||
// callback method which passes into tracer implementation. | ||
CollectedSpans []basictracer.RawSpan | ||
// exhausted being true means there is no more result. | ||
exhausted bool | ||
zhexuany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// stmtNode is the real query ast tree and it is used for building real query's plan. | ||
stmtNode ast.StmtNode | ||
// rootTrace represents root span which is father of all other span. | ||
rootTrace opentracing.Span | ||
|
||
builder *executorBuilder | ||
} | ||
|
||
// Next executes real query and collects span later. | ||
func (e *TraceExec) Next(ctx context.Context, chk *chunk.Chunk) error { | ||
chk.Reset() | ||
if e.exhausted { | ||
return nil | ||
} | ||
|
||
// record how much time was spent for optimizeing plan | ||
optimizeSp := e.rootTrace.Tracer().StartSpan("plan_optimize", opentracing.FollowsFrom(e.rootTrace.Context())) | ||
stmtPlan, err := plan.Optimize(e.builder.ctx, e.stmtNode, e.builder.is) | ||
if err != nil { | ||
return err | ||
} | ||
optimizeSp.Finish() | ||
|
||
pp, ok := stmtPlan.(plan.PhysicalPlan) | ||
if !ok { | ||
return errors.New("cannot cast logical plan to physical plan") | ||
} | ||
|
||
// append select executor to trace executor | ||
stmtExec := e.builder.build(pp) | ||
|
||
e.rootTrace = tracing.NewRecordedTrace("trace_exec", func(sp basictracer.RawSpan) { | ||
e.CollectedSpans = append(e.CollectedSpans, sp) | ||
}) | ||
err = stmtExec.Open(ctx) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
stmtExecChk := stmtExec.newChunk() | ||
|
||
// store span into context | ||
ctx = opentracing.ContextWithSpan(ctx, e.rootTrace) | ||
|
||
for { | ||
if err := stmtExec.Next(ctx, stmtExecChk); err != nil { | ||
return errors.Trace(err) | ||
} | ||
if stmtExecChk.NumRows() == 0 { | ||
break | ||
} | ||
} | ||
|
||
e.rootTrace.LogKV("event", "tracing completed") | ||
e.rootTrace.Finish() | ||
var rootSpan basictracer.RawSpan | ||
|
||
treeSpans := make(map[uint64][]basictracer.RawSpan) | ||
for _, sp := range e.CollectedSpans { | ||
treeSpans[sp.ParentSpanID] = append(treeSpans[sp.ParentSpanID], sp) | ||
// if a span's parentSpanID is 0, then it is root span | ||
// this is by design | ||
if sp.ParentSpanID == 0 { | ||
rootSpan = sp | ||
} | ||
} | ||
|
||
dfsTree(rootSpan, treeSpans, "", false, chk) | ||
e.exhausted = true | ||
return nil | ||
} | ||
|
||
func dfsTree(span basictracer.RawSpan, tree map[uint64][]basictracer.RawSpan, prefix string, isLast bool, chk *chunk.Chunk) { | ||
suffix := "" | ||
spans := tree[span.Context.SpanID] | ||
var newPrefix string | ||
if span.ParentSpanID == 0 { | ||
newPrefix = prefix | ||
} else { | ||
if len(tree[span.ParentSpanID]) > 0 && !isLast { | ||
suffix = "├─" | ||
newPrefix = prefix + "│ " | ||
} else { | ||
suffix = "└─" | ||
newPrefix = prefix + " " | ||
} | ||
} | ||
|
||
chk.AppendString(0, prefix+suffix+span.Operation) | ||
chk.AppendString(1, span.Start.Format(time.StampNano)) | ||
chk.AppendString(2, span.Duration.String()) | ||
|
||
for i, sp := range spans { | ||
dfsTree(sp, tree, newPrefix, i == (len(spans))-1 /*last element of array*/, chk) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor_test | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/util/testkit" | ||
) | ||
|
||
type testTraceExec struct{} | ||
|
||
func (s *testTraceExec) SetupSuite(c *C) { | ||
} | ||
|
||
func (s *testSuite) TestTraceExec(c *C) { | ||
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustExec("use test") | ||
testSQL := `create table trace (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);` | ||
tk.MustExec(testSQL) | ||
// TODO: check result later in another PR. | ||
tk.MustExec("trace select * from trace where id = 0;") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package plan | ||
|
||
import ( | ||
"github.com/pingcap/tidb/ast" | ||
) | ||
|
||
// Trace represents a trace plan. | ||
type Trace struct { | ||
baseSchemaProducer | ||
|
||
StmtNode ast.StmtNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we store the plain text of the
select
statement to be traced? We can trace the time spent by parser with the help of this.