forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransferLogManager.h
384 lines (321 loc) · 12.3 KB
/
TransferLogManager.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
/**
* 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 <wdt/Protocol.h>
#include <wdt/WdtOptions.h>
#include <condition_variable>
#include <iostream>
#include <map>
#include <mutex>
#include <set>
#include <string>
#include <thread>
namespace facebook {
namespace wdt {
/**
* Download Resumption in WDT:
* WDT can resume download in two modes.
*
* 1) Log based resumption : In this mode, WDT writes all important events like
* start of a new transfer, file creation, file resize, block write to .wdt.log
* in the destination directory. Before the transfer is started, this log is
* parsed and a list of FileChunksInfo is created. This information is passed to
* the sender which does a diff and sends only the missing portions.
*
* 2) Size based resumption : In this mode, WDT traverses the destination
* directory and trusts the size of the file. Sender only sends the extra
* portions of the file. Before traversing the directory .wdt.log is checked to
* see if previous transfers were also done in this mode or not. If the mode has
* changed, we add an INCONSISTENT_DIRECTORY entry to the log and transfer
* everything. This mode enabled by setting resume_using_dir_tree option to
* true.
*/
constexpr char kWdtLogName[] = ".wdt.log";
constexpr char kWdtBuggyLogName[] = ".wdt.log.bug";
/**
* class responsible for encoding and decoding transfer log entries
*/
class LogEncoderDecoder {
public:
/// encodes header entry
int64_t encodeLogHeader(char *dest, int64_t max,
const std::string &recoveryId,
const std::string &senderIp, int64_t config);
/// decodes header entry
bool decodeLogHeader(char *buf, int16_t size, int64_t ×tamp,
int &version, std::string &recoveryId,
std::string &senderIp, int64_t &config);
/// encodes file creation entry
int64_t encodeFileCreationEntry(char *dest, int64_t max,
const std::string &fileName,
const int64_t seqId, const int64_t fileSize);
/// decodes file creation entry
bool decodeFileCreationEntry(char *buf, int16_t size, int64_t ×tamp,
std::string &fileName, int64_t &seqId,
int64_t &fileSize);
/// encodes block write entry
int64_t encodeBlockWriteEntry(char *dest, int64_t max, const int64_t seqId,
const int64_t offset, const int64_t blockSize);
/// decodes block write entry
bool decodeBlockWriteEntry(char *buf, int16_t size, int64_t ×tamp,
int64_t &seqId, int64_t &offset,
int64_t &blockSize);
/// encodes file resize entry
int64_t encodeFileResizeEntry(char *dest, int64_t max, const int64_t seqId,
const int64_t fileSize);
/// decodes file resize entry
bool decodeFileResizeEntry(char *buf, int16_t size, int64_t ×tamp,
int64_t &seqId, int64_t &fileSize);
/// encodes file invalidation entry
int64_t encodeFileInvalidationEntry(char *dest, int64_t max,
const int64_t &seqId);
/// decodes file invalidation entry
bool decodeFileInvalidationEntry(char *buf, int16_t size, int64_t ×tamp,
int64_t &seqId);
/// encodes directory invalidation entry
int64_t encodeDirectoryInvalidationEntry(char *buf, int64_t max);
/// decodes directory invalidation entry
bool decodeDirectoryInvalidationEntry(char *buf, int16_t size,
int64_t ×tamp);
private:
/// @return timestamp in microseconds
int64_t timestampInMicroseconds() const;
};
/**
* This class manages reads and writes to receiver side transfer log. This
* class buffers writes to transfer log and starts a writer thread to
* periodically write log entries to the disk. This also has a function to read
* all the entries and correct the log if needed.
*/
class TransferLogManager {
public:
const static int WLOG_VERSION;
enum EntryType {
HEADER, // log header
FILE_CREATION, // File created and space allocated
BLOCK_WRITE, // Complete block fsynced to disk
FILE_INVALIDATION, // Missing file
FILE_RESIZE, // File Resized
DIRECTORY_INVALIDATION, // Directory content is invalid
};
/// 2 bytes for entry size, 1 byte for entry-type, PATH_MAX for file-name, 10
/// bytes for seq-id, 10 bytes for file-size, 10 bytes for timestamp
static const int64_t kMaxEntryLength = 2 + 1 + 10 + PATH_MAX + 2 * 10;
TransferLogManager(const WdtOptions &options, const std::string &rootDir)
: options_(options) {
rootDir_ = rootDir;
if (rootDir_.back() != '/') {
rootDir_.push_back('/');
}
}
/**
* Opens the log for reading and writing. In case of log based
* resumption, starts writer thread. If the log is not present, a new one is
* created
*/
ErrorCode openLog();
/// Start the log writer thread
ErrorCode startThread();
/**
* In case of log based resumption, signals to the writer thread to finish.
* Waits for the writer thread to finish. Closes the transfer log.
*/
void closeLog();
/**
* Verifies sender ip
*
* @param curSenderIp current sender ip
*
* @return whether sender-ip matched log ip
*/
bool verifySenderIp(const std::string &curSenderIp);
/**
* If resumption is based on directory tree, writes log header to the
* transfer log. Else, adds a file creation entry to the log buffer
*/
void writeLogHeader();
/**
* Adds a file creation entry to the log buffer
*
* @param fileName Name of the file
* @param seqId seq-id of the file
* @param fileSize size of the file
*/
void addFileCreationEntry(const std::string &fileName, int64_t seqId,
int64_t fileSize);
/**
* Adds a block write entry to the log buffer
*
* @param seqId seq-id of the file
* @param offset block offset
* @param blockSize size of the block
*/
void addBlockWriteEntry(int64_t seqId, int64_t offset, int64_t blockSize);
/**
* Adds a file resize entry to the log buffer
*
* @param seqId seq-id of the file
* @param fileSize size of the file
*/
void addFileResizeEntry(int64_t seqId, int64_t fileSize);
/**
* Adds an invalidation entry to the log buffer
*
* @param seqId seq-id of the file
*/
void addFileInvalidationEntry(int64_t seqId);
/// Writes an invalidation entry for the directory to the transfer log.
void invalidateDirectory();
/**
* parses the transfer log and prints entries
*
* @return Whether the log is valid or not
*/
bool parseAndPrint();
/// renames .wdt.log to .wdt.log.bug
void renameBuggyLog();
/// Compacts transfer log
void compactLog();
/**
* parses transfer log, does validation and fixes the log in case of partial
* writes from previous transfer. Also parsed info is cached for later use and
* can be accessed through getParsedFileChunksInfo
*
* @param recoveryId recovery-id of the current transfer
* @param config transfer config encoded as int
* @param fileChunksInfo this vector is populated with parsed chunks info
*
* @return status of the parsing
*/
ErrorCode parseAndMatch(const std::string &recoveryId, int64_t config,
std::vector<FileChunksInfo> &fileChunksInfo);
/// @return returns current resumption status
ErrorCode getResumptionStatus();
/**
* Unlinks wdt transfer log
*/
void unlink();
/// destructor
~TransferLogManager();
private:
/// Shutdown the log writer thread
void shutdownThread();
std::string getFullPath(const std::string &relPath);
/**
* entry point for the writer thread. This thread periodically writes buffer
* contents to disk
*/
void threadProcWriteEntriesToDisk();
/// Write 'entries' to disk synchronously
/// return true if entries are written successfully, otherwise false.
bool writeEntriesToDiskNoLock(const std::vector<std::string> &entries);
/// fsync transfer log
void fsyncLog();
/// Check log or directory hasn't been removed under us
/// TODO: consider calling periodically from the thread for early warning
ErrorCode checkLog();
/// closes transfer log
void close();
/**
* Parses the transfer log. Verifies if all the file exists or not(This is
* done to verify whether directory entries were synced to disk before or
* not). Also writes invalidation entries for files with verification failure.
*
* @param recoveryId recovery-id, this is verified against the logged
* recovery-id
* @param config config of the current transfer
* @param parseOnly If true, all parsed entries are logged, and the
* log is not modified or verified
* @param parsedInfo vector to populate with parsed data, only
* populated if parseOnly is false
*
* @return Log parsing status
*/
ErrorCode parseVerifyAndFix(const std::string &recoveryId, int64_t config,
bool parseOnly,
std::vector<FileChunksInfo> &parsedInfo);
LogEncoderDecoder encoderDecoder_;
const WdtOptions &options_;
/// File handler for writing
int fd_{-1};
/// root directory
std::string rootDir_;
/// recovery id
std::string recoveryId_;
/// sender ip
std::string senderIp_;
/// transfer config
int64_t config_;
/// Entry buffer
std::vector<std::string> entries_;
/// Flag to signal end to the writer thread
bool finished_{false};
/// current resumption status
ErrorCode resumptionStatus_;
/// Whether the header is written or not. If it is not written, that means the
/// sender is has not asked for file chunks, which means that sender does not
/// want to resume. If this is false, file-creation, block-write and
/// file-resize entries are not written.
bool headerWritten_{false};
/// Writer thread
std::thread writerThread_;
std::mutex mutex_;
std::condition_variable conditionFinished_;
};
/// class responsible for parsing and fixing transfer log
class LogParser {
public:
LogParser(const WdtOptions &options, LogEncoderDecoder &encoderDecoder,
const std::string &rootDir, const std::string &recoveryId,
int64_t config, bool parseOnly);
ErrorCode parseLog(int fd, std::string &senderIp,
std::vector<FileChunksInfo> &fileChunksInfo);
private:
std::string getFormattedTimestamp(int64_t timestamp);
void clearParsedData();
/**
* Truncates the log
*
* @param fd file descriptor
* @param extraBytes extra bytes at the end of the file
*
* @return If successful, true, else false
*/
bool truncateExtraBytesAtEnd(int fd, int64_t extraBytes);
/**
* writes invalidation entries to the disk.
*
* @param seqIds Invalid seq-ids
*/
bool writeFileInvalidationEntries(int fd, const std::set<int64_t> &seqIds);
ErrorCode processHeaderEntry(char *buf, int64_t max, int64_t size,
std::string &senderIp);
// TODO: switch to ByteRange
ErrorCode processFileCreationEntry(char *buf, int64_t size);
ErrorCode processBlockWriteEntry(char *buf, int64_t size);
ErrorCode processFileResizeEntry(char *buf, int64_t size);
ErrorCode processFileInvalidationEntry(char *buf, int64_t size);
ErrorCode processDirectoryInvalidationEntry(char *buf, int64_t size);
const WdtOptions &options_;
LogEncoderDecoder &encoderDecoder_;
std::string rootDir_;
std::string recoveryId_;
int64_t config_;
bool parseOnly_;
/// whether header is parsed or not
bool headerParsed_{false};
/// seq-id to chunks map
std::map<int64_t, FileChunksInfo> fileInfoMap_;
/// seq-id to file-size map
std::map<int64_t, int64_t> seqIdToSizeMap_;
/// set of invalid seq-ids
std::set<int64_t> invalidSeqIds_;
};
}
}