Skip to content

Commit

Permalink
Merge pull request pingcap#4 from zhongzc/stmtsummary
Browse files Browse the repository at this point in the history
fix build
  • Loading branch information
mornyx authored Jan 31, 2023
2 parents dc67841 + c768c8c commit 7d92952
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 78 deletions.
129 changes: 70 additions & 59 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 6 additions & 12 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
20 changes: 15 additions & 5 deletions util/stmtsummary/v2/stmtsummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
}
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions util/stmtsummary/v2/tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 7d92952

Please sign in to comment.