forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wdt.cpp
202 lines (183 loc) · 6.94 KB
/
Wdt.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <wdt/Wdt.h>
#include <wdt/util/WdtFlags.h>
using std::string;
namespace facebook {
namespace wdt {
// this must be called first and exactly once:
Wdt &Wdt::initializeWdt(const std::string &appName) {
static bool doGlobalFlagsInit = true;
Wdt &res = getWdtInternal(appName, []() -> Wdt * { return new Wdt(); });
WdtFlags::initializeFromFlags(res.options_);
if (doGlobalFlagsInit) {
WdtFlags::initializeFromFlags(WdtOptions::getMutable());
doGlobalFlagsInit = false;
}
res.initializeWdtInternal(appName);
// At fb we do this for services - that's floody for cmd line though
// res.printWdtOptions(WLOG(INFO));
return res;
}
ErrorCode Wdt::initializeWdtInternal(const std::string &appName) {
WLOG(INFO) << "One time initialization of WDT for " << appName;
if (initDone_) {
WLOG(ERROR) << "Called initializeWdt() more than once";
return ERROR;
}
appName_ = appName;
initDone_ = true;
resourceController_.getWdtThrottler()->setThrottlerRates(options_);
return OK;
}
// this can be called many times after initializeWdt()
Wdt &Wdt::getWdt(const std::string &appName) {
Wdt &res = getWdtInternal(appName, nullptr);
if (!res.initDone_) {
WLOG(ERROR) << "Called getWdt() before/without initializeWdt()";
WDT_CHECK(false) << "Must call initializeWdt() once before getWdt()";
}
return res;
}
ErrorCode Wdt::printWdtOptions(std::ostream &out) {
WdtFlags::printOptions(out, options_);
return OK;
}
ErrorCode Wdt::wdtSend(const std::string &wdtNamespace,
const WdtTransferRequest &req,
std::shared_ptr<IAbortChecker> abortChecker,
bool terminateExistingOne) {
if (req.errorCode != OK) {
WLOG(ERROR) << "Transfer request error " << errorCodeToStr(req.errorCode);
return req.errorCode;
}
// Protocol issues will/should be flagged as error when we call createSender
// try to create sender
SenderPtr sender;
// Allow more than 1 destination per host by using the host:firstport
std::string secondKey;
folly::toAppend(req.hostName, ":", req.ports[0], &secondKey);
ErrorCode errCode =
resourceController_.createSender(wdtNamespace, secondKey, req, sender);
if (errCode == ALREADY_EXISTS && terminateExistingOne) {
WLOG(WARNING) << "Found pre-existing sender for " << wdtNamespace << " "
<< secondKey << " aborting it and making a new one";
if (sender->getTransferRequest() == req) {
WLOG(WARNING) << "No need to recreate same sender with key: " << secondKey
<< " TransferRequest: " << req;
return errCode;
}
sender->abort(ABORTED_BY_APPLICATION);
// This may log an error too
resourceController_.releaseSender(wdtNamespace, secondKey);
// Try#2
errCode =
resourceController_.createSender(wdtNamespace, secondKey, req, sender);
}
if (errCode != OK) {
WLOG(ERROR) << "Failed to create sender " << errorCodeToStr(errCode) << " "
<< wdtNamespace << " " << secondKey;
return errCode;
}
wdtSetAbortSocketCreatorAndReporter(wdtNamespace, sender.get(), req,
abortChecker);
auto validatedReq = sender->init();
if (validatedReq.errorCode != OK) {
WLOG(ERROR) << "Couldn't init sender with request for " << wdtNamespace
<< " " << secondKey;
return validatedReq.errorCode;
}
auto transferReport = sender->transfer();
ErrorCode ret = transferReport->getSummary().getErrorCode();
resourceController_.releaseSender(wdtNamespace, secondKey);
WLOG(INFO) << "wdtSend for " << wdtNamespace << " " << secondKey
<< " ended with " << errorCodeToStr(ret);
return ret;
}
ErrorCode Wdt::wdtReceiveStart(const std::string &wdtNamespace,
WdtTransferRequest &req,
const std::string &identifier,
std::shared_ptr<IAbortChecker> abortChecker) {
if (req.errorCode != OK) {
WLOG(ERROR) << "Transfer request namespace:" << wdtNamespace
<< " identifier:" << identifier
<< " error:" << errorCodeToStr(req.errorCode);
return req.errorCode;
}
ReceiverPtr receiver;
ErrorCode errCode = resourceController_.createReceiver(
wdtNamespace, identifier, req, receiver);
if (errCode != OK) {
WLOG(ERROR) << "Failed to create receiver " << errorCodeToStr(errCode)
<< " " << wdtNamespace << " " << identifier;
req.errorCode = errCode;
return errCode;
}
wdtSetAbortSocketCreatorAndReporter(wdtNamespace, receiver.get(), req,
abortChecker);
req = receiver->init();
if (req.errorCode != OK) {
WLOG(ERROR) << "Couldn't init receiver with request for " << wdtNamespace
<< " " << identifier;
return req.errorCode;
}
errCode = receiver->transferAsync();
WLOG(INFO) << "wdtReceiveStart for " << wdtNamespace << " " << identifier
<< " : " << errorCodeToStr(errCode);
req.errorCode = errCode;
return errCode;
}
ErrorCode Wdt::wdtReceiveFinish(const std::string &wdtNamespace,
const std::string &identifier) {
ReceiverPtr receiver =
resourceController_.getReceiver(wdtNamespace, identifier);
if (receiver == nullptr) {
WLOG(ERROR) << "Failed to get receiver " << errorCodeToStr(NOT_FOUND) << " "
<< wdtNamespace << " " << identifier;
return NOT_FOUND;
}
auto report = receiver->finish();
ErrorCode errCode = report->getSummary().getErrorCode();
WLOG(INFO) << "wdtReceiveFinish for " << wdtNamespace << " " << identifier
<< " ended with " << errorCodeToStr(errCode);
resourceController_.releaseReceiver(wdtNamespace, identifier);
return errCode;
}
ErrorCode Wdt::wdtSetAbortSocketCreatorAndReporter(
const std::string &, WdtBase *target, const WdtTransferRequest &,
std::shared_ptr<IAbortChecker> abortChecker) {
if (abortChecker.get() != nullptr) {
target->setAbortChecker(abortChecker);
}
return OK;
}
WdtOptions &Wdt::getWdtOptions() {
return options_;
}
static std::unordered_map<std::string, std::unique_ptr<Wdt>> s_wdtMap;
static std::mutex s_mutex;
// private version
Wdt &Wdt::getWdtInternal(const std::string &appName,
std::function<Wdt *()> factory) {
std::lock_guard<std::mutex> lock(s_mutex);
auto it = s_wdtMap.find(appName);
if (it != s_wdtMap.end()) {
return *(it->second);
}
WDT_CHECK(factory) << "Must call initializeWdt() once before getWdt() "
<< appName;
Wdt *wdtPtr = factory();
std::unique_ptr<Wdt> wdt(wdtPtr);
s_wdtMap.emplace(appName, std::move(wdt));
return *wdtPtr;
}
void Wdt::releaseWdt(const std::string& appName) {
LOG(INFO) << "Releasing WDT for " << appName;
std::lock_guard<std::mutex> lock(s_mutex);
auto it = s_wdtMap.find(appName);
if (it == s_wdtMap.end()) {
LOG(ERROR) << appName << " not found in releaseWdt";
return;
}
s_wdtMap.erase(it);
}
}
} // namespaces