diff --git a/.vscode/settings.json b/.vscode/settings.json index c4d2515283c9902be7b84fbec8ec6758a2af6dab..655596c50ec554634060d116adb9c4fd40fe5427 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "C_Cpp.default.configurationProvider": "go2sh.cmake-integration", + "cmake.parallelJobs": 4, "fileHeaderCommentHelper.languageConfigs": { "language_cpp":{ "template": [ diff --git a/ChaosMetadataService/CMakeLists.txt b/ChaosMetadataService/CMakeLists.txt index 580ce85256f50740bb4b8b0bfc4b27671f7ceaee..b8f0dbfaf42df97da5a732be133b0c0738f7c80d 100644 --- a/ChaosMetadataService/CMakeLists.txt +++ b/ChaosMetadataService/CMakeLists.txt @@ -223,16 +223,16 @@ SET(cnd_src QueryDataMsgPSConsumer.cpp ) - SET(service_src ../chaos_service_common/persistence/mongodb/MongoDBHAConnectionManager.cpp - ../chaos_service_common/persistence/mongodb/MongoDBAccessor.cpp - ../chaos_service_common/DriverPoolManager.cpp ../chaos_service_common/health_system/HealtManagerDirect.cpp) + SET(service_src ../chaos_service_common/persistence/mongodb/MongoDBHAConnectionManager.cpp + ../chaos_service_common/persistence/mongodb/MongoDBAccessor.cpp + ../chaos_service_common/DriverPoolManager.cpp ../chaos_service_common/health_system/HealtManagerDirect.cpp) - SET(service_src ${service_src} ../chaos_service_common/persistence/mongodb/mongodb_cxx/MongoDBCXXDriver.cpp) + SET(service_src ${service_src} ../chaos_service_common/persistence/mongodb/mongodb_cxx/MongoDBCXXDriver.cpp) -SET(service_src ${service_src} ../chaos_service_common/persistence/data_access/AbstractDataAccess.cpp - ../chaos_service_common/persistence/data_access/AbstractPersistenceDriver.cpp - ../chaos_service_common/ChaosManager.cpp - ../chaos_service_common/ChaosServiceToolkit.cpp) + SET(service_src ${service_src} ../chaos_service_common/persistence/data_access/AbstractDataAccess.cpp + ../chaos_service_common/persistence/data_access/AbstractPersistenceDriver.cpp + ../chaos_service_common/ChaosManager.cpp + ../chaos_service_common/ChaosServiceToolkit.cpp) SET(cnd_src ${cnd_src} cron_job/MDSCronusManager.cpp cron_job/MDSCronJob.cpp @@ -409,9 +409,9 @@ SET(api_src ${api_src} object_storage/mongodb_3/MongoDBObjectStorageDataAccess. #POSIX FILE -SET(posix_src object_storage/posixFile/PosixFile.cpp -object_storage/posixFile/PosixFileObjectStorageDriver.cpp -object_storage/posixFile/BsonFStream.cpp object_storage/posixFile/FileLock.cpp) + SET(posix_src object_storage/posixFile/PosixFile.cpp + object_storage/posixFile/PosixFileObjectStorageDriver.cpp + object_storage/posixFile/BsonFStream.cpp object_storage/posixFile/FileLock.cpp) SET(api_src ${api_src} object_storage/influxDB/InfluxDB.cpp diff --git a/chaos/common/ChaosCommon.cpp b/chaos/common/ChaosCommon.cpp index 6b76ece8b7bbcd4d7ab481a479fcbffd81df4c01..f49a259937f9e440cb6854cf71c57ef58e42780f 100644 --- a/chaos/common/ChaosCommon.cpp +++ b/chaos/common/ChaosCommon.cpp @@ -20,9 +20,9 @@ */ #include <chaos/common/ChaosCommon.h> #include <chaos/common/chaos_errors.h> -#include <chaos/common/network/NetworkBroker.h> #include <chaos/common/healt_system/HealtManager.h> #include <chaos/common/metadata_logging/metadata_logging.h> +#include <chaos/common/network/NetworkBroker.h> #if CHAOS_PROMETHEUS #include <chaos/common/metric/MetricManager.h> @@ -32,8 +32,8 @@ #include <csignal> #include <fstream> #include <sstream> -//#include <signal.h> -//#include <csignal> +// #include <signal.h> +// #include <csignal> #ifndef _WIN32 #include <execinfo.h> @@ -43,8 +43,8 @@ #include <DbgHelp.h> #include <signal.h> #include <windows.h> -#include "ChaosCommonWin.h" #include "../cu_toolkit/windowsCompliant.h" +#include "ChaosCommonWin.h" #endif using namespace chaos; @@ -55,452 +55,460 @@ using namespace chaos::common::healt_system; using namespace chaos::common::metadata_logging; #ifndef _WIN32 -//http://stackoverflow.com/questions/11465148/using-sigaction-c-cpp +// http://stackoverflow.com/questions/11465148/using-sigaction-c-cpp void crit_err_hdlr(int sig_num, siginfo_t *info, void *ucontext) { - std::cerr << "SIGNAL " << sig_num - << " (" << strsignal(sig_num) << "), address is " - << info->si_addr << std::endl - << std::endl; - - void *array[50]; - - int size = backtrace(array, 50); - - char **messages = backtrace_symbols(array, size); - - // skip first stack frame (points here) - for (int i = 1; i < size && messages != NULL; ++i) { - char *mangled_name = 0, *offset_begin = 0, *offset_end = 0; - - // find parantheses and +address offset surrounding mangled name - for (char *p = messages[i]; *p; ++p) { - if (*p == '(') { - mangled_name = p; - } else if (*p == '+') { - offset_begin = p; - } else if (*p == ')') { - offset_end = p; - break; - } - } - - // if the line could be processed, attempt to demangle the symbol - if (mangled_name && offset_begin && offset_end && - mangled_name < offset_begin) { - *mangled_name++ = '\0'; - *offset_begin++ = '\0'; - *offset_end++ = '\0'; - - int status; - char *real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status); - - // if demangling is successful, output the demangled function name - if (status == 0) { - std::cerr << "[bt]: (" << i << ") " << messages[i] << " : " - << real_name << "+" << offset_begin << offset_end - << std::endl; - - } - // otherwise, output the mangled function name - else { - std::cerr << "[bt]: (" << i << ") " << messages[i] << " : " - << mangled_name << "+" << offset_begin << offset_end - << std::endl; - } - free(real_name); - } - // otherwise, print the whole line - else { - std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl; - } + std::cerr << "SIGNAL " << sig_num + << " (" << strsignal(sig_num) << "), address is " + << info->si_addr << std::endl + << std::endl; + + void *array[50]; + + int size = backtrace(array, 50); + + char **messages = backtrace_symbols(array, size); + + // skip first stack frame (points here) + for (int i = 1; i < size && messages != NULL; ++i) { + char *mangled_name = 0, *offset_begin = 0, *offset_end = 0; + + // find parantheses and +address offset surrounding mangled name + for (char *p = messages[i]; *p; ++p) { + if (*p == '(') { + mangled_name = p; + } else if (*p == '+') { + offset_begin = p; + } else if (*p == ')') { + offset_end = p; + break; + } + } + + // if the line could be processed, attempt to demangle the symbol + if (mangled_name && offset_begin && offset_end && + mangled_name < offset_begin) { + *mangled_name++ = '\0'; + *offset_begin++ = '\0'; + *offset_end++ = '\0'; + + int status; + char *real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status); + + // if demangling is successful, output the demangled function name + if (status == 0) { + std::cerr << "[bt]: (" << i << ") " << messages[i] << " : " + << real_name << "+" << offset_begin << offset_end + << std::endl; + + } + // otherwise, output the mangled function name + else { + std::cerr << "[bt]: (" << i << ") " << messages[i] << " : " + << mangled_name << "+" << offset_begin << offset_end + << std::endl; + } + free(real_name); + } + // otherwise, print the whole line + else { + std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl; } - std::cerr << std::endl; - - free(messages); - - exit(EXIT_FAILURE); + } + std::cerr << std::endl; + + free(messages); + + exit(EXIT_FAILURE); } #else void crit_err_hdlr(int sig_num) { - std::cerr << "SIGNAL " << sig_num << " (" << strsignal(sig_num) << ")"; - - void * array[50]; - SYMBOL_INFO *symbol; - HANDLE process; - - process = GetCurrentProcess(); - - SymInitialize(process, NULL, TRUE); - int size = CaptureStackBackTrace(0, 50, array, NULL); - symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1); - symbol->MaxNameLen = 255; - symbol->SizeOfStruct = sizeof(SYMBOL_INFO); - // skip first stack frame (points here) - for (int i = 1; i < size; i++) { - SymFromAddr(process, (DWORD64)(array[i]), 0, symbol); - - //printf("%i: %s - 0x%0X\n", size - i - 1, symbol->Name, symbol->Address); - std::cerr << "[bt]: (" << i << ") " << symbol->Name << " : " - << symbol->Address << std::endl; - } - - free(symbol); - std::cerr << std::endl; - - exit(EXIT_FAILURE); + std::cerr << "SIGNAL " << sig_num << " (" << strsignal(sig_num) << ")"; + + void *array[50]; + SYMBOL_INFO *symbol; + HANDLE process; + + process = GetCurrentProcess(); + + SymInitialize(process, NULL, TRUE); + int size = CaptureStackBackTrace(0, 50, array, NULL); + symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1); + symbol->MaxNameLen = 255; + symbol->SizeOfStruct = sizeof(SYMBOL_INFO); + // skip first stack frame (points here) + for (int i = 1; i < size; i++) { + SymFromAddr(process, (DWORD64)(array[i]), 0, symbol); + + // printf("%i: %s - 0x%0X\n", size - i - 1, symbol->Name, symbol->Address); + std::cerr << "[bt]: (" << i << ") " << symbol->Name << " : " + << symbol->Address << std::endl; + } + + free(symbol); + std::cerr << std::endl; + + exit(EXIT_FAILURE); } #endif ChaosAbstractCommon::ChaosAbstractCommon() -: ingore_unreg_po(false), initialized(false), deinitialized(false) { - GlobalConfiguration::getInstance()->preParseStartupParameters(); - initialized = deinitialized = false; - stringstream ss,vv,dd; - ss << (BOOST_VERSION / 100000) << "." << ((BOOST_VERSION / 100) % 1000) << "." << (BOOST_VERSION / 100000); - vv <<CSLIB_VERSION_MAJOR<<"."<<CSLIB_VERSION_MINOR<<"."<<CSLIB_VERSION_NUMBER; - dd <<__DATE__ <<" " <<__TIME__; - GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("BoostVer",ss.str()); - GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("CompilerVer",BOOST_COMPILER); - GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("ChaosVersion",vv.str()); - GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("BuildDate",dd.str()); - GlobalConfiguration::getInstance()->getBuildInfoRef().addInt32Value("BuildID",CSLIB_BUILD_ID); + : ingore_unreg_po(false), initialized(false), deinitialized(false) { + GlobalConfiguration::getInstance()->preParseStartupParameters(); + initialized = deinitialized = false; + stringstream ss, vv, dd; + ss << (BOOST_VERSION / 100000) << "." << ((BOOST_VERSION / 100) % 1000) << "." << (BOOST_VERSION / 100000); + vv << CSLIB_VERSION_MAJOR << "." << CSLIB_VERSION_MINOR << "." << CSLIB_VERSION_NUMBER; + dd << __DATE__ << " " << __TIME__; + GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("BoostVer", ss.str()); + GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("CompilerVer", BOOST_COMPILER); + GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("ChaosVersion", vv.str()); + GlobalConfiguration::getInstance()->getBuildInfoRef().addStringValue("BuildDate", dd.str()); + GlobalConfiguration::getInstance()->getBuildInfoRef().addInt32Value("BuildID", CSLIB_BUILD_ID); } ChaosAbstractCommon::~ChaosAbstractCommon() {} void ChaosAbstractCommon::preparseConfigFile(std::istream &config_file_stream) { - GlobalConfiguration::getInstance()->loadStartupParameterFromEnv(); - GlobalConfiguration::getInstance()->loadStreamParameter(config_file_stream); - GlobalConfiguration::getInstance()->scanOption(); + GlobalConfiguration::getInstance()->loadStartupParameterFromEnv(); + GlobalConfiguration::getInstance()->loadStreamParameter(config_file_stream); + GlobalConfiguration::getInstance()->scanOption(); } void ChaosAbstractCommon::preparseCommandOption(int argc, const char *argv[]) { - GlobalConfiguration::getInstance()->loadStartupParameterFromEnv(); - GlobalConfiguration::getInstance()->loadStartupParameter(argc, argv); - GlobalConfiguration::getInstance()->scanOption(); - - //check if we have a config file - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_CONF_FILE)) { - //reload configuraiton from file - std::string file_option = GlobalConfiguration::getInstance()->getOption<std::string>(InitOption::OPT_CONF_FILE); - std::ifstream option_file_stream; - option_file_stream.open(file_option.c_str(), std::ifstream::in); - if (!option_file_stream) { - throw chaos::CException(-1, "Error opening configuration file", "Startup sequence"); - } - //reparse the config file - preparseConfigFile(option_file_stream); + GlobalConfiguration::getInstance()->loadStartupParameterFromEnv(); + GlobalConfiguration::getInstance()->loadStartupParameter(argc, argv); + GlobalConfiguration::getInstance()->scanOption(); + + // check if we have a config file + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_CONF_FILE)) { + // reload configuraiton from file + std::string file_option = GlobalConfiguration::getInstance()->getOption<std::string>(InitOption::OPT_CONF_FILE); + std::ifstream option_file_stream; + option_file_stream.open(file_option.c_str(), std::ifstream::in); + if (!option_file_stream) { + throw chaos::CException(-1, "Error opening configuration file", "Startup sequence"); } - //parse the dafult framework option - GlobalConfiguration::getInstance()->checkDefaultOption(); + // reparse the config file + preparseConfigFile(option_file_stream); + } + // parse the dafult framework option + GlobalConfiguration::getInstance()->checkDefaultOption(); } void ChaosAbstractCommon::init(int argc, const char *argv[]) { - preparseCommandOption(argc, argv); - if (argv != NULL) { - if (ingore_unreg_po) { - GlobalConfiguration::getInstance()->parseStartupParametersAllowingUnregistered(argc, argv); - } else { - GlobalConfiguration::getInstance()->parseStartupParameters(argc, argv); - } + preparseCommandOption(argc, argv); + if (argv != NULL) { + if (ingore_unreg_po) { + GlobalConfiguration::getInstance()->parseStartupParametersAllowingUnregistered(argc, argv); + } else { + GlobalConfiguration::getInstance()->parseStartupParameters(argc, argv); } - init(NULL); + } + init(NULL); } void ChaosAbstractCommon::init(std::istream &initStream) { - GlobalConfiguration::getInstance()->parseStringStream(initStream); - init(NULL); + GlobalConfiguration::getInstance()->parseStringStream(initStream); + init(NULL); } void ChaosAbstractCommon::init(void *init_data) { - int err = 0; - struct utsname u_name; - if (initialized) - return; - try { + int err = 0; + struct utsname u_name; + if (initialized) + return; + try { #ifdef _WIN32 - std::string lockFileName = "./chaos_init.lock"; + std::string lockFileName = "./chaos_init.lock"; #else - std::string lockFileName = "/tmp/chaos_init.lock"; + std::string lockFileName = "/tmp/chaos_init.lock"; #endif - //lock file for permit to choose different tcp port for services - std::fstream domain_file_lock_stream(lockFileName.c_str(), - std::ios_base::out | - std::ios_base::binary); //check if we have got the lock - boost::interprocess::file_lock flock(lockFileName.c_str()); - boost::interprocess::scoped_lock<boost::interprocess::file_lock> e_lock(flock); - __file_remover__ fr(lockFileName.c_str()); - - initialized = true; - deinitialized = false; - - //startup logger - chaos::common::log::LogManager::getInstance()->init(); - - //print chaos library header - PRINT_LIB_HEADER + // lock file for permit to choose different tcp port for services + std::fstream domain_file_lock_stream(lockFileName.c_str(), + std::ios_base::out | + std::ios_base::binary); // check if we have got the lock + boost::interprocess::file_lock flock(lockFileName.c_str()); + boost::interprocess::scoped_lock<boost::interprocess::file_lock> e_lock(flock); + __file_remover__ fr(lockFileName.c_str()); + + initialized = true; + deinitialized = false; + + // startup logger + chaos::common::log::LogManager::getInstance()->init(); + + // print chaos library header + PRINT_LIB_HEADER #if CHAOS_PROMETHEUS - LAPP_ << "Metric HTTP port: " << GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_METRIC_WEB_SERVER_PORT); + LAPP_ << "Metric HTTP port: " << GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_METRIC_WEB_SERVER_PORT); #endif - err = uname(&u_name); - if (err == -1) { - LAPP_ << "Platform: " << strerror(errno); - } else { - LAPP_ << "Platform: " << u_name.sysname << " " << u_name.nodename << " " << u_name.release << " " << u_name.version << " " << u_name.machine; - } - - LAPP_ << "Boost version: " << (BOOST_VERSION / 100000) << "." << ((BOOST_VERSION / 100) % 1000) << "." << (BOOST_VERSION / 100000); - LAPP_ << "Compiler Version: " << BOOST_COMPILER; - LAPP_ << "-----------------------------------------"; - //find our ip - string local_ip; - if (GlobalConfiguration::getInstance()->getConfiguration()->hasKey(InitOption::OPT_PUBLISHING_IP)) { - local_ip = GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_PUBLISHING_IP); - } else { - if (GlobalConfiguration::getInstance()->getConfiguration()->hasKey(InitOption::OPT_PUBLISHING_INTERFACE)) - local_ip = common::utility::InetUtility::scanForLocalNetworkAddress(GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_PUBLISHING_INTERFACE)); - else - local_ip = common::utility::InetUtility::scanForLocalNetworkAddress(); - } - GlobalConfiguration::getInstance()->addLocalServerAddress(local_ip); - - LAPP_ << "The local address choosen is: " << GlobalConfiguration::getInstance()->getLocalServerAddress(); - + err = uname(&u_name); + if (err == -1) { + LAPP_ << "Platform: " << strerror(errno); + } else { + LAPP_ << "Platform: " << u_name.sysname << " " << u_name.nodename << " " << u_name.release << " " << u_name.version << " " << u_name.machine; + } + + LAPP_ << "Boost version: " << (BOOST_VERSION / 100000) << "." << ((BOOST_VERSION / 100) % 1000) << "." << (BOOST_VERSION / 100000); + LAPP_ << "Compiler Version: " << BOOST_COMPILER; + LAPP_ << "-----------------------------------------"; + // find our ip + string local_ip; + if (GlobalConfiguration::getInstance()->getConfiguration()->hasKey(InitOption::OPT_PUBLISHING_IP)) { + local_ip = GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_PUBLISHING_IP); + } else { + if (GlobalConfiguration::getInstance()->getConfiguration()->hasKey(InitOption::OPT_PUBLISHING_INTERFACE)) + local_ip = common::utility::InetUtility::scanForLocalNetworkAddress(GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_PUBLISHING_INTERFACE)); + else + local_ip = common::utility::InetUtility::scanForLocalNetworkAddress(); + } + GlobalConfiguration::getInstance()->addLocalServerAddress(local_ip); + + LAPP_ << "The local address choosen is: " << GlobalConfiguration::getInstance()->getLocalServerAddress(); + #if CHAOS_PROMETHEUS - common::utility::InizializableService::initImplementation(chaos::common::metric::MetricManager::getInstance(), init_data, "MetricManager", __PRETTY_FUNCTION__); + common::utility::InizializableService::initImplementation(chaos::common::metric::MetricManager::getInstance(), init_data, "MetricManager", __PRETTY_FUNCTION__); #endif - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_NODEUID)) { - nodeuid=GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_NODEUID); - } - //Starting Async central - common::utility::InizializableService::initImplementation(AsyncCentralManager::getInstance(), init_data, "AsyncCentralManager", __PRETTY_FUNCTION__); - common::utility::StartableService::initImplementation(NetworkBroker::getInstance(), init_data, "NetworkBroker", __PRETTY_FUNCTION__); - common::utility::StartableService::startImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__); - - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_TIME_CALIBRATION)) { - //enable timestamp calibration - chaos::common::utility::TimingUtil::getInstance()->enableTimestampCalibration(); - } - - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_PLUGIN_ENABLE)) { - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_PLUGIN_DIRECTORY_PATH) == false) { - throw chaos::CException(-1, "Plugin path need to be specified", __PRETTY_FUNCTION__); - } - //initialize the plugin manager - chaos::common::utility::InizializableService::initImplementation(chaos::common::plugin::PluginManager::getInstance(), NULL, "PluginManager", __PRETTY_FUNCTION__); - } - - //finally we can register the system rpc api for common uses - AbstActionDescShrPtr + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_NODEUID)) { + nodeuid = GlobalConfiguration::getInstance()->getConfiguration()->getStringValue(InitOption::OPT_NODEUID); + } + // Starting Async central + common::utility::InizializableService::initImplementation(AsyncCentralManager::getInstance(), init_data, "AsyncCentralManager", __PRETTY_FUNCTION__); + common::utility::StartableService::initImplementation(NetworkBroker::getInstance(), init_data, "NetworkBroker", __PRETTY_FUNCTION__); + common::utility::StartableService::startImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__); + + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_TIME_CALIBRATION)) { + // enable timestamp calibration + chaos::common::utility::TimingUtil::getInstance()->enableTimestampCalibration(); + } + + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_PLUGIN_ENABLE)) { + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_PLUGIN_DIRECTORY_PATH) == false) { + throw chaos::CException(-1, "Plugin path need to be specified", __PRETTY_FUNCTION__); + } + // initialize the plugin manager + chaos::common::utility::InizializableService::initImplementation(chaos::common::plugin::PluginManager::getInstance(), NULL, "PluginManager", __PRETTY_FUNCTION__); + } + + // finally we can register the system rpc api for common uses + AbstActionDescShrPtr action_description = addActionDescritionInstance<ChaosAbstractCommon>(this, &ChaosAbstractCommon::getBuildInfo, NodeDomainAndActionRPC::RPC_DOMAIN, NodeDomainAndActionRPC::ACTION_GET_BUILD_INFO, "Return the build info of current chaos node instance"); - - AbstActionDescShrPtr action_description2 = addActionDescritionInstance<ChaosAbstractCommon>(this, - &ChaosAbstractCommon::getProcessInfo, - NodeDomainAndActionRPC::RPC_DOMAIN, - NodeDomainAndActionRPC::ACTION_GET_PROCESS_INFO, - "Return the process info of current chaos node instance"); - - AbstActionDescShrPtr action_description3 = addActionDescritionInstance<ChaosAbstractCommon>(this, - &ChaosAbstractCommon::nodeShutDown, - NodeDomainAndActionRPC::RPC_DOMAIN, - NodeDomainAndActionRPC::ACTION_NODE_SHUTDOWN, - "Shutdown node immediately"); - AbstActionDescShrPtr action_description4 = addActionDescritionInstance<ChaosAbstractCommon>(this, - &ChaosAbstractCommon::clearAlarm, - NodeDomainAndActionRPC::RPC_DOMAIN, - NodeDomainAndActionRPC::ACTION_NODE_CLRALRM, - "Clear Alarms"); - - AbstActionDescShrPtr actionDescription5 = addActionDescritionInstance<ChaosAbstractCommon>(this, - &ChaosAbstractCommon::_registrationAck, - NodeDomainAndActionRPC::RPC_DOMAIN, - NodeDomainAndActionRPC::ACTION_REGISTRATION_ACK, - "Generic ack to a registration request"); - - - - NetworkBroker::getInstance()->registerAction(this); - } catch (std::exception& e){ - LERR_<< "Unexpected std exception received: "<<e.what(); - exit(1); - - } catch (...) { - LERR_<< "Unexpected exception received "; - exit(1); - } + + AbstActionDescShrPtr action_description2 = addActionDescritionInstance<ChaosAbstractCommon>(this, + &ChaosAbstractCommon::getProcessInfo, + NodeDomainAndActionRPC::RPC_DOMAIN, + NodeDomainAndActionRPC::ACTION_GET_PROCESS_INFO, + "Return the process info of current chaos node instance"); + + AbstActionDescShrPtr action_description3 = addActionDescritionInstance<ChaosAbstractCommon>(this, + &ChaosAbstractCommon::nodeShutDown, + NodeDomainAndActionRPC::RPC_DOMAIN, + NodeDomainAndActionRPC::ACTION_NODE_SHUTDOWN, + "Shutdown node immediately"); + AbstActionDescShrPtr action_description4 = addActionDescritionInstance<ChaosAbstractCommon>(this, + &ChaosAbstractCommon::clearAlarm, + NodeDomainAndActionRPC::RPC_DOMAIN, + NodeDomainAndActionRPC::ACTION_NODE_CLRALRM, + "Clear Alarms"); + + AbstActionDescShrPtr actionDescription5 = addActionDescritionInstance<ChaosAbstractCommon>(this, + &ChaosAbstractCommon::_registrationAck, + NodeDomainAndActionRPC::RPC_DOMAIN, + NodeDomainAndActionRPC::ACTION_REGISTRATION_ACK, + "Generic ack to a registration request"); + + NetworkBroker::getInstance()->registerAction(this); + } catch (std::exception &e) { + LERR_ << "Unexpected std exception received: " << e.what(); + exit(1); + + } catch (...) { + LERR_ << "Unexpected exception received "; + exit(1); + } #ifndef _WIN32 - struct sigaction sigact; - std::memset(&sigact, 0, sizeof(struct sigaction)); - sigact.sa_sigaction = crit_err_hdlr; - sigact.sa_flags = SA_RESTART | SA_SIGINFO; - if (sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL) != 0) { - LERR_ << "error setting signal handler for SIGSEGV"; - } + struct sigaction sigact; + std::memset(&sigact, 0, sizeof(struct sigaction)); + sigact.sa_sigaction = crit_err_hdlr; + sigact.sa_flags = SA_RESTART | SA_SIGINFO; + if (sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL) != 0) { + LERR_ << "error setting signal handler for SIGSEGV"; + } #else - if (signal(SIGSEGV, crit_err_hdlr) == SIG_ERR) { - LERR_ << "error setting signal handler for SIGSEGV"; - } + if (signal(SIGSEGV, crit_err_hdlr) == SIG_ERR) { + LERR_ << "error setting signal handler for SIGSEGV"; + } #endif - } -chaos::common::data::CDWUniquePtr ChaosAbstractCommon::_registrationAck(chaos::common::data::CDWUniquePtr ack_pack){ - CHECK_CDW_THROW_AND_LOG(ack_pack, LERR_, -1, "ACK message with no content for NODE"); - CHECK_KEY_THROW_AND_LOG(ack_pack, NodeDefinitionKey::NODE_UNIQUE_ID, LERR_, -2, "No identification of the device contained into the ack message for NODE"); - std::string agent_uid=ack_pack->getStringValue(NodeDefinitionKey::NODE_UNIQUE_ID); - if(ack_pack->hasKey(AgentNodeDomainAndActionRPC::REGISTRATION_RESULT)) { - int ack_val=ack_pack->getInt32Value(AgentNodeDomainAndActionRPC::REGISTRATION_RESULT); - switch(ack_val){ - case ErrorCode::EC_MDS_NODE_REGISTRATION_OK: - LAPP_ << CHAOS_FORMAT("NODE %1% has been registered", %agent_uid); - - break; - - default: - LERR_ << CHAOS_FORMAT("NODE %1% registration denied", %agent_uid); - break; - } +chaos::common::data::CDWUniquePtr ChaosAbstractCommon::_registrationAck(chaos::common::data::CDWUniquePtr ack_pack) { + CHECK_CDW_THROW_AND_LOG(ack_pack, LERR_, -1, "ACK message with no content for NODE"); + CHECK_KEY_THROW_AND_LOG(ack_pack, NodeDefinitionKey::NODE_UNIQUE_ID, LERR_, -2, "No identification of the device contained into the ack message for NODE"); + std::string agent_uid = ack_pack->getStringValue(NodeDefinitionKey::NODE_UNIQUE_ID); + if (ack_pack->hasKey(AgentNodeDomainAndActionRPC::REGISTRATION_RESULT)) { + int ack_val = ack_pack->getInt32Value(AgentNodeDomainAndActionRPC::REGISTRATION_RESULT); + switch (ack_val) { + case ErrorCode::EC_MDS_NODE_REGISTRATION_OK: + LAPP_ << CHAOS_FORMAT("NODE %1% has been registered", % agent_uid); + + break; + + default: + LERR_ << CHAOS_FORMAT("NODE %1% registration denied", % agent_uid); + break; } - return CDWUniquePtr(); + } + return CDWUniquePtr(); } void ChaosAbstractCommon::deinit() { - if (deinitialized) return; - deinitialized = true; - initialized = false; - - NetworkBroker::getInstance()->deregisterAction(this); - - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_PLUGIN_ENABLE)) { - //initialize the plugin manager - chaos::common::utility::InizializableService::deinitImplementation(chaos::common::plugin::PluginManager::getInstance(), "PluginManager", __PRETTY_FUNCTION__); - } - if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_TIME_CALIBRATION)) { - //enable timestamp calibration - chaos::common::utility::TimingUtil::getInstance()->disableTimestampCalibration(); - } - //dellocate all - CHAOS_NOT_THROW(common::utility::StartableService::stopImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__);); - CHAOS_NOT_THROW(common::utility::StartableService::deinitImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__);); - CHAOS_NOT_THROW(common::utility::InizializableService::deinitImplementation(AsyncCentralManager::getInstance(), "AsyncCentralManager", __PRETTY_FUNCTION__);); - - //shutdown logger - chaos::common::log::LogManager::getInstance()->deinit(); + if (deinitialized) return; + deinitialized = true; + initialized = false; + + NetworkBroker::getInstance()->deregisterAction(this); + + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_PLUGIN_ENABLE)) { + // initialize the plugin manager + chaos::common::utility::InizializableService::deinitImplementation(chaos::common::plugin::PluginManager::getInstance(), "PluginManager", __PRETTY_FUNCTION__); + } + if (GlobalConfiguration::getInstance()->hasOption(InitOption::OPT_TIME_CALIBRATION)) { + // enable timestamp calibration + chaos::common::utility::TimingUtil::getInstance()->disableTimestampCalibration(); + } + // dellocate all + CHAOS_NOT_THROW(common::utility::StartableService::stopImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__);); + CHAOS_NOT_THROW(common::utility::StartableService::deinitImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__);); + CHAOS_NOT_THROW(common::utility::InizializableService::deinitImplementation(AsyncCentralManager::getInstance(), "AsyncCentralManager", __PRETTY_FUNCTION__);); + + // shutdown logger + chaos::common::log::LogManager::getInstance()->deinit(); #if CHAOS_PROMETHEUS - CHAOS_NOT_THROW(common::utility::InizializableService::deinitImplementation(chaos::common::metric::MetricManager::getInstance(), "MetricManager", __PRETTY_FUNCTION__);) + CHAOS_NOT_THROW(common::utility::InizializableService::deinitImplementation(chaos::common::metric::MetricManager::getInstance(), "MetricManager", __PRETTY_FUNCTION__);) #endif } void ChaosAbstractCommon::start() {} void ChaosAbstractCommon::stop() { - // CHAOS_NOT_THROW(common::utility::StartableService::stopImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__);); - + // CHAOS_NOT_THROW(common::utility::StartableService::stopImplementation(NetworkBroker::getInstance(), "NetworkBroker", __PRETTY_FUNCTION__);); } GlobalConfiguration *ChaosAbstractCommon::getGlobalConfigurationInstance() { - return GlobalConfiguration::getInstance(); + return GlobalConfiguration::getInstance(); } CDWUniquePtr ChaosAbstractCommon::getBuildInfo(CHAOS_UNUSED CDWUniquePtr data) { - CDWUniquePtr buildInfo(new CDataWrapper()); - GlobalConfiguration::getInstance()->getBuildInfoRef().copyAllTo(*buildInfo); - return buildInfo; + CDWUniquePtr buildInfo(new CDataWrapper()); + GlobalConfiguration::getInstance()->getBuildInfoRef().copyAllTo(*buildInfo); + return buildInfo; } CDWUniquePtr ChaosAbstractCommon::getProcessInfo(CHAOS_UNUSED CDWUniquePtr data) { - return GlobalConfiguration::getInstance()->getProcessInfoRef().fullStat(); - + return GlobalConfiguration::getInstance()->getProcessInfoRef().fullStat(); } -static void clean_exit_th(int32_t sleeps,ChaosAbstractCommon*t){ - LAPP_ << "STOPPING after:"<<sleeps; +static void clean_exit_th(int32_t sleeps, ChaosAbstractCommon *t) { + LAPP_ << "STOPPING after:" << sleeps; - t->stop(); - LAPP_ << "DEINIT"; - - t->deinit(); - sleep(sleeps); - LAPP_ << "CLEAN EXIT!!!"; - exit(0); + t->stop(); + LAPP_ << "DEINIT"; + t->deinit(); + sleep(sleeps); + LAPP_ << "CLEAN EXIT!!!"; + exit(0); } -static void exit_th(int32_t sleeps){ - sleep(sleeps*2); - LAPP_ << "FORCE EXIT!!!"; - exit(0); - +static void exit_th(int32_t sleeps) { + sleep(sleeps * 2); + LAPP_ << "FORCE EXIT!!!"; + exit(0); } CDWUniquePtr ChaosAbstractCommon::nodeShutDown(CHAOS_UNUSED CDWUniquePtr data) { - if(data->hasKey("kill") && - data->isBoolValue("kill") && - data->getBoolValue("kill")) { - int32_t timeout = CDW_GET_INT32_WITH_DEFAULT(data, "timeout", 5); - - // boost::thread th0(clean_exit_th,timeout,this); - boost::thread th1(exit_th,timeout); - LAPP_ << "SHUTDOWN COMMAND:"<<data->getCompliantJSONString()<<" ABOUT TO EXIT IN:"<<timeout<< " seconds"; - - stop(); - deinit(); - // in case something blocks the thread will kill the process. - - } - return CDWUniquePtr(); + if (data->hasKey("kill") && + data->isBoolValue("kill") && + data->getBoolValue("kill")) { + int32_t timeout = CDW_GET_INT32_WITH_DEFAULT(data, "timeout", 5); + + // boost::thread th0(clean_exit_th,timeout,this); + boost::thread th1(exit_th, timeout); + LAPP_ << "SHUTDOWN COMMAND:" << data->getCompliantJSONString() << " ABOUT TO EXIT IN:" << timeout << " seconds"; + + stop(); + deinit(); + // in case something blocks the thread will kill the process. + } + return CDWUniquePtr(); } CDWUniquePtr ChaosAbstractCommon::clearAlarm(CHAOS_UNUSED CDWUniquePtr data) { + if (data.get() && data->hasKey(NodeDefinitionKey::NODE_UNIQUE_ID)) { + std::string uid = data->getStringValue(NodeDefinitionKey::NODE_UNIQUE_ID); - if(data.get()&&data->hasKey(NodeDefinitionKey::NODE_UNIQUE_ID)){ - std::string uid=data->getStringValue(NodeDefinitionKey::NODE_UNIQUE_ID); - - if(uid.size()) { - logError(uid,"Clear Alarm State",__PRETTY_FUNCTION__,0); - return data; - - - } + if (uid.size()) { + logError(uid, "Clear Alarm State", __PRETTY_FUNCTION__, 0); + return data; } - LERR_<<"Clear alarms without '"<<NodeDefinitionKey::NODE_UNIQUE_ID<<"'"; + } + LERR_ << "Clear alarms without '" << NodeDefinitionKey::NODE_UNIQUE_ID << "'"; - return data; + return data; } -void ChaosAbstractCommon::logError(const std::string&uid, const std::string&msg,const std::string&org,int lvl){ - static int oldlvl=-3; - static uint32_t nmsg=0; - - chaos::common::metadata_logging::StandardLoggingChannel* standard_logging_channel; - StandardLoggingChannel::LogLevel llvl=chaos::common::metadata_logging::StandardLoggingChannel::LogLevelError; - standard_logging_channel = (StandardLoggingChannel*)MetadataLoggingManager::getInstance()->getChannel("StandardLoggingChannel"); - LDBG_ << uid<< "- ["<<nmsg<<"] Alarm level:"<<lvl<<" message:"<<msg<<" origin:"<<org; - - if(standard_logging_channel){ - if(lvl==1){ - llvl=chaos::common::metadata_logging::StandardLoggingChannel::LogLevelWarning; - } else if((lvl==0)||(lvl==-1)){ - llvl=chaos::common::metadata_logging::StandardLoggingChannel::LogLevelInfo; - - } - if(!((lvl>-1) && (lvl!=oldlvl))){ - //chaos::common::metadata_logging::StandardLoggingChannel::LogLevelError; - standard_logging_channel->logMessage(uid, - org, - llvl, - msg); - oldlvl=lvl; - nmsg++; - } - } else { - LERR_<<"Cannot retrive Standard logging channel"; +void ChaosAbstractCommon::logError(const std::string &uid, const std::string &msg, const std::string &org, int lvl) { + static int oldlvl = -3; + static uint32_t nmsg = 0; + + chaos::common::metadata_logging::StandardLoggingChannel *standard_logging_channel; + StandardLoggingChannel::LogLevel llvl = chaos::common::metadata_logging::StandardLoggingChannel::LogLevelError; + standard_logging_channel = (StandardLoggingChannel *)MetadataLoggingManager::getInstance()->getChannel("StandardLoggingChannel"); + LDBG_ << uid << "- [" << nmsg << "] Alarm level:" << lvl << " message:" << msg << " origin:" << org; + + if (standard_logging_channel) { + if (lvl == 1) { + llvl = chaos::common::metadata_logging::StandardLoggingChannel::LogLevelWarning; + } else if ((lvl == 0) || (lvl == -1)) { + llvl = chaos::common::metadata_logging::StandardLoggingChannel::LogLevelInfo; } - if(lvl!=-1){ - // if -1 just make info without change alarm level - HealtManager::getInstance()->addNodeMetricValue(uid, - ControlUnitHealtDefinitionValue::CU_HEALT_OUTPUT_ALARM_LEVEL, - lvl,true); + if (!((lvl > -1) && (lvl != oldlvl))) { + // chaos::common::metadata_logging::StandardLoggingChannel::LogLevelError; + standard_logging_channel->logMessage(uid, + org, + llvl, + msg); + oldlvl = lvl; + nmsg++; } + } else { + LERR_ << "Cannot retrive Standard logging channel"; + } + if (lvl != -1) { + // if -1 just make info without change alarm level + HealtManager::getInstance()->addNodeMetricValue(uid, + ControlUnitHealtDefinitionValue::CU_HEALT_OUTPUT_ALARM_LEVEL, + lvl, + true); + } } +namespace chaos { +std::vector<std::string> split(const std::string &str, const std::string &delimiter, bool compress) { + std::vector<std::string> tokens; + std::regex re(delimiter); + std::sregex_iterator it(str.begin(), str.end(), re); + std::sregex_iterator end; + + while (it != end) { + auto token = it->prefix(); + if (compress && (token.length() == 0)) { + // Skip empty tokens. + } else { + tokens.push_back(token.str()); + } + ++it; + } + return tokens; +} +} // namespace chaos \ No newline at end of file diff --git a/chaos/common/ChaosCommon.h b/chaos/common/ChaosCommon.h index f00a20f3c26fc5641f8d8191f7d23363bdc0ca19..5a8dd57675dde734b4da91c3e427b58dc50bebd2 100644 --- a/chaos/common/ChaosCommon.h +++ b/chaos/common/ChaosCommon.h @@ -42,6 +42,8 @@ //! Default chaos namespace used to group all common api namespace chaos { + std::vector<std::string> split(const std::string& str, const std::string& delimiter, bool compress = true); + struct __file_remover__ { const char*name; __file_remover__(const char*_name):name(_name){} @@ -157,4 +159,5 @@ namespace chaos { }; } + #endif diff --git a/chaos/common/configuration/GlobalConfiguration.cpp b/chaos/common/configuration/GlobalConfiguration.cpp index d305314ed57bc84f73f1079bdb4c6a215052d947..f411eb93e0127d4c8947f16a6ea8225f30cdd6a0 100644 --- a/chaos/common/configuration/GlobalConfiguration.cpp +++ b/chaos/common/configuration/GlobalConfiguration.cpp @@ -26,8 +26,9 @@ #include <boost/filesystem.hpp> -#include <boost/algorithm/string.hpp> +//#include <boost/algorithm/string.hpp> #include <regex> +#include <chaos/common/ChaosCommon.h> #include "GlobalConfiguration.h" #include <chaos/common/version.h> #include <chaos/common/external_unit/external_unit.h> @@ -70,10 +71,12 @@ namespace chaos{ std::vector<std::string> kv_splitted; //get new pair - boost::algorithm::split(kv_splitted, + /* boost::algorithm::split(kv_splitted, kv_param_value, boost::algorithm::is_any_of(":"), - boost::algorithm::token_compress_on); + boost::algorithm::token_compress_on);*/ + + kv_splitted=chaos::split(kv_param_value,":",true); // add key/value pair if((kv_splitted.size()>1)&&(kv_splitted[0].size())){ kvmap.insert(make_pair(kv_splitted[0], kv_splitted[1])); diff --git a/chaos/common/state_flag/StateFlagSDWrapper.h b/chaos/common/state_flag/StateFlagSDWrapper.h index 44c7b2fe636f21b9fe7540637b3e584af76bc088..28d7acdfb62d0fb89a0a12c1f695367d0a9b8974 100644 --- a/chaos/common/state_flag/StateFlagSDWrapper.h +++ b/chaos/common/state_flag/StateFlagSDWrapper.h @@ -23,10 +23,10 @@ #define __CHAOSFramework_A4A802EC_5299_484B_AFF5_2FF3271807C2_StateFlagSDWrapper_h #include <chaos/common/state_flag/StateFlag.h> - +#include <chaos/common/ChaosCommon.h> #include <chaos/common/data/TemplatedDataSDWrapper.h> -#include <boost/algorithm/string.hpp> +//#include <boost/algorithm/string.hpp> namespace chaos { namespace common { @@ -65,10 +65,13 @@ namespace chaos { dataWrapped().flag_description.name = CDW_GET_SRT_WITH_DEFAULT(serialized_data, NodeStateFlagDefinitionKey::NODE_SF_NAME, ""); //check if we have a catalog name ChaosStringVector splitted_name; - boost::split( splitted_name, + /* boost::split( splitted_name, dataWrapped().flag_description.name , boost::is_any_of("/"), - boost::token_compress_on); + boost::token_compress_on);*/ + + splitted_name=chaos::split(dataWrapped().flag_description.name, "/", true); + if(splitted_name.size() > 1) { dataWrapped().flag_description.name = splitted_name[splitted_name.size()-1]; }