]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/log_reader.h
4727be24c8636e420fc02bf64f5667fd20f95f7f
[ceph.git] / ceph / src / rocksdb / db / log_reader.h
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
10 #pragma once
11 #include <memory>
12 #include <stdint.h>
13
14 #include "db/log_format.h"
15 #include "rocksdb/slice.h"
16 #include "rocksdb/status.h"
17 #include "rocksdb/options.h"
18
19 namespace rocksdb {
20
21 class SequentialFileReader;
22 class Logger;
23 using std::unique_ptr;
24
25 namespace log {
26
27 /**
28 * Reader is a general purpose log stream reader implementation. The actual job
29 * of reading from the device is implemented by the SequentialFile interface.
30 *
31 * Please see Writer for details on the file and record layout.
32 */
33 class Reader {
34 public:
35 // Interface for reporting errors.
36 class Reporter {
37 public:
38 virtual ~Reporter();
39
40 // Some corruption was detected. "size" is the approximate number
41 // of bytes dropped due to the corruption.
42 virtual void Corruption(size_t bytes, const Status& status) = 0;
43 };
44
45 // Create a reader that will return log records from "*file".
46 // "*file" must remain live while this Reader is in use.
47 //
48 // If "reporter" is non-nullptr, it is notified whenever some data is
49 // dropped due to a detected corruption. "*reporter" must remain
50 // live while this Reader is in use.
51 //
52 // If "checksum" is true, verify checksums if available.
53 Reader(std::shared_ptr<Logger> info_log,
54 // @lint-ignore TXT2 T25377293 Grandfathered in
55 unique_ptr<SequentialFileReader>&& file, Reporter* reporter,
56 bool checksum, uint64_t log_num);
57
58 ~Reader();
59
60 // Read the next record into *record. Returns true if read
61 // successfully, false if we hit end of the input. May use
62 // "*scratch" as temporary storage. The contents filled in *record
63 // will only be valid until the next mutating operation on this
64 // reader or the next mutation to *scratch.
65 bool ReadRecord(Slice* record, std::string* scratch,
66 WALRecoveryMode wal_recovery_mode =
67 WALRecoveryMode::kTolerateCorruptedTailRecords);
68
69 // Returns the physical offset of the last record returned by ReadRecord.
70 //
71 // Undefined before the first call to ReadRecord.
72 uint64_t LastRecordOffset();
73
74 // returns true if the reader has encountered an eof condition.
75 bool IsEOF() {
76 return eof_;
77 }
78
79 // when we know more data has been written to the file. we can use this
80 // function to force the reader to look again in the file.
81 // Also aligns the file position indicator to the start of the next block
82 // by reading the rest of the data from the EOF position to the end of the
83 // block that was partially read.
84 void UnmarkEOF();
85
86 SequentialFileReader* file() { return file_.get(); }
87
88 private:
89 std::shared_ptr<Logger> info_log_;
90 const unique_ptr<SequentialFileReader> file_;
91 Reporter* const reporter_;
92 bool const checksum_;
93 char* const backing_store_;
94 Slice buffer_;
95 bool eof_; // Last Read() indicated EOF by returning < kBlockSize
96 bool read_error_; // Error occurred while reading from file
97
98 // Offset of the file position indicator within the last block when an
99 // EOF was detected.
100 size_t eof_offset_;
101
102 // Offset of the last record returned by ReadRecord.
103 uint64_t last_record_offset_;
104 // Offset of the first location past the end of buffer_.
105 uint64_t end_of_buffer_offset_;
106
107 // which log number this is
108 uint64_t const log_number_;
109
110 // Whether this is a recycled log file
111 bool recycled_;
112
113 // Extend record types with the following special values
114 enum {
115 kEof = kMaxRecordType + 1,
116 // Returned whenever we find an invalid physical record.
117 // Currently there are three situations in which this happens:
118 // * The record has an invalid CRC (ReadPhysicalRecord reports a drop)
119 // * The record is a 0-length record (No drop is reported)
120 kBadRecord = kMaxRecordType + 2,
121 // Returned when we fail to read a valid header.
122 kBadHeader = kMaxRecordType + 3,
123 // Returned when we read an old record from a previous user of the log.
124 kOldRecord = kMaxRecordType + 4,
125 // Returned when we get a bad record length
126 kBadRecordLen = kMaxRecordType + 5,
127 // Returned when we get a bad record checksum
128 kBadRecordChecksum = kMaxRecordType + 6,
129 };
130
131 // Return type, or one of the preceding special values
132 unsigned int ReadPhysicalRecord(Slice* result, size_t* drop_size);
133
134 // Read some more
135 bool ReadMore(size_t* drop_size, int *error);
136
137 // Reports dropped bytes to the reporter.
138 // buffer_ must be updated to remove the dropped bytes prior to invocation.
139 void ReportCorruption(size_t bytes, const char* reason);
140 void ReportDrop(size_t bytes, const Status& reason);
141
142 // No copying allowed
143 Reader(const Reader&);
144 void operator=(const Reader&);
145 };
146
147 } // namespace log
148 } // namespace rocksdb