@@ -49,10 +49,10 @@ type Checker struct {
49
49
// From the group runaway settings, which will be applied when a query lacks a specified watch rule.
50
50
settings * rmpb.RunawaySettings
51
51
52
- // markedByRule is set to true when the query matches the group runaway settings.
53
- markedByRule atomic.Bool
54
- // markedByWatch is set to true when the query matches the specified watch rules.
55
- markedByWatch bool
52
+ // markedByIdentifyInRunawaySettings is set to true when the query matches the group runaway settings.
53
+ markedByIdentifyInRunawaySettings atomic.Bool
54
+ // markedByQueryWatchRule is set to true when the query matches the specified watch rules.
55
+ markedByQueryWatchRule bool
56
56
// watchAction is the specified watch action for the runaway query.
57
57
// If it's not given, the action defined in `settings` will be used.
58
58
watchAction rmpb.RunawayAction
@@ -65,14 +65,14 @@ func NewChecker(
65
65
originalSQL , sqlDigest , planDigest string , startTime time.Time ,
66
66
) * Checker {
67
67
c := & Checker {
68
- manager : manager ,
69
- resourceGroupName : resourceGroupName ,
70
- originalSQL : originalSQL ,
71
- sqlDigest : sqlDigest ,
72
- planDigest : planDigest ,
73
- settings : settings ,
74
- markedByRule : atomic.Bool {},
75
- markedByWatch : false ,
68
+ manager : manager ,
69
+ resourceGroupName : resourceGroupName ,
70
+ originalSQL : originalSQL ,
71
+ sqlDigest : sqlDigest ,
72
+ planDigest : planDigest ,
73
+ settings : settings ,
74
+ markedByIdentifyInRunawaySettings : atomic.Bool {},
75
+ markedByQueryWatchRule : false ,
76
76
}
77
77
if settings != nil {
78
78
// avoid setting deadline if the threshold is 0
@@ -92,6 +92,10 @@ func (rm *Manager) DeriveChecker(resourceGroupName, originalSQL, sqlDigest, plan
92
92
logutil .BgLogger ().Warn ("cannot setup up runaway checker" , zap .Error (err ))
93
93
return nil
94
94
}
95
+ // Only check the normal statement.
96
+ if len (planDigest ) == 0 {
97
+ return nil
98
+ }
95
99
rm .ActiveLock .RLock ()
96
100
defer rm .ActiveLock .RUnlock ()
97
101
if group .RunawaySettings == nil && rm .ActiveGroup [resourceGroupName ] == 0 {
@@ -129,7 +133,7 @@ func (r *Checker) BeforeExecutor() (string, error) {
129
133
switchGroupName = r .settings .SwitchGroupName
130
134
}
131
135
// Mark it if this is the first time being watched.
132
- r .markRunawayByWatch (action , switchGroupName , exceedCause )
136
+ r .markRunawayByQueryWatchRule (action , switchGroupName , exceedCause )
133
137
// Take action if needed.
134
138
switch action {
135
139
case rmpb .RunawayAction_Kill :
@@ -157,16 +161,16 @@ func (r *Checker) BeforeCopRequest(req *tikvrpc.Request) error {
157
161
return nil
158
162
}
159
163
// If the group settings are not available, and it's not marked by watch, skip this part.
160
- if r .settings == nil && ! r .markedByWatch {
164
+ if r .settings == nil && ! r .markedByQueryWatchRule {
161
165
return nil
162
166
}
163
167
// If it's marked by watch and the action is cooldown, override the priority,
164
- if r .markedByWatch && r .watchAction == rmpb .RunawayAction_CoolDown {
168
+ if r .markedByQueryWatchRule && r .watchAction == rmpb .RunawayAction_CoolDown {
165
169
req .ResourceControlContext .OverridePriority = 1 // set priority to lowest
166
170
}
167
171
// If group settings are available and the query is not marked by a rule,
168
172
// verify if it matches any rules in the settings.
169
- if r .settings != nil && ! r .markedByRule .Load () {
173
+ if r .settings != nil && ! r .markedByIdentifyInRunawaySettings .Load () {
170
174
now := time .Now ()
171
175
// only check time and need to ensure deadline existed.
172
176
exceedCause := r .exceedsThresholds (now , nil , 0 )
@@ -181,7 +185,7 @@ func (r *Checker) BeforeCopRequest(req *tikvrpc.Request) error {
181
185
return nil
182
186
}
183
187
// execution time exceeds the threshold, mark the query as runaway
184
- r .markRunawayByIdentify (& now , exceedCause )
188
+ r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause )
185
189
// Take action if needed.
186
190
switch r .settings .Action {
187
191
case rmpb .RunawayAction_Kill :
@@ -205,10 +209,10 @@ func (r *Checker) CheckAction() rmpb.RunawayAction {
205
209
if r == nil {
206
210
return rmpb .RunawayAction_NoneAction
207
211
}
208
- if r .markedByWatch {
212
+ if r .markedByQueryWatchRule {
209
213
return r .watchAction
210
214
}
211
- if r .markedByRule .Load () {
215
+ if r .markedByIdentifyInRunawaySettings .Load () {
212
216
return r .settings .Action
213
217
}
214
218
return rmpb .RunawayAction_NoneAction
@@ -217,17 +221,17 @@ func (r *Checker) CheckAction() rmpb.RunawayAction {
217
221
// CheckRuleKillAction checks whether the query should be killed according to the group settings.
218
222
func (r * Checker ) CheckRuleKillAction () (string , bool ) {
219
223
// If the group settings are not available, and it's not marked by watch, skip this part.
220
- if r == nil || r .settings == nil && ! r .markedByWatch {
224
+ if r == nil || r .settings == nil && ! r .markedByQueryWatchRule {
221
225
return "" , false
222
226
}
223
227
// If the group settings are available, and it's not marked by rule, check the execution time.
224
- if r .settings != nil && ! r .markedByRule .Load () {
228
+ if r .settings != nil && ! r .markedByIdentifyInRunawaySettings .Load () {
225
229
now := time .Now ()
226
230
exceedCause := r .exceedsThresholds (now , nil , 0 )
227
231
if exceedCause == "" {
228
232
return "" , false
229
233
}
230
- r .markRunawayByIdentify (& now , exceedCause )
234
+ r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause )
231
235
return exceedCause , r .settings .Action == rmpb .RunawayAction_Kill
232
236
}
233
237
return "" , false
@@ -243,19 +247,19 @@ func (r *Checker) markQuarantine(now *time.Time, exceedCause string) {
243
247
r .settings .Action , r .settings .SwitchGroupName , ttl , now , exceedCause )
244
248
}
245
249
246
- func (r * Checker ) markRunawayByIdentify (now * time.Time , exceedCause string ) bool {
247
- swapped := r .markedByRule .CompareAndSwap (false , true )
250
+ func (r * Checker ) markRunawayByIdentifyInRunawaySettings (now * time.Time , exceedCause string ) bool {
251
+ swapped := r .markedByIdentifyInRunawaySettings .CompareAndSwap (false , true )
248
252
if swapped {
249
253
r .markRunaway ("identify" , r .settings .Action , r .settings .SwitchGroupName , now , exceedCause )
250
- if ! r .markedByWatch {
254
+ if ! r .markedByQueryWatchRule {
251
255
r .markQuarantine (now , exceedCause )
252
256
}
253
257
}
254
258
return swapped
255
259
}
256
260
257
- func (r * Checker ) markRunawayByWatch (action rmpb.RunawayAction , switchGroupName , exceedCause string ) {
258
- r .markedByWatch = true
261
+ func (r * Checker ) markRunawayByQueryWatchRule (action rmpb.RunawayAction , switchGroupName , exceedCause string ) {
262
+ r .markedByQueryWatchRule = true
259
263
r .watchAction = action
260
264
now := time .Now ()
261
265
r .markRunaway ("watch" , action , switchGroupName , & now , exceedCause )
@@ -326,15 +330,15 @@ func (r *Checker) CheckThresholds(ruDetail *util.RUDetails, processKeys int64, e
326
330
// add the processed keys to the total processed keys.
327
331
r .totalProcessedKeys += processKeys
328
332
exceedCause := r .exceedsThresholds (checkTime , ruDetail , r .totalProcessedKeys )
329
- if ! r .markedByRule .Load () {
330
- if exceedCause != "" && r .markRunawayByIdentify (& now , exceedCause ) {
331
- if r .markRunawayByIdentify (& now , exceedCause ) {
333
+ if ! r .markedByIdentifyInRunawaySettings .Load () {
334
+ if exceedCause != "" && r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause ) {
335
+ if r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause ) {
332
336
return exeerrors .ErrResourceGroupQueryRunawayInterrupted .FastGenByArgs (exceedCause )
333
337
}
334
338
}
335
339
}
336
340
// Due to concurrency, check again.
337
- if r .markedByRule .Load () {
341
+ if r .markedByIdentifyInRunawaySettings .Load () {
338
342
return exeerrors .ErrResourceGroupQueryRunawayInterrupted .FastGenByArgs (exceedCause )
339
343
}
340
344
0 commit comments