예제 #1
0
void
CClientProxy1_0::resetHeartbeatTimer()
{
	// reset the alarm
	removeHeartbeatTimer();
	addHeartbeatTimer();
}
예제 #2
0
void RaftConsensus::becomeCandidate() {
    std::cout << "RaftState: become candidate" << std::endl;

    state_ = StateCandidate;
    votes_ = 0;  
    
    // 删除心跳定时器
    removeHeartbeatTimer();

    // 开启选举超时定时器
    resetElectionTimer();

    ++term_;    // term + 1
    ++votes_;   // 给自己投票

    if (votes_ >= majority_)
        becomeLeader();

    VoteRequest req;
    req.set_term(term_);
    req.set_candidateid(id_); 
    req.set_lastlogterm(0);  //TODO: set logger
    req.set_lastlogindex(0);
    for (auto& kv: peers_) {
        auto stub = rpc_stubs_->getStub(kv.first);
        if (stub) {
            stub->RequesetVoteAsync(
                    req, 
                    std::bind(&RaftConsensus::handleVoteResponse, this, term_, std::placeholders::_1, std::placeholders::_2),
                    1000
                    );
        }
    }
}
예제 #3
0
void
CClientProxy1_0::resetOptions()
{
	LOG((CLOG_DEBUG1 "send reset options to \"%s\"", getName().c_str()));
	CProtocolUtil::writef(getStream(), kMsgCResetOptions);

	// reset heart rate and death
	resetHeartbeatRate();
	removeHeartbeatTimer();
	addHeartbeatTimer();
}
예제 #4
0
void RaftConsensus::becomeFollower(uint64_t term, uint64_t leader) {
    std::cout << "RaftState: become follower" << std::endl;

    term_ = term;
    leaderid_ = leader;
    state_ = StateFollower;
    vote_for_ = 0;

    // 删除心跳超时器
    removeHeartbeatTimer();

    // 开启选举超时定时器
    resetElectionTimer();
}
예제 #5
0
void
CClientProxy1_0::removeHandlers()
{
	// uninstall event handlers
	EVENTQUEUE->removeHandler(IStream::getInputReadyEvent(),
							getStream()->getEventTarget());
	EVENTQUEUE->removeHandler(IStream::getOutputErrorEvent(),
							getStream()->getEventTarget());
	EVENTQUEUE->removeHandler(IStream::getInputShutdownEvent(),
							getStream()->getEventTarget());
	EVENTQUEUE->removeHandler(IStream::getOutputShutdownEvent(),
							getStream()->getEventTarget());
	EVENTQUEUE->removeHandler(CEvent::kTimer, this);

	// remove timer
	removeHeartbeatTimer();
}
예제 #6
0
void
CClientProxy1_0::removeHandlers()
{
	// uninstall event handlers
	m_events->removeHandler(m_events->forIStream().inputReady(),
							getStream()->getEventTarget());
	m_events->removeHandler(m_events->forIStream().outputError(),
							getStream()->getEventTarget());
	m_events->removeHandler(m_events->forIStream().inputShutdown(),
							getStream()->getEventTarget());
	m_events->removeHandler(m_events->forIStream().outputShutdown(),
							getStream()->getEventTarget());
	m_events->removeHandler(CEvent::kTimer, this);

	// remove timer
	removeHeartbeatTimer();
}
예제 #7
0
void
CClientProxy1_0::setOptions(const COptionsList& options)
{
	LOG((CLOG_DEBUG1 "send set options to \"%s\" size=%d", getName().c_str(), options.size()));
	CProtocolUtil::writef(getStream(), kMsgDSetOptions, &options);

	// check options
	for (UInt32 i = 0, n = (UInt32)options.size(); i < n; i += 2) {
		if (options[i] == kOptionHeartbeat) {
			double rate = 1.0e-3 * static_cast<double>(options[i + 1]);
			if (rate <= 0.0) {
				rate = -1.0;
			}
			setHeartbeatRate(rate, rate * kHeartBeatsUntilDeath);
			removeHeartbeatTimer();
			addHeartbeatTimer();
		}
	}
}
예제 #8
0
CClientProxy1_3::~CClientProxy1_3()
{
	// cannot do this in superclass or our override wouldn't get called
	removeHeartbeatTimer();
}