|
| 1 | +#!/usr/bin/env bash |
| 2 | +# race-shard.sh - run one shard of the race-detector suite. |
| 3 | +# |
| 4 | +# Race detection was one job of nineteen minutes, and seventeen of them were |
| 5 | +# one package: internal/connector is 1031s of a 1061s critical path. Sharding |
| 6 | +# by package therefore buys nothing — whichever shard gets that package still |
| 7 | +# takes seventeen minutes — so the unit here is the test, not the package. |
| 8 | +# |
| 9 | +# The hazard that comes with that is the one this repository keeps finding: |
| 10 | +# a shard scheme that silently stops running some tests is green and measures |
| 11 | +# nothing. Two checks answer it. This script refuses to report success unless |
| 12 | +# every test it was assigned actually ran, and race-shard-union.sh, in the |
| 13 | +# aggregate job, refuses unless the shards between them covered the lot. |
| 14 | +# |
| 15 | +# race-shard.sh <index> <total> <output directory> |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +index="${1:?shard index, 1-based}" |
| 19 | +total="${2:?number of shards}" |
| 20 | +out="${3:?directory to write the shard record into}" |
| 21 | +mkdir -p "$out" |
| 22 | + |
| 23 | +all="$out/all.txt" |
| 24 | +plan="$out/shard.$index.txt" |
| 25 | + |
| 26 | +# -json, not a bare -list: a plain listing prints test names with no package, |
| 27 | +# and six names in this repository exist in more than one. The union check |
| 28 | +# downstream compares package-and-test pairs, so the attribution has to be |
| 29 | +# unambiguous here. |
| 30 | +go test -tags dev -list '.*' -json ./... | |
| 31 | + python3 -c ' |
| 32 | +import sys, json |
| 33 | +for line in sys.stdin: |
| 34 | + try: |
| 35 | + event = json.loads(line) |
| 36 | + except ValueError: |
| 37 | + continue |
| 38 | + if event.get("Action") != "output": |
| 39 | + continue |
| 40 | + name = event.get("Output", "").strip() |
| 41 | + # Benchmarks are listed but -run does not run them without -bench, so |
| 42 | + # assigning one would be assigning work no shard can do. The race job |
| 43 | + # has never run them; it does not start now. |
| 44 | + if name and " " not in name and name.startswith(("Test", "Fuzz", "Example")): |
| 45 | + print(event["Package"] + "\t" + name) |
| 46 | +' | LC_ALL=C sort -u > "$all" |
| 47 | + |
| 48 | +[ -s "$all" ] || { echo "enumerated no tests at all" >&2; exit 1; } |
| 49 | +awk -v i="$index" -v n="$total" 'NR % n == i % n' "$all" > "$plan" |
| 50 | +[ -s "$plan" ] || { echo "shard $index of $total was assigned no tests" >&2; exit 1; } |
| 51 | + |
| 52 | +echo "shard $index of $total: $(wc -l < "$plan") of $(wc -l < "$all") tests" |
| 53 | + |
| 54 | +# One invocation over ./... keeps go test's own package parallelism. A name |
| 55 | +# that exists in two packages runs in both, which costs eight extra tests |
| 56 | +# across the repository and keeps the command line to a single regex. |
| 57 | +regex="^($(cut -f2 "$plan" | LC_ALL=C sort -u | paste -sd'|'))$" |
| 58 | + |
| 59 | +set +e |
| 60 | +go test -tags dev -race -timeout 20m -json -run "$regex" ./... | tee "$out/events.$index.json" | |
| 61 | + python3 -c ' |
| 62 | +import sys, json |
| 63 | +for line in sys.stdin: |
| 64 | + try: |
| 65 | + event = json.loads(line) |
| 66 | + except ValueError: |
| 67 | + sys.stdout.write(line) |
| 68 | + continue |
| 69 | + if event.get("Action") == "output": |
| 70 | + sys.stdout.write(event.get("Output", "")) |
| 71 | +' |
| 72 | +status=${PIPESTATUS[0]} |
| 73 | +set -e |
| 74 | + |
| 75 | +# A -run regex that matches nothing exits 0 with "no tests to run". That is |
| 76 | +# exactly the shape of failure this scheme has to rule out, so what ran is |
| 77 | +# compared against what was assigned. |
| 78 | +python3 - "$plan" "$out/events.$index.json" <<'PY' |
| 79 | +import json, sys |
| 80 | +
|
| 81 | +plan, events = sys.argv[1], sys.argv[2] |
| 82 | +
|
| 83 | +assigned = set() |
| 84 | +with open(plan) as handle: |
| 85 | + for line in handle: |
| 86 | + package, _, name = line.rstrip("\n").partition("\t") |
| 87 | + if name: |
| 88 | + assigned.add((package, name)) |
| 89 | +
|
| 90 | +ran = set() |
| 91 | +with open(events) as handle: |
| 92 | + for line in handle: |
| 93 | + try: |
| 94 | + event = json.loads(line) |
| 95 | + except ValueError: |
| 96 | + continue |
| 97 | + name = event.get("Test", "") |
| 98 | + if event.get("Action") == "run" and name and "/" not in name: |
| 99 | + ran.add((event["Package"], name)) |
| 100 | +
|
| 101 | +missing = assigned - ran |
| 102 | +if missing: |
| 103 | + print(f"SHARD RAN {len(assigned) - len(missing)} OF ITS {len(assigned)} TESTS", file=sys.stderr) |
| 104 | + for package, name in sorted(missing)[:20]: |
| 105 | + print(f" did not run: {package}\t{name}", file=sys.stderr) |
| 106 | + sys.exit(1) |
| 107 | +
|
| 108 | +print(f"shard ran all {len(assigned)} tests it was assigned") |
| 109 | +PY |
| 110 | + |
| 111 | +exit "$status" |
0 commit comments