Error: "Failed to do query, no good nodes" when setting reading preferences to ReadPreference_Nearest

571 views
Skip to first unread message

Kirill Bogdanov

unread,
Aug 6, 2014, 5:47:26 AM8/6/14
to mongod...@googlegroups.com
Hi,

I am just starting with MongoDB and I trying to use c++ driver to talk to my db. I have a cluster of 3 nodes: master and 2 slaves, I can read and write to db, although I am getting an error when I setting reading preferences to nearest, in that case my reads fail. Could you please explain what I am doing wrong here.

Error message:
terminate called after throwing an instance of 'mongo::UserException'
  what():  Failed to do query, no good nodes in rs0
Aborted (core dumped)

I have checked source code of the driver and I found that I am getting exception because I can't get LastHost here:
bool DBClientReplicaSet::checkLastHost(const ReadPreferenceSetting* readPref) {
        // Can't use a cached host if we don't have one.
        if (!_lastSlaveOkConn.get() || _lastSlaveOkHost.empty()) {
            return false;
        }


My code:

void doWork( string collName , DBClientReplicaSet * conn ) {

BSONObj p1 = BSON( "name1" << "Joe" << "age" << 3 );
BSONObj p2 = BSON( "name2" << "Joe1" << "age" << 332 );
BSONObj p3 = BSON( "name3" << "Joe2" << "age" << 333 );

conn->insert( collName , p1 , 0 );
conn->insert( collName , p2 , 0 );
conn->insert( collName , p3, 0 );

BSONArray arr = BSON_ARRAY(  p1 );
Query query;

query.readPref(mongo::ReadPreference_Nearest, arr);   //<- fail
//query.readPref(mongo::ReadPreference_SecondaryPreferred, arr); <-works OK

auto_ptr<DBClientCursor> cursor = conn->query( "mytestdb.sample" ,query);
cout<<" Reading: "<<endl;
int count = 0;
while ( cursor->more() ) {
count++;
BSONObj obj = cursor->next();
cout << "\t " << obj << endl;
}


}

int main( int argc , const char ** argv ) {

    Status status = client::initialize();
    string errmsg;
    ConnectionString cs = ConnectionString::parse( "mongodb://nslrack60/?replicaSet=rs0" , errmsg ); <-connecting to master


    DBClientReplicaSet * conn = static_cast<DBClientReplicaSet*>( cs.connect( errmsg,  0 ) );

    string collName = "mytestdb.sample";

    conn->dropCollection( collName );

    doWork(collName, static_cast<DBClientReplicaSet*>( cs.connect(errmsg) ) );

    return EXIT_SUCCESS;
}


Thank you
 

Tyler Brock

unread,
Aug 18, 2014, 2:21:40 PM8/18/14
to mongod...@googlegroups.com
The reason the first one works and the second fails is because of the tags you are providing:
{name1: "Joe", age : 3}

I'll walk through why this is the case. Your MonogDB connection string provides a master node only so this is what happens during node the node selection phase:

ReadPreference_SecondaryPreferred
 - Attempt to select a secondary with the tag set you provided fails (there are no secondaries)
 - Read from primary is performed (tag set filtering is not applied to primary node)

ReadPreference_Nearest
 - Treat all nodes the same, irrespective of type so apply tag set filtering to all nodes
 - No nodes match your tag set, the query fails.

To understand the SecondaryPreferred case consider what the semantics of SecondaryPreferred mean. When we are falling back on the primary node the read should succeed at this point regardless of the tags applied to our ideal selection for the read because at this point we've exhausted our preferential options.

You can read more about the way they work here:

I assume this is not what you want (keep in mind, tag sets for a read preference are different from a query on the object you want)

Change the query line to pass an empty BSONArray like this:
query.readPref(mongo::ReadPreference_Nearest, BSONArray());

It works here when I try it with the latest driver on our legacy branch. Give it a whirl and let me know if that works for you.

Also it is good practive to put all of the nodes in your seed list for the replica set connection. If you don't then it is possible that the single node you provide goes down and then no "discovery" of other nodes will happen.

-Tyler
Reply all
Reply to author
Forward
0 new messages