-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathprow.go
More file actions
1365 lines (1198 loc) · 40.8 KB
/
prow.go
File metadata and controls
1365 lines (1198 loc) · 40.8 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package prowloader
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"sync"
"sync/atomic"
"time"
"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
"cloud.google.com/go/storage"
"github.com/jackc/pgtype"
"github.com/lib/pq"
"github.com/openshift/sippy/pkg/bigquery/bqlabel"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/push"
log "github.com/sirupsen/logrus"
"google.golang.org/api/iterator"
"gorm.io/gorm"
"gorm.io/gorm/clause"
bqcachedclient "github.com/openshift/sippy/pkg/bigquery"
"github.com/openshift/sippy/pkg/db/query"
v1config "github.com/openshift/sippy/pkg/apis/config/v1"
"github.com/openshift/sippy/pkg/apis/junit"
"github.com/openshift/sippy/pkg/apis/prow"
sippyprocessingv1 "github.com/openshift/sippy/pkg/apis/sippyprocessing/v1"
"github.com/openshift/sippy/pkg/dataloader/prowloader/gcs"
"github.com/openshift/sippy/pkg/dataloader/prowloader/github"
"github.com/openshift/sippy/pkg/dataloader/prowloader/testconversion"
"github.com/openshift/sippy/pkg/db"
"github.com/openshift/sippy/pkg/db/models"
"github.com/openshift/sippy/pkg/github/commenter"
"github.com/openshift/sippy/pkg/synthetictests"
"github.com/openshift/sippy/pkg/testidentification"
"github.com/openshift/sippy/pkg/util"
"github.com/openshift/sippy/pkg/util/sets"
)
// gcsPathStrip is used to strip out everything but the path, i.e. match "/view/gs/origin-ci-test/"
// from the path "/view/gs/origin-ci-test/logs/periodic-ci-openshift-release-master-nightly-4.14-e2e-gcp-sdn/1737420379221135360"
var gcsPathStrip = regexp.MustCompile(`.*/gs/[^/]+/`)
type ProwLoader struct {
ctx context.Context
dbc *db.DB
errors []error
githubClient *github.Client
bigQueryClient *bqcachedclient.Client
maxConcurrency int
prowJobCache map[string]*models.ProwJob
prowJobCacheLock sync.RWMutex
prowJobRunCache map[uint]bool
prowJobRunCacheLock sync.RWMutex
prowJobRunTestCache map[string]uint
prowJobRunTestCacheLock sync.RWMutex
variantManager testidentification.VariantManager
suiteCache map[string]*uint
suiteCacheLock sync.RWMutex
syntheticTestManager synthetictests.SyntheticTestManager
releases []string
config *v1config.SippyConfig
ghCommenter *commenter.GitHubCommenter
jobsImportedCount atomic.Int32
jobsProcessedCount atomic.Int32
gcsClient *storage.Client
promPusher *push.Pusher
}
func New(
ctx context.Context,
dbc *db.DB,
gcsClient *storage.Client,
bigQueryClient *bqcachedclient.Client,
githubClient *github.Client,
variantManager testidentification.VariantManager,
syntheticTestManager synthetictests.SyntheticTestManager,
releases []string,
config *v1config.SippyConfig,
ghCommenter *commenter.GitHubCommenter,
promPusher *push.Pusher) *ProwLoader {
return &ProwLoader{
ctx: ctx,
dbc: dbc,
gcsClient: gcsClient,
githubClient: githubClient,
bigQueryClient: bigQueryClient,
maxConcurrency: 10,
prowJobRunCache: loadProwJobRunCache(dbc),
prowJobCache: loadProwJobCache(dbc),
prowJobRunTestCache: make(map[string]uint),
suiteCache: make(map[string]*uint),
syntheticTestManager: syntheticTestManager,
variantManager: variantManager,
releases: releases,
config: config,
ghCommenter: ghCommenter,
promPusher: promPusher,
}
}
var clusterDataDateTimeName = regexp.MustCompile(`cluster-data_(?P<DATE>.*)-(?P<TIME>.*).json`)
var prowLoaderQueriedMetricGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "sippy_prow_jobs_loaded",
Help: "The number of jobs loaded (queried)",
})
var prowLoaderProcessedMetricGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "sippy_prow_jobs_processed",
Help: "The number of jobs processed (new)",
})
type DateTimeName struct {
Name string
Date string
Time string
}
func loadProwJobCache(dbc *db.DB) map[string]*models.ProwJob {
prowJobCache := map[string]*models.ProwJob{}
var allJobs []*models.ProwJob
dbc.DB.Model(&models.ProwJob{}).Find(&allJobs)
for _, j := range allJobs {
if _, ok := prowJobCache[j.Name]; !ok {
prowJobCache[j.Name] = j
}
}
log.Infof("job cache created with %d entries from database", len(prowJobCache))
return prowJobCache
}
// Cache the IDs of all known ProwJobRuns. Will be used to skip job run and test
// results we've already processed.
// TODO: over 800k in our db now, should we only cache those within last two weeks?
func loadProwJobRunCache(dbc *db.DB) map[uint]bool {
prowJobRunCache := map[uint]bool{} // value is unused, just hashing
knownJobRuns := []models.ProwJobRun{}
ids := make([]uint, 0)
dbc.DB.Select("id").Find(&knownJobRuns).Pluck("id", &ids)
for _, kjr := range ids {
prowJobRunCache[kjr] = true
}
return prowJobRunCache
}
func (pl *ProwLoader) Name() string {
return "prow"
}
func (pl *ProwLoader) Errors() []error {
return pl.errors
}
// PartitionManagementConfig defines partition lifecycle settings for a table
type PartitionManagementConfig struct {
TableName string // Name of the partitioned table
FuturePartitionWindow time.Duration // How far in the future to create partitions
DetachAfter int // Detach partitions older than this many days
DropDetachedAfter int // Drop detached partitions older than this many days
InitialLookbackDays int // Days to look back when initializing a new table
}
// sippy_backup currently deletes data older than 90 days
// this adds grace of 5 days before detaching and
// 10 before deleting. test_analysis_by_job_by_dates
// is not currently managed by sippy_backup
var partitionConfigs = []PartitionManagementConfig{
{
TableName: "test_analysis_by_job_by_dates",
FuturePartitionWindow: 48 * time.Hour,
DetachAfter: 95,
DropDetachedAfter: 100,
InitialLookbackDays: 15,
},
{
TableName: "prow_job_run_tests",
FuturePartitionWindow: 48 * time.Hour,
DetachAfter: 95,
DropDetachedAfter: 100,
InitialLookbackDays: 15,
},
{
TableName: "prow_job_run_test_outputs",
FuturePartitionWindow: 48 * time.Hour,
DetachAfter: 95,
DropDetachedAfter: 100,
InitialLookbackDays: 15,
},
}
func (pl *ProwLoader) updatePartitions(config PartitionManagementConfig) error {
err := pl.agePartitions(config)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error aging %s", config.TableName))
}
err = pl.preparePartitions(config)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error preparing %s", config.TableName))
}
return nil
}
func (pl *ProwLoader) Load() {
start := time.Now()
log.Infof("started loading prow jobs to DB...")
for _, config := range partitionConfigs {
err := pl.updatePartitions(config)
if err != nil {
pl.errors = append(pl.errors, err)
// if we have errors with partition management we can't be sure that we have created
// the necessary partitions to proceed with loading
// we could possibly differentiate between removing old and creating new but for now
// any failures here block any loading
return
}
}
// Update unmerged PR statuses in case any have merged
if err := pl.syncPRStatus(); err != nil {
pl.errors = append(pl.errors, errors.Wrap(err, "error in syncPRStatus"))
}
// Grab the ProwJob definitions from prow or CI bigquery. Note that these are the Kube
// ProwJob CRDs, not our sippy db model ProwJob.
var prowJobs []prow.ProwJob
// Fetch/update job data
if pl.bigQueryClient != nil {
var bqErrs []error
prowJobs, bqErrs = pl.fetchProwJobsFromOpenShiftBigQuery()
if len(bqErrs) > 0 {
pl.errors = append(pl.errors, bqErrs...)
}
} else {
jobsJSON, err := fetchJobsJSON(pl.config.Prow.URL)
if err != nil {
pl.errors = append(pl.errors, errors.Wrap(err, "error fetching job JSON data from prow"))
return
}
prowJobs, err = jobsJSONToProwJobs(jobsJSON)
if err != nil {
pl.errors = append(pl.errors, errors.Wrap(err, "error decoding job JSON data from prow"))
return
}
}
queue := make(chan *prow.ProwJob)
errsCh := make(chan error, len(prowJobs))
total := len(prowJobs)
// Producer to keep feeding the queue
go prowJobsProducer(pl.ctx, queue, prowJobs)
// Start pl.maxConcurrency consumers
var wg sync.WaitGroup
for i := 0; i < pl.maxConcurrency; i++ {
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
for job := range queue {
if err := ctx.Err(); err != nil {
errsCh <- err
log.WithError(err).Warningf("consumer exiting, got error")
break
}
if err := pl.processProwJob(ctx, job); err != nil {
errsCh <- err
log.WithError(err).Warningf("couldn't import job %s/%s, continuing", job.Spec.Job, job.Status.BuildID)
}
pl.jobsImportedCount.Add(1)
log.Infof("%d of %d job runs processed", pl.jobsImportedCount.Load(), total)
}
}(pl.ctx)
}
wg.Wait()
close(errsCh)
for err := range errsCh {
pl.errors = append(pl.errors, err)
}
// load the test analysis by job data into tables partitioned by day, letting bigquery do the
// heavy lifting for us.
err := pl.loadDailyTestAnalysisByJob(pl.ctx)
if err != nil {
pl.errors = append(pl.errors, errors.Wrap(err, "error updating daily test analysis by job"))
}
if len(pl.errors) > 0 {
log.Warningf("encountered %d errors while importing job runs", len(pl.errors))
}
log.Infof("finished importing new job runs in %+v", time.Since(start))
if pl.promPusher != nil {
prowLoaderQueriedMetricGauge.Set(float64(pl.jobsImportedCount.Load()))
pl.promPusher.Collector(prowLoaderQueriedMetricGauge)
prowLoaderProcessedMetricGauge.Set(float64(pl.jobsProcessedCount.Load()))
pl.promPusher.Collector(prowLoaderProcessedMetricGauge)
}
}
func prowJobsProducer(ctx context.Context, queue chan *prow.ProwJob, jobs []prow.ProwJob) {
defer close(queue)
for i := range jobs {
select {
case queue <- &jobs[i]:
case <-ctx.Done():
return
}
}
}
// tempBQTestAnalysisByJobForDate is a dupe type to work around date parsing issues.
type tempBQTestAnalysisByJobForDate struct {
Date civil.Date
TestID uint
Release string
TestName string `bigquery:"test_name"`
JobName string `bigquery:"job_name"`
Runs int
Passes int
Flakes int
Failures int
}
// getTestAnalysisByJobFromToDates uses the last daily report date to calculate the
// date range we should request from bigquery for import. We don't want to import
// the prior day if jobs are still running, so if we're not at least 8 hours into
// the current day (UTC), we will keep waiting to import yesterday. Once we cross
// that threshold we will import. (assume hourly imports)
// If our most recent import is yesterday, we're done for the day.
// If the lastDailySummary is empty, this implies a new database, and we'll do an initial
// bulk load.
//
// Returns a slice of day strings YYYY-MM-DD in ascending order. We'll import a day at
// a time, with a separate transaction for each. If something goes wrong we can fail and
// pick up at that date the next time.
//
// At present in prod, each day takes about 20 minutes
func getTestAnalysisByJobFromToDates(lastDailySummary, now time.Time) []string {
to := now.UTC().Add(-32 * time.Hour)
// If this is a new db, do an initial larger import:
if lastDailySummary.IsZero() {
from := to.Add(-14 * 24 * time.Hour)
return DaysBetween(from, to)
}
ldsStr := lastDailySummary.UTC().Format("2006-01-02")
if ldsStr == to.Format("2006-01-02") {
return []string{}
}
from := lastDailySummary.UTC().Add(24 * time.Hour)
return DaysBetween(from, to)
}
// DaysBetween returns a slice of strings representing each day in YYYY-MM-DD format between two dates
func DaysBetween(start, end time.Time) []string {
var days []string
// Normalize times to midnight to count full days
start = start.Truncate(24 * time.Hour)
end = end.Truncate(24 * time.Hour)
// Ensure start is before or equal to end
if end.Before(start) {
start, end = end, start
}
// Iterate from start to end date
for d := start; !d.After(end); d = d.Add(24 * time.Hour) {
days = append(days, d.Format("2006-01-02"))
}
return days
}
// agePartitions detaches and drops old partitions based on configuration
func (pl *ProwLoader) agePartitions(config PartitionManagementConfig) error {
detached, err := pl.dbc.DetachOldPartitions(config.TableName, config.DetachAfter, false)
if err != nil {
log.WithError(err).Errorf("error detaching partitions for %s", config.TableName)
return err
}
log.Infof("detached %d partitions from %s", detached, config.TableName)
dropped, err := pl.dbc.DropOldDetachedPartitions(config.TableName, config.DropDetachedAfter, false)
if err != nil {
log.WithError(err).Errorf("error dropping detached partitions for %s", config.TableName)
return err
}
log.Infof("dropped %d detached partitions from %s", dropped, config.TableName)
return nil
}
// preparePartitions creates missing partitions for future data based on configuration
func (pl *ProwLoader) preparePartitions(config PartitionManagementConfig) error {
log.Infof("preparing partitions for %s", config.TableName)
stats, err := pl.dbc.GetAttachedPartitionStats(config.TableName)
if err != nil {
log.WithError(err).Errorf("error getting partition stats for %s", config.TableName)
return err
}
fmt.Printf(" Total: %d partitions (%s)\n", stats.TotalPartitions, stats.TotalSizePretty)
// When initializing a new table, look back the configured number of days
oldestDate := time.Now().Add(-time.Duration(config.InitialLookbackDays) * 24 * time.Hour)
if stats.TotalPartitions > 0 {
var startRange, endRange string
if stats.OldestDate.Valid {
startRange = stats.OldestDate.Time.Format("2006-01-02")
oldestDate = stats.OldestDate.Time
}
if stats.NewestDate.Valid {
endRange = stats.OldestDate.Time.Format("2006-01-02")
}
fmt.Printf(" Range: %s to %s\n",
startRange,
endRange)
}
futureDate := time.Now().Add(config.FuturePartitionWindow)
created, err := pl.dbc.CreateMissingPartitions(config.TableName, oldestDate, futureDate, false)
if err != nil {
log.WithError(err).Errorf("error creating partitions for %s", config.TableName)
return err
}
log.Infof("created %d partitions for %s", created, config.TableName)
return nil
}
// loadDailyTestAnalysisByJob loads test analysis data into partitioned tables in postgres, one per
// day. The data is calculated by querying bigquery to do the heavy lifting for us. Each day is committed
// transactionally so the process is safe to interrupt and resume later. The process takes about 20 minutes
// per day at the time of writing, so an initial load for all releases can be quite time consuming.
func (pl *ProwLoader) loadDailyTestAnalysisByJob(ctx context.Context) error {
// Figure out our last imported daily summary.
var lastDailySummary time.Time
row := pl.dbc.DB.Table("test_analysis_by_job_by_dates").Select("MAX(date)").Row()
// Ignoring error, the function below handles the zero time if needed: (new db)
_ = row.Scan(&lastDailySummary)
importDates := getTestAnalysisByJobFromToDates(lastDailySummary, time.Now())
if len(importDates) == 0 {
log.Info("test analysis summary already completed today")
return nil
}
log.Infof("importing test analysis by job for dates: %v", importDates)
jobCache, err := query.LoadProwJobCache(pl.dbc)
if err != nil {
log.WithError(err).Error("error loading job cache")
return err
}
testCache, err := query.LoadTestCache(pl.dbc, []string{})
if err != nil {
log.WithError(err).Error("error loading test cache")
return err
}
for _, dateToImport := range importDates {
dLog := log.WithField("date", dateToImport)
dLog.Infof("Loading test analysis by job daily summaries")
q := pl.bigQueryClient.Query(ctx, bqlabel.ProwLoaderTestAnalysis, fmt.Sprintf(`WITH
deduped_testcases AS (
SELECT
junit.*,
ROW_NUMBER() OVER(PARTITION BY file_path, test_name, testsuite ORDER BY CASE WHEN flake_count > 0 THEN 0 WHEN success_val > 0 THEN 1 ELSE 2 END ) AS row_num,
jobs. prowjob_job_name AS variant_registry_job_name,
jobs.org,
jobs.repo,
jobs.pr_number,
jobs.pr_sha,
CASE
WHEN flake_count > 0 THEN 0
ELSE success_val
END
AS adjusted_success_val,
CASE
WHEN flake_count > 0 THEN 1
ELSE 0
END
AS adjusted_flake_count
FROM
%s.junit
INNER JOIN
%s.jobs jobs
ON
junit.prowjob_build_id = jobs.prowjob_build_id
AND DATE(jobs.prowjob_start) <= DATE(@DateToImport)
WHERE
DATE(junit.modified_time) = DATE(@DateToImport)
AND skipped = FALSE )
SELECT
test_name,
DATE(modified_time) AS date,
prowjob_name AS job_name,
branch AS release,
COUNT(*) AS runs,
SUM(adjusted_success_val) AS passes,
SUM(adjusted_flake_count) AS flakes,
FROM
deduped_testcases
WHERE
row_num = 1
AND branch IN UNNEST(@Releases)
GROUP BY
test_name,
date,
release,
prowjob_name
ORDER BY
date,
test_name,
prowjob_name
`, pl.bigQueryClient.Dataset, pl.bigQueryClient.Dataset))
q.Parameters = []bigquery.QueryParameter{
{
Name: "DateToImport",
Value: dateToImport,
},
{
Name: "Releases",
Value: pl.releases,
},
}
it, err := q.Read(ctx)
if err != nil {
dLog.WithError(err).Error("error querying test analysis from bigquery")
return err
}
insertRows := []models.TestAnalysisByJobByDate{}
for {
row := tempBQTestAnalysisByJobForDate{}
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
log.WithError(err).Error("error parsing prowjob from bigquery")
return err
}
psqlDate := pgtype.Date{}
err = psqlDate.Set(row.Date.String())
if err != nil {
return err
}
// Skip jobs and tests we don't know about in our postgres db:
test, ok := testCache[row.TestName]
if !ok {
continue
}
if _, ok := jobCache[row.JobName]; !ok {
continue
}
// we have to infer failures due to the bigquery query we leveraged:
failures := row.Runs - row.Passes - row.Flakes
// convert to a db row for postgres insertion:
psqlRow := models.TestAnalysisByJobByDate{
Date: row.Date.In(time.UTC),
TestID: test.ID,
Release: row.Release,
TestName: row.TestName,
JobName: row.JobName,
Runs: row.Runs,
Passes: row.Passes,
Flakes: row.Flakes,
Failures: failures,
}
insertRows = append(insertRows, psqlRow)
}
st := time.Now()
dLog.Infof("inserting %d rows", len(insertRows))
err = pl.dbc.DB.Transaction(func(tx *gorm.DB) error {
err = pl.dbc.DB.WithContext(ctx).CreateInBatches(insertRows, 2000).Error
if err != nil {
log.WithError(err).Error("error inserting rows")
}
return err
})
if err != nil {
return err
}
dLog.Infof("insert complete after %s", time.Since(st))
}
return nil
}
func (pl *ProwLoader) processProwJob(ctx context.Context, pj *prow.ProwJob) error {
pjLog := log.WithFields(log.Fields{
"job": pj.Spec.Job,
"buildID": pj.Status.BuildID,
})
for _, release := range pl.releases {
cfg, ok := pl.config.Releases[release]
if !ok {
log.Warningf("configuration not found for release %q", release)
continue
}
if val, ok := cfg.Jobs[pj.Spec.Job]; val && ok {
if err := pl.prowJobToJobRun(ctx, pj, release); err != nil {
err = errors.Wrapf(err, "error converting prow job to job run: %s", pj.Spec.Job)
pjLog.WithError(err).Warning("prow import error")
return err
}
return nil
}
for _, expr := range cfg.Regexp {
re, err := regexp.Compile(expr)
if err != nil {
err = errors.Wrap(err, "invalid regex in configuration")
log.WithError(err).Errorf("config regex error")
continue
}
if re.MatchString(pj.Spec.Job) {
if err := pl.prowJobToJobRun(ctx, pj, release); err != nil {
err = errors.Wrapf(err, "error converting prow job to job run: %s", pj.Spec.Job)
pjLog.WithError(err).Warning("prow import error")
return err
}
return nil
}
}
}
pjLog.Debugf("no match for release in sippy configuration, skipping")
return nil
}
func (pl *ProwLoader) syncPRStatus() error {
if pl.githubClient == nil {
log.Infof("No GitHub client, skipping PR sync")
return nil
}
pulls := make([]models.ProwPullRequest, 0)
if res := pl.dbc.DB.
Table("prow_pull_requests").
Where("merged_at IS NULL").Scan(&pulls); res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
return errors.Wrap(res.Error, "could not fetch prow_pull_requests")
}
for _, pr := range pulls {
logger := log.WithField("org", pr.Org).
WithField("repo", pr.Repo).
WithField("number", pr.Number).
WithField("sha", pr.SHA)
// first check to see if this pr has recently closed (indicating it may have merged)
recentMergedAt, mergeCommitSha, err := pl.githubClient.IsPrRecentlyMerged(pr.Org, pr.Repo, pr.Number)
// the client should have logged the error, we want
// to see if we are rate limited or not, if so return
// otherwise keep processing
if err != nil {
if pl.githubClient.IsWithinRateLimitThreshold() {
return err
}
}
if recentMergedAt != nil {
// we have the recentMergedAt but, we don't know if it is associated with this SHA so do
// the SHA specific verification
if mergeCommitSha != nil && *mergeCommitSha == pr.SHA {
if pr.MergedAt != recentMergedAt {
pr.MergedAt = recentMergedAt
if res := pl.dbc.DB.Save(pr); res.Error != nil {
logger.WithError(res.Error).Errorf("unexpected error updating pull request %s (%s)", pr.Link, pr.SHA)
continue
}
}
}
// if we see that any sha has merged for this pr then we should clear out any risk analysis pending comment records
// if we don't get them here we will catch them before writing the risk analysis comment
// but, we should clean up here if possible
pendingComments, err := pl.ghCommenter.QueryPRPendingComments(pr.Org, pr.Repo, pr.Number, models.CommentTypeRiskAnalysis)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logger.WithError(err).Error("Unable to fetch pending comments ")
}
for _, pc := range pendingComments {
pcp := pc
pl.ghCommenter.ClearPendingRecord(pcp.Org, pcp.Repo, pcp.PullNumber, pcp.SHA, models.CommentTypeRiskAnalysis, &pcp)
}
}
}
return nil
}
func fetchJobsJSON(prowURL string) ([]byte, error) {
resp, err := http.Get(prowURL) // #nosec G107
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func jobsJSONToProwJobs(jobJSON []byte) ([]prow.ProwJob, error) {
results := make(map[string][]prow.ProwJob)
if err := json.Unmarshal(jobJSON, &results); err != nil {
return nil, err
}
return results["items"], nil
}
func (pl *ProwLoader) generateTestGridURL(release, jobName string) *url.URL {
if releaseConfig, ok := pl.config.Releases[release]; ok {
dashboard := "redhat-openshift-ocp-release-" + release
blockingJobs := sets.NewString(releaseConfig.BlockingJobs...)
informingJobs := sets.NewString(releaseConfig.InformingJobs...)
jobType := ""
if blockingJobs.Has(jobName) {
jobType = "blocking"
} else if informingJobs.Has(jobName) {
jobType = "informing"
}
if len(jobType) != 0 {
dashboard = dashboard + "-" + jobType
return util.URLForJob(dashboard, jobName)
}
}
return &url.URL{}
}
func GetClusterDataBytes(ctx context.Context, bkt *storage.BucketHandle, path string, matches []string) ([]byte, error) {
// get the variant cluster data for this job run
gcsJobRun := gcs.NewGCSJobRun(bkt, path)
// return empty struct to pass along
match := findMostRecentDateTimeMatch(matches)
if match == "" {
return []byte{}, nil
}
bytes, err := gcsJobRun.GetContent(ctx, match)
if err != nil {
log.WithError(err).Errorf("failed to read cluster-data bytes for: %s", match)
return []byte{}, err
} else if bytes == nil {
log.Warnf("empty cluster-data bytes found for: %s", match)
return []byte{}, nil
}
return bytes, nil
}
func ParseVariantDataFile(bytes []byte) (map[string]string, error) {
rawJSONMap := make(map[string]interface{})
err := json.Unmarshal(bytes, &rawJSONMap)
if err != nil {
log.WithError(err).Errorf("failed to unmarshal prow cluster data")
return map[string]string{}, err
}
// Convert the raw json map to string->string, discarding anything that doesn't parse to a string.
clusterData := map[string]string{}
for k, v := range rawJSONMap {
if sv, ok := v.(string); ok {
clusterData[k] = sv
}
}
return clusterData, nil
}
func findMostRecentDateTimeMatch(names []string) string {
if len(names) < 1 {
return ""
}
if len(names) == 1 {
return names[0]
}
// get the times stamps and compare
currMatchDateTime := extractDateTimeName(names[0])
for _, m := range names[1:] {
nextMatchDateTime := extractDateTimeName(m)
if currMatchDateTime == nil {
currMatchDateTime = nextMatchDateTime
continue
}
if nextMatchDateTime != nil {
mostRecentMatchDateTime := mostRecentDateTimeName(*currMatchDateTime, *nextMatchDateTime)
currMatchDateTime = &mostRecentMatchDateTime
}
}
if currMatchDateTime == nil {
return ""
}
return currMatchDateTime.Name
}
func extractDateTimeName(name string) *DateTimeName {
if !clusterDataDateTimeName.MatchString(name) {
log.Errorf("Name did not match date time format: %s", name)
return nil
}
dateTimeName := &DateTimeName{Name: name}
subMatches := clusterDataDateTimeName.FindStringSubmatch(name)
subNames := clusterDataDateTimeName.SubexpNames()
for i, sName := range subNames {
switch sName {
case "DATE":
dateTimeName.Date = subMatches[i]
case "TIME":
dateTimeName.Time = subMatches[i]
}
}
if len(dateTimeName.Date) > 0 && len(dateTimeName.Time) > 0 {
return dateTimeName
}
return nil
}
func mostRecentDateTimeName(one, two DateTimeName) DateTimeName {
oneDate, err := strconv.ParseInt(one.Date, 10, 64)
if err != nil {
log.WithError(err).Errorf("Error parsing date for %s", one.Name)
}
twoDate, err := strconv.ParseInt(two.Date, 10, 64)
if err != nil {
log.WithError(err).Errorf("Error parsing date for %s", two.Name)
}
if oneDate > twoDate {
return one
}
if twoDate > oneDate {
return two
}
// they are the same so compare the times
oneTime, err := strconv.ParseInt(one.Time, 10, 64)
if err != nil {
log.WithError(err).Errorf("Error parsing time for %s", one.Name)
}
twoTime, err := strconv.ParseInt(two.Time, 10, 64)
if err != nil {
log.WithError(err).Errorf("Error parsing time for %s", two.Name)
}
if oneTime > twoTime {
return one
}
return two
}
func (pl *ProwLoader) prowJobToJobRun(ctx context.Context, pj *prow.ProwJob, release string) error {
pjLog := log.WithFields(log.Fields{
"job": pj.Spec.Job,
"buildID": pj.Status.BuildID,
"start": pj.Status.StartTime,
})
if pj.Status.State == prow.PendingState || pj.Status.State == prow.TriggeredState {
pjLog.Infof("skipping, job not in a terminal state yet")
return nil
}
id, err := strconv.ParseUint(pj.Status.BuildID, 0, 64)
if err != nil {
pjLog.Warningf("skipping, couldn't parse build ID: %+v", err)
return nil
}
pjLog.Infof("starting processing")
// find all files here then pass to getClusterData
// and prowJobRunTestsFromGCS
// add more regexes if we require more
// results from scanning for file names
path, err := GetGCSPathForProwJobURL(pjLog, pj.Status.URL)
if err != nil {
pjLog.WithError(err).WithField("prowJobURL", pj.Status.URL).Error("error getting GCS path for prow job URL")
return err
}
bkt := pl.gcsClient.Bucket(pj.Spec.DecorationConfig.GCSConfiguration.Bucket)
gcsJobRun := gcs.NewGCSJobRun(bkt, path)
allMatches, err := gcsJobRun.FindAllMatches([]*regexp.Regexp{gcs.GetDefaultJunitFile()})
if err != nil {
return errors.Wrap(err, "error finding junit file")
}
var junitMatches []string
if len(allMatches) > 0 {
junitMatches = allMatches[0]
}
// Lock the whole prow job block to avoid trying to create the pj multiple times concurrently\
// (resulting in a DB error)
pl.prowJobCacheLock.Lock()
dbProwJob, err := pl.createOrUpdateProwJob(ctx, pj, release, pjLog)
pl.prowJobCacheLock.Unlock()
if err != nil {
return err
}
pl.prowJobRunCacheLock.RLock()
_, ok := pl.prowJobRunCache[uint(id)]
pl.prowJobRunCacheLock.RUnlock()
if ok {
pjLog.Infof("processing complete; job run was already processed")
return nil
}
pjLog.Info("processing GCS bucket")
if err := pl.processGCSBucketJobRun(ctx, pj, id, path, junitMatches, dbProwJob); err != nil {
return err
}
pl.jobsProcessedCount.Add(1)
pjLog.Infof("processing complete")
return nil
}
func (pl *ProwLoader) createOrUpdateProwJob(ctx context.Context, pj *prow.ProwJob, release string, pjLog *log.Entry) (*models.ProwJob, error) {
dbProwJob, foundProwJob := pl.prowJobCache[pj.Spec.Job]
if !foundProwJob {
pjLog.Info("creating new ProwJob")
dbProwJob = &models.ProwJob{
Name: pj.Spec.Job,
Kind: models.ProwKind(pj.Spec.Type),
Release: release,
Variants: pl.variantManager.IdentifyVariants(pj.Spec.Job),
TestGridURL: pl.generateTestGridURL(release, pj.Spec.Job).String(),
}
err := pl.dbc.DB.WithContext(ctx).Clauses(clause.OnConflict{UpdateAll: true}).Create(dbProwJob).Error
if err != nil {
return nil, errors.Wrapf(err, "error loading prow job into db: %s", pj.Spec.Job)
}
pl.prowJobCache[pj.Spec.Job] = dbProwJob
} else {
saveDB := false
newVariants := pl.variantManager.IdentifyVariants(pj.Spec.Job)
if !reflect.DeepEqual(newVariants, []string(dbProwJob.Variants)) || dbProwJob.Kind != models.ProwKind(pj.Spec.Type) {
dbProwJob.Kind = models.ProwKind(pj.Spec.Type)
dbProwJob.Variants = newVariants
saveDB = true
}
if len(dbProwJob.TestGridURL) == 0 {
dbProwJob.TestGridURL = pl.generateTestGridURL(release, pj.Spec.Job).String()
if len(dbProwJob.TestGridURL) > 0 {
saveDB = true
}
}
if saveDB {
if res := pl.dbc.DB.WithContext(ctx).Save(&dbProwJob); res.Error != nil {
return nil, res.Error
}
}
}
return dbProwJob, nil
}