DbHandle.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. 
  2. #include <iterator>
  3. #include <algorithm>
  4. #include "DbHandle.h"
  5. TC_ReadersWriterData<ObjectsCache> CDbHandle::_objectsCache;
  6. TC_ReadersWriterData<CDbHandle::SetDivisionCache> CDbHandle::_setDivisionCache;
  7. TC_ReadersWriterData<std::map<int, CDbHandle::GroupPriorityEntry> > CDbHandle::_mapGroupPriority;
  8. //key-ip, value-组编号
  9. TC_ReadersWriterData<map<string, int> > CDbHandle::_groupIdMap;
  10. //key-group_name, value-组编号
  11. TC_ReadersWriterData<map<string, int> > CDbHandle::_groupNameMap;
  12. int CDbHandle::init(TC_Config *pconf)
  13. {
  14. return 0;
  15. }
  16. vector<EndpointF> CDbHandle::findObjectById(const string& id)
  17. {
  18. ObjectsCache::iterator it;
  19. ObjectsCache& usingCache = _objectsCache.getReaderData();
  20. if ((it = usingCache.find(id)) != usingCache.end())
  21. {
  22. return it->second.vActiveEndpoints;
  23. }
  24. else
  25. {
  26. vector<EndpointF> activeEp;
  27. return activeEp;
  28. }
  29. }
  30. int CDbHandle::findObjectById4All(const string& id, vector<EndpointF>& activeEp, vector<EndpointF>& inactiveEp)
  31. {
  32. TLOGDEBUG(__FUNCTION__ << " id: " << id << endl);
  33. ObjectsCache::iterator it;
  34. ObjectsCache& usingCache = _objectsCache.getReaderData();
  35. if ((it = usingCache.find(id)) != usingCache.end())
  36. {
  37. activeEp = it->second.vActiveEndpoints;
  38. inactiveEp = it->second.vInactiveEndpoints;
  39. }
  40. else
  41. {
  42. activeEp.clear();
  43. inactiveEp.clear();
  44. }
  45. return 0;
  46. }
  47. vector<EndpointF> CDbHandle::getEpsByGroupId(const vector<EndpointF>& vecEps, const GroupUseSelect GroupSelect, int iGroupId, ostringstream& os)
  48. {
  49. os << "|";
  50. vector<EndpointF> vResult;
  51. for (unsigned i = 0; i < vecEps.size(); i++)
  52. {
  53. os << vecEps[i].host << ":" << vecEps[i].port << "(" << vecEps[i].groupworkid << ");";
  54. if (GroupSelect == ENUM_USE_WORK_GROUPID && vecEps[i].groupworkid == iGroupId)
  55. {
  56. vResult.push_back(vecEps[i]);
  57. }
  58. if (GroupSelect == ENUM_USE_REAL_GROUPID && vecEps[i].grouprealid == iGroupId)
  59. {
  60. vResult.push_back(vecEps[i]);
  61. }
  62. }
  63. return vResult;
  64. }
  65. vector<EndpointF> CDbHandle::getEpsByGroupId(const vector<EndpointF>& vecEps, const GroupUseSelect GroupSelect, const set<int>& setGroupID, ostringstream& os)
  66. {
  67. os << "|";
  68. std::vector<EndpointF> vecResult;
  69. for (std::vector<EndpointF>::size_type i = 0; i < vecEps.size(); i++)
  70. {
  71. os << vecEps[i].host << ":" << vecEps[i].port << "(" << vecEps[i].groupworkid << ")";
  72. if (GroupSelect == ENUM_USE_WORK_GROUPID && setGroupID.count(vecEps[i].groupworkid) == 1)
  73. {
  74. vecResult.push_back(vecEps[i]);
  75. }
  76. if (GroupSelect == ENUM_USE_REAL_GROUPID && setGroupID.count(vecEps[i].grouprealid) == 1)
  77. {
  78. vecResult.push_back(vecEps[i]);
  79. }
  80. }
  81. return vecResult;
  82. }
  83. int CDbHandle::findObjectByIdInSameGroup(const string& id, const string& ip, vector<EndpointF>& activeEp, vector<EndpointF>& inactiveEp, ostringstream& os)
  84. {
  85. activeEp.clear();
  86. inactiveEp.clear();
  87. int iClientGroupId = getGroupId(ip);
  88. os << "|(" << iClientGroupId << ")";
  89. if (iClientGroupId == -1)
  90. {
  91. return findObjectById4All(id, activeEp, inactiveEp);
  92. }
  93. ObjectsCache::iterator it;
  94. ObjectsCache& usingCache = _objectsCache.getReaderData();
  95. if ((it = usingCache.find(id)) != usingCache.end())
  96. {
  97. activeEp = getEpsByGroupId(it->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupId, os);
  98. inactiveEp = getEpsByGroupId(it->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupId, os);
  99. if (activeEp.size() == 0) //没有同组的endpoit,匹配未启用分组的服务
  100. {
  101. activeEp = getEpsByGroupId(it->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os);
  102. inactiveEp = getEpsByGroupId(it->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os);
  103. }
  104. if (activeEp.size() == 0) //没有同组的endpoit
  105. {
  106. activeEp = it->second.vActiveEndpoints;
  107. inactiveEp = it->second.vInactiveEndpoints;
  108. }
  109. }
  110. return 0;
  111. }
  112. int CDbHandle::findObjectByIdInGroupPriority(const std::string& sID, const std::string& sIP, std::vector<EndpointF>& vecActive, std::vector<EndpointF>& vecInactive, std::ostringstream& os)
  113. {
  114. vecActive.clear();
  115. vecInactive.clear();
  116. int iClientGroupID = getGroupId(sIP);
  117. os << "|(" << iClientGroupID << ")";
  118. if (iClientGroupID == -1)
  119. {
  120. return findObjectById4All(sID, vecActive, vecInactive);
  121. }
  122. ObjectsCache& usingCache = _objectsCache.getReaderData();
  123. ObjectsCache::iterator itObject = usingCache.find(sID);
  124. if (itObject == usingCache.end()) return 0;
  125. //首先在同组中查找
  126. {
  127. vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupID, os);
  128. vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, iClientGroupID, os);
  129. os << "|(In Same Group: " << iClientGroupID << " Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")";
  130. }
  131. //启用分组,但同组中没有找到,在优先级序列中查找
  132. std::map<int, GroupPriorityEntry> & mapPriority = _mapGroupPriority.getReaderData();
  133. for (std::map<int, GroupPriorityEntry>::iterator it = mapPriority.begin(); it != mapPriority.end() && vecActive.empty(); it++)
  134. {
  135. if (it->second.setGroupID.count(iClientGroupID) == 0)
  136. {
  137. os << "|(Not In Priority " << it->second.sGroupID << ")";
  138. continue;
  139. }
  140. vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, it->second.setGroupID, os);
  141. vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, it->second.setGroupID, os);
  142. os << "|(In Priority: " << it->second.sGroupID << " Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")";
  143. }
  144. //没有同组的endpoit,匹配未启用分组的服务
  145. if (vecActive.empty())
  146. {
  147. vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os);
  148. vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_WORK_GROUPID, -1, os);
  149. os << "|(In No Grouop: Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")";
  150. }
  151. //在未分组的情况下也没有找到,返回全部地址(此时基本上所有的服务都已挂掉)
  152. if (vecActive.empty())
  153. {
  154. vecActive = itObject->second.vActiveEndpoints;
  155. vecInactive = itObject->second.vInactiveEndpoints;
  156. os << "|(In All: Active=" << vecActive.size() << " Inactive=" << vecInactive.size() << ")";
  157. }
  158. return 0;
  159. }
  160. int CDbHandle::findObjectByIdInSameStation(const std::string& sID, const std::string& sStation, std::vector<EndpointF>& vecActive, std::vector<EndpointF>& vecInactive, std::ostringstream& os)
  161. {
  162. vecActive.clear();
  163. vecInactive.clear();
  164. //获得station所有组
  165. std::map<int, GroupPriorityEntry> & mapPriority = _mapGroupPriority.getReaderData();
  166. std::map<int, GroupPriorityEntry>::iterator itGroup = mapPriority.end();
  167. for (itGroup = mapPriority.begin(); itGroup != mapPriority.end(); itGroup++)
  168. {
  169. if (itGroup->second.sStation != sStation) continue;
  170. break;
  171. }
  172. if (itGroup == mapPriority.end())
  173. {
  174. os << "|not found station:" << sStation;
  175. return -1;
  176. }
  177. ObjectsCache& usingCache = _objectsCache.getReaderData();
  178. ObjectsCache::iterator itObject = usingCache.find(sID);
  179. if (itObject == usingCache.end()) return 0;
  180. //查找对应所有组下的IP地址
  181. vecActive = getEpsByGroupId(itObject->second.vActiveEndpoints, ENUM_USE_REAL_GROUPID, itGroup->second.setGroupID, os);
  182. vecInactive = getEpsByGroupId(itObject->second.vInactiveEndpoints, ENUM_USE_REAL_GROUPID, itGroup->second.setGroupID, os);
  183. return 0;
  184. }
  185. int CDbHandle::findObjectByIdInSameSet(const string& sID, const vector<string>& vtSetInfo, std::vector<EndpointF>& vecActive, std::vector<EndpointF>& vecInactive, std::ostringstream& os)
  186. {
  187. string sSetName = vtSetInfo[0];
  188. string sSetArea = vtSetInfo[0] + "." + vtSetInfo[1];
  189. string sSetId = vtSetInfo[0] + "." + vtSetInfo[1] + "." + vtSetInfo[2];
  190. SetDivisionCache& usingSetDivisionCache = _setDivisionCache.getReaderData();
  191. SetDivisionCache::iterator it = usingSetDivisionCache.find(sID);
  192. if (it == usingSetDivisionCache.end())
  193. {
  194. //此情况下没启动set
  195. TLOGINFO("CDbHandle::findObjectByIdInSameSet:" << __LINE__ << "|" << sID << " haven't start set|" << sSetId << endl);
  196. return -1;
  197. }
  198. map<string, vector<SetServerInfo> >::iterator setNameIt = it->second.find(sSetName);
  199. if (setNameIt == (it->second).end())
  200. {
  201. //此情况下没启动set
  202. TLOGINFO("CDbHandle::findObjectByIdInSameSet:" << __LINE__ << "|" << sID << " haven't start set|" << sSetId << endl);
  203. return -1;
  204. }
  205. if (vtSetInfo[2] == "*")
  206. {
  207. //检索通配组和set组中的所有服务
  208. vector<SetServerInfo> vServerInfo = setNameIt->second;
  209. for (size_t i = 0; i < vServerInfo.size(); i++)
  210. {
  211. if (vServerInfo[i].sSetArea == sSetArea)
  212. {
  213. if (vServerInfo[i].bActive)
  214. {
  215. vecActive.push_back(vServerInfo[i].epf);
  216. }
  217. else
  218. {
  219. vecInactive.push_back(vServerInfo[i].epf);
  220. }
  221. }
  222. }
  223. return (vecActive.empty() && vecInactive.empty()) ? -2 : 0;
  224. }
  225. else
  226. {
  227. // 1.从指定set组中查找
  228. int iRet = findObjectByIdInSameSet(sSetId, setNameIt->second, vecActive, vecInactive, os);
  229. if (iRet != 0 && vtSetInfo[2] != "*")
  230. {
  231. // 2. 步骤1中没找到,在通配组里找
  232. string sWildSetId = vtSetInfo[0] + "." + vtSetInfo[1] + ".*";
  233. iRet = findObjectByIdInSameSet(sWildSetId, setNameIt->second, vecActive, vecInactive, os);
  234. }
  235. return iRet;
  236. }
  237. }
  238. int CDbHandle::findObjectByIdInSameSet(const string& sSetId, const vector<SetServerInfo>& vSetServerInfo, std::vector<EndpointF>& vecActive, std::vector<EndpointF>& vecInactive, std::ostringstream& os)
  239. {
  240. for (size_t i = 0; i < vSetServerInfo.size(); ++i)
  241. {
  242. if (vSetServerInfo[i].sSetId == sSetId)
  243. {
  244. if (vSetServerInfo[i].bActive)
  245. {
  246. vecActive.push_back(vSetServerInfo[i].epf);
  247. }
  248. else
  249. {
  250. vecInactive.push_back(vSetServerInfo[i].epf);
  251. }
  252. }
  253. }
  254. int iRet = (vecActive.empty() && vecInactive.empty()) ? -2 : 0;
  255. return iRet;
  256. }
  257. void CDbHandle::updateObjectsCache(const ObjectsCache& objCache, bool updateAll)
  258. {
  259. //全量更新
  260. if (updateAll)
  261. {
  262. _objectsCache.getWriterData() = objCache;
  263. _objectsCache.swap();
  264. }
  265. else
  266. {
  267. //用查询数据覆盖一下
  268. _objectsCache.getWriterData() = _objectsCache.getReaderData();
  269. ObjectsCache& tmpObjCache = _objectsCache.getWriterData();
  270. ObjectsCache::const_iterator it = objCache.begin();
  271. for (; it != objCache.end(); it++)
  272. {
  273. //增量的时候加载的是服务的所有节点,因此这里直接替换
  274. tmpObjCache[it->first] = it->second;
  275. }
  276. _objectsCache.swap();
  277. }
  278. }
  279. void CDbHandle::updateInactiveObjectsCache(const ObjectsCache& objCache, bool updateAll)
  280. {
  281. //全量更新
  282. if (updateAll)
  283. {
  284. _objectsCache.getWriterData() = objCache;
  285. _objectsCache.swap();
  286. }
  287. else
  288. {
  289. //用查询数据覆盖一下
  290. _objectsCache.getWriterData() = _objectsCache.getReaderData();
  291. ObjectsCache& tmpObjCache = _objectsCache.getWriterData();
  292. ObjectsCache::const_iterator it = objCache.begin();
  293. for (; it != objCache.end(); it++)
  294. {
  295. //增量的时候加载的是服务的所有节点,因此这里直接替换
  296. tmpObjCache[it->first].vInactiveEndpoints.push_back((it->second).vInactiveEndpoints[0]);
  297. }
  298. _objectsCache.swap();
  299. }
  300. }
  301. void CDbHandle::updateActiveObjectsCache(const ObjectsCache& objCache, bool updateAll)
  302. {
  303. //全量更新
  304. if (updateAll)
  305. {
  306. _objectsCache.getWriterData() = objCache;
  307. _objectsCache.swap();
  308. }
  309. else
  310. {
  311. //用查询数据覆盖一下
  312. _objectsCache.getWriterData() = _objectsCache.getReaderData();
  313. ObjectsCache& tmpObjCache = _objectsCache.getWriterData();
  314. ObjectsCache::const_iterator it = objCache.begin();
  315. for (; it != objCache.end(); it++)
  316. {
  317. //增量的时候加载的是服务的所有节点,因此这里直接替换
  318. tmpObjCache[it->first].vActiveEndpoints.push_back((it->second).vActiveEndpoints[0]);
  319. }
  320. _objectsCache.swap();
  321. }
  322. }
  323. void CDbHandle::addActiveEndPoint(const string& objName, const Int32 port, const Int32 istcp)
  324. {
  325. #define LOCAL_HOST "127.0.0.1"
  326. ObjectsCache objectsCache;
  327. EndpointF endPoint;
  328. endPoint.host = LOCAL_HOST;
  329. endPoint.port = port;
  330. endPoint.timeout = 30000;
  331. endPoint.istcp = istcp;
  332. //endPoint.setId = setName + "." + setArea + "." + setGroup;
  333. objectsCache[objName].vActiveEndpoints.push_back(endPoint);
  334. updateActiveObjectsCache(objectsCache, false);
  335. }
  336. void CDbHandle::addEndPointbySet(const string& objName, const Int32 port, const Int32 istcp, const string& setName, const string& setArea, const string& setGroup)
  337. {
  338. #define LOCAL_HOST "127.0.0.1"
  339. ObjectsCache objectsCache;
  340. EndpointF endPoint;
  341. endPoint.host = LOCAL_HOST;
  342. endPoint.port = port;
  343. endPoint.timeout = 30000;
  344. endPoint.istcp = istcp;
  345. endPoint.setId = setName + "." + setArea + "." + setGroup;
  346. objectsCache[objName].vActiveEndpoints.push_back(endPoint);
  347. updateActiveObjectsCache(objectsCache, false);
  348. if (setName.size())
  349. {
  350. InsertSetRecord(objName, setName, setArea, setGroup, endPoint);
  351. }
  352. }
  353. void CDbHandle::addActiveWeight1EndPoint(const string& objName, const Int32 port, const Int32 istcp, const string& setName)
  354. {
  355. #define LOCAL_HOST "127.0.0.1"
  356. ObjectsCache objectsCache;
  357. EndpointF endPoint;
  358. endPoint.host = LOCAL_HOST;
  359. endPoint.port = port;
  360. endPoint.timeout = 30000;
  361. endPoint.istcp = istcp;
  362. endPoint.setId = setName;
  363. endPoint.weight = 2;
  364. endPoint.weightType = 1;
  365. objectsCache[objName].vActiveEndpoints.push_back(endPoint);
  366. updateActiveObjectsCache(objectsCache, false);
  367. }
  368. void CDbHandle::addInActiveWeight1EndPoint(const string& objName, const Int32 port, const Int32 istcp, const string& setName)
  369. {
  370. #define LOCAL_HOST "127.0.0.1"
  371. ObjectsCache objectsCache;
  372. EndpointF endPoint;
  373. endPoint.host = LOCAL_HOST;
  374. endPoint.port = port;
  375. endPoint.timeout = 30000;
  376. endPoint.istcp = istcp;
  377. endPoint.setId = setName;
  378. endPoint.weight = 2;
  379. endPoint.weightType = 1;
  380. objectsCache[objName].vInactiveEndpoints.push_back(endPoint);
  381. updateInactiveObjectsCache(objectsCache, false);
  382. }
  383. void CDbHandle::addActiveWeight2EndPoint(const string& objName, const Int32 port, const Int32 istcp, const string& setName)
  384. {
  385. #define LOCAL_HOST "127.0.0.1"
  386. ObjectsCache objectsCache;
  387. EndpointF endPoint;
  388. endPoint.host = LOCAL_HOST;
  389. endPoint.port = port;
  390. endPoint.timeout = 30000;
  391. endPoint.istcp = istcp;
  392. endPoint.setId = setName;
  393. endPoint.weight = 2;
  394. endPoint.weightType = 2;
  395. objectsCache[objName].vActiveEndpoints.push_back(endPoint);
  396. updateActiveObjectsCache(objectsCache, false);
  397. }
  398. void CDbHandle::addInactiveEndPoint(const string& objName, const Int32 port, const Int32 istcp)
  399. {
  400. #define LOCAL_HOST "127.0.0.1"
  401. ObjectsCache objectsCache;
  402. EndpointF endPoint;
  403. endPoint.host = LOCAL_HOST;
  404. endPoint.port = port;
  405. endPoint.timeout = 30000;
  406. endPoint.istcp = istcp;
  407. //endPoint.setId = setName;
  408. objectsCache[objName].vInactiveEndpoints.push_back(endPoint);
  409. updateInactiveObjectsCache(objectsCache, false);
  410. }
  411. void CDbHandle::cleanEndPoint()
  412. {
  413. ObjectsCache objectsCache;
  414. updateObjectsCache(objectsCache, true);
  415. }
  416. int CDbHandle::getGroupId(const string& ip)
  417. {
  418. map<string, int>& groupIdMap = _groupIdMap.getReaderData();
  419. map<string, int>::iterator it = groupIdMap.find(ip);
  420. if (it != groupIdMap.end())
  421. {
  422. return it->second;
  423. }
  424. uint32_t uip = stringIpToInt(ip);
  425. string ipStar = Ip2StarStr(uip);
  426. it = groupIdMap.find(ipStar);
  427. if (it != groupIdMap.end())
  428. {
  429. return it->second;
  430. }
  431. return -1;
  432. }
  433. uint32_t CDbHandle::stringIpToInt(const std::string& sip)
  434. {
  435. string ip1, ip2, ip3, ip4;
  436. uint32_t dip, p1, p2, p3;
  437. dip = 0;
  438. p1 = sip.find('.');
  439. p2 = sip.find('.', p1 + 1);
  440. p3 = sip.find('.', p2 + 1);
  441. ip1 = sip.substr(0, p1);
  442. ip2 = sip.substr(p1 + 1, p2 - p1 - 1);
  443. ip3 = sip.substr(p2 + 1, p3 - p2 - 1);
  444. ip4 = sip.substr(p3 + 1, sip.size() - p3 - 1);
  445. (((unsigned char *)&dip)[0]) = TC_Common::strto<unsigned int>(ip1);
  446. (((unsigned char *)&dip)[1]) = TC_Common::strto<unsigned int>(ip2);
  447. (((unsigned char *)&dip)[2]) = TC_Common::strto<unsigned int>(ip3);
  448. (((unsigned char *)&dip)[3]) = TC_Common::strto<unsigned int>(ip4);
  449. return htonl(dip);
  450. }
  451. string CDbHandle::Ip2Str(uint32_t ip)
  452. {
  453. char str[50];
  454. unsigned char *p = (unsigned char *)&ip;
  455. sprintf(str, "%u.%u.%u.%u", p[3], p[2], p[1], p[0]);
  456. return string(str);
  457. }
  458. string CDbHandle::Ip2StarStr(uint32_t ip)
  459. {
  460. char str[50];
  461. unsigned char *p = (unsigned char *)&ip;
  462. sprintf(str, "%u.%u.%u.*", p[3], p[2], p[1]);
  463. return string(str);
  464. }
  465. void CDbHandle::InsertSetRecord(const string& objName, const string& setName, const string& setArea, const string& setGroup, EndpointF epf)
  466. {
  467. SetDivisionCache setDivisionCache;
  468. string setId = setName + "." + setArea + "." + setGroup;
  469. SetServerInfo setServerInfo;
  470. setServerInfo.bActive = true;
  471. setServerInfo.epf = epf;
  472. setServerInfo.sSetId = setId;
  473. setServerInfo.sSetArea = setArea;
  474. setDivisionCache[objName][setName].push_back(setServerInfo);
  475. setServerInfo.bActive = false;
  476. setServerInfo.epf.port = 10204;
  477. setDivisionCache[objName][setName].push_back(setServerInfo);
  478. updateDivisionCache(setDivisionCache, true);
  479. }
  480. void CDbHandle::InsertSetRecord4Inactive(const string& objName, const string& setName, const string& setArea, const string& setGroup, EndpointF epf)
  481. {
  482. SetDivisionCache setDivisionCache;
  483. string setId = setName + "." + setArea + "." + setGroup;
  484. SetServerInfo setServerInfo;
  485. setServerInfo.bActive = false;
  486. setServerInfo.epf = epf;
  487. setServerInfo.sSetId = setId;
  488. setServerInfo.sSetArea = setArea;
  489. setDivisionCache[objName][setName].push_back(setServerInfo);
  490. updateDivisionCache(setDivisionCache, false);
  491. }
  492. void CDbHandle::updateDivisionCache(SetDivisionCache& setDivisionCache,bool updateAll)
  493. {
  494. //ȫ������
  495. if(updateAll)
  496. {
  497. if (setDivisionCache.size() == 0)
  498. {
  499. return;
  500. }
  501. SetDivisionCache::iterator it = setDivisionCache.begin();
  502. for(;it != setDivisionCache.end();it++)
  503. {
  504. if(it->second.size() > 0)
  505. {
  506. map<string,vector<CDbHandle::SetServerInfo> >::iterator it_inner = it->second.begin();
  507. for(;it_inner != it->second.end();it_inner++)
  508. {
  509. //updateCpuLoadInfo(it_inner->second);
  510. }
  511. }
  512. }
  513. _setDivisionCache.getWriterData() = setDivisionCache;
  514. _setDivisionCache.swap();
  515. }
  516. else
  517. {
  518. _setDivisionCache.getWriterData() = _setDivisionCache.getReaderData();
  519. SetDivisionCache& tmpsetCache = _setDivisionCache.getWriterData();
  520. SetDivisionCache::const_iterator it = setDivisionCache.begin();
  521. for(;it != setDivisionCache.end();it++)
  522. {
  523. //��set��Ϣ�Ÿ���
  524. if(it->second.size() > 0)
  525. {
  526. tmpsetCache[it->first] = it->second;
  527. }
  528. else if(tmpsetCache.count(it->first))
  529. {
  530. //�����������нڵ㶼û������set��ɾ�������е�set��Ϣ
  531. tmpsetCache.erase(it->first);
  532. }
  533. }
  534. _setDivisionCache.swap();
  535. }
  536. }
  537. #if 0
  538. void CDbHandle::updateCpuLoadInfo(vector<EndpointF> &vEndpointF)
  539. {
  540. CpuLoadCache &cpuLoadCacheMap = _cpuLoadCacheMap.getReaderData();
  541. for(size_t i = 0; i < vEndpointF.size(); ++i)
  542. {
  543. map<string,CpuLoadInfo>::const_iterator const_it_cpu = cpuLoadCacheMap.find(vEndpointF[i].host);
  544. if(const_it_cpu != cpuLoadCacheMap.end())
  545. {
  546. struct tm tb;
  547. int ret = TC_Common::str2tm(const_it_cpu->second.sHeartTime, "%Y-%m-%d %H:%M:%S", tb);
  548. if(ret == 0)
  549. {
  550. vEndpointF[i].cpuload = const_it_cpu->second.iCpuLoad;
  551. vEndpointF[i].sampletime = mktime(&tb);
  552. }
  553. else
  554. {
  555. vEndpointF[i].cpuload = -1;
  556. vEndpointF[i].sampletime = 0;
  557. }
  558. }
  559. else
  560. {
  561. vEndpointF[i].cpuload = -1;
  562. vEndpointF[i].sampletime = 0;
  563. }
  564. }
  565. }
  566. void CDbHandle::updateCpuLoadInfo(vector<CDbHandle::SetServerInfo> &vSetServerInfo)
  567. {
  568. CpuLoadCache &cpuLoadCacheMap = _cpuLoadCacheMap.getReaderData();
  569. for(size_t i = 0; i < vSetServerInfo.size(); ++i)
  570. {
  571. map<string,CpuLoadInfo>::const_iterator const_it_cpu = cpuLoadCacheMap.find(vSetServerInfo[i].epf.host);
  572. if(const_it_cpu != cpuLoadCacheMap.end())
  573. {
  574. struct tm tb;
  575. int ret = TC_Common::str2tm(const_it_cpu->second.sHeartTime, "%Y-%m-%d %H:%M:%S", tb);
  576. if(ret == 0)
  577. {
  578. vSetServerInfo[i].epf.cpuload = const_it_cpu->second.iCpuLoad;
  579. vSetServerInfo[i].epf.sampletime = mktime(&tb);
  580. }
  581. else
  582. {
  583. vSetServerInfo[i].epf.cpuload = -1;
  584. vSetServerInfo[i].epf.sampletime = 0;
  585. }
  586. }
  587. else
  588. {
  589. vSetServerInfo[i].epf.cpuload = -1;
  590. vSetServerInfo[i].epf.sampletime = 0;
  591. }
  592. }
  593. }
  594. #endif