@@ -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,11 @@ 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 query.
96
+ if len (planDigest ) == 0 {
97
+ logutil .BgLogger ().Warn ("cannot setup up runaway checker, there is no plan digest" , zap .Error (err ))
98
+ return nil
99
+ }
95
100
rm .ActiveLock .RLock ()
96
101
defer rm .ActiveLock .RUnlock ()
97
102
if group .RunawaySettings == nil && rm .ActiveGroup [resourceGroupName ] == 0 {
@@ -129,7 +134,7 @@ func (r *Checker) BeforeExecutor() (string, error) {
129
134
switchGroupName = r .settings .SwitchGroupName
130
135
}
131
136
// Mark it if this is the first time being watched.
132
- r .markRunawayByWatch (action , switchGroupName , exceedCause )
137
+ r .markRunawayByQueryWatchRule (action , switchGroupName , exceedCause )
133
138
// Take action if needed.
134
139
switch action {
135
140
case rmpb .RunawayAction_Kill :
@@ -157,16 +162,16 @@ func (r *Checker) BeforeCopRequest(req *tikvrpc.Request) error {
157
162
return nil
158
163
}
159
164
// If the group settings are not available, and it's not marked by watch, skip this part.
160
- if r .settings == nil && ! r .markedByWatch {
165
+ if r .settings == nil && ! r .markedByQueryWatchRule {
161
166
return nil
162
167
}
163
168
// If it's marked by watch and the action is cooldown, override the priority,
164
- if r .markedByWatch && r .watchAction == rmpb .RunawayAction_CoolDown {
169
+ if r .markedByQueryWatchRule && r .watchAction == rmpb .RunawayAction_CoolDown {
165
170
req .ResourceControlContext .OverridePriority = 1 // set priority to lowest
166
171
}
167
172
// If group settings are available and the query is not marked by a rule,
168
173
// verify if it matches any rules in the settings.
169
- if r .settings != nil && ! r .markedByRule .Load () {
174
+ if r .settings != nil && ! r .markedByIdentifyInRunawaySettings .Load () {
170
175
now := time .Now ()
171
176
// only check time and need to ensure deadline existed.
172
177
exceedCause := r .exceedsThresholds (now , nil , 0 )
@@ -181,7 +186,7 @@ func (r *Checker) BeforeCopRequest(req *tikvrpc.Request) error {
181
186
return nil
182
187
}
183
188
// execution time exceeds the threshold, mark the query as runaway
184
- r .markRunawayByIdentify (& now , exceedCause )
189
+ r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause )
185
190
// Take action if needed.
186
191
switch r .settings .Action {
187
192
case rmpb .RunawayAction_Kill :
@@ -205,10 +210,10 @@ func (r *Checker) CheckAction() rmpb.RunawayAction {
205
210
if r == nil {
206
211
return rmpb .RunawayAction_NoneAction
207
212
}
208
- if r .markedByWatch {
213
+ if r .markedByQueryWatchRule {
209
214
return r .watchAction
210
215
}
211
- if r .markedByRule .Load () {
216
+ if r .markedByIdentifyInRunawaySettings .Load () {
212
217
return r .settings .Action
213
218
}
214
219
return rmpb .RunawayAction_NoneAction
@@ -217,17 +222,17 @@ func (r *Checker) CheckAction() rmpb.RunawayAction {
217
222
// CheckRuleKillAction checks whether the query should be killed according to the group settings.
218
223
func (r * Checker ) CheckRuleKillAction () (string , bool ) {
219
224
// 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 {
225
+ if r == nil || r .settings == nil && ! r .markedByQueryWatchRule {
221
226
return "" , false
222
227
}
223
228
// 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 () {
229
+ if r .settings != nil && ! r .markedByIdentifyInRunawaySettings .Load () {
225
230
now := time .Now ()
226
231
exceedCause := r .exceedsThresholds (now , nil , 0 )
227
232
if exceedCause == "" {
228
233
return "" , false
229
234
}
230
- r .markRunawayByIdentify (& now , exceedCause )
235
+ r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause )
231
236
return exceedCause , r .settings .Action == rmpb .RunawayAction_Kill
232
237
}
233
238
return "" , false
@@ -243,19 +248,19 @@ func (r *Checker) markQuarantine(now *time.Time, exceedCause string) {
243
248
r .settings .Action , r .settings .SwitchGroupName , ttl , now , exceedCause )
244
249
}
245
250
246
- func (r * Checker ) markRunawayByIdentify (now * time.Time , exceedCause string ) bool {
247
- swapped := r .markedByRule .CompareAndSwap (false , true )
251
+ func (r * Checker ) markRunawayByIdentifyInRunawaySettings (now * time.Time , exceedCause string ) bool {
252
+ swapped := r .markedByIdentifyInRunawaySettings .CompareAndSwap (false , true )
248
253
if swapped {
249
254
r .markRunaway ("identify" , r .settings .Action , r .settings .SwitchGroupName , now , exceedCause )
250
- if ! r .markedByWatch {
255
+ if ! r .markedByQueryWatchRule {
251
256
r .markQuarantine (now , exceedCause )
252
257
}
253
258
}
254
259
return swapped
255
260
}
256
261
257
- func (r * Checker ) markRunawayByWatch (action rmpb.RunawayAction , switchGroupName , exceedCause string ) {
258
- r .markedByWatch = true
262
+ func (r * Checker ) markRunawayByQueryWatchRule (action rmpb.RunawayAction , switchGroupName , exceedCause string ) {
263
+ r .markedByQueryWatchRule = true
259
264
r .watchAction = action
260
265
now := time .Now ()
261
266
r .markRunaway ("watch" , action , switchGroupName , & now , exceedCause )
@@ -326,15 +331,15 @@ func (r *Checker) CheckThresholds(ruDetail *util.RUDetails, processKeys int64, e
326
331
// add the processed keys to the total processed keys.
327
332
r .totalProcessedKeys += processKeys
328
333
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 ) {
334
+ if ! r .markedByIdentifyInRunawaySettings .Load () {
335
+ if exceedCause != "" && r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause ) {
336
+ if r .markRunawayByIdentifyInRunawaySettings (& now , exceedCause ) {
332
337
return exeerrors .ErrResourceGroupQueryRunawayInterrupted .FastGenByArgs (exceedCause )
333
338
}
334
339
}
335
340
}
336
341
// Due to concurrency, check again.
337
- if r .markedByRule .Load () {
342
+ if r .markedByIdentifyInRunawaySettings .Load () {
338
343
return exeerrors .ErrResourceGroupQueryRunawayInterrupted .FastGenByArgs (exceedCause )
339
344
}
340
345
0 commit comments