Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
int MasterStateMachine :: UpdateMasterToStore(const nodeid_t llMasterNodeID, const uint64_t llVersion, const uint32_t iLeaseTime)
{
    MasterVariables oVariables;
    oVariables.set_masternodeid(llMasterNodeID);
    oVariables.set_version(llVersion);
    oVariables.set_leasetime(iLeaseTime);

    WriteOptions oWriteOptions;
    oWriteOptions.bSync = false;
    
    return m_oMVStore.Write(oWriteOptions, m_iMyGroupIdx, oVariables);
}
Ejemplo n.º 3
0
int MasterStateMachine :: GetCheckpointBuffer(std::string & sCPBuffer)
{
    if (m_llMasterVersion == (uint64_t)-1)
    {
        return 0;
    }
    
    MasterVariables oVariables;
    oVariables.set_masternodeid(m_iMasterNodeID);
    oVariables.set_version(m_llMasterVersion);
    oVariables.set_leasetime(m_iLeaseTime);
    
    bool sSucc = oVariables.SerializeToString(&sCPBuffer);
    if (!sSucc)
    {
        PLG1Err("Variables.Serialize fail");
        return -1;
    }

    return 0;
}
Ejemplo n.º 4
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;
    }

    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::GetTimestampMS() + oVariables.leasetime();
    }

    return 0;
}
Ejemplo n.º 5
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);

    bool bMasterChange = false;
    m_llMasterVersion = oVariables.version();

    if (oVariables.masternodeid() == m_iMyNodeID)
    {
        m_iMasterNodeID = nullnode;
        m_llAbsExpireTime = 0;
    }
    else
    {
        if (m_iMasterNodeID != oVariables.masternodeid())
        {
            bMasterChange = true;
        }
        m_iMasterNodeID = oVariables.masternodeid();
        m_llAbsExpireTime = Time::GetSteadyClockMS() + oVariables.leasetime();
    }

    if (bMasterChange)
    {
        if (m_pMasterChangeCallback != nullptr)
        {
            m_pMasterChangeCallback(m_iMyGroupIdx, NodeInfo(m_iMasterNodeID), m_llMasterVersion);
        }
    }

    return 0;
}