diff --git a/monitor/src/CMakeLists.txt b/monitor/src/CMakeLists.txt index 82cfc07..cdc1471 100644 --- a/monitor/src/CMakeLists.txt +++ b/monitor/src/CMakeLists.txt @@ -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 diff --git a/monitor/src/monitor.cc b/monitor/src/monitor.cc index bff4be0..e9d0ded 100644 --- a/monitor/src/monitor.cc +++ b/monitor/src/monitor.cc @@ -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(); @@ -36,129 +112,33 @@ 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; @@ -166,18 +146,12 @@ int main(int argc, char** argv){ //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(); @@ -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; diff --git a/monitor/src/monitor_config.cc b/monitor/src/monitor_config.cc index 3ba984d..39b773e 100644 --- a/monitor/src/monitor_config.cc +++ b/monitor/src/monitor_config.cc @@ -1,3 +1,6 @@ +#include +#include + #include #include #include @@ -7,9 +10,6 @@ #include #include -#include -#include - #include "monitor_config.h" #include "monitor_util.h" #include "monitor_log.h" diff --git a/monitor/src/monitor_load_balance.cc b/monitor/src/monitor_load_balance.cc index 5d117b6..04dbcfa 100644 --- a/monitor/src/monitor_load_balance.cc +++ b/monitor/src/monitor_load_balance.cc @@ -19,6 +19,7 @@ #include "monitor_zk.h" using namespace std; + extern char _zkLockBuf[512]; bool LoadBalance::reBalance = false; diff --git a/monitor/src/monitor_multi_thread.cc b/monitor/src/monitor_multi_thread.cc index 5f1d2f1..7b2e256 100644 --- a/monitor/src/monitor_multi_thread.cc +++ b/monitor/src/monitor_multi_thread.cc @@ -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; } diff --git a/monitor/src/monitor_zk.cc b/monitor/src/monitor_zk.cc index ca7d132..cfe74b4 100644 --- a/monitor/src/monitor_zk.cc +++ b/monitor/src/monitor_zk.cc @@ -1,13 +1,13 @@ +#include +#include +#include + #include #include #include #include #include -#include -#include -#include - #include "monitor_const.h" #include "monitor_config.h" #include "monitor_util.h" diff --git a/monitor/src/monitor_zk.h b/monitor/src/monitor_zk.h index e8b4e53..e29dd34 100644 --- a/monitor/src/monitor_zk.h +++ b/monitor/src/monitor_zk.h @@ -1,13 +1,13 @@ #ifndef ZK_H #define ZK_H +#include +#include + #include #include #include #include -#include -#include - #include "monitor_config.h" #include "monitor_service_item.h"