Skip to content

Disambiguate duplicate per-command JSON keys and HDR file names when --command lines share a name - #588

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix/command-stats-breakdown-json-keys
Oct 14, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix/command-stats-breakdown-json-keys

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

When two --command lines share a name, the JSON output repeats the same object key. Two examples:

  • --command="GET __key__" --command="GET __key__"
  • the README's own --command="SET foo ..." --command="SET bar ..."

On master, ALL STATS has the keys ['Runtime', 'CPU', 'Gets', 'Gets', 'Per-Key Misses', 'Totals', 'CPU Stats', 'GET', 'GET']. JSON parsers keep only the last duplicate, so a whole per-command row disappears and the rows no longer add up to Totals. A review run showed 1676 against 2694. Per-Key Misses repeats "GET" even in the default --command-stats-breakdown=command mode.

The per-command --hdr-file-prefix files have the same problem. Both lines write <prefix>_GET_command_run_N.{txt,hgrm}, and the second overwrites the first. On master, the surviving .hgrm held only the second line's 20 samples, and the first line's 60 were lost.

Root cause

command_list[i].command_name is used as an identifier. For a plain --command it is just the upper-cased first word, so it is not unique per line. It is used at five places:

  • per-line rows in print_json() (pluralized, "Gets")
  • per-line histogram arrays in print_histogram() ("GET")
  • Per-Key Misses
  • Arbitrary Read Routing
  • HDR file names in save_hdr_arbitrary_commands()

Fix

A small helper, make_unique_labels(names, sep), used through unique_command_labels(), leaves names that occur once unchanged. Each occurrence of a repeated name gets sep plus a 1-based ordinal in output-row order. For user lines, that is --command order.

  • JSON keys use " #N": "Gets #1", "Gets #2", "GET #1", "GET #2".
  • HDR file names use the shell-safe "_N": <prefix>_GET_1_command_run_1.hgrm. An unquoted # would start a shell comment or a URL fragment.
  • Compatible: when all names are distinct, which is the usual case, every key and file name stays exactly as before. The aggregated (command) rows are already unique by type, so they are unchanged.
  • Unchanged outputs: the text table and CSV are not changed.
  • Monitor collisions: a __monitor_line@__ stats slot GET next to a user GET, or a repeated __monitor_lineN__, is disambiguated the same way.
  • Docs: README documents both schemes.

Tests

In all tests the first GET line runs at --command-ratio=3, so the two lines have different counts and a reversed numbering or swapped rows cannot pass. JSON is loaded with an object_pairs_hook that collects duplicate keys.

  • test_monitor_input.py::test_command_stats_breakdown_line_duplicate_json_keys (GET×2 + SET, 300 requests) checks:
    • there are no duplicate keys, and no plain Gets/GET
    • the exact Counts are Gets #1=180, Gets #2=60 and Sets=60
    • each row's decoded histogram holds the same number of samples
    • Totals=300
    • the GET #1, GET #2 and SET histogram arrays exist
    • Per-Key Misses hits plus misses are 180 for GET #1 and 60 for GET #2
  • ..._command_duplicate_json_keys (default mode) checks:
    • there are no duplicates
    • Gets=240 and Sets=60
    • Per-Key Misses is GET #1=180 and GET #2=60
  • ..._line_duplicate_monitor_lines uses __monitor_line1__ twice (ratio 3/1) and checks:
    • there are no duplicates
    • Get (line 1)s #1=225 and #2=75, with matching histogram totals
    • both histogram arrays exist
  • test_hdr_histogram.py::test_hdr_files_for_duplicate_command_names checks:
    • GET_1, GET_2 and SET each have a .txt and a .hgrm
    • each .hgrm decodes to that line's own sample count (60/20/20)
    • there is no plain GET file, and no file name contains # or a space
  • test_read_preference_modes.py::test_arbitrary_read_routing_duplicate_command_names (replica cluster cell, --read-preference=secondary) checks:
    • there are no duplicates
    • Arbitrary Read Routing has exactly GET #1 and GET #2, with 150 and 50 routed ops

Results with RLTest:

origin/master 6b6d6df this branch
4 standalone tests 4/4 fail 4/4 pass, in 6/6 runs
read-routing test (3 shards + replicas) fails (['GET', 'GET'] == []) passes, in 5/5 runs

A run on the earlier head e4d14e8 also passed the 3-shard OSS cluster, in 3/3 runs.

Mutation check: all 12 hand-made mutants of run_stats.cpp are killed.

Mutant Killed by
Numbering reversed standalone + routing
Suffix always added standalone
Suffix never added standalone + routing
Line rows reversed standalone
Per-key site reverted standalone + routing
Histogram site reverted standalone
Routing site reverted routing
HDR uses #N HDR test
Per-key keys swapped standalone
HDR site reverted HDR test
Routing keys swapped routing
HDR names swapped HDR test

Regression suites on this branch:

Suite Result
test_monitor_input.py 17/17
test_hdr_histogram.py 4/4
test_arbitrary_command_misses.py 16/16
tests_oss_simple_flow.py::test_default_set_get_with_print_percentiles pass
test_read_preference_modes.py (replicas) 7/7

make format-check-staged is clean.

