Library: MongoDB
Package: MongoDB
Header: Poco/MongoDB/ServerDescription.h
Description
Represents the state of a single MongoDB server in a replica set.
This class stores metadata about a MongoDB server obtained from the 'hello' command response (requires MongoDB 5.1 or later), including: - Server type (primary, secondary, arbiter, etc.) - Replica set membership information - Round-trip time for server selection - Server tags for tag-based read preferences
THREAD SAFETY: This class is NOT thread-safe. External synchronization is required if instances are accessed from multiple threads.
Member Summary
Member Functions: address, error, hasError, isPrimary, isSecondary, isWritable, lastUpdateTime, markError, operator !=, operator =, operator ==, reset, roundTripTime, setAddress, setName, tags, type, typeToString, updateFromHelloResponse
Enumerations
ServerType
MongoDB server type enumeration
Server type not yet determined
Standalone MongoDB instance (not in a replica set)
Replica set primary (writable)
Replica set secondary (read-only)
Replica set arbiter (no data, votes only)
Other replica set member type
Ghost member (removed or not yet initialized)
MongoDB sharding router
Constructors
ServerDescription
Creates an Unknown server description.
ServerDescription
explicit ServerDescription(
const Net::SocketAddress & address
);
Creates an Unknown server description for the given address.
ServerDescription
ServerDescription(
const ServerDescription & other
);
Copy constructor.
ServerDescription
ServerDescription(
ServerDescription && other
) noexcept;
Move constructor.
Destructor
~ServerDescription
Destroys the ServerDescription.
Member Functions
address
[[nodiscard]]
const Net::SocketAddress & address() const;
Returns the server address.
error
[[nodiscard]]
const std::string & error() const;
Returns the last error message, or empty string if no error.
hasError
[[nodiscard]]
bool hasError() const;
Returns true if the last update attempt resulted in an error.
isPrimary
[[nodiscard]]
bool isPrimary() const;
Returns true if this is a primary server (RsPrimary) or a standalone server. Standalone servers are treated as primaries for read preference purposes.
isSecondary
[[nodiscard]]
bool isSecondary() const;
Returns true if this is a secondary server.
isWritable
[[nodiscard]]
bool isWritable() const;
Returns true if this server can accept write operations. Only primary servers are writable.
lastUpdateTime
[[nodiscard]]
Timestamp lastUpdateTime() const;
Returns the timestamp of the last successful update.
markError
void markError(
const std::string & errorMessage
);
Marks this server as having an error. This sets the type to Unknown and stores the error message.
operator !=
bool operator != (
const ServerDescription & other
) const;
Inequality comparison operator.
operator =
ServerDescription & operator = (
const ServerDescription & other
);
Assignment operator.
operator =
ServerDescription & operator = (
ServerDescription && other
) noexcept;
Move assignment operator.
operator ==
bool operator == (
const ServerDescription & other
) const;
Equality comparison operator. Compares type, address, setName, and error state.
reset
void reset();
Resets the server description to Unknown state.
roundTripTime
[[nodiscard]]
Poco::Int64 roundTripTime() const;
Returns the round-trip time in microseconds. This is used for "nearest" read preference selection.
setAddress
void setAddress(
const Net::SocketAddress & address
);
Sets the server address.
setName
[[nodiscard]]
const std::string & setName() const;
Returns the replica set name, or empty string if not in a replica set.
tags
[[nodiscard]]
const Document & tags() const;
Returns the server tags for tag-based read preferences. Returns an empty document if no tags are configured.
type
[[nodiscard]]
ServerType type() const;
Returns the server type.
typeToString
[[nodiscard]]
static std::string typeToString(
ServerType type
);
Converts a server type enum to a human-readable string. Returns "PRIMARY", "SECONDARY", "ARBITER", "STANDALONE", "MONGOS", "OTHER", "GHOST", or "UNKNOWN".
updateFromHelloResponse
[[nodiscard]]
std::vector < Net::SocketAddress > updateFromHelloResponse(
const Document & helloResponse,
Poco::Int64 rttMicros
);
Updates the server description from a 'hello' command response. The rttMicros parameter should contain the round-trip time of the hello command in microseconds. Returns a list of all replica set members (hosts, passives, arbiters) discovered in the hello response.