Skip to content

Commit eead554

Browse files
authored
DRIVERS-1918 Update rules for $readPreference passing to include OP_MSG (#1076)
1 parent ccba1fc commit eead554

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

source/server-selection/server-selection.rst

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,20 @@ Passing read preference to mongos and load balancers
579579
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
580580

581581
If a server of type Mongos or LoadBalancer is selected for a read operation, the read
582-
preference is passed to the selected mongos through the use of the
583-
``SecondaryOk`` wire protocol flag, the ``$readPreference`` query
584-
modifier or both, according to the following rules.
582+
preference is passed to the selected mongos through the use of ``$readPreference``
583+
(as a `Global Command Argument`_ for OP_MSG or a query modifier for OP_QUERY) and, for
584+
OP_QUERY only, the ``SecondaryOk`` wire protocol flag, according to the following rules.
585+
586+
For OP_MSG:
587+
```````````
588+
589+
- For mode 'primary', drivers MUST NOT set ``$readPreference``
590+
591+
- For all other read preference modes (i.e. 'secondary', 'primaryPreferred',
592+
...), drivers MUST set ``$readPreference``
593+
594+
For OP_QUERY:
595+
`````````````
585596

586597
If the read preference contains **only** a ``mode`` parameter and the mode is
587598
'primary' or 'secondaryPreferred', for maximum backwards compatibility with
@@ -591,23 +602,23 @@ and MUST NOT use a ``$readPreference`` query modifier.
591602

592603
Therefore, when sending queries to a mongos or load balancer, the following rules apply:
593604

594-
- For mode 'primary', drivers MUST NOT set the ``SecondaryOk`` wire protocol flag
595-
and MUST NOT use ``$readPreference``
605+
- For mode 'primary', drivers MUST NOT set the ``SecondaryOk`` wire protocol flag
606+
and MUST NOT use ``$readPreference``
596607

597-
- For mode 'secondary', drivers MUST set the ``SecondaryOk`` wire protocol flag
598-
and MUST also use ``$readPreference``
608+
- For mode 'secondary', drivers MUST set the ``SecondaryOk`` wire protocol flag
609+
and MUST also use ``$readPreference``
599610

600-
- For mode 'primaryPreferred', drivers MUST set the ``SecondaryOk`` wire protocol flag
601-
and MUST also use ``$readPreference``
611+
- For mode 'primaryPreferred', drivers MUST set the ``SecondaryOk`` wire protocol flag
612+
and MUST also use ``$readPreference``
602613

603-
- For mode 'secondaryPreferred', drivers MUST set the ``SecondaryOk`` wire protocol flag.
604-
If the read preference contains a non-empty ``tag_sets`` parameter,
605-
``maxStalenessSeconds`` is a positive integer, or the ``hedge`` parameter is
606-
non-empty, drivers MUST use ``$readPreference``; otherwise, drivers MUST NOT
607-
use ``$readPreference``
614+
- For mode 'secondaryPreferred', drivers MUST set the ``SecondaryOk`` wire protocol flag.
615+
If the read preference contains a non-empty ``tag_sets`` parameter,
616+
``maxStalenessSeconds`` is a positive integer, or the ``hedge`` parameter is
617+
non-empty, drivers MUST use ``$readPreference``; otherwise, drivers MUST NOT
618+
use ``$readPreference``
608619

609-
- For mode 'nearest', drivers MUST set the ``SecondaryOk`` wire protocol flag
610-
and MUST also use ``$readPreference``
620+
- For mode 'nearest', drivers MUST set the ``SecondaryOk`` wire protocol flag
621+
and MUST also use ``$readPreference``
611622

612623
The ``$readPreference`` query modifier sends the read preference as part of the
613624
query. The read preference fields ``tag_sets`` is represented in a ``$readPreference``
@@ -629,6 +640,9 @@ the query MUST be provided using the ``$query`` modifier like so::
629640
}
630641
}
631642

643+
Document structure
644+
``````````````````
645+
632646
A valid ``$readPreference`` document for mongos or load balancer has the following requirements:
633647

634648
1. The ``mode`` field MUST be present exactly once with the mode represented
@@ -955,14 +969,19 @@ to the server differently:
955969
- For all other types, using OP_MSG: If no read preference is configured by the
956970
application, or if the application read preference is Primary, then
957971
$readPreference MUST be set to ``{ "mode": "primaryPreferred" }`` to ensure
958-
that any server type can handle the request.
972+
that any server type can handle the request. If the application read
973+
preference is set otherwise, $readPreference MUST be set following
974+
`Document structure`_.
959975

960976
The single server is always suitable for write operations if it is available.
961977

962978
Topology type: LoadBalanced
963979
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
964980

965-
See the `Load Balancer Specification <../load-balancers/load-balancers.rst#server-selection>`__ for details.
981+
During command construction, drivers MUST add a $readPreference field to the
982+
command when required by `Passing read preference to mongos and load balancers`_;
983+
see the `Load Balancer Specification <../load-balancers/load-balancers.rst#server-selection>`__
984+
for details.
966985

967986

968987
Topology types: ReplicaSetWithPrimary or ReplicaSetNoPrimary
@@ -1789,10 +1808,13 @@ References
17891808
.. _Driver Authentication: https://github.com/mongodb/specifications/blob/master/source/auth
17901809
.. _maxConnecting: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#connection-pool
17911810
.. _Connection Monitoring and Pooling: /source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst
1811+
.. _Global Command Argument: /source/message/OP_MSG.rst#global-command-arguments
17921812

17931813
Changes
17941814
=======
17951815

1816+
2021-08-05: Updated $readPreference logic to describe OP_MSG behavior.
1817+
17961818
2015-06-26: Updated single-threaded selection logic with "stale" and serverSelectionTryOnce.
17971819

17981820
2015-08-10: Updated single-threaded selection logic to ensure a scan always

0 commit comments

Comments
 (0)