Skip to content

Commit e26770a

Browse files
center has something wrong
1 parent 2c7333b commit e26770a

File tree

206 files changed

+861
-18633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+861
-18633
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if(NOT TARGET nanodbc)
1414
endif()
1515

1616
#find- rapidjson
17-
find_package(RapidJson REQUIRED)
17+
find_package(RapidJSON REQUIRED)
1818

1919
#already put muduo in the project
2020

Src/Center/Center.cpp

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void center::read_config()
3131
static std::string config_file_path {"CenterConfig.txt"};
3232
static std::ifstream in {config_file_path, std::ios::in};
3333
Setting set {}; memset(&set, 0, sizeof(Setting));
34-
uint next_read_interval {0};
34+
uint next_read_interval {15};
3535

3636
auto read_data = [&]() {
3737
if(!in.is_open())
@@ -43,19 +43,19 @@ void center::read_config()
4343
if(line.size() < 2 || line.front() == '/')
4444
continue;
4545
else if(line.find("next_read") != std::string::npos)
46-
next_read_interval = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
46+
next_read_interval = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1,line.begin() + line.find_first_of(']')));
4747
else if(line.find("load_record") != std::string::npos)
48-
set.mir_load_record_interval_ = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
48+
set.mir_load_record_interval_ = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1,line.begin() + line.find_first_of(']')));
4949
else if(line.find("load_dblog") != std::string::npos)
50-
set.mir_dblog_interval_ = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
50+
set.mir_dblog_interval_ = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1, line.begin() + line.find_first_of(']')));
5151
else if(line.find("max_disbeat") != std::string::npos)
52-
set.mir_max_disbeat_time_ = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
52+
set.mir_max_disbeat_time_ = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1,line.begin() + line.find_first_of(']')));
5353
else if(line.find("login_cache") != std::string::npos)
54-
set.cli_login_cache_time_ = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
54+
set.cli_login_cache_time_ = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1, line.begin() + line.find_first_of(']')));
5555
else if(line.find("balance") != std::string::npos)
56-
set.load_balance_interval_ = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
56+
set.load_balance_interval_ = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1, line.begin() + line.find_first_of(']')));
5757
else if(line.find("clear_mirs") != std::string::npos)
58-
set.clear_mirs_data_time_ = std::stoi(std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
58+
set.clear_mirs_data_time_ = std::stoi(std::string(line.begin() + line.find_first_of('[') + 1, line.begin() + line.find_first_of(']')));
5959
}
6060
};
6161

@@ -76,8 +76,15 @@ void center::read_config()
7676
//交换主副配置指针
7777
swap(telemeter::setting_copy, telemeter::setting); //atomic operation
7878

79+
LOG_INFO << "read config, interval: " << next_read_interval << "s, current setting:";
80+
LOG_INFO << "load record interval: " << telemeter::setting->mir_load_record_interval_;
81+
LOG_INFO << "load database interval: " << telemeter::setting->mir_dblog_interval_;
82+
LOG_INFO << "load balance interval: " << telemeter::setting->load_balance_interval_;
83+
LOG_INFO << "max disbeat interval: " << telemeter::setting->mir_max_disbeat_time_;
84+
LOG_INFO << "clear mirs data time: " << telemeter::setting->clear_mirs_data_time_;
85+
LOG_INFO << "login cache time: " << telemeter::setting->cli_login_cache_time_;
7986
//挂起该进程
80-
sleep(next_read_interval * 1000);
87+
sleep(next_read_interval);
8188
}
8289
}
8390

@@ -92,6 +99,7 @@ void center::wait_cli_login()
9299

