transactions.js 4.9 KB

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