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

infoschema, session: support for events_statements_summary_by_digest #12017

Merged
merged 26 commits into from
Sep 12, 2019

Conversation

djshow832
Copy link
Contributor

@djshow832 djshow832 commented Sep 4, 2019

What problem does this PR solve?

Support system table events_statements_summary_by_digest, which is used to summary statements by digest.

What is changed and how it works?

  • Add methods to perfSchemaTable, and add a new table events_statements_summary_by_digest.
  • Add a system variable tidb_enable_stmt_summary and add configurations max-stmt-count & max-sql-length.
  • Summary the statement execution information and put it into StmtSummary after a SQL runs.
  • Get information from StmtSummary when the user queries events_statements_summary_by_digest.

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change
  • Has exported variable/fields change
  • Has interface methods change

Side effects

  • Possible performance regression
  • Increased code complexity

Related changes

  • Need to cherry-pick to the release branch

@sre-bot sre-bot added the contribution This PR is from a community contributor. label Sep 4, 2019
@codecov
Copy link

codecov bot commented Sep 4, 2019

Codecov Report

Merging #12017 into master will decrease coverage by 0.2214%.
The diff coverage is 94.2196%.

@@               Coverage Diff                @@
##             master     #12017        +/-   ##
================================================
- Coverage   81.4122%   81.1907%   -0.2215%     
================================================
  Files           454        454                
  Lines         99291      97973      -1318     
================================================
- Hits          80835      79545      -1290     
- Misses        12664      12707        +43     
+ Partials       5792       5721        -71

@djshow832
Copy link
Contributor Author

/bench

@sre-bot
Copy link
Contributor

sre-bot commented Sep 9, 2019

@@                               Benchmark Diff                               @@
================================================================================
--- tidb: 1ef81a8620997103dd81fa2f74fd09a3a0683c1b
+++ tidb: e1e3cf3127b3e1eeb6d25aa507d0b541b1bbade1
tikv: 5ad30cba22f80853279c990b4030fb247f32b3ad
pd: c7c572a7dc9710aca57cb0bddf4d9bca6c4a111b
================================================================================
test-1: < oltp_insert >
    * QPS : 21432.92 ± 0.3035% (std=48.71) delta: 0.12%
    * AvgMs : 11.94 ± 0.2933% (std=0.03) delta: -0.14%
    * PercentileMs99 : 42.61 ± 0.0000% (std=0.00) delta: 0.00%
            
test-2: < oltp_update_non_index >
    * QPS : 29627.64 ± 0.3367% (std=62.74) delta: 0.05%
    * AvgMs : 8.64 ± 0.3241% (std=0.02) delta: -0.05%
    * PercentileMs99 : 30.59 ± 1.0788% (std=0.27) delta: -0.71%
            
test-3: < oltp_read_write >
    * QPS : 36883.62 ± 0.2365% (std=61.13) delta: -0.24%
    * AvgMs : 139.37 ± 0.2188% (std=0.21) delta: 0.24%
    * PercentileMs99 : 257.95 ± 0.0000% (std=0.00) delta: 0.00%
            
test-4: < oltp_point_select >
    * QPS : 74881.82 ± 3.0726% (std=1336.49) delta: -0.92%
    * AvgMs : 3.42 ± 3.2768% (std=0.06) delta: 0.97%
    * PercentileMs99 : 7.43 ± 0.0000% (std=0.00) delta: -0.70%
            
test-5: < oltp_update_index >
    * QPS : 17080.78 ± 0.2005% (std=22.25) delta: -0.02%
    * AvgMs : 14.98 ± 0.2169% (std=0.02) delta: -0.01%
    * PercentileMs99 : 47.99 ± 1.0877% (std=0.43) delta: 0.00%
            

https://perf.pingcap.com

@sre-bot
Copy link
Contributor

sre-bot commented Sep 9, 2019

@@                               Benchmark Diff                               @@
================================================================================
--- tidb: bb5bfa4bd7f9b3bb67e06d3de14a82c61c2eb9d8
+++ tidb: e1e3cf3127b3e1eeb6d25aa507d0b541b1bbade1
tikv: 5ad30cba22f80853279c990b4030fb247f32b3ad
pd: c7c572a7dc9710aca57cb0bddf4d9bca6c4a111b
================================================================================
tidb_max_cpu: 19.40, delta: -0.50%
tikv_max_cpu: 13.15, delta: 3.45%
tidb_max_memory: 1784.65 MiB, delta: -1.13%
tikv_max_memory: 59841.52 MiB, delta: -0.02%

Measured tpmC (NewOrders): 21981.93, delta: 0.49%

https://perf.pingcap.com

@djshow832 djshow832 force-pushed the sql_monitor_by_digest branch from e1e3cf3 to d15dbd2 Compare September 10, 2019 02:58
@@ -14,10 +14,19 @@
package perfschema

import (
"fmt"
Copy link
Member

Choose a reason for hiding this comment

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

Plz add a blank line below this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

)

