Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/stmtsummary: discard the plan if it is too long and enlarge the tidb_stmt_summary_max_stmt_count value to 3000 (#25843) #25873

Merged
merged 6 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,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 @@ -448,7 +448,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 @@ -615,10 +615,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 @@ -423,6 +424,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