Skip to content

Commit fa60ecc

Browse files
authored
Merge pull request pingcap#3 from zhongzc/stmtsummary
unify
2 parents 9bf28ff + a64f1b4 commit fa60ecc

File tree

7 files changed

+174
-86
lines changed

7 files changed

+174
-86
lines changed

bindinfo/handle.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141
"github.com/pingcap/tidb/util/logutil"
4242
utilparser "github.com/pingcap/tidb/util/parser"
4343
"github.com/pingcap/tidb/util/sqlexec"
44-
"github.com/pingcap/tidb/util/stmtsummary"
4544
tablefilter "github.com/pingcap/tidb/util/table-filter"
4645
"github.com/pingcap/tidb/util/timeutil"
4746
"go.uber.org/zap"
@@ -892,7 +891,7 @@ func (h *BindHandle) CaptureBaselines() {
892891
parser4Capture := parser.New()
893892
captureFilter := h.extractCaptureFilterFromStorage()
894893
emptyCaptureFilter := captureFilter.isEmpty()
895-
bindableStmts := getBindableStmts(h.sctx.GetSessionVars(), captureFilter.frequency)
894+
bindableStmts := h.sctx.GetSessionVars().StmtSummary.GetBindableStmts(captureFilter.frequency)
896895
for _, bindableStmt := range bindableStmts {
897896
stmt, err := parser4Capture.ParseOneStmt(bindableStmt.Query, bindableStmt.Charset, bindableStmt.Collation)
898897
if err != nil {
@@ -949,13 +948,6 @@ func (h *BindHandle) CaptureBaselines() {
949948
}
950949
}
951950

952-
func getBindableStmts(sessVars *variable.SessionVars, frequency int64) []*stmtsummary.BindableStmt {
953-
if sessVars.EnablePersistentStmtSummary {
954-
return sessVars.StmtSummaryV2.GetMoreThanCntBindableStmt(frequency)
955-
}
956-
return stmtsummary.StmtSummaryByDigestMap.GetMoreThanCntBindableStmt(frequency)
957-
}
958-
959951
func getHintsForSQL(sctx sessionctx.Context, sql string) (string, error) {
960952
origVals := sctx.GetSessionVars().UsePlanBaselines
961953
sctx.GetSessionVars().UsePlanBaselines = false

executor/adapter.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import (
6262
"github.com/pingcap/tidb/util/replayer"
6363
"github.com/pingcap/tidb/util/sqlexec"
6464
"github.com/pingcap/tidb/util/stmtsummary"
65-
stmtsummaryv2 "github.com/pingcap/tidb/util/stmtsummary/v2"
6665
"github.com/pingcap/tidb/util/stringutil"
6766
"github.com/pingcap/tidb/util/topsql"
6867
topsqlstate "github.com/pingcap/tidb/util/topsql/state"
@@ -1806,19 +1805,8 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
18061805
userString = sessVars.User.Username
18071806
}
18081807

1809-
var enabled, enabledInternalQuery bool
1810-
if sessVars.EnablePersistentStmtSummary {
1811-
if sessVars.StmtSummaryV2 == nil && stmtsummaryv2.GlobalStmtSummary != nil {
1812-
sessVars.StmtSummaryV2 = stmtsummaryv2.GlobalStmtSummary
1813-
}
1814-
if sessVars.StmtSummaryV2 != nil {
1815-
enabled = sessVars.StmtSummaryV2.Enabled()
1816-
enabledInternalQuery = sessVars.StmtSummaryV2.EnableInternalQuery()
1817-
}
1818-
} else {
1819-
enabled = stmtsummary.StmtSummaryByDigestMap.Enabled()
1820-
enabledInternalQuery = stmtsummary.StmtSummaryByDigestMap.EnabledInternal()
1821-
}
1808+
enabled := sessVars.StmtSummary.Enabled()
1809+
enabledInternalQuery := sessVars.StmtSummary.EnabledInternal()
18221810
// Internal SQLs must also be recorded to keep the consistency of `PrevStmt` and `PrevStmtDigest`.
18231811
if !enabled || ((sessVars.InRestrictedSQL || len(userString) == 0) && !enabledInternalQuery) {
18241812
sessVars.SetPrevStmtDigest("")
@@ -1937,11 +1925,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
19371925
if a.retryCount > 0 {
19381926
stmtExecInfo.ExecRetryTime = costTime - sessVars.DurationParse - sessVars.DurationCompile - time.Since(a.retryStartTime)
19391927
}
1940-
if sessVars.EnablePersistentStmtSummary {
1941-
sessVars.StmtSummaryV2.Add(stmtExecInfo)
1942-
} else {
1943-
stmtsummary.StmtSummaryByDigestMap.AddStatement(stmtExecInfo)
1944-
}
1928+
sessVars.StmtSummary.Add(stmtExecInfo)
19451929
}
19461930

19471931
// GetTextToLog return the query text to log.

executor/builder.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,26 +1980,10 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
19801980
if v.Extractor != nil {
19811981
extractor = v.Extractor.(*plannercore.StatementsSummaryExtractor)
19821982
}
1983-
if b.ctx.GetSessionVars().EnablePersistentStmtSummary {
1984-
return &MemTableReaderExec{
1985-
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ID()),
1986-
table: v.Table,
1987-
retriever: &stmtSummaryRetrieverV2{
1988-
stmtSummary: b.ctx.GetSessionVars().StmtSummaryV2,
1989-
table: v.Table,
1990-
columns: v.Columns,
1991-
extractor: extractor,
1992-
},
1993-
}
1994-
}
19951983
return &MemTableReaderExec{
19961984
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ID()),
19971985
table: v.Table,
1998-
retriever: &stmtSummaryRetriever{
1999-
table: v.Table,
2000-
columns: v.Columns,
2001-
extractor: extractor,
2002-
},
1986+
retriever: buildStmtSummaryRetriever(b.ctx, v.Table, v.Columns, extractor),
20031987
}
20041988
case strings.ToLower(infoschema.TableColumns):
20051989
return &MemTableReaderExec{

executor/stmtsummary.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ import (
2929
stmtsummaryv2 "github.com/pingcap/tidb/util/stmtsummary/v2"
3030
)
3131

32+
func buildStmtSummaryRetriever(
33+
ctx sessionctx.Context,
34+
table *model.TableInfo,
35+
columns []*model.ColumnInfo,
36+
extractor *plannercore.StatementsSummaryExtractor,
37+
) memTableRetriever {
38+
if ctx.GetSessionVars().StmtSummary.EnablePersistent {
39+
return &stmtSummaryRetrieverV2{
40+
stmtSummary: ctx.GetSessionVars().StmtSummary.StmtSummaryV2,
41+
table: table,
42+
columns: columns,
43+
extractor: extractor,
44+
}
45+
}
46+
return &stmtSummaryRetriever{
47+
table: table,
48+
columns: columns,
49+
extractor: extractor,
50+
}
51+
}
52+
3253
// stmtSummaryRetriever is used to retrieve statements summary.
3354
type stmtSummaryRetriever struct {
3455
dummyCloser

sessionctx/variable/session.go

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
"github.com/pingcap/tidb/util/memory"
5555
"github.com/pingcap/tidb/util/replayer"
5656
"github.com/pingcap/tidb/util/rowcodec"
57+
"github.com/pingcap/tidb/util/stmtsummary"
5758
stmtsummaryv2 "github.com/pingcap/tidb/util/stmtsummary/v2"
5859
"github.com/pingcap/tidb/util/stringutil"
5960
"github.com/pingcap/tidb/util/tableutil"
@@ -1339,12 +1340,8 @@ type SessionVars struct {
13391340
// is enabled.
13401341
PessimisticTransactionAggressiveLocking bool
13411342

1342-
// EnablePersistentStmtSummary indicates whether to enable the persistence
1343-
// of statements summary in the current session.
1344-
EnablePersistentStmtSummary bool
1345-
// StmtSummaryV2 refers to the global StmtSummary instance that will be used when
1346-
// EnablePersistentStmtSummary is true.
1347-
StmtSummaryV2 *stmtsummaryv2.StmtSummary
1343+
// StmtSummary collects statements summary for the current session.
1344+
StmtSummary *stmtSummary
13481345
}
13491346

13501347
// planReplayerSessionFinishedTaskKeyLen is used to control the max size for the finished plan replayer task key in session
@@ -1719,8 +1716,7 @@ func NewSessionVars(hctx HookContext) *SessionVars {
17191716
EnableReuseCheck: DefTiDBEnableReusechunk,
17201717
preUseChunkAlloc: DefTiDBUseAlloc,
17211718
ChunkPool: ReuseChunkPool{Alloc: nil},
1722-
EnablePersistentStmtSummary: config.GetGlobalConfig().Instance.StmtSummaryEnablePersistent,
1723-
StmtSummaryV2: stmtsummaryv2.GlobalStmtSummary,
1719+
StmtSummary: newStmtSummary(),
17241720
}
17251721
vars.KVVars = tikvstore.NewVariables(&vars.Killed)
17261722
vars.Concurrency = Concurrency{
@@ -3256,3 +3252,129 @@ func (lst *protectedTSList) Size() (size int) {
32563252
lst.Unlock()
32573253
return
32583254
}
3255+
3256+
// stmtSummary is an adapter to redirect executions to instances of different types of statement summary.
3257+
type stmtSummary struct {
3258+
// EnablePersistent indicates whether to enable the persistence
3259+
// of statements summary in the current session.
3260+
EnablePersistent bool
3261+
// StmtSummaryV2 refers to the global StmtSummary instance that will be used when
3262+
// EnablePersistent is true.
3263+
StmtSummaryV2 *stmtsummaryv2.StmtSummary
3264+
}
3265+
3266+
func newStmtSummary() *stmtSummary {
3267+
return &stmtSummary{
3268+
EnablePersistent: config.GetGlobalConfig().Instance.StmtSummaryEnablePersistent,
3269+
StmtSummaryV2: stmtsummaryv2.GlobalStmtSummary,
3270+
}
3271+
}
3272+
3273+
func (s *stmtSummary) Add(stmtExecInfo *stmtsummary.StmtExecInfo) {
3274+
s.tryInit()
3275+
3276+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3277+
s.StmtSummaryV2.Add(stmtExecInfo)
3278+
} else if !s.EnablePersistent {
3279+
stmtsummary.StmtSummaryByDigestMap.AddStatement(stmtExecInfo)
3280+
}
3281+
}
3282+
3283+
func (s *stmtSummary) Enabled() bool {
3284+
s.tryInit()
3285+
3286+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3287+
return s.StmtSummaryV2.Enabled()
3288+
} else if !s.EnablePersistent {
3289+
return stmtsummary.StmtSummaryByDigestMap.Enabled()
3290+
}
3291+
3292+
return false
3293+
}
3294+
3295+
func (s *stmtSummary) EnabledInternal() bool {
3296+
s.tryInit()
3297+
3298+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3299+
return s.StmtSummaryV2.EnableInternalQuery()
3300+
} else if !s.EnablePersistent {
3301+
return stmtsummary.StmtSummaryByDigestMap.EnabledInternal()
3302+
}
3303+
3304+
return false
3305+
}
3306+
3307+
func (s *stmtSummary) GetBindableStmts(frequency int64) []*stmtsummary.BindableStmt {
3308+
s.tryInit()
3309+
3310+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3311+
return s.StmtSummaryV2.GetMoreThanCntBindableStmt(frequency)
3312+
} else if !s.EnablePersistent {
3313+
return stmtsummary.StmtSummaryByDigestMap.GetMoreThanCntBindableStmt(frequency)
3314+
}
3315+
3316+
return nil
3317+
}
3318+
3319+
func (s *stmtSummary) SetEnabled(v bool) {
3320+
s.tryInit()
3321+
3322+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3323+
s.StmtSummaryV2.SetEnabled(v)
3324+
} else if !s.EnablePersistent {
3325+
stmtsummary.StmtSummaryByDigestMap.SetEnabled(v)
3326+
}
3327+
}
3328+
3329+
func (s *stmtSummary) SetEnableInternalQuery(v bool) {
3330+
s.tryInit()
3331+
3332+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3333+
s.StmtSummaryV2.SetEnableInternalQuery(v)
3334+
} else if !s.EnablePersistent {
3335+
stmtsummary.StmtSummaryByDigestMap.SetEnabledInternalQuery(v)
3336+
}
3337+
}
3338+
3339+
func (s *stmtSummary) SetRefreshInterval(v int64) {
3340+
s.tryInit()
3341+
3342+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3343+
s.StmtSummaryV2.SetRefreshInterval(uint32(v))
3344+
} else if !s.EnablePersistent {
3345+
stmtsummary.StmtSummaryByDigestMap.SetRefreshInterval(v)
3346+
}
3347+
}
3348+
3349+
func (s *stmtSummary) SetHistorySize(v int) {
3350+
if !s.EnablePersistent {
3351+
stmtsummary.StmtSummaryByDigestMap.SetHistorySize(v)
3352+
}
3353+
}
3354+
3355+
func (s *stmtSummary) SetMaxStmtCount(v int) {
3356+
s.tryInit()
3357+
3358+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3359+
s.StmtSummaryV2.SetMaxStmtCount(uint32(v))
3360+
} else if !s.EnablePersistent {
3361+
stmtsummary.StmtSummaryByDigestMap.SetMaxStmtCount(uint(v))
3362+
}
3363+
}
3364+
3365+
func (s *stmtSummary) SetMaxSQLLength(v int) {
3366+
s.tryInit()
3367+
3368+
if s.EnablePersistent && s.StmtSummaryV2 != nil {
3369+
s.StmtSummaryV2.SetMaxSQLLength(uint32(v))
3370+
} else if !s.EnablePersistent {
3371+
stmtsummary.StmtSummaryByDigestMap.SetMaxSQLLength(v)
3372+
}
3373+
}
3374+
3375+
// tryInit initials StmtSummaryV2 in case it is nil.
3376+
func (s *stmtSummary) tryInit() {
3377+
if s.EnablePersistent && s.StmtSummaryV2 == nil {
3378+
s.StmtSummaryV2 = stmtsummaryv2.GlobalStmtSummary
3379+
}
3380+
}

