DbHandle.h 7.0 KB

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