QueryImp.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 
  2. #include "QueryImp.h"
  3. #include "util/tc_logger.h"
  4. #include "util/tc_clientsocket.h"
  5. void QueryImp::initialize()
  6. {
  7. TLOGDEBUG("begin QueryImp init"<<endl);
  8. }
  9. vector<EndpointF> QueryImp::findObjectById(const string & id, tars::CurrentPtr current)
  10. {
  11. LOG_CONSOLE_DEBUG << endl;
  12. vector<EndpointF> eps = _db.findObjectById(id);
  13. ostringstream os;
  14. doDaylog(FUNID_findObjectById,id,eps,vector<EndpointF>(),current,os);
  15. return eps;
  16. }
  17. tars::Int32 QueryImp::findObjectById4Any(const std::string & id,vector<tars::EndpointF> &activeEp,vector<tars::EndpointF> &inactiveEp,tars::CurrentPtr current)
  18. {
  19. LOG_CONSOLE_DEBUG << endl;
  20. int iRet = _db.findObjectById4All(id, activeEp, inactiveEp);
  21. ostringstream os;
  22. doDaylog(FUNID_findObjectById4Any,id,activeEp,inactiveEp,current,os);
  23. return iRet;
  24. }
  25. int QueryImp::findObjectById4All(const std::string & id, vector<tars::EndpointF> &activeEp,vector<tars::EndpointF> &inactiveEp,tars::CurrentPtr current)
  26. {
  27. LOG_CONSOLE_DEBUG << endl;
  28. ostringstream os;
  29. int iRet = _db.findObjectByIdInGroupPriority(id,current->getIp(),activeEp, inactiveEp,os);
  30. doDaylog(FUNID_findObjectById4All,id,activeEp,inactiveEp,current,os);
  31. return iRet;
  32. }
  33. int QueryImp::findObjectByIdInSameGroup(const std::string & id, vector<tars::EndpointF> &activeEp,vector<tars::EndpointF> &inactiveEp, tars::CurrentPtr current)
  34. {
  35. LOG_CONSOLE_DEBUG << endl;
  36. ostringstream os;
  37. TLOGINFO(__FUNCTION__ << ":" << __LINE__ << "|" << id << "|" << current->getIp() << endl);
  38. int iRet = _db.findObjectByIdInGroupPriority(id, current->getIp(), activeEp, inactiveEp, os);
  39. doDaylog(FUNID_findObjectByIdInSameGroup,id,activeEp,inactiveEp,current,os);
  40. return iRet;
  41. }
  42. Int32 QueryImp::findObjectByIdInSameStation(const std::string & id, const std::string & sStation, vector<tars::EndpointF> &activeEp, vector<tars::EndpointF> &inactiveEp, tars::CurrentPtr current)
  43. {
  44. LOG_CONSOLE_DEBUG << endl;
  45. ostringstream os;
  46. int iRet = _db.findObjectByIdInSameStation(id, sStation, activeEp, inactiveEp, os);
  47. doDaylog(FUNID_findObjectByIdInSameStation,id,activeEp,inactiveEp,current,os);
  48. return iRet;
  49. }
  50. Int32 QueryImp::findObjectByIdInSameSet(const std::string & id,const std::string & setId,vector<tars::EndpointF> &activeEp,vector<tars::EndpointF> &inactiveEp, tars::CurrentPtr current)
  51. {
  52. LOG_CONSOLE_DEBUG << endl;
  53. vector<string> vtSetInfo = TC_Common::sepstr<string>(setId,".");
  54. if (vtSetInfo.size()!=3 ||(vtSetInfo.size()==3&&(vtSetInfo[0]=="*"||vtSetInfo[1]=="*")))
  55. {
  56. TLOGERROR("QueryImp::findObjectByIdInSameSet:|set full name error[" << id << "_" << setId <<"]|" << current->getIp() << endl);
  57. return -1;
  58. }
  59. ostringstream os;
  60. int iRet = _db.findObjectByIdInSameSet(id, vtSetInfo, activeEp, inactiveEp, os);
  61. if (-1 == iRet)
  62. {
  63. //未启动set,启动ip分组策略
  64. return findObjectByIdInSameGroup(id, activeEp, inactiveEp, current);
  65. }
  66. else if (-2 == iRet)
  67. {
  68. //启动了set,但未找到任何服务节点
  69. TLOGERROR("QueryImp::findObjectByIdInSameSet |no one server found for [" << id << "_" << setId <<"]|" << current->getIp() << endl);
  70. return -1;
  71. }
  72. else if (-3 == iRet)
  73. {
  74. //启动了set,但未找到任何地区set,严格上不应该出现此类情形,配置错误或主调设置错误会引起此类错误
  75. TLOGERROR("QueryImp::findObjectByIdInSameSet |no set area found [" << id << "_" << setId <<"]|" << current->getIp() << endl);
  76. return -1;
  77. }
  78. doDaylog(FUNID_findObjectByIdInSameSet,id,activeEp,inactiveEp,current,os,setId);
  79. return iRet;
  80. }
  81. void QueryImp::doDaylog(const FUNID eFnId,const string& id,const vector<tars::EndpointF> &activeEp, const vector<tars::EndpointF> &inactiveEp, const tars::CurrentPtr& current,const ostringstream& os,const string& sSetid)
  82. {
  83. string sEpList;
  84. for(size_t i = 0; i < activeEp.size(); i++)
  85. {
  86. if(0 != i)
  87. {
  88. sEpList += ";";
  89. }
  90. sEpList += activeEp[i].host + ":" + TC_Common::tostr(activeEp[i].port);
  91. }
  92. sEpList += "|";
  93. for(size_t i = 0; i < inactiveEp.size(); i++)
  94. {
  95. if(0 != i)
  96. {
  97. sEpList += ";";
  98. }
  99. sEpList += inactiveEp[i].host + ":" + TC_Common::tostr(inactiveEp[i].port);
  100. }
  101. switch(eFnId)
  102. {
  103. case FUNID_findObjectById4All:
  104. case FUNID_findObjectByIdInSameGroup:
  105. {
  106. FDLOG("query_idc") << eFunTostr(eFnId)<<"|"<<current->getIp() << "|"<< current->getPort() << "|" << id << "|" <<sSetid << "|" << sEpList <<os.str()<< endl;
  107. }
  108. break;
  109. case FUNID_findObjectByIdInSameSet:
  110. {
  111. FDLOG("query_set") << eFunTostr(eFnId)<<"|"<<current->getIp() << "|"<< current->getPort() << "|" << id << "|" <<sSetid << "|" << sEpList <<os.str()<< endl;
  112. }
  113. break;
  114. case FUNID_findObjectById4Any:
  115. case FUNID_findObjectById:
  116. case FUNID_findObjectByIdInSameStation:
  117. default:
  118. {
  119. FDLOG("query") << eFunTostr(eFnId)<<"|"<<current->getIp() << "|"<< current->getPort() << "|" << id << "|" <<sSetid << "|" << sEpList <<os.str()<< endl;
  120. }
  121. break;
  122. }
  123. }
  124. string QueryImp::eFunTostr(const FUNID eFnId)
  125. {
  126. string sFun = "";
  127. switch(eFnId)
  128. {
  129. case FUNID_findObjectByIdInSameGroup:
  130. {
  131. sFun = "findObjectByIdInSameGroup";
  132. }
  133. break;
  134. case FUNID_findObjectByIdInSameSet:
  135. {
  136. sFun = "findObjectByIdInSameSet";
  137. }
  138. break;
  139. case FUNID_findObjectById4Any:
  140. {
  141. sFun = "findObjectById4All";
  142. }
  143. break;
  144. case FUNID_findObjectById:
  145. {
  146. sFun = "findObjectById";
  147. }
  148. break;
  149. case FUNID_findObjectById4All:
  150. {
  151. sFun = "findObjectById4All";
  152. }
  153. break;
  154. case FUNID_findObjectByIdInSameStation:
  155. {
  156. sFun = "findObjectByIdInSameStation";
  157. }
  158. break;
  159. default:
  160. sFun = "UNKNOWN";
  161. break;
  162. }
  163. return sFun;
  164. }