[SERVER-48769] Shell does not respect read preference in connection string Created: 11/Jun/20  Updated: 27/Oct/23  Resolved: 31/Jan/23

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 4.4.0-rc9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Siyuan Zhou Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Gone away Votes: 1
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-39222 mongo shell should parse readPreferen... Closed
Assigned Teams:
Server Tooling & Methods
Operating System: ALL
Steps To Reproduce:

In on terminal, start a replset with 3 nodes.

$ mongo-4.4.0 --nodb
MongoDB shell version v4.4.0-rc9
> var r = ReplSetTest({name: "TestRepl", nodes: 3}); r.startSet(); r.initiate();

In another terminal, start a mongo shell with read preference secondary in the URI. The prompt shows it connects to the primary.

$ mongo-4.4.0 --host "mongodb://localhost:20000,localhost:20001,localhost:20002/?replicaSet=TestRepl&readPreference=secondary"
MongoDB shell version v4.4.0-rc9
connecting to: mongodb://localhost:20000,localhost:20001,localhost:20002/?compressors=disabled&gssapiServiceName=mongodb&readPreference=secondary&replicaSet=TestRepl
Implicit session: session { "id" : UUID("cd5cff0a-7690-4b46-b094-fa6e0be01e1b") }
MongoDB server version: 0.0.0
WARNING: shell and server versions do not match
---
The server generated these startup warnings when booting:
        2020-06-11T19:32:29.034-04:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2020-06-11T19:32:29.958-04:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-06-11T19:32:29.959-04:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
        2020-06-11T19:32:29.960-04:00: Soft rlimits too low
        2020-06-11T19:32:29.960-04:00:         currentValue: 1024
        2020-06-11T19:32:29.960-04:00:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).
 
        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.
 
        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
TestRepl:PRIMARY>

Participants:

 Description   

As described in the steps to reproduce, the shell failed to connect to a secondary even though the connection string specifies read preference "secondary" according to the documentation.

URI option support isn't consistent and user-friendly in the shell. Some URI options are supported. For example a wrong "replicaSet" option will be detected as it fails to connect. However read preference is ignored silently. Initially I thought my options were wrong or read preference has to be given together with "replicaSet" or the database was misconfigured when I tried to connect the data warehouse not owned by my team. Misspelling an option is also a silent error. For example, "replSet" option will be ignored silently.

The shell seems the easiest way to try out read preferences settings, so I feel the lack of support is a bug. As least, the unsupported or unknown URI options should be an explicit error.



 Comments   
Comment by Chris Harris [ 31/Jan/23 ]

This ticket was opened against the legacy mongo shell which has since been deprecated and removed. The newer MongoDB Shell (mongosh) accepts and respects all configurations in a connection string.

Comment by Steven Vannelli [ 10/May/22 ]

Moving this ticket to the Backlog and removing the "Backlog" fixVersion as per our latest policy for using fixVersions.

Comment by Dmitry Ryabtsev [ 05/Jan/21 ]

This appears to be a duplicate of SERVER-39222

Comment by Max Hirschhorn [ 12/Jun/20 ]

Setting the read preference on a Mongo connection object must currently be done through Mongo.prototype.setReadPref(). It doesn't automatically get filled in based on the connection string.

TestRepl:PRIMARY> db.getMongo().setReadPref('secondary')
TestRepl:PRIMARY> db.runReadCommand({explain: {find: 'mycoll'}})
...
	"serverInfo" : {
		"port" : 20002,

The prompt shows it connects to the primary.

I think it is worth clarifying that setting a "secondary" read preference at a connection string level isn't meant to cause all commands to be routed to a secondary. The mongo shell only attaches the read preference when DB.prototype.runReadCommand() is used. The read preference will also be ignored if the command isn't part of this list.

Misspelling an option is also a silent error. For example, "replSet" option will be ignored silently.

The shell seems the easiest way to try out read preferences settings, so I feel the lack of support is a bug. As least, the unsupported or unknown URI options should be an explicit error.

According to the driver's specification, the mongo shell ought to be logging a warning about unknown options.

Generated at Thu Feb 08 05:18:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.