Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
refactor(sessions): move increment methods onto ClientSession
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Apr 17, 2018
1 parent 36bf925 commit 2e1e09f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
20 changes: 19 additions & 1 deletion lib/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ClientSession extends EventEmitter {
* @param {Boolean} [options.causalConsistency] Whether causal consistency should be enabled on this session
* @param {Boolean} [options.autoStartTransaction=false] When enabled this session automatically starts a transaction with the provided defaultTransactionOptions.
* @param {Object} [options.defaultTransactionOptions] The default TransactionOptions to use for transactions started on this session.
* @param {Object} [clientOptions] Optional settings provided when creating a client in the porcelain driver
*/
constructor(topology, sessionPool, options, clientOptions) {
super();
Expand Down Expand Up @@ -136,6 +137,23 @@ class ClientSession extends EventEmitter {
return this.id.id.buffer.equals(session.id.id.buffer);
}

/**
* Increment the transaction number on the internal ServerSession
*/
incrementTransactionNumber() {
this.serverSession.txnNumber++;
}

/**
* Increment the statement id on the internal ServerSession
*
* @param {Number} [operationCount] the number of operations performed
*/
incrementStatementId(operationCount) {
operationCount = operationCount || 1;
this.serverSession.stmtId += operationCount;
}

/**
* @returns whether this session is current in a transaction or not
*/
Expand Down Expand Up @@ -265,7 +283,7 @@ function endTransaction(clientSession, commandName, callback) {
const command = { [commandName]: 1 };
if (clientSession.transactionOptions.writeConcern) {
Object.assign(command, { writeConcern: clientSession.transactionOptions.writeConcern });
} else if (clientSession.clientOptions.w) {
} else if (clientSession.clientOptions && clientSession.clientOptions.w) {
Object.assign(command, { writeConcern: { w: clientSession.clientOptions.w } });
}

Expand Down
6 changes: 2 additions & 4 deletions lib/topologies/replset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const Interval = require('./shared').Interval;
const createClientInfo = require('./shared').createClientInfo;
const SessionMixins = require('./shared').SessionMixins;
const isRetryableWritesSupported = require('./shared').isRetryableWritesSupported;
const incrementTransactionNumber = require('./shared').incrementTransactionNumber;
const incrementStatementId = require('./shared').incrementStatementId;
const relayEvents = require('./shared').relayEvents;

var MongoCR = require('../auth/mongocr'),
Expand Down Expand Up @@ -1244,7 +1242,7 @@ function executeWriteOperation(args, options, callback) {

// increment and assign txnNumber
if (willRetryWrite) {
incrementTransactionNumber(options.session);
options.session.incrementTransactionNumber();
}

// optionally autostart transaction if requested
Expand All @@ -1254,7 +1252,7 @@ function executeWriteOperation(args, options, callback) {

// We need to increment the statement id if we're in a transaction
if (options.session && options.session.inTransaction()) {
incrementStatementId(options.session, ops.length);
options.session.incrementStatementId(ops.length);
}
}

Expand Down
22 changes: 0 additions & 22 deletions lib/topologies/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,26 +417,6 @@ const isRetryableWritesSupported = function(topology) {
return true;
};

/**
* Increment the transaction number on the ServerSession contained by the provided ClientSession
*
* @param {ClientSession} session
*/
function incrementTransactionNumber(session) {
session.serverSession.txnNumber++;
}

/**
* Increment the statement id on the ServerSession contained by the provided ClientSession
*
* @param {ClientSession} session the client sessions
* @param {Number} [operationCount] the number of operations performed
*/
function incrementStatementId(session, operationCount) {
operationCount = operationCount || 1;
session.serverSession.stmtId += operationCount;
}

/**
* Relays events for a given listener and emitter
*
Expand All @@ -461,6 +441,4 @@ module.exports.diff = diff;
module.exports.Interval = Interval;
module.exports.Timeout = Timeout;
module.exports.isRetryableWritesSupported = isRetryableWritesSupported;
module.exports.incrementTransactionNumber = incrementTransactionNumber;
module.exports.incrementStatementId = incrementStatementId;
module.exports.relayEvents = relayEvents;
14 changes: 6 additions & 8 deletions lib/wireprotocol/3_2_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const f = require('util').format;
const MongoError = require('../error').MongoError;
const MongoNetworkError = require('../error').MongoNetworkError;
const getReadPreference = require('./shared').getReadPreference;
const incrementStatementId = require('../topologies/shared').incrementStatementId;
const BSON = retrieveBSON();
const Long = BSON.Long;

Expand Down Expand Up @@ -41,15 +40,14 @@ function decorateWithTransactionsData(command, session) {

if (serverSession.stmtId === 0) {
command.startTransaction = true;
command.readConcern = { level: 'snapshot' };

if (session.transactionOptions.readConcern) {
Object.assign(command, { readConcern: session.transactionOptions.readConcern });
} else if (session.clientOptions.readConcern) {
Object.assign(command, { readConcern: session.clientOptions.readConcern });
const readConcern = session.transactionOptions.readConcern || session.clientOptions.readConcern;
if (readConcern) {
command.readConcern = readConcern;
}

if (session.supports.causalConsistency && session.operationTime) {
command.readConcern = command.readConcern || {};
Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
}
} else {
Expand Down Expand Up @@ -318,7 +316,7 @@ WireProtocol.prototype.getMore = function(

// We need to increment the statement id if we're in a transaction
if (options.session && options.session.inTransaction()) {
incrementStatementId(options.session);
options.session.incrementStatementId();
}

// Write out the getMore command
Expand Down Expand Up @@ -353,7 +351,7 @@ WireProtocol.prototype.command = function(bson, ns, cmd, cursorState, topology,

// We need to increment the statement id if we're in a transaction
if (options.session && options.session.inTransaction()) {
incrementStatementId(options.session);
options.session.incrementStatementId();
}

return query;
Expand Down

0 comments on commit 2e1e09f

Please sign in to comment.