forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWdt.cpp
132 lines (120 loc) · 4.3 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
#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);
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_.getThrottler()->setThrottlerRates(options_);
return OK;
}
// this can be called many times after initializeWdt()
Wdt &Wdt::getWdt(const std::string &appName) {
Wdt &res = getWdtInternal(appName);
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;
// TODO should be using recoverid
const std::string secondKey = req.hostName;
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::wdtSetAbortSocketCreatorAndReporter(
const std::string &, Sender *sender, const WdtTransferRequest &,
std::shared_ptr<IAbortChecker> abortChecker) {
if (abortChecker.get() != nullptr) {
sender->setAbortChecker(abortChecker);
}
return OK;
}
WdtOptions &Wdt::getWdtOptions() {
return options_;
}
// private version
Wdt &Wdt::getWdtInternal(const std::string &appName) {
static std::unordered_map<std::string, std::unique_ptr<Wdt>> s_wdtMap;
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
auto it = s_wdtMap.find(appName);
if (it != s_wdtMap.end()) {
return *(it->second);
}
Wdt *wdtPtr = new Wdt();
std::unique_ptr<Wdt> wdt(wdtPtr);
s_wdtMap.emplace(appName, std::move(wdt));
return *wdtPtr;
}
}
} // namespaces