|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology,"," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import {newError, SERVICE_UNAVAILABLE, SESSION_EXPIRED} from '../error'; |
| 21 | + |
| 22 | +const DEFAULT_MAX_RETRY_TIME_MS = 30 * 1000; // 30 seconds |
| 23 | +const DEFAULT_INITIAL_RETRY_DELAY_MS = 1000; // 1 seconds |
| 24 | +const DEFAULT_RETRY_DELAY_MULTIPLIER = 2.0; |
| 25 | +const DEFAULT_RETRY_DELAY_JITTER_FACTOR = 0.2; |
| 26 | + |
| 27 | +export default class TransactionExecutor { |
| 28 | + |
| 29 | + constructor(maxRetryTimeMs, initialRetryDelayMs, multiplier, jitterFactor) { |
| 30 | + this._maxRetryTimeMs = maxRetryTimeMs || DEFAULT_MAX_RETRY_TIME_MS; |
| 31 | + this._initialRetryDelayMs = initialRetryDelayMs || DEFAULT_INITIAL_RETRY_DELAY_MS; |
| 32 | + this._multiplier = multiplier || DEFAULT_RETRY_DELAY_MULTIPLIER; |
| 33 | + this._jitterFactor = jitterFactor || DEFAULT_RETRY_DELAY_JITTER_FACTOR; |
| 34 | + |
| 35 | + this._inFlightTimeoutIds = []; |
| 36 | + |
| 37 | + this._verifyAfterConstruction(); |
| 38 | + } |
| 39 | + |
| 40 | + execute(transactionCreator, transactionWork) { |
| 41 | + return new Promise((resolve, reject) => { |
| 42 | + this._executeTransactionInsidePromise(transactionCreator, transactionWork, resolve, reject); |
| 43 | + }).catch(error => { |
| 44 | + const retryStartTimeMs = Date.now(); |
| 45 | + const retryDelayMs = this._initialRetryDelayMs; |
| 46 | + return this._retryTransactionPromise(transactionCreator, transactionWork, error, retryStartTimeMs, retryDelayMs); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + close() { |
| 51 | + // cancel all existing timeouts to prevent further retries |
| 52 | + this._inFlightTimeoutIds.forEach(timeoutId => clearTimeout(timeoutId)); |
| 53 | + this._inFlightTimeoutIds = []; |
| 54 | + } |
| 55 | + |
| 56 | + _retryTransactionPromise(transactionCreator, transactionWork, error, retryStartTime, retryDelayMs) { |
| 57 | + const elapsedTimeMs = Date.now() - retryStartTime; |
| 58 | + |
| 59 | + if (elapsedTimeMs > this._maxRetryTimeMs || !TransactionExecutor._canRetryOn(error)) { |
| 60 | + return Promise.reject(error); |
| 61 | + } |
| 62 | + |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + const nextRetryTime = this._computeDelayWithJitter(retryDelayMs); |
| 65 | + const timeoutId = setTimeout(() => { |
| 66 | + // filter out this timeoutId when time has come and function is being executed |
| 67 | + this._inFlightTimeoutIds = this._inFlightTimeoutIds.filter(id => id !== timeoutId); |
| 68 | + this._executeTransactionInsidePromise(transactionCreator, transactionWork, resolve, reject); |
| 69 | + }, nextRetryTime); |
| 70 | + // add newly created timeoutId to the list of all in-flight timeouts |
| 71 | + this._inFlightTimeoutIds.push(timeoutId); |
| 72 | + }).catch(error => { |
| 73 | + const nextRetryDelayMs = retryDelayMs * this._multiplier; |
| 74 | + return this._retryTransactionPromise(transactionCreator, transactionWork, error, retryStartTime, nextRetryDelayMs); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + _executeTransactionInsidePromise(transactionCreator, transactionWork, resolve, reject) { |
| 79 | + try { |
| 80 | + const tx = transactionCreator(); |
| 81 | + const transactionWorkResult = transactionWork(tx); |
| 82 | + |
| 83 | + // user defined callback is supposed to return a promise, but it might not; so to protect against an |
| 84 | + // incorrect API usage we wrap the returned value with a resolved promise; this is effectively a |
| 85 | + // validation step without type checks |
| 86 | + const resultPromise = Promise.resolve(transactionWorkResult); |
| 87 | + |
| 88 | + resultPromise.then(result => { |
| 89 | + // transaction work returned resolved promise, try to commit the transaction |
| 90 | + tx.commit().then(() => { |
| 91 | + // transaction was committed, return result to the user |
| 92 | + resolve(result); |
| 93 | + }).catch(error => { |
| 94 | + // transaction failed to commit, propagate the failure |
| 95 | + reject(error); |
| 96 | + }); |
| 97 | + }).catch(error => { |
| 98 | + // transaction work returned rejected promise, propagate the failure |
| 99 | + reject(error); |
| 100 | + }); |
| 101 | + |
| 102 | + } catch (error) { |
| 103 | + reject(error); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + _computeDelayWithJitter(delayMs) { |
| 108 | + const jitter = (delayMs * this._jitterFactor); |
| 109 | + const min = delayMs - jitter; |
| 110 | + const max = delayMs + jitter; |
| 111 | + return Math.random() * (max - min) + min; |
| 112 | + } |
| 113 | + |
| 114 | + static _canRetryOn(error) { |
| 115 | + return error && error.code && |
| 116 | + (error.code === SERVICE_UNAVAILABLE || |
| 117 | + error.code === SESSION_EXPIRED || |
| 118 | + error.code.indexOf('TransientError') >= 0); |
| 119 | + } |
| 120 | + |
| 121 | + _verifyAfterConstruction() { |
| 122 | + if (this._maxRetryTimeMs < 0) { |
| 123 | + throw newError('Max retry time should be >= 0: ' + this._maxRetryTimeMs); |
| 124 | + } |
| 125 | + if (this._initialRetryDelayMs < 0) { |
| 126 | + throw newError('Initial retry delay should >= 0: ' + this._initialRetryDelayMs); |
| 127 | + } |
| 128 | + if (this._multiplier < 1.0) { |
| 129 | + throw newError('Multiplier should be >= 1.0: ' + this._multiplier); |
| 130 | + } |
| 131 | + if (this._jitterFactor < 0 || this._jitterFactor > 1) { |
| 132 | + throw newError('Jitter factor should be in [0.0, 1.0]: ' + this._jitterFactor); |
| 133 | + } |
| 134 | + } |
| 135 | +}; |
0 commit comments