Skip to content

Commit 0e08660

Browse files
jorgemanrubiaclaudeJorge Manrubia
authored
Race detection is nineteen minutes, and seventeen of them are one package (#777)
* Shard race detection by test, and check that the shards cover the suite Race Detection is nineteen minutes and runs on every push, and seventeen of those minutes are one package: internal/connector is 1031s of a 1061s critical path. That is why sharding by package group, which the card proposed, buys about 4% — whichever shard gets that package still takes seventeen minutes. The unit has to be the test. Four shards. Each enumerates the suite with `go test -list -json`, which gives package-and-test pairs; a bare -list prints names with no package and six names here exist in more than one. 4988 pairs across 41 packages, sorted, sliced by index, and run in one `go test -race -run` over ./... so Go's own package parallelism survives inside the shard. The six duplicated names cost eight extra test runs in total. The hazard that comes with sharding is the one this repository keeps finding: a split that silently stops running some tests is green and measures nothing. Two checks answer it, and neither is a warning. Each shard compares what actually ran, from the run events, against what it was assigned, and fails on any shortfall. A -run regex that matches nothing exits 0 saying "no tests to run", which is exactly the shape this has to rule out. That check earned itself immediately: the first real shard ran 1247 of its 1251 tests, and the four missing were benchmarks — -list enumerates them and -run does not run them without -bench, so they were being assigned to a shard that could not run them. They are out of the enumeration now. The aggregate then fails unless every shard passed, all four enumerations are byte-identical, the slices are pairwise disjoint, and their union is the whole list. It is the `Race Detection` job itself, so the required status check keeps its name and the ruleset is untouched however many shards there are; `if: always()` so a failed shard cannot skip it, because a skipped required check is not a failed one. The union check has its own tests, in make check, because a check that cannot fail is worse than no check. Eight cases: one complete disjoint split accepted, and seven refused — a shard assigned nothing, a shard with no record, the same test in two shards, a test in no shard, shards that enumerated different sets, a test that does not exist, and an empty enumeration. No wall-clock claim here yet. The point of this commit is that the split is provably complete; what it is worth is the next thing to measure, on CI, before the shard count is touched. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * The aggregate could not read the reports it judges Two from review, and the first is this change's own defect aimed back at it. download-artifact extracts each artifact to <path>/<name>/, and the uploads were named race-shard-N while the union check reads shard-N. So the aggregate would have found no reports at all — on every run. It would have failed rather than passed, which is the one mercy: the check refuses a shard whose enumeration it cannot read. But a checker that can never see its inputs is not a checker, and "shard 1 recorded no enumeration" would have sent whoever met it looking in the wrong place. The artifact is named for the directory the aggregate reads, and the message now allows for a report that did not arrive. Proved rather than reasoned about: the union check's tests gained the two cases that would have caught this. A shard whose report never arrived is refused, and so is a report that arrived under a name the checker does not read — which is exactly the defect, kept as a test so it cannot come back. Ten cases now, one accepted and nine refused. And md5sum is not on stock macOS, which would have made check-race-shards fail before testing anything for anyone not on Linux — a local check that only runs on Linux quietly stops being run. The enumerations are compared as sorted files with cmp instead, which is POSIX and needs no hash at all. Nothing GNU-only is left in the script. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * The union check refused every correct split on macOS The review flagged seq as unavailable on macOS. That part is wrong: macOS ships /usr/bin/seq, and make check already runs seq twice in test-e2e long before it reaches this script. But looking at the loop for that reason found a worse bug behind it. BSD seq reads "first larger than last" as a request to count down. GNU seq prints nothing for `seq 5 4`; BSD seq prints "5" and "4". The overlap loop ends on exactly that range — at i == total it asks for seq $((total+1)) total — so on macOS the last iteration ran with j == total+1 and j == total, read a shard directory that does not exist, and compared the last shard against itself. Every correct split was refused, with the message "shards 4 and 4 were both assigned 1 test(s)". So the fix the review asked for is right, for a reason it did not give. All three ranges now count in the shell. Proved it with a shim implementing BSD's documented rule: with seq the new case fails exactly as macOS would, naming shards 4 and 4; counting in the shell, it passes. The shim is the test, so a reintroduced seq cannot pass. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Jorge Manrubia <jorge.agent.3@hey.com>
1 parent f91189c commit 0e08660

5 files changed

Lines changed: 345 additions & 9 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,20 @@ jobs:
126126
go install golang.org/x/vuln/cmd/govulncheck@latest
127127
govulncheck -tags dev ./...
128128
129-
race-check:
130-
name: Race Detection
129+
# The shards, and then Race Detection over them. The required status check
130+
# is the aggregate, so its name never changes and the ruleset never has to
131+
# be edited when the shard count does.
132+
race-shard:
133+
name: Race Detection (shard ${{ matrix.shard }} of 4)
131134
runs-on: ubuntu-latest
132135
permissions:
133136
contents: read
137+
strategy:
138+
# Every shard runs: stopping the others on the first failure would
139+
# leave the aggregate unable to tell a real gap from a cancelled job.
140+
fail-fast: false
141+
matrix:
142+
shard: [1, 2, 3, 4]
134143
env:
135144
BASECAMP_NO_KEYRING: "1"
136145
steps:
@@ -143,12 +152,67 @@ jobs:
143152
with:
144153
go-version-file: 'go.mod'
145154

146-
- name: Run tests with race detector
147-
# 20 minutes, not Go's default 10 per package. internal/connector runs
148-
# real sockets, real processes and the recovery harness; under -race it
149-
# takes about five minutes on a fast box and roughly twice that on a
150-
# runner. The default was not a hung test, it was the budget.
151-
run: go test -tags dev -race -v -timeout 20m ./...
155+
# 20 minutes, not Go's default 10 per package. internal/connector runs
156+
# real sockets, real processes and the recovery harness; under -race it
157+
# takes about five minutes on a fast box and roughly twice that on a
158+
# runner. The default was not a hung test, it was the budget.
159+
- name: Run this shard under the race detector
160+
run: scripts/race-shard.sh "${{ matrix.shard }}" 4 "shard-records/shard-${{ matrix.shard }}"
161+
162+
# Uploaded even when the shard failed: the aggregate has to be able to
163+
# tell "this shard ran its tests and one of them failed" from "this
164+
# shard never got as far as knowing what it was meant to run".
165+
- name: Record what this shard was assigned
166+
if: always()
167+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
168+
with:
169+
# The name is the directory the aggregate reads: download-artifact
170+
# extracts each one to <path>/<name>/, so this has to be the
171+
# shard-N the union check looks for, not a name of its own.
172+
name: shard-${{ matrix.shard }}
173+
path: |
174+
shard-records/shard-${{ matrix.shard }}/all.txt
175+
shard-records/shard-${{ matrix.shard }}/shard.${{ matrix.shard }}.txt
176+
retention-days: 3
177+
if-no-files-found: error
178+
179+
race-check:
180+
name: Race Detection
181+
runs-on: ubuntu-latest
182+
needs: [race-shard]
183+
# always(), or a failed shard would skip this job — and a skipped required
184+
# check is not a failed one.
185+
if: always()
186+
permissions:
187+
contents: read
188+
steps:
189+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
190+
with:
191+
persist-credentials: false
192+
193+
- name: Download what each shard recorded
194+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
195+
with:
196+
pattern: shard-*
197+
path: shard-records
198+
199+
# Naming what came back before judging it: a check that runs over
200+
# nothing is the failure this whole arrangement exists to prevent.
201+
- name: Show the records
202+
run: find shard-records -type f | sort
203+
204+
- name: Every shard passed
205+
env:
206+
SHARDS: ${{ needs.race-shard.result }}
207+
run: |
208+
echo "shards: $SHARDS"
209+
[ "$SHARDS" = success ] || {
210+
echo "a race-detection shard did not pass" >&2
211+
exit 1
212+
}
213+
214+
- name: The shards between them ran every test, once
215+
run: scripts/race-shard-union.sh shard-records 4
152216

153217
integration:
154218
name: Integration Tests

‎Makefile‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,13 @@ replace-check:
407407
fi
408408
@echo "Replace check passed (no local replace directives)"
409409

410+
# The race job is sharded, and the union check is what makes that safe: it
411+
# refuses unless the shards between them ran every test, once. A check that
412+
# cannot fail would be worse than no check, so it has its own tests.
413+
.PHONY: check-race-shards
414+
check-race-shards:
415+
@scripts/race-shard-union-test.sh
416+
410417
# Verify every leaf command is accounted for in smoke tests
411418
.PHONY: check-smoke-coverage
412419
check-smoke-coverage: build
@@ -429,7 +436,7 @@ check-eval-patterns:
429436

430437
# Run all checks (local CI gate)
431438
.PHONY: check
432-
check: fmt-check vet lint lint-actions test test-e2e test-sync-skills check-naming check-surface check-skill-drift test-skill-drift check-bare-groups check-lint-lockstep check-smoke-coverage check-eval-patterns provenance-check tidy-check
439+
check: fmt-check vet lint lint-actions test test-e2e test-sync-skills check-naming check-surface check-skill-drift test-skill-drift check-bare-groups check-lint-lockstep check-smoke-coverage check-eval-patterns check-race-shards provenance-check tidy-check
433440

434441
# Lint GitHub Actions workflows (requires actionlint + zizmor)
435442
.PHONY: lint-actions
@@ -650,6 +657,7 @@ help:
650657
@echo " coverage Run tests with coverage and open in browser"
651658
@echo " record-cassettes Record happy-path cassettes (TOKEN+TARGET+ACCOUNT+PROJECT)"
652659
@echo " smoke Run pre-release smoke suite (BASECAMP_TOKEN=...)"
660+
@echo " check-race-shards Test the race shards' union check"
653661
@echo " qa-report Show QA coverage report from smoke traces"
654662
@echo ""
655663
@echo "Performance:"

‎scripts/race-shard-union-test.sh‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
# race-shard-union-test.sh — the union check's own tests.
3+
#
4+
# The union check is the reason it is safe to shard this job at all, so it
5+
# is worth knowing it fails. Each case below is a way the shards could
6+
# silently stop covering the suite; all of them must be refused.
7+
set -euo pipefail
8+
cd "$(dirname "$0")/.."
9+
union=scripts/race-shard-union.sh
10+
11+
work=$(mktemp -d)
12+
trap 'rm -rf "$work"' EXIT
13+
14+
plant() {
15+
rm -rf "${work:?}"/*
16+
for i in 1 2 3 4; do
17+
mkdir -p "$work/shard-$i"
18+
printf 'pkg/a\tTestOne\npkg/a\tTestTwo\npkg/b\tTestThree\npkg/b\tTestFour\n' > "$work/shard-$i/all.txt"
19+
done
20+
printf 'pkg/a\tTestOne\n' > "$work/shard-1/shard.1.txt"
21+
printf 'pkg/a\tTestTwo\n' > "$work/shard-2/shard.2.txt"
22+
printf 'pkg/b\tTestThree\n' > "$work/shard-3/shard.3.txt"
23+
printf 'pkg/b\tTestFour\n' > "$work/shard-4/shard.4.txt"
24+
}
25+
26+
refuses() {
27+
local what="$1"
28+
if "$union" "$work" 4 >/dev/null 2>&1; then
29+
echo "FAIL: the union check accepted $what" >&2
30+
exit 1
31+
fi
32+
echo "ok - refuses $what"
33+
}
34+
35+
plant
36+
"$union" "$work" 4 >/dev/null || { echo "FAIL: the union check refused a complete, disjoint split" >&2; exit 1; }
37+
echo "ok - accepts a complete, disjoint split"
38+
39+
plant; : > "$work/shard-3/shard.3.txt"
40+
refuses "a shard assigned nothing"
41+
42+
plant; rm "$work/shard-2/shard.2.txt"
43+
refuses "a shard that recorded no assignment"
44+
45+
plant; printf 'pkg/a\tTestOne\n' >> "$work/shard-3/shard.3.txt"
46+
refuses "the same test assigned to two shards"
47+
48+
plant; printf 'pkg/b\tTestFive\n' >> "$work/shard-1/all.txt"
49+
refuses "a test in the enumeration and in no shard"
50+
51+
plant; printf 'pkg/b\tTestFive\n' >> "$work/shard-2/all.txt"
52+
refuses "shards that enumerated different sets of tests"
53+
54+
plant; printf 'pkg/z\tTestGhost\n' >> "$work/shard-4/shard.4.txt"
55+
refuses "a test assigned that the repository does not have"
56+
57+
plant; : > "$work/shard-1/all.txt"
58+
refuses "a shard whose enumeration is empty"
59+
60+
# The aggregate reads the shards through uploaded artifacts, so a report
61+
# that never arrives looks exactly like a shard with nothing to say. It
62+
# must not: an aggregator that cannot read a shard's report would have an
63+
# opinion about it anyway.
64+
plant; rm -rf "$work/shard-2"
65+
refuses "a shard whose report never arrived"
66+
67+
plant; mv "$work/shard-3" "$work/race-shard-3"
68+
refuses "a report that arrived under a name the checker does not read"
69+
70+
# The check must not depend on how seq reads a backwards range. macOS ships
71+
# seq, but BSD seq treats "first larger than last" as counting down, and the
72+
# overlap loop ends on exactly that range. With seq, this case compared the
73+
# last shard against itself and refused every correct split on macOS.
74+
plant
75+
bsd=$work/bsdbin
76+
mkdir -p "$bsd"
77+
cat > "$bsd/seq" <<'SEQ'
78+
#!/usr/bin/env bash
79+
# BSD seq(1): "If first is larger than last the default incr is -1."
80+
first=$1; last=$2
81+
if [ "$first" -gt "$last" ]; then
82+
for ((n = first; n >= last; n--)); do echo "$n"; done
83+
else
84+
for ((n = first; n <= last; n++)); do echo "$n"; done
85+
fi
86+
SEQ
87+
chmod +x "$bsd/seq"
88+
PATH="$bsd:$PATH" "$union" "$work" 4 >/dev/null || {
89+
echo "FAIL: the union check refused a complete, disjoint split where seq counts down over a backwards range, as BSD seq does on macOS" >&2
90+
exit 1
91+
}
92+
echo "ok - accepts a complete, disjoint split under BSD seq semantics"
93+
94+
echo "All union checks behaved."

‎scripts/race-shard-union.sh‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
# race-shard-union.sh - the aggregate job's check that the shards between
3+
# them ran every test, once.
4+
#
5+
# This is the point of sharding the race job rather than a nicety attached to
6+
# it. A split that silently stops running some tests reports a fast green and
7+
# measures nothing, which is the defect this repository has spent a lot of
8+
# effort closing elsewhere. So every one of these is a failure, not a warning:
9+
#
10+
# - a shard that enumerated a different set of tests than its siblings,
11+
# which means they did not all see the same repository
12+
# - the same test assigned to two shards, which means the split is wrong
13+
# even where it looks complete
14+
# - a test in no shard at all, which is the one that matters
15+
#
16+
# race-shard-union.sh <directory of shard records> <number of shards>
17+
set -euo pipefail
18+
19+
dir="${1:?directory holding the shard records}"
20+
total="${2:?number of shards}"
21+
22+
fail() { echo "SHARD COVERAGE FAILED: $*" >&2; exit 1; }
23+
24+
sorted() { LC_ALL=C sort -u "$1"; }
25+
26+
# Compared as sorted files rather than hashed: cmp is POSIX and md5sum is
27+
# not on macOS, and this check is in make check, which people run locally.
28+
#
29+
# The loops count in the shell rather than through seq for the same reason.
30+
# macOS does ship seq, but BSD seq reads "first larger than last" as a
31+
# request to count down, so `seq 5 4` prints "5 4" where GNU seq prints
32+
# nothing. The inner loop below ends on exactly that range, so on macOS
33+
# every correct run was refused, comparing the last shard against itself.
34+
for ((i = 1; i <= total; i++)); do
35+
[ -s "$dir/shard-$i/all.txt" ] || fail "shard $i recorded no enumeration; either it did not get far enough to have one, or its report did not reach here"
36+
sorted "$dir/shard-$i/all.txt" > "$dir/all.$i.sorted"
37+
if [ "$i" != 1 ] && ! cmp -s "$dir/all.1.sorted" "$dir/all.$i.sorted"; then
38+
fail "shard $i enumerated a different set of tests than shard 1, so the shards did not all see the same repository"
39+
fi
40+
done
41+
42+
for ((i = 1; i <= total; i++)); do
43+
[ -s "$dir/shard-$i/shard.$i.txt" ] || fail "shard $i was assigned no tests"
44+
for ((j = i + 1; j <= total; j++)); do
45+
overlap=$(LC_ALL=C comm -12 <(sorted "$dir/shard-$i/shard.$i.txt") <(sorted "$dir/shard-$j/shard.$j.txt"))
46+
[ -z "$overlap" ] || fail "shards $i and $j were both assigned $(echo "$overlap" | wc -l) test(s), e.g. $(echo "$overlap" | head -1)"
47+
done
48+
done
49+
50+
cat "$dir"/shard-*/shard.*.txt | LC_ALL=C sort -u > "$dir/union.txt"
51+
cp "$dir/all.1.sorted" "$dir/expected.txt"
52+
53+
missing=$(LC_ALL=C comm -23 "$dir/expected.txt" "$dir/union.txt")
54+
[ -z "$missing" ] || fail "$(echo "$missing" | wc -l) test(s) were in no shard and so did not run, e.g. $(echo "$missing" | head -1)"
55+
56+
extra=$(LC_ALL=C comm -13 "$dir/expected.txt" "$dir/union.txt")
57+
[ -z "$extra" ] || fail "$(echo "$extra" | wc -l) test(s) were assigned that the repository does not have, e.g. $(echo "$extra" | head -1)"
58+
59+
echo "shard coverage OK: $(wc -l < "$dir/expected.txt") tests across $total shards, disjoint and complete"

‎scripts/race-shard.sh‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)