This commit is contained in:
aozhiwei 2020-08-18 21:30:20 +08:00
parent 5706ff6992
commit d8eabad407
2 changed files with 97 additions and 59 deletions

View File

@ -27,6 +27,9 @@ void GSMgr::UnInit()
void GSMgr::_SS_WSP_RequestTargetServer(f8::MsgHdr& hdr, const ss::SS_WSP_RequestTargetServer& msg) void GSMgr::_SS_WSP_RequestTargetServer(f8::MsgHdr& hdr, const ss::SS_WSP_RequestTargetServer& msg)
{ {
int channel = f8::ExtractChannelIdFromAccountId(msg.account_id()); int channel = f8::ExtractChannelIdFromAccountId(msg.account_id());
if (!App::Instance()->IsSeparateChannel(channel)) {
channel = 0;
}
ss::SS_MS_ResponseTargetServer respmsg; ss::SS_MS_ResponseTargetServer respmsg;
respmsg.set_context_id(msg.context_id()); respmsg.set_context_id(msg.context_id());
if (msg.is_reconnect()) { if (msg.is_reconnect()) {
@ -82,11 +85,16 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
if (itr->second.online_num != online_num || if (itr->second.online_num != online_num ||
itr->second.room_num != room_num || itr->second.room_num != room_num ||
itr->second.alive_count != alive_count || itr->second.alive_count != alive_count ||
itr->second.servicing != servicing itr->second.servicing != servicing ||
itr->second.channel != channel
) { ) {
itr->second.online_num = online_num; itr->second.online_num = online_num;
itr->second.room_num = room_num; itr->second.room_num = room_num;
itr->second.servicing = servicing; itr->second.servicing = servicing;
if (itr->second.channel != channel) {
itr->second.channel = channel;
OnChannelChange(&itr->second);
}
RearrangeNode(); RearrangeNode();
} }
itr->second.alive_count = alive_count; itr->second.alive_count = alive_count;
@ -107,7 +115,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
gs.channel = channel; gs.channel = channel;
gs.last_active_tick = a8::XGetTickCount(); gs.last_active_tick = a8::XGetTickCount();
node_key_hash_[key] = gs; node_key_hash_[key] = gs;
node_sorted_list_.push_back(&node_key_hash_[key]); AddNodeToSortedNodes(&node_key_hash_[key]);
RearrangeNode(); RearrangeNode();
} }
@ -130,6 +138,7 @@ void GSMgr::___GSList(f8::JsonHttpRequest* request)
node->SetVal("ip", pair.second.ip); node->SetVal("ip", pair.second.ip);
node->SetVal("port", pair.second.port); node->SetVal("port", pair.second.port);
node->SetVal("servicing", pair.second.servicing); node->SetVal("servicing", pair.second.servicing);
node->SetVal("channel", pair.second.channel);
node_list->Push(*node); node_list->Push(*node);
delete node; delete node;
} }
@ -142,17 +151,20 @@ void GSMgr::___GSList(f8::JsonHttpRequest* request)
{ {
a8::MutableXObject* node_list = a8::MutableXObject::NewArray(); a8::MutableXObject* node_list = a8::MutableXObject::NewArray();
for (GSNode* gs_node : node_sorted_list_) { for (auto& pair : sorted_node_hash_) {
a8::MutableXObject* node = a8::MutableXObject::NewObject(); for (GSNode* gs_node : pair.second) {
node->SetVal("node_id", gs_node->node_id); a8::MutableXObject* node = a8::MutableXObject::NewObject();
node->SetVal("instance_id", gs_node->instance_id); node->SetVal("node_id", gs_node->node_id);
node->SetVal("room_num", gs_node->room_num); node->SetVal("instance_id", gs_node->instance_id);
node->SetVal("online_num", gs_node->online_num); node->SetVal("room_num", gs_node->room_num);
node->SetVal("ip", gs_node->ip); node->SetVal("online_num", gs_node->online_num);
node->SetVal("port", gs_node->port); node->SetVal("ip", gs_node->ip);
node->SetVal("servicing", gs_node->servicing); node->SetVal("port", gs_node->port);
node_list->Push(*node); node->SetVal("servicing", gs_node->servicing);
delete node; node->SetVal("channel", gs_node->channel);
node_list->Push(*node);
delete node;
}
} }
request->resp_xobj->SetVal("errcode", 0); request->resp_xobj->SetVal("errcode", 0);
@ -176,20 +188,8 @@ GSNode* GSMgr::GetNodeByNodeKey(const std::string& node_key)
GSNode* GSMgr::AllocNode(int channel) GSNode* GSMgr::AllocNode(int channel)
{ {
std::vector<GSNode*>* sorted_nodes = &node_sorted_list_; std::vector<GSNode*>* sorted_nodes = GetSortedNodesByChannel(channel);
std::vector<GSNode*> spec_nodes; if (sorted_nodes && !sorted_nodes->empty()) {
if (App::Instance()->IsSeparateChannel(channel)) {
for (GSNode* node : node_sorted_list_) {
if (spec_nodes.size() >= 2) {
break;
}
if (node->channel == channel) {
spec_nodes.push_back(node);
}
}
sorted_nodes = &spec_nodes;
}
if (!sorted_nodes->empty()) {
size_t rnd = std::min((size_t)2, sorted_nodes->size()); size_t rnd = std::min((size_t)2, sorted_nodes->size());
int idx = rand() % rnd; int idx = rand() % rnd;
while (idx >= 0) { while (idx >= 0) {
@ -200,9 +200,9 @@ GSNode* GSMgr::AllocNode(int channel)
} }
} }
a8::UdpLog::Instance()->Warning a8::UdpLog::Instance()->Warning
("节点分配失败 node_sorted_list.size:%d node_list.size:%d sorted_node.size:%d channel:%d", ("节点分配失败 sorted_node_hashlist.size:%d node_list.size:%d sorted_node.size:%d channel:%d",
{ {
node_sorted_list_.size(), sorted_node_hash_.size(),
node_key_hash_.size(), node_key_hash_.size(),
sorted_nodes->size(), sorted_nodes->size(),
channel channel
@ -212,33 +212,35 @@ GSNode* GSMgr::AllocNode(int channel)
void GSMgr::RearrangeNode() void GSMgr::RearrangeNode()
{ {
std::sort(node_sorted_list_.begin(), node_sorted_list_.end(), for (auto& pair : sorted_node_hash_) {
[] (const GSNode* a, const GSNode* b) std::sort(pair.second.begin(), pair.second.end(),
{ [] (const GSNode* a, const GSNode* b)
if (a->servicing && b->servicing) { {
if (a->online_num < b->online_num) { if (a->servicing && b->servicing) {
if (a->online_num < b->online_num) {
return true;
}
if (a->online_num > b->online_num) {
return false;
}
if (a->room_num < b->room_num) {
return true;
}
if (a->room_num > b->room_num) {
return false;
}
return a->node_idx > b->node_idx;
}
if (a->servicing) {
return true; return true;
} }
if (a->online_num > b->online_num) { if (b->servicing) {
return false;
}
if (a->room_num < b->room_num) {
return true;
}
if (a->room_num > b->room_num) {
return false; return false;
} }
return a->node_idx > b->node_idx; return a->node_idx > b->node_idx;
} }
if (a->servicing) { );
return true; }
}
if (b->servicing) {
return false;
}
return a->node_idx > b->node_idx;
}
);
} }
void GSMgr::ClearTimeOutNode() void GSMgr::ClearTimeOutNode()
@ -250,14 +252,7 @@ void GSMgr::ClearTimeOutNode()
} }
} }
for (GSNode* node : time_out_nodes) { for (GSNode* node : time_out_nodes) {
{ RemoveNodeRomSortedNodes(node);
for (size_t i = 0; i < node_sorted_list_.size(); ++i) {
if (node_sorted_list_[i] == node) {
node_sorted_list_.erase(node_sorted_list_.begin() + i);
break;
}
}
}
{ {
std::vector<std::string> deleted_teams; std::vector<std::string> deleted_teams;
for (auto& pair : team_hash_) { for (auto& pair : team_hash_) {
@ -273,3 +268,42 @@ void GSMgr::ClearTimeOutNode()
} }
RearrangeNode(); RearrangeNode();
} }
void GSMgr::AddNodeToSortedNodes(GSNode* node)
{
auto itr = sorted_node_hash_.find(node->channel);
if (itr != sorted_node_hash_.end()) {
itr->second.push_back(node);
} else {
sorted_node_hash_[node->channel] = std::vector<GSNode*>({node});
}
}
void GSMgr::RemoveNodeRomSortedNodes(GSNode* node)
{
for (auto& pair : sorted_node_hash_) {
std::vector<GSNode*>& node_list = pair.second;
for (size_t i = 0; i < node_list.size(); ++i) {
if (node_list[i] == node) {
node_list.erase(node_list.begin() + i);
break;
}
}
}
}
std::vector<GSNode*>* GSMgr::GetSortedNodesByChannel(int channel)
{
auto itr = sorted_node_hash_.find(channel);
if (itr != sorted_node_hash_.end()) {
return &itr->second;
} else {
return nullptr;
}
}
void GSMgr::OnChannelChange(GSNode* node)
{
RemoveNodeRomSortedNodes(node);
AddNodeToSortedNodes(node);
}

View File

@ -44,10 +44,14 @@ class GSMgr : public a8::Singleton<GSMgr>
GSNode* AllocNode(int channel); GSNode* AllocNode(int channel);
void RearrangeNode(); void RearrangeNode();
void ClearTimeOutNode(); void ClearTimeOutNode();
void AddNodeToSortedNodes(GSNode* node);
void RemoveNodeRomSortedNodes(GSNode* node);
std::vector<GSNode*>* GetSortedNodesByChannel(int channel);
void OnChannelChange(GSNode* node);
private: private:
std::map<std::string, GSNode*> team_hash_; std::map<std::string, GSNode*> team_hash_;
std::map<std::string, GSNode> node_key_hash_; std::map<std::string, GSNode> node_key_hash_;
std::vector<GSNode*> node_sorted_list_; std::map<int, std::vector<GSNode*>> sorted_node_hash_;
}; };