/
PropertyServer.cpp
181 lines (153 loc) · 5.38 KB
/
PropertyServer.cpp
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
/**
* Tencent is pleased to support the open source community by making DCache available.
* Copyright (C) 2019 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 "PropertyServer.h"
#include "PropertyImp.h"
#include "CacheInfo.h"
#include "CacheInfoManager.h"
#include "PropertyDbManager.h"
PropertyServer g_app;
void PropertyServer::initialize()
{
try
{
// 加载配置文件
addConfig("PropertyServer.conf");
_conf.parseFile(ServerConfig::BasePath + "PropertyServer.conf");
// 添加处理对象
addServant<PropertyImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".PropertyObj");
// 读取上报数据入库的时间间隔,单位分钟
_insertInterval = TC_Common::strto<int>(_conf.get("/Main/HashMap<InsertInterval>", "5"));
if (_insertInterval < 5)
{
_insertInterval = 5;
}
_propertyNameMap = _conf.getDomainMap("/Main/NameMap");
CacheInfoManager::getInstance()->init(_conf);
PropertyDbManager::getInstance()->init(_conf);
initHashMap();
// 读取cache服务信息更新时间间隔,单位秒
_updateInterval = TC_Common::strto<int>(_conf.get("/Main/CacheInfo<UpdateInterval>", "600"));
_updateThread = new CacheInfoUpdateThread(_updateInterval);
_updateThread->start();
_reapThread = new PropertyReapThread();
_reapThread->start();
}
catch (exception& ex)
{
TLOGERROR("PropertyServer::initialize catch exception:" << ex.what() << endl);
exit(0);
}
catch (...)
{
TLOGERROR("PropertyServer::initialize unknow exception catched");
exit(0);
}
}
void PropertyServer::initHashMap()
{
TLOGDEBUG("PropertyServer::initHashMap begin ..." << endl);
int iMinBlock = TC_Common::strto<int>(_conf.get("/Main/HashMap<MinBlock>", "100"));
int iMaxBlock = TC_Common::strto<int>(_conf.get("/Main/HashMap<MaxBlock>", "200"));
float iFactor = TC_Common::strto<float>(_conf.get("/Main/HashMap<Factor>", "1.5"));
int iSize = TC_Common::toSize(_conf.get("/Main/HashMap<Size>"), 1024*1024*10);
string sFile = ServerConfig::DataPath + "/" + _conf.get("/Main/HashMap<File>", "hashmap.txt");
string sPath = TC_File::extractFilePath(sFile);
if (!TC_File::makeDirRecursive(sPath))
{
TLOGERROR("PropertyServer::initHashMap can not create hashmap file path " << sPath << endl);
exit(0);
}
_clonePatch = ServerConfig::DataPath + "/" + _conf.get("/Main/HashMap<ClonePatch>", "clone");
if (!TC_File::makeDirRecursive(_clonePatch))
{
TLOGERROR("PropertyServer::initHashMap can not create clone file path " << _clonePatch << endl);
exit(0);
}
try
{
_hashMap.initDataBlockSize(iMinBlock, iMaxBlock, iFactor);
if (TC_File::isFileExist(sFile))
{
iSize = TC_File::getFileSize(sFile);
}
_hashMap.initStore(sFile.c_str(), iSize);
}
catch (exception &ex)
{
TC_File::removeFile(sFile, false);
throw runtime_error(ex.what());
}
TLOGDEBUG("PropertyServer::initHashMap succ" << endl);
}
const string & PropertyServer::getClonePath() const
{
return _clonePatch;
}
int PropertyServer::getInsertInterval() const
{
return _insertInterval;
}
const map<string, string> & PropertyServer::getPropertyNameMap() const
{
return _propertyNameMap;
}
PropertyHashMap & PropertyServer::getHashMap()
{
return _hashMap;
}
void PropertyServer::destroyApp()
{
if (_reapThread)
{
delete _reapThread;
_reapThread = NULL;
}
if (_updateThread)
{
delete _updateThread;
_updateThread = NULL;
}
TLOGDEBUG("PropertyServer::destroyApp ok" << endl);
}
void PropertyServer::getTimeInfo(time_t &tTime, string &sDate, string &sFlag)
{
// InsertInterval 单位为分钟
string sTime, sHour, sMinute;
time_t t = TNOW;
t = (t / (_insertInterval * 60)) * _insertInterval * 60; //要求必须为Inteval整数倍
tTime = t;
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);
sMinute = (sMinute == "59") ? "60" : sMinute; //要求将9点写作0860
sMinute = (TC_Common::strto<int>(sMinute.substr(1)) >= 5)
? (sMinute.substr(0, 1) + "5")
: (sMinute.substr(0, 1) + "0"); //按五的倍数
sFlag = sHour + sMinute;
}
int main( int argc, char* argv[] )
{
try
{
g_app.main( argc, argv );
g_app.waitForShutdown();
}
catch ( exception& ex )
{
cout << ex.what() << endl;
}
return 0;
}