sessionctx/variable/sysvar.go

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"github.com/pingcap/tidb/util/logutil"
4141
"github.com/pingcap/tidb/util/mathutil"
4242
"github.com/pingcap/tidb/util/memory"
43-
"github.com/pingcap/tidb/util/stmtsummary"
4443
"github.com/pingcap/tidb/util/tikvutil"
4544
"github.com/pingcap/tidb/util/tls"
4645
topsqlstate "github.com/pingcap/tidb/util/topsql/state"
@@ -667,48 +666,34 @@ var defaultSysVars = []*SysVar{
667666
{Scope: ScopeGlobal, Name: TiDBScatterRegion, Value: BoolToOnOff(DefTiDBScatterRegion), Type: TypeBool},
668667
{Scope: ScopeGlobal, Name: TiDBEnableStmtSummary, Value: BoolToOnOff(DefTiDBEnableStmtSummary), Type: TypeBool, AllowEmpty: true,
669668
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
670-
v := TiDBOptOn(val)
671-
if s.StmtSummaryV2 != nil {
672-
s.StmtSummaryV2.SetEnabled(v)
673-
}
674-
return stmtsummary.StmtSummaryByDigestMap.SetEnabled(v)
669+
s.StmtSummary.SetEnabled(TiDBOptOn(val))
670+
return nil
675671
}},
676672
{Scope: ScopeGlobal, Name: TiDBStmtSummaryInternalQuery, Value: BoolToOnOff(DefTiDBStmtSummaryInternalQuery), Type: TypeBool, AllowEmpty: true,
677673
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
678-
v := TiDBOptOn(val)
679-
if s.StmtSummaryV2 != nil {
680-
s.StmtSummaryV2.SetEnableInternalQuery(v)
681-
}
682-
return stmtsummary.StmtSummaryByDigestMap.SetEnabledInternalQuery(v)
674+
s.StmtSummary.SetEnableInternalQuery(TiDBOptOn(val))
675+
return nil
683676
}},
684677
{Scope: ScopeGlobal, Name: TiDBStmtSummaryRefreshInterval, Value: strconv.Itoa(DefTiDBStmtSummaryRefreshInterval), Type: TypeInt, MinValue: 1, MaxValue: math.MaxInt32, AllowEmpty: true,
685678
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
686679
// convert val to int64
687-
v := TidbOptInt64(val, DefTiDBStmtSummaryRefreshInterval)
688-
if s.StmtSummaryV2 != nil {
689-
s.StmtSummaryV2.SetRefreshInterval(uint32(v))
690-
}
691-
return stmtsummary.StmtSummaryByDigestMap.SetRefreshInterval(v)
680+
s.StmtSummary.SetRefreshInterval(TidbOptInt64(val, DefTiDBStmtSummaryRefreshInterval))
681+
return nil
692682
}},
693683
{Scope: ScopeGlobal, Name: TiDBStmtSummaryHistorySize, Value: strconv.Itoa(DefTiDBStmtSummaryHistorySize), Type: TypeInt, MinValue: 0, MaxValue: math.MaxUint8, AllowEmpty: true,
694684
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
695-
return stmtsummary.StmtSummaryByDigestMap.SetHistorySize(TidbOptInt(val, DefTiDBStmtSummaryHistorySize))
685+
s.StmtSummary.SetHistorySize(TidbOptInt(val, DefTiDBStmtSummaryHistorySize))
686+
return nil
696687
}},
697688
{Scope: ScopeGlobal, Name: TiDBStmtSummaryMaxStmtCount, Value: strconv.Itoa(DefTiDBStmtSummaryMaxStmtCount), Type: TypeInt, MinValue: 1, MaxValue: math.MaxInt16, AllowEmpty: true,
698689
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
699-
v := TidbOptInt(val, DefTiDBStmtSummaryMaxStmtCount)
700-
if s.StmtSummaryV2 != nil {
701-
s.StmtSummaryV2.SetMaxStmtCount(uint32(v))
702-
}
703-
return stmtsummary.StmtSummaryByDigestMap.SetMaxStmtCount(uint(v))
690+
s.StmtSummary.SetMaxStmtCount(TidbOptInt(val, DefTiDBStmtSummaryMaxStmtCount))
691+
return nil
704692
}},
705693
{Scope: ScopeGlobal, Name: TiDBStmtSummaryMaxSQLLength, Value: strconv.Itoa(DefTiDBStmtSummaryMaxSQLLength), Type: TypeInt, MinValue: 0, MaxValue: math.MaxInt32, AllowEmpty: true,
706694
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
707-
v := TidbOptInt(val, DefTiDBStmtSummaryMaxSQLLength)
708-
if s.StmtSummaryV2 != nil {
709-
s.StmtSummaryV2.SetMaxSQLLength(uint32(v))
710-
}
711-
return stmtsummary.StmtSummaryByDigestMap.SetMaxSQLLength(v)
695+
s.StmtSummary.SetMaxSQLLength(TidbOptInt(val, DefTiDBStmtSummaryMaxSQLLength))
696+
return nil
712697
}},
713698
{Scope: ScopeGlobal, Name: TiDBCapturePlanBaseline, Value: DefTiDBCapturePlanBaseline, Type: TypeBool, AllowEmptyAll: true},
714699
{Scope: ScopeGlobal, Name: TiDBEvolvePlanTaskMaxTime, Value: strconv.Itoa(DefTiDBEvolvePlanTaskMaxTime), Type: TypeInt, MinValue: -1, MaxValue: math.MaxInt64},

