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.
|
|
|
|
//
|
2013-08-06 19:54:37 +00:00
|
|
|
#include "db/transaction_log_impl.h"
|
2012-11-30 01:28:37 +00:00
|
|
|
#include "db/write_batch_internal.h"
|
2013-03-21 22:12:35 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2012-11-30 01:28:37 +00:00
|
|
|
|
|
|
|
TransactionLogIteratorImpl::TransactionLogIteratorImpl(
|
2013-10-01 21:46:52 +00:00
|
|
|
const std::string& dir,
|
2013-03-04 18:44:04 +00:00
|
|
|
const Options* options,
|
2013-06-07 22:35:17 +00:00
|
|
|
const EnvOptions& soptions,
|
2013-08-06 19:54:37 +00:00
|
|
|
const SequenceNumber seq,
|
|
|
|
std::unique_ptr<VectorLogPtr> files,
|
2013-03-04 18:44:04 +00:00
|
|
|
SequenceNumber const * const lastFlushedSequence) :
|
2013-10-01 21:46:52 +00:00
|
|
|
dir_(dir),
|
2013-08-06 19:54:37 +00:00
|
|
|
options_(options),
|
|
|
|
soptions_(soptions),
|
|
|
|
startingSequenceNumber_(seq),
|
|
|
|
files_(std::move(files)),
|
|
|
|
started_(false),
|
|
|
|
isValid_(false),
|
|
|
|
currentFileIndex_(0),
|
|
|
|
lastFlushedSequence_(lastFlushedSequence) {
|
|
|
|
assert(startingSequenceNumber_ <= *lastFlushedSequence_);
|
2013-10-13 22:28:24 +00:00
|
|
|
assert(files_ != nullptr);
|
2012-11-30 01:28:37 +00:00
|
|
|
|
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(
|
2013-08-06 19:54:37 +00:00
|
|
|
const LogFile* logFile,
|
2013-03-04 18:44:04 +00:00
|
|
|
unique_ptr<SequentialFile>* file) {
|
2012-11-30 01:28:37 +00:00
|
|
|
Env* env = options_->env;
|
2013-08-06 19:54:37 +00:00
|
|
|
if (logFile->Type() == kArchivedLogFile) {
|
2013-10-01 21:46:52 +00:00
|
|
|
std::string fname = ArchivedLogFileName(dir_, logFile->LogNumber());
|
2013-03-15 00:00:04 +00:00
|
|
|
return env->NewSequentialFile(fname, file, soptions_);
|
2012-11-30 01:28:37 +00:00
|
|
|
} else {
|
2013-10-01 21:46:52 +00:00
|
|
|
std::string fname = LogFileName(dir_, logFile->LogNumber());
|
2013-03-15 00:00:04 +00:00
|
|
|
Status status = env->NewSequentialFile(fname, file, soptions_);
|
2012-11-30 01:28:37 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
// If cannot open file in DB directory.
|
|
|
|
// Try the archive dir, as it could have moved in the meanwhile.
|
2013-10-01 21:46:52 +00:00
|
|
|
fname = ArchivedLogFileName(dir_, logFile->LogNumber());
|
2013-03-15 00:00:04 +00:00
|
|
|
status = env->NewSequentialFile(fname, file, soptions_);
|
2012-11-30 01:28:37 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return Status::IOError(" Requested file not present in the dir");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 18:44:04 +00:00
|
|
|
BatchResult TransactionLogIteratorImpl::GetBatch() {
|
2012-11-30 01:28:37 +00:00
|
|
|
assert(isValid_); // cannot call in a non valid state.
|
2013-03-04 18:44:04 +00:00
|
|
|
BatchResult result;
|
2013-10-13 22:28:24 +00:00
|
|
|
result.sequence = currentBatchSeq_;
|
2013-03-04 18:44:04 +00:00
|
|
|
result.writeBatchPtr = std::move(currentBatch_);
|
|
|
|
return result;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status TransactionLogIteratorImpl::status() {
|
|
|
|
return currentStatus_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransactionLogIteratorImpl::Valid() {
|
|
|
|
return started_ && isValid_;
|
|
|
|
}
|
|
|
|
|
2013-10-13 22:28:24 +00:00
|
|
|
void TransactionLogIteratorImpl::SeekToStartSequence() {
|
|
|
|
std::string scratch;
|
|
|
|
Slice record;
|
2013-03-04 18:44:04 +00:00
|
|
|
isValid_ = false;
|
2013-04-08 23:28:09 +00:00
|
|
|
if (startingSequenceNumber_ > *lastFlushedSequence_) {
|
2013-03-04 18:44:04 +00:00
|
|
|
currentStatus_ = Status::IOError("Looking for a sequence, "
|
2013-10-13 22:28:24 +00:00
|
|
|
"which is not flushed yet.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (files_->size() == 0) {
|
2013-03-04 18:44:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-13 22:28:24 +00:00
|
|
|
Status s = OpenLogReader(files_->at(0).get());
|
2013-04-08 23:28:09 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
currentStatus_ = s;
|
2012-11-30 01:28:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-08 23:28:09 +00:00
|
|
|
while (currentLogReader_->ReadRecord(&record, &scratch)) {
|
2012-11-30 01:28:37 +00:00
|
|
|
if (record.size() < 12) {
|
2013-04-29 20:19:24 +00:00
|
|
|
reporter_.Corruption(
|
2012-11-30 01:28:37 +00:00
|
|
|
record.size(), Status::Corruption("log record too small"));
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-04 18:44:04 +00:00
|
|
|
UpdateCurrentWriteBatch(record);
|
2013-10-13 22:28:24 +00:00
|
|
|
if (currentBatchSeq_ + currentBatchCount_ - 1 >=
|
|
|
|
startingSequenceNumber_) {
|
|
|
|
assert(currentBatchSeq_ <= *lastFlushedSequence_);
|
2012-11-30 01:28:37 +00:00
|
|
|
isValid_ = true;
|
2013-10-13 22:28:24 +00:00
|
|
|
started_ = true; // set started_ as we could seek till starting sequence
|
|
|
|
return;
|
2013-04-08 23:28:09 +00:00
|
|
|
} else {
|
|
|
|
isValid_ = false;
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-13 22:28:24 +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
|
|
|
|
if (files_->size() != 1) {
|
|
|
|
currentStatus_ = Status::Corruption("Start sequence was not found, "
|
|
|
|
"skipping to the next available");
|
|
|
|
reporter_.Corruption(0, currentStatus_);
|
|
|
|
started_ = true; // Let Next find next available entry
|
|
|
|
Next();
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
2013-10-13 22:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionLogIteratorImpl::Next() {
|
|
|
|
// TODO:Next() says that it requires Valid to be true but this is not true
|
|
|
|
// assert(Valid());
|
|
|
|
std::string scratch;
|
|
|
|
Slice record;
|
|
|
|
isValid_ = false;
|
|
|
|
if (!started_) { // Runs every time until we can seek to the start sequence
|
|
|
|
return SeekToStartSequence();
|
2013-04-08 23:28:09 +00:00
|
|
|
}
|
2013-10-13 22:28:24 +00:00
|
|
|
while(true) {
|
2013-01-20 10:07:13 +00:00
|
|
|
assert(currentLogReader_);
|
2013-10-13 22:28:24 +00:00
|
|
|
if (currentBatchSeq_ < *lastFlushedSequence_) {
|
2013-03-21 22:12:35 +00:00
|
|
|
if (currentLogReader_->IsEOF()) {
|
|
|
|
currentLogReader_->UnmarkEOF();
|
|
|
|
}
|
|
|
|
while (currentLogReader_->ReadRecord(&record, &scratch)) {
|
|
|
|
if (record.size() < 12) {
|
2013-04-29 20:19:24 +00:00
|
|
|
reporter_.Corruption(
|
2013-03-21 22:12:35 +00:00
|
|
|
record.size(), Status::Corruption("log record too small"));
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
UpdateCurrentWriteBatch(record);
|
2013-10-13 22:28:24 +00:00
|
|
|
return;
|
2013-03-21 22:12:35 +00:00
|
|
|
}
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 22:28:24 +00:00
|
|
|
// Open the next file
|
|
|
|
if (currentFileIndex_ < files_->size() - 1) {
|
|
|
|
++currentFileIndex_;
|
|
|
|
Status status =OpenLogReader(files_->at(currentFileIndex_).get());
|
|
|
|
if (!status.ok()) {
|
2012-11-30 01:28:37 +00:00
|
|
|
isValid_ = false;
|
2013-10-13 22:28:24 +00:00
|
|
|
currentStatus_ = status;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isValid_ = false;
|
|
|
|
if (currentBatchSeq_ == *lastFlushedSequence_) {
|
|
|
|
currentStatus_ = Status::OK();
|
|
|
|
} else {
|
|
|
|
currentStatus_ = Status::IOError(" NO MORE DATA LEFT");
|
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-03-04 18:44:04 +00:00
|
|
|
void TransactionLogIteratorImpl::UpdateCurrentWriteBatch(const Slice& record) {
|
|
|
|
WriteBatch* batch = new WriteBatch();
|
|
|
|
WriteBatchInternal::SetContents(batch, record);
|
2013-10-13 22:28:24 +00:00
|
|
|
currentBatchSeq_ = WriteBatchInternal::Sequence(batch);
|
|
|
|
currentBatchCount_ = WriteBatchInternal::Count(batch);
|
2013-03-04 18:44:04 +00:00
|
|
|
currentBatch_.reset(batch);
|
2013-03-21 22:12:35 +00:00
|
|
|
isValid_ = true;
|
2013-03-21 22:49:20 +00:00
|
|
|
currentStatus_ = Status::OK();
|
2013-03-04 18:44:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 19:54:37 +00:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogReader(const LogFile* logFile) {
|
2013-04-08 23:28:09 +00:00
|
|
|
unique_ptr<SequentialFile> file;
|
|
|
|
Status status = OpenLogFile(logFile, &file);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
assert(file);
|
|
|
|
currentLogReader_.reset(
|
2013-04-29 20:19:24 +00:00
|
|
|
new log::Reader(std::move(file), &reporter_, true, 0)
|
2013-04-08 23:28:09 +00:00
|
|
|
);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|