当主服务器节点在replicaset

时间:2016-07-20 18:32:14

标签: java mongodb replicaset mongodb-replica-set

问题: MongoDB写入失败并显示错误 -

  

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

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

我们的设置

我们在名为rs0的replicaSet中有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 Replicaset状态信息

> 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。怀疑它导致了这个问题,但我在开发者文档站点中找不到任何关于它的信息。

1 个答案:

答案 0 :(得分:0)

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

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=xxx:27117, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xxx}, caused by {java.net.UnknownHostException: xxx}}, {address=xxx:27118, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xxx}, caused by {java.net.UnknownHostException: xxx}}, {address=xxx:27119, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xxx}, caused by {java.net.UnknownHostException: xxx}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(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.BaseWriteOperation.execute(BaseWriteOperation.java:106)
    at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:58)
    at com.mongodb.Mongo.execute(Mongo.java:747)
    at com.mongodb.Mongo$2.execute(Mongo.java:730)
    at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:327)
    at com.mongodb.DBCollection.insert(DBCollection.java:323)
    at com.mongodb.DBCollection.insert(DBCollection.java:314)
    at com.mongodb.DBCollection.insert(DBCollection.java:284)
    at com.mongodb.DBCollection.insert(DBCollection.java:250)
    at com.mongodb.DBCollection.insert(DBCollection.java:187)
    at InsertTest2.main(InsertTest2.java:26)

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