Skip to content

Commit

Permalink
executor: fix query panic on INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY wi…
Browse files Browse the repository at this point in the history
…th some slow query files (#54325)

close #54324
  • Loading branch information
tiancaiamao authored Jul 1, 2024
1 parent 585c631 commit 6006822
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 2 additions & 7 deletions pkg/executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,13 +1147,8 @@ func readLastLines(ctx context.Context, file *os.File, endCursor int64) ([]strin
lines = append(chars, lines...) // nozero

// find first '\n' or '\r'
for i := 0; i < len(chars); i++ {
// reach the line end
// the first newline may be in the line end at the first round
if i >= len(lines)-1 {
break
}
if (chars[i] == 10 || chars[i] == 13) && chars[i+1] != 10 && chars[i+1] != 13 {
for i := 0; i < len(chars)-1; i++ {
if (chars[i] == '\n' || chars[i] == '\r') && chars[i+1] != '\n' && chars[i+1] != '\r' {
firstNonNewlinePos = i + 1
break
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/executor/slow_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,27 @@ func removeFiles(fileNames []string) {
os.Remove(fileName)
}
}

func TestIssue54324(t *testing.T) {
f, err := os.CreateTemp("", "test-tidb-slow-query-issue54324")
require.NoError(t, err)
defer os.Remove(f.Name()) // clean up

w := bufio.NewWriter(f)
for i := 0; i < 8191; i++ {
w.WriteByte('x')
}
w.WriteByte('\n')
for i := 0; i < 4096; i++ {
w.WriteByte('a')
}
require.NoError(t, w.Flush())

stat, err := f.Stat()
require.NoError(t, err)
endCursor := stat.Size()
lines, readBytes, err := readLastLines(context.Background(), f, endCursor)
require.NoError(t, err)
require.Len(t, lines, 2)
require.Equal(t, readBytes, 8192+4096)
}

0 comments on commit 6006822

Please sign in to comment.