@@ -31,7 +31,7 @@ void center::read_config()
31
31
static std::string config_file_path {" CenterConfig.txt" };
32
32
static std::ifstream in {config_file_path, std::ios::in};
33
33
Setting set {}; memset (&set, 0 , sizeof (Setting));
34
- uint next_read_interval {0 };
34
+ uint next_read_interval {15 };
35
35
36
36
auto read_data = [&]() {
37
37
if (!in.is_open ())
@@ -43,19 +43,19 @@ void center::read_config()
43
43
if (line.size () < 2 || line.front () == ' /' )
44
44
continue ;
45
45
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 (' ]' )));
47
47
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 (' ]' )));
49
49
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 (' ]' )));
51
51
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 (' ]' )));
53
53
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 (' ]' )));
55
55
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 (' ]' )));
57
57
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 (' ]' )));
59
59
}
60
60
};
61
61
@@ -76,8 +76,15 @@ void center::read_config()
76
76
// 交换主副配置指针
77
77
swap (telemeter::setting_copy, telemeter::setting); // atomic operation
78
78
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_ ;
79
86
// 挂起该进程
80
- sleep (next_read_interval * 1000 );
87
+ sleep (next_read_interval);
81
88
}
82
89
}
83
90
@@ -92,6 +99,7 @@ void center::wait_cli_login()
92
99
93
100
// 纠正树形结构中cli信息的任务, 将交给线程池异步执行
94
101
auto correct_tree_structure = [&](uuid cli_id){
102
+ LOG_INFO << " correct info in tree-like structure of client id: " << cli_id;
95
103
for (auto & kvp : mirs_data_)
96
104
{
97
105
if (kvp.second .contains (cli_id))
@@ -144,6 +152,8 @@ void center::wait_cli_login()
144
152
145
153
// 按照协议格式进行解析, 并且得到ip等其他信息
146
154
memcpy (uidbuf.data (), packet.data (), protocal::kCLI_LOGIN_UID_ );
155
+ for (auto & ch :uidbuf)
156
+ ch += ' 0' ;
147
157
size_t uid = std::atol (uidbuf.data ());
148
158
memcpy (statebuf.data (), packet.data () + protocal::kCLI_LOGIN_UID_ , protocal::kCLI_LOGIN_STATE_ );
149
159
size_t state = statebuf[0 ];
@@ -190,12 +200,17 @@ void center::wait_cli_login()
190
200
// cookie为空或者没有找到时, 记录cli登录信息
191
201
if (cached == false )
192
202
{
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 ;
193
207
// 回复可用mir地址
194
208
int send_num = sendto (sock_fd, &available_mir, sizeof (available_mir), 0 , (sockaddr*)&addr_cli, sizeof (addr_cli));
195
209
login_count++;
196
210
197
211
// 数据库记录日志
198
212
// dblog(cli_login, uid);
213
+ LOG_INFO << " client login, uid: " << uid << " , reply mirs ip: " << available_mir.to_string ();
199
214
if (this ->all_cli_ .count (uid) == 0 )
200
215
this ->all_cli_ .insert (uid);
201
216
else
@@ -220,8 +235,10 @@ void center::wait_cli_login()
220
235
if (atom_mutex_ == false )
221
236
{
222
237
atom_mutex_ = true ;
238
+ uuid id = cookie_.front ();
223
239
cookie_.pop_front ();
224
240
atom_mutex_ = false ;
241
+ LOG_INFO << " erase login cache of client: " << id;
225
242
}
226
243
else
227
244
continue ;
@@ -231,6 +248,7 @@ void center::wait_cli_login()
231
248
}
232
249
else // 锁被pop队头或者查询任务使用中
233
250
{
251
+ LOG_INFO << " cached client login, no reply for client : " << uid;
234
252
continue ;
235
253
}
236
254
}
@@ -314,6 +332,7 @@ void center::listen_mir_beat()
314
332
int i = 3 ; // 占位避免编译器报warning
315
333
// dblog(MIR_DISCONECT);
316
334
mirs_data_.erase (kvp.first );
335
+ LOG_INFO << " mirror disconnect: " << const_cast <IP*>(&kvp.first )->to_string ();
317
336
}
318
337
}
319
338
}
@@ -324,16 +343,28 @@ void center::listen_mir_beat()
324
343
325
344
void center::clear_mirs_data ()
326
345
{
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 )
328
357
{
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" ;
332
362
}
333
363
}
334
364
335
365
IP center::load_balance ()
336
366
{
367
+ LOG_INFO << " load balance occurs" ;
337
368
// 暂时不优化
338
369
for (auto & kvp : mirs_data_)
339
370
{
@@ -379,7 +410,7 @@ void center::log_info(std::exception& e, const char* crush_file)
379
410
log << " Mirrors & dispatched client:\n " ;
380
411
for (auto & kvp : mirs_data_)
381
412
{
382
- log << " ip: " << kvp.first
413
+ log << " ip: " << const_cast <IP*>(& kvp.first )-> to_string ()
383
414
<< " load state: " << tostring (kvp.second .get_load_level ()) << ' \n ' ;
384
415
log << " dispatched client: \n " ;
385
416
for (auto uid : kvp.second .get_dispatched_cli ())
@@ -398,8 +429,3 @@ void center::log_info(std::exception& e, const char* crush_file)
398
429
}
399
430
}
400
431
}
401
-
402
-
403
-
404
-
405
-
0 commit comments