const (
tableNameEventsStatementsSummaryByDigest = "events_statements_summary_by_digest"
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if we should resurrect perf_schema, maybe we can put it to info_schema?

Copy link
Contributor Author

@djshow832 djshow832 Sep 10, 2019

Choose a reason for hiding this comment

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

Some table names are the same, like tables.go.
And performance schema is independent with information schema in MySQL.
So I prefer the directory to be like this:

--virtual_table
----infoschema
----perfschema
----sys

But now, I prefer not to change the directory structure in this PR. Maybe fixing it in another PR is better, if needed.

@@ -0,0 +1,198 @@
// Copyright 2016 PingCAP, Inc.
Copy link
Member

Choose a reason for hiding this comment

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

It is 2019 now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

@@ -0,0 +1,303 @@
package stmtsummary
Copy link
Member

Choose a reason for hiding this comment

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

Add license and format imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

@lysu lysu added component/server type/usability type/new-feature and removed contribution This PR is from a community contributor. labels Sep 10, 2019
config/config.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
config/config.toml.example Outdated Show resolved Hide resolved
executor/adapter.go Outdated Show resolved Hide resolved

// AddRecord implements table.Table AddRecord interface.
func (vt *perfSchemaTable) AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...table.AddRecordOption) (recordID int64, err error) {
fmt.Println("add record")
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this manual-debugging code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.


// UpdateRecord implements table.Table UpdateRecord interface.
func (vt *perfSchemaTable) UpdateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datum, touched []bool) error {
fmt.Println("update record")
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

infoschema/perfschema/tables_test.go Outdated Show resolved Hide resolved
sessionctx/variable/session.go Outdated Show resolved Hide resolved
sessionctx/variable/tidb_vars.go Outdated Show resolved Hide resolved
func NewStmtSummaryByDigest() *stmtSummaryByDigest {
maxStmtCount := config.GetGlobalConfig().StmtSummary.MaxStmtCount
return &stmtSummaryByDigest{
summaryMap: kvcache.NewSimpleLRUCache(maxStmtCount, 0, 0),
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible that we still need memory quota, say, if the maxStmtCount is set to be very very large?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It complicates the configuration of statement summary, which makes users more confusing. If the user sets max-stmt-count to a very large value, he can also set the quota to a very large value.

Copy link
Member

Choose a reason for hiding this comment

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

OK

@djshow832 djshow832 force-pushed the sql_monitor_by_digest branch 2 times, most recently from 5d66398 to 2c6dcc7 Compare September 10, 2019 06:25
return table.VirtualTable
}

func dataForEventsStatementsSummaryByDigest(ctx sessionctx.Context) [][]types.Datum {
Copy link
Member

Choose a reason for hiding this comment

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

We can inline this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

}

// RowWithCols implements table.Table RowWithCols interface.
func (vt *perfSchemaTable) RowWithCols(ctx sessionctx.Context, h int64, cols []*table.Column) ([]types.Datum, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Those unsupported methods are already implemented by dummy VirtualTable which is embedded in perfSchemaTable, we can remove those methods for perfSchemaTable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.


// A LRU cache that stores statement summary.
type stmtSummaryByDigest struct {
// It's rare to read concurrently, so RWMutex is not needed.
Copy link
Member

Choose a reason for hiding this comment

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

The comment contradicts the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean, we need Mutex instead of RWMutex.

Copy link
Member

Choose a reason for hiding this comment

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

Then why use RWMutex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not using RWMutex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

ss.Unlock()

// Lock a single entry, not the whole cache.
if summary != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Seems summary can not be nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If an entry is not found in the cache, we create newSummary, so summary stays nil.

Copy link
Member

Choose a reason for hiding this comment

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

Then why not define summary and addStmtExecInfoToSummary in the ok block?

Copy link
Member

Choose a reason for hiding this comment

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

Or we can create a new summary without adding the stats, call addStmtExecInfoToSummary later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separate addStmtExecInfoToSummary and the ok block because I want to move addStmtExecInfoToSummary out of the big cache lock, and add a smaller lock to it.
Why do I need to create a new summary?

Copy link
Member

Choose a reason for hiding this comment

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

So we can remove the if summary != nil check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code is simplified a little.

if sei.TotalLatency < ss.minLatency {
ss.minLatency = sei.TotalLatency
}
// TODO: uint64 overflow
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to worry about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

@djshow832 djshow832 force-pushed the sql_monitor_by_digest branch from 8d1c9f0 to f29aad4 Compare September 11, 2019 04:26
}

// Add a new StmtExecInfo to stmtSummary
func addStmtExecInfoToSummary(sei *StmtExecInfo, ss *stmtSummary) {
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 that we make this function a method of stmtSummary ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. change the function name to NewStmtSummary(sei *StmtExecInfo)
  2. ss := new(stmtSummary); ss.convertFromExecInfo(sei)
    which one?

Copy link
Member

@coocood coocood Sep 11, 2019

Choose a reason for hiding this comment

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

func (ss *stmtSummary) add(sei *StmtExecInfo)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

@djshow832 djshow832 force-pushed the sql_monitor_by_digest branch from d8ce20f to 5e59a41 Compare September 12, 2019 11:07
Copy link
Member

@bb7133 bb7133 left a comment

Choose a reason for hiding this comment

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

LGTM

@bb7133 bb7133 added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 12, 2019
@bb7133
Copy link
Member

bb7133 commented Sep 12, 2019

/run-all-tests

@bb7133 bb7133 added the status/can-merge Indicates a PR has been approved by a committer. label Sep 12, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Sep 12, 2019

/run-all-tests

@sre-bot sre-bot merged commit 0f55274 into pingcap:master Sep 12, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Sep 12, 2019

cherry pick to release-3.0 failed

djshow832 added a commit to djshow832/tidb that referenced this pull request Sep 23, 2019
djshow832 added a commit to djshow832/tidb that referenced this pull request Sep 24, 2019
bb7133 added a commit to bb7133/tidb that referenced this pull request Sep 26, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Apr 7, 2020

It seems that, not for sure, we failed to cherry-pick this commit to release-3.0. Please comment '/run-cherry-picker' to try to trigger the cherry-picker if we did fail to cherry-pick this commit before. @djshow832 PTAL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/server status/can-merge Indicates a PR has been approved by a committer. status/LGT3 The PR has already had 3 LGTM. type/new-feature type/usability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants