2

问题: MongoDB 写入失败并出现错误 -

等待与 PrimaryServerSelector 匹配的服务器时,在 30000 毫秒后超时。集群状态的客户端视图是 {type=REPLICA_SET, servers=[{address=intdb01:27017, type=REPLICA_SET_SECONDARY, roundTripTime=0.7 ms, state=CONNECTED}]

当主数据库从intdb01切换到intdb02时会发生这种情况。看起来客户端驱动程序仍在寻找intdb01 作为主节点。

我们的设置

我们在一个名为 rs0 的副本集中有 3 个 mongoDb 节点。当我们使用 Java 连接到它时,我们在连接字符串中给所有 3 个服务器如下: mongodb://intdb01:27017,intdb02:27017,intdb03:27017/?replicaSet=rs0

db.version --> 3.0.4

Java驱动版本:mongodb-driver-3.0.4.jar, mongodb-driver-core-3.0.4.jar

客户端连接代码:

if( mongoClient == null) {
    MongoClientURI mcu = new MongoClientURI(mongoConnect);
    mongoClient = new MongoClient(mcu);
}

mongoConnect 包含上面显示的连接字符串。

Mongo 副本集状态信息

> rs.status()
{
  "set" : "rs0",
  "date" : ISODate("2016-07-19T21:14:03.001Z"),
  "myState" : 1,
  "members" : [{
        "_id" : 0,
        "name" : "intdb01",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 439786,
        "optime" : Timestamp(1468521291, 5),
        "optimeDate" : ISODate("2016-07-14T18:34:51Z"),
        "lastHeartbeat" : ISODate("2016-07-19T21:14:01.877Z"),
        "lastHeartbeatRecv" : ISODate("2016-07-19T21:14:01.611Z"),
        "pingMs" : 0,
        "configVersion" : 4
    },
    {
        "_id" : 1,
        "name" : "intdb02:27017",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 2948844,
        "optime" : Timestamp(1468521291, 5),
        "optimeDate" : ISODate("2016-07-14T18:34:51Z"),
        "electionTime" : Timestamp(1468523057, 1),
        "electionDate" : ISODate("2016-07-14T19:04:17Z"),
        "configVersion" : 4,
        "self" : true
    },
    {
        "_id" : 2,
        "name" : "intdb03:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 439779,
        "optime" : Timestamp(1468521291, 5),
        "optimeDate" : ISODate("2016-07-14T18:34:51Z"),
        "lastHeartbeat" : ISODate("2016-07-19T21:14:01.294Z"),
        "lastHeartbeatRecv" : ISODate("2016-07-19T21:14:01.294Z"),
        "pingMs" : 0,
        "configVersion" : 4
    }
  ],
  "ok" : 1
}

我已经检查并重新检查了文档中的代码/配置,但找不到问题所在。我们可以通过降低主节点来模拟这一点。辅助节点接管,但应用程序写入开始失败。:-(

非常感谢任何指针/提示!

添加堆栈跟踪-

Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector. Client view of cluster state is {type=REPLICA_SET, servers=[{address=intdb01:27017, type=REPLICA_SET_SECONDARY, roundTripTime=0.7 ms, state=CONNECTED}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:175)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:141)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:72)
at com.mongodb.Mongo.execute(Mongo.java:747)
at com.mongodb.Mongo$2.execute(Mongo.java:730)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:482)
at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:474)
at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:325)
at com.grid.core.persistence.mongodb.dao.GridEventDao.save(GridEventDao.java:69)
... 6 more
2016-07-19 17:05:01,882 [pool-2-thread-2] INFO   org.mongodb.driver.cluster - No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=SINGLE, all=[ServerDescription{address=intdb01:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 4]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=724681, setName='rs0', canonicalAddress=intdb01:27017, hosts=[intdb01:27017, intdb02:27017], passives=[intdb03:27017], arbiters=[], primary='intdb02:27017', tagSet=TagSet{[]}}]}. Waiting for 30000 ms before timing out

我试图弄清楚它是编码错误还是配置。堆栈跟踪中的最后一条信息消息,说 connectionMode 是 SINGLE。怀疑它会导致此问题,但我在开发人员文档站点中找不到任何有关它的信息。

4

1 回答 1

0

所有服务器地址都应出现在错误消息中。例如,尝试使用 3 个无法解析的任意主机名:

com.mongodb.MongoTimeoutException:在等待与 PrimaryServerSelector 匹配的服务器时,在 30000 毫秒后超时。集群状态的客户端视图是 {type=REPLICA_SET, servers=[{address=xxx:27117, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xxx}, 由 {java.net.UnknownHostException: xxx}}, {address=xxx:27118, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xxx}, 由 {java.net.UnknownHostException: xxx}} 引起,{address=xxx:27119 , type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xxx}, 由 {java.net.UnknownHostException: xxx}} 引起]
    在 com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
    在 com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    在 com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:75)
    在 com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:71)
    在 com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
    在 com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:175)
    在 com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:106)
    在 com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:58)
    在 com.mongodb.Mongo.execute(Mongo.java:747)
    在 com.mongodb.Mongo$2.execute(Mongo.java:730)
    在 com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:327)
    在 com.mongodb.DBCollection.insert(DBCollection.java:323)
    在 com.mongodb.DBCollection.insert(DBCollection.java:314)
    在 com.mongodb.DBCollection.insert(DBCollection.java:284)
    在 com.mongodb.DBCollection.insert(DBCollection.java:250)
    在 com.mongodb.DBCollection.insert(DBCollection.java:187)
    在 InsertTest2.main(InsertTest2.java:26)

注意所有 3 个服务器地址是如何出现的。我建议您创建一个像这样的简单测试程序并进行验证,然后从那里向后工作。还要验证主机名是否正确解析,您也可以尝试 IP 地址。

于 2016-07-20T21:24:05.307 回答