Database :: ~Database() { delete m_poValueStore; delete m_poLevelDB; PLG1Head("LevelDB Deleted. Path %s", m_sDBPath.c_str()); }
int MasterStateMachine :: Init() { MasterVariables oVariables; int ret = m_oMVStore.Read(m_iMyGroupIdx, oVariables); if (ret != 0 && ret != 1) { PLG1Err("Master variables read from store fail, ret %d", ret); return -1; } if (ret == 1) { PLG1Imp("no master variables exist"); } else { m_llMasterVersion = oVariables.version(); if (oVariables.masternodeid() == m_iMyNodeID) { m_iMasterNodeID = nullnode; m_llAbsExpireTime = 0; } else { m_iMasterNodeID = oVariables.masternodeid(); m_llAbsExpireTime = Time::GetTimestampMS() + oVariables.leasetime(); } } PLG1Head("OK, master nodeid %lu version %lu expiretime %u", m_iMasterNodeID, m_llMasterVersion, m_llAbsExpireTime); return 0; }
int MasterStateMachine :: LearnMaster( const uint64_t llInstanceID, const MasterOperator & oMasterOper, const uint64_t llAbsMasterTimeout) { ScopedLock<Mutex> oLockGuard(m_oMutex); if (oMasterOper.version() != m_llMasterVersion) { PLG1Err("version conflit, op version %lu now master version %lu", oMasterOper.version(), m_llMasterVersion); return 0; } int ret = UpdateMasterToStore(oMasterOper.nodeid(), llInstanceID, oMasterOper.timeout()); if (ret != 0) { PLG1Err("UpdateMasterToStore fail, ret %d", ret); return -1; } m_iMasterNodeID = oMasterOper.nodeid(); if (m_iMasterNodeID == m_iMyNodeID) { //self be master //use local abstimeout m_llAbsExpireTime = llAbsMasterTimeout; PLG1Head("Be master success, absexpiretime %lu", m_llAbsExpireTime); } else { //other be master //use new start timeout m_llAbsExpireTime = Time::GetTimestampMS() + oMasterOper.timeout(); PLG1Head("Ohter be master, absexpiretime %lu", m_llAbsExpireTime); } m_iLeaseTime = oMasterOper.timeout(); m_llMasterVersion = llInstanceID; PLG1Imp("OK, masternodeid %lu version %lu abstimeout %lu", m_iMasterNodeID, m_llMasterVersion, m_llAbsExpireTime); return 0; }
int SystemVSM :: UpdateByCheckpoint(const std::string & sCPBuffer, bool & bChange) { if (sCPBuffer.size() == 0) { return 0; } bChange = false; SystemVariables oVariables; bool bSucc = oVariables.ParseFromArray(sCPBuffer.data(), sCPBuffer.size()); if (!bSucc) { PLG1Err("Variables.ParseFromArray fail, bufferlen %zu", sCPBuffer.size()); return -1; } if (oVariables.version() == (uint64_t)-1) { PLG1Err("variables.version not init, this is not checkpoint"); return -2; } if (m_oSystemVariables.gid() != 0 && oVariables.gid() != m_oSystemVariables.gid()) { PLG1Err("gid not same, cp.gid %lu now.gid %lu", oVariables.gid(), m_oSystemVariables.gid()); return -2; } if (m_oSystemVariables.version() != (uint64_t)-1 && oVariables.version() <= m_oSystemVariables.version()) { PLG1Imp("lag checkpoint, no need update, cp.version %lu now.version %lu", oVariables.version(), m_oSystemVariables.version()); return 0; } bChange = true; SystemVariables oOldVariables = m_oSystemVariables; int ret = UpdateSystemVariables(oVariables); if (ret != 0) { return -1; } PLG1Head("ok, cp.version %lu cp.membercount %d old.version %lu old.membercount %d", oVariables.version(), oVariables.membership_size(), oOldVariables.version(), oOldVariables.membership_size()); return 0; }
int MasterStateMachine :: UpdateByCheckpoint(const std::string & sCPBuffer, bool & bChange) { if (sCPBuffer.size() == 0) { return 0; } MasterVariables oVariables; bool bSucc = oVariables.ParseFromArray(sCPBuffer.data(), sCPBuffer.size()); if (!bSucc) { PLG1Err("Variables.ParseFromArray fail, bufferlen %zu", sCPBuffer.size()); return -1; } std::lock_guard<std::mutex> oLockGuard(m_oMutex); if (oVariables.version() <= m_llMasterVersion && m_llMasterVersion != (uint64_t)-1) { PLG1Imp("lag checkpoint, no need update, cp.version %lu now.version %lu", oVariables.version(), m_llMasterVersion); return 0; } int ret = UpdateMasterToStore(oVariables.masternodeid(), oVariables.version(), oVariables.leasetime()); if (ret != 0) { return -1; } PLG1Head("ok, cp.version %lu cp.masternodeid %lu old.version %lu old.masternodeid %lu", oVariables.version(), oVariables.masternodeid(), m_llMasterVersion, m_iMasterNodeID); m_llMasterVersion = oVariables.version(); if (oVariables.masternodeid() == m_iMyNodeID) { m_iMasterNodeID = nullnode; m_llAbsExpireTime = 0; } else { m_iMasterNodeID = oVariables.masternodeid(); m_llAbsExpireTime = Time::GetSteadyClockMS() + oVariables.leasetime(); } return 0; }
int Config :: Init() { int ret = m_oSystemVSM.Init(); if (ret != 0) { PLG1Err("fail, ret %d", ret); return ret; } m_oSystemVSM.AddNodeIDList(m_vecNodeInfoList); PLG1Head("OK"); return 0; }
bool SystemVSM :: Execute(const int iGroupIdx, const uint64_t llInstanceID, const std::string & sValue, SMCtx * poSMCtx) { SystemVariables oVariables; bool bSucc = oVariables.ParseFromArray(sValue.data(), sValue.size()); if (!bSucc) { PLG1Err("Variables.ParseFromArray fail, bufferlen %zu", sValue.size()); return false; } int * smret = nullptr; if (poSMCtx != nullptr && poSMCtx->m_pCtx != nullptr) { smret = (int *)poSMCtx->m_pCtx; } if (m_oSystemVariables.gid() != 0 && oVariables.gid() != m_oSystemVariables.gid()) { PLG1Err("modify.gid %lu not equal to now.gid %lu", oVariables.gid(), m_oSystemVariables.gid()); if (smret != nullptr) *smret = Paxos_MembershipOp_GidNotSame; return true; } if (oVariables.version() != m_oSystemVariables.version()) { PLG1Err("modify.version %lu not equal to now.version %lu", oVariables.version(), m_oSystemVariables.version()); if (smret != nullptr) *smret = Paxos_MembershipOp_VersionConflit; return true; } oVariables.set_version(llInstanceID); int ret = UpdateSystemVariables(oVariables); if (ret != 0) { return false; } PLG1Head("OK, new version %lu gid %lu", m_oSystemVariables.version(), m_oSystemVariables.gid()); if (smret != nullptr) *smret = 0; return true; }
Config :: Config( const LogStorage * poLogStorage, const bool bLogSync, const int iSyncInterval, const bool bUseMembership, const NodeInfo & oMyNode, const NodeInfoList & vecNodeInfoList, const FollowerNodeInfoList & vecFollowerNodeInfoList, const int iMyGroupIdx, const int iGroupCount, MembershipChangeCallback pMembershipChangeCallback) : m_bLogSync(bLogSync), m_iSyncInterval(iSyncInterval), m_bUseMembership(bUseMembership), m_iMyNodeID(oMyNode.GetNodeID()), m_iNodeCount(vecNodeInfoList.size()), m_iMyGroupIdx(iMyGroupIdx), m_iGroupCount(iGroupCount), m_oSystemVSM(iMyGroupIdx, oMyNode.GetNodeID(), poLogStorage, pMembershipChangeCallback), m_poMasterSM(nullptr) { m_vecNodeInfoList = vecNodeInfoList; m_bIsIMFollower = false; m_iFollowToNodeID = nullnode; for (auto & oFollowerNodeInfo : vecFollowerNodeInfoList) { if (oFollowerNodeInfo.oMyNode.GetNodeID() == oMyNode.GetNodeID()) { PLG1Head("I'm follower, ip %s port %d nodeid %lu", oMyNode.GetIP().c_str(), oMyNode.GetPort(), oMyNode.GetNodeID()); m_bIsIMFollower = true; m_iFollowToNodeID = oFollowerNodeInfo.oFollowNode.GetNodeID(); InsideOptions::Instance()->SetAsFollower(); } } }
void SystemVSM :: RefleshNodeID() { m_setNodeID.clear(); NodeInfoList vecNodeInfoList; for (int i = 0; i < m_oSystemVariables.membership_size(); i++) { PaxosNodeInfo oNodeInfo = m_oSystemVariables.membership(i); NodeInfo tTmpNode(oNodeInfo.nodeid()); PLG1Head("ip %s port %d nodeid %lu", tTmpNode.GetIP().c_str(), tTmpNode.GetPort(), tTmpNode.GetNodeID()); m_setNodeID.insert(tTmpNode.GetNodeID()); vecNodeInfoList.push_back(tTmpNode); } if (m_pMembershipChangeCallback != nullptr) { m_pMembershipChangeCallback(m_iMyGroupIdx, vecNodeInfoList); } }
int MasterStateMachine :: LearnMaster( const uint64_t llInstanceID, const MasterOperator & oMasterOper, const uint64_t llAbsMasterTimeout) { std::lock_guard<std::mutex> oLockGuard(m_oMutex); PLG1Debug("my last version %lu other last version %lu this version %lu instanceid %lu", m_llMasterVersion, oMasterOper.lastversion(), oMasterOper.version(), llInstanceID); if (oMasterOper.lastversion() != 0 && llInstanceID > m_llMasterVersion && oMasterOper.lastversion() != m_llMasterVersion) { BP->GetMasterBP()->MasterSMInconsistent(); PLG1Err("other last version %lu not same to my last version %lu, instanceid %lu", oMasterOper.lastversion(), m_llMasterVersion, llInstanceID); PLG1Err("try to fix, set my master version %lu as other last version %lu, instanceid %lu", m_llMasterVersion, oMasterOper.lastversion(), llInstanceID); m_llMasterVersion = oMasterOper.lastversion(); } if (oMasterOper.version() != m_llMasterVersion) { PLG1Debug("version conflit, op version %lu now master version %lu", oMasterOper.version(), m_llMasterVersion); return 0; } int ret = UpdateMasterToStore(oMasterOper.nodeid(), llInstanceID, oMasterOper.timeout()); if (ret != 0) { PLG1Err("UpdateMasterToStore fail, ret %d", ret); return -1; } bool bMasterChange = false; if (m_iMasterNodeID != oMasterOper.nodeid()) { bMasterChange = true; } m_iMasterNodeID = oMasterOper.nodeid(); if (m_iMasterNodeID == m_iMyNodeID) { //self be master //use local abstimeout m_llAbsExpireTime = llAbsMasterTimeout; BP->GetMasterBP()->SuccessBeMaster(); PLG1Head("Be master success, absexpiretime %lu", m_llAbsExpireTime); } else { //other be master //use new start timeout m_llAbsExpireTime = Time::GetSteadyClockMS() + oMasterOper.timeout(); BP->GetMasterBP()->OtherBeMaster(); PLG1Head("Ohter be master, absexpiretime %lu", m_llAbsExpireTime); } m_iLeaseTime = oMasterOper.timeout(); m_llMasterVersion = llInstanceID; if (bMasterChange) { if (m_pMasterChangeCallback != nullptr) { m_pMasterChangeCallback(m_iMyGroupIdx, NodeInfo(m_iMasterNodeID), m_llMasterVersion); } } PLG1Imp("OK, masternodeid %lu version %lu abstimeout %lu", m_iMasterNodeID, m_llMasterVersion, m_llAbsExpireTime); return 0; }