/src/rocksdb/db/wal_manager.h
Line | Count | Source (jump to first uncovered line) |
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 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
7 | | // Use of this source code is governed by a BSD-style license that can be |
8 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
9 | | #pragma once |
10 | | |
11 | | #include <atomic> |
12 | | #include <deque> |
13 | | #include <limits> |
14 | | #include <memory> |
15 | | #include <set> |
16 | | #include <string> |
17 | | #include <utility> |
18 | | #include <vector> |
19 | | |
20 | | #include "db/version_set.h" |
21 | | #include "file/file_util.h" |
22 | | #include "options/db_options.h" |
23 | | #include "port/port.h" |
24 | | #include "rocksdb/env.h" |
25 | | #include "rocksdb/status.h" |
26 | | #include "rocksdb/transaction_log.h" |
27 | | #include "rocksdb/types.h" |
28 | | #include "util/atomic.h" |
29 | | |
30 | | namespace ROCKSDB_NAMESPACE { |
31 | | |
32 | | |
33 | | // WAL manager provides the abstraction for reading the WAL files as a single |
34 | | // unit. Internally, it opens and reads the files using Reader or Writer |
35 | | // abstraction. |
36 | | class WalManager { |
37 | | public: |
38 | | WalManager(const ImmutableDBOptions& db_options, |
39 | | const FileOptions& file_options, |
40 | | const std::shared_ptr<IOTracer>& io_tracer, |
41 | | const bool seq_per_batch = false) |
42 | | : db_options_(db_options), |
43 | | file_options_(file_options), |
44 | | env_(db_options.env), |
45 | | fs_(db_options.fs, io_tracer), |
46 | | purge_wal_files_last_run_(0), |
47 | | seq_per_batch_(seq_per_batch), |
48 | | wal_dir_(db_options_.GetWalDir()), |
49 | | wal_in_db_path_(db_options_.IsWalDirSameAsDBPath()), |
50 | 11.0k | io_tracer_(io_tracer) {} |
51 | | |
52 | | Status GetSortedWalFiles(VectorWalPtr& files, bool need_seqnos = true, |
53 | | bool include_archived = true); |
54 | | |
55 | | // Allow user to tail transaction log to find all recent changes to the |
56 | | // database that are newer than `seq_number`. |
57 | | Status GetUpdatesSince( |
58 | | SequenceNumber seq_number, std::unique_ptr<TransactionLogIterator>* iter, |
59 | | const TransactionLogIterator::ReadOptions& read_options, |
60 | | VersionSet* version_set); |
61 | | |
62 | | void PurgeObsoleteWALFiles(); |
63 | | |
64 | | void ArchiveWALFile(const std::string& fname, uint64_t number); |
65 | | |
66 | | Status DeleteFile(const std::string& fname, uint64_t number); |
67 | | |
68 | | Status GetLiveWalFile(uint64_t number, std::unique_ptr<WalFile>* log_file); |
69 | | |
70 | | Status TEST_ReadFirstRecord(const WalFileType type, const uint64_t number, |
71 | 0 | SequenceNumber* sequence) { |
72 | 0 | return ReadFirstRecord(type, number, sequence); |
73 | 0 | } |
74 | | |
75 | | Status TEST_ReadFirstLine(const std::string& fname, const uint64_t number, |
76 | 0 | SequenceNumber* sequence) { |
77 | 0 | return ReadFirstLine(fname, number, sequence); |
78 | 0 | } |
79 | | |
80 | | private: |
81 | | Status GetSortedWalsOfType(const std::string& path, VectorWalPtr& log_files, |
82 | | WalFileType type, bool need_seqnos); |
83 | | // Requires: all_logs should be sorted with earliest log file first |
84 | | // Retains all log files in all_logs which contain updates with seq no. |
85 | | // Greater Than or Equal to the requested SequenceNumber. |
86 | | Status RetainProbableWalFiles(VectorWalPtr& all_logs, |
87 | | const SequenceNumber target); |
88 | | |
89 | | // ReadFirstRecord checks the read_first_record_cache_ to see if the entry |
90 | | // exists or not. If not, it will read the WAL file. |
91 | | // In case of wal_compression, WAL contains a `kSetCompressionType` record |
92 | | // which is not associated with any sequence number. So the sequence_number is |
93 | | // set to 1 if that WAL doesn't include any other record (basically empty) in |
94 | | // order to include that WAL and is inserted in read_first_record_cache_. |
95 | | // Therefore, sequence_number is used as boolean if WAL should be included or |
96 | | // not and that sequence_number shouldn't be use for any other purpose. |
97 | | Status ReadFirstRecord(const WalFileType type, const uint64_t number, |
98 | | SequenceNumber* sequence); |
99 | | |
100 | | // In case of no wal_compression, ReadFirstLine returns status.ok() and |
101 | | // sequence == 0 if the file exists, but is empty. |
102 | | // In case of wal_compression, WAL contains |
103 | | // `kSetCompressionType` record which is not associated with any sequence |
104 | | // number if that WAL doesn't include any other record (basically empty). As |
105 | | // result for an empty file, GetSortedWalsOfType() will skip these WALs |
106 | | // causing the operations to fail. To avoid that, it sets sequence_number to |
107 | | // 1 inorder to include that WAL. |
108 | | Status ReadFirstLine(const std::string& fname, const uint64_t number, |
109 | | SequenceNumber* sequence); |
110 | | |
111 | | // ------- state from DBImpl ------ |
112 | | const ImmutableDBOptions& db_options_; |
113 | | const FileOptions file_options_; |
114 | | Env* env_; |
115 | | const FileSystemPtr fs_; |
116 | | |
117 | | // ------- WalManager state ------- |
118 | | // cache for ReadFirstRecord() calls |
119 | | std::unordered_map<uint64_t, SequenceNumber> read_first_record_cache_; |
120 | | port::Mutex read_first_record_cache_mutex_; |
121 | | |
122 | | // last time when PurgeObsoleteWALFiles ran. |
123 | | RelaxedAtomic<uint64_t> purge_wal_files_last_run_; |
124 | | |
125 | | bool seq_per_batch_; |
126 | | |
127 | | const std::string& wal_dir_; |
128 | | |
129 | | bool wal_in_db_path_; |
130 | | |
131 | | // obsolete files will be deleted every this seconds if ttl deletion is |
132 | | // enabled and archive size_limit is disabled. |
133 | | static constexpr uint64_t kDefaultIntervalToDeleteObsoleteWAL = 600; |
134 | | |
135 | | std::shared_ptr<IOTracer> io_tracer_; |
136 | | }; |
137 | | |
138 | | } // namespace ROCKSDB_NAMESPACE |