2013-10-16 21:59:46 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2013-01-20 10:07:13 +00:00
|
|
|
#include <memory>
|
2011-05-21 02:17:43 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/log_format.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/status.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
class SequentialFile;
|
2013-01-20 10:07:13 +00:00
|
|
|
using std::unique_ptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace log {
|
|
|
|
|
|
|
|
class Reader {
|
|
|
|
public:
|
|
|
|
// Interface for reporting errors.
|
|
|
|
class Reporter {
|
|
|
|
public:
|
|
|
|
virtual ~Reporter();
|
|
|
|
|
|
|
|
// Some corruption was detected. "size" is the approximate number
|
|
|
|
// of bytes dropped due to the corruption.
|
|
|
|
virtual void Corruption(size_t bytes, const Status& status) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a reader that will return log records from "*file".
|
|
|
|
// "*file" must remain live while this Reader is in use.
|
|
|
|
//
|
2013-03-01 02:04:58 +00:00
|
|
|
// If "reporter" is non-nullptr, it is notified whenever some data is
|
2011-03-18 22:37:00 +00:00
|
|
|
// dropped due to a detected corruption. "*reporter" must remain
|
|
|
|
// live while this Reader is in use.
|
|
|
|
//
|
|
|
|
// If "checksum" is true, verify checksums if available.
|
2011-05-21 02:17:43 +00:00
|
|
|
//
|
|
|
|
// The Reader will start reading at the first record located at physical
|
|
|
|
// position >= initial_offset within the file.
|
2013-01-20 10:07:13 +00:00
|
|
|
Reader(unique_ptr<SequentialFile>&& file, Reporter* reporter,
|
|
|
|
bool checksum, uint64_t initial_offset);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
~Reader();
|
|
|
|
|
|
|
|
// Read the next record into *record. Returns true if read
|
|
|
|
// successfully, false if we hit end of the input. May use
|
|
|
|
// "*scratch" as temporary storage. The contents filled in *record
|
|
|
|
// will only be valid until the next mutating operation on this
|
|
|
|
// reader or the next mutation to *scratch.
|
|
|
|
bool ReadRecord(Slice* record, std::string* scratch);
|
|
|
|
|
2011-05-21 02:17:43 +00:00
|
|
|
// Returns the physical offset of the last record returned by ReadRecord.
|
|
|
|
//
|
|
|
|
// Undefined before the first call to ReadRecord.
|
|
|
|
uint64_t LastRecordOffset();
|
|
|
|
|
2013-03-21 22:12:35 +00:00
|
|
|
// returns true if the reader has encountered an eof condition.
|
|
|
|
bool IsEOF() {
|
|
|
|
return eof_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when we know more data has been written to the file. we can use this
|
|
|
|
// function to force the reader to look again in the file.
|
Fix UnmarkEOF for partial blocks
Summary:
Blocks in the transaction log are a fixed size, but the last block in the transaction log file is usually a partial block. When a new record is added after the reader hit the end of the file, a new physical record will be appended to the last block. ReadPhysicalRecord can only read full blocks and assumes that the file position indicator is aligned to the start of a block. If the reader is forced to read further by simply clearing the EOF flag, ReadPhysicalRecord will read a full block starting from somewhere in the middle of a real block, causing it to lose alignment and to have a partial physical record at the end of the read buffer. This will result in length mismatches and checksum failures. When the log file is tailed for replication this will cause the log iterator to become invalid, necessitating the creation of a new iterator which will have to read the log file from scratch.
This diff fixes this issue by reading the remaining portion of the last block we read from. This is done when the reader is forced to read further (UnmarkEOF is called).
Test Plan:
- Added unit tests
- Stress test (with replication). Check dbdir/LOG file for corruptions.
- Test on test tier
Reviewers: emayanke, haobo, dhruba
Reviewed By: haobo
CC: vamsi, sheki, dhruba, kailiu, igor
Differential Revision: https://reviews.facebook.net/D15249
2014-01-27 22:49:10 +00:00
|
|
|
// Also aligns the file position indicator to the start of the next block
|
|
|
|
// by reading the rest of the data from the EOF position to the end of the
|
|
|
|
// block that was partially read.
|
|
|
|
void UnmarkEOF();
|
2013-03-21 22:12:35 +00:00
|
|
|
|
2013-01-20 10:07:13 +00:00
|
|
|
SequentialFile* file() { return file_.get(); }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
2013-01-20 10:07:13 +00:00
|
|
|
const unique_ptr<SequentialFile> file_;
|
2011-03-18 22:37:00 +00:00
|
|
|
Reporter* const reporter_;
|
|
|
|
bool const checksum_;
|
|
|
|
char* const backing_store_;
|
|
|
|
Slice buffer_;
|
|
|
|
bool eof_; // Last Read() indicated EOF by returning < kBlockSize
|
Fix UnmarkEOF for partial blocks
Summary:
Blocks in the transaction log are a fixed size, but the last block in the transaction log file is usually a partial block. When a new record is added after the reader hit the end of the file, a new physical record will be appended to the last block. ReadPhysicalRecord can only read full blocks and assumes that the file position indicator is aligned to the start of a block. If the reader is forced to read further by simply clearing the EOF flag, ReadPhysicalRecord will read a full block starting from somewhere in the middle of a real block, causing it to lose alignment and to have a partial physical record at the end of the read buffer. This will result in length mismatches and checksum failures. When the log file is tailed for replication this will cause the log iterator to become invalid, necessitating the creation of a new iterator which will have to read the log file from scratch.
This diff fixes this issue by reading the remaining portion of the last block we read from. This is done when the reader is forced to read further (UnmarkEOF is called).
Test Plan:
- Added unit tests
- Stress test (with replication). Check dbdir/LOG file for corruptions.
- Test on test tier
Reviewers: emayanke, haobo, dhruba
Reviewed By: haobo
CC: vamsi, sheki, dhruba, kailiu, igor
Differential Revision: https://reviews.facebook.net/D15249
2014-01-27 22:49:10 +00:00
|
|
|
bool read_error_; // Error occurred while reading from file
|
|
|
|
|
|
|
|
// Offset of the file position indicator within the last block when an
|
|
|
|
// EOF was detected.
|
|
|
|
size_t eof_offset_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-05-21 02:17:43 +00:00
|
|
|
// Offset of the last record returned by ReadRecord.
|
|
|
|
uint64_t last_record_offset_;
|
|
|
|
// Offset of the first location past the end of buffer_.
|
|
|
|
uint64_t end_of_buffer_offset_;
|
|
|
|
|
|
|
|
// Offset at which to start looking for the first record to return
|
|
|
|
uint64_t const initial_offset_;
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// Extend record types with the following special values
|
|
|
|
enum {
|
|
|
|
kEof = kMaxRecordType + 1,
|
2011-05-21 02:17:43 +00:00
|
|
|
// Returned whenever we find an invalid physical record.
|
|
|
|
// Currently there are three situations in which this happens:
|
|
|
|
// * The record has an invalid CRC (ReadPhysicalRecord reports a drop)
|
|
|
|
// * The record is a 0-length record (No drop is reported)
|
|
|
|
// * The record is below constructor's initial_offset (No drop is reported)
|
2011-03-18 22:37:00 +00:00
|
|
|
kBadRecord = kMaxRecordType + 2
|
|
|
|
};
|
|
|
|
|
2011-05-21 02:17:43 +00:00
|
|
|
// Skips all blocks that are completely before "initial_offset_".
|
|
|
|
//
|
|
|
|
// Returns true on success. Handles reporting.
|
|
|
|
bool SkipToInitialBlock();
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// Return type, or one of the preceding special values
|
|
|
|
unsigned int ReadPhysicalRecord(Slice* result);
|
2011-05-21 02:17:43 +00:00
|
|
|
|
|
|
|
// Reports dropped bytes to the reporter.
|
|
|
|
// buffer_ must be updated to remove the dropped bytes prior to invocation.
|
|
|
|
void ReportCorruption(size_t bytes, const char* reason);
|
|
|
|
void ReportDrop(size_t bytes, const Status& reason);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
Reader(const Reader&);
|
|
|
|
void operator=(const Reader&);
|
|
|
|
};
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace log
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|