Skip to content

Use Read-Time Index for Multi-Tab #2128

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
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
34 changes: 10 additions & 24 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import { Deferred } from '../util/promise';
import { SortedMap } from '../util/sorted_map';

import { ignoreIfPrimaryLeaseLoss } from '../local/indexeddb_persistence';
import { isDocumentChangeMissingError } from '../local/indexeddb_remote_document_cache';
import { ClientId, SharedClientState } from '../local/shared_client_state';
import {
QueryTargetState,
Expand Down Expand Up @@ -983,29 +982,16 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
switch (state) {
case 'current':
case 'not-current': {
try {
const changes = await this.localStore.getNewDocumentChanges();
const synthesizedRemoteEvent = RemoteEvent.createSynthesizedRemoteEventForCurrentChange(
targetId,
state === 'current'
);
await this.emitNewSnapsAndNotifyLocalStore(
changes,
synthesizedRemoteEvent
);
break;
// Catch errors thrown by getNewDocumentchanges().
} catch (error) {
if (isDocumentChangeMissingError(error)) {
const activeTargets: TargetId[] = [];
objUtils.forEachNumber(this.queryViewsByTarget, target =>
activeTargets.push(target)
);
await this.synchronizeQueryViewsAndRaiseSnapshots(activeTargets);
} else {
throw error;
}
}
const changes = await this.localStore.getNewDocumentChanges();
const synthesizedRemoteEvent = RemoteEvent.createSynthesizedRemoteEventForCurrentChange(
targetId,
state === 'current'
);
await this.emitNewSnapsAndNotifyLocalStore(
changes,
synthesizedRemoteEvent
);
break;
}
case 'rejected': {
const queryView = this.queryViewsByTarget[targetId];
Expand Down
65 changes: 19 additions & 46 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ const LOG_TAG = 'IndexedDbPersistence';

/**
* Oldest acceptable age in milliseconds for client metadata before the client
* is considered inactive and its associated data (such as the remote document
* cache changelog) is garbage collected.
* is considered inactive and its associated data is garbage collected.
*/
const MAX_CLIENT_AGE_MS = 30 * 60 * 1000; // 30 minutes

Expand Down Expand Up @@ -259,7 +258,7 @@ export class IndexedDbPersistence implements Persistence {
/** The client metadata refresh task. */
private clientMetadataRefresher: CancelablePromise<void> | null = null;

/** The last time we garbage collected the Remote Document Changelog. */
/** The last time we garbage collected the client metadata object store. */
private lastGarbageCollectionTime = Number.NEGATIVE_INFINITY;

/** Whether to allow shared multi-tab access to the persistence layer. */
Expand Down Expand Up @@ -304,8 +303,7 @@ export class IndexedDbPersistence implements Persistence {
this.indexManager = new IndexedDbIndexManager();
this.remoteDocumentCache = new IndexedDbRemoteDocumentCache(
this.serializer,
this.indexManager,
/*keepDocumentChangeLog=*/ this.allowTabSynchronization
this.indexManager
);
if (platform.window && platform.window.localStorage) {
this.window = platform.window;
Expand Down Expand Up @@ -430,8 +428,7 @@ export class IndexedDbPersistence implements Persistence {
this.clientId,
Date.now(),
this.networkEnabled,
this.inForeground,
this.remoteDocumentCache.lastProcessedDocumentChangeId
this.inForeground
)
)
.next(() => {
Expand Down Expand Up @@ -506,46 +503,22 @@ export class IndexedDbPersistence implements Persistence {
DbClientMetadata
>(txn, DbClientMetadata.store);

return metadataStore
.loadAll()
.next(existingClients => {
activeClients = this.filterActiveClients(
existingClients,
MAX_CLIENT_AGE_MS
);
inactiveClients = existingClients.filter(
client => activeClients.indexOf(client) === -1
);
})
.next(() =>
// Delete metadata for clients that are no longer considered active.
PersistencePromise.forEach(
inactiveClients,
(inactiveClient: DbClientMetadata) =>
metadataStore.delete(inactiveClient.clientId)
)
)
.next(() => {
// Retrieve the minimum change ID from the set of active clients.

// The primary client doesn't read from the document change log,
// and hence we exclude it when we determine the minimum
// `lastProcessedDocumentChangeId`.
activeClients = activeClients.filter(
client => client.clientId !== this.clientId
);
return metadataStore.loadAll().next(existingClients => {
activeClients = this.filterActiveClients(
existingClients,
MAX_CLIENT_AGE_MS
);
inactiveClients = existingClients.filter(
client => activeClients.indexOf(client) === -1
);

if (activeClients.length > 0) {
const processedChangeIds = activeClients.map(
client => client.lastProcessedDocumentChangeId || 0
);
const oldestChangeId = Math.min(...processedChangeIds);
return this.remoteDocumentCache.removeDocumentChangesThroughChangeId(
txn,
oldestChangeId
);
}
});
// Delete metadata for clients that are no longer considered active.
return PersistencePromise.forEach(
inactiveClients,
(inactiveClient: DbClientMetadata) =>
metadataStore.delete(inactiveClient.clientId)
);
});
}
);

Expand Down
Loading