Skip to content

[FIXED] Start time binary search is delete-aware#7751

Merged
neilalexander merged 1 commit intomainfrom
maurice/fs-start-search
Jan 22, 2026
Merged

[FIXED] Start time binary search is delete-aware#7751
neilalexander merged 1 commit intomainfrom
maurice/fs-start-search

Conversation

@MauriceVanVeen
Copy link
Member

The filestore's GetSeqFromTime which is used to determine the starting sequence for consumers and msg/direct/batch get requests with a start time used a binary search that wasn't aware of deletes. This lead to various incorrect results if the binary search came across such a delete. This PR fixes that by making the search aware of deletes and essentially skipping over these gaps and moving the ranges accordingly.

Signed-off-by: Maurice van Veen github@mauricevanveen.com

@MauriceVanVeen MauriceVanVeen requested a review from a team as a code owner January 21, 2026 17:09
@jnmoyne
Copy link
Contributor

jnmoyne commented Jan 21, 2026

LGTM for me, the issue is not reproducible in my application.

wallyqs pushed a commit to wallyqs/nats-server that referenced this pull request Jan 21, 2026
These tests demonstrate that:

1. memstore has the same binary search bug as filestore (PR nats-io#7751 fixes filestore)
   - TestMemStoreGetSeqFromTimeBinarySearchBug shows memstore returns wrong
     sequence when interior deletes exist (returns 7, expected 3)

2. Extended test coverage for GetSeqFromTime with various delete patterns:
   - Large gaps (more deleted than remaining)
   - Single message remaining
   - Consecutive blocks with deletions
   - All but first deleted
   - All but last deleted
   - Alternating deletes (maximum fragmentation)

Test results against current codebase:
- Both filestore and memstore fail on multiple patterns
- These tests can be used to verify the fix in PR nats-io#7751 and
  guide a similar fix for memstore
wallyqs added a commit to wallyqs/nats-server that referenced this pull request Jan 21, 2026
These tests demonstrate that:

1. memstore has the same binary search bug as filestore (PR nats-io#7751 fixes filestore)
   - TestMemStoreGetSeqFromTimeBinarySearchBug shows memstore returns wrong
     sequence when interior deletes exist (returns 7, expected 3)

2. Extended test coverage for GetSeqFromTime with various delete patterns:
   - Large gaps (more deleted than remaining)
   - Single message remaining
   - Consecutive blocks with deletions
   - All but first deleted
   - All but last deleted
   - Alternating deletes (maximum fragmentation)

Test results against current codebase:
- Both filestore and memstore fail on multiple patterns
- These tests can be used to verify the fix in PR nats-io#7751 and
  guide a similar fix for memstore

Signed-off-by: Waldemar Quevedo <wally@nats.io>
wallyqs pushed a commit to wallyqs/nats-server that referenced this pull request Jan 21, 2026
Apply the same fix from PR nats-io#7751 (filestore) to memstore.

The issue: sort.Search was used with a function that returns false
for deleted messages, violating the monotonicity requirement of
binary search. This caused incorrect results when searching for
timestamps in streams with interior deletes.

The fix: Use a custom binary search that skips over deleted messages
(gaps) and properly adjusts the search bounds when encountering
consecutive deletions.

Signed-off-by: Claude <claude@anthropic.com>
Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, think we may need similar fix in memstore.

@MauriceVanVeen
Copy link
Member Author

think we may need similar fix in memstore

The added test already confirms this is no issue in the memstore. It can use a simple binary search as-is, since its messages are only stored in ms.msgs map, which is already aware of deletes in that way.

@MauriceVanVeen MauriceVanVeen marked this pull request as draft January 21, 2026 19:33
@MauriceVanVeen
Copy link
Member Author

Although.. there might be different issues that are not captured by the new (or pre-existing) tests. Will check again tomorrow.

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
@MauriceVanVeen MauriceVanVeen force-pushed the maurice/fs-start-search branch from af1f69e to e46dc71 Compare January 22, 2026 08:13
@MauriceVanVeen MauriceVanVeen changed the title [FIXED] Filestore start time binary search is delete-aware [FIXED] Start time binary search is delete-aware Jan 22, 2026
@MauriceVanVeen MauriceVanVeen marked this pull request as ready for review January 22, 2026 08:47
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@neilalexander neilalexander merged commit 4aa275e into main Jan 22, 2026
89 of 92 checks passed
@neilalexander neilalexander deleted the maurice/fs-start-search branch January 22, 2026 12:06
neilalexander added a commit that referenced this pull request Jan 26, 2026
Includes the following:

- #7750
- #7752
- #7751
- #7753
- #7758
- #7759
- #7763

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit that referenced this pull request Jan 26, 2026
Includes the following:

- #7750
- #7752
- #7751
- #7753
- #7759
- #7763

Signed-off-by: Neil Twigg <neil@nats.io>
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