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

Commit 189e428

Browse files
daprahamianmbroadst
authored andcommitted
feat: update proxy selection to consider pinned server on session
1 parent da13e55 commit 189e428

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

lib/cursor.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,16 @@ function initializeCursor(cursor, callback) {
606606
}
607607
}
608608

609-
return cursor.topology.selectServer(cursor.options, (err, server) => {
609+
// Very explicitly choose what is passed to selectServer
610+
const serverSelectOptions = {};
611+
if (cursor.cursorState.session) {
612+
serverSelectOptions.session = cursor.cursorState.session;
613+
}
614+
if (cursor.options.readPreference) {
615+
serverSelectOptions.readPreference = cursor.options.readPreference;
616+
}
617+
618+
return cursor.topology.selectServer(serverSelectOptions, (err, server) => {
610619
if (err) {
611620
const disconnectHandler = cursor.disconnectHandler;
612621
if (disconnectHandler != null) {

lib/topologies/mongos.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,14 @@ function connectProxies(self, servers) {
464464
}
465465
}
466466

467-
function pickProxy(self) {
467+
function pickProxy(self, session) {
468+
// TODO: Destructure :)
469+
const transaction = session && session.transaction;
470+
471+
if (transaction && transaction.server) {
472+
return transaction.server;
473+
}
474+
468475
// Get the currently connected Proxies
469476
var connectedProxies = self.connectedProxies.slice(0);
470477

@@ -488,15 +495,22 @@ function pickProxy(self) {
488495
}
489496
});
490497

498+
let proxy;
499+
491500
// We have no connectedProxies pick first of the connected ones
492501
if (connectedProxies.length === 0) {
493-
return self.connectedProxies[0];
502+
proxy = self.connectedProxies[0];
503+
} else {
504+
// Get proxy
505+
proxy = connectedProxies[self.index % connectedProxies.length];
506+
// Update the index
507+
self.index = (self.index + 1) % connectedProxies.length;
508+
}
509+
510+
if (transaction) {
511+
transaction.pinServer(proxy);
494512
}
495513

496-
// Get proxy
497-
var proxy = connectedProxies[self.index % connectedProxies.length];
498-
// Update the index
499-
self.index = (self.index + 1) % connectedProxies.length;
500514
// Return the proxy
501515
return proxy;
502516
}
@@ -846,7 +860,7 @@ var executeWriteOperation = function(self, op, ns, ops, options, callback) {
846860
options = options || {};
847861

848862
// Pick a server
849-
let server = pickProxy(self);
863+
let server = pickProxy(self, options.session);
850864
// No server found error out
851865
if (!server) return callback(new MongoError('no mongos proxy available'));
852866

@@ -866,7 +880,7 @@ var executeWriteOperation = function(self, op, ns, ops, options, callback) {
866880
}
867881

868882
// Pick another server
869-
server = pickProxy(self);
883+
server = pickProxy(self, options.session);
870884

871885
// No server found error out with original error
872886
if (!server || !isRetryableWritesSupported(server)) {
@@ -1007,7 +1021,7 @@ Mongos.prototype.command = function(ns, cmd, options, callback) {
10071021
var self = this;
10081022

10091023
// Pick a proxy
1010-
var server = pickProxy(self);
1024+
var server = pickProxy(self, options.session);
10111025

10121026
// Topology is not connected, save the call in the provided store to be
10131027
// Executed at some point when the handler deems it's reconnected
@@ -1087,7 +1101,8 @@ Mongos.prototype.cursor = function(ns, cmd, options) {
10871101
*
10881102
* @method
10891103
* @param {function} selector Unused
1090-
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
1104+
* @param {ReadPreference} [options.readPreference] Unused
1105+
* @param {ClientSession} [options.session] Specify a session if it is being used
10911106
* @param {function} callback
10921107
*/
10931108
Mongos.prototype.selectServer = function(selector, options, callback) {
@@ -1097,7 +1112,7 @@ Mongos.prototype.selectServer = function(selector, options, callback) {
10971112
(callback = options), (options = selector), (selector = undefined);
10981113
options = options || {};
10991114

1100-
const server = pickProxy(this);
1115+
const server = pickProxy(this, options.session);
11011116
if (this.s.debug) this.emit('pickedServer', null, server);
11021117
callback(null, server);
11031118
};

lib/topologies/replset.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ ReplSet.prototype.isDestroyed = function() {
10741074
* @method
10751075
* @param {function} selector Unused
10761076
* @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
1077+
* @param {ClientSession} [options.session] Unused
10771078
* @param {function} callback
10781079
*/
10791080
ReplSet.prototype.selectServer = function(selector, options, callback) {

lib/topologies/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,10 @@ Server.prototype.connections = function() {
778778

779779
/**
780780
* Selects a server
781+
* @method
782+
* @param {function} selector Unused
783+
* @param {ReadPreference} [options.readPreference] Unused
784+
* @param {ClientSession} [options.session] Unused
781785
* @return {Server}
782786
*/
783787
Server.prototype.selectServer = function(selector, options, callback) {

0 commit comments

Comments
 (0)