Skip to content

Don't fail client if lease refresh fails #2307

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 3 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 0 deletions packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Unreleased
- [changed] The client can now recover if certain periodic IndexedDB operations
fail.

# 1.6.3
- [changed] Improved iOS 13 support by eliminating an additional crash in our
IndexedDB persistence layer.

Expand Down
102 changes: 61 additions & 41 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,47 +380,61 @@ export class IndexedDbPersistence implements Persistence {
* primary lease.
*/
private updateClientMetadataAndTryBecomePrimary(): Promise<void> {
return this.simpleDb.runTransaction('readwrite', ALL_STORES, txn => {
const metadataStore = clientMetadataStore(txn);
return metadataStore
.put(
new DbClientMetadata(
this.clientId,
Date.now(),
this.networkEnabled,
this.inForeground
return this.simpleDb
.runTransaction('readwrite-idempotent', ALL_STORES, txn => {
const metadataStore = clientMetadataStore(txn);
return metadataStore
.put(
new DbClientMetadata(
this.clientId,
Date.now(),
this.networkEnabled,
this.inForeground
)
)
)
.next(() => {
if (this.isPrimary) {
return this.verifyPrimaryLease(txn).next(success => {
if (!success) {
this.isPrimary = false;
this.queue.enqueueAndForget(() =>
this.primaryStateListener(false)
);
}
});
}
})
.next(() => this.canActAsPrimary(txn))
.next(canActAsPrimary => {
const wasPrimary = this.isPrimary;
this.isPrimary = canActAsPrimary;

if (wasPrimary !== this.isPrimary) {
this.queue.enqueueAndForget(() =>
this.primaryStateListener(this.isPrimary)
);
}
.next(() => {
if (this.isPrimary) {
return this.verifyPrimaryLease(txn).next(success => {
if (!success) {
this.isPrimary = false;
this.queue.enqueueAndForget(() =>
this.primaryStateListener(false)
);
}
});
}
})
.next(() => this.canActAsPrimary(txn))
.next(canActAsPrimary => {
if (this.isPrimary && !canActAsPrimary) {
return this.releasePrimaryLeaseIfHeld(txn).next(() => false);
} else if (canActAsPrimary) {
return this.acquireOrExtendPrimaryLease(txn).next(() => true);
} else {
return /* canActAsPrimary= */ false;
}
});
})
.catch(e => {
if (!this.allowTabSynchronization) {
throw e;
}

if (wasPrimary && !this.isPrimary) {
return this.releasePrimaryLeaseIfHeld(txn);
} else if (this.isPrimary) {
return this.acquireOrExtendPrimaryLease(txn);
}
});
});
log.debug(
LOG_TAG,
'Releasing owner lease after error during lease refresh',
e
);
return /* isPrimary= */ false;
})
.then(isPrimary => {
if (this.isPrimary !== isPrimary) {
this.queue.enqueueAndForget(() =>
this.primaryStateListener(isPrimary)
);
}
this.isPrimary = isPrimary;
});
}

private verifyPrimaryLease(
Expand Down Expand Up @@ -767,8 +781,14 @@ export class IndexedDbPersistence implements Persistence {
// transactionOperation takes a long time, we'll use a recent
// leaseTimestampMs in the extended (or newly acquired) lease.
return this.verifyPrimaryLease(simpleDbTxn)
.next(success => {
if (!success) {
.next(holdsPrimaryLease => {
if (holdsPrimaryLease) {
return /* holdsPrimaryLease= */ true;
}
return this.canActAsPrimary(simpleDbTxn);
})
.next(holdsPrimaryLease => {
if (!holdsPrimaryLease) {
log.error(
`Failed to obtain primary lease for action '${action}'.`
);
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class SimpleDb {
// TODO(schmidt-sebastian): We could probably be smarter about this and
// not retry exceptions that are likely unrecoverable (such as quota
// exceeded errors).

// Note: We cannot use an instanceof check for FirestoreException, since the
// exception is wrapped in a generic error by our async/await handling.
const retryable =
Expand Down
39 changes: 39 additions & 0 deletions packages/firestore/test/unit/local/indexeddb_persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,28 @@ describe('IndexedDb: canActAsPrimary', () => {
simpleDb.close();
}

async function getCurrentLeaseOwner(): Promise<ClientId | null> {
const simpleDb = await SimpleDb.openOrCreate(
INDEXEDDB_TEST_DATABASE_NAME,
SCHEMA_VERSION,
new SchemaConverter(TEST_SERIALIZER)
);
const leaseOwner = await simpleDb.runTransaction(
'readwrite-idempotent',
[DbPrimaryClient.store],
txn => {
const primaryStore = txn.store<DbPrimaryClientKey, DbPrimaryClient>(
DbPrimaryClient.store
);
return primaryStore
.get(DbPrimaryClient.key)
.next(owner => (owner ? owner.ownerId : null));
}
);
simpleDb.close();
return leaseOwner;
}

beforeEach(() => {
return SimpleDb.delete(INDEXEDDB_TEST_DATABASE_NAME);
});
Expand Down Expand Up @@ -1035,6 +1057,23 @@ describe('IndexedDb: canActAsPrimary', () => {
expect(isPrimary).to.be.true;
});
});

it('regains lease if available', () => {
return withPersistence('clientA', async persistence => {
expect(await getCurrentLeaseOwner()).to.not.be.null;

await clearPrimaryLease();
expect(await getCurrentLeaseOwner()).to.be.null;

await persistence.runTransaction(
'regain lease',
'readwrite-primary',
Copy link
Contributor

Choose a reason for hiding this comment

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

readwrite-primary-idempotent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

() => PersistencePromise.resolve()
);

expect(await getCurrentLeaseOwner()).to.not.be.null;
});
});
});

describe('IndexedDb: allowTabSynchronization', () => {
Expand Down