93100
//纠正树形结构中cli信息的任务, 将交给线程池异步执行
94101
auto correct_tree_structure = [&](uuid cli_id){
102+
LOG_INFO << "correct info in tree-like structure of client id: " << cli_id;
95103
for(auto& kvp : mirs_data_)
96104
{
97105
if(kvp.second.contains(cli_id))
@@ -144,6 +152,8 @@ void center::wait_cli_login()
144152

145153
//按照协议格式进行解析, 并且得到ip等其他信息
146154
memcpy(uidbuf.data(), packet.data(), protocal::kCLI_LOGIN_UID_);
155+
for(auto& ch :uidbuf)
156+
ch += '0';
147157
size_t uid = std::atol(uidbuf.data());
148158
memcpy(statebuf.data(), packet.data() + protocal::kCLI_LOGIN_UID_, protocal::kCLI_LOGIN_STATE_);
149159
size_t state = statebuf[0];
@@ -190,12 +200,17 @@ void center::wait_cli_login()
190200
//cookie为空或者没有找到时, 记录cli登录信息
191201
if(cached == false)
192202
{
203+
if(this->mirs_data_.size() == 0)
204+
memset(&available_mir, 0, sizeof(IP));
205+
else if(this->mirs_data_.size() == 1)
206+
available_mir = (*mirs_data_.begin()).first;
193207
//回复可用mir地址
194208
int send_num = sendto(sock_fd, &available_mir, sizeof(available_mir), 0, (sockaddr*)&addr_cli, sizeof(addr_cli));
195209
login_count++;
196210

197211
//数据库记录日志
198212
//dblog(cli_login, uid);
213+
LOG_INFO << "client login, uid: " << uid << ", reply mirs ip: " << available_mir.to_string();
199214
if(this->all_cli_.count(uid) == 0)
200215
this->all_cli_.insert(uid);
201216
else
@@ -220,8 +235,10 @@ void center::wait_cli_login()
220235
if(atom_mutex_ == false)
221236
{
222237
atom_mutex_ = true;
238+
uuid id = cookie_.front();
223239
cookie_.pop_front();
224240
atom_mutex_ = false;
241+
LOG_INFO << "erase login cache of client: " << id;
225242
}
226243
else
227244
continue;
@@ -231,6 +248,7 @@ void center::wait_cli_login()
231248
}
232249
else //锁被pop队头或者查询任务使用中
233250
{
251+
LOG_INFO << "cached client login, no reply for client : " << uid;
234252
continue;
235253
}
236254
}
@@ -314,6 +332,7 @@ void center::listen_mir_beat()
314332
int i = 3; //占位避免编译器报warning
315333
//dblog(MIR_DISCONECT);
316334
mirs_data_.erase(kvp.first);
335+
LOG_INFO << "mirror disconnect: " << const_cast<IP*>(&kvp.first)->to_string();
317336
}
318337
}
319338
}
@@ -324,16 +343,28 @@ void center::listen_mir_beat()
324343

325344
void center::clear_mirs_data()
326345
{
327-
//while(true)
346+
auto get_next_zeropoint = [](){
347+
time_t t = time(NULL);
348+
struct tm * tm= localtime(&t);
349+
tm->tm_mday += 1;
350+
tm->tm_hour = 0;
351+
tm->tm_min = 0;
352+
tm->tm_sec = 0;
353+
return mktime(tm);
354+
};
355+
356+
while(true)
328357
{
329-
//sleep(nowtime - nextzeropoint + setting->clear_mirsdata_time);
330-
//mirs_data_.clear();
331-
//all_cli_.clear();
358+
sleep(get_next_zeropoint() - time(NULL) + telemeter::setting->clear_mirs_data_time_);
359+
mirs_data_.clear();
360+
all_cli_.clear();
361+
LOG_INFO << "clear mirrors data successfully";
332362
}
333363
}
334364

335365
IP center::load_balance()
336366
{
367+
LOG_INFO << "load balance occurs";
337368
//暂时不优化
338369
for(auto& kvp : mirs_data_)
339370
{
@@ -379,7 +410,7 @@ void center::log_info(std::exception& e, const char* crush_file)
379410
log << "Mirrors & dispatched client:\n";
380411
for(auto& kvp : mirs_data_)
381412
{
382-
log << "ip: " << kvp.first
413+
log << "ip: " << const_cast<IP*>(&kvp.first)->to_string()
383414
<< " load state: " << tostring(kvp.second.get_load_level()) << '\n';
384415
log << "dispatched client: \n";
385416
for(auto uid : kvp.second.get_dispatched_cli())
@@ -398,8 +429,3 @@ void center::log_info(std::exception& e, const char* crush_file)
398429
}
399430
}
400431
}
401-
402-
403-
404-
405-

Src/Common/ServerBase.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ scu_time scu_time::toScutime(const muduo::Timestamp &timestamp){
4949
int seconds = allSeconds % 86400;
5050
size_t hour = seconds / 3600;
5151
size_t minute = (seconds - hour * 3600) / 60;
52+
if(hour >= 16)
53+
hour -= 16;
54+
else
55+
hour += 8;
5256
return scu_time(hour, minute);
5357
}
5458

0 commit comments

Comments
 (0)