void RaftConsensus::becomeLeader() { std::cout << "RaftState: become Leader" << std::endl; state_ = StateLeader; leaderid_ = id_; // 删除选举定时器 removeElectionTimer(); // 开启心跳超时器 resetHeartbeatTimer(); // 给其他节点发送心跳,建立Leader权威 AppendEntriesRequest req; req.set_term(term_); req.set_leaderid(id_); req.set_prevlogterm(0); // TODO: req.set_prevlogindex(0); req.set_leadercommit(0); for (auto& kv: peers_) { auto stub = rpc_stubs_->getStub(kv.first); if (stub) { stub->AppendEntriesAsync( req, std::function<void(const AppendEntriesResponse&, const grpc::Status&)>(), // TODO:无视返回值? 1000 ); } } }
void CClientProxy1_0::handleData(const CEvent&, void*) { // handle messages until there are no more. first read message code. UInt8 code[4]; UInt32 n = getStream()->read(code, 4); while (n != 0) { // verify we got an entire code if (n != 4) { LOG((CLOG_ERR "incomplete message from \"%s\": %d bytes", getName().c_str(), n)); disconnect(); return; } // parse message LOG((CLOG_DEBUG2 "msg from \"%s\": %c%c%c%c", getName().c_str(), code[0], code[1], code[2], code[3])); if (!(this->*m_parser)(code)) { LOG((CLOG_ERR "invalid message from client \"%s\": %c%c%c%c", getName().c_str(), code[0], code[1], code[2], code[3])); disconnect(); return; } // next message n = getStream()->read(code, 4); } // restart heartbeat timer resetHeartbeatTimer(); }
void RaftConsensus::onHeartbeatTimeout() { std::lock_guard<std::mutex> lock(mu_); std::cout << "Heartbeat timeout..." << std::endl; if (StateLeader != state_) { return; } // 发送心跳 AppendEntriesRequest req; req.set_term(term_); req.set_leaderid(id_); req.set_prevlogterm(0); // TODO: req.set_prevlogindex(0); req.set_leadercommit(0); for (auto& kv: peers_) { auto stub = rpc_stubs_->getStub(kv.first); if (stub) { stub->AppendEntriesAsync( req, std::function<void(const AppendEntriesResponse&, const grpc::Status&)>(), // TODO:无视返回值? 1000 ); } } resetHeartbeatTimer(); }
void StatusSubscribe::fsmUpHeartbeatTickEvent() { if (m_state == State::Up) { #ifdef QT_DEBUG DEBUG_TAG(1, m_debugName, "Event HEARTBEAT TICK"); #endif // execute actions resetHeartbeatTimer(); } }
void StatusSubscribe::fsmUpAnyMsgReceivedEvent() { if (m_state == State::Up) { #ifdef QT_DEBUG DEBUG_TAG(1, m_debugName, "Event ANY MSG RECEIVED"); #endif // execute actions resetHeartbeatLiveness(); resetHeartbeatTimer(); } }
bool CClientProxy1_3::parseMessage(const UInt8* code) { // process message if (memcmp(code, kMsgCKeepAlive, 4) == 0) { // reset alarm resetHeartbeatTimer(); return true; } else { return CClientProxy1_2::parseMessage(code); } }
void Client::setSocket(QTcpSocket *socket) { this->socket = socket; this->socket->setParent(this); socketIn.setDevice(socket); socketOut.setDevice(socket); connect(socket, SIGNAL(disconnected()), SLOT(onSocketDisconnected())); connect(socket, SIGNAL(disconnected()), SIGNAL(socketDisconnected())); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError))); connect(socket, SIGNAL(readyRead()), SLOT(onSocketReadyRead())); initiateHandshake(); resetHeartbeatTimer(); }
void StatusSubscribe::startHeartbeatTimer() { resetHeartbeatTimer(); }
void HalrcompSubscribe::startHeartbeatTimer() { resetHeartbeatTimer(); }