forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WdtBase.cpp
186 lines (163 loc) · 5.4 KB
/
WdtBase.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
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <wdt/WdtBase.h>
#include <wdt/WdtTransferRequest.h>
#include <random>
using namespace std;
namespace facebook {
namespace wdt {
// TODO: force callers to pass options in
WdtBase::WdtBase() : abortCheckerCallback_(this) {
options_.copyInto(WdtOptions::get());
}
void WdtBase::setWdtOptions(const WdtOptions& src) {
options_.copyInto(src);
}
WdtBase::~WdtBase() {
abortChecker_ = nullptr;
delete threadsController_;
}
void WdtBase::abort(const ErrorCode abortCode) {
folly::RWSpinLock::WriteHolder guard(abortCodeLock_);
if (abortCode == VERSION_MISMATCH && abortCode_ != OK) {
// VERSION_MISMATCH is the lowest priority abort code. If the abort code is
// anything other than OK, we should not override it
return;
}
WLOG(WARNING) << "Setting the abort code " << abortCode;
abortCode_ = abortCode;
}
void WdtBase::clearAbort() {
folly::RWSpinLock::WriteHolder guard(abortCodeLock_);
if (abortCode_ != VERSION_MISMATCH) {
// We do no clear abort code unless it is VERSION_MISMATCH
return;
}
WLOG(WARNING) << "Clearing the abort code";
abortCode_ = OK;
}
void WdtBase::setAbortChecker(const std::shared_ptr<IAbortChecker>& checker) {
abortChecker_ = checker;
}
ErrorCode WdtBase::getCurAbortCode() {
// external check, if any:
if (abortChecker_ && abortChecker_->shouldAbort()) {
return ABORTED_BY_APPLICATION;
}
folly::RWSpinLock::ReadHolder guard(abortCodeLock_);
// internal check:
return abortCode_;
}
void WdtBase::setProgressReporter(
std::unique_ptr<ProgressReporter>& progressReporter) {
progressReporter_ = std::move(progressReporter);
}
void WdtBase::setThrottler(std::shared_ptr<Throttler> throttler) {
WVLOG(2) << "Setting an external throttler";
throttler_ = throttler;
}
std::shared_ptr<Throttler> WdtBase::getThrottler() const {
return throttler_;
}
void WdtBase::setTransferId(const std::string& transferId) {
transferRequest_.transferId = transferId;
WLOG(INFO) << "Setting transfer id " << transferId;
}
void WdtBase::setProtocolVersion(int64_t protocol) {
WDT_CHECK(protocol > 0) << "Protocol version can't be <= 0 " << protocol;
int negotiatedPv = Protocol::negotiateProtocol(protocol);
if (negotiatedPv != protocol) {
WLOG(WARNING) << "Negotiated protocol version " << protocol << " -> "
<< negotiatedPv;
}
protocolVersion_ = negotiatedPv;
WLOG(INFO) << "using wdt protocol version " << protocolVersion_;
}
int WdtBase::getProtocolVersion() const {
return protocolVersion_;
}
std::string WdtBase::getTransferId() {
return transferRequest_.transferId;
}
const WdtTransferRequest& WdtBase::getTransferRequest() {
return transferRequest_;
}
void WdtBase::checkAndUpdateBufferSize() {
int64_t bufSize = options_.buffer_size;
if (bufSize < Protocol::kMaxHeader) {
bufSize = Protocol::kMaxHeader;
WLOG(WARNING) << "Specified buffer size " << options_.buffer_size
<< " less than " << Protocol::kMaxHeader << ", using "
<< bufSize;
}
if (bufSize % kDiskBlockSize != 0) {
int64_t alignedBufSize =
((bufSize + kDiskBlockSize - 1) / kDiskBlockSize) * kDiskBlockSize;
WLOG(WARNING) << "Buffer size " << bufSize
<< " not divisible by disk block size " << kDiskBlockSize
<< ", changing it to " << alignedBufSize;
bufSize = alignedBufSize;
}
options_.buffer_size = bufSize;
}
WdtBase::TransferStatus WdtBase::getTransferStatus() {
std::lock_guard<std::mutex> lock(mutex_);
return transferStatus_;
}
ErrorCode WdtBase::validateTransferRequest() {
ErrorCode code = transferRequest_.errorCode;
if (code != OK) {
WLOG(ERROR) << "WDT object initiated with erroneous transfer request "
<< transferRequest_.getLogSafeString();
return code;
}
if (transferRequest_.directory.empty() ||
(transferRequest_.protocolVersion < 0) ||
transferRequest_.ports.empty()) {
WLOG(ERROR) << "Transfer request validation failed for wdt object "
<< transferRequest_.getLogSafeString();
code = INVALID_REQUEST;
transferRequest_.errorCode = code;
}
return code;
}
void WdtBase::setTransferStatus(TransferStatus transferStatus) {
std::lock_guard<std::mutex> lock(mutex_);
transferStatus_ = transferStatus;
if (transferStatus_ == THREADS_JOINED) {
conditionFinished_.notify_one();
}
}
bool WdtBase::isStale() {
TransferStatus status = getTransferStatus();
return (status == FINISHED || status == THREADS_JOINED);
}
void WdtBase::configureThrottler() {
WDT_CHECK(!throttler_);
WVLOG(1) << "Configuring throttler options";
throttler_ = Throttler::makeThrottler(options_);
if (throttler_) {
WLOG(INFO) << "Enabling throttling " << *throttler_;
} else {
WLOG(INFO) << "Throttling not enabled";
}
}
string WdtBase::generateTransferId() {
static std::default_random_engine randomEngine{std::random_device()()};
static std::mutex mutex;
string transferId;
{
std::lock_guard<std::mutex> lock(mutex);
transferId = to_string(randomEngine());
}
WVLOG(1) << "Generated a transfer id " << transferId;
return transferId;
}
}
} // namespace facebook::wdt