Skip to content

Implement Eager Reference Delegate for Memory Persistence, drop old GC #1256

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 12 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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
14 changes: 2 additions & 12 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

import { CredentialsProvider } from '../api/credentials';
import { User } from '../auth/user';
import { EagerGarbageCollector } from '../local/eager_garbage_collector';
import { GarbageCollector } from '../local/garbage_collector';
import { IndexedDbPersistence } from '../local/indexeddb_persistence';
import { LocalStore } from '../local/local_store';
import { MemoryPersistence } from '../local/memory_persistence';
import { NoOpGarbageCollector } from '../local/no_op_garbage_collector';
import { Persistence } from '../local/persistence';
import {
DocumentKeySet,
Expand Down Expand Up @@ -83,7 +80,6 @@ export class FirestoreClient {
// with the types rather than littering the code with '!' or unnecessary
// undefined checks.
private eventMgr: EventManager;
private garbageCollector: GarbageCollector;
private persistence: Persistence;
private localStore: LocalStore;
private remoteStore: RemoteStore;
Expand Down Expand Up @@ -292,7 +288,6 @@ export class FirestoreClient {

// TODO(http://b/33384523): For now we just disable garbage collection
// when persistence is enabled.
this.garbageCollector = new NoOpGarbageCollector();
const storagePrefix = IndexedDbPersistence.buildStoragePrefix(
this.databaseInfo
);
Expand Down Expand Up @@ -347,8 +342,7 @@ export class FirestoreClient {
* @returns A promise that will successfully resolve.
*/
private startMemoryPersistence(): Promise<void> {
this.garbageCollector = new EagerGarbageCollector();
this.persistence = new MemoryPersistence(this.clientId);
this.persistence = MemoryPersistence.createEagerPersistence(this.clientId);
this.sharedClientState = new MemorySharedClientState();
return Promise.resolve();
}
Expand All @@ -363,11 +357,7 @@ export class FirestoreClient {
return this.platform
.loadConnection(this.databaseInfo)
.then(async connection => {
this.localStore = new LocalStore(
this.persistence,
user,
this.garbageCollector
);
this.localStore = new LocalStore(this.persistence, user);
const serializer = this.platform.newSerializer(
this.databaseInfo.databaseId
);
Expand Down
9 changes: 1 addition & 8 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
this.remoteStore.unlisten(queryView.targetId);
return this.removeAndCleanupQuery(queryView);
})
.then(() => this.localStore.collectGarbage())
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
}
} else {
Expand Down Expand Up @@ -583,7 +582,6 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
// NOTE: Both these methods are no-ops for batches that originated from
// other clients.
this.processUserCallback(batchId, error ? error : null);

this.localStore.removeCachedMutationBatchMetadata(batchId);
} else {
fail(`Unknown batchState: ${batchState}`);
Expand Down Expand Up @@ -820,12 +818,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {

await Promise.all(queriesProcessed);
this.syncEngineListener!.onWatchChange(newSnaps);
this.localStore.notifyLocalViewChanges(docChangesInAllViews);
if (this.isPrimary) {
await this.localStore
.collectGarbage()
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
}
await this.localStore.notifyLocalViewChanges(docChangesInAllViews);
}

/**
Expand Down
103 changes: 0 additions & 103 deletions packages/firestore/src/local/eager_garbage_collector.ts

This file was deleted.

79 changes: 0 additions & 79 deletions packages/firestore/src/local/garbage_collector.ts

This file was deleted.

48 changes: 0 additions & 48 deletions packages/firestore/src/local/garbage_source.ts

This file was deleted.

26 changes: 9 additions & 17 deletions packages/firestore/src/local/indexeddb_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { primitiveComparator } from '../util/misc';
import { SortedSet } from '../util/sorted_set';

import * as EncodedResourcePath from './encoded_resource_path';
import { GarbageCollector } from './garbage_collector';
import {
IndexedDbPersistence,
IndexedDbTransaction
Expand All @@ -43,7 +42,7 @@ import {
} from './indexeddb_schema';
import { LocalSerializer } from './local_serializer';
import { MutationQueue } from './mutation_queue';
import { PersistenceTransaction } from './persistence';
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { SimpleDbStore, SimpleDbTransaction } from './simple_db';

Expand All @@ -63,15 +62,14 @@ export class IndexedDbMutationQueue implements MutationQueue {
// PORTING NOTE: Multi-tab only.
private documentKeysByBatchId = {} as { [batchId: number]: DocumentKeySet };

private garbageCollector: GarbageCollector | null = null;

constructor(
/**
* The normalized userId (e.g. null UID => "" userId) used to store /
* retrieve mutations.
*/
private userId: string,
private serializer: LocalSerializer
private serializer: LocalSerializer,
private readonly referenceDelegate: ReferenceDelegate
) {}

/**
Expand All @@ -81,15 +79,16 @@ export class IndexedDbMutationQueue implements MutationQueue {
*/
static forUser(
user: User,
serializer: LocalSerializer
serializer: LocalSerializer,
referenceDelegate: ReferenceDelegate
): IndexedDbMutationQueue {
// TODO(mcg): Figure out what constraints there are on userIDs
// In particular, are there any reserved characters? are empty ids allowed?
// For the moment store these together in the same mutations table assuming
// that empty userIDs aren't allowed.
assert(user.uid !== '', 'UserID must not be an empty string.');
const userId = user.isAuthenticated() ? user.uid! : '';
return new IndexedDbMutationQueue(userId, serializer);
return new IndexedDbMutationQueue(userId, serializer, referenceDelegate);
}

start(transaction: PersistenceTransaction): PersistencePromise<void> {
Expand Down Expand Up @@ -470,12 +469,9 @@ export class IndexedDbMutationQueue implements MutationQueue {
batch
).next(removedDocuments => {
this.removeCachedMutationKeys(batch.batchId);
if (this.garbageCollector !== null) {
for (const key of removedDocuments) {
// TODO(gsoltis): tell reference delegate that mutation was ack'd
this.garbageCollector.addPotentialGarbageKey(key);
}
}
return PersistencePromise.waitForEach(removedDocuments, key => {
return this.referenceDelegate.removeMutationReference(transaction, key);
Copy link
Contributor

Choose a reason for hiding this comment

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

This line seems to be missing from Android (at least in master). Is that still coming?

Copy link
Author

Choose a reason for hiding this comment

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

Yes. I have a few changes that I need to make to Android as a result of porting. This is one of them.

});
});
}

Expand Down Expand Up @@ -519,10 +515,6 @@ export class IndexedDbMutationQueue implements MutationQueue {
});
}

setGarbageCollector(gc: GarbageCollector | null): void {
this.garbageCollector = gc;
}

containsKey(
txn: PersistenceTransaction,
key: DocumentKey
Expand Down
Loading