EndpointManager.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_port.h"
  17. #include "servant/EndpointManager.h"
  18. #include "servant/ObjectProxy.h"
  19. #include "servant/RemoteLogger.h"
  20. #include "servant/AppCache.h"
  21. #include "servant/Application.h"
  22. #include "servant/CommunicatorEpoll.h"
  23. namespace tars
  24. {
  25. std::mutex QueryEpBase::_mutex;
  26. QueryPushFImpPtr QueryEpBase::_queryCallback;
  27. void QueryPushFImp::replacePrx(QueryFPrx queryFPrx)
  28. {
  29. std::lock_guard<std::mutex> lock(_mutex);
  30. _queryFPrx = queryFPrx;
  31. for(auto it : _queryBase)
  32. {
  33. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_registerQuery(NULL, it.first, _queryFPrx->tars_communicator()->clientConfig().ModuleName);
  34. }
  35. }
  36. void QueryPushFImp::registerQuery(const string &obj, QueryEpBase *pQueryBase)
  37. {
  38. {
  39. std::lock_guard<std::mutex> lock(_mutex);
  40. _queryBase[obj].insert(pQueryBase);
  41. }
  42. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_registerQuery(NULL, obj, _queryFPrx->tars_communicator()->clientConfig().ModuleName);
  43. }
  44. void QueryPushFImp::onConnect(const TC_Endpoint& ep)
  45. {
  46. std::lock_guard<std::mutex> lock(_mutex);
  47. for(auto it : _queryBase)
  48. {
  49. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_registerQuery(NULL, it.first, _queryFPrx->tars_communicator()->clientConfig().ModuleName);
  50. }
  51. }
  52. void QueryPushFImp::callback_onQuery(const std::string& obj)
  53. {
  54. // LOG_CONSOLE_DEBUG << obj << endl;
  55. std::lock_guard<std::mutex> lock(_mutex);
  56. auto it = _queryBase.find(obj);
  57. if(it != _queryBase.end())
  58. {
  59. for(auto &e : it->second)
  60. {
  61. e->resetRefreshTime();
  62. }
  63. }
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. QueryEpBase::QueryEpBase(Communicator * pComm, bool bFirstNetThread,bool bInterfaceReq)
  67. : _communicator(pComm)
  68. , _firstNetThread(bFirstNetThread)
  69. , _interfaceReq(bInterfaceReq)
  70. , _direct(false)
  71. , _objName("")
  72. , _invokeSetId("")
  73. , _locator("")
  74. , _valid(false)
  75. , _weightType(E_LOOP)
  76. , _rootServant(true)
  77. , _requestRegistry(false)
  78. , _requestTimeout(0)
  79. , _timeoutInterval(5*1000)
  80. , _refreshTime(0)
  81. , _refreshInterval(60*1000)
  82. , _activeEmptyInterval(10*1000)
  83. , _failInterval(2*1000)
  84. , _manyFailInterval(30*1000)
  85. , _failTimesLimit(3)
  86. , _failTimes(0)
  87. {
  88. _refreshInterval = TC_Common::strto<int>(_communicator->getProperty("refresh-endpoint-interval", "60*1000"));
  89. if(_refreshInterval < 5*1000)
  90. {
  91. _refreshInterval = 5 * 1000;
  92. }
  93. setNoDelete(true);
  94. }
  95. void QueryEpBase::callback_findObjectById4All(Int32 ret, const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp)
  96. {
  97. TLOGTARS("[callback_findObjectById4All _objName:" << _objName << "|ret:" << ret
  98. << ",active:" << activeEp.size()
  99. << ",inactive:" << inactiveEp.size() << "]" << endl);
  100. doEndpoints(activeEp,inactiveEp,ret);
  101. }
  102. void QueryEpBase::callback_findObjectById4All_exception(Int32 ret)
  103. {
  104. TLOGERROR("[callback_findObjectById4All_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
  105. doEndpointsExp(ret);
  106. }
  107. void QueryEpBase::callback_findObjectById4Any(Int32 ret, const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp)
  108. {
  109. TLOGTARS("[callback_findObjectById4Any _objName:" << _objName << "|ret:" << ret
  110. << ",active:" << activeEp.size()
  111. << ",inactive:" << inactiveEp.size() << "]" << endl);
  112. doEndpoints(activeEp,inactiveEp,ret);
  113. }
  114. void QueryEpBase::callback_findObjectById4Any_exception(Int32 ret)
  115. {
  116. TLOGERROR("[callback_findObjectById4Any_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
  117. doEndpointsExp(ret);
  118. }
  119. void QueryEpBase::callback_findObjectByIdInSameGroup(Int32 ret, const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp)
  120. {
  121. TLOGTARS("[callback_findObjectByIdInSameGroup _objName:" << _objName << "|ret:"<<ret
  122. << ",active:" << activeEp.size()
  123. << ",inactive:" << inactiveEp.size() << "]" << endl);
  124. doEndpoints(activeEp,inactiveEp,ret);
  125. }
  126. void QueryEpBase::callback_findObjectByIdInSameGroup_exception(Int32 ret)
  127. {
  128. TLOGERROR("[callback_findObjectByIdInSameGroup_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
  129. doEndpointsExp(ret);
  130. }
  131. void QueryEpBase::callback_findObjectByIdInSameSet( Int32 ret, const vector<EndpointF> &activeEp, const vector<EndpointF> & inactiveEp)
  132. {
  133. TLOGTARS("[callback_findObjectByIdInSameSet _objName:" << _objName << "|ret:" << ret
  134. << ",active:" << activeEp.size()
  135. << ",inactive:" << inactiveEp.size() << "]" << endl);
  136. doEndpoints(activeEp,inactiveEp,ret);
  137. }
  138. void QueryEpBase::callback_findObjectByIdInSameSet_exception( Int32 ret)
  139. {
  140. TLOGERROR("[callback_findObjectByIdInSameSet_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
  141. doEndpointsExp(ret);
  142. }
  143. void QueryEpBase::callback_findObjectByIdInSameStation( Int32 ret, const vector<EndpointF> &activeEp, const vector<EndpointF> &inactiveEp)
  144. {
  145. TLOGTARS("[callback_findObjectByIdInSameStation _objName:" << _objName << "|ret:" << ret
  146. << ",active:" << activeEp.size()
  147. << ",inactive:" << inactiveEp.size() << "]" << endl);
  148. doEndpoints(activeEp,inactiveEp,ret);
  149. }
  150. void QueryEpBase::callback_findObjectByIdInSameStation_exception( Int32 ret)
  151. {
  152. TLOGERROR("[callback_findObjectByIdInSameStation_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
  153. doEndpointsExp(ret);
  154. }
  155. int QueryEpBase::setLocatorPrx(QueryFPrx prx)
  156. {
  157. std::lock_guard<std::mutex> lock(_mutex);
  158. if(_queryFPrx && _queryFPrx.get() != prx.get())
  159. {
  160. prx->tars_set_push_callback(_queryFPrx->tars_get_push_callback());
  161. }
  162. _queryFPrx = prx;
  163. return 0;
  164. }
  165. void QueryEpBase::resetRefreshTime()
  166. {
  167. _refreshTime = 0;
  168. }
  169. bool QueryEpBase::init(const string & sObjName, const string& setName, bool rootServant)
  170. {
  171. _locator = _communicator->getProperty("locator");
  172. TLOGTARS("QueryEpBase::init sObjName:" << sObjName << ", sLocator:" << _locator << ", setName:" << setName << ", rootServant: " << rootServant << endl);
  173. _invokeSetId = setName;
  174. _rootServant = rootServant;
  175. setObjName(sObjName);
  176. return true;
  177. }
  178. void QueryEpBase::setObjName(const string & sObjName)
  179. {
  180. string::size_type pos = sObjName.find_first_of('@');
  181. string sEndpoints;
  182. string sInactiveEndpoints;
  183. if (pos != string::npos)
  184. {
  185. //[直接连接]指定服务的IP和端口列表
  186. _objName = sObjName.substr(0,pos);
  187. sEndpoints = sObjName.substr(pos + 1);
  188. pos = _objName.find_first_of("#");
  189. if(pos != string::npos)
  190. {
  191. _objName = _objName.substr(0, pos);
  192. }
  193. _direct = true;
  194. _valid = true;
  195. }
  196. else
  197. {
  198. //[间接连接]通过registry查询服务端的IP和端口列表
  199. _direct = false;
  200. _valid = false;
  201. _objName = sObjName;
  202. if(_locator.find_first_not_of('@') == string::npos)
  203. {
  204. TLOGERROR("[QueryEpBase::setObjName locator is not valid,_locator:" << _locator << "]" << endl);
  205. throw TarsRegistryException("locator is not valid,_locator:" + _locator);
  206. }
  207. pos = _objName.find_first_of("#");
  208. if(pos != string::npos)
  209. {
  210. _objName = _objName.substr(0, pos);
  211. }
  212. _queryFPrx = _communicator->stringToProxy<QueryFPrx>(_locator);
  213. if(this->_communicator->getProperty("open-query-push", "n") == "y")
  214. {
  215. {
  216. std::lock_guard<std::mutex> lock(_mutex);
  217. if (!_queryCallback)
  218. {
  219. _queryCallback = new QueryPushFImp(_queryFPrx);
  220. _queryFPrx->tars_set_push_callback(_queryCallback);
  221. }
  222. }
  223. _queryCallback->registerQuery(_objName, this);
  224. }
  225. string sLocatorKey = _locator;
  226. //如果启用set,则获取按set分组的缓存
  227. if(_communicator->clientConfig().SetOpen)
  228. {
  229. sLocatorKey += "_" + _communicator->clientConfig().SetDivision;
  230. }
  231. string objName = _objName + string(_invokeSetId.empty() ? "" : ":") + _invokeSetId;
  232. //[间接连接]第一次使用cache,如果是接口级请求则不从缓存读取
  233. if(!_interfaceReq)
  234. {
  235. sEndpoints = _communicator->getAppCache()->get(objName,sLocatorKey);
  236. sInactiveEndpoints = _communicator->getAppCache()->get("inactive_"+objName,sLocatorKey);
  237. }
  238. }
  239. setEndpoints(sEndpoints,_activeEndpoints);
  240. setEndpoints(sInactiveEndpoints,_inactiveEndpoints);
  241. if(!_activeEndpoints.empty())
  242. {
  243. _valid = true;
  244. }
  245. if((!_activeEndpoints.empty() || !_inactiveEndpoints.empty()))
  246. {
  247. //非直接指定端口, 且从cache中能查到服务端口的, 不需要通知所有ObjectProxy更新地址
  248. notifyEndpoints(_activeEndpoints,_inactiveEndpoints,true);
  249. }
  250. }
  251. void QueryEpBase::setEndpoints(const string & sEndpoints, set<EndpointInfo> & setEndpoints)
  252. {
  253. if(sEndpoints == "")
  254. {
  255. return ;
  256. }
  257. bool bSameWeightType = true;
  258. bool bFirstWeightType = true;
  259. unsigned int iWeightType = 0;
  260. vector<string> vEndpoints = TC_Endpoint::sepEndpoint(sEndpoints);
  261. for (size_t i = 0; i < vEndpoints.size(); ++i)
  262. {
  263. try
  264. {
  265. TC_Endpoint ep(vEndpoints[i]);
  266. string sSetDivision;
  267. //解析set分组信息
  268. if (!_direct)
  269. {
  270. string sep = " -s ";
  271. size_t pos = vEndpoints[i].rfind(sep);
  272. if (pos != string::npos)
  273. {
  274. sSetDivision = TC_Common::trim(vEndpoints[i].substr(pos+sep.size()));
  275. size_t endPos = sSetDivision.find(" ");
  276. if (endPos != string::npos)
  277. {
  278. sSetDivision = sSetDivision.substr(0, endPos);
  279. }
  280. }
  281. }
  282. if(bFirstWeightType)
  283. {
  284. bFirstWeightType = false;
  285. iWeightType = ep.getWeightType();
  286. }
  287. else
  288. {
  289. if(ep.getWeightType() != iWeightType)
  290. {
  291. bSameWeightType = false;
  292. }
  293. }
  294. EndpointInfo epi(ep, sSetDivision);
  295. setEndpoints.insert(epi);
  296. }
  297. catch (exception &ex)
  298. {
  299. TLOGERROR("[QueryEpBase::setEndpoints parse error,objname:" << _objName << ",endpoint:" << vEndpoints[i] << "]" << endl);
  300. }
  301. }
  302. if(bSameWeightType)
  303. {
  304. if(iWeightType == 1)
  305. {
  306. _weightType = E_STATIC_WEIGHT;
  307. }
  308. else
  309. {
  310. _weightType = E_LOOP;
  311. }
  312. }
  313. else
  314. {
  315. _weightType = E_LOOP;
  316. }
  317. }
  318. void QueryEpBase::refreshReg(GetEndpointType type, const string & sName)
  319. {
  320. onUpdateOutter();
  321. if(_direct)
  322. {
  323. return;
  324. }
  325. // LOG_CONSOLE_DEBUG << this->_objName << ", _requestRegistry:" << _requestRegistry << ", _requestTimeout:" << _requestTimeout << ", _refreshTime:" << _refreshTime << endl;
  326. int64_t iNow = TNOWMS;
  327. //正在请求状态 而且请求超时了,或者第一次
  328. if(_requestRegistry && _requestTimeout < iNow)
  329. {
  330. doEndpointsExp(0);
  331. }
  332. //如果是间接连接,通过registry定时查询服务列表
  333. //正在请求状态 而且请求超时了
  334. //非请求状态 到了下一个刷新时间了
  335. if( (!_requestRegistry) && (_refreshTime <= iNow))
  336. {
  337. _requestRegistry = true;
  338. //一定时间不回调就算超时了
  339. _requestTimeout = iNow + _timeoutInterval;
  340. TLOGTARS("[QueryEpBase::refresh," << _objName << "]" <<endl);
  341. if(_valid && !_rootServant && _valid)
  342. {
  343. return;
  344. }
  345. //判断是同步调用还是异步调用
  346. //内部请求主控都是异步请求
  347. //接口请求主控第一次是同步请求
  348. bool bSync = (!_valid && _interfaceReq);
  349. //如果是异步且不是根servant(通过#1创建的servant, 不主动更新主控信息)
  350. if(!bSync && !_rootServant)
  351. return;
  352. try
  353. {
  354. if(bSync)
  355. {
  356. vector<EndpointF> activeEp;
  357. vector<EndpointF> inactiveEp;
  358. int iRet = 0;
  359. switch(type)
  360. {
  361. case E_ALL:
  362. {
  363. iRet = _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->findObjectById4Any(_objName,activeEp,inactiveEp, ServerConfig::Context);
  364. break;
  365. }
  366. case E_STATION:
  367. {
  368. iRet = _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->findObjectByIdInSameStation(_objName,sName,activeEp,inactiveEp, ServerConfig::Context);
  369. break;
  370. }
  371. case E_SET:
  372. {
  373. iRet = _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->findObjectByIdInSameSet(_objName,sName,activeEp,inactiveEp, ServerConfig::Context);
  374. break;
  375. }
  376. case E_DEFAULT:
  377. default:
  378. {
  379. if(_communicator->clientConfig().SetOpen || !_invokeSetId.empty())
  380. {
  381. //指定set调用时,指定set的优先级最高
  382. string setId = _invokeSetId.empty()?_communicator->clientConfig().SetDivision : _invokeSetId;
  383. iRet = _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->findObjectByIdInSameSet(_objName,setId,activeEp,inactiveEp, ServerConfig::Context);
  384. }
  385. else
  386. {
  387. iRet = _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->findObjectByIdInSameGroup(_objName,activeEp,inactiveEp, ServerConfig::Context);
  388. }
  389. break;
  390. }
  391. }
  392. doEndpoints(activeEp, inactiveEp, iRet, true);
  393. }
  394. else
  395. {
  396. switch(type)
  397. {
  398. case E_ALL:
  399. {
  400. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_findObjectById4Any(this,_objName, ServerConfig::Context);
  401. break;
  402. }
  403. case E_STATION:
  404. {
  405. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_findObjectByIdInSameStation(this,_objName,sName, ServerConfig::Context);
  406. break;
  407. }
  408. case E_SET:
  409. {
  410. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_findObjectByIdInSameSet(this,_objName,sName, ServerConfig::Context);
  411. break;
  412. }
  413. case E_DEFAULT:
  414. default:
  415. {
  416. if(_communicator->clientConfig().SetOpen || !_invokeSetId.empty())
  417. {
  418. //指定set调用时,指定set的优先级最高
  419. string setId = _invokeSetId.empty()?_communicator->clientConfig().SetDivision:_invokeSetId;
  420. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_findObjectByIdInSameSet(this,_objName,setId, ServerConfig::Context);
  421. }
  422. else
  423. {
  424. _queryFPrx->tars_hash((uint64_t)_queryFPrx.get())->async_findObjectByIdInSameGroup(this,_objName, ServerConfig::Context);
  425. }
  426. break;
  427. }
  428. }//end switch
  429. }
  430. }
  431. catch(TC_Exception & ex)
  432. {
  433. TLOGERROR("[QueryEpBase::refreshReg obj:"<<_objName<<"exception:"<<ex.what() << "]"<<endl);
  434. doEndpointsExp(TARSSERVERUNKNOWNERR);
  435. }
  436. catch(...)
  437. {
  438. TLOGERROR("[QueryEpBase::refreshReg obj:"<<_objName<<"unknown exception]" <<endl);
  439. doEndpointsExp(TARSSERVERUNKNOWNERR);
  440. }
  441. }
  442. }
  443. void QueryEpBase::doEndpoints(const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp, int iRet, bool bSync)
  444. {
  445. if(iRet != 0)
  446. {
  447. doEndpointsExp(iRet);
  448. return ;
  449. }
  450. _failTimes = 0;
  451. _requestRegistry = false;
  452. int64_t iNow = TNOWMS;
  453. //有返回成功的结点,按照正常的频率
  454. //如果返回空列表或者返回失败 2s去刷新一次
  455. //接口请求主控的方式 不管是不是空都要去刷新
  456. if(activeEp.empty() && (!_interfaceReq) )
  457. {
  458. _refreshTime = iNow + _activeEmptyInterval;
  459. //如果registry返回Active服务列表为空,不做更新
  460. TLOGERROR("[QueryEpBase::doEndpoints, callback activeEps is empty,objname:"<< _objName << "]" << endl);
  461. return;
  462. }
  463. else
  464. {
  465. _refreshTime = iNow + _refreshInterval;
  466. }
  467. bool bNeedNotify = false;
  468. bool bSameWeightType = true;
  469. bool bFirstWeightType = true;
  470. int iWeightType = 0;
  471. set<string> sActiveEndpoints;
  472. set<string> sInactiveEndpoints;
  473. set<EndpointInfo> activeEps;
  474. set<EndpointInfo> inactiveEps;
  475. //生成active set 用于比较
  476. for (uint32_t i = 0; i < activeEp.size(); ++i)
  477. {
  478. if(bFirstWeightType)
  479. {
  480. bFirstWeightType = false;
  481. iWeightType = activeEp[i].weightType;
  482. }
  483. else
  484. {
  485. if(activeEp[i].weightType != iWeightType)
  486. {
  487. bSameWeightType = false;
  488. }
  489. }
  490. // taf istcp意思和这里枚举值对应
  491. activeEps.insert(EndpointInfo(activeEp[i]));
  492. }
  493. //生成inactive set 用于比较
  494. for (uint32_t i = 0; i < inactiveEp.size(); ++i)
  495. {
  496. // taf istcp意思和这里枚举值对应
  497. inactiveEps.insert(EndpointInfo(inactiveEp[i]));
  498. }
  499. if(bSameWeightType)
  500. {
  501. if(iWeightType == 1)
  502. {
  503. _weightType = E_STATIC_WEIGHT;
  504. }
  505. else
  506. {
  507. _weightType = E_LOOP;
  508. }
  509. }
  510. else
  511. {
  512. _weightType = E_LOOP;
  513. }
  514. if(activeEps != _activeEndpoints)
  515. {
  516. bNeedNotify = true;
  517. _activeEndpoints = activeEps;
  518. if(_firstNetThread)
  519. {
  520. setEndPointToCache(false);
  521. }
  522. }
  523. if(inactiveEps != _inactiveEndpoints)
  524. {
  525. bNeedNotify = true;
  526. _inactiveEndpoints = inactiveEps;
  527. if(_firstNetThread)
  528. {
  529. setEndPointToCache(true);
  530. }
  531. }
  532. if(bNeedNotify)
  533. {
  534. notifyEndpoints(_activeEndpoints,_inactiveEndpoints,bSync);
  535. }
  536. if(!_valid)
  537. {
  538. _valid = true;
  539. doNotify();
  540. }
  541. }
  542. void QueryEpBase::doEndpointsExp(int iRet)
  543. {
  544. _failTimes++;
  545. _requestRegistry = false;
  546. int64_t iNow = TNOWMS;
  547. //频率控制获取主控失败 2秒钟再更新
  548. _refreshTime = iNow + _failInterval;
  549. //获取主控连续失败3次就等30s再更新一次
  550. //连续失败 强制设成数据是有效的
  551. if(_failTimes > _failTimesLimit)
  552. {
  553. if(!_valid)
  554. {
  555. _valid = true;
  556. doNotify();
  557. }
  558. _refreshTime = iNow + _manyFailInterval;
  559. }
  560. }
  561. void QueryEpBase::setEndPointToCache(bool bInactive)
  562. {
  563. //如果是接口级请求则不缓存到文件
  564. if(_interfaceReq)
  565. {
  566. return;
  567. }
  568. string sEndpoints;
  569. set<EndpointInfo> doEndpoints;
  570. if(!bInactive)
  571. {
  572. doEndpoints = _activeEndpoints;
  573. }
  574. else
  575. {
  576. doEndpoints = _inactiveEndpoints;
  577. }
  578. set<EndpointInfo>::iterator iter;
  579. iter = doEndpoints.begin();
  580. for (; iter != doEndpoints.end(); ++iter)
  581. {
  582. //这里的超时时间 只是对服务端有效。这里的值无效。所以默认用3000了
  583. TC_Endpoint ep = iter->getEndpoint();
  584. if (!sEndpoints.empty())
  585. {
  586. sEndpoints += ":";
  587. }
  588. sEndpoints += ep.toString();
  589. if (!iter->setDivision().empty())
  590. {
  591. sEndpoints += " -s " + iter->setDivision();
  592. }
  593. }
  594. //如果启用set,则按set分组保存
  595. string sLocatorKey = _locator;
  596. if(_communicator->clientConfig().SetOpen)
  597. {
  598. sLocatorKey += "_" + _communicator->clientConfig().SetDivision;
  599. }
  600. string objName = _objName + string(_invokeSetId.empty()?"":":") + _invokeSetId;
  601. if(bInactive)
  602. {
  603. _communicator->getAppCache()->set("inactive_"+objName,sEndpoints,sLocatorKey);
  604. }
  605. else
  606. {
  607. _communicator->getAppCache()->set(objName,sEndpoints,sLocatorKey);
  608. }
  609. TLOGTARS("[setEndPointToCache,obj:" << _objName << ",invokeSetId:" << _invokeSetId << ",endpoint:" << sEndpoints << "]" << endl);
  610. }
  611. EndpointManager::EndpointManager(ObjectProxy * pObjectProxy, Communicator* pComm, bool bFirstNetThread)
  612. : QueryEpBase(pComm, bFirstNetThread, false)
  613. ,_objectProxy(pObjectProxy)
  614. ,_lastRoundPosition(0)
  615. ,_update(true)
  616. ,_updateWeightInterval(60)
  617. ,_lastSWeightPosition(0)
  618. ,_consistentHashWeight(E_TC_CONHASH_KETAMAHASH)
  619. ,_consistentHash(E_TC_CONHASH_KETAMAHASH)
  620. {
  621. setNetThreadProcess(true);
  622. }
  623. EndpointManager::~EndpointManager()
  624. {
  625. map<string,AdapterProxy*>::iterator iterAdapter;
  626. for(iterAdapter = _allProxys.begin();iterAdapter != _allProxys.end();iterAdapter++)
  627. {
  628. if(iterAdapter->second)
  629. {
  630. delete iterAdapter->second;
  631. iterAdapter->second = NULL;
  632. }
  633. }
  634. _allProxys.clear();
  635. }
  636. void EndpointManager::onUpdateOutter()
  637. {
  638. // LOG_CONSOLE_DEBUG << this->_objectProxy << ", valid:" << _valid << ", " << _outterUpdate.get() << endl;
  639. assert(this->_objectProxy->getCommunicatorEpoll()->getThreadId() == this_thread::get_id());
  640. lock_guard<mutex> l(_outterLocker);
  641. if(_outterUpdate)
  642. {
  643. shared_ptr<OutterUpdate> outterUpdate = _outterUpdate;
  644. updateEndpoints(outterUpdate->active, outterUpdate->inactive);
  645. _valid = true;
  646. _outterUpdate.reset();
  647. }
  648. }
  649. void EndpointManager::updateEndpointsOutter(const set<EndpointInfo> & active, const set<EndpointInfo> & inactive)
  650. {
  651. // LOG_CONSOLE_DEBUG << this->_objectProxy << ", " << active.begin()->desc() << endl;
  652. //创新新对象, 避免线程冲突
  653. shared_ptr<OutterUpdate> outterUpdate = std::make_shared<OutterUpdate>();
  654. outterUpdate->active = active;
  655. outterUpdate->inactive = inactive;
  656. //更新时间
  657. _refreshTime = TNOWMS + _refreshInterval;
  658. lock_guard<mutex> l(_outterLocker);
  659. _outterUpdate = outterUpdate;
  660. }
  661. void EndpointManager::updateEndpoints(const set<EndpointInfo> & active, const set<EndpointInfo> & inactive)
  662. {
  663. TLOGTARS("[EndpointManager::updateEndpoints obj:" << this->_objName << ", active:" << active.size() << ", inactive size:" << inactive.size() << endl);
  664. pair<map<string,AdapterProxy*>::iterator,bool> result;
  665. _activeProxys.clear();
  666. _regProxys.clear();
  667. _indexActiveProxys.clear();
  668. _sortActivProxys.clear();
  669. if(!active.empty())
  670. {
  671. //先把服务都设置为非活跃
  672. for (auto iter = _allProxys.begin(); iter != _allProxys.end(); ++iter)
  673. {
  674. iter->second->setActiveInReg(false);
  675. }
  676. }
  677. //更新active
  678. for(auto iter = active.begin(); iter != active.end(); ++iter)
  679. {
  680. if(!_direct && _weightType == E_STATIC_WEIGHT && iter->weight() <= 0)
  681. {
  682. continue;
  683. }
  684. // LOG_CONSOLE_DEBUG << std::this_thread::get_id() << ", allProxys size:" << _allProxys.size() << ", " << iter->cmpDesc() << endl;
  685. auto iterAdapter = _allProxys.find(iter->cmpDesc());
  686. if(iterAdapter == _allProxys.end())
  687. {
  688. AdapterProxy* ap = new AdapterProxy(_objectProxy, *iter, _communicator);
  689. result = _allProxys.insert(make_pair(iter->cmpDesc(),ap));
  690. iterAdapter = result.first;
  691. _vAllProxys.push_back(ap);
  692. }
  693. //该节点在主控的状态为active
  694. iterAdapter->second->setActiveInReg(true);
  695. _activeProxys.push_back(iterAdapter->second);
  696. _regProxys.insert(make_pair(iter->cmpDesc(),iterAdapter->second));
  697. const string &host = iterAdapter->second->endpoint().host();
  698. _indexActiveProxys.insert(make_pair(host, iterAdapter->second));
  699. _sortActivProxys.insert(make_pair(host, iterAdapter->second));
  700. //设置该节点的静态权重值
  701. iterAdapter->second->setWeight(iter->weight());
  702. }
  703. //更新inactive
  704. for(auto iter = inactive.begin(); iter != inactive.end(); ++iter)
  705. {
  706. if(!_direct && _weightType == E_STATIC_WEIGHT && iter->weight() <= 0)
  707. {
  708. continue;
  709. }
  710. auto iterAdapter = _allProxys.find(iter->cmpDesc());
  711. if(iterAdapter == _allProxys.end())
  712. {
  713. AdapterProxy* ap = new AdapterProxy(_objectProxy, *iter, _communicator);
  714. result = _allProxys.insert(make_pair(iter->cmpDesc(),ap));
  715. assert(result.second);
  716. iterAdapter = result.first;
  717. _vAllProxys.push_back(ap);
  718. }
  719. //该节点在主控的状态为inactive
  720. iterAdapter->second->setActiveInReg(false);
  721. _regProxys.insert(make_pair(iter->cmpDesc(),iterAdapter->second));
  722. //设置该节点的静态权重值
  723. iterAdapter->second->setWeight(iter->weight());
  724. }
  725. //_vRegProxys 需要按顺序来 重排
  726. _vRegProxys.clear();
  727. auto iterAdapter = _regProxys.begin();
  728. for(;iterAdapter != _regProxys.end();++iterAdapter)
  729. {
  730. _vRegProxys.push_back(iterAdapter->second);
  731. }
  732. _update = true;
  733. }
  734. void EndpointManager::notifyEndpoints(const set<EndpointInfo> & active,const set<EndpointInfo> & inactive,bool bNotify)
  735. {
  736. updateEndpoints(active, inactive);
  737. //丢给外层统一做
  738. _objectProxy->onNotifyEndpoints(active, inactive);
  739. }
  740. void EndpointManager::doNotify()
  741. {
  742. _objectProxy->doInvoke();
  743. }
  744. bool EndpointManager::selectAdapterProxy(ReqMessage * msg,AdapterProxy * & pAdapterProxy)
  745. {
  746. pAdapterProxy = NULL;
  747. //刷新主控
  748. refreshReg(E_DEFAULT, "");
  749. //无效的数据 返回true
  750. if (!_valid)
  751. {
  752. return true;
  753. }
  754. //如果有hash,则先使用hash策略
  755. if (msg->data._hash)
  756. {
  757. pAdapterProxy = getHashProxy(msg->data._hashCode, msg->data._conHash);
  758. return false;
  759. }
  760. if(_weightType == E_STATIC_WEIGHT)
  761. {
  762. //权重模式
  763. bool bStaticWeighted = false;
  764. if(_weightType == E_STATIC_WEIGHT || msg->eType == ReqMessage::ONE_WAY)
  765. bStaticWeighted = true;
  766. pAdapterProxy = getWeightedProxy(bStaticWeighted);
  767. }
  768. else
  769. {
  770. //普通轮询模式
  771. pAdapterProxy = getNextValidProxy();
  772. }
  773. return false;
  774. }
  775. AdapterProxy * EndpointManager::getNextValidProxy()
  776. {
  777. if (_activeProxys.empty())
  778. {
  779. TLOGERROR("[EndpointManager::getNextValidProxy activeEndpoints is empty][obj:"<<_objName<<"]" << endl);
  780. return NULL;
  781. }
  782. vector<AdapterProxy*> conn;
  783. for(size_t i=0;i<_activeProxys.size();i++)
  784. {
  785. ++_lastRoundPosition;
  786. if(_lastRoundPosition >= _activeProxys.size())
  787. {
  788. _lastRoundPosition = 0;
  789. }
  790. if(_activeProxys[_lastRoundPosition]->checkActive(false))
  791. {
  792. return _activeProxys[_lastRoundPosition];
  793. }
  794. if(!_activeProxys[_lastRoundPosition]->isConnTimeout() && !_activeProxys[_lastRoundPosition]->isConnExc()) {
  795. conn.push_back(_activeProxys[_lastRoundPosition]);
  796. }
  797. }
  798. if(conn.size() > 0)
  799. {
  800. //都有问题, 随机选择一个没有connect超时或者链接异常的发送
  801. AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())];
  802. //该proxy可能已经被屏蔽,需重新连一次
  803. adapterProxy->checkActive(true);
  804. return adapterProxy;
  805. }
  806. //所有adapter都有问题 选不到结点,随机找一个重试
  807. AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
  808. adapterProxy->resetRetryTime(false);
  809. //该proxy可能已经被屏蔽,需重新连一次
  810. adapterProxy->checkActive(true);
  811. return adapterProxy;
  812. }
  813. AdapterProxy* EndpointManager::getHashProxy(uint32_t hashCode, bool bConsistentHash)
  814. {
  815. if(_weightType == E_STATIC_WEIGHT)
  816. {
  817. if(bConsistentHash)
  818. {
  819. return getConHashProxyForWeight(hashCode, true);
  820. }
  821. else
  822. {
  823. return getHashProxyForWeight(hashCode, true, _hashStaticRouterCache);
  824. }
  825. }
  826. else
  827. {
  828. if(bConsistentHash)
  829. {
  830. return getConHashProxyForNormal(hashCode);
  831. }
  832. else
  833. {
  834. return getHashProxyForNormal(hashCode);
  835. }
  836. }
  837. }
  838. AdapterProxy* EndpointManager::getHashProxyForWeight(uint32_t hashCode, bool bStatic, vector<size_t> &vRouterCache)
  839. {
  840. if(_vRegProxys.empty())
  841. {
  842. TLOGERROR("[EndpointManager::getHashProxyForWeight _vRegProxys is empty], bStatic:" << bStatic << endl);
  843. return NULL;
  844. }
  845. if(checkHashStaticWeightChange(bStatic))
  846. {
  847. int64_t iBegin = TNOWMS;
  848. updateHashProxyWeighted(bStatic);
  849. int64_t iEnd = TNOWMS;
  850. TLOGTARS("[EndpointManager::getHashProxyForWeight update bStatic:" << bStatic << "|_objName:" << _objName << "|timecost(ms):" << (iEnd - iBegin) << endl);
  851. }
  852. if(vRouterCache.size() > 0)
  853. {
  854. size_t hash = hashCode % vRouterCache.size();
  855. //这里做判断的原因是:32位系统下,如果hashCode为负值,hash经过上面的计算会是一个超大值,导致越界
  856. if(hash >= vRouterCache.size())
  857. {
  858. hash = hash % vRouterCache.size();
  859. }
  860. size_t iIndex = vRouterCache[hash];
  861. if(iIndex >= _vRegProxys.size())
  862. {
  863. iIndex = iIndex % _vRegProxys.size();
  864. }
  865. //被hash到的节点在主控是active的才走在流程
  866. if (_vRegProxys[iIndex]->isActiveInReg() && _vRegProxys[iIndex]->checkActive(true))
  867. {
  868. return _vRegProxys[iIndex];
  869. }
  870. else
  871. {
  872. TLOGWARN("[EndpointManager::getHashProxyForWeight, hash not active," << _objectProxy->name() << "@" << _vRegProxys[iIndex]->endpoint().desc() << endl);
  873. if(_activeProxys.empty())
  874. {
  875. TLOGERROR("[EndpointManager::getHashProxyForWeight _activeEndpoints is empty], bStatic:" << bStatic << endl);
  876. return NULL;
  877. }
  878. //在active节点中再次hash
  879. vector<AdapterProxy*> thisHash = _activeProxys;
  880. vector<AdapterProxy*> conn;
  881. do
  882. {
  883. hash = hashCode % thisHash.size();
  884. if (thisHash[hash]->checkActive(true))
  885. {
  886. return thisHash[hash];
  887. }
  888. if(!thisHash[hash]->isConnTimeout() &&
  889. !thisHash[hash]->isConnExc())
  890. {
  891. conn.push_back(thisHash[hash]);
  892. }
  893. thisHash.erase(thisHash.begin() + hash);
  894. }
  895. while(!thisHash.empty());
  896. if(conn.size() > 0)
  897. {
  898. hash = hashCode % conn.size();
  899. //都有问题, 随机选择一个没有connect超时或者链接异常的发送
  900. AdapterProxy *adapterProxy = conn[hash];
  901. //该proxy可能已经被屏蔽,需重新连一次
  902. adapterProxy->checkActive(true);
  903. return adapterProxy;
  904. }
  905. //所有adapter都有问题 选不到结点,随机找一个重试
  906. AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
  907. adapterProxy->resetRetryTime(false);
  908. //该proxy可能已经被屏蔽,需重新连一次
  909. adapterProxy->checkActive(true);
  910. return adapterProxy;
  911. }
  912. }
  913. return getHashProxyForNormal(hashCode);
  914. }
  915. AdapterProxy* EndpointManager::getConHashProxyForWeight(uint32_t hashCode, bool bStatic)
  916. {
  917. if(_vRegProxys.empty())
  918. {
  919. TLOGERROR("[EndpointManager::getConHashProxyForWeight _vRegProxys is empty], bStatic:" << bStatic << endl);
  920. return NULL;
  921. }
  922. if(checkConHashChange(bStatic, _lastConHashWeightProxys))
  923. {
  924. int64_t iBegin = TNOWMS;
  925. updateConHashProxyWeighted(bStatic, _lastConHashWeightProxys, _consistentHashWeight);
  926. int64_t iEnd = TNOWMS;
  927. TLOGTARS("[EndpointManager::getConHashProxyForWeight update bStatic:" << bStatic << "|_objName:" << _objName << "|timecost(ms):" << (iEnd - iBegin) << endl);
  928. }
  929. while(_consistentHashWeight.size() > 0)
  930. {
  931. string sNode;
  932. // 通过一致性hash取到对应的节点
  933. _consistentHashWeight.getNodeName(hashCode, sNode);
  934. auto it = _indexActiveProxys.find(sNode);
  935. // 节点不存在,可能是下线或者服务不可用
  936. if (it == _indexActiveProxys.end())
  937. {
  938. updateConHashProxyWeighted(bStatic, _lastConHashWeightProxys, _consistentHashWeight);
  939. continue;
  940. }
  941. //被hash到的节点在主控是active的才走在流程
  942. if (it->second->isActiveInReg() && it->second->checkActive(true))
  943. {
  944. return it->second;
  945. }
  946. else
  947. {
  948. TLOGWARN("[EndpointManager::getHashProxyForWeight, hash not active," << _objectProxy->name() << "@" << it->second->endpoint().desc() << endl);
  949. // 剔除节点再次hash
  950. if (!it->second->isActiveInReg())
  951. {
  952. // 如果在主控的注册状态不是active直接删除,如果状态有变更由updateEndpoints函数里重新添加
  953. _indexActiveProxys.erase(sNode);
  954. }
  955. // checkConHashChange里重新加回到_sortActivProxys重试
  956. _sortActivProxys.erase(sNode);
  957. updateConHashProxyWeighted(bStatic, _lastConHashWeightProxys, _consistentHashWeight);
  958. if (_indexActiveProxys.empty())
  959. {
  960. TLOGERROR("[EndpointManager::getConHashProxyForNormal _activeEndpoints is empty]" << endl);
  961. return NULL;
  962. }
  963. }
  964. }
  965. return getHashProxyForNormal(hashCode);
  966. }
  967. bool EndpointManager::checkHashStaticWeightChange(bool bStatic)
  968. {
  969. if(bStatic)
  970. {
  971. if(_lastHashStaticProxys.size() != _vRegProxys.size())
  972. {
  973. return true;
  974. }
  975. for(size_t i = 0; i < _vRegProxys.size(); i++)
  976. {
  977. //解决服务权重更新时哈希表不更新的问题
  978. if((_lastHashStaticProxys[i]->endpoint().desc() != _vRegProxys[i]->endpoint().desc()) || _vRegProxys[i]->checkWeightChanged(true))
  979. {
  980. return true;
  981. }
  982. }
  983. }
  984. return false;
  985. }
  986. bool EndpointManager::checkConHashChange(bool bStatic, const map<string, AdapterProxy*> &mLastConHashProxys)
  987. {
  988. // 将之前故障临时剔除的节点重新加回来重试
  989. if (_indexActiveProxys.size() != _sortActivProxys.size())
  990. {
  991. for (auto &it : _indexActiveProxys)
  992. {
  993. _sortActivProxys[it.first] = it.second;
  994. }
  995. }
  996. if(mLastConHashProxys.size() != _sortActivProxys.size())
  997. {
  998. return true;
  999. }
  1000. auto itLast = mLastConHashProxys.begin();
  1001. auto itSort = _sortActivProxys.begin();
  1002. for (; itLast!=mLastConHashProxys.end() && itSort!=_sortActivProxys.end(); ++itLast,++itSort)
  1003. {
  1004. if (itLast->first != itSort->first)
  1005. {
  1006. return true;
  1007. }
  1008. //解决服务权重更新时一致性哈希环不更新的问题
  1009. if(bStatic && itSort->second->checkWeightChanged(true))
  1010. {
  1011. return true;
  1012. }
  1013. }
  1014. /*
  1015. if(vLastConHashProxys.size() != _vRegProxys.size())
  1016. {
  1017. return true;
  1018. }
  1019. for(size_t i = 0; i < _vRegProxys.size(); i++)
  1020. {
  1021. if(vLastConHashProxys[i]->endpoint().desc() != _vRegProxys[i]->endpoint().desc())
  1022. {
  1023. return true;
  1024. }
  1025. //解决服务权重更新时一致性哈希环不更新的问题
  1026. if(bStatic && _vRegProxys[i]->checkWeightChanged(true))
  1027. {
  1028. return true;
  1029. }
  1030. }
  1031. */
  1032. return false;
  1033. }
  1034. void EndpointManager::updateHashProxyWeighted(bool bStatic)
  1035. {
  1036. if(_vRegProxys.size() <= 0)
  1037. {
  1038. TLOGERROR("[EndpointManager::updateHashProxyWeighted _vRegProxys is empty], bStatic:" << bStatic << endl);
  1039. return ;
  1040. }
  1041. if(bStatic)
  1042. {
  1043. _lastHashStaticProxys = _vRegProxys;
  1044. _hashStaticRouterCache.clear();
  1045. }
  1046. vector<AdapterProxy*> vRegProxys;
  1047. vector<size_t> vIndex;
  1048. for(size_t i = 0; i < _vRegProxys.size(); ++i)
  1049. {
  1050. if(_vRegProxys[i]->getWeight() > 0)
  1051. {
  1052. vRegProxys.push_back(_vRegProxys[i]);
  1053. vIndex.push_back(i);
  1054. }
  1055. //防止多个服务节点权重同时更新时哈希表多次更新
  1056. _vRegProxys[i]->resetWeightChanged();
  1057. }
  1058. if(vRegProxys.size() <= 0)
  1059. {
  1060. TLOGERROR("[EndpointManager::updateHashProxyWeighted vRegProxys is empty], bStatic:" << bStatic << endl);
  1061. return ;
  1062. }
  1063. size_t iHashStaticWeightSize = vRegProxys.size();
  1064. map<size_t, int> mIdToWeight;
  1065. multimap<int, size_t> mWeightToId;
  1066. size_t iMaxR = 0;
  1067. size_t iMaxRouterR = 0;
  1068. size_t iMaxWeight = vRegProxys[0]->getWeight();
  1069. size_t iMinWeight = vRegProxys[0]->getWeight();
  1070. size_t iTempWeight = 0;
  1071. for(size_t i = 1;i < iHashStaticWeightSize; i++)
  1072. {
  1073. iTempWeight = vRegProxys[i]->getWeight();
  1074. if(iTempWeight > iMaxWeight)
  1075. {
  1076. iMaxWeight = iTempWeight;
  1077. }
  1078. if(iTempWeight < iMinWeight)
  1079. {
  1080. iMinWeight = iTempWeight;
  1081. }
  1082. }
  1083. if(iMinWeight > 0)
  1084. {
  1085. iMaxR = iMaxWeight / iMinWeight;
  1086. if(iMaxR < iMinWeightLimit)
  1087. iMaxR = iMinWeightLimit;
  1088. if(iMaxR > iMaxWeightLimit)
  1089. iMaxR = iMaxWeightLimit;
  1090. }
  1091. else
  1092. {
  1093. iMaxR = 1;
  1094. iMaxWeight = 1;
  1095. }
  1096. for(size_t i = 0;i < iHashStaticWeightSize; i++)
  1097. {
  1098. int iWeight = (vRegProxys[i]->getWeight() * iMaxR) / iMaxWeight;
  1099. if(iWeight > 0)
  1100. {
  1101. iMaxRouterR += iWeight;
  1102. mIdToWeight.insert(map<size_t, int>::value_type(vIndex[i], iWeight));
  1103. mWeightToId.insert(make_pair(iWeight, vIndex[i]));
  1104. }
  1105. else
  1106. {
  1107. if(bStatic)
  1108. {
  1109. _hashStaticRouterCache.push_back(vIndex[i]);
  1110. }
  1111. }
  1112. TLOGTARS("EndpointManager::updateHashProxyWeighted bStatic:" << bStatic << "|_objName:" << _objName << "|endpoint:" << vRegProxys[i]->endpoint().desc() << "|iWeight:" << vRegProxys[i]->getWeight() << "|iWeightR:" << iWeight << "|iIndex:" << vIndex[i] << endl);
  1113. }
  1114. for(size_t i = 0; i < iMaxRouterR; i++)
  1115. {
  1116. bool bFirst = true;
  1117. multimap<int, size_t> mulTemp;
  1118. multimap<int, size_t>::reverse_iterator mIter = mWeightToId.rbegin();
  1119. while(mIter != mWeightToId.rend())
  1120. {
  1121. if(bFirst)
  1122. {
  1123. bFirst = false;
  1124. if(bStatic)
  1125. {
  1126. _hashStaticRouterCache.push_back(mIter->second);
  1127. }
  1128. mulTemp.insert(make_pair((mIter->first - iMaxRouterR + mIdToWeight[mIter->second]), mIter->second));
  1129. }
  1130. else
  1131. {
  1132. mulTemp.insert(make_pair((mIter->first + mIdToWeight[mIter->second]), mIter->second));
  1133. }
  1134. mIter++;
  1135. }
  1136. mWeightToId.clear();
  1137. mWeightToId.swap(mulTemp);
  1138. }
  1139. }
  1140. void EndpointManager::updateConHashProxyWeighted(bool bStatic, map<string, AdapterProxy*> &mLastConHashProxys, TC_ConsistentHashNew &conHash)
  1141. {
  1142. conHash.clear();
  1143. if(_sortActivProxys.empty())
  1144. {
  1145. TLOGERROR("[EndpointManager::updateHashProxyWeighted _indexActiveProxys is empty], bStatic:" << bStatic << endl);
  1146. return ;
  1147. }
  1148. mLastConHashProxys = _sortActivProxys;
  1149. for (auto it = _sortActivProxys.begin(); it != _sortActivProxys.end(); ++it)
  1150. {
  1151. int iWeight = (bStatic ? (it->second->getWeight()) : 100);
  1152. if(iWeight > 0)
  1153. {
  1154. iWeight = iWeight / 4;
  1155. if(iWeight <= 0)
  1156. {
  1157. iWeight = 1;
  1158. }
  1159. // 同一服务有多个obj的情况
  1160. // 同一hash值调用不同的obj会hash到不同的服务器
  1161. // 因为addNode会根据desc(ip+port)计算md5,导致顺序不一致
  1162. // 一致性hash用host进行索引,不使用index,这里传0
  1163. conHash.addNode(it->second->endpoint().host(), 0, iWeight);
  1164. }
  1165. //防止多个服务节点权重同时更新时一致性哈希环多次更新
  1166. it->second->resetWeightChanged();
  1167. }
  1168. /*
  1169. if(_vRegProxys.size() <= 0)
  1170. {
  1171. TLOGERROR("[EndpointManager::updateHashProxyWeighted _vRegProxys is empty], bStatic:" << bStatic << endl);
  1172. return ;
  1173. }
  1174. vLastConHashProxys = _vRegProxys;
  1175. conHash.clear();
  1176. for(size_t i = 0; i < _vRegProxys.size(); ++i)
  1177. {
  1178. int iWeight = (bStatic ? (_vRegProxys[i]->getWeight()) : 100);
  1179. if(iWeight > 0)
  1180. {
  1181. iWeight = iWeight / 4;
  1182. if(iWeight <= 0)
  1183. {
  1184. iWeight = 1;
  1185. }
  1186. // 同一服务有多个obj的情况
  1187. // 同一hash值调用不同的obj会hash到不同的服务器
  1188. // 因为addNode会根据desc(ip+port)计算md5,导致顺序不一致
  1189. conHash.addNode(_vRegProxys[i]->endpoint().host(), i, iWeight);
  1190. }
  1191. //防止多个服务节点权重同时更新时一致性哈希环多次更新
  1192. _vRegProxys[i]->resetWeightChanged();
  1193. }
  1194. */
  1195. conHash.sortNode();
  1196. }
  1197. AdapterProxy* EndpointManager::getHashProxyForNormal(uint32_t hashCode)
  1198. {
  1199. if(_vRegProxys.empty())
  1200. {
  1201. TLOGERROR("[EndpointManager::getHashProxyForNormal _vRegProxys is empty]" << endl);
  1202. return NULL;
  1203. }
  1204. // 1 _vRegProxys从客户端启动之后,就不会再改变,除非有节点增加
  1205. // 2 如果有增加节点,则_vRegProxys顺序会重新排序,之前的hash会改变
  1206. // 3 节点下线后,需要下次启动客户端后,_vRegProxys内容才会生效
  1207. size_t hash = hashCode % _vRegProxys.size();
  1208. //被hash到的节点在主控是active的才走在流程
  1209. if (_vRegProxys[hash]->isActiveInReg() && _vRegProxys[hash]->checkActive(true))
  1210. {
  1211. return _vRegProxys[hash];
  1212. }
  1213. else
  1214. {
  1215. // TLOGWARN("[EndpointManager::getHashProxyForNormal, hash not active," << _objectProxy->name() << "@" << _vRegProxys[hash]->endpoint().desc() << endl);
  1216. if(_activeProxys.empty())
  1217. {
  1218. TLOGERROR("[EndpointManager::getHashProxyForNormal _activeEndpoints is empty]" << endl);
  1219. return NULL;
  1220. }
  1221. //在active节点中再次hash
  1222. vector<AdapterProxy*> thisHash = _activeProxys;
  1223. vector<AdapterProxy*> conn;
  1224. do
  1225. {
  1226. hash = hashCode % thisHash.size();
  1227. if (thisHash[hash]->checkActive(true))
  1228. {
  1229. return thisHash[hash];
  1230. }
  1231. if(!thisHash[hash]->isConnTimeout() &&
  1232. !thisHash[hash]->isConnExc())
  1233. {
  1234. conn.push_back(thisHash[hash]);
  1235. }
  1236. thisHash.erase(thisHash.begin() + hash);
  1237. }
  1238. while(!thisHash.empty());
  1239. if(conn.size() > 0)
  1240. {
  1241. hash = hashCode % conn.size();
  1242. //都有问题, 随机选择一个没有connect超时或者链接异常的发送
  1243. AdapterProxy *adapterProxy = conn[hash];
  1244. //该proxy可能已经被屏蔽,需重新连一次
  1245. adapterProxy->checkActive(true);
  1246. return adapterProxy;
  1247. }
  1248. //所有adapter都有问题 选不到结点,随机找一个重试
  1249. AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
  1250. adapterProxy->resetRetryTime(false);
  1251. //该proxy可能已经被屏蔽,需重新连一次
  1252. adapterProxy->checkActive(true);
  1253. return adapterProxy;
  1254. }
  1255. }
  1256. AdapterProxy* EndpointManager::getConHashProxyForNormal(uint32_t hashCode)
  1257. {
  1258. if(_vRegProxys.empty())
  1259. {
  1260. TLOGERROR("[EndpointManager::getConHashProxyForNormal _vRegProxys is empty]" << endl);
  1261. return NULL;
  1262. }
  1263. if(checkConHashChange(false, _lastConHashProxys))
  1264. {
  1265. int64_t iBegin = TNOWMS;
  1266. updateConHashProxyWeighted(false, _lastConHashProxys, _consistentHash);
  1267. int64_t iEnd = TNOWMS;
  1268. TLOGTARS("[EndpointManager::getConHashProxyForNormal update _objName:" << _objName << "|timecost(ms):" << (iEnd - iBegin) << endl);
  1269. }
  1270. while(_consistentHash.size() > 0)
  1271. {
  1272. string sNode;
  1273. // 通过一致性hash取到对应的节点
  1274. _consistentHash.getNodeName(hashCode, sNode);
  1275. auto it = _indexActiveProxys.find(sNode);
  1276. // 节点不存在,可能是下线或者服务不可用
  1277. if (it == _indexActiveProxys.end())
  1278. {
  1279. updateConHashProxyWeighted(false, _lastConHashProxys, _consistentHash);
  1280. continue;
  1281. }
  1282. //被hash到的节点在主控是active的才走在流程
  1283. if (it->second->isActiveInReg() && it->second->checkActive(true))
  1284. {
  1285. return it->second;
  1286. }
  1287. else
  1288. {
  1289. TLOGWARN("[EndpointManager::getConHashProxyForNormal, hash not active," << _objectProxy->name() << "@" << it->second->endpoint().desc() << endl);
  1290. // 剔除节点再次hash
  1291. if (!it->second->isActiveInReg())
  1292. {
  1293. // 如果在主控的注册状态不是active直接删除,如果状态有变更由updateEndpoints函数里重新添加
  1294. _indexActiveProxys.erase(sNode);
  1295. }
  1296. // checkConHashChange里重新加回到_sortActivProxys重试
  1297. _sortActivProxys.erase(sNode);
  1298. updateConHashProxyWeighted(false, _lastConHashProxys, _consistentHash);
  1299. if (_indexActiveProxys.empty())
  1300. {
  1301. TLOGERROR("[EndpointManager::getConHashProxyForNormal _activeEndpoints is empty]" << endl);
  1302. return NULL;
  1303. }
  1304. }
  1305. }
  1306. return getHashProxyForNormal(hashCode);
  1307. }
  1308. AdapterProxy* EndpointManager::getWeightedProxy(bool bStaticWeighted)
  1309. {
  1310. return getWeightedForNormal(bStaticWeighted);
  1311. }
  1312. AdapterProxy* EndpointManager::getWeightedForNormal(bool bStaticWeighted)
  1313. {
  1314. if (_activeProxys.empty())
  1315. {
  1316. TLOGERROR("[EndpointManager::getWeightedForNormal activeEndpoints is empty][obj:"<<_objName<<"]" << endl);
  1317. return NULL;
  1318. }
  1319. int64_t iNow = TNOW;
  1320. if(_lastBuildWeightTime <= iNow)
  1321. {
  1322. updateProxyWeighted();
  1323. if(!_first)
  1324. {
  1325. _lastBuildWeightTime = iNow + _updateWeightInterval;
  1326. }
  1327. else
  1328. {
  1329. _first = false;
  1330. _lastBuildWeightTime = iNow + _updateWeightInterval + 5;
  1331. }
  1332. }
  1333. bool bEmpty = false;
  1334. int iActiveSize = _activeWeightProxy.size();
  1335. if(iActiveSize > 0)
  1336. {
  1337. size_t iProxyIndex = 0;
  1338. set<AdapterProxy*> sConn;
  1339. if(_staticRouterCache.size() > 0)
  1340. {
  1341. for(size_t i = 0;i < _staticRouterCache.size(); i++)
  1342. {
  1343. ++_lastSWeightPosition;
  1344. if(_lastSWeightPosition >= _staticRouterCache.size())
  1345. _lastSWeightPosition = 0;
  1346. iProxyIndex = _staticRouterCache[_lastSWeightPosition];
  1347. if(_activeWeightProxy[iProxyIndex]->checkActive(false))
  1348. {
  1349. return _activeWeightProxy[iProxyIndex];
  1350. }
  1351. if(!_activeWeightProxy[iProxyIndex]->isConnTimeout() &&
  1352. !_activeWeightProxy[iProxyIndex]->isConnExc())
  1353. {
  1354. sConn.insert(_activeWeightProxy[iProxyIndex]);
  1355. }
  1356. }
  1357. }
  1358. else
  1359. {
  1360. bEmpty = true;
  1361. }
  1362. if(!bEmpty)
  1363. {
  1364. if(sConn.size() > 0)
  1365. {
  1366. vector<AdapterProxy*> conn;
  1367. set<AdapterProxy*>::iterator it_conn = sConn.begin();
  1368. while(it_conn != sConn.end())
  1369. {
  1370. conn.push_back(*it_conn);
  1371. ++it_conn;
  1372. }
  1373. //都有问题, 随机选择一个没有connect超时或者链接异常的发送
  1374. AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())];
  1375. //该proxy可能已经被屏蔽,需重新连一次
  1376. adapterProxy->checkActive(true);
  1377. return adapterProxy;
  1378. }
  1379. //所有adapter都有问题 选不到结点,随机找一个重试
  1380. AdapterProxy * adapterProxy = _activeWeightProxy[((uint32_t)rand() % iActiveSize)];
  1381. adapterProxy->resetRetryTime(false);
  1382. //该proxy可能已经被屏蔽,需重新连一次
  1383. adapterProxy->checkActive(true);
  1384. return adapterProxy;
  1385. }
  1386. }
  1387. vector<AdapterProxy*> conn;
  1388. for(size_t i=0;i<_activeProxys.size();i++)
  1389. {
  1390. ++_lastRoundPosition;
  1391. if(_lastRoundPosition >= _activeProxys.size())
  1392. _lastRoundPosition = 0;
  1393. if(_activeProxys[_lastRoundPosition]->checkActive(false))
  1394. {
  1395. return _activeProxys[_lastRoundPosition];
  1396. }
  1397. if(!_activeProxys[_lastRoundPosition]->isConnTimeout() &&
  1398. !_activeProxys[_lastRoundPosition]->isConnExc())
  1399. conn.push_back(_activeProxys[_lastRoundPosition]);
  1400. }
  1401. if(conn.size() > 0)
  1402. {
  1403. //都有问题, 随机选择一个没有connect超时或者链接异常的发送
  1404. AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())];
  1405. //该proxy可能已经被屏蔽,需重新连一次
  1406. adapterProxy->checkActive(true);
  1407. return adapterProxy;
  1408. }
  1409. //所有adapter都有问题 选不到结点,随机找一个重试
  1410. AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
  1411. adapterProxy->resetRetryTime(false);
  1412. //该proxy可能已经被屏蔽,需重新连一次
  1413. adapterProxy->checkActive(true);
  1414. return adapterProxy;
  1415. }
  1416. void EndpointManager::updateProxyWeighted()
  1417. {
  1418. size_t iWeightProxySize = _activeProxys.size();
  1419. if(iWeightProxySize <= 0)
  1420. {
  1421. TLOGERROR("[EndpointManager::updateProxyWeighted _objName:" << _objName << ", activeProxys.size() <= 0]" << endl);
  1422. return ;
  1423. }
  1424. vector<AdapterProxy*> vProxy;
  1425. for(size_t i = 0; i < _activeProxys.size(); ++i)
  1426. {
  1427. if(_activeProxys[i]->getWeight() > 0)
  1428. {
  1429. vProxy.push_back(_activeProxys[i]);
  1430. }
  1431. }
  1432. iWeightProxySize = vProxy.size();
  1433. if(iWeightProxySize <= 0)
  1434. {
  1435. TLOGERROR("[EndpointManager::updateProxyWeighted _objName:" << _objName << ", vProxy.size() <= 0]" << endl);
  1436. return ;
  1437. }
  1438. if(_update)
  1439. {
  1440. _activeWeightProxy = vProxy;
  1441. updateStaticWeighted();
  1442. }
  1443. _update = false;
  1444. }
  1445. void EndpointManager::updateStaticWeighted()
  1446. {
  1447. size_t iWeightProxySize = _activeWeightProxy.size();
  1448. vector<int> vWeight;
  1449. vWeight.resize(iWeightProxySize);
  1450. for(size_t i = 0; i < iWeightProxySize; i++)
  1451. {
  1452. vWeight[i] = _activeWeightProxy[i]->getWeight();
  1453. }
  1454. dispatchEndpointCache(vWeight);
  1455. }
  1456. void EndpointManager::dispatchEndpointCache(const vector<int> &vWeight)
  1457. {
  1458. if(vWeight.size() <= 0)
  1459. {
  1460. TLOGERROR("[EndpointManager::dispatchEndpointCache vWeight.size() < 0]" << endl);
  1461. return ;
  1462. }
  1463. size_t iWeightProxySize = vWeight.size();
  1464. map<size_t, int> mIdToWeight;
  1465. multimap<int, size_t> mWeightToId;
  1466. size_t iMaxR = 0;
  1467. size_t iMaxRouterR = 0;
  1468. size_t iMaxWeight = 0;
  1469. size_t iMinWeight = 0;
  1470. size_t iTotalCapacty = 0;
  1471. size_t iTempWeight = 0;
  1472. for(size_t i = 0; i < vWeight.size(); ++i)
  1473. {
  1474. iTotalCapacty += vWeight[i];
  1475. }
  1476. _staticRouterCache.clear();
  1477. _lastSWeightPosition = 0;
  1478. _staticRouterCache.reserve(iTotalCapacty+100);
  1479. iMaxWeight = vWeight[0];
  1480. iMinWeight = vWeight[0];
  1481. for(size_t i = 1;i < iWeightProxySize; i++)
  1482. {
  1483. iTempWeight = vWeight[i];
  1484. if(iTempWeight > iMaxWeight)
  1485. {
  1486. iMaxWeight = iTempWeight;
  1487. }
  1488. if(iTempWeight < iMinWeight)
  1489. {
  1490. iMinWeight = iTempWeight;
  1491. }
  1492. }
  1493. if(iMinWeight > 0)
  1494. {
  1495. iMaxR = iMaxWeight / iMinWeight;
  1496. if(iMaxR < iMinWeightLimit)
  1497. iMaxR = iMinWeightLimit;
  1498. if(iMaxR > iMaxWeightLimit)
  1499. iMaxR = iMaxWeightLimit;
  1500. }
  1501. else
  1502. {
  1503. iMaxR = 1;
  1504. iMaxWeight = 1;
  1505. }
  1506. for(size_t i = 0;i < iWeightProxySize; i++)
  1507. {
  1508. int iWeight = (vWeight[i] * iMaxR) / iMaxWeight;
  1509. if(iWeight > 0)
  1510. {
  1511. iMaxRouterR += iWeight;
  1512. mIdToWeight.insert(map<size_t, int>::value_type(i, iWeight));
  1513. mWeightToId.insert(make_pair(iWeight, i));
  1514. }
  1515. else
  1516. {
  1517. _staticRouterCache.push_back(i);
  1518. }
  1519. TLOGTARS("EndpointManager::dispatchEndpointCache _objName:" << _objName << "|endpoint:" << _activeWeightProxy[i]->endpoint().desc() << "|iWeightR:" << iWeight << endl);
  1520. }
  1521. for(size_t i = 0; i < iMaxRouterR; i++)
  1522. {
  1523. bool bFirst = true;
  1524. multimap<int, size_t> mulTemp;
  1525. multimap<int, size_t>::reverse_iterator mIter = mWeightToId.rbegin();
  1526. while(mIter != mWeightToId.rend())
  1527. {
  1528. if(bFirst)
  1529. {
  1530. bFirst = false;
  1531. _staticRouterCache.push_back(mIter->second);
  1532. mulTemp.insert(make_pair((mIter->first - iMaxRouterR + mIdToWeight[mIter->second]), mIter->second));
  1533. }
  1534. else
  1535. {
  1536. mulTemp.insert(make_pair((mIter->first + mIdToWeight[mIter->second]), mIter->second));
  1537. }
  1538. mIter++;
  1539. }
  1540. mWeightToId.clear();
  1541. mWeightToId.swap(mulTemp);
  1542. }
  1543. }
  1544. /////////////////////////////////////////////////////////////////////////////
  1545. EndpointThread::EndpointThread(Communicator* pComm, const string & sObjName, GetEndpointType type, const string & sName, bool bFirstNetThread)
  1546. : QueryEpBase(pComm,bFirstNetThread,true)
  1547. , _type(type)
  1548. , _name(sName)
  1549. {
  1550. init(sObjName, "", true);
  1551. }
  1552. void EndpointThread::getEndpoints(vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint)
  1553. {
  1554. //直连调用这个接口无效
  1555. if(_direct)
  1556. {
  1557. return ;
  1558. }
  1559. {
  1560. TC_LockT<TC_ThreadMutex> lock(_mutex);
  1561. refreshReg(_type,_name);
  1562. activeEndPoint = _activeEndPoint;
  1563. inactiveEndPoint = _inactiveEndPoint;
  1564. }
  1565. }
  1566. void EndpointThread::getTCEndpoints(vector<TC_Endpoint> &activeEndPoint, vector<TC_Endpoint> &inactiveEndPoint)
  1567. {
  1568. //直连调用这个接口无效
  1569. if(_direct)
  1570. {
  1571. return ;
  1572. }
  1573. {
  1574. TC_LockT<TC_ThreadMutex> lock(_mutex);
  1575. refreshReg(_type,_name);
  1576. activeEndPoint = _activeTCEndPoint;
  1577. inactiveEndPoint = _inactiveTCEndPoint;
  1578. }
  1579. }
  1580. void EndpointThread::notifyEndpoints(const set<EndpointInfo> & active,const set<EndpointInfo> & inactive,bool bSync)
  1581. {
  1582. if(!bSync)
  1583. {
  1584. TC_LockT<TC_ThreadMutex> lock(_mutex);
  1585. update(active, inactive);
  1586. }
  1587. else
  1588. {
  1589. update(active, inactive);
  1590. }
  1591. }
  1592. void EndpointThread::update(const set<EndpointInfo> & active, const set<EndpointInfo> & inactive)
  1593. {
  1594. _activeEndPoint.clear();
  1595. _inactiveEndPoint.clear();
  1596. _activeTCEndPoint.clear();
  1597. _inactiveTCEndPoint.clear();
  1598. set<EndpointInfo>::iterator iter= active.begin();
  1599. for(;iter != active.end(); ++iter)
  1600. {
  1601. _activeTCEndPoint.push_back(iter->getEndpoint());
  1602. _activeEndPoint.push_back(*iter);
  1603. }
  1604. iter = inactive.begin();
  1605. for(;iter != inactive.end(); ++iter)
  1606. {
  1607. _inactiveTCEndPoint.push_back(iter->getEndpoint());
  1608. _inactiveEndPoint.push_back(*iter);
  1609. }
  1610. }
  1611. /////////////////////////////////////////////////////////////////////////////
  1612. EndpointManagerThread::EndpointManagerThread(Communicator * pComm,const string & sObjName)
  1613. :_communicator(pComm)
  1614. ,_objName(sObjName)
  1615. {
  1616. }
  1617. EndpointManagerThread::~EndpointManagerThread()
  1618. {
  1619. map<string,EndpointThread*>::iterator iter;
  1620. for(iter=_info.begin();iter != _info.end();iter++)
  1621. {
  1622. if(iter->second)
  1623. {
  1624. delete iter->second;
  1625. iter->second = NULL;
  1626. }
  1627. }
  1628. }
  1629. void EndpointManagerThread::getEndpoint(vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint)
  1630. {
  1631. EndpointThread * pThread = getEndpointThread(E_DEFAULT,"");
  1632. pThread->getEndpoints(activeEndPoint,inactiveEndPoint);
  1633. }
  1634. void EndpointManagerThread::getEndpointByAll(vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint)
  1635. {
  1636. EndpointThread * pThread = getEndpointThread(E_ALL,"");
  1637. pThread->getEndpoints(activeEndPoint,inactiveEndPoint);
  1638. }
  1639. void EndpointManagerThread::getEndpointBySet(const string &sName, vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint)
  1640. {
  1641. EndpointThread * pThread = getEndpointThread(E_SET,sName);
  1642. pThread->getEndpoints(activeEndPoint,inactiveEndPoint);
  1643. }
  1644. void EndpointManagerThread::getEndpointByStation(const string &sName, vector<EndpointInfo> &activeEndPoint, vector<EndpointInfo> &inactiveEndPoint)
  1645. {
  1646. EndpointThread * pThread = getEndpointThread(E_STATION,sName);
  1647. pThread->getEndpoints(activeEndPoint,inactiveEndPoint);
  1648. }
  1649. void EndpointManagerThread::getTCEndpoint(vector<TC_Endpoint> &activeEndPoint, vector<TC_Endpoint> &inactiveEndPoint)
  1650. {
  1651. EndpointThread * pThread = getEndpointThread(E_DEFAULT,"");
  1652. pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint);
  1653. }
  1654. void EndpointManagerThread::getTCEndpointByAll(vector<TC_Endpoint> &activeEndPoint, vector<TC_Endpoint> &inactiveEndPoint)
  1655. {
  1656. EndpointThread * pThread = getEndpointThread(E_ALL,"");
  1657. pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint);
  1658. }
  1659. void EndpointManagerThread::getTCEndpointBySet(const string &sName, vector<TC_Endpoint> &activeEndPoint, vector<TC_Endpoint> &inactiveEndPoint)
  1660. {
  1661. EndpointThread * pThread = getEndpointThread(E_SET,sName);
  1662. pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint);
  1663. }
  1664. void EndpointManagerThread::getTCEndpointByStation(const string &sName, vector<TC_Endpoint> &activeEndPoint, vector<TC_Endpoint> &inactiveEndPoint)
  1665. {
  1666. EndpointThread * pThread = getEndpointThread(E_STATION,sName);
  1667. pThread->getTCEndpoints(activeEndPoint,inactiveEndPoint);
  1668. }
  1669. EndpointThread * EndpointManagerThread::getEndpointThread(GetEndpointType type,const string & sName)
  1670. {
  1671. string sAllName = TC_Common::tostr((int)type) + ":" + sName;
  1672. {
  1673. TC_RW_RLockT<TC_ThreadRWLocker> lock(_mutex);
  1674. auto iter = _info.find(sAllName);
  1675. if (iter != _info.end())
  1676. {
  1677. return iter->second;
  1678. }
  1679. }
  1680. {
  1681. TC_RW_WLockT<TC_ThreadRWLocker> lock(_mutex);
  1682. auto iter = _info.find(sAllName);
  1683. if (iter != _info.end())
  1684. {
  1685. return iter->second;
  1686. }
  1687. EndpointThread* pThread = new EndpointThread(_communicator, _objName, type, sName);
  1688. _info[sAllName] = pThread;
  1689. return pThread;
  1690. }
  1691. }
  1692. }