Skip to content

Add clearPersistence(), separate functionality out from shutdown() #1712

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 27 commits into from
Apr 23, 2019
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
44 changes: 33 additions & 11 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from '../core/query';
import { Transaction as InternalTransaction } from '../core/transaction';
import { ChangeType, ViewSnapshot } from '../core/view_snapshot';
import { IndexedDbPersistence } from '../local/indexeddb_persistence';
import { LruParams } from '../local/lru_garbage_collector';
import { Document, MaybeDocument, NoDocument } from '../model/document';
import { DocumentKey } from '../model/document_key';
Expand Down Expand Up @@ -307,13 +308,16 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
// are already set to synchronize on the async queue.
private _firestoreClient: FirestoreClient | undefined;

private clientRunning: boolean;

// Public for use in tests.
// TODO(mikelehen): Use modularized initialization instead.
readonly _queue = new AsyncQueue();

_dataConverter: UserDataConverter;

constructor(databaseIdOrApp: FirestoreDatabase | FirebaseApp) {
this.clientRunning = false;
const config = new FirestoreConfig();
if (typeof (databaseIdOrApp as FirebaseApp).options === 'object') {
// This is very likely a Firebase app object
Expand Down Expand Up @@ -406,6 +410,19 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
);
}

_clearPersistence(): Promise<void> {
if (this.clientRunning) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Persistence cannot be cleared while the client is running'
);
}
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
this.makeDatabaseInfo()
);
return IndexedDbPersistence.clearPersistence(persistenceKey);
}

