2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
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
|
2011-05-21 02:17:43 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-02-18 00:18:01 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/log_format.h"
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/sequence_file_reader.h"
|
|
|
|
#include "rocksdb/options.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/status.h"
|
2022-02-18 00:18:01 +00:00
|
|
|
#include "util/compression.h"
|
2022-07-05 22:44:35 +00:00
|
|
|
#include "util/xxhash.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-10-08 17:06:16 +00:00
|
|
|
class Logger;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace log {
|
|
|
|
|
2015-04-07 01:15:00 +00:00
|
|
|
/**
|
|
|
|
* Reader is a general purpose log stream reader implementation. The actual job
|
|
|
|
* of reading from the device is implemented by the SequentialFile interface.
|
|
|
|
*
|
|
|
|
* Please see Writer for details on the file and record layout.
|
|
|
|
*/
|
2011-03-18 22:37:00 +00:00
|
|
|
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.
|
2015-10-08 17:06:16 +00:00
|
|
|
Reader(std::shared_ptr<Logger> info_log,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFileReader>&& file, Reporter* reporter,
|
2019-03-26 23:41:31 +00:00
|
|
|
bool checksum, uint64_t log_num);
|
2019-09-12 01:07:12 +00:00
|
|
|
// No copying allowed
|
|
|
|
Reader(const Reader&) = delete;
|
|
|
|
void operator=(const Reader&) = delete;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-03-26 23:41:31 +00:00
|
|
|
virtual ~Reader();
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Read the next record into *record. Returns true if read
|
|
|
|
// successfully, false if we hit end of the input. May use
|
2022-07-05 22:44:35 +00:00
|
|
|
// "*scratch" as temporary storage. The contents filled in *record
|
2011-03-18 22:37:00 +00:00
|
|
|
// will only be valid until the next mutating operation on this
|
|
|
|
// reader or the next mutation to *scratch.
|
2022-07-05 22:44:35 +00:00
|
|
|
// If record_checksum is not nullptr, then this function will calculate the
|
|
|
|
// checksum of the record read and set record_checksum to it. The checksum is
|
|
|
|
// calculated from the original buffers that contain the contents of the
|
|
|
|
// record.
|
2019-03-26 23:41:31 +00:00
|
|
|
virtual bool ReadRecord(Slice* record, std::string* scratch,
|
|
|
|
WALRecoveryMode wal_recovery_mode =
|
2022-07-05 22:44:35 +00:00
|
|
|
WALRecoveryMode::kTolerateCorruptedTailRecords,
|
|
|
|
uint64_t* record_checksum = nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
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();
|
|
|
|
|
2020-08-26 17:37:59 +00:00
|
|
|
// Returns the first physical offset after the last record returned by
|
|
|
|
// ReadRecord, or zero before first call to ReadRecord. This can also be
|
|
|
|
// thought of as the "current" position in processing the file bytes.
|
|
|
|
uint64_t LastRecordEnd();
|
|
|
|
|
2013-03-21 22:12:35 +00:00
|
|
|
// returns true if the reader has encountered an eof condition.
|
|
|
|
bool IsEOF() {
|
|
|
|
return eof_;
|
|
|
|
}
|
|
|
|
|
2019-03-26 23:41:31 +00:00
|
|
|
// returns true if the reader has encountered read error.
|
|
|
|
bool hasReadError() const { return read_error_; }
|
|
|
|
|
2013-03-21 22:12:35 +00:00
|
|
|
// 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.
|
2019-03-26 23:41:31 +00:00
|
|
|
virtual void UnmarkEOF();
|
2013-03-21 22:12:35 +00:00
|
|
|
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
SequentialFileReader* file() { return file_.get(); }
|
2013-01-20 10:07:13 +00:00
|
|
|
|
2019-03-26 23:41:31 +00:00
|
|
|
Reporter* GetReporter() const { return reporter_; }
|
|
|
|
|
2019-04-24 19:05:29 +00:00
|
|
|
uint64_t GetLogNumber() const { return log_number_; }
|
|
|
|
|
2020-02-04 19:37:03 +00:00
|
|
|
size_t GetReadOffset() const {
|
|
|
|
return static_cast<size_t>(end_of_buffer_offset_);
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:16:43 +00:00
|
|
|
bool IsCompressedAndEmptyFile() {
|
|
|
|
return !first_record_read_ && compression_type_record_read_;
|
|
|
|
}
|
|
|
|
|
2019-03-26 23:41:31 +00:00
|
|
|
protected:
|
2015-10-08 17:06:16 +00:00
|
|
|
std::shared_ptr<Logger> info_log_;
|
2018-11-09 19:17:34 +00:00
|
|
|
const std::unique_ptr<SequentialFileReader> file_;
|
2011-03-18 22:37:00 +00:00
|
|
|
Reporter* const reporter_;
|
|
|
|
bool const checksum_;
|
|
|
|
char* const backing_store_;
|
2019-03-26 23:41:31 +00:00
|
|
|
|
|
|
|
// Internal state variables used for reading records
|
2011-03-18 22:37:00 +00:00
|
|
|
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_;
|
|
|
|
|
2015-10-08 17:06:16 +00:00
|
|
|
// which log number this is
|
|
|
|
uint64_t const log_number_;
|
|
|
|
|
2015-12-11 22:17:43 +00:00
|
|
|
// Whether this is a recycled log file
|
|
|
|
bool recycled_;
|
|
|
|
|
2022-02-18 00:18:01 +00:00
|
|
|
// Whether the first record has been read or not.
|
|
|
|
bool first_record_read_;
|
|
|
|
// Type of compression used
|
|
|
|
CompressionType compression_type_;
|
|
|
|
// Track whether the compression type record has been read or not.
|
|
|
|
bool compression_type_record_read_;
|
2022-03-09 23:49:53 +00:00
|
|
|
StreamingUncompress* uncompress_;
|
|
|
|
// Reusable uncompressed output buffer
|
|
|
|
std::unique_ptr<char[]> uncompressed_buffer_;
|
|
|
|
// Reusable uncompressed record
|
|
|
|
std::string uncompressed_record_;
|
2022-07-25 23:27:26 +00:00
|
|
|
// Used for stream hashing fragment content in ReadRecord()
|
2022-07-05 22:44:35 +00:00
|
|
|
XXH3_state_t* hash_state_;
|
2022-07-25 23:27:26 +00:00
|
|
|
// Used for stream hashing uncompressed buffer in ReadPhysicalRecord()
|
|
|
|
XXH3_state_t* uncompress_hash_state_;
|
2022-02-18 00:18:01 +00:00
|
|
|
|
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)
|
2015-10-12 21:04:33 +00:00
|
|
|
kBadRecord = kMaxRecordType + 2,
|
|
|
|
// Returned when we fail to read a valid header.
|
|
|
|
kBadHeader = kMaxRecordType + 3,
|
2015-10-19 21:24:05 +00:00
|
|
|
// Returned when we read an old record from a previous user of the log.
|
|
|
|
kOldRecord = kMaxRecordType + 4,
|
2015-12-02 21:35:20 +00:00
|
|
|
// Returned when we get a bad record length
|
|
|
|
kBadRecordLen = kMaxRecordType + 5,
|
|
|
|
// Returned when we get a bad record checksum
|
|
|
|
kBadRecordChecksum = kMaxRecordType + 6,
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Return type, or one of the preceding special values
|
2022-07-25 23:27:26 +00:00
|
|
|
// If WAL compressioned is enabled, fragment_checksum is the checksum of the
|
|
|
|
// fragment computed from the orginal buffer containinng uncompressed
|
|
|
|
// fragment.
|
|
|
|
unsigned int ReadPhysicalRecord(Slice* result, size_t* drop_size,
|
|
|
|
uint64_t* fragment_checksum = nullptr);
|
2011-05-21 02:17:43 +00:00
|
|
|
|
2015-10-19 21:24:05 +00:00
|
|
|
// Read some more
|
|
|
|
bool ReadMore(size_t* drop_size, int *error);
|
|
|
|
|
2019-03-26 23:41:31 +00:00
|
|
|
void UnmarkEOFInternal();
|
|
|
|
|
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);
|
2022-03-09 23:49:53 +00:00
|
|
|
|
|
|
|
void InitCompression(const CompressionTypeRecord& compression_record);
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 23:41:31 +00:00
|
|
|
class FragmentBufferedReader : public Reader {
|
|
|
|
public:
|
|
|
|
FragmentBufferedReader(std::shared_ptr<Logger> info_log,
|
|
|
|
std::unique_ptr<SequentialFileReader>&& _file,
|
|
|
|
Reporter* reporter, bool checksum, uint64_t log_num)
|
|
|
|
: Reader(info_log, std::move(_file), reporter, checksum, log_num),
|
|
|
|
fragments_(),
|
|
|
|
in_fragmented_record_(false) {}
|
|
|
|
~FragmentBufferedReader() override {}
|
|
|
|
bool ReadRecord(Slice* record, std::string* scratch,
|
|
|
|
WALRecoveryMode wal_recovery_mode =
|
2022-07-05 22:44:35 +00:00
|
|
|
WALRecoveryMode::kTolerateCorruptedTailRecords,
|
|
|
|
uint64_t* record_checksum = nullptr) override;
|
2019-03-26 23:41:31 +00:00
|
|
|
void UnmarkEOF() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string fragments_;
|
|
|
|
bool in_fragmented_record_;
|
|
|
|
|
|
|
|
bool TryReadFragment(Slice* result, size_t* drop_size,
|
|
|
|
unsigned int* fragment_type_or_err);
|
|
|
|
|
|
|
|
bool TryReadMore(size_t* drop_size, int* error);
|
|
|
|
|
|
|
|
// No copy allowed
|
|
|
|
FragmentBufferedReader(const FragmentBufferedReader&);
|
|
|
|
void operator=(const FragmentBufferedReader&);
|
|
|
|
};
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace log
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|