From c2cc00574415c612e82f0955523d422f59594912 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 06 五月 2021 19:40:38 +0800
Subject: [PATCH] disable center console log.
---
box/center.cpp | 172 ++++++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 120 insertions(+), 52 deletions(-)
diff --git a/box/center.cpp b/box/center.cpp
index 0f547e9..c57d34d 100644
--- a/box/center.cpp
+++ b/box/center.cpp
@@ -18,6 +18,7 @@
#include "center.h"
#include "bh_util.h"
#include "defs.h"
+#include "log.h"
#include "shm.h"
#include <chrono>
#include <set>
@@ -37,9 +38,9 @@
{
public:
typedef std::string ProcId;
- typedef std::string Address;
+ typedef MQId Address;
typedef bhome_msg::ProcInfo ProcInfo;
- typedef std::function<void(Address const &)> Cleaner;
+ typedef std::function<void(Address const)> Cleaner;
private:
enum {
@@ -52,12 +53,15 @@
struct ProcState {
int64_t timestamp_ = 0;
uint32_t flag_ = 0; // reserved
+ void PutOffline(const int64_t offline_time)
+ {
+ timestamp_ = NowSec() - offline_time;
+ flag_ = kStateOffline;
+ }
void UpdateState(const int64_t now, const int64_t offline_time, const int64_t kill_time)
{
auto diff = now - timestamp_;
-#ifndef NDEBUG
- printf("state %p diff: %ld\n", this, diff);
-#endif
+ LOG_DEBUG() << "state " << this << " diff: " << diff;
if (diff < offline_time) {
flag_ = kStateNormal;
} else if (diff < kill_time) {
@@ -84,7 +88,7 @@
WeakNode weak_node_;
bool operator<(const TopicDest &a) const { return mq_ < a.mq_; }
};
- inline const std::string &SrcAddr(const BHMsgHead &head) { return head.route(0).mq_id(); }
+ inline MQId SrcAddr(const BHMsgHead &head) { return head.route(0).mq_id(); }
inline bool MatchAddr(std::set<Address> const &addrs, const Address &addr) { return addrs.find(addr) != addrs.end(); }
NodeCenter(const std::string &id, const Cleaner &cleaner, const int64_t offline_time, const int64_t kill_time) :
@@ -98,7 +102,22 @@
// center name, no relative to shm.
const std::string &id() const { return id_; }
+ void OnNodeInit(const int64_t msg)
+ {
+ MQId ssn = msg;
+ auto UpdateRegInfo = [&](Node &node) {
+ for (int i = 0; i < 10; ++i) {
+ node->addrs_.insert(ssn + i);
+ }
+ node->state_.timestamp_ = NowSec() - offline_time_;
+ node->state_.UpdateState(NowSec(), offline_time_, kill_time_);
+ };
+ Node node(new NodeInfo);
+ UpdateRegInfo(node);
+ nodes_[ssn] = node;
+ LOG_INFO() << "new node ssn (" << ssn << ") init";
+ }
MsgCommonReply Register(const BHMsgHead &head, MsgRegister &msg)
{
if (msg.proc().proc_id() != head.proc_id()) {
@@ -106,6 +125,9 @@
}
try {
+ MQId ssn = head.ssn_id();
+ // when node restart, ssn will change,
+ // and old node will be removed after timeout.
auto UpdateRegInfo = [&](Node &node) {
node->addrs_.insert(SrcAddr(head));
for (auto &addr : msg.addrs()) {
@@ -116,19 +138,27 @@
node->state_.UpdateState(NowSec(), offline_time_, kill_time_);
};
- auto pos = nodes_.find(head.proc_id());
- if (pos != nodes_.end()) { // new client
+ auto pos = nodes_.find(ssn);
+ if (pos != nodes_.end()) { // update
Node &node = pos->second;
- if (node->addrs_.find(SrcAddr(head)) == node->addrs_.end()) {
- // node restarted, release old mq.
- RemoveNode(node);
- node.reset(new NodeInfo);
- }
UpdateRegInfo(node);
} else {
Node node(new NodeInfo);
UpdateRegInfo(node);
- nodes_[node->proc_.proc_id()] = node;
+ nodes_[ssn] = node;
+ }
+ LOG_DEBUG() << "node (" << head.proc_id() << ") ssn (" << ssn << ")";
+
+ auto old = online_node_addr_map_.find(head.proc_id());
+ if (old != online_node_addr_map_.end()) { // old session
+ auto &old_ssn = old->second;
+ if (old_ssn != ssn) {
+ nodes_[old_ssn]->state_.PutOffline(offline_time_);
+ LOG_DEBUG() << "put node (" << nodes_[old_ssn]->proc_.proc_id() << ") ssn (" << old->second << ") offline";
+ old_ssn = ssn;
+ }
+ } else {
+ online_node_addr_map_.emplace(head.proc_id(), ssn);
}
return MakeReply(eSuccess);
} catch (...) {
@@ -140,7 +170,7 @@
Reply HandleMsg(const BHMsgHead &head, Func const &op)
{
try {
- auto pos = nodes_.find(head.proc_id());
+ auto pos = nodes_.find(head.ssn_id());
if (pos == nodes_.end()) {
return MakeReply<Reply>(eNotRegistered, "Node is not registered.");
} else {
@@ -166,16 +196,30 @@
return HandleMsg<MsgCommonReply, Func>(head, op);
}
+ MsgCommonReply Unregister(const BHMsgHead &head, MsgUnregister &msg)
+ {
+ return HandleMsg(
+ head, [&](Node node) -> MsgCommonReply {
+ NodeInfo &ni = *node;
+ ni.state_.PutOffline(offline_time_);
+ return MakeReply(eSuccess);
+ });
+ }
+
MsgCommonReply RegisterRPC(const BHMsgHead &head, MsgRegisterRPC &msg)
{
return HandleMsg(
head, [&](Node node) -> MsgCommonReply {
- auto &src = SrcAddr(head);
+ auto src = SrcAddr(head);
auto &topics = msg.topics().topic_list();
node->services_[src].insert(topics.begin(), topics.end());
TopicDest dest = {src, node};
for (auto &topic : topics) {
service_map_[topic].insert(dest);
+ }
+ LOG_DEBUG() << "node " << node->proc_.proc_id() << " ssn " << *node->addrs_.begin() << " serve " << topics.size() << " topics:\n";
+ for (auto &topic : topics) {
+ LOG_DEBUG() << "\t" << topic;
}
return MakeReply(eSuccess);
});
@@ -206,20 +250,17 @@
auto query = [&](Node self) -> MsgQueryTopicReply {
auto pos = service_map_.find(req.topic());
if (pos != service_map_.end() && !pos->second.empty()) {
- // now just find first one.
- const TopicDest &dest = *(pos->second.begin());
- Node dest_node(dest.weak_node_.lock());
- if (!dest_node) {
- service_map_.erase(pos);
- return MakeReply<Reply>(eOffline, "topic server offline.");
- } else if (!Valid(*dest_node)) {
- return MakeReply<Reply>(eNoRespond, "topic server not responding.");
- } else {
- MsgQueryTopicReply reply = MakeReply<Reply>(eSuccess);
- reply.mutable_address()->set_mq_id(dest.mq_);
- return reply;
+ auto &clients = pos->second;
+ Reply reply = MakeReply<Reply>(eSuccess);
+ for (auto &dest : clients) {
+ Node dest_node(dest.weak_node_.lock());
+ if (dest_node && Valid(*dest_node)) {
+ auto node_addr = reply.add_node_address();
+ node_addr->set_proc_id(dest_node->proc_.proc_id());
+ node_addr->mutable_addr()->set_mq_id(dest.mq_);
+ }
}
-
+ return reply;
} else {
return MakeReply<Reply>(eNotFound, "topic server not found.");
}
@@ -231,7 +272,7 @@
MsgCommonReply Subscribe(const BHMsgHead &head, const MsgSubscribe &msg)
{
return HandleMsg(head, [&](Node node) {
- auto &src = SrcAddr(head);
+ auto src = SrcAddr(head);
auto &topics = msg.topics().topic_list();
node->subscriptions_[src].insert(topics.begin(), topics.end());
TopicDest dest = {src, node};
@@ -244,7 +285,7 @@
MsgCommonReply Unsubscribe(const BHMsgHead &head, const MsgUnsubscribe &msg)
{
return HandleMsg(head, [&](Node node) {
- auto &src = SrcAddr(head);
+ auto src = SrcAddr(head);
auto pos = node->subscriptions_.find(src);
auto RemoveSubTopicDestRecord = [this](const Topic &topic, const TopicDest &dest) {
@@ -367,16 +408,26 @@
EraseMapRec(service_map_, node->services_);
EraseMapRec(subscribe_map_, node->subscriptions_);
+ // remove online record.
+ auto pos = online_node_addr_map_.find(node->proc_.proc_id());
+ if (pos != online_node_addr_map_.end()) {
+ if (node->addrs_.find(pos->second) != node->addrs_.end()) {
+ online_node_addr_map_.erase(pos);
+ }
+ }
+
for (auto &addr : node->addrs_) {
cleaner_(addr);
}
+
node->addrs_.clear();
}
std::string id_; // center proc id;
std::unordered_map<Topic, Clients> service_map_;
std::unordered_map<Topic, Clients> subscribe_map_;
- std::unordered_map<ProcId, Node> nodes_;
+ std::unordered_map<Address, Node> nodes_;
+ std::unordered_map<std::string, Address> online_node_addr_map_;
Cleaner cleaner_; // remove mqs.
int64_t offline_time_;
int64_t kill_time_;
@@ -411,16 +462,24 @@
msg, head, [&](auto &body) { return center->MsgTag(head, body); }, replyer); \
return true;
-bool AddCenter(const std::string &id, const NodeCenter::Cleaner &cleaner)
+auto MakeReplyer(ShmSocket &socket, BHMsgHead &head, const std::string &proc_id)
{
- auto center_ptr = std::make_shared<Synced<NodeCenter>>(id, cleaner, 60s, 60s * 2);
- auto MakeReplyer = [](ShmSocket &socket, BHMsgHead &head, const std::string &proc_id) {
- return [&](auto &&rep_body) {
- auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.msg_id()));
- auto &remote = head.route(0).mq_id();
- socket.Send(remote.data(), reply_head, rep_body);
- };
+ return [&](auto &&rep_body) {
+ auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.ssn_id(), head.msg_id()));
+ auto remote = head.route(0).mq_id();
+ socket.Send(remote, reply_head, rep_body);
};
+}
+
+bool AddCenter(std::shared_ptr<Synced<NodeCenter>> center_ptr)
+{
+ auto OnNodeInit = [center_ptr](ShmSocket &socket, MsgI &msg) {
+ auto ¢er = *center_ptr;
+ center->OnNodeInit(msg.Offset());
+ };
+ auto Nothing = [](ShmSocket &socket) {};
+
+ BHCenter::Install("#centetr.Init", OnNodeInit, Nothing, BHInitAddress(), 16);
auto OnCenterIdle = [center_ptr](ShmSocket &socket) {
auto ¢er = *center_ptr;
@@ -433,12 +492,14 @@
switch (head.type()) {
CASE_ON_MSG_TYPE(Register);
CASE_ON_MSG_TYPE(Heartbeat);
+ CASE_ON_MSG_TYPE(Unregister);
CASE_ON_MSG_TYPE(RegisterRPC);
CASE_ON_MSG_TYPE(QueryTopic);
default: return false;
}
};
+ BHCenter::Install("#center.main", OnCenter, OnCenterIdle, BHTopicCenterAddress(), 1000);
auto OnBusIdle = [=](ShmSocket &socket) {};
auto OnPubSub = [=](ShmSocket &socket, MsgI &msg, BHMsgHead &head) -> bool {
@@ -463,7 +524,7 @@
if (node) {
// should also make sure that mq is not killed before msg expires.
// it would be ok if (kill_time - offline_time) is longer than expire time.
- socket.Send(cli.mq_.data(), msg);
+ socket.Send(cli.mq_, msg);
++it;
} else {
it = clients.erase(it);
@@ -479,7 +540,6 @@
}
};
- BHCenter::Install("#center.reg", OnCenter, OnCenterIdle, BHTopicCenterAddress(), 1000);
BHCenter::Install("#center.bus", OnPubSub, OnBusIdle, BHTopicBusAddress(), 1000);
return true;
@@ -495,28 +555,32 @@
return rec;
}
-bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const std::string &mqid, const int mq_len)
+bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const MQId mqid, const int mq_len)
{
- Centers()[name] = CenterInfo{name, handler, idle, mqid, mq_len};
+ Centers()[name] = CenterInfo{name, handler, MsgIHandler(), idle, mqid, mq_len};
return true;
}
-bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const MQId &mqid, const int mq_len)
+bool BHCenter::Install(const std::string &name, MsgIHandler handler, IdleHandler idle, const MQId mqid, const int mq_len)
{
- return Install(name, handler, idle, std::string((const char *) &mqid, sizeof(mqid)), mq_len);
+ Centers()[name] = CenterInfo{name, MsgHandler(), handler, idle, mqid, mq_len};
+ return true;
}
BHCenter::BHCenter(Socket::Shm &shm)
{
- auto gc = [&](const std::string &id) {
- auto r = ShmSocket::Remove(shm, *(MQId *) id.data());
- printf("remove mq : %s\n", r ? "ok" : "failed");
+ auto gc = [&](const MQId id) {
+ auto r = ShmSocket::Remove(shm, id);
+ if (r) {
+ LOG_DEBUG() << "remove mq " << id << " ok\n";
+ }
};
- AddCenter("#bhome_center", gc);
+ auto center_ptr = std::make_shared<Synced<NodeCenter>>("#bhome_center", gc, 6s, 6s * 2);
+ AddCenter(center_ptr);
for (auto &kv : Centers()) {
auto &info = kv.second;
- sockets_[info.name_] = std::make_shared<ShmSocket>(shm, *(MQId *) info.mqid_.data(), info.mq_len_);
+ sockets_[info.name_] = std::make_shared<ShmSocket>(shm, info.mqid_, info.mq_len_);
}
}
@@ -524,7 +588,11 @@
{
for (auto &kv : Centers()) {
auto &info = kv.second;
- sockets_[info.name_]->Start(info.handler_, info.idle_);
+ if (info.handler_) {
+ sockets_[info.name_]->Start(info.handler_, info.idle_);
+ } else {
+ sockets_[info.name_]->Start(info.raw_handler_, info.idle_);
+ }
}
return true;
--
Gitblit v1.8.0