-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.go
410 lines (359 loc) · 11.1 KB
/
collect.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
// Copyright 2017 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 main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"sync"
"time"
"github.com/maruel/subcommands"
"go.chromium.org/luci/auth"
"go.chromium.org/luci/common/api/swarming/swarming/v1"
"go.chromium.org/luci/common/clock"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/retry"
"go.chromium.org/luci/common/retry/transient"
"go.chromium.org/luci/common/system/signals"
)
type taskOutputOption int64
const (
taskOutputNone taskOutputOption = 0
taskOutputConsole taskOutputOption = 1 << 0
taskOutputJSON taskOutputOption = 1 << 1
taskOutputAll taskOutputOption = taskOutputConsole | taskOutputJSON
)
func (t *taskOutputOption) String() string {
switch *t {
case taskOutputJSON:
return "json"
case taskOutputConsole:
return "console"
case taskOutputAll:
return "all"
case taskOutputNone:
fallthrough
default:
return "none"
}
}
func (t *taskOutputOption) Set(s string) error {
switch s {
case "json":
*t = taskOutputJSON
case "console":
*t = taskOutputConsole
case "all":
*t = taskOutputAll
case "", "none":
*t = taskOutputNone
default:
return errors.Reason("invalid task output option: %s", s).Err()
}
return nil
}
func (t *taskOutputOption) includesJSON() bool {
return (*t & taskOutputJSON) != 0
}
func (t *taskOutputOption) includesConsole() bool {
return (*t & taskOutputConsole) != 0
}
// taskResult is a consolidation of the results of packaging up swarming
// task results from collect.
type taskResult struct {
// taskID is the ID of the swarming task for which this results were retrieved.
taskID string
// result is the raw result structure returned by a swarming RPC call.
// result may be nil if err is non-nil.
result *swarming.SwarmingRpcsTaskResult
// output is the console output produced by the swarming task.
// output will only be populated if requested.
output string
// outputs is a list of file outputs from a task, downloaded from an isolate server.
// outputs will only be populated if requested.
outputs []string
// err is set if an operational error occurred while doing RPCs to gather the
// task result, which includes errors received from the server.
err error
}
func (t *taskResult) Print(w io.Writer) {
if t.err != nil {
fmt.Fprintf(w, "%s: %v\n", t.taskID, t.err)
} else {
fmt.Fprintf(w, "%s: exit %d\n", t.taskID, t.result.ExitCode)
if t.output != "" {
fmt.Fprintln(w, t.output)
}
}
}
func cmdCollect(defaultAuthOpts auth.Options) *subcommands.Command {
return &subcommands.Command{
UsageLine: "collect <options> (-requests-json file | task_id...)",
ShortDesc: "Waits on a set of Swarming tasks",
LongDesc: "Waits on a set of Swarming tasks.",
CommandRun: func() subcommands.CommandRun {
r := &collectRun{}
r.Init(defaultAuthOpts)
return r
},
}
}
type collectRun struct {
commonFlags
timeout time.Duration
taskSummaryJSON string
taskSummaryPython bool
taskOutput taskOutputOption
outputDir string
perf bool
jsonInput string
}
func (c *collectRun) Init(defaultAuthOpts auth.Options) {
c.commonFlags.Init(defaultAuthOpts)
c.Flags.DurationVar(&c.timeout, "timeout", 0, "Timeout to wait for result. Set to 0 for no timeout.")
c.Flags.StringVar(&c.taskSummaryJSON, "task-summary-json", "", "Dump a summary of task results to a file as json.")
//TODO(tikuta): Remove this flag once crbug.com/894045 is fixed.
c.Flags.BoolVar(&c.taskSummaryPython, "task-summary-python", false, "Generate python client compatible task summary json.")
c.Flags.BoolVar(&c.perf, "perf", false, "Includes performance statistics.")
c.Flags.Var(&c.taskOutput, "task-output-stdout", "Where to output each task's console output (stderr/stdout). (none|json|console|all)")
c.Flags.StringVar(&c.outputDir, "output-dir", "", "Where to download isolated output to.")
c.Flags.StringVar(&c.jsonInput, "requests-json", "", "Load the task IDs from a .json file as saved by \"trigger -dump-json\"")
}
func (c *collectRun) Parse(args *[]string) error {
var err error
if err := c.commonFlags.Parse(); err != nil {
return err
}
// Validate timeout duration.
if c.timeout < 0 {
return errors.Reason("negative timeout is not allowed").Err()
}
// Validate arguments.
if c.jsonInput != "" {
data, err := ioutil.ReadFile(c.jsonInput)
if err != nil {
return errors.Annotate(err, "reading json input").Err()
}
input := triggerResults{}
if err := json.Unmarshal(data, &input); err != nil {
return errors.Annotate(err, "unmarshalling json input").Err()
}
// Modify args to contain all the task IDs.
for _, task := range input.Tasks {
*args = append(*args, task.TaskId)
}
}
for _, arg := range *args {
if !regexp.MustCompile("^[a-z0-9]+$").MatchString(arg) {
return errors.Reason("task ID %q must contain only [a-z0-9]", arg).Err()
}
}
if len(*args) == 0 {
return errors.Reason("must specify at least one task id, either directly or through -json").Err()
}
return err
}
func (c *collectRun) Run(a subcommands.Application, args []string, env subcommands.Env) int {
if err := c.Parse(&args); err != nil {
printError(a, err)
return 1
}
cl, err := c.defaultFlags.StartTracing()
if err != nil {
printError(a, err)
return 1
}
if err = cl.Close(); err != nil {
printError(a, err)
return 1
}
if err := c.main(a, args); err != nil {
printError(a, err)
return 1
}
return 0
}
func (c *collectRun) fetchTaskResults(ctx context.Context, taskID string, service swarmingService) taskResult {
var result *swarming.SwarmingRpcsTaskResult
var output string
var outputs []string
err := retry.Retry(ctx, transient.Only(retry.Default), func() error {
var err error
// Fetch the result details.
result, err = service.GetTaskResult(ctx, taskID, c.perf)
if err != nil {
return tagTransientGoogleAPIError(err)
}
// TODO(mknyszek): Fetch output and outputs in parallel.
// If we got the result details, try to fetch stdout if the
// user asked for it.
if c.taskOutput != taskOutputNone {
taskOutput, err := service.GetTaskOutput(ctx, taskID)
if err != nil {
return tagTransientGoogleAPIError(err)
}
output = taskOutput.Output
}
// Download the result isolated if available and if we have a place to put it.
if c.outputDir != "" {
outputs, err = service.GetTaskOutputs(ctx, taskID, c.outputDir, result.OutputsRef)
if err != nil {
return tagTransientGoogleAPIError(err)
}
}
return nil
}, func(err error, d time.Duration) {
logging.WithError(err).Warningf(ctx, "Transient error while making request, retrying in %s...", d)
})
if err != nil {
return taskResult{taskID: taskID, err: err}
}
return taskResult{
taskID: taskID,
result: result,
output: output,
outputs: outputs,
}
}
func (c *collectRun) pollForTaskResult(ctx context.Context, taskID string, service swarmingService) taskResult {
var result taskResult
startedTime := clock.Now(ctx)
for {
result = c.fetchTaskResults(ctx, taskID, service)
if result.err != nil {
// If we received an error from fetchTaskResults, it either hit a fatal
// failure, or it hit too many transient failures.
return result
}
// Only stop if the swarming bot is "dead" (i.e. not running).
state, err := parseTaskState(result.result.State)
if err != nil {
return taskResult{taskID: taskID, err: err}
}
if !state.Alive() {
return result
}
currentTime := clock.Now(ctx)
// Start with a 1 second delay and for each 30 seconds of waiting
// add another second until hitting a 15 second ceiling.
delay := time.Second + (currentTime.Sub(startedTime) / 30)
if delay >= 15*time.Second {
delay = 15 * time.Second
}
timerResult := <-clock.After(ctx, delay)
// timerResult should have an error if the context's deadline was exceeded,
// or if the context was cancelled.
if timerResult.Err != nil {
err := timerResult.Err
if result.err != nil {
result.err = errors.Annotate(result.err, "%v", timerResult.Err).Err()
} else {
result.err = err
}
return result
}
logging.Debugf(ctx, "Waiting task_id: %s", taskID)
}
}
// summarizeResultsPython generates summary JSON file compatible with python's
// swarming client.
func summarizeResultsPython(results []taskResult) ([]byte, error) {
var shards []map[string]interface{}
for _, result := range results {
buf, err := json.Marshal(result.result)
if err != nil {
return nil, err
}
var jsonResult map[string]interface{}
if err := json.Unmarshal(buf, &jsonResult); err != nil {
return nil, err
}
if jsonResult != nil {
jsonResult["output"] = result.output
}
shards = append(shards, jsonResult)
}
return json.MarshalIndent(map[string]interface{}{
"shards": shards,
}, "", " ")
}
// summarizeResults generate a marshalled JSON summary of the task results.
func (c *collectRun) summarizeResults(results []taskResult) ([]byte, error) {
if c.taskSummaryPython {
return summarizeResultsPython(results)
}
jsonResults := map[string]interface{}{}
for _, result := range results {
jsonResult := map[string]interface{}{}
if result.err != nil {
jsonResult["error"] = result.err.Error()
}
if result.result != nil {
jsonResult["results"] = *result.result
if c.taskOutput.includesJSON() {
jsonResult["output"] = result.output
}
jsonResult["outputs"] = result.outputs
}
jsonResults[result.taskID] = jsonResult
}
return json.MarshalIndent(jsonResults, "", " ")
}
func (c *collectRun) main(a subcommands.Application, taskIDs []string) error {
// Set up swarming service.
ctx, cancel := context.WithCancel(c.defaultFlags.MakeLoggingContext(os.Stderr))
signals.HandleInterrupt(cancel)
service, err := c.createSwarmingClient(ctx)
if err != nil {
return err
}
// Prepare context.
if c.timeout > 0 {
var cancel func()
ctx, cancel = clock.WithTimeout(ctx, c.timeout)
defer cancel()
}
// Aggregate results by polling and fetching across multiple goroutines.
results := make([]taskResult, len(taskIDs))
var wg sync.WaitGroup
wg.Add(len(taskIDs))
for i := range taskIDs {
go func(i int) {
defer wg.Done()
results[i] = c.pollForTaskResult(ctx, taskIDs[i], service)
}(i)
}
wg.Wait()
// Summarize and write summary json if applicable.
if c.taskSummaryJSON != "" {
jsonSummary, err := c.summarizeResults(results)
if err != nil {
return err
}
if err := ioutil.WriteFile(c.taskSummaryJSON, jsonSummary, 0664); err != nil {
return err
}
}
for _, result := range results {
if c.taskOutput.includesConsole() || result.err != nil {
result.Print(os.Stdout)
}
}
return nil
}