Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 101

慕言/Tars

forked from TarsCloud/Tars
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (2)
Tags (4)
master
tars-java-dev
v1.3.0
v1.2.0
v1.1.0
v1.0.1
master
Branches (2)
Tags (4)
master
tars-java-dev
v1.3.0
v1.2.0
v1.1.0
v1.0.1
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (2)
Tags (4)
master
tars-java-dev
v1.3.0
v1.2.0
v1.1.0
v1.0.1
Tars
/
cpp
/
framework
/
NodeServer
/
KeepAliveThread.cpp
Tars
/
cpp
/
framework
/
NodeServer
/
KeepAliveThread.cpp
KeepAliveThread.cpp 10.71 KB
Copy Edit Raw Blame History
suziliu authored 2017年04月14日 17:07 +08:00 . fix log level
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL 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 "KeepAliveThread.h"
#include "RegistryProxy.h"
#include "NodeRollLogger.h"
#include "util/tc_timeprovider.h"
#include "util.h"
KeepAliveThread::KeepAliveThread()
: _terminate(false)
, _registryPrx(NULL)
{
_runTime = time(0);
_nodeInfo = _platformInfo.getNodeInfo();
_heartTimeout = TC_Common::strto<int>(g_pconf->get("/tars/node/keepalive<heartTimeout>", "10"));
_monitorInterval = TC_Common::strto<int>(g_pconf->get("/tars/node/keepalive<monitorInterval>", "2"));
_monitorIntervalMs = TC_Common::strto<int>(g_pconf->get("/tars/node/keepalive<monitorIntervalMs>", "10"));
_synInterval = TC_Common::strto<int>(g_pconf->get("/tars/node/keepalive<synStatInterval>", "60"));
_synStatBatch = g_pconf->get("/tars/node/keepalive<synStatBatch>", "Y");
_monitorInterval = _monitorInterval > 10 ? 10 : (_monitorInterval < 1 ? 1 : _monitorInterval);
}
KeepAliveThread::~KeepAliveThread()
{
terminate();
}
void KeepAliveThread::terminate()
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << endl;
_terminate = true;
if (isAlive())
{
_lock.notifyAll();
getThreadControl().join();
}
}
bool KeepAliveThread::timedWait(int millsecond)
{
TC_ThreadLock::Lock lock(_lock);
if (_terminate)
{
return true;
}
return _lock.timedWait(millsecond);
}
void KeepAliveThread::run()
{
bool bLoaded = false;
bool bRegistered = false;
while (!_terminate)
{
int64_t startMs = TC_TimeProvider::getInstance()->getNowMs();
try
{
//获取主控代理
if (!_registryPrx)
{
_registryPrx = AdminProxy::getInstance()->getRegistryProxy();
if (!_registryPrx)
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "RegistryProxy init fail ! retry it after " + TC_Common::tostr(_monitorInterval) + " second" << endl;
}
}
//注册node信息
if (_registryPrx && bRegistered == false)
{
bRegistered = registerNode();
}
//加载服务
if (bLoaded == false)
{
bLoaded = loadAllServers();
}
//检查服务的limit配置是否需要更新
if (bLoaded)
{
ServerFactory::getInstance()->setAllServerResourceLimit();
}
//检查服务
checkAlive();
// 上报node状态
if (reportAlive() != 0)
{
bRegistered = false; //registry服务重启 需要重新注册
}
}
catch (exception& e)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch exception|" << e.what() << endl;
}
catch (...)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch unkown exception|" << endl;
}
{
int64_t useMs = (TC_TimeProvider::getInstance()->getNowMs() - startMs);
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "run use:" << useMs << " ms" << endl;
}
timedWait(ServerFactory::getInstance()->getMinMonitorIntervalMs());
}
}
bool KeepAliveThread::registerNode()
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "registerNode begin===============|node name|" << _nodeInfo.nodeName << endl;
try
{
int iRet = _registryPrx->registerNode(_nodeInfo.nodeName, _nodeInfo, _platformInfo.getLoadInfo());
if (iRet == 0)
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "register node succ" << endl;
return true;
}
}
catch (exception& e)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "KeepAliveThread::registerNode catch exception|" << e.what() << endl;
}
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "register node fail" << endl;
return false;
}
bool KeepAliveThread::loadAllServers()
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "load server begin===============|node name|" << _nodeInfo.nodeName << endl;
/**
* 由于加载失败或者node下没有部署服务,这里就会一直去访问主控
* 增加这个限制,如果超过5次失败,则不去加载,10分钟重试10次
* 如果之后有新服务部署,会自动添加到node缓存中,不影响监控流程
*/
static int iFailNum = 0;
if (iFailNum >= 5)
{
static time_t tLastLoadTime = 0;
if ((TNOW - tLastLoadTime) < 600)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "load server fail" << endl;
return false;
}
tLastLoadTime = TNOW;
iFailNum = 0;
}
try
{
if (ServerFactory::getInstance()->loadServer())
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "load server succ" << endl;
return true;
}
}
catch (exception& e)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch exception|" << e.what() << endl;
}
iFailNum++;
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "load server fail" << endl;
return false;
}
int KeepAliveThread::reportAlive()
{
try
{
static time_t tReport;
time_t tNow = TNOW;
if (tNow - tReport > _heartTimeout)
{
tReport = tNow;
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "node keep alive ----------------------------------------------------|" << time(0) << "|" << pthread_self() << '\n' << endl;
int iRet = _registryPrx->keepAlive(_nodeInfo.nodeName, _platformInfo.getLoadInfo());
return iRet;
}
}
catch (exception& e)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "KeepAliveThread::reportAlive catch exception|" << e.what() << endl;
}
return 0;
}
int KeepAliveThread::synStat()
{
try
{
int iRet = -1;
vector<ServerStateInfo> v;
_stat.swap(v);
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "node syn stat size|" << v.size() << "|" << _synStatBatch << "|" << pthread_self() << endl;
if (v.size() > 0)
{
iRet = _registryPrx->updateServerBatch(v);
}
return iRet;
}
catch (exception& e)
{
string s = e.what();
if (s.find("server function mismatch exception") != string::npos)
{
_synStatBatch = "N";
}
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << "catch exception|" << s << endl;
}
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "fail,set SynStatBatch = " << _synStatBatch << endl;
return -1;
}
void KeepAliveThread::checkAlive()
{
int64_t startMs = TC_TimeProvider::getInstance()->getNowMs();
static time_t tSyn = 0;
bool bNeedSynServerState = false;
time_t tNow = TNOW;
if (tNow - tSyn > _synInterval)
{
tSyn = tNow;
bNeedSynServerState = true;
}
string sServerId;
_stat.clear();
map<string, ServerGroup> mmServerList = ServerFactory::getInstance()->getAllServers();
map<string, ServerGroup>::const_iterator it = mmServerList.begin();
for (; it != mmServerList.end(); it++)
{
map<string, ServerObjectPtr>::const_iterator p = it->second.begin();
for (; p != it->second.end(); p++)
{
try
{
sServerId = it->first + "." + p->first;
ServerObjectPtr pServerObjectPtr = p->second;
if (!pServerObjectPtr)
{
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << sServerId << "|=NULL|" << endl;
continue;
}
pServerObjectPtr->doMonScript();
if (TNOW - _runTime < ServantHandle::HEART_BEAT_INTERVAL * 5)
{
//等待心跳包
continue;
}
//corelimit checking
pServerObjectPtr->checkCoredumpLimit();
//(checkServer时,上报Server所占用的内存给主控)
pServerObjectPtr->checkServer(_heartTimeout);
//cache 同步server 状态
if (bNeedSynServerState == true)
{
ServerInfo tServerInfo;
tServerInfo.application = it->first;
tServerInfo.serverName = p->first;
ServerDescriptor tServerDescriptor = pServerObjectPtr->getServerDescriptor();
tServerDescriptor.settingState = pServerObjectPtr->isEnabled() == true ? "active" : "inactive";
g_serverInfoHashmap.set(tServerInfo, tServerDescriptor);
ServerStateInfo tServerStateInfo;
tServerStateInfo.serverState = (pServerObjectPtr->IsEnSynState() ? pServerObjectPtr->getState() : tars::Inactive);
tServerStateInfo.processId = pServerObjectPtr->getPid();
tServerStateInfo.nodeName = _nodeInfo.nodeName;
tServerStateInfo.application = it->first;
tServerStateInfo.serverName = p->first;
if (TC_Common::lower(_synStatBatch) == "y")
{
_stat.push_back(tServerStateInfo);
}
else
{
pServerObjectPtr->asyncSynState();
}
}
}
catch (exception& e)
{
NODE_LOG("KeepAliveThread")->error() << FILE_FUN << sServerId << " catch exception|" << e.what() << endl;
}
}
}
if (bNeedSynServerState)
{
synStat();
}
{
int64_t useMs = (TC_TimeProvider::getInstance()->getNowMs() - startMs);
NODE_LOG("KeepAliveThread")->debug() << FILE_FUN << "checkAlive use:" << useMs << " ms" << endl;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Tars 是基于名字服务使用 Tars 协议的高性能 RPC 开发框架,同时配套一体化的服务治理平台,帮助个人或者企业快速的以微服务的方式构建自己稳定可靠的分布式应用
No labels
Apache-2.0
Use Apache-2.0
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jinlongcode/Tars.git
git@gitee.com:jinlongcode/Tars.git
jinlongcode
Tars
Tars
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /