Skip to content

Commit

Permalink
feat: perform selection before operation execution if needed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbroadst authored and daprahamian committed Aug 13, 2019
1 parent d0ccb56 commit 1a25876
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/operations/execute_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const OperationBase = require('./operation').OperationBase;
const ReadPreference = require('../core').ReadPreference;
const isRetryableError = require('../core/error').isRetryableError;
const maxWireVersion = require('../core/utils').maxWireVersion;
const TopologyType = require('../core/sdam/topology_description').TopologyType;

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

if (
topology.description != null &&
!operation.hasAspect(Aspect.SKIP_SESSION) &&
shouldCheckForSessionSupport(topology)
) {
// TODO: this is only supported for unified topology, the first part of this check
// should go away when we drop legacy topology types.
topology.selectServer(ReadPreference.primaryPreferred, err => {
if (err) {
callback(err);
return;
}

executeOperation(topology, operation, callback);
});

return;
}

const Promise = topology.s.promiseLibrary;

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

function shouldCheckForSessionSupport(topology) {
return (
(topology.description.type === TopologyType.Single && !topology.description.hasKnownServers) ||
!topology.description.hasDataBearingServers
);
}

module.exports = executeOperation;

0 comments on commit 1a25876

Please sign in to comment.