ensureClientConfigured(): FirestoreClient {
if (!this._firestoreClient) {
// Kick off starting the client but don't actually wait for it.
Expand All @@ -415,6 +432,16 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
return this._firestoreClient as FirestoreClient;
}

private makeDatabaseInfo(): DatabaseInfo {
return new DatabaseInfo(
this._config.databaseId,
this._config.persistenceKey,
this._config.settings.host,
this._config.settings.ssl,
this._config.settings.forceLongPolling
);
}

private configureClient(
persistenceSettings: InternalPersistenceSettings
): Promise<void> {
Expand All @@ -425,13 +452,8 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {

assert(!this._firestoreClient, 'configureClient() called multiple times');

const databaseInfo = new DatabaseInfo(
this._config.databaseId,
this._config.persistenceKey,
this._config.settings.host,
this._config.settings.ssl,
this._config.settings.forceLongPolling
);
this.clientRunning = true;
const databaseInfo = this.makeDatabaseInfo();

const preConverter = (value: unknown) => {
if (value instanceof DocumentReference) {
Expand All @@ -458,6 +480,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
this._config.credentials,
this._queue
);

return this._firestoreClient.start(persistenceSettings);
}

Expand Down Expand Up @@ -492,13 +515,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
}

INTERNAL = {
delete: async (options?: {
purgePersistenceWithDataLoss?: boolean;
}): Promise<void> => {
delete: async (): Promise<void> => {
// The client must be initalized to ensure that all subsequent API usage
// throws an exception.
this.ensureClientConfigured();
return this._firestoreClient!.shutdown(options);
await this._firestoreClient!.shutdown();
this.clientRunning = false;
}
};

Expand Down
8 changes: 2 additions & 6 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,7 @@ export class FirestoreClient {
});
}

shutdown(options?: {
purgePersistenceWithDataLoss?: boolean;
}): Promise<void> {
shutdown(): Promise<void> {
if (this.isShutdown === true) {
return Promise.resolve();
}
Expand All @@ -501,9 +499,7 @@ export class FirestoreClient {
}
await this.remoteStore.shutdown();
await this.sharedClientState.shutdown();
await this.persistence.shutdown(
options && options.purgePersistenceWithDataLoss
);
await this.persistence.shutdown();

// `removeChangeListener` must be called after shutting down the
// RemoteStore as it will prevent the RemoteStore from retrieving
Expand Down
13 changes: 9 additions & 4 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export class IndexedDbPersistence implements Persistence {
});
}

async shutdown(deleteData?: boolean): Promise<void> {
async shutdown(): Promise<void> {
// The shutdown() operations are idempotent and can be called even when
// start() aborted (e.g. because it couldn't acquire the persistence lease).
this._started = false;
Expand All @@ -696,9 +696,6 @@ export class IndexedDbPersistence implements Persistence {
// Remove the entry marking the client as zombied from LocalStorage since
// we successfully deleted its metadata from IndexedDb.
this.removeClientZombiedEntry();
if (deleteData) {
await SimpleDb.delete(this.dbName);
}
}

/**
Expand Down Expand Up @@ -732,6 +729,14 @@ export class IndexedDbPersistence implements Persistence {
);
}

static async clearPersistence(persistenceKey: string): Promise<void> {
if (!IndexedDbPersistence.isAvailable()) {
return Promise.resolve();
}
const dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE;
await SimpleDb.delete(dbName);
}

get started(): boolean {
return this._started;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/memory_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class MemoryPersistence implements Persistence {
);
}

shutdown(deleteData?: boolean): Promise<void> {
shutdown(): Promise<void> {
// No durable state to ensure is closed on shutdown.
this._started = false;
return Promise.resolve();
Expand Down
5 changes: 1 addition & 4 deletions packages/firestore/src/local/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,8 @@ export interface Persistence {

/**
* Releases any resources held during eager shutdown.
*
* @param deleteData Whether to delete the persisted data. This causes
* irrecoverable data loss and should only be used to delete test data.
*/
shutdown(deleteData?: boolean): Promise<void>;
shutdown(): Promise<void>;

/**
* Registers a listener that gets called when the primary state of the
Expand Down
52 changes: 52 additions & 0 deletions packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { EventsAccumulator } from '../util/events_accumulator';
import firebase from '../util/firebase_export';
import {
apiDescribe,
clearPersistence,
withTestCollection,
withTestDb,
withTestDbs,
Expand Down Expand Up @@ -928,6 +929,57 @@ apiDescribe('Database', persistence => {
});
});

(persistence ? it : it.skip)(
'maintains persistence after restarting app',
async () => {
await withTestDoc(persistence, async docRef => {
await docRef.set({ foo: 'bar' });
const app = docRef.firestore.app;
const name = app.name;
const options = app.options;

await app.delete();
const app2 = firebase.initializeApp(options, name);
const firestore2 = firebase.firestore!(app2);
await firestore2.enablePersistence();
const docRef2 = firestore2.doc(docRef.path);
const docSnap2 = await docRef2.get({ source: 'cache' });
expect(docSnap2.exists).to.be.true;
});
}
);

(persistence ? it : it.skip)(
'can clear persistence if the client has not been initialized',
async () => {
await withTestDoc(persistence, async docRef => {
await docRef.set({ foo: 'bar' });
const app = docRef.firestore.app;
const name = app.name;
const options = app.options;

await app.delete();
await clearPersistence(docRef.firestore);
const app2 = firebase.initializeApp(options, name);
const firestore2 = firebase.firestore!(app2);
await firestore2.enablePersistence();
const docRef2 = firestore2.doc(docRef.path);
await expect(
docRef2.get({ source: 'cache' })
).to.eventually.be.rejectedWith('Failed to get document from cache.');
});
}
);

it('can not clear persistence if the client has been initialized', async () => {
await withTestDoc(persistence, async docRef => {
const firestore = docRef.firestore;
await expect(clearPersistence(firestore)).to.eventually.be.rejectedWith(
'Persistence cannot be cleared while the client is running'
);
});
});

it('can get documents while offline', async () => {
await withTestDoc(persistence, async docRef => {
const firestore = docRef.firestore;
Expand Down
50 changes: 23 additions & 27 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as firestore from '@firebase/firestore-types';
import { clearTestPersistence } from './../../unit/local/persistence_test_helpers';
import firebase from './firebase_export';

/**
Expand Down Expand Up @@ -105,6 +106,14 @@ function apiDescribeInternal(
}
}

// TODO(b/131094514): Remove after clearPersistence() is updated in index.d.ts.
export async function clearPersistence(
firestore: firestore.FirebaseFirestore
): Promise<void> {
// tslint:disable-next-line:no-any
await (firestore as any)._clearPersistence();
}

/** Converts the documents in a QuerySnapshot to an array with the data of each document. */
export function toDataArray(
docSet: firestore.QuerySnapshot
Expand Down Expand Up @@ -176,7 +185,7 @@ export function withTestDbs(

let appCount = 0;

export function withTestDbsSettings(
export async function withTestDbsSettings(
persistence: boolean,
projectId: string,
settings: firestore.Settings,
Expand Down Expand Up @@ -208,32 +217,19 @@ export function withTestDbsSettings(
promises.push(ready);
}

return Promise.all(promises).then((dbs: firestore.FirebaseFirestore[]) => {
const cleanup = () => {
return wipeDb(dbs[0]).then(() =>
dbs.reduce(
(chain, db) =>
chain.then(
db.INTERNAL.delete.bind(this, {
purgePersistenceWithDataLoss: true
})
),
Promise.resolve()
)
);
};

return fn(dbs).then(
() => cleanup(),
err => {
// Do cleanup but propagate original error.
return cleanup().then(
() => Promise.reject(err),
() => Promise.reject(err)
);
}
);
});
const dbs = await Promise.all(promises);

try {
await fn(dbs);
} finally {
await wipeDb(dbs[0]);
for (const db of dbs) {
await db.INTERNAL.delete();
}
if (persistence) {
await clearTestPersistence();
}
}
}

export function withTestDoc(
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/test/unit/local/index_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function genericIndexManagerTests(

afterEach(async () => {
if (persistence.started) {
await persistence.shutdown(/* deleteData= */ true);
await persistence.shutdown();
await persistenceHelpers.clearTestPersistence();
}
});

Expand Down
5 changes: 4 additions & 1 deletion packages/firestore/test/unit/local/local_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ function genericLocalStoreTests(
localStore = new LocalStore(persistence, User.UNAUTHENTICATED);
});

afterEach(() => persistence.shutdown(/* deleteData= */ true));
afterEach(async () => {
await persistence.shutdown();
await persistenceHelpers.clearTestPersistence();
});

function expectLocalStore(): LocalStoreTester {
return new LocalStoreTester(localStore, gcIsEager);
Expand Down
10 changes: 8 additions & 2 deletions packages/firestore/test/unit/local/lru_garbage_collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ function genericLruGarbageCollectorTests(
});

afterEach(async () => {
await queue.enqueue(() => persistence.shutdown(/* deleteData= */ true));
await queue.enqueue(async () => {
await persistence.shutdown();
await PersistenceTestHelpers.clearTestPersistence();
});
});

let persistence: Persistence;
Expand All @@ -102,7 +105,10 @@ function genericLruGarbageCollectorTests(
params: LruParams = LruParams.DEFAULT
): Promise<void> {
if (persistence && persistence.started) {
await queue.enqueue(() => persistence.shutdown(/* deleteData= */ true));
await queue.enqueue(async () => {
await persistence.shutdown();
await PersistenceTestHelpers.clearTestPersistence();
});
}
lruParams = params;
persistence = await newPersistence(params, queue);
Expand Down
5 changes: 4 additions & 1 deletion packages/firestore/test/unit/local/mutation_queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ function genericMutationQueueTests(): void {
);
});

afterEach(() => persistence.shutdown(/* deleteData= */ true));
afterEach(async () => {
await persistence.shutdown();
await persistenceHelpers.clearTestPersistence();
});

/**
* Creates a new MutationBatch with the next batch ID and a set of dummy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export async function testMemoryLruPersistence(
);
}

/** Clears the persistence in tests */
export async function clearTestPersistence(): Promise<void> {
await IndexedDbPersistence.clearPersistence(TEST_PERSISTENCE_PREFIX);
}

class NoOpSharedClientStateSyncer implements SharedClientStateSyncer {
constructor(private readonly activeClients: ClientId[]) {}
async applyBatchState(
Expand Down
Loading