Skip to content

Commit

Permalink
refactor: synchronize pool clearing and mark unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
dariakp committed Aug 29, 2022
1 parent e2940bf commit d7ce532
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
32 changes: 15 additions & 17 deletions src/operations/execute_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,23 @@ function executeWithServerSelection<TResult>(
}

// select a new server, and attempt to retry the operation
setTimeout(() => {
topology.selectServer(selector, serverSelectionOptions, (error?: Error, server?: Server) => {
if (!error && isWriteOperation && !supportsRetryableWrites(server)) {
return callback(
new MongoUnexpectedServerResponseError(
'Selected server does not support retryable writes'
)
);
}
topology.selectServer(selector, serverSelectionOptions, (error?: Error, server?: Server) => {
if (!error && isWriteOperation && !supportsRetryableWrites(server)) {
return callback(
new MongoUnexpectedServerResponseError(
'Selected server does not support retryable writes'
)
);
}

if (error || !server) {
return callback(
error ?? new MongoUnexpectedServerResponseError('Server selection failed without error')
);
}
if (error || !server) {
return callback(
error ?? new MongoUnexpectedServerResponseError('Server selection failed without error')
);
}

operation.execute(server, session, callback);
});
}, 1);
operation.execute(server, session, callback);
});
}

if (
Expand Down
7 changes: 5 additions & 2 deletions src/sdam/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,10 @@ function makeOperationHandler(
// In load balanced mode we never mark the server as unknown and always
// clear for the specific service id.

server.s.pool.clear(connection.serviceId);
if (!server.loadBalanced) {
markServerUnknown(server, error);
} else {
server.s.pool.clear(connection.serviceId);
}
}
} else {
Expand All @@ -516,7 +517,9 @@ function makeOperationHandler(
if (isSDAMUnrecoverableError(error)) {
if (shouldHandleStateChangeError(server, error)) {
if (maxWireVersion(server) <= 7 || isNodeShuttingDownError(error)) {
server.s.pool.clear(connection.serviceId);
if (server.loadBalanced) {
server.s.pool.clear(connection.serviceId);
}
}

if (!server.loadBalanced) {
Expand Down
7 changes: 6 additions & 1 deletion src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,12 +813,17 @@ function updateServers(topology: Topology, incomingServerDescription?: ServerDes
if (incomingServerDescription && topology.s.servers.has(incomingServerDescription.address)) {
const server = topology.s.servers.get(incomingServerDescription.address);
if (server) {
server.s.description = incomingServerDescription;
if (incomingServerDescription.error) {
server.s.pool.clear();
return;
}

const newTopologyType = topology.s.description.type;
const shouldMarkPoolReady =
incomingServerDescription.isDataBearing ||
(incomingServerDescription.type !== ServerType.Unknown &&
newTopologyType === TopologyType.Single);
server.s.description = incomingServerDescription;
if (shouldMarkPoolReady) {
server.s.pool.ready();
}
Expand Down

0 comments on commit d7ce532

Please sign in to comment.