分服逻辑ok

This commit is contained in:
aozhiwei 2020-08-14 21:39:56 +08:00
parent 668074da06
commit 5706ff6992
5 changed files with 94 additions and 17 deletions

View File

@ -96,6 +96,7 @@ void App::Init(int argc, char* argv[])
f8::MsgQueue::Instance()->Init();
f8::TGLog::Instance()->Init(a8::Format(PROJ_NAME_FMT, {GAME_ID}), false);
JsonDataMgr::Instance()->Init();
LoadSeparateChannelConfig();
uuid.SetMachineId((node_id - 1) * MAX_NODE_ID + instance_id);
GGListener::Instance()->Init();
GSMgr::Instance()->Init();
@ -113,6 +114,14 @@ void App::Init(int argc, char* argv[])
SavePerfLog();
});
}
{
a8::Timer::Instance()->AddRepeatTimer(1000 * 60 * 5,
a8::XParams(),
[] (const a8::XParams& param)
{
App::Instance()->LoadSeparateChannelConfig();
});
}
}
void App::UnInit()
@ -465,3 +474,36 @@ a8::XParams* App::GetContext(long long context_id)
auto itr = context_hash_.find(context_id);
return itr != context_hash_.end() ? &(itr->second) : nullptr;
}
bool App::IsSeparateChannel(int channel)
{
return separate_channel_hash_.find(channel) != separate_channel_hash_.end();
}
void App::LoadSeparateChannelConfig()
{
std::string data;
a8::ReadStringFromFile("../config/separate_channels.csv", data);
std::vector<std::string> strings;
a8::Split(data, strings, ',');
separate_channel_hash_.clear();
for (std::string& str : strings) {
int channel = a8::XValue(str);
if (channel != 0) {
separate_channel_hash_.insert(channel);
}
}
a8::UdpLog::Instance()->Info("LoadSeparateChannelConfig channelids:",
{
GetSeparateChannelConfigData()
});
}
std::string App::GetSeparateChannelConfigData()
{
std::string data;
for (int channel : separate_channel_hash_) {
data += a8::XValue(channel).GetString() + ",";
}
return data;
}

View File

@ -32,6 +32,9 @@ public:
a8::XParams* AddContext(long long context_id);
void DelContext(long long context_id);
a8::XParams* GetContext(long long context_id);
bool IsSeparateChannel(int channel);
void LoadSeparateChannelConfig();
std::string GetSeparateChannelConfigData();
private:
void QuickExecute(int delta_time);
@ -78,6 +81,7 @@ private:
IMMsgNode* im_bot_node_ = nullptr;
IMMsgNode* im_work_node_ = nullptr;
std::set<int> separate_channel_hash_;
std::map<long long, a8::XParams> context_hash_;
public:

View File

