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

Connecting to single mongos makes driver think it is connected to a standalone

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.3, 4.0.0
    • Affects Version/s: 3.6.2
    • Component/s: None

      Not sure if this is a bug or intended behavior, but I wasn't able to find a workaround.

      I created a sharded cluster with 1 mongos using this Node.js script: https://github.com/Automattic/mongoose/blob/ebdbe41171000a3900ac075114d952b76441d768/tools/sharded.js , and I enabled sharding:

      $ mongo --port 51000
      MongoDB shell version v4.2.3-rc1
      connecting to: mongodb://127.0.0.1:51000/?compressors=disabled&gssapiServiceName=mongodb
      Implicit session: session { "id" : UUID("399d5b08-5423-45a7-aac8-17dfb97586a5") }
      MongoDB server version: 4.2.3-rc1
      Server has startup warnings: 
      2020-09-18T14:02:23.050-0400 I  CONTROL  [main] 
      2020-09-18T14:02:23.050-0400 I  CONTROL  [main] ** WARNING: Access control is not enabled for the database.
      2020-09-18T14:02:23.050-0400 I  CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
      2020-09-18T14:02:23.050-0400 I  CONTROL  [main] 
      mongos> use test
      switched to db test
      mongos> db.createCollection('activity')
      {
      	"ok" : 1,
      	"operationTime" : Timestamp(1600452398, 2),
      	"$clusterTime" : {
      		"clusterTime" : Timestamp(1600452398, 2),
      		"signature" : {
      			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
      			"keyId" : NumberLong(0)
      		}
      	}
      }
      mongos>
      mongos> sh.shardCollection('test.activity', {domain: 1, sender: 1 ,objecttype:1 })
      {
      	"collectionsharded" : "test.activity",
      	"collectionUUID" : UUID("1a615b36-6461-445a-a791-2aa65bf7c1d4"),
      	"ok" : 1,
      	"operationTime" : Timestamp(1600452428, 8),
      	"$clusterTime" : {
      		"clusterTime" : Timestamp(1600452428, 8),
      		"signature" : {
      			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
      			"keyId" : NumberLong(0)
      		}
      	}
      }
      mongos>
      

      With this setup, the below script consistently throws a "MongoError: Must run update to shard key field in a multi-statement transaction or with retryWrites: true" error:

      'use strict';
        
      const { MongoClient } = require('mongodb');
      
      run().catch(err => console.log(err));
      
      async function run() { 
        const client = await MongoClient.connect('mongodb://localhost:51000/test?retryWrites=true', {
          useNewUrlParser: true,
          useUnifiedTopology: true
        });
        const db = client.db();
      
        await db.collection('activity').deleteMany({});
      
        await db.collection('activity').insertOne({ domain: 'a', sender: 'b', objecttype: 'chair', hash: 'random1' });
      
        console.log(client.topology.description);
      
        await db.collection('activity').updateOne(
        { domain: 'a', sender: 'b', objecttype: 'chair', hash: 'random1'},  
        { '$set': { domain: 'example.com', sender: 'joe@example.com', objecttype: 'chair', foo: 'bar'}
        } ); 
      
        console.log('Done');
      }
      

      The console.log(client.topology.description) line prints:

      TopologyDescription {
        type: 'Single',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map {
          'localhost:51000' => ServerDescription {
            address: 'localhost:51000',
            error: undefined,
            roundTripTime: 9,
            lastUpdateTime: 261861846,
            lastWriteDate: null,
            opTime: null,
            type: 'Mongos',
            topologyVersion: undefined,
            minWireVersion: 0,
            maxWireVersion: 8,
            maxBsonObjectSize: 16777216,
            maxMessageSizeBytes: 48000000,
            maxWriteBatchSize: 100000,
            hosts: [],
            passives: [],
            arbiters: [],
            tags: [],
            logicalSessionTimeoutMinutes: 30,
            '$clusterTime': [Object]
          }
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: 30,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: 8
      }
      

      As far as I can tell, this issue is due to the driver thinking that the mongos is a standalone. If you turn off useUnifiedTopology, this script succeeds.

            Assignee:
            thomas.reggi@mongodb.com Thomas Reggi (Inactive)
            Reporter:
            val@karpov.io Valeri Karpov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: