1
1
.. uses server-selection.rst
2
2
3
- Server Selector Example
4
- =======================
3
+ .. _pymongo-server-selection:
5
4
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
+ ================
10
8
9
+ .. contents:: On this page
10
+ :local:
11
+ :backlinks: none
12
+ :depth: 1
13
+ :class: singlecol
11
14
12
- .. warning:
15
+ .. facet::
16
+ :name: genre
17
+ :values: reference
13
18
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
22
21
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``.
24
26
27
+ .. warning::
25
28
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.
28
32
29
33
Example: Selecting Servers Running on ``localhost``
30
34
---------------------------------------------------
31
35
32
- To start, we need to write the server selector function that will be used .
36
+ First, write the server selector function.
33
37
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
35
39
list of server descriptions that are suitable for the read or write operation.
36
40
A server selector must not create or modify
37
41
``~pymongo.server_description.ServerDescription`` objects, and must return
38
42
the selected instances unchanged.
39
43
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
41
45
``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.
45
49
46
- In addition to comparing the hostname with ``localhost``, our server selector
50
+ In addition to comparing the hostname with ``localhost``, the server selector
47
51
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``.
56
56
57
+ The following example implements the server selection logic:
57
58
58
59
.. code-block:: python
59
60
@@ -66,47 +67,38 @@ selector function:
66
67
... return servers
67
68
...
68
69
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:
74
72
75
73
.. code-block:: python
76
74
77
75
>>> client = MongoClient(server_selector=server_selector)
78
76
79
-
80
-
81
77
Server Selection Process
82
78
------------------------
83
79
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:
87
83
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.
88
87
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
93
89
selector is **not** called if there are no servers left from the previous
94
90
filtering stage.
95
91
96
92
#. 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
98
94
``localThresholdMS`` milliseconds higher than the lowest observed latency.
99
95
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
101
97
operation is then performed against the selected server.
102
98
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