forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileCreator.h
199 lines (175 loc) · 6.54 KB
/
FileCreator.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
/**
* 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/WdtConfig.h>
#include <wdt/util/CommonImpl.h>
#include <wdt/util/TransferLogManager.h>
#include <folly/SpinLock.h>
#include <glog/logging.h>
#include <condition_variable>
#include <map>
#include <mutex>
#include <string>
#include <unordered_set>
namespace facebook {
namespace wdt {
/**
* Utility class for creating/opening files for writing while
* creating subdirs automatically and only once in case multiple
* files are created relative to the rootDir directory.
*
* Path to rootDir doesn't need to have a trailing slash
* (it's added for you if missing)
*
* This class is thread-safe. (yeah!)
*/
class FileCreator {
public:
FileCreator(const std::string &rootDir, int numThreads,
TransferLogManager &transferLogManager, bool skipWrites)
: transferLogManager_(transferLogManager), skipWrites_(skipWrites) {
CHECK(!rootDir.empty());
// For creating root directory, we are using createDirRecursively.
// Since, this function adds rootDir to the path provided to it,
// we are setting the value of rootDir after the function call.
// So, createDirRecursively uses empty rootDir for this call.
std::string rootDirPath = rootDir;
addTrailingSlash(rootDirPath);
createDirRecursively(rootDirPath, false);
resetDirCache();
rootDir_ = rootDirPath;
threadConditionVariables_ = new std::condition_variable[numThreads];
}
virtual ~FileCreator() {
delete[] threadConditionVariables_;
}
/**
* This is used to open the file in block mode. If the current thread is the
* first one to try to open the file, then it allocates space using
* openAndSetSize function. Other threads wait for the first thread to finish
* and opens the file without setting size.
*
* @param threadCtx context of the calling thread
* @param blockDetails block-details
*
* @return file descriptor in case of success, -1 otherwise
*/
int openForBlocks(ThreadCtx &threadCtx, BlockDetails const *blockDetails);
/// reset internal directory cache
void resetDirCache() {
std::lock_guard<std::mutex> lock(mutex_);
createdDirs_.clear();
}
/// clears allocation status map, called after end of each session
void clearAllocationMap() {
folly::SpinLockGuard guard(lock_);
fileStatusMap_.clear();
}
private:
/**
* Opens the file and sets its size. If the existing file size is greater than
* required size, the file is truncated using ftruncate. Space is
* allocated using posix_fallocate.
*
* @param threadCtx context of the calling thread
* @param blockDetails block-details
*
* @return file descriptor in case of success, -1 otherwise
*/
int openAndSetSize(ThreadCtx &threadCtx, BlockDetails const *blockDetails);
/**
* Create a file and open for writing, recursively create subdirs.
* Subdirs are only created once due to createdDirs_ cache, but
* if an open fails where we assumed the directory already exists
* based on cache, we try creating the dir and open again before
* failing. Will not overwrite existing files unless overwrite option
* is set.
*
* @param threadCtx context of the calling thread
* @param relPath path relative to root dir
*
* @return file descriptor or -1 on error
*/
int createFile(ThreadCtx &threadCtx, const std::string &relPath);
/**
* Open existing file
*/
int openExistingFile(ThreadCtx &threadCtx, const std::string &relPath);
/**
* sets the size of the file. If the size is greater then the
* file is truncated using ftruncate. Space is allocated using fallocate.
*
* @param threadCtx context of the calling thread
* @param fd file descriptor
* @param fileSize size of the file
*
* @return true for success, false otherwise
*/
bool setFileSize(ThreadCtx &threadCtx, int fd, int64_t fileSize);
/**
* opens the file and sets it size. Called only for the first block to request
* opening a multi-block file. Sets the allocation status in fileStatusMap_
* and notifies other waiting thread.
*
* @param threadCtx context of the calling thread
* @param blockDetails block-details
*
* @return file descriptor or -1 on error
*/
int openForFirstBlock(ThreadCtx &threadCtx, BlockDetails const *blockDetails);
/// waits for allocation of a file to finish
bool waitForAllocationFinish(int allocatingThreadIndex, int64_t seqId);
/// appends a trailing / if not already there to path
static void addTrailingSlash(std::string &path);
/**
* Create directory recursively, populating cache. Cache is only
* used if force is false (but it's still populated in any case).
*
* @param dir dir to create recursively, should end with
* '/' and not start with '/'
* @param force whether to force trying to create/skip
* checking the cache
*
* @return true iff successful
*/
bool createDirRecursively(const std::string dir, bool force = false);
/// Check whether directory has been created/is in cache
bool dirCreated(const std::string &dir) {
std::lock_guard<std::mutex> lock(mutex_);
return createdDirs_.find(dir) != createdDirs_.end();
}
/// returns full path of a file
std::string getFullPath(const std::string &relPath);
/// root directory
std::string rootDir_;
/// directories created so far, relative to root
std::unordered_set<std::string> createdDirs_;
/// protects createdDirs_
std::mutex mutex_;
const int ALLOCATED{-1};
const int FAILED{-2};
/// map from file sequence id to allocation status. There are four possible
/// allocation status. NOT STARTED(no entry in the map), ALLOCATED(-1),
/// FAILED(-2) and IN_PROGRESS(map value is the index of the allocating
/// thread)
std::map<int64_t, int> fileStatusMap_;
/// transfer log manger used by receiver
TransferLogManager &transferLogManager_;
/// mutex to coordinate waiting among threads
std::mutex allocationMutex_;
/// array of condition_variables for different threads
std::condition_variable *threadConditionVariables_;
/// lock protecting fileStatusMap_
folly::SpinLock lock_;
// Set to prevent creating files
bool skipWrites_;
};
}
}