YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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/path_util.h"
48
#include "yb/util/strongly_typed_bool.h"
49
50
DECLARE_bool(enable_data_block_fsync);
51
52
namespace google {
53
namespace protobuf {
54
class Message;
55
} // namespace protobuf
56
} // namespace google
57
58
namespace yb {
59
60
class MemTracker;
61
class MetricEntity;
62
63
namespace itest {
64
class ExternalMiniClusterFsInspector;
65
}
66
67
class InstanceMetadataPB;
68
69
YB_STRONGLY_TYPED_BOOL(ShouldDeleteLogs);
70
71
struct FsManagerOpts {
72
  FsManagerOpts();
73
  ~FsManagerOpts();
74
75
  FsManagerOpts(const FsManagerOpts&);
76
  FsManagerOpts& operator=(const FsManagerOpts&);
77
78
  // The entity under which all metrics should be grouped. If NULL, metrics
79
  // will not be produced.
80
  //
81
  // Defaults to NULL.
82
  scoped_refptr<MetricEntity> metric_entity;
83
84
  // The memory tracker under which all new memory trackers will be parented.
85
  // If NULL, new memory trackers will be parented to the root tracker.
86
  std::shared_ptr<MemTracker> parent_mem_tracker;
87
88
  // The paths where WALs will be stored. Cannot be empty.
89
  std::vector<std::string> wal_paths;
90
91
  // The paths where data blocks will be stored. Cannot be empty.
92
  std::vector<std::string> data_paths;
93
94
  // Whether or not read-write operations should be allowed. Defaults to false.
95
  bool read_only;
96
97
  // This field is to be used as a path component for all the fs roots. For now, we expect it to be
98
  // either master or tserver.
99
  std::string server_type;
100
};
101
102
// FsManager provides helpers to read data and metadata files,
103
// and it's responsible for abstracting the file-system layout.
104
//
105
// The user should not be aware of where files are placed,
106
// but instead should interact with the storage in terms of "open the file xyz"
107
// or "write a new schema metadata file for table kwz".
108
//
109
// The current top-level dir layout is <yb.root.dir>/yb-data/<server>/. Subdirs under it are:
110
//     logs/
111
//     instance
112
//     wals/<table>/<tablet>
113
//     tablet-meta/<tablet>
114
//     data/rocksdb/<table>/<tablet>/
115
//     consensus-meta/<tablet>
116
117
class FsManager {
118
 public:
119
  static const char *kWalDirName;
120
  static const char *kWalFileNamePrefix;
121
  static const char *kWalsRecoveryDirSuffix;
122
  static const char *kRocksDBDirName;
123
  static const char *kDataDirName;
124
125
  // Only for unit tests.
126
  FsManager(Env* env, const std::string& root_path, const std::string& server_type);
127
128
  FsManager(Env* env, const FsManagerOpts& opts);
129
  ~FsManager();
130
131
  // Initialize and load the basic filesystem metadata.
132
  // If the file system has not been initialized, returns NotFound.
133
  // In that case, CreateInitialFileSystemLayout may be used to initialize
134
  // the on-disk structures.
135
  CHECKED_STATUS Open();
136
137
  //
138
  // Returns an error if the file system is already initialized.
139
  CHECKED_STATUS CreateInitialFileSystemLayout(bool delete_fs_if_lock_found = false);
140
141
  // Deletes the yb-data directory contents for data/wal. "logs" subdirectory deletion is skipped
142
  // when 'also_delete_logs' is set to false.
143
  // Needed for a master shell process to be stoppable and restartable correctly in shell mode.
144
  CHECKED_STATUS DeleteFileSystemLayout(
145
      ShouldDeleteLogs also_delete_logs = ShouldDeleteLogs::kFalse);
146
147
  // Check if a lock file is present.
148
  bool HasAnyLockFiles();
149
  // Delete the lock files. Used once the caller deems fs creation was succcessful.
150
  CHECKED_STATUS DeleteLockFiles();
151
152
  void DumpFileSystemTree(std::ostream& out);
153
154
  // Return the UUID persisted in the local filesystem. If Open()
155
  // has not been called, this will crash.
156
  const std::string& uuid() const;
157
158
  // ==========================================================================
159
  //  on-disk path
160
  // ==========================================================================
161
  std::set<std::string> GetFsRootDirs() const;
162
163
  std::vector<std::string> GetDataRootDirs() const;
164
165
  std::vector<std::string> GetWalRootDirs() const;
166
167
  // Used for tests only. If GetWalRootDirs returns an empty vector, we will crash the process.
168
  std::string GetFirstTabletWalDirOrDie(const std::string& table_id,
169
                                        const std::string& tablet_id) const;
170
171
  static std::string GetTabletWalRecoveryDir(const std::string& tablet_wal_path);
172
173
  static std::string GetWalSegmentFileName(uint64_t sequence_number);
174
175
  static bool IsWalSegmentFileName(const std::string& file_name);
176
177
  static std::string GetWalSegmentFilePath(
178
99.3k
      const std::string& wal_path, uint64_t sequence_number) {
179
99.3k
    return JoinPathSegments(wal_path, GetWalSegmentFileName(sequence_number));
180
99.3k
  }
181
182
  // Return the directory where Raft group superblocks should be stored.
183
  std::string GetRaftGroupMetadataDir() const;
184
  static std::string GetRaftGroupMetadataDir(const std::string& data_dir);
185
186
  // Return the path for a specific Raft group's superblock.
187
  std::string GetRaftGroupMetadataPath(const std::string& tablet_id) const;
188
189
  // List the tablet IDs in the metadata directory.
190
  CHECKED_STATUS ListTabletIds(std::vector<std::string>* tablet_ids);
191
192
  // Return the path where InstanceMetadataPB is stored.
193
  std::string GetInstanceMetadataPath(const std::string& root) const;
194
195
  // Return the path where the fs lock file is stored.
196
  std::string GetFsLockFilePath(const std::string& root) const;
197
198
  // Return the directory where the consensus metadata is stored.
199
  std::string GetConsensusMetadataDir() const;
200
  static std::string GetConsensusMetadataDir(const std::string& data_dir);
201
202
  // Return the path where ConsensusMetadataPB is stored.
203
1.00M
  std::string GetConsensusMetadataPath(const std::string& tablet_id) const {
204
1.00M
    return JoinPathSegments(GetConsensusMetadataDir(), tablet_id);
205
1.00M
  }
206
207
2.32M
  Env *env() { return env_; }
208
209
0
  bool read_only() const {
210
0
    return read_only_;
211
0
  }
212
213
  // ==========================================================================
214
  //  file-system helpers
215
  // ==========================================================================
216
53.4k
  bool Exists(const std::string& path) const {
217
53.4k
    return env_->FileExists(path);
218
53.4k
  }
219
220
  CHECKED_STATUS ListDir(const std::string& path, std::vector<std::string> *objects) const;
221
222
  Result<std::vector<std::string>> ListDir(const std::string& path) const;
223
224
  CHECKED_STATUS CreateDirIfMissing(const std::string& path, bool* created = NULL);
225
226
  CHECKED_STATUS CreateDirIfMissingAndSync(const std::string& path, bool* created = NULL);
227
228
 private:
229
  FRIEND_TEST(FsManagerTestBase, TestDuplicatePaths);
230
231
  // Initializes, sanitizes, and canonicalizes the filesystem roots.
232
  CHECKED_STATUS Init();
233
234
  // Creates filesystem roots from 'roots', writing new on-disk
235
  // instances using 'metadata'.
236
  CHECKED_STATUS CreateFileSystemRoots(const std::set<std::string>& roots,
237
                                       const std::set<std::string>& ancillary_dirs,
238
                                       const InstanceMetadataPB& metadata,
239
                                       bool create_lock = false);
240
241
  std::set<std::string> GetAncillaryDirs(bool add_metadata_dirs) const;
242
243
  // Create a new InstanceMetadataPB.
244
  void CreateInstanceMetadata(InstanceMetadataPB* metadata);
245
246
  // Save a InstanceMetadataPB to the filesystem.
247
  // Does not mutate the current state of the fsmanager.
248
  CHECKED_STATUS WriteInstanceMetadata(
249
      const InstanceMetadataPB& metadata,
250
      const std::string& path);
251
252
  // Checks if 'path' is an empty directory.
253
  //
254
  // Returns an error if it's not a directory. Otherwise, sets 'is_empty'
255
  // accordingly.
256
  CHECKED_STATUS IsDirectoryEmpty(const std::string& path, bool* is_empty);
257
258
  // ==========================================================================
259
  //  file-system helpers
260
  // ==========================================================================
261
  void DumpFileSystemTree(std::ostream& out,
262
                          const std::string& prefix,
263
                          const std::string& path,
264
                          const std::vector<std::string>& objects);
265
266
  Env *env_;
267
268
  // If false, operations that mutate on-disk state are prohibited.
269
  const bool read_only_;
270
271
  // These roots are the constructor input verbatim. None of them are used
272
  // as-is; they are first canonicalized during Init().
273
  const std::vector<std::string> wal_fs_roots_;
274
  const std::vector<std::string> data_fs_roots_;
275
  const std::string server_type_;
276
277
  scoped_refptr<MetricEntity> metric_entity_;
278
279
  std::shared_ptr<MemTracker> parent_mem_tracker_;
280
281
  // Canonicalized forms of 'wal_fs_roots_ and 'data_fs_roots_'. Constructed
282
  // during Init().
283
  //
284
  // - The first data root is used as the metadata root.
285
  // - Common roots in the collections have been deduplicated.
286
  std::set<std::string> canonicalized_wal_fs_roots_;
287
  std::string canonicalized_metadata_fs_root_;
288
  std::set<std::string> canonicalized_data_fs_roots_;
289
  std::set<std::string> canonicalized_all_fs_roots_;
290
291
  std::unique_ptr<InstanceMetadataPB> metadata_;
292
293
  bool initted_;
294
295
  DISALLOW_COPY_AND_ASSIGN(FsManager);
296
};
297
298
} // namespace yb
299
300
#endif  // YB_FS_FS_MANAGER_H