Skip to content

Fix #297: OLC B+Tree concurrent insert/remove key loss - #304

Merged
nockawa merged 1 commit into
mainfrom
fix/297-olc-concurrent-insert-key-loss
May 5, 2026
Merged

nockawa merged 1 commit into
mainfrom
fix/297-olc-concurrent-insert-key-loss

Conversation

@nockawa

@nockawa nockawa commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #297 — OLC B+Tree concurrent inserts/removes silently losing keys under heavy parallel CPU pressure.

Engine fixes (src/Typhon.Engine/Data/Index/)

  • BTree.Remove.cs — Option D stale-separator detection in RemoveIterative: after Phase 1.5A version validation, if node.Next is valid and key ≥ HighKey, abort the write lock and restart. Stale-head/tail OLC checks in begin/end fast paths and pessimistic begin-fast-path. Snapshot-once on _reverseLinkList updates.
  • BTree.Insert.cs — CAS-init lock around the initial PushLast (prevents unsynchronized writes when concurrent threads observe a just-published _rootChunkId); root-split lock around newRoot writes (prevents descent through a partially-constructed new root); defensive _reverseLinkList null guard in tail-cache update.
  • BTree.cs — B-link follow uses the actual leaf last key instead of stale HighKey.
  • BTree.NodeWrapper.cs — SpillLeftSortInvariantHolds / SpillRightSortInvariantHolds for spill-path correctness asserts.
  • String64BTree.cs — explicit HighKey[64] field + GetHighKey / SetHighKey to maintain node.HighKey == nextNode.firstKey for String64 keys (parity with Index64Chunk / L16/L32/L64).
  • OlcDescentTrace.cs (new) — minimal diagnostic hooks (RecordStep, OnInvalidChunkId, OnRemoveNotFound). JIT short-circuits when null; near-zero production cost.

Test infrastructure

  • OlcBTreeRaceStressTests.cs — new stress fixture, 5 scenarios under heavy parallel CPU pressure. HANG-safe iter cleanup, file-based progress logging, descent-ring dump on OnInvalidChunkId, OnRemoveNotFound capture.

Results

Scenario Baseline After
Remove_Disjoint silent key loss 7.4% 0.00%
AV crashes during stress recurring eliminated
Full unit test suite flaky under load 3748/3748 stable

Known residual

Add_* scenarios show a ~0.08–0.40% silent-loss rate, suspected origin in HandlePromotedInsert spill paths for internal nodes. Out of scope for this PR — recommend follow-up issue once a deterministic repro lands.

Test plan

  • 100 standalone runs of previously-flaky tests pass
  • 10 back-to-back full-suite runs clean for Remove_* and AV regressions
  • Full suite: 3748/3748

🤖 Generated with Claude Code

@nockawa
nockawa merged commit 6901667 into main May 5, 2026
5 checks passed
@nockawa
nockawa deleted the fix/297-olc-concurrent-insert-key-loss branch May 5, 2026 14:13
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.

OLC: concurrent inserts can lose keys under contention without throwing

1 participant