forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCreator.h
More file actions
185 lines (163 loc) · 5.93 KB
/
Copy pathFileCreator.h
File metadata and controls
185 lines (163 loc) · 5.93 KB
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
/**
* 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/WdtConfig.h>
#include "Protocol.h"
#include "TransferLogManager.h"
#include <glog/logging.h>
#include <mutex>
#include <string>
#include <unordered_set>
#include <folly/SpinLock.h>
#include <map>
#include <condition_variable>
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:
/// rootDir is assumed to exist
FileCreator(const std::string &rootDir, int numThreads,
TransferLogManager &transferLogManager)
: transferLogManager_(transferLogManager) {
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_;
}
/**
* 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 blockDetails block-details
*
* @return file descriptor in case of success, -1 otherwise
*/
int openAndSetSize(BlockDetails const *blockDetails);
/**
* 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 threadIndex index of the calling thread
* @param blockDetails block-details
*
* @return file descriptor in case of success, -1 otherwise
*/
int openForBlocks(int threadIndex, 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:
/**
* 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.
*
* @param relPath path relative to root dir
*
* @return file descriptor or -1 on error
*/
int createFile(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 fd file descriptor
* @param fileSize size of the file
*
* @return true for suzzess, false otherwise
*/
bool setFileSize(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 threadIndex index of the calling thread
* @param blockDetails block-details
*
* @return file descriptor or -1 on error
*/
int openForFirstBlock(int threadIndex, 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 '/'
* @parm 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();
}
/// 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_;
};
}
}