DbHandle.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 
  2. #ifndef __DB_HANDLE_H__
  3. #define __DB_HANDLE_H__
  4. #include "util/tc_common.h"
  5. #include "util/tc_config.h"
  6. #include "util/tc_monitor.h"
  7. #include "util/tc_file.h"
  8. #include "jmem/jmem_hashmap.h"
  9. #include "util/tc_readers_writer_data.h"
  10. #include <set>
  11. #include "servant/RemoteLogger.h"
  12. #include "servant/EndpointF.h"
  13. #include "servant/QueryPushF.h"
  14. #include "RegistryDescriptor.h"
  15. using namespace tars;
  16. //////////////////////////////////////////////////////
  17. //<servant, ObjectItem>
  18. typedef map<string, ObjectItem> ObjectsCache;
  19. //////////////////////////////////////////////////////
  20. /**
  21. * 数据库操作类
  22. */
  23. class CDbHandle
  24. {
  25. private:
  26. struct GroupPriorityEntry
  27. {
  28. std::string sGroupID;
  29. std::string sStation;
  30. std::set<int> setGroupID;
  31. };
  32. // enum GroupUseSelect
  33. // {
  34. // ENUM_USE_WORK_GROUPID,
  35. // ENUM_USE_REAL_GROUPID
  36. // };
  37. //set中服务的信息
  38. struct SetServerInfo
  39. {
  40. string sSetId;
  41. string sSetArea;
  42. bool bActive;
  43. EndpointF epf;
  44. };
  45. //<servant,setname,vector>
  46. typedef map<string,map<string,vector<CDbHandle::SetServerInfo> > > SetDivisionCache;
  47. public:
  48. /**
  49. * 构造函数
  50. */
  51. CDbHandle()
  52. {
  53. }
  54. /**
  55. * 初始化
  56. * @param pconf 配置文件
  57. * @return 0-成功 others-失败
  58. */
  59. int init(TC_Config *pconf);
  60. /** 根据id获取对象
  61. *
  62. * @param id 对象名称
  63. *
  64. * @return 返回所有该对象的活动endpoint列表
  65. */
  66. vector<EndpointF> findObjectById(const string & id);
  67. /** 根据id获取对象
  68. *
  69. * @param id 对象名称
  70. * @out param activeEp 存活的列表
  71. * @out param inactiveEp 非存活的列表
  72. *
  73. * @return 0-成功 others-失败
  74. */
  75. int findObjectById4All(const string & id, vector<EndpointF>& activeEp, vector<EndpointF>& inactiveEp);
  76. /** 根据id获取同组对象
  77. *
  78. * @param id 对象名称
  79. * @param ip
  80. * @out param activeEp 存活的列表
  81. * @out param inactiveEp 非存活的列表
  82. * @out param os 打印日志使用
  83. *
  84. * @return 0-成功 others-失败
  85. */
  86. int findObjectByIdInSameGroup(const string & id, const string & ip, vector<EndpointF>& activeEp, vector<EndpointF>& inactiveEp, ostringstream &os);
  87. /** 根据id获取优先级序列中的对象
  88. *
  89. * @param id 对象名称
  90. * @param ip
  91. * @out param vecActive 存活的列表
  92. * @out param vecInactive 非存活的列表
  93. * @out param os 打印日志使用
  94. *
  95. * @return 0-成功 others-失败
  96. */
  97. int findObjectByIdInGroupPriority(const std::string &sID, const std::string &sIP, std::vector<EndpointF> & vecActive, std::vector<EndpointF> & vecInactive, std::ostringstream & os);
  98. /** 根据id和归属地获取全部对象
  99. *
  100. * @param id 对象名称
  101. * @param sStation 归属地
  102. * @out param vecActive 存活的列表
  103. * @out param vecInactive 非存活的列表
  104. * @out param os 打印日志使用
  105. *
  106. * @return 0-成功 others-失败
  107. */
  108. int findObjectByIdInSameStation(const std::string &sID, const std::string & sStation, std::vector<EndpointF> & vecActive, std::vector<EndpointF> & vecInactive, std::ostringstream & os);
  109. /** 根据id和set信息获取全部对象
  110. *
  111. * @param sID 对象名称
  112. * @param vtSetInfo set信息
  113. * @out param vecActive 存活的列表
  114. * @out param vecInactive 非存活的列表
  115. * @out param os 打印日志使用
  116. *
  117. * @return 0-成功 others-失败
  118. */
  119. int findObjectByIdInSameSet(const string &sID, const vector<string> &vtSetInfo, std::vector<EndpointF> & vecActive, std::vector<EndpointF> & vecInactive, std::ostringstream & os);
  120. /** 根据setId获取全部对象
  121. *
  122. * @param sSetId set名称
  123. * @param vSetServerInfo SetName下部署的服务信息
  124. * @out param vecActive 存活的列表
  125. * @out param vecInactive 非存活的列表
  126. * @out param os 打印日志使用
  127. *
  128. * @return 0-成功 others-失败
  129. */
  130. int findObjectByIdInSameSet(const string &sSetId, const vector<SetServerInfo>& vSetServerInfo, std::vector<EndpointF> & vecActive, std::vector<EndpointF> & vecInactive, std::ostringstream & os);
  131. /**
  132. * 根据ip获取组id
  133. * @return int <0 失败 其它正常
  134. */
  135. int getGroupId(const string& ip);
  136. /**
  137. * ip转换
  138. */
  139. static uint32_t stringIpToInt(const std::string& sip);
  140. /**
  141. * ip转换
  142. */
  143. static string Ip2Str(uint32_t ip);
  144. /**
  145. * ip转换
  146. */
  147. static string Ip2StarStr(uint32_t ip);
  148. static void updateObjectsCache(const ObjectsCache& objCache, bool updateAll);
  149. static void updateActiveObjectsCache(const ObjectsCache& objCache, bool updateAll);
  150. static void updateInactiveObjectsCache(const ObjectsCache& objCache, bool updateAll);
  151. static void updateDivisionCache(SetDivisionCache& setDivisionCache,bool updateAll);
  152. static void updateCpuLoadInfo(vector<EndpointF> &vEndpointF);
  153. static void updateCpuLoadInfo(vector<CDbHandle::SetServerInfo> &vSetServerInfo);
  154. static void InsertSetRecord(const string& objName, const string& setName, const string& setArea, const string& setGroup, EndpointF epf);
  155. static void InsertSetRecord4Inactive(const string& objName, const string& setName, const string& setArea, const string& setGroup, EndpointF epf);
  156. static void addActiveEndPoint(const string& objName, const Int32 port, const Int32 istcp);
  157. static void addInactiveEndPoint(const string& objName, const Int32 port, const Int32 istcp);
  158. static void addEndPointbySet(const string& objName, const Int32 port, const Int32 istcp, const string& setName, const string& setArea, const string& setGroup);
  159. static void addActiveWeight1EndPoint(const string& objName, const Int32 port, const Int32 istcp, const string& setName = "");
  160. static void addInActiveWeight1EndPoint(const string& objName, const Int32 port, const Int32 istcp, const string& setName = "");
  161. static void addActiveWeight2EndPoint(const string& objName, const Int32 port, const Int32 istcp, const string& setName = "");
  162. static void cleanEndPoint();
  163. static void push();
  164. protected:
  165. /**
  166. * 根据group id获取Endpoint
  167. */
  168. vector<EndpointF> getEpsByGroupId(const vector<EndpointF> & vecEps, const GroupUseSelect GroupSelect, int iGroupId, ostringstream &os);
  169. vector<EndpointF> getEpsByGroupId(const vector<EndpointF> & vecEps, const GroupUseSelect GroupSelect, const set<int> & setGroupID, ostringstream & os);
  170. protected:
  171. static TC_ReadersWriterData<ObjectsCache> _objectsCache;
  172. //set划分缓存
  173. static TC_ReadersWriterData<SetDivisionCache> _setDivisionCache;
  174. //优先级的序列
  175. static TC_ReadersWriterData<std::map<int, GroupPriorityEntry> > _mapGroupPriority;
  176. //分组信息
  177. static TC_ReadersWriterData<map<string,int> > _groupIdMap;
  178. static TC_ReadersWriterData<map<string,int> > _groupNameMap;
  179. };
  180. #endif