-
Notifications
You must be signed in to change notification settings - Fork 568
/
main.go
300 lines (286 loc) · 8.59 KB
/
main.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
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"github.com/jackc/pgconn"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/jmoiron/sqlx"
"github.com/pachyderm/pachyderm/v2/src/internal/dbutil"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
gotestresults "github.com/pachyderm/pachyderm/v2/src/testing/cmds/go-test-results"
)
const (
jobInfoFileName string = "JobInfo.json"
uploadGoroutineLimit int = 10
uploadChunkSize int = 50
testResultFieldCount int = 9
)
var startTime time.Time
type TestResultLine struct {
Time time.Time `json:"Time"`
Action string `json:"Action"`
TestName string `json:"Test"`
Package string `json:"Package"`
Elapsed float32 `json:"Elapsed"`
Output string `json:"Output"`
}
type ResultRow struct {
Id string `db:"id"`
WorkflowId string `db:"workflowid"`
JobId string `db:"jobid"`
JobExecutor int `db:"jobexecutor"`
TestName string `db:"testname"`
Package string `db:"package"`
Action string `db:"action"`
ElapsedSeconds float32 `db:"elapsedseconds"`
Output string `db:"output"`
}
// This is built and runs in a pachyderm pipeline to egress data uploaded to pachyderm to postgresql.
func main() {
startTime = time.Now()
log.InitPachctlLogger()
ctx := pctx.Background("")
err := run(ctx)
if err != nil {
log.Exit(ctx, "Error during metric collection", zap.Error(err))
}
}
func run(ctx context.Context) error {
log.Info(ctx, "Running DB Migrate")
out, err := exec.Command("tern", "migrate").CombinedOutput()
if err != nil {
return errors.Wrapf(err, "running migrates. %v", string(out))
}
log.Info(ctx, "Migrate successful, beginning egress transform of data to sql DB")
inputFolder := os.Args[1]
db, err := dbutil.NewDB(
dbutil.WithHostPort(gotestresults.PostgresqlHost, 5432),
dbutil.WithDBName("ci_metrics"),
dbutil.WithUserPassword(gotestresults.PostgresqlUser, gotestresults.PostgresqlPassword),
)
if err != nil {
return errors.Wrapf(err, "opening db connection")
}
defer db.Close()
// filpathWalkDir does not evaluate the PFS symlink
sym, err := filepath.EvalSymlinks(inputFolder)
if err != nil {
return errors.Wrapf(err, "evaluating symlinks")
}
jobInfoPaths := make(map[string]gotestresults.JobInfo)
var testResultPaths []string
err = filepath.WalkDir(sym, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if d.Name() == jobInfoFileName {
jobInfo, err := readJobInfo(path)
if err != nil {
return err
}
return insertJobInfo(ctx, jobInfo, jobInfoPaths, path, d, db)
} else {
// we need the job info inserted first due to foreign key constraints,
// so save results to loop over later ensuring ordered inserts
testResultPaths = append(testResultPaths, path)
}
return nil
})
logPerf(ctx, "job info done")
if err != nil {
return errors.Wrapf(err, "walking file tree for job info ")
}
if len(jobInfoPaths) == 0 {
log.Info(ctx, "No new job info found, exiting without inserting test results.")
return nil
}
for _, path := range testResultPaths {
if err = insertTestResultFile(ctx, path, jobInfoPaths, db); err != nil {
return errors.Wrapf(err, "inserting test results into DB ")
}
}
logPerf(ctx, "test results done")
return nil
}
func readJobInfo(path string) (*gotestresults.JobInfo, error) {
jobInfoFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "opening job info json file")
}
defer jobInfoFile.Close()
jobInfoJson, err := io.ReadAll(jobInfoFile)
if err != nil {
return nil, errors.Wrapf(err, "reading job info data")
}
var jobInfo gotestresults.JobInfo
if err = json.Unmarshal(jobInfoJson, &jobInfo); err != nil {
return nil, errors.Wrapf(err, "unmarshalling job info json")
}
return &jobInfo, nil
}
func insertJobInfo(
ctx context.Context,
jobInfo *gotestresults.JobInfo,
jobInfoPaths map[string]gotestresults.JobInfo,
path string,
d fs.DirEntry,
db *sqlx.DB,
) error {
_, err := db.NamedExec(`INSERT INTO public.ci_jobs (
workflow_id,
job_id,
job_executor,
job_name,
job_timestamp,
job_num_executors,
commit_sha,
branch,
tag,
pull_requests,
username
) VALUES (
:workflowid,
:jobid,
:jobexecutor,
:jobname,
:jobtimestamp,
:jobnumexecutors,
:commit,
:branch,
:tag,
:pullrequests,
:username)`,
jobInfo,
)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
// Unique key constraint violation. In other words, the record already exists.
// Ignore insert to since we don't want to change the timestamp, but log it.
log.Info(ctx, "Job info already inserted, Skipping insert for workflow/job",
zap.String("Workflow id", jobInfo.WorkflowId),
zap.String("Job id", jobInfo.JobId),
zap.Int("Job Executor", jobInfo.JobExecutor),
)
return nil
}
return errors.Wrapf(err, "inserting job info for workflow %v job %v executor %v",
jobInfo.WorkflowId,
jobInfo.JobId,
jobInfo.JobExecutor,
)
}
jobInfoPaths[strings.TrimSuffix(path, d.Name())] = *jobInfo // for use when inserting individual test results, don't insert results if errored
log.Info(ctx, "Inserted job info for workflow and job",
zap.String("Workflow id", jobInfo.WorkflowId),
zap.String("Job id", jobInfo.JobId),
zap.Int("Job Executor", jobInfo.JobExecutor),
)
return nil
}
func insertTestResultFile(ctx context.Context, path string, jobInfoPaths map[string]gotestresults.JobInfo, db *sqlx.DB) error {
resultsFile, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "opening results file ")
}
defer resultsFile.Close()
// get job info previously inserted in order to link foreign keys to jobs table
fileName := filepath.Base(path)
jobInfo, ok := jobInfoPaths[strings.TrimSuffix(path, fileName)]
if !ok {
return errors.WithStack(fmt.Errorf(
"Failed to find job info for %v - file name %v - job infos %v ",
path,
filepath.Base(path),
jobInfoPaths,
))
}
query := `INSERT INTO public.test_results (
id,
workflow_id,
job_id,
job_executor,
test_name,
package,
"action",
elapsed_seconds,
"output"
) VALUES (:id, :workflowid, :jobid, :jobexecutor, :testname, :package, :action, :elapsedseconds, :output)`
paramVals := &[]ResultRow{}
resultsScanner := bufio.NewScanner(resultsFile)
egUpload, ctx := errgroup.WithContext(ctx)
chunkIdx := 0
egUpload.SetLimit(uploadGoroutineLimit)
for resultsScanner.Scan() {
resultJson := resultsScanner.Bytes()
var result TestResultLine
err = json.Unmarshal(resultJson, &result)
if err != nil {
log.Info(ctx, "Failed unmarshalling file as a test result. Continuing to parse results file.",
zap.String("path", path),
zap.Error(err),
)
continue
}
if result.Action == "output" {
continue // output is mostly logs and coverage and it's not that useful, so we don't store output-only actions
}
*paramVals = append(*paramVals, ResultRow{
Id: uuid.New().String(),
WorkflowId: jobInfo.WorkflowId,
JobId: jobInfo.JobId,
JobExecutor: jobInfo.JobExecutor,
TestName: result.TestName,
Package: result.Package,
Action: result.Action,
ElapsedSeconds: result.Elapsed,
Output: result.Output,
})
chunkIdx++
if chunkIdx >= uploadChunkSize {
chunkIdx = 0
flushResults(ctx, db, egUpload, query, paramVals)
paramVals = &[]ResultRow{}
}
}
if chunkIdx > 0 { // we have a partial chunk that needs to upload
flushResults(ctx, db, egUpload, query, paramVals)
}
err = egUpload.Wait()
if err != nil {
return errors.EnsureStack(err)
}
if err = resultsScanner.Err(); err != nil {
return errors.EnsureStack(err)
}
return nil
}
func flushResults(ctx context.Context, db *sqlx.DB, egUpload *errgroup.Group, query string, paramVals *[]ResultRow) {
threadParamVals := paramVals
egUpload.Go(func() error { // upload in goroutine so scanner can continue
if _, err := db.NamedExec(query, *threadParamVals); err != nil {
log.Info(ctx, "Failed updating SQL DB - continuing to parse results file.", zap.Error(err))
}
return nil
})
}
func logPerf(ctx context.Context, msg string) {
log.Info(ctx, fmt.Sprintf("Performance: %s", msg), zap.Duration("time since start", time.Since(startTime)))
}