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

stmtsummary: reduce the memory consumption by information_schema.statements_summary_evicted (#56722) #56945

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ func TestStmtSummaryEvictedPointGet(t *testing.T) {
tk.MustExec(fmt.Sprintf("select p from th%v where p=2333;", i%6))
}
tk.MustQuery("select EVICTED_COUNT from information_schema.statements_summary_evicted;").
Check(testkit.Rows("7"))
Check(testkit.Rows("996"))

tk.MustExec("set @@global.tidb_enable_stmt_summary=0;")
tk.MustQuery("select count(*) from information_schema.statements_summary_evicted;").
Expand Down
13 changes: 6 additions & 7 deletions util/stmtsummary/evicted.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type stmtSummaryByDigestEvictedElement struct {
beginTime int64
// endTime is the end time of current interval
endTime int64
// digestKeyMap contains *Kinds* of digest being evicted
digestKeyMap map[string]struct{}
// count is the number of digest being evicted
count int64
// otherSummary contains summed up information of evicted elements
otherSummary *stmtSummaryByDigestElement
}
Expand All @@ -54,9 +54,8 @@ func newStmtSummaryByDigestEvicted() *stmtSummaryByDigestEvicted {
// spawn a new pointer to stmtSummaryByDigestEvictedElement
func newStmtSummaryByDigestEvictedElement(beginTime int64, endTime int64) *stmtSummaryByDigestEvictedElement {
return &stmtSummaryByDigestEvictedElement{
beginTime: beginTime,
endTime: endTime,
digestKeyMap: make(map[string]struct{}),
beginTime: beginTime,
endTime: endTime,
otherSummary: &stmtSummaryByDigestElement{
beginTime: beginTime,
endTime: endTime,
Expand Down Expand Up @@ -153,7 +152,7 @@ func (ssbde *stmtSummaryByDigestEvicted) Clear() {
// add an evicted record to stmtSummaryByDigestEvictedElement
func (seElement *stmtSummaryByDigestEvictedElement) addEvicted(digestKey *stmtSummaryByDigestKey, digestValue *stmtSummaryByDigestElement) {
if digestKey != nil {
seElement.digestKeyMap[string(digestKey.Hash())] = struct{}{}
seElement.count++
addInfo(seElement.otherSummary, digestValue)
}
}
Expand Down Expand Up @@ -200,7 +199,7 @@ func (seElement *stmtSummaryByDigestEvictedElement) toEvictedCountDatum() []type
datum := types.MakeDatums(
types.NewTime(types.FromGoTime(time.Unix(seElement.beginTime, 0)), mysql.TypeTimestamp, 0),
types.NewTime(types.FromGoTime(time.Unix(seElement.endTime, 0)), mysql.TypeTimestamp, 0),
int64(len(seElement.digestKeyMap)),
seElement.count,
)
return datum
}
Expand Down
18 changes: 9 additions & 9 deletions util/stmtsummary/evicted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,19 @@ func TestSimpleStmtSummaryByDigestEvicted(t *testing.T) {
require.Equal(t, "{begin: 1, end: 2, count: 1}", getAllEvicted(ssbde))
// test insert same *kind* of digest
ssbde.AddEvicted(evictedKey, evictedValue, 1)
require.Equal(t, "{begin: 1, end: 2, count: 1}", getAllEvicted(ssbde))
require.Equal(t, "{begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))

evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("b", 1, 2)
ssbde.AddEvicted(evictedKey, evictedValue, 1)
require.Equal(t, "{begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
require.Equal(t, "{begin: 1, end: 2, count: 3}", getAllEvicted(ssbde))

evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("b", 5, 6)
ssbde.AddEvicted(evictedKey, evictedValue, 2)
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 1, end: 2, count: 3}", getAllEvicted(ssbde))

evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("b", 3, 4)
ssbde.AddEvicted(evictedKey, evictedValue, 3)
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 3, end: 4, count: 1}, {begin: 1, end: 2, count: 2}", getAllEvicted(ssbde))
require.Equal(t, "{begin: 5, end: 6, count: 1}, {begin: 3, end: 4, count: 1}, {begin: 1, end: 2, count: 3}", getAllEvicted(ssbde))

// test evicted element with multi-time range value.
ssbde = newStmtSummaryByDigestEvicted()
Expand Down Expand Up @@ -235,13 +235,13 @@ func TestStmtSummaryByDigestEvictedElement(t *testing.T) {

// test add same *kind* of values.
record.addEvicted(evictedKey, digestValue)
require.Equal(t, "{begin: 0, end: 1, count: 1}", getEvicted(record))
require.Equal(t, "{begin: 0, end: 1, count: 2}", getEvicted(record))

// test add different *kind* of values.
evictedKey, evictedValue = generateStmtSummaryByDigestKeyValue("bravo", 0, 1)
digestValue = evictedValue.history.Back().Value.(*stmtSummaryByDigestElement)
record.addEvicted(evictedKey, digestValue)
require.Equal(t, "{begin: 0, end: 1, count: 2}", getEvicted(record))
require.Equal(t, "{begin: 0, end: 1, count: 3}", getEvicted(record))
}

// test stmtSummaryByDigestEvicted.addEvicted
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestNewStmtSummaryByDigestEvictedElement(t *testing.T) {
stmtEvictedElement := newStmtSummaryByDigestEvictedElement(now, end)
require.Equal(t, now, stmtEvictedElement.beginTime)
require.Equal(t, end, stmtEvictedElement.endTime)
require.Equal(t, 0, len(stmtEvictedElement.digestKeyMap))
require.Equal(t, int64(0), stmtEvictedElement.count)
}

func TestStmtSummaryByDigestEvicted(t *testing.T) {
Expand Down Expand Up @@ -624,13 +624,13 @@ func getAllEvicted(ssdbe *stmtSummaryByDigestEvicted) string {
buf.WriteString(", ")
}
val := e.Value.(*stmtSummaryByDigestEvictedElement)
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", val.beginTime, val.endTime, len(val.digestKeyMap)))
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", val.beginTime, val.endTime, val.count))
}
return buf.String()
}

func getEvicted(ssbdee *stmtSummaryByDigestEvictedElement) string {
buf := bytes.NewBuffer(nil)
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", ssbdee.beginTime, ssbdee.endTime, len(ssbdee.digestKeyMap)))
buf.WriteString(fmt.Sprintf("{begin: %v, end: %v, count: %v}", ssbdee.beginTime, ssbdee.endTime, ssbdee.count))
return buf.String()
}