2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02: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 22:39:26 +02:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2014-10-30 01:43:37 +01:00
|
|
|
|
2013-08-06 21:54:37 +02:00
|
|
|
#include "db/transaction_log_impl.h"
|
2022-04-15 02:12:16 +02:00
|
|
|
|
2019-06-06 22:52:39 +02:00
|
|
|
#include <cinttypes>
|
2022-04-15 02:12:16 +02:00
|
|
|
|
2012-11-30 02:28:37 +01:00
|
|
|
#include "db/write_batch_internal.h"
|
2019-09-16 19:31:27 +02:00
|
|
|
#include "file/sequence_file_reader.h"
|
2022-04-15 02:12:16 +02:00
|
|
|
#include "util/defer.h"
|
2013-03-21 23:12:35 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2012-11-30 02:28:37 +01:00
|
|
|
|
|
|
|
TransactionLogIteratorImpl::TransactionLogIteratorImpl(
|
2016-09-24 01:34:04 +02:00
|
|
|
const std::string& dir, const ImmutableDBOptions* options,
|
2014-02-28 20:50:36 +01:00
|
|
|
const TransactionLogIterator::ReadOptions& read_options,
|
2014-02-05 22:12:23 +01:00
|
|
|
const EnvOptions& soptions, const SequenceNumber seq,
|
2017-11-11 02:18:01 +01:00
|
|
|
std::unique_ptr<VectorLogPtr> files, VersionSet const* const versions,
|
2020-08-13 02:28:10 +02:00
|
|
|
const bool seq_per_batch, const std::shared_ptr<IOTracer>& io_tracer)
|
2014-02-05 22:12:23 +01:00
|
|
|
: dir_(dir),
|
|
|
|
options_(options),
|
2014-02-28 20:50:36 +01:00
|
|
|
read_options_(read_options),
|
2014-02-05 22:12:23 +01:00
|
|
|
soptions_(soptions),
|
2019-03-27 20:20:43 +01:00
|
|
|
starting_sequence_number_(seq),
|
2014-02-05 22:12:23 +01:00
|
|
|
files_(std::move(files)),
|
2022-04-15 02:12:16 +02:00
|
|
|
versions_(versions),
|
|
|
|
seq_per_batch_(seq_per_batch),
|
|
|
|
io_tracer_(io_tracer),
|
2014-02-05 22:12:23 +01:00
|
|
|
started_(false),
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_(false),
|
|
|
|
current_file_index_(0),
|
|
|
|
current_batch_seq_(0),
|
2022-04-15 02:12:16 +02:00
|
|
|
current_last_seq_(0) {
|
2013-10-14 00:28:24 +02:00
|
|
|
assert(files_ != nullptr);
|
2014-10-30 01:43:37 +01:00
|
|
|
assert(versions_ != nullptr);
|
2022-04-15 02:12:16 +02:00
|
|
|
assert(!seq_per_batch_);
|
2020-12-23 08:44:44 +01:00
|
|
|
current_status_.PermitUncheckedError(); // Clear on start
|
2013-04-29 22:19:24 +02:00
|
|
|
reporter_.env = options_->env;
|
|
|
|
reporter_.info_log = options_->info_log.get();
|
2013-10-14 00:28:24 +02:00
|
|
|
SeekToStartSequence(); // Seek till starting sequence
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
|
2013-01-20 11:07:13 +01:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogFile(
|
2019-03-27 20:20:43 +01:00
|
|
|
const LogFile* log_file,
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<SequentialFileReader>* file_reader) {
|
2020-08-13 02:28:10 +02: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 23:47:08 +01:00
|
|
|
std::unique_ptr<FSSequentialFile> file;
|
2018-06-21 17:34:24 +02: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-18 01:16:11 +02: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 23:47:08 +01:00
|
|
|
EnvOptions optimized_env_options = fs->OptimizeForLogRead(soptions_);
|
2019-03-27 20:20:43 +01: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 23:47:08 +01:00
|
|
|
s = fs->NewSequentialFile(fname, optimized_env_options, &file, nullptr);
|
2012-11-30 02:28:37 +01:00
|
|
|
} else {
|
2019-03-27 20:20:43 +01: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 23:47:08 +01:00
|
|
|
s = fs->NewSequentialFile(fname, optimized_env_options, &file, nullptr);
|
2014-11-06 20:14:28 +01:00
|
|
|
if (!s.ok()) {
|
2012-11-30 02:28:37 +01:00
|
|
|
// If cannot open file in DB directory.
|
|
|
|
// Try the archive dir, as it could have moved in the meanwhile.
|
2019-03-27 20:20:43 +01: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 23:47:08 +01:00
|
|
|
s = fs->NewSequentialFile(fname, optimized_env_options,
|
|
|
|
&file, nullptr);
|
2012-11-30 02:28:37 +01: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-18 01:16:11 +02:00
|
|
|
if (s.ok()) {
|
2021-10-05 19:50:27 +02:00
|
|
|
file_reader->reset(new SequentialFileReader(
|
|
|
|
std::move(file), fname, io_tracer_, options_->listeners));
|
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-18 01:16:11 +02:00
|
|
|
}
|
|
|
|
return s;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 19:44:04 +01:00
|
|
|
BatchResult TransactionLogIteratorImpl::GetBatch() {
|
2019-03-27 20:20:43 +01:00
|
|
|
assert(is_valid_); // cannot call in a non valid state.
|
2013-03-04 19:44:04 +01:00
|
|
|
BatchResult result;
|
2019-03-27 20:20:43 +01:00
|
|
|
result.sequence = current_batch_seq_;
|
|
|
|
result.writeBatchPtr = std::move(current_batch_);
|
2013-03-04 19:44:04 +01:00
|
|
|
return result;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
Status TransactionLogIteratorImpl::status() { return current_status_; }
|
2012-11-30 02:28:37 +01:00
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
bool TransactionLogIteratorImpl::Valid() { return started_ && is_valid_; }
|
2012-11-30 02:28:37 +01:00
|
|
|
|
2019-08-26 20:24:40 +02:00
|
|
|
bool TransactionLogIteratorImpl::RestrictedRead(Slice* record) {
|
2013-10-21 04:06:19 +02:00
|
|
|
// Don't read if no more complete entries to read from logs
|
2019-03-27 20:20:43 +01:00
|
|
|
if (current_last_seq_ >= versions_->LastSequence()) {
|
2013-10-21 04:06:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-26 20:24:40 +02:00
|
|
|
return current_log_reader_->ReadRecord(record, &scratch_);
|
2013-10-21 04:06:19 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
void TransactionLogIteratorImpl::SeekToStartSequence(uint64_t start_file_index,
|
|
|
|
bool strict) {
|
2013-10-21 04:06:19 +02:00
|
|
|
Slice record;
|
|
|
|
started_ = false;
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_ = false;
|
2022-04-15 02:12:16 +02: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 20:20:43 +01:00
|
|
|
if (files_->size() <= start_file_index) {
|
2013-10-21 04:06:19 +02:00
|
|
|
return;
|
2022-04-15 02:12:16 +02:00
|
|
|
} else if (!current_status_.ok()) {
|
|
|
|
return;
|
2013-10-21 04:06:19 +02:00
|
|
|
}
|
2019-03-27 20:20:43 +01:00
|
|
|
Status s =
|
|
|
|
OpenLogReader(files_->at(static_cast<size_t>(start_file_index)).get());
|
2013-10-21 04:06:19 +02:00
|
|
|
if (!s.ok()) {
|
2019-03-27 20:20:43 +01:00
|
|
|
current_status_ = s;
|
|
|
|
reporter_.Info(current_status_.ToString().c_str());
|
2013-10-21 04:06:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-08-26 20:24:40 +02:00
|
|
|
while (RestrictedRead(&record)) {
|
2016-03-30 19:35:22 +02:00
|
|
|
if (record.size() < WriteBatchInternal::kHeader) {
|
2013-10-21 04:06:19 +02:00
|
|
|
reporter_.Corruption(
|
|
|
|
record.size(), Status::Corruption("very small log record"));
|
|
|
|
continue;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
2013-10-21 04:06:19 +02:00
|
|
|
UpdateCurrentWriteBatch(record);
|
2019-03-27 20:20:43 +01: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-14 00:28:24 +02:00
|
|
|
return;
|
2013-10-21 04:06:19 +02:00
|
|
|
} else if (strict) {
|
|
|
|
reporter_.Info("Could seek required sequence number. Iterator will "
|
|
|
|
"continue.");
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_ = true;
|
2013-10-21 04:06:19 +02:00
|
|
|
started_ = true; // set started_ as we could seek till starting sequence
|
|
|
|
return;
|
|
|
|
} else {
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_ = false;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
2013-10-21 04:06:19 +02:00
|
|
|
}
|
2013-10-25 04:09:02 +02:00
|
|
|
|
2013-10-21 04:06:19 +02: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 04:09:02 +02: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 20:20:43 +01: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 04:09:02 +02:00
|
|
|
} else if (files_->size() != 1) {
|
2019-03-27 20:20:43 +01: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 04:09:02 +02: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 04:06:19 +02:00
|
|
|
}
|
2013-10-14 00:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionLogIteratorImpl::Next() {
|
2022-04-15 02:12:16 +02:00
|
|
|
if (!current_status_.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 04:09:02 +02:00
|
|
|
return NextImpl(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionLogIteratorImpl::NextImpl(bool internal) {
|
2013-10-14 00:28:24 +02:00
|
|
|
Slice record;
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_ = false;
|
2013-10-25 04:09:02 +02:00
|
|
|
if (!internal && !started_) {
|
|
|
|
// Runs every time until we can seek to the start sequence
|
2022-04-15 02:12:16 +02:00
|
|
|
SeekToStartSequence();
|
2013-04-09 01:28:09 +02:00
|
|
|
}
|
2013-10-14 00:28:24 +02:00
|
|
|
while(true) {
|
2019-03-27 20:20:43 +01:00
|
|
|
assert(current_log_reader_);
|
|
|
|
if (current_log_reader_->IsEOF()) {
|
|
|
|
current_log_reader_->UnmarkEOF();
|
2013-10-25 04:09:02 +02:00
|
|
|
}
|
2019-08-26 20:24:40 +02:00
|
|
|
while (RestrictedRead(&record)) {
|
2016-03-30 19:35:22 +02:00
|
|
|
if (record.size() < WriteBatchInternal::kHeader) {
|
2013-10-25 04:09:02 +02: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 23:12:35 +01:00
|
|
|
}
|
2013-10-25 04:09:02 +02:00
|
|
|
return;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 00:28:24 +02:00
|
|
|
// Open the next file
|
2019-03-27 20:20:43 +01:00
|
|
|
if (current_file_index_ < files_->size() - 1) {
|
|
|
|
++current_file_index_;
|
|
|
|
Status s = OpenLogReader(files_->at(current_file_index_).get());
|
2014-11-06 20:14:28 +01:00
|
|
|
if (!s.ok()) {
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_ = false;
|
|
|
|
current_status_ = s;
|
2013-10-14 00:28:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-27 20:20:43 +01:00
|
|
|
is_valid_ = false;
|
|
|
|
if (current_last_seq_ == versions_->LastSequence()) {
|
|
|
|
current_status_ = Status::OK();
|
2013-10-14 00:28:24 +02:00
|
|
|
} else {
|
2019-06-19 17:02:21 +02:00
|
|
|
const char* msg = "Create a new iterator to fetch the new tail.";
|
|
|
|
current_status_ = Status::TryAgain(msg);
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
2013-10-14 00:28:24 +02:00
|
|
|
return;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 04:09:02 +02:00
|
|
|
bool TransactionLogIteratorImpl::IsBatchExpected(
|
2019-03-27 20:20:43 +01:00
|
|
|
const WriteBatch* batch, const SequenceNumber expected_seq) {
|
2013-10-21 04:06:19 +02:00
|
|
|
assert(batch);
|
|
|
|
SequenceNumber batchSeq = WriteBatchInternal::Sequence(batch);
|
2019-03-27 20:20:43 +01:00
|
|
|
if (batchSeq != expected_seq) {
|
2013-10-21 04:06:19 +02:00
|
|
|
char buf[200];
|
|
|
|
snprintf(buf, sizeof(buf),
|
2014-10-30 01:43:37 +01: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 20:20:43 +01:00
|
|
|
batchSeq, expected_seq, versions_->LastSequence());
|
2013-10-21 04:06:19 +02:00
|
|
|
reporter_.Info(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-04 19:44:04 +01:00
|
|
|
void TransactionLogIteratorImpl::UpdateCurrentWriteBatch(const Slice& record) {
|
2013-11-08 00:46:48 +01:00
|
|
|
std::unique_ptr<WriteBatch> batch(new WriteBatch());
|
2020-12-23 08:44:44 +01:00
|
|
|
Status s = WriteBatchInternal::SetContents(batch.get(), record);
|
|
|
|
s.PermitUncheckedError(); // TODO: What should we do with this error?
|
2013-10-21 04:06:19 +02:00
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
SequenceNumber expected_seq = current_last_seq_ + 1;
|
2013-10-25 04:09:02 +02:00
|
|
|
// If the iterator has started, then confirm that we get continuous batches
|
2019-03-27 20:20:43 +01:00
|
|
|
if (started_ && !IsBatchExpected(batch.get(), expected_seq)) {
|
2013-10-21 04:06:19 +02:00
|
|
|
// Seek to the batch having expected sequence number
|
2019-03-27 20:20:43 +01:00
|
|
|
if (expected_seq < files_->at(current_file_index_)->StartSequence()) {
|
2013-10-21 04:06:19 +02:00
|
|
|
// Expected batch must lie in the previous log file
|
2013-11-13 05:05:28 +01:00
|
|
|
// Avoid underflow.
|
2019-03-27 20:20:43 +01:00
|
|
|
if (current_file_index_ != 0) {
|
|
|
|
current_file_index_--;
|
2013-11-13 05:05:28 +01:00
|
|
|
}
|
2013-10-21 04:06:19 +02:00
|
|
|
}
|
2019-03-27 20:20:43 +01:00
|
|
|
starting_sequence_number_ = expected_seq;
|
2013-10-25 04:09:02 +02:00
|
|
|
// currentStatus_ will be set to Ok if reseek succeeds
|
2017-11-11 02:18:01 +01:00
|
|
|
// Note: this is still ok in seq_pre_batch_ && two_write_queuesp_ mode
|
2017-10-23 23:20:22 +02:00
|
|
|
// that allows gaps in the WAL since it will still skip over the gap.
|
2019-03-27 20:20:43 +01:00
|
|
|
current_status_ = Status::NotFound("Gap in sequence numbers");
|
2017-11-11 02:18:01 +01:00
|
|
|
// In seq_per_batch_ mode, gaps in the seq are possible so the strict mode
|
2017-10-23 23:20:22 +02:00
|
|
|
// should be disabled
|
2019-03-27 20:20:43 +01:00
|
|
|
return SeekToStartSequence(current_file_index_, !seq_per_batch_);
|
2013-10-21 04:06:19 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
current_batch_seq_ = WriteBatchInternal::Sequence(batch.get());
|
2022-04-15 02:12:16 +02:00
|
|
|
assert(!seq_per_batch_);
|
|
|
|
current_last_seq_ =
|
|
|
|
current_batch_seq_ + WriteBatchInternal::Count(batch.get()) - 1;
|
2013-10-21 04:06:19 +02:00
|
|
|
// currentBatchSeq_ can only change here
|
2019-03-27 20:20:43 +01:00
|
|
|
assert(current_last_seq_ <= versions_->LastSequence());
|
2013-10-21 04:06:19 +02:00
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
current_batch_ = std::move(batch);
|
|
|
|
is_valid_ = true;
|
|
|
|
current_status_ = Status::OK();
|
2013-03-04 19:44:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:20:43 +01:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogReader(const LogFile* log_file) {
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<SequentialFileReader> file;
|
2019-03-27 20:20:43 +01:00
|
|
|
Status s = OpenLogFile(log_file, &file);
|
2014-11-06 20:14:28 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
2013-04-09 01:28:09 +02:00
|
|
|
}
|
|
|
|
assert(file);
|
2019-03-27 20:20:43 +01:00
|
|
|
current_log_reader_.reset(
|
2018-09-14 02:08:04 +02:00
|
|
|
new log::Reader(options_->info_log, std::move(file), &reporter_,
|
2019-03-27 20:20:43 +01:00
|
|
|
read_options_.verify_checksums_, log_file->LogNumber()));
|
2013-04-09 01:28:09 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-04-15 22:39:26 +02:00
|
|
|
#endif // ROCKSDB_LITE
|