Skip to content

Commit 1a25876

Browse files
mbroadstdaprahamian
authored andcommitted
feat: perform selection before operation execution if needed
In accordance with the drivers-sessions and SDAM specs, we must perform server selection under certain circumstances in order to ensure we have accurate information about sessions support. NODE-2068
1 parent d0ccb56 commit 1a25876

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/operations/execute_operation.js

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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;
910

1011
/**
1112
* Executes the given operation with provided arguments.
@@ -29,6 +30,25 @@ function executeOperation(topology, operation, callback) {
2930
throw new TypeError('This method requires a valid operation instance');
3031
}
3132

33+
if (
34+
topology.description != null &&
35+
!operation.hasAspect(Aspect.SKIP_SESSION) &&
36+
shouldCheckForSessionSupport(topology)
37+
) {
38+
// TODO: this is only supported for unified topology, the first part of this check
39+
// should go away when we drop legacy topology types.
40+
topology.selectServer(ReadPreference.primaryPreferred, err => {
41+
if (err) {
42+
callback(err);
43+
return;
44+
}
45+
46+
executeOperation(topology, operation, callback);
47+
});
48+
49+
return;
50+
}
51+
3252
const Promise = topology.s.promiseLibrary;
3353

3454
// The driver sessions spec mandates that we implicitly create sessions for operations
@@ -142,4 +162,11 @@ function executeWithServerSelection(topology, operation, callback) {
142162
});
143163
}
144164

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

0 commit comments

Comments
 (0)