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).
|
2014-04-15 20:39:26 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2013-08-06 19:54:37 +00:00
|
|
|
#include "db/transaction_log_impl.h"
|
2022-04-15 00:12:16 +00:00
|
|
|
|
2019-06-06 20:52:39 +00:00
|
|
|
#include <cinttypes>
|
2022-04-15 00:12:16 +00:00
|
|
|
|
2012-11-30 01:28:37 +00:00
|
|
|
#include "db/write_batch_internal.h"
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/sequence_file_reader.h"
|
2022-04-15 00:12:16 +00:00
|
|
|
#include "util/defer.h"
|
2013-03-21 22:12:35 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2012-11-30 01:28:37 +00:00
|
|
|
|
|
|
|
TransactionLogIteratorImpl::TransactionLogIteratorImpl(
|
2016-09-23 23:34:04 +00:00
|
|
|
const std::string& dir, const ImmutableDBOptions* options,
|
2014-02-28 19:50:36 +00:00
|
|
|
const TransactionLogIterator::ReadOptions& read_options,
|
2014-02-05 21:12:23 +00:00
|
|
|
const EnvOptions& soptions, const SequenceNumber seq,
|
2017-11-11 01:18:01 +00:00
|
|
|
std::unique_ptr<VectorLogPtr> files, VersionSet const* const versions,
|
2020-08-13 00:28:10 +00:00
|
|
|
const bool seq_per_batch, const std::shared_ptr<IOTracer>& io_tracer)
|
2014-02-05 21:12:23 +00:00
|
|
|
: dir_(dir),
|
|
|
|
options_(options),
|
2014-02-28 19:50:36 +00:00
|
|
|
read_options_(read_options),
|
2014-02-05 21:12:23 +00:00
|
|
|
soptions_(soptions),
|
2019-03-27 19:20:43 +00:00
|
|
|
starting_sequence_number_(seq),
|
2014-02-05 21:12:23 +00:00
|
|
|
files_(std::move(files)),
|
2022-04-15 00:12:16 +00:00
|
|
|
versions_(versions),
|
|
|
|
seq_per_batch_(seq_per_batch),
|
|
|
|
io_tracer_(io_tracer),
|
2014-02-05 21:12:23 +00:00
|
|
|
started_(false),
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_(false),
|
|
|
|
current_file_index_(0),
|
|
|
|
current_batch_seq_(0),
|
2022-04-15 00:12:16 +00:00
|
|
|
current_last_seq_(0) {
|
2013-10-13 22:28:24 +00:00
|
|
|
assert(files_ != nullptr);
|
2014-10-30 00:43:37 +00:00
|
|
|
assert(versions_ != nullptr);
|
2022-04-15 00:12:16 +00:00
|
|
|
assert(!seq_per_batch_);
|
2020-12-23 07:44:44 +00:00
|
|
|
current_status_.PermitUncheckedError(); // Clear on start
|
2013-04-29 20:19:24 +00:00
|
|
|
reporter_.env = options_->env;
|
|
|
|
reporter_.info_log = options_->info_log.get();
|
2013-10-13 22:28:24 +00:00
|
|
|
SeekToStartSequence(); // Seek till starting sequence
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 10:07:13 +00:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogFile(
|
2019-03-27 19:20:43 +00:00
|
|
|
const LogFile* log_file,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFileReader>* file_reader) {
|
2020-08-13 00:28:10 +00:00
|
|
|
FileSystemPtr fs(options_->fs, io_tracer_);
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
std::unique_ptr<FSSequentialFile> file;
|
2018-06-21 15:34:24 +00:00
|
|
|
std::string fname;
|
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
|
|
|
Status s;
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
EnvOptions optimized_env_options = fs->OptimizeForLogRead(soptions_);
|
2019-03-27 19:20:43 +00:00
|
|
|
if (log_file->Type() == kArchivedLogFile) {
|
|
|
|
fname = ArchivedLogFileName(dir_, log_file->LogNumber());
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
s = fs->NewSequentialFile(fname, optimized_env_options, &file, nullptr);
|
2012-11-30 01:28:37 +00:00
|
|
|
} else {
|
2019-03-27 19:20:43 +00:00
|
|
|
fname = LogFileName(dir_, log_file->LogNumber());
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
s = fs->NewSequentialFile(fname, optimized_env_options, &file, nullptr);
|
2014-11-06 19:14:28 +00:00
|
|
|
if (!s.ok()) {
|
2012-11-30 01:28:37 +00:00
|
|
|
// If cannot open file in DB directory.
|
|
|
|
// Try the archive dir, as it could have moved in the meanwhile.
|
2019-03-27 19:20:43 +00:00
|
|
|
fname = ArchivedLogFileName(dir_, log_file->LogNumber());
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
s = fs->NewSequentialFile(fname, optimized_env_options,
|
|
|
|
&file, nullptr);
|
2012-11-30 01:28:37 +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
|
|
|
if (s.ok()) {
|
2022-05-24 17:28:57 +00:00
|
|
|
file_reader->reset(new SequentialFileReader(std::move(file), fname,
|
|
|
|
io_tracer_, options_->listeners,
|
|
|
|
options_->rate_limiter.get()));
|
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
|
|
|
}
|
|
|
|
return s;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 18:44:04 +00:00
|
|
|
BatchResult TransactionLogIteratorImpl::GetBatch() {
|
2019-03-27 19:20:43 +00:00
|
|
|
assert(is_valid_); // cannot call in a non valid state.
|
2013-03-04 18:44:04 +00:00
|
|
|
BatchResult result;
|
2019-03-27 19:20:43 +00:00
|
|
|
result.sequence = current_batch_seq_;
|
|
|
|
result.writeBatchPtr = std::move(current_batch_);
|
2013-03-04 18:44:04 +00:00
|
|
|
return result;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
Status TransactionLogIteratorImpl::status() { return current_status_; }
|
2012-11-30 01:28:37 +00:00
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
bool TransactionLogIteratorImpl::Valid() { return started_ && is_valid_; }
|
2012-11-30 01:28:37 +00:00
|
|
|
|
2019-08-26 18:24:40 +00:00
|
|
|
bool TransactionLogIteratorImpl::RestrictedRead(Slice* record) {
|
2013-10-21 02:06:19 +00:00
|
|
|
// Don't read if no more complete entries to read from logs
|
2019-03-27 19:20:43 +00:00
|
|
|
if (current_last_seq_ >= versions_->LastSequence()) {
|
2013-10-21 02:06:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-26 18:24:40 +00:00
|
|
|
return current_log_reader_->ReadRecord(record, &scratch_);
|
2013-10-21 02:06:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
void TransactionLogIteratorImpl::SeekToStartSequence(uint64_t start_file_index,
|
|
|
|
bool strict) {
|
2013-10-21 02:06:19 +00:00
|
|
|
Slice record;
|
|
|
|
started_ = false;
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_ = false;
|
2022-04-15 00:12:16 +00:00
|
|
|
// Check invariant of TransactionLogIterator when SeekToStartSequence()
|
|
|
|
// succeeds.
|
|
|
|
const Defer defer([this]() {
|
|
|
|
if (is_valid_) {
|
|
|
|
assert(current_status_.ok());
|
|
|
|
if (starting_sequence_number_ > current_batch_seq_) {
|
|
|
|
assert(current_batch_seq_ < current_last_seq_);
|
|
|
|
assert(current_last_seq_ >= starting_sequence_number_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-03-27 19:20:43 +00:00
|
|
|
if (files_->size() <= start_file_index) {
|
2013-10-21 02:06:19 +00:00
|
|
|
return;
|
2022-04-15 00:12:16 +00:00
|
|
|
} else if (!current_status_.ok()) {
|
|
|
|
return;
|
2013-10-21 02:06:19 +00:00
|
|
|
}
|
2019-03-27 19:20:43 +00:00
|
|
|
Status s =
|
|
|
|
OpenLogReader(files_->at(static_cast<size_t>(start_file_index)).get());
|
2013-10-21 02:06:19 +00:00
|
|
|
if (!s.ok()) {
|
2019-03-27 19:20:43 +00:00
|
|
|
current_status_ = s;
|
|
|
|
reporter_.Info(current_status_.ToString().c_str());
|
2013-10-21 02:06:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-26 18:24:40 +00:00
|
|
|
while (RestrictedRead(&record)) {
|
2016-03-30 17:35:22 +00:00
|
|
|
if (record.size() < WriteBatchInternal::kHeader) {
|
2013-10-21 02:06:19 +00:00
|
|
|
reporter_.Corruption(
|
|
|
|
record.size(), Status::Corruption("very small log record"));
|
|
|
|
continue;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
2013-10-21 02:06:19 +00:00
|
|
|
UpdateCurrentWriteBatch(record);
|
2019-03-27 19:20:43 +00:00
|
|
|
if (current_last_seq_ >= starting_sequence_number_) {
|
|
|
|
if (strict && current_batch_seq_ != starting_sequence_number_) {
|
|
|
|
current_status_ = Status::Corruption(
|
|
|
|
"Gap in sequence number. Could not "
|
|
|
|
"seek to required sequence number");
|
|
|
|
reporter_.Info(current_status_.ToString().c_str());
|
2013-10-13 22:28:24 +00:00
|
|
|
return;
|
2013-10-21 02:06:19 +00:00
|
|
|
} else if (strict) {
|
|
|
|
reporter_.Info("Could seek required sequence number. Iterator will "
|
|
|
|
"continue.");
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_ = true;
|
2013-10-21 02:06:19 +00:00
|
|
|
started_ = true; // set started_ as we could seek till starting sequence
|
|
|
|
return;
|
|
|
|
} else {
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_ = false;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
2013-10-21 02:06:19 +00:00
|
|
|
}
|
2013-10-25 02:09:02 +00:00
|
|
|
|
2013-10-21 02:06:19 +00:00
|
|
|
// Could not find start sequence in first file. Normally this must be the
|
|
|
|
// only file. Otherwise log the error and let the iterator return next entry
|
2013-10-25 02:09:02 +00:00
|
|
|
// If strict is set, we want to seek exactly till the start sequence and it
|
|
|
|
// should have been present in the file we scanned above
|
|
|
|
if (strict) {
|
2019-03-27 19:20:43 +00:00
|
|
|
current_status_ = Status::Corruption(
|
|
|
|
"Gap in sequence number. Could not "
|
|
|
|
"seek to required sequence number");
|
|
|
|
reporter_.Info(current_status_.ToString().c_str());
|
2013-10-25 02:09:02 +00:00
|
|
|
} else if (files_->size() != 1) {
|
2019-03-27 19:20:43 +00:00
|
|
|
current_status_ = Status::Corruption(
|
|
|
|
"Start sequence was not found, "
|
|
|
|
"skipping to the next available");
|
|
|
|
reporter_.Info(current_status_.ToString().c_str());
|
2013-10-25 02:09:02 +00:00
|
|
|
// Let NextImpl find the next available entry. started_ remains false
|
|
|
|
// because we don't want to check for gaps while moving to start sequence
|
|
|
|
NextImpl(true);
|
2013-10-21 02:06:19 +00:00
|
|
|
}
|
2013-10-13 22:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionLogIteratorImpl::Next() {
|
2022-04-15 00:12:16 +00:00
|
|
|
if (!current_status_.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 02:09:02 +00:00
|
|
|
return NextImpl(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionLogIteratorImpl::NextImpl(bool internal) {
|
2013-10-13 22:28:24 +00:00
|
|
|
Slice record;
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_ = false;
|
2013-10-25 02:09:02 +00:00
|
|
|
if (!internal && !started_) {
|
|
|
|
// Runs every time until we can seek to the start sequence
|
2022-04-15 00:12:16 +00:00
|
|
|
SeekToStartSequence();
|
2013-04-08 23:28:09 +00:00
|
|
|
}
|
2013-10-13 22:28:24 +00:00
|
|
|
while(true) {
|
2019-03-27 19:20:43 +00:00
|
|
|
assert(current_log_reader_);
|
|
|
|
if (current_log_reader_->IsEOF()) {
|
|
|
|
current_log_reader_->UnmarkEOF();
|
2013-10-25 02:09:02 +00:00
|
|
|
}
|
2019-08-26 18:24:40 +00:00
|
|
|
while (RestrictedRead(&record)) {
|
2016-03-30 17:35:22 +00:00
|
|
|
if (record.size() < WriteBatchInternal::kHeader) {
|
2013-10-25 02:09:02 +00:00
|
|
|
reporter_.Corruption(
|
|
|
|
record.size(), Status::Corruption("very small log record"));
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// started_ should be true if called by application
|
|
|
|
assert(internal || started_);
|
|
|
|
// started_ should be false if called internally
|
|
|
|
assert(!internal || !started_);
|
|
|
|
UpdateCurrentWriteBatch(record);
|
|
|
|
if (internal && !started_) {
|
|
|
|
started_ = true;
|
2013-03-21 22:12:35 +00:00
|
|
|
}
|
2013-10-25 02:09:02 +00:00
|
|
|
return;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 22:28:24 +00:00
|
|
|
// Open the next file
|
2019-03-27 19:20:43 +00:00
|
|
|
if (current_file_index_ < files_->size() - 1) {
|
|
|
|
++current_file_index_;
|
|
|
|
Status s = OpenLogReader(files_->at(current_file_index_).get());
|
2014-11-06 19:14:28 +00:00
|
|
|
if (!s.ok()) {
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_ = false;
|
|
|
|
current_status_ = s;
|
2013-10-13 22:28:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-27 19:20:43 +00:00
|
|
|
is_valid_ = false;
|
|
|
|
if (current_last_seq_ == versions_->LastSequence()) {
|
|
|
|
current_status_ = Status::OK();
|
2013-10-13 22:28:24 +00:00
|
|
|
} else {
|
2019-06-19 15:02:21 +00:00
|
|
|
const char* msg = "Create a new iterator to fetch the new tail.";
|
|
|
|
current_status_ = Status::TryAgain(msg);
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
2013-10-13 22:28:24 +00:00
|
|
|
return;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 02:09:02 +00:00
|
|
|
bool TransactionLogIteratorImpl::IsBatchExpected(
|
2019-03-27 19:20:43 +00:00
|
|
|
const WriteBatch* batch, const SequenceNumber expected_seq) {
|
2013-10-21 02:06:19 +00:00
|
|
|
assert(batch);
|
|
|
|
SequenceNumber batchSeq = WriteBatchInternal::Sequence(batch);
|
2019-03-27 19:20:43 +00:00
|
|
|
if (batchSeq != expected_seq) {
|
2013-10-21 02:06:19 +00:00
|
|
|
char buf[200];
|
|
|
|
snprintf(buf, sizeof(buf),
|
2014-10-30 00:43:37 +00:00
|
|
|
"Discontinuity in log records. Got seq=%" PRIu64
|
|
|
|
", Expected seq=%" PRIu64 ", Last flushed seq=%" PRIu64
|
|
|
|
".Log iterator will reseek the correct batch.",
|
2019-03-27 19:20:43 +00:00
|
|
|
batchSeq, expected_seq, versions_->LastSequence());
|
2013-10-21 02:06:19 +00:00
|
|
|
reporter_.Info(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-04 18:44:04 +00:00
|
|
|
void TransactionLogIteratorImpl::UpdateCurrentWriteBatch(const Slice& record) {
|
2013-11-07 23:46:48 +00:00
|
|
|
std::unique_ptr<WriteBatch> batch(new WriteBatch());
|
2020-12-23 07:44:44 +00:00
|
|
|
Status s = WriteBatchInternal::SetContents(batch.get(), record);
|
|
|
|
s.PermitUncheckedError(); // TODO: What should we do with this error?
|
2013-10-21 02:06:19 +00:00
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
SequenceNumber expected_seq = current_last_seq_ + 1;
|
2013-10-25 02:09:02 +00:00
|
|
|
// If the iterator has started, then confirm that we get continuous batches
|
2019-03-27 19:20:43 +00:00
|
|
|
if (started_ && !IsBatchExpected(batch.get(), expected_seq)) {
|
2013-10-21 02:06:19 +00:00
|
|
|
// Seek to the batch having expected sequence number
|
2019-03-27 19:20:43 +00:00
|
|
|
if (expected_seq < files_->at(current_file_index_)->StartSequence()) {
|
2013-10-21 02:06:19 +00:00
|
|
|
// Expected batch must lie in the previous log file
|
2013-11-13 04:05:28 +00:00
|
|
|
// Avoid underflow.
|
2019-03-27 19:20:43 +00:00
|
|
|
if (current_file_index_ != 0) {
|
|
|
|
current_file_index_--;
|
2013-11-13 04:05:28 +00:00
|
|
|
}
|
2013-10-21 02:06:19 +00:00
|
|
|
}
|
2019-03-27 19:20:43 +00:00
|
|
|
starting_sequence_number_ = expected_seq;
|
2013-10-25 02:09:02 +00:00
|
|
|
// currentStatus_ will be set to Ok if reseek succeeds
|
2017-11-11 01:18:01 +00:00
|
|
|
// Note: this is still ok in seq_pre_batch_ && two_write_queuesp_ mode
|
2017-10-23 21:20:22 +00:00
|
|
|
// that allows gaps in the WAL since it will still skip over the gap.
|
2019-03-27 19:20:43 +00:00
|
|
|
current_status_ = Status::NotFound("Gap in sequence numbers");
|
2017-11-11 01:18:01 +00:00
|
|
|
// In seq_per_batch_ mode, gaps in the seq are possible so the strict mode
|
2017-10-23 21:20:22 +00:00
|
|
|
// should be disabled
|
2019-03-27 19:20:43 +00:00
|
|
|
return SeekToStartSequence(current_file_index_, !seq_per_batch_);
|
2013-10-21 02:06:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
current_batch_seq_ = WriteBatchInternal::Sequence(batch.get());
|
2022-04-15 00:12:16 +00:00
|
|
|
assert(!seq_per_batch_);
|
|
|
|
current_last_seq_ =
|
|
|
|
current_batch_seq_ + WriteBatchInternal::Count(batch.get()) - 1;
|
2013-10-21 02:06:19 +00:00
|
|
|
// currentBatchSeq_ can only change here
|
2019-03-27 19:20:43 +00:00
|
|
|
assert(current_last_seq_ <= versions_->LastSequence());
|
2013-10-21 02:06:19 +00:00
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
current_batch_ = std::move(batch);
|
|
|
|
is_valid_ = true;
|
|
|
|
current_status_ = Status::OK();
|
2013-03-04 18:44:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:20:43 +00:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogReader(const LogFile* log_file) {
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFileReader> file;
|
2019-03-27 19:20:43 +00:00
|
|
|
Status s = OpenLogFile(log_file, &file);
|
2014-11-06 19:14:28 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
2013-04-08 23:28:09 +00:00
|
|
|
}
|
|
|
|
assert(file);
|
2019-03-27 19:20:43 +00:00
|
|
|
current_log_reader_.reset(
|
2018-09-14 00:08:04 +00:00
|
|
|
new log::Reader(options_->info_log, std::move(file), &reporter_,
|
2019-03-27 19:20:43 +00:00
|
|
|
read_options_.verify_checksums_, log_file->LogNumber()));
|
2013-04-08 23:28:09 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-04-15 20:39:26 +00:00
|
|
|
#endif // ROCKSDB_LITE
|