forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wdt.cpp
227 lines (205 loc) · 7.76 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
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
#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;
if (doGlobalFlagsInit) {
WdtFlags::initializeFromFlags(WdtOptions::getMutable());
doGlobalFlagsInit = false;
}
Wdt &res = getWdtInternal(appName, []() -> Wdt * { return new Wdt(); });
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_.getThrottlerOptions());
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;
}
Wdt::Wdt() {
WdtFlags::initializeFromFlags(options_);
resourceController_ = std::make_unique<WdtResourceController>(options_);
}
std::string Wdt::getSenderIdentifier(const WdtTransferRequest &req) {
if (req.destIdentifier.empty()) {
return req.hostName;
}
return req.destIdentifier;
}
ErrorCode Wdt::createWdtSender(const WdtTransferRequest &req,
std::shared_ptr<IAbortChecker> abortChecker,
bool terminateExistingOne,
SenderPtr &senderPtr) {
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
const std::string &wdtNamespace = req.wdtNamespace;
const std::string secondKey = getSenderIdentifier(req);
ErrorCode errCode = resourceController_->createSender(wdtNamespace, secondKey,
req, senderPtr);
if (errCode == ALREADY_EXISTS && terminateExistingOne) {
WLOG(WARNING) << "Found pre-existing sender for " << wdtNamespace << " "
<< secondKey << " aborting it and making a new one";
if (senderPtr->getTransferRequest().transferId == req.transferId) {
WLOG(WARNING) << "No need to recreate same sender with key: " << secondKey
<< " TransferRequest: " << req;
return ALREADY_EXISTS;
}
senderPtr->abort(ABORTED_BY_APPLICATION);
// This may log an error too
resourceController_->releaseSender(wdtNamespace, secondKey);
// Try#2
errCode = resourceController_->createSender(wdtNamespace, secondKey, req,
senderPtr);
}
if (errCode != OK) {
WLOG(ERROR) << "Failed to create sender " << errorCodeToStr(errCode) << " "
<< wdtNamespace << " " << secondKey;
return errCode;
}
wdtSetAbortSocketCreatorAndReporter(
senderPtr.get(), senderPtr->getTransferRequest(), abortChecker);
return OK;
}
ErrorCode Wdt::releaseWdtSender(const WdtTransferRequest &wdtRequest) {
return resourceController_->releaseSender(wdtRequest.wdtNamespace,
getSenderIdentifier(wdtRequest));
}
ErrorCode Wdt::wdtSend(const WdtTransferRequest &req,
std::shared_ptr<IAbortChecker> abortChecker,
bool terminateExistingOne) {
SenderPtr sender;
ErrorCode errCode =
createWdtSender(req, abortChecker, terminateExistingOne, sender);
if (errCode != OK) {
return errCode;
}
const std::string &wdtNamespace = req.wdtNamespace;
auto validatedReq = sender->init();
if (validatedReq.errorCode != OK) {
WLOG(ERROR) << "Couldn't init sender with request for " << wdtNamespace;
return validatedReq.errorCode;
}
auto transferReport = sender->transfer();
errCode = transferReport->getSummary().getErrorCode();
releaseWdtSender(req);
WLOG(INFO) << "wdtSend for " << wdtNamespace << " " << req.hostName
<< " ended with " << errorCodeToStr(errCode);
return errCode;
}
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(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(
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