ReactiveMongo 0.11.9 throws 连接到 mongodb 3.0.4 时主节点不可用

ReactiveMongo 0.11.9 throws No primary node is available while connecting to mongodb 3.0.4

我只是有一个反应式mongo(版本:0.11.9)插入查询如下,

class MongoInsertQuery extends Query {
  val DbName = "events-db"
  val CollectionName = "EventStream"

  val driver = new MongoDriver
  val connection = driver.connection(List("127.0.0.1:27018"))

    def insert() = {
        val db = connection(DbName)
        val collection: BSONCollection = db(CollectionName)
        val futureResult: Future[WriteResult] = collection.insert(BSONDocument("type" -> "Product_Received"))
        futureResult onComplete {
          case Success(s) => println("Success " + s)
          case Failure(e) => {
            println("error " + e.getMessage)
            e.printStackTrace()
          }
        }
      }
}

没那么重要,但 Scala Play REST 控制器 会调用该方法

  def sayBeard = Action { request =>
    new MongoInsertQuery().insert()
    Ok(Json.obj("message" -> "TODO respond writeResult"))
  }

当我调用端点时,

$ curl -XGET localhost:9000/sayBeard
{"message":"TODO respond writeResult"}

成功插入到mongodb 2.4.14,

precise64(mongod-2.4.14) events-db> db.EventStream.find({})
{
  "_id": ObjectId("5683337120906c127504eb79"),
  "type": "Product_Received"
}

但是 future onComplete 上的 Failure 出现 readAndDeserialize 错误。

[info] - play.api.Play - Application started (Dev)
error 83
java.lang.ArrayIndexOutOfBoundsException: 83
    at org.jboss.netty.buffer.LittleEndianHeapChannelBuffer.getInt(LittleEndianHeapChannelBuffer.java:69)
    at reactivemongo.api.SerializationPack$class.readAndDeserialize(serializationpack.scala:31)
    at reactivemongo.api.BSONSerializationPack$.readAndDeserialize(serializationpack.scala:41)
    at reactivemongo.api.collections.GenericCollection$$anonfun$insert$$anonfun$apply.apply(genericcollection.scala:279)
    at reactivemongo.api.collections.GenericCollection$$anonfun$insert$$anonfun$apply.apply(genericcollection.scala:279)
    at scala.util.Success$$anonfun$map.apply(Try.scala:237)
    at scala.util.Try$.apply(Try.scala:192)
    at scala.util.Success.map(Try.scala:237)
    at scala.concurrent.Future$$anonfun$map.apply(Future.scala:235)
    at scala.concurrent.Future$$anonfun$map.apply(Future.scala:235)
    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.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

我这里的目的是在插入Collection后在writeResult上读取WriteResultNmessage)。

使用 mongo 3.0.6,它甚至找不到 mongodb.

23:02:02.172 [ForkJoinPool-6-worker-9] ERROR reactivemongo.api.Failover2 - Got an error, no more attempts to do. Completing with a failure...
reactivemongo.core.actors.Exceptions$PrimaryUnavailableException$: MongoError['No primary node is available!']
    at reactivemongo.core.actors.Exceptions$PrimaryUnavailableException$.<clinit>(actors.scala) ~[reactivemongo_2.11-0.11.9.jar:0.11.9]
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$reactivemongo$core$actors$MongoDBSystem$$pickChannel.apply(actors.scala:681) ~[reactivemongo_2.11-0.11.9.jar:0.11.9]
    at reactivemongo.core.actors.MongoDBSystem$$anonfun$reactivemongo$core$actors$MongoDBSystem$$pickChannel.apply(actors.scala:681) ~[reactivemongo_2.11-0.11.9.jar:0.11.9]
    at scala.Option.getOrElse(Option.scala:121) ~[scala-library-2.11.7.jar:?]
    at reactivemongo.core.actors.MongoDBSystem$class.reactivemongo$core$actors$MongoDBSystem$$pickChannel(actors.scala:681) ~[reactivemongo_2.11-0.11.9.jar:0.11.9]
    at reactivemongo.core.actors.MongoDBSystem$$anonfun.applyOrElse(actors.scala:335) ~[reactivemongo_2.11-0.11.9.jar:0.11.9]
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:170) ~[scala-library-2.11.7.jar:?]
    at akka.actor.Actor$class.aroundReceive(Actor.scala:467) ~[akka-actor_2.11-2.3.13.jar:?]
    at reactivemongo.core.actors.LegacyDBSystem.aroundReceive(actors.scala:796) ~[reactivemongo_2.11-0.11.9.jar:0.11.9]
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) ~[akka-actor_2.11-2.3.13.jar:?]
    at akka.actor.ActorCell.invoke(ActorCell.scala:487) ~[akka-actor_2.11-2.3.13.jar:?]
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238) ~[akka-actor_2.11-2.3.13.jar:?]
    at akka.dispatch.Mailbox.run(Mailbox.scala:220) ~[akka-actor_2.11-2.3.13.jar:?]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) ~[akka-actor_2.11-2.3.13.jar:?]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.7.jar:?]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.7.jar:?]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.7.jar:?]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.7.jar:?]

不过,我在 mongo、

上看到连接登录
2015-12-30T23:21:57.688-0500 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51663 #11 (11 connections now open)

参考

http://reactivemongo.org/releases/0.11/documentation/tutorial/write-documents.html

https://groups.google.com/forum/#!topic/reactivemongo/0jxS1cgXjP8

所以,事情是 reactivemongo 0.11.4 支持 mongodb 3.0+。然后我不得不更新 mongodb 主机。

  val driver = new MongoDriver
  val connection = driver.connection(List("127.0.0.1:27017"))

阅读

java.lang.ArrayIndexOutOfBoundsException on update with mongo 2.4.12 #394