Skip to content

Fix docs unnecessarily undergo limbo resolution while resuming query bug #7740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/firestore/src/core/sync_engine_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,10 +1081,13 @@ async function applyDocChanges(

const targetChange =
remoteEvent && remoteEvent.targetChanges.get(queryView.targetId);
const waitForRequeryResult =
remoteEvent && remoteEvent.targetMismatches.get(queryView.targetId) != null;
const viewChange = queryView.view.applyChanges(
viewDocChanges,
/* updateLimboDocuments= */ syncEngineImpl.isPrimaryClient,
targetChange
targetChange,
waitForRequeryResult
);
updateTrackedLimbos(
syncEngineImpl,
Expand Down
21 changes: 16 additions & 5 deletions packages/firestore/src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,16 @@ export class View {
* this change.
* @param targetChange - A target change to apply for computing limbo docs and
* sync state.
* @param waitForRequeryResult - Whether the target is pending to run a full
* re-query due to existence filter mismatch.
* @returns A new ViewChange with the given docs, changes, and sync state.
*/
// PORTING NOTE: The iOS/Android clients always compute limbo document changes.
applyChanges(
docChanges: ViewDocumentChanges,
updateLimboDocuments: boolean,
targetChange?: TargetChange
targetChange?: TargetChange,
waitForRequeryResult?: boolean
): ViewChange {
debugAssert(
!docChanges.needsRefill,
Expand All @@ -301,9 +304,15 @@ export class View {

this.applyTargetChange(targetChange);
const limboChanges = updateLimboDocuments
? this.updateLimboDocuments()
? this.updateLimboDocuments(waitForRequeryResult ?? false)
: [];
const synced = this.limboDocuments.size === 0 && this.current;

// We are at synced state if there is no limbo docs are waiting to be resolved, view is current
// with the backend, and the query is not pending for full re-query result due to existence
// filter mismatch.
const synced =
this.limboDocuments.size === 0 && this.current && !waitForRequeryResult;

const newSyncState = synced ? SyncState.Synced : SyncState.Local;
const syncStateChanged = newSyncState !== this.syncState;
this.syncState = newSyncState;
Expand Down Expand Up @@ -403,9 +412,11 @@ export class View {
}
}

private updateLimboDocuments(): LimboDocumentChange[] {
private updateLimboDocuments(
waitForRequeryResult: boolean
): LimboDocumentChange[] {
// We can only determine limbo documents when we're in-sync with the server.
if (!this.current) {
if (waitForRequeryResult || !this.current) {
return [];
}

Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/test/unit/specs/limbo_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ describeSpec('Limbo Documents:', [], () => {
// to just send the docBs with an existence filter with a count of 3.
.watchSends({ affects: [query1] }, docB1, docB2, docB3)
.watchFilters([query1], [docB1.key, docB2.key, docB3.key])
.watchCurrents(query1, 'resume-token-1001')
.watchSnapshots(1001)
.expectEvents(query1, {
added: [docB1, docB2, docB3],
Expand Down Expand Up @@ -968,6 +969,7 @@ describeSpec('Limbo Documents:', [], () => {
[docB1.key, docB2.key, docB3.key],
bloomFilterProto
)
.watchCurrents(query1, 'resume-token-1001')
.watchSnapshots(1001)
.expectEvents(query1, {
added: [docB1, docB2, docB3],
Expand All @@ -978,8 +980,6 @@ describeSpec('Limbo Documents:', [], () => {
// existence filter mismatch. Bloom filter checks membership of the
// docs, and filters out docAs, while docBs returns true. Number of
// existing docs matches the expected count, so skip the re-query.
.watchCurrents(query1, 'resume-token-1002')
.watchSnapshots(1002)
// The docAs are now in limbo; the client begins limbo resolution.
.expectLimboDocs(docA1.key, docA2.key)
.expectEnqueuedLimboDocs(docA3.key)
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/test/unit/specs/limit_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ describeSpec('Limits:', [], () => {
// out of sync.
.watchSends({ affects: [limitQuery] }, secondDocument)
.watchFilters([limitQuery], [secondDocument.key])
.watchCurrents(limitQuery, 'resume-token-1004')
.watchSnapshots(1004)
.expectActiveTargets({
query: limitQuery,
Expand Down