1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #include "db/log_reader.h"
8 #include "leveldb/env.h"
9 #include "util/coding.h"
10 #include "util/crc32c.h"
15 Reader::Reporter::~Reporter() {
18 Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
19 uint64_t initial_offset)
23 backing_store_(new char[kBlockSize]),
26 last_record_offset_(0),
27 end_of_buffer_offset_(0),
28 initial_offset_(initial_offset) {
32 delete[] backing_store_;
35 bool Reader::SkipToInitialBlock() {
36 size_t offset_in_block = initial_offset_ % kBlockSize;
37 uint64_t block_start_location = initial_offset_ - offset_in_block;
39 // Don't search a block if we'd be in the trailer
40 if (offset_in_block > kBlockSize - 6) {
42 block_start_location += kBlockSize;
45 end_of_buffer_offset_ = block_start_location;
47 // Skip to start of first block that can contain the initial record
48 if (block_start_location > 0) {
49 Status skip_status = file_->Skip(block_start_location);
50 if (!skip_status.ok()) {
51 ReportDrop(block_start_location, skip_status);
59 bool Reader::ReadRecord(Slice* record, std::string* scratch) {
60 if (last_record_offset_ < initial_offset_) {
61 if (!SkipToInitialBlock()) {
68 bool in_fragmented_record = false;
69 // Record offset of the logical record that we're reading
70 // 0 is a dummy value to make compilers happy
71 uint64_t prospective_record_offset = 0;
75 uint64_t physical_record_offset = end_of_buffer_offset_ - buffer_.size();
76 const unsigned int record_type = ReadPhysicalRecord(&fragment);
77 switch (record_type) {
79 if (in_fragmented_record) {
80 // Handle bug in earlier versions of log::Writer where
81 // it could emit an empty kFirstType record at the tail end
82 // of a block followed by a kFullType or kFirstType record
83 // at the beginning of the next block.
84 if (scratch->empty()) {
85 in_fragmented_record = false;
87 ReportCorruption(scratch->size(), "partial record without end(1)");
90 prospective_record_offset = physical_record_offset;
93 last_record_offset_ = prospective_record_offset;
97 if (in_fragmented_record) {
98 // Handle bug in earlier versions of log::Writer where
99 // it could emit an empty kFirstType record at the tail end
100 // of a block followed by a kFullType or kFirstType record
101 // at the beginning of the next block.
102 if (scratch->empty()) {
103 in_fragmented_record = false;
105 ReportCorruption(scratch->size(), "partial record without end(2)");
108 prospective_record_offset = physical_record_offset;
109 scratch->assign(fragment.data(), fragment.size());
110 in_fragmented_record = true;
114 if (!in_fragmented_record) {
115 ReportCorruption(fragment.size(),
116 "missing start of fragmented record(1)");
118 scratch->append(fragment.data(), fragment.size());
123 if (!in_fragmented_record) {
124 ReportCorruption(fragment.size(),
125 "missing start of fragmented record(2)");
127 scratch->append(fragment.data(), fragment.size());
128 *record = Slice(*scratch);
129 last_record_offset_ = prospective_record_offset;
135 if (in_fragmented_record) {
136 // This can be caused by the writer dying immediately after
137 // writing a physical record but before completing the next; don't
138 // treat it as a corruption, just ignore the entire logical record.
144 if (in_fragmented_record) {
145 ReportCorruption(scratch->size(), "error in middle of record");
146 in_fragmented_record = false;
153 snprintf(buf, sizeof(buf), "unknown record type %u", record_type);
155 (fragment.size() + (in_fragmented_record ? scratch->size() : 0)),
157 in_fragmented_record = false;
166 uint64_t Reader::LastRecordOffset() {
167 return last_record_offset_;
170 void Reader::ReportCorruption(uint64_t bytes, const char* reason) {
171 ReportDrop(bytes, Status::Corruption(reason));
174 void Reader::ReportDrop(uint64_t bytes, const Status& reason) {
175 if (reporter_ != NULL &&
176 end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) {
177 reporter_->Corruption(static_cast<size_t>(bytes), reason);
181 unsigned int Reader::ReadPhysicalRecord(Slice* result) {
183 if (buffer_.size() < kHeaderSize) {
185 // Last read was a full read, so this is a trailer to skip
187 Status status = file_->Read(kBlockSize, &buffer_, backing_store_);
188 end_of_buffer_offset_ += buffer_.size();
191 ReportDrop(kBlockSize, status);
194 } else if (buffer_.size() < kBlockSize) {
199 // Note that if buffer_ is non-empty, we have a truncated header at the
200 // end of the file, which can be caused by the writer crashing in the
201 // middle of writing the header. Instead of considering this an error,
209 const char* header = buffer_.data();
210 const uint32_t a = static_cast<uint32_t>(header[4]) & 0xff;
211 const uint32_t b = static_cast<uint32_t>(header[5]) & 0xff;
212 const unsigned int type = header[6];
213 const uint32_t length = a | (b << 8);
214 if (kHeaderSize + length > buffer_.size()) {
215 size_t drop_size = buffer_.size();
218 ReportCorruption(drop_size, "bad record length");
221 // If the end of the file has been reached without reading |length| bytes
222 // of payload, assume the writer died in the middle of writing the record.
223 // Don't report a corruption.
227 if (type == kZeroType && length == 0) {
228 // Skip zero length record without reporting any drops since
229 // such records are produced by the mmap based writing code in
230 // env_posix.cc that preallocates file regions.
237 uint32_t expected_crc = crc32c::Unmask(DecodeFixed32(header));
238 uint32_t actual_crc = crc32c::Value(header + 6, 1 + length);
239 if (actual_crc != expected_crc) {
240 // Drop the rest of the buffer since "length" itself may have
241 // been corrupted and if we trust it, we could find some
242 // fragment of a real log record that just happens to look
243 // like a valid log record.
244 size_t drop_size = buffer_.size();
246 ReportCorruption(drop_size, "checksum mismatch");
251 buffer_.remove_prefix(kHeaderSize + length);
253 // Skip physical record that started before initial_offset_
254 if (end_of_buffer_offset_ - buffer_.size() - kHeaderSize - length <
260 *result = Slice(header + kHeaderSize, length);
266 } // namespace leveldb