fix(MongoDB): Fixes based on static analysis report.

This commit is contained in:
Matej Kenda
2025-11-27 17:44:52 +01:00
parent 94643762cf
commit c35d6ed17e
2 changed files with 29 additions and 28 deletions

View File

@@ -23,20 +23,20 @@ namespace MongoDB {
// MongoDB error codes that indicate retriable errors // MongoDB error codes that indicate retriable errors
namespace ErrorCodes enum class ErrorCode
{ {
const int NotMaster = 10107; NotMaster = 10107,
const int NotMasterNoSlaveOk = 13435; NotMasterNoSlaveOk = 13435,
const int NotMasterOrSecondary = 13436; NotMasterOrSecondary = 13436,
const int InterruptedAtShutdown = 11600; InterruptedAtShutdown = 11600,
const int InterruptedDueToReplStateChange = 11602; InterruptedDueToReplStateChange = 11602,
const int PrimarySteppedDown = 189; PrimarySteppedDown = 189,
const int ShutdownInProgress = 91; ShutdownInProgress = 91,
const int HostNotFound = 7; HostNotFound = 7,
const int HostUnreachable = 6; HostUnreachable = 6,
const int NetworkTimeout = 89; NetworkTimeout = 89,
const int SocketException = 9001; SocketException = 9001
} };
ReplicaSetConnection::ReplicaSetConnection(ReplicaSet& replicaSet, const ReadPreference& readPref): ReplicaSetConnection::ReplicaSetConnection(ReplicaSet& replicaSet, const ReadPreference& readPref):
@@ -266,21 +266,21 @@ bool ReplicaSetConnection::isRetriableMongoDBError(const OpMsgMessage& response)
// Check for error code // Check for error code
if (body.exists("code")) if (body.exists("code"))
{ {
int code = body.get<int>("code"); ErrorCode code = static_cast<ErrorCode>(body.get<int>("code"));
switch (code) switch (code)
{ {
case ErrorCodes::NotMaster: case ErrorCode::NotMaster:
case ErrorCodes::NotMasterNoSlaveOk: case ErrorCode::NotMasterNoSlaveOk:
case ErrorCodes::NotMasterOrSecondary: case ErrorCode::NotMasterOrSecondary:
case ErrorCodes::InterruptedAtShutdown: case ErrorCode::InterruptedAtShutdown:
case ErrorCodes::InterruptedDueToReplStateChange: case ErrorCode::InterruptedDueToReplStateChange:
case ErrorCodes::PrimarySteppedDown: case ErrorCode::PrimarySteppedDown:
case ErrorCodes::ShutdownInProgress: case ErrorCode::ShutdownInProgress:
case ErrorCodes::HostNotFound: case ErrorCode::HostNotFound:
case ErrorCodes::HostUnreachable: case ErrorCode::HostUnreachable:
case ErrorCodes::NetworkTimeout: case ErrorCode::NetworkTimeout:
case ErrorCodes::SocketException: case ErrorCode::SocketException:
return true; return true;
default: default:
return false; return false;

View File

@@ -224,7 +224,7 @@ void TopologyDescription::addServer(const Net::SocketAddress& address)
{ {
Mutex::ScopedLock lock(_mutex); Mutex::ScopedLock lock(_mutex);
auto [it, inserted] = _servers.try_emplace(address, address); auto [_, inserted] = _servers.try_emplace(address, address);
if (inserted) if (inserted)
{ {
updateTopologyType(); updateTopologyType();
@@ -271,7 +271,6 @@ void TopologyDescription::updateTopologyType()
int primaries = 0; int primaries = 0;
int secondaries = 0; int secondaries = 0;
int mongosCount = 0; int mongosCount = 0;
int unknownCount = 0;
int standaloneCount = 0; int standaloneCount = 0;
for (const auto& [address, server] : _servers) for (const auto& [address, server] : _servers)
@@ -294,7 +293,7 @@ void TopologyDescription::updateTopologyType()
standaloneCount++; standaloneCount++;
break; break;
case ServerDescription::Unknown: case ServerDescription::Unknown:
unknownCount++; // Unknown servers don't affect topology classification
break; break;
} }
} }
@@ -306,6 +305,8 @@ void TopologyDescription::updateTopologyType()
} }
else if (standaloneCount > 0 && _servers.size() == 1) else if (standaloneCount > 0 && _servers.size() == 1)
{ {
// Single standalone server - treat as Single topology
// Standalone servers behave like a single primary for read preferences
_type = Single; _type = Single;
} }
else if (primaries > 0) else if (primaries > 0)