1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors.
4*9507f98cSAndroid Build Coastguard Worker
5*9507f98cSAndroid Build Coastguard Worker #include "db/log_reader.h"
6*9507f98cSAndroid Build Coastguard Worker
7*9507f98cSAndroid Build Coastguard Worker #include <cstdio>
8*9507f98cSAndroid Build Coastguard Worker
9*9507f98cSAndroid Build Coastguard Worker #include "leveldb/env.h"
10*9507f98cSAndroid Build Coastguard Worker #include "util/coding.h"
11*9507f98cSAndroid Build Coastguard Worker #include "util/crc32c.h"
12*9507f98cSAndroid Build Coastguard Worker
13*9507f98cSAndroid Build Coastguard Worker namespace leveldb {
14*9507f98cSAndroid Build Coastguard Worker namespace log {
15*9507f98cSAndroid Build Coastguard Worker
16*9507f98cSAndroid Build Coastguard Worker Reader::Reporter::~Reporter() = default;
17*9507f98cSAndroid Build Coastguard Worker
Reader(SequentialFile * file,Reporter * reporter,bool checksum,uint64_t initial_offset)18*9507f98cSAndroid Build Coastguard Worker Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
19*9507f98cSAndroid Build Coastguard Worker uint64_t initial_offset)
20*9507f98cSAndroid Build Coastguard Worker : file_(file),
21*9507f98cSAndroid Build Coastguard Worker reporter_(reporter),
22*9507f98cSAndroid Build Coastguard Worker checksum_(checksum),
23*9507f98cSAndroid Build Coastguard Worker backing_store_(new char[kBlockSize]),
24*9507f98cSAndroid Build Coastguard Worker buffer_(),
25*9507f98cSAndroid Build Coastguard Worker eof_(false),
26*9507f98cSAndroid Build Coastguard Worker last_record_offset_(0),
27*9507f98cSAndroid Build Coastguard Worker end_of_buffer_offset_(0),
28*9507f98cSAndroid Build Coastguard Worker initial_offset_(initial_offset),
29*9507f98cSAndroid Build Coastguard Worker resyncing_(initial_offset > 0) {}
30*9507f98cSAndroid Build Coastguard Worker
~Reader()31*9507f98cSAndroid Build Coastguard Worker Reader::~Reader() { delete[] backing_store_; }
32*9507f98cSAndroid Build Coastguard Worker
SkipToInitialBlock()33*9507f98cSAndroid Build Coastguard Worker bool Reader::SkipToInitialBlock() {
34*9507f98cSAndroid Build Coastguard Worker const size_t offset_in_block = initial_offset_ % kBlockSize;
35*9507f98cSAndroid Build Coastguard Worker uint64_t block_start_location = initial_offset_ - offset_in_block;
36*9507f98cSAndroid Build Coastguard Worker
37*9507f98cSAndroid Build Coastguard Worker // Don't search a block if we'd be in the trailer
38*9507f98cSAndroid Build Coastguard Worker if (offset_in_block > kBlockSize - 6) {
39*9507f98cSAndroid Build Coastguard Worker block_start_location += kBlockSize;
40*9507f98cSAndroid Build Coastguard Worker }
41*9507f98cSAndroid Build Coastguard Worker
42*9507f98cSAndroid Build Coastguard Worker end_of_buffer_offset_ = block_start_location;
43*9507f98cSAndroid Build Coastguard Worker
44*9507f98cSAndroid Build Coastguard Worker // Skip to start of first block that can contain the initial record
45*9507f98cSAndroid Build Coastguard Worker if (block_start_location > 0) {
46*9507f98cSAndroid Build Coastguard Worker Status skip_status = file_->Skip(block_start_location);
47*9507f98cSAndroid Build Coastguard Worker if (!skip_status.ok()) {
48*9507f98cSAndroid Build Coastguard Worker ReportDrop(block_start_location, skip_status);
49*9507f98cSAndroid Build Coastguard Worker return false;
50*9507f98cSAndroid Build Coastguard Worker }
51*9507f98cSAndroid Build Coastguard Worker }
52*9507f98cSAndroid Build Coastguard Worker
53*9507f98cSAndroid Build Coastguard Worker return true;
54*9507f98cSAndroid Build Coastguard Worker }
55*9507f98cSAndroid Build Coastguard Worker
ReadRecord(Slice * record,std::string * scratch)56*9507f98cSAndroid Build Coastguard Worker bool Reader::ReadRecord(Slice* record, std::string* scratch) {
57*9507f98cSAndroid Build Coastguard Worker if (last_record_offset_ < initial_offset_) {
58*9507f98cSAndroid Build Coastguard Worker if (!SkipToInitialBlock()) {
59*9507f98cSAndroid Build Coastguard Worker return false;
60*9507f98cSAndroid Build Coastguard Worker }
61*9507f98cSAndroid Build Coastguard Worker }
62*9507f98cSAndroid Build Coastguard Worker
63*9507f98cSAndroid Build Coastguard Worker scratch->clear();
64*9507f98cSAndroid Build Coastguard Worker record->clear();
65*9507f98cSAndroid Build Coastguard Worker bool in_fragmented_record = false;
66*9507f98cSAndroid Build Coastguard Worker // Record offset of the logical record that we're reading
67*9507f98cSAndroid Build Coastguard Worker // 0 is a dummy value to make compilers happy
68*9507f98cSAndroid Build Coastguard Worker uint64_t prospective_record_offset = 0;
69*9507f98cSAndroid Build Coastguard Worker
70*9507f98cSAndroid Build Coastguard Worker Slice fragment;
71*9507f98cSAndroid Build Coastguard Worker while (true) {
72*9507f98cSAndroid Build Coastguard Worker const unsigned int record_type = ReadPhysicalRecord(&fragment);
73*9507f98cSAndroid Build Coastguard Worker
74*9507f98cSAndroid Build Coastguard Worker // ReadPhysicalRecord may have only had an empty trailer remaining in its
75*9507f98cSAndroid Build Coastguard Worker // internal buffer. Calculate the offset of the next physical record now
76*9507f98cSAndroid Build Coastguard Worker // that it has returned, properly accounting for its header size.
77*9507f98cSAndroid Build Coastguard Worker uint64_t physical_record_offset =
78*9507f98cSAndroid Build Coastguard Worker end_of_buffer_offset_ - buffer_.size() - kHeaderSize - fragment.size();
79*9507f98cSAndroid Build Coastguard Worker
80*9507f98cSAndroid Build Coastguard Worker if (resyncing_) {
81*9507f98cSAndroid Build Coastguard Worker if (record_type == kMiddleType) {
82*9507f98cSAndroid Build Coastguard Worker continue;
83*9507f98cSAndroid Build Coastguard Worker } else if (record_type == kLastType) {
84*9507f98cSAndroid Build Coastguard Worker resyncing_ = false;
85*9507f98cSAndroid Build Coastguard Worker continue;
86*9507f98cSAndroid Build Coastguard Worker } else {
87*9507f98cSAndroid Build Coastguard Worker resyncing_ = false;
88*9507f98cSAndroid Build Coastguard Worker }
89*9507f98cSAndroid Build Coastguard Worker }
90*9507f98cSAndroid Build Coastguard Worker
91*9507f98cSAndroid Build Coastguard Worker switch (record_type) {
92*9507f98cSAndroid Build Coastguard Worker case kFullType:
93*9507f98cSAndroid Build Coastguard Worker if (in_fragmented_record) {
94*9507f98cSAndroid Build Coastguard Worker // Handle bug in earlier versions of log::Writer where
95*9507f98cSAndroid Build Coastguard Worker // it could emit an empty kFirstType record at the tail end
96*9507f98cSAndroid Build Coastguard Worker // of a block followed by a kFullType or kFirstType record
97*9507f98cSAndroid Build Coastguard Worker // at the beginning of the next block.
98*9507f98cSAndroid Build Coastguard Worker if (!scratch->empty()) {
99*9507f98cSAndroid Build Coastguard Worker ReportCorruption(scratch->size(), "partial record without end(1)");
100*9507f98cSAndroid Build Coastguard Worker }
101*9507f98cSAndroid Build Coastguard Worker }
102*9507f98cSAndroid Build Coastguard Worker prospective_record_offset = physical_record_offset;
103*9507f98cSAndroid Build Coastguard Worker scratch->clear();
104*9507f98cSAndroid Build Coastguard Worker *record = fragment;
105*9507f98cSAndroid Build Coastguard Worker last_record_offset_ = prospective_record_offset;
106*9507f98cSAndroid Build Coastguard Worker return true;
107*9507f98cSAndroid Build Coastguard Worker
108*9507f98cSAndroid Build Coastguard Worker case kFirstType:
109*9507f98cSAndroid Build Coastguard Worker if (in_fragmented_record) {
110*9507f98cSAndroid Build Coastguard Worker // Handle bug in earlier versions of log::Writer where
111*9507f98cSAndroid Build Coastguard Worker // it could emit an empty kFirstType record at the tail end
112*9507f98cSAndroid Build Coastguard Worker // of a block followed by a kFullType or kFirstType record
113*9507f98cSAndroid Build Coastguard Worker // at the beginning of the next block.
114*9507f98cSAndroid Build Coastguard Worker if (!scratch->empty()) {
115*9507f98cSAndroid Build Coastguard Worker ReportCorruption(scratch->size(), "partial record without end(2)");
116*9507f98cSAndroid Build Coastguard Worker }
117*9507f98cSAndroid Build Coastguard Worker }
118*9507f98cSAndroid Build Coastguard Worker prospective_record_offset = physical_record_offset;
119*9507f98cSAndroid Build Coastguard Worker scratch->assign(fragment.data(), fragment.size());
120*9507f98cSAndroid Build Coastguard Worker in_fragmented_record = true;
121*9507f98cSAndroid Build Coastguard Worker break;
122*9507f98cSAndroid Build Coastguard Worker
123*9507f98cSAndroid Build Coastguard Worker case kMiddleType:
124*9507f98cSAndroid Build Coastguard Worker if (!in_fragmented_record) {
125*9507f98cSAndroid Build Coastguard Worker ReportCorruption(fragment.size(),
126*9507f98cSAndroid Build Coastguard Worker "missing start of fragmented record(1)");
127*9507f98cSAndroid Build Coastguard Worker } else {
128*9507f98cSAndroid Build Coastguard Worker scratch->append(fragment.data(), fragment.size());
129*9507f98cSAndroid Build Coastguard Worker }
130*9507f98cSAndroid Build Coastguard Worker break;
131*9507f98cSAndroid Build Coastguard Worker
132*9507f98cSAndroid Build Coastguard Worker case kLastType:
133*9507f98cSAndroid Build Coastguard Worker if (!in_fragmented_record) {
134*9507f98cSAndroid Build Coastguard Worker ReportCorruption(fragment.size(),
135*9507f98cSAndroid Build Coastguard Worker "missing start of fragmented record(2)");
136*9507f98cSAndroid Build Coastguard Worker } else {
137*9507f98cSAndroid Build Coastguard Worker scratch->append(fragment.data(), fragment.size());
138*9507f98cSAndroid Build Coastguard Worker *record = Slice(*scratch);
139*9507f98cSAndroid Build Coastguard Worker last_record_offset_ = prospective_record_offset;
140*9507f98cSAndroid Build Coastguard Worker return true;
141*9507f98cSAndroid Build Coastguard Worker }
142*9507f98cSAndroid Build Coastguard Worker break;
143*9507f98cSAndroid Build Coastguard Worker
144*9507f98cSAndroid Build Coastguard Worker case kEof:
145*9507f98cSAndroid Build Coastguard Worker if (in_fragmented_record) {
146*9507f98cSAndroid Build Coastguard Worker // This can be caused by the writer dying immediately after
147*9507f98cSAndroid Build Coastguard Worker // writing a physical record but before completing the next; don't
148*9507f98cSAndroid Build Coastguard Worker // treat it as a corruption, just ignore the entire logical record.
149*9507f98cSAndroid Build Coastguard Worker scratch->clear();
150*9507f98cSAndroid Build Coastguard Worker }
151*9507f98cSAndroid Build Coastguard Worker return false;
152*9507f98cSAndroid Build Coastguard Worker
153*9507f98cSAndroid Build Coastguard Worker case kBadRecord:
154*9507f98cSAndroid Build Coastguard Worker if (in_fragmented_record) {
155*9507f98cSAndroid Build Coastguard Worker ReportCorruption(scratch->size(), "error in middle of record");
156*9507f98cSAndroid Build Coastguard Worker in_fragmented_record = false;
157*9507f98cSAndroid Build Coastguard Worker scratch->clear();
158*9507f98cSAndroid Build Coastguard Worker }
159*9507f98cSAndroid Build Coastguard Worker break;
160*9507f98cSAndroid Build Coastguard Worker
161*9507f98cSAndroid Build Coastguard Worker default: {
162*9507f98cSAndroid Build Coastguard Worker char buf[40];
163*9507f98cSAndroid Build Coastguard Worker std::snprintf(buf, sizeof(buf), "unknown record type %u", record_type);
164*9507f98cSAndroid Build Coastguard Worker ReportCorruption(
165*9507f98cSAndroid Build Coastguard Worker (fragment.size() + (in_fragmented_record ? scratch->size() : 0)),
166*9507f98cSAndroid Build Coastguard Worker buf);
167*9507f98cSAndroid Build Coastguard Worker in_fragmented_record = false;
168*9507f98cSAndroid Build Coastguard Worker scratch->clear();
169*9507f98cSAndroid Build Coastguard Worker break;
170*9507f98cSAndroid Build Coastguard Worker }
171*9507f98cSAndroid Build Coastguard Worker }
172*9507f98cSAndroid Build Coastguard Worker }
173*9507f98cSAndroid Build Coastguard Worker return false;
174*9507f98cSAndroid Build Coastguard Worker }
175*9507f98cSAndroid Build Coastguard Worker
LastRecordOffset()176*9507f98cSAndroid Build Coastguard Worker uint64_t Reader::LastRecordOffset() { return last_record_offset_; }
177*9507f98cSAndroid Build Coastguard Worker
ReportCorruption(uint64_t bytes,const char * reason)178*9507f98cSAndroid Build Coastguard Worker void Reader::ReportCorruption(uint64_t bytes, const char* reason) {
179*9507f98cSAndroid Build Coastguard Worker ReportDrop(bytes, Status::Corruption(reason));
180*9507f98cSAndroid Build Coastguard Worker }
181*9507f98cSAndroid Build Coastguard Worker
ReportDrop(uint64_t bytes,const Status & reason)182*9507f98cSAndroid Build Coastguard Worker void Reader::ReportDrop(uint64_t bytes, const Status& reason) {
183*9507f98cSAndroid Build Coastguard Worker if (reporter_ != nullptr &&
184*9507f98cSAndroid Build Coastguard Worker end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) {
185*9507f98cSAndroid Build Coastguard Worker reporter_->Corruption(static_cast<size_t>(bytes), reason);
186*9507f98cSAndroid Build Coastguard Worker }
187*9507f98cSAndroid Build Coastguard Worker }
188*9507f98cSAndroid Build Coastguard Worker
ReadPhysicalRecord(Slice * result)189*9507f98cSAndroid Build Coastguard Worker unsigned int Reader::ReadPhysicalRecord(Slice* result) {
190*9507f98cSAndroid Build Coastguard Worker while (true) {
191*9507f98cSAndroid Build Coastguard Worker if (buffer_.size() < kHeaderSize) {
192*9507f98cSAndroid Build Coastguard Worker if (!eof_) {
193*9507f98cSAndroid Build Coastguard Worker // Last read was a full read, so this is a trailer to skip
194*9507f98cSAndroid Build Coastguard Worker buffer_.clear();
195*9507f98cSAndroid Build Coastguard Worker Status status = file_->Read(kBlockSize, &buffer_, backing_store_);
196*9507f98cSAndroid Build Coastguard Worker end_of_buffer_offset_ += buffer_.size();
197*9507f98cSAndroid Build Coastguard Worker if (!status.ok()) {
198*9507f98cSAndroid Build Coastguard Worker buffer_.clear();
199*9507f98cSAndroid Build Coastguard Worker ReportDrop(kBlockSize, status);
200*9507f98cSAndroid Build Coastguard Worker eof_ = true;
201*9507f98cSAndroid Build Coastguard Worker return kEof;
202*9507f98cSAndroid Build Coastguard Worker } else if (buffer_.size() < kBlockSize) {
203*9507f98cSAndroid Build Coastguard Worker eof_ = true;
204*9507f98cSAndroid Build Coastguard Worker }
205*9507f98cSAndroid Build Coastguard Worker continue;
206*9507f98cSAndroid Build Coastguard Worker } else {
207*9507f98cSAndroid Build Coastguard Worker // Note that if buffer_ is non-empty, we have a truncated header at the
208*9507f98cSAndroid Build Coastguard Worker // end of the file, which can be caused by the writer crashing in the
209*9507f98cSAndroid Build Coastguard Worker // middle of writing the header. Instead of considering this an error,
210*9507f98cSAndroid Build Coastguard Worker // just report EOF.
211*9507f98cSAndroid Build Coastguard Worker buffer_.clear();
212*9507f98cSAndroid Build Coastguard Worker return kEof;
213*9507f98cSAndroid Build Coastguard Worker }
214*9507f98cSAndroid Build Coastguard Worker }
215*9507f98cSAndroid Build Coastguard Worker
216*9507f98cSAndroid Build Coastguard Worker // Parse the header
217*9507f98cSAndroid Build Coastguard Worker const char* header = buffer_.data();
218*9507f98cSAndroid Build Coastguard Worker const uint32_t a = static_cast<uint32_t>(header[4]) & 0xff;
219*9507f98cSAndroid Build Coastguard Worker const uint32_t b = static_cast<uint32_t>(header[5]) & 0xff;
220*9507f98cSAndroid Build Coastguard Worker const unsigned int type = header[6];
221*9507f98cSAndroid Build Coastguard Worker const uint32_t length = a | (b << 8);
222*9507f98cSAndroid Build Coastguard Worker if (kHeaderSize + length > buffer_.size()) {
223*9507f98cSAndroid Build Coastguard Worker size_t drop_size = buffer_.size();
224*9507f98cSAndroid Build Coastguard Worker buffer_.clear();
225*9507f98cSAndroid Build Coastguard Worker if (!eof_) {
226*9507f98cSAndroid Build Coastguard Worker ReportCorruption(drop_size, "bad record length");
227*9507f98cSAndroid Build Coastguard Worker return kBadRecord;
228*9507f98cSAndroid Build Coastguard Worker }
229*9507f98cSAndroid Build Coastguard Worker // If the end of the file has been reached without reading |length| bytes
230*9507f98cSAndroid Build Coastguard Worker // of payload, assume the writer died in the middle of writing the record.
231*9507f98cSAndroid Build Coastguard Worker // Don't report a corruption.
232*9507f98cSAndroid Build Coastguard Worker return kEof;
233*9507f98cSAndroid Build Coastguard Worker }
234*9507f98cSAndroid Build Coastguard Worker
235*9507f98cSAndroid Build Coastguard Worker if (type == kZeroType && length == 0) {
236*9507f98cSAndroid Build Coastguard Worker // Skip zero length record without reporting any drops since
237*9507f98cSAndroid Build Coastguard Worker // such records are produced by the mmap based writing code in
238*9507f98cSAndroid Build Coastguard Worker // env_posix.cc that preallocates file regions.
239*9507f98cSAndroid Build Coastguard Worker buffer_.clear();
240*9507f98cSAndroid Build Coastguard Worker return kBadRecord;
241*9507f98cSAndroid Build Coastguard Worker }
242*9507f98cSAndroid Build Coastguard Worker
243*9507f98cSAndroid Build Coastguard Worker // Check crc
244*9507f98cSAndroid Build Coastguard Worker if (checksum_) {
245*9507f98cSAndroid Build Coastguard Worker uint32_t expected_crc = crc32c::Unmask(DecodeFixed32(header));
246*9507f98cSAndroid Build Coastguard Worker uint32_t actual_crc = crc32c::Value(header + 6, 1 + length);
247*9507f98cSAndroid Build Coastguard Worker if (actual_crc != expected_crc) {
248*9507f98cSAndroid Build Coastguard Worker // Drop the rest of the buffer since "length" itself may have
249*9507f98cSAndroid Build Coastguard Worker // been corrupted and if we trust it, we could find some
250*9507f98cSAndroid Build Coastguard Worker // fragment of a real log record that just happens to look
251*9507f98cSAndroid Build Coastguard Worker // like a valid log record.
252*9507f98cSAndroid Build Coastguard Worker size_t drop_size = buffer_.size();
253*9507f98cSAndroid Build Coastguard Worker buffer_.clear();
254*9507f98cSAndroid Build Coastguard Worker ReportCorruption(drop_size, "checksum mismatch");
255*9507f98cSAndroid Build Coastguard Worker return kBadRecord;
256*9507f98cSAndroid Build Coastguard Worker }
257*9507f98cSAndroid Build Coastguard Worker }
258*9507f98cSAndroid Build Coastguard Worker
259*9507f98cSAndroid Build Coastguard Worker buffer_.remove_prefix(kHeaderSize + length);
260*9507f98cSAndroid Build Coastguard Worker
261*9507f98cSAndroid Build Coastguard Worker // Skip physical record that started before initial_offset_
262*9507f98cSAndroid Build Coastguard Worker if (end_of_buffer_offset_ - buffer_.size() - kHeaderSize - length <
263*9507f98cSAndroid Build Coastguard Worker initial_offset_) {
264*9507f98cSAndroid Build Coastguard Worker result->clear();
265*9507f98cSAndroid Build Coastguard Worker return kBadRecord;
266*9507f98cSAndroid Build Coastguard Worker }
267*9507f98cSAndroid Build Coastguard Worker
268*9507f98cSAndroid Build Coastguard Worker *result = Slice(header + kHeaderSize, length);
269*9507f98cSAndroid Build Coastguard Worker return type;
270*9507f98cSAndroid Build Coastguard Worker }
271*9507f98cSAndroid Build Coastguard Worker }
272*9507f98cSAndroid Build Coastguard Worker
273*9507f98cSAndroid Build Coastguard Worker } // namespace log
274*9507f98cSAndroid Build Coastguard Worker } // namespace leveldb
275