Skip to content

fix(store): reconcile mixed-version sequence state monotonically - #653

Closed
almondous wants to merge 1 commit into
flop-labs:mainfrom
almondous:fix/seqstate-rolling-monotonic-reconcile
Closed

almondous wants to merge 1 commit into
flop-labs:mainfrom
almondous:fix/seqstate-rolling-monotonic-reconcile

Conversation

@almondous

@almondous almondous commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Status: HOLD — not merge-ready. A follow-up cross-version process test found an
omitted ordering that this commit does not fix. See the self-review comment below.

What

This draft investigates #652. Its current commit reconciles legacy flat and sharded
sequence state by taking independent maximum floor and generation values.

Why

That rule fixes the originally reported ordering where OLD reaps and NEW recreates
before a recovery split. It is not sufficient for the inverse ordering: after NEW
retires the flat map, OLD can recreate an absent room before another flat map exists.
OLD then accepts sequence 1 and generation 1 despite a retained shard floor of 2 and
generation 1. Reconciliation happens after the collision and cannot undo it.

A correct rolling migration needs a compatibility phase that keeps authoritative
legacy state available to OLD and dual-writes NEW lifecycle updates, followed by an
explicit cutover after OLD workers drain (or an explicit non-rolling migration
boundary). I am keeping this PR in draft while that design boundary is resolved.

Verified with separate OLD and NEW processes against one disposable filesystem:

  • OLD: 487caca186a3da60597bffe29dac69ac0802a4ca
  • upstream base: c0455ff53ffe468dd814525c8aa9e8ad236f1c79
  • current draft: eebdaddc3f11adf3f2c5096e31ce4727c86a0382

@almondous

Copy link
Copy Markdown
Contributor Author

Self-review update: HOLD.

I reproduced an omitted ordering with separate OLD and NEW Python processes using the
real store implementations and one disposable root:

  1. OLD creates seq 1/2, then reaps: flat = {floor: 2, gen: 1}.
  2. NEW performs the initial split: flat absent, shard = {2, 1}.
  3. OLD recreates the absent room before another flat map exists.

At step 3 OLD reads no .seqstate, accepts a new record at seq 1 / generation 1, and
writes flat = {floor: 0, gen: 1}. The distinct conversations have already collided;
NEW-side component maxima cannot repair that after acceptance.

A subsequent real NEW reap also confirms the write-path blocker:

  • upstream c0455ff: retained floor 2 -> 0 (the same reap's recovery split applies
    whole-entry flat precedence);
  • this draft eebdadd: retained floor 2 -> 1.

Making _set_seq_entry() monotonic would prevent the second decrease but not the earlier
OLD acceptance. A correct rolling design must keep legacy state authoritative and have
NEW dual-write during a compatibility phase, then finalize only after OLD workers drain;
otherwise the migration must require a non-rolling cutover. I will not mark this ready or
stack another partial fix on it.

@alisemut0711-dev

Copy link
Copy Markdown
Contributor

Independent verification, run against current main a782b0c — your base c0455ff is four
commits behind, though the diff still applies clean to it, and the full suite is
644 passed, 1 skipped with it applied on top.

I drove your OLD/NEW lifecycle as separate processes against separate checkouts — OLD
487caca, NEW a782b0c, and NEW a782b0c+this diff — no monkeypatching anywhere, so
every decision below is the shipped code's own.

Your HOLD is right, and it reproduces identically on both sides

The ordering from your self-review, step for step:

upstream a782b0c this diff
OLD appends 1, 2 then reaps flat {floor 2, gen 1} flat {floor 2, gen 1}
NEW splits shard {2, 1}, flat renamed away shard {2, 1}, flat renamed away
OLD recreates, no flat map present accepts seq 1, gen 1 accepts seq 1, gen 1

Byte-identical. The acceptance happens in a process that never reads a shard, so nothing on
the NEW side — component maxima included — is in the path to prevent it. Your conclusion that
this cannot be repaired after acceptance is confirmed here, not just plausible.

The second decrease you reported also reproduces exactly: after that OLD recreate, a real NEW
reap retains floor 2 -> 0 on upstream and 2 -> 1 with this diff. Both decrease.

The issue-body ordering, though, this diff does fix

Same probe, the ordering in #652:

upstream a782b0c this diff
after recovery split last_seq=1 gen=0 last_seq=3 gen=2
next NEW append seq=2 — 1 and 2 both reused seq=4, no reuse
generation across the split 2 -> 0 2 -> 2

So the whole-entry precedence really does drop a live generation and hand out a sequence a
previous conversation already used, and component-wise maxima really do stop it. Whatever
happens to this PR, that part measures as a genuine improvement.

Two things the diff changes that I don't think you intended

1. t is dropped on the split's merge path. _seq_entry returns a comprehension over
exactly ("floor", "gen"), so every other key in the winning entry is discarded.
{**entries, **shard} kept whole entries; this keeps two fields:

before split : {'floor': 0, 'gen': 1, 't': 1788358358}
after  split : {'floor': 1, 'gen': 1}

