Skip to content

Commit ecbcf44

Browse files
rusackasclaude
andcommitted
ci(python): shard the unit tests across three runners
The unsharded unit-test job had grown to ~40 minutes of tests on a 2-core runner (pytest-xdist only gets two workers), so with setup, the scoped coverage gates and uploads it was hitting the 45-minute timeout on ordinary variance even when every test passed. Split the suite by test file across a 3-way matrix with a small pytest plugin (tests/pytest_shard.py): each file hashes to one shard, so module-scoped fixtures and --dist loadfile behave as before, and the shards together run every test once. The SDK tests and the scoped 100% coverage gates run on shard 1 only. Each shard uploads coverage and its own JUnit artifact; unit-tests-required already aggregates the matrix, so branch protection is unchanged. Co-Authored-By: Evan Rusackas <evan@preset.io> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3565acb commit ecbcf44

3 files changed

Lines changed: 119 additions & 3 deletions

File tree

‎.github/workflows/superset-python-unittest.yml‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,27 @@ jobs:
4343
needs: changes
4444
if: needs.changes.outputs.python == 'true'
4545
runs-on: ubuntu-26.04
46-
# Leave time for setup, the full suite, scoped coverage gates, and uploads.
46+
# Leave time for setup, one shard of the suite, the scoped coverage gates
47+
# (shard 1 only), and uploads.
4748
timeout-minutes: 45
4849
permissions:
4950
# Declaring any permission drops the defaults, and checkout and `$/` actions
5051
# need read access wherever the repository is not public.
5152
contents: read
5253
id-token: write
5354
strategy:
55+
# Let every shard finish so one failure doesn't hide the others.
56+
fail-fast: false
5457
matrix:
5558
# Just "current" here, on both push and PR — the "next" Python minor is
5659
# exercised separately by the nightly canary (python-nightly-canary.yml)
5760
# so cross-version breaks don't cost a runner on every push/PR.
5861
python-version: ["current"]
62+
# The suite is split by test file across these runners (see
63+
# tests/pytest_shard.py): on a 2-core runner pytest-xdist only gets two
64+
# workers, and the unsharded run had grown to ~40 minutes against this
65+
# job's timeout. Keep NUM_SHARDS below in step with this list.
66+
shard: [1, 2, 3]
5967
env:
6068
PYTHONPATH: ${{ github.workspace }}
6169
steps:
@@ -71,6 +79,7 @@ jobs:
7179
- name: Install HiveServer2 transport test dependencies
7280
run: uv pip install --system -e ".[hive,impala]"
7381
- name: SDK unit tests
82+
if: matrix.shard == 1
7483
env:
7584
PYTEST_DISABLE_PLUGIN_AUTOLOAD: "1"
7685
PYTHONPATH: ${{ github.workspace }}/superset-core/src
@@ -98,8 +107,10 @@ jobs:
98107
env:
99108
SUPERSET_TESTENV: true
100109
SUPERSET_SECRET_KEY: not-a-secret
110+
NUM_SHARDS: 3
111+
SHARD_ID: ${{ matrix.shard }}
101112
run: |
102-
pytest -n auto --dist loadfile --durations=0 --durations-min=0.5 --cov-report= --cov=superset ./tests/common ./tests/unit_tests --cache-clear --maxfail=50 --junit-xml=test-results/junit-unit.xml
113+
pytest -p tests.pytest_shard --num-shards "$NUM_SHARDS" --shard-id "$SHARD_ID" -n auto --dist loadfile --durations=0 --durations-min=0.5 --cov-report= --cov=superset ./tests/common ./tests/unit_tests --cache-clear --maxfail=50 --junit-xml=test-results/junit-unit.xml
103114
# COVERAGE_FILE keeps these scoped gates off the default .coverage that
104115
# the step above wrote. pytest-cov starts a fresh data file per run, so
105116
# without it the last gate replaces the full-suite data and the report
@@ -110,6 +121,7 @@ jobs:
110121
# runner whose other cores sit idle. pytest-cov combines the
111122
# per-worker data before --cov-fail-under is evaluated.
112123
- name: Python 100% coverage unit tests
124+
if: matrix.shard == 1
113125
env:
114126
SUPERSET_TESTENV: true
115127
SUPERSET_SECRET_KEY: not-a-secret
@@ -133,7 +145,7 @@ jobs:
133145
if: always()
134146
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
135147
with:
136-
name: junit-results-${{ matrix.python-version }}
148+
name: junit-results-${{ matrix.python-version }}-${{ matrix.shard }}
137149
path: test-results/
138150
retention-days: 7
139151

‎tests/pytest_shard.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""Split one pytest run into deterministic shards, one test file per shard.
18+
19+
Load with ``-p tests.pytest_shard`` and pass ``--num-shards N --shard-id K``
20+
(``K`` counts from 1). Each test file lands on exactly one shard, chosen by a
21+
stable hash of its path, so module-scoped fixtures behave as in a full run
22+
and every pytest-xdist worker computes the same selection. The shards of one
23+
commit together run each collected test exactly once.
24+
"""
25+
26+
from __future__ import annotations
27+
28+
import zlib
29+
30+
import pytest
31+
32+
33+
def pytest_addoption(parser: pytest.Parser) -> None:
34+
group = parser.getgroup("shard")
35+
group.addoption(
36+
"--num-shards",
37+
type=int,
38+
default=1,
39+
help="Number of shards the run is split into (default: 1, no split).",
40+
)
41+
group.addoption(
42+
"--shard-id",
43+
type=int,
44+
default=1,
45+
help="Which shard to run, from 1 to --num-shards.",
46+
)
47+
48+
49+
def shard_of(nodeid: str, num_shards: int) -> int:
50+
"""Return the 1-based shard for a test, keyed on its file path."""
51+
path = nodeid.split("::", 1)[0]
52+
return zlib.crc32(path.encode("utf-8")) % num_shards + 1
53+
54+
55+
def pytest_collection_modifyitems(
56+
config: pytest.Config, items: list[pytest.Item]
57+
) -> None:
58+
num_shards: int = config.getoption("num_shards")
59+
shard_id: int = config.getoption("shard_id")
60+
if num_shards < 1 or not 1 <= shard_id <= num_shards:
61+
raise pytest.UsageError(
62+
f"--shard-id must be between 1 and --num-shards ({num_shards}); "
63+
f"got {shard_id}"
64+
)
65+
if num_shards == 1:
66+
return
67+
selected = [item for item in items if shard_of(item.nodeid, num_shards) == shard_id]
68+
deselected = [
69+
item for item in items if shard_of(item.nodeid, num_shards) != shard_id
70+
]
71+
items[:] = selected
72+
config.hook.pytest_deselected(items=deselected)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from tests.pytest_shard import shard_of
18+
19+
20+
def test_shard_of_keeps_a_file_on_one_shard() -> None:
21+
"""Every test in a file lands on the same shard, so module fixtures hold."""
22+
shards = {
23+
shard_of(f"tests/unit_tests/foo_test.py::{name}", 3)
24+
for name in ("test_a", "TestBar::test_b", "test_c[param]")
25+
}
26+
assert len(shards) == 1
27+
28+
29+
def test_shard_of_covers_every_shard_within_range() -> None:
30+
"""Shard ids stay in 1..N, and a realistic set of files reaches all of them."""
31+
files = [f"tests/unit_tests/module_{i}_test.py" for i in range(200)]
32+
assert {shard_of(path, 3) for path in files} == {1, 2, 3}

0 commit comments

Comments
 (0)