开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
开源项目 > WEB应用开发 > RPC开发框架 &&
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
7 Star 4 Fork 7

cxxjava/Tars

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
DbProxy.cpp 28.37 KB
一键复制 编辑 原始数据 按行查看 历史
suziliu 提交于 2017年01月18日 16:19 +08:00 . tars 1.0.1
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
/**
* 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 "DbProxy.h"
#include <time.h>
///////////////////////////////////////////////////////////
string tFlagInc(const string& stflag);
string dateInc(const string& sDate);
void selectLastMinTime(const string& sUid, int iThread , const string& tbname, const TC_DBConf& tcDbInfo, string & ret, QueryParam &queryParam);
void query(int iThread, const TC_DBConf & conf, map<string,string>& mSqlPart, map<string, vector<double> > &result, string &sRes, QueryParam &queryParam,string &sPolicy);
DbProxy::DbProxy()
{
}
DbProxy::~DbProxy()
{
}
int DbProxy::createRespHead(const vector<string> &res, const string& sLasttime, string& result, bool bDbCountFlag)
{
int iRet = 0;
string sRes;
//检查查询返回值,如果一个线程失败,就返回失败。
for(size_t i=0; i< res.size(); i++)
{
sRes += res[i] ;
if ( res[i][4] != '0' && iRet == 0)
{
iRet = -1;
}
}
//int total = bDbCountFlag ? g_app.getDbNumber() : g_app.getDbNumber();
int total = g_app.getDbNumber();
result = "Ret:";
result += TC_Common::tostr(iRet);
result += "\nlasttime:";
result += sLasttime;
result += "\nActiveDb:";
result += TC_Common::tostr(res.size());
result += "\nTotalDb:";
result += TC_Common::tostr(total);
result += "\n";
result += sRes;
return iRet;
}
int DbProxy::createRespData(const string& sUid, const map<string,string>& mSqlPart, const vector<map<string, vector<double> > >& vDataList, const string& sHead, string &result, string& sPolicy)
{
// 组合多线程结果
//map first由goupby生成
//map second 由index生成
int64_t tStart = TNOWMS;
vector<map<string, vector<double> > >::const_iterator dataItr = vDataList.begin();
map<string, vector<double> > mStatData;
map<string, vector<double> >::iterator _it;
for(size_t i = 0; dataItr != vDataList.end(); dataItr++, i++)
{
TLOGDEBUG(sUid << "sum["<<i<<"].size"<< ":" << dataItr->size() << endl);
for(map<string, vector<double> >::const_iterator it = dataItr->begin(); it != dataItr->end(); it++)
{
_it = mStatData.find(it->first);
if (_it != mStatData.end())
{
const vector<double> &number1 = it->second;
vector<double> &number2 = _it->second;
// 相同key的值 求和,number1和number1的大小是一样的
for (size_t j=0; j<number1.size(); j++)
{
number2.push_back(number1[j]);
}
}
else
{
mStatData[it->first] = it->second;
}
}
// dataItr->clear();
}
string groupField("");
string sTemp("");
// int iLineNum = 0;
for(_it = mStatData.begin(); _it != mStatData.end(); _it++)
{
string sKey = _it->first;
vector<double> &vValue = _it->second;
if(sPolicy == "Avg")
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
iResult += vValue[i];
}
TLOGDEBUG(sUid << "iResult:" << iResult << "|size:" << vValue.size() << endl);
if(vValue.size() == 0)
{
iResult = 0;
}
else
{
iResult = iResult / vValue.size();
}
TLOGDEBUG(sUid << "Policy Avg iResult:" << iResult << "|size:" << vValue.size() << endl);
vValue.clear();
vValue.push_back(iResult);
}
else if(sPolicy == "Max")
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
if(i == 0)
{
iResult = vValue[i];
}
if(iResult < vValue[i])
{
iResult = vValue[i];
}
}
TLOGDEBUG(sUid << "Policy Max iResult:" << iResult << "|size:" << vValue.size() << endl);
vValue.clear();
vValue.push_back(iResult);
}
else if(sPolicy == "Min")
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
if(i == 0)
{
iResult = vValue[i];
}
if(iResult > vValue[i])
{
iResult = vValue[i];
}
}
TLOGDEBUG(sUid << "Policy Min iResult:" << iResult << "|size:" << vValue.size() << endl);
vValue.clear();
vValue.push_back(iResult);
}
else
{
string::size_type position;
if((position =sKey.find("Avg")) != string::npos)
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
iResult += vValue[i];
}
TLOGDEBUG(sUid << "Avg iResult:" << iResult << "|size:" << vValue.size() << endl);
if(vValue.size() == 0)
{
iResult = 0;
}
else
{
iResult = iResult / vValue.size();
}
vValue.clear();
vValue.push_back(iResult);
}
else if((position =sKey.find("Min")) != string::npos)
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
if(i == 0)
{
iResult = vValue[i];
}
if(iResult > vValue[i])
{
iResult = vValue[i];
}
}
TLOGDEBUG(sUid << "Min iResult:" << iResult << "|size:" << vValue.size() << endl);
vValue.clear();
vValue.push_back(iResult);
}
else if((position =sKey.find("Max")) != string::npos)
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
if(i == 0)
{
iResult = vValue[i];
}
if(iResult < vValue[i])
{
iResult = vValue[i];
}
}
TLOGDEBUG(sUid << "Max iResult:" << iResult << "|size:" << vValue.size() << endl);
vValue.clear();
vValue.push_back(iResult);
}
else
{
double iResult = 0;
for(size_t i = 0; i < vValue.size(); ++i)
{
iResult += vValue[i];
}
TLOGDEBUG(sUid << "Sum iResult:" << iResult << "|size:" << vValue.size() << endl);
vValue.clear();
vValue.push_back(iResult);
}
}
}
result += sHead + "linecount:" + TC_Common::tostr(mStatData.size()) + "\n";
//把 查询结果转换成一行一行的串
/*
* input :groupby, f_date, f_tflag
* input : index, succ_count, timeout_count
*all map <string, vector<double> >
*string =>> f_date, f_tflag
*vector<double> =>> succ_count, timeout_count
*/
_it = mStatData.begin();
while(_it != mStatData.end())
{
string valueBuffer = "";
vector<double>::iterator valueIt = _it->second.begin();
while(valueIt != _it->second.end()) // value is vector int, need transfer to string;
{
valueBuffer += TC_Common::tostr(*valueIt) + ",";
valueIt++;
}
result += _it->first + ",";
result += valueBuffer + "\n";
_it++;
}
TLOGDEBUG("result:"<<result<<endl);
int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs();
//int64_t tEnd = TNOWMS;
TLOGDEBUG("DbProxy::createRespData "<< sUid << "createRespData size:"<< result.length() << "|timecost(ms):" << (tEnd-tStart) << endl);
return 0;
}
/**
* 通过线程池进行并发查询
*/
void DbProxy::queryData(map<string, string> &mSqlPart, string &sResult, bool bDbCountFlag)
{
typedef TC_Functor<void, TL::TLMaker<int, const TC_DBConf &, map<string,string>&, map<string,vector<double> >&, string&, QueryParam &,string&>::Result> QueryFun;
typedef QueryFun::wrapper_type QueryFunWrapper;
try
{
string sUid = mSqlPart.find("uid")->second;
vector<TC_DBConf> vActive;
if(bDbCountFlag)
{
vActive = g_app.getActiveDbInfo();
}
else
{
//vActive = g_app.getActiveDbInfo();
}
int iThreads = vActive.size();
//int iThreads = bDbCountFlag ? g_app.getDbNumber() : g_app.getDbNumber();
if(iThreads > 0)
{
vector<string> vPolicy(iThreads);
vector<string> res(iThreads);
vector<map<string, vector<double> > > vDataList(iThreads);
QueryFun qeryCMD(query);
_queryParam._run_times = iThreads;
//TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "all thread query data begin." << endl);
int64_t tStart = TC_TimeProvider::getInstance()->getNowMs();
for(int i=0; i < iThreads; i++)
{
QueryFunWrapper fwrapper(qeryCMD, i, vActive[i], mSqlPart, vDataList[i], res[i], _queryParam,vPolicy[i]);
if(bDbCountFlag)
{
g_app.getThreadPoolDb().exec(fwrapper);
}
else
{
//g_app.getThreadPoolDb().exec(fwrapper);
}
}
//等待线程结束
TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "wait for all thread query data done." << endl);
bool rc = true;
int ifail = 0;
while(_queryParam._atomic.get() != _queryParam._run_times)
{
{
TC_ThreadLock::Lock lock(_queryParam._monitor);
rc = _queryParam._monitor.timedWait(6000);
}
++ifail;
if(!rc)
{
if(ifail >= 10)
{
break;
}
}
}
if(ifail >= 10)
{
TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "wait for all thread query data timeout." << endl);
while(_queryParam._atomic.get() != _queryParam._run_times)
{
{
TC_ThreadLock::Lock lock(_queryParam._monitor);
_queryParam._monitor.timedWait(1000);
}
}
}
if(_queryParam._atomic.get() == _queryParam._run_times)
rc = true;
/*bool rc = false;
{
TC_ThreadLock::Lock lock(_queryParam._monitor);
rc = _queryParam._monitor.timedWait(60000);
}*/
int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs();
if(rc)
{
_queryParam._run_times = 0;
_queryParam._run_result = 0;
_queryParam._atomic = 0;
TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "all thread done return:" << _queryParam._run_result <<"|timecost(ms):" << (tEnd - tStart) << endl);
// 返回ret code
string sHead;
string sLasttime = getLastTime(mSqlPart);
if(createRespHead(res, sLasttime, sHead, bDbCountFlag) != 0)
{
_queryParam._run_times = 0;
_queryParam._run_result = 0;
_queryParam._atomic = 0;
sResult = sHead;
TLOGERROR("DbProxy::queryData query error:" << sHead << endl);
return;
}
createRespData(sUid, mSqlPart, vDataList, sHead, sResult,vPolicy[vPolicy.size()-1]);
}
else
{
sResult ="Ret:-1\nquery timeout\n";
TLOGDEBUG("DbProxy::queryData sUid:" << sUid << "Ret:-1|query timeout." << endl);
}
}
else
{
sResult ="Ret:-1\nno active db\n";
}
}
catch (exception &ex)
{
TLOGERROR("DbProxy::queryData exception:" << ex.what() << endl);
sResult ="Ret:-1\n" + string(ex.what()) + "\n";
}
_queryParam._run_times = 0;
_queryParam._run_result = 0;
_queryParam._atomic = 0;
}
void query(int iThread, const TC_DBConf & conf, map<string,string>& mSqlPart, map<string, vector<double> > &result, string &sRes, QueryParam &queryParam,string &sPolicy)
{
string sUid = mSqlPart.find("uid")->second;
TLOGDEBUG("queryData " << sUid << "thread iIndex:" << iThread << endl);
int64_t tStart = TNOWMS;
try
{
//dateFrom =>> 20111120
string dateFrom = mSqlPart["date1"];
string dateTo = mSqlPart["date2"];
//tflagFrom =>> 2360
string tflagFrom = mSqlPart["tflag1"];
string tflagTo = mSqlPart["tflag2"];
// 输入tflag 条件检查
if (dateFrom.length() != 8 || dateTo.length() != 8 || tflagFrom.length() != 4 ||
tflagTo.length() != 4 ||
TC_Common::isdigit(tflagFrom) == false ||
TC_Common::isdigit(tflagTo) == false)
{
sRes += "ret:-1|iDb:" + TC_Common::tostr(iThread) + "|wrong tflag:" + tflagFrom + "-" + tflagTo + "\n";
TLOGERROR("query sUid:" << sUid << sRes << endl);
queryParam._run_result = -1;
queryParam._atomic.inc();
if(queryParam._atomic.get() == queryParam._run_times)
{
TC_ThreadLock::Lock lock(queryParam._monitor);
queryParam._monitor.notifyAll();
}
return ;
}
//groupCond =>> "where slave_name like 'MTTsh2.BrokerServer' and f_tflag >='0000' and f_tflag <='2360' and f_date = '20111120'"
string whereCond = mSqlPart["whereCond"];
string::size_type position;
if((position =whereCond.find("policy")) != string::npos)
{
string temp = whereCond.substr(position);
if((position =whereCond.find("Avg")) != string::npos)
{
sPolicy = "Avg";
}
else if((position =whereCond.find("Max")) != string::npos)
{
sPolicy = "Max";
}
else if((position =whereCond.find("Min")) != string::npos)
{
sPolicy = "Min";
}
else
{
sPolicy = "NULL";
}
}
//groupCond =>> "group by f_date, f_tflag"
string groupCond = mSqlPart["groupCond"];
//sumField =>> "succ_count, timeout_count";
string sumField = mSqlPart["sumField"];
//groupField =>> "f_date, f_tflag"
string groupField = mSqlPart["groupField"];
//selectCond =>> "succ_count, timeout_count, f_date, f_tflag"
string selectCond = sumField +"," + groupField;
//日期格式20111019
string::size_type pos = string::npos;
string tmpDate = "f_date";
if ((pos = selectCond.find(tmpDate, 0)) != string::npos)
{
selectCond.replace(pos, tmpDate.length(), "DATE_FORMAT( f_date, '%Y%m%d') as f_date");
}
string sDbName = mSqlPart["dataid"];
string ignoreKey("");
vector<string> vGroupField = TC_Common::sepstr<string>(groupField, ", ");
vector<string> vSumField = TC_Common::sepstr<string>(sumField, ", ");
TC_Mysql tcMysql;
TC_DBConf tcDbConf = conf;
/*if(bFlag)
{
tcDbConf = g_app.getDbInfo(iThread);
}
else
{
tcDbConf = g_app.getDbInfo(iThread);
}*/
tcDbConf._database = sDbName;
tcMysql.init(tcDbConf);
string sTbNamePre = tcDbConf._database + "_";
string sTbName("");
string sSql("");
//select range by f_date and f_tflag
for(string day = dateFrom; day <= dateTo; day = dateInc(day))
{
for(string tflag = tflagFrom; tflag <= tflagTo && (tflag.substr(0,2) < "24"); tflag = tFlagInc(tflag))
{
//table name:tars_2012060723
sTbName = sTbNamePre + day + tflag.substr(0,2);
sSql = "select " + selectCond + " from " + sTbName + " " + ignoreKey + whereCond + " order by null;";
tars::TC_Mysql::MysqlData res = tcMysql.queryRecord(sSql);
TLOGINFO(sUid << "res.size:" << res.size() << "|sSql:" << sSql << endl);
// result is key:value pair;
//sKey 由groupby生成
//value由index生成
//int64_t t2Start = TNOWMS;
for(size_t iRow = 0; iRow < res.size(); iRow++)
{
string sKey = "";
for(size_t j = 0; j < vGroupField.size(); j++)
{
sKey += sKey.empty()?"":",";
sKey += res[iRow][vGroupField[j]];
}
map<string,vector<double> >::iterator itResult = result.find(sKey);
if (itResult != result.end())
{
vector<double>& data = itResult->second;
for (size_t j = 0; j < vSumField.size() && j < data.size(); j++)
{
data.push_back(TC_Common::strto<double>(res[iRow][vSumField[j]]));// 相同key的值 求和
}
}
else
{
vector<double>& vRes = result[sKey];
for(size_t j = 0; j < vSumField.size(); j++)
{
vRes.push_back( TC_Common::strto<double>(res[iRow][vSumField[j]]));;
}
}
TLOGINFO("query iDb:" << iThread <<" {"<< sKey << ":" << TC_Common::tostr(result[sKey]) << "}" << endl);
}
TLOGINFO("query iDb :" << iThread << " day:" << day <<" tflag:" << tflag << endl);
}
} //day
sRes = "ret:0 iDb:" + TC_Common::tostr(iThread) + "\n";
//queryParam._atomic.inc();
}
catch(TC_Mysql_Exception & ex)
{
sRes = "ret:-1|iDb:" + TC_Common::tostr(iThread) + string("|exception:") + ex.what() + "\n";
TLOGERROR("query sUid:" << sUid << "query:" << sRes << endl);
queryParam._run_result = -1;
//queryParam._atomic.inc();
}
catch(exception & ex)
{
sRes = "ret:-1|iDb:" + TC_Common::tostr(iThread) + string("|exception:") + ex.what() + "\n";
TLOGERROR("query sUid:" << sUid << "query:" << sRes << endl);
queryParam._run_result = -1;
//queryParam._atomic.inc();
}
int64_t tEnd = TNOWMS;
TLOGDEBUG("query sUid:" << sUid << "exit query iDb:" << iThread <<"|timecost(ms):" << (tEnd - tStart) << "|res:" << sRes << endl);
queryParam._atomic.inc();
if(queryParam._atomic.get() == queryParam._run_times)
{
{
TC_ThreadLock::Lock lock(queryParam._monitor);
queryParam._monitor.notifyAll();
}
TLOGDEBUG("query sUid:" << sUid << "notify query finish." << endl);
}
}
///////////////////////////////////////////////////////////////////////////////
string tFlagInc(const string& stflag)
{
int h = TC_Common::strto<int>(stflag.substr(0,2));
int m = TC_Common::strto<int>(stflag.substr(2,2));
h += 1;
char buf[5];
snprintf(buf,sizeof(buf),"%.2d%.2d",h,m);
return string(buf);
}
///////////////////////////////////////////////////////////////////////////////
string dateInc(const string& sDate)
{
string ret("20991231"); // 返回大数
try
{
int year = TC_Common::strto<int>(sDate.substr(0, 4));
int mon = TC_Common::strto<int>(sDate.substr(4, 2));
int day = TC_Common::strto<int>(sDate.substr(6, 2));
struct tm *p = NULL;
time_t timep;
struct tm tt = {0};
time(&timep);
p=localtime_r(&timep, &tt);
p->tm_mon = mon -1;
p->tm_mday = day +1;
p->tm_year = year -1900 ;
timep = mktime(p);
ret = TC_Common::tm2str(timep, "%Y%m%d");
}
catch(exception & ex)
{
TLOGERROR("DbProxy::dateInc exception:" << ex.what() << endl);
}
return ret;
}
///////////////////////////////////////////////////////////////////////////////
void selectLastMinTime(const string& sUid, int iThread , const string& tbname, const TC_DBConf& tcDbInfo, string & ret, QueryParam &queryParam)
{
string sId = sUid;
try
{
TC_Mysql tcMysql;
//TC_DBConf tcDbConf = tcDbInfo;
//tcDbConf._database = TC_Common::trimright(tbname, "_");
//tcDbConf._database = tbname;
string sTbNamePre = tbname + ".t_ecstatus";
//TLOGDEBUG("selectLastMinTime database name:" << tcDbConf._database << "|tbname:" << tbname << endl);
tcMysql.init(tcDbInfo);
int interval = g_app.getInsertInterval();
time_t now = TC_TimeProvider::getInstance()->getNow();
string sDate,sFlag;
// 排除历史过期数据
string sTime,sHour,sMinute;
time_t t = (now - interval * 60 * 2);
interval = g_app.getInsertInterval();
t = (t/(interval*60))*interval*60; //要求必须为loadIntev整数倍
t = (t%3600 == 0?t-60:t); //要求将9点写作0860
sTime = TC_Common::tm2str(t,"%Y%m%d%H%M");
sDate = sTime.substr(0,8);
sHour = sTime.substr(8,2);
sMinute = sTime.substr(10,2);
sFlag = sHour + (sMinute=="59"?"60":sMinute); //要求将9点写作0860
string sLast = sDate + " " + sFlag;
string sSql = "select min(lasttime) as lasttime from "+ sTbNamePre+" where appname like '" +"%' and lasttime > '" + sLast + "'" ;
tars::TC_Mysql::MysqlData res = tcMysql.queryRecord(sSql);
if (res.size() > 0)
{
TLOGINFO("selectLastTime" << sId << "sSql:" << sSql << "|lasttime:" << res[0]["lasttime"] << endl);
ret = res[0]["lasttime"];
}
else
{
ret = "";
}
//queryParam._atomic.inc();
}
catch(TC_Mysql_Exception & ex)
{
TLOGERROR("selectLastTime sUid="<< sId <<"exception:"<< ex.what() << endl);
ret = "";
queryParam._run_result = -1;
//queryParam._atomic.inc();
}
catch(exception& e)
{
TLOGERROR("selectLastTime sUid="<< sId <<"exception:"<< e.what() << endl);
ret = "";
queryParam._run_result = -1;
//queryParam._atomic.inc();
}
queryParam._atomic.inc();
if(queryParam._atomic.get() == queryParam._run_times)
{
TC_ThreadLock::Lock lock(queryParam._monitor);
queryParam._monitor.notifyAll();
TLOGDEBUG("query sUid:" << sId << "notify checktime finish." << endl);
}
}
///////////////////////////////////////////////////////////////////////////////
string DbProxy::getLastTime(const map<string,string>& mSqlPart)
{
string sUid = mSqlPart.find("uid")->second;
string min = "99999999999"; // 求最小的,初始使用很大的数据
//TLOGDEBUG("mSqlPart"<< mSqlPart.find("dataid")->second <<endl);
try
{
typedef TC_Functor<void, TL::TLMaker<const string &, int, const string &, const TC_DBConf &, string &, QueryParam &>::Result> CheckTimeFun;
typedef CheckTimeFun::wrapper_type CheckTimeFunWrapper;
vector<TC_DBConf> vDbInfo = g_app.getAllActiveDbInfo();
int iThreads = vDbInfo.size();
if(iThreads > 0)
{
vector<string> res(iThreads);
//vector<string> vsTbNamePre(iThreads);
CheckTimeFun qeryCMD(selectLastMinTime);
_queryParam._run_times = iThreads;
int64_t tStart = TC_TimeProvider::getInstance()->getNowMs();
for (int i=0; i< iThreads; i++)
{
//vsTbNamePre[i] = vDbInfo[i]._database + "_";
//vsTbNamePre[i] = vDbInfo[i]._database;
// TLOGDEBUG("mSqlPart"<< mSqlPart["dataid"] <<endl);
// CheckTimeFunWrapper fwrapper(qeryCMD, sUid, i, mSqlPart["dataid"], vDbInfo[i], res[i], _queryParam);
CheckTimeFunWrapper fwrapper(qeryCMD, sUid, i, mSqlPart.find("dataid")->second, vDbInfo[i], res[i], _queryParam);
g_app.getThreadPoolTimeCheck().exec(fwrapper);
}
TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "wait for getLastTime done." << endl);
bool rc = true;
int ifail = 0;
while(_queryParam._atomic.get() != _queryParam._run_times)
{
{
TC_ThreadLock::Lock lock(_queryParam._monitor);
rc = _queryParam._monitor.timedWait(300);
}
++ifail;
if(!rc)
{
if(ifail >= 10)
{
break;
}
}
}
if(ifail >= 10)
{
TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "wait for getLastTime timeout." << endl);
while(_queryParam._atomic.get() != _queryParam._run_times)
{
{
TC_ThreadLock::Lock lock(_queryParam._monitor);
_queryParam._monitor.timedWait(1000);
}
}
}
if(_queryParam._atomic.get() == _queryParam._run_times)
rc = true;
/*bool rc = false;
{
TC_ThreadLock::Lock lock(_queryParam._monitor);
rc = _queryParam._monitor.timedWait(3000);
}*/
int64_t tEnd = TC_TimeProvider::getInstance()->getNowMs();
if(rc)
{
TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "getLastTime all done|return:" << _queryParam._run_result <<"|timecost(ms):" << (tEnd-tStart) << endl);
for(int i = 0; i < iThreads; ++i)
{
if(res[i] < min)
{
min = res[i];
}
}
}
else
{
min = "";
TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "getLastTime timeout." << endl);
}
}
else
{
min = "";
}
TLOGDEBUG("DbProxy::getLastTime sUid:" << sUid << "final lasttime:" << min << endl);
}
catch (exception &ex)
{
TLOGERROR("DbProxy::getLastTime exception:" << ex.what() << endl);
min = "";
}
_queryParam._run_times = 0;
_queryParam._run_result = 0;
_queryParam._atomic = 0;
return min;
}
string DbProxy::makeResult(int iRet, const string& sRes)
{
size_t act = g_app.getActiveDbInfo().size();
int total = g_app.getDbInfo().size();
string result = "Ret:" + TC_Common::tostr(iRet) + "\n"
+ "ActiveDb:" + TC_Common::tostr(act) + "\n"
+ "TotalDb:" + TC_Common::tostr(total) + "\n"
+ sRes;
return result;
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

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

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

取消
提交

简介

腾讯Tars(TAF)框架OSX平台移植版
取消

发行版

暂无发行版

开源评估指数源自 OSS-Compass 评估体系,评估体系围绕以下三个维度对项目展开评估:

1. 开源生态

  • 生产力:来评估开源项目输出软件制品和开源价值的能力。
  • 创新力:用于评估开源软件及其生态系统的多样化程度。
  • 稳健性:用于评估开源项目面对多变的发展环境,抵御内外干扰并自我恢复的能力。

2. 协作、人、软件

  • 协作:代表了开源开发行为中协作的程度和深度。
  • 人:观察开源项目核心人员在开源项目中的影响力,并通过第三方视角考察用户和开发者对开源项目的评价。
  • 软件:从开源项目对外输出的制品评估其价值最终落脚点。也是开源评估最"古老"的主流方向之一"开源软件" 的具体表现。

3. 评估模型

    基于"开源生态"与"协作、人、软件"的维度,找到与该目标直接或间接相关的可量化指标,对开源项目健康与生态进行量化评估,最终形成开源评估指数。

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cxxjava/Tars.git
git@gitee.com:cxxjava/Tars.git
cxxjava
Tars
Tars
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

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