util/stmtsummary/v2/tests/table_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ func TestPerformanceSchemaforPlanCache(t *testing.T) {
465465

466466
func newTestKit(t *testing.T, store kv.Storage, stmtSummary *stmtsummaryv2.StmtSummary) *testkit.TestKit {
467467
tk := testkit.NewTestKit(t, store)
468-
tk.Session().GetSessionVars().EnablePersistentStmtSummary = true
469-
tk.Session().GetSessionVars().StmtSummaryV2 = stmtSummary
468+
tk.Session().GetSessionVars().StmtSummary.EnablePersistent = true
469+
tk.Session().GetSessionVars().StmtSummary.StmtSummaryV2 = stmtSummary
470470
tk.MustExec("use test")
471471
return tk
472472
}
@@ -483,8 +483,8 @@ func newTestKitWithPlanCache(t *testing.T, store kv.Storage, stmtSummary *stmtsu
483483
PreparedPlanCache: plannercore.NewLRUPlanCache(100, 0.1, math.MaxUint64, tk.Session()),
484484
})
485485
require.NoError(t, err)
486-
se.GetSessionVars().EnablePersistentStmtSummary = true
487-
se.GetSessionVars().StmtSummaryV2 = stmtSummary
486+
tk.Session().GetSessionVars().StmtSummary.EnablePersistent = true
487+
tk.Session().GetSessionVars().StmtSummary.StmtSummaryV2 = stmtSummary
488488
tk.SetSession(se)
489489
tk.RefreshConnectionID()
490490
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil))
@@ -496,7 +496,7 @@ func enableStmtSummaryV2InBindHandle(t *testing.T, store kv.Storage, stmtSummary
496496
require.NoError(t, err)
497497
ss, err := session.CreateSession4Test(store)
498498
require.NoError(t, err)
499-
ss.GetSessionVars().EnablePersistentStmtSummary = true
500-
ss.GetSessionVars().StmtSummaryV2 = stmtSummary
499+
tk.Session().GetSessionVars().StmtSummary.EnablePersistent = true
500+
tk.Session().GetSessionVars().StmtSummary.StmtSummaryV2 = stmtSummary
501501
dom.BindHandle().Reset(ss)
502502
}

0 commit comments

Comments
 (0)