forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileCreator.cpp
304 lines (283 loc) · 8.91 KB
/
FileCreator.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
/**
* 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 "FileCreator.h"
#include "ErrorCodes.h"
#include "Reporting.h"
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <folly/Conv.h>
namespace facebook {
namespace wdt {
bool FileCreator::setFileSize(int fd, int64_t fileSize) {
struct stat fileStat;
if (fstat(fd, &fileStat) != 0) {
PLOG(ERROR) << "fstat() failed for " << fd;
return false;
}
auto &options = WdtOptions::get();
if (fileStat.st_size > fileSize) {
// existing file is larger than required
int64_t sizeToTruncate = (options.shouldPreallocateFiles() ? fileSize : 0);
if (ftruncate(fd, sizeToTruncate) != 0) {
PLOG(ERROR) << "ftruncate() failed for " << fd << " " << sizeToTruncate;
return false;
}
}
if (fileSize == 0) {
return true;
}
if (!options.shouldPreallocateFiles()) {
// pre-allocation is disabled
return true;
}
#ifdef HAS_POSIX_FALLOCATE
int status = posix_fallocate(fd, 0, fileSize);
if (status != 0) {
LOG(ERROR) << "fallocate() failed " << strerrorStr(status);
return false;
}
return true;
#else
WDT_CHECK(false) << "Should never reach here";
#endif
}
int FileCreator::openAndSetSize(BlockDetails const *blockDetails) {
const auto &options = WdtOptions::get();
int fd;
const bool doCreate = blockDetails->allocationStatus == NOT_EXISTS;
const bool isTooLarge = (blockDetails->allocationStatus == EXISTS_TOO_LARGE);
if (doCreate) {
fd = createFile(blockDetails->fileName);
} else {
fd = openExistingFile(blockDetails->fileName);
}
if (fd < 0) {
return -1;
}
if (blockDetails->allocationStatus == EXISTS_CORRECT_SIZE) {
return fd;
}
if (!setFileSize(fd, blockDetails->fileSize)) {
close(fd);
return -1;
}
if (options.isLogBasedResumption()) {
if (isTooLarge) {
LOG(WARNING) << "File size smaller in the sender side "
<< blockDetails->fileName
<< ", marking previous transferred chunks as invalid";
transferLogManager_.addInvalidationEntry(blockDetails->prevSeqId);
}
if (isTooLarge || doCreate) {
transferLogManager_.addFileCreationEntry(
blockDetails->fileName, blockDetails->seqId, blockDetails->fileSize);
} else {
WDT_CHECK_EQ(EXISTS_TOO_SMALL, blockDetails->allocationStatus);
transferLogManager_.addFileResizeEntry(blockDetails->seqId,
blockDetails->fileSize);
}
}
return fd;
}
int FileCreator::openForFirstBlock(int threadIndex,
BlockDetails const *blockDetails) {
int fd = openAndSetSize(blockDetails);
{
folly::SpinLockGuard guard(lock_);
auto it = fileStatusMap_.find(blockDetails->seqId);
WDT_CHECK(it != fileStatusMap_.end());
it->second = fd >= 0 ? ALLOCATED : FAILED;
}
std::unique_lock<std::mutex> waitLock(allocationMutex_);
threadConditionVariables_[threadIndex].notify_all();
return fd;
}
bool FileCreator::waitForAllocationFinish(int allocatingThreadIndex,
int64_t seqId) {
std::unique_lock<std::mutex> waitLock(allocationMutex_);
while (true) {
{
folly::SpinLockGuard guard(lock_);
auto it = fileStatusMap_.find(seqId);
WDT_CHECK(it != fileStatusMap_.end());
if (it->second == ALLOCATED) {
return true;
}
if (it->second == FAILED) {
return false;
}
}
threadConditionVariables_[allocatingThreadIndex].wait(waitLock);
}
}
int FileCreator::openForBlocks(int threadIndex,
BlockDetails const *blockDetails) {
lock_.lock();
auto it = fileStatusMap_.find(blockDetails->seqId);
if (blockDetails->allocationStatus == EXISTS_CORRECT_SIZE &&
it == fileStatusMap_.end()) {
it = fileStatusMap_.insert(std::make_pair(blockDetails->seqId,
FileCreator::ALLOCATED))
.first;
}
if (it == fileStatusMap_.end()) {
// allocation has not started for this file
fileStatusMap_.insert(std::make_pair(blockDetails->seqId, threadIndex));
lock_.unlock();
return openForFirstBlock(threadIndex, blockDetails);
}
auto status = it->second;
lock_.unlock();
if (status == FAILED) {
// allocation failed previously
return -1;
}
if (status != ALLOCATED) {
// allocation in progress
if (!waitForAllocationFinish(it->second, blockDetails->seqId)) {
return -1;
}
}
return openExistingFile(blockDetails->fileName);
}
using std::string;
int FileCreator::openExistingFile(const string &relPathStr) {
// This should have been validated earlier and errored out
// instead of crashing here
WDT_CHECK(!relPathStr.empty());
WDT_CHECK(relPathStr[0] != '/');
WDT_CHECK(relPathStr.back() != '/');
string path(rootDir_);
path.append(relPathStr);
int openFlags = O_WRONLY;
START_PERF_TIMER
int res = open(path.c_str(), openFlags, 0644);
RECORD_PERF_RESULT(PerfStatReport::FILE_OPEN)
if (res < 0) {
PLOG(ERROR) << "failed opening file " << path;
return -1;
}
VLOG(1) << "successfully opened file " << path;
return res;
}
int FileCreator::createFile(const string &relPathStr) {
CHECK(!relPathStr.empty());
CHECK(relPathStr[0] != '/');
CHECK(relPathStr.back() != '/');
std::string path(rootDir_);
path.append(relPathStr);
int p = relPathStr.size();
while (p && relPathStr[p - 1] != '/') {
--p;
}
std::string dir;
if (p) {
dir.assign(relPathStr.data(), p);
START_PERF_TIMER
const bool dirSuccess1 = createDirRecursively(dir);
RECORD_PERF_RESULT(PerfStatReport::DIRECTORY_CREATE)
if (!dirSuccess1) {
// retry with force
LOG(ERROR) << "failed to create dir " << dir << " recursively, "
<< "trying to force directory creation";
START_PERF_TIMER
const bool dirSuccess2 = createDirRecursively(dir, true /* force */);
RECORD_PERF_RESULT(PerfStatReport::DIRECTORY_CREATE)
if (!dirSuccess2) {
LOG(ERROR) << "failed to create dir " << dir << " recursively";
return -1;
}
}
}
int openFlags = O_CREAT | O_WRONLY;
auto &options = WdtOptions::get();
// When doing download resumption we sometime open files that do already
// exist and we need to overwrite them anyway (files which have been
// discarded from the log for some reason)
if (options.overwrite || options.enable_download_resumption) {
// Make sure file size resumption will not get messed up if we
// expect to create this file
openFlags |= O_TRUNC;
} else {
// Make sure open will fail if we don't allow overwriting and
// the file happens to already exist
openFlags |= O_EXCL;
}
START_PERF_TIMER
int res = open(path.c_str(), openFlags, 0644);
RECORD_PERF_RESULT(PerfStatReport::FILE_OPEN)
if (res < 0) {
if (dir.empty()) {
PLOG(ERROR) << "failed creating file " << path;
return -1;
}
PLOG(ERROR) << "failed creating file " << path << ", trying to "
<< "force directory creation";
{
START_PERF_TIMER
const bool dirSuccess = createDirRecursively(dir, true /* force */);
RECORD_PERF_RESULT(PerfStatReport::DIRECTORY_CREATE)
if (!dirSuccess) {
LOG(ERROR) << "failed to create dir " << dir << " recursively";
return -1;
}
}
START_PERF_TIMER
res = open(path.c_str(), openFlags, 0644);
RECORD_PERF_RESULT(PerfStatReport::FILE_OPEN)
if (res < 0) {
PLOG(ERROR) << "failed creating file " << path;
return -1;
}
}
VLOG(1) << "successfully created file " << path;
return res;
}
bool FileCreator::createDirRecursively(const std::string dir, bool force) {
if (!force && dirCreated(dir)) {
return true;
}
WDT_CHECK(dir.back() == '/');
int64_t lastIndex = dir.size() - 1;
while (lastIndex > 0 && dir[lastIndex - 1] != '/') {
lastIndex--;
}
if (lastIndex > 0) {
if (!createDirRecursively(dir.substr(0, lastIndex), force)) {
return false;
}
}
std::string fullDirPath;
folly::toAppend(rootDir_, dir, &fullDirPath);
int code = mkdir(fullDirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (code != 0 && errno != EEXIST && errno != EISDIR) {
PLOG(ERROR) << "failed to make directory " << fullDirPath;
return false;
} else if (code != 0) {
LOG(INFO) << "dir already exists " << fullDirPath;
} else {
LOG(INFO) << "made dir " << fullDirPath;
}
{
std::lock_guard<std::mutex> lock(mutex_);
createdDirs_.insert(dir);
}
return true;
}
/* static */
void FileCreator::addTrailingSlash(string &path) {
if (path.back() != '/') {
path.push_back('/');
VLOG(1) << "Added missing trailing / to " << path;
}
}
}
}