when I run the job, I see spike on mongo fault at my primary nodes monitoring. When I checked the log at primary nodes, it is confirmed that query run against primary instead of secondary.
Hi Rendy,
I’ve just tested readPreference=secondary behaviour and it worked as expected. The test environment :
The scala config settings:
mongoConfig.set("mongo.input.uri", "mongodb://<mongos>:<port>/<dbname>.<collection>?readPreference=secondary")
mongoConfig.set("mongo.input.query", "{'field': {'$gte': 100} }")
To view the query operations, the db.setProfilingLevel() was set to 2 for displaying all operations. See Profiling levels for more info.
By specifying the readPreference=secondary option in the mongodb URI, the below query in the primary doesn’t return any result:
db.system.profile.find({ns:"dbname.collection"}).pretty()
However in the secondary the query above shows results for the query operation. Note the $readPreference mode:
{
"op" : "query",
"ns" : "dbname.collection",
"query" : {
"$min" : {
"_id" : ObjectId("56cfb5ce7d9d2ff96a8dc820")
},
"$max" : {
},
"$orderby" : {
},
"$readPreference" : {
"mode" : "secondary"
},
"$query" : {
"field" : {
"$gte" : 100
}
}
},
...
When the readPreference=secondary option was removed from the mongodb URI. Executing the same query above will show the other way around. i.e. results show up in the primary, but not in the secondary. Below is the output from the primary, note the missing $readPreference mode.
{
"op" : "query",
"ns" : "dbname.collection",
"query" : {
"$min" : {
},
"$max" : {
"_id" : ObjectId("56cfb5ce7d9d2ff96a8dc820")
},
"$orderby" : {
},
"$query" : {
"field" : {
"$gte" : 100
}
}
},
...
The spike of activity in your primary nodes maybe related to the collStats call if you are using mongo-java-driver early version of 3.0.x. Where the getStats() helper does not respect the DBCollection read preference, the issue was resolved in v3.0.4. See JAVA-1921 and HADOOP-220 for more info.
If you are still having difficulty with read preference on secondary, could you provide the following:
You may also find hadoop connector commands useful.
Kind regards,
Wan.
Failed with exception java.io.IOException:java.io.IOException: com.mongodb.MongoNotPrimaryException: The server is not the primary and did not execute the operation
I encountered the same problem when I tried to use mongo-hadoop to create a hive table to query mongodb data.
Hi Chang,
Please open a new thread with the following information:
readPreference=secondary from the URI ? Regards,
Wan.