Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-5817

Read preference not applied to commands properly

    • 3
    • 0
    • Not Needed
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      What problem are you facing?

      When connecting to a secondary in a replica set with a direct connection, the topology is of type Single.  If a read operation is performed, the driver should attach a read preference of "primaryPreferred" to the command.

      The driver does not do this, which results in errors when you attempt a read operation (see the reproduction).

       

      There are specific rules for attaching read preference to command documents, outline in the server selection spec: https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#topology-type-single

       

      This bug is specific to "Single" topologies, but we should audit our logic for all topology types.

      What driver and relevant dependency versions are you using?

      I tested driver latest and driver 5.5, with the same results.

      Steps to reproduce?

       

      import { MongoClient } from './src';
      
      async function main() {
        const client = new MongoClient(
          'mongodb://127.0.0.1:27017/?directConnection=true&readPreference=primary'
        );
        await client.connect();
      
        await client.db('foo').collection('bar').find({}).toArray();
        await client.close();
      }
      main();
      
      
      •  For this test specifically, create a replica set and connect to a secondary with a direct connection
      • specify a primary read preference
      • run the script
      • receive a `MongoServerError: not primary and secondaryOk=false` error

       

      AC:

      Ensure the following:

      Topology type: Single

      https://github.com/mongodb/specifications/blob/5de9d45528a07d51579253f57f7ba23dbbaa68d1/source/server-selection/server-selection.md#topology-type-single
      A deployment of topology type Single contains only a single server of any type. Topology type Single signifies a direct connection intended to receive all read and write operations.

      Therefore, read preference is ignored during server selection with topology type Single. The single server is always suitable for reads if it is available. Depending on server type, the read preference is communicated to the server differently:

      • Type Mongos: the read preference is sent to the server using the rules for Passing read preference to mongos and load balancers.
      • Type Standalone: clients MUST NOT send the read preference to the server
      • For all other types, using OP_QUERY: clients MUST always set the SecondaryOk wire protocol flag on reads to ensure that any server type can handle the request.
      • For all other types, using OP_MSG: If no read preference is configured by the application, or if the application read preference is Primary, then $readPreference MUST be set to {{ { "mode": "primaryPreferred" }

        }} to ensure that any server type can handle the request. If the application read preference is set otherwise, $readPreference MUST be set following Document structure.

      Testing AC:

      • Unskip the related runCommand spec test.
      • Integration test that direct connects to a secondary and asserts the correct read preference.

        There are no Sub-Tasks for this issue.

            Assignee:
            alena.khineika@mongodb.com Alena Khineika
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: