Skip to content

IndexedDB Recovery for Limbo documents #3039

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 4 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 13 additions & 9 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,6 @@ export class SyncEngine implements RemoteSyncer {
const limboResolution = this.activeLimboResolutionsByTarget.get(targetId);
const limboKey = limboResolution && limboResolution.key;
if (limboKey) {
// Since this query failed, we won't want to manually unlisten to it.
// So go ahead and remove it from bookkeeping.
this.activeLimboTargetsByKey = this.activeLimboTargetsByKey.remove(
limboKey
);
this.activeLimboResolutionsByTarget.delete(targetId);
this.pumpEnqueuedLimboResolutions();

// TODO(klimt): We really only should do the following on permission
// denied errors, but we don't have the cause code here.

Expand All @@ -513,7 +505,19 @@ export class SyncEngine implements RemoteSyncer {
documentUpdates,
resolvedLimboDocuments
);
return this.applyRemoteEvent(event);

await this.applyRemoteEvent(event);

// Since this query failed, we won't want to manually unlisten to it.
Copy link
Contributor

Choose a reason for hiding this comment

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

It took me a moment to think I understand why this ordering is better. If I'm understanding correctly, the idea is to avoid removing the limbo target until we successfully apply the remote event because if we fail, we'll shut down the listen stream, and then when we resume we'll resend the limbo targets, right?

A comment to this effect could help future readers understand why the watch start/stop lifecycle makes this a reasonable choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. If we can't update the cache, it is better for the documents to stay in limbo rather than go out of limbo temporarily. I modified the existing comment to say:

// Since this query failed, we won't want to manually unlisten to it.
// We only remove it from bookkeeping after we successfully applied the
// RemoteEvent. If `applyRemoteEvent()` throws, we want to re-listen to 
// this query when the RemoteStore restarts the Watch stream, which should 
// re-trigger the target failure.

// We only remove it from bookkeeping after we successfully applied the
// RemoteEvent. If `applyRemoteEvent()` throws, we want to re-listen to
// this query when the RemoteStore restarts the Watch stream, which should
// re-trigger the target failure.
this.activeLimboTargetsByKey = this.activeLimboTargetsByKey.remove(
limboKey
);
this.activeLimboResolutionsByTarget.delete(targetId);
this.pumpEnqueuedLimboResolutions();
} else {
await this.localStore
.releaseTarget(targetId, /* keepPersistedTargetData */ false)
Expand Down
99 changes: 98 additions & 1 deletion packages/firestore/test/unit/specs/recovery_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { client, spec } from './spec_builder';
import { TimerId } from '../../../src/util/async_queue';
import { Query } from '../../../src/core/query';
import { Code } from '../../../src/util/error';
import { doc, path } from '../../util/helpers';
import { doc, filter, path } from '../../util/helpers';
import { RpcError } from './spec_rpc_error';

describeSpec('Persistence Recovery', ['no-ios', 'no-android'], () => {
Expand Down Expand Up @@ -309,4 +309,101 @@ describeSpec('Persistence Recovery', ['no-ios', 'no-android'], () => {
);
}
);

specTest(
'Recovers when Limbo acknowledgement cannot be persisted',
['durable-persistence'],
() => {
const fullQuery = Query.atPath(path('collection'));
const filteredQuery = Query.atPath(path('collection')).addFilter(
filter('included', '==', true)
);
const doc1a = doc('collection/key1', 1, { included: true });
const doc1b = doc('collection/key1', 1500, { included: false });
const limboQuery = Query.atPath(doc1a.key.path);
return spec()
.userListens(fullQuery)
.watchAcksFull(fullQuery, 1000, doc1a)
.expectEvents(fullQuery, {
added: [doc1a]
})
.userUnlistens(fullQuery)
.userListens(filteredQuery)
.expectEvents(filteredQuery, {
added: [doc1a],
fromCache: true
})
.watchAcksFull(filteredQuery, 2000)
.expectLimboDocs(doc1a.key)
.failDatabaseTransactions({ 'Get last remote snapshot version': true })
.watchAcksFull(limboQuery, 3000, doc1b)
.expectActiveTargets()
.recoverDatabase()
.runTimer(TimerId.AsyncQueueRetry)
.expectActiveTargets(
{
query: filteredQuery,
resumeToken: 'resume-token-2000'
},
{ query: limboQuery }
)
.watchAcksFull(filteredQuery, 4000)
.watchAcksFull(limboQuery, 4000, doc1b)
.expectLimboDocs()
.expectEvents(filteredQuery, {
removed: [doc1a]
});
}
);

specTest(
'Recovers when Limbo rejection cannot be persisted',
['durable-persistence'],
() => {
const fullQuery = Query.atPath(path('collection'));
const filteredQuery = Query.atPath(path('collection')).addFilter(
filter('included', '==', true)
);
const doc1 = doc('collection/key1', 1, { included: true });
const limboQuery = Query.atPath(doc1.key.path);
return spec()
.userListens(fullQuery)
.watchAcksFull(fullQuery, 1000, doc1)
.expectEvents(fullQuery, {
added: [doc1]
})
.userUnlistens(fullQuery)
.userListens(filteredQuery)
.expectEvents(filteredQuery, {
added: [doc1],
fromCache: true
})
.watchAcksFull(filteredQuery, 2000)
.expectLimboDocs(doc1.key)
.failDatabaseTransactions({
'Apply remote event': true,
'Get last remote snapshot version': true
})
.watchRemoves(
limboQuery,
new RpcError(Code.PERMISSION_DENIED, 'Test error')
)
.expectActiveTargets()
.recoverDatabase()
.runTimer(TimerId.AsyncQueueRetry)
.expectActiveTargets(
{ query: filteredQuery, resumeToken: 'resume-token-2000' },
{ query: limboQuery }
)
.watchAcksFull(filteredQuery, 3000)
.watchRemoves(
limboQuery,
new RpcError(Code.PERMISSION_DENIED, 'Test error')
)
.expectLimboDocs()
.expectEvents(filteredQuery, {
removed: [doc1]
});
}
);
});
5 changes: 5 additions & 0 deletions packages/firestore/test/unit/specs/spec_test_components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ export class MockConnection implements Connection {
/** A Deferred that is resolved once watch opens. */
watchOpen = new Deferred<void>();

/** Whether the Watch stream is open. */
isWatchOpen = false;

invokeRPC<Req>(rpcName: string, request: Req): never {
throw new Error('Not implemented!');
}
Expand Down Expand Up @@ -263,6 +266,7 @@ export class MockConnection implements Connection {
this.watchOpen = new Deferred<void>();
this.watchStream!.callOnClose(err);
this.watchStream = null;
this.isWatchOpen = false;
}

openStream<Req, Resp>(
Expand Down Expand Up @@ -351,6 +355,7 @@ export class MockConnection implements Connection {
this.queue.enqueueAndForget(async () => {
if (this.watchStream === watchStream) {
watchStream.callOnOpen();
this.isWatchOpen = true;
this.watchOpen.resolve();
}
});
Expand Down
28 changes: 16 additions & 12 deletions packages/firestore/test/unit/specs/spec_test_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,18 +847,22 @@ abstract class TestRunner {

private validateActiveLimboDocs(): void {
let actualLimboDocs = this.syncEngine.activeLimboDocumentResolutions();
// Validate that each active limbo doc has an expected active target
actualLimboDocs.forEach((key, targetId) => {
const targetIds = new Array(this.expectedActiveTargets.keys()).map(
n => '' + n
);
expect(this.expectedActiveTargets.has(targetId)).to.equal(
true,
`Found limbo doc ${key.toString()}, but its target ID ${targetId} ` +
`was not in the set of expected active target IDs ` +
`(${targetIds.join(', ')})`
);
});

if (this.connection.isWatchOpen) {
// Validate that each active limbo doc has an expected active target
actualLimboDocs.forEach((key, targetId) => {
const targetIds = new Array(this.expectedActiveTargets.keys()).map(
n => '' + n
);
expect(this.expectedActiveTargets.has(targetId)).to.equal(
true,
`Found limbo doc ${key.toString()}, but its target ID ${targetId} ` +
`was not in the set of expected active target IDs ` +
`(${targetIds.join(', ')})`
);
});
}

for (const expectedLimboDoc of this.expectedActiveLimboDocs) {
expect(actualLimboDocs.get(expectedLimboDoc)).to.not.equal(
null,
Expand Down