Lab - Read Concern and Read Preferences - Count() fails

I am trying to complete the lab for M103: Chapter 2. I successfully shutdown 2 of the 3 nodes, and then connected directly to the one that was still running. The first thing I noticed was that when I tried to read, I got the error message “not master and slaveOk=false”. So with the basic understanding I have reached so far :innocent:, I used rs.slaveOk(). Afterwards, I found I was able to successfully read regardless of the readPref setting, so my first attempt at answering the question was to select all of the options. This was not the right answer. :frowning_face:

So, I searched the forum for some answers and found this thread Lab - Read Concern and Read Preferences. In there, I found that @007_jb says not to use slaveOk(). So, I turned it off, and tried again. This time nothing would work. I was using these statements:

db.products.find().readPref("primary").count();
db.products.find().readPref("primaryPreferred").count();
etc.

The error I received was:

MongoDB Enterprise m103-repl:SECONDARY> db.products.find().readPref("primaryPreferred").count()
2020-02-25T03:26:51.209+0000 E QUERY    [thread1] Error: count failed: {
        "operationTime" : Timestamp(1582597419, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1582597419, 1),
                "signature" : {
                        "hash" : BinData(0,"IRt9/pViwQK1lMKUHHFRrSYYT5A="),
                        "keyId" : NumberLong("6796809948060712962")
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBQuery.prototype.count@src/mongo/shell/query.js:391:11
@(shell):1:1

Reading further in the thread, @007_jb assures us that this statement will work.

db.coll.find().readPref("secondaryPreferred").pretty()

I changed mine to use pretty() instead of count() and suddenly it worked just like he said. It seems that count() is not compatible with readPref(), but pretty() is???

SUPPORTING LOG

vagrant@m103:~$ mongo --host "m103:27003" -u "m103-admin" -p "m103-pass" --authenticationDatabase "admin"
MongoDB shell version v3.6.17
connecting to: mongodb://m103:27003/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("28aa0638-7ab5-484b-97c7-3359768747a4") }
MongoDB server version: 3.6.17
Server has startup warnings:
2020-02-25T02:20:24.104+0000 I STORAGE  [initandlisten]
2020-02-25T02:20:24.104+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-02-25T02:20:24.104+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten]
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten]
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-02-25T02:20:24.827+0000 I CONTROL  [initandlisten]

MongoDB Enterprise m103-repl:SECONDARY> rs.status()
{
        "set" : "m103-repl",
        "date" : ISODate("2020-02-25T02:54:59.911Z"),
        "myState" : 2,
        "term" : NumberLong(5),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1582597419, 1),
                        "t" : NumberLong(4)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1582597419, 1),
                        "t" : NumberLong(4)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1582597419, 1),
                        "t" : NumberLong(4)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1582597419, 1),
                        "t" : NumberLong(4)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.103.100:27001",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-02-25T02:54:59.521Z"),
                        "lastHeartbeatRecv" : ISODate("2020-02-25T02:23:36.490Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Connection refused",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : -1
                },
                {
                        "_id" : 1,
                        "name" : "m103:27002",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-02-25T02:54:59.521Z"),
                        "lastHeartbeatRecv" : ISODate("2020-02-25T02:23:45.231Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Connection refused",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : -1
                },
                {
                        "_id" : 2,
                        "name" : "m103:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 2075,
                        "optime" : {
                                "ts" : Timestamp(1582597419, 1),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2020-02-25T02:23:39Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "configVersion" : 3,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1582597419, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1582597419, 1),
                "signature" : {
                        "hash" : BinData(0,"IRt9/pViwQK1lMKUHHFRrSYYT5A="),
                        "keyId" : NumberLong("6796809948060712962")
                }
        }
}

MongoDB Enterprise m103-repl:SECONDARY> use applicationData
switched to db applicationData
MongoDB Enterprise m103-repl:SECONDARY> db.products.find({sku: 20000372}).readPref("secondaryPreferred").count()
2020-02-25T02:59:14.056+0000 E QUERY    [thread1] Error: count failed: {
        "operationTime" : Timestamp(1582597419, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1582597419, 1),
                "signature" : {
                        "hash" : BinData(0,"IRt9/pViwQK1lMKUHHFRrSYYT5A="),
                        "keyId" : NumberLong("6796809948060712962")
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBQuery.prototype.count@src/mongo/shell/query.js:391:11
@(shell):1:1

MongoDB Enterprise m103-repl:SECONDARY> db.products.find({sku: 20000372}).readPref("secondaryPreferred").pretty()
{
        "_id" : ObjectId("573f7197f29313caab89b240"),
        "sku" : 20000372,
        "name" : "Emerald (EP) - CD",
        "type" : "Music",
        "regularPrice" : 14.99,
        "salePrice" : 14.99,
        "shippingWeight" : "0.25"
}
1 Like

It’s perfectly acceptable to set slaveOk when reading from a Secondary, however, as this lab is partly about Read Preferences, it’s necessary to use readPref() in order to test out all the read preference options, hence the recommendation to not use slaveOk.

Good observation! The db.collection.find().readPref() method works at the cursor level, whereas, count() performs a document count that is not a cursor operation. And pretty() plays no part in this.

There are a couple of ways to count the documents returned from db.collection.find(), that uses an appropriate secondary read preference but without slaveOk:

  1. db.collection.find().readPref("secondaryPreferred").itcount()

  2. db.getMongo().setReadPref("secondaryPreferred") // Connection level
    db.collection.find().count()
    db.collection.find().itcount()
    db.collection.aggregate({$count: "count_docs"})

  3. var count = db.collection.find().readPref("secondaryPreferred")
    count.length()

3 Likes