Skip to content

Commit

Permalink
util: refine expensive query log during bootstrap (#14181) (#16144)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Apr 13, 2020
1 parent 1347df8 commit b8fbf56
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions util/expensivequery/expensivequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
type Handle struct {
mu sync.RWMutex
exitCh chan struct{}
sm util.SessionManager
sm atomic.Value
}

// NewExpensiveQueryHandle builds a new expensive query handler.
Expand All @@ -45,7 +45,7 @@ func NewExpensiveQueryHandle(exitCh chan struct{}) *Handle {
// SetSessionManager sets the SessionManager which is used to fetching the info
// of all active sessions.
func (eqh *Handle) SetSessionManager(sm util.SessionManager) *Handle {
eqh.sm = sm
eqh.sm.Store(sm)
return eqh
}

Expand All @@ -55,10 +55,12 @@ func (eqh *Handle) Run() {
// use 100ms as tickInterval temply, may use given interval or use defined variable later
tickInterval := time.Millisecond * time.Duration(100)
ticker := time.NewTicker(tickInterval)
defer ticker.Stop()
sm := eqh.sm.Load().(util.SessionManager)
for {
select {
case <-ticker.C:
processInfo := eqh.sm.ShowProcessList()
processInfo := sm.ShowProcessList()
for _, info := range processInfo {
if info.Info == nil || info.ExceedExpensiveTimeThresh {
continue
Expand All @@ -69,7 +71,7 @@ func (eqh *Handle) Run() {
info.ExceedExpensiveTimeThresh = true

} else if info.MaxExecutionTime > 0 && costTime > time.Duration(info.MaxExecutionTime)*time.Millisecond {
eqh.sm.Kill(info.ID, true)
sm.Kill(info.ID, true)
}
}
threshold = atomic.LoadUint64(&variable.ExpensiveQueryTimeThreshold)
Expand All @@ -89,7 +91,17 @@ func (eqh *Handle) LogOnQueryExceedMemQuota(connID uint64) {
if log.GetLevel() > zapcore.WarnLevel {
return
}
info, ok := eqh.sm.GetProcessInfo(connID)
// The out-of-memory SQL may be the internal SQL which is executed during
// the bootstrap phase, and the `sm` is not set at this phase. This is
// unlikely to happen except for testing. Thus we do not need to log
// detailed message for it.
v := eqh.sm.Load()
if v == nil {
logutil.Logger(context.Background()).Info("expensive_query during bootstrap phase", zap.Uint64("conn_id", connID))
return
}
sm := v.(util.SessionManager)
info, ok := sm.GetProcessInfo(connID)
if !ok {
return
}
Expand Down

0 comments on commit b8fbf56

Please sign in to comment.