-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadBalance.cpp
More file actions
311 lines (291 loc) · 9.17 KB
/
Copy pathLoadBalance.cpp
File metadata and controls
311 lines (291 loc) · 9.17 KB
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
#include <cstring>
#include <map>
#include <algorithm>
#include <cstdio>
#include <string>
#include <signal.h>
#include <sys/types.h>
#include <iostream>
#include <errno.h>
#include <pthread.h>
#include "ConstDef.h"
#include "Util.h"
#include "Log.h"
#include "Process.h"
#include "LoadBalance.h"
#include "Config.h"
#include "Zk.h"
using namespace std;
extern char _zkLockBuf[512];
bool LoadBalance::reBalance = false;
LoadBalance* LoadBalance::lbInstance = NULL;
int LoadBalance::initEnv(){
string zkHost = conf->getZkHost();
int revcTimeout = conf->getZkRecvTimeout();
zh = zookeeper_init(zkHost.c_str(), watcher, revcTimeout, NULL, NULL, 0);
if (!zh) {
LOG(LOG_ERROR, "zookeeper_init failed. Check whether zk_host(%s) is correct or not", zkHost.c_str());
return M_ERR;
}
LOG(LOG_INFO, "zookeeper init success");
return M_OK;
}
int LoadBalance::destroyEnv() {
if (zh) {
LOG(LOG_INFO, "zookeeper close. func %s, line %d", __func__, __LINE__);
zookeeper_close(zh);
zh = NULL;
}
lbInstance = NULL;
return M_OK;
}
LoadBalance::LoadBalance() : zh(NULL) {
//md5ToServiceFatherLock = SPINLOCK_INITIALIZER;
pthread_mutex_init(&md5ToServiceFatherLock, NULL);
conf = Config::getInstance();
md5ToServiceFather.clear();
monitors.clear();
myServiceFather.clear();
}
LoadBalance::~LoadBalance(){
destroyEnv();
}
LoadBalance* LoadBalance::getInstance() {
if (!lbInstance) {
lbInstance = new LoadBalance();
}
return lbInstance;
}
void LoadBalance::processChildEvent(zhandle_t* zhandle, const string path) {
string monitorsPath = Config::getInstance()->getMonitorList();
string md5Path = Config::getInstance()->getNodeList();
//the number of registed monitors has changed. So we need rebalance
if (path == monitorsPath) {
LOG(LOG_INFO, "the number of monitors has changed. Rebalance...");
setReBalance();
}
else if (path == md5Path) {
LOG(LOG_DEBUG, "the number of serviceFather has changed. Rebalance...");
setReBalance();
}
}
void LoadBalance::processChangedEvent(zhandle_t* zhandle, const string path) {
LoadBalance* lb = LoadBalance::getInstance();
LOG(LOG_INFO, "the data of node %s changed.", path.c_str());
char serviceFather[256] = {0};
int dataLen = 256;
LOG(LOG_INFO, "md5Path: %s, serviceFather: %s, *dataLen: %d", path.c_str(), serviceFather, dataLen);
int ret = zoo_get(zhandle, path.c_str(), 1, serviceFather, &dataLen, NULL);
if (ret == ZOK) {
LOG(LOG_INFO, "get data success");
size_t pos = path.rfind('/');
string md5Path = path.substr(pos + 1);
lb->updateMd5ToServiceFather(md5Path, string(serviceFather));
}
else if (ret == ZNONODE) {
LOG(LOG_TRACE, "%s...out...node:%s not exist.", __FUNCTION__, path.c_str());
}
else {
LOG(LOG_ERROR, "parameter error. zhandle is NULL");
}
#ifdef DEBUGL
for (auto it = (lb->md5ToServiceFather).begin(); it != (lb->md5ToServiceFather).end(); ++it) {
cout << it->first << " " << it->second << endl;
}
#endif
return;
}
void LoadBalance::watcher(zhandle_t* zhandle, int type, int state, const char* path, void* context) {
switch (type) {
case SESSION_EVENT_DEF:
if (state == ZOO_EXPIRED_SESSION_STATE) {
LOG(LOG_INFO, "[ session event ] state: ZOO_EXPIRED_SESSION_STATE");
LOG(LOG_INFO, "restart the main loop!");
kill(getpid(), SIGUSR2);
}
else {
LOG(LOG_INFO, "[session event] state: %d", state);
}
break;
//this two events should be processed by class _zk
case CREATED_EVENT_DEF:
LOG(LOG_INFO, "zookeeper watcher [ create event ] path:%s", path);
break;
case DELETED_EVENT_DEF:
LOG(LOG_INFO, "zookeeper watcher [ delete event ] path:%s", path);
break;
case CHILD_EVENT_DEF:
LOG(LOG_INFO, "zookeeper watcher [ child event ] path:%s", path);
//redo loadBalance
processChildEvent(zhandle, string(path));
break;
case CHANGED_EVENT_DEF:
LOG(LOG_INFO, "zookeeper watcher [ change event ] path:%s", path);
processChangedEvent(zhandle, string(path));
break;
}
}
int LoadBalance::zkGetChildren(const string path, struct String_vector* children) {
if (!zh) {
LOG(LOG_ERROR, "zhandle is NULL");
return M_ERR;
}
int ret = zoo_get_children(zh, path.c_str(), 1, children);
if (ret == ZOK) {
LOG(LOG_INFO, "get children success");
return M_OK;
}
else if (ret == ZNONODE) {
LOG(LOG_TRACE, "%s...out...node:%s not exist.", __FUNCTION__, path.c_str());
return M_ERR;
}
else {
LOG(LOG_ERROR, "zoo_get_children. error: %s node:%s", zerror(ret), path.c_str());
return M_ERR;
}
return M_ERR;
}
int LoadBalance::zkGetNode(const char* md5Path, char* serviceFather, int* dataLen) {
if (!zh) {
LOG(LOG_ERROR, "zhandle is NULL");
return M_ERR;
}
LOG(LOG_INFO, "md5Path: %s, serviceFather: %s, *dataLen: %d", md5Path, serviceFather, *dataLen);
int ret = zoo_get(zh, md5Path, 1, serviceFather, dataLen, NULL);
if (ret == ZOK) {
LOG(LOG_INFO, "get data success");
return M_OK;
}
else if (ret == ZNONODE) {
LOG(LOG_TRACE, "%s...out...node:%s not exist.", __FUNCTION__, md5Path);
return M_ERR;
}
else {
LOG(LOG_ERROR, "parameter error. zhandle is NULL");
return M_ERR;
}
return M_ERR;
}
int LoadBalance::getMd5ToServiceFather() {
string path = conf->getNodeList();
struct String_vector md5Node = {0};
//get all md5 node
int ret = zkGetChildren(path, &md5Node);
if (ret == M_ERR) {
return M_ERR;
}
for (int i = 0; i < md5Node.count; ++i) {
char serviceFather[256] = {0};
string md5Path = conf->getNodeList() + "/" + string(md5Node.data[i]);
int dataLen = sizeof(serviceFather);
//get the value of md5Node which is serviceFather
ret = zkGetNode(md5Path.c_str(), serviceFather, &dataLen);
if (ret == M_ERR) {
LOG(LOG_ERROR, "get value of node:%s failed", md5Path.c_str());
continue;
}
updateMd5ToServiceFather(string(md5Node.data[i]), string(serviceFather));
LOG(LOG_INFO, "md5: %s, serviceFather: %s", md5Path.c_str(), serviceFather);
}
deallocate_String_vector(&md5Node);
#ifdef DEBUGL
for (auto it = md5ToServiceFather.begin(); it != md5ToServiceFather.end(); ++it) {
LOG(LOG_DEBUG, "md5:%s, service father:%s", (it->first).c_str(), (it->second).c_str());
}
#endif
return M_OK;
}
int LoadBalance::getMonitors(bool flag /*=false*/) {
string path = conf->getMonitorList();
struct String_vector monitorNode = {0};
int ret = zkGetChildren(path, &monitorNode);
if (ret == M_ERR) {
LOG(LOG_ERROR, "get monitors failes. path:%s", path.c_str());
return M_ERR;
}
for (int i = 0; i < monitorNode.count; ++i) {
string monitor = string(monitorNode.data[i]);
monitors.insert(monitor);
}
LOG(LOG_INFO, "There are %d monitors, I am %s", monitors.size(), _zkLockBuf);
deallocate_String_vector(&monitorNode);
return M_OK;
}
int LoadBalance::balance(bool flag /*=false*/) {
vector<string> md5Node;
pthread_mutex_lock(&md5ToServiceFatherLock);
for (auto it = md5ToServiceFather.begin(); it != md5ToServiceFather.end(); ++it) {
md5Node.push_back(it->first);
}
pthread_mutex_unlock(&md5ToServiceFatherLock);
#ifdef DEBUG
LOG(LOG_DEBUG, "md5 node value:");
for (auto it = md5Node.begin(); it != md5Node.end(); ++it) {
LOG(LOG_DEBUG, "%s", (*it).c_str());
}
#endif
vector<unsigned int> sequence;
for (auto it = monitors.begin(); it != monitors.end(); ++it) {
unsigned int tmp = stoi((*it).substr((*it).size() - 10));
sequence.push_back(tmp);
}
#ifdef DEBUG
LOG(LOG_DEBUG, "sequence number of monitors registed:");
for (auto it = sequence.begin(); it != sequence.end(); ++it) {
LOG(LOG_DEBUG, "%u", *it);
}
#endif
sort(sequence.begin(), sequence.end());
#ifdef DEBUG
LOG(LOG_DEBUG, "sorted sequence number of monitors registed:");
for (auto it = sequence.begin(); it != sequence.end(); ++it) {
LOG(LOG_DEBUG, "%d", *it);
}
#endif
string monitor = string(_zkLockBuf);
unsigned int mySeq = stoi(monitor.substr(monitor.size() - 10));
size_t rank = 0;
for (; rank < sequence.size(); ++rank) {
if (sequence[rank] == mySeq) {
break;
}
}
if (rank == sequence.size()) {
LOG(LOG_INFO, "I'm connect to zk. But the monitor registed is removed. Restart main loop");
Process::setStop();
return M_ERR;
}
for (size_t i = rank; i < md5Node.size(); i += monitors.size()) {
pthread_mutex_lock(&md5ToServiceFatherLock);
myServiceFather.push_back(md5ToServiceFather[md5Node[i]]);
pthread_mutex_unlock(&md5ToServiceFatherLock);
LOG(LOG_INFO, "my service father:%s", myServiceFather.back().c_str());
}
#ifdef DEBUG
LOG(LOG_DEBUG, "my service father:");
for (auto it = myServiceFather.begin(); it != myServiceFather.end(); ++it) {
LOG(LOG_DEBUG, "%s", (*it).c_str());
}
#endif
return M_OK;
}
const vector<string> LoadBalance::getMyServiceFather() {
return myServiceFather;
}
void LoadBalance::setReBalance() {
reBalance = true;
}
void LoadBalance::clearReBalance() {
reBalance = false;
}
bool LoadBalance::getReBalance() {
return reBalance;
}
void LoadBalance::updateMd5ToServiceFather(const string& md5Path, const string& serviceFather) {
if (serviceFather.size() <= 0) {
return;
}
pthread_mutex_lock(&md5ToServiceFatherLock);
md5ToServiceFather[md5Path] = serviceFather;
pthread_mutex_unlock(&md5ToServiceFatherLock);
}