|
| 1 | +Server Selector Example |
| 2 | +======================= |
| 3 | + |
| 4 | +Users can exert fine-grained control over the `server selection algorithm`_ |
| 5 | +by setting the `server_selector` option on the :class:`~pymongo.MongoClient` |
| 6 | +to an appropriate callable. This example shows how to use this functionality |
| 7 | +to prefer servers running on ``localhost``. |
| 8 | + |
| 9 | + |
| 10 | +.. warning:: |
| 11 | + |
| 12 | + Use of custom server selector functions is a power user feature. Misusing |
| 13 | + custom server selectors can have unintended consequences such as degraded |
| 14 | + read/write performance. |
| 15 | + |
| 16 | + |
| 17 | +.. testsetup:: |
| 18 | + |
| 19 | + from pymongo import MongoClient |
| 20 | + |
| 21 | + |
| 22 | +.. _server selection algorithm: https://docs.mongodb.com/manual/core/read-preference-mechanics/ |
| 23 | + |
| 24 | + |
| 25 | +Example: Selecting Servers Running on ``localhost`` |
| 26 | +--------------------------------------------------- |
| 27 | + |
| 28 | +To start, we need to write the server selector function that will be used. |
| 29 | +The server selector function should accept a list of |
| 30 | +:class:`~pymongo.server_description.ServerDescription` objects and return a |
| 31 | +list of server descriptions that are suitable for the read or write operation. |
| 32 | +A server selector must not create or modify |
| 33 | +:class:`~pymongo.server_description.ServerDescription` objects, and must return |
| 34 | +the selected instances unchanged. |
| 35 | + |
| 36 | +In this example, we write a server selector that prioritizes servers running on |
| 37 | +``localhost``. This can be desirable when using a sharded cluster with multiple |
| 38 | +``mongos``, as locally run queries are likely to see lower latency and higher |
| 39 | +throughput. Please note, however, that it is highly dependent on the |
| 40 | +application if preferring ``localhost`` is beneficial or not. |
| 41 | + |
| 42 | +In addition to comparing the hostname with ``localhost``, our server selector |
| 43 | +function accounts for the edge case when no servers are running on |
| 44 | +``localhost``. In this case, we allow the default server selection logic to |
| 45 | +prevail by passing through the received server description list unchanged. |
| 46 | +Failure to do this would render the client unable to communicate with MongoDB |
| 47 | +in the event that no servers were running on ``localhost``. |
| 48 | + |
| 49 | + |
| 50 | +The described server selection logic is implemented in the following server |
| 51 | +selector function: |
| 52 | + |
| 53 | + |
| 54 | +.. doctest:: |
| 55 | + |
| 56 | + >>> def server_selector(server_descriptions): |
| 57 | + ... servers = [ |
| 58 | + ... server for server in server_descriptions |
| 59 | + ... if server.address[0] == 'localhost' |
| 60 | + ... ] |
| 61 | + ... if not servers: |
| 62 | + ... return server_descriptions |
| 63 | + ... return servers |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +Finally, we can create a :class:`~pymongo.MongoClient` instance with this |
| 68 | +server selector. |
| 69 | + |
| 70 | + |
| 71 | +.. doctest:: |
| 72 | + |
| 73 | + >>> client = MongoClient(server_selector=server_selector) |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +Server Selection Process |
| 78 | +------------------------ |
| 79 | + |
| 80 | +This section dives deeper into the server selection process for reads and |
| 81 | +writes. In the case of a write, the driver performs the following operations |
| 82 | +(in order) during the selection process: |
| 83 | + |
| 84 | + |
| 85 | +#. Select all writeable servers from the list of known hosts. For a replica set |
| 86 | + this is the primary, while for a sharded cluster this is all the known mongoses. |
| 87 | + |
| 88 | +#. Apply the user-defined server selector function. Note that the custom server |
| 89 | + selector is **not** called if there are no servers left from the previous |
| 90 | + filtering stage. |
| 91 | + |
| 92 | +#. Apply the ``localThresholdMS`` setting to the list of remaining hosts. This |
| 93 | + whittles the host list down to only contain servers whose latency is at most |
| 94 | + ``localThresholdMS`` milliseconds higher than the lowest observed latency. |
| 95 | + |
| 96 | +#. Select a server at random from the remaining host list. The desired |
| 97 | + operation is then performed against the selected server. |
| 98 | + |
| 99 | + |
| 100 | +In the case of **reads** the process is identical except for the first step. |
| 101 | +Here, instead of selecting all writeable servers, we select all servers |
| 102 | +matching the user's :class:`~pymongo.read_preferences.ReadPreference` from the |
| 103 | +list of known hosts. As an example, for a 3-member replica set with a |
| 104 | +:class:`~pymongo.read_preferences.Secondary` read preference, we would select |
| 105 | +all available secondaries. |
| 106 | + |
| 107 | + |
| 108 | +.. _server selection algorithm: https://docs.mongodb.com/manual/core/read-preference-mechanics/ |
0 commit comments