-
Notifications
You must be signed in to change notification settings - Fork 2
/
gent_repl.cpp
583 lines (540 loc) · 14.7 KB
/
gent_repl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include "gent_repl.h"
#include "gent_db.h"
#include "gent_event.h"
#include "gent_app_mgr.h"
#include "gent_frame.h"
#include "gent_util.h"
#include "gent_thread.h"
GentRepMgr *GentRepMgr::intanceMaster_ = NULL;
GentRepMgr *GentRepMgr::intanceSlave_ = NULL;
GentRepMgr *GentRepMgr::Instance(const string &name) {
if(name == "master") {
if(intanceMaster_ == NULL) {
intanceMaster_ = new GentRepMgr(name);
}
return intanceMaster_;
}
if(intanceSlave_ == NULL) {
intanceSlave_ = new GentRepMgr(name);
}
return intanceSlave_;
}
void GentRepMgr::UnInstance() {
if(intanceMaster_) delete intanceMaster_;
if(intanceSlave_) delete intanceSlave_;
}
GentRepMgr::GentRepMgr(const string &name):connect_(NULL),status(GentRepMgr::INIT)
{
if(name == "master") {
string pathname = GentDb::Instance()->GetPath()+"/REPLICATION_INFO";
repfile_ = new GentFile<repinfo>(pathname, SLAVE_NUM);
if(!repfile_->Init(rep_map_)) {
LOG(GentLog::ERROR, "init replication infomation failed.");
}
}
server_id_ = GentFrame::Instance()->s->server_id;
slave_send_time = 0;
slave_start_time = 0;
}
GentRepMgr::~GentRepMgr()
{
std::map<string,GentReplication*>::iterator it;
for(it=rep_list_.begin();it!=rep_list_.end();it++){
delete (*it).second;
}
}
void GentRepMgr::MasterHandle(int fd, short which, void *arg)
{
char buf[2];
if (read(fd, buf, 1) != 1){
LOG(GentLog::WARN, "Can't read from libevent pipe");
}
GentReplication *rep = GentFrame::Instance()->master_msg_.Pop();
if(!rep->GetConn()) return;
GentRedis *redis =static_cast<GentRedis *>(rep->GetConn()->comm);
rep = GentRepMgr::Instance("master")->Get(redis->keystr, redis->is_sync_all);
string outstr;
if(rep == NULL) {
outstr = REDIS_ERROR+" slave full,manual clear\r\n";
}else{
if(!rep->Start(redis->repmsg, redis->conn, outstr)) {
redis->conn->SetStatus(Status::CONN_WAIT);
}
}
redis->conn->OutString(outstr);
redis->conn->Reset();
}
void GentRepMgr::SlaveHandle(int fd, short which, void *arg)
{
GentEvent *e = static_cast<GentEvent *>(arg);
GentRepMgr::Instance("slave")->Slave(e);
e->DelEvent();
struct timeval tv = {1,0};
int ret = e->AddTimeEvent(&tv, GentRepMgr::SlaveHandle);
while(ret == -1) {
ret = e->AddTimeEvent(&tv, GentRepMgr::SlaveHandle);
}
}
void GentRepMgr::Slave(GentEvent *e)
{
//string s = (conn_)?conn_->GetStatus():"null";
//LOG(GentLog::WARN, "GentRepMgr::Slave,status: %d,conn:%s",status, s.c_str());
if(status == GentRepMgr::AUTH || status == GentRepMgr::WAIT){
if(conn_ && conn_->fd>0){
if(slave_send_time > 0 && time(NULL) - slave_send_time > 60) {
//CannelConnect();
status = GentRepMgr::CONTINUE;
}
return;
}
status = GentRepMgr::CONTINUE;
}
GentConfig &config = GentFrame::Instance()->config;
if(!conn_ || conn_->GetStatus() == "close") {
if(config["slaveof_ip"] == "" || config["slaveof_port"] == "") return;
int port = atoi(config["slaveof_port"].c_str());
if(conn_) {
conn_ = NULL;
delete conn_;
}
if(LinkMaster(e, config["slaveof_ip"], port)<=0) return;
if(slave_start_time == 0) {
slave_start_time = time(NULL);
}
status = GentRepMgr::INIT;
}
if(status == GentRepMgr::INIT) {
if(config["master_auth"] == "") {
status = GentRepMgr::CONTINUE;
cout << "master_auth is null "<<endl;
}else{
//认证过程
int sdnum = SlaveAuth(server_id_, config["master_auth"]);
if(sdnum == -1) {
//CannelConnect();
return;
}
status = GentRepMgr::AUTH;
}
e->AddEvent(conn_, GentEvent::Handle);
return;
}else if(status == GentRepMgr::CONTINUE) {
char str[300] = {0};
snprintf(str, 300,"*3\r\n$3\r\nrep\r\n$%ld\r\n%s\r\n$4\r\ndata\r\n",
(unsigned long)server_id_.size(),server_id_.c_str());
string sendstr(str);
int sdnum = conn_->OutString(sendstr);
if(sdnum == -1) {
//CannelConnect();
return;
}
slave_send_time = time(NULL);
status = GentRepMgr::WAIT;
}
}
int GentRepMgr::LinkMaster(GentEvent *ev_, const string &host, int port) {
int sfd = ev_->Client(host,port);
if(sfd < 0) return sfd;
conn_ = GentAppMgr::Instance()->GetConnect(sfd);
conn_->RegDestroy(this);
conn_->SetAuth(1);
conn_->is_slave = true;
return sfd;
}
int GentRepMgr::SlaveAuth(const string &client_name, const string &auth)
{
char str[300] = {0};
GentConfig &config = GentFrame::Instance()->config;
string is_sync_all = "true";
if(config["slave_sync_all"] == "false") {
is_sync_all = "false";
}
snprintf(str, 300,"*4\r\n$3\r\nrep\r\n$%ld\r\n%s\r\n$4\r\nauth\r\n$%ld\r\n%s\r\n$%ld\r\n%s\r\n",
(unsigned long)client_name.size(),client_name.c_str(),
(unsigned long)auth.size(),auth.c_str(),
(unsigned long)is_sync_all.size(),is_sync_all.c_str());
string sendstr(str);
return conn_->OutString(sendstr);
}
void GentRepMgr::SlaveReply(string &outstr, int suc)
{
string t = (suc==1)?"ok":"error";
char str[300] = {0};
snprintf(str, 300,"*3\r\n$3\r\nrep\r\n$%ld\r\n%s\r\n$%ld\r\n%s\r\n",
(unsigned long)server_id_.size(),server_id_.c_str(), (unsigned long)t.size(),t.c_str());
outstr = str;
}
void GentRepMgr::SlaveSetStatus(int t)
{
status = t;
}
void GentRepMgr::CannelConnect() {
event_del(&conn_->ev);
if(conn_->fd > 0) {
conn_->Destruct();
GentAppMgr::Instance()->RetConnect(conn_);
}
conn_ = NULL;
}
bool GentRepMgr::Logout(string &name)
{
//注销队列
std::map<string,GentReplication*>::iterator it = rep_list_.find(name);
if(it == rep_list_.end()) return true;
if(!rep_list_[name]->SetLogout()) return false;
pthread_attr_t attr;
pthread_t pid;
pthread_attr_init(&attr);
GentReplication *r = rep_list_[name];
int ret = pthread_create(&pid,&attr,GentRepMgr::ClearRep,r);
if(ret != 0) {
LOG(GentLog::ERROR, "start clear %s replication failed.", name.c_str());
return false;
}
rep_list_.erase(it);
return true;
}
void *GentRepMgr::ClearRep(void *arg)
{
LOG(GentLog::WARN, "start clearRep replication.");
GentReplication *r = static_cast<GentReplication *>(arg);
delete r;
return ((void *)0);
}
void GentRepMgr::Init()
{
map<string,repinfo *>::iterator it;
for(it=rep_map_.begin(); it!=rep_map_.end();it++) {
rep_list_[it->first] = new GentReplication(it->first, it->second);
}
}
GentReplication *GentRepMgr::Get(const string &name, bool is_sync_all)
{
std::map<string,GentReplication*>::iterator it = rep_list_.find(name);
if(it == rep_list_.end()) {
map<string,repinfo *>::iterator it_info = rep_map_.find(name);
repinfo *info;
if(it_info == rep_map_.end()) {
info = repfile_->AddItem(name);
}else{
info = it_info->second;
}
if(info == NULL) return NULL;
rep_list_[name] = new GentReplication(name, info, is_sync_all);
}
return rep_list_[name];
}
void GentRepMgr::Push(int type, string &key)
{
std::map<string,GentReplication*>::iterator it;
for(it=rep_list_.begin(); it!=rep_list_.end(); it++)
{
it->second->Push(type, key);
}
}
void GentRepMgr::GetSlaveInfo(string &str)
{
str = "";
std::map<string,GentReplication*>::iterator it;
for(it=rep_list_.begin(); it!=rep_list_.end(); it++)
{
string info;
it->second->GetInfo(info);
str+=info;
}
if(str == ""){
str = "no slave\r\n";
}
}
void GentRepMgr::GetInfo(string &str)
{
GentConfig &config = GentFrame::Instance()->config;
if(config["slaveof_ip"] != "" && config["slaveof_port"] != "")
{
char buf[200] = {0};
map<int, string> st_map;
st_map[GentRepMgr::INIT] = "initialization";
st_map[GentRepMgr::AUTH] = "authentication";
st_map[GentRepMgr::WAIT] = "wait";
st_map[GentRepMgr::CONTINUE] = "transmit";
snprintf(buf,200, "connect master %s:%s,start_time:%ld,stage: %s\r\n",
config["slaveof_ip"].c_str(),config["slaveof_port"].c_str(),
(unsigned long)slave_start_time,st_map[status].c_str());
str = buf;
}else{
str = "no master\r\n";
}
}
uint32_t GentRepMgr::GetReplicationNum()
{
return rep_list_.size();
}
uint64_t GentRepMgr::QueLength()
{
uint64_t len = 0;
std::map<string,GentReplication*>::iterator it;
for(it=rep_list_.begin(); it!=rep_list_.end(); it++)
{
len += it->second->QueLength();
}
return len;
}
GentReplication::GentReplication(const string &name, repinfo *rinfo, bool is_sync_all):status(0),
main_que_length(0)
{
is_run = true;
slave_start_time = time(NULL);
rep_name = name;
rinfo_ = rinfo;
current_node = NULL;
if(!rinfo_->ser_time || rinfo_->ser_time > rinfo_->rep_time) {
if(is_sync_all) {
LOG(GentLog::WARN, "sync all data for %s slave.", name.c_str());
//设置所有的磁盘数据到队列
is_update_que = true;
vector<string> outvec;
vector<string>::iterator it;
GentDb::Instance()->Keys(outvec, "*");
for(it=outvec.begin();it!=outvec.end();it++) {
itemData *item = new itemData(*it,itemData::ADD);
que.push(item);
main_que_length++;
}
if(!rinfo_->ser_time) {
AutoLock lock(&que_push_lock);
rinfo_->set("ser_time",time(NULL));
}
}
}else{
LOG(GentLog::WARN, "not need to sync all data for %s slave.", name.c_str());
}
}
GentReplication::~GentReplication(){
while(main_que_length>0) {
Pop(false);
}
}
void GentReplication::Push(int type, string &key)
{
if(is_run == false) return;
AutoLock lock(&que_push_lock);
itemData *item = new itemData(key,type);
main_que.push(item);
//通知slave
if(main_que_length == 0 && conn_ && conn_->fd>0) {
GentThread::Intance()->SendMasterMsg(this);
}
main_que_length++;
rinfo_->set("ser_time",time(NULL));
}
void GentReplication::Pop(bool is_rep)
{
AutoLock lock(&que_push_lock);
itemData *it;
int num = 1;
int max = SYNC_NUM*2;
if(!is_update_que) {
while(num <= max) {
it = main_que.front_element();
if(it == NULL || it->is_sync == false) break;
it= main_que.pop();
if(is_rep == true) {
rinfo_->set("rep_time",time(NULL));
}
delete it;
it = NULL;
main_que_length--;
num++;
}
}else{
while(num <= max) {
it = que.front_element();
if(it == NULL) break;
if(it->is_sync == false) break;
it= que.pop();
if(it == NULL && !rinfo_->rep_time ) {
if(is_rep == true) {
rinfo_->set("rep_time", time(NULL));
}
}
delete it;
it = NULL;
main_que_length--;
num++;
}
}
if(it && it->is_sync) delete it;
it = NULL;
}
itemData *GentReplication::front_element()
{
AutoLock lock(&que_push_lock);
if(!is_update_que) {
return main_que.front_element();
}
itemData *it= que.front_element();
if(it == NULL) {
is_update_que = false;
if(!rinfo_->rep_time ) {
rinfo_->set("rep_time", time(NULL));
}
return main_que.front_element();
}
return it;
}
bool GentReplication::front_nums_element(std::vector<itemData *> &dat, int num)
{
AutoLock lock(&que_push_lock);
if(!is_update_que) {
return main_que.front_nums_element(dat, num);
}
if(!que.front_nums_element(dat, num)){
is_update_que = false;
if(!rinfo_->rep_time ) {
rinfo_->set("rep_time", time(NULL));
}
return main_que.front_nums_element(dat, num);
}
return true;
}
bool GentReplication::SetLogout()
{
if(conn_) return false;
is_run = false;
rinfo_->set("available",0);
return true;
}
void GentReplication::GetInfo(string &str)
{
string s = "";
if(!conn_) {
s = "close";
}else{
s = conn_->GetStatus();
}
char ret[500]={0};
snprintf(ret,500,"name:%s,ip:%s,start:%s,last:%s,status:%s,need_sync: %lld\r\n",
rep_name.c_str(),slave_ip.c_str(),
GentUtil::TimeToStr(slave_start_time).c_str(),
GentUtil::TimeToStr(slave_last_time).c_str(),
s.c_str(),
(unsigned long long)main_que_length);
str = ret;
}
bool GentReplication::Start(string &msg, GentConnect *c, string &outstr)
{
conn_ = c;
slave_ip = conn_->ip;
c->RegDestroy(this);
slave_last_time = time(NULL);
if(msg == "auth" ) {
outstr = "*2\r\n$5\r\nreply\r\n$6\r\nauthok\r\n";
return true;
}else if(msg == "autherror"){
outstr = "*2\r\n$5\r\nreply\r\n$9\r\nautherror\r\n";
return true;
}
if(msg != "ok" && msg != "data"){
outstr = "*2\r\n$5\r\nreply\r\n$5\r\nclose\r\n";
return true;
}
if(status == 1) {
status = 0;
//删除节点数据
if(msg == "ok") {
Pop();
}else if(msg == "error") {
outstr = "*2\r\n$5\r\nreply\r\n$5\r\nclose\r\n";
return true;
}
}
outstr = "";
//同步数据
vector<itemData *> syncData;
if(!front_nums_element(syncData, SYNC_NUM)) {
outstr = "";
return false;
}
status = 1;
vector<itemData *>::iterator iter;
int msetNum = 0;
for(iter = syncData.begin(); iter != syncData.end(); iter++) {
itemData *it = *iter;
if(it->type != itemData::ADD && msetNum != 0) return MsetReply(outstr, msetNum);
if(it->type != itemData::ADD) {
Reply(itemData::DEL, it->name, outstr);
it->setSync();
return true;
}
string nr = "";
LOG(GentLog::INFO, "sync the key of %s data.", it->name.c_str());
uint64_t expire;
if(!GentDb::Instance()->Get(it->name, nr, expire)) {
if(msetNum == 0) {
Reply(itemData::DEL, it->name, outstr);
it->setSync();
return true;
}
return MsetReply(outstr, msetNum);
}
uint64_t t = (unsigned long long)time(NULL);
if(expire != 0 && expire < t) {
if(msetNum == 0) {
Reply(itemData::DEL, it->name, outstr);
it->setSync();
return true;
}
return MsetReply(outstr, msetNum);
}
if(expire != 0) {
if(msetNum > 0) return MsetReply(outstr, msetNum);
expire = expire - t;
LOG(GentLog::INFO, "reply %s.", it->name.c_str());
Reply(itemData::ADD, it->name, outstr, nr, expire);
it->setSync();
return true;
}
string itstr;
if(it->name != "") {
ReplyItem(it->name, itstr, nr);
outstr += itstr;
msetNum++;
}
it->setSync();
}
return MsetReply(outstr, msetNum);
}
bool GentReplication::MsetReply(string &outstr, int num)
{
char headstr[30]={0};
snprintf(headstr,30,"*%d\r\n$4\r\nmset\r\n", num*2+1);
outstr = headstr + outstr;
return true;
}
void GentReplication::Reply(int type, string &key,string &outstr, const string &nr, uint64_t expire)
{
char retstr[300] = {0};
if(type == itemData::ADD) {
if(expire == 0){
snprintf(retstr,300,"*3\r\n$3\r\nset\r\n$%ld\r\n%s\r\n$%ld\r\n",
(unsigned long)key.size(),key.c_str(),(unsigned long)nr.size());
}else{
char expstr[20]={0};
snprintf(expstr, 20, "%llu",(unsigned long long)expire);
snprintf(retstr,300,"*4\r\n$5\r\nsetex\r\n$%ld\r\n%s\r\n$%ld\r\n%s\r\n$%ld\r\n",
(unsigned long)key.size(),key.c_str(),(unsigned long)strlen(expstr),expstr, (unsigned long)nr.size());
}
outstr=retstr+nr+"\r\n";
}else if(type == itemData::DEL){
snprintf(retstr,300,"*2\r\n$3\r\ndel\r\n$%ld\r\n%s\r\n",(unsigned long)key.size(), key.c_str());
outstr=retstr;
}
//cout << outstr <<endl;
}
void GentReplication::ReplyItem(string &key,string &outstr, const string &nr)
{
char retstr[100] = {0};
snprintf(retstr,100,"$%ld\r\n%s\r\n$%ld\r\n",
(unsigned long)key.size(),key.c_str(),(unsigned long)nr.size());
outstr=retstr+nr+"\r\n";
}