Skip to content

Fix flaky 01396_inactive_replica_cleanup_nodes_zookeeper - #107062

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-01396-inactive-replica-cleanup-empty-read
Jun 22, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-01396-inactive-replica-cleanup-empty-read

Conversation

@groeneai

@groeneai groeneai commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

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):

...

Description

Fixes flaky 01396_inactive_replica_cleanup_nodes_zookeeper (issue #106297).

The cleanup-wait loop reads numChildren with stderr suppressed and compares with [[ $count -lt $((SCALE / 4)) ]]. On a transient empty read $count is empty, and [[ "" -lt N ]] is true in bash, so the loop broke immediately before the ZooKeeper log znode was trimmed. The following assertion then printed 0 instead of 1.

This matches every failing run in CIDB: only the first output line flips 1 -> 0; the is_lost lines are unchanged because cleanup completes in the gap before they run. It also explains the low rate (the empty-read window is only the brief interval before cleanup fires, which is wider on loaded arm_binary runners).

Fix: require count to be a non-empty integer before the comparison, so a transient empty read keeps polling instead of breaking early. The assertion and threshold are unchanged, so the test still fails if cleanup never happens.

Verified locally: with the old logic a forced empty read on the first iteration reproduces the failure (7/10); with the guard it passes (15/15).

Version info

  • Merged into: 26.6.1.1131

The cleanup-wait loop read numChildren with stderr suppressed (2>/dev/null)
and compared it with [[ $count -lt $((SCALE / 4)) ]]. On a transient empty
read the variable is empty, and in bash [[ "" -lt N ]] evaluates to true, so
the loop broke immediately, before the ZooKeeper log had been trimmed. The
assertion that follows then printed 0 instead of 1, producing the observed
"result differs with reference" failure (only the first output line flips
1 -> 0; the is_lost lines are unchanged because cleanup completes in the gap
before they run). This matched every failing run in CIDB and explains the low
rate: the empty-read window is only the brief interval before cleanup fires,
which is wider on loaded arm_binary runners.

Require count to be a non-empty integer before the comparison so a transient
empty read keeps polling instead of breaking early. The assertion and its
threshold are unchanged, so the test still fails if cleanup never happens.

Verified locally: with the old logic a forced empty read on the first
iteration reproduces the failure (7/10); with the guard it passes (15/15).

Closes: ClickHouse#106297

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. Forcing a transient empty read on the first poll iteration with the OLD loop logic reproduces "result differs" on demand (assertion prints 0). Demonstrated against a live server with embedded Keeper.
b Root cause explained? The poll loop reads numChildren with 2>/dev/null; on a transient empty read $count is empty, and bash [[ "" -lt N ]] evaluates true, so the loop breaks immediately before the ZK log znode is trimmed. The next assertion then prints 0 instead of 1. Matches every CIDB diff: only line 1 flips 1->0, is_lost lines unchanged (cleanup completes in the gap before they run).
c Fix matches root cause? Yes. The fix requires count to be a non-empty integer ([[ $count =~ ^[0-9]+$ ]]) before the -lt comparison, so an empty read keeps polling instead of breaking early. It targets the false-break mechanism directly, not the symptom. No assertion bounds widened, no no-random-* tag added, no data reduced.
d Test intent preserved / new tests added? Preserved exactly. The assertion SELECT numChildren < SCALE/4 and its threshold are unchanged, so the test still fails if cleanup genuinely never happens. This is a test-logic fix (shell race), not a server-code bug, so no new test is added.
e Both directions demonstrated? Yes. With the OLD logic + forced empty read on iter 1: 7/10 FAIL. With the FIXED guard + identical condition: 15/15 PASS.
f Fix is general, not a narrow patch? Yes. Grep confirmed this is the only stateless test using the system.zookeeper ... 2>/dev/null poll idiom, so the fix scope is complete for this bug class here.

Session id: cron:clickhouse-worker-slot-21:20260610-184200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar — could you review? Test-only fix for the flaky test you flagged in #106297: the cleanup-wait poll loop broke early on a transient empty read because [[ "" -lt N ]] is true in bash. The guard requires the count to be a number before comparing; the assertion and threshold are unchanged.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jun 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [684a339]

Summary: ✅


AI Review

Summary
  • This PR fixes the flaky 01396_inactive_replica_cleanup_nodes_zookeeper stateless test by making the cleanup polling loop ignore empty/non-numeric numChildren reads instead of treating them as 0. The change is narrow, the PR metadata matches a CI-only fix, and I did not find a correctness issue worth an inline review comment.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 10, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI fully finished (Finish Workflow passed) on d74fb2c. The single failure is Stateless tests (arm_asan_ubsan, azure, sequential) -- the known azure-infra-outage / db-disk "Server died" chronic class, unrelated to this PR (which fixes the flaky 01396_inactive_replica_cleanup_nodes_zookeeper itself). Not PR-caused. No Fast/Bugfix/Style failure. Ready for review.

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, Azure infra has been fixed, please update the branch.

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, do you know why the query can return an empty result? This sounds strange.

@alexey-milovidov alexey-milovidov self-assigned this Jun 22, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov Branch updated: merged current master and pushed, CI is re-running.

On the empty result, you are right to be suspicious. It is not a legitimately empty row.

The poll query is SELECT numChildren FROM system.zookeeper WHERE path = '.../{shard}' AND name = 'log'. StorageSystemZooKeeper serves it by tryGetChildren on the table root (an Exact path), then a tryGet on each child for its stat, and the name = 'log' predicate keeps the log row. The only ways it produces zero rows without an error are if the table root or the /log node returns ZNONODE (the continue branches in the storage). But both are permanent structural nodes of the ReplicatedMergeTree that live for the whole lifetime of the table (DROP only happens at the very end of the test). So a successful query here always returns exactly one row with a numeric numChildren.

So an empty stdout is a suppressed error, not an empty row. The poll uses 2>/dev/null, so a transient client/Keeper failure during the read (a session reconnect under the heavy single-row INSERT load, or the tryGetChildren/tryGet retryLoop exhausting its retries) prints nothing to stdout and the error text is swallowed. $count is then empty, and [[ "" -lt 50 ]] is true in bash, so the loop broke early before cleanup trimmed the log, and the post-loop assertion printed 0 instead of 1.

The fix keeps the loop polling until it gets a real reading ([[ $count =~ ^[0-9]+$ ]] before the comparison). A transient error mid-poll is exactly what should be retried, not read as "cleanup done". I kept 2>/dev/null so the poll does not spam transient errors; the final assertion after the loop does not suppress stderr, so a persistent failure would still surface there. I can drop the suppression instead if you would rather see the errors.

@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.50% 92.50% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 490 line(s), 50 function(s) across 133 file(s) · Details

Top files
  • src/Databases/DataLake/RestCatalog.cpp: 109 line(s), 8 function(s)
  • src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp: 23 line(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesLinearRegression.h: 14 line(s), 32 function(s)
  • src/Databases/DataLake/DatabaseDataLake.cpp: 10 line(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/MetadataGenerator.cpp: 10 line(s)

Full report

@alexey-milovidov

alexey-milovidov commented Jun 22, 2026 •

Copy link
Copy Markdown
Member

@groeneai

Let's do it in two steps.

  1. I merge this branch to stabilize the test.
  2. You submit another PR that hardens the test to expose more potential errors.

@alexey-milovidov
alexey-milovidov merged commit 9c9f0af into ClickHouse:master Jun 22, 2026
166 of 167 checks passed
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov Sounds good, thanks for merging step 1.

For step 2 I'll open a follow-up PR that hardens 01396 to surface the poll errors instead of swallowing them. One constraint shapes the approach: clickhouse-test fails a .sh test on any non-empty stderr, so simply removing the 2>/dev/null would make the test fail on a transient-but-recovered Keeper read during the wait loop (the very blips that caused the original flake), trading one flake for another.

So instead I'll capture the in-loop poll errors and surface them (plus the last numChildren reading) only when the wait never converges. A healthy run stays byte-identical and quiet; a genuinely broken run now fails with the real Keeper/client error visible instead of a silent wrong count. I'll link the PR here.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Step 2 follow-up: #108213 (hardens 01396 to surface the poll errors instead of swallowing them).

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 22, 2026
pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Jul 29, 2026
…rrors

Follow-up to ClickHouse#107062, which made the cleanup-wait loop require numChildren
to be a non-empty integer before comparing (a transient empty read had let
[[ "" -lt N ]] break the loop early). That stabilized the test but still
discarded the underlying read error via 2>/dev/null.

The numChildren poll is served by StorageSystemZooKeeper: tryGetChildren on
the table root, tryGet on each child for its stat, then the name='log'
predicate. The only errorless zero-row paths are root-ZNONODE or /log-ZNONODE,
but both are permanent structural ReplicatedMergeTree nodes for the whole test
(DROP only at the end). So a successful query always returns exactly one
numeric row, and an empty count is a suppressed transient error (a Keeper/client
hiccup under the heavy single-row INSERT load), not an empty result.

Capture the poll's stderr per iteration and, if the loop never converges,
print the last error and last count to stderr so the real failure is
diagnosable. clickhouse-test fails a .sh test on any non-empty stderr, so a
healthy run stays byte-identical and silent; only a genuinely stuck wait now
fails with the underlying error exposed instead of a silent wrong count.
The temp file uses CLICKHOUSE_TEST_UNIQUE_NAME so the test stays parallel-safe.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors 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.

4 participants