对于Replica Set中的选择策略:
We use a consensus protocol to pick a primary. Exact details will be spared here but that basic process is:
1 get maxLocalOpOrdinal from each server.
2 if a majority of servers are not up (from this server's POV), remain in Secondary mode and stop.
3 if the last op time seems very old, stop and await human intervention.
4 else, using a consensus protocol, pick the server with the highest maxLocalOpOrdinal as the Primary.
对于策略2:当集群里的大多数服务器发生down 机了,剩余的节点就会保持在secondary模式并停止服务。
做了实验结果是对于4节点的 Replica Set,当两个secondary节点down了的时候,主节点变为secondary。整个集群相当于挂了,因为secondary 不提供读写操作。。
在一个集群中关闭两个secondary 节点:rac4:27019和rac3:27017
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27019
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27019/test
SECONDARY>
SECONDARY> use admin
switched to db admin
SECONDARY> db.shutdownServer();
Wed Nov 2 11:02:29 DBClientCursor::init call() failed
Wed Nov 2 11:02:29 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:27019
server should be down...
[mongodb@rac3 bin]$ ./mongo 10.250.7.241:27017
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27017/test
SECONDARY>
SECONDARY> use admin
switched to db admin
SECONDARY> db.shutdownServer();
Tue Nov 1 22:02:46 DBClientCursor::init call() failed
Tue Nov 1 22:02:46 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:27017
server should be down...
Tue Nov 1 22:02:46 trying reconnect to 127.0.0.1:27017
Tue Nov 1 22:02:46 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
Tue Nov 1 22:02:46 Error: error doing query: unknown shell/collection.js:150
从主库的客户端退出以后,再次进入提示符发生变化:由PRIMARY--->SECONDARY ,查看Replica Set的状态信息:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
SECONDARY>
SECONDARY> rs.status();
{
"set" : "myset",
"date" : ISODate("2011-11-01T13:56:05Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "10.250.7.220:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 101,
"optime" : {
"t" : 1320154033000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T13:27:13Z"),
"lastHeartbeat" : ISODate("2011-11-01T13:56:04Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "10.250.7.220:27019",
"health" : 0, --已经关闭
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"t" : 1320154033000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T13:27:13Z"),
"lastHeartbeat" : ISODate("2011-11-01T13:53:50Z"),
"pingMs" : 0,
"errmsg" : "socket exception"
},
{
"_id" : 2,
"name" : "10.250.7.220:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", ---由主库变为从库
"optime" : {
"t" : 1320154033000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T13:27:13Z"),
"self" : true
},
{
"_id" : 3,
"name" : "10.250.7.241:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"t" : 1320154033000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T13:27:13Z"),
"lastHeartbeat" : ISODate("2011-11-01T13:53:54Z"),
"pingMs" : 0,
"errmsg" : "socket exception"
}
],
"ok" : 1
}
SECONDARY> exut
Wed Nov 2 15:23:02 ReferenceError: exut is not defined (shell):1
Wed Nov 2 15:23:02 DBClientCursor::init call() failed
> exit
bye
未完 待续。。。。