-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClusterNotifier.cpp
More file actions
277 lines (235 loc) · 7.01 KB
/
Copy pathClusterNotifier.cpp
File metadata and controls
277 lines (235 loc) · 7.01 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
#include <fstream>
#include "Setting.h"
#include "FPLog.h"
#include "FpnnError.h"
#include "ServerInfo.h"
#include "StringUtil.h"
#include "ClusterNotifier.h"
std::vector<std::string> ClusterNotifier::loadEndpoints(const std::string& endpoints_file)
{
std::vector<std::string> endpoints;
std::ifstream fin(endpoints_file.c_str());
if (fin.is_open())
{
while(fin.good())
{
std::string line;
std::getline(fin, line);
StringUtil::trim(line);
if (line.empty())
continue;
size_t pos = line.find_first_of(":");
if(pos == std::string::npos)
{
LOG_ERROR("Bad endpoints %s in config file: %s", line.c_str(), endpoints_file.c_str());
continue;
}
endpoints.push_back(line);
}
fin.close();
return endpoints;
}
LOG_FATAL("Cannot open endpoints config file: %s", endpoints_file.c_str());
return endpoints;
}
void ClusterNotifier::initSelfEndpoints()
{
std::string port = Setting::getString("FPNN.server.listening.port");
_selfEndpoints.insert(std::string("127.0.0.1:").append(port));
std::string ip = ServerInfo::getServerLocalIP4();
if (ip.empty() == false)
_selfEndpoints.insert(ip.append(":").append(port));
ip = ServerInfo::getServerPublicIP4();
if (ip.empty() == false)
_selfEndpoints.insert(ip.append(":").append(port));
_selfEndpoints.insert(std::string("localhost:").append(port));
std::string domain = ServerInfo::getServerDomain();
if (domain.empty() == false)
_selfEndpoints.insert(domain.append(":").append(port));
}
void ClusterNotifier::buildNotifyClients(std::map<std::string, InvalidateInfoPtr>& notifyClients)
{
std::string endpointConfigFile = Setting::getString("TableCache.cluster.endpointsSet.configFile");
std::vector<std::string> endpoints = loadEndpoints(endpointConfigFile);
for (auto& endpoint: endpoints)
{
bool matched = false;
for (const auto& self: _selfEndpoints)
if (strcasecmp(self.c_str(), endpoint.c_str()) == 0)
{
matched = true;
break;
}
if (matched)
continue;
if (notifyClients.find(endpoint) != notifyClients.end())
continue;
addNotifyClient(endpoint, notifyClients);
}
if (notifyClients.empty())
LOG_WARN("Load empty endpoints! TableCache will running in singleton mode.");
}
void ClusterNotifier::addNotifyClient(const std::string& endpoint, std::map<std::string, InvalidateInfoPtr>& notifyClients)
{
std::vector<std::string> ip_port;
StringUtil::split(endpoint, ":", ip_port);
if (ip_port.size() != 2)
{
LOG_ERROR("Bad endpoint %s when adding notify client.", endpoint.c_str());
return;
}
if (notifyClients.find(endpoint) != notifyClients.end())
return;
InvalidateInfoPtr iip(new InvalidateInfo);
iip->client = TCPClient::createClient(ip_port[0], atoi(ip_port[1].c_str()));
iip->client->setQuestTimeout(2);
notifyClients[endpoint] = iip;
}
ClusterNotifier::ClusterNotifier()
{
initSelfEndpoints();
buildNotifyClients(_clients);
_running = true;
_notifyThread = std::thread(&ClusterNotifier::notify_thread, this);
}
ClusterNotifier::~ClusterNotifier()
{
_running = false;
_notifyThread.join();
}
void ClusterNotifier::refreshCluster()
{
std::map<std::string, InvalidateInfoPtr> notifyClients;
buildNotifyClients(notifyClients);
std::unique_lock<std::mutex> lck(_mutex);
for (auto& cliPair: notifyClients)
{
auto it = _clients.find(cliPair.first);
if (it != _clients.end())
cliPair.second = it->second;
}
_clients.swap(notifyClients);
}
void ClusterNotifier::invalidate(const std::string& tableName, int64_t hintId)
{
std::unique_lock<std::mutex> lck(_mutex);
for (auto& clientPair: _clients)
{
auto& invalidMap = clientPair.second->invalidateData;
if (invalidMap.find(tableName) == invalidMap.end())
invalidMap[tableName].insert(hintId);
else
{
if (invalidMap[tableName].empty() == false)
invalidMap[tableName].insert(hintId);
}
}
}
void ClusterNotifier::invalidateTable(const std::string& tableName)
{
std::unique_lock<std::mutex> lck(_mutex);
for (auto& clientPair: _clients)
clientPair.second->invalidateData[tableName].clear();
}
void ClusterNotifier::reinvalidate(const std::string& endpoint, const std::string& tableName)
{
std::unique_lock<std::mutex> lck(_mutex);
_clients[endpoint]->invalidateData[tableName].clear();
}
void ClusterNotifier::reinvalidate(const std::string& endpoint, const std::string& tableName, std::set<int64_t>& hintIds)
{
std::unique_lock<std::mutex> lck(_mutex);
auto& invalidMap = _clients[endpoint]->invalidateData;
if (invalidMap.find(tableName) == invalidMap.end())
invalidMap[tableName] = hintIds;
else
{
auto &idSet = invalidMap[tableName];
if (idSet.empty() == false)
{
for (int64_t hintId: hintIds)
idSet.insert(hintId);
}
}
}
FPQuestPtr ClusterNotifier::buildQuest(const std::string& tableName, const std::set<int64_t>& hintIds)
{
if (hintIds.empty())
{
FPQWriter qw(2, "invalidateTable");
qw.param("table", tableName);
qw.param("internal", true);
return qw.take();
}
else
{
FPQWriter qw(2, "invalidate");
qw.param("table", tableName);
qw.param("hintIds", hintIds);
return qw.take();
}
}
TCPClientPtr ClusterNotifier::getClient(const std::string& endpoint)
{
std::unique_lock<std::mutex> lck(_mutex);
return _clients[endpoint]->client;
}
ClusterNotifier::NotifyAnswerCallback::NotifyAnswerCallback(ClusterNotifierPtr clusterNotifier,
const std::string& endpoint, const std::string& tableName, const std::set<int64_t>& hintIds):
_processed(false), _endpoint(endpoint), _tableName(tableName), _hintIds(hintIds),
_clusterNotifier(clusterNotifier)
{
}
ClusterNotifier::NotifyAnswerCallback::~NotifyAnswerCallback()
{
if (!_processed)
onException(nullptr, FPNN_EC_CORE_UNKNOWN_ERROR);
}
void ClusterNotifier::NotifyAnswerCallback::onException(FPAnswerPtr answer, int errorCode)
{
if (_hintIds.empty())
_clusterNotifier->reinvalidate(_endpoint, _tableName);
else
_clusterNotifier->reinvalidate(_endpoint, _tableName, _hintIds);
_processed = true;
}
void ClusterNotifier::notify_thread()
{
while (_running)
{
std::map<std::string, InvalidateInfoPtr> notifyInfo;
{
std::unique_lock<std::mutex> lck(_mutex);
for (auto& cliPair: _clients)
{
InvalidateInfoPtr iip = std::make_shared<InvalidateInfo>();
notifyInfo[cliPair.first] = iip;
iip->client = cliPair.second->client;
iip->invalidateData.swap(cliPair.second->invalidateData);
}
}
for (auto& noPair: notifyInfo)
{
TCPClientPtr client = noPair.second->client;
std::map<std::string, std::set<int64_t>>& tableInfos = noPair.second->invalidateData;
for (auto& tableInfo: tableInfos)
{
FPQuestPtr quest = buildQuest(tableInfo.first, tableInfo.second);
NotifyAnswerCallback* callback = new NotifyAnswerCallback(
shared_from_this(), noPair.first, tableInfo.first, tableInfo.second);
bool status = client->sendQuest(quest, callback);
if (!status)
{
status = client->sendQuest(quest, callback);
if (!status)
{
delete callback;
LOG_WARN("Resend invalid notification to %s failed. Retry later.", noPair.first.c_str());
break;
}
}
}
}
usleep(100 * 1000);
}
}