ObjectProxy.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "servant/ObjectProxy.h"
  17. #include "servant/Communicator.h"
  18. #include "servant/CommunicatorEpoll.h"
  19. #include "servant/Global.h"
  20. #include "servant/EndpointManager.h"
  21. #include "servant/AppCache.h"
  22. #include "util/tc_common.h"
  23. #include "util/tc_clientsocket.h"
  24. #include "servant/RemoteLogger.h"
  25. namespace tars
  26. {
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////////////////////
  29. ObjectProxy::ObjectProxy(CommunicatorEpoll *pCommunicatorEpoll, ServantProxy *servantProxy, const string & sObjectProxyName,const string& setName)
  30. : _communicatorEpoll(pCommunicatorEpoll)
  31. , _sObjectProxyName(sObjectProxyName)
  32. , _invokeSetId(setName)
  33. , _isInvokeBySet(false)
  34. , _servantProxy(servantProxy)
  35. {
  36. string::size_type pos = sObjectProxyName.find_first_of('@');
  37. if(pos != string::npos)
  38. {
  39. _name = sObjectProxyName.substr(0,pos);
  40. _address = sObjectProxyName.substr(pos+1);
  41. }
  42. else
  43. {
  44. _name = sObjectProxyName;
  45. //启用set或者指定set调用
  46. if(ClientConfig::SetOpen || !_invokeSetId.empty())
  47. {
  48. //指定set调用时,指定set的优先级最高
  49. _invokeSetId = _invokeSetId.empty()?ClientConfig::SetDivision:_invokeSetId;
  50. _isInvokeBySet = true;
  51. }
  52. }
  53. pos = _name.find_first_of('#');
  54. if(pos != string::npos)
  55. {
  56. _hash = _name.substr(pos+1);
  57. _name = _name.substr(0,pos);
  58. }
  59. }
  60. ObjectProxy::~ObjectProxy()
  61. {
  62. }
  63. void ObjectProxy::initialize(bool rootServant)
  64. {
  65. _endpointManger.reset(new EndpointManager(this, _communicatorEpoll->getCommunicator(), _communicatorEpoll->isFirstNetThread()));
  66. _endpointManger->init(_sObjectProxyName, _invokeSetId, rootServant);
  67. }
  68. const vector<AdapterProxy*> & ObjectProxy::getAdapters()
  69. {
  70. return _endpointManger->getAdapters();
  71. }
  72. int ObjectProxy::loadLocator()
  73. {
  74. if(_endpointManger->getDirectProxy())
  75. {
  76. //直接连接
  77. return 0;
  78. }
  79. string locator = _communicatorEpoll->getCommunicator()->getProperty("locator");
  80. if (locator.find_first_not_of('@') == string::npos)
  81. {
  82. TLOGERROR("[Locator is not valid:" << locator << "]" << endl);
  83. return -1;
  84. }
  85. QueryFPrx prx = _communicatorEpoll->getCommunicator()->stringToProxy<QueryFPrx>(locator);
  86. _endpointManger->setLocatorPrx(prx);
  87. return 0;
  88. }
  89. //void ObjectProxy::setPushCallbacks(const ServantProxyCallbackPtr& cb)
  90. //{
  91. // _pushCallback = cb;
  92. //}
  93. //
  94. //ServantProxyCallbackPtr ObjectProxy::getPushCallback()
  95. //{
  96. // return _pushCallback;
  97. //}
  98. //
  99. //void ObjectProxy::setProxyProtocol(const ProxyProtocol& protocol)
  100. //{
  101. // if(_hasSetProtocol)
  102. // {
  103. // return ;
  104. // }
  105. //
  106. // _hasSetProtocol = true;
  107. // _proxyProtocol = protocol;
  108. //}
  109. //
  110. //ProxyProtocol& ObjectProxy::getProxyProtocol()
  111. //{
  112. // return _proxyProtocol;
  113. //}
  114. //
  115. //void ObjectProxy::setSocketOpt(int level, int optname, const void *optval, SOCKET_LEN_TYPE optlen)
  116. //{
  117. // SocketOpt socketOpt;
  118. //
  119. // socketOpt.level = level;
  120. // socketOpt.optname = optname;
  121. // socketOpt.optval = optval;
  122. // socketOpt.optlen = optlen;
  123. //
  124. // _socketOpts.push_back(socketOpt);
  125. //}
  126. //
  127. //vector<SocketOpt>& ObjectProxy::getSocketOpt()
  128. //{
  129. // return _socketOpts;
  130. //}
  131. void ObjectProxy::invoke(ReqMessage * msg)
  132. {
  133. TLOGTARS("[ObjectProxy::invoke, objname:" << _name << ", begin...]" << endl);
  134. //选择一个远程服务的Adapter来调用
  135. AdapterProxy * pAdapterProxy = NULL;
  136. bool bFirst = _endpointManger->selectAdapterProxy(msg, pAdapterProxy);
  137. if(bFirst)
  138. {
  139. //判断是否请求过主控
  140. bool bRet = _reqTimeoutQueue.push(msg,msg->request.iTimeout+msg->iBeginTime);
  141. assert(bRet);
  142. //把数据缓存在obj里面
  143. TLOGTARS("[ObjectProxy::invoke, objname:" << _name << ", select adapter proxy not valid (have not invoke reg)]" << endl);
  144. return;
  145. }
  146. if(!pAdapterProxy)
  147. {
  148. TLOGERROR("[ObjectProxy::invoke, objname:"<< _name << ", selectAdapterProxy is null]"<<endl);
  149. msg->response->iRet = TARSADAPTERNULL;
  150. doInvokeException(msg);
  151. return ;
  152. }
  153. msg->adapter = pAdapterProxy;
  154. //连接还没有建立, 暂时先放队列里面
  155. if(!msg->adapter->trans()->hasConnected())
  156. {
  157. bool bRet = _reqTimeoutQueue.push(msg, this->_servantProxy->tars_connect_timeout() + msg->iBeginTime);
  158. assert(bRet);
  159. //把数据缓存在obj里面
  160. TLOGTARS("[ObjectProxy::invoke, " << _name << ", select adapter proxy not connected (have not invoke reg)]" << endl);
  161. return;
  162. }
  163. pAdapterProxy->invoke(msg);
  164. }
  165. void ObjectProxy::prepareConnection(AdapterProxy *adapterProxy)
  166. {
  167. while(!_reqTimeoutQueue.empty())
  168. {
  169. TLOGTARS("[ObjectProxy::doInvoke, " << _name << ", conection queue pop size:" << _reqTimeoutQueue.size() << "]" << endl);
  170. ReqMessage * msg = NULL;
  171. _reqTimeoutQueue.pop(msg);
  172. assert(msg != NULL);
  173. //第一个请求包,adapter必然为NULL,如果需要hash,则重新选择一次
  174. if (msg->adapter == NULL && msg->data._hash)
  175. {
  176. //选取的adapter和之前的不一样(hash的原因), 需要重新选择一个远程服务的Adapter来调用
  177. _endpointManger->selectAdapterProxy(msg, adapterProxy);
  178. if (!adapterProxy)
  179. {
  180. //这里肯定是请求过主控
  181. TLOGERROR("[ObjectProxy::doInvoke, " << _name << ", selectAdapterProxy is null]" << endl);
  182. msg->response->iRet = TARSADAPTERNULL;
  183. doInvokeException(msg);
  184. return;
  185. }
  186. msg->adapter = adapterProxy;
  187. }
  188. else
  189. {
  190. msg->adapter = adapterProxy;
  191. }
  192. adapterProxy->invoke(msg);
  193. }
  194. }
  195. void ObjectProxy::onConnect(AdapterProxy *adapterProxy)
  196. {
  197. prepareConnection(adapterProxy);
  198. }
  199. void ObjectProxy::onNotifyEndpoints(const set<EndpointInfo> & active,const set<EndpointInfo> & inactive)
  200. {
  201. if(this->getRootServantProxy()) {
  202. this->getRootServantProxy()->onNotifyEndpoints(_communicatorEpoll, active, inactive);
  203. }
  204. }
  205. //主控查询到地址后过来的
  206. void ObjectProxy::doInvoke()
  207. {
  208. TLOGTARS("[ObjectProxy::doInvoke, objname:" << _name << ", begin...]" << endl);
  209. for(auto it = _reqTimeoutQueue.begin(); it != _reqTimeoutQueue.end(); ++it)
  210. {
  211. ReqMessage * msg = (*it).ptr;
  212. AdapterProxy* adapterProxy;
  213. //选择一个远程服务的Adapter来调用, selectAdapterProxy会发起连接
  214. _endpointManger->selectAdapterProxy(msg, adapterProxy);
  215. }
  216. }
  217. void ObjectProxy::doInvokeException(ReqMessage * msg)
  218. {
  219. //单向调用出现异常直接删除请求
  220. if(msg->eType == ReqMessage::ONE_WAY)
  221. {
  222. delete msg;
  223. return;
  224. }
  225. //标识请求异常
  226. msg->eStatus = ReqMessage::REQ_EXC;
  227. if(msg->eType == ReqMessage::SYNC_CALL)
  228. {
  229. if(!msg->sched)
  230. {
  231. assert(msg->pMonitor);
  232. msg->pMonitor->notify();
  233. }
  234. else
  235. {
  236. msg->sched->put(msg->iCoroId);
  237. }
  238. return;
  239. }
  240. if(msg->callback)
  241. {
  242. if(!msg->sched)
  243. {
  244. if(msg->callback->getNetThreadProcess())
  245. {
  246. ReqMessagePtr msgPtr = msg;
  247. //如果是本线程的回调,直接本线程处理
  248. //比如获取endpoint
  249. try
  250. {
  251. msg->callback->dispatch(msgPtr);
  252. }
  253. catch(exception & e)
  254. {
  255. TLOGERROR("[ObjectProxy::doInvokeException exp:"<<e.what()<<" ,line:"<<__LINE__<<endl);
  256. }
  257. catch(...)
  258. {
  259. TLOGERROR("[ObjectProxy::doInvokeException exp:unknown line:|"<<__LINE__<<endl);
  260. }
  261. }
  262. else
  263. {
  264. //先确保adapter 非null
  265. if (msg->adapter)
  266. {
  267. //异步回调,放入回调处理线程中
  268. _communicatorEpoll->pushAsyncThreadQueue(msg);
  269. }
  270. else
  271. {
  272. TLOGERROR("[ObjectProxy::doInvokeException push adapter is null|" << __LINE__ << endl);
  273. }
  274. }
  275. }
  276. else
  277. {
  278. CoroParallelBasePtr ptr = msg->callback->getCoroParallelBasePtr();
  279. if(ptr)
  280. {
  281. ptr->insert(msg);
  282. if(ptr->checkAllReqReturn())
  283. {
  284. msg->sched->put(msg->iCoroId);
  285. }
  286. }
  287. else
  288. {
  289. TLOGERROR("[ObjectProxy::doInvokeException coro parallel callback error,obj:" << _name << endl);
  290. delete msg;
  291. msg = NULL;
  292. }
  293. }
  294. }
  295. }
  296. void ObjectProxy::doTimeout()
  297. {
  298. const vector<AdapterProxy*> & vAdapterProxy = _endpointManger->getAdapters();
  299. for(size_t iAdapter=0; iAdapter< vAdapterProxy.size();++iAdapter)
  300. {
  301. if(vAdapterProxy[iAdapter] != NULL)
  302. {
  303. vAdapterProxy[iAdapter]->doTimeout();
  304. }
  305. }
  306. ReqMessage * reqInfo = NULL;
  307. while(_reqTimeoutQueue.timeout(reqInfo))
  308. {
  309. // TLOGERROR("[ObjectProxy::doTimeout, objname:" << _name << ", queue timeout error]" << endl);
  310. reqInfo->response->iRet = TARSINVOKETIMEOUT;
  311. doInvokeException(reqInfo);
  312. }
  313. }
  314. void ObjectProxy::doKeepAlive()
  315. {
  316. const vector<AdapterProxy*> & vAdapterProxy = _endpointManger->getAdapters();
  317. for(size_t iAdapter=0; iAdapter< vAdapterProxy.size();++iAdapter)
  318. {
  319. if(vAdapterProxy[iAdapter] != NULL)
  320. {
  321. vAdapterProxy[iAdapter]->doKeepAlive();
  322. }
  323. }
  324. }
  325. void ObjectProxy::mergeStat(map<StatMicMsgHead, StatMicMsgBody> & mStatMicMsg)
  326. {
  327. const vector<AdapterProxy*> & vAdapterProxy = _endpointManger->getAdapters();
  328. for(size_t iAdapter=0; iAdapter< vAdapterProxy.size();++iAdapter)
  329. {
  330. if(vAdapterProxy[iAdapter] != NULL)
  331. {
  332. vAdapterProxy[iAdapter]->mergeStat(mStatMicMsg);
  333. }
  334. }
  335. }
  336. void ObjectProxy::onSetInactive(const EndpointInfo& ep)
  337. {
  338. const vector<AdapterProxy*> & vAdapterProxy = _endpointManger->getAdapters();
  339. for(size_t iAdapter=0; iAdapter< vAdapterProxy.size();++iAdapter)
  340. {
  341. if(vAdapterProxy[iAdapter] != NULL)
  342. {
  343. if (vAdapterProxy[iAdapter]->endpoint() == ep)
  344. {
  345. vAdapterProxy[iAdapter]->onSetInactive();
  346. }
  347. }
  348. }
  349. }
  350. //////////////////////////////////////////////////////////////////////////////////
  351. }