2020-02-07 13:50:04 +08:00
/**
2018-09-05 11:26:21 +08:00
* Tencent is pleased to support the open source community by making Tars available .
*
* Copyright ( C ) 2016 THL A29 Limited , a Tencent company . All rights reserved .
*
* Licensed under the BSD 3 - Clause License ( the " License " ) ; you may not use this file except
* in compliance with the License . You may obtain a copy of the License at
*
* https : //opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing , software distributed
* under the License is distributed on an " AS IS " BASIS , WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND , either express or implied . See the License for the
* specific language governing permissions and limitations under the License .
*/
# include "util/tc_thread_pool.h"
# include "util/tc_timeprovider.h"
# include "servant/ServantHandle.h"
# include "servant/Application.h"
# include "servant/ServantHelper.h"
# include "servant/AppProtocol.h"
# include "servant/BaseF.h"
2020-04-01 20:29:29 +08:00
# include "servant/KeepAliveNodeF.h"
# include "servant/Cookie.h"
2020-11-09 13:51:03 +08:00
# include "servant/Application.h"
2021-08-23 15:35:24 +08:00
// #ifdef TARS_OPENTRACKING
// #include "servant/text_map_carrier.h"
// #endif
2018-09-05 11:26:21 +08:00
namespace tars
{
/////////////////////////////////////////////////////////////////////////
//
2020-11-09 13:51:03 +08:00
ServantHandle : : ServantHandle ( Application * application )
2021-08-23 15:35:24 +08:00
: _application ( application )
2018-09-05 11:26:21 +08:00
{
}
ServantHandle : : ~ ServantHandle ( )
{
2020-02-22 21:20:30 +08:00
auto it = _servants . begin ( ) ;
2018-09-05 11:26:21 +08:00
while ( it ! = _servants . end ( ) )
{
try
{
it - > second - > destroy ( ) ;
}
catch ( exception & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::destroy error: " < < ex . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::destroy unknown exception error] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
+ + it ;
}
}
void ServantHandle : : handleAsyncResponse ( )
{
ReqMessagePtr resp ;
2020-02-22 21:20:30 +08:00
auto it = _servants . begin ( ) ;
2018-09-05 11:26:21 +08:00
while ( it ! = _servants . end ( ) )
{
2020-02-22 21:20:30 +08:00
while ( it - > second - > getResponseQueue ( ) . pop_front ( resp ) )
2018-09-05 11:26:21 +08:00
{
try
{
2020-01-28 21:51:45 +08:00
if ( resp - > response - > iRet = = TARSSERVERSUCCESS )
2018-09-05 11:26:21 +08:00
{
it - > second - > doResponse ( resp ) ;
}
else if ( resp - > pObjectProxy = = NULL )
{
it - > second - > doResponseNoRequest ( resp ) ;
}
else
{
it - > second - > doResponseException ( resp ) ;
}
}
catch ( exception & e )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::doResponse ex: " < < e . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::doResponse error] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
}
//业务处理附加的自有消息
try
{
it - > second - > doCustomMessage ( false ) ;
it - > second - > doCustomMessage ( ) ;
}
catch ( exception & e )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::doCustemMessage ex: " < < e . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::doCustemMessage ex.] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
+ + it ;
}
}
void ServantHandle : : handleCustomMessage ( bool bExpectIdle )
{
2020-02-22 21:20:30 +08:00
for ( auto it = _servants . begin ( ) ; it ! = _servants . end ( ) ; it + + )
2018-09-05 11:26:21 +08:00
{
//业务处理附加的自有消息
try
{
it - > second - > doCustomMessage ( bExpectIdle ) ;
it - > second - > doCustomMessage ( ) ;
}
catch ( exception & e )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::doCustemMessage ex: " < < e . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::doCustemMessage ex.] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
}
}
bool ServantHandle : : allFilterIsEmpty ( )
{
2021-08-23 15:35:24 +08:00
auto it = _servants . begin ( ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
while ( it ! = _servants . end ( ) )
{
if ( ! it - > second - > getResponseQueue ( ) . empty ( ) )
{
return false ;
}
+ + it ;
}
return true ;
2018-09-05 11:26:21 +08:00
}
void ServantHandle : : initialize ( )
{
2021-09-07 10:31:54 +08:00
if ( TC_CoroutineScheduler : : scheduler ( ) )
{
ServantProxyThreadData : : getData ( ) - > _sched = TC_CoroutineScheduler : : scheduler ( ) ;
}
2020-11-09 13:51:03 +08:00
ServantPtr servant = _application - > getServantHelper ( ) - > create ( _bindAdapter - > getName ( ) ) ;
2020-01-28 21:51:45 +08:00
if ( servant )
{
_servants [ servant - > getName ( ) ] = servant ;
}
else
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle initialize createServant ret null, for adapter ` " + _bindAdapter - > getName ( ) + " `] " < < endl ) ;
cerr < < " ServantHandle initialize createServant ret null, for adapter ` " + _bindAdapter - > getName ( ) + " `] " < < endl ;
2020-03-14 17:53:34 +08:00
2020-04-01 20:29:29 +08:00
RemoteNotify : : getInstance ( ) - > report ( " initialize createServant error: no adapter: " + _bindAdapter - > getName ( ) ) ;
2020-03-14 17:53:34 +08:00
TC_Common : : msleep ( 100 ) ;
2020-02-15 19:40:08 +08:00
exit ( - 1 ) ;
2018-09-05 11:26:21 +08:00
}
2020-02-22 21:20:30 +08:00
auto it = _servants . begin ( ) ;
2018-09-05 11:26:21 +08:00
if ( it = = _servants . end ( ) )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [initialize error: no servant exists] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2020-04-01 20:29:29 +08:00
RemoteNotify : : getInstance ( ) - > report ( " initialize error: no servant exists. " ) ;
2018-09-05 11:26:21 +08:00
2020-03-14 17:53:34 +08:00
TC_Common : : msleep ( 100 ) ;
2018-09-05 11:26:21 +08:00
exit ( - 1 ) ;
}
while ( it ! = _servants . end ( ) )
{
try
{
it - > second - > setHandle ( this ) ;
it - > second - > initialize ( ) ;
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [ " < < it - > second - > getName ( ) < < " initialize] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
catch ( exception & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [initialize error: " < < ex . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2020-04-01 20:29:29 +08:00
RemoteNotify : : getInstance ( ) - > report ( " initialize error: " + string ( ex . what ( ) ) ) ;
2018-09-05 11:26:21 +08:00
2020-03-14 17:53:34 +08:00
TC_Common : : msleep ( 100 ) ;
exit ( - 1 ) ;
2018-09-05 11:26:21 +08:00
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [initialize unknown exception error] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2020-04-01 20:29:29 +08:00
RemoteNotify : : getInstance ( ) - > report ( " initialize unknown exception error " ) ;
2020-03-14 17:53:34 +08:00
TC_Common : : msleep ( 100 ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
exit ( - 1 ) ;
}
+ + it ;
}
2018-09-05 11:26:21 +08:00
}
void ServantHandle : : heartbeat ( )
{
2021-08-23 15:35:24 +08:00
time_t fcur = TNOW ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
if ( abs ( fcur - _bindAdapter - > getHeartBeatTime ( ) ) > HEART_BEAT_INTERVAL )
{
_bindAdapter - > setHeartBeatTime ( fcur ) ;
2018-09-05 11:26:21 +08:00
2020-01-28 21:51:45 +08:00
TARS_KEEPALIVE ( _bindAdapter - > getName ( ) ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
//上报连接数 比率
if ( _bindAdapter - > _pReportConRate )
{
_bindAdapter - > _pReportConRate - > report ( ( int ) ( _bindAdapter - > getNowConnection ( ) * 1000 / _bindAdapter - > getMaxConns ( ) ) ) ;
}
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
//有队列, 且队列长度>0才上报
if ( _bindAdapter - > _pReportQueue )
{
_bindAdapter - > _pReportQueue - > report ( ( int ) _bindAdapter - > getRecvBufferSize ( ) ) ;
}
}
2018-09-05 11:26:21 +08:00
}
2020-09-17 21:35:19 +08:00
CurrentPtr ServantHandle : : createCurrent ( const shared_ptr < TC_EpollServer : : RecvContext > & data )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
CurrentPtr current = new Current ( this ) ;
2018-09-05 11:26:21 +08:00
try
{
2020-01-28 21:51:45 +08:00
current - > initialize ( data ) ;
2018-09-05 11:26:21 +08:00
}
catch ( TarsDecodeException & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handle request protocol decode error: " < < ex . what ( ) < < " ] " < < endl ) ;
2020-01-28 21:51:45 +08:00
close ( data ) ;
2018-09-05 11:26:21 +08:00
return NULL ;
}
//只有TARS协议才处理
if ( current - > getBindAdapter ( ) - > isTarsProtocol ( ) )
{
int64_t now = TNOWMS ;
2021-08-23 15:35:24 +08:00
//数据在队列中的时间超过了客户端等待的时间(TARS协议)
if ( current - > _request . iTimeout > 0 & & ( now - data - > recvTimeStamp ( ) ) > current - > _request . iTimeout )
{
//上报超时数目
if ( data - > adapter ( ) - > _pReportTimeoutNum )
data - > adapter ( ) - > _pReportTimeoutNum - > report ( 1 ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
TLOGERROR ( " [TARS][ServantHandle::handle queue timeout: "
2020-04-13 19:35:01 +08:00
< < current - > _request . sServantName < < " , func: "
< < current - > _request . sFuncName < < " , recv time: "
< < data - > recvTimeStamp ( ) < < " , queue timeout: "
< < data - > adapter ( ) - > getQueueTimeout ( ) < < " , timeout: "
< < current - > _request . iTimeout < < " , now: "
< < now < < " , ip: " < < data - > ip ( ) < < " , port: " < < data - > port ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
current - > sendResponse ( TARSSERVERQUEUETIMEOUT ) ;
2021-08-23 15:35:24 +08:00
return NULL ;
}
}
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
return current ;
2018-09-05 11:26:21 +08:00
}
2020-09-17 21:35:19 +08:00
CurrentPtr ServantHandle : : createCloseCurrent ( const shared_ptr < TC_EpollServer : : RecvContext > & data )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
CurrentPtr current = new Current ( this ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
current - > initializeClose ( data ) ;
current - > setReportStat ( false ) ;
current - > setCloseType ( data - > closeType ( ) ) ;
return current ;
2018-09-05 11:26:21 +08:00
}
2020-01-28 21:51:45 +08:00
void ServantHandle : : handleClose ( const shared_ptr < TC_EpollServer : : RecvContext > & data )
2018-09-05 11:26:21 +08:00
{
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [ServantHandle::handleClose,adapter: " < < data - > adapter ( ) - > getName ( ) < < " ,peer: " < < data - > ip ( ) < < " : " < < data - > port ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
CurrentPtr current = createCloseCurrent ( data ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
auto sit = _servants . find ( current - > getServantName ( ) ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
if ( sit = = _servants . end ( ) )
{
TLOGERROR ( " [TARS]ServantHandle::handleClose,adapter: " < < data - > adapter ( ) - > getName ( ) < < " ,peer: " < < data - > ip ( ) < < " : " < < data - > port ( ) < < " , " < < current - > getServantName ( ) < < " not found " < < endl ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
return ;
}
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
try
{
//业务逻辑处理
sit - > second - > doClose ( current ) ;
}
catch ( exception & ex )
{
TLOGERROR ( " [TARS]ServantHandle::handleClose " < < ex . what ( ) < < endl ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
return ;
}
catch ( . . . )
{
TLOGERROR ( " [TARS]ServantHandle::handleClose unknown error " < < endl ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
return ;
}
2018-09-05 11:26:21 +08:00
}
2020-01-28 21:51:45 +08:00
void ServantHandle : : handleTimeout ( const shared_ptr < TC_EpollServer : : RecvContext > & data )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
CurrentPtr current = createCurrent ( data ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
if ( ! current ) return ;
2018-09-05 11:26:21 +08:00
//上报超时数目
2020-01-28 21:51:45 +08:00
if ( data - > adapter ( ) - > _pReportTimeoutNum )
data - > adapter ( ) - > _pReportTimeoutNum - > report ( 1 ) ;
2018-09-05 11:26:21 +08:00
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleTimeout adapter ' "
2020-01-28 21:51:45 +08:00
< < data - > adapter ( ) - > getName ( )
< < " ', recvtime: " < < data - > recvTimeStamp ( ) < < " | "
< < " , timeout: " < < data - > adapter ( ) - > getQueueTimeout ( )
2020-04-13 19:35:01 +08:00
< < " , id: " < < current - > getRequestId ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
if ( current - > getBindAdapter ( ) - > isTarsProtocol ( ) )
{
current - > sendResponse ( TARSSERVERQUEUETIMEOUT ) ;
}
}
2020-01-28 21:51:45 +08:00
void ServantHandle : : handleOverload ( const shared_ptr < TC_EpollServer : : RecvContext > & data )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
CurrentPtr current = createCurrent ( data ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
if ( ! current ) return ;
2018-09-05 11:26:21 +08:00
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleOverload adapter ' "
2020-01-28 21:51:45 +08:00
< < data - > adapter ( ) - > getName ( )
2018-09-05 11:26:21 +08:00
< < " ',overload:-1,queue capacity: "
2020-01-28 21:51:45 +08:00
< < data - > adapter ( ) - > getQueueCapacity ( )
2020-04-13 19:35:01 +08:00
< < " ,id: " < < current - > getRequestId ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
if ( current - > getBindAdapter ( ) - > isTarsProtocol ( ) )
{
current - > sendResponse ( TARSSERVEROVERLOAD ) ;
}
}
2020-01-28 21:51:45 +08:00
void ServantHandle : : handle ( const shared_ptr < TC_EpollServer : : RecvContext > & data )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
CurrentPtr current = createCurrent ( data ) ;
2018-09-05 11:26:21 +08:00
2020-09-17 21:35:19 +08:00
if ( ! current ) return ;
2018-09-05 11:26:21 +08:00
if ( current - > getBindAdapter ( ) - > isTarsProtocol ( ) )
{
handleTarsProtocol ( current ) ;
}
else
{
handleNoTarsProtocol ( current ) ;
}
}
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// #ifdef TARS_OPENTRACKING
// void ServantHandle::processTracking(const TarsCurrentPtr ¤t)
// {
// if(!(Application::getCommunicator()->_traceManager))
// {
// return;
// }
// ServantProxyThreadData * sptd = ServantProxyThreadData::getData();
// assert(sptd);
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
// if(!sptd)
// {
// return;
// }
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
// //提取packet中的span信息, 更新为被调的span信息后设置到sptd->_trackInfoMap;
// sptd->_trackInfoMap.clear();
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// if (IS_MSG_TYPE(current->getMessageType(), tars::TARSMESSAGETYPETRACK))
// {
// map<string, string>::const_iterator trackinfoIter = current->getRequestStatus().find(ServantProxy::STATUS_TRACK_KEY);
// TLOGTARS("[TARS] servant got a tracking request, message_type set" << current->getMessageType() << endl);
// if (trackinfoIter != current->getRequestStatus().end())
// {
// TLOGTARS("[TARS] servant got a tracking request, tracking key:" << trackinfoIter->second << endl);
// string context = trackinfoIter->second;
// char szBuffer[context.size() + 1];
// memset(szBuffer, 0x00, context.size() + 1);
// memcpy(szBuffer, context.c_str(), context.size());
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// std::unordered_map<std::string, std::string> text_map;
// write_span_context(text_map, szBuffer);
// TextMapCarrier carrier(text_map);
// auto tracer = Application::getCommunicator()->_traceManager->_tracer;
// auto span_context_maybe = tracer->Extract(carrier);
// if(!span_context_maybe)
// {
// //error
// TLOGERROR("[TARS] servant got a tracking request, but extract the span context fail");
// return ;
// }
// string funcName = current->getFuncName();
// auto child_span = tracer->StartSpan(funcName, {opentracing::ChildOf(span_context_maybe->get())});
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// //text_map.clear();
// auto err = tracer->Inject(child_span->context(), carrier);
// assert(err);
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
// sptd->_trackInfoMap = text_map;
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// _spanMap[current->getRequestId()].reset(child_span.release());
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// return ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
// }
// }
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// return ;
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// }
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// void ServantHandle::finishTracking(int ret, const TarsCurrentPtr ¤t)
// {
// int requestId = current->getRequestId();
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// if(_spanMap.find(requestId) != _spanMap.end())
// {
// auto spanIter = _spanMap.find(requestId);
// spanIter->second->SetTag("Retcode", ret);
// spanIter->second->Finish();
2019-02-21 21:10:13 +08:00
2021-08-23 15:35:24 +08:00
// _spanMap.erase(requestId);
// }
// }
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
// #endif
2019-02-21 21:10:13 +08:00
2020-09-17 21:35:19 +08:00
bool ServantHandle : : processDye ( const CurrentPtr & current , string & dyeingKey )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
//当前线程的线程数据
ServantProxyThreadData * sptd = ServantProxyThreadData : : getData ( ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
if ( sptd )
{
sptd - > _data . _dyeingKey = " " ;
}
2018-09-05 11:26:21 +08:00
//当前请求已经被染色, 需要打印染色日志
map < string , string > : : const_iterator dyeingIt = current - > getRequestStatus ( ) . find ( ServantProxy : : STATUS_DYED_KEY ) ;
if ( IS_MSG_TYPE ( current - > getMessageType ( ) , tars : : TARSMESSAGETYPEDYED ) )
{
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [servant got a dyeing request, message_type set: " < < current - > getMessageType ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
if ( dyeingIt ! = current - > getRequestStatus ( ) . end ( ) )
{
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [servant got a dyeing request, dyeing key: " < < dyeingIt - > second < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
dyeingKey = dyeingIt - > second ;
}
return true ;
}
2020-09-23 10:25:23 +08:00
//servant已经被染色, 开启染色日志
2020-11-09 13:51:03 +08:00
if ( _application - > getServantHelper ( ) - > isDyeing ( ) )
2020-09-23 10:25:23 +08:00
{
map < string , string > : : const_iterator dyeingKeyIt = current - > getRequestStatus ( ) . find ( ServantProxy : : STATUS_GRID_KEY ) ;
if ( dyeingKeyIt ! = current - > getRequestStatus ( ) . end ( ) & &
2020-11-09 13:51:03 +08:00
_application - > getServantHelper ( ) - > isDyeingReq ( dyeingKeyIt - > second , current - > getServantName ( ) , current - > getFuncName ( ) ) )
2020-09-23 10:25:23 +08:00
{
TLOGTARS ( " [TARS] dyeing servant got a dyeing req, key: " < < dyeingKeyIt - > second < < endl ) ;
dyeingKey = dyeingKeyIt - > second ;
return true ;
}
}
2021-08-23 15:35:24 +08:00
return false ;
2018-09-05 11:26:21 +08:00
}
2021-10-03 22:47:25 +08:00
bool ServantHandle : : processTrace ( const CurrentPtr & current )
{
//当前线程的线程数据
ServantProxyThreadData * sptd = ServantProxyThreadData : : getData ( ) ;
if ( sptd )
{
sptd - > _traceCall = false ;
sptd - > _traceContext . reset ( ) ;
}
// 如果调用链需要追踪,需要初始化线程私有追踪参数
map < string , string > : : const_iterator traceIt = current - > getRequestStatus ( ) . find ( ServantProxy : : STATUS_TRACE_KEY ) ;
if ( IS_MSG_TYPE ( current - > getMessageType ( ) , tars : : TARSMESSAGETYPETRACE ) )
{
TLOGTARS ( " [TARS] servant got a trace request, message_type set " < < current - > getMessageType ( ) < < endl ) ;
if ( traceIt ! = current - > getRequestStatus ( ) . end ( ) )
{
TLOGTARS ( " [TARS] servant got a trace request, trace key: " < < traceIt - > second < < endl ) ;
if ( sptd - > initTrace ( traceIt - > second ) )
{
sptd - > _traceCall = true ;
return true ;
}
else
{
TLOGTARS ( " [TARS] servant got a trace request, but trace key is error: " < < traceIt - > second < < endl ) ;
}
}
}
return false ;
}
2020-03-25 17:20:32 +08:00
2020-09-17 21:35:19 +08:00
bool ServantHandle : : processCookie ( const CurrentPtr & current , map < string , string > & cookie )
2020-03-25 17:20:32 +08:00
{
2020-03-31 22:04:16 +08:00
const static string STATUS = " STATUS_ " ;
2020-03-25 17:20:32 +08:00
2020-03-31 22:04:16 +08:00
std : : for_each ( current - > getRequestStatus ( ) . begin ( ) , current - > getRequestStatus ( ) . end ( ) , [ & ] ( const map < string , string > : : value_type & p ) {
if ( p . first . size ( ) > STATUS . size ( ) & & TC_Port : : strncasecmp ( p . first . c_str ( ) , STATUS . c_str ( ) , STATUS . size ( ) ) = = 0 ) {
return ;
}
cookie . insert ( make_pair ( p . first , p . second ) ) ;
} ) ;
2020-03-25 17:20:32 +08:00
2020-03-31 22:04:16 +08:00
return ! cookie . empty ( ) ;
2020-03-25 17:20:32 +08:00
}
2020-09-17 21:35:19 +08:00
bool ServantHandle : : checkValidSetInvoke ( const CurrentPtr & current )
2018-09-05 11:26:21 +08:00
{
2021-08-23 15:35:24 +08:00
/*是否允许检查合法性*/
if ( ServerConfig : : IsCheckSet = = 0 )
{
//不检查
return true ;
}
2018-09-05 11:26:21 +08:00
bool isSetInvoke = IS_MSG_TYPE ( current - > getMessageType ( ) , tars : : TARSMESSAGETYPESETNAME ) ;
//客户端按set规则调用且服务端启用set
if ( isSetInvoke & & ClientConfig : : SetOpen )
{
/**
* 合 法 性 规 则 :
* 1 客 户 端 set名称与服务端set在同一分组 , eg , test . s .1 < - > test . s .1
* 2 客 户 端 set名称与服务端set在同一地区 , eg , test . s . * < - > test . s .1 | test . s .2 | test . s . *
* 3 客 户 端 set名称与服务端set属于不同名称 , eg , test1 . s .1 < - > test2 . n .2
* 4 1 , 2 , 3 条 件 都 不 满 足 , 则 认 为 该 调 用 不 合 法
*/
map < string , string > : : const_iterator setIt = current - > getRequestStatus ( ) . find ( ServantProxy : : STATUS_SETNAME_VALUE ) ;
string sSetName ( " " ) ;
if ( setIt ! = current - > getRequestStatus ( ) . end ( ) )
{
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [servant got a setname request, setname key: " < < setIt - > second < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
sSetName = setIt - > second ;
2018-09-05 11:26:21 +08:00
if ( ClientConfig : : SetDivision = = sSetName )
{
return true ;
}
else
{
//属于同一地区是也属于合法调用
string setArea1 = ClientConfig : : SetDivision . substr ( 0 , ClientConfig : : SetDivision . find_last_of ( " . " ) ) ;
string setArea2 = sSetName . substr ( 0 , sSetName . find_last_of ( " . " ) ) ;
if ( setArea1 = = setArea2 )
{
return true ;
}
else if ( ClientConfig : : SetDivision . substr ( 0 , ClientConfig : : SetDivision . find_first_of ( " . " ) ) ! =
sSetName . substr ( 0 , sSetName . find_first_of ( " . " ) ) )
{
//属于不同的set之间调用也属于合法
return true ;
}
else
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::checkValidSetInvoke| "
2018-09-05 11:26:21 +08:00
< < current - > getIp ( ) < < " | "
< < current - > getMessageType ( ) < < " | "
< < current - > getServantName ( ) < < " | "
< < current - > getFuncName ( ) < < " |client: "
< < ClientConfig : : SetDivision < < " |server: "
2020-04-13 19:35:01 +08:00
< < sSetName < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
current - > sendResponse ( TARSINVOKEBYINVALIDESET ) ;
return false ;
}
}
}
else
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::checkValidSetInvoke| "
2018-09-05 11:26:21 +08:00
< < current - > getIp ( ) < < " | "
< < current - > getMessageType ( ) < < " | "
< < current - > getServantName ( ) < < " | "
< < current - > getFuncName ( ) < < " |client: "
< < ClientConfig : : SetDivision < < " |server: "
2020-04-13 19:35:01 +08:00
< < sSetName < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
current - > sendResponse ( TARSINVOKEBYINVALIDESET ) ;
return false ;
}
}
2021-08-23 15:35:24 +08:00
//没有按set规则调用
return true ;
2018-09-05 11:26:21 +08:00
}
2020-09-17 21:35:19 +08:00
void ServantHandle : : handleTarsProtocol ( const CurrentPtr & current )
2018-09-05 11:26:21 +08:00
{
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [ServantHandle::handleTarsProtocol current: "
2018-09-05 11:26:21 +08:00
< < current - > getIp ( ) < < " | "
< < current - > getPort ( ) < < " | "
< < current - > getMessageType ( ) < < " | "
< < current - > getServantName ( ) < < " | "
< < current - > getFuncName ( ) < < " | "
< < current - > getRequestId ( ) < < " | "
2020-04-13 19:35:01 +08:00
< < TC_Common : : tostr ( current - > getRequestStatus ( ) ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2021-08-23 15:35:24 +08:00
//检查set调用合法性
if ( ! checkValidSetInvoke ( current ) )
{
return ;
}
2018-09-05 11:26:21 +08:00
//处理染色消息
string dyeingKey = " " ;
TarsDyeingSwitch dyeSwitch ;
if ( processDye ( current , dyeingKey ) )
{
dyeSwitch . enableDyeing ( dyeingKey ) ;
}
2020-03-25 17:20:32 +08:00
2021-10-03 22:47:25 +08:00
processTrace ( current ) ;
2021-08-23 15:35:24 +08:00
//处理cookie
map < string , string > cookie ;
CookieOp cookieOp ;
if ( processCookie ( current , cookie ) )
{
cookieOp . setCookie ( cookie ) ;
current - > setCookie ( cookie ) ;
}
// processSample(current);
2020-03-25 17:20:32 +08:00
2021-08-23 15:35:24 +08:00
auto sit = _servants . find ( current - > getServantName ( ) ) ;
2018-09-05 11:26:21 +08:00
if ( sit = = _servants . end ( ) )
{
current - > sendResponse ( TARSSERVERNOSERVANTERR ) ;
2021-08-23 15:35:24 +08:00
// #ifdef TARS_OPENTRACKING
// finishTracking(TARSSERVERNOSERVANTERR, current);
// #endif
2018-09-05 11:26:21 +08:00
return ;
}
int ret = TARSSERVERUNKNOWNERR ;
2021-08-23 15:35:24 +08:00
string sResultDesc = " " ;
2018-09-05 11:26:21 +08:00
2020-09-17 21:35:19 +08:00
ResponsePacket response ;
2021-08-23 15:35:24 +08:00
2018-09-05 11:26:21 +08:00
try
{
//业务逻辑处理
2020-09-17 21:35:19 +08:00
ret = sit - > second - > dispatch ( current , response . sBuffer ) ;
2018-09-05 11:26:21 +08:00
}
catch ( TarsDecodeException & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleTarsProtocol " < < ex . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
ret = TARSSERVERDECODEERR ;
sResultDesc = ex . what ( ) ;
}
catch ( TarsEncodeException & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleTarsProtocol " < < ex . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
ret = TARSSERVERENCODEERR ;
sResultDesc = ex . what ( ) ;
}
catch ( exception & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleTarsProtocol " < < ex . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
ret = TARSSERVERUNKNOWNERR ;
sResultDesc = ex . what ( ) ;
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleTarsProtocol unknown error] " < < endl ) ;
2018-09-05 11:26:21 +08:00
ret = TARSSERVERUNKNOWNERR ;
sResultDesc = " handleTarsProtocol unknown exception error " ;
}
//单向调用或者业务不需要同步返回
if ( current - > isResponse ( ) )
{
2020-09-17 21:35:19 +08:00
current - > sendResponse ( ret , response , Current : : TARS_STATUS ( ) , sResultDesc ) ;
2018-09-05 11:26:21 +08:00
}
2020-04-01 20:29:29 +08:00
# ifdef TARS_OPENTRACKING
2019-02-21 21:10:13 +08:00
finishTracking ( ret , current ) ;
# endif
2018-09-05 11:26:21 +08:00
}
void ServantHandle : : handleNoTarsProtocol ( const TarsCurrentPtr & current )
{
2020-04-13 19:35:01 +08:00
TLOGTARS ( " [ServantHandle::handleNoTarsProtocol current: "
2018-09-05 11:26:21 +08:00
< < current - > getIp ( ) < < " | "
< < current - > getPort ( ) < < " | "
2020-04-13 19:35:01 +08:00
< < current - > getServantName ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
2020-02-22 21:20:30 +08:00
auto sit = _servants . find ( current - > getServantName ( ) ) ;
2018-09-05 11:26:21 +08:00
2020-09-17 21:35:19 +08:00
assert ( sit ! = _servants . end ( ) ) ;
2018-09-05 11:26:21 +08:00
2020-09-17 21:35:19 +08:00
vector < char > buffer ;
2018-09-05 11:26:21 +08:00
try
{
//业务逻辑处理
sit - > second - > dispatch ( current , buffer ) ;
}
catch ( exception & ex )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleNoTarsProtocol " < < ex . what ( ) < < " ] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
catch ( . . . )
{
2020-04-13 19:35:01 +08:00
TLOGERROR ( " [ServantHandle::handleNoTarsProtocol unknown error] " < < endl ) ;
2018-09-05 11:26:21 +08:00
}
2020-09-17 21:35:19 +08:00
if ( current - > isResponse ( ) & & ! buffer . empty ( ) )
{
current - > sendResponse ( ( const char * ) ( buffer . data ( ) ) , ( uint32_t ) buffer . size ( ) ) ;
}
2018-09-05 11:26:21 +08:00
}
////////////////////////////////////////////////////////////////////////////
}