集群选举算法实现 (2)

  检查Beacon配置,若只有一个Beacon节点则自动成为Leader:

void SessionOnlineNodes::InitElection(const neb::CJsonObject& oBeacon) { neb::CJsonObject oBeaconList = oBeacon; for (int i = 0; i < oBeaconList.GetArraySize(); ++i) { m_mapBeacon.insert(std::make_pair(oBeaconList(i) + ".1", 0)); } if (m_mapBeacon.size() == 0) { m_bIsLeader = true; } else if (m_mapBeacon.size() == 1 && GetNodeIdentify() == m_mapBeacon.begin()->first) { m_bIsLeader = true; } else { SendBeaconBeat(); } }

  发送Beacon心跳:

void SessionOnlineNodes::SendBeaconBeat() { LOG4_TRACE(""); MsgBody oMsgBody; Election oElection; if (m_bIsLeader) { oElection.set_is_leader(1); oElection.set_last_node_id(m_unLastNodeId); for (auto it = m_setAddedNodeId.begin(); it != m_setAddedNodeId.end(); ++it) { oElection.add_added_node_id(*it); } for (auto it = m_setRemovedNodeId.begin(); it != m_setRemovedNodeId.end(); ++it) { oElection.add_removed_node_id(*it); } } else { oElection.set_is_leader(0); } m_setAddedNodeId.clear(); m_setRemovedNodeId.clear(); oMsgBody.set_data(oElection.SerializeAsString()); for (auto iter = m_mapBeacon.begin(); iter != m_mapBeacon.end(); ++iter) { if (GetNodeIdentify() != iter->first) { SendTo(iter->first, neb::CMD_REQ_LEADER_ELECTION, GetSequence(), oMsgBody); } } }

  接收Beacon心跳:

void SessionOnlineNodes::AddBeaconBeat(const std::string& strNodeIdentify, const Election& oElection) { if (!m_bIsLeader) { if (oElection.last_node_id() > 0) { m_unLastNodeId = oElection.last_node_id(); } for (int32 i = 0; i < oElection.added_node_id_size(); ++i) { m_setNodeId.insert(oElection.added_node_id(i)); } for (int32 j = 0; j < oElection.removed_node_id_size(); ++j) { m_setNodeId.erase(m_setNodeId.find(oElection.removed_node_id(j))); } } auto iter = m_mapBeacon.find(strNodeIdentify); if (iter == m_mapBeacon.end()) { uint32 uiBeaconAttr = 1; if (oElection.is_leader() != 0) { uiBeaconAttr |= mc_uiLeader; } m_mapBeacon.insert(std::make_pair(strNodeIdentify, uiBeaconAttr)); } else { iter->second |= 1; if (oElection.is_leader() != 0) { iter->second |= mc_uiLeader; } } }

  检查在线leader,成为leader:

void SessionOnlineNodes::CheckLeader() { LOG4_TRACE(""); std::string strLeader; for (auto iter = m_mapBeacon.begin(); iter != m_mapBeacon.end(); ++iter) { if (mc_uiAlive & iter->second) { if (mc_uiLeader & iter->second) { strLeader = iter->first; } else if (strLeader.size() == 0) { strLeader = iter->first; } } else { iter->second &= (~mc_uiLeader); } uint32 uiLeaderBit = mc_uiLeader & iter->second; iter->second = ((iter->second << 1) & mc_uiAlive) | uiLeaderBit; if (iter->first == GetNodeIdentify()) { iter->second |= 1; } } if (strLeader == GetNodeIdentify()) { m_bIsLeader = true; } } 6. Beacon节点切换leader

  通过Nebula集群的命令行管理工具nebcli可以很方便的查看Beacon节点状态,nebcli的使用说明见Nebcli项目的README。下面启动三个Beacon节点,并反复kill掉Beacon进程和重启,查看leader节点的切换情况。

  启动三个beacon节点:

nebcli): show beacon node is_leader is_online 192.168.157.176:16000.1 yes yes 192.168.157.176:17000.1 no yes 192.168.157.176:18000.1 no yes

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdxsx.html