@ -7,6 +7,8 @@
#include "app.h"
#include "GGListener.h"
#include "framework/cpp/utils.h"
void GSMgr::Init()
{
a8::Timer::Instance()->AddRepeatTimer(1000 * 2,
@ -24,6 +26,7 @@ void GSMgr::UnInit()
void GSMgr::_SS_WSP_RequestTargetServer(f8::MsgHdr& hdr, const ss::SS_WSP_RequestTargetServer& msg)
{
int channel = f8::ExtractChannelIdFromAccountId(msg.account_id());
ss::SS_MS_ResponseTargetServer respmsg;
respmsg.set_context_id(msg.context_id());
if (msg.is_reconnect()) {
@ -41,7 +44,7 @@ void GSMgr::_SS_WSP_RequestTargetServer(f8::MsgHdr& hdr, const ss::SS_WSP_Reques
respmsg.set_host(node->ip);
respmsg.set_port(node->port);
} else {
node = AllocNode();
node = AllocNode(channel);
if (node) {
respmsg.set_host(node->ip);
respmsg.set_port(node->port);
@ -70,6 +73,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
int room_num = request->request.Get("room_num");
int instance_id = request->request.Get("instance_id");
int node_id = request->request.Get("node_id");
int channel = request->request.Get("channel");
bool servicing = request->request.Get("servicing");
std::string key = ip + ":" + a8::XValue(port).GetString();
@ -86,6 +90,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
RearrangeNode();
}
itr->second.alive_count = alive_count;
itr->second.channel = channel;
itr->second.last_active_tick = a8::XGetTickCount();
} else {
GSNode gs;
@ -99,6 +104,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
gs.ip = ip;
gs.port = port;
gs.servicing = servicing;
gs.channel = channel;
gs.last_active_tick = a8::XGetTickCount();
node_key_hash_[key] = gs;
node_sorted_list_.push_back(&node_key_hash_[key]);
@ -168,24 +174,39 @@ GSNode* GSMgr::GetNodeByNodeKey(const std::string& node_key)
return itr != node_key_hash_.end() ? &itr->second : nullptr;
}
GSNode* GSMgr::AllocNode()
GSNode* GSMgr::AllocNode(int channel)
{
if (node_sorted_list_.empty()) {
return nullptr;
}
size_t rnd = std::min((size_t)2, node_sorted_list_.size());
int idx = rand() % rnd;
while (idx >= 0) {
if (node_sorted_list_[idx]->servicing) {
return node_sorted_list_[idx];
std::vector<GSNode*>* sorted_nodes = &node_sorted_list_;
std::vector<GSNode*> spec_nodes;
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);
}
}
--idx;
sorted_nodes = &spec_nodes;
}
a8::UdpLog::Instance()->Warning("节点分配失败 node_sorted_list.size:%d node_list.size:%d",
{
node_sorted_list_.size(),
node_key_hash_.size()
});
if (!sorted_nodes->empty()) {
size_t rnd = std::min((size_t)2, sorted_nodes->size());
int idx = rand() % rnd;
while (idx >= 0) {
if (sorted_nodes->at(idx)->servicing) {
return sorted_nodes->at(idx);
}
--idx;
}
}
a8::UdpLog::Instance()->Warning
("节点分配失败 node_sorted_list.size:%d node_list.size:%d sorted_node.size:%d channel:%d",
{
node_sorted_list_.size(),
node_key_hash_.size(),
sorted_nodes->size(),
channel
});
return nullptr;
}

View File

@ -14,6 +14,7 @@ struct GSNode
std::string ip;
int port = 0;
bool servicing = false;
int channel = 0;
long long last_active_tick = 0;
};
@ -40,7 +41,7 @@ class GSMgr : public a8::Singleton<GSMgr>
private:
GSNode* GetNodeByTeamId(const std::string& team_id);
GSNode* GetNodeByNodeKey(const std::string& node_key);
GSNode* AllocNode();
GSNode* AllocNode(int channel);
void RearrangeNode();
void ClearTimeOutNode();

View File

@ -19,16 +19,25 @@ static void _GMOpsSelfChecking(f8::JsonHttpRequest* request)
static void _GMOpsReload(f8::JsonHttpRequest* request)
{
App::Instance()->LoadSeparateChannelConfig();
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
a8::UdpLog::Instance()->Warning("reload config files", {});
}
static void _GMOpsShowConfig(f8::JsonHttpRequest* request)
{
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
request->resp_xobj->SetVal("separate_channels", App::Instance()->GetSeparateChannelConfigData());
}
void HandlerMgr::Init()
{
RegisterNetMsgHandlers();
RegisterGMMsgHandler("Ops@selfChecking", _GMOpsSelfChecking);
RegisterGMMsgHandler("Ops@reload", _GMOpsReload);
RegisterGMMsgHandler("Ops@showConfig", _GMOpsShowConfig);
RegisterGMMsgHandler("GS@report", [] (f8::JsonHttpRequest* request)
{
GSMgr::Instance()->___GSReport(request);