Set readPreference=secondaryPreferred in conf but still 'read' is done from primary

530 views
Skip to first unread message

Ritesh Jha

unread,
Sep 10, 2017, 3:59:21 AM9/10/17
to ReactiveMongo - http://reactivemongo.org
I am having issue with reactive mongo when I tried to set readPreference in mongo db uri in application.conf. Query always executed at primary. 

mongodb.uri="mongodb://host1:27017,host2:27017,host3:27017/ecom?readPreference=secondaryPreferred"

log from my primary db:

query ecom.order query: { $readPreference: { mode: "primary" }, $query: { created_at: { $gte: new Date(1370016000000), $lte: new Date(1498838399999) }, items.store: 151 }, $hint: { created_at: 1, items.store: 1 } } planSummary: IXSCAN { created_at: 1.0, items.store: 1.0 } ntoreturn:2147483647 ntoskip:0 nscanned:32097 nscannedObjects:0 keyUpdates:0 writeConflicts:0 numYields:331 nreturned:0 reslen:20 locks:{ Global: { acquireCount: { r: 664 } }, MMAPV1Journal: { acquireCount: { r: 332 } }, Database: { acquireCount: { r: 332 } }, Collection: { acquireCount: { R: 332 } } } 105ms

But when i set readPreference in code of GenericQueryBuilder like below then it hit secondary. 

.cursor[Order](ReadPreference.secondaryPreferred) 

Am I missing any option in uri ? Please help. 

FYI
ReactiveMongo Version 0.12.4
MongoDB version 3.0.7
Operating System  MacOS 10.12.4

Cédric Chantepie

unread,
Sep 10, 2017, 8:21:39 AM9/10/17
to ReactiveMongo - http://reactivemongo.org
Hi,

Adding debug to the query builder, I can confirm that the read preference is taken in account (no regression).

scala> db.collection("foo").find(BSONDocument.empty).cursor[BSONDocument](ReadPreference.secondaryPreferred).head--> {
  $query
: {

 
},
  $readPreference
: {
    mode
: "secondaryPreferred"
 
}
}
res8
: scala.concurrent.Future[reactivemongo.bson.BSONDocument] = Future(<not completed>)

scala
> db.collection("foo").find(BSONDocument.empty).cursor[BSONDocument](ReadPreference.secondary).head
--> {
  $query
: {

 
},
  $readPreference
: {
    mode
: "secondary"
 
}
}
res9
: scala.concurrent.Future[reactivemongo.bson.BSONDocument] = Future(<not completed>)

scala
> db.collection("foo").find(BSONDocument.empty).cursor[BSONDocument](ReadPreference.nearest).head
--> {
  $query
: {

 
},
  $readPreference
: {
    mode
: "nearest"
 
}
}
res10
: scala.concurrent.Future[reactivemongo.bson.BSONDocument] = Future(<not completed>)

Ritesh Jha

unread,
Sep 10, 2017, 8:32:01 AM9/10/17
to ReactiveMongo - http://reactivemongo.org
Hi Cédric,

At query level it is working. I also mentioned that when I set readPreference in query builder it worked.
But when I set in configuration ("mongodb.uri") as per doc http://reactivemongo.org/releases/0.11/documentation/tutorial/connect-database.html query is reading from primary.

Cédric Chantepie

unread,
Sep 11, 2017, 1:12:14 PM9/11/17
to ReactiveMongo - http://reactivemongo.org
You can try 0.12.7-SNAPSHOT

Ritesh Jha

unread,
Sep 12, 2017, 3:28:29 AM9/12/17
to ReactiveMongo - http://reactivemongo.org
Hi Cédric,

I tried latest version.

"org.reactivemongo" %% "reactivemongo" % "0.12.7-SNAPSHOT",
"org.reactivemongo" %% "play2-reactivemongo" % "0.12.7-play25-SNAPSHOT"

from "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

but now I am getting below exception.

java.lang.AbstractMethodError: reactivemongo.play.json.collection.JSONQueryBuilder.readPreference()Lreactivemongo/api/ReadPreference;
at reactivemongo.api.collections.GenericQueryBuilder$class.one(GenericQueryBuilder.scala:111)
at reactivemongo.play.json.collection.JSONQueryBuilder.one(collection.scala:441)
at com.redmart.mporder.dao.mongo.MPOrderMongo$$anonfun$getOrderByOrderId$1.apply(MPOrderMongo.scala:89)
at com.redmart.mporder.dao.mongo.MPOrderMongo$$anonfun$getOrderByOrderId$1.apply(MPOrderMongo.scala:89)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1253)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1346)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

Please suggest what I should do to fix this. 

On Tuesday, 12 September 2017 01:12:14 UTC+8, Cédric Chantepie wrote:
You can try 0.12.7-SNAPSHOT

Cédric Chantepie

unread,
Sep 12, 2017, 8:19:32 AM9/12/17
to ReactiveMongo - http://reactivemongo.org
Make sure to clean your IVY cache.

Also note that as indicated in the documentation, "when the dependency to the Play plugin is used, no separate dependency to the ReactiveMongo driver must be declared".

Ritesh Jha

unread,
Sep 12, 2017, 12:13:23 PM9/12/17
to ReactiveMongo - http://reactivemongo.org
Thanks for suggestion. Now I am using only play plugin, clear my local cache and pointing to version "0.12.7-play25-SNAPSHOT".
But still query is hitting to primary.

query: { $readPreference: { mode: "primary" }, $query: { created_at: { $gte: new Date(1370016000000), $lte: new Date(1498838399999) }, items.merchant.sub_vendor_id: 151 } } planSummary: IXSCAN { created_at: 1.0 } ntoreturn:2147483647 ntoskip:0 nscanned:31373 nscannedObjects:31373 keyUpdates:0 writeConflicts:0 numYields:406 nreturned:0 reslen:20 locks:{ Global: { acquireCount: { r: 814 } }, MMAPV1Journal: { acquireCount: { r: 407 } }, Database: { acquireCount: { r: 407 } }, Collection: { acquireCount: { R: 407 } } } 309ms

Cédric Chantepie

unread,
Sep 12, 2017, 2:25:06 PM9/12/17
to ReactiveMongo - http://reactivemongo.org
The tests aren't reproducing such issue. Without an isolated reproducer, nothing more is actionable about that. Best regards.

Ritesh Jha

unread,
Sep 13, 2017, 3:22:25 AM9/13/17
to ReactiveMongo - http://reactivemongo.org



Hi Cédric,


Issue is with initialisation from uri mentioned in configuration. By the way in source code of GenericQueryBuilder still primaryPreferred is assigned as default value. 




Cédric Chantepie

unread,
Sep 13, 2017, 6:20:51 AM9/13/17
to ReactiveMongo - http://reactivemongo.org
Reply all
Reply to author
Forward
0 new messages