Skip to content

Commit

Permalink
executor: fix issue that query slow_query table return wrong result (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Oct 28, 2024
1 parent e6459b3 commit 484c1ae
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
16 changes: 12 additions & 4 deletions pkg/executor/cluster_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,23 @@ select 7;`
},
{
sql: "select count(*),min(time),max(time) from %s",
result: []string{"1|2020-05-14 19:03:54.314615|2020-05-14 19:03:54.314615"},
result: []string{"7|2020-02-15 18:00:01.000000|2020-05-14 19:03:54.314615"},
},
{
sql: "select count(*),min(time) from %s where time > '2020-02-16 20:00:00'",
result: []string{"1|2020-02-17 18:00:05.000000"},
sql: "select count(*),min(time),max(time) from %s where time > '2020-02-16 20:00:00'",
result: []string{"2|2020-02-17 18:00:05.000000|2020-05-14 19:03:54.314615"},
},
{
sql: "select count(*) from %s where time > '2020-02-17 20:00:00'",
result: []string{"0"},
result: []string{"1"},
},
{
sql: "select count(*) from %s where time > '1980-01-11 00:00:00'",
result: []string{"7"},
},
{
sql: "select count(*) from %s where time < '2024-01-01 00:00:00'",
result: []string{"7"},
},
{
sql: "select query from %s where time > '2019-01-26 21:51:00' and time < now()",
Expand Down
52 changes: 22 additions & 30 deletions pkg/executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,18 +926,6 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
e.stats.totalFileNum = totalFileNum
}()
}
if e.extractor == nil || !e.extractor.Enable {
totalFileNum = 1
//nolint: gosec
file, err := os.Open(logFilePath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return []logFile{{file: file}}, nil
}
var logFiles []logFile
logDir := filepath.Dir(logFilePath)
ext := filepath.Ext(logFilePath)
Expand Down Expand Up @@ -982,15 +970,17 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
return handleErr(err)
}
start := types.NewTime(types.FromGoTime(fileStartTime), mysql.TypeDatetime, types.MaxFsp)
notInAllTimeRanges := true
for _, tr := range e.checker.timeRanges {
if start.Compare(tr.endTime) <= 0 {
notInAllTimeRanges = false
break
if e.checker.enableTimeCheck {
notInAllTimeRanges := true
for _, tr := range e.checker.timeRanges {
if start.Compare(tr.endTime) <= 0 {
notInAllTimeRanges = false
break
}
}
if notInAllTimeRanges {
return nil
}
}
if notInAllTimeRanges {
return nil
}

// If we want to get the end time from a compressed file,
Expand All @@ -1001,16 +991,18 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
if err != nil {
return handleErr(err)
}
end := types.NewTime(types.FromGoTime(fileEndTime), mysql.TypeDatetime, types.MaxFsp)
inTimeRanges := false
for _, tr := range e.checker.timeRanges {
if !(start.Compare(tr.endTime) > 0 || end.Compare(tr.startTime) < 0) {
inTimeRanges = true
break
if e.checker.enableTimeCheck {
end := types.NewTime(types.FromGoTime(fileEndTime), mysql.TypeDatetime, types.MaxFsp)
inTimeRanges := false
for _, tr := range e.checker.timeRanges {
if !(start.Compare(tr.endTime) > 0 || end.Compare(tr.startTime) < 0) {
inTimeRanges = true
break
}
}
if !inTimeRanges {
return nil
}
}
if !inTimeRanges {
return nil
}
}
_, err = file.Seek(0, io.SeekStart)
Expand Down Expand Up @@ -1038,7 +1030,7 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
// Assume no time range overlap in log files and remove unnecessary log files for compressed files.
var ret []logFile
for i, file := range logFiles {
if i == len(logFiles)-1 || !file.compressed {
if i == len(logFiles)-1 || !file.compressed || !e.checker.enableTimeCheck {
ret = append(ret, file)
continue
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/executor/slow_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,12 @@ select 7;`
{
startTime: "",
endTime: "",
files: []string{fileName3},
files: []string{fileName1, fileName2, fileName3},
querys: []string{
"select 1;",
"select 2;",
"select 3;",
"select 4;",
"select 5;",
"select 6;",
"select 7;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestSelectClusterTable(t *testing.T) {
tk.MustQuery("select query_time, conn_id, session_alias from `CLUSTER_SLOW_QUERY` order by time desc limit 1").Check(testkit.Rows("25.571605962 40507 alias123"))
tk.MustQuery("select count(*) from `CLUSTER_SLOW_QUERY` group by digest").Check(testkit.Rows("1", "1"))
tk.MustQuery("select digest, count(*) from `CLUSTER_SLOW_QUERY` group by digest order by digest").Check(testkit.Rows("124acb3a0bec903176baca5f9da00b4e7512a41c93b417923f26502edeb324cc 1", "42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772 1"))
tk.MustQuery(`select length(query) as l,time from information_schema.cluster_slow_query where time > "2019-02-12 19:33:56" order by abs(l) desc limit 10;`).Check(testkit.Rows("21 2019-02-12 19:33:56.571953"))
tk.MustQuery(`select length(query) as l,time from information_schema.cluster_slow_query where time > "2019-02-12 19:33:56" order by abs(l) desc limit 10;`).Check(testkit.Rows("21 2019-02-12 19:33:56.571953", "16 2021-09-08 14:39:54.506967"))
tk.MustQuery("select count(*) from `CLUSTER_SLOW_QUERY` where time > now() group by digest").Check(testkit.Rows())
re := tk.MustQuery("select * from `CLUSTER_statements_summary`")
require.NotNil(t, re)
Expand Down
13 changes: 5 additions & 8 deletions pkg/planner/core/memtable_predicate_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1305,22 +1305,19 @@ func (e *SlowQueryExtractor) Extract(ctx base.PlanContext,
}

func (e *SlowQueryExtractor) setTimeRange(start, end int64) {
const defaultSlowQueryDuration = 24 * time.Hour
var startTime, endTime time.Time
if start == 0 && end == 0 {
return
}
var startTime, endTime time.Time
if start != 0 {
startTime = e.convertToTime(start)
} else {
startTime, _ = types.MinDatetime.GoTime(time.UTC)
}
if end != 0 {
endTime = e.convertToTime(end)
}
if start == 0 {
startTime = endTime.Add(-defaultSlowQueryDuration)
}
if end == 0 {
endTime = startTime.Add(defaultSlowQueryDuration)
} else {
endTime, _ = types.MaxDatetime.GoTime(time.UTC)
}
timeRange := &TimeRange{
StartTime: startTime,
Expand Down

0 comments on commit 484c1ae

Please sign in to comment.