Repository navigation
Expand file tree
/
Copy pathNodeImp.cpp
More file actions
771 lines (682 loc) · 25.6 KB
/
Copy pathNodeImp.cpp
File metadata and controls
771 lines (682 loc) · 25.6 KB
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
/**
* 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 "util/tc_clientsocket.h"
#include "BatchPatchThread.h"
#include "NodeImp.h"
#include "RegistryProxy.h"
#include "NodeServer.h"
#include "CommandStart.h"
#include "CommandNotify.h"
#include "CommandStop.h"
#include "CommandDestroy.h"
#include "CommandAddFile.h"
#include "CommandPatch.h"
#include "NodeRollLogger.h"
#include "AdminReg.h"
#include "util/tc_timeprovider.h"
extern BatchPatch * g_BatchPatchThread;
void NodeImp::initialize()
{
try
{
TLOGDEBUG("initialize NodeImp" << endl);
_nodeInfo = _platformInfo.getNodeInfo();
_downloadPath = _platformInfo.getDownLoadDir();
}
catch ( exception& e )
{
TLOGERROR(FILE_FUN << e.what() << endl);
exit( 0 );
}
}
int NodeImp::destroyServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current )
{
int64_t startMs = TC_TimeProvider::getInstance()->getNowMs();
int iRet = EM_TARS_SUCCESS;
if ( g_app.isValid(current->getIp()) == false )
{
result += " erro:ip "+ current->getIp()+" is invalid";
iRet = EM_TARS_INVALID_IP_ERR;
NODE_LOG("destroyServer")->error() << FILE_FUN << "access_log|" << iRet << "|" << (TC_TimeProvider::getInstance()->getNowMs() - startMs)
<< "|" << application << "." << serverName << "|" << result << endl;
return iRet;
}
result = " ["+application + "." + serverName+"] ";
NODE_LOG("destroyServer")->debug() << result << endl;
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
string s;
CommandDestroy command(pServerObjectPtr);
iRet = command.doProcess(current, s, false);
if ( iRet == 0 && ServerFactory::getInstance()->eraseServer(application, serverName) == 0)
{
pServerObjectPtr = NULL;
result = result+"succ:" + s;
}
else
{
result = FILE_FUN_STR+"error:"+s;
}
}
else
{
result += FILE_FUN_STR+"server is not exist";
iRet = -2;
}
NODE_LOG("destroyServer")->error() << FILE_FUN << "access_log|" << iRet << "|" << (TC_TimeProvider::getInstance()->getNowMs() - startMs)
<< "|" << application << "|" << serverName << "|" << result << endl;
return iRet;
}
int NodeImp::patchPro(const tars::PatchRequest & req, string & result, TarsCurrentPtr current)
{
NODE_LOG("patchPro")->debug() << FILE_FUN
<< req.appname + "." + req.servername + "_" + req.nodename << "|"
<< req.groupname << "|"
<< req.version << "|"
<< req.user << "|"
<< req.servertype << "|"
<< req.patchobj << "|"
<< req.md5 << "|"
<< req.ostype << endl;
if ( g_app.isValid(current->getIp()) == false )
{
result +=FILE_FUN_STR+ " erro:ip "+ current->getIp()+" is invalid";
NODE_LOG("patchPro")->error()<<FILE_FUN << result << endl;
return EM_TARS_INVALID_IP_ERR;
}
try
{
//加载服务信息
string sError("");
ServerObjectPtr server = ServerFactory::getInstance()->loadServer(req.appname, req.servername, true, sError);
if (!server)
{
result =FILE_FUN_STR+ "load " + req.appname + "." + req.servername + "|" + sError;
NODE_LOG("patchPro")->error() <<FILE_FUN<<req.appname + "." + req.servername + "_" + req.nodename << "|" << result << endl;
if(sError.find("server state") != string::npos)
return EM_TARS_SERVICE_STATE_ERR;
else
return EM_TARS_LOAD_SERVICE_DESC_ERR;
}
{
TC_ThreadRecLock::Lock lock(*server);
ServerObject::InternalServerState eState = server->getInternalState();
//去掉启动中不能发布的限制
if (server->toStringState(eState).find("ing") != string::npos && eState != ServerObject::Activating)
{
result = FILE_FUN_STR+"cannot patch the server ,the server state is " + server->toStringState(eState);
NODE_LOG("patchPro")->error() <<FILE_FUN<< req.appname + "." + req.servername << "|" << result << endl;
return EM_TARS_SERVICE_STATE_ERR;
}
if(req.md5 == "")
{
result = FILE_FUN_STR+"parameter error, md5 not setted.";
NODE_LOG("patchPro")->error() <<FILE_FUN<< req.appname + "." + req.servername << "|" << result << endl;
return EM_TARS_PARAMETER_ERR;
}
if(CommandPatch::checkOsType())
{
string osType = CommandPatch::getOsType();
if(req.ostype != "" && req.ostype != osType)
{
result = FILE_FUN_STR+"parameter error, req.ostype=" + req.ostype + ", local osType:" + osType;
NODE_LOG("patchPro")->error() <<FILE_FUN<< req.appname + "." + req.servername << "|" << result << endl;
return EM_TARS_PARAMETER_ERR;
}
}
//保存patching前的状态,patch完成后恢复
NODE_LOG("patchPro")->debug() <<FILE_FUN<<req.appname + "." + req.servername + "_" + req.nodename << "|saved state:" << server->toStringState(eState) << endl;
server->setLastState(eState);
server->setState(ServerObject::BatchPatching);
//将百分比初始化为0,防止上次patch结果影响本次patch进度
server->setPatchPercent(0);
NODE_LOG("patchPro")->debug()<<FILE_FUN << req.appname + "." + req.servername + "_" + req.nodename << "|" << req.groupname << "|preset success" << endl;
}
g_BatchPatchThread->push_back(req,server);
}
catch (std::exception & ex)
{
NODE_LOG("patchPro")->error() <<FILE_FUN<< req.appname + "." + req.servername + "_" + req.nodename << "|Exception:" << ex.what() << endl;
result = FILE_FUN_STR+ex.what();
if(result.find("reduplicate") != string::npos)
{
return EM_TARS_REQ_ALREADY_ERR;
}
return EM_TARS_UNKNOWN_ERR;
}
catch (...)
{
NODE_LOG("patchPro")->error() <<FILE_FUN<< req.appname + "." + req.servername + "_" + req.nodename << "|Unknown Exception" << endl;
result = FILE_FUN_STR+"Unknown Exception";
return EM_TARS_UNKNOWN_ERR;
}
NODE_LOG("patchPro")->debug() <<FILE_FUN
<< req.appname + "." + req.servername + "_" + req.nodename << "|"
<< req.groupname << "|"
<< req.version << "|"
<< req.user << "|"
<< req.servertype << "|return success"<< endl;
return EM_TARS_SUCCESS;
}
int NodeImp::addFile(const string &application,const string &serverName,const string &file, string &result, TarsCurrentPtr current)
{
result = string(__FUNCTION__)+" server ["+application + "." + serverName+"] file:"+file;
TLOGDEBUG(FILE_FUN << result << endl);
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
string s;
CommandAddFile command(pServerObjectPtr,file);
int iRet = command.doProcess(current,s);
if ( iRet == 0)
{
result = result+"succ:" + s;
}
else
{
result = FILE_FUN_STR+"error:"+s;
}
return iRet;
}
result += FILE_FUN_STR+"server is not exist";
return -1;
}
string NodeImp::getName( TarsCurrentPtr current )
{
TLOGDEBUG(FILE_FUN << endl);
_nodeInfo = _platformInfo.getNodeInfo();
return _nodeInfo.nodeName;
}
LoadInfo NodeImp::getLoad( TarsCurrentPtr current )
{
TLOGDEBUG(FILE_FUN<< endl);
return _platformInfo.getLoadInfo();
}
int NodeImp::shutdown( string &result,TarsCurrentPtr current )
{
try
{
result = FILE_FUN_STR;
TLOGDEBUG( result << endl);
if ( g_app.isValid(current->getIp()) == false )
{
result += "erro:ip "+ current->getIp()+" is invalid";
TLOGERROR(FILE_FUN << result << endl);
return -1;
}
Application::terminate();
return 0;
}
catch ( exception& e )
{
TLOGERROR( "NodeImp::shutdown catch exception :" << e.what() << endl);
}
catch ( ... )
{
TLOGERROR( "NodeImp::shutdown catch unkown exception" << endl);
}
return -1;
}
int NodeImp::stopAllServers( string &result,TarsCurrentPtr current )
{
try
{
if ( g_app.isValid(current->getIp()) == false )
{
result = FILE_FUN_STR+" erro:ip "+ current->getIp()+" is invalid";
TLOGERROR( result << endl);
return -1;
}
map<string, ServerGroup> mmServerList;
mmServerList.clear();
mmServerList = ServerFactory::getInstance()->getAllServers();
for ( map<string, ServerGroup>::const_iterator it = mmServerList.begin(); it != mmServerList.end(); it++ )
{
for ( map<string, ServerObjectPtr>::const_iterator p = it->second.begin(); p != it->second.end(); p++ )
{
ServerObjectPtr pServerObjectPtr = p->second;
if ( pServerObjectPtr )
{
result =result+"stop server ["+it->first+"."+ p->first+"] ";
TLOGDEBUG(FILE_FUN << result << endl);
bool bByNode = true;
string s;
CommandStop command(pServerObjectPtr,true,bByNode);
int iRet = command.doProcess(s);
if (iRet == 0)
{
result = result+"succ:"+s+"\n";
}
else
{
result = result+"error:"+s+"\n";
}
}
}
}
return 0;
}
catch ( exception& e )
{
TLOGERROR( FILE_FUN<<" catch exception :" << e.what() << endl);
result += e.what();
}
catch ( ... )
{
TLOGERROR( FILE_FUN<<" catch unkown exception" << endl);
result += "catch unkown exception";
}
TLOGDEBUG("stop succ"<<endl);
return -1;
}
int NodeImp::loadServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current )
{
try
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
TLOGDEBUG("NodeImp::loadServer"<<result<<endl);
if ( g_app.isValid(current->getIp()) == false )
{
result += " erro:ip "+ current->getIp()+" is invalid";
TLOGERROR(FILE_FUN << result << endl);
return -1;
}
string s;
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->loadServer(application,serverName,false,s);
if (!pServerObjectPtr)
{
result =result+" error::cannot load server description.\n"+s;
return EM_TARS_LOAD_SERVICE_DESC_ERR;
}
result = result + " succ" + s;
return 0;
}
catch ( exception& e )
{
TLOGERROR( FILE_FUN<<" catch exception :" << e.what() << endl);
result += e.what();
}
catch ( ... )
{
TLOGERROR( FILE_FUN<<" catch unkown exception" << endl);
result += "catch unkown exception";
}
return -1;
}
int NodeImp::startServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current )
{
int iRet = EM_TARS_UNKNOWN_ERR;
try
{
result = string(__FUNCTION__)+ " ["+application + "." + serverName+"] from "+ current->getIp()+" ";
NODE_LOG("startServer")->debug()<<FILE_FUN << result << endl;
if ( g_app.isValid(current->getIp()) == false )
{
result += " erro:ip "+ current->getIp()+" is invalid";
NODE_LOG("startServer")->error()<<FILE_FUN << result << endl;
return EM_TARS_INVALID_IP_ERR;
}
NODE_LOG("startServer")->debug()<<FILE_FUN<<result <<"|load begin"<< endl;
string s;
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->loadServer( application, serverName,true,s);
NODE_LOG("startServer")->debug()<<FILE_FUN<<result<<"|load"<< endl;
if ( pServerObjectPtr )
{
bool bByNode = true;
CommandStart command(pServerObjectPtr,bByNode);
iRet = command.doProcess(s);
if (iRet == 0 )
{
result = result+":server is activating, please wait ...|"+s;
}
else
{
result = result+"error:"+s;
}
NODE_LOG("startServer")->debug()<<FILE_FUN <<result << endl;
return iRet;
}
//设置服务状态enable
pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if (pServerObjectPtr)
{
pServerObjectPtr->setEnabled(true);
}
result += "error::cannot load server description from regisrty.\n" + s;
iRet = EM_TARS_LOAD_SERVICE_DESC_ERR;
}
catch ( exception& e )
{
NODE_LOG("startServer")->error() << FILE_FUN << " catch exception:" << e.what() << endl;
result += e.what();
}
catch ( ... )
{
NODE_LOG("startServer")->error() << FILE_FUN <<" catch unkown exception" << endl;
result += "catch unkown exception";
}
return iRet;
}
int NodeImp::stopServer( const string& application, const string& serverName,string &result, TarsCurrentPtr current )
{
int iRet = EM_TARS_UNKNOWN_ERR;
try
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] from "+ current->getIp()+" ";
NODE_LOG("stopServer")->debug() <<FILE_FUN<<result << endl;
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
string s;
bool bByNode = true;
CommandStop command(pServerObjectPtr,true, bByNode);
iRet = command.doProcess(current,s);
if (iRet == 0 )
{
result = result+"succ:"+s;
}
else
{
result = result+"error:"+s;
}
NODE_LOG("stopServer")->debug()<<FILE_FUN <<result << endl;
}
else
{
result += "server is not exist";
iRet = EM_TARS_LOAD_SERVICE_DESC_ERR;
}
return iRet;
}
catch ( exception& e )
{
NODE_LOG("stopServer")->error() << FILE_FUN<<" catch exception :" << e.what() << endl;
result += e.what();
}
catch ( ... )
{
NODE_LOG("stopServer")->error() << FILE_FUN<<" catch unkown exception" << endl;
result += "catch unkown exception";
}
return EM_TARS_UNKNOWN_ERR;
}
int NodeImp::notifyServer( const string& application, const string& serverName, const string &sMsg, string &result, TarsCurrentPtr current )
{
try
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] '" + sMsg + "' ";
TLOGDEBUG(result<<endl);
if ( g_app.isValid(current->getIp()) == false )
{
result += " erro:ip "+ current->getIp()+" is invalid";
TLOGERROR("NodeImp::notifyServer " << result << endl);
return EM_TARS_INVALID_IP_ERR;
}
if ( application == "tars" && serverName == "tarsnode" )
{
AdminFPrx pAdminPrx; //服务管理代理
pAdminPrx = Application::getCommunicator()->stringToProxy<AdminFPrx>("AdminObj@"+ServerConfig::Local);
result = pAdminPrx->notify(sMsg);
return 0;
}
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
string s;
CommandNotify command(pServerObjectPtr,sMsg);
int iRet = command.doProcess(s);
if (iRet == 0 )
{
result = result + "succ:" + s;
}
else
{
result = result + "error:" + s;
}
return iRet;
}
result += "server is not exist";
}
catch ( exception& e )
{
TLOGERROR( "NodeImp::notifyServer catch exception :" << e.what() << endl);
result += e.what();
}
catch ( ... )
{
TLOGERROR( "NodeImp::notifyServer catch unkown exception" << endl);
result += "catch unkown exception";
}
return -1;
}
int NodeImp::getServerPid( const string& application, const string& serverName,string &result,TarsCurrentPtr current )
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
TLOGDEBUG("NodeImp::getServerPid " <<result << endl);
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
result += "succ";
return pServerObjectPtr->getPid();
}
result += "server not exist";
return -1;
}
ServerState NodeImp::getSettingState( const string& application, const string& serverName,string &result, TarsCurrentPtr current )
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
result += "succ";
return pServerObjectPtr->isEnabled()==true?tars::Active:tars::Inactive;
}
result += "server not exist";
TLOGERROR( "NodeImp::getServerPid" <<" "<<result<< endl);
return tars::Inactive;
}
ServerState NodeImp::getState( const string& application, const string& serverName, string &result, TarsCurrentPtr current )
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
result += "succ";
return pServerObjectPtr->getState();
}
result += "server not exist";
TLOGERROR("NodeImp::getState " << result << endl);
return tars::Inactive;
}
tars::Int32 NodeImp::getStateInfo(const std::string & application,const std::string & serverName,tars::ServerStateInfo &info,std::string &result,tars::TarsCurrentPtr current)
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
result += "succ";
info.serverState = pServerObjectPtr->getState();
info.processId = pServerObjectPtr->getPid();
info.settingState = pServerObjectPtr->isEnabled()==true?tars::Active:tars::Inactive;
TLOGDEBUG("NodeImp::getStateInfo " <<result << endl);
return EM_TARS_SUCCESS;
}
result += "server not exist";
TLOGERROR("NodeImp::getStateInfo " << result << endl);
info.serverState = tars::Inactive;
info.processId = -1;
info.settingState = tars::Inactive;
return EM_TARS_UNKNOWN_ERR;
}
int NodeImp::synState( const string& application, const string& serverName, string &result, TarsCurrentPtr current )
{
try
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
result += "succ";
pServerObjectPtr->synState();
return 0;
}
result += "server not exist";
TLOGERROR("NodeImp::synState "<<result<< endl);
}
catch ( exception& e )
{
TLOGERROR( "NodeImp::synState catch exception :" << e.what() << endl);
result += e.what();
}
catch ( ... )
{
TLOGERROR( "NodeImp::synState catch unkown exception" << endl);
result += "catch unkown exception";
}
return -1;
}
int NodeImp::getPatchPercent( const string& application, const string& serverName,PatchInfo &tPatchInfo, TarsCurrentPtr current)
{
string &result = tPatchInfo.sResult;
try
{
result = string(__FUNCTION__)+" ["+application + "." + serverName+"] ";
ServerObjectPtr pServerObjectPtr = ServerFactory::getInstance()->getServer( application, serverName );
if ( pServerObjectPtr )
{
result += "succ";
NODE_LOG("getPatchPercent")->debug()<<FILE_FUN << result << "|get ServerObj" << endl;
return pServerObjectPtr->getPatchPercent(tPatchInfo);
}
result += "server not exist";
NODE_LOG("getPatchPercent")->error() << FILE_FUN <<" "<< result<< endl;
return EM_TARS_LOAD_SERVICE_DESC_ERR;
}
catch ( exception& e )
{
NODE_LOG("getPatchPercent")->error() << FILE_FUN << " catch exception :" << e.what() << endl;
result += e.what();
}
catch ( ... )
{
NODE_LOG("getPatchPercent")->error() << FILE_FUN <<" catch unkown exception" << endl;
result += "catch unkown exception";
}
return EM_TARS_UNKNOWN_ERR;
}
tars::Int32 NodeImp::delCache(const string &sFullCacheName, const std::string &sBackupPath, const std::string & sKey, std :: string &result, TarsCurrentPtr current)
{
try
{
TLOGDEBUG("NodeImp::delCache "<<sFullCacheName << "|" << sBackupPath << "|" << sKey << endl);
if (sFullCacheName.empty())
{
result = "cache server name is empty";
throw runtime_error(result);
}
string sBasePath = "/usr/local/app/tars/tarsnode/data/"+sFullCacheName+"/bin/";
if (!TC_File::isFileExistEx(sBasePath, S_IFDIR))
{
result = "no such directory:" + sBasePath;
return 0;
}
key_t key;
if (sKey.empty())
{
key = ftok(sBasePath.c_str(), 'D');
}
else
{
key = TC_Common::strto<key_t>(sKey);
}
TLOGDEBUG("NodeImp::delCache|key=" << key << endl);
int shmid = shmget(key, 0, 0666);
if (shmid == -1)
{
result = "failed to shmget " + sBasePath + "|key=" + TC_Common::tostr(key);
//如果获取失败则认为共享内存已经删除,直接返回成功
TLOGDEBUG("NodeImp::delCache"<< "|" << sFullCacheName << "|" << result << endl);
return 0;
}
shmid_ds *pShmInfo= new shmid_ds;
int iRet = shmctl(shmid, IPC_STAT, pShmInfo);
if (iRet != 0)
{
result = "failed to shmctl " + sBasePath + "|key=" + TC_Common::tostr(key) + "|ret=" + TC_Common::tostr(iRet);
delete pShmInfo;
pShmInfo = NULL;
throw runtime_error(result);
}
if (pShmInfo->shm_nattch>=1)
{
result = "current attach count >= 1,please check it |" + sBasePath + "|key=" +TC_Common::tostr(key)+"|attch_count=" + TC_Common::tostr(pShmInfo->shm_nattch);
delete pShmInfo;
pShmInfo = NULL;
throw runtime_error(result);
};
delete pShmInfo;
pShmInfo = NULL;
int ret =shmctl(shmid, IPC_RMID, NULL);
if (ret !=0)
{
result = "failed to rm share memory|key=" + TC_Common::tostr(key) + "|shmid=" + TC_Common::tostr(shmid) + "|ret=" + TC_Common::tostr(ret);
throw runtime_error(result);
}
TLOGDEBUG("NodeImp::delCache success delete cache:" << sFullCacheName <<", no need backup it"<< endl);
return 0;
}
catch ( exception& e )
{
result = TC_Common::tostr(__FILE__)+":"+TC_Common::tostr(__FUNCTION__) + ":"+ TC_Common::tostr(__LINE__) + "|" + e.what();
TLOGERROR("NodeImp::delCache " << result << endl);
TARS_NOTIFY_ERROR(result);
}
return -1;
}
tars::Int32 NodeImp::getUnusedShmKeys(tars::Int32 count,vector<tars::Int32> &shm_keys,tars::TarsCurrentPtr current) {
int ret = 0;
for(size_t i = 0; i < 256 && shm_keys.size() < (size_t)count; i++)
{
key_t key = ftok(ServerConfig::BasePath.c_str(), i);
if(key == -1)
{
TLOGDEBUG("NodeImp::getUnusedShmKeys create an key failed, i=" << i << endl);
}
else
{
int shmid = shmget(key, 0, 0666);
if(shmid == -1)
{
TLOGDEBUG("NodeImp::getUnusedShmKeys find an key:" << key << " 0x" << keyToStr(key) << endl);
shm_keys.push_back(key);
}
else
{
TLOGDEBUG("NodeImp::getUnusedShmKeys key: " << key << " 0x" << keyToStr(key) << " is busy"<< endl);
}
}
}
return ret;
}
string NodeImp::keyToStr(key_t key_value)
{
char buf[32];
snprintf(buf, 32, "%08x", key_value);
string s(buf);
return s;
}
/*********************************************************************/