-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.cpp
More file actions
198 lines (186 loc) · 4.79 KB
/
Copy pathAgent.cpp
File metadata and controls
198 lines (186 loc) · 4.79 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
#include <iostream>
#include <cstdio>
#include <thread>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include "Util.h"
#include "Config.h"
#include "ConstDef.h"
#include "Log.h"
#include "Process.h"
#include "Zk.h"
#include "LoadBalance.h"
#include "ServiceListener.h"
#include "MultiThread.h"
using namespace std;
int main(int argc, char** argv){
Config* conf = Config::getInstance();
Util::printConfig();
if (Process::isProcessRunning(MONITOR_PROCESS_NAME)) {
LOG(LOG_ERROR, "Monitor is already running.");
return -1;
}
if (conf->isDaemonMode()) {
Process::daemonize();
}
if (conf->isAutoStart()) {
int childExitStatus = -1;
int ret = Process::processKeepalive(childExitStatus, PIDFILE);
//parent process
if (ret > 0) {
return childExitStatus;
}
else if (ret < 0) {
return -1;
}
else {
//child process write pid to PIDFILE
if (Util::writePid(PIDFILE.c_str()) != 0) {
return -1;
}
}
}
while (1) {
LOG(LOG_INFO, " main loop start -> !!!!!!");
Process::clearStop();
conf->clearServiceMap();
Zk* _zk = Zk::getInstance();
string zkHost = conf->getZkHost();
string zkLogPath = conf->getZkLogPath();
int recvTimeout = conf->getZkRecvTimeout();
// init zookeeper handler
if (_zk->initEnv(zkHost, zkLogPath, recvTimeout) == M_OK) {
LOG(LOG_INFO, "Zk init env succeeded. host:%s zk log path:%s", zkHost.c_str(), zkLogPath.c_str());
}
else {
LOG(LOG_ERROR, "Zk init env failed, host:%s, zk log path:%s", zkHost.c_str(), zkLogPath.c_str());
if (_zk) {
delete _zk;
}
sleep(2);
return 0;
}
//check qconf_monitor_lock_node/default_instance/md5_list
//if(_zk->checkAndCreateZnode(conf->getNodeList()) == M_OK) {
if (_zk->createZnode2(conf->getNodeList()) == M_OK) {
LOG(LOG_INFO, "check znode %s done. node exist", (conf->getNodeList()).c_str());
}
else {
LOG(LOG_ERROR, "create znode %s failed", (conf->getNodeList()).c_str());
if (_zk) {
delete _zk;
}
sleep(2);
return 0;
}
//check qconf_monitor_lock_node/default_instance/monitor_list
//if(_zk->checkAndCreateZnode(conf->getMonitorList()) == M_OK) {
if (_zk->createZnode2(conf->getMonitorList()) == M_OK) {
LOG(LOG_INFO, "check znode %s done. node exist", (conf->getMonitorList()).c_str());
}
else {
LOG(LOG_ERROR, "create znode %s failed", (conf->getMonitorList()).c_str());
if (_zk) {
delete _zk;
}
sleep(2);
return 0;
}
// monitor register, this function should in LoadBalance
if (_zk->registerMonitor(conf->getMonitorList() + "/monitor_") == M_OK) {
LOG(LOG_INFO, "Monitor register success");
}
else {
LOG(LOG_ERROR, "Monitor register failed");
if (_zk) {
delete _zk;
}
sleep(2);
continue;
}
/*
this loop is for load balance.
If rebalance is needed, the loop will be reiterate
*/
while (1) {
if (Process::isStop() || MultiThread::isThreadError()) {
break;
}
LOG(LOG_INFO, " second loop start -> !!!!!!");
LoadBalance::clearReBalance();
//load balance
LoadBalance* lb = LoadBalance::getInstance();
if (lb->initEnv() == M_OK) {
LOG(LOG_INFO, "init load balance env succeeded");
}
else {
LOG(LOG_ERROR, "init load balance env failed");
delete lb;
sleep(2);
continue;
}
if (lb->getMd5ToServiceFather() == M_OK) {
LOG(LOG_INFO, "get md5 to service father succeeded");
}
else {
LOG(LOG_ERROR, "get md5 to service father failed");
/*
how to deal with this in a better way?
if the reason of failure is node not exist, we should restart main loop
*/
delete lb;
sleep(2);
continue;
}
if (lb->getMonitors() == M_OK) {
LOG(LOG_INFO, "get monitors secceeded");
}
else {
LOG(LOG_INFO, "get monitors failed");
delete lb;
sleep(2);
continue;
}
if (lb->balance() == M_OK) {
LOG(LOG_INFO, "balance secceeded");
}
else {
LOG(LOG_INFO, "balance failed");
delete lb;
sleep(2);
continue;
}
Config::getInstance()->clearServiceMap();
//after load balance. Each monitor should load the service to Config
ServiceListener* sl = ServiceListener::getInstance();
if (sl->initEnv() == M_OK) {
LOG(LOG_INFO, "init service listener env succeeded");
}
else {
LOG(LOG_INFO, "init service listener env failed");
delete sl;
delete lb;
sleep(2);
continue;
}
sl->getAllIp();
sl->loadAllService();
//multiThread module
MultiThread* ml = MultiThread::getInstance();
ml->runMainThread();
//It's important !! Remember to close it always
delete lb;
delete sl;
delete ml;
if (Process::isStop() || MultiThread::isThreadError()) {
break;
}
}
delete _zk;
sleep(2);
}
LOG(LOG_ERROR, "EXIT main loop!!!");
return 0;
}