-
Notifications
You must be signed in to change notification settings - Fork 387
/
Sender.cpp
476 lines (433 loc) · 16.3 KB
/
Sender.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* 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.
*/
#include <folly/Conv.h>
#include <folly/Memory.h>
#include <folly/ScopeGuard.h>
#include <folly/String.h>
#include <folly/hash/Checksum.h>
#include <folly/lang/Bits.h>
#include <wdt/Sender.h>
#include <wdt/SenderThread.h>
#include <wdt/Throttler.h>
#include <wdt/util/ClientSocket.h>
namespace facebook {
namespace wdt {
void Sender::endCurTransfer() {
endTime_ = Clock::now();
WLOG(INFO) << "Last thread finished "
<< durationSeconds(endTime_ - startTime_) << " for transfer id "
<< getTransferId();
setTransferStatus(TransferStatus::FINISHED);
if (throttler_) {
throttler_->endTransfer();
}
}
void Sender::startNewTransfer() {
if (throttler_) {
throttler_->startTransfer();
}
WLOG(INFO) << "Starting a new transfer " << getTransferId() << " to "
<< transferRequest_.hostName;
}
Sender::Sender(const WdtTransferRequest &transferRequest)
: queueAbortChecker_(this) {
WLOG(INFO) << "WDT Sender " << Protocol::getFullVersion();
transferRequest_ = transferRequest;
if (getTransferId().empty()) {
WLOG(WARNING) << "Sender without transferId... will likely fail to connect";
}
}
ErrorCode Sender::validateTransferRequest() {
ErrorCode code = WdtBase::validateTransferRequest();
// If the request is still valid check for other
// sender specific validations
if (code == OK && transferRequest_.hostName.empty()) {
WLOG(ERROR) << "Transfer request validation failed for wdt sender "
<< transferRequest_.getLogSafeString();
code = INVALID_REQUEST;
}
transferRequest_.errorCode = code;
return code;
}
const WdtTransferRequest &Sender::init() {
WVLOG(1) << "Sender Init() with encryption set = "
<< transferRequest_.encryptionData.isSet();
negotiateProtocol();
if (validateTransferRequest() != OK) {
WLOG(ERROR) << "Couldn't validate the transfer request "
<< transferRequest_.getLogSafeString();
return transferRequest_;
}
// TODO Figure out what to do with file info
// transferRequest.fileInfo = dirQueue_->getFileInfo();
transferRequest_.errorCode = OK;
bool encrypt = transferRequest_.encryptionData.isSet();
WLOG_IF(INFO, encrypt) << "Encryption is enabled for this transfer";
return transferRequest_;
}
Sender::~Sender() {
TransferStatus status = getTransferStatus();
if (status == TransferStatus::ONGOING) {
WLOG(WARNING) << "Sender being deleted. Forcefully aborting the transfer";
abort(ABORTED_BY_APPLICATION);
}
finish();
}
ProtoNegotiationStatus Sender::getNegotiationStatus() {
return protoNegotiationStatus_;
}
std::vector<int> Sender::getNegotiatedProtocols() const {
std::vector<int> ret;
for (const auto &senderThread : senderThreads_) {
ret.push_back(senderThread->getNegotiatedProtocol());
}
return ret;
}
void Sender::setProtoNegotiationStatus(ProtoNegotiationStatus status) {
protoNegotiationStatus_ = status;
}
bool Sender::isSendFileChunks() const {
return (downloadResumptionEnabled_ &&
getProtocolVersion() >= Protocol::DOWNLOAD_RESUMPTION_VERSION);
}
bool Sender::isFileChunksReceived() {
std::lock_guard<std::mutex> lock(mutex_);
return fileChunksReceived_;
}
void Sender::setFileChunksInfo(
std::vector<FileChunksInfo> &fileChunksInfoList) {
std::lock_guard<std::mutex> lock(mutex_);
if (fileChunksReceived_) {
WLOG(WARNING) << "File chunks list received multiple times";
return;
}
dirQueue_->setPreviouslyReceivedChunks(fileChunksInfoList);
fileChunksReceived_ = true;
}
const std::string &Sender::getDestination() const {
return transferRequest_.hostName;
}
std::unique_ptr<TransferReport> Sender::getTransferReport() {
int64_t totalFileSize = 0;
int64_t fileCount = 0;
bool fileDiscoveryFinished = false;
if (dirQueue_ != nullptr) {
totalFileSize = dirQueue_->getTotalSize();
fileCount = dirQueue_->getCount();
fileDiscoveryFinished = dirQueue_->fileDiscoveryFinished();
}
double totalTime = durationSeconds(Clock::now() - startTime_);
auto globalStats = getGlobalTransferStats();
std::unique_ptr<TransferReport> transferReport =
std::make_unique<TransferReport>(std::move(globalStats), totalTime,
totalFileSize, fileCount,
fileDiscoveryFinished);
TransferStatus status = getTransferStatus();
ErrorCode errCode = transferReport->getSummary().getErrorCode();
if (status == TransferStatus::NOT_STARTED && errCode == OK) {
WLOG(INFO) << "Transfer not started, setting the error code to ERROR";
transferReport->setErrorCode(ERROR);
}
return transferReport;
}
Clock::time_point Sender::getEndTime() {
return endTime_;
}
TransferStats Sender::getGlobalTransferStats() const {
TransferStats globalStats;
for (const auto &thread : senderThreads_) {
globalStats += thread->getTransferStats();
}
return globalStats;
}
std::unique_ptr<TransferReport> Sender::finish() {
std::unique_lock<std::mutex> instanceLock(instanceManagementMutex_);
WVLOG(1) << "Sender::finish()";
TransferStatus status = getTransferStatus();
if (status == TransferStatus::NOT_STARTED) {
WLOG(WARNING) << "Even though transfer has not started, finish is called";
// getTransferReport will set the error code to ERROR
return getTransferReport();
}
if (status == TransferStatus::THREADS_JOINED) {
WVLOG(1) << "Threads have already been joined. Returning the"
<< " existing transfer report";
return getTransferReport();
}
const bool twoPhases = options_.two_phases;
bool progressReportEnabled =
progressReporter_ && progressReportIntervalMillis_ > 0;
for (auto &senderThread : senderThreads_) {
senderThread->finish();
}
if (!twoPhases) {
dirThread_.join();
}
WDT_CHECK(numActiveThreads_ == 0);
setTransferStatus(TransferStatus::THREADS_JOINED);
if (progressReportEnabled) {
progressReporterThread_.join();
}
std::vector<TransferStats> threadStats;
for (auto &senderThread : senderThreads_) {
threadStats.push_back(senderThread->moveStats());
}
bool allSourcesAcked = false;
for (auto &senderThread : senderThreads_) {
auto &stats = senderThread->getTransferStats();
if (stats.getErrorCode() == OK) {
// at least one thread finished correctly
// that means all transferred sources are acked
allSourcesAcked = true;
break;
}
}
std::vector<TransferStats> transferredSourceStats;
for (auto port : transferRequest_.ports) {
auto &transferHistory =
transferHistoryController_->getTransferHistory(port);
if (allSourcesAcked) {
transferHistory.markAllAcknowledged();
} else {
transferHistory.returnUnackedSourcesToQueue();
}
if (options_.full_reporting) {
std::vector<TransferStats> stats = transferHistory.popAckedSourceStats();
transferredSourceStats.insert(transferredSourceStats.end(),
std::make_move_iterator(stats.begin()),
std::make_move_iterator(stats.end()));
}
}
if (options_.full_reporting) {
validateTransferStats(transferredSourceStats,
dirQueue_->getFailedSourceStats());
}
int64_t totalFileSize = dirQueue_->getTotalSize();
double totalTime = durationSeconds(endTime_ - startTime_);
std::unique_ptr<TransferReport> transferReport =
std::make_unique<TransferReport>(
transferredSourceStats, dirQueue_->getFailedSourceStats(),
threadStats, dirQueue_->getFailedDirectories(), totalTime,
totalFileSize, dirQueue_->getCount(),
dirQueue_->getPreviouslySentBytes(),
dirQueue_->fileDiscoveryFinished());
if (progressReportEnabled) {
progressReporter_->end(transferReport);
}
logPerfStats();
double directoryTime;
directoryTime = dirQueue_->getDirectoryTime();
WLOG(INFO) << "Total sender time = " << totalTime << " seconds ("
<< directoryTime << " dirTime)"
<< ". Transfer summary : " << *transferReport << "\n"
<< WDT_LOG_PREFIX << "Total sender throughput = "
<< transferReport->getThroughputMBps() << " Mbytes/sec ("
<< transferReport->getSummary().getEffectiveTotalBytes() /
(totalTime - directoryTime) / kMbToB
<< " Mbytes/sec pure transfer rate)";
return transferReport;
}
ErrorCode Sender::transferAsync() {
return start();
}
std::unique_ptr<TransferReport> Sender::transfer() {
start();
return finish();
}
folly::Optional<std::vector<WdtFileInfo>>
Sender::getFilesFromFileInfoGenerator() {
auto runningThreads = [this]() {
return threadsController_->numRunningThreads();
};
dirQueue_->waitForPreviousTransfer(
std::chrono::milliseconds(options_.progress_report_interval_millis),
runningThreads);
const auto status = getTransferStatus();
if (status != TransferStatus::ONGOING) {
WLOG(INFO) << "Terminating transafer since status isn't ONGOING. "
<< "Status: " << status;
return folly::none;
}
return transferRequest_.fileInfoGenerator();
}
ErrorCode Sender::start() {
{
std::lock_guard<std::mutex> lock(mutex_);
if (transferStatus_ != TransferStatus::NOT_STARTED) {
WLOG(ERROR) << "duplicate start() call detected " << transferStatus_;
return ALREADY_EXISTS;
}
transferStatus_ = TransferStatus::ONGOING;
}
// set up directory queue
dirQueue_.reset(new DirectorySourceQueue(options_, transferRequest_.directory,
&queueAbortChecker_));
WVLOG(3) << "Configuring the directory queue";
dirQueue_->setIncludePattern(options_.include_regex);
dirQueue_->setExcludePattern(options_.exclude_regex);
dirQueue_->setPruneDirPattern(options_.prune_dir_regex);
dirQueue_->setFollowSymlinks(options_.follow_symlinks);
dirQueue_->setBlockSizeMbytes(options_.block_size_mbytes);
dirQueue_->setNumClientThreads(transferRequest_.ports.size());
dirQueue_->setOpenFilesDuringDiscovery(options_.open_files_during_discovery);
dirQueue_->setDirectReads(options_.odirect_reads);
if (!transferRequest_.fileInfo.empty() ||
transferRequest_.fileInfoGenerator ||
transferRequest_.disableDirectoryTraversal) {
dirQueue_->setFileInfo(transferRequest_.fileInfo);
if (transferRequest_.fileInfoGenerator) {
dirQueue_->setFileInfoGenerator(
[this]() { return getFilesFromFileInfoGenerator(); });
}
}
transferHistoryController_ =
std::make_unique<TransferHistoryController>(*dirQueue_);
checkAndUpdateBufferSize();
const bool twoPhases = options_.two_phases;
WLOG(INFO) << "Client (sending) to " << getDestination() << ", Using ports [ "
<< transferRequest_.ports << "]";
startTime_ = Clock::now();
progressReportIntervalMillis_ = options_.progress_report_interval_millis;
downloadResumptionEnabled_ = (transferRequest_.downloadResumptionEnabled ||
options_.enable_download_resumption);
bool deleteExtraFiles = (transferRequest_.downloadResumptionEnabled ||
options_.delete_extra_files);
if (!progressReporter_) {
WVLOG(1) << "No progress reporter provided, making a default one";
progressReporter_ = std::make_unique<ProgressReporter>(transferRequest_);
}
bool progressReportEnabled =
progressReporter_ && progressReportIntervalMillis_ > 0;
if (throttler_) {
WLOG(INFO) << "Skipping throttler setup. External throttler set."
<< "Throttler details : " << *throttler_;
} else {
configureThrottler();
}
threadsController_ = new ThreadsController(transferRequest_.ports.size());
threadsController_->setNumBarriers(SenderThread::NUM_BARRIERS);
threadsController_->setNumFunnels(SenderThread::NUM_FUNNELS);
threadsController_->setNumConditions(SenderThread::NUM_CONDITIONS);
// TODO: fix this ! use transferRequest! (and dup from Receiver)
senderThreads_ = threadsController_->makeThreads<Sender, SenderThread>(
this, transferRequest_.ports.size(), transferRequest_.ports);
if (downloadResumptionEnabled_ && deleteExtraFiles) {
if (getProtocolVersion() >= Protocol::DELETE_CMD_VERSION) {
dirQueue_->enableFileDeletion();
} else {
WLOG(WARNING) << "Turning off extra file deletion on the receiver side "
"because of protocol version "
<< getProtocolVersion();
}
}
dirThread_ = dirQueue_->buildQueueAsynchronously();
if (twoPhases) {
dirThread_.join();
}
for (auto &senderThread : senderThreads_) {
senderThread->startThread();
}
if (progressReportEnabled) {
progressReporter_->start();
std::thread reporterThread(&Sender::reportProgress, this);
progressReporterThread_ = std::move(reporterThread);
}
return OK;
}
void Sender::validateTransferStats(
const std::vector<TransferStats> &transferredSourceStats,
const std::vector<TransferStats> &failedSourceStats) {
int64_t sourceFailedAttempts = 0;
int64_t sourceDataBytes = 0;
int64_t sourceEffectiveDataBytes = 0;
int64_t sourceNumBlocks = 0;
int64_t threadFailedAttempts = 0;
int64_t threadDataBytes = 0;
int64_t threadEffectiveDataBytes = 0;
int64_t threadNumBlocks = 0;
for (const auto &stat : transferredSourceStats) {
sourceFailedAttempts += stat.getFailedAttempts();
sourceDataBytes += stat.getDataBytes();
sourceEffectiveDataBytes += stat.getEffectiveDataBytes();
sourceNumBlocks += stat.getNumBlocks();
}
for (const auto &stat : failedSourceStats) {
sourceFailedAttempts += stat.getFailedAttempts();
sourceDataBytes += stat.getDataBytes();
sourceEffectiveDataBytes += stat.getEffectiveDataBytes();
sourceNumBlocks += stat.getNumBlocks();
}
for (const auto &senderThread : senderThreads_) {
const auto &stat = senderThread->getTransferStats();
threadFailedAttempts += stat.getFailedAttempts();
threadDataBytes += stat.getDataBytes();
threadEffectiveDataBytes += stat.getEffectiveDataBytes();
threadNumBlocks += stat.getNumBlocks();
}
WDT_CHECK(sourceFailedAttempts == threadFailedAttempts);
WDT_CHECK(sourceDataBytes == threadDataBytes);
WDT_CHECK(sourceEffectiveDataBytes == threadEffectiveDataBytes);
WDT_CHECK(sourceNumBlocks == threadNumBlocks);
}
void Sender::setSocketCreator(Sender::ISocketCreator *socketCreator) {
socketCreator_ = socketCreator;
}
void Sender::reportProgress() {
WDT_CHECK(progressReportIntervalMillis_ > 0);
int throughputUpdateIntervalMillis =
options_.throughput_update_interval_millis;
WDT_CHECK(throughputUpdateIntervalMillis >= 0);
int throughputUpdateInterval =
throughputUpdateIntervalMillis / progressReportIntervalMillis_;
int64_t lastEffectiveBytes = 0;
std::chrono::time_point<Clock> lastUpdateTime = Clock::now();
int intervalsSinceLastUpdate = 0;
double currentThroughput = 0;
auto waitingTime = std::chrono::milliseconds(progressReportIntervalMillis_);
WLOG(INFO) << "Progress reporter tracking every "
<< progressReportIntervalMillis_ << " ms";
while (true) {
{
std::unique_lock<std::mutex> lock(mutex_);
conditionFinished_.wait_for(lock, waitingTime);
if (transferStatus_ == TransferStatus::THREADS_JOINED) {
break;
}
}
std::unique_ptr<TransferReport> transferReport = getTransferReport();
intervalsSinceLastUpdate++;
if (intervalsSinceLastUpdate >= throughputUpdateInterval) {
auto curTime = Clock::now();
int64_t curEffectiveBytes =
transferReport->getSummary().getEffectiveDataBytes();
double time = durationSeconds(curTime - lastUpdateTime);
currentThroughput = (curEffectiveBytes - lastEffectiveBytes) / time;
lastEffectiveBytes = curEffectiveBytes;
lastUpdateTime = curTime;
intervalsSinceLastUpdate = 0;
}
transferReport->setCurrentThroughput(currentThroughput);
progressReporter_->progress(transferReport);
if (reportPerfSignal_.notified()) {
logPerfStats();
}
}
}
void Sender::logPerfStats() const {
if (!options_.enable_perf_stat_collection) {
return;
}
PerfStatReport report(options_);
for (auto &senderThread : senderThreads_) {
report += senderThread->getPerfReport();
}
report += dirQueue_->getPerfReport();
WLOG(INFO) << report;
}
} // namespace wdt
} // namespace facebook