[IMPROVED] Index stream sources to avoid backward scans - #8282
[IMPROVED] Index stream sources to avoid backward scans#8282MauriceVanVeen wants to merge 4 commits into
Conversation
|
Claude finished @MauriceVanVeen's task in 9m 36s —— View job PR Review
Commit 1 — Combined TTL/schedule recovery scanThe refactor is clean. Replacing two independent linear scans with a single unified pass is a straightforward correctness win. The new One semantic change to be aware of: I/O errors reading the TTL or scheduling state files during Commit 2 — Stream source indexingThe design is sound. Indexing No correctness bugs found. Issues flagged inline are all low/minor severity:
The performance improvement is well-motivated: the old backward scan's cost was proportional to the depth of the stream, while the new index scales only with the number of distinct sources. For large streams this can be orders of magnitude cheaper. |
580e20c to
74fe1f5
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 74fe1f50ff
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
74fe1f5 to
4ad9160
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cba650b81e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
cba650b to
6d489ae
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6d489ae5bd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
6d489ae to
11fbc5f
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 11fbc5f828
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
11fbc5f to
27f6d14
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 27f6d1415e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@claude, re-review. |
|
Claude finished @MauriceVanVeen's task in 7m 38s —— View job Re-Review
Status of Previous Review Findings
Evaluation of Codex P2 Findings"Apply persisted source positions before empty-stream return" (stream.go) — ✅ Addressed. In "Store source index outside purged message directory" (filestore.go) — ✅ Not an issue. "Point source-index tests at the store root" (filestore_test.go) — ✅ Not an issue. All test assertions use "Validate source headers before indexing them" (filestore.go) — ✅ Addressed by commit 3. Both "Include root source index in snapshots" (filestore.go:12041) — Author acknowledged: state can be rebuilt and the snapshot format is being replaced. Accepted. New Finding: Test Regression in
|
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
27f6d14 to
a13b5e8
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a13b5e808b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@claude, brief re-review. |
|
Claude finished @MauriceVanVeen's task in 2m 56s —— View job Brief Re-Review
Status of All Previous Findings
New Codex Finding: Old-style Source Positions (2026-06-10)The finding and author response both hold up on inspection. For a pre-2.10 upgraded stream:
The "gap" Codex describes (purge before first new message → restart starts from seq 1) is identical to pre-PR behaviour — the old code also had no index and would find nothing on an empty stream. The new code adds no regression here; it simply defers index creation until new messages actually land. Author's response is correct. AssessmentThe only actionable finding from the previous round — the hollow test — has been fixed correctly. The remaining low/minor items are all style/defense-in-depth and do not affect correctness. The new Codex concern is a pre-existing behaviour, not a regression. The PR is in good shape. |
The first commit refactors
fs.recoverTTLState()andfs.recoverMsgSchedulingState(), since if both required recovering that would result in two separate linear scans, instead of one linear scan reused for recovering both.The second commit introduces indexing for stream sources, which will ensure that backward scanning for pre-existing stream sources is eliminated.
Nats-Stream-Sourceis inspected and the source key is mapped to the sequence that's sourced.mset.startingSequenceForSources()to use the index, and only fall back to backward scanning remaining sources if necessary.sources.dbwhen using file-based streams, similar to thethw.dbandsched.dbfiles.The approach of having an index that scales by sources versus that scans the stream by messages results in a tremendous performance improvement, especially as streams get larger and the backward scan would otherwise need to reach way further back.
The
sources.dbfile is stored at the stream's root, as opposed to in themsgsdirectory, since it should not be removed if the stream is purged:streams/<stream>/ ├── meta.inf, meta.sum,meta.key + ├── sources.db ├── msgs/ │ ├── 1.blk, 2.blk, … │ ├── index.db, thw.db, sched.db └── obs/ └── <consumer>/…The third commit adds a check preventing users from publishing messages with the
Nats-Stream-Sourceheader directly. And the fourth commit fixes a test that was (incorrectly) checking pre-2.10 coverage.