Skip to content

Commit

Permalink
stmtsummary: reduce the memory consumption by information_schema.stat…
Browse files Browse the repository at this point in the history
…ements_summary_evicted

Signed-off-by: crazycs520 <[email protected]>
  • Loading branch information
crazycs520 committed Oct 18, 2024
1 parent 602f980 commit 0025c8c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
13 changes: 6 additions & 7 deletions pkg/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 @@ -199,7 +198,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 pkg/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()
}

0 comments on commit 0025c8c

Please sign in to comment.