首页 > > > MongoDB复制集与Raft协议异同点分析
五、如何确保每个节点在一个term中只投票一次:
a、raft “Each server gives only one vote per term (persist on disk)”,也就是说会将相应信息持久化到磁盘上,具体可参考mongodb。
b、mongodb将投票信息持久化到local库下replset.election中,内容如:{ "_id" : ObjectId("58cbe1844857daa6e06ed9da"), "term" : NumberLong(4), "candidateIndex" : NumberLong(0) },记录了在那个term中给谁(candidateIndex)投票了。通过_id字段的ObjectId对象能获取投票时间。
六、新主是否会做catchup:
a、raft,“Leader’s log is “the truth””,主节点的数据是真理,新主产生后,不会从存活的从节点上拷最新的数据;
b、mongodb,默认会有2s的catchup时间,如果发现从节点数据比新主新,那么在这时间内会catchup
//两则不同的原因是,mongodb是个AP系统,C无法满足。存在2种情况,如果设置为w=1,那么如果主挂了,数据可能丢失。如果w=majority,那么如果还未满足majority时,主挂了,也就是说客户端返回错误,但这并不表示数据就写入失败了,需要等新主产生后进一步确认,因为即使新主本来没有这部分数据,也可能在catchup节点从其他节点获取。所以,这跟mysql等关系型数据库不一样。
七、主怎么知道从已经收到日志/回放了:
a、raft,通过AppendEntries RPC返回结果;
b、通过replSetUpdatePosition命令;
2017-03-30T10:48:12.839+0800 I COMMAND [conn647] command admin.$cmd command: replSetUpdatePosition { replSetUpdatePosition: 1, optimes: [ { durableOpTime: { ts: Timestamp 1490797135000|2, t: 3 }, appliedOpTime: { ts: Timestamp 1490797135000|2, t: 3 }, memberId: 0, cfgver: 454570 }, { durableOpTime: { ts: Timestamp 1490842087000|1, t: 4 }, appliedOpTime: { ts: Timestamp 1490842087000|1, t: 4 }, memberId: 1, cfgver: 454570 }, { durableOpTime: { ts: Timestamp 1490842087000|1, t: 4 }, appliedOpTime: { ts: Timestamp 1490842087000|1, t: 4 }, memberId: 2, cfgver: 454570 } ], $replData: { term: 4, lastOpCommitted: { ts: Timestamp 1490842087000|1, t: 4 }, lastOpVisible: { ts: Timestamp 0|0, t: -1 }, configVersion: 454570, replicaSetId: ObjectId('58cbe1844857daa6e06ed9d7'), primaryIndex: 0, syncSourceIndex: 0 } } numYields:0 reslen:22 locks:{} protocol:op_command 0ms原文作者:;如有侵权,请联系管理员删除。