diff options
author | A. Jesse Jiryu Davis <jesse@mongodb.com> | 2018-12-06 23:15:36 -0500 |
---|---|---|
committer | A. Jesse Jiryu Davis <jesse@mongodb.com> | 2018-12-20 08:31:31 -0600 |
commit | 759846ffced5ef84734bd917a99061edf44dd786 (patch) | |
tree | 546d39ee4612b98a7a365031c4673d0ed764a763 /src/mongo/shell/session.js | |
parent | 6b641e86656df78e9f7ad2f364dec35e19df332e (diff) | |
download | mongo-759846ffced5ef84734bd917a99061edf44dd786.tar.gz |
SERVER-36902 Abort transaction on shell exit
Diffstat (limited to 'src/mongo/shell/session.js')
-rw-r--r-- | src/mongo/shell/session.js | 72 |
1 files changed, 25 insertions, 47 deletions
diff --git a/src/mongo/shell/session.js b/src/mongo/shell/session.js index 9c68c35a063..cc68e97d575 100644 --- a/src/mongo/shell/session.js +++ b/src/mongo/shell/session.js @@ -486,17 +486,10 @@ var { // The server session maintains the state of a transaction, a monotonically increasing txn // number, and a transaction's read/write concerns. function ServerSession(client) { - // The default txnState is `inactive` until we call startTransaction. - let _txnState = ServerSession.TransactionStates.kInactive; - let _txnOptions; // Keep track of the next available statement id of a transaction. let _nextStatementId = 0; - - // _txnNumber starts at -1 because when we increment it, the first transaction - // and retryable write will both have a txnNumber of 0. - let _txnNumber = -1; let _lastUsed = new Date(); this.client = new SessionAwareClient(client); @@ -511,8 +504,11 @@ var { wireVersion <= client.getMaxWireVersion(); } + const hasTxnState = ((name) => this.handle.getTxnState() === name); + const setTxnState = ((name) => this.handle.setTxnState(name)); + this.isTxnActive = function isTxnActive() { - return _txnState === ServerSession.TransactionStates.kActive; + return hasTxnState("active"); }; this.isFirstStatement = function isFirstStatement() { @@ -524,7 +520,7 @@ var { }; this.getTxnNumber = function getTxnNumber() { - return _txnNumber; + return this.handle.getTxnNumber(); }; this.setTxnNumber_forTesting = function setTxnNumber_forTesting(newTxnNumber) { @@ -577,11 +573,8 @@ var { } if (!cmdObjUnwrapped.hasOwnProperty("txnNumber")) { - // Since there's no native support for adding NumberLong instances and getting back - // another NumberLong instance, converting from a 64-bit floating-point value to a - // 64-bit integer value will overflow at 2**53. - _txnNumber++; - cmdObjUnwrapped.txnNumber = new NumberLong(_txnNumber); + this.handle.incrementTxnNumber(); + cmdObjUnwrapped.txnNumber = this.handle.getTxnNumber(); } return cmdObj; @@ -675,22 +668,21 @@ var { this.assignTxnInfo = function assignTxnInfo(cmdObj) { // We will want to reset the transaction state to 'inactive' if a normal operation // follows a committed or aborted transaction. - if ((_txnState === ServerSession.TransactionStates.kAborted) || - (_txnState === ServerSession.TransactionStates.kCommitted && - Object.keys(cmdObj)[0] !== "commitTransaction")) { - _txnState = ServerSession.TransactionStates.kInactive; + if ((hasTxnState("aborted")) || + (hasTxnState("committed") && Object.keys(cmdObj)[0] !== "commitTransaction")) { + setTxnState("inactive"); } // If we're not in an active transaction or performing a retry on commitTransaction, // return early. - if (_txnState === ServerSession.TransactionStates.kInactive) { + if (hasTxnState("inactive")) { return cmdObj; } // If we reconnect to a 3.6 server in the middle of a transaction, we // catch it here. if (!serverSupports(kWireVersionSupportingMultiDocumentTransactions)) { - _txnState = ServerSession.TransactionStates.kInactive; + setTxnState("inactive"); throw new Error( "Transactions are only supported on server versions 4.0 and greater."); } @@ -707,10 +699,7 @@ var { } if (!cmdObjUnwrapped.hasOwnProperty("txnNumber")) { - // Since there's no native support for adding NumberLong instances and getting back - // another NumberLong instance, converting from a 64-bit floating-point value to a - // 64-bit integer value will overflow at 2**53. - cmdObjUnwrapped.txnNumber = new NumberLong(_txnNumber); + cmdObjUnwrapped.txnNumber = this.handle.getTxnNumber(); } // All operations of a multi-statement transaction must specify autocommit=false. @@ -760,18 +749,18 @@ var { "Transactions are only supported on server versions 4.0 and greater."); } _txnOptions = new TransactionOptions(txnOptsObj); - _txnState = ServerSession.TransactionStates.kActive; + setTxnState("active"); _nextStatementId = 0; - _txnNumber++; + this.handle.incrementTxnNumber(); }; this.commitTransaction = function commitTransaction(driverSession) { // If the transaction state is already 'aborted' we cannot try to commit it. - if (_txnState === ServerSession.TransactionStates.kAborted) { + if (hasTxnState("aborted")) { throw new Error("Cannot call commitTransaction after calling abortTransaction."); } // If the session has no active transaction, raise an error. - if (_txnState === ServerSession.TransactionStates.kInactive) { + if (hasTxnState("inactive")) { throw new Error("There is no active transaction to commit on this session."); } // run commitTxn command @@ -780,15 +769,15 @@ var { this.abortTransaction = function abortTransaction(driverSession) { // If the transaction state is already 'aborted' we cannot try to abort it again. - if (_txnState === ServerSession.TransactionStates.kAborted) { + if (hasTxnState("aborted")) { throw new Error("Cannot call abortTransaction twice."); } // We cannot attempt to abort a transaction that has already been committed. - if (_txnState === ServerSession.TransactionStates.kCommitted) { + if (hasTxnState("committed")) { throw new Error("Cannot call abortTransaction after calling commitTransaction."); } // If the session has no active transaction, raise an error. - if (_txnState === ServerSession.TransactionStates.kInactive) { + if (hasTxnState("inactive")) { throw new Error("There is no active transaction to abort on this session."); } // run abortTxn command @@ -801,14 +790,14 @@ var { // transaction as 'committed' or 'aborted' accordingly. if (this.isFirstStatement()) { if (commandName === "commitTransaction") { - _txnState = ServerSession.TransactionStates.kCommitted; + setTxnState("committed"); } else { - _txnState = ServerSession.TransactionStates.kAborted; + setTxnState("aborted"); } return {"ok": 1}; } - let cmd = {[commandName]: 1, txnNumber: NumberLong(_txnNumber)}; + let cmd = {[commandName]: 1, txnNumber: this.handle.getTxnNumber()}; // writeConcern should only be specified on commit or abort. If a writeConcern is // not specified from the default transaction options, it will be inherited from // the session. @@ -826,26 +815,15 @@ var { res = this.client.runCommand(driverSession, "admin", cmd, 0); } finally { if (commandName === "commitTransaction") { - _txnState = ServerSession.TransactionStates.kCommitted; + setTxnState("committed"); } else { - _txnState = ServerSession.TransactionStates.kAborted; + setTxnState("aborted"); } } return res; }; } - // TransactionStates represents the state of the current transaction. The default state - // is 'inactive' until startTransaction is called and changes the state to 'active'. - // Calling abortTransaction or commitTransaction will change the state to 'aborted' or - // 'committed' respectively, even on error. - ServerSession.TransactionStates = { - kActive: 'active', - kInactive: 'inactive', - kCommitted: 'committed', - kAborted: 'aborted', - }; - function makeDriverSessionConstructor(implMethods, defaultOptions = {}) { var driverSessionConstructor = function(client, options = defaultOptions) { let _options = options; |