Repository navigation
Export every operation in memtier_latency_seconds (fixes #637) - #665
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this, and for the instrumentation in the description. It makes the root cause easy to follow. I went through the C++ side and the new tests, and a few comments are below. The design looks right to me. The worker folds into The metric's meaning does change for anyone scraping it. I don't think that's a compat problem in the usual sense, though. The old HELP text explicitly said A couple of questions:
Nitpicking: Overall the fix looks solid to me. The threading changes in (would file this as a comment, not a formal block) |
0eaf5e8 to
48df4ac
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 48df4ac. Configure here.
e31b472 to
07e4370
Compare
|
Thanks for the review. Replies to your points:
07e4370 also fixes the Bugbot finding about Ctrl+C: on that path the run-end The |
07e4370 to
0090da4
Compare
|
Follow-up on the updated review, now at 0090da4 (rebased on master 57ad921):
|
|
Correction to my earlier note: I said a scrape test can't see the run-end snapshot after Ctrl+C. That was wrong. Ctrl+C does not stop later 43ee3f8 adds
|
43ee3f8 to
92dedfe
Compare
The Prometheus exporter read each client's per-second latency histogram at its 1 Hz tick. That histogram is reset at each worker-second boundary, and the main loop's tick lands at an almost constant phase just after the reset (instrumented: 0.3-0.6 ms into the worker second, 6-10 samples visible), so each tick saw only a handful of samples. The positive-delta copy then dropped part of those too. memtier_latency_seconds_count grew by tens of samples per second while memtier_ops_total grew by tens of thousands, contrary to the "about half" the docs promised. Instead, the worker now folds each completed second into a per-client pending histogram right before the per-second reset (only when the exporter is enabled), and the exporter tick drains it (add + reset). Both sides hold m_inst_histogram_mutex, so every sample is exported exactly once. The last partial second is folded by set_end_time(), and the run-end publish drains every thread, so _count matches the ops completed in the run. While a run is in progress _count trails ops_total by up to about one second. This removes copy_inst_histogram_if_changed and the now-unused prom::hdr_add_positive_delta (and its U9 unit test). The README, docs/REALTIME_METRICS.md and the HELP text no longer claim sampling. Fixes #637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The 1 Hz tick skipped threads that had already finished, so with --requests a thread that finished early kept its last partial second out of memtier_latency_seconds until the run-end publish. The drain is protected by m_inst_histogram_mutex and every m_cg lives until after the run-end publish, so finished threads are now drained too. The run-end drain stays for the exits that skip the loop's last tick (Ctrl+C, connection-stage abort). Docs and HELP text: the histogram also lags while the server is stalled, and the HDR quantization is 1% only above ~1 ms (8 us slots below ~2 ms). Refs #637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
test_F26 runs --requests with --run-count=2 and asserts every scrape taken during run 2 already counts all of run 1's operations in memtier_latency_seconds_count. Also document how F25's 0.7 bound follows from the at-most-one-second lag. Refs #637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The 1 Hz tick read every thread's ops counter, then drained the latency histograms. If a thread finished, or a connection rolled its second, in between, the drain picked up samples whose ops were not yet counted, and the scrape showed memtier_latency_seconds_count > memtier_ops_total until the next tick. Draining first makes that impossible: a second is folded under the drain's mutex only after its ops were counted. Docs: the lag is about one second, longer when a connection's next op is slow (a stalled server, a low --rate-limiting rate), and _count never exceeds ops_total. Re-wrap the tsan_suppressions.txt comment. Refs #637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
On Ctrl+C and the connection-stage abort, client_group::interrupt() calls set_end_time() from the main thread while a worker may still be finishing its current read callback. Two things lost latency samples there: - set_end_time() folded the live inst-totals histogram into the pending histogram and reset it on the main thread, while the worker recorded into it lock-free. Samples recorded between the add and the reset were dropped. - Replies the worker completed after that fold were counted in memtier_ops_total but never reached the pending histogram, because m_end_set stops a second set_end_time(). So the run-end memtier_latency_seconds_count could trail memtier_ops_total (7 of 20 SIGINT runs, by up to 988 ops). When interrupted with the exporter enabled, set_end_time() now leaves the live totals in place, and the post-join run-end drain moves what is left in them as well. Both are no-ops on the --test-time / --requests paths. Also re-wrap the over-long tsan_suppressions.txt comment line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ctrl+C does not stop later --run-count runs: run 1 publishes its run-end snapshot right away and run 2 starts a second later, so a scrape in that gap sees run 1's final memtier_ops_total and memtier_latency_seconds_count. F27 interrupts run 1 mid-traffic, lets the 1 s render cache expire first so its next scrape renders inside the gap, and asserts the two are equal. It skips if no scrape lands between the runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
set_end_time() inferred "called from interrupt() on the main thread" from m_interrupted, a plain bool the main thread writes while a worker may read it. interrupt() now passes the intent through finalize_all_clients(true) -> client::set_end_time_keep_live_totals() -> run_stats::set_end_time(NULL, true); every other caller keeps the default (fold on the worker), so no flag is read across threads and a new main-thread caller has to opt in explicitly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
92dedfe to
082233d
Compare
|
On the updated review, now at 082233d:
|
|
On the latest review:
|

Decision:
memtier_latency_secondsnow counts every completed operation exactly once. It is no longer a sample, and the docs and HELP text no longer promise "about half". While a run is in progress,_counttrailsmemtier_ops_totalby up to about one second. At run end the two are equal, including after Ctrl+C.Release note:
memtier_latency_secondsnow records every completed operation, somemtier_latency_seconds_countis comparable tomemtier_ops_total(it trails it by about one second during a run and equals it at run end); before, it was a small, phase-dependent sample and its HELP text said it was not comparable. Dashboards that derived a rate from_countwill now show the real operation rate.Problem
memtier_latency_seconds_countstops growing after the first second. It gains a few dozen samples per second whilememtier_ops_totalgains tens of thousands. On master with-c 5 -t 2, scraped once a second:Root cause
I confirmed this by logging each exporter tick in
run_stats::copy_inst_histogram_if_changed()with-c 1 -t 1:The exporter read each client's per-second histogram,
inst_m_totals_latency_histogram. The worker resets that histogram at every second boundary insummarize_current_second(). The main loop's 1 Hz tick lands at an almost fixed phase, 0.3 to 0.6 ms after that reset, so it sees only a handful of samples.prom::hdr_add_positive_delta()then drops part of those too: 8 samples seen, 5 added. Now and then a tick lands just before the reset and catches one full second. That is why the capture rate depends on phase and is never close to the 50% the docs claim.Fix
run_statsgets a per-clientm_prom_pending_latency_histogram. Right before the per-second reset, the worker adds the second's histogram into it. This only happens when the exporter is enabled (m_config->prometheus != NULL), so the cost is onehdr_addper client per second and nothing per operation.drain_prom_latency_histogram(). Both sides holdm_inst_histogram_mutex, and only those two code paths touch the pending histogram, so every sample is exported exactly once with no race.set_end_time()already summarizes the last partial second. Every tick drains every thread, finished ones included, so threads that finish early under--requestsdon't lag. That is safe because the drain holds the mutex and everym_cglives until after the run-end publish. The run-end publish drains once more and passes the result topublish_run_end(), which used to publish a NULL histogram. In the normal flow the loop's last tick has already drained everything. The run-end drain covers the exits that skip that tick: Ctrl+C and the connection-stage abort.client_group::interrupt()callsset_end_time()from the main thread while a worker may still be finishing its current read callback. When the run is interrupted and the exporter is enabled,set_end_time()now leaves the liveinst_m_totals_latency_histogramin place instead of folding and resetting it from the main thread, and the post-join run-end drain (drain_prom_latency_histogram(target, run_end=true)) also moves whatever is left in it. A main-thread fold+reset races the worker's lock-free recording and drops the samples recorded between the add and the reset; ops completed after it were never folded at all. Both changes are no-ops on the--test-time/--requestspaths, whereset_end_time()runs on the worker after all connections are idle.copy_inst_histogram_if_changed(),prom::hdr_add_positive_delta()with its U9 unit test, and the matching TSan suppression line. None of them is used any more.docs/REALTIME_METRICS.md(error model) and the HELP text now describe the actual behaviour. That includes a longer lag while the server is stalled, and HDR quantization that is 1% only above ~1 ms: the slots are 8 µs wide below ~2 ms, so up to 8% at 100 µs.Tests
New
tests/test_prometheus.py::test_F25_latency_count_tracks_ops(10 s run,-t 2 -c 2). It scrapes every 0.5 s and asserts two things:_count <= ops_totalon every scrape._countgrows by at least 0.7 times whatops_totalgrows.Measured locally (standalone,
--env existing-env). The ratio is Δcount/Δops:Run-end exactness with
--run-count=2: at the run-1 boundary,ops_totaland_countwere both 254998.New
test_F26_run_end_drains_latency(--requests=50000 -t 2 -c 2 --run-count=2) asserts that every scrape taken during run 2 already counts all 200000 run-1 operations in_count, and that_count <= ops_total._count50789 to 181412 of 200000)publish_run_endpublishes NULLCtrl+C at run end (
-t 4 -c 2 --pipeline 1024 --test-time 30, SIGINT after a random 1.2-2.0 s, run-end_countvsmemtier_ops_totalread from a temporary debug print inpublish_run_end, not committed)._count<ops_totalat run endNew
test_F27_interrupt_run_end_counts_every_op(--test-time=30 -t 4 -c 10 --pipeline=16 --ratio=1:1 --run-count=2). Ctrl+C does not stop later--run-countruns: run 1 publishes its run-end snapshot right away and run 2 starts about a second later, so a scrape in that gap sees run 1's final counters. F27 stops scraping for 1.2 s (so the 1 s render cache expires), sends SIGINT with traffic in flight, then scrapes until exit. On the last scrape that shows run 1 with 0 threads, it asserts_count == memtier_ops_total. It skips if no scrape lands between the runs.prom_drain_latency(cfg, threads, false))_countshort by 224096 to 318240 ops--pipeline=16; a reviewer's probe saw 2/35 short by 16So F27 guards the run-end drain of the live histogram, the part a revert would break wholesale. The narrow loss it was written for, between the main thread's add and reset, can't be caught reliably from outside the process. That part is covered by code review and the reviewers' instrumented SIGINT probes: 2/25 runs short on the parent commit, 0/40 at this head. A deterministic
run_statsunit test is a follow-up.With
--test-time 3,--requests 20000and--test-time 2 --run-count 2, the run-end_countequalsops_totalexactly (2198912, 160000, 1579712 / 3976064).F25's 0.7 bound: the window is about 7-8 s and
_countlags by at most about 1 s of ops at either end, so the ratio cannot drop much below 0.86. It also holds in the sanitizer builds: at e31b472, F25 and F26 passed in the ASAN and TSANSingle Endpoint: TCP TLS (misc)cells (which runtest_prometheus.py), and every ASAN/TSAN/UBSan cell was green.The rest of
test_prometheus.pypasses with this PR. F18 and F23 are skipped because they need a cluster. F15 needsROOT_FOLDERset for its fixture, and passes with it.make check(unit tests) passes.Affected versions
2.5.0 through 2.5.2 (latest) and master.
git tag --contains 16df2dbgives 2.5.0 as the first tag.Fixes #637
🤖 Generated with Claude Code