MongoDB源码阅读之ReplSet源码分析
1. ReplSet源码结构rs_config.h replSet间同步设置的工具类
rs_member.h 心跳检测类和replSet成员状态的定义
rs_sync.h 同步数据类
rs.h 定义了几乎所有replSet相关的类(Member:replSet中的节点成员,
GhostSync:备份同步类,ReplSet:管理所有Member的核心类)
2. ReplSet的结构分析
RSBase定义了线程锁。
ReplSetImpl完成了大部分Replication Set的操作:心跳检测,选举,设置主节点,查询节点信息。
Member记录其他节点的信息,Manager查询节点信息,ReplSetHealthPoolTask心跳检测,ReplSetConfig同步设置,Consensus选举主节点,StateBox设置主节点。
ReplSet封装了很多方便的操作。
3. 创建并设置ReplSet
在mongod开始运行后(mongoDBMain),会启动监听程序同时,根据运行参数中是否标明了”--replSet”,调用startReplication来创建ReplSet。
void startReplication() {
/* if we are going to be a replica set, we aren't doing other forms of replication. */
if( !cmdLine._replSet.empty() ) {//看看参数里面有没有--replSet
if( replSettings.slave || replSettings.master ) { //这个参数不能与—slave与—master共存
log() box.getPrimary(); //当前的主服务器
const Member* hopeful = theReplSet->findById(id); //希望成为主机的服务器(从上面的代码看是发送请求方服务器)
const Member *highestPriority = theReplSet->getMostElectable(); //当前节点心目中的主机
//以下判断发现不符合条件的就否决投票
if( !hopeful ) {//没有目标服务器
errmsg = str::stream() lastOpTimeWritten >= hopeful->hbinfo().opTime ) {
// hbinfo is not updated, so we have to check the primary's last optime separately
errmsg = str::stream() = hopeful->hbinfo().opTime ) {
// other members might be aware of more up-to-date nodes
errmsg = str::stream() fullName()hopeful->config().priority) {
errmsg = str::stream() fullName() isElectable(id) ||
(highestPriority && highestPriority->config().priority > hopeful->config().priority)) {
return true;
}
return false;
}
//得到投票数据以后解析
bool Consensus::weAreFreshest(bool& allUp, int& nTies) {
//省略发送请求的部分
//请求返回后存储在list中
int nok = 0;
allUp = true;
for( list::iterator i = L.begin(); i != L.end(); i++ ) {
//i存储这其他节点返回的结果
if( i->ok ) {
nok++;
if( i->result["fresher"].trueValue() ) { //当前服务器不是最新的
log() result["errmsg"];
if (!msg.eoo()) {
log()
页:
[1]