Skip to content

Commit

Permalink
util/stmtsummary: discard the plan if it is too long and enlarge the …
Browse files Browse the repository at this point in the history
…tidb_stmt_summary_max_stmt_count value to 3000 (#25843) (#25872)
  • Loading branch information
ti-srebot authored Jul 14, 2021
1 parent 934220e commit 67b641d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ var defaultConf = Config{
StmtSummary: StmtSummary{
Enable: true,
EnableInternalQuery: false,
MaxStmtCount: 200,
MaxStmtCount: 3000,
MaxSQLLength: 4096,
RefreshInterval: 1800,
HistorySize: 24,
Expand Down
2 changes: 1 addition & 1 deletion config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ enable = true
enable-internal-query = false

# max number of statements kept in memory.
max-stmt-count = 200
max-stmt-count = 3000

# max length of displayed normalized sql and sample sql.
max-sql-length = 4096
Expand Down
9 changes: 9 additions & 0 deletions util/plancodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const (
separatorStr = "\t"
)

var (
// PlanDiscardedEncoded indicates the discard plan because it is too long
PlanDiscardedEncoded = "[discard]"
planDiscardedDecoded = "(plan discarded because too long)"
)

var decoderPool = sync.Pool{
New: func() interface{} {
return &planDecoder{}
Expand Down Expand Up @@ -87,6 +93,9 @@ type planInfo struct {
func (pd *planDecoder) decode(planString string) (string, error) {
str, err := decompress(planString)
if err != nil {
if planString == PlanDiscardedEncoded {
return planDiscardedDecoded, nil
}
return "", err
}
return pd.buildPlanTree(str)
Expand Down
6 changes: 6 additions & 0 deletions util/plancodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ func (s *testPlanCodecSuite) TestEncodeTaskType(c *C) {
_, err = decodeTaskType("1_x")
c.Assert(err, NotNil)
}

func (s *testPlanCodecSuite) TestDecodeDiscardPlan(c *C) {
plan, err := DecodePlan(PlanDiscardedEncoded)
c.Assert(err, IsNil)
c.Assert(plan, DeepEquals, planDiscardedDecoded)
}
5 changes: 5 additions & 0 deletions util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,15 @@ func (ssbd *stmtSummaryByDigest) collectHistorySummaries(historySize int) []*stm
return ssElements
}

var maxEncodedPlanSizeInBytes = 1024 * 1024

func newStmtSummaryByDigestElement(sei *StmtExecInfo, beginTime int64, intervalSeconds int64) *stmtSummaryByDigestElement {
// sampleSQL / authUsers(sampleUser) / samplePlan / prevSQL / indexNames store the values shown at the first time,
// because it compacts performance to update every time.
samplePlan, planHint := sei.PlanGenerator()
if len(samplePlan) > maxEncodedPlanSizeInBytes {
samplePlan = plancodec.PlanDiscardedEncoded
}
ssElement := &stmtSummaryByDigestElement{
beginTime: beginTime,
sampleSQL: formatSQL(sei.OriginalSQL),
Expand Down
26 changes: 26 additions & 0 deletions util/stmtsummary/statement_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/plancodec"
)

var _ = Suite(&testStmtSummarySuite{})
Expand Down Expand Up @@ -410,6 +411,31 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) {
c.Assert(s.ssMap.summaryMap.Size(), Equals, 4)
_, ok = s.ssMap.summaryMap.Get(key)
c.Assert(ok, IsTrue)

// Test for plan too large
stmtExecInfo7 := stmtExecInfo1
stmtExecInfo7.PlanDigest = "plan_digest7"
stmtExecInfo7.PlanGenerator = func() (string, string) {
buf := make([]byte, maxEncodedPlanSizeInBytes+1)
for i := range buf {
buf[i] = 'a'
}
return string(buf), ""
}
key = &stmtSummaryByDigestKey{
schemaName: stmtExecInfo7.SchemaName,
digest: stmtExecInfo7.Digest,
planDigest: stmtExecInfo7.PlanDigest,
}
s.ssMap.AddStatement(stmtExecInfo7)
c.Assert(s.ssMap.summaryMap.Size(), Equals, 5)
v, ok := s.ssMap.summaryMap.Get(key)
c.Assert(ok, IsTrue)
stmt := v.(*stmtSummaryByDigest)
c.Assert(stmt.digest, DeepEquals, key.digest)
e := stmt.history.Back()
ssElement := e.Value.(*stmtSummaryByDigestElement)
c.Assert(ssElement.samplePlan, Equals, plancodec.PlanDiscardedEncoded)
}

func matchStmtSummaryByDigest(first, second *stmtSummaryByDigest) bool {
Expand Down

0 comments on commit 67b641d

Please sign in to comment.