Skip to content

Do not log TABLE_IS_READ_ONLY at error level in ServerAsynchronousMetrics - #104981

Merged
hanfei1991 merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104912-async-metrics-readonly-noise
May 19, 2026
Merged

hanfei1991 merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104912-async-metrics-readonly-noise

Conversation

@groeneai

@groeneai groeneai commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

Description

ServerAsynchronousMetrics::updateImpl collects replica delay metrics from every ReplicatedMergeTree table on a background thread. It first reads is_readonly via getStatus(...) and, only if the table is not readonly, calls getReplicaDelays(...) to compute the actual delay. getReplicaDelays calls assertNotReadonly internally, which throws TABLE_IS_READ_ONLY when the table is in readonly mode (replica path or static storage).

Between the if (!status.is_readonly) check and the getReplicaDelays(...) call, the table can transition into readonly state — for example because of Keeper churn, replica restart, shutdown, or rapid CREATE/DROP cycles in stress tests like 01671_ddl_hang_timeout_long. When that race hits, tryLogCurrentException writes a <Error> line. With the test default --send_logs_level=warning, the line is forwarded to the client's stderr, which clickhouse-test interprets as having stderror and fails any test that happened to be running in parallel — even when the parallel test has nothing to do with the readonly table.

This is the reported failure mode of #104912, where 04204_mergetree_arena_routes_writes was flagged as flaky because 01671_ddl_hang_timeout_long running in parallel created/dropped ReplicatedMergeTree replicas while the async metrics thread was mid-update.

The race is benign for a background metrics collector. The fix inspects the current exception code and demotes only TABLE_IS_READ_ONLY to LogsLevel::debug. Every other exception keeps its LogsLevel::error level, so genuine problems are still surfaced.

Why not a regression test: the race window is microseconds wide and the failure rate in CI is ~1 occurrence per 30 days even with the stress test 01671 running constantly. A deterministic local reproducer would require explicit failpoints between getStatus and getReplicaDelays, which would be more invasive than the fix itself. The change is purely log-level: no functional behavior is altered.

