/Users/deen/code/yugabyte-db/src/yb/fs/fs_manager.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #ifndef YB_FS_FS_MANAGER_H |
34 | | #define YB_FS_FS_MANAGER_H |
35 | | |
36 | | #include <iosfwd> |
37 | | #include <memory> |
38 | | #include <set> |
39 | | #include <string> |
40 | | #include <vector> |
41 | | |
42 | | #include <gflags/gflags_declare.h> |
43 | | #include <gtest/gtest_prod.h> |
44 | | |
45 | | #include "yb/gutil/ref_counted.h" |
46 | | #include "yb/util/env.h" |
47 | | #include "yb/util/metrics.h" |
48 | | #include "yb/util/path_util.h" |
49 | | #include "yb/util/strongly_typed_bool.h" |
50 | | |
51 | | DECLARE_bool(enable_data_block_fsync); |
52 | | |
53 | | namespace google { |
54 | | namespace protobuf { |
55 | | class Message; |
56 | | } // namespace protobuf |
57 | | } // namespace google |
58 | | |
59 | | namespace yb { |
60 | | |
61 | | class MemTracker; |
62 | | class MetricEntity; |
63 | | |
64 | | namespace itest { |
65 | | class ExternalMiniClusterFsInspector; |
66 | | } |
67 | | |
68 | | class InstanceMetadataPB; |
69 | | |
70 | | YB_STRONGLY_TYPED_BOOL(ShouldDeleteLogs); |
71 | | |
72 | | struct FsManagerOpts { |
73 | | FsManagerOpts(); |
74 | | ~FsManagerOpts(); |
75 | | |
76 | | FsManagerOpts(const FsManagerOpts&); |
77 | | FsManagerOpts& operator=(const FsManagerOpts&); |
78 | | |
79 | | // The entity under which all metrics should be grouped. If NULL, metrics |
80 | | // will not be produced. |
81 | | // |
82 | | // Defaults to NULL. |
83 | | scoped_refptr<MetricEntity> metric_entity; |
84 | | |
85 | | // The memory tracker under which all new memory trackers will be parented. |
86 | | // If NULL, new memory trackers will be parented to the root tracker. |
87 | | std::shared_ptr<MemTracker> parent_mem_tracker; |
88 | | |
89 | | // The paths where WALs will be stored. Cannot be empty. |
90 | | std::vector<std::string> wal_paths; |
91 | | |
92 | | // The paths where data blocks will be stored. Cannot be empty. |
93 | | std::vector<std::string> data_paths; |
94 | | |
95 | | // Whether or not read-write operations should be allowed. Defaults to false. |
96 | | bool read_only; |
97 | | |
98 | | // This field is to be used as a path component for all the fs roots. For now, we expect it to be |
99 | | // either master or tserver. |
100 | | std::string server_type; |
101 | | }; |
102 | | |
103 | | // FsManager provides helpers to read data and metadata files, |
104 | | // and it's responsible for abstracting the file-system layout. |
105 | | // |
106 | | // The user should not be aware of where files are placed, |
107 | | // but instead should interact with the storage in terms of "open the file xyz" |
108 | | // or "write a new schema metadata file for table kwz". |
109 | | // |
110 | | // The current top-level dir layout is <yb.root.dir>/yb-data/<server>/. Subdirs under it are: |
111 | | // logs/ |
112 | | // instance |
113 | | // wals/<table>/<tablet> |
114 | | // tablet-meta/<tablet> |
115 | | // data/rocksdb/<table>/<tablet>/ |
116 | | // consensus-meta/<tablet> |
117 | | |
118 | | class FsManager { |
119 | | public: |
120 | | static const char *kWalDirName; |
121 | | static const char *kWalFileNamePrefix; |
122 | | static const char *kWalsRecoveryDirSuffix; |
123 | | static const char *kRocksDBDirName; |
124 | | static const char *kDataDirName; |
125 | | |
126 | | // Only for unit tests. |
127 | | FsManager(Env* env, const std::string& root_path, const std::string& server_type); |
128 | | |
129 | | FsManager(Env* env, const FsManagerOpts& opts); |
130 | | ~FsManager(); |
131 | | |
132 | | // Initialize and load the basic filesystem metadata. |
133 | | // If the file system has not been initialized, returns NotFound. |
134 | | // In that case, CreateInitialFileSystemLayout may be used to initialize |
135 | | // the on-disk structures. |
136 | | CHECKED_STATUS CheckAndOpenFileSystemRoots(); |
137 | | |
138 | | // |
139 | | // Returns an error if the file system is already initialized. |
140 | | CHECKED_STATUS CreateInitialFileSystemLayout(bool delete_fs_if_lock_found = false); |
141 | | |
142 | | // Deletes the yb-data directory contents for data/wal. "logs" subdirectory deletion is skipped |
143 | | // when 'also_delete_logs' is set to false. |
144 | | // Needed for a master shell process to be stoppable and restartable correctly in shell mode. |
145 | | CHECKED_STATUS DeleteFileSystemLayout( |
146 | | ShouldDeleteLogs also_delete_logs = ShouldDeleteLogs::kFalse); |
147 | | |
148 | | // Check if a lock file is present. |
149 | | bool HasAnyLockFiles(); |
150 | | // Delete the lock files. Used once the caller deems fs creation was succcessful. |
151 | | CHECKED_STATUS DeleteLockFiles(); |
152 | | |
153 | | void DumpFileSystemTree(std::ostream& out); |
154 | | |
155 | | // Return the UUID persisted in the local filesystem. If Open() |
156 | | // has not been called, this will crash. |
157 | | const std::string& uuid() const; |
158 | | |
159 | | // ========================================================================== |
160 | | // on-disk path |
161 | | // ========================================================================== |
162 | | std::set<std::string> GetFsRootDirs() const; |
163 | | |
164 | | std::vector<std::string> GetDataRootDirs() const; |
165 | | |
166 | | std::vector<std::string> GetWalRootDirs() const; |
167 | | |
168 | | // Used for tests only. If GetWalRootDirs returns an empty vector, we will crash the process. |
169 | | std::string GetFirstTabletWalDirOrDie(const std::string& table_id, |
170 | | const std::string& tablet_id) const; |
171 | | |
172 | | static std::string GetTabletWalRecoveryDir(const std::string& tablet_wal_path); |
173 | | |
174 | | static std::string GetWalSegmentFileName(uint64_t sequence_number); |
175 | | |
176 | | static bool IsWalSegmentFileName(const std::string& file_name); |
177 | | |
178 | | static std::string GetWalSegmentFilePath( |
179 | 164k | const std::string& wal_path, uint64_t sequence_number) { |
180 | 164k | return JoinPathSegments(wal_path, GetWalSegmentFileName(sequence_number)); |
181 | 164k | } |
182 | | |
183 | | // Return the directory where Raft group superblocks should be stored. |
184 | | std::string GetRaftGroupMetadataDir() const; |
185 | | static std::string GetRaftGroupMetadataDir(const std::string& data_dir); |
186 | | |
187 | | // Return the path for a specific Raft group's superblock. |
188 | | std::string GetRaftGroupMetadataPath(const std::string& tablet_id) const; |
189 | | |
190 | | // List the tablet IDs in the metadata directory. |
191 | | CHECKED_STATUS ListTabletIds(std::vector<std::string>* tablet_ids); |
192 | | |
193 | | // Return the path where InstanceMetadataPB is stored. |
194 | | std::string GetInstanceMetadataPath(const std::string& root) const; |
195 | | |
196 | | // Return the path where the fs lock file is stored. |
197 | | std::string GetFsLockFilePath(const std::string& root) const; |
198 | | |
199 | | // Return the directory where the consensus metadata is stored. |
200 | | std::string GetConsensusMetadataDir() const; |
201 | | static std::string GetConsensusMetadataDir(const std::string& data_dir); |
202 | | |
203 | | // Return the path where ConsensusMetadataPB is stored. |
204 | 1.72M | std::string GetConsensusMetadataPath(const std::string& tablet_id) const { |
205 | 1.72M | return JoinPathSegments(GetConsensusMetadataDir(), tablet_id); |
206 | 1.72M | } |
207 | | |
208 | 4.11M | Env *env() { return env_; } |
209 | | |
210 | 0 | bool read_only() const { |
211 | 0 | return read_only_; |
212 | 0 | } |
213 | | |
214 | | // ========================================================================== |
215 | | // file-system helpers |
216 | | // ========================================================================== |
217 | 79.7k | bool Exists(const std::string& path) const { |
218 | 79.7k | return env_->FileExists(path); |
219 | 79.7k | } |
220 | | |
221 | | CHECKED_STATUS ListDir(const std::string& path, std::vector<std::string> *objects) const; |
222 | | |
223 | | Result<std::vector<std::string>> ListDir(const std::string& path) const; |
224 | | |
225 | | CHECKED_STATUS CreateDirIfMissing(const std::string& path, bool* created = NULL); |
226 | | |
227 | | CHECKED_STATUS CreateDirIfMissingAndSync(const std::string& path, bool* created = NULL); |
228 | | |
229 | | private: |
230 | | FRIEND_TEST(FsManagerTestBase, TestDuplicatePaths); |
231 | | |
232 | | // Initializes, sanitizes, and canonicalizes the filesystem roots. |
233 | | CHECKED_STATUS Init(); |
234 | | |
235 | | // Creates filesystem roots, writing new on-disk instances using 'metadata'. |
236 | | CHECKED_STATUS CreateFileSystemRoots(bool create_metadata_dir, |
237 | | const InstanceMetadataPB& metadata, |
238 | | bool create_lock = false); |
239 | | |
240 | | std::set<std::string> GetAncillaryDirs(bool add_metadata_dirs) const; |
241 | | |
242 | | // Create a new InstanceMetadataPB. |
243 | | void CreateInstanceMetadata(InstanceMetadataPB* metadata); |
244 | | |
245 | | // Save a InstanceMetadataPB to the filesystem. |
246 | | // Does not mutate the current state of the fsmanager. |
247 | | CHECKED_STATUS WriteInstanceMetadata( |
248 | | const InstanceMetadataPB& metadata, |
249 | | const std::string& path); |
250 | | |
251 | | // Checks if 'path' is an empty directory. |
252 | | // |
253 | | // Returns an error if it's not a directory. Otherwise, sets 'is_empty' |
254 | | // accordingly. |
255 | | CHECKED_STATUS IsDirectoryEmpty(const std::string& path, bool* is_empty); |
256 | | |
257 | | // Checks write to temporary file on root. |
258 | | CHECKED_STATUS CheckWrite(const std::string& path); |
259 | | |
260 | | void CreateAndSetFaultDriveMetric(const std::string& path); |
261 | | |
262 | | // ========================================================================== |
263 | | // file-system helpers |
264 | | // ========================================================================== |
265 | | void DumpFileSystemTree(std::ostream& out, |
266 | | const std::string& prefix, |
267 | | const std::string& path, |
268 | | const std::vector<std::string>& objects); |
269 | | |
270 | | Env *env_; |
271 | | |
272 | | // If false, operations that mutate on-disk state are prohibited. |
273 | | const bool read_only_; |
274 | | |
275 | | // These roots are the constructor input verbatim. None of them are used |
276 | | // as-is; they are first canonicalized during Init(). |
277 | | const std::vector<std::string> wal_fs_roots_; |
278 | | const std::vector<std::string> data_fs_roots_; |
279 | | const std::string server_type_; |
280 | | |
281 | | scoped_refptr<MetricEntity> metric_entity_; |
282 | | |
283 | | std::shared_ptr<MemTracker> parent_mem_tracker_; |
284 | | |
285 | | // Canonicalized forms of 'wal_fs_roots_ and 'data_fs_roots_'. Constructed |
286 | | // during Init(). |
287 | | // |
288 | | // - The first data root is used as the metadata root. |
289 | | // - Common roots in the collections have been deduplicated. |
290 | | std::set<std::string> canonicalized_wal_fs_roots_; |
291 | | std::string canonicalized_metadata_fs_root_; |
292 | | std::set<std::string> canonicalized_data_fs_roots_; |
293 | | std::set<std::string> canonicalized_all_fs_roots_; |
294 | | |
295 | | std::map<std::string, scoped_refptr<Counter>> counters_; |
296 | | |
297 | | std::unique_ptr<InstanceMetadataPB> metadata_; |
298 | | |
299 | | bool initted_; |
300 | | |
301 | | DISALLOW_COPY_AND_ASSIGN(FsManager); |
302 | | }; |
303 | | |
304 | | } // namespace yb |
305 | | |
306 | | #endif // YB_FS_FS_MANAGER_H |