Skip to content

ci: fix module scope re-entry with --dist=each in flaky/targeted check - #100627

Merged
fm4v merged 8 commits into
masterfrom
nik/fix-add-instance-reentry
Mar 25, 2026
Merged

fm4v merged 8 commits into
masterfrom
nik/fix-add-instance-reentry

Conversation

@fm4v

@fm4v fm4v commented Mar 24, 2026 •

Copy link
Copy Markdown
Member

After merging #100181 (which introduced --dist=each for flaky/targeted integration test checks), the targeted check started failing with:

Exception: Can't add instance 'node1': there is already an instance with
the same name in [dict_keys(['node1'])]

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:

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

EachScheduling.send_runtest_all() sends all items to each worker in CLI argument (collection) order. When pytest processes items in this order, it finalizes the scope="module" fixture for test_userspace_page_cache after test_basics (because the next item is from a different module), then re-enters it for test_size_adjustment. Tests that call cluster.add_instance() inside the fixture — rather than at module level — fail on the second setup because cluster.instances still contains the instance from the first cycle.

Fix

Sort parallel_test_modules and sequential_test_modules by file path (before ::) before passing them to pytest. This groups all tests from the same file consecutively, so scope="module" fixtures are entered and finalized exactly once per module per worker session.

Also reduces sequential_repeat_cnt floor from 4 to 3 to match the parallel workers floor.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.241

fm4v and others added 5 commits March 24, 2026 16:55
…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]>
…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]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [725fed0]

Summary: ✅


AI Review

Summary

This PR fixes flaky/targeted integration scheduling by grouping tests by module when using --dist=each, preventing module fixture re-entry issues, and adds UNKNOWN_STATUS_OF_INSERT to stress smoke-check transient ignores. I did not find correctness, safety, performance, or rollout issues that require changes.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ➖
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ➖
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 24, 2026
@@ -1,3 +1,4 @@
# ci: trigger flaky check

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
@fm4v
fm4v enabled auto-merge March 24, 2026 21:09
@fm4v
fm4v disabled auto-merge March 24, 2026 21:09
Comment thread ci/jobs/integration_test_job.py Outdated
# 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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@fm4v
fm4v enabled auto-merge March 25, 2026 00:42
@fm4v
fm4v added this pull request to the merge queue Mar 25, 2026
Merged via the queue into master with commit 3989f3f Mar 25, 2026
149 checks passed
@fm4v
fm4v deleted the nik/fix-add-instance-reentry branch March 25, 2026 01:18
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants