-
Notifications
You must be signed in to change notification settings - Fork 943
Retrying transactions with backoff #2063
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0dc9426
retrying transactions with backoff, override asyncQueue
3203431
new attempt of asyncqueue
487fcaa
Merge branch 'master' into bc/tx-backoff
500c9e1
Merge branch 'master' into bc/tx-backoff
aaa6943
update to override backoff
09b095b
Remove timerIdsToSkip
15b4255
Revert "Remove timerIdsToSkip"
88748d8
Adding transaction runner and skipTimerIdDelays
64d2541
update comments
45daee4
another round of lints
d7e977e
Merge branch 'master' into bc/tx-backoff
bd0c591
runTransaction should return void
005be59
add changelog and caught error names
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Deferred } from '../util/promise'; | ||
import { TimerId, AsyncQueue } from '../util/async_queue'; | ||
import { ExponentialBackoff } from '../remote/backoff'; | ||
import { Transaction } from './transaction'; | ||
import { RemoteStore } from '../remote/remote_store'; | ||
import { isNullOrUndefined } from '../util/types'; | ||
import { isPermanentError } from '../remote/rpc_error'; | ||
import { FirestoreError } from '../util/error'; | ||
|
||
const RETRY_COUNT = 5; | ||
|
||
/** | ||
thebrianchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* TransactionRunner encapsulates the logic needed to run and retry transactions | ||
* with backoff. | ||
*/ | ||
export class TransactionRunner<T> { | ||
private retries = RETRY_COUNT; | ||
private backoff: ExponentialBackoff; | ||
|
||
constructor( | ||
private readonly asyncQueue: AsyncQueue, | ||
private readonly remoteStore: RemoteStore, | ||
private readonly updateFunction: (transaction: Transaction) => Promise<T>, | ||
private readonly deferred: Deferred<T> | ||
) { | ||
this.backoff = new ExponentialBackoff( | ||
this.asyncQueue, | ||
TimerId.RetryTransaction | ||
); | ||
} | ||
|
||
/** Runs the transaction and sets the result on deferred. */ | ||
run(): void { | ||
this.runWithBackOff(); | ||
} | ||
|
||
private runWithBackOff(): void { | ||
this.backoff.backoffAndRun(async () => { | ||
const transaction = this.remoteStore.createTransaction(); | ||
const userPromise = this.tryRunUpdateFunction(transaction); | ||
if (userPromise) { | ||
userPromise | ||
.then(result => { | ||
this.asyncQueue.enqueueAndForget(() => { | ||
return transaction | ||
.commit() | ||
.then(() => { | ||
this.deferred.resolve(result); | ||
}) | ||
.catch(commitError => { | ||
this.handleTransactionError(commitError); | ||
}); | ||
}); | ||
}) | ||
.catch(userPromiseError => { | ||
this.handleTransactionError(userPromiseError); | ||
}); | ||
} | ||
thebrianchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
|
||
private tryRunUpdateFunction(transaction: Transaction): Promise<T> | null { | ||
try { | ||
const userPromise = this.updateFunction(transaction); | ||
if ( | ||
isNullOrUndefined(userPromise) || | ||
!userPromise.catch || | ||
!userPromise.then | ||
) { | ||
this.deferred.reject( | ||
Error('Transaction callback must return a Promise') | ||
); | ||
return null; | ||
} | ||
return userPromise; | ||
} catch (error) { | ||
// Do not retry errors thrown by user provided updateFunction. | ||
this.deferred.reject(error); | ||
return null; | ||
} | ||
} | ||
|
||
private handleTransactionError(error: Error): void { | ||
if (this.retries > 0 && this.isRetryableTransactionError(error)) { | ||
this.retries -= 1; | ||
this.asyncQueue.enqueueAndForget(() => { | ||
this.runWithBackOff(); | ||
return Promise.resolve(); | ||
}); | ||
} else { | ||
this.deferred.reject(error); | ||
} | ||
} | ||
|
||
private isRetryableTransactionError(error: Error): boolean { | ||
if (error.name === 'FirebaseError') { | ||
// In transactions, the backend will fail outdated reads with FAILED_PRECONDITION and | ||
// non-matching document versions with ABORTED. These errors should be retried. | ||
const code = (error as FirestoreError).code; | ||
return ( | ||
code === 'aborted' || | ||
code === 'failed-precondition' || | ||
!isPermanentError(code) | ||
); | ||
} | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.