-
Notifications
You must be signed in to change notification settings - Fork 26
DOCSP-49056: Server Selection #618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,68 +1,193 @@ | ||||||||
| .. TODO: Server Selection page | ||||||||
|
|
||||||||
| Why Does the Driver Throw a Timeout During Server Selection? | ||||||||
| ------------------------------------------------------------ | ||||||||
|
|
||||||||
| Each driver operation requires that you choose a healthy server | ||||||||
| satisfying the :manual:`server selection criteria | ||||||||
| </core/read-preference-mechanics>`. If you do not select an appropriate | ||||||||
| server within the `server selection timeout <{+new-api-root+}/MongoDB.Driver.Legacy/MongoDB.Driver.MongoServerSettings.ServerSelectionTimeout.html>`__, the driver throws a | ||||||||
| server selection timeout exception. The exception looks similar to the | ||||||||
| following: | ||||||||
|
|
||||||||
| .. code-block:: none | ||||||||
|
|
||||||||
| A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. | ||||||||
| Client view of cluster state is | ||||||||
| { | ||||||||
| ClusterId : "1", | ||||||||
| Type : "Unknown", | ||||||||
| State : "Disconnected", | ||||||||
| Servers : | ||||||||
| [{ | ||||||||
| ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", | ||||||||
| EndPoint: "Unspecified/localhost:27017", | ||||||||
| ReasonChanged: "Heartbeat", | ||||||||
| State: "Disconnected", | ||||||||
| ServerVersion: , | ||||||||
| TopologyVersion: , | ||||||||
| Type: "Unknown", | ||||||||
| HeartbeatException: "<exception details>" | ||||||||
| }] | ||||||||
| }. | ||||||||
|
|
||||||||
| The error message consists of multiple parts: | ||||||||
|
|
||||||||
| 1. The server selection timeout (30000 ms). | ||||||||
| #. The server selectors considered (``CompositeServerSelector`` | ||||||||
| containing ``AreSessionsSupportedServerSelector``, | ||||||||
| ``LatencyLimitingServerSelector``, and | ||||||||
| ``OperationsCountServerSelector``). | ||||||||
| #. The driver’s current view of the cluster topology. The list of | ||||||||
| servers that the driver is aware of is a key part of this view. Each | ||||||||
| server description contains an exhaustive description of its current | ||||||||
| state including information about an endpoint, a server version, a | ||||||||
| server type, and its current health state. If the server encounters issues in | ||||||||
| reporting its health, ``HeartbeatException`` contains the exception from the | ||||||||
| last failed heartbeat. Analyzing the ``HeartbeatException`` on each | ||||||||
| cluster node can assist in diagnosing most server selection issues. | ||||||||
| The following heartbeat exceptions are common: | ||||||||
|
|
||||||||
| * ``No connection could be made because the target machine actively | ||||||||
| refused it``: The driver cannot see this cluster node. This can be | ||||||||
| because the cluster node has crashed, a firewall is preventing | ||||||||
| network traffic from reaching the cluster node or port, or some other | ||||||||
| network error is preventing traffic from being successfully routed to | ||||||||
| the cluster node. | ||||||||
| * ``Attempted to read past the end of the stream``: This error | ||||||||
| happens when the driver cannot connect to the cluster nodes due to a | ||||||||
| network error, misconfigured firewall, or other network issue. To | ||||||||
| address this exception, ensure that all cluster nodes are reachable. | ||||||||
| This error commonly occurs when the client machine’s IP address is | ||||||||
| not configured in the Atlas IPs Access List, which can be found under | ||||||||
| the :guilabel:`Network Access` tab for your Atlas Project. | ||||||||
| * ``The remote certificate is invalid according to the validation | ||||||||
| procedure``: This error typically indicates a TLS/SSL-related problem | ||||||||
| such as an expired/invalid certificate or an untrusted root CA. You | ||||||||
| can use tools like ``openssl s_client`` to debug TLS/SSL-related | ||||||||
| certificate problems. | ||||||||
| ========================== | ||||||||
| Customize Server Selection | ||||||||
| ========================== | ||||||||
|
|
||||||||
| .. contents:: On this page | ||||||||
| :local: | ||||||||
| :backlinks: none | ||||||||
| :depth: 1 | ||||||||
| :class: singlecol | ||||||||
|
|
||||||||
| .. facet:: | ||||||||
| :name: genre | ||||||||
| :values: reference | ||||||||
|
|
||||||||
| .. meta:: | ||||||||
| :keywords: code example, read preference, write | ||||||||
|
|
||||||||
| Overview | ||||||||
| -------- | ||||||||
|
|
||||||||
| All MongoDB drivers follow a defined algorithm when selecting a server to read or write | ||||||||
| from. By using the ``ClusterConfigurator`` property of a ``MongoClient`` object, you can | ||||||||
| customize this algorithm to choose the server that works best for your application. | ||||||||
|
|
||||||||
| .. important:: | ||||||||
|
|
||||||||
| Customizing the server-selection algorithm can have unintended consequences, | ||||||||
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| such as degraded read or write performance. | ||||||||
|
|
||||||||
| Default Algorithm | ||||||||
| ----------------- | ||||||||
|
|
||||||||
| When the {+driver-short+} executes a read operation, it performs the following steps, | ||||||||
| in order, to select a MongoDB deployment: | ||||||||
|
|
||||||||
| 1. From the list of known servers, the {+driver-short+} selects all servers | ||||||||
| that match the active read preference. | ||||||||
|
|
||||||||
| #. If at least one readable server exists, the {+driver-short+} calls the user-defined | ||||||||
| server-selector function and passes in the list from the previous step. | ||||||||
|
|
||||||||
| #. The {+driver-short+} applies the ``LocalThreshold`` connection setting to the list of | ||||||||
| servers returned from the function. | ||||||||
|
|
||||||||
| #. The {+driver-short+} selects a server at random from the servers still on the list and | ||||||||
| executes the operation against this server. | ||||||||
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| When the {+driver-short+} executes a write operation, it begins by selecting all writeable | ||||||||
| servers, not just those that match the active read preference. The remaining steps are | ||||||||
| identical. | ||||||||
|
|
||||||||
| To learn more about the default server-selection algorithm, which the driver follows | ||||||||
| when you don't specify any custom server-selection logic, see | ||||||||
|
||||||||
| To learn more about the default server-selection algorithm, which the driver follows | |
| when you don't specify any custom server-selection logic, see | |
| The driver follows a default server-selection algorithm when you don't specify any custom server-selection logic. To learn more about the default server-selection algorithm, see |
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is wall-clock time a standard term? I've never heard that before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's one of many ways to refer to real time. Can change it to that instead.
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using MongoDB.Driver; | ||
| using MongoDB.Driver.Core.Clusters; | ||
| using MongoDB.Driver.Core.Clusters.ServerSelectors; | ||
| using MongoDB.Driver.Core.Servers; | ||
|
|
||
| public class ServerSelection | ||
| { | ||
| // Replace with your connection string | ||
| private const string MongoConnectionString = "<connection URI>"; | ||
|
|
||
| public static void Main(string[] args) | ||
| { | ||
| { | ||
| // start-server-selector | ||
| var settings = MongoClientSettings.FromConnectionString("<connection string>"); | ||
| var clusterConfigurator = builder => | ||
| { | ||
| builder.ConfigureCluster(c => | ||
| c.With(PreServerSelector: new RandomServerSelector())); | ||
| }; | ||
|
|
||
| settings.ClusterConfigurator = clusterConfigurator; | ||
| var client = new MongoClient(settings); | ||
| // end-server-selector | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // start-custom-class | ||
| public class CustomServerSelector : IServerSelector | ||
| { | ||
| public IEnumerable<ServerDescription> SelectServers(ClusterDescription cluster, | ||
| IEnumerable<ServerDescription> servers) | ||
| { | ||
| return servers.Where(server => server.Type == ServerType.ReplicaSetSecondary); | ||
| } | ||
| } | ||
| // end-custom-class |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| Driver Throws a Timeout During Server Selection | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Each driver operation requires that you choose a server that | ||
| satisfies the :manual:`server selection criteria | ||
| </core/read-preference-mechanics>`. If you do not select an appropriate | ||
| server within the `server selection timeout <{+new-api-root+}/MongoDB.Driver.Legacy/MongoDB.Driver.MongoServerSettings.ServerSelectionTimeout.html>`__, the driver throws a | ||
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| server selection timeout exception. The exception looks similar to the | ||
| following: | ||
|
|
||
| .. code-block:: none | ||
| A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. | ||
| Client view of cluster state is | ||
| { | ||
| ClusterId : "1", | ||
| Type : "Unknown", | ||
| State : "Disconnected", | ||
| Servers : | ||
| [{ | ||
| ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", | ||
| EndPoint: "Unspecified/localhost:27017", | ||
| ReasonChanged: "Heartbeat", | ||
| State: "Disconnected", | ||
| ServerVersion: , | ||
| TopologyVersion: , | ||
| Type: "Unknown", | ||
| HeartbeatException: "<exception details>" | ||
| }] | ||
| }. | ||
| The error message consists of multiple parts: | ||
|
|
||
| 1. The server selection timeout (30000 ms). | ||
| #. The server selectors considered (``CompositeServerSelector`` | ||
| containing ``AreSessionsSupportedServerSelector``, | ||
| ``LatencyLimitingServerSelector``, and | ||
| ``OperationsCountServerSelector``). | ||
| #. The driver’s current view of the cluster topology. The list of | ||
| servers that the driver is aware of is a key part of this view. Each | ||
| server description contains an exhaustive description of its current | ||
| state including information about an endpoint, a server version, a | ||
| server type, and its current health state. If the server encounters issues in | ||
| reporting its health, ``HeartbeatException`` contains the exception from the | ||
| last failed heartbeat. Analyzing the ``HeartbeatException`` on each | ||
| cluster node can assist in diagnosing most server selection issues. | ||
| The following heartbeat exceptions are common: | ||
|
|
||
| * ``No connection could be made because the target machine actively | ||
| refused it``: The driver cannot see this cluster node. This can be | ||
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| because the cluster node has crashed, a firewall is preventing | ||
| network traffic from reaching the cluster node or port, or some other | ||
| network error is preventing traffic from being successfully routed to | ||
| the cluster node. | ||
| * ``Attempted to read past the end of the stream``: This error | ||
| happens when the driver cannot connect to the cluster nodes due to a | ||
| network error, misconfigured firewall, or other network issue. To | ||
| address this exception, ensure that all cluster nodes are reachable. | ||
| This error commonly occurs when the client machine’s IP address is | ||
| not configured in the Atlas IPs Access List, which can be found under | ||
| the :guilabel:`Network Access` tab for your Atlas Project. | ||
mcmorisi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * ``The remote certificate is invalid according to the validation | ||
| procedure``: This error typically indicates a TLS/SSL-related problem | ||
| such as an expired/invalid certificate or an untrusted root CA. You | ||
| can use tools like ``openssl s_client`` to debug TLS/SSL-related | ||
| certificate problems. | ||
Uh oh!
There was an error while loading. Please reload this page.