Skip to content

Commit

Permalink
infoschema: print line number in parser slow log error for locating t…
Browse files Browse the repository at this point in the history
…he wrong line (#12781)
  • Loading branch information
crazycs520 authored and sre-bot committed Nov 13, 2019
1 parent 7f63a80 commit f7067ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 7 additions & 5 deletions infoschema/slow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func ParseSlowLog(tz *time.Location, reader *bufio.Reader) ([][]types.Datum, err
var rows [][]types.Datum
startFlag := false
var st *slowQueryTuple
lineNum := 0
for {
lineNum++
lineByte, err := getOneLine(reader)
if err != nil {
if err == io.EOF {
Expand All @@ -116,7 +118,7 @@ func ParseSlowLog(tz *time.Location, reader *bufio.Reader) ([][]types.Datum, err
// Check slow log entry start flag.
if !startFlag && strings.HasPrefix(line, variable.SlowLogStartPrefixStr) {
st = &slowQueryTuple{}
err = st.setFieldValue(tz, variable.SlowLogTimeStr, line[len(variable.SlowLogStartPrefixStr):])
err = st.setFieldValue(tz, variable.SlowLogTimeStr, line[len(variable.SlowLogStartPrefixStr):], lineNum)
if err != nil {
return rows, err
}
Expand All @@ -137,15 +139,15 @@ func ParseSlowLog(tz *time.Location, reader *bufio.Reader) ([][]types.Datum, err
if strings.HasSuffix(field, ":") {
field = field[:len(field)-1]
}
err = st.setFieldValue(tz, field, fieldValues[i+1])
err = st.setFieldValue(tz, field, fieldValues[i+1], lineNum)
if err != nil {
return rows, err
}
}
}
} else if strings.HasSuffix(line, variable.SlowLogSQLSuffixStr) {
// Get the sql string, and mark the start flag to false.
err = st.setFieldValue(tz, variable.SlowLogQuerySQLStr, string(hack.Slice(line)))
err = st.setFieldValue(tz, variable.SlowLogQuerySQLStr, string(hack.Slice(line)), lineNum)
if err != nil {
return rows, err
}
Expand Down Expand Up @@ -235,7 +237,7 @@ type slowQueryTuple struct {
plan string
}

func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string) error {
func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string, lineNum int) error {
var err error
switch field {
case variable.SlowLogTimeStr:
Expand Down Expand Up @@ -334,7 +336,7 @@ func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string)
st.sql = value
}
if err != nil {
return errors.Wrap(err, "parse slow log failed `"+field+"` error")
return errors.Wrap(err, "Parse slow log at line "+strconv.FormatInt(int64(lineNum), 10)+" failed. Field: `"+field+"`, error")
}
return nil
}
Expand Down
14 changes: 13 additions & 1 deletion infoschema/slow_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ select * from t;
reader = bufio.NewReader(slowLog)
_, err = infoschema.ParseSlowLog(loc, reader)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "parse slow log failed `Succ` error: strconv.ParseBool: parsing \"abc\": invalid syntax")
c.Assert(err.Error(), Equals, "Parse slow log at line 2 failed. Field: `Succ`, error: strconv.ParseBool: parsing \"abc\": invalid syntax")
}

func (s *testSuite) TestSlowLogParseTime(c *C) {
Expand Down Expand Up @@ -172,4 +172,16 @@ select * from t;`)
c.Assert(err, IsNil)
_, err = infoschema.ParseSlowLog(loc, scanner)
c.Assert(err, IsNil)

// Test parser error.
slowLog = bytes.NewBufferString(
`# Time: 2019-05-12-11:23:29.614327491 +0800
# Txn_start_ts: 405888132465033227#
`)

scanner = bufio.NewReader(slowLog)
_, err = infoschema.ParseSlowLog(loc, scanner)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Parse slow log at line 2 failed. Field: `Txn_start_ts`, error: strconv.ParseUint: parsing \"405888132465033227#\": invalid syntax")

}

0 comments on commit f7067ea

Please sign in to comment.