Skip to content

Commit

Permalink
DRIVERS-2577: $readPreference is not sent to standalone server (#1416)
Browse files Browse the repository at this point in the history
Also test that primary $readPreference is never set
  • Loading branch information
jmikola committed May 9, 2023
1 parent 9e122d9 commit d697477
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/run-command/run-command.rst
Expand Up @@ -113,7 +113,7 @@ To facilitate server selection the RunCommand operation MUST accept an optional

* See Server Selection's section on `Use of read preferences with commands <https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#use-of-read-preferences-with-commands>`_

If the provided ReadPreference is NOT ``{mode: primary}``, the command sent MUST include the ``$readPreference`` global command argument.
If the provided ReadPreference is NOT ``{mode: primary}`` and the selected server is NOT a standalone, the command sent MUST include the ``$readPreference`` global command argument.

* See OP_MSG's section on `Global Command Arguments <https://github.com/mongodb/specifications/blob/master/source/message/OP_MSG.rst#global-command-arguments>`_

Expand Down Expand Up @@ -190,4 +190,5 @@ Drivers MUST document the behavior of RunCommand if a ``maxTimeMS`` field is al
Changelog
=========

:2023:05-08: ``$readPreference`` is not sent to standalone servers
:2023-04-20: Add run command specification.
97 changes: 97 additions & 0 deletions source/run-command/tests/unified/runCommand.json
Expand Up @@ -163,6 +163,16 @@
},
{
"description": "attaches the provided $readPreference to given command",
"runOnRequirements": [
{
"topologies": [
"replicaset",
"sharded-replicaset",
"load-balanced",
"sharded"
]
}
],
"operations": [
{
"name": "runCommand",
Expand Down Expand Up @@ -201,6 +211,93 @@
}
]
},
{
"description": "does not attach $readPreference to given command on standalone",
"runOnRequirements": [
{
"topologies": [
"single"
]
}
],
"operations": [
{
"name": "runCommand",
"object": "db",
"arguments": {
"commandName": "ping",
"command": {
"ping": 1
},
"readPreference": {
"mode": "nearest"
}
},
"expectResult": {
"ok": 1
}
}
],
"expectEvents": [
{
"client": "client",
"events": [
{
"commandStartedEvent": {
"command": {
"ping": 1,
"$readPreference": {
"$$exists": false
},
"$db": "db"
},
"commandName": "ping"
}
}
]
}
]
},
{
"description": "does not attach primary $readPreference to given command",
"operations": [
{
"name": "runCommand",
"object": "db",
"arguments": {
"commandName": "ping",
"command": {
"ping": 1
},
"readPreference": {
"mode": "primary"
}
},
"expectResult": {
"ok": 1
}
}
],
"expectEvents": [
{
"client": "client",
"events": [
{
"commandStartedEvent": {
"command": {
"ping": 1,
"$readPreference": {
"$$exists": false
},
"$db": "db"
},
"commandName": "ping"
}
}
]
}
]
},
{
"description": "does not inherit readConcern specified at the db level",
"operations": [
Expand Down
47 changes: 47 additions & 0 deletions source/run-command/tests/unified/runCommand.yml
Expand Up @@ -87,6 +87,9 @@ tests:
commandName: ping

- description: attaches the provided $readPreference to given command
runOnRequirements:
# Exclude single topology, which is most likely a standalone server
- topologies: [ replicaset, sharded-replicaset, load-balanced, sharded ]
operations:
- name: runCommand
object: *db
Expand All @@ -105,6 +108,50 @@ tests:
$db: *db
commandName: ping

- description: does not attach $readPreference to given command on standalone
runOnRequirements:
# This test assumes that the single topology contains a standalone server;
# however, it is possible for a single topology to contain a direct
# connection to another server type.
# See: https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#topology-type-single
- topologies: [ single ]
operations:
- name: runCommand
object: *db
arguments:
commandName: ping
command: { ping: 1 }
readPreference: { mode: 'nearest' }
expectResult: { ok: 1 }
expectEvents:
- client: *client
events:
- commandStartedEvent:
command:
ping: 1
$readPreference: { $$exists: false }
$db: *db
commandName: ping

- description: does not attach primary $readPreference to given command
operations:
- name: runCommand
object: *db
arguments:
commandName: ping
command: { ping: 1 }
readPreference: { mode: 'primary' }
expectResult: { ok: 1 }
expectEvents:
- client: *client
events:
- commandStartedEvent:
command:
ping: 1
$readPreference: { $$exists: false }
$db: *db
commandName: ping

- description: does not inherit readConcern specified at the db level
operations:
- name: runCommand
Expand Down

0 comments on commit d697477

Please sign in to comment.