Repository navigation
fix(json): stop truncating/colliding deep-percentile keys in JSON output - #517
Conversation
result_print_to_json() built the "Percentile Latencies" key with snprintf(buf, sizeof(buf) - 1, "p%.3f", quantile) into an 8-byte buffer. The size-1 arg wasted a byte, and %.3f both rounds sub-100 quantiles up to "100" and doesn't fit 3+ digit integer parts + 3 decimals + NUL. For --print-percentiles requests with more than 2 decimal places (e.g. 99.999, 99.9999) this silently duplicated JSON keys (99.999 emitted under "p99.99", already used by 99.99) and mislabeled 99.9999 as "p100.0", colliding with an explicit p100 request. The plain-text table was unaffected; only the JSON was wrong. Fix: widen the buffer and use "%.10g", matching the format already used for the equivalent text-table column header in run_stats::print() (which fixed the same class of bug for the CLI table). This keeps every requested quantile distinct and correctly labeled (e.g. "p50", "p99", "p99.9", "p99.999", "p99.9999") without rounding tail values up to "p100".
The per-second "Time-Serie" block had the same defect as "Percentile
Latencies": `snprintf(buf8, sizeof(buf)-1, "p%.2f", quantile)` rounds and
then truncates, so `--print-percentiles=99.99999,99.999999` emitted the
key "p100.0" twice and one value was silently overwritten in the JSON
object. Switch it to `p%.10g` into a 32-byte buffer so both JSON blocks
now agree with each other and with the stdout table headers.
BREAKING CHANGE: this deliberately drops the backwards-compat "pNN.NN"
key shape that the removed comment in result_print_to_json() promised
("legacy consumers and tests expect this exact format"). Time-Serie
percentile keys change p50.00 -> p50, p99.00 -> p99, p99.90 -> p99.9,
matching the "Percentile Latencies" change in the previous commit.
Consumers that hard-code these key names need updating; consumers that
iterate the dict are unaffected. Keeping the old shape is not possible
while also being collision-free, since no fixed-decimal format can
distinguish 99.99999 from 99.999999.
Tests updated: tests_oss_simple_flow.py (4 sites) and
test_monitor_input.py (2 sites).
…ld_flag
test_cpu_warn_threshold_flag counted the substring "of a core" and compared
the result against the number of threads whose authoritative run-average
cores_used exceeds 1.0. But two different warnings contain that substring:
memtier_benchmark.cpp:3059 "high CPU on thread %u: %.1f%% of a core ..."
run_stats.cpp:2435 "thread %u averaged %.1f%% of a core over the
run ..."
Only the second is the end-of-run warning the assertion is about. With
--cpu-warn-threshold=100 the live per-second warning is also armed at 100%
of a core, and a 1s sample can legitimately exceed that because the wall
window and the CPU-clock sampling do not line up (the same file already
allows up to PER_THREAD_PCT_CEILING = 110%). When that happens the live
warning fires, end_warnings becomes 1 while over_one_core is 0, and the
test fails - intermittently, depending on runner load.
Match "of a core over the run", which is unique to the end-of-run message.
Verified against a live run: a single invocation emits both warnings, so
count("of a core") == 2 while count("of a core over the run") == 1.
Pre-existing since redis#471; unrelated to the JSON percentile-key change on
this branch.
|
Thanks for this — went through it, the root-cause diagnosis (the The big one: And separately — does the fix actually need to touch every key, or only the ones that collide? The default config is On testing: per #364's own bar, a JSON-format fix needs a regression test that fails on master and passes here — not just a manual Nitpicking: The flaky-test fix looks fine and orthogonal — you already offered to split it, that's enough for me, no need to actually do it unless someone else feels strongly. Buffer widening and the switch to (would file this as a comment, not a formal block) |
Pushed by a maintainer (fcostaoliveira) in response to the review posted on this PR, implementing the two asks from that comment: 1. Keep the legacy "p%.2f"/"p%.3f" key shape (byte-for-byte, including its original undersized-buffer truncation) for any quantile that doesn't collide with another requested quantile -- only the specific colliding entries fall back to full precision. The default config (50,99,99.9) is now byte-for-byte identical to pristine master; the 64d863e/fbb94d9 commits on this branch renamed every key unconditionally, which is what the review pushed back on. 2. Added tests/test_json_percentile_key_collision.py: a regression test that reproduces the exact collision from this PR's own bug report (--print-percentiles=50,99,99.9,99.99,99.99999,99.999999) and asserts it fails on unpatched master (verified: master produces 5 keys for 6 requested quantiles) and passes here. Also covers the default-config back-compat invariant, and a harder case where two quantiles are close enough that even "%.10g" can't tell them apart -- which needed a third escalation tier, "%.17g" (a double's round-trip-exact precision), to stay genuinely collision-free. Consequently reverted the 4 test files this branch had updated to the new key names (differential_redis_benchmark.py, test_monitor_cluster_backpressure.py, test_monitor_input.py, tests_oss_simple_flow.py) back to their original expectations, since the default config they exercise no longer changes key shape. tests/test_cpu_stats.py (the unrelated flaky-test fix) is untouched. Does NOT resolve: the review's question about why this reverses the back-compat precedent set in redis#379 for a similar bug in a different output channel -- that's a judgment call for the PR author/maintainers, not something this patch can answer on its own. Also left run_stats.cpp:1931's pre-existing `sizeof(buf) - 1` idiom alone (out of scope, noted only as a nitpick in the review). This is a suggested patch, not a final answer -- happy to have any part of it reverted, amended, or replaced if a different direction is preferred.
|
Pushed a follow-up commit (c346638) implementing the two asks above — didn't want to just leave the questions hanging, and figured a concrete patch would make the discussion easier to react to. To be clear: this is a suggested direction, not a decision — please revert, amend, or replace any part of it if you'd rather go a different way. It's your PR. What it does:
What it doesn't answer: the #379 back-compat-precedent question is still open — that's a real judgment call, not something I could resolve unilaterally. Also left the Happy to discuss/adjust — just didn't want the review to stall waiting on a from-scratch redesign when a starting point was quick to put together. |
c346638 escalates a percentile key when it collides with another requested quantile. Collision is used there as a proxy for "this key is wrong", but the implication only runs one way: a colliding key is wrong, while a wrong key need not collide. --print-percentiles=99.9999 legacy "p%.2f" -> "p100.00" -> chopped to 6 chars -> "p100.0" legacy_keys_collide()? only one quantile requested -> false -> key stays "p100.0" Nothing is missing and no key is duplicated; the JSON simply labels a deep percentile as the maximum. The rule also misfires the other way: when two keys collide both escalate, so a key that was already correct moves because of its neighbour -- 99.9 is "p99.90" alone but becomes "p99.900000000000006" when 99.900000001 is requested alongside it. Ask the question about the key rather than about the set: use the shortest format in the existing legacy -> %.10g -> %.17g ladder whose output parses back to the quantile exactly. Collisions then become impossible rather than detected -- an accepted key parses back to its own quantile, so two distinct quantiles cannot produce the same one. The result is that a key changes if and only if it was wrong. Full JSON key-path diffs against pristine master are identical for the default config, for an explicit 50,99,99.9, and for 50,99,100 (204/204 paths, "p100.0" included). c346638's four tests pass unchanged. Adds the lone-deep-percentile case, which fails on master and on c346638, and pins that 99.9 keeps "p99.90" when requested next to 99.900000001.
|
Thanks @fcostaoliveira . Taking c346638 as the baseline — thanks for pushing it. I've kept all of it and changed one The one changec346638 escalates a key when it collides with another requested quantile. Collision Nothing is missing, nothing is duplicated. The JSON just says the maximum and means the It misfires the other way too. Both sides of a collision escalate, so a correct key
The fixAsk about the key, not the set: use the shortest format in your existing strtod(key + 1, NULL) == quantile
Why I deleted the collision scansA key is only accepted if reading it back gives exactly the quantile it labels. That The ladder can't run out of options either: So collisions are now prevented instead of detected. VerifiedThree binaries, same Redis,
Your four tests pass unchanged. Added the On the #379 questionWith this change, it isn't a reversal of #379, and the PR no longer needs its " Left alone: |
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for pushing this further — the shift from pairwise collision-escalation to a per-key round-trip test is a real improvement, not just a rename. Went through the logic by hand and then verified it against a build. I built this commit (fa15654) and ran it against a live Redis for every row in your comparison table — default The structural argument for "collisions are impossible" is sound, and it's actually stronger than the PR describes: because One real gap in the round-trip proof, though I think it's out of scope rather than a blocker: " On the #379 question: I think this resolves it, not just sidesteps it — the earlier concern was that the fix moved keys that didn't need to move; this version's per-key independence means a key only ever changes if it doesn't already name its quantile, which is the actual bar #379 set. Agreed the "Breaking change" section can go. The Nitpicking: Nice work narrowing this down through three iterations — the final design is meaningfully simpler than where it started. |
fcostaoliveira
left a comment
There was a problem hiding this comment.
Approving based on the review above — the fix is now structurally sound (per-key round-trip check, verified byte-for-byte against a real build across the full comparison table), CI is fully green, and the one open item (NaN round-trip edge case) is explicitly non-blocking. Nice collaborative iteration getting here.
|
Thanks @fcostaoliveira for the collaborative effort. |
Problem
--json-out-filegives percentile keys that name the wrong number, and sometimes tworequested percentiles the same one. Both JSON blocks are affected, and both start
failing at the same unremarkable input — this is not a deep-tail-only bug.
Both sites build the key with a fixed-decimal format into an 8-byte buffer, but pass
sizeof(buf) - 1as the size, so only 6 characters of key survive. The longest key thatfits is
p99.99. That 6-char ceiling — not the format string — is what caps both blocksat two effective decimal places.
Percentile Latencies(result_print_to_json()inrun_stats.cpp):Time-Serie(per-second stats, same function):The
%.3flooks like it carries an extra decimal, butp99.991is 7 characters andgets chopped back to
p99.99, so in practice both blocks resolve to two decimals.Where it starts
The format rounds, then the buffer truncates. Any quantile needing three or more
decimals comes out labelled as a different number:
That value is the 99.9999th percentile, and the JSON calls it the maximum. Nothing is
missing and no key is duplicated, so nothing downstream can detect it.
When two requested quantiles are mangled onto the same string, the second write also
silently overwrites the first and one percentile disappears.
--print-percentiles=99.9,99.99,99.999,100is enough to lose a value. The two blocksdiffer only in how they are wrong, not in when:
Time-Serierounds99.995and up to100.00and truncates top100.0;Percentile Latenciestruncates the same values top99.99.The plain-text table is unaffected; only the JSON is wrong.
Fix
A key is accepted only if reading it back gives exactly the quantile it labels:
Keys that pass are emitted unchanged, in their historical
p%.2f/p%.3fshape —including that shape's original truncation, since that truncated string is what
consumers read today. Keys that fail escalate to
p%.10g, and then top%.17gifneeded.
That single test also makes collisions impossible rather than detected. Reading
p99.99back gives 99.99 and nothing else, so it cannot also be a faithful spelling of99.991 — one of the two would have failed the test and escalated. Two different numbers
can't both pass with the same string.
p%.17greads back exactly for any double, sosome tier always passes.
Compatibility
A key changes if and only if it does not name the quantile it holds. Keys that are
correct today do not move — not just under the default config, but under any
configuration, regardless of what else is requested alongside them.
--print-percentiles50,99,99.9(default)p50.00,p99.00,p99.9050,99,100p50.00,p99.00,p100.050,99,99.9,99.999p99.99(names 99.99)p99.99999.9999p100.0(names 100)p99.999999.99,99.991p99.99(one value lost)p99.99,p99.991Consumers that look up
p50.00,p99.00,p99.90orp100.0keep working. Consumersreading a key that currently names the wrong quantile will see it renamed to the right
one — but that key was already returning a value that didn't match its label, so there
was nothing correct to depend on.
This does not reverse the back-compat decision in #379. That decision protected JSON
keys that were correct, and every one of them is still protected here.
Testing
makeclean with no new warnings;make format-checkreports nothing forrun_stats.cpp.Added
tests/test_json_percentile_key_collision.py(5 tests, all passing):50,99,99.9,99.99,99.99999,99.999999yields six distinct keys, each parsingback to its requested quantile — master emits five for six requests
Time-Seriebucket99.9999requested alone is keyedp99.9999, notp100.0— the mislabel case, whichcollides with nothing
99.9,99.900000001,99.99999991,99.99999992: four distinct keys, and99.9keepsp99.90even though its neighbour needs%.17gp50.00,p99.00,p99.90in both blocksVerified beyond the suite:
flag, for an explicit
50,99,99.9, and for50,99,100check in the code
been reverted, so the net diff is
run_stats.cppplus the new testDrive-by: unrelated flaky test fix
test_cpu_stats.py::test_cpu_warn_threshold_flagwas failing CI on this branch. It isnot related to this change — it has been flaky since #471.
The test counted the substring
'of a core'in stderr and compared it against thenumber of threads whose run-average
cores_usedexceeds 1.0. Two different warningscontain that substring:
Only the second is the end-of-run warning the assertion is about. With
--cpu-warn-threshold=100the live per-second warning is armed at 100% of a core too,and a 1-second sample can legitimately exceed that because the wall window and the
CPU-clock sampling do not line up — the same file already tolerates this via
PER_THREAD_PCT_CEILING = 110.0. When the live warning fires,end_warningsbecomes 1while
over_one_coreis 0 and the test fails, depending on runner load.Fixed by matching
'of a core over the run', which is unique to the end-of-run message.Happy to split this into its own PR if you'd prefer to keep this one single-purpose.