Skip to content

Commit

Permalink
support tars cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Mar 31, 2020
1 parent 524799f commit 0703a8f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 85 deletions.
1 change: 0 additions & 1 deletion servant/libservant/AsyncProcThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ void AsyncProcThread::callback(ReqMessage * msg)
pServantProxyThreadData->_dyeing = msg->bDyeing;
pServantProxyThreadData->_dyeingKey = msg->sDyeingKey;

pServantProxyThreadData->_hasCookie = msg->hasCookie;
pServantProxyThreadData->_cookie = msg->cookie;

if(msg->adapter)
Expand Down
70 changes: 42 additions & 28 deletions servant/libservant/ServantHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,51 +675,66 @@ bool ServantHandle::processDye(const TarsCurrentPtr &current, string& dyeingKey)

if (IS_MSG_TYPE(current->getMessageType(), tars::TARSMESSAGETYPEDYED))
{
TLOGTARS("[TARS] servant got a dyeing request, message_type set" << current->getMessageType() << endl);
TLOGTARS("[TARS] servant got a dyeing request, message_type set: " << current->getMessageType() << endl);

if (dyeingIt != current->getRequestStatus().end())
{
TLOGTARS("[TARS] servant got a dyeing request, dyeing key:" << dyeingIt->second << endl);
TLOGTARS("[TARS] servant got a dyeing request, dyeing key: " << dyeingIt->second << endl);

dyeingKey = dyeingIt->second;
}
return true;
}

//servant已经被染色, 开启染色日志
if (ServantHelperManager::getInstance()->isDyeing())
{
map<string, string>::const_iterator dyeingKeyIt = current->getRequestStatus().find(ServantProxy::STATUS_GRID_KEY);

if (dyeingKeyIt != current->getRequestStatus().end() &&
ServantHelperManager::getInstance()->isDyeingReq(dyeingKeyIt->second, current->getServantName(), current->getFuncName()))
{
TLOGTARS("[TARS] dyeing servant got a dyeing req, key:" << dyeingKeyIt->second << endl);

dyeingKey = dyeingKeyIt->second;

return true;
}
}
// //servant已经被染色, 开启染色日志
// if (ServantHelperManager::getInstance()->isDyeing())
// {
// map<string, string>::const_iterator dyeingKeyIt = current->getRequestStatus().find(ServantProxy::STATUS_GRID_KEY);
//
// if (dyeingKeyIt != current->getRequestStatus().end() &&
// ServantHelperManager::getInstance()->isDyeingReq(dyeingKeyIt->second, current->getServantName(), current->getFuncName()))
// {
// TLOGTARS("[TARS] dyeing servant got a dyeing req, key:" << dyeingKeyIt->second << endl);
//
// dyeingKey = dyeingKeyIt->second;
//
// return true;
// }
// }

return false;
}


bool ServantHandle::processCookie(const TarsCurrentPtr &current, map<string, string> &cookie)
{
map<string, string>::const_iterator cookieIt = current->getContext().find(ServantProxy::STATUS_COOKIE);
if (cookieIt != current->getContext().end())
{
TLOGINFO("[TARS] cookie:" << cookieIt->second << endl);
const static string STATUS = "STATUS_";

Cookie stCookie;
stCookie.readFromJsonString(cookieIt->second);
cookie = stCookie.cookie;
return true;
}
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));
});

return false;
return !cookie.empty();
//
// map<string, string>::const_iterator cookieIt = current->getRequestStatus().find(ServantProxy::STATUS_COOKIE);
// if (cookieIt != current->getContext().end())
// {
// std::for_each(pSptd->_cookie.begin(), pSptd->_cookie.end(),[&](map<string, string>::value_type& p){
// req.status.insert(make_pair(p.first, p.second));
// });
//
//// TLOGINFO("[TARS] cookie:" << cookieIt->second << endl);
//
// Cookie stCookie;
// stCookie.readFromJsonString(cookieIt->second);
// cookie = stCookie.cookie;
// return true;
// }
//
// return false;

}

Expand Down Expand Up @@ -837,7 +852,6 @@ void ServantHandle::handleTarsProtocol(const TarsCurrentPtr &current)
current->setCookie(cookie);
}