Note on the review's "first row Misses/sec 0.00 vs 14837": the per-line hit/miss counters are correct. That asymmetry comes from per-command options (--command-key-pattern, --command-ratio) that attach only to the preceding --command, so the two lines were not really identical. The duplicate Gets labels hid that.

Out of scope: a command named CPU colliding with the fixed CPU key will be tracked separately.

Fixes #587

Affected versions

1.2.16 (7cdbaff, #72) through 2.5.2 and master.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. I went through the diff, and the fix looks right to me. Names that occur only once keep their current key and file name, so the usual case doesn't change for anyone reading the JSON. The tests run the exact broken case (two GET lines with different ratios), and they would catch both a dropped row and swapped numbering. That matches what #364 already did. A couple of small things:

  • I think the only real compat question is what happens to a consumer that reads all_stats["Gets"] in line mode when two lines share a name. Before, it silently got the last row. Now it gets a KeyError. Leaving the first occurrence unsuffixed wouldn't really keep the old behavior either, since parsers returned the last duplicate. So I guess failing loudly is better than silently returning a different row. It might still be worth a line in the release notes.
  • Nitpicking: _hist_count() in test_monitor_input.py reimplements the V2 histogram decoding. test_cluster_redirect_stats.py already does from test_hdr_histogram import _decode_v2. Could this do the same, i.e. _decode_v2(encoded)[0]?
  • Nitpicking: the no_duplicates object_pairs_hook appears twice, once in test_monitor_input.py and once in test_read_preference_modes.py. It might be worth moving it into include.py, so the next JSON test can reuse it.

Neither nitpick blocks anything. The core fix looks solid to me.

(I'd post this as a comment, not a formal block.)

fcostaoliveira and others added 3 commits October 14, 2026 03:46
Two --command lines with the same name (e.g. two "GET __key__" lines)
were written to the JSON output under the same object key: "Gets" twice
in ALL STATS with --command-stats-breakdown=line, "GET" twice for the
per-line latency histograms, and "GET" twice in "Per-Key Misses" (in both
breakdown modes) and "Arbitrary Read Routing". JSON parsers keep only the
last duplicate key, so a whole row was silently dropped and the
per-command rows no longer added up to Totals.

Names that occur once keep their existing key, so the common case is
unchanged for consumers. Each occurrence of a repeated name gets a " #N"
suffix (1-based, in --command order, matching the row order of the text
table), e.g. "Gets #1"/"Gets #2" and "GET #1"/"GET #2". The text table
and CSV are unchanged.

Fixes #587

Co-Authored-By: Claude Opus 5.5 <[email protected]>
save_hdr_arbitrary_commands() named each per-command HDR file after
command_name, so two --command lines with the same name (e.g. two
"GET __key__" lines) both wrote <prefix>_GET_command_run_N.{txt,hgrm} and
the second silently overwrote the first, losing one line's histogram.
This happens in both --command-stats-breakdown modes.

Reuse the JSON-key disambiguation (renamed make_unique_labels /
unique_command_labels and moved above its first user): unique names keep
their file name, repeated names get the same " #N" suffix as their JSON
keys, e.g. <prefix>_GET #1_command_run_1.hgrm.

Fixes #587

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tests

" #N" is fine as a JSON key, but in a file name an unquoted "#" starts a
shell comment (and a URL fragment): `hdr-plot histogram_GET #1_...`
receives just "histogram_GET". Keep " #N" for JSON keys and use "_N" for
the per-command --hdr-file-prefix files, e.g.
<prefix>_GET_1_command_run_1.hgrm. Unique names are unchanged.

Tests:
- Run the first GET line with --command-ratio=3 so the two lines have
  different counts, and assert exact per-key Counts plus the decoded
  histogram sample totals, so a reversed numbering or swapped rows fail.
- Cover the "Arbitrary Read Routing" site in test_read_preference_modes.py
  (replica cluster cell).
- Cover a repeated __monitor_line1__.

Fixes #587

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/command-stats-breakdown-json-keys branch from cd9b021 to 139ba83 Compare October 14, 2026 03:04
@fcostaoliveira fcostaoliveira changed the title Disambiguate duplicate per-command JSON keys when --command lines share a name Disambiguate duplicate per-command JSON keys and HDR file names when --command lines share a name Oct 14, 2026
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Here is what changed for each point, as of head 139ba83:

  • HDR file names: agreed, and changed. JSON keys keep #N, but HDR files now use a shell-safe _N suffix, e.g. <prefix>_GET_1_command_run_1.hgrm and <prefix>_GET_2_command_run_1.hgrm. An unquoted # starts a shell comment, so hdr-plot histogram_GET #1_... only received histogram_GET. Unique names keep their file names. The README describes both schemes, and the HDR test now also checks that no file name contains # or a space.
  • Title / release notes: the PR title now mentions the HDR file names too, and the squashed message should keep that wording.

Other changes from the follow-up review:

  • Tests: the first GET line now runs at --command-ratio=3, so the tests check exact per-line counts and histogram sample totals. A reversed numbering or swapped rows now fail.
  • New test for Arbitrary Read Routing: added in test_read_preference_modes.py, the replica cluster cell.
  • New test for a repeated __monitor_line1__.
  • Mutants: all 12 mutants of the change are killed. The PR body has the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON output has duplicate keys when two --command lines share a name (rows silently dropped by parsers)

1 participant