Skip to content

Speed up consensus trace replay - #8848

Closed
cjen1-msft wants to merge 5 commits into
cjen1-msft-laughing-memoryfrom
cjen1-msft-crispy-system
Closed

cjen1-msft wants to merge 5 commits into
cjen1-msft-laughing-memoryfrom
cjen1-msft-crispy-system

Conversation

@cjen1-msft

@cjen1-msft cjen1-msft commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Makes the Lean consensus replayer faster, so the cft_replay job added in #8732 costs less CI time: its Validate e2e consensus traces step drops from 76 s to 20 s.

Stacked on #8732, which records consensus trace events in every e2e test and replays them against the Lean model. These optimisations, authored by Amaury Chamayou (@achamayou), were split out of that PR so they can be reviewed on their own.

Step timings of the Replay e2e consensus traces job on ubuntu-26.04:

Step Before (18991a2) Optimised (aca3082)
Install Lean 15 s 15 s
Build replayer 9 s 10 s
Download logs 7 s 9 s
Validate traces 76 s 20 s
  • The two runs replayed the captures of different CI runs, so the comparison is approximate.
  • The optimised run still installed Lean and built the replayer, so it was not a cache hit.

Ablation

The per-node observation cache and the reuse of parsed msg objects account for most of the gain; the node-id index and the empty-send shortcut have no measurable effect.

Both tables time the Validate e2e consensus traces step on an ubuntu-26.04 runner, on the same captures (the 138 replayable captures of CI run 45581973686). The two tables come from different runner machines, the second about 20% faster, so compare seconds within a table, not between them.

Cumulative

Each row adds one optimisation to all the rows above it, ending at this PR. The per-row gains depend on this order, which puts each optimisation after the ones it needs and otherwise the largest expected gain first.

Added Step wall time Gain over previous row Gain over original CPU time Peak RSS
Original (before this PR) 58.2 s 210 s 2806 MB
Restructured reader, every optimisation below off [1] 55.3 s 2.9 s 2.9 s 210 s 1836 MB
+ one-pass observation summary 51.5 s 3.9 s 6.8 s 186 s 1836 MB
+ per-node observation cache 39.6 s 11.8 s 18.6 s 138 s 1836 MB
+ fluentd line scanner [2] 38.1 s 1.5 s 20.1 s 132 s 1850 MB
+ reuse of the parsed msg of identical consecutive events 25.9 s 12.3 s 32.4 s 91 s 805 MB
+ skip of an already-verified identical snapshot 23.4 s 2.5 s 34.9 s 82 s 805 MB
+ rule dispatch on the head event's function 19.6 s 3.7 s 38.6 s 69 s 805 MB
+ streaming line reads 16.6 s 3.0 s 41.6 s 59 s 552 MB
+ scheduler fast path, no group recording without --schedule 15.4 s 1.2 s 42.8 s 55 s 551 MB
+ largest-first ordering in tests/cft_trace_validation.py 14.5 s 0.9 s 43.7 s 57 s 551 MB
+ per-pid buckets with qsort 14.1 s 0.4 s 44.1 s 56 s 556 MB
+ skip of non-ccf.cft lines before JSON parsing 13.6 s 0.5 s 44.6 s 54 s 554 MB
+ shortcut for an empty send observation 13.5 s 0.2 s 44.8 s 53 s 555 MB
+ node-id HashMap index (this PR) 13.7 s -0.2 s 44.6 s 53 s 554 MB
  1. This PR with every optimisation below switched off. It differs from the original by code that no single switch removes, mainly that the new reader does not keep each line's whole parsed record.
  2. Here the scanner only extracts the envelope fields, so only msg is JSON-parsed; the msg reuse and the tag skip that build on it are separate rows.

Wall times within a row range over 0.7 s to 2.0 s across the five rounds, except 6.9 s in the second row and 4.5 s in the fourth, so the gains under about 1 s in the last five rows are not distinguishable from noise.

Leave one out

Each row is this PR with one optimisation removed.

