@@ -13,6 +13,7 @@ import (
1313
1414 "github.com/cockroachdb/cockroach/pkg/settings/cluster"
1515 "github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
16+ "github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
1617 "github.com/stretchr/testify/require"
1718)
1819
@@ -34,17 +35,17 @@ func TestAnyDetector(t *testing.T) {
3435
3536 t .Run ("isSlow is false without any detectors" , func (t * testing.T ) {
3637 detector := & compositeDetector {}
37- require .False (t , detector .isSlow (& Statement {}))
38+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats {}))
3839 })
3940
4041 t .Run ("isSlow is false without any concerned detectors" , func (t * testing.T ) {
4142 detector := & compositeDetector {[]detector {& fakeDetector {}, & fakeDetector {}}}
42- require .False (t , detector .isSlow (& Statement {}))
43+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats {}))
4344 })
4445
4546 t .Run ("isSlow is true with at least one concerned detector" , func (t * testing.T ) {
4647 detector := & compositeDetector {[]detector {& fakeDetector {stubIsSlow : true }, & fakeDetector {}}}
47- require .True (t , detector .isSlow (& Statement {}))
48+ require .True (t , detector .isSlow (& sqlstats. RecordedStmtStats {}))
4849 })
4950
5051 t .Run ("isSlow consults all detectors without short-circuiting" , func (t * testing.T ) {
@@ -55,7 +56,7 @@ func TestAnyDetector(t *testing.T) {
5556 d2 := & fakeDetector {stubIsSlow : true }
5657
5758 detector := & compositeDetector {[]detector {d1 , d2 }}
58- detector .isSlow (& Statement {})
59+ detector .isSlow (& sqlstats. RecordedStmtStats {})
5960 require .True (t , d1 .isSlowCalled , "the first detector should be consulted" )
6061 require .True (t , d2 .isSlowCalled , "the second detector should be consulted" )
6162 })
@@ -106,16 +107,18 @@ func TestLatencyQuantileDetector(t *testing.T) {
106107 for _ , test := range tests {
107108 t .Run (test .name , func (t * testing.T ) {
108109 d := newAnomalyDetector (st , NewMetrics ())
110+ stmtWithSeedLatency := & sqlstats.RecordedStmtStats {ServiceLatencySec : test .seedLatency .Seconds ()}
109111 for i := 0 ; i < 1000 ; i ++ {
110- d .isSlow (& Statement { LatencyInSeconds : test . seedLatency . Seconds ()} )
112+ d .isSlow (stmtWithSeedLatency )
111113 }
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 ()}))
113116 })
114117 }
115118 })
116119
117120 // 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 ) {
119122 ctx := context .Background ()
120123 st := cluster .MakeTestingClusterSettings ()
121124 AnomalyDetectionEnabled .Override (ctx , & st .SV , true )
@@ -127,46 +130,42 @@ func TestLatencyQuantileDetector(t *testing.T) {
127130 candidateLatency time.Duration
128131 status Statement_Status
129132 isSlow bool
130- isFailed bool
131133 }{{
132134 name : "slow and failed statement" ,
133135 seedLatency : 100 * time .Millisecond ,
134136 candidateLatency : 200 * time .Millisecond ,
135137 status : Statement_Failed ,
136138 isSlow : true ,
137- isFailed : true ,
138139 }, {
139140 name : "slow and non-failed statement" ,
140141 seedLatency : 100 * time .Millisecond ,
141142 candidateLatency : 200 * time .Millisecond ,
142143 status : Statement_Completed ,
143144 isSlow : true ,
144- isFailed : false ,
145145 }, {
146146 name : "fast and non-failed statement" ,
147147 seedLatency : 100 * time .Millisecond ,
148148 candidateLatency : 50 * time .Millisecond ,
149149 status : Statement_Completed ,
150150 isSlow : false ,
151- isFailed : false ,
152151 }, {
153152 name : "fast and failed statement" ,
154153 seedLatency : 100 * time .Millisecond ,
155154 candidateLatency : 50 * time .Millisecond ,
156155 status : Statement_Failed ,
157156 isSlow : false ,
158- isFailed : true ,
159157 }}
160158
161159 for _ , test := range tests {
162160 t .Run (test .name , func (t * testing.T ) {
163161 d := newAnomalyDetector (st , NewMetrics ())
162+ stmtWithSeedLatency := & sqlstats.RecordedStmtStats {ServiceLatencySec : test .seedLatency .Seconds ()}
164163 for i := 0 ; i < 1000 ; i ++ {
165- d .isSlow (& Statement { LatencyInSeconds : test . seedLatency . Seconds ()} )
164+ d .isSlow (stmtWithSeedLatency )
166165 }
167- stmt := & Statement {LatencyInSeconds : test .candidateLatency .Seconds (), Status : test .status }
166+ // Now determine if the candidate latency is slow.
167+ stmt := & sqlstats.RecordedStmtStats {Failed : true , ServiceLatencySec : test .candidateLatency .Seconds ()}
168168 require .Equal (t , test .isSlow , d .isSlow (stmt ))
169- require .Equal (t , test .isFailed , isFailed (stmt ))
170169 })
171170 }
172171 })
@@ -223,10 +222,11 @@ func TestLatencyQuantileDetector(t *testing.T) {
223222 d := newAnomalyDetector (st , metrics )
224223 // Show the detector `test.fingerprints` distinct fingerprints.
225224 for i := 0 ; i < test .fingerprints ; i ++ {
226- d .isSlow (& Statement {
227- LatencyInSeconds : AnomalyDetectionLatencyThreshold .Get (& st .SV ).Seconds (),
228- FingerprintID : appstatspb .StmtFingerprintID (i ),
229- })
225+ stmt := & sqlstats.RecordedStmtStats {
226+ ServiceLatencySec : AnomalyDetectionLatencyThreshold .Get (& st .SV ).Seconds (),
227+ FingerprintID : appstatspb .StmtFingerprintID (i ),
228+ }
229+ d .isSlow (stmt )
230230 }
231231 test .assertion (t , metrics )
232232 })
@@ -241,10 +241,11 @@ func BenchmarkLatencyQuantileDetector(b *testing.B) {
241241 settings := cluster .MakeTestingClusterSettings ()
242242 AnomalyDetectionEnabled .Override (context .Background (), & settings .SV , true )
243243 d := newAnomalyDetector (settings , NewMetrics ())
244+ stmt := & sqlstats.RecordedStmtStats {
245+ ServiceLatencySec : random .Float64 (),
246+ }
244247 for i := 0 ; i < b .N ; i ++ {
245- d .isSlow (& Statement {
246- LatencyInSeconds : random .Float64 (),
247- })
248+ d .isSlow (stmt )
248249 }
249250}
250251
@@ -267,21 +268,21 @@ func TestLatencyThresholdDetector(t *testing.T) {
267268 st := cluster .MakeTestingClusterSettings ()
268269 LatencyThreshold .Override (context .Background (), & st .SV , 0 )
269270 detector := latencyThresholdDetector {st : st }
270- require .False (t , detector .isSlow (& Statement { LatencyInSeconds : 1 }))
271+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats { ServiceLatencySec : 1 }))
271272 })
272273
273274 t .Run ("isSlow false when fast enough" , func (t * testing.T ) {
274275 st := cluster .MakeTestingClusterSettings ()
275276 LatencyThreshold .Override (context .Background (), & st .SV , 1 * time .Second )
276277 detector := latencyThresholdDetector {st : st }
277- require .False (t , detector .isSlow (& Statement { LatencyInSeconds : 0.5 }))
278+ require .False (t , detector .isSlow (& sqlstats. RecordedStmtStats { ServiceLatencySec : 0.5 }))
278279 })
279280
280281 t .Run ("isSlow true beyond threshold" , func (t * testing.T ) {
281282 st := cluster .MakeTestingClusterSettings ()
282283 LatencyThreshold .Override (context .Background (), & st .SV , 1 * time .Second )
283284 detector := latencyThresholdDetector {st : st }
284- require .True (t , detector .isSlow (& Statement { LatencyInSeconds : 1 }))
285+ require .True (t , detector .isSlow (& sqlstats. RecordedStmtStats { ServiceLatencySec : 1 }))
285286 })
286287}
287288
@@ -295,7 +296,7 @@ func (f *fakeDetector) enabled() bool {
295296 return f .stubEnabled
296297}
297298
298- func (f * fakeDetector ) isSlow (* Statement ) bool {
299+ func (f * fakeDetector ) isSlow (stats * sqlstats. RecordedStmtStats ) bool {
299300 f .isSlowCalled = true
300301 return f .stubIsSlow
301302}
0 commit comments