Skip to content

Commit 2c274cf

Browse files
server selection updates (#10)
1 parent 75f966c commit 2c274cf

File tree

1 file changed

+51
-59
lines changed

1 file changed

+51
-59
lines changed
Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
11
.. uses server-selection.rst
22

3-
Server Selector Example
4-
=======================
3+
.. _pymongo-server-selection:
54

6-
Users can exert fine-grained control over the `server selection algorithm`_
7-
by setting the ``server_selector`` option on the ``~pymongo.MongoClient``
8-
to an appropriate callable. This example shows how to use this functionality
9-
to prefer servers running on ``localhost``.
5+
================
6+
Server Selection
7+
================
108

9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
1114

12-
.. warning:
15+
.. facet::
16+
:name: genre
17+
:values: reference
1318

14-
.. code-block:: python
15-
16-
Use of custom server selector functions is a power user feature. Misusing
17-
custom server selectors can have unintended consequences such as degraded
18-
read/write performance.
19-
20-
21-
.. code-block:: python
19+
.. meta::
20+
:keywords: code example
2221

23-
from pymongo import MongoClient
22+
Users can exert fine-grained control over the :manual:`Server Selection
23+
Algorithm </core/read-preference-mechanics/>` by setting the ``server_selector``
24+
option on the ``~pymongo.MongoClient`` to an appropriate callable. This guide
25+
shows you how to use this functionality to prefer servers running on ``localhost``.
2426

27+
.. warning::
2528

26-
.. _server selection algorithm: https://mongodb.com/docs/manual/core/read-preference-mechanics/
27-
29+
Use of custom server selector functions is a power-user feature. Misusing
30+
custom server selectors can have unintended consequences, such as degraded
31+
read or write performance.
2832

2933
Example: Selecting Servers Running on ``localhost``
3034
---------------------------------------------------
3135

32-
To start, we need to write the server selector function that will be used.
36+
First, write the server selector function.
3337
The server selector function should accept a list of
34-
``~pymongo.server_description.ServerDescription`` objects and return a
38+
``~pymongo.server_description.ServerDescription`` objects, and return a
3539
list of server descriptions that are suitable for the read or write operation.
3640
A server selector must not create or modify
3741
``~pymongo.server_description.ServerDescription`` objects, and must return
3842
the selected instances unchanged.
3943

40-
In this example, we write a server selector that prioritizes servers running on
44+
This example shows a server selector that prioritizes servers running on
4145
``localhost``. This can be desirable when using a sharded cluster with multiple
42-
``mongos``, as locally run queries are likely to see lower latency and higher
43-
throughput. Please note, however, that it is highly dependent on the
44-
application if preferring ``localhost`` is beneficial or not.
46+
``mongos`` servers, because locally run queries usually have lower latency and higher
47+
throughput. However, the benefit of preferring ``localhost`` is highly dependent on the
48+
application.
4549

46-
In addition to comparing the hostname with ``localhost``, our server selector
50+
In addition to comparing the hostname with ``localhost``, the server selector
4751
function accounts for the edge case when no servers are running on
48-
``localhost``. In this case, we allow the default server selection logic to
49-
prevail by passing through the received server description list unchanged.
50-
Failure to do this would render the client unable to communicate with MongoDB
51-
in the event that no servers were running on ``localhost``.
52-
53-
54-
The described server selection logic is implemented in the following server
55-
selector function:
52+
``localhost``. In this case, the default server selection logic
53+
prevails by returning the received server description list unchanged.
54+
Failure to do this renders the client unable to communicate with MongoDB
55+
if no servers are running on ``localhost``.
5656

57+
The following example implements the server selection logic:
5758

5859
.. code-block:: python
5960

@@ -66,47 +67,38 @@ selector function:
6667
... return servers
6768
...
6869

69-
70-
71-
Finally, we can create a ``~pymongo.MongoClient`` instance with this
72-
server selector.
73-
70+
Finally, create a ``~pymongo.MongoClient`` instance with the
71+
server selector:
7472

7573
.. code-block:: python
7674

7775
>>> client = MongoClient(server_selector=server_selector)
7876

79-
80-
8177
Server Selection Process
8278
------------------------
8379

84-
This section dives deeper into the server selection process for reads and
85-
writes. In the case of a write, the driver performs the following operations
86-
(in order) during the selection process:
80+
This section describes the server selection process for reads and
81+
writes. For writes, the driver performs the following operations, in order,
82+
during the selection process:
8783

84+
1. Select all writeable servers from the list of known hosts. For a replica set,
85+
the writeable server is the primary. For a sharded cluster, the
86+
writeable servers are all the known ``mongos`` servers.
8887

89-
#. Select all writeable servers from the list of known hosts. For a replica set
90-
this is the primary, while for a sharded cluster this is all the known mongoses.
91-
92-
#. Apply the user-defined server selector function. Note that the custom server
88+
#. Apply the user-defined server selector function. The custom server
9389
selector is **not** called if there are no servers left from the previous
9490
filtering stage.
9591

9692
#. Apply the ``localThresholdMS`` setting to the list of remaining hosts. This
97-
whittles the host list down to only contain servers whose latency is at most
93+
whittles the host list down to contain only servers whose latency is at most
9894
``localThresholdMS`` milliseconds higher than the lowest observed latency.
9995

100-
#. Select a server at random from the remaining host list. The desired
96+
#. Select a server at random from the remaining host list. The appropriate
10197
operation is then performed against the selected server.
10298

103-
104-
In the case of **reads** the process is identical except for the first step.
105-
Here, instead of selecting all writeable servers, we select all servers
106-
matching the user's ``~pymongo.read_preferences.ReadPreference`` from the
107-
list of known hosts. As an example, for a 3-member replica set with a
108-
``~pymongo.read_preferences.Secondary`` read preference, we would select
109-
all available secondaries.
110-
111-
112-
.. _server selection algorithm: https://mongodb.com/docs/manual/core/read-preference-mechanics/
99+
For reads, the process is identical, except for the first step.
100+
Instead of selecting all writeable servers, the driver selects all servers from the
101+
list of known hosts that match the user's
102+
``~pymongo.read_preferences.ReadPreference``. For example, for a 3-member
103+
replica set with a ``~pymongo.read_preferences.Secondary`` read preference,
104+
the driver selects all available secondaries.

0 commit comments

Comments
 (0)