OLD | NEW |
1 // Utility that contains methods for both CT master and worker scripts. | 1 // Utility that contains methods for both CT master and worker scripts. |
2 package util | 2 package util |
3 | 3 |
4 import ( | 4 import ( |
5 "bufio" | 5 "bufio" |
6 "encoding/json" | 6 "encoding/json" |
7 "fmt" | 7 "fmt" |
8 "io" | 8 "io" |
9 "io/ioutil" | 9 "io/ioutil" |
10 "os" | 10 "os" |
11 "path" | 11 "path" |
12 "path/filepath" | 12 "path/filepath" |
13 "runtime" | 13 "runtime" |
14 "strconv" | 14 "strconv" |
15 "sync" | 15 "sync" |
16 "time" | 16 "time" |
17 | 17 |
18 "go.skia.org/infra/go/exec" | 18 "go.skia.org/infra/go/exec" |
| 19 "go.skia.org/infra/go/swarming" |
19 "go.skia.org/infra/go/util" | 20 "go.skia.org/infra/go/util" |
20 | 21 |
21 "github.com/skia-dev/glog" | 22 "github.com/skia-dev/glog" |
22 ) | 23 ) |
23 | 24 |
24 const ( | 25 const ( |
25 MAX_SYNC_TRIES = 3 | 26 MAX_SYNC_TRIES = 3 |
26 | 27 |
27 TS_FORMAT = "20060102150405" | 28 TS_FORMAT = "20060102150405" |
28 | 29 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 })) | 384 })) |
384 } | 385 } |
385 }(i) | 386 }(i) |
386 } | 387 } |
387 | 388 |
388 // Wait for all spawned goroutines to complete. | 389 // Wait for all spawned goroutines to complete. |
389 wg.Wait() | 390 wg.Wait() |
390 | 391 |
391 return nil | 392 return nil |
392 } | 393 } |
| 394 |
| 395 // GetStartRange returns the range worker should start processing at based on it
s num and how many |
| 396 // artifacts it is allowed to process. |
| 397 func GetStartRange(workerNum, artifactsPerWorker int) int { |
| 398 return ((workerNum - 1) * artifactsPerWorker) + 1 |
| 399 } |
| 400 |
| 401 func TriggerSwarmingTask(pagesetType, taskPrefix, isolateName string, hardTimeou
t, ioTimeout time.Duration, maxPagesPerBot int, isolateExtraArgs map[string]stri
ng) error { |
| 402 // Instantiate the swarming client. |
| 403 workDir, err := ioutil.TempDir("", "swarming_work_") |
| 404 if err != nil { |
| 405 return fmt.Errorf("Could not get temp dir: %s", err) |
| 406 } |
| 407 s, err := swarming.NewSwarmingClient(workDir) |
| 408 if err != nil { |
| 409 return fmt.Errorf("Could not instantiate swarming client: %s", e
rr) |
| 410 } |
| 411 defer s.Cleanup() |
| 412 // Create isolated.gen.json files from tasks. |
| 413 genJSONs := []string{} |
| 414 // Get path to isolate files. |
| 415 _, currentFile, _, _ := runtime.Caller(0) |
| 416 pathToIsolates := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(c
urrentFile))), "isolates") |
| 417 for i := 1; i <= PagesetTypeToInfo[pagesetType].NumPages/maxPagesPerBot;
i++ { |
| 418 isolateArgs := map[string]string{ |
| 419 "START_RANGE": strconv.Itoa(GetStartRange(i, maxPagesPe
rBot)), |
| 420 "NUM": strconv.Itoa(maxPagesPerBot), |
| 421 "PAGESET_TYPE": pagesetType, |
| 422 } |
| 423 // Add isolateExtraArgs (if specified) into the isolateArgs. |
| 424 for k, v := range isolateExtraArgs { |
| 425 isolateArgs[k] = v |
| 426 } |
| 427 taskName := fmt.Sprintf("%s_%d", taskPrefix, i) |
| 428 genJSON, err := s.CreateIsolatedGenJSON(path.Join(pathToIsolates
, isolateName), s.WorkDir, "linux", taskName, isolateArgs, []string{}) |
| 429 if err != nil { |
| 430 return fmt.Errorf("Could not create isolated.gen.json fo
r task %s: %s", taskName, err) |
| 431 } |
| 432 genJSONs = append(genJSONs, genJSON) |
| 433 } |
| 434 |
| 435 // Batcharchive the tasks. |
| 436 tasksToHashes, err := s.BatchArchiveTargets(genJSONs, BATCHARCHIVE_TIMEO
UT) |
| 437 if err != nil { |
| 438 return fmt.Errorf("Could not batch archive targets: %s", err) |
| 439 } |
| 440 if len(genJSONs) != len(tasksToHashes) { |
| 441 return fmt.Errorf("len(genJSONs) was %d and len(tasksToHashes) w
as %d", len(genJSONs), len(tasksToHashes)) |
| 442 } |
| 443 // Trigger swarming using the isolate hashes. |
| 444 dimensions := map[string]string{"pool": SWARMING_POOL} |
| 445 tasks, err := s.TriggerSwarmingTasks(tasksToHashes, dimensions, swarming
.RECOMMENDED_PRIORITY, swarming.RECOMMENDED_EXPIRATION, hardTimeout, ioTimeout,
false) |
| 446 if err != nil { |
| 447 return fmt.Errorf("Could not trigger swarming task: %s", err) |
| 448 } |
| 449 // Collect all tasks and log the ones that fail. |
| 450 for _, task := range tasks { |
| 451 if _, _, err := task.Collect(s); err != nil { |
| 452 glog.Errorf("task %s failed: %s", task.Title, err) |
| 453 continue |
| 454 } |
| 455 } |
| 456 return nil |
| 457 } |
| 458 |
| 459 // GetPathToPyFiles returns the location of CT's python scripts. |
| 460 func GetPathToPyFiles(runOnSwarming bool) string { |
| 461 if runOnSwarming { |
| 462 return filepath.Join(filepath.Dir(filepath.Dir(os.Args[0])), "sr
c", "go.skia.org", "infra", "ct", "py") |
| 463 } else { |
| 464 _, currentFile, _, _ := runtime.Caller(0) |
| 465 return filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(curr
entFile))), "py") |
| 466 } |
| 467 } |
OLD | NEW |