Repository navigation
ci: fix module scope re-entry with --dist=each in flaky/targeted check - #100627
Conversation
…dist=each With --dist=each, pytest-xdist sends all tests to all workers within a single session. When multiple test modules are present, the scheduler can interleave tests from different modules on the same worker, causing module-scoped fixtures to be torn down and re-set-up multiple times. Some tests (e.g. test_userspace_page_cache) call cluster.add_instance() inside the fixture rather than at module level. On the second fixture setup cycle, the instance already exists in cluster.instances, causing: Exception: Can't add instance 'node1': there is already an instance with the same name in [dict_keys(['node1'])] Fix: when add_instance() is called and the name already exists (and the cluster is not currently up), log a warning and return the existing instance instead of raising. This makes the call idempotent for fixture re-entry, matching the expected behaviour when --dist=each triggers multiple setup/teardown cycles. Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
… with --dist=each" This reverts commit 49a19c7.
…re-entry
With --dist=each, pytest-xdist sends all test items to every worker via
send_runtest_all(). Items are sent in CLI argument (collection) order, and
pytest finalizes module-scoped fixtures when the module changes between
consecutive items.
The targeted/flaky check builds the test list from CIDB query results which
are ordered by failure time, not by module file. This can produce an
interleaved order like:
test_userspace_page_cache/test.py::test_basics
test_backup_restore_on_cluster/test_concurrency.py::test_create_or_drop[Atomic]
test_userspace_page_cache/test.py::test_size_adjustment
On each worker, pytest sees a module change after test_basics (next is
backup_restore), finalizes the module scope (cluster.shutdown()), then
encounters test_size_adjustment — re-entering the module scope. This calls
cluster.add_instance("node1") again while cluster.instances already contains
"node1", raising:
Exception: Can't add instance 'node1': there is already an instance with
the same name in [dict_keys(['node1'])]
Fix: sort parallel_test_modules and sequential_test_modules by file path
(the part before "::") before passing them to pytest. This groups all tests
from the same file together, so module-scoped fixtures are set up and torn
down exactly once per module per worker session.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [725fed0] Summary: ✅ AI ReviewSummaryThis PR fixes flaky/targeted integration scheduling by grouping tests by module when using ClickHouse Rules
Final Verdict
|
| @@ -1,3 +1,4 @@ | |||
| # ci: trigger flaky check | |||
There was a problem hiding this comment.
This inserts a comment before the shebang, so #!/usr/bin/env python3 is no longer on the first line. That breaks direct script execution semantics (the kernel won’t honor the interpreter directive when the file is executed as a program).
Please either remove this temporary trigger comment from the PR, or keep the shebang as line 1 (move any comment below it).
ZooKeeper unavailability during async insert causes this transient error when the insert status is unknown after a ZooKeeper connection loss. It is not a bug in the query being tested. Example: Code: 319. DB::Exception: We tried to commit part, but ZooKeeper is unavailable, insert status is unknown, client must retry: While executing WaitForAsyncInsert. (UNKNOWN_STATUS_OF_INSERT)
This reverts commit aff1b2d.
| # returns them sorted by failure time), pytest finalizes and re-enters | ||
| # module-scoped fixtures between them, breaking tests that call | ||
| # cluster.add_instance() inside the fixture. | ||
| parallel_test_modules = sorted(parallel_test_modules, key=lambda t: t.split("::")[0]) |
There was a problem hiding this comment.
This sort now runs for every integration job, not only the flaky/targeted --dist=each mode described in the PR.\n\nget_optimal_test_batch already orders parallel modules by historical duration to reduce tail latency, but re-sorting by file path here discards that ordering for normal --dist=loadfile jobs and can increase CI wall time.\n\nPlease gate this sort behind the same condition as parallel_dist = --dist=each (or sort only in flaky/targeted checks), so regular integration batching keeps duration-aware ordering.\n
The sort by module file path is needed to prevent module-scope fixture re-entry with `--dist=each`, which is only used in flaky and targeted checks. For regular integration jobs, preserve the duration-aware ordering produced by `get_optimal_test_batch` to avoid increasing CI wall time.
After merging #100181 (which introduced
--dist=eachfor flaky/targeted integration test checks), the targeted check started failing with:Root cause
The targeted/flaky check builds the test list from CIDB query results sorted by failure time — not by module file. This produces an interleaved order like:
EachScheduling.send_runtest_all()sends all items to each worker in CLI argument (collection) order. When pytest processes items in this order, it finalizes thescope="module"fixture fortest_userspace_page_cacheaftertest_basics(because the next item is from a different module), then re-enters it fortest_size_adjustment. Tests that callcluster.add_instance()inside the fixture — rather than at module level — fail on the second setup becausecluster.instancesstill contains the instance from the first cycle.Fix
Sort
parallel_test_modulesandsequential_test_modulesby file path (before::) before passing them to pytest. This groups all tests from the same file consecutively, soscope="module"fixtures are entered and finalized exactly once per module per worker session.Also reduces
sequential_repeat_cntfloor from 4 to 3 to match the parallel workers floor.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Version info
26.4.1.241