Removed Step wall time Slower than this PR by CPU time Peak RSS
Nothing (this PR) 17.8 s 70 s 555 MB
Everything (before this PR) 67.7 s 49.9 s 268 s 2806 MB
Per-node observation cache [1] 37.8 s 20.0 s 149 s 555 MB
Per-node observation cache and the one-pass summary [1] 42.3 s 24.5 s 168 s 555 MB
Fluentd line scanner [2] 35.5 s 17.7 s 140 s 1707 MB
Reuse of the parsed msg of identical consecutive events [3] 32.3 s 14.5 s 128 s 1722 MB
Rule dispatch on the head event's function 21.9 s 4.1 s 87 s 554 MB
Streaming line reads 21.6 s 3.8 s 85 s 805 MB
Skip of an already-verified identical snapshot 21.1 s 3.3 s 83 s 555 MB
Scheduler fast path and not recording groups without --schedule 19.3 s 1.5 s 76 s 555 MB
Largest-first ordering in tests/cft_trace_validation.py 19.3 s 1.5 s 69 s 555 MB
Per-pid buckets with qsort [4] 18.4 s 0.6 s 73 s 553 MB
Skip of non-ccf.cft lines before JSON parsing 18.2 s 0.4 s 72 s 557 MB
Shortcut for an empty send observation 17.8 s 0.0 s 70 s 555 MB
Node-id HashMap index 17.7 s -0.1 s 70 s 554 MB

Some optimisations depend on others, so these rows overlap rather than add up:

  1. Without the cache the summary is rebuilt at every observation, and the snapshot skip, which lives in the cache, is lost too. The second row also derives the observed fields from the model definitions, as before this PR.
  2. Every line takes the full JSON parse, which also removes the msg reuse, the snapshot skip and the tag skip.
  3. Also removes the snapshot skip, which relies on the identifier that reuse assigns.
  4. Approximated by one global merge sort of the bucketed entries and a per-node filter, since the bucketed reader cannot be restored exactly without also restoring the old one.
How the ablation was measured
  • Temporary workflows on this branch, since reverted, built the replayer variants and ran python3 tests/cft_trace_validation.py <replayer> <captures> for each, as the CI step does. Cumulative: run 45664027844, scripts in dd15fe6. Leave one out: run 45652302139, scripts in 87fd16b.
  • Runner: ubuntu-26.04, 4 cores in use (CPU time is about 3.9 times wall time, so the step is CPU bound). Each run used one runner for all its variants.
  • Five rounds per run, with the variants interleaved within each round. The tables give medians.
  • Leave one out: the range of wall times within a variant is at most 1.5 s, nearly all of it from a slower first round; rounds two to five agree within 0.2 s.
  • CPU time is user plus system time of the whole step, including the Python driver. Peak RSS is the largest single process.
  • Every variant in every round of both runs exited 0 with byte-identical step output (138 accepted, 0 rejected), so the variants did the same work. They also give identical output on the 20 fixtures in strict, --lenient and --schedule modes.
  • The first table's 76 s and 20 s come from two different CI runs' captures; the ablation tables compare the same two replayers on one fixed set.

Safety and compatibility

The replayer is a test oracle, so the risk is a wrong verdict rather than a product change. No verdict difference was found (evidence below), but the following are worth a reviewer's attention:

  1. buildNodeObsCache in Interpreter.lean re-derives in one fold what the model defines as activeConfigurations, retirementIndexInLog and NodeState.retirement (Consensus/Model/ClusterConfig.lean). The checker no longer compares traces against those model definitions directly, and nothing proves the two agree.
  2. The observation cache is invalidated only for the acting node of each model step, which relies on a step changing only that node's state.
  3. matchFluentdLine in Input.lean is tied to the exact key order and spacing that json.dumps produces. Lines with a tag other than ccf.cft are skipped without being parsed, so a malformed non-ccf.cft line no longer fails the trace.
  4. prepareEvents, readLog and parseLog in Input.lean are no longer called, and readEvents repeats one block three times.
Differential testing against the unoptimised replayer

The replayer built before and after this change gave identical exit status and output on:

  • All 20 fixtures in strict, --lenient and --schedule modes (60 runs).
  • 140 real captures from CI run 45581973686 in strict and --lenient modes (280 runs).
  • About 250 single-line mutants of two captures: corrupted state fields, reformatted lines that miss the fast path, and consecutive byte-identical messages.

Restores the replayer changes from 4fb00c2 that were moved out of the base PR.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cjen1-msft
cjen1-msft requested a review from a team as a code owner November 25, 2026 15:59
cjen1-msft and others added 4 commits November 25, 2026 16:16
To be removed once the numbers are in the PR description.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reverts 87fd16b; its timings are in the PR description.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
To be removed once the numbers are in the PR description.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reverts dd15fe6; its timings are in the PR description.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@achamayou

Copy link
Copy Markdown
Member

We prefer #8849

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.

2 participants