2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2014-05-30 23:31:55 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
#include "db/forward_iterator.h"
|
|
|
|
|
2014-08-29 23:32:37 +02:00
|
|
|
#include <limits>
|
2014-05-30 23:31:55 +02:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2014-08-29 23:32:37 +02:00
|
|
|
|
2016-03-01 03:38:03 +01:00
|
|
|
#include "db/column_family.h"
|
2014-05-30 23:31:55 +02:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/db_iter.h"
|
2016-03-01 03:38:03 +01:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/job_context.h"
|
2014-05-30 23:31:55 +02:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/slice_transform.h"
|
2017-02-03 01:38:40 +01:00
|
|
|
#include "table/merging_iterator.h"
|
2016-03-01 03:38:03 +01:00
|
|
|
#include "util/string_util.h"
|
2015-08-25 22:40:58 +02:00
|
|
|
#include "util/sync_point.h"
|
2014-05-30 23:31:55 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
// Usage:
|
|
|
|
// LevelIterator iter;
|
|
|
|
// iter.SetFileIndex(file_index);
|
|
|
|
// iter.Seek(target);
|
|
|
|
// iter.Next()
|
2015-10-13 00:06:38 +02:00
|
|
|
class LevelIterator : public InternalIterator {
|
2014-05-30 23:31:55 +02:00
|
|
|
public:
|
|
|
|
LevelIterator(const ColumnFamilyData* const cfd,
|
2016-08-12 04:10:16 +02:00
|
|
|
const ReadOptions& read_options,
|
|
|
|
const std::vector<FileMetaData*>& files)
|
|
|
|
: cfd_(cfd),
|
|
|
|
read_options_(read_options),
|
|
|
|
files_(files),
|
|
|
|
valid_(false),
|
|
|
|
file_index_(std::numeric_limits<uint32_t>::max()),
|
|
|
|
file_iter_(nullptr),
|
|
|
|
pinned_iters_mgr_(nullptr) {}
|
2014-05-30 23:31:55 +02:00
|
|
|
|
2016-08-12 08:34:19 +02:00
|
|
|
~LevelIterator() {
|
|
|
|
// Reset current pointer
|
|
|
|
if (pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled()) {
|
|
|
|
pinned_iters_mgr_->PinIterator(file_iter_);
|
|
|
|
} else {
|
|
|
|
delete file_iter_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
void SetFileIndex(uint32_t file_index) {
|
|
|
|
assert(file_index < files_.size());
|
|
|
|
if (file_index != file_index_) {
|
|
|
|
file_index_ = file_index;
|
2014-08-29 23:32:37 +02:00
|
|
|
Reset();
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2014-08-29 23:32:37 +02:00
|
|
|
void Reset() {
|
|
|
|
assert(file_index_ < files_.size());
|
2016-08-12 04:10:16 +02:00
|
|
|
|
|
|
|
// Reset current pointer
|
|
|
|
if (pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled()) {
|
|
|
|
pinned_iters_mgr_->PinIterator(file_iter_);
|
|
|
|
} else {
|
|
|
|
delete file_iter_;
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:30:14 +01:00
|
|
|
RangeDelAggregator range_del_agg(
|
|
|
|
cfd_->internal_comparator(), {} /* snapshots */);
|
2016-08-12 04:10:16 +02:00
|
|
|
file_iter_ = cfd_->table_cache()->NewIterator(
|
2014-08-29 23:32:37 +02:00
|
|
|
read_options_, *(cfd_->soptions()), cfd_->internal_comparator(),
|
2017-01-23 22:30:14 +01:00
|
|
|
files_[file_index_]->fd, read_options_.ignore_range_deletions ? nullptr : &range_del_agg,
|
2016-11-16 02:18:32 +01:00
|
|
|
nullptr /* table_reader_ptr */, nullptr, false);
|
2016-08-12 04:10:16 +02:00
|
|
|
file_iter_->SetPinnedItersMgr(pinned_iters_mgr_);
|
2017-01-23 22:30:14 +01:00
|
|
|
if (!range_del_agg.IsEmpty()) {
|
|
|
|
status_ = Status::NotSupported(
|
|
|
|
"Range tombstones unsupported with ForwardIterator");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2014-08-29 23:32:37 +02:00
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
void SeekToLast() override {
|
|
|
|
status_ = Status::NotSupported("LevelIterator::SeekToLast()");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
void Prev() override {
|
2014-05-30 23:31:55 +02:00
|
|
|
status_ = Status::NotSupported("LevelIterator::Prev()");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
bool Valid() const override {
|
|
|
|
return valid_;
|
|
|
|
}
|
|
|
|
void SeekToFirst() override {
|
|
|
|
SetFileIndex(0);
|
|
|
|
file_iter_->SeekToFirst();
|
|
|
|
valid_ = file_iter_->Valid();
|
|
|
|
}
|
|
|
|
void Seek(const Slice& internal_key) override {
|
|
|
|
assert(file_iter_ != nullptr);
|
|
|
|
file_iter_->Seek(internal_key);
|
|
|
|
valid_ = file_iter_->Valid();
|
|
|
|
}
|
2016-09-28 03:20:57 +02:00
|
|
|
void SeekForPrev(const Slice& internal_key) override {
|
|
|
|
status_ = Status::NotSupported("LevelIterator::SeekForPrev()");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
void Next() override {
|
|
|
|
assert(valid_);
|
|
|
|
file_iter_->Next();
|
2014-08-29 23:32:37 +02:00
|
|
|
for (;;) {
|
|
|
|
if (file_iter_->status().IsIncomplete() || file_iter_->Valid()) {
|
|
|
|
valid_ = !file_iter_->status().IsIncomplete();
|
|
|
|
return;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
if (file_index_ + 1 >= files_.size()) {
|
|
|
|
valid_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetFileIndex(file_index_ + 1);
|
|
|
|
file_iter_->SeekToFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Slice key() const override {
|
|
|
|
assert(valid_);
|
|
|
|
return file_iter_->key();
|
|
|
|
}
|
|
|
|
Slice value() const override {
|
|
|
|
assert(valid_);
|
|
|
|
return file_iter_->value();
|
|
|
|
}
|
|
|
|
Status status() const override {
|
2014-07-10 02:46:18 +02:00
|
|
|
if (!status_.ok()) {
|
|
|
|
return status_;
|
|
|
|
} else if (file_iter_ && !file_iter_->status().ok()) {
|
|
|
|
return file_iter_->status();
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
2016-08-12 04:10:16 +02:00
|
|
|
bool IsKeyPinned() const override {
|
|
|
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
|
|
|
file_iter_->IsKeyPinned();
|
|
|
|
}
|
|
|
|
bool IsValuePinned() const override {
|
|
|
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
|
|
|
file_iter_->IsValuePinned();
|
|
|
|
}
|
|
|
|
void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) override {
|
|
|
|
pinned_iters_mgr_ = pinned_iters_mgr;
|
|
|
|
if (file_iter_) {
|
|
|
|
file_iter_->SetPinnedItersMgr(pinned_iters_mgr_);
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
const ColumnFamilyData* const cfd_;
|
|
|
|
const ReadOptions& read_options_;
|
|
|
|
const std::vector<FileMetaData*>& files_;
|
|
|
|
|
|
|
|
bool valid_;
|
|
|
|
uint32_t file_index_;
|
|
|
|
Status status_;
|
2016-08-12 04:10:16 +02:00
|
|
|
InternalIterator* file_iter_;
|
|
|
|
PinnedIteratorsManager* pinned_iters_mgr_;
|
2014-05-30 23:31:55 +02:00
|
|
|
};
|
|
|
|
|
2014-06-03 21:28:58 +02:00
|
|
|
ForwardIterator::ForwardIterator(DBImpl* db, const ReadOptions& read_options,
|
2015-08-20 01:05:51 +02:00
|
|
|
ColumnFamilyData* cfd,
|
|
|
|
SuperVersion* current_sv)
|
2014-05-30 23:31:55 +02:00
|
|
|
: db_(db),
|
|
|
|
read_options_(read_options),
|
|
|
|
cfd_(cfd),
|
2014-11-18 19:20:10 +01:00
|
|
|
prefix_extractor_(cfd->ioptions()->prefix_extractor),
|
2014-05-30 23:31:55 +02:00
|
|
|
user_comparator_(cfd->user_comparator()),
|
|
|
|
immutable_min_heap_(MinIterComparator(&cfd_->internal_comparator())),
|
2014-10-24 00:34:21 +02:00
|
|
|
sv_(current_sv),
|
2014-05-30 23:31:55 +02:00
|
|
|
mutable_iter_(nullptr),
|
|
|
|
current_(nullptr),
|
2015-09-01 01:44:34 +02:00
|
|
|
valid_(false),
|
2014-09-26 23:20:24 +02:00
|
|
|
status_(Status::OK()),
|
|
|
|
immutable_status_(Status::OK()),
|
2015-08-20 01:05:51 +02:00
|
|
|
has_iter_trimmed_for_upper_bound_(false),
|
2015-09-01 01:44:34 +02:00
|
|
|
current_over_upper_bound_(false),
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
is_prev_set_(false),
|
2016-08-12 04:10:16 +02:00
|
|
|
is_prev_inclusive_(false),
|
|
|
|
pinned_iters_mgr_(nullptr) {
|
2014-10-24 00:34:21 +02:00
|
|
|
if (sv_) {
|
|
|
|
RebuildIterators(false);
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
|
|
|
|
ForwardIterator::~ForwardIterator() {
|
2014-10-24 00:34:21 +02:00
|
|
|
Cleanup(true);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 04:10:16 +02:00
|
|
|
namespace {
|
|
|
|
// Used in PinnedIteratorsManager to release pinned SuperVersion
|
|
|
|
static void ReleaseSuperVersionFunc(void* sv) {
|
|
|
|
delete reinterpret_cast<SuperVersion*>(sv);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
void ForwardIterator::SVCleanup() {
|
|
|
|
if (sv_ != nullptr && sv_->Unref()) {
|
|
|
|
// Job id == 0 means that this is not our background process, but rather
|
|
|
|
// user thread
|
|
|
|
JobContext job_context(0);
|
|
|
|
db_->mutex_.Lock();
|
|
|
|
sv_->Cleanup();
|
|
|
|
db_->FindObsoleteFiles(&job_context, false, true);
|
2016-08-04 02:42:06 +02:00
|
|
|
if (read_options_.background_purge_on_iterator_cleanup) {
|
|
|
|
db_->ScheduleBgLogWriterClose(&job_context);
|
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
db_->mutex_.Unlock();
|
2016-08-12 04:10:16 +02:00
|
|
|
if (pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled()) {
|
|
|
|
pinned_iters_mgr_->PinPtr(sv_, &ReleaseSuperVersionFunc);
|
|
|
|
} else {
|
|
|
|
delete sv_;
|
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
if (job_context.HaveSomethingToDelete()) {
|
2016-08-04 02:42:06 +02:00
|
|
|
db_->PurgeObsoleteFiles(
|
|
|
|
job_context, read_options_.background_purge_on_iterator_cleanup);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
job_context.Clean();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 00:34:21 +02:00
|
|
|
void ForwardIterator::Cleanup(bool release_sv) {
|
2014-09-05 02:40:41 +02:00
|
|
|
if (mutable_iter_ != nullptr) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(mutable_iter_, true /* is_arena */);
|
2014-09-05 02:40:41 +02:00
|
|
|
}
|
2016-08-12 04:10:16 +02:00
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
for (auto* m : imm_iters_) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(m, true /* is_arena */);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
imm_iters_.clear();
|
2016-08-12 04:10:16 +02:00
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
for (auto* f : l0_iters_) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(f);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
l0_iters_.clear();
|
2016-08-12 04:10:16 +02:00
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
for (auto* l : level_iters_) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(l);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
level_iters_.clear();
|
|
|
|
|
2014-10-24 00:34:21 +02:00
|
|
|
if (release_sv) {
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
SVCleanup();
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForwardIterator::Valid() const {
|
2015-09-01 01:44:34 +02:00
|
|
|
// See UpdateCurrent().
|
|
|
|
return valid_ ? !current_over_upper_bound_ : false;
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForwardIterator::SeekToFirst() {
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
if (sv_ == nullptr) {
|
2014-10-24 00:34:21 +02:00
|
|
|
RebuildIterators(true);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
} else if (sv_->version_number != cfd_->GetSuperVersionNumber()) {
|
|
|
|
RenewIterators();
|
2014-09-26 23:20:24 +02:00
|
|
|
} else if (immutable_status_.IsIncomplete()) {
|
2014-08-29 23:32:37 +02:00
|
|
|
ResetIncompleteIterators();
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
SeekInternal(Slice(), true);
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:05:51 +02:00
|
|
|
bool ForwardIterator::IsOverUpperBound(const Slice& internal_key) const {
|
|
|
|
return !(read_options_.iterate_upper_bound == nullptr ||
|
|
|
|
cfd_->internal_comparator().user_comparator()->Compare(
|
|
|
|
ExtractUserKey(internal_key),
|
|
|
|
*read_options_.iterate_upper_bound) < 0);
|
|
|
|
}
|
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
void ForwardIterator::Seek(const Slice& internal_key) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if (IsOverUpperBound(internal_key)) {
|
|
|
|
valid_ = false;
|
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
if (sv_ == nullptr) {
|
2014-10-24 00:34:21 +02:00
|
|
|
RebuildIterators(true);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
} else if (sv_->version_number != cfd_->GetSuperVersionNumber()) {
|
|
|
|
RenewIterators();
|
2014-09-26 23:20:24 +02:00
|
|
|
} else if (immutable_status_.IsIncomplete()) {
|
2014-08-29 23:32:37 +02:00
|
|
|
ResetIncompleteIterators();
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
SeekInternal(internal_key, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForwardIterator::SeekInternal(const Slice& internal_key,
|
|
|
|
bool seek_to_first) {
|
2014-10-24 00:34:21 +02:00
|
|
|
assert(mutable_iter_);
|
2014-05-30 23:31:55 +02:00
|
|
|
// mutable
|
|
|
|
seek_to_first ? mutable_iter_->SeekToFirst() :
|
|
|
|
mutable_iter_->Seek(internal_key);
|
|
|
|
|
|
|
|
// immutable
|
|
|
|
// TODO(ljin): NeedToSeekImmutable has negative impact on performance
|
|
|
|
// if it turns to need to seek immutable often. We probably want to have
|
|
|
|
// an option to turn it off.
|
|
|
|
if (seek_to_first || NeedToSeekImmutable(internal_key)) {
|
2014-09-26 23:20:24 +02:00
|
|
|
immutable_status_ = Status::OK();
|
2016-11-08 22:44:38 +01:00
|
|
|
if (has_iter_trimmed_for_upper_bound_ &&
|
|
|
|
(
|
|
|
|
// prev_ is not set yet
|
|
|
|
is_prev_set_ == false ||
|
|
|
|
// We are doing SeekToFirst() and internal_key.size() = 0
|
|
|
|
seek_to_first ||
|
|
|
|
// prev_key_ > internal_key
|
|
|
|
cfd_->internal_comparator().InternalKeyComparator::Compare(
|
2017-04-04 23:17:16 +02:00
|
|
|
prev_key_.GetInternalKey(), internal_key) > 0)) {
|
2015-08-20 01:05:51 +02:00
|
|
|
// Some iterators are trimmed. Need to rebuild.
|
|
|
|
RebuildIterators(true);
|
|
|
|
// Already seeked mutable iter, so seek again
|
|
|
|
seek_to_first ? mutable_iter_->SeekToFirst()
|
|
|
|
: mutable_iter_->Seek(internal_key);
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
{
|
|
|
|
auto tmp = MinIterHeap(MinIterComparator(&cfd_->internal_comparator()));
|
|
|
|
immutable_min_heap_.swap(tmp);
|
|
|
|
}
|
2015-08-20 01:05:51 +02:00
|
|
|
for (size_t i = 0; i < imm_iters_.size(); i++) {
|
|
|
|
auto* m = imm_iters_[i];
|
2014-05-30 23:31:55 +02:00
|
|
|
seek_to_first ? m->SeekToFirst() : m->Seek(internal_key);
|
2014-09-26 23:20:24 +02:00
|
|
|
if (!m->status().ok()) {
|
|
|
|
immutable_status_ = m->status();
|
|
|
|
} else if (m->Valid()) {
|
2015-08-28 20:07:07 +02:00
|
|
|
immutable_min_heap_.push(m);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:40:42 +02:00
|
|
|
Slice user_key;
|
|
|
|
if (!seek_to_first) {
|
|
|
|
user_key = ExtractUserKey(internal_key);
|
|
|
|
}
|
2014-10-31 16:48:19 +01:00
|
|
|
const VersionStorageInfo* vstorage = sv_->current->storage_info();
|
2014-10-27 23:49:46 +01:00
|
|
|
const std::vector<FileMetaData*>& l0 = vstorage->LevelFiles(0);
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < l0.size(); ++i) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if (!l0_iters_[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
if (seek_to_first) {
|
|
|
|
l0_iters_[i]->SeekToFirst();
|
|
|
|
} else {
|
|
|
|
// If the target key passes over the larget key, we are sure Next()
|
|
|
|
// won't go over this file.
|
2014-07-08 20:40:42 +02:00
|
|
|
if (user_comparator_->Compare(user_key,
|
2014-10-28 18:08:41 +01:00
|
|
|
l0[i]->largest.user_key()) > 0) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if (read_options_.iterate_upper_bound != nullptr) {
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(l0_iters_[i]);
|
2015-08-20 01:05:51 +02:00
|
|
|
l0_iters_[i] = nullptr;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
l0_iters_[i]->Seek(internal_key);
|
|
|
|
}
|
2014-08-29 23:32:37 +02:00
|
|
|
|
2014-09-26 23:20:24 +02:00
|
|
|
if (!l0_iters_[i]->status().ok()) {
|
|
|
|
immutable_status_ = l0_iters_[i]->status();
|
2014-08-29 23:32:37 +02:00
|
|
|
} else if (l0_iters_[i]->Valid()) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if (!IsOverUpperBound(l0_iters_[i]->key())) {
|
|
|
|
immutable_min_heap_.push(l0_iters_[i]);
|
|
|
|
} else {
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(l0_iters_[i]);
|
2015-08-20 01:05:51 +02:00
|
|
|
l0_iters_[i] = nullptr;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-08 20:40:42 +02:00
|
|
|
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
|
2014-10-28 18:08:41 +01:00
|
|
|
const std::vector<FileMetaData*>& level_files =
|
2014-10-27 23:49:46 +01:00
|
|
|
vstorage->LevelFiles(level);
|
2014-10-28 18:08:41 +01:00
|
|
|
if (level_files.empty()) {
|
2014-05-30 23:31:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-20 01:05:51 +02:00
|
|
|
if (level_iters_[level - 1] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
uint32_t f_idx = 0;
|
|
|
|
if (!seek_to_first) {
|
2016-10-28 19:25:39 +02:00
|
|
|
f_idx = FindFileInRange(level_files, internal_key, 0,
|
|
|
|
static_cast<uint32_t>(level_files.size()));
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
2014-07-08 20:40:42 +02:00
|
|
|
|
|
|
|
// Seek
|
2014-10-28 18:08:41 +01:00
|
|
|
if (f_idx < level_files.size()) {
|
2014-05-30 23:31:55 +02:00
|
|
|
level_iters_[level - 1]->SetFileIndex(f_idx);
|
|
|
|
seek_to_first ? level_iters_[level - 1]->SeekToFirst() :
|
|
|
|
level_iters_[level - 1]->Seek(internal_key);
|
2014-08-29 23:32:37 +02:00
|
|
|
|
2014-09-26 23:20:24 +02:00
|
|
|
if (!level_iters_[level - 1]->status().ok()) {
|
|
|
|
immutable_status_ = level_iters_[level - 1]->status();
|
2014-08-29 23:32:37 +02:00
|
|
|
} else if (level_iters_[level - 1]->Valid()) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if (!IsOverUpperBound(level_iters_[level - 1]->key())) {
|
|
|
|
immutable_min_heap_.push(level_iters_[level - 1]);
|
|
|
|
} else {
|
|
|
|
// Nothing in this level is interesting. Remove.
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(level_iters_[level - 1]);
|
2015-08-20 01:05:51 +02:00
|
|
|
level_iters_[level - 1] = nullptr;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
if (seek_to_first) {
|
2014-05-30 23:31:55 +02:00
|
|
|
is_prev_set_ = false;
|
|
|
|
} else {
|
2017-04-04 23:17:16 +02:00
|
|
|
prev_key_.SetInternalKey(internal_key);
|
2014-05-30 23:31:55 +02:00
|
|
|
is_prev_set_ = true;
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
is_prev_inclusive_ = true;
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
2015-09-01 01:44:34 +02:00
|
|
|
|
|
|
|
TEST_SYNC_POINT_CALLBACK("ForwardIterator::SeekInternal:Immutable", this);
|
ForwardIterator seek bugfix
Summary:
If `NeedToSeekImmutable()` returns false, `SeekInternal()` won't reset the
contents of `immutable_min_heap_`. However, since it calls `UpdateCurrent()`
unconditionally, if `current_` is one of immutable iterators (previously popped
from `immutable_min_heap_`), `UpdateCurrent()` will overwrite it. As a result,
if old `current_` in fact pointed to the smallest entry, forward iterator will
skip some records.
Fix implemented in this diff pushes `current_` back to `immutable_min_heap_`
before calling `UpdateCurrent()`.
Test Plan:
New unit test (courtesy of @lovro):
$ ROCKSDB_TESTS=TailingIteratorSeekToSame ./db_test
Reviewers: igor, dhruba, haobo, ljin
Reviewed By: ljin
Subscribers: lovro, leveldb
Differential Revision: https://reviews.facebook.net/D19653
2014-07-11 00:14:24 +02:00
|
|
|
} else if (current_ && current_ != mutable_iter_) {
|
|
|
|
// current_ is one of immutable iterators, push it back to the heap
|
|
|
|
immutable_min_heap_.push(current_);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateCurrent();
|
2015-08-25 22:40:58 +02:00
|
|
|
TEST_SYNC_POINT_CALLBACK("ForwardIterator::SeekInternal:Return", this);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForwardIterator::Next() {
|
|
|
|
assert(valid_);
|
2015-08-18 23:40:06 +02:00
|
|
|
bool update_prev_key = false;
|
2014-05-30 23:31:55 +02:00
|
|
|
|
|
|
|
if (sv_ == nullptr ||
|
2014-08-29 23:32:37 +02:00
|
|
|
sv_->version_number != cfd_->GetSuperVersionNumber()) {
|
2014-05-30 23:31:55 +02:00
|
|
|
std::string current_key = key().ToString();
|
|
|
|
Slice old_key(current_key.data(), current_key.size());
|
|
|
|
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
if (sv_ == nullptr) {
|
|
|
|
RebuildIterators(true);
|
|
|
|
} else {
|
|
|
|
RenewIterators();
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
SeekInternal(old_key, false);
|
|
|
|
if (!valid_ || key().compare(old_key) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (current_ != mutable_iter_) {
|
|
|
|
// It is going to advance immutable iterator
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
|
|
|
|
if (is_prev_set_ && prefix_extractor_) {
|
|
|
|
// advance prev_key_ to current_ only if they share the same prefix
|
|
|
|
update_prev_key =
|
2017-04-04 23:17:16 +02:00
|
|
|
prefix_extractor_->Transform(prev_key_.GetUserKey())
|
|
|
|
.compare(prefix_extractor_->Transform(current_->key())) == 0;
|
2015-08-18 23:40:06 +02:00
|
|
|
} else {
|
|
|
|
update_prev_key = true;
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 23:40:06 +02:00
|
|
|
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
if (update_prev_key) {
|
2017-04-04 23:17:16 +02:00
|
|
|
prev_key_.SetInternalKey(current_->key());
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
is_prev_set_ = true;
|
|
|
|
is_prev_inclusive_ = false;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
current_->Next();
|
2014-08-29 23:32:37 +02:00
|
|
|
if (current_ != mutable_iter_) {
|
2014-09-26 23:20:24 +02:00
|
|
|
if (!current_->status().ok()) {
|
|
|
|
immutable_status_ = current_->status();
|
2015-08-20 01:05:51 +02:00
|
|
|
} else if ((current_->Valid()) && (!IsOverUpperBound(current_->key()))) {
|
2014-08-29 23:32:37 +02:00
|
|
|
immutable_min_heap_.push(current_);
|
2015-08-20 01:05:51 +02:00
|
|
|
} else {
|
|
|
|
if ((current_->Valid()) && (IsOverUpperBound(current_->key()))) {
|
|
|
|
// remove the current iterator
|
|
|
|
DeleteCurrentIter();
|
|
|
|
current_ = nullptr;
|
|
|
|
}
|
2015-09-04 23:28:45 +02:00
|
|
|
if (update_prev_key) {
|
2017-04-04 23:17:16 +02:00
|
|
|
mutable_iter_->Seek(prev_key_.GetInternalKey());
|
2015-08-20 01:05:51 +02:00
|
|
|
}
|
2014-08-29 23:32:37 +02:00
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
UpdateCurrent();
|
2015-08-25 22:40:58 +02:00
|
|
|
TEST_SYNC_POINT_CALLBACK("ForwardIterator::Next:Return", this);
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Slice ForwardIterator::key() const {
|
|
|
|
assert(valid_);
|
|
|
|
return current_->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice ForwardIterator::value() const {
|
|
|
|
assert(valid_);
|
|
|
|
return current_->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status ForwardIterator::status() const {
|
|
|
|
if (!status_.ok()) {
|
|
|
|
return status_;
|
|
|
|
} else if (!mutable_iter_->status().ok()) {
|
|
|
|
return mutable_iter_->status();
|
|
|
|
}
|
2014-07-10 02:46:18 +02:00
|
|
|
|
2014-09-26 23:20:24 +02:00
|
|
|
return immutable_status_;
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:38:03 +01:00
|
|
|
Status ForwardIterator::GetProperty(std::string prop_name, std::string* prop) {
|
|
|
|
assert(prop != nullptr);
|
2016-03-03 22:18:56 +01:00
|
|
|
if (prop_name == "rocksdb.iterator.super-version-number") {
|
2016-03-01 03:38:03 +01:00
|
|
|
*prop = ToString(sv_->version_number);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::InvalidArgument();
|
|
|
|
}
|
|
|
|
|
2016-08-12 04:10:16 +02:00
|
|
|
void ForwardIterator::SetPinnedItersMgr(
|
|
|
|
PinnedIteratorsManager* pinned_iters_mgr) {
|
|
|
|
pinned_iters_mgr_ = pinned_iters_mgr;
|
|
|
|
UpdateChildrenPinnedItersMgr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForwardIterator::UpdateChildrenPinnedItersMgr() {
|
|
|
|
// Set PinnedIteratorsManager for mutable memtable iterator.
|
|
|
|
if (mutable_iter_) {
|
|
|
|
mutable_iter_->SetPinnedItersMgr(pinned_iters_mgr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set PinnedIteratorsManager for immutable memtable iterators.
|
|
|
|
for (InternalIterator* child_iter : imm_iters_) {
|
|
|
|
if (child_iter) {
|
|
|
|
child_iter->SetPinnedItersMgr(pinned_iters_mgr_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set PinnedIteratorsManager for L0 files iterators.
|
|
|
|
for (InternalIterator* child_iter : l0_iters_) {
|
|
|
|
if (child_iter) {
|
|
|
|
child_iter->SetPinnedItersMgr(pinned_iters_mgr_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set PinnedIteratorsManager for L1+ levels iterators.
|
|
|
|
for (LevelIterator* child_iter : level_iters_) {
|
|
|
|
if (child_iter) {
|
|
|
|
child_iter->SetPinnedItersMgr(pinned_iters_mgr_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForwardIterator::IsKeyPinned() const {
|
|
|
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
|
|
|
current_->IsKeyPinned();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForwardIterator::IsValuePinned() const {
|
|
|
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
|
|
|
current_->IsValuePinned();
|
|
|
|
}
|
|
|
|
|
2014-10-24 00:34:21 +02:00
|
|
|
void ForwardIterator::RebuildIterators(bool refresh_sv) {
|
2014-05-30 23:31:55 +02:00
|
|
|
// Clean up
|
2014-10-24 00:34:21 +02:00
|
|
|
Cleanup(refresh_sv);
|
|
|
|
if (refresh_sv) {
|
|
|
|
// New
|
|
|
|
sv_ = cfd_->GetReferencedSuperVersion(&(db_->mutex_));
|
|
|
|
}
|
2017-01-23 22:30:14 +01:00
|
|
|
RangeDelAggregator range_del_agg(
|
|
|
|
InternalKeyComparator(cfd_->internal_comparator()), {} /* snapshots */);
|
2014-09-05 02:40:41 +02:00
|
|
|
mutable_iter_ = sv_->mem->NewIterator(read_options_, &arena_);
|
|
|
|
sv_->imm->AddIterators(read_options_, &imm_iters_, &arena_);
|
2017-01-23 22:30:14 +01:00
|
|
|
if (!read_options_.ignore_range_deletions) {
|
|
|
|
std::unique_ptr<InternalIterator> range_del_iter(
|
|
|
|
sv_->mem->NewRangeTombstoneIterator(read_options_));
|
|
|
|
range_del_agg.AddTombstones(std::move(range_del_iter));
|
|
|
|
sv_->imm->AddRangeTombstoneIterators(read_options_, &arena_,
|
|
|
|
&range_del_agg);
|
|
|
|
}
|
2015-08-25 22:40:58 +02:00
|
|
|
has_iter_trimmed_for_upper_bound_ = false;
|
2014-10-27 23:49:46 +01:00
|
|
|
|
2014-10-31 16:48:19 +01:00
|
|
|
const auto* vstorage = sv_->current->storage_info();
|
2014-10-27 23:49:46 +01:00
|
|
|
const auto& l0_files = vstorage->LevelFiles(0);
|
2014-05-30 23:31:55 +02:00
|
|
|
l0_iters_.reserve(l0_files.size());
|
|
|
|
for (const auto* l0 : l0_files) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if ((read_options_.iterate_upper_bound != nullptr) &&
|
|
|
|
cfd_->internal_comparator().user_comparator()->Compare(
|
|
|
|
l0->smallest.user_key(), *read_options_.iterate_upper_bound) > 0) {
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
|
|
|
l0_iters_.push_back(nullptr);
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
l0_iters_.push_back(cfd_->table_cache()->NewIterator(
|
2016-11-16 02:18:32 +01:00
|
|
|
read_options_, *cfd_->soptions(), cfd_->internal_comparator(), l0->fd,
|
2017-01-23 22:30:14 +01:00
|
|
|
read_options_.ignore_range_deletions ? nullptr : &range_del_agg));
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
BuildLevelIterators(vstorage);
|
|
|
|
current_ = nullptr;
|
|
|
|
is_prev_set_ = false;
|
2016-08-12 04:10:16 +02:00
|
|
|
|
|
|
|
UpdateChildrenPinnedItersMgr();
|
2017-01-23 22:30:14 +01:00
|
|
|
if (!range_del_agg.IsEmpty()) {
|
|
|
|
status_ = Status::NotSupported(
|
|
|
|
"Range tombstones unsupported with ForwardIterator");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForwardIterator::RenewIterators() {
|
|
|
|
SuperVersion* svnew;
|
|
|
|
assert(sv_);
|
|
|
|
svnew = cfd_->GetReferencedSuperVersion(&(db_->mutex_));
|
|
|
|
|
|
|
|
if (mutable_iter_ != nullptr) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(mutable_iter_, true /* is_arena */);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
for (auto* m : imm_iters_) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(m, true /* is_arena */);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
imm_iters_.clear();
|
|
|
|
|
|
|
|
mutable_iter_ = svnew->mem->NewIterator(read_options_, &arena_);
|
|
|
|
svnew->imm->AddIterators(read_options_, &imm_iters_, &arena_);
|
2017-01-23 22:30:14 +01:00
|
|
|
RangeDelAggregator range_del_agg(
|
|
|
|
InternalKeyComparator(cfd_->internal_comparator()), {} /* snapshots */);
|
|
|
|
if (!read_options_.ignore_range_deletions) {
|
|
|
|
std::unique_ptr<InternalIterator> range_del_iter(
|
|
|
|
svnew->mem->NewRangeTombstoneIterator(read_options_));
|
|
|
|
range_del_agg.AddTombstones(std::move(range_del_iter));
|
|
|
|
sv_->imm->AddRangeTombstoneIterators(read_options_, &arena_,
|
|
|
|
&range_del_agg);
|
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
|
|
|
|
const auto* vstorage = sv_->current->storage_info();
|
|
|
|
const auto& l0_files = vstorage->LevelFiles(0);
|
|
|
|
const auto* vstorage_new = svnew->current->storage_info();
|
|
|
|
const auto& l0_files_new = vstorage_new->LevelFiles(0);
|
2015-12-16 00:26:20 +01:00
|
|
|
size_t iold, inew;
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
bool found;
|
|
|
|
std::vector<InternalIterator*> l0_iters_new;
|
|
|
|
l0_iters_new.reserve(l0_files_new.size());
|
|
|
|
|
|
|
|
for (inew = 0; inew < l0_files_new.size(); inew++) {
|
|
|
|
found = false;
|
|
|
|
for (iold = 0; iold < l0_files.size(); iold++) {
|
|
|
|
if (l0_files[iold] == l0_files_new[inew]) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
if (l0_iters_[iold] == nullptr) {
|
|
|
|
l0_iters_new.push_back(nullptr);
|
|
|
|
TEST_SYNC_POINT_CALLBACK("ForwardIterator::RenewIterators:Null", this);
|
|
|
|
} else {
|
|
|
|
l0_iters_new.push_back(l0_iters_[iold]);
|
|
|
|
l0_iters_[iold] = nullptr;
|
|
|
|
TEST_SYNC_POINT_CALLBACK("ForwardIterator::RenewIterators:Copy", this);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
l0_iters_new.push_back(cfd_->table_cache()->NewIterator(
|
|
|
|
read_options_, *cfd_->soptions(), cfd_->internal_comparator(),
|
2017-01-23 22:30:14 +01:00
|
|
|
l0_files_new[inew]->fd,
|
|
|
|
read_options_.ignore_range_deletions ? nullptr : &range_del_agg));
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* f : l0_iters_) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(f);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
l0_iters_.clear();
|
|
|
|
l0_iters_ = l0_iters_new;
|
|
|
|
|
|
|
|
for (auto* l : level_iters_) {
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(l);
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
2015-11-17 19:27:51 +01:00
|
|
|
level_iters_.clear();
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
BuildLevelIterators(vstorage_new);
|
|
|
|
current_ = nullptr;
|
|
|
|
is_prev_set_ = false;
|
|
|
|
SVCleanup();
|
|
|
|
sv_ = svnew;
|
2016-08-12 04:10:16 +02:00
|
|
|
|
|
|
|
UpdateChildrenPinnedItersMgr();
|
2017-01-23 22:30:14 +01:00
|
|
|
if (!range_del_agg.IsEmpty()) {
|
|
|
|
status_ = Status::NotSupported(
|
|
|
|
"Range tombstones unsupported with ForwardIterator");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
Reuse file iterators in tailing iterator when memtable is flushed
Summary:
Under a tailing workload, there were increased block cache
misses when a memtable was flushed because we were rebuilding iterators
in that case since the version set changed. This was exacerbated in the
case of iterate_upper_bound, since file iterators which were over the
iterate_upper_bound would have been deleted and are now brought back as
part of the Rebuild, only to be deleted again. We now renew the iterators
and only build iterators for files which are added and delete file
iterators for files which are deleted.
Refer to https://reviews.facebook.net/D50463 for previous version
Test Plan: DBTestTailingIterator.TailingIteratorTrimSeekToNext
Reviewers: anthony, IslamAbdelRahman, igor, tnovak, yhchiang, sdong
Reviewed By: sdong
Subscribers: yhchiang, march, dhruba, leveldb, lovro
Differential Revision: https://reviews.facebook.net/D50679
2015-11-14 00:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForwardIterator::BuildLevelIterators(const VersionStorageInfo* vstorage) {
|
2015-11-17 19:27:51 +01:00
|
|
|
level_iters_.reserve(vstorage->num_levels() - 1);
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
|
2014-10-27 23:49:46 +01:00
|
|
|
const auto& level_files = vstorage->LevelFiles(level);
|
2015-08-20 01:05:51 +02:00
|
|
|
if ((level_files.empty()) ||
|
|
|
|
((read_options_.iterate_upper_bound != nullptr) &&
|
|
|
|
(user_comparator_->Compare(*read_options_.iterate_upper_bound,
|
|
|
|
level_files[0]->smallest.user_key()) <
|
|
|
|
0))) {
|
2015-11-17 19:27:51 +01:00
|
|
|
level_iters_.push_back(nullptr);
|
2015-08-25 22:40:58 +02:00
|
|
|
if (!level_files.empty()) {
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
|
|
|
}
|
2014-05-30 23:31:55 +02:00
|
|
|
} else {
|
2015-11-17 19:27:51 +01:00
|
|
|
level_iters_.push_back(
|
|
|
|
new LevelIterator(cfd_, read_options_, level_files));
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 23:32:37 +02:00
|
|
|
void ForwardIterator::ResetIncompleteIterators() {
|
2014-10-31 16:48:19 +01:00
|
|
|
const auto& l0_files = sv_->current->storage_info()->LevelFiles(0);
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < l0_iters_.size(); ++i) {
|
2014-08-29 23:32:37 +02:00
|
|
|
assert(i < l0_files.size());
|
2015-08-25 22:38:35 +02:00
|
|
|
if (!l0_iters_[i] || !l0_iters_[i]->status().IsIncomplete()) {
|
2014-08-29 23:32:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(l0_iters_[i]);
|
2014-08-29 23:32:37 +02:00
|
|
|
l0_iters_[i] = cfd_->table_cache()->NewIterator(
|
|
|
|
read_options_, *cfd_->soptions(), cfd_->internal_comparator(),
|
2016-11-16 02:18:32 +01:00
|
|
|
l0_files[i]->fd, nullptr /* range_del_agg */);
|
2016-08-12 04:10:16 +02:00
|
|
|
l0_iters_[i]->SetPinnedItersMgr(pinned_iters_mgr_);
|
2014-08-29 23:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* level_iter : level_iters_) {
|
|
|
|
if (level_iter && level_iter->status().IsIncomplete()) {
|
|
|
|
level_iter->Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current_ = nullptr;
|
|
|
|
is_prev_set_ = false;
|
|
|
|
}
|
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
void ForwardIterator::UpdateCurrent() {
|
|
|
|
if (immutable_min_heap_.empty() && !mutable_iter_->Valid()) {
|
|
|
|
current_ = nullptr;
|
|
|
|
} else if (immutable_min_heap_.empty()) {
|
|
|
|
current_ = mutable_iter_;
|
|
|
|
} else if (!mutable_iter_->Valid()) {
|
|
|
|
current_ = immutable_min_heap_.top();
|
|
|
|
immutable_min_heap_.pop();
|
|
|
|
} else {
|
|
|
|
current_ = immutable_min_heap_.top();
|
|
|
|
assert(current_ != nullptr);
|
|
|
|
assert(current_->Valid());
|
|
|
|
int cmp = cfd_->internal_comparator().InternalKeyComparator::Compare(
|
2014-06-10 18:57:26 +02:00
|
|
|
mutable_iter_->key(), current_->key());
|
2014-05-30 23:31:55 +02:00
|
|
|
assert(cmp != 0);
|
|
|
|
if (cmp > 0) {
|
|
|
|
immutable_min_heap_.pop();
|
|
|
|
} else {
|
|
|
|
current_ = mutable_iter_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valid_ = (current_ != nullptr);
|
|
|
|
if (!status_.ok()) {
|
|
|
|
status_ = Status::OK();
|
|
|
|
}
|
2015-09-01 01:44:34 +02:00
|
|
|
|
|
|
|
// Upper bound doesn't apply to the memtable iterator. We want Valid() to
|
|
|
|
// return false when all iterators are over iterate_upper_bound, but can't
|
|
|
|
// just set valid_ to false, as that would effectively disable the tailing
|
|
|
|
// optimization (Seek() would be called on all immutable iterators regardless
|
|
|
|
// of whether the target key is greater than prev_key_).
|
|
|
|
current_over_upper_bound_ = valid_ && IsOverUpperBound(current_->key());
|
2014-05-30 23:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ForwardIterator::NeedToSeekImmutable(const Slice& target) {
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
// We maintain the interval (prev_key_, immutable_min_heap_.top()->key())
|
|
|
|
// such that there are no records with keys within that range in
|
|
|
|
// immutable_min_heap_. Since immutable structures (SST files and immutable
|
|
|
|
// memtables) can't change in this version, we don't need to do a seek if
|
|
|
|
// 'target' belongs to that interval (immutable_min_heap_.top() is already
|
|
|
|
// at the correct position).
|
|
|
|
|
2014-09-26 23:20:24 +02:00
|
|
|
if (!valid_ || !current_ || !is_prev_set_ || !immutable_status_.ok()) {
|
2014-05-30 23:31:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-04 23:17:16 +02:00
|
|
|
Slice prev_key = prev_key_.GetInternalKey();
|
2014-05-30 23:31:55 +02:00
|
|
|
if (prefix_extractor_ && prefix_extractor_->Transform(target).compare(
|
|
|
|
prefix_extractor_->Transform(prev_key)) != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (cfd_->internal_comparator().InternalKeyComparator::Compare(
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
prev_key, target) >= (is_prev_inclusive_ ? 1 : 0)) {
|
2014-05-30 23:31:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
ForwardIterator: update prev_key_ only if prefix hasn't changed
Summary:
Since ForwardIterator is on a level below DBIter, the latter may call Next() on
it (e.g. in order to skip deletion markers). Since this also updates
`prev_key_`, it may prevent the Seek() optimization.
For example, assume that there's only one SST file and it contains the following
entries: 0101, 0201 (`ValueType::kTypeDeletion`, i.e. a tombstone record), 0201
(`kTypeValue`), 0202. Memtable is empty. `Seek(0102)` will result in `prev_key_`
being set to `0201` instead of `0102`, since `DBIter::Seek()` will call
`ForwardIterator::Next()` to skip record 0201. Therefore, when `Seek(0102)` is
called again, `NeedToSeekImmutable()` will return true.
This fix relies on `prefix_extractor_` to detect prefix changes. `prev_key_` is
only set to `current_->key()` as long as they have the same prefix.
I also made a small change to `NeedToSeekImmutable()` so it no longer returns
true when the db is empty (i.e. there's nothing but a memtable).
Test Plan:
$ TEST_TMPDIR=/dev/shm/rocksdbtest ROCKSDB_TESTS=TailingIterator ./db_test
Reviewers: sdong, igor, ljin
Reviewed By: ljin
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D23823
2014-09-23 00:20:03 +02:00
|
|
|
|
|
|
|
if (immutable_min_heap_.empty() && current_ == mutable_iter_) {
|
|
|
|
// Nothing to seek on.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (cfd_->internal_comparator().InternalKeyComparator::Compare(
|
|
|
|
target, current_ == mutable_iter_ ? immutable_min_heap_.top()->key()
|
|
|
|
: current_->key()) > 0) {
|
2014-05-30 23:31:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:05:51 +02:00
|
|
|
void ForwardIterator::DeleteCurrentIter() {
|
|
|
|
const VersionStorageInfo* vstorage = sv_->current->storage_info();
|
|
|
|
const std::vector<FileMetaData*>& l0 = vstorage->LevelFiles(0);
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < l0.size(); ++i) {
|
2015-08-20 01:05:51 +02:00
|
|
|
if (!l0_iters_[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (l0_iters_[i] == current_) {
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(l0_iters_[i]);
|
2015-08-20 01:05:51 +02:00
|
|
|
l0_iters_[i] = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
|
|
|
|
if (level_iters_[level - 1] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (level_iters_[level - 1] == current_) {
|
|
|
|
has_iter_trimmed_for_upper_bound_ = true;
|
2016-08-12 04:10:16 +02:00
|
|
|
DeleteIterator(level_iters_[level - 1]);
|
2015-08-20 01:05:51 +02:00
|
|
|
level_iters_[level - 1] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 23:28:45 +02:00
|
|
|
bool ForwardIterator::TEST_CheckDeletedIters(int* pdeleted_iters,
|
|
|
|
int* pnum_iters) {
|
|
|
|
bool retval = false;
|
|
|
|
int deleted_iters = 0;
|
|
|
|
int num_iters = 0;
|
2015-08-25 22:40:58 +02:00
|
|
|
|
|
|
|
const VersionStorageInfo* vstorage = sv_->current->storage_info();
|
|
|
|
const std::vector<FileMetaData*>& l0 = vstorage->LevelFiles(0);
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < l0.size(); ++i) {
|
2015-08-25 22:40:58 +02:00
|
|
|
if (!l0_iters_[i]) {
|
2015-09-04 23:28:45 +02:00
|
|
|
retval = true;
|
|
|
|
deleted_iters++;
|
|
|
|
} else {
|
|
|
|
num_iters++;
|
2015-08-25 22:40:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
|
|
|
|
if ((level_iters_[level - 1] == nullptr) &&
|
|
|
|
(!vstorage->LevelFiles(level).empty())) {
|
2015-09-04 23:28:45 +02:00
|
|
|
retval = true;
|
|
|
|
deleted_iters++;
|
|
|
|
} else if (!vstorage->LevelFiles(level).empty()) {
|
|
|
|
num_iters++;
|
2015-08-25 22:40:58 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 23:28:45 +02:00
|
|
|
if ((!retval) && num_iters <= 1) {
|
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
if (pdeleted_iters) {
|
|
|
|
*pdeleted_iters = deleted_iters;
|
|
|
|
}
|
|
|
|
if (pnum_iters) {
|
|
|
|
*pnum_iters = num_iters;
|
|
|
|
}
|
|
|
|
return retval;
|
2015-08-25 22:40:58 +02:00
|
|
|
}
|
2015-09-04 23:28:45 +02:00
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
uint32_t ForwardIterator::FindFileInRange(
|
|
|
|
const std::vector<FileMetaData*>& files, const Slice& internal_key,
|
|
|
|
uint32_t left, uint32_t right) {
|
|
|
|
while (left < right) {
|
|
|
|
uint32_t mid = (left + right) / 2;
|
|
|
|
const FileMetaData* f = files[mid];
|
|
|
|
if (cfd_->internal_comparator().InternalKeyComparator::Compare(
|
|
|
|
f->largest.Encode(), internal_key) < 0) {
|
|
|
|
// Key at "mid.largest" is < "target". Therefore all
|
|
|
|
// files at or before "mid" are uninteresting.
|
|
|
|
left = mid + 1;
|
|
|
|
} else {
|
|
|
|
// Key at "mid.largest" is >= "target". Therefore all files
|
|
|
|
// after "mid" are uninteresting.
|
|
|
|
right = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
2016-08-12 04:10:16 +02:00
|
|
|
void ForwardIterator::DeleteIterator(InternalIterator* iter, bool is_arena) {
|
|
|
|
if (iter == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled()) {
|
|
|
|
pinned_iters_mgr_->PinIterator(iter, is_arena);
|
|
|
|
} else {
|
|
|
|
if (is_arena) {
|
|
|
|
iter->~InternalIterator();
|
|
|
|
} else {
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 23:31:55 +02:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|