Skip to content

Commit

Permalink
executor: avoid log empty binary plan into slow log (#36619) (#36625)
Browse files Browse the repository at this point in the history
close #36617
  • Loading branch information
ti-srebot authored Jul 28, 2022
1 parent 1a89a6a commit 5772997
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,9 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
binaryPlan := ""
if variable.GenerateBinaryPlan.Load() {
binaryPlan = getBinaryPlan(a.Ctx)
binaryPlan = variable.SlowLogBinaryPlanPrefix + binaryPlan + variable.SlowLogPlanSuffix
if len(binaryPlan) > 0 {
binaryPlan = variable.SlowLogBinaryPlanPrefix + binaryPlan + variable.SlowLogPlanSuffix
}
}

resultRows := GetResultRowsCount(stmtCtx, a.Plan)
Expand Down
30 changes: 30 additions & 0 deletions planner/core/binary_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package core_test
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -502,3 +503,32 @@ func TestInvalidDecodeBinaryPlan(t *testing.T) {
tk.MustQuery(`select tidb_decode_binary_plan('` + str3 + `')`).Check(testkit.Rows(""))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 proto: illegal wireType 7"))
}

func TestUnnecessaryBinaryPlanInSlowLog(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
require.True(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil))

originCfg := config.GetGlobalConfig()
newCfg := *originCfg
f, err := os.CreateTemp("", "tidb-slow-*.log")
require.NoError(t, err)
newCfg.Log.SlowQueryFile = f.Name()
config.StoreGlobalConfig(&newCfg)
defer func() {
config.StoreGlobalConfig(originCfg)
require.NoError(t, f.Close())
require.NoError(t, os.Remove(newCfg.Log.SlowQueryFile))
}()
require.NoError(t, logutil.InitLogger(newCfg.Log.ToLogConfig()))
tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name()))

tk.MustExec("use test")
tk.MustExec("drop table if exists th")
tk.MustExec("set global tidb_slow_log_threshold = 1;")
tk.MustExec("create table th (i int, a int,b int, c int, index (a)) partition by hash (a) partitions 100;")
slowLogBytes, err := ioutil.ReadAll(f)
require.NoError(t, err)
require.NotContains(t, string(slowLogBytes), `tidb_decode_binary_plan('')`)
}

0 comments on commit 5772997

Please sign in to comment.