Repository navigation
test: account for %lld truncation and %.3f rounding in JSON latency integrity checks - #636
Conversation
f8b59be to
45172b9
Compare
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. I went through the bound in One question: the third commit makes the bound one-sided and adds Nitpicking: Test-only change and the math checks out, so I'd mainly want the description to match what the PR now does before merge. |
45172b9 to
6a1c613
Compare
|
Thanks for the review. Point by point:
The branch is rebased on master 0298524. |
6a1c613 to
3230e29
Compare
3230e29 to
dfc7f41
Compare
|
Update for the current head, dfc7f41, rebased on master 91eec24. This replaces my earlier reply, which described the previous revision.
|
dfc7f41 to
bca47c4
Compare
The per-second consistency check allowed 1/count ms of slack for the %lld truncation of "Accumulated Latency", but "Average Latency" is also rounded by %.3f (up to 0.0005 ms). Both fields come from the same whole-microsecond sum, so the gap is at most 0.999/count + 0.0005. When the true sum is just under an integer ms and the avg rounds up (6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and float error made the check fail (seen in TSAN CI on PR #616). Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic regression case with two buckets: the CI values (exactly on 1/count, needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143 > 1/7, needs the rounding term). Fixes #635 Co-Authored-By: Claude Opus 5.5 <[email protected]>
_assert_latency_consistent() compared Latency with Accumulated/Count using only max(0.01 ms, 2%). "Accumulated Latency" is printed with %lld and is truncated by up to 0.999 ms, the same as in the Time-Serie buckets, so a low-count section fails on correct output. For example, -n 3 --ratio 1:1 gives Sets Count 2, Latency 0.478 and Accumulated Latency 0. Move the per-bucket bound (1/count + 0.0005 + epsilon) into _latency_tolerance() and use it in both checks. Add a synthetic section case (3 ops summing to 999 us) that fails with the old formula and passes with the new one. Also update the comment above ABS_TOLERANCE_MS, as suggested in the automated review. This covers the false-failure half of #645. The false-pass half (the symmetric 1/count slack accepts a truncated average for count <= 3) is left for #645. Refs #645 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A correct average can only be above Accumulated/Count by more than the static floor. "Accumulated Latency" is truncated by %lld, so Accumulated/Count is at most 0.999/count low, plus 0.0005 ms of %.3f rounding on the average. Below Accumulated/Count, only print error is possible. Time-Serie and Totals averages use double division and are off by up to 0.0005 ms. Sets/Gets and arbitrary-command sections use integer us division (totals_cmd::summarize), which is exact in %.3f and less than 0.001 ms low. So the 1/count + 0.0005 term now applies only above, and the 0.01 ms / 2% floor applies below. With that, an average truncated to an integer is rejected even for low counts. A new test covers the #645 example: 3 ops, Accumulated 4, average 1.000. Drop FLOAT_EPSILON_MS. The bound leaves at least 0.001/count of headroom over the worst case, so the epsilon never decided a result. Also fix the docstring, which said the integer-division error came from the Totals section and could reach 0.0015 ms. Refs #645 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every real run in the file used >= 1000 requests, so a writer regression that only shows at low counts (e.g. Accumulated rounded up instead of truncated) passed the whole suite. Add a 3-request run that goes through _validate_run_section, and a synthetic case on each side of the bound: a correct integer-us average just below Accumulated/Count (rejects a zero lower bound) and one just above 1/count + 0.0005 (rejects a looser upper bound). The docstring now says the bounds do not apply to the default per-type arbitrary-command sections, whose average is an HDR mean. Co-Authored-By: Claude Opus 5.5 <[email protected]>
bca47c4 to
71717c5
Compare
crash_handler() called localtime() and strftime() before it printed anything. localtime() takes glibc's time zone lock, and the first call in the process (always the one in the handler, since nothing else in memtier calls it) runs tzset_internal(), which mallocs and opens /etc/localtime. A crash that interrupts a thread holding the malloc or time zone lock therefore deadlocked the handler before the "BUG REPORT START" line, and its own alarm(5) watchdog then killed the process with SIGALRM and no output. That is the flaky test_crash_handler_worker_thread failure seen on PR #636. The handler now: - takes the local UTC offset once in prewarm_crash_handler() and formats the timestamp with integer arithmetic, in the same "%d %b %Y %H:%M:%S" format; - writes the START header, the signal number and name, the signal code and the fault address with write(2) to stderr, before backtrace(), stdio or anything else that can lock; - arms a SIGALRM handler that writes a "BUG REPORT INCOMPLETE" line before the watchdog ends the process, so a later hang is visible. The rest of the report (backtrace, fprintf, client list) is unchanged and stays after the essential lines; a comment lists which of those calls are not async-signal-safe and why. The new test_crash_handler_time_zone_lock_held preloads a shim whose localtime/localtime_r/strftime/tzset block when called on a signal stack, which reproduces the deadlock on every run: it fails on master (exit -14, no report) and passes with this change. Fixes #681 Co-Authored-By: Claude Opus 5.5 <[email protected]>
crash_handler() called localtime() and strftime() before it printed anything. localtime() takes glibc's time zone lock, and the first call in the process (always the one in the handler, since nothing else in memtier calls it) runs tzset_internal(), which mallocs and opens /etc/localtime. A crash that interrupts a thread holding the malloc or time zone lock therefore deadlocked the handler before the "BUG REPORT START" line, and its own alarm(5) watchdog then killed the process with SIGALRM and no output. That is the flaky test_crash_handler_worker_thread failure seen on PR #636. The handler now: - takes the local UTC offset once in prewarm_crash_handler() and formats the timestamp with integer arithmetic, in the same "%d %b %Y %H:%M:%S" format; - writes the START header, the signal number and name, the signal code and the fault address with write(2) to stderr, before backtrace(), stdio or anything else that can lock; - arms a SIGALRM handler that writes a "BUG REPORT INCOMPLETE" line before the watchdog ends the process, so a later hang is visible. The rest of the report (backtrace, fprintf, client list) is unchanged and stays after the essential lines; a comment lists which of those calls are not async-signal-safe and why. The new test_crash_handler_time_zone_lock_held preloads a shim whose localtime/localtime_r/strftime/tzset block when called on a signal stack, which reproduces the deadlock on every run: it fails on master (exit -14, no report) and passes with this change. Fixes #681 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
test_json_output_integrity.pyfails intermittently on low-count per-second buckets. Seen in TSAN "Single Endpoint TCP TLS (misc)" on PR #616:Average Latency 7.0000 ms inconsistent with Accumulated/Count (41.0/6 = 6.8333 ms); tolerance 0.1667 msThe section-level check (
_assert_latency_consistent) is worse, because it has no1/countterm at all. A low-countSets/Gets/Totalssection fails on correct output. With-n 3 --ratio 1:1, all three sections fail. This was raised in the automated review and reported in #645.Root cause
Test-only. All fields come from the same whole-microsecond sum, so memtier's output is consistent.
Accumulated Latencyis printed with%lld, which truncates it by less than 1 ms. That makes Accumulated/Count low by at most 0.999/count ms.%.3f, which rounds by up to 0.0005 ms.1/count). In the CI case (6 ops, true sum just under 42 ms, acc 41, avg 7.000),7.0 - 41/6is 0.16666666666666696 in floating point, which is greater than1/6(0.16666666666666666).max(0.01 ms, 2%). With sub-ms latencies and a few ops,Accumulated Latencytruncates to 0 and the check fails.Fix
Both checks now share
_latency_bounds(derived, count), which is one-sided:max(0.01, 2%, 1/count + 0.0005). The worst correct case is 0.999/count + 0.0005, so there are at least 0.001/count ms of headroom. No float epsilon is needed.max(0.01, 2%). Here a correct average can only be off by print error: up to 0.0005 ms for the Time-Serie and Totals averages (double division), and less than 0.001 ms for Sets/Gets and arbitrary-command sections. Those use integer-µs division intotals_cmd::summarize, which is exact in%.3f.Making the bound one-sided keeps the integer-truncation regression detectable at low counts. The previous symmetric
1/countslack accepted it for count <= 3 (#645).Deterministic tests:
test_time_series_tolerance_covers_truncation_and_rounding: buckets built from the CI values, plus a 7 ops / 7.999 ms bucket that needs the%.3frounding term.test_section_tolerance_covers_truncation: 3 ops summing to 999 µs (Accumulated 0, Latency 0.333). This must pass.test_tolerance_rejects_truncated_average: the test_json_output_integrity: latency tolerance lets a truncated average pass on low-count buckets and wrongly fails low-count sections #645 example (3 ops, 4.2 ms, Accumulated 4, average printed as 1.000). Both checks must reject it.Test evidence
-n 3 --ratio 1:1output (Sets 2 / 0.478 / 0, Gets 1 / 0.169 / 0, Totals 3 / 0.375 / 1): master's check fails all 3 sections, and the new check passes them.Latency/Average Latencyto an integer. The new checks flag every section and bucket (6/6 per run).-n 1..4with skewed--ratio, plus--pipeline 8/40/48runs with--run-count 2, which include the BEST/WORST/AGGREGATED sections. There were 0 failures.RLTest --env oss -t test_json_output_integrity.pytwice. Run 2 passed 15/15. Run 1 passed 14/15:test_worker_finalization_failure_skips_process_cleanupfailed once. That test does not run the latency checks, and it then passed 12/12 times when run alone.Scope relative to #645
max(0.01, 2%)), not test_json_output_integrity: latency tolerance lets a truncated average pass on low-count buckets and wrongly fails low-count sections #645's tighter0.0005, so a truncation that drops less than about1/count + 0.01ms is still accepted. Count = 1 can't be detected from the printed fields at all, as test_json_output_integrity: latency tolerance lets a truncated average pass on low-count buckets and wrongly fails low-count sections #645 notes.Fixes #635
Refs #645
Affected versions
Test-only; binaries unaffected. Both checks came from 7344622 (#364), and the Time-Serie
1/countterm was added in 324553f. Both commits are first in 2.4.0, so 2.4.0 through 2.5.2 and master are affected.🤖 Generated with Claude Code
Note
Low Risk
Test-only changes to assertion tolerances and new regression tests; memtier binaries and runtime behavior are unchanged.
Overview
Fixes flaky false failures in JSON latency integrity tests by modeling how memtier prints numbers:
Accumulated Latencytruncates to whole ms (%lld) and averages round to three decimals (%.3f).Section and Time-Serie checks now share
_latency_bounds(), which allows a wider slack aboveAccumulated/Count(1/count + 0.0005on top of the existing 0.01 ms / 2% floor) but keeps the below side tight so regressions like averages truncated to integers (#645) still fail.Adds a 3-request integration test plus synthetic cases (including a
_FailureCounterharness) that lock in the CI TSAN bucket, low-count Sets sections, valid truncation/rounding, and rejection of bogus integer averages.Reviewed by Cursor Bugbot for commit 71717c5. Bugbot is set up for automated code reviews on this repo. Configure here.