-
Notifications
You must be signed in to change notification settings - Fork 389
/
Receiver.h
620 lines (529 loc) · 21.4 KB
/
Receiver.h
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/**
* 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.
*/
#pragma once
#include "WdtBase.h"
#include "FileCreator.h"
#include "ErrorCodes.h"
#include "WdtOptions.h"
#include "Reporting.h"
#include "ServerSocket.h"
#include "Protocol.h"
#include "Writer.h"
#include "Throttler.h"
#include "TransferLogManager.h"
#include <memory>
#include <string>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>
namespace facebook {
namespace wdt {
/**
* Receiver is the receiving side of the transfer. Receiver listens on ports
* accepts connections, receives the files and writes to the destination
* directory. Receiver has two modes of operation : You can spawn a receiver
* for one transfer or alternatively it can also be used in a long running
* mode where it accepts subsequent transfers and runs in an infinite loop.
*/
class Receiver : public WdtBase {
public:
/// Constructor using wdt transfer request (@see in WdtBase.h)
explicit Receiver(const WdtTransferRequest &transferRequest);
/**
* Constructor with start port, number of ports and directory to write to.
* If the start port is specified as zero, it auto configures the ports
*/
Receiver(int port, int numSockets, const std::string &destDir);
/// Setup before starting (@see WdtBase.h)
WdtTransferRequest init() override;
/**
* Joins on the threads spawned by start. This method
* is called by default when the wdt receiver is expected
* to run as forever running process. However this has to
* be explicitly called when the caller expects to conclude
* a transfer.
*/
std::unique_ptr<TransferReport> finish() override;
/**
* Call this method instead of transferAsync() when you don't
* want the wdt receiver to stop after one transfer.
*/
ErrorCode runForever();
/**
* Starts the threads, and returns. Caller should call finish() after
* calling this method to get the statistics of the transfer.
*/
ErrorCode transferAsync() override;
/// Setter for the directory where the files received are written to
void setDir(const std::string &destDir);
/// Get the dir where receiver is transferring
const std::string &getDir();
/// @param recoveryId unique-id used to verify transfer log
void setRecoveryId(const std::string &recoveryId);
/**
* Destructor for the receiver. The destructor automatically cancels
* any incomplete transfers that are going on. 'Incomplete transfer' is a
* transfer where there is no receiver thread that has received
* confirmation from wdt sender that the transfer is 'DONE'. Destructor also
* internally calls finish() for every transfer if finish() wasn't called
*/
virtual ~Receiver();
/**
* Take a lock on the instance mutex and return the value of
* whether the existing transfer has been finished
*/
bool hasPendingTransfer();
/**
* Use the method to get the list of ports receiver is listening on
*/
std::vector<int32_t> getPorts() const;
private:
/**
* @param isFinished Mark transfer active/inactive
*/
void markTransferFinished(bool isFinished);
/**
* Traverses root directory and returns discovered file information
*
* @param fileChunksInfo discovered file info
*/
void traverseDestinationDir(std::vector<FileChunksInfo> &fileChunksInfo);
/**
* Wdt receiver has logic to maintain the consistency of the
* transfers through connection errors. All threads are run by the logic
* defined as a state machine. These are the all the states in that
* state machine
*/
enum ReceiverState {
LISTEN,
ACCEPT_FIRST_CONNECTION,
ACCEPT_WITH_TIMEOUT,
SEND_LOCAL_CHECKPOINT,
READ_NEXT_CMD,
PROCESS_FILE_CMD,
PROCESS_SETTINGS_CMD,
PROCESS_DONE_CMD,
PROCESS_SIZE_CMD,
SEND_FILE_CHUNKS,
SEND_GLOBAL_CHECKPOINTS,
SEND_DONE_CMD,
SEND_ABORT_CMD,
WAIT_FOR_FINISH_OR_NEW_CHECKPOINT,
WAIT_FOR_FINISH_WITH_THREAD_ERROR,
FAILED,
END
};
/**
* Structure to pass data to the state machine and also to share data between
* different state
*/
struct ThreadData {
/// Index of the thread that this data belongs to
const int threadIndex_;
/**
* Server socket object that provides functionality such as listen()
* accept, read, write on the socket
*/
ServerSocket &socket_;
/// Statistics of the transfer for this thread
TransferStats &threadStats_;
/// protocol version for this thread. This per thread protocol version is
/// kept separately from the global one to avoid locking
int threadProtocolVersion_;
/// Buffer that receivers reads data into from the network
std::unique_ptr<char[]> buf_;
/// Maximum size of the buffer
const int64_t bufferSize_;
/// Marks the number of bytes already read in the buffer
int64_t numRead_{0};
/// Following two are markers to mark how much data has been read/parsed
int64_t off_{0};
int64_t oldOffset_{0};
/// number of checkpoints already transferred
int checkpointIndex_{0};
/**
* Pending value of checkpoint count. Since write call success does not
* guarantee actual transfer, we do not apply checkpoint count update after
* the write. Only after receiving next cmd from sender, we apply the
* update
*/
int pendingCheckpointIndex_{0};
/// a counter incremented each time a new session starts for this thread
int64_t transferStartedCount_{0};
/// a counter incremented each time a new session ends for this thread
int64_t transferFinishedCount_{0};
/// read timeout for sender
int64_t senderReadTimeout_{-1};
/// write timeout for sender
int64_t senderWriteTimeout_{-1};
/// whether checksum verification is enabled or not
bool enableChecksum_{false};
/**
* Whether SEND_DONE_CMD state has already failed for this session or not.
* This has to be separately handled, because session barrier is
* implemented before sending done cmd
*/
bool doneSendFailure_{false};
/// Checkpoints that have not been sent back to the sender
std::vector<Checkpoint> newCheckpoints_;
/// whether settings has been received and verified for the current
/// connection. This is used to determine round robin order for polling in
/// the server socket
bool curConnectionVerified_{false};
/// whether the transfer is in block mode or not
bool isBlockMode_{true};
Checkpoint checkpoint_;
/// Constructor for thread data
ThreadData(int threadIndex, ServerSocket &socket,
TransferStats &threadStats, int protocolVersion,
int64_t bufferSize)
: threadIndex_(threadIndex),
socket_(socket),
threadStats_(threadStats),
threadProtocolVersion_(protocolVersion),
bufferSize_(bufferSize),
checkpoint_(socket.getPort()) {
buf_.reset(new char[bufferSize_]);
}
/**
* In long running mode, we need to reset thread variables after each
* session. Before starting each session, reset() has to called to do that.
*/
void reset() {
numRead_ = off_ = 0;
checkpointIndex_ = pendingCheckpointIndex_ = 0;
doneSendFailure_ = false;
senderReadTimeout_ = senderWriteTimeout_ = -1;
curConnectionVerified_ = false;
threadStats_.reset();
}
/// Get the raw pointer to the buffer
char *getBuf() {
return buf_.get();
}
};
/// Overloaded operator for printing thread info
friend std::ostream &operator<<(std::ostream &os, const ThreadData &data);
typedef ReceiverState (Receiver::*StateFunction)(ThreadData &data);
/**
* Tries to listen/bind to port. If this fails, thread is considered failed.
* Previous states : n/a (start state)
* Next states : ACCEPT_FIRST_CONNECTION(success),
* FAILED(failure)
*/
ReceiverState listen(ThreadData &data);
/**
* Tries to accept first connection of a new session. Periodically checks
* whether a new session has started or not. If a new session has started then
* goes to ACCEPT_WITH_TIMEOUT state. Also does session initialization. In
* joinable mode, tries to accept for a limited number of user specified
* retries.
* Previous states : LISTEN,
* END(if in long running mode)
* Next states : ACCEPT_WITH_TIMEOUT(if a new transfer has started and this
* thread has not received a connection),
* WAIT_FOR_FINISH_WITH_THREAD_ERROR(if did not receive a
* connection in specified number of retries),
* READ_NEXT_CMD(if a connection was received)
*/
ReceiverState acceptFirstConnection(ThreadData &data);
/**
* Tries to accept a connection with timeout. There are 2 kinds of timeout. At
* the beginning of the session, it uses accept window as the timeout. Later
* when sender settings are known it uses max(readTimeOut, writeTimeout)) +
* buffer(500) as the timeout.
* Previous states : Almost all states(for any network errors during transfer,
* we transition to this state),
* Next states : READ_NEXT_CMD(if there are no previous errors and accept
* was successful),
* SEND_LOCAL_CHECKPOINT(if there were previous errors and
* accept was successful),
* WAIT_FOR_FINISH_WITH_THREAD_ERROR(if accept failed and
* transfer previously failed during SEND_DONE_CMD state. Thus
* case needs special handling to ensure that we do not mess up
* session variables),
* END(if accept fails otherwise)
*/
ReceiverState acceptWithTimeout(ThreadData &data);
/**
* Sends local checkpoint to the sender. In case of previous error during
* SEND_LOCAL_CHECKPOINT state, we send -1 as the checkpoint.
* Previous states : ACCEPT_WITH_TIMEOUT
* Next states : ACCEPT_WITH_TIMEOUT(if sending fails),
* SEND_DONE_CMD(if send is successful and we have previous
* SEND_DONE_CMD error),
* READ_NEXT_CMD(if send is successful otherwise)
*/
ReceiverState sendLocalCheckpoint(ThreadData &data);
/**
* Reads next cmd and transitions to the state accordingly.
* Previous states : SEND_LOCAL_CHECKPOINT,
* ACCEPT_FIRST_CONNECTION,
* ACCEPT_WITH_TIMEOUT,
* PROCESS_SETTINGS_CMD,
* PROCESS_FILE_CMD,
* SEND_GLOBAL_CHECKPOINTS,
* Next states : PROCESS_FILE_CMD,
* PROCESS_DONE_CMD,
* PROCESS_SETTINGS_CMD,
* PROCESS_SIZE_CMD,
* ACCEPT_WITH_TIMEOUT(in case of read failure),
* WAIT_FOR_FINISH_WITH_THREAD_ERROR(in case of protocol errors)
*/
ReceiverState readNextCmd(ThreadData &data);
/**
* Processes file cmd. Logic of how we write the file to the destination
* directory is defined here.
* Previous states : READ_NEXT_CMD
* Next states : READ_NEXT_CMD(success),
* WAIT_FOR_FINISH_WITH_THREAD_ERROR(protocol error),
* ACCEPT_WITH_TIMEOUT(socket read failure)
*/
ReceiverState processFileCmd(ThreadData &data);
/**
* Processes settings cmd. Settings has a connection settings,
* protocol version, transfer id, etc. For more info check Protocol.h
* Previous states : READ_NEXT_CMD,
* Next states : READ_NEXT_CMD(success),
* WAIT_FOR_FINISH_WITH_THREAD_ERROR(protocol error),
* ACCEPT_WITH_TIMEOUT(socket read failure),
* SEND_FILE_CHUNKS(If the sender wants to resume transfer)
*/
ReceiverState processSettingsCmd(ThreadData &data);
/**
* Processes done cmd. Also checks to see if there are any new global
* checkpoints or not
* Previous states : READ_NEXT_CMD,
* Next states : WAIT_FOR_FINISH_OR_ERROR(protocol error),
* WAIT_FOR_FINISH_OR_NEW_CHECKPOINT(success),
* SEND_GLOBAL_CHECKPOINTS(if there are global errors)
*/
ReceiverState processDoneCmd(ThreadData &data);
/**
* Processes size cmd. Sets the value of totalSenderBytes_
* Previous states : READ_NEXT_CMD,
* Next states : READ_NEXT_CMD(success),
* WAIT_FOR_FINISH_WITH_THREAD_ERROR(protocol error)
*/
ReceiverState processSizeCmd(ThreadData &data);
/**
* Sends file chunks that were received successfully in any previous transfer,
* this is the first step in download resumption.
* Checks to see if they have already been transferred or not.
* If yes, send ACK. If some other thread is sending it, sends wait cmd
* and checks again later. Otherwise, breaks the entire data into bufferSIze_
* chunks and sends it.
* Previous states: PROCESS_SETTINGS_CMD,
* Next states : ACCEPT_WITH_TIMEOUT(network error),
* READ_NEXT_CMD(success)
*/
ReceiverState sendFileChunks(ThreadData &data);
/**
* Sends global checkpoints to sender
* Previous states : PROCESS_DONE_CMD,
* WAIT_FOR_FINISH_OR_ERROR
* Next states : READ_NEXT_CMD(success),
* ACCEPT_WITH_TIMEOUT(socket write failure)
*/
ReceiverState sendGlobalCheckpoint(ThreadData &data);
/**
* Sends DONE to sender, also tries to read back ack. If anything fails during
* this state, doneSendFailure_ thread variable is set. This flag makes the
* state machine behave differently, effectively bypassing all session related
* things.
* Previous states : SEND_LOCAL_CHECKPOINT,
* WAIT_FOR_FINISH_OR_ERROR
* Next states : END(success),
* ACCEPT_WITH_TIMEOUT(failure)
*/
ReceiverState sendDoneCmd(ThreadData &data);
/**
* Sends ABORT cmd back to the sender
* Previous states : PROCESS_FILE_CMD
* Next states : WAIT_FOR_FINISH_WITH_THREAD_ERROR
*/
ReceiverState sendAbortCmd(ThreadData &data);
/**
* Waits for transfer to finish or new checkpoints. This state first
* increments waitingThreadCount_. Then, it
* waits till all the threads have finished. It sends periodic WAIT signal to
* prevent sender from timing out. If a new checkpoint is found, we move to
* SEND_GLOBAL_CHECKPOINTS state.
* Previous states : PROCESS_DONE_CMD
* Next states : SEND_DONE_CMD(all threads finished),
* SEND_GLOBAL_CHECKPOINTS(if new checkpoints are found),
* ACCEPT_WITH_TIMEOUT(if socket write fails)
*/
ReceiverState waitForFinishOrNewCheckpoint(ThreadData &data);
/**
* Waits for transfer to finish. Only called when there is an error for the
* thread. It adds a checkpoint to the global list of checkpoints if a
* connection was received. It increments waitingWithErrorThreadCount_ and
* waits till the session ends.
* Previous states : Almost all states
* Next states : END
*/
ReceiverState waitForFinishWithThreadError(ThreadData &data);
/// Mapping from receiver states to state functions
static const StateFunction stateMap_[];
/// Responsible for basic setup and starting threads
void start();
/// This method is the entry point for each thread.
void receiveOne(int threadIndex, ServerSocket &s, int64_t bufferSize,
TransferStats &threadStats);
/**
* Periodically calculates current transfer report and send it to progress
* reporter. This only works in the single transfer mode.
*/
void progressTracker();
/**
* Adds a checkpoint to the global checkpoint list
* @param checkpoint checkpoint to be added
*/
void addCheckpoint(Checkpoint checkpoint);
/**
* @param startIndex number of checkpoints already transferred by the
* calling thread
* @return list of new checkpoints
*/
std::vector<Checkpoint> getNewCheckpoints(int startIndex);
/// Returns true if all threads finished for this session
bool areAllThreadsFinished(bool checkpointAdded);
/// Ends current global session
void endCurGlobalSession();
/**
* Returns if a new session has started and the thread is not aware of it
* A thread must hold lock on mutex_ before calling this
*/
bool hasNewSessionStarted(ThreadData &data);
/**
* Start new transfer by incrementing transferStartedCount_
* A thread must hold lock on mutex_ before calling this
*/
void startNewGlobalSession(ThreadData &data);
/**
* Returns whether the current session has finished or not.
* A thread must hold lock on mutex_ before calling this
*/
bool hasCurSessionFinished(ThreadData &data);
/// Starts a new session for the thread
void startNewThreadSession(ThreadData &data);
/// Ends current thread session
void endCurThreadSession(ThreadData &data);
/// Increments failed thread count, does not wait for transfer to finish
void incrFailedThreadCountAndCheckForSessionEnd(ThreadData &data);
/// adds log header and also a directory invalidation entry if needed
void addTransferLogHeader(bool isBlockMode, bool isSenderResuming);
/// fix and close transfer log
void fixAndCloseTransferLog(bool transferSuccess);
/**
* Get transfer report, meant to be called after threads have been finished
* This method is not thread safe
*/
std::unique_ptr<TransferReport> getTransferReport();
/// @return transfer config encoded as int
int64_t getTransferConfig() const;
/// The thread that is responsible for calling running the progress tracker
std::thread progressTrackerThread_;
/**
* Flags that represents if a transfer has finished. Threads on completion
* set this flag. This is always accurate even if you don't call finish()
* No transfer can be started as long as this flag is false.
*/
bool transferFinished_{true};
/// Flag based on which threads finish processing on receiving a done
bool isJoinable_{false};
/// Destination directory where the received files will be written
std::string destDir_;
/// Responsible for writing files on the disk
std::unique_ptr<FileCreator> fileCreator_;
/**
* Unique-id used to verify transfer log. This value must be same for
* transfers across resumption
*/
std::string recoveryId_;
/**
* Progress tracker thread is a thread which has to be joined when the
* transfer is finished. The root thread in finish() and the progress
* tracker coordinate with each other through the boolean and this
* condition variable.
*/
std::condition_variable conditionRecvFinished_;
/**
* The instance of the receiver threads are stored in this vector.
* This will not be destroyed until this object is destroyed, hence
* it has to be made sure that these threads are joined at least before
* the destruction of this object.
*/
std::vector<std::thread> receiverThreads_;
/**
* start() gives each thread the instance of the serversocket, these
* sockets can be closed and changed completely by the progress tracker
* thread thus ending any hope of receiver threads doing any further
* successful transfer
*/
std::vector<ServerSocket> threadServerSockets_;
/**
* Bunch of stats objects given to each thread by the root thread
* so that finish() can summarize the result at the end of joining.
*/
std::vector<TransferStats> threadStats_;
/// Per thread perf report
std::vector<PerfStatReport> perfReports_;
/// Transfer log manager
TransferLogManager transferLogManager_;
/// Enum representing status of file chunks transfer
enum SendChunkStatus { NOT_STARTED, IN_PROGRESS, SENT };
/// State of the receiver when sending file chunks in sendFileChunksCmd
SendChunkStatus sendChunksStatus_{NOT_STARTED};
/**
* All threads coordinate with each other to send previously received file
* chunks using this condition variable.
*/
mutable std::condition_variable conditionFileChunksSent_;
/// Number of blocks sent by the sender
int64_t numBlocksSend_{-1};
/// Global list of checkpoints
std::vector<Checkpoint> checkpoints_;
/// Number of threads which failed in the transfer
int failedThreadCount_{0};
/// Number of threads which are waiting for finish or new checkpoint
int waitingThreadCount_{0};
/// Number of threads which are waiting with an error
int waitingWithErrorThreadCount_{0};
/// Counter that is incremented each time a new session starts
int64_t transferStartedCount_{0};
/// Counter that is incremented each time a new session ends
int64_t transferFinishedCount_{0};
/// Total number of data bytes sender wants to transfer
int64_t totalSenderBytes_{-1};
/// Start time of the session
std::chrono::time_point<Clock> startTime_;
/// already transferred file chunks
std::vector<FileChunksInfo> fileChunksInfo_;
/// Mutex to guard all the shared variables
mutable std::mutex mutex_;
/// Condition variable to coordinate transfer finish
mutable std::condition_variable conditionAllFinished_;
/**
* Returns true if threads have been joined (done in finish())
* This is how destructor determines whether it should join threads
*/
bool areThreadsJoined_{false};
/// Number of active threads, decremented every time a thread is finished
int32_t numActiveThreads_{0};
/**
* Mutex for the management of this instance, specifically to keep the
* instance sane for multi threaded public API calls
*/
std::mutex instanceManagementMutex_;
};
}
} // namespace facebook::wdt