@@ -13,6 +13,7 @@ import (
13
13
14
14
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
15
15
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
16
+ "github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
16
17
"github.com/stretchr/testify/require"
17
18
)
18
19
@@ -34,17 +35,17 @@ func TestAnyDetector(t *testing.T) {
34
35
35
36
t .Run ("isSlow is false without any detectors" , func (t * testing.T ) {
36
37
detector := & compositeDetector {}
37
- require .False (t , detector .isSlow (& Statement {}))
38
+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats {}))
38
39
})
39
40
40
41
t .Run ("isSlow is false without any concerned detectors" , func (t * testing.T ) {
41
42
detector := & compositeDetector {[]detector {& fakeDetector {}, & fakeDetector {}}}
42
- require .False (t , detector .isSlow (& Statement {}))
43
+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats {}))
43
44
})
44
45
45
46
t .Run ("isSlow is true with at least one concerned detector" , func (t * testing.T ) {
46
47
detector := & compositeDetector {[]detector {& fakeDetector {stubIsSlow : true }, & fakeDetector {}}}
47
- require .True (t , detector .isSlow (& Statement {}))
48
+ require .True (t , detector .isSlow (& sqlstats. RecordedStmtStats {}))
48
49
})
49
50
50
51
t .Run ("isSlow consults all detectors without short-circuiting" , func (t * testing.T ) {
@@ -55,7 +56,7 @@ func TestAnyDetector(t *testing.T) {
55
56
d2 := & fakeDetector {stubIsSlow : true }
56
57
57
58
detector := & compositeDetector {[]detector {d1 , d2 }}
58
- detector .isSlow (& Statement {})
59
+ detector .isSlow (& sqlstats. RecordedStmtStats {})
59
60
require .True (t , d1 .isSlowCalled , "the first detector should be consulted" )
60
61
require .True (t , d2 .isSlowCalled , "the second detector should be consulted" )
61
62
})
@@ -106,16 +107,18 @@ func TestLatencyQuantileDetector(t *testing.T) {
106
107
for _ , test := range tests {
107
108
t .Run (test .name , func (t * testing.T ) {
108
109
d := newAnomalyDetector (st , NewMetrics ())
110
+ stmtWithSeedLatency := & sqlstats.RecordedStmtStats {ServiceLatencySec : test .seedLatency .Seconds ()}
109
111
for i := 0 ; i < 1000 ; i ++ {
110
- d .isSlow (& Statement { LatencyInSeconds : test . seedLatency . Seconds ()} )
112
+ d .isSlow (stmtWithSeedLatency )
111
113
}
112
- require .Equal (t , test .isSlow , d .isSlow (& Statement {LatencyInSeconds : test .candidateLatency .Seconds ()}))
114
+ // Now determine if the candidate latency is slow.
115
+ require .Equal (t , test .isSlow , d .isSlow (& sqlstats.RecordedStmtStats {ServiceLatencySec : test .candidateLatency .Seconds ()}))
113
116
})
114
117
}
115
118
})
116
119
117
120
// Testing the slow and failure detectors at the same time.
118
- t .Run ("isSlow and isFailed " , func (t * testing.T ) {
121
+ t .Run ("isSlow with slow and failed statement " , func (t * testing.T ) {
119
122
ctx := context .Background ()
120
123
st := cluster .MakeTestingClusterSettings ()
121
124
AnomalyDetectionEnabled .Override (ctx , & st .SV , true )
@@ -127,46 +130,41 @@ func TestLatencyQuantileDetector(t *testing.T) {
127
130
candidateLatency time.Duration
128
131
status Statement_Status
129
132
isSlow bool
130
- isFailed bool
131
133
}{{
132
134
name : "slow and failed statement" ,
133
135
seedLatency : 100 * time .Millisecond ,
134
136
candidateLatency : 200 * time .Millisecond ,
135
137
status : Statement_Failed ,
136
138
isSlow : true ,
137
- isFailed : true ,
138
139
}, {
139
140
name : "slow and non-failed statement" ,
140
141
seedLatency : 100 * time .Millisecond ,
141
142
candidateLatency : 200 * time .Millisecond ,
142
143
status : Statement_Completed ,
143
144
isSlow : true ,
144
- isFailed : false ,
145
145
}, {
146
146
name : "fast and non-failed statement" ,
147
147
seedLatency : 100 * time .Millisecond ,
148
148
candidateLatency : 50 * time .Millisecond ,
149
149
status : Statement_Completed ,
150
150
isSlow : false ,
151
- isFailed : false ,
152
151
}, {
153
152
name : "fast and failed statement" ,
154
153
seedLatency : 100 * time .Millisecond ,
155
154
candidateLatency : 50 * time .Millisecond ,
156
155
status : Statement_Failed ,
157
156
isSlow : false ,
158
- isFailed : true ,
159
157
}}
160
158
161
159
for _ , test := range tests {
162
160
t .Run (test .name , func (t * testing.T ) {
163
161
d := newAnomalyDetector (st , NewMetrics ())
162
+ stmtWithSeedLatency := & sqlstats.RecordedStmtStats {ServiceLatencySec : test .seedLatency .Seconds ()}
164
163
for i := 0 ; i < 1000 ; i ++ {
165
- d .isSlow (& Statement { LatencyInSeconds : test . seedLatency . Seconds ()} )
164
+ d .isSlow (stmtWithSeedLatency )
166
165
}
167
- stmt := & Statement {LatencyInSeconds : test .candidateLatency .Seconds (), Status : test .status }
168
- require .Equal (t , test .isSlow , d .isSlow (stmt ))
169
- require .Equal (t , test .isFailed , isFailed (stmt ))
166
+ // Now determine if the candidate latency is slow.
167
+ require .Equal (t , test .isSlow , d .isSlow (& sqlstats.RecordedStmtStats {ServiceLatencySec : test .candidateLatency .Seconds ()}))
170
168
})
171
169
}
172
170
})
@@ -223,10 +221,11 @@ func TestLatencyQuantileDetector(t *testing.T) {
223
221
d := newAnomalyDetector (st , metrics )
224
222
// Show the detector `test.fingerprints` distinct fingerprints.
225
223
for i := 0 ; i < test .fingerprints ; i ++ {
226
- d .isSlow (& Statement {
227
- LatencyInSeconds : AnomalyDetectionLatencyThreshold .Get (& st .SV ).Seconds (),
228
- FingerprintID : appstatspb .StmtFingerprintID (i ),
229
- })
224
+ stmt := & sqlstats.RecordedStmtStats {
225
+ ServiceLatencySec : AnomalyDetectionLatencyThreshold .Get (& st .SV ).Seconds (),
226
+ FingerprintID : appstatspb .StmtFingerprintID (i ),
227
+ }
228
+ d .isSlow (stmt )
230
229
}
231
230
test .assertion (t , metrics )
232
231
})
@@ -241,10 +240,11 @@ func BenchmarkLatencyQuantileDetector(b *testing.B) {
241
240
settings := cluster .MakeTestingClusterSettings ()
242
241
AnomalyDetectionEnabled .Override (context .Background (), & settings .SV , true )
243
242
d := newAnomalyDetector (settings , NewMetrics ())
243
+ stmt := & sqlstats.RecordedStmtStats {
244
+ ServiceLatencySec : random .Float64 (),
245
+ }
244
246
for i := 0 ; i < b .N ; i ++ {
245
- d .isSlow (& Statement {
246
- LatencyInSeconds : random .Float64 (),
247
- })
247
+ d .isSlow (stmt )
248
248
}
249
249
}
250
250
@@ -267,21 +267,21 @@ func TestLatencyThresholdDetector(t *testing.T) {
267
267
st := cluster .MakeTestingClusterSettings ()
268
268
LatencyThreshold .Override (context .Background (), & st .SV , 0 )
269
269
detector := latencyThresholdDetector {st : st }
270
- require .False (t , detector .isSlow (& Statement { LatencyInSeconds : 1 }))
270
+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats { ServiceLatencySec : 1 }))
271
271
})
272
272
273
273
t .Run ("isSlow false when fast enough" , func (t * testing.T ) {
274
274
st := cluster .MakeTestingClusterSettings ()
275
275
LatencyThreshold .Override (context .Background (), & st .SV , 1 * time .Second )
276
276
detector := latencyThresholdDetector {st : st }
277
- require .False (t , detector .isSlow (& Statement { LatencyInSeconds : 0.5 }))
277
+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats { ServiceLatencySec : 0.5 }))
278
278
})
279
279
280
280
t .Run ("isSlow true beyond threshold" , func (t * testing.T ) {
281
281
st := cluster .MakeTestingClusterSettings ()
282
282
LatencyThreshold .Override (context .Background (), & st .SV , 1 * time .Second )
283
283
detector := latencyThresholdDetector {st : st }
284
- require .True (t , detector .isSlow (& Statement { LatencyInSeconds : 1 }))
284
+ require .True (t , detector .isSlow (& sqlstats. RecordedStmtStats { ServiceLatencySec : 1 }))
285
285
})
286
286
}
287
287
@@ -295,7 +295,7 @@ func (f *fakeDetector) enabled() bool {
295
295
return f .stubEnabled
296
296
}
297
297
298
- func (f * fakeDetector ) isSlow (* Statement ) bool {
298
+ func (f * fakeDetector ) isSlow (stats * sqlstats. RecordedStmtStats ) bool {
299
299
f .isSlowCalled = true
300
300
return f .stubIsSlow
301
301
}
0 commit comments