From 65ef4d68321e56906920be75831b5e968f7abd7b Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期二, 13 四月 2021 09:34:05 +0800 Subject: [PATCH] add heartbeat; refactor. --- src/topic_node.cpp | 91 ++++++++++++++++++++++----------------------- 1 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/topic_node.cpp b/src/topic_node.cpp index 5afec3f..788c536 100644 --- a/src/topic_node.cpp +++ b/src/topic_node.cpp @@ -17,6 +17,7 @@ */ #include "topic_node.h" #include "bh_util.h" +#include "failed_msg.h" #include <chrono> #include <list> @@ -32,60 +33,27 @@ std::string msg_id; }; -class ServerFailedQ -{ - struct FailedMsg { - steady_clock::time_point xpr; - std::string remote_; - BHMsgHead head_; - MsgRequestTopicReply body_; - FailedMsg(const std::string &addr, BHMsgHead &&head, MsgRequestTopicReply &&body) : - xpr(steady_clock::now() + 10s), remote_(addr), head_(std::move(head)), body_(std::move(body)) {} - bool Expired() { return steady_clock::now() > xpr; } - }; - typedef std::list<FailedMsg> Queue; - Synced<Queue> queue_; - -public: - void Push(const std::string &remote, BHMsgHead &&head, MsgRequestTopicReply &&body) - { - queue_->emplace_back(remote, std::move(head), std::move(body)); - } - void TrySend(ShmSocket &socket, const int timeout_ms = 0) - { - queue_.Apply([&](Queue &q) { - if (!q.empty()) { - auto it = q.begin(); - do { - if (it->Expired()) { - // it->msg_.Release(socket.shm()); - it = q.erase(it); - } else if (socket.Send(it->remote_.data(), it->head_, it->body_, timeout_ms)) { - it = q.erase(it); - } else { - ++it; - } - } while (it != q.end()); - } - }); - } -}; +typedef FailedMsgQ ServerFailedQ; } // namespace TopicNode::TopicNode(SharedMemory &shm) : shm_(shm), sock_node_(shm), sock_request_(shm), sock_reply_(shm), sock_sub_(shm) { - SockNode().Start(); - SockClient().Start(); - SockServer().Start(); + Start(); } TopicNode::~TopicNode() { - StopAll(); + Stop(); } -void TopicNode::StopAll() +void TopicNode::Start() +{ + SockNode().Start(); + SockClient().Start(); + SockServer().Start(); +} +void TopicNode::Stop() { SockServer().Stop(); SockClient().Stop(); @@ -112,10 +80,37 @@ BHMsgHead reply_head; bool r = sock.SendAndRecv(&BHTopicCenterAddress(), head, body, reply, reply_head, timeout_ms); r = r && reply_head.type() == kMsgTypeCommonReply && reply.ParseBody(reply_body); - if (r) { + if (r && IsSuccess(reply_body.errmsg().errcode())) { info_ = body; } return r; +} + +bool TopicNode::Heartbeat(ProcInfo &proc, MsgCommonReply &reply_body, const int timeout_ms) +{ + auto &sock = SockNode(); + MsgHeartbeat body; + *body.mutable_proc() = proc; + + auto head(InitMsgHead(GetType(body), body.proc().proc_id())); + AddRoute(head, sock.id()); + + MsgI reply; + DEFER1(reply.Release(shm_);); + BHMsgHead reply_head; + bool r = sock.SendAndRecv(&BHTopicCenterAddress(), head, body, reply, reply_head, timeout_ms); + r = r && reply_head.type() == kMsgTypeCommonReply && reply.ParseBody(reply_body); + if (r && IsSuccess(reply_body.errmsg().errcode())) { + // TODO update proc info + } + return r; +} +bool TopicNode::Heartbeat(const int timeout_ms) +{ + ProcInfo proc; + proc.set_proc_id(proc_id()); + MsgCommonReply reply_body; + return Heartbeat(proc, reply_body, timeout_ms) && IsSuccess(reply_body.errmsg().errcode()); } bool TopicNode::ServerRegisterRPC(MsgTopicList &topics, MsgCommonReply &reply_body, const int timeout_ms) @@ -158,8 +153,12 @@ for (int i = 0; i < head.route_size() - 1; ++i) { reply_head.add_route()->Swap(head.mutable_route(i)); } - if (!sock.Send(head.route().rbegin()->mq_id().data(), reply_head, reply_body, 10)) { - failed_q->Push(head.route().rbegin()->mq_id(), std::move(reply_head), std::move(reply_body)); + MsgI msg; + if (msg.Make(sock.shm(), reply_head, reply_body)) { + auto &remote = head.route().rbegin()->mq_id(); + if (!sock.Send(remote.data(), msg, 10)) { + failed_q->Push(remote, msg, 10s); + } } } } -- Gitblit v1.8.0