简体   繁体   中英

MongoClient doesn't want to discover replicaset nodes

I hope you're all doing well.

I have a replicaset of two nodes:

messagesdbvirtual:SECONDARY> rs.status()
{
        "set" : "messagesdbvirtual",
        "date" : ISODate("2020-06-23T18:42:24.820Z"),
        "myState" : 2,
        "term" : NumberLong(1),
        "syncingTo" : "messagesdb1:27017",
        "syncSourceHost" : "messagesdb1:27017",
        "syncSourceId" : 0,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1592937734, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-06-23T18:42:14.862Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1592937734, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-06-23T18:42:14.862Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1592937734, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1592937734, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-06-23T18:42:14.862Z"),
                "lastDurableWallTime" : ISODate("2020-06-23T18:42:14.862Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1592937704, 1),
        "lastStableCheckpointTimestamp" : Timestamp(1592937704, 1),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "messagesdb1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 176458,
                        "optime" : {
                                "ts" : Timestamp(1592937734, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1592937734, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-06-23T18:42:14Z"),
                        "optimeDurableDate" : ISODate("2020-06-23T18:42:14Z"),
                        "lastHeartbeat" : ISODate("2020-06-23T18:42:23.544Z"),
                        "lastHeartbeatRecv" : ISODate("2020-06-23T18:42:23.865Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1592761269, 2),
                        "electionDate" : ISODate("2020-06-21T17:41:09Z"),
                        "configVersion" : 2
                },
                {
                        "_id" : 1,
                        "name" : "messagesdb2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 184894,
                        "optime" : {
                                "ts" : Timestamp(1592937734, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-06-23T18:42:14Z"),
                        "syncingTo" : "messagesdb1:27017",
                        "syncSourceHost" : "messagesdb1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1592937734, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1592937734, 1)
}

However, when I'm trying to list the seed using MongoClient I see none:

In [81]: import newpymongo

In [82]: newpymongo.version
Out[82]: '3.10.1'

In [83]: client = newpymongo.MongoClient("messagesdb1.virtual.ninternal.com:27017", replicaSet="messagesdbvirtual")

In [84]: client
Out[84]: MongoClient(host=[u'messagesdb1.virtual.ninternal.com:27017'], document_class=dict, tz_aware=False, connect=True, replicaset=u'messagesdbvirtual')

In [85]: client.nodes
Out[85]: frozenset()

In [86]: client.nodes
Out[86]: frozenset()

A client created without replicaSet argument would see a single node:

In [87]: client = newpymongo.MongoClient("messagesdb1.virtual.ninternal.com:27017")

In [88]: client.nodes()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-88-93c215ff96d9> in <module>()
----> 1 client.nodes()

TypeError: 'frozenset' object is not callable

In [89]: client.nodes
Out[89]: frozenset({(u'messagesdb1.virtual.ninternal.com', 27017)})

Any suggestions where I should be looking further? Thanks.

UPDATE: The problem was hostnames discover. See @D. SM's comment for details.

It is likely that the client prints the nodes without having a chance first to discover them all.

From the API documentation :

The MongoClient constructor is non-blocking: the constructor returns immediately while the client connects to the replica set using background threads. Note how, if you create a client and immediately print the string representation of its nodes attribute, the list may be empty initially. If you wait a moment, MongoClient discovers the whole replica set:

>>> from time import sleep
>>> c = MongoClient(replicaset='foo'); print(c.nodes); sleep(0.1); print(c.nodes)
frozenset([])
frozenset([(u'localhost', 27019), (u'localhost', 27017), (u'localhost', 27018)])

So just add sleep for a short while.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM