transactions.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. const MongoError = require('./error').MongoError;
  3. const ReadPreference = require('./topologies/read_preference');
  4. const ReadConcern = require('../read_concern');
  5. const WriteConcern = require('../write_concern');
  6. let TxnState;
  7. let stateMachine;
  8. (() => {
  9. const NO_TRANSACTION = 'NO_TRANSACTION';
  10. const STARTING_TRANSACTION = 'STARTING_TRANSACTION';
  11. const TRANSACTION_IN_PROGRESS = 'TRANSACTION_IN_PROGRESS';
  12. const TRANSACTION_COMMITTED = 'TRANSACTION_COMMITTED';
  13. const TRANSACTION_COMMITTED_EMPTY = 'TRANSACTION_COMMITTED_EMPTY';
  14. const TRANSACTION_ABORTED = 'TRANSACTION_ABORTED';
  15. TxnState = {
  16. NO_TRANSACTION,
  17. STARTING_TRANSACTION,
  18. TRANSACTION_IN_PROGRESS,
  19. TRANSACTION_COMMITTED,
  20. TRANSACTION_COMMITTED_EMPTY,
  21. TRANSACTION_ABORTED
  22. };
  23. stateMachine = {
  24. [NO_TRANSACTION]: [NO_TRANSACTION, STARTING_TRANSACTION],
  25. [STARTING_TRANSACTION]: [
  26. TRANSACTION_IN_PROGRESS,
  27. TRANSACTION_COMMITTED,
  28. TRANSACTION_COMMITTED_EMPTY,
  29. TRANSACTION_ABORTED
  30. ],
  31. [TRANSACTION_IN_PROGRESS]: [
  32. TRANSACTION_IN_PROGRESS,
  33. TRANSACTION_COMMITTED,
  34. TRANSACTION_ABORTED
  35. ],
  36. [TRANSACTION_COMMITTED]: [
  37. TRANSACTION_COMMITTED,
  38. TRANSACTION_COMMITTED_EMPTY,
  39. STARTING_TRANSACTION,
  40. NO_TRANSACTION
  41. ],
  42. [TRANSACTION_ABORTED]: [STARTING_TRANSACTION, NO_TRANSACTION],
  43. [TRANSACTION_COMMITTED_EMPTY]: [TRANSACTION_COMMITTED_EMPTY, NO_TRANSACTION]
  44. };
  45. })();
  46. /**
  47. * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
  48. * of the data read from replica sets and replica set shards.
  49. * @typedef {Object} ReadConcern
  50. * @property {'local'|'available'|'majority'|'linearizable'|'snapshot'} level The readConcern Level
  51. * @see https://docs.mongodb.com/manual/reference/read-concern/
  52. */
  53. /**
  54. * A MongoDB WriteConcern, which describes the level of acknowledgement
  55. * requested from MongoDB for write operations.
  56. * @typedef {Object} WriteConcern
  57. * @property {number|'majority'|string} [w=1] requests acknowledgement that the write operation has
  58. * propagated to a specified number of mongod hosts
  59. * @property {boolean} [j=false] requests acknowledgement from MongoDB that the write operation has
  60. * been written to the journal
  61. * @property {number} [wtimeout] a time limit, in milliseconds, for the write concern
  62. * @see https://docs.mongodb.com/manual/reference/write-concern/
  63. */
  64. /**
  65. * Configuration options for a transaction.
  66. * @typedef {Object} TransactionOptions
  67. * @property {ReadConcern} [readConcern] A default read concern for commands in this transaction
  68. * @property {WriteConcern} [writeConcern] A default writeConcern for commands in this transaction
  69. * @property {ReadPreference} [readPreference] A default read preference for commands in this transaction
  70. */
  71. /**
  72. * A class maintaining state related to a server transaction. Internal Only
  73. * @ignore
  74. */
  75. class Transaction {
  76. /**
  77. * Create a transaction
  78. *
  79. * @ignore
  80. * @param {TransactionOptions} [options] Optional settings
  81. */
  82. constructor(options) {
  83. options = options || {};
  84. this.state = TxnState.NO_TRANSACTION;
  85. this.options = {};
  86. const writeConcern = WriteConcern.fromOptions(options);
  87. if (writeConcern) {
  88. if (writeConcern.w <= 0) {
  89. throw new MongoError('Transactions do not support unacknowledged write concern');
  90. }
  91. this.options.writeConcern = writeConcern;
  92. }
  93. if (options.readConcern) {
  94. this.options.readConcern = ReadConcern.fromOptions(options);
  95. }
  96. if (options.readPreference) {
  97. this.options.readPreference = ReadPreference.fromOptions(options);
  98. }
  99. if (options.maxCommitTimeMS) {
  100. this.options.maxTimeMS = options.maxCommitTimeMS;
  101. }
  102. // TODO: This isn't technically necessary
  103. this._pinnedServer = undefined;
  104. this._recoveryToken = undefined;
  105. }
  106. get server() {
  107. return this._pinnedServer;
  108. }
  109. get recoveryToken() {
  110. return this._recoveryToken;
  111. }
  112. get isPinned() {
  113. return !!this.server;
  114. }
  115. /**
  116. * @ignore
  117. * @return Whether this session is presently in a transaction
  118. */
  119. get isActive() {
  120. return (
  121. [TxnState.STARTING_TRANSACTION, TxnState.TRANSACTION_IN_PROGRESS].indexOf(this.state) !== -1
  122. );
  123. }
  124. /**
  125. * Transition the transaction in the state machine
  126. * @ignore
  127. * @param {TxnState} state The new state to transition to
  128. */
  129. transition(nextState) {
  130. const nextStates = stateMachine[this.state];
  131. if (nextStates && nextStates.indexOf(nextState) !== -1) {
  132. this.state = nextState;
  133. if (this.state === TxnState.NO_TRANSACTION || this.state === TxnState.STARTING_TRANSACTION) {
  134. this.unpinServer();
  135. }
  136. return;
  137. }
  138. throw new MongoError(
  139. `Attempted illegal state transition from [${this.state}] to [${nextState}]`
  140. );
  141. }
  142. pinServer(server) {
  143. if (this.isActive) {
  144. this._pinnedServer = server;
  145. }
  146. }
  147. unpinServer() {
  148. this._pinnedServer = undefined;
  149. }
  150. }
  151. function isTransactionCommand(command) {
  152. return !!(command.commitTransaction || command.abortTransaction);
  153. }
  154. module.exports = { TxnState, Transaction, isTransactionCommand };