/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/log_reader.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #ifndef YB_ROCKSDB_DB_LOG_READER_H |
25 | | #define YB_ROCKSDB_DB_LOG_READER_H |
26 | | |
27 | | #include <stdint.h> |
28 | | |
29 | | #include <memory> |
30 | | |
31 | | #include "yb/rocksdb/db/log_format.h" |
32 | | #include "yb/util/slice.h" |
33 | | #include "yb/rocksdb/status.h" |
34 | | #include "yb/rocksdb/options.h" |
35 | | |
36 | | namespace rocksdb { |
37 | | |
38 | | class SequentialFileReader; |
39 | | class Logger; |
40 | | |
41 | | namespace log { |
42 | | |
43 | | /** |
44 | | * Reader is a general purpose log stream reader implementation. The actual job |
45 | | * of reading from the device is implemented by the SequentialFile interface. |
46 | | * |
47 | | * Please see Writer for details on the file and record layout. |
48 | | */ |
49 | | class Reader { |
50 | | public: |
51 | | // Interface for reporting errors. |
52 | | class Reporter { |
53 | | public: |
54 | | virtual ~Reporter(); |
55 | | |
56 | | // Some corruption was detected. "size" is the approximate number |
57 | | // of bytes dropped due to the corruption. |
58 | | virtual void Corruption(size_t bytes, const Status& status) = 0; |
59 | | }; |
60 | | |
61 | | // Create a reader that will return log records from "*file". |
62 | | // "*file" must remain live while this Reader is in use. |
63 | | // |
64 | | // If "reporter" is non-nullptr, it is notified whenever some data is |
65 | | // dropped due to a detected corruption. "*reporter" must remain |
66 | | // live while this Reader is in use. |
67 | | // |
68 | | // If "checksum" is true, verify checksums if available. |
69 | | // |
70 | | // The Reader will start reading at the first record located at physical |
71 | | // position >= initial_offset within the file. |
72 | | Reader( |
73 | | std::shared_ptr<Logger> info_log, |
74 | | std::unique_ptr<SequentialFileReader>&& file, |
75 | | Reporter* reporter, |
76 | | bool checksum, |
77 | | uint64_t initial_offset, |
78 | | uint64_t log_num); |
79 | | |
80 | | ~Reader(); |
81 | | |
82 | | // Read the next record into *record. Returns true if read |
83 | | // successfully, false if we hit end of the input. May use |
84 | | // "*scratch" as temporary storage. The contents filled in *record |
85 | | // will only be valid until the next mutating operation on this |
86 | | // reader or the next mutation to *scratch. |
87 | | bool ReadRecord(Slice* record, std::string* scratch, |
88 | | WALRecoveryMode wal_recovery_mode = |
89 | | WALRecoveryMode::kTolerateCorruptedTailRecords); |
90 | | |
91 | | // Returns the physical offset of the last record returned by ReadRecord. |
92 | | // |
93 | | // Undefined before the first call to ReadRecord. |
94 | | uint64_t LastRecordOffset(); |
95 | | |
96 | | // returns true if the reader has encountered an eof condition. |
97 | 2.78k | bool IsEOF() { |
98 | 2.78k | return eof_; |
99 | 2.78k | } |
100 | | |
101 | | // when we know more data has been written to the file. we can use this |
102 | | // function to force the reader to look again in the file. |
103 | | // Also aligns the file position indicator to the start of the next block |
104 | | // by reading the rest of the data from the EOF position to the end of the |
105 | | // block that was partially read. |
106 | | void UnmarkEOF(); |
107 | | |
108 | | SequentialFileReader* file() { return file_.get(); } |
109 | | |
110 | | private: |
111 | | std::shared_ptr<Logger> info_log_; |
112 | | const std::unique_ptr<SequentialFileReader> file_; |
113 | | Reporter* const reporter_; |
114 | | bool const checksum_; |
115 | | uint8_t* const backing_store_; |
116 | | Slice buffer_; |
117 | | bool eof_; // Last Read() indicated EOF by returning < kBlockSize |
118 | | bool read_error_; // Error occurred while reading from file |
119 | | |
120 | | // Offset of the file position indicator within the last block when an |
121 | | // EOF was detected. |
122 | | size_t eof_offset_; |
123 | | |
124 | | // Offset of the last record returned by ReadRecord. |
125 | | uint64_t last_record_offset_; |
126 | | |
127 | | // Offset of the first location past the end of buffer_. |
128 | | uint64_t end_of_buffer_offset_; |
129 | | |
130 | | // Offset at which to start looking for the first record to return |
131 | | uint64_t const initial_offset_; |
132 | | |
133 | | // which log number this is |
134 | | uint64_t const log_number_; |
135 | | |
136 | | // Extend record types with the following special values |
137 | | enum { |
138 | | kEof = kMaxRecordType + 1, |
139 | | // Returned whenever we find an invalid physical record. |
140 | | // Currently there are three situations in which this happens: |
141 | | // * The record has an invalid CRC (ReadPhysicalRecord reports a drop) |
142 | | // * The record is a 0-length record (No drop is reported) |
143 | | // * The record is below constructor's initial_offset (No drop is reported) |
144 | | kBadRecord = kMaxRecordType + 2, |
145 | | // Returned when we fail to read a valid header. |
146 | | kBadHeader = kMaxRecordType + 3, |
147 | | // Returned when we read an old record from a previous user of the log. |
148 | | kOldRecord = kMaxRecordType + 4, |
149 | | }; |
150 | | |
151 | | // Skips all blocks that are completely before "initial_offset_". |
152 | | // |
153 | | // Returns true on success. Handles reporting. |
154 | | bool SkipToInitialBlock(); |
155 | | |
156 | | // Return type, or one of the preceding special values |
157 | | unsigned int ReadPhysicalRecord(Slice* result, size_t* drop_size); |
158 | | |
159 | | // Read some more |
160 | | bool ReadMore(size_t* drop_size, int *error); |
161 | | |
162 | | // Reports dropped bytes to the reporter. |
163 | | // buffer_ must be updated to remove the dropped bytes prior to invocation. |
164 | | void ReportCorruption(size_t bytes, const char* reason); |
165 | | void ReportDrop(size_t bytes, const Status& reason); |
166 | | |
167 | | // No copying allowed |
168 | | Reader(const Reader&); |
169 | | void operator=(const Reader&); |
170 | | }; |
171 | | |
172 | | } // namespace log |
173 | | } // namespace rocksdb |
174 | | |
175 | | #endif // YB_ROCKSDB_DB_LOG_READER_H |