Reported failure example (from #104912):

<Error> virtual void DB::ServerAsynchronousMetrics::updateImpl(...): Cannot get replica delay for table: test_12qwc89a.t1: Code: 242. DB::Exception: Table is in readonly mode (replica path: /clickhouse/tables/01671_ddl_hang_timeout_long_test_12qwc89a/test_01671/replicas/r_1). (TABLE_IS_READ_ONLY)

Closes #104912

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

Stop logging TABLE_IS_READ_ONLY at error level from the background async metrics thread when a replicated table races into readonly state during getReplicaDelays. The condition is expected during shutdown / replica restart / Keeper churn and previously leaked into client stderr, causing unrelated parallel tests to fail with having stderror.

Documentation entry for user-facing changes

  • Documentation is unchanged

Version info

  • Merged into: 26.5.1.830

…rics

The async metrics thread reads `is_readonly` from `getStatus(...)` and then
calls `getReplicaDelays(...)`, which calls `assertNotReadonly` internally.
Between those two operations the table can transition into readonly state
(e.g. due to Keeper churn, replica restart, or shutdown). When that race
hits, `getReplicaDelays` throws `TABLE_IS_READ_ONLY` and the previously
unconditional `tryLogCurrentException` writes an `<Error>` line. With the
client default `--send_logs_level=warning`, that line gets sent to the
client's stderr and `clickhouse-test` marks any parallel test as `having
stderror`.

The race is benign for a background metrics collector. Inspect the current
exception code and log only `TABLE_IS_READ_ONLY` at debug level, keeping
all other exceptions at error.

Source of the parallel stderr-leak observed in CI is a churning RMT test
such as `01671_ddl_hang_timeout_long`, which rapidly creates and drops
ReplicatedMergeTree replicas and routinely flips `is_readonly` while the
async metrics thread is mid-update. The leaked stderr previously caused
unrelated parallel tests to fail with `having stderror` (e.g.
`04204_mergetree_arena_routes_writes`, reported in ClickHouse#104912).

Closes ClickHouse#104912
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano @azat — could you review this? It demotes the TABLE_IS_READ_ONLY exception from ServerAsynchronousMetrics::getReplicaDelays to debug level when the async metrics thread races with a replica becoming readonly between the is_readonly check and the actual call, so a benign background race no longer leaks an <Error> line into client stderr and fails unrelated parallel tests (issue #104912).

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

Self-review per TASK.md Phase 4 Step 9 / fix-ci-failure SKILL.md.

# Question Answer
a Deterministic repro? No — the race window between getStatus(...) setting status.is_readonly = false and the in-call assertNotReadonly is microseconds wide. CI sees ~1 occurrence per 30 days even with 01671_ddl_hang_timeout_long stress-testing the RMT readonly transition in parallel. Producing a deterministic local reproducer would require explicit failpoints between the two operations — more invasive than the fix. The mechanism is fully understood.
b Root cause explained? Yes. ServerAsynchronousMetrics::updateImpl calls getStatus(status, false) which snapshots is_readonly into the local status struct, then checks if (!status.is_readonly), then calls getReplicaDelays(...) which internally calls assertNotReadonly. Between the snapshot and the call, ReplicatedMergeTreeRestartingThread::setReadonly (driven by Keeper churn, replica restart, or shutdown) can flip the live is_readonly flag from false to true. The call then throws TABLE_IS_READ_ONLY, which the catch-all logs at the default LogsLevel::error. With the test default --send_logs_level=warning (>=warning), the line is sent to the client's stderr; clickhouse-test then marks the parallel test as having stderror.
c Fix matches root cause? Yes. The TABLE_IS_READ_ONLY race is benign for a background metrics collector — the outer if already excludes the deterministic-readonly case. The fix uses getCurrentExceptionCode() to differentiate: TABLE_IS_READ_ONLY → LogsLevel::debug (stays in server log, not client stderr); any other exception keeps LogsLevel::error. Genuine problems are still surfaced.
d Test intent preserved? / New tests added? No tests modified or deleted. 01671_ddl_hang_timeout_long continues to exercise the RMT churn that triggers the race; 04204_mergetree_arena_routes_writes continues to assert what it actually tests (arena allocations). No new regression test was added because the race is non-deterministic — adding one would itself be flaky. The fix is a localized log-level change with a clear causal chain.
e Demonstrated in both directions? Mechanism direction only. <Error> log with send_logs_level=warning → forwarded to client stderr → having stderror failure. <Debug> log with the same setting → stays in server log, never reaches the client. Reading the code at tests/clickhouse-test lines 2880-2966 and tests/clickhouse-test line 6217 confirms this. The build succeeds and a smoke run (server start + RMT create + async metrics scrape) shows no regression in the happy path.
f Fix is general, not narrow patch? Yes. The fix targets the exception code, not a specific call site. Any TABLE_IS_READ_ONLY thrown out of getReplicaDelays is captured — including the static-storage case in assertNotStaticStorage (which also throws TABLE_IS_READ_ONLY). The same pattern of getStatus→if (!is_readonly)→getReplicaDelays exists only at this one site in ServerAsynchronousMetrics; grep confirms no sibling code paths with the same bug. The fix does not mask a deeper upstream defect — the readonly transition is the legitimate operational state, the fix only adjusts how loudly we report a benign race in a background thread.

@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c7a3a43]

Summary: ✅


AI Review

Summary

This PR narrows logging severity in ServerAsynchronousMetrics::updateImpl so TABLE_IS_READ_ONLY thrown by getReplicaDelays is logged at debug instead of error, while keeping other exceptions at error. The change matches the intended CI-noise mitigation without altering functional behavior, and I did not find unresolved correctness, safety, or compatibility issues in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 14, 2026
@hanfei1991 hanfei1991 self-assigned this May 15, 2026
@hanfei1991 hanfei1991 closed this May 15, 2026
@hanfei1991 hanfei1991 reopened this May 15, 2026
@hanfei1991 hanfei1991 added the can be tested Allows running workflows for external contributors label May 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 14.29% (1/7) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@hanfei1991
hanfei1991 enabled auto-merge May 19, 2026 14:03
@hanfei1991
hanfei1991 added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit 99e6161 May 19, 2026
165 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 5, 2026
ServerAsynchronousMetrics::updateImpl reads `status.is_readonly` and then calls
`getReplicaDelays`, whose first statement is `assertNotReadonly()`. A concurrent
DROP/RESTART flips `is_readonly` in between (ReplicatedMergeTreeRestartingThread::
setReadonly), so the call throws TABLE_IS_READ_ONLY before doing any work. The
Exception constructor increments the error counter, so the code lands in
system.errors and system.error_log even though no user action caused it and the
background thread simply skips the table.

PR ClickHouse#104981 identified the same race but only demoted the log level from error to
debug, leaving the system.errors accounting in place. This completes that change
by wrapping the speculative call in Exception::SuppressErrorCodesScope, which
exists for exactly this shape.

The scope suppresses every code constructed on the thread, and getReplicaDelays
also does Keeper I/O after the readonly check (getZooKeeper, getChildren,
tryGet), whose Coordination::Exception is a DB::Exception. Those failures are
real and must stay visible, so the handler re-records every code except
TABLE_IS_READ_ONLY via recordToSystemErrors(). Log-level selection from ClickHouse#104981
is unchanged.

The other getReplicaDelays caller, ReplicasStatusHandler, has the same window and
is deliberately left alone: it serves an explicit /replicas_status request, so its
errors belong in system.errors.

Verified with a temporary probe widening the window, over 12 forced flips per
binary: the TABLE_IS_READ_ONLY counter moves by 12 on the pre-fix binary and
stays 0 on the fixed one, while the debug line naming this call site appears 12
times on both, so the race still happens and is still reported. With Keeper
blocked, KEEPER_EXCEPTION rises by 8 on both binaries.
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.

Flaky test: 04204_mergetree_arena_routes_writes

3 participants