diff --git a/source/run-command/run-command.rst b/source/run-command/run-command.rst index b054f8f241..3ba6638026 100644 --- a/source/run-command/run-command.rst +++ b/source/run-command/run-command.rst @@ -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 `_ -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 `_ @@ -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. diff --git a/source/run-command/tests/unified/runCommand.json b/source/run-command/tests/unified/runCommand.json index 0ae0e9d66e..9c7f912f95 100644 --- a/source/run-command/tests/unified/runCommand.json +++ b/source/run-command/tests/unified/runCommand.json @@ -163,6 +163,16 @@ }, { "description": "attaches the provided $readPreference to given command", + "runOnRequirements": [ + { + "topologies": [ + "replicaset", + "sharded-replicaset", + "load-balanced", + "sharded" + ] + } + ], "operations": [ { "name": "runCommand", @@ -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": [ diff --git a/source/run-command/tests/unified/runCommand.yml b/source/run-command/tests/unified/runCommand.yml index 3c5f231361..edff7f6410 100644 --- a/source/run-command/tests/unified/runCommand.yml +++ b/source/run-command/tests/unified/runCommand.yml @@ -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 @@ -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