From 0a3a2aa30196f5c93423f79b8a3cb911cc00e506 Mon Sep 17 00:00:00 2001 From: ruanshudong Date: Mon, 6 Nov 2023 13:58:39 +0800 Subject: [PATCH] delete auto create table in tarsnotify --- NotifyServer/NotifyImp.cpp | 369 +++++------------- NotifyServer/NotifyImp.h | 32 -- deploy/framework/sql/template/tars.tarsnotify | 11 - 3 files changed, 91 insertions(+), 321 deletions(-) diff --git a/NotifyServer/NotifyImp.cpp b/NotifyServer/NotifyImp.cpp index 43f373ab..7d6dee1d 100644 --- a/NotifyServer/NotifyImp.cpp +++ b/NotifyServer/NotifyImp.cpp @@ -19,7 +19,6 @@ #include "NotifyServer.h" extern TC_Config * g_pconf; -//extern TarsHashMap * g_notifyHash; std::string Alarm = "[alarm]"; std::string Error = "[error]"; @@ -61,11 +60,7 @@ void NotifyImp::loadconf() _mysqlConfig.init(tcDBConf); - _sql = (*g_pconf)["/tars/"]; - _maxPageSize = TC_Common::strto((*g_pconf)["/tars/hash"]); - _maxPageNum = TC_Common::strto((*g_pconf)["/tars/hash"]); - - map m = g_pconf->getDomainMap("/tars/filter"); + map m = g_pconf->getDomainMap("/tars/filter"); for(map::iterator it= m.begin();it != m.end();it++) { vector vFilters = TC_Common::sepstr(it->second,";|"); @@ -79,60 +74,6 @@ void NotifyImp::initialize() loadconf(); } -bool NotifyImp::IsdbTableExist(const string& sTbName) -{ - try - { - TC_Mysql::MysqlData tTotalRecord = _mysqlConfig.queryRecord("show tables like '%"+sTbName+"%'"); - - TLOGINFO("NotifyImp::IsdbTableExist show tables like '%" + sTbName + "%|affected:" << tTotalRecord.size() << endl); - - if (tTotalRecord.size() > 0) - { - return true; - } - else - { - TLOGERROR("NotifyImp::IsdbTableExist sTbName:" << sTbName << "|doesn't exist" << endl); - return false; - } - - } - catch(TC_Mysql_Exception& ex) - { - TLOGERROR("NotifyImp::IsdbTableExist exception:" << ex.what() << endl); - return false; - } - catch(...) - { - TLOGERROR("NotifyImp::IsdbTableExist unknown exception." << endl); - return false; - } -} - -void NotifyImp::creatTb(const string &sTbName) -{ - try - { - if (!IsdbTableExist(sTbName)) - { - string sSql = TC_Common::replace(_sql, "${TABLE}",sTbName); - - _mysqlConfig.execute(sSql); - - TLOGDEBUG("NotifyImp::creatTb sSql:" << sSql << endl); - } - } - catch (TC_Mysql_Exception& ex) - { - TLOGERROR("NotifyImp::creatTb exception:" << ex.what() << endl); - } - catch(...) - { - TLOGERROR("NotifyImp::IsdbTableExist unknown exception." << endl); - } -} - bool NotifyImp::IsNeedFilte(const string& sServerName,const string& sResult) { if(_setFilter.find(sServerName) != _setFilter.end()) @@ -162,233 +103,105 @@ bool NotifyImp::IsNeedFilte(const string& sServerName,const string& sResult) return false; } -// -//void NotifyImp::reportServer(const string& sServerName, const string& sThreadId, const string& sResult, tars::TarsCurrentPtr current) -//{ -// string nodeName = current->getHostName() ; -// // if(nodeName.empty()) -// // { -// // nodeName = current->getIp(); -// // } -// -// TLOGDEBUG("NotifyImp::reportServer sServerName:" << sServerName << "|ip:" << nodeName << "|sThreadId:" << sThreadId << "|sResult:" << sResult << endl); -// -// if(IsNeedFilte(sServerName,sResult)) -// { -// TLOGWARN("NotifyImp::reportServer sServerName:" << sServerName << "|ip:" << nodeName << "|sThreadId:" << sThreadId << "|sResult:" << sResult <<"|filted"<< endl); -// return; -// } -// -// ReportInfo info; -// info.sApp = sServerName; -// info.sServer = sServerName; -// -// vector vModule = TC_Common::sepstr(sServerName,"."); -// if(vModule.size() >= 2) -// { -// info.sApp = vModule[0]; -// info.sServer = vModule[1]; -// } -// -// info.eType = REPORT; -// info.sSet = g_app.getLoadDbThread()->getSetName(sServerName + nodeName); -// info.sMessage = sResult; -// info.sThreadId = sThreadId; -// -// reportNotifyInfo(info, current); -// -// return; -//} -// -//void NotifyImp::notifyServer(const string& sServerName, NOTIFYLEVEL level, const string& sMessage, tars::TarsCurrentPtr current) -//{ -// string nodeName = current->getHostName() ; -// // if(nodeName.empty()) -// // { -// // nodeName = current->getIp(); -// // } -// -// ReportInfo info; -// info.sApp = sServerName; -// info.sServer = sServerName; -// -// vector vModule = TC_Common::sepstr(sServerName, "."); -// if (vModule.size() >= 2) -// { -// info.sApp = vModule[0]; -// info.sServer = vModule[1]; -// } -// -// info.eType = NOTIFY; -// info.sSet = g_app.getLoadDbThread()->getSetName(sServerName + nodeName); -// info.sMessage = sMessage; -// info.eLevel = level; -// -// reportNotifyInfo(info, current); -// -// return; -//} -// -//int NotifyImp::getNotifyInfo(const tars::NotifyKey & stKey,tars::NotifyInfo &stInfo,tars::TarsCurrentPtr current) -//{ -// int iRet = g_notifyHash->get(stKey, stInfo); -// -// ostringstream os; -// stKey.displaySimple(os); -// os << "|"; -// stInfo.displaySimple(os); -// -// TLOGDEBUG("NotifyImp::getNotifyInfo iRet:" << iRet << "|os:" << os.str() << endl); -// -// return iRet; -//} void NotifyImp::reportNotifyInfo(const tars::ReportInfo & info, tars::TarsCurrentPtr current) { - string nodeId = info.sNodeName; - if(nodeId.empty()) - { - nodeId = current->getIp(); - } + string nodeId = info.sNodeName; + if (nodeId.empty()) + { + nodeId = current->getIp(); + } switch (info.eType) { -// case (NOTIFY): -// { -// TLOGDEBUG("NotifyImp::reportNotifyInfo notifyServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet << "|sContainer:" << info.sContainer << "|nodeId:" << nodeId -// << "|eLevel:" << tars::etos(info.eLevel) << "|sMessage:" << info.sMessage << endl); -// -// string sServerId = info.sApp + info.sServer + "_" + nodeId; -// -// NotifyKey stKey0; -// stKey0.name = info.sApp + info.sServer; -// stKey0.ip = nodeId; -// stKey0.page = 0; -// -// NotifyInfo stInfo0; -// -// NotifyItem stItem; -// stItem.sTimeStamp = TC_Common::now2str("%Y-%m-%d %H:%M:%S"); -// stItem.sServerId = sServerId; -// stItem.iLevel = info.eLevel; -// stItem.sMessage = info.sMessage; -// -// int iRet; -// iRet = g_notifyHash->get(stKey0, stInfo0); -// TLOGDEBUG("get " << sServerId << " page " << stKey0.page << " info return :" << iRet << endl); -// if (iRet == TC_HashMap::RT_LOAD_DATA_ERR) -// { -// return; -// } -// -// if (stInfo0.notifyItems.size() < _maxPageSize) -// { -// stInfo0.notifyItems.push_back(stItem); -// iRet = g_notifyHash->set(stKey0, stInfo0); -// TLOGDEBUG("NotifyImp::reportNotifyInfo set sServerId:" << sServerId << "|page:" << stKey0.page << "|iRet:" << iRet << endl); -// } -// else -// { -// //0页置换出去 -// NotifyKey stKeyReplPage = stKey0; -// stKeyReplPage.page = (stInfo0.nextpage + 1) % _maxPageNum; -// if (stKeyReplPage.page == 0) -// { -// stKeyReplPage.page = 1; -// } -// iRet = g_notifyHash->set(stKeyReplPage, stInfo0); -// -// //修改0页 -// stInfo0.nextpage = stKeyReplPage.page; -// stInfo0.notifyItems.clear(); -// stInfo0.notifyItems.push_back(stItem); -// iRet = g_notifyHash->set(stKey0, stInfo0); -// } -// } - case (REPORT): - default: { - TLOGDEBUG( - "NotifyImp::reportNotifyInfo reportServer:" << info.sApp + "." + info.sServer << "|sSet:" << info.sSet - << "|sContainer:" << info.sContainer << "|ip:" - << current->getHostName() - << "|nodeName:" << info.sNodeName << "|sThreadId:" - << info.sThreadId << "|sMessage:" << info.sMessage << endl); - - if (IsNeedFilte(info.sApp + info.sServer, info.sMessage)) { - TLOGWARN( - "NotifyImp::reportNotifyInfo reportServer filter:" << info.sApp + "." + info.sServer << "|sSet:" - << info.sSet << "|sContainer:" << info.sContainer - << "|ip:" << current->getHostName() - << "|nodeName:" << info.sNodeName - << "|sThreadId:" << info.sThreadId - << "|sMessage:" << info.sMessage << "|filted" - << endl); - - return; - } - - string sql; - TC_Mysql::RECORD_DATA rd; + case (NOTIFY): + case (REPORT): + default: + { + TLOGDEBUG( + "NotifyImp::reportNotifyInfo reportServer:" << info.sApp + "." + info.sServer << "|sSet:" + << info.sSet + << "|sContainer:" << info.sContainer << "|ip:" + << current->getHostName() + << "|nodeName:" << info.sNodeName << "|sThreadId:" + << info.sThreadId << "|sMessage:" << info.sMessage + << endl); + + if (IsNeedFilte(info.sApp + info.sServer, info.sMessage)) + { + TLOGWARN( + "NotifyImp::reportNotifyInfo reportServer filter:" << info.sApp + "." + info.sServer << "|sSet:" + << info.sSet << "|sContainer:" + << info.sContainer + << "|ip:" << current->getHostName() + << "|nodeName:" << info.sNodeName + << "|sThreadId:" << info.sThreadId + << "|sMessage:" << info.sMessage << "|filted" + << endl); + + return; + } - rd["application"] = make_pair(TC_Mysql::DB_STR, info.sApp); - rd["server_name"] = make_pair(TC_Mysql::DB_STR, info.sServer); - rd["container_name"] = make_pair(TC_Mysql::DB_STR, info.sContainer); - rd["server_id"] = make_pair(TC_Mysql::DB_STR, info.sApp + "." + info.sServer + "_" + nodeId); - rd["node_name"] = make_pair(TC_Mysql::DB_STR, nodeId); - rd["thread_id"] = make_pair(TC_Mysql::DB_STR, info.sThreadId); + string sql; + TC_Mysql::RECORD_DATA rd; - if (!info.sSet.empty()) { - vector v = TC_Common::sepstr(info.sSet, "."); - if (v.size() != 3 || (v.size() == 3 && (v[0] == "*" || v[1] == "*"))) { - TLOGERROR("NotifyImp::reportNotifyInfo bad set name:" << info.sSet << endl); - } - else { - rd["set_name"] = make_pair(TC_Mysql::DB_STR, v[0]); - rd["set_area"] = make_pair(TC_Mysql::DB_STR, v[1]); - rd["set_group"] = make_pair(TC_Mysql::DB_STR, v[2]); - } + rd["application"] = make_pair(TC_Mysql::DB_STR, info.sApp); + rd["server_name"] = make_pair(TC_Mysql::DB_STR, info.sServer); + rd["container_name"] = make_pair(TC_Mysql::DB_STR, info.sContainer); + rd["server_id"] = make_pair(TC_Mysql::DB_STR, info.sApp + "." + info.sServer + "_" + nodeId); + rd["node_name"] = make_pair(TC_Mysql::DB_STR, nodeId); + rd["thread_id"] = make_pair(TC_Mysql::DB_STR, info.sThreadId); - } - else { - rd["set_name"] = make_pair(TC_Mysql::DB_STR, ""); - rd["set_area"] = make_pair(TC_Mysql::DB_STR, ""); - rd["set_group"] = make_pair(TC_Mysql::DB_STR, ""); - } + if (!info.sSet.empty()) + { + vector v = TC_Common::sepstr(info.sSet, "."); + if (v.size() != 3 || (v.size() == 3 && (v[0] == "*" || v[1] == "*"))) + { + TLOGERROR("NotifyImp::reportNotifyInfo bad set name:" << info.sSet << endl); + } else + { + rd["set_name"] = make_pair(TC_Mysql::DB_STR, v[0]); + rd["set_area"] = make_pair(TC_Mysql::DB_STR, v[1]); + rd["set_group"] = make_pair(TC_Mysql::DB_STR, v[2]); + } + + } else + { + rd["set_name"] = make_pair(TC_Mysql::DB_STR, ""); + rd["set_area"] = make_pair(TC_Mysql::DB_STR, ""); + rd["set_group"] = make_pair(TC_Mysql::DB_STR, ""); + } - if (info.eType == REPORT) { - rd["command"] = make_pair(TC_Mysql::DB_STR, getNotifyLevel(info.sMessage)); - } else { - rd["command"] = make_pair(TC_Mysql::DB_STR, getNotifyLevel(info.eLevel)); - } - rd["result"] = make_pair(TC_Mysql::DB_STR, info.sMessage); - rd["notifytime"] = make_pair(TC_Mysql::DB_INT, "now()"); - string sTable = "t_server_notifys"; - try { - _mysqlConfig.insertRecord(sTable, rd); - } - catch (TC_Mysql_Exception & ex) { - string err = string(ex.what()); - if (std::string::npos != err.find("doesn't exist")) { - creatTb(sTable); - } - // else { - // string sInfo = - // string("insert2Db exception") + "|" + ServerConfig::LocalIp + "|" + ServerConfig::Application - // + "." + ServerConfig::ServerName; - // TARS_NOTIFY_ERROR(sInfo); - // } - TLOGERROR("NotifyImp::reportNotifyInfo insert2Db exception:" << ex.what() << endl); - } - catch (exception & ex) { - TLOGERROR("NotifyImp::reportNotifyInfo insert2Db exception:" << ex.what() << endl); - string sInfo = - string("insert2Db exception") + "|" + ServerConfig::LocalIp + "|" + ServerConfig::Application + "." - + ServerConfig::ServerName; - } + if (info.eType == REPORT) + { + rd["command"] = make_pair(TC_Mysql::DB_STR, getNotifyLevel(info.sMessage)); + } else + { + rd["command"] = make_pair(TC_Mysql::DB_STR, getNotifyLevel(info.eLevel)); + } + rd["result"] = make_pair(TC_Mysql::DB_STR, info.sMessage); + rd["notifytime"] = make_pair(TC_Mysql::DB_INT, "now()"); + string sTable = "t_server_notifys"; + try + { + _mysqlConfig.insertRecord(sTable, rd); + } + catch (TC_Mysql_Exception &ex) + { + TLOGERROR("NotifyImp::reportNotifyInfo insert2Db exception:" << ex.what() << endl); + } + catch (exception &ex) + { + TLOGERROR("NotifyImp::reportNotifyInfo insert2Db exception:" << ex.what() << endl); + string sInfo = + string("insert2Db exception") + "|" + ServerConfig::LocalIp + "|" + ServerConfig::Application + + "." + + ServerConfig::ServerName; + } - TLOGERROR("reportNotifyInfo unknown type:" << info.writeToJsonString() << endl); - break; - } - } + TLOGERROR("reportNotifyInfo unknown type:" << info.writeToJsonString() << endl); + break; + } + } - return; + return; } diff --git a/NotifyServer/NotifyImp.h b/NotifyServer/NotifyImp.h index db60903a..2cbdf7f5 100644 --- a/NotifyServer/NotifyImp.h +++ b/NotifyServer/NotifyImp.h @@ -22,7 +22,6 @@ #include "util/tc_config.h" #include "util/tc_mysql.h" #include "servant/RemoteLogger.h" -//#include "LoadDbThread.h" using namespace tars; @@ -40,30 +39,6 @@ class NotifyImp : public Notify * 退出 */ virtual void destroy() {}; -// -// /** -// * report -// * @param sServerName -// * @param sThreadId -// * @param sResult -// * @param current -// */ -// virtual void reportServer(const string& sServerName, const string& sThreadId, const string& sResult, tars::TarsCurrentPtr current); -// -// /** -// * notify -// * @param sServerName -// * @param sThreadId -// * @param sCommand -// * @param sResult -// * @param current -// */ -// virtual void notifyServer(const string& sServerName, NOTIFYLEVEL level, const string& sMessage, tars::TarsCurrentPtr current); -// -// /** -// * get notify info -// */ -// virtual tars::Int32 getNotifyInfo(const tars::NotifyKey & stKey,tars::NotifyInfo &stInfo,tars::TarsCurrentPtr current); /* *reportNotifyInfo @@ -77,17 +52,10 @@ class NotifyImp : public Notify private: - bool IsdbTableExist(const string& sTbName); - - void creatTb(const string &sTbName); - bool IsNeedFilte(const string& sServerName,const string& sResult); protected: TC_Mysql _mysqlConfig; - string _sql; //创建表 - size_t _maxPageSize; - size_t _maxPageNum; /* * 按模块配置过滤规则 * key=default 表示全局规则 diff --git a/deploy/framework/sql/template/tars.tarsnotify b/deploy/framework/sql/template/tars.tarsnotify index 6bc44067..c1cf68a1 100755 --- a/deploy/framework/sql/template/tars.tarsnotify +++ b/deploy/framework/sql/template/tars.tarsnotify @@ -11,15 +11,4 @@ #retain history records retainHistory = 100 - - min_block=50 - max_block=200 - factor=1.5 - file_path=./notify - file_size=50000000 - max_page_num=30 - max_page_size=20 - - - sql=CREATE TABLE `${TABLE}` ( `id` int(11) NOT NULL AUTO_INCREMENT, `application` varchar(128) DEFAULT '', `server_name` varchar(128) DEFAULT NULL, `container_name` varchar(128) DEFAULT '' , `node_name` varchar(128) NOT NULL DEFAULT '', `set_name` varchar(16) DEFAULT NULL, `set_area` varchar(16) DEFAULT NULL, `set_group` varchar(16) DEFAULT NULL, `server_id` varchar(100) DEFAULT NULL, `thread_id` varchar(20) DEFAULT NULL, `command` varchar(50) DEFAULT NULL, `result` text, `notifytime` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name` (`server_name`), KEY `servernoticetime_i_1` (`notifytime`), KEY `indx_1_server_id` (`server_id`), KEY `query_index` (`application`,`server_name`,`node_name`,`set_name`,`set_area`,`set_group`) ) ENGINE\=InnoDB DEFAULT CHARSET\=utf8