_set_seq_entry is the only writer of t, and its docstring says why the field is there —
"so that reclaiming entries for rooms long gone — the half of #489 this change does not do —
needs no second migration to date what it finds." Undated entries are exactly what that
sentence is trying to avoid. Nothing reads t today, so no behaviour breaks now; the cost
lands on the deferred half of #489, which is the one thing the field was added for. I hit it
on both split paths — the first split and the recovery split — since both go through the same
loop.

2. The read path now consults both files unconditionally. _seq_field is on every room
read: read_messages asks for the generation, last_seq asks for the floor. Counting real
_read_seq_state calls in the permanent post-migration steady state — shard entry present,
flat map absent:

upstream:  _seq_field(gen) opens 1, _seq_field(floor) opens 1  -> 2 per room read
this diff: _seq_field(gen) opens 2, _seq_field(floor) opens 2  -> 4 per room read

The generator for item in (room, "") is consumed by _seq_entry's max() over both
entries, so the flat-map open is attempted whether or not the shard answered. Upstream's
docstring is precise about that cost being conditional — the pre-shard map is read "only when
the shard has no entry, which after _split_seq_state has run is one failed open and no
parse." After this change it is one failed open per field per read, forever, on the path
#489 was opened to make cheap. If the reconcile stays, it probably belongs where the split
already holds the lock rather than on every request.

I have not filed anything against #652 myself — one issue, one PR, and this is yours. Posting
the runs in case the numbers are useful, whichever way you take it.

Copy link
Copy Markdown
Contributor Author

Thanks @alisemut0711-dev for independently reproducing both orderings and for confirming both the real improvement and the remaining OLD-recreate blocker. Good catches on the dropped t metadata and the permanent extra flat-map read as well. I’ll keep #653 in draft/HOLD pending a decision on the mixed-version migration boundary.

@WIZARDspace

Copy link
Copy Markdown
Contributor

The design boundary this is holding for is already asserted in the code, which makes the open question narrower than it looks.

_split_seq_state's docstring, on the "a map that came back" branch (src/store.py:1517-1522, 674c2aa):

The backup exists, so this map was written after a split had already consumed and renamed the original — which only an old worker still running the pre-shard code does, during a rolling upgrade. Its entry is then the newer fact and the shard's is stale, so the map wins. Getting this backwards silently drops that worker's reap or create: the room's floor regresses and cursors past it miss messages, or a generation bump is lost and a stateful reader is told nothing changed.

That branch has no other purpose. The whole-entry flat precedence you and @alisemut0711-dev measured regressing the floor is not an unconsidered scenario — it is the deliberate handling of exactly the window #652 exercises, written to prevent the very regression it now causes when both sides write between passes. The .pre-shard rename is committed to the same premise: it is kept "never deleted" so "a downgrade puts the old code back in front of a map it still understands" (src/store.py:1528-1531).

So "an explicit non-rolling migration boundary" would not be settling something undefined. It would retire a guarantee store.py currently makes in writing, and drop the documented downgrade path with it. That is a decision someone can actually take; "what is the migration boundary" is not.

The window is also wider than a startup step, which I have not seen stated here. The split does not run at boot. It rides _reap (src/store.py:1695 — "once in the life of a store; a no-op every pass after"), and _reap returns early unless REAP_EVERY has elapsed (src/store.py:1643, REAP_EVERY = 300 at :267). Two consequences:

  • NEW's initial split can land up to ~5 minutes after NEW starts serving, so it happens inside the overlap rather than before it.
  • The throttle marker is root / ".reaped" — a file in the shared store root, not per-process state. An OLD worker that reaps resets the interval for NEW as well, so OLD activity can push NEW's split later still.

Your ordering — OLD reaps, NEW splits, OLD recreates — is therefore not a narrow interleaving. Each step is a separate throttled pass with minutes of room between them, which is consistent with how cleanly it reproduces on both sides.

I am not proposing a fix on top of your HOLD; you are right that a partial one makes this worse, and the two reproductions already establish the behaviour. This is only to point at the smaller question underneath: honour the rolling-upgrade guarantee _split_seq_state already documents, or retire it deliberately and say so in the same place.

sv commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Triage: closing as a duplicate of #708.

Both PRs fix #652 with the same insight: during a rolling upgrade an old worker can update the legacy seq map while a new worker updates the shard, and the two updates need not be the same transition — one may advance floor while the other bumps gen. Picking a whole entry based on which file exists therefore preserves one field and regresses the other. Both replace that with an independent per-field high-water merge, on the grounds that both fields are monotonic.

Keeping #708: this PR is still a draft, and #708 is a finished, non-draft change of the same shape that merges cleanly against main.

Credit to @almondous for filing #652 and reaching the fix first — per CONTRIBUTING's "Overlapping work", the useful next step is to review #708 rather than finish a parallel branch. Reopen if you would rather carry this one and a maintainer agrees; the two differ mainly in whether the merge helper takes varargs entries or one entry at a time.


Generated by Claude Code

@sv sv closed this Sep 6, 2026
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.

4 participants