Skip to content

Commit

Permalink
executor: fix unordered trace events in row format (#12314) (#12333)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklove authored and sre-bot committed Sep 24, 2019
1 parent fb637ba commit 97eec5a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions executor/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ func dfsTree(t *appdash.Trace, prefix string, isLast bool, chk *chunk.Chunk) {
chk.AppendString(1, start.Format("15:04:05.000000"))
chk.AppendString(2, duration.String())

// Sort events by their start time
sort.Slice(t.Sub, func(i, j int) bool {
var istart, jstart time.Time
if ievent, err := t.Sub[i].TimespanEvent(); err == nil {
istart = ievent.Start()
}
if jevent, err := t.Sub[j].TimespanEvent(); err == nil {
jstart = jevent.Start()
}
return istart.Before(jstart)
})

for i, sp := range t.Sub {
dfsTree(sp, newPrefix, i == (len(t.Sub))-1 /*last element of array*/, chk)
}
Expand Down
27 changes: 26 additions & 1 deletion executor/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package executor_test

import (
"strings"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/util/testkit"
)
Expand All @@ -39,6 +41,29 @@ func (s *testSuite1) TestTraceExec(c *C) {
// | └─recordSet.Next | 22:08:38.249340 | 155.317µs |
// +---------------------------+-----------------+------------+
rows = tk.MustQuery("trace format='row' select * from trace where id = 0;").Rows()

c.Assert(len(rows) > 1, IsTrue)
c.Assert(rowsOrdered(rows), IsTrue)
}

func rowsOrdered(rows [][]interface{}) (ordered bool) {
for idx := range rows {
if idx == 0 || !isSibling(rows[idx-1][0].(string), rows[idx][0].(string)) {
continue
}

if rows[idx-1][1].(string) > rows[idx][1].(string) {
return false
}
}
return true
}

func isSibling(x string, y string) bool {
indexF := func(c rune) bool {
if (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') {
return false
}
return true
}
return strings.IndexFunc(x, indexF) == strings.IndexFunc(y, indexF)
}

0 comments on commit 97eec5a

Please sign in to comment.