Repository navigation
Fix flaky test_prometheus_before_tables: wait for the log line - #112044
nikitamikhaylov merged 2 commits into
Conversation
… reading it
test_prometheus_with_handlers_starts_after_tables_are_loaded failed with
Exception: Command [docker exec ...-node_handlers-1 bash -c
grep -n -m1 'Application: Listening for Prometheus' /var/log/clickhouse-server/clickhouse-server.log]
return non-zero code 1
grep exit 1 means the line was not in the log yet, so the assertion never ran.
Startup ordering itself is correct in both configurations and is not changed here.
The integration harness reports an instance as started as soon as a bare
sock.connect((ip, 9000)) succeeds (helpers/cluster.py:6008). But createServers
(programs/server/Server.cpp:3454, start_servers=false) already calls
socketBindListen, which ends in socket.listen(backlog)
(src/Server/socketBindListen.cpp:31), so the kernel accepts into the backlog with
no accept loop and no log line. The "Listening for {}" lines are written later, in
the sequential start loop at Server.cpp:3692, and Prometheus is pushed last
(:4162). The prometheus.handlers instance skips the early-start path (:3318, gated
on !config().has("prometheus.handlers")), so its Prometheus line is the last line
of startup. first_log_line_number read it with a one-shot grep and no wait.
On the failing run the harness wait returned at 00:51:18.720 while the line was
written at 00:51:19.011710, i.e. 291 ms later. The harness's own probe being
accepted (TCPHandlerFactory: TCP Request) is logged 10 us before that line, which
shows the socket was reachable well before the loop reached Prometheus. The two
"Application: Loaded metadata" greps in the same module had only about 5 ms of
margin on that run, so they were latently racy too; the fix is in the shared
helper and covers all four call sites.
The contains_in_log guard matters for runtime: wait_for_log_line runs
"tail -F ... | grep -Em1" in the container and grep exits on the match, but tail -F
only notices the closed pipe on the server's next log write, so an unconditional
barrier costs about 7 s per call even when the line is already present. Measured on
the whole module: 17.19 s unpatched, 35.88 s with an unconditional barrier, 16.17 s
as written here.
This cannot mask an ordering regression. The failure mode being fixed is a missing
line, not a wrong order; if the lines were ever emitted in the wrong order, both
would still be found and the assert would still fail.
Verified with a deterministic reproducer (a slow startup_scripts entry on
node_handlers, which runs between the bind and the "Listening for" loop): the
unpatched test fails every time with the signature above, the patched one passes
with the assertion really evaluated. Inverting the ordering assertion under that
reproducer fails, and disabling the new wait fails again with the original grep
error. pytest --count 10 over the module: 50 executions, 50 passed.
`.claude/CLAUDE.md` asks for literal excerpts from log messages to be wrapped in inline code blocks. The comment added to `first_log_line_number` quoted the `Listening for ...` excerpt with double quotes instead of backticks. Comment-only change: the file is byte-identical to the previous commit once comment and blank lines are stripped.
Internal second-model review (click to expand)Two review rounds by a second model, independent of the author, plus my own cold read of
Checks that came back clean:
Session id: cron:clickhouse-review-slot-48:20260727-084200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-42:20260727-043200 |
|
cc @tavplubix — could you review this? It is a test-only fix for a flaky integration test: the harness reports an instance started as soon as its TCP port accepts, but the port is bound and listening before the |
|
Workflow [PR], commit [5c6ee2d] Summary: ✅ AI ReviewSummaryThis PR fixes the flaky Missing context / blind spots
Final Verdict✅ No inline review findings. The added wait matches the reported race and does not weaken the test's ordering checks. |
Closes: #111782
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
test_prometheus_with_handlers_starts_after_tables_are_loadedfailed withgrepexit 1 means the line was not in the log yet, so the assertion never ran. Startupordering itself is correct in both configurations and is not changed here.
The integration harness reports an instance as started as soon as a bare
sock.connect((ip, 9000))succeeds (ClickHouseInstance.wait_until_port_is_readyinhelpers/cluster.py). ButcreateServers(
programs/server/Server.cpp:3454,start_servers=false) already callssocketBindListen, which ends insocket.listen(backlog)(
src/Server/socketBindListen.cpp:31), so the kernel accepts into the backlog with noaccept loop and no log line. The
Listening for {}lines are written later, in thesequential start loop at
Server.cpp:3692, and Prometheus is pushed last(
:4162). Theprometheus.handlersinstance skips the early-start path(
:3318, gated on!config().has("prometheus.handlers")), so its Prometheus line is thelast line of startup.
first_log_line_numberread it with a one-shotgrepand no wait.On the failing run the harness wait returned at 00:51:18.720 while the line was written at
00:51:19.011710, i.e. 291 ms later. The harness's own probe being accepted
(
TCPHandlerFactory: TCP Request. Address: 172.16.1.1:62718) is logged 10 us before thatline, which shows the socket was reachable well before the loop reached Prometheus. The
two
Application: Loaded metadatagreps in the same module had only about 5 ms of marginon that run, so they were latently racy too; the fix is in the shared helper and covers all
four call sites.
first_log_line_numbernow waits for the line when it is not there yet:The
contains_in_logguard matters for runtime:wait_for_log_linerunstail -F ... | grep -Em1in the container andgrepexits on the match, buttail -Fonly notices the closed pipe on the server's next log write, so an unconditional barrier
costs about 7 s per call even when the line is already present. Measured on the whole
module: 17.19 s pristine, 35.88 s with an unconditional barrier, 16.17 s as written here.
This cannot mask an ordering regression. The failure mode being fixed is a missing line,
not a wrong order; if the lines were ever emitted in the wrong order, both would still be
found and the
assertwould still fail.Verification (integration harness, debug build):
startup_scriptsentry onnode_handlers.loadStartupScripts(Server.cpp:3541) runs between the bind at:3454and theListening forloop at:3692, so it widens the window while port 9000 already accepts.On the unpatched test this fails every time with the exact signature above; with the fix
it passes and the assertion is really evaluated (line 324 before line 562).
assert 536 < 324), so thewait did not make the test vacuous.
greperror.pytest --count 10over the module (50 executions): 50 passed.CI reports for the two failures:
An alternative would be to wait once for
Application: Ready for connections.(
Server.cpp:3699) in the fixture. That is also a sound barrier, but it is coarser andwould leave
first_log_line_numberunsafe for any future caller.Version info
26.8.1.211(included in26.8and later)