diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 547ebba14add0..303a2f62dcb1a 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -3271,110 +3271,121 @@ func newStmtSummary() *stmtSummary { } func (s *stmtSummary) Add(stmtExecInfo *stmtsummary.StmtExecInfo) { - s.tryInit() + if !s.ensureInit() { + return + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { + if s.EnablePersistent { s.StmtSummaryV2.Add(stmtExecInfo) - } else if !s.EnablePersistent { - stmtsummary.StmtSummaryByDigestMap.AddStatement(stmtExecInfo) } + stmtsummary.StmtSummaryByDigestMap.AddStatement(stmtExecInfo) } func (s *stmtSummary) Enabled() bool { - s.tryInit() + if !s.ensureInit() { + return false + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { + if s.EnablePersistent { return s.StmtSummaryV2.Enabled() - } else if !s.EnablePersistent { - return stmtsummary.StmtSummaryByDigestMap.Enabled() } - - return false + return stmtsummary.StmtSummaryByDigestMap.Enabled() } func (s *stmtSummary) EnabledInternal() bool { - s.tryInit() + if !s.ensureInit() { + return false + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { + if s.EnablePersistent { return s.StmtSummaryV2.EnableInternalQuery() - } else if !s.EnablePersistent { - return stmtsummary.StmtSummaryByDigestMap.EnabledInternal() } - - return false + return stmtsummary.StmtSummaryByDigestMap.EnabledInternal() } -func (s *stmtSummary) GetBindableStmts(frequency int64) []*stmtsummary.BindableStmt { - s.tryInit() - - if s.EnablePersistent && s.StmtSummaryV2 != nil { - return s.StmtSummaryV2.GetMoreThanCntBindableStmt(frequency) - } else if !s.EnablePersistent { - return stmtsummary.StmtSummaryByDigestMap.GetMoreThanCntBindableStmt(frequency) +func (s *stmtSummary) SetEnabled(v bool) error { + if !s.ensureInit() { + return nil } - return nil + if s.EnablePersistent { + return s.StmtSummaryV2.SetEnabled(v) + } + return stmtsummary.StmtSummaryByDigestMap.SetEnabled(v) } -func (s *stmtSummary) SetEnabled(v bool) { - s.tryInit() +func (s *stmtSummary) SetEnableInternalQuery(v bool) error { + if !s.ensureInit() { + return nil + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { - s.StmtSummaryV2.SetEnabled(v) - } else if !s.EnablePersistent { - _ = stmtsummary.StmtSummaryByDigestMap.SetEnabled(v) + if s.EnablePersistent { + return s.StmtSummaryV2.SetEnableInternalQuery(v) } + return stmtsummary.StmtSummaryByDigestMap.SetEnabledInternalQuery(v) } -func (s *stmtSummary) SetEnableInternalQuery(v bool) { - s.tryInit() +func (s *stmtSummary) SetRefreshInterval(v int64) error { + if !s.ensureInit() { + return nil + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { - s.StmtSummaryV2.SetEnableInternalQuery(v) - } else if !s.EnablePersistent { - _ = stmtsummary.StmtSummaryByDigestMap.SetEnabledInternalQuery(v) + if s.EnablePersistent { + return s.StmtSummaryV2.SetRefreshInterval(uint32(v)) } + return stmtsummary.StmtSummaryByDigestMap.SetRefreshInterval(v) } -func (s *stmtSummary) SetRefreshInterval(v int64) { - s.tryInit() +func (s *stmtSummary) SetHistorySize(v int) error { + if !s.ensureInit() { + return nil + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { - s.StmtSummaryV2.SetRefreshInterval(uint32(v)) - } else if !s.EnablePersistent { - _ = stmtsummary.StmtSummaryByDigestMap.SetRefreshInterval(v) + if s.EnablePersistent { + return nil // not support } + return stmtsummary.StmtSummaryByDigestMap.SetHistorySize(v) } -func (s *stmtSummary) SetHistorySize(v int) { - if !s.EnablePersistent { - _ = stmtsummary.StmtSummaryByDigestMap.SetHistorySize(v) +func (s *stmtSummary) SetMaxStmtCount(v int) error { + if !s.ensureInit() { + return nil } + + if s.EnablePersistent { + return s.StmtSummaryV2.SetMaxStmtCount(uint32(v)) + } + return stmtsummary.StmtSummaryByDigestMap.SetMaxStmtCount(uint(v)) } -func (s *stmtSummary) SetMaxStmtCount(v int) { - s.tryInit() +func (s *stmtSummary) SetMaxSQLLength(v int) error { + if !s.ensureInit() { + return nil + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { - s.StmtSummaryV2.SetMaxStmtCount(uint32(v)) - } else if !s.EnablePersistent { - _ = stmtsummary.StmtSummaryByDigestMap.SetMaxStmtCount(uint(v)) + if s.EnablePersistent { + return s.StmtSummaryV2.SetMaxSQLLength(uint32(v)) } + return stmtsummary.StmtSummaryByDigestMap.SetMaxSQLLength(v) } -func (s *stmtSummary) SetMaxSQLLength(v int) { - s.tryInit() +func (s *stmtSummary) GetBindableStmts(frequency int64) []*stmtsummary.BindableStmt { + if !s.ensureInit() { + return nil + } - if s.EnablePersistent && s.StmtSummaryV2 != nil { - s.StmtSummaryV2.SetMaxSQLLength(uint32(v)) - } else if !s.EnablePersistent { - _ = stmtsummary.StmtSummaryByDigestMap.SetMaxSQLLength(v) + if s.EnablePersistent { + return s.StmtSummaryV2.GetMoreThanCntBindableStmt(frequency) } + return stmtsummary.StmtSummaryByDigestMap.GetMoreThanCntBindableStmt(frequency) } -// tryInit initials StmtSummaryV2 in case it is nil. -func (s *stmtSummary) tryInit() { +// ensureInit initials StmtSummaryV2 in case it is nil, or +// return false if the initialization fails. +func (s *stmtSummary) ensureInit() bool { if s.EnablePersistent && s.StmtSummaryV2 == nil { s.StmtSummaryV2 = stmtsummaryv2.GlobalStmtSummary + return s.StmtSummaryV2 != nil } + return true } diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 7228e9d57e4f8..cf4ada14c2b79 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -666,34 +666,28 @@ var defaultSysVars = []*SysVar{ {Scope: ScopeGlobal, Name: TiDBScatterRegion, Value: BoolToOnOff(DefTiDBScatterRegion), Type: TypeBool}, {Scope: ScopeGlobal, Name: TiDBEnableStmtSummary, Value: BoolToOnOff(DefTiDBEnableStmtSummary), Type: TypeBool, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { - s.StmtSummary.SetEnabled(TiDBOptOn(val)) - return nil + return s.StmtSummary.SetEnabled(TiDBOptOn(val)) }}, {Scope: ScopeGlobal, Name: TiDBStmtSummaryInternalQuery, Value: BoolToOnOff(DefTiDBStmtSummaryInternalQuery), Type: TypeBool, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { - s.StmtSummary.SetEnableInternalQuery(TiDBOptOn(val)) - return nil + return s.StmtSummary.SetEnableInternalQuery(TiDBOptOn(val)) }}, {Scope: ScopeGlobal, Name: TiDBStmtSummaryRefreshInterval, Value: strconv.Itoa(DefTiDBStmtSummaryRefreshInterval), Type: TypeInt, MinValue: 1, MaxValue: math.MaxInt32, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { // convert val to int64 - s.StmtSummary.SetRefreshInterval(TidbOptInt64(val, DefTiDBStmtSummaryRefreshInterval)) - return nil + return s.StmtSummary.SetRefreshInterval(TidbOptInt64(val, DefTiDBStmtSummaryRefreshInterval)) }}, {Scope: ScopeGlobal, Name: TiDBStmtSummaryHistorySize, Value: strconv.Itoa(DefTiDBStmtSummaryHistorySize), Type: TypeInt, MinValue: 0, MaxValue: math.MaxUint8, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { - s.StmtSummary.SetHistorySize(TidbOptInt(val, DefTiDBStmtSummaryHistorySize)) - return nil + return s.StmtSummary.SetHistorySize(TidbOptInt(val, DefTiDBStmtSummaryHistorySize)) }}, {Scope: ScopeGlobal, Name: TiDBStmtSummaryMaxStmtCount, Value: strconv.Itoa(DefTiDBStmtSummaryMaxStmtCount), Type: TypeInt, MinValue: 1, MaxValue: math.MaxInt16, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { - s.StmtSummary.SetMaxStmtCount(TidbOptInt(val, DefTiDBStmtSummaryMaxStmtCount)) - return nil + return s.StmtSummary.SetMaxStmtCount(TidbOptInt(val, DefTiDBStmtSummaryMaxStmtCount)) }}, {Scope: ScopeGlobal, Name: TiDBStmtSummaryMaxSQLLength, Value: strconv.Itoa(DefTiDBStmtSummaryMaxSQLLength), Type: TypeInt, MinValue: 0, MaxValue: math.MaxInt32, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { - s.StmtSummary.SetMaxSQLLength(TidbOptInt(val, DefTiDBStmtSummaryMaxSQLLength)) - return nil + return s.StmtSummary.SetMaxSQLLength(TidbOptInt(val, DefTiDBStmtSummaryMaxSQLLength)) }}, {Scope: ScopeGlobal, Name: TiDBCapturePlanBaseline, Value: DefTiDBCapturePlanBaseline, Type: TypeBool, AllowEmptyAll: true}, {Scope: ScopeGlobal, Name: TiDBEvolvePlanTaskMaxTime, Value: strconv.Itoa(DefTiDBEvolvePlanTaskMaxTime), Type: TypeInt, MinValue: -1, MaxValue: math.MaxInt64}, diff --git a/util/stmtsummary/v2/stmtsummary.go b/util/stmtsummary/v2/stmtsummary.go index 9d7a178fabfbc..c903eb8962ce1 100644 --- a/util/stmtsummary/v2/stmtsummary.go +++ b/util/stmtsummary/v2/stmtsummary.go @@ -149,11 +149,13 @@ func (s *StmtSummary) Enabled() bool { // SetEnabled is used to enable or disable StmtSummary. If disabled, in-memory // data will be cleared, (persisted data will still be remained). -func (s *StmtSummary) SetEnabled(v bool) { +func (s *StmtSummary) SetEnabled(v bool) error { s.optEnabled.Store(v) if !v { s.Clear() } + + return nil } // EnableInternalQuery returns whether the StmtSummary counts internal queries. @@ -164,11 +166,13 @@ func (s *StmtSummary) EnableInternalQuery() bool { // SetEnableInternalQuery is used to enable or disable StmtSummary's internal // query statistics. If disabled, in-memory internal queries will be cleared, // (persisted internal queries will still be remained). -func (s *StmtSummary) SetEnableInternalQuery(v bool) { +func (s *StmtSummary) SetEnableInternalQuery(v bool) error { s.optEnableInternalQuery.Store(v) if !v { s.ClearInternal() } + + return nil } // MaxStmtCount returns the maximum number of statements. @@ -178,7 +182,7 @@ func (s *StmtSummary) MaxStmtCount() uint32 { // SetMaxStmtCount is used to set the maximum number of statements. // If the current number exceeds the maximum number, the excess will be evicted. -func (s *StmtSummary) SetMaxStmtCount(v uint32) { +func (s *StmtSummary) SetMaxStmtCount(v uint32) error { if v < 1 { v = 1 } @@ -187,6 +191,8 @@ func (s *StmtSummary) SetMaxStmtCount(v uint32) { w.Lock() _ = w.lru.SetCapacity(uint(v)) w.Unlock() + + return nil } // MaxSQLLength returns the maximum size of a single SQL statement. @@ -195,8 +201,10 @@ func (s *StmtSummary) MaxSQLLength() uint32 { } // SetMaxSQLLength sets the maximum size of a single SQL statement. -func (s *StmtSummary) SetMaxSQLLength(v uint32) { +func (s *StmtSummary) SetMaxSQLLength(v uint32) error { s.optMaxSQLLength.Store(v) + + return nil } // RefreshInterval returns the period (in seconds) at which the statistics @@ -208,11 +216,13 @@ func (s *StmtSummary) RefreshInterval() uint32 { // SetRefreshInterval sets the period (in seconds) for the statistics window // to be refreshed (persisted). This may trigger a refresh (persistence) of // the current statistics window early. -func (s *StmtSummary) SetRefreshInterval(v uint32) { +func (s *StmtSummary) SetRefreshInterval(v uint32) error { if v < 1 { v = 1 } s.optRefreshInterval.Store(v) + + return nil } // Add adds a single stmtsummary.StmtExecInfo to the current statistics window diff --git a/util/stmtsummary/v2/tests/table_test.go b/util/stmtsummary/v2/tests/table_test.go index 976631d410b74..3ac3b39f8e941 100644 --- a/util/stmtsummary/v2/tests/table_test.go +++ b/util/stmtsummary/v2/tests/table_test.go @@ -496,7 +496,7 @@ func enableStmtSummaryV2InBindHandle(t *testing.T, store kv.Storage, stmtSummary require.NoError(t, err) ss, err := session.CreateSession4Test(store) require.NoError(t, err) - tk.Session().GetSessionVars().StmtSummary.EnablePersistent = true - tk.Session().GetSessionVars().StmtSummary.StmtSummaryV2 = stmtSummary + ss.GetSessionVars().StmtSummary.EnablePersistent = true + ss.GetSessionVars().StmtSummary.StmtSummaryV2 = stmtSummary dom.BindHandle().Reset(ss) }