Skip to content

Commit 808cf37

Browse files
mbroadstdaprahamian
authored andcommitted
feat: perform selection before cursor operation execution if needed
This is the counterpart to the previous commit which is temporarily required because cursors do not use `executeOperation`. This fix should no longer be required in a very short time.
1 parent 1a25876 commit 808cf37

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

lib/core/cursor.js

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const MongoNetworkError = require('./error').MongoNetworkError;
77
const mongoErrorContextSymbol = require('./error').mongoErrorContextSymbol;
88
const f = require('util').format;
99
const collationNotSupported = require('./utils').collationNotSupported;
10+
const ReadPreference = require('./topologies/read_preference');
1011
const BSON = retrieveBSON();
1112
const Long = BSON.Long;
1213

@@ -584,6 +585,20 @@ var nextFunction = function(self, callback) {
584585
Cursor.prototype._initializeCursor = function(callback) {
585586
const cursor = this;
586587

588+
// NOTE: this goes away once cursors use `executeOperation`
589+
if (cursor.topology.shouldCheckForSessionSupport()) {
590+
cursor.topology.selectServer(ReadPreference.primaryPreferred, err => {
591+
if (err) {
592+
callback(err);
593+
return;
594+
}
595+
596+
cursor.next(callback);
597+
});
598+
599+
return;
600+
}
601+
587602
// Very explicitly choose what is passed to selectServer
588603
const serverSelectOptions = {};
589604
if (cursor.cursorState.session) {

lib/core/sdam/topology.js

+11
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ class Topology extends EventEmitter {
391391
}
392392

393393
// Sessions related methods
394+
395+
/**
396+
* @return Whether the topology should initiate selection to determine session support
397+
*/
398+
shouldCheckForSessionSupport() {
399+
return (
400+
(this.description.type === TopologyType.Single && !this.description.hasKnownServers) ||
401+
!this.description.hasDataBearingServers
402+
);
403+
}
404+
394405
/**
395406
* @return Whether sessions are supported on the current topology
396407
*/

lib/operations/execute_operation.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const OperationBase = require('./operation').OperationBase;
66
const ReadPreference = require('../core').ReadPreference;
77
const isRetryableError = require('../core/error').isRetryableError;
88
const maxWireVersion = require('../core/utils').maxWireVersion;
9-
const TopologyType = require('../core/sdam/topology_description').TopologyType;
109

1110
/**
1211
* Executes the given operation with provided arguments.
@@ -33,7 +32,7 @@ function executeOperation(topology, operation, callback) {
3332
if (
3433
topology.description != null &&
3534
!operation.hasAspect(Aspect.SKIP_SESSION) &&
36-
shouldCheckForSessionSupport(topology)
35+
topology.shouldCheckForSessionSupport()
3736
) {
3837
// TODO: this is only supported for unified topology, the first part of this check
3938
// should go away when we drop legacy topology types.
@@ -162,11 +161,4 @@ function executeWithServerSelection(topology, operation, callback) {
162161
});
163162
}
164163

165-
function shouldCheckForSessionSupport(topology) {
166-
return (
167-
(topology.description.type === TopologyType.Single && !topology.description.hasKnownServers) ||
168-
!topology.description.hasDataBearingServers
169-
);
170-
}
171-
172164
module.exports = executeOperation;

0 commit comments

Comments
 (0)