如何使用mongodb java driver 3.0.0从Secondary获取配置文件状态?

时间:2015-05-05 16:16:28

标签: java mongodb

我无法获取辅助服务器的配置文件状态(s132),因为驱动程序正在循环并最终超时:

2015-05-05 17:59:55,829 [main] INFO  org.mongodb.driver.cluster - No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=SINGLE, all=[ServerDescription{address=s132:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[2, 6, 4]}, minWireVersion=0, maxWireVersion=2, maxDocumentSize=16777216, roundTripTimeNanos=8836176, setName='offerStoreDE3', hosts=[s132:27017, s124:27017], passives=[], arbiters=[], primary='s124:27017', tagSet=TagSet{[]}}]}. Waiting for 30000 ms before timing out
2015-05-05 17:59:56,315 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017
2015-05-05 17:59:56,324 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=REPLICA_SET, servers=[{address=s132:27017, type=REPLICA_SET_SECONDARY, roundTripTime=8,8 ms, state=CONNECTED}]
2015-05-05 17:59:56,824 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017
2015-05-05 17:59:56,834 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=REPLICA_SET, servers=[{address=s132:27017, type=REPLICA_SET_SECONDARY, roundTripTime=8,9 ms, state=CONNECTED}]
2015-05-05 17:59:57,333 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017
2015-05-05 17:59:57,343 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=REPLICA_SET, servers=[{address=s132:27017, type=REPLICA_SET_SECONDARY, roundTripTime=8,8 ms, state=CONNECTED}]
2015-05-05 17:59:57,842 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017

但是,询问主要的配置文件状态(s124)可以正常工作。

我的java代码是这样的:

MongoClientOptions options = MongoClientOptions.builder().
                    connectTimeout(2000).
                    socketTimeout(10000).
                    readPreference(ReadPreference.secondaryPreferred()).
                    build();
MongoCredential mc = MongoCredential.createCredential("user", "db", "XXXX".toCharArray());
MongoClient mongo = new MongoClient(new ServerAddress("s132:27017"), Lists.newArrayList(mc), options);
MongoDatabase myDB = mongo.getDatabase("myDB");

BsonDocument bd = BsonDocument.parse("{profile:1}");
Bson bson = (Bson)bd;
Document doc = myDB.runCommand(bson); //loops until timeout
Object obj = doc.get("slowms");

在mongo shell中,我可以毫无问题地做同样的事情:

offerStoreDE3:SECONDARY> db.runCommand({profile:1})
{ "was" : 0, "slowms" : 100, "ok" : 1 }

replSet的状态如下:

offerStoreDE3:SECONDARY> rs.status()
{
    "set" : "offerStoreDE3",
    "date" : ISODate("2015-05-05T16:09:47Z"),
    "myState" : 2,
    "syncingTo" : "s124:27017",
    "members" : [
        {
            "_id" : 7,
            "name" : "s124:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1400,
            "optime" : Timestamp(1430740008, 1),
            "optimeDate" : ISODate("2015-05-04T11:46:48Z"),
            "lastHeartbeat" : ISODate("2015-05-05T16:09:45Z"),
            "lastHeartbeatRecv" : ISODate("2015-05-05T16:09:47Z"),
            "pingMs" : 0,
            "electionTime" : Timestamp(1430840794, 1),
            "electionDate" : ISODate("2015-05-05T15:46:34Z")
        },
        {
            "_id" : 8,
            "name" : "s132:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1740,
            "optime" : Timestamp(1430740008, 1),
            "optimeDate" : ISODate("2015-05-04T11:46:48Z"),
            "self" : true
        }
    ],
    "ok" : 1
}

1 个答案:

答案 0 :(得分:1)

runCommand(bson);将始终将命令定向到主节点,因为它下面执行写命令。

对于显式读取命令,必须提供读取首选项,例如:

Document doc = myDB.runCommand(bson, ReadPreference.secondaryPreferred());