Skip to content

CBG-5555 better fix for activeOnly+limit#8420

Open
torcolvin wants to merge 5 commits into
mainfrom
CBG-5555
Open

CBG-5555 better fix for activeOnly+limit#8420
torcolvin wants to merge 5 commits into
mainfrom
CBG-5555

Conversation

@torcolvin

@torcolvin torcolvin commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

CBG-5555 better fix for activeOnly+limit

This is a reimplementation of #8144 CBG-5262 without a regression that occured:

  • The channel cache does not contain all the requested sequence for a given channel.
  • The backfill N1QL query to find entries has to be paginated. The default pagination is 5000.
  • Some of the returned documents ARE in the channel cache and appended to the paginated entries.

Approach:

  • Determine if we have reached the end of the query before appending the results:
    ** If not active_only, we have always received the full backfill
    ** If active_only and the number of results is less than limit, we have reached the end of the query backfill
    ** If active_only and the number of results or the sequence value expected by pagination is equal to the last row (last entry is on pagination boundary)
  • Separate concatenation of results from query and cache into active only and non active only to make the diff easy to read despite small duplication
    ** If activeOnly, only append cache results if the query has reached the start of the cached results and is less than the value of limit.
    ** If not active only, preserve existing behavior

Pre-review checklist

  • Removed debug logging (fmt.Print, log.Print, ...)
  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Dependencies (if applicable)

  • Link upstream PRs
  • Update Go module dependencies when merged

Integration Tests

Copilot AI review requested due to automatic review settings July 6, 2026 02:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a pagination correctness issue in Sync Gateway’s channel cache path when ActiveOnly=true is used together with Limit, ensuring the changes feed doesn’t prematurely append cached results before the query has reached the cache boundary.

Changes:

  • Update singleChannelCacheImpl.GetChanges to only append cache results for ActiveOnly+Limit when the query has reached the cache boundary.
  • Add new regression subtests to cover “cache populated + query pagination” scenarios (with and without a request limit).
  • Add a fuzz test to validate ActiveOnly change enumeration properties across randomized cache/query/limit configurations.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
db/channel_cache_single.go Adds a guard (queryReachedCache) to prevent prematurely appending cache results during ActiveOnly+Limit pagination.
db/channel_cache_test.go Adds regression tests and a fuzz test to exercise ActiveOnly pagination across query batches and cache boundary behavior.

Comment thread db/channel_cache_test.go
Comment thread db/changes_view.go Outdated
// If we've reached limit before exhausting the range up to endSeq, we're done, but the
// range beyond the last returned entry hasn't been fully scanned.
if limit != 0 && activeEntryCount >= limit {
reachedEnd = endSeq > 0 && highSeq >= endSeq

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Here highSeq is the highest sequence returned by the query, and endSeq is the upper boundary of the query boundary, correct? There's no guarantee that there's a sequence in this channel (and so in the query results) greater than or equal to endSeq, so it seems like there will be many cases where reachedEnd will be incorrectly set to false here?

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.

3 participants