Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-43310

mongos tries to connect to the secondary to create the index if `rs.slaveOk()` is set.

    • Fully Compatible
    • ALL
    • v4.2
    • Hide
      1. Start a sharded cluster on 4.2:
        mongos> db.version()
        4.2.0
        
      2. Connect to the mongos and run rs.slaveOk().
        mongos> rs.slaveOk()
        
      3. After that, the insert succeeds - meaning that the mongos is connecting to the primary to insert the document.
        mongos> db.docs.insert({x:1})
        WriteResult({ "nInserted" : 1 })
        
      4. But ensureIndex fails - the mongos is trying to connect to a secondary to run ensureIndex.
        mongos> db.docs.ensureIndex({x:1})
        {
        	"raw" : {
        		"shard01/localhost:20002,localhost:20003,localhost:20004" : {
        			"ok" : 0,
        			"errmsg" : "not master",
        			"code" : 10107,
        			"codeName" : "NotMaster"
        		}
        	},
        	"code" : 10107,
        	"codeName" : "NotMaster",
        	"ok" : 0,
        	"errmsg" : "not master",
        	"operationTime" : Timestamp(1568357018, 1),
        	"$clusterTime" : {
        		"clusterTime" : Timestamp(1568357018, 1),
        		"signature" : {
        			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
        			"keyId" : NumberLong(0)
        		}
        	}
        }
        
      Show
      Start a sharded cluster on 4.2: mongos> db.version() 4.2.0 Connect to the mongos and run rs.slaveOk() . mongos> rs.slaveOk() After that, the insert succeeds - meaning that the mongos is connecting to the primary to insert the document. mongos> db.docs.insert({x:1}) WriteResult({ "nInserted" : 1 }) But ensureIndex fails - the mongos is trying to connect to a secondary to run ensureIndex . mongos> db.docs.ensureIndex({x:1}) { "raw" : { "shard01/localhost:20002,localhost:20003,localhost:20004" : { "ok" : 0, "errmsg" : "not master" , "code" : 10107, "codeName" : "NotMaster" } }, "code" : 10107, "codeName" : "NotMaster" , "ok" : 0, "errmsg" : "not master" , "operationTime" : Timestamp(1568357018, 1), "$clusterTime" : { "clusterTime" : Timestamp(1568357018, 1), "signature" : { "hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA=" ), "keyId" : NumberLong(0) } } }
    • Sharding 2019-12-02, Sharding 2019-12-16, Sharding 2019-12-30, Sharding 2020-01-13

      On the mongos, we can run rs.slaveOk() to set the read preference to secondaryPreferred.

      After running rs.slaveOk() on the mongos, the insert operation will still be routed to the primary so the insert will succeed.

      However, ensureIndex will be routed to the secondary, so this command would fail.

            Assignee:
            blake.oler@mongodb.com Blake Oler
            Reporter:
            linda.qin@mongodb.com Linda Qin
            Votes:
            0 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:
              Resolved: