mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
query.go
483 lines (434 loc) · 14.7 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testvariants
import (
"context"
"text/template"
"cloud.google.com/go/spanner"
"google.golang.org/protobuf/proto"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/trace"
"go.chromium.org/luci/resultdb/internal/invocations"
"go.chromium.org/luci/resultdb/internal/pagination"
uipb "go.chromium.org/luci/resultdb/internal/proto/ui"
"go.chromium.org/luci/resultdb/internal/spanutil"
"go.chromium.org/luci/resultdb/internal/testresults"
"go.chromium.org/luci/resultdb/pbutil"
pb "go.chromium.org/luci/resultdb/proto/v1"
)
// testResultLimit is the limit of test results each test variant includes.
const testResultLimit = 10
// Query specifies test variants to fetch.
type Query struct {
InvocationIDs invocations.IDSet
Predicate *uipb.TestVariantPredicate
PageSize int // must be positive
// Consists of test variant status, test id and variant hash.
PageToken string
decompressBuf []byte // buffer for decompressing blobs
params map[string]interface{} // query parameters
}
// tvResult matches the result STRUCT of a test variant from the query.
type tvResult struct {
InvocationID string
ResultID string
IsUnexpected spanner.NullBool
Status int64
StartTime spanner.NullTime
RunDurationUsec spanner.NullInt64
SummaryHTML []byte
Tags []string
}
func (q *Query) decompressText(src []byte) (string, error) {
if len(src) == 0 {
return "", nil
}
var err error
if q.decompressBuf, err = spanutil.Decompress(src, q.decompressBuf); err != nil {
return "", err
}
return string(q.decompressBuf), nil
}
func (q *Query) toTestResultProto(r *tvResult, testID string) (*pb.TestResult, error) {
tr := &pb.TestResult{
Name: pbutil.TestResultName(string(invocations.IDFromRowID(r.InvocationID)), testID, r.ResultID),
ResultId: r.ResultID,
Status: pb.TestStatus(r.Status),
}
if r.StartTime.Valid {
tr.StartTime = pbutil.MustTimestampProto(r.StartTime.Time)
}
testresults.PopulateExpectedField(tr, r.IsUnexpected)
testresults.PopulateDurationField(tr, r.RunDurationUsec)
var err error
if tr.SummaryHtml, err = q.decompressText(r.SummaryHTML); err != nil {
return nil, err
}
// Populate Tags.
tr.Tags = make([]*pb.StringPair, len(r.Tags))
for i, p := range r.Tags {
if tr.Tags[i], err = pbutil.StringPairFromString(p); err != nil {
return nil, err
}
}
return tr, nil
}
func (q *Query) queryTestVariantsWithUnexpectedResults(ctx context.Context, f func(*uipb.TestVariant) error) (err error) {
ctx, ts := trace.StartSpan(ctx, "testvariants.Query.run")
ts.Attribute("cr.dev/invocations", len(q.InvocationIDs))
defer func() { ts.End(err) }()
if q.PageSize < 0 {
panic("PageSize < 0")
}
st, err := spanutil.GenerateStatement(testVariantsWithUnexpectedResultsSQLTmpl, map[string]interface{}{
"StatusFilter": q.Predicate.GetStatus() != 0,
})
if err != nil {
return
}
st.Params = q.params
st.Params["limit"] = q.PageSize
st.Params["testResultLimit"] = testResultLimit
var b spanutil.Buffer
return spanutil.Query(ctx, st, func(row *spanner.Row) error {
tv := &uipb.TestVariant{}
var tvStatus int64
var results []*tvResult
var exoExplanationHtmls [][]byte
var tmd spanutil.Compressed
if err := b.FromSpanner(row, &tv.TestId, &tv.VariantHash, &tv.Variant, &tmd, &tvStatus, &results, &exoExplanationHtmls); err != nil {
return err
}
tv.Status = uipb.TestVariantStatus(tvStatus)
if tv.Status == uipb.TestVariantStatus_EXPECTED {
panic("query of test variants with unexpected results returned a test variant with only expected results.")
}
if err := populateTestMetadata(tv, tmd); err != nil {
return errors.Annotate(err, "error unmarshalling test_metadata for %s", tv.TestId).Err()
}
// Populate tv.Results
tv.Results = make([]*uipb.TestResultBundle, len(results))
for i, r := range results {
tr, err := q.toTestResultProto(r, tv.TestId)
if err != nil {
return err
}
tv.Results[i] = &uipb.TestResultBundle{
Result: tr,
}
}
// Populate tv.Exonerations
if len(exoExplanationHtmls) == 0 {
return f(tv)
}
tv.Exonerations = make([]*pb.TestExoneration, len(exoExplanationHtmls))
for i, ex := range exoExplanationHtmls {
tv.Exonerations[i] = &pb.TestExoneration{}
if tv.Exonerations[i].ExplanationHtml, err = q.decompressText(ex); err != nil {
return err
}
}
return f(tv)
})
}
func (q *Query) queryTestResults(ctx context.Context, limit int, f func(*pb.TestResult, spanutil.Compressed) error) (err error) {
ctx, ts := trace.StartSpan(ctx, "testvariants.Query.queryTestResults")
ts.Attribute("cr.dev/invocations", len(q.InvocationIDs))
defer func() { ts.End(err) }()
st := spanner.Statement{SQL: allTestResultsSQL, Params: q.params}
st.Params["limit"] = limit
var b spanutil.Buffer
var summaryHTML spanutil.Compressed
return spanutil.Query(ctx, st, func(row *spanner.Row) error {
var invID invocations.ID
var maybeUnexpected spanner.NullBool
var micros spanner.NullInt64
var tmd spanutil.Compressed
tr := &pb.TestResult{}
err := b.FromSpanner(
row, &tr.TestId, &tr.VariantHash, &tr.Variant, &tmd,
&invID, &tr.ResultId, &maybeUnexpected, &tr.Status, &tr.StartTime,
µs, &summaryHTML, &tr.Tags,
)
if err != nil {
return err
}
tr.Name = pbutil.TestResultName(string(invID), tr.TestId, tr.ResultId)
tr.SummaryHtml = string(summaryHTML)
testresults.PopulateExpectedField(tr, maybeUnexpected)
testresults.PopulateDurationField(tr, micros)
return f(tr, tmd)
})
}
func (q *Query) fetchTestVariantsWithOnlyExpectedResults(ctx context.Context) (tvs []*uipb.TestVariant, nextPageToken string, err error) {
tvs = make([]*uipb.TestVariant, 0, q.PageSize)
// Number of the total test results returned by the query.
trLen := 0
type tvId struct {
TestId string
VariantHash string
}
// The last test variant we have completely processed.
var lastProcessedTV tvId
// The test variant we're processing right now.
// It will be appended to tvs when all of its results are processed unless
// it has unexpected results.
var current *uipb.TestVariant
var currentOnlyExpected bool
// Query q.PageSize+1 test results for test variants with
// only expected results, so that in the case of all test results are
// expected in that page, we will return q.PageSize test variants instead of
// q.PageSize-1.
pageSize := q.PageSize + 1
err = q.queryTestResults(ctx, pageSize, func(tr *pb.TestResult, tmd spanutil.Compressed) error {
trLen++
if current != nil {
if current.TestId == tr.TestId && current.VariantHash == tr.VariantHash {
if len(current.Results) < testResultLimit {
current.Results = append(current.Results, &uipb.TestResultBundle{
Result: tr,
})
}
currentOnlyExpected = currentOnlyExpected && tr.Expected
return nil
}
// Different TestId or VariantHash from current, so all test results of
// current have been processed.
lastProcessedTV.TestId = current.TestId
lastProcessedTV.VariantHash = current.VariantHash
if currentOnlyExpected {
tvs = append(tvs, current)
}
}
// New test variant.
current = &uipb.TestVariant{
TestId: tr.TestId,
VariantHash: tr.VariantHash,
Variant: tr.Variant,
Status: uipb.TestVariantStatus_EXPECTED,
Results: []*uipb.TestResultBundle{
{
Result: tr,
},
},
}
currentOnlyExpected = tr.Expected
if err := populateTestMetadata(current, tmd); err != nil {
return errors.Annotate(err, "error unmarshalling test_metadata for %s", current.TestId).Err()
}
return nil
})
switch {
case err != nil:
tvs = nil
case trLen < pageSize && currentOnlyExpected:
// We have exhausted the test results, add current to tvs.
tvs = append(tvs, current)
case trLen == pageSize && !currentOnlyExpected:
// Got page size of test results, need to return the next page token.
// And current has unexpected results, skip it in the next page.
nextPageToken = pagination.Token(uipb.TestVariantStatus_EXPECTED.String(), current.TestId, current.VariantHash)
case trLen == pageSize:
// In this page current only has expected results, but we're not sure if
// we have exhausted its test results or not. Calculate the token using lastProcessedTV.
nextPageToken = pagination.Token(uipb.TestVariantStatus_EXPECTED.String(), lastProcessedTV.TestId, lastProcessedTV.VariantHash)
}
return
}
// Fetch returns a page of test variants matching q.
// Returned test variants are ordered by test variant status, test ID and variant hash.
func (q *Query) Fetch(ctx context.Context) (tvs []*uipb.TestVariant, nextPageToken string, err error) {
if q.PageSize <= 0 {
panic("PageSize <= 0")
}
q.params = map[string]interface{}{
"invIDs": q.InvocationIDs,
"skipStatus": int(pb.TestStatus_SKIP),
"unexpected": int(uipb.TestVariantStatus_UNEXPECTED),
"unexpectedlySkipped": int(uipb.TestVariantStatus_UNEXPECTEDLY_SKIPPED),
"flaky": int(uipb.TestVariantStatus_FLAKY),
"exonerated": int(uipb.TestVariantStatus_EXONERATED),
"expected": int(uipb.TestVariantStatus_EXPECTED),
"status": int(q.Predicate.GetStatus()),
}
var expected bool
switch parts, err := pagination.ParseToken(q.PageToken); {
case err != nil:
return nil, "", err
case len(parts) == 0:
expected = false
q.params["afterTvStatus"] = 0
q.params["afterTestId"] = ""
q.params["afterVariantHash"] = ""
case len(parts) != 3:
return nil, "", pagination.InvalidToken(errors.Reason("expected 3 components, got %q", parts).Err())
default:
status, ok := uipb.TestVariantStatus_value[parts[0]]
if !ok {
return nil, "", pagination.InvalidToken(errors.Reason("unrecognized test variant status: %q", parts[0]).Err())
}
expected = uipb.TestVariantStatus(status) == uipb.TestVariantStatus_EXPECTED
q.params["afterTvStatus"] = int(status)
q.params["afterTestId"] = parts[1]
q.params["afterVariantHash"] = parts[2]
}
if q.Predicate.GetStatus() == uipb.TestVariantStatus_EXPECTED {
expected = true
}
if expected {
return q.fetchTestVariantsWithOnlyExpectedResults(ctx)
}
tvs = make([]*uipb.TestVariant, 0, q.PageSize)
// Fetch test variants with unexpected results.
err = q.queryTestVariantsWithUnexpectedResults(ctx, func(tv *uipb.TestVariant) error {
tvs = append(tvs, tv)
return nil
})
switch {
case err != nil:
tvs = nil
case len(tvs) < q.PageSize && q.Predicate.GetStatus() != 0:
// The query is for test variants with specific status, so the query reaches
// to its last results already.
case len(tvs) < q.PageSize:
// If we got less than one page of test variants with unexpected results,
// and the query is not for test variants with specific status,
// compute the nextPageToken for test variants with only expected results.
nextPageToken = pagination.Token(uipb.TestVariantStatus_EXPECTED.String(), "", "")
default:
last := tvs[q.PageSize-1]
nextPageToken = pagination.Token(last.Status.String(), last.TestId, last.VariantHash)
}
return
}
var testVariantsWithUnexpectedResultsSQLTmpl = template.Must(template.New("testVariantsWithUnexpectedResultsSQL").Parse(`
@{USE_ADDITIONAL_PARALLELISM=TRUE}
WITH unexpectedTestVariants AS (
SELECT DISTINCT TestId, VariantHash
FROM TestResults@{FORCE_INDEX=UnexpectedTestResults, spanner_emulator.disable_query_null_filtered_index_check=true}
WHERE IsUnexpected AND InvocationId in UNNEST(@invIDs)
),
-- Get test variants and their results.
-- Also count the number of unexpected results and total results for each test
-- variant, which will be used to classify test variants.
test_variants AS (
SELECT
TestId,
VariantHash,
ANY_VALUE(Variant) Variant,
ANY_VALUE(TestMetadata) TestMetadata,
COUNTIF(IsUnexpected) num_unexpected,
COUNTIF(Status=@skipStatus) num_skipped,
COUNT(TestId) num_total,
ARRAY_AGG(STRUCT(
InvocationId,
ResultId,
IsUnexpected,
Status,
StartTime,
RunDurationUsec,
SummaryHTML,
Tags
)) results,
FROM unexpectedTestVariants vur
JOIN@{FORCE_JOIN_ORDER=TRUE, JOIN_METHOD=HASH_JOIN} TestResults tr USING (TestId, VariantHash)
WHERE InvocationId in UNNEST(@invIDs)
GROUP BY TestId, VariantHash
),
exonerated AS (
SELECT
TestId,
VariantHash,
ARRAY_AGG(ExplanationHTML) exonerationExplanations
FROM TestExonerations
WHERE InvocationId IN UNNEST(@invIDs)
GROUP BY TestId, VariantHash
),
testVariantsWithUnexpectedResults AS (
SELECT
tv.TestId,
tv.VariantHash,
tv.Variant,
tv.TestMetadata,
CASE
WHEN exonerated.TestId IS NOT NULL THEN @exonerated
WHEN num_unexpected = 0 THEN @expected -- should never happen in this query
WHEN num_skipped = num_unexpected AND num_skipped = num_total THEN @unexpectedlySkipped
WHEN num_unexpected = num_total THEN @unexpected
ELSE @flaky
END TvStatus,
ARRAY(
SELECT AS STRUCT *
FROM UNNEST(tv.results)
LIMIT @testResultLimit) results,
exonerated.exonerationExplanations
FROM test_variants tv
LEFT JOIN exonerated USING(TestId, VariantHash)
ORDER BY TvStatus, TestId, VariantHash
)
SELECT
TestId,
VariantHash,
Variant,
TestMetadata,
TvStatus,
results,
exonerationExplanations,
FROM testVariantsWithUnexpectedResults
{{if .StatusFilter}}
WHERE (
(TvStatus = @status AND TestId > @afterTestId) OR
(TvStatus = @status AND TestId = @afterTestId AND VariantHash > @afterVariantHash)
)
{{else}}
WHERE (
(TvStatus > @afterTvStatus) OR
(TvStatus = @afterTvStatus AND TestId > @afterTestId) OR
(TvStatus = @afterTvStatus AND TestId = @afterTestId AND VariantHash > @afterVariantHash)
)
{{end}}
ORDER BY TvStatus, TestId, VariantHash
LIMIT @limit
`))
var allTestResultsSQL = `
@{USE_ADDITIONAL_PARALLELISM=TRUE}
SELECT
TestId,
VariantHash,
Variant,
TestMetadata,
InvocationId,
ResultId,
IsUnexpected,
Status,
StartTime,
RunDurationUsec,
SummaryHTML,
Tags,
FROM TestResults
WHERE InvocationId in UNNEST(@invIDs)
AND (
(TestId > @afterTestId) OR
(TestId = @afterTestId AND VariantHash > @afterVariantHash)
)
ORDER BY TestId, VariantHash
LIMIT @limit
`
func populateTestMetadata(tv *uipb.TestVariant, tmd spanutil.Compressed) error {
if len(tmd) == 0 {
return nil
}
tv.TestMetadata = &pb.TestMetadata{}
return proto.Unmarshal(tmd, tv.TestMetadata)
}