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

topsql: use new cache policy for top-n SQL #25744

Merged
merged 21 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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 sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ const (
DefTiDBTopSQLEnable = false
DefTiDBTopSQLAgentAddress = ""
DefTiDBTopSQLPrecisionSeconds = 1
DefTiDBTopSQLMaxStatementCount = 2000
DefTiDBTopSQLMaxStatementCount = 200
DefTiDBTopSQLMaxCollect = 10000
DefTiDBTopSQLReportIntervalSeconds = 60
DefTiDBEnableGlobalTemporaryTable = false
Expand Down
2 changes: 1 addition & 1 deletion util/topsql/reporter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *GRPCReportClient) Close() {
}

// sendBatchCPUTimeRecord sends a batch of TopSQL records by stream.
func (r *GRPCReportClient) sendBatchCPUTimeRecord(ctx context.Context, records map[string]*dataPoints) error {
func (r *GRPCReportClient) sendBatchCPUTimeRecord(ctx context.Context, records []*dataPoints) error {
if len(records) == 0 {
return nil
}
Expand Down
117 changes: 73 additions & 44 deletions util/topsql/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,31 @@ type dataPoints struct {
CPUTimeMsTotal uint64
}

// cpuTimeSort is used to sort TopSQL records by total CPU time
type cpuTimeSort struct {
Key string
SQLDigest []byte
PlanDigest []byte
CPUTimeMsTotal uint64 // The sorting field
type dataPointsSlice []*dataPoints
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved

func (t dataPointsSlice) Len() int {
return len(t)
}

func (t dataPointsSlice) Less(i, j int) bool {
// We need find the kth largest value, so here should use >
return t[i].CPUTimeMsTotal > t[j].CPUTimeMsTotal
}
func (t dataPointsSlice) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

type cpuTimeSortSlice []cpuTimeSort
type sqlCPUTimeRecordSlice []tracecpu.SQLCPUTimeRecord

func (t cpuTimeSortSlice) Len() int {
func (t sqlCPUTimeRecordSlice) Len() int {
return len(t)
}

func (t cpuTimeSortSlice) Less(i, j int) bool {
func (t sqlCPUTimeRecordSlice) Less(i, j int) bool {
// We need find the kth largest value, so here should use >
return t[i].CPUTimeMsTotal > t[j].CPUTimeMsTotal
return t[i].CPUTimeMs > t[j].CPUTimeMs
}
func (t cpuTimeSortSlice) Swap(i, j int) {
func (t sqlCPUTimeRecordSlice) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

Expand Down Expand Up @@ -215,11 +221,37 @@ func encodeKey(buf *bytes.Buffer, sqlDigest, planDigest []byte) string {
return buf.String()
}

func (tsr *RemoteTopSQLReporter) getTopNRecords(records []tracecpu.SQLCPUTimeRecord) (topN, shouldEvict []tracecpu.SQLCPUTimeRecord) {
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
maxStmt := int(variable.TopSQLVariable.MaxStatementCount.Load())
if len(records) <= maxStmt {
return records, nil
}
if err := quickselect.QuickSelect(sqlCPUTimeRecordSlice(records), maxStmt); err != nil {
// skip eviction
return records, nil
}
return records[:maxStmt], records[maxStmt:]
}

func (tsr *RemoteTopSQLReporter) getTopNDataPoints(records []*dataPoints) (topN, shouldEvict []*dataPoints) {
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
maxStmt := int(variable.TopSQLVariable.MaxStatementCount.Load())
if len(records) <= maxStmt {
return records, nil
}
if err := quickselect.QuickSelect(dataPointsSlice(records), maxStmt); err != nil {
// skip eviction
return records, nil
}
return records[:maxStmt], records[maxStmt:]
}

// doCollect uses a hashmap to store records in every second, and evict when necessary.
func (tsr *RemoteTopSQLReporter) doCollect(
collectTarget map[string]*dataPoints, timestamp uint64, records []tracecpu.SQLCPUTimeRecord) {
defer util.Recover("top-sql", "doCollect", nil, false)

var evicted []tracecpu.SQLCPUTimeRecord
records, evicted = tsr.getTopNRecords(records)
keyBuf := bytes.NewBuffer(make([]byte, 0, 64))
listCapacity := int(variable.TopSQLVariable.ReportIntervalSeconds.Load()/variable.TopSQLVariable.PrecisionSeconds.Load() + 1)
if listCapacity < 1 {
Expand All @@ -245,37 +277,14 @@ func (tsr *RemoteTopSQLReporter) doCollect(
entry.CPUTimeMsTotal += uint64(record.CPUTimeMs)
}

// evict records according to `MaxStatementCount` variable.
// TODO: Better to pass in the variable in the constructor, instead of referencing directly.
maxStmt := int(variable.TopSQLVariable.MaxStatementCount.Load())
if len(collectTarget) <= maxStmt {
return
}

// find the max CPUTimeMsTotal that should be evicted
digestCPUTimeList := make([]cpuTimeSort, len(collectTarget))
idx := 0
for key, value := range collectTarget {
digestCPUTimeList[idx] = cpuTimeSort{
Key: key,
SQLDigest: value.SQLDigest,
PlanDigest: value.PlanDigest,
CPUTimeMsTotal: value.CPUTimeMsTotal,
}
idx++
}

// QuickSelect will only return error when the second parameter is out of range
if err := quickselect.QuickSelect(cpuTimeSortSlice(digestCPUTimeList), maxStmt); err != nil {
// skip eviction
return
}

itemsToEvict := digestCPUTimeList[maxStmt:]
normalizedSQLMap := tsr.normalizedSQLMap.Load().(*sync.Map)
normalizedPlanMap := tsr.normalizedPlanMap.Load().(*sync.Map)
for _, evict := range itemsToEvict {
delete(collectTarget, evict.Key)
for _, evict := range evicted {
key := encodeKey(keyBuf, evict.SQLDigest, evict.PlanDigest)
_, ok := collectTarget[key]
if ok {
continue
}
_, loaded := normalizedSQLMap.LoadAndDelete(string(evict.SQLDigest))
if loaded {
tsr.sqlMapLength.Add(-1)
Expand All @@ -290,10 +299,30 @@ func (tsr *RemoteTopSQLReporter) doCollect(
// takeDataAndSendToReportChan takes out (resets) collected data. These data will be send to a report channel
// for reporting later.
func (tsr *RemoteTopSQLReporter) takeDataAndSendToReportChan(collectedDataPtr *map[string]*dataPoints) {
// Fetch TopN dataPoints.
records := make([]*dataPoints, 0, len(*collectedDataPtr))
for _, v := range *collectedDataPtr {
records = append(records, v)
}
var evicted []*dataPoints
records, evicted = tsr.getTopNDataPoints(records)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine to not to evict it here, since immediately after that the map will be dropped entirely. Maybe we can simply ignore them at the report time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This evict is use to avoid report useless SQL/Plan. And since the data structure of evicted is a slice, it's a little bit of trouble for ignore them at the report time, since we should traverse the slice for each SQL/Plan record.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about traversing the records and generate a "retain map" first, then iterate all records in the hash map and send records that only in the retain map?

normalizedSQLMap := tsr.normalizedSQLMap.Load().(*sync.Map)
normalizedPlanMap := tsr.normalizedPlanMap.Load().(*sync.Map)
for _, evict := range evicted {
_, loaded := normalizedSQLMap.LoadAndDelete(string(evict.SQLDigest))
if loaded {
tsr.sqlMapLength.Add(-1)
}
_, loaded = normalizedPlanMap.LoadAndDelete(string(evict.PlanDigest))
if loaded {
tsr.planMapLength.Add(-1)
}
}

data := reportData{
collectedData: *collectedDataPtr,
normalizedSQLMap: tsr.normalizedSQLMap.Load().(*sync.Map),
normalizedPlanMap: tsr.normalizedPlanMap.Load().(*sync.Map),
collectedData: records,
normalizedSQLMap: normalizedSQLMap,
normalizedPlanMap: normalizedPlanMap,
}

// Reset data for next report.
Expand All @@ -312,7 +341,7 @@ func (tsr *RemoteTopSQLReporter) takeDataAndSendToReportChan(collectedDataPtr *m

// reportData contains data that reporter sends to the agent
type reportData struct {
collectedData map[string]*dataPoints
collectedData []*dataPoints
normalizedSQLMap *sync.Map
normalizedPlanMap *sync.Map
}
Expand Down
72 changes: 72 additions & 0 deletions util/topsql/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,78 @@ func (s *testTopSQLReporter) TestCollectAndEvicted(c *C) {
}
}

func (s *testTopSQLReporter) newSQLCPUTimeRecord(tsr *RemoteTopSQLReporter, sqlID int, cpuTimeMs uint32) tracecpu.SQLCPUTimeRecord {
key := []byte("sqlDigest" + strconv.Itoa(sqlID))
value := "sqlNormalized" + strconv.Itoa(sqlID)
tsr.RegisterSQL(key, value)

key = []byte("planDigest" + strconv.Itoa(sqlID))
value = "planNormalized" + strconv.Itoa(sqlID)
tsr.RegisterPlan(key, value)

return tracecpu.SQLCPUTimeRecord{
SQLDigest: []byte("sqlDigest" + strconv.Itoa(sqlID)),
PlanDigest: []byte("planDigest" + strconv.Itoa(sqlID)),
CPUTimeMs: cpuTimeMs,
}
}

func (s *testTopSQLReporter) collectAndWait(tsr *RemoteTopSQLReporter, timestamp uint64, records []tracecpu.SQLCPUTimeRecord) {
tsr.Collect(timestamp, records)
time.Sleep(time.Millisecond * 100)
}

func (s *testTopSQLReporter) TestCollectAndTopN(c *C) {
agentServer, err := mock.StartMockAgentServer()
c.Assert(err, IsNil)
defer agentServer.Stop()

tsr := setupRemoteTopSQLReporter(2, 1, agentServer.Address())
defer tsr.Close()

records := []tracecpu.SQLCPUTimeRecord{
s.newSQLCPUTimeRecord(tsr, 1, 1),
s.newSQLCPUTimeRecord(tsr, 2, 2),
}
s.collectAndWait(tsr, 1, records)

records = []tracecpu.SQLCPUTimeRecord{
s.newSQLCPUTimeRecord(tsr, 3, 3),
s.newSQLCPUTimeRecord(tsr, 1, 1),
}
s.collectAndWait(tsr, 2, records)

records = []tracecpu.SQLCPUTimeRecord{
s.newSQLCPUTimeRecord(tsr, 4, 1),
s.newSQLCPUTimeRecord(tsr, 1, 1),
}
s.collectAndWait(tsr, 3, records)

// Wait agent server collect finish.
agentServer.WaitCollectCnt(1, time.Second*10)

// check for equality of server received batch and the original data
results := agentServer.GetLatestRecords()
c.Assert(results, HasLen, 2)
for _, req := range results {
id := 0
prefix := "sqlDigest"
if strings.HasPrefix(string(req.SqlDigest), prefix) {
n, err := strconv.Atoi(string(req.SqlDigest)[len(prefix):])
c.Assert(err, IsNil)
id = n
}
if id != 1 && id != 3 {
c.Fatalf("the id should be 1 or 3, got: %v", id)
}
total := uint32(0)
for _, v := range req.CpuTimeMsList {
total += v
}
c.Assert(total, Equals, uint32(3))
}
}

func (s *testTopSQLReporter) TestCollectCapacity(c *C) {
tsr := setupRemoteTopSQLReporter(maxSQLNum, 60, "")
defer tsr.Close()
Expand Down