Skip to content

Commit

Permalink
1) optimize monitor.cc; 2) format code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaodunqiao committed Nov 22, 2016
1 parent e7a0155 commit 7150f43
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 136 deletions.
2 changes: 1 addition & 1 deletion monitor/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ execute_process(WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../deps/zookeeper COMMAN
execute_process(WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../deps/zookeeper COMMAND ./configure
--prefix=${PROJECT_SOURCE_DIR}/../deps/zookeeper/_install)
execute_process(WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../deps/zookeeper COMMAND
make)
make -j24)
execute_process(WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/../deps/zookeeper COMMAND
make install)
execute_process(WORKING_DIRECTORY
Expand Down
211 changes: 90 additions & 121 deletions monitor/src/monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,88 @@

using namespace std;

int main(int argc, char** argv){
// TODO (gaodunqiao@360.cn) classify error
int initMonitorEnv(Zk* _zk) {
Config* conf = Config::getInstance();
Util::printConfig();
Process::clearStop();
conf->clearServiceMap();
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_ERROR, "Zk init env failed, host:%s, zk log path:%s", zkHost.c_str(), zkLogPath.c_str());
return M_ERR;
}

//check qconf_monitor_lock_node/default_instance/md5_list
if (_zk->createZnode2(conf->getNodeList()) != M_OK) {
LOG(LOG_ERROR, "create znode %s failed", (conf->getNodeList()).c_str());
return M_ERR;
}

//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_ERROR, "create znode %s failed", (conf->getMonitorList()).c_str());
return M_ERR;
}

// monitor register, this function should in LoadBalance
if (_zk->registerMonitor(conf->getMonitorList() + "/monitor_") != M_OK) {
LOG(LOG_ERROR, "Monitor register failed");
return M_ERR;
}
return M_OK;
}

// TODO classify error
int doLoadBalance(LoadBalance *lb) {
LoadBalance::clearReBalance();
//load balance
if (lb->initEnv() != M_OK) {
LOG(LOG_ERROR, "init load balance env failed");
return M_ERR;
}

if (lb->getMd5ToServiceFather() != M_OK) {
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
*/
return M_ERR;
}

if (lb->getMonitors() != M_OK) {
LOG(LOG_ERROR, "get monitors failed");
return M_ERR;
}

if (lb->balance() != M_OK) {
LOG(LOG_ERROR, "balance failed");
return M_ERR;
}
return M_OK;
}

int loadServiceToConf(ServiceListener *sl) {
if (sl->initEnv() != M_OK) {
LOG(LOG_ERROR, "init service listener env failed");
return M_ERR;
}
sl->getAllIp();
sl->loadAllService();
return M_OK;
}

int main(int argc, char** argv) {
Config* conf = Config::getInstance();

if (Process::isProcessRunning(MONITOR_PROCESS_NAME)) {
LOG(LOG_ERROR, "Monitor is already running.");
return -1;
return M_ERR;
}
if (conf->isDaemonMode()) {
Process::daemonize();
Expand All @@ -36,148 +112,46 @@ int main(int argc, char** argv){
return childExitStatus;
}
else if (ret < 0) {
return -1;
return M_ERR;
}
else {
//child process write pid to PIDFILE
if (Util::writePid(PIDFILE.c_str()) != 0) {
return -1;
if (Util::writePid(PIDFILE.c_str()) != M_OK) {
return M_ERR;
}
}
}

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;
if (initMonitorEnv(_zk) != M_OK) {
delete _zk;
LOG(LOG_ERROR, "init monitor env failed");
return M_ERR;
}
/*
this loop is for load balance.
If rebalance is needed, the loop will be reiterate
*/
while (1) {
if (Process::isStop() || MultiThread::isThreadError()) {
break;
}
while (!Process::isStop() && !MultiThread::isThreadError()) {
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");
LoadBalance* lb = LoadBalance::getInstance();
if (doLoadBalance(lb) != M_OK) {
delete lb;
sleep(2);
continue;
}

//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");
if (loadServiceToConf(sl) != M_OK) {
delete sl;
delete lb;
sleep(2);
continue;
}
sl->getAllIp();
sl->loadAllService();

//multiThread module
MultiThread* ml = MultiThread::getInstance();
Expand All @@ -187,12 +161,7 @@ int main(int argc, char** argv){
delete lb;
delete sl;
delete ml;
if (Process::isStop() || MultiThread::isThreadError()) {
break;
}
}
delete _zk;
sleep(2);
}
LOG(LOG_ERROR, "EXIT main loop!!!");
return 0;
Expand Down
6 changes: 3 additions & 3 deletions monitor/src/monitor_config.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <unistd.h>
#include <pthread.h>

#include <fstream>
#include <string>
#include <unordered_map>
Expand All @@ -7,9 +10,6 @@
#include <vector>
#include <iostream>

#include <unistd.h>
#include <pthread.h>

#include "monitor_config.h"
#include "monitor_util.h"
#include "monitor_log.h"
Expand Down
1 change: 1 addition & 0 deletions monitor/src/monitor_load_balance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "monitor_zk.h"

using namespace std;

extern char _zkLockBuf[512];

bool LoadBalance::reBalance = false;
Expand Down
5 changes: 1 addition & 4 deletions monitor/src/monitor_multi_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ bool MultiThread::isOnlyOneUp(string node, int val) {
sl->setWatchFlag();
sl->modifyServiceFatherStatus(serviceFather, STATUS_UP, -1);
sl->modifyServiceFatherStatus(serviceFather, STATUS_DOWN, 1);
pthread_mutex_unlock(&updateServiceLock);
ret = false;
}
else {
pthread_mutex_unlock(&updateServiceLock);
}
pthread_mutex_unlock(&updateServiceLock);
return ret;
}

Expand Down
8 changes: 4 additions & 4 deletions monitor/src/monitor_zk.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <signal.h>
#include <sys/types.h>
#include <errno.h>

#include <map>
#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>

#include <signal.h>
#include <sys/types.h>
#include <errno.h>

#include "monitor_const.h"
#include "monitor_config.h"
#include "monitor_util.h"
Expand Down
6 changes: 3 additions & 3 deletions monitor/src/monitor_zk.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef ZK_H
#define ZK_H
#include <zookeeper.h>
#include <zk_adaptor.h>

#include <map>
#include <cstdio>
#include <string>
#include <iostream>

#include <zookeeper.h>
#include <zk_adaptor.h>

#include "monitor_config.h"
#include "monitor_service_item.h"

Expand Down

0 comments on commit 7150f43

Please sign in to comment.