-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathSender.cpp
535 lines (503 loc) · 19.2 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include "Sender.h"
#include "ClientSocket.h"
#include "Protocol.h"
#include "Throttler.h"
#include <folly/Conv.h>
#include <folly/Memory.h>
#include <folly/String.h>
#include <thread>
// Constants for different calculations
/*
* If you change any of the multipliers be
* sure to replace them in the description above.
*/
const double kMbToB = 1024 * 1024;
const double kPeakMultiplier = 1.2;
const int kBucketMultiplier = 2;
const double kTimeMultiplier = 0.25;
namespace {
template <typename T>
double durationSeconds(T d) {
return std::chrono::duration_cast<std::chrono::duration<double>>(d).count();
}
} // anonymous namespace
namespace facebook {
namespace wdt {
/// default progress reporter
static void progressReporter(const TransferStats &stats,
size_t numDiscoveredSources,
size_t totalDiscoveredSize, double throughput) {
int progress = 0;
if (totalDiscoveredSize > 0) {
progress = stats.getEffectiveDataBytes() * 100 / totalDiscoveredSize;
}
int scaledProgress = progress / 2;
std::cout << '\r';
std::cout << '[';
for (int i = 0; i < scaledProgress - 1; i++) {
std::cout << '=';
}
if (scaledProgress != 0) {
std::cout << (scaledProgress == 50 ? '=' : '>');
}
for (int i = 0; i < 50 - scaledProgress - 1; i++) {
std::cout << ' ';
}
std::cout << "] " << progress << "% " << std::setprecision(2) << std::fixed
<< throughput << " Mbytes/sec";
if (progress == 100) {
// transfer finished
std::cout << '\n';
}
std::cout.flush();
}
Sender::Sender(int port, int numSockets, const std::string &destHost,
const std::string &srcDir)
: Sender(destHost, srcDir) {
this->port_ = port;
this->numSockets_ = numSockets;
}
Sender::Sender(const std::string &destHost, const std::string &srcDir)
: progressReporter_(&progressReporter) {
this->destHost_ = destHost;
this->srcDir_ = srcDir;
const auto &options = WdtOptions::get();
this->port_ = options.port_;
this->numSockets_ = options.numSockets_;
this->followSymlinks_ = options.followSymlinks_;
this->includeRegex_ = options.includeRegex_;
this->excludeRegex_ = options.excludeRegex_;
this->pruneDirRegex_ = options.pruneDirRegex_;
this->progressReportIntervalMillis_ = options.progressReportIntervalMillis_;
}
void Sender::setIncludeRegex(const std::string &includeRegex) {
includeRegex_ = includeRegex;
}
void Sender::setExcludeRegex(const std::string &excludeRegex) {
excludeRegex_ = excludeRegex;
}
void Sender::setPruneDirRegex(const std::string &pruneDirRegex) {
pruneDirRegex_ = pruneDirRegex;
}
void Sender::setPort(const int port) {
port_ = port;
}
void Sender::setNumSockets(const int numSockets) {
numSockets_ = numSockets;
}
void Sender::setSrcFileInfo(const std::vector<FileInfo> &srcFileInfo) {
srcFileInfo_ = srcFileInfo;
}
void Sender::setFollowSymlinks(const bool followSymlinks) {
followSymlinks_ = followSymlinks;
}
void Sender::setProgressReportIntervalMillis(
const int progressReportIntervalMillis) {
progressReportIntervalMillis_ = progressReportIntervalMillis;
}
void Sender::setProgressReporter(const ProgressReporter &progressReporter) {
progressReporter_ = progressReporter;
}
std::unique_ptr<TransferReport> Sender::start() {
const auto &options = WdtOptions::get();
const bool twoPhases = options.twoPhases_;
LOG(INFO) << "Client (sending) to " << destHost_ << " port " << port_ << " : "
<< numSockets_ << " sockets, source dir " << srcDir_;
auto startTime = Clock::now();
DirectorySourceQueue queue(srcDir_);
queue.setIncludePattern(includeRegex_);
queue.setExcludePattern(excludeRegex_);
queue.setPruneDirPattern(pruneDirRegex_);
queue.setFileInfo(srcFileInfo_);
queue.setFollowSymlinks(followSymlinks_);
std::thread dirThread = queue.buildQueueAsynchronously();
std::thread progressReporterThread;
bool progressReportEnabled =
progressReporter_ && progressReportIntervalMillis_ > 0;
double directoryTime;
if (twoPhases) {
dirThread.join();
directoryTime = durationSeconds(Clock::now() - startTime);
}
std::vector<std::thread> vt;
std::vector<TransferStats> threadStats;
for (int i = 0; i < numSockets_; i++) {
threadStats.emplace_back(true);
}
std::vector<std::vector<TransferStats>> sourceStats(numSockets_);
double avgRateBytesPerSec = options.avgMbytesPerSec_ * kMbToB;
double peakRateBytesPerSec = options.maxMbytesPerSec_ * kMbToB;
double bucketLimitBytes = options.throttlerBucketLimit_ * kMbToB;
double perThreadAvgRateBytesPerSec = avgRateBytesPerSec / numSockets_;
double perThreadPeakRateBytesPerSec = peakRateBytesPerSec / numSockets_;
double perThreadBucketLimit = bucketLimitBytes / numSockets_;
if (avgRateBytesPerSec < 1.0 && avgRateBytesPerSec >= 0) {
LOG(FATAL) << "Realistic average rate"
" should be greater than 1.0 bytes/sec";
}
if (perThreadPeakRateBytesPerSec < perThreadAvgRateBytesPerSec &&
perThreadPeakRateBytesPerSec >= 0) {
LOG(WARNING) << "Per thread peak rate should be greater "
<< "than per thread average rate. "
<< "Making peak rate 1.2 times the average rate";
perThreadPeakRateBytesPerSec =
kPeakMultiplier * perThreadAvgRateBytesPerSec;
}
if (perThreadBucketLimit <= 0 && perThreadPeakRateBytesPerSec > 0) {
perThreadBucketLimit =
kTimeMultiplier * kBucketMultiplier * perThreadPeakRateBytesPerSec;
LOG(INFO) << "Burst limit not specified but peak "
<< "rate is configured. Auto configuring to "
<< perThreadBucketLimit / kMbToB << " mbytes";
}
VLOG(1) << "Per thread (Avg Rate, Peak Rate) = "
<< "(" << perThreadAvgRateBytesPerSec << ", "
<< perThreadPeakRateBytesPerSec << ")";
for (int i = 0; i < numSockets_; i++) {
threadStats[i].setId(folly::to<std::string>(i));
vt.emplace_back(&Sender::sendOne, this, startTime, std::ref(destHost_),
port_ + i, std::ref(queue), perThreadAvgRateBytesPerSec,
perThreadPeakRateBytesPerSec, perThreadBucketLimit,
std::ref(threadStats[i]), std::ref(sourceStats[i]));
}
if (progressReportEnabled) {
std::thread reporterThread(&Sender::reportProgress, this, startTime,
std::ref(threadStats), std::ref(queue));
progressReporterThread = std::move(reporterThread);
}
if (!twoPhases) {
dirThread.join();
directoryTime = durationSeconds(Clock::now() - startTime);
}
for (int i = 0; i < numSockets_; i++) {
vt[i].join();
}
if (progressReportEnabled) {
{
std::unique_lock<std::mutex> lock(mutex_);
transferFinished_ = true;
conditionFinished_.notify_all();
}
progressReporterThread.join();
}
std::vector<TransferStats> transferredSourceStats;
if (WdtOptions::get().fullReporting_) {
for (auto &stats : sourceStats) {
transferredSourceStats.insert(transferredSourceStats.end(),
std::make_move_iterator(stats.begin()),
std::make_move_iterator(stats.end()));
}
validateTransferStats(transferredSourceStats, queue.getFailedSourceStats(),
threadStats);
}
std::unique_ptr<TransferReport> report = folly::make_unique<TransferReport>(
transferredSourceStats, queue.getFailedSourceStats(), threadStats);
double totalTime = durationSeconds(Clock::now() - startTime);
LOG(INFO) << "Total sender time = " << totalTime << " seconds ("
<< directoryTime << " dirTime)"
<< ". Transfer summary : " << *report
<< "\nTotal sender throughput = "
<< report->getSummary().getEffectiveTotalBytes() / totalTime /
kMbToB << " Mbytes/sec ("
<< report->getSummary().getEffectiveTotalBytes() /
(totalTime - directoryTime) / kMbToB
<< " Mbytes/sec pure transf rate)";
return report;
}
void Sender::validateTransferStats(
const std::vector<TransferStats> &transferredSourceStats,
const std::vector<TransferStats> &failedSourceStats,
const std::vector<TransferStats> &threadStats) {
size_t sourceFailedAttempts = 0;
size_t sourceNumFiles = transferredSourceStats.size();
size_t sourceDataBytes = 0;
size_t sourceEffectiveDataBytes = 0;
size_t threadFailedAttempts = 0;
size_t threadNumFiles = 0;
size_t threadDataBytes = 0;
size_t threadEffectiveDataBytes = 0;
for (const auto &stat : transferredSourceStats) {
sourceFailedAttempts += stat.getFailedAttempts();
WDT_CHECK(stat.getNumFiles() == 1);
sourceDataBytes += stat.getDataBytes();
sourceEffectiveDataBytes += stat.getEffectiveDataBytes();
}
for (const auto &stat : failedSourceStats) {
sourceFailedAttempts += stat.getFailedAttempts();
WDT_CHECK(stat.getNumFiles() == 0);
sourceDataBytes += stat.getDataBytes();
sourceEffectiveDataBytes += stat.getEffectiveDataBytes();
}
for (const auto &stat : threadStats) {
threadFailedAttempts += stat.getFailedAttempts();
threadNumFiles += stat.getNumFiles();
threadDataBytes += stat.getDataBytes();
threadEffectiveDataBytes += stat.getEffectiveDataBytes();
}
WDT_CHECK(sourceFailedAttempts == threadFailedAttempts);
WDT_CHECK(sourceNumFiles == threadNumFiles);
WDT_CHECK(sourceDataBytes == threadDataBytes);
WDT_CHECK(sourceEffectiveDataBytes == threadEffectiveDataBytes);
}
std::unique_ptr<ClientSocket> Sender::makeSocket(const std::string &destHost,
int port) {
return folly::make_unique<ClientSocket>(destHost,
folly::to<std::string>(port));
}
std::unique_ptr<ClientSocket> Sender::connectToReceiver(
const std::string &destHost, const int port, ErrorCode &errCode,
Clock::time_point startTime) {
const auto &options = WdtOptions::get();
int connectAttempts = 0;
std::unique_ptr<ClientSocket> socket = makeSocket(destHost, port);
for (int i = 1; i <= options.maxRetries_; ++i) {
++connectAttempts;
errCode = socket->connect();
if (errCode == OK) {
break;
} else if (errCode == CONN_ERROR) {
return nullptr;
}
if (i != options.maxRetries_) {
// sleep between attempts but not after the last
VLOG(1) << "Sleeping after failed attempt " << i;
usleep(options.sleepMillis_ * 1000);
}
}
double elapsedSecsConn = durationSeconds(Clock::now() - startTime);
if (errCode != OK) {
LOG(ERROR) << "Unable to connect despite " << connectAttempts
<< " retries in " << elapsedSecsConn << " seconds.";
errCode = CONN_ERROR;
return nullptr;
}
((connectAttempts > 1) ? LOG(WARNING) : LOG(INFO))
<< "Connection took " << connectAttempts << " attempt(s) and "
<< elapsedSecsConn << " seconds.";
return socket;
}
/**
* @param startTime Time when this thread was spawned
* @param destHost Address to the destination, see ClientSocket
* @param port Port to establish connect
* @param queue DirectorySourceQueue object for reading files
* @param avgRateBytes Average rate of throttler in bytes/sec
* @param maxRateBytes Peak rate for Token Bucket algorithm in
bytes/sec
Checkout Throttler.h for Token Bucket
* @param bucketLimitBytes Bucket Limit for Token Bucket algorithm in
bytes
* @param threadStats Per thread statistics
* @param transferredFileStats Stats for successfully transferred files
*/
void Sender::sendOne(Clock::time_point startTime, const std::string &destHost,
int port, DirectorySourceQueue &queue, double avgRateBytes,
double maxRateBytes, double bucketLimitBytes,
TransferStats &threadStats,
std::vector<TransferStats> &transferredFileStats) {
std::unique_ptr<Throttler> throttler;
const auto &options = WdtOptions::get();
const bool doThrottling = (avgRateBytes > 0 || maxRateBytes > 0);
if (doThrottling) {
throttler = folly::make_unique<Throttler>(startTime, avgRateBytes,
maxRateBytes, bucketLimitBytes,
options.throttlerLogTimeMillis_);
} else {
VLOG(1) << "No throttling in effect";
}
ErrorCode code;
std::unique_ptr<ClientSocket> socket =
connectToReceiver(destHost, port, code, startTime);
if (code != OK) {
threadStats.setErrorCode(code);
return;
}
char headerBuf[Protocol::kMaxHeader];
while (true) {
auto source = queue.getNextSource();
if (!source) {
break;
}
source->open();
if (options.ignoreOpenErrors_ && source->hasError()) {
continue;
}
TransferStats transferStats;
size_t totalBytes = threadStats.getTotalBytes();
transferStats =
sendOneByteSource(socket, throttler, source, doThrottling, totalBytes);
threadStats += transferStats;
source->addTransferStats(transferStats);
if (transferStats.getErrorCode() == OK) {
if (options.fullReporting_) {
transferredFileStats.emplace_back(
std::move(source->getTransferStats()));
}
} else {
queue.returnToQueue(source);
if (transferStats.getErrorCode() == SOCKET_WRITE_ERROR) {
socket = connectToReceiver(destHost, port, code, Clock::now());
if (code != OK) {
threadStats.setErrorCode(code);
return;
}
}
}
}
headerBuf[0] = Protocol::DONE_CMD;
ssize_t written = socket->write(headerBuf, 1);
if (written != 1) {
LOG(ERROR) << "Socket write failure " << written;
threadStats.setErrorCode(SOCKET_WRITE_ERROR);
return;
}
threadStats.addHeaderBytes(1);
threadStats.addEffectiveBytes(1, 0);
socket->shutdown();
VLOG(1) << "Wrote done cmd on " << socket->getFd() << " waiting for reply...";
ssize_t numRead = socket->read(headerBuf, 1); // TODO: returns 0 on disk full
if (numRead != 1) {
LOG(ERROR) << "READ unexpected " << numRead;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return;
}
if (headerBuf[0] != Protocol::DONE_CMD) {
LOG(ERROR) << "Unexpected reply " << headerBuf[0];
threadStats.setErrorCode(PROTOCOL_ERROR);
return;
}
numRead = socket->read(headerBuf, Protocol::kMaxHeader);
if (numRead != 0) {
LOG(ERROR) << "EOF not found when expected " << (numRead < 0)
? "-1"
: folly::humanify(std::string(headerBuf, numRead));
threadStats.setErrorCode(SOCKET_READ_ERROR);
return;
}
threadStats.setErrorCode(OK);
double totalTime = durationSeconds(Clock::now() - startTime);
LOG(INFO) << "Got reply - all done for fd:" << socket->getFd() << ". "
<< "Transfer stat : " << threadStats << " Total throughput = "
<< threadStats.getEffectiveTotalBytes() / totalTime / kMbToB
<< " Mbytes/sec";
return;
}
TransferStats Sender::sendOneByteSource(
const std::unique_ptr<ClientSocket> &socket,
const std::unique_ptr<Throttler> &throttler,
const std::unique_ptr<ByteSource> &source, const bool doThrottling,
const size_t totalBytes) {
TransferStats stats;
char headerBuf[Protocol::kMaxHeader];
size_t off = 0;
headerBuf[off++] = Protocol::FILE_CMD;
const size_t expectedSize = source->getSize();
size_t actualSize = 0;
Protocol::encode(headerBuf, off, Protocol::kMaxHeader,
source->getIdentifier(), expectedSize);
ssize_t written = socket->write(headerBuf, off);
if (written != off) {
LOG(ERROR) << "Write error/mismatch " << written << " " << off;
stats.setErrorCode(SOCKET_WRITE_ERROR);
stats.incrFailedAttempts();
return stats;
}
stats.addHeaderBytes(written);
VLOG(3) << "Sent " << written << " on " << socket->getFd() << " : "
<< folly::humanify(std::string(headerBuf, off));
while (!source->finished()) {
size_t size;
char *buffer = source->read(size);
if (source->hasError()) {
LOG(ERROR) << "Failed reading file " << source->getIdentifier()
<< " for fd " << socket->getFd();
break;
}
WDT_CHECK(buffer && size > 0);
written = 0;
if (doThrottling) {
/**
* If throttling is enabled we call limit(totalBytes) which
* used both the methods of throttling peak and average.
* Always call it with totalBytes written till now, throttler
* will do the rest. Total bytes includes header and the data bytes.
* The throttler was constructed at the time when the header
* was being written and it is okay to start throttling with the
* next expected write.
*/
throttler->limit(totalBytes + stats.getTotalBytes() + size);
}
do {
ssize_t w = socket->write(buffer + written, size - written);
if (w < 0) {
// TODO: retries, close connection etc...
LOG(ERROR) << "Write error " << written << " (" << size << ")";
stats.setErrorCode(SOCKET_WRITE_ERROR);
stats.incrFailedAttempts();
return stats;
}
stats.addDataBytes(w);
written += w;
if (w != size) {
VLOG(1) << "Short write " << w << " sub total now " << written << " on "
<< socket->getFd() << " out of " << size;
} else {
VLOG(3) << "Wrote all of " << size << " on " << socket->getFd();
}
} while (written < size);
if (written > size) {
LOG(ERROR) << "Write error " << written << " > " << size;
stats.setErrorCode(SOCKET_WRITE_ERROR);
stats.incrFailedAttempts();
return stats;
}
actualSize += written;
}
if (actualSize != expectedSize) {
LOG(ERROR) << "UGH " << source->getIdentifier() << " " << expectedSize
<< " " << actualSize;
// Can only happen if sender thread can not read complete source byte
// stream
stats.setErrorCode(BYTE_SOURCE_READ_ERROR);
stats.incrFailedAttempts();
return stats;
}
stats.setErrorCode(OK);
stats.incrNumFiles();
stats.addEffectiveBytes(stats.getHeaderBytes(), stats.getDataBytes());
return stats;
}
void Sender::reportProgress(Clock::time_point startTime,
std::vector<TransferStats> &threadStats,
DirectorySourceQueue &queue) {
WDT_CHECK(progressReportIntervalMillis_ > 0);
auto waitingTime = std::chrono::milliseconds(progressReportIntervalMillis_);
bool done = false;
do {
{
std::unique_lock<std::mutex> lock(mutex_);
conditionFinished_.wait_for(lock, waitingTime);
done = transferFinished_;
}
if (!queue.fileDiscoveryFinished()) {
continue;
}
TransferStats stats;
for (int index = 0; index < threadStats.size(); index++) {
stats += threadStats[index];
}
auto pair = queue.getCountAndSize();
double totalTime = durationSeconds(Clock::now() - startTime);
double throughput = stats.getEffectiveTotalBytes() / totalTime / kMbToB;
(*progressReporter_)(stats, pair.first, pair.second, throughput);
if (stats.getEffectiveDataBytes() == pair.second) {
// transfer finished. this check is needed to ensure progress report
// callback does not get called with 100% done multiple time. In our
// default implementation of progress reporter, we print a newline after
// transfer completion.
done = true;
}
} while (!done);
}
}
} // namespace facebook::wdt