-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgent_repl.h
191 lines (182 loc) · 4.54 KB
/
gent_repl.h
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
#ifndef riser_gent_repl_h
#define riser_gent_repl_h
#include "gent_queue_list.h"
#include "gent_file.h"
#include "gent_connect.h"
class GentEvent;
class pageitem
{
public:
uint32_t available;
protected:
char *head;
public:
pageitem(){};
pageitem(char *str){head = str;};
virtual ~pageitem(){};
virtual size_t size()=0;
virtual void set(const string &str)=0;
};
class repinfo : public pageitem
{
public:
char name[SLAVE_NAME_SIZE];
uint32_t rep_time;
uint32_t ser_time;
repinfo(){};
repinfo(char *str):pageitem(str) {
init(str);
};
~repinfo(){};
void set(const string &nstr) {
string n = nstr;
if((int)n.size()<SLAVE_NAME_SIZE) {
size_t len = SLAVE_NAME_SIZE - n.size();
string tmp= "";
for(size_t i=0;i<len; i++) {
tmp+="\x00";
}
n+=tmp;
}
memcpy(name, n.c_str(), SLAVE_NAME_SIZE);
available = 1;
rep_time = 0;
ser_time = 0;
string wstr;
tostring(wstr);
memcpy(head, wstr.c_str(), wstr.size());
};
void set(const string &fieldname, uint64_t val)
{
int offset = 0;
int ln = 0;
if(fieldname == "ser_time") {
ser_time = val;
offset = 1;
ln = 10;
}else if(fieldname == "rep_time") {
rep_time = val;
offset = 11;
ln = 10;
}else if(fieldname == "available") {
available = val;
offset = 0;
ln = 1;
}
char buf[12] = {0};
snprintf(buf,ln,"%llu",(unsigned long long)val);
memcpy(head+offset, buf, ln);
};
void tostring(string &str) {
char buf[100] = {0};
char st[12] = {0};
if(ser_time == 0){
memcpy(st,"0000000000",10);
}else{
snprintf(st,12,"%ld",(unsigned long)ser_time);
}
char rt[12] = {0};
if(rep_time == 0){
memcpy(rt,"0000000000",10);
}else{
snprintf(rt,12,"%ld",(unsigned long)rep_time);
}
snprintf(buf,100,"%ld%s%s%s",(unsigned long)available,st,rt,name);
cout << buf<<endl;
str = buf;
};
size_t size() {
return SLAVE_NAME_SIZE+21;
};
void init(char *str) {
char t[2] ={0};
memcpy(t,str,1);
available = atoi(t);
char tt[11];
memcpy(tt,str+1,10);
ser_time = atoi(tt);
memcpy(tt,str+11,10);
rep_time = atoi(tt);
memcpy(name,str+21,SLAVE_NAME_SIZE);
};
};
class GentReplication : public GentDestroy
{
string rep_name;
bool is_run;
//0:初始状态;1:正在同步状态;
int status;
//当全量同步时候,导出所有影片的ID到que
bool is_update_que;
//slave的IP地址
string slave_ip;
//queue node point
NODE<itemData*> *current_node;
//主队列
GentListQueue<itemData*> main_que;
//在同步所有的数据时,不写如main_que,而写入que_
GentListQueue<itemData*> que;
//队列长度
uint64_t main_que_length;
repinfo *rinfo_;
uint64_t slave_start_time;
uint64_t slave_last_time;
CommLock que_push_lock;
CommLock que_pop_lock;
public:
GentReplication(const string &name, repinfo *rinfo, bool is_sync_all=true);
~GentReplication();
bool Start(string &msg, GentConnect *c, string &outstr);
void Push(int type, string &key);
uint64_t QueLength(){return main_que_length;};
void GetInfo(string &str);
bool SetLogout();
private:
void Reply(int type, string &key,string &outstr, const string &nr="", uint64_t expire=0);
void ReplyItem(string &key,string &outstr, const string &nr);
void Pop(bool is_rep=true);
itemData *front_element();
bool front_nums_element(std::vector<itemData *> &dat, int num);
bool MsetReply(string &outstr, int num);
};
class GentRepMgr : public GentDestroy
{
static GentRepMgr *intanceMaster_;
static GentRepMgr *intanceSlave_;
std::map<string,GentReplication*> rep_list_;
GentConnect *connect_;
int status;
GentFile<repinfo> *repfile_;
map<string,repinfo *> rep_map_;
uint64_t slave_start_time;
size_t slave_send_time;
string server_id_;
private:
int LinkMaster(GentEvent *ev_, const string &host, int port);
public:
enum status {INIT=0,AUTH=1,WAIT=2,CONTINUE=3};
public:
static GentRepMgr *Instance(const string &name);
static void *ClearRep(void *arg);
static void UnInstance();
static void SlaveHandle(int fd, short which, void *arg);
static void MasterHandle(int fd, short which, void *arg);
public:
GentRepMgr(const string &name);
~GentRepMgr();
void Init();
void Destroy(int id);
bool Logout(string &name);
GentReplication *Get(const string &name, bool is_sync_all=true);
void Push(int type, string &key);
uint32_t GetReplicationNum();
uint64_t QueLength();
void GetSlaveInfo(string &str);
int SlaveAuth(const string &client_name, const string &authstr);
void SlaveReply(string &outstr, int suc);
void SlaveSetStatus(int t);
void Slave(GentEvent *e);
void CannelConnect();
void GetInfo(string &str);
};
#endif