mongoDB pair replication [ two master when remote unreachable ]

测试环境如图:
使用对外连接的网卡作为心跳网卡,VIP作为与外部应用服务器通信的端口。

mongoDB pair replication [ two master when remote unreachable ]

当比较神奇的现象出现的时候,假设这两台服务器不通了,但是和外面的应用服务器已经交换机是通的。正常情况下mongoDB会选出新的primary(下面用iptables来模拟)
如下图,A机和B机都变成primary了,
在mongo shell中的表现:
A(原来的primary)机器:
经过一段时间之后(约20秒)
> db.isMaster()
{
        "ismaster" : 1,
        "remote" : "192.168.169.90:5281",
        "info" : "remote unreachable",
        "ok" : 1
}
B机器:
经过一段时间之后(约20秒)
> db.isMaster()
{
        "ismaster" : 1,
        "remote" : "192.168.169.90:5281",
        "info" : "remote unreachable",
        "ok" : 1
}
对于VIP的漂移来说,判断对方不可达了就启VIP。当然你可以选择另外的心跳(比如串口),这里是模拟两台都primary了后面会怎么样?
那么在交换机上192.168.169.99的mac地址会不断的被这两台mongodb服务器刷新成自己的MAC。
应用程序幸运的话可能会对两个数据库都有写入操作.
模拟:
A机器写入数据:
> db.tbl_test.insert({"id" : "a"})
> db.tbl_test.insert({"id" : "a"})
> db.tbl_test.insert({"id" : "a"})
> db.tbl_test.insert({"id" : "a"})
> db.tbl_test.insert({"id" : "a"})
> db.tbl_test.insert({"id" : "a"})
> db.tbl_test.insert({"id" : "a"})
B机器写入数据:
> db.tbl_test.insert({"id" : "b"})
> db.tbl_test.insert({"id" : "b"})
> db.tbl_test.insert({"id" : "b"})
> db.tbl_test.insert({"id" : "b"})
> db.tbl_test.insert({"id" : "b"})
> db.tbl_test.insert({"id" : "b"})
> db.tbl_test.insert({"id" : "b"})

 
mongoDB pair replication [ two master when remote unreachable ]
 

假设A机器和B机器之间的网络恢复正常了,两台mongodb将重新协商谁将成为primary。(具体的选择算法参考mongoDB手册)
假设A又变成primary了。
来查询一下刚才插入的数据表:
> db.tbl_test.find()
{ "_id" : ObjectId("4d26c6127073f6939f0149e1"), "id" : "b" }
{ "_id" : ObjectId("4d26c6197073f6939f0149e2"), "id" : "b" }
{ "_id" : ObjectId("4d26c6197073f6939f0149e3"), "id" : "b" }
{ "_id" : ObjectId("4d26c6197073f6939f0149e4"), "id" : "b" }
{ "_id" : ObjectId("4d26c6197073f6939f0149e5"), "id" : "b" }
{ "_id" : ObjectId("4d26c61a7073f6939f0149e6"), "id" : "b" }
{ "_id" : ObjectId("4d26c61a7073f6939f0149e7"), "id" : "b" }
{ "_id" : ObjectId("4d26c60355fd2c402a5c025c"), "id" : "a" }
{ "_id" : ObjectId("4d26c61555fd2c402a5c025d"), "id" : "a" }
{ "_id" : ObjectId("4d26c61655fd2c402a5c025e"), "id" : "a" }
{ "_id" : ObjectId("4d26c61655fd2c402a5c025f"), "id" : "a" }
{ "_id" : ObjectId("4d26c61655fd2c402a5c0260"), "id" : "a" }
{ "_id" : ObjectId("4d26c61655fd2c402a5c0261"), "id" : "a" }
{ "_id" : ObjectId("4d26c61655fd2c402a5c0262"), "id" : "a" }
数据被合并了!

mongoDB pair replication [ two master when remote unreachable ]
 
总结:
1. 选择好的心跳。加入更完善的判断。