/src/rocksdb/file/file_util.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root directory). |
5 | | // |
6 | | #pragma once |
7 | | #include <string> |
8 | | |
9 | | #include "file/filename.h" |
10 | | #include "options/db_options.h" |
11 | | #include "rocksdb/env.h" |
12 | | #include "rocksdb/file_system.h" |
13 | | #include "rocksdb/sst_file_writer.h" |
14 | | #include "rocksdb/statistics.h" |
15 | | #include "rocksdb/status.h" |
16 | | #include "rocksdb/system_clock.h" |
17 | | #include "rocksdb/types.h" |
18 | | #include "trace_replay/io_tracer.h" |
19 | | |
20 | | namespace ROCKSDB_NAMESPACE { |
21 | | // use_fsync maps to options.use_fsync, which determines the way that |
22 | | // the file is synced after copying. |
23 | | IOStatus CopyFile(FileSystem* fs, const std::string& source, |
24 | | Temperature src_temp_hint, |
25 | | std::unique_ptr<WritableFileWriter>& dest_writer, |
26 | | uint64_t size, bool use_fsync, |
27 | | const std::shared_ptr<IOTracer>& io_tracer, |
28 | | uint64_t max_read_buffer_size = 4096, |
29 | | const std::optional<IOOptions>& readIOOptions = {}, |
30 | | const std::optional<IOOptions>& writeIOOptions = {}); |
31 | | IOStatus CopyFile(FileSystem* fs, const std::string& source, |
32 | | Temperature src_temp_hint, const std::string& destination, |
33 | | Temperature dst_temp, uint64_t size, bool use_fsync, |
34 | | const std::shared_ptr<IOTracer>& io_tracer, |
35 | | uint64_t max_read_buffer_size = 4096, |
36 | | const std::optional<IOOptions>& readIOOptions = {}, |
37 | | const std::optional<IOOptions>& writeIOOptions = {}); |
38 | | inline IOStatus CopyFile(const std::shared_ptr<FileSystem>& fs, |
39 | | const std::string& source, Temperature src_temp_hint, |
40 | | const std::string& destination, Temperature dst_temp, |
41 | | uint64_t size, bool use_fsync, |
42 | | const std::shared_ptr<IOTracer>& io_tracer, |
43 | | uint64_t max_read_buffer_size = 4096, |
44 | | const std::optional<IOOptions>& readIOOptions = {}, |
45 | 0 | const std::optional<IOOptions>& writeIOOptions = {}) { |
46 | 0 | return CopyFile(fs.get(), source, src_temp_hint, destination, dst_temp, size, |
47 | 0 | use_fsync, io_tracer, max_read_buffer_size, readIOOptions, |
48 | 0 | writeIOOptions); |
49 | 0 | } |
50 | | IOStatus CreateFile(FileSystem* fs, const std::string& destination, |
51 | | const std::string& contents, bool use_fsync); |
52 | | |
53 | | inline IOStatus CreateFile(const std::shared_ptr<FileSystem>& fs, |
54 | | const std::string& destination, |
55 | 0 | const std::string& contents, bool use_fsync) { |
56 | 0 | return CreateFile(fs.get(), destination, contents, use_fsync); |
57 | 0 | } |
58 | | |
59 | | // Delete a DB file, if this file is a SST file or Blob file and SstFileManager |
60 | | // is used, it should have already been tracked by SstFileManager via its |
61 | | // `OnFileAdd` API before passing to this API to be deleted, to ensure |
62 | | // SstFileManager and its DeleteScheduler are tracking DB size and trash size |
63 | | // properly. |
64 | | Status DeleteDBFile(const ImmutableDBOptions* db_options, |
65 | | const std::string& fname, const std::string& path_to_sync, |
66 | | const bool force_bg, const bool force_fg); |
67 | | |
68 | | // Delete an unaccounted DB file that is not tracked by SstFileManager and will |
69 | | // not be tracked by its DeleteScheduler when getting deleted. |
70 | | // If a legitimate bucket is provided and this file is scheduled for slow |
71 | | // deletion, it will be assigned to the specified trash bucket. |
72 | | Status DeleteUnaccountedDBFile(const ImmutableDBOptions* db_options, |
73 | | const std::string& fname, |
74 | | const std::string& dir_to_sync, |
75 | | const bool force_bg, const bool force_fg, |
76 | | std::optional<int32_t> bucket); |
77 | | |
78 | | // TODO(hx235): pass the whole DBOptions intead of its individual fields |
79 | | IOStatus GenerateOneFileChecksum( |
80 | | FileSystem* fs, const std::string& file_path, |
81 | | FileChecksumGenFactory* checksum_factory, |
82 | | const std::string& requested_checksum_func_name, std::string* file_checksum, |
83 | | std::string* file_checksum_func_name, |
84 | | size_t verify_checksums_readahead_size, bool allow_mmap_reads, |
85 | | std::shared_ptr<IOTracer>& io_tracer, RateLimiter* rate_limiter, |
86 | | const ReadOptions& read_options, Statistics* stats, SystemClock* clock, |
87 | | const FileOptions& file_options); |
88 | | |
89 | | inline IOStatus PrepareIOFromReadOptions(const ReadOptions& ro, |
90 | | SystemClock* clock, IOOptions& opts, |
91 | 730k | IODebugContext* dbg = nullptr) { |
92 | 730k | if (ro.request_id != nullptr) { |
93 | 0 | if (dbg != nullptr && dbg->request_id == nullptr) { |
94 | 0 | dbg->SetRequestId(ro.request_id); |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | 730k | if (ro.deadline.count()) { |
99 | 0 | std::chrono::microseconds now = |
100 | 0 | std::chrono::microseconds(clock->NowMicros()); |
101 | | // Ensure there is atleast 1us available. We don't want to pass a value of |
102 | | // 0 as that means no timeout |
103 | 0 | if (now >= ro.deadline) { |
104 | 0 | return IOStatus::TimedOut("Deadline exceeded"); |
105 | 0 | } |
106 | 0 | opts.timeout = ro.deadline - now; |
107 | 0 | } |
108 | | |
109 | 730k | if (ro.io_timeout.count() && |
110 | 0 | (!opts.timeout.count() || ro.io_timeout < opts.timeout)) { |
111 | 0 | opts.timeout = ro.io_timeout; |
112 | 0 | } |
113 | | |
114 | 730k | opts.rate_limiter_priority = ro.rate_limiter_priority; |
115 | 730k | opts.io_activity = ro.io_activity; |
116 | | |
117 | 730k | return IOStatus::OK(); |
118 | 730k | } |
119 | | |
120 | | inline IOStatus PrepareIOFromWriteOptions(const WriteOptions& wo, |
121 | 2.21M | IOOptions& opts) { |
122 | 2.21M | opts.rate_limiter_priority = wo.rate_limiter_priority; |
123 | 2.21M | opts.io_activity = wo.io_activity; |
124 | | |
125 | 2.21M | return IOStatus::OK(); |
126 | 2.21M | } |
127 | | |
128 | | // Test method to delete the input directory and all of its contents. |
129 | | // This method is destructive and is meant for use only in tests!!! |
130 | | Status DestroyDir(Env* env, const std::string& dir); |
131 | | |
132 | 873k | inline bool CheckFSFeatureSupport(FileSystem* fs, FSSupportedOps feat) { |
133 | 873k | int64_t supported_ops = 0; |
134 | 873k | fs->SupportedOps(supported_ops); |
135 | 873k | if (supported_ops & (1ULL << feat)) { |
136 | 77.6k | return true; |
137 | 77.6k | } |
138 | 795k | return false; |
139 | 873k | } |
140 | | |
141 | | } // namespace ROCKSDB_NAMESPACE |