#ifdef _USE_OPENTRACKING
//处理tracking信息
processTracking(current);
Expand Down
47 changes: 24 additions & 23 deletions servant/libservant/ServantProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,21 @@ void coroWhenAll(const CoroParallelBasePtr &ptr)
///////////////////////////////////////////////////////////////
string ServantProxy::STATUS_DYED_KEY = "STATUS_DYED_KEY";

string ServantProxy::STATUS_GRID_KEY = "STATUS_GRID_KEY";
//string ServantProxy::STATUS_GRID_KEY = "STATUS_GRID_KEY";

string ServantProxy::STATUS_SAMPLE_KEY = "STATUS_SAMPLE_KEY";
//string ServantProxy::STATUS_SAMPLE_KEY = "STATUS_SAMPLE_KEY";

string ServantProxy::STATUS_RESULT_CODE = "STATUS_RESULT_CODE";

string ServantProxy::STATUS_RESULT_DESC = "STATUS_RESULT_DESC";

string ServantProxy::STATUS_SETNAME_VALUE = "STATUS_SETNAME_VALUE";

string ServantProxy::TARS_MASTER_KEY = "TARS_MASTER_KEY";
//string ServantProxy::TARS_MASTER_KEY = "TARS_MASTER_KEY";

string ServantProxy::STATUS_TRACK_KEY = "STATUS_TRACK_KEY";

string ServantProxy::STATUS_COOKIE = "STATUS_COOKIE";
string ServantProxy::STATUS_COOKIE = "STATUS_COOKIE";


////////////////////////////////////
Expand Down Expand Up @@ -573,10 +573,8 @@ void ServantProxy::invoke(ReqMessage * msg, bool bCoroAsync)
TLOGTARS("[TARS][ServantProxy::invoke, set dyeing, key=" << pSptd->_dyeingKey << endl);
}

msg->hasCookie = pSptd->_hasCookie;
msg->cookie = pSptd->_cookie;


#ifdef _USE_OPENTRACKING
msg->trackInfoMap = pSptd->_trackInfoMap;
#endif
Expand Down Expand Up @@ -777,11 +775,11 @@ void ServantProxy::tars_invoke_async(char cPacketType,
msg->request.status = status;
msg->request.iTimeout = _asyncTimeout;

// 在RequestPacket中的context设置主调信息
if(_masterFlag)
{
msg->request.context.insert(std::make_pair(TARS_MASTER_KEY,ClientConfig::ModuleName)); //TARS_MASTER_KEY clientConfig.ModuleName
}
// // 在RequestPacket中的context设置主调信息
// if(_masterFlag)
// {
// msg->request.context.insert(std::make_pair(TARS_MASTER_KEY,ClientConfig::ModuleName)); //TARS_MASTER_KEY clientConfig.ModuleName
// }

checkDye(msg->request);

Expand Down Expand Up @@ -811,12 +809,11 @@ shared_ptr<ResponsePacket> ServantProxy::tars_invoke(char cPacketType,
msg->request.status = status;
msg->request.iTimeout = _syncTimeout;

// 在RequestPacket中的context设置主调信息
if(_masterFlag)
{
msg->request.context.insert(std::make_pair(TARS_MASTER_KEY,ClientConfig::ModuleName));
}

// // 在RequestPacket中的context设置主调信息
// if(_masterFlag)
// {
// msg->request.context.insert(std::make_pair(TARS_MASTER_KEY,ClientConfig::ModuleName));
// }

checkDye(msg->request);

Expand Down Expand Up @@ -992,12 +989,16 @@ void ServantProxy::checkCookie(RequestPacket& req)
//线程私有数据
ServantProxyThreadData * pSptd = ServantProxyThreadData::getData();
assert(pSptd != NULL);
if(pSptd->_hasCookie)
{
Cookie stCookie;
stCookie.cookie = pSptd->_cookie;
req.context[ServantProxy::STATUS_COOKIE] = stCookie.writeToJsonString();
}

std::for_each(pSptd->_cookie.begin(), pSptd->_cookie.end(),[&](map<string, string>::value_type& p){
req.status.insert(make_pair(p.first, p.second));
});

// Cookie stCookie;
// stCookie.cookie = pSptd->_cookie;
//
// req.status.insert(make_pair());
// req.context[ServantProxy::STATUS_COOKIE] = stCookie.writeToJsonString();
}


Expand Down
5 changes: 1 addition & 4 deletions servant/servant/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ struct ReqMessage : public TC_HandleBase
, bCoroFlag(false)
, sched(NULL)
, iCoroId(0)
, hasCookie(0)
{
}

Expand Down Expand Up @@ -251,7 +250,6 @@ struct ReqMessage : public TC_HandleBase
bCoroFlag = false;
sched = NULL;
iCoroId = 0;
hasCookie = false;
}


