Skip to content

Expose operation counts from Persistence layer #2164

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

Closed
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
4 changes: 3 additions & 1 deletion packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ export class FirestoreClient {
* @returns A promise that will successfully resolve.
*/
private startMemoryPersistence(): Promise<LruGarbageCollector | null> {
this.persistence = MemoryPersistence.createEagerPersistence(this.clientId);
this.persistence = MemoryPersistence.createEagerPersistence({
clientId: this.clientId
});
this.sharedClientState = new MemorySharedClientState();
return Promise.resolve(null);
}
Expand Down
40 changes: 33 additions & 7 deletions packages/firestore/src/local/indexeddb_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ import {
DbMutationQueueKey
} from './indexeddb_schema';
import { LocalSerializer } from './local_serializer';
import { MutationQueue } from './mutation_queue';
import { MUTATION_QUEUE_STATS_TAG, MutationQueue } from './mutation_queue';
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { SimpleDbStore, SimpleDbTransaction } from './simple_db';
import { StatsCollector } from './stats_collector';

/** A mutation queue for a specific user, backed by IndexedDB. */
export class IndexedDbMutationQueue implements MutationQueue {
Expand All @@ -73,7 +74,8 @@ export class IndexedDbMutationQueue implements MutationQueue {
private userId: string,
private readonly serializer: LocalSerializer,
private readonly indexManager: IndexManager,
private readonly referenceDelegate: ReferenceDelegate
private readonly referenceDelegate: ReferenceDelegate,
private readonly statsCollector: StatsCollector
) {}

/**
Expand All @@ -85,7 +87,8 @@ export class IndexedDbMutationQueue implements MutationQueue {
user: User,
serializer: LocalSerializer,
indexManager: IndexManager,
referenceDelegate: ReferenceDelegate
referenceDelegate: ReferenceDelegate,
statsCollector: StatsCollector
): IndexedDbMutationQueue {
// TODO(mcg): Figure out what constraints there are on userIDs
// In particular, are there any reserved characters? are empty ids allowed?
Expand All @@ -97,7 +100,8 @@ export class IndexedDbMutationQueue implements MutationQueue {
userId,
serializer,
indexManager,
referenceDelegate
referenceDelegate,
statsCollector
);
}

Expand Down Expand Up @@ -197,6 +201,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
)
);
}
this.statsCollector.recordRowsWritten(MUTATION_QUEUE_STATS_TAG, 1);
return PersistencePromise.waitFor(promises).next(() => batch);
});
}
Expand Down Expand Up @@ -258,6 +263,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
'Should have found mutation after ' + nextBatchId
);
foundBatch = this.serializer.fromDbMutationBatch(dbBatch);
this.statsCollector.recordRowsRead(MUTATION_QUEUE_STATS_TAG, 1);
}
control.done();
}
Expand Down Expand Up @@ -296,7 +302,14 @@ export class IndexedDbMutationQueue implements MutationQueue {
.loadAll(DbMutationBatch.userMutationsIndex, range)
.next(dbBatches =>
dbBatches.map(dbBatch => this.serializer.fromDbMutationBatch(dbBatch))
);
)
.next(batches => {
this.statsCollector.recordRowsRead(
MUTATION_QUEUE_STATS_TAG,
batches.length
);
return batches;
});
}

getAllMutationBatchesAffectingDocumentKey(
Expand Down Expand Up @@ -347,7 +360,13 @@ export class IndexedDbMutationQueue implements MutationQueue {
results.push(this.serializer.fromDbMutationBatch(mutation!));
});
})
.next(() => results);
.next(() => {
this.statsCollector.recordRowsRead(
MUTATION_QUEUE_STATS_TAG,
results.length
);
return results;
});
}

getAllMutationBatchesAffectingDocumentKeys(
Expand Down Expand Up @@ -479,7 +498,13 @@ export class IndexedDbMutationQueue implements MutationQueue {
})
);
});
return PersistencePromise.waitFor(promises).next(() => results);
return PersistencePromise.waitFor(promises).next(() => {
this.statsCollector.recordRowsRead(
MUTATION_QUEUE_STATS_TAG,
results.length
);
return results;
});
}

removeMutationBatch(
Expand All @@ -491,6 +516,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
this.userId,
batch
).next(removedDocuments => {
this.statsCollector.recordRowsDeleted(MUTATION_QUEUE_STATS_TAG, 1);
this.removeCachedMutationKeys(batch.batchId);
return PersistencePromise.forEach(
removedDocuments,
Expand Down
22 changes: 16 additions & 6 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { QueryData } from './query_data';
import { ReferenceSet } from './reference_set';
import { ClientId } from './shared_client_state';
import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';
import { StatsCollector } from './stats_collector';

const LOG_TAG = 'IndexedDbPersistence';

Expand Down Expand Up @@ -192,10 +193,11 @@ export class IndexedDbPersistence implements Persistence {
persistenceKey: string;
clientId: ClientId;
platform: Platform;
lruParams: LruParams;
queue: AsyncQueue;
serializer: JsonProtoSerializer;
sequenceNumberSyncer: SequenceNumberSyncer;
lruParams?: LruParams;
statsCollector?: StatsCollector;
}): Promise<IndexedDbPersistence> {
if (!IndexedDbPersistence.isAvailable()) {
throw new FirestoreError(
Expand All @@ -204,15 +206,20 @@ export class IndexedDbPersistence implements Persistence {
);
}

const lruParams = options.lruParams || LruParams.DEFAULT;
const statsCollector =
options.statsCollector || StatsCollector.newNoOpStatsCollector();

const persistence = new IndexedDbPersistence(
options.allowTabSynchronization,
options.persistenceKey,
options.clientId,
options.platform,
options.lruParams,
lruParams,
options.queue,
options.serializer,
options.sequenceNumberSyncer
options.sequenceNumberSyncer,
statsCollector
);
await persistence.start();
return persistence;
Expand Down Expand Up @@ -264,7 +271,8 @@ export class IndexedDbPersistence implements Persistence {
lruParams: LruParams,
private readonly queue: AsyncQueue,
serializer: JsonProtoSerializer,
private readonly sequenceNumberSyncer: SequenceNumberSyncer
private readonly sequenceNumberSyncer: SequenceNumberSyncer,
private readonly statsCollector: StatsCollector
) {
this.referenceDelegate = new IndexedDbLruDelegate(this, lruParams);
this.dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE;
Expand All @@ -277,7 +285,8 @@ export class IndexedDbPersistence implements Persistence {
this.indexManager = new IndexedDbIndexManager();
this.remoteDocumentCache = new IndexedDbRemoteDocumentCache(
this.serializer,
this.indexManager
this.indexManager,
this.statsCollector
);
if (platform.window && platform.window.localStorage) {
this.window = platform.window;
Expand Down Expand Up @@ -712,7 +721,8 @@ export class IndexedDbPersistence implements Persistence {
user,
this.serializer,
this.indexManager,
this.referenceDelegate
this.referenceDelegate,
this.statsCollector
);
}

Expand Down
32 changes: 28 additions & 4 deletions packages/firestore/src/local/indexeddb_remote_document_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ import {
import { LocalSerializer } from './local_serializer';
import { PersistenceTransaction } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { RemoteDocumentCache } from './remote_document_cache';
import {
REMOTE_DOCUMENT_CACHE_STATS_TAG,
RemoteDocumentCache
} from './remote_document_cache';
import { RemoteDocumentChangeBuffer } from './remote_document_change_buffer';
import {
IterateOptions,
SimpleDb,
SimpleDbStore,
SimpleDbTransaction
} from './simple_db';
import { StatsCollector } from './stats_collector';
import { ObjectMap } from '../util/obj_map';

export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
Expand All @@ -64,7 +68,8 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
*/
constructor(
readonly serializer: LocalSerializer,
private readonly indexManager: IndexManager
private readonly indexManager: IndexManager,
private readonly statsCollector: StatsCollector
) {}

/**
Expand Down Expand Up @@ -92,6 +97,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
): PersistencePromise<void> {
const documentStore = remoteDocumentsStore(transaction);
return documentStore.put(dbKey(key), doc).next(() => {
this.statsCollector.recordRowsWritten(REMOTE_DOCUMENT_CACHE_STATS_TAG, 1);
this.indexManager.addToCollectionParentIndex(
transaction,
key.path.popLast()
Expand All @@ -109,6 +115,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
transaction: PersistenceTransaction,
documentKey: DocumentKey
): PersistencePromise<void> {
this.statsCollector.recordRowsDeleted(REMOTE_DOCUMENT_CACHE_STATS_TAG, 1);
const store = remoteDocumentsStore(transaction);
const key = dbKey(documentKey);
return store.delete(key);
Expand Down Expand Up @@ -292,6 +299,8 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
iterationOptions.index = DbRemoteDocument.collectionReadTimeIndex;
}

let rowsRead = 0;

return remoteDocumentsStore(transaction)
.iterate(iterationOptions, (key, dbRemoteDoc, control) => {
// The query is actually returning any path that starts with the query
Expand All @@ -303,14 +312,22 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
return;
}

++rowsRead;

const maybeDoc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
if (!query.path.isPrefixOf(maybeDoc.key.path)) {
control.done();
} else if (maybeDoc instanceof Document && query.matches(maybeDoc)) {
results = results.insert(maybeDoc.key, maybeDoc);
}
})
.next(() => results);
.next(() => {
this.statsCollector.recordRowsRead(
REMOTE_DOCUMENT_CACHE_STATS_TAG,
rowsRead
);
return results;
});
}

getNewDocumentChanges(
Expand All @@ -337,7 +354,13 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
);
}
)
.next(() => changedDocs);
.next(() => {
this.statsCollector.recordRowsRead(
REMOTE_DOCUMENT_CACHE_STATS_TAG,
changedDocs.size
);
return changedDocs;
});
}

/**
Expand Down Expand Up @@ -417,6 +440,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
return null;
}

this.statsCollector.recordRowsRead(REMOTE_DOCUMENT_CACHE_STATS_TAG, 1);
return doc;
}
return null;
Expand Down
14 changes: 12 additions & 2 deletions packages/firestore/src/local/memory_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import { SortedMap } from '../util/sorted_map';
import { SortedSet } from '../util/sorted_set';

import { IndexManager } from './index_manager';
import { MutationQueue } from './mutation_queue';
import { MUTATION_QUEUE_STATS_TAG, MutationQueue } from './mutation_queue';
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { DocReference } from './reference_set';
import { StatsCollector } from './stats_collector';

export class MemoryMutationQueue implements MutationQueue {
/**
Expand All @@ -55,7 +56,8 @@ export class MemoryMutationQueue implements MutationQueue {

constructor(
private readonly indexManager: IndexManager,
private readonly referenceDelegate: ReferenceDelegate
private readonly referenceDelegate: ReferenceDelegate,
private readonly statsCollector: StatsCollector
) {}

checkEmpty(transaction: PersistenceTransaction): PersistencePromise<boolean> {
Expand Down Expand Up @@ -141,6 +143,8 @@ export class MemoryMutationQueue implements MutationQueue {
);
}

this.statsCollector.recordRowsWritten(MUTATION_QUEUE_STATS_TAG, 1);

return PersistencePromise.resolve(batch);
}

Expand Down Expand Up @@ -186,6 +190,10 @@ export class MemoryMutationQueue implements MutationQueue {
getAllMutationBatches(
transaction: PersistenceTransaction
): PersistencePromise<MutationBatch[]> {
this.statsCollector.recordRowsRead(
MUTATION_QUEUE_STATS_TAG,
this.mutationQueue.length
);
return PersistencePromise.resolve(this.mutationQueue.slice());
}

Expand Down Expand Up @@ -317,6 +325,7 @@ export class MemoryMutationQueue implements MutationQueue {
mutation.key
);
}).next(() => {
this.statsCollector.recordRowsDeleted(MUTATION_QUEUE_STATS_TAG, 1);
this.batchesByDocumentKey = references;
});
}
Expand Down Expand Up @@ -398,6 +407,7 @@ export class MemoryMutationQueue implements MutationQueue {

const batch = this.mutationQueue[index];
assert(batch.batchId === batchId, 'If found batch must match');
this.statsCollector.recordRowsRead(MUTATION_QUEUE_STATS_TAG, 1);
return batch;
}
}
Loading