D
D
Dmitry Labutin2014-04-09 08:53:12
PHP
Dmitry Labutin, 2014-04-09 08:53:12

MONGO: Why doesn't the connection immediately switch to the second server if the first one fails?

There are three mongo servers: PRIMARY, SECONDARY and ARBITER

pkrs:PRIMARY> rs.status()
{
"set" : "pkrs",
"date" : ISODate("2014-04-09T05:44:14Z"),
"myState" : 1,
"members" : [
{
" _id" : 0,
"name" : "mongodb01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 93,
"optime" : Timestamp(1397022230, 2 ),
"optimeDate" : ISODate("2014-04-09T05:43:50Z"),
"self" : true
},
{
"_id" : 1,
"name" : "mongodb02:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14,
"optime" : Timestamp(1397022230, 2),
"optimeDate" : ISODate("2014-04-09T05:43:50Z"),
"lastHeartbeat" : ISODate("2014-04-09T05:44:14Z"),
"lastHeartbeatRecv" : ISODate("2014- 04-09T05:44:13Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "syncing to: mongodb01:27017",
"syncingTo" : "mongodb01:27017"
},
{
"_id" : 2,
"name" : "mongodba:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime " : 92,
"lastHeartbeat" : ISODate("2014-04-09T05:44:14Z"),
"lastHeartbeatRecv" : ISODate("2014-04-09T05:44:14Z"),
"pingMs" :0
}
],
"ok" : 1
}

Normally everything works fine. This is how the connection is done + some test code:
$m = new MongoClient("mongodb://mongodb01,mongodb02,mysql-develop01,mongodba/?replicaSet=pkrs");
$db = $m->main;
$collection = $db->addPrice;
$collection->drop();

The last line is number 6. This is important because below will be the text of the error and there is a link to the 6th line.
I stop the PRIMARY server.
We have this picture.
pkrs:PRIMARY> rs.status()
{
"set" : "pkrs",
"date" : ISODate("2014-04-09T05:49:17Z"),
"myState" : 1,
"members" : [
{
" _id" : 0,
"name" : "mongodb01:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(1397022230, 2),
"optimeDate" :ISODate("2014-04-09T05:43:50Z"),
"lastHeartbeat" : ISODate("2014-04-09T05:49:16Z"),
"lastHeartbeatRecv" : ISODate("2014-04-09T05:49:04Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "mongodb02:27017",
"health" : 1,
" state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 318,
"optime" : Timestamp(1397022230, 2),
"optimeDate" : ISODate("2014-04-09T05:43:50Z"),
" self": true
},
{
"_id" : 2,
"name" : "mongodba:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 317,
"lastHeartbeat" : ISODate("2014-04-09T05:49 :16Z"),
"lastHeartbeatRecv" : ISODate("2014-04-09T05:49:16Z"),
"pingMs" : 0
}
],
"ok" : 1
}

But if we execute the above code in PHP, then within a few seconds we have the following error:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'No candidate servers found' in /var/www/test/mongo.php on line 6
MongoConnectionException: No candidate servers found in /var/www/test/mongo.php on line 6

Actually, the question is: why? After all, the second server has already assumed the role of PRIMARY. Why error? Well, she herself disappears after 4-5 seconds.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2014-04-09
@Labutin

Replica set members send heartbeats (pings) to each other every two seconds. If a heartbeat does not return within 10 seconds, the other members mark the delinquent member as inaccessible.
docs.mongodb.org/manual/core/replica-set-elections
In short, this is how it should be. It doesn't work instantly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question