Expand Down Expand Up @@ -295,8 +293,7 @@ struct ReqMessage : public TC_HandleBase
std::unordered_map<std::string, std::string> trackInfoMap; //调用链信息
#endif

bool hasCookie; // 是否包含cookie
map<string, string> cookie; // cookie内容
map<string, string> cookie; // cookie内容
};

typedef TC_AutoPtr<ReqMessage> ReqMessagePtr;
Expand Down
9 changes: 4 additions & 5 deletions servant/servant/ServantProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ class ServantProxyThreadData
/**
* cookie
*/
bool _hasCookie; // 是否包含cookie
map<string, string> _cookie; // cookie内容
map<string, string> _cookie; // cookie内容
};


Expand Down Expand Up @@ -398,17 +397,17 @@ class ServantProxy : public TC_HandleBase, public TC_ThreadMutex
*/
static string STATUS_DYED_KEY; //需要染色的用户ID

static string STATUS_GRID_KEY; //需要灰度的用户ID
// static string STATUS_GRID_KEY; //需要灰度的用户ID

static string STATUS_SAMPLE_KEY; //stat 采样的信息
// static string STATUS_SAMPLE_KEY; //stat 采样的信息

static string STATUS_RESULT_CODE; //处理结果码,tup使用

static string STATUS_RESULT_DESC; //处理结果描述,tup使用

static string STATUS_SETNAME_VALUE; //set调用

static string TARS_MASTER_KEY; //透传主调名称信息
// static string TARS_MASTER_KEY; //透传主调名称信息

static string STATUS_TRACK_KEY; //track信息

Expand Down
21 changes: 4 additions & 17 deletions servant/servant/TarsCookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class TarsCookieOp
* 构造函数
*/
TarsCookieOp()
:_hasCookie(false)
{
}

Expand All @@ -37,16 +36,11 @@ class TarsCookieOp
*/
~TarsCookieOp()
{
if(_hasCookie)
ServantProxyThreadData * td = ServantProxyThreadData::getData();
assert(NULL != td);
if (td)
{
ServantProxyThreadData * td = ServantProxyThreadData::getData();
assert(NULL != td);
if (td)
{
td->_hasCookie = false;
td->_cookie.clear();
}
_hasCookie = false;
td->_cookie.clear();
}
}

Expand All @@ -70,16 +64,9 @@ class TarsCookieOp
assert(NULL != td);
if(td)
{
td->_hasCookie = true;
td->_cookie = cookie;
}

_hasCookie = true;

}

protected:
bool _hasCookie;
};

#endif
6 changes: 0 additions & 6 deletions servant/tup/RequestF.tars
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,4 @@ module tars
8 optional string sResultDesc;
9 optional map<string, string> context;
};

// Cookie, 在服务调用链上透传
struct Cookie
{
1 optional map<string, string> cookie;
};
};
5 changes: 4 additions & 1 deletion util/include/util/tc_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ namespace tars
class TC_Port
{
public:
static int strcmp(const char *s1, const char *s2);

static int strcasecmp(const char *s1, const char *s2);
static int strncmp(const char *s1, const char *s2, size_t n);

static int strcasecmp(const char *s1, const char *s2);

static int strncasecmp(const char *s1, const char *s2, size_t n);

Expand Down
19 changes: 19 additions & 0 deletions util/src/tc_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ using namespace std;

namespace tars
{

int TC_Port::strcmp(const char *s1, const char *s2)
{
#if TARGET_PLATFORM_WINDOWS
return ::_strcmp(s1, s2);
#else
return ::strcmp(s1, s2);
#endif
}

int TC_Port::strncmp(const char *s1, const char *s2, size_t n)
{
#if TARGET_PLATFORM_WINDOWS
return ::_strncmp(s1, s2, n);
#else
return ::strncmp(s1, s2, n);
#endif
}

int TC_Port::strcasecmp(const char *s1, const char *s2)
{
#if TARGET_PLATFORM_WINDOWS
Expand Down

0 comments on commit 0703a8f

Please sign in to comment.