2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-05-30 21:31:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-07-14 17:09:31 +00:00
|
|
|
#include "rocksdb/comparator.h"
|
2014-05-30 21:31:55 +00:00
|
|
|
|
2022-11-02 21:34:24 +00:00
|
|
|
#include <queue>
|
2014-05-30 21:31:55 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "memory/arena.h"
|
2014-05-30 21:31:55 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
|
|
|
#include "rocksdb/options.h"
|
2015-10-12 22:06:38 +00:00
|
|
|
#include "table/internal_iterator.h"
|
2014-05-30 21:31:55 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-05-30 21:31:55 +00:00
|
|
|
|
|
|
|
class DBImpl;
|
|
|
|
class Env;
|
|
|
|
struct SuperVersion;
|
|
|
|
class ColumnFamilyData;
|
2018-02-13 21:44:22 +00:00
|
|
|
class ForwardLevelIterator;
|
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-13 23:50:59 +00:00
|
|
|
class VersionStorageInfo;
|
2014-06-03 19:28:58 +00:00
|
|
|
struct FileMetaData;
|
2014-05-30 21:31:55 +00:00
|
|
|
|
|
|
|
class MinIterComparator {
|
|
|
|
public:
|
2022-07-14 17:09:31 +00:00
|
|
|
explicit MinIterComparator(const CompareInterface* comparator)
|
|
|
|
: comparator_(comparator) {}
|
2014-05-30 21:31:55 +00:00
|
|
|
|
2015-10-12 22:06:38 +00:00
|
|
|
bool operator()(InternalIterator* a, InternalIterator* b) {
|
2014-05-30 21:31:55 +00:00
|
|
|
return comparator_->Compare(a->key(), b->key()) > 0;
|
|
|
|
}
|
2022-11-02 21:34:24 +00:00
|
|
|
|
2014-05-30 21:31:55 +00:00
|
|
|
private:
|
2022-07-14 17:09:31 +00:00
|
|
|
const CompareInterface* comparator_;
|
2014-05-30 21:31:55 +00:00
|
|
|
};
|
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using MinIterHeap =
|
|
|
|
std::priority_queue<InternalIterator*, std::vector<InternalIterator*>,
|
|
|
|
MinIterComparator>;
|
2014-05-30 21:31:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ForwardIterator is a special type of iterator that only supports Seek()
|
|
|
|
* and Next(). It is expected to perform better than TailingIterator by
|
|
|
|
* removing the encapsulation and making all information accessible within
|
|
|
|
* the iterator. At the current implementation, snapshot is taken at the
|
|
|
|
* time Seek() is called. The Next() followed do not see new values after.
|
|
|
|
*/
|
2015-10-12 22:06:38 +00:00
|
|
|
class ForwardIterator : public InternalIterator {
|
2014-05-30 21:31:55 +00:00
|
|
|
public:
|
2014-06-03 19:28:58 +00:00
|
|
|
ForwardIterator(DBImpl* db, const ReadOptions& read_options,
|
Properly report IO errors when IndexType::kBinarySearchWithFirstKey is used (#6621)
Summary:
Context: Index type `kBinarySearchWithFirstKey` added the ability for sst file iterator to sometimes report a key from index without reading the corresponding data block. This is useful when sst blocks are cut at some meaningful boundaries (e.g. one block per key prefix), and many seeks land between blocks (e.g. for each prefix, the ranges of keys in different sst files are nearly disjoint, so a typical seek needs to read a data block from only one file even if all files have the prefix). But this added a new error condition, which rocksdb code was really not equipped to deal with: `InternalIterator::value()` may fail with an IO error or Status::Incomplete, but it's just a method returning a Slice, with no way to report error instead. Before this PR, this type of error wasn't handled at all (an empty slice was returned), and kBinarySearchWithFirstKey implementation was considered a prototype.
Now that we (LogDevice) have experimented with kBinarySearchWithFirstKey for a while and confirmed that it's really useful, this PR is adding the missing error handling.
It's a pretty inconvenient situation implementation-wise. The error needs to be reported from InternalIterator when trying to access value. But there are ~700 call sites of `InternalIterator::value()`, most of which either can't hit the error condition (because the iterator is reading from memtable or from index or something) or wouldn't benefit from the deferred loading of the value (e.g. compaction iterator that reads all values anyway). Adding error handling to all these call sites would needlessly bloat the code. So instead I made the deferred value loading optional: only the call sites that may use deferred loading have to call the new method `PrepareValue()` before calling `value()`. The feature is enabled with a new bool argument `allow_unprepared_value` to a bunch of methods that create iterators (it wouldn't make sense to put it in ReadOptions because it's completely internal to iterators, with virtually no user-visible effect). Lmk if you have better ideas.
Note that the deferred value loading only happens for *internal* iterators. The user-visible iterator (DBIter) always prepares the value before returning from Seek/Next/etc. We could go further and add an API to defer that value loading too, but that's most likely not useful for LogDevice, so it doesn't seem worth the complexity for now.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6621
Test Plan: make -j5 check . Will also deploy to some logdevice test clusters and look at stats.
Reviewed By: siying
Differential Revision: D20786930
Pulled By: al13n321
fbshipit-source-id: 6da77d918bad3780522e918f17f4d5513d3e99ee
2020-04-16 00:37:23 +00:00
|
|
|
ColumnFamilyData* cfd, SuperVersion* current_sv = nullptr,
|
|
|
|
bool allow_unprepared_value = false);
|
2014-05-30 21:31:55 +00:00
|
|
|
virtual ~ForwardIterator();
|
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
void SeekForPrev(const Slice& /*target*/) override {
|
2016-09-28 01:20:57 +00:00
|
|
|
status_ = Status::NotSupported("ForwardIterator::SeekForPrev()");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2014-05-30 21:31:55 +00:00
|
|
|
void SeekToLast() override {
|
|
|
|
status_ = Status::NotSupported("ForwardIterator::SeekToLast()");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
2015-02-26 19:28:41 +00:00
|
|
|
void Prev() override {
|
2014-05-30 21:31:55 +00:00
|
|
|
status_ = Status::NotSupported("ForwardIterator::Prev");
|
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
|
2024-01-31 21:14:42 +00:00
|
|
|
bool Valid() const override;
|
2014-05-30 21:31:55 +00:00
|
|
|
void SeekToFirst() override;
|
2024-01-31 21:14:42 +00:00
|
|
|
void Seek(const Slice& target) override;
|
|
|
|
void Next() override;
|
|
|
|
Slice key() const override;
|
|
|
|
Slice value() const override;
|
Support returning write unix time in iterator property (#12428)
Summary:
This PR adds support to return data's approximate unix write time in the iterator property API. The general implementation is:
1) If the entry comes from a SST file, the sequence number to time mapping recorded in that file's table properties will be used to deduce the entry's write time from its sequence number. If no such recording is available, `std::numeric_limits<uint64_t>::max()` is returned to indicate the write time is unknown except if the entry's sequence number is zero, in which case, 0 is returned. This also means that even if `preclude_last_level_data_seconds` and `preserve_internal_time_seconds` can be toggled off between DB reopens, as long as the SST file's table property has the mapping available, the entry's write time can be deduced and returned.
2) If the entry comes from memtable, we will use the DB's sequence number to write time mapping to do similar things. A copy of the DB's seqno to write time mapping is kept in SuperVersion to allow iterators to have lock free access. This also means a new `SuperVersion` is installed each time DB's seqno to time mapping updates, which is originally proposed by Peter in https://github.com/facebook/rocksdb/issues/11928 . Similarly, if the feature is not enabled, `std::numeric_limits<uint64_t>::max()` is returned to indicate the write time is unknown.
Needed follow up:
1) The write time for `kTypeValuePreferredSeqno` should be special cased, where it's already specified by the user, so we can directly return it.
2) Flush job can be updated to use DB's seqno to time mapping copy in the SuperVersion.
3) Handle the case when `TimedPut` is called with a write time that is `std::numeric_limits<uint64_t>::max()`. We can make it a regular `Put`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12428
Test Plan: Added unit test
Reviewed By: pdillinger
Differential Revision: D54967067
Pulled By: jowlyzhang
fbshipit-source-id: c795b1b7ec142e09e53f2ed3461cf719833cb37a
2024-03-15 22:37:37 +00:00
|
|
|
uint64_t write_unix_time() const override;
|
2024-01-31 21:14:42 +00:00
|
|
|
Status status() const override;
|
|
|
|
bool PrepareValue() override;
|
|
|
|
Status GetProperty(std::string prop_name, std::string* prop) override;
|
|
|
|
void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) override;
|
|
|
|
bool IsKeyPinned() const override;
|
|
|
|
bool IsValuePinned() const override;
|
2016-03-01 02:38:03 +00:00
|
|
|
|
2015-09-04 21:28:45 +00:00
|
|
|
bool TEST_CheckDeletedIters(int* deleted_iters, int* num_iters);
|
2014-05-30 21:31:55 +00:00
|
|
|
|
|
|
|
private:
|
2014-10-23 22:34:21 +00:00
|
|
|
void Cleanup(bool release_sv);
|
2018-02-03 05:16:26 +00:00
|
|
|
// Unreference and, if needed, clean up the current SuperVersion. This is
|
|
|
|
// either done immediately or deferred until this iterator is unpinned by
|
|
|
|
// PinnedIteratorsManager.
|
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-13 23:50:59 +00:00
|
|
|
void SVCleanup();
|
2022-11-02 21:34:24 +00:00
|
|
|
static void SVCleanup(DBImpl* db, SuperVersion* sv,
|
|
|
|
bool background_purge_on_iterator_cleanup);
|
2018-02-03 05:16:26 +00:00
|
|
|
static void DeferredSVCleanup(void* arg);
|
|
|
|
|
2014-10-23 22:34:21 +00:00
|
|
|
void RebuildIterators(bool refresh_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-13 23:50:59 +00:00
|
|
|
void RenewIterators();
|
2022-01-21 19:36:36 +00:00
|
|
|
void BuildLevelIterators(const VersionStorageInfo* vstorage,
|
|
|
|
SuperVersion* sv);
|
2014-08-29 21:32:37 +00:00
|
|
|
void ResetIncompleteIterators();
|
2022-10-10 22:48:48 +00:00
|
|
|
void SeekInternal(const Slice& internal_key, bool seek_to_first,
|
|
|
|
bool seek_after_async_io);
|
|
|
|
|
2014-05-30 21:31:55 +00:00
|
|
|
void UpdateCurrent();
|
|
|
|
bool NeedToSeekImmutable(const Slice& internal_key);
|
2015-08-19 23:05:51 +00:00
|
|
|
void DeleteCurrentIter();
|
2022-11-02 21:34:24 +00:00
|
|
|
uint32_t FindFileInRange(const std::vector<FileMetaData*>& files,
|
|
|
|
const Slice& internal_key, uint32_t left,
|
|
|
|
uint32_t right);
|
2014-05-30 21:31:55 +00:00
|
|
|
|
2015-08-19 23:05:51 +00:00
|
|
|
bool IsOverUpperBound(const Slice& internal_key) const;
|
|
|
|
|
2016-08-12 02:10:16 +00:00
|
|
|
// Set PinnedIteratorsManager for all children Iterators, this function should
|
|
|
|
// be called whenever we update children Iterators or pinned_iters_mgr_.
|
|
|
|
void UpdateChildrenPinnedItersMgr();
|
|
|
|
|
|
|
|
// A helper function that will release iter in the proper manner, or pass it
|
|
|
|
// to pinned_iters_mgr_ to release it later if pinning is enabled.
|
|
|
|
void DeleteIterator(InternalIterator* iter, bool is_arena = false);
|
|
|
|
|
2014-05-30 21:31:55 +00:00
|
|
|
DBImpl* const db_;
|
2023-03-17 21:57:09 +00:00
|
|
|
ReadOptions read_options_;
|
2014-05-30 21:31:55 +00:00
|
|
|
ColumnFamilyData* const cfd_;
|
|
|
|
const SliceTransform* const prefix_extractor_;
|
|
|
|
const Comparator* user_comparator_;
|
Properly report IO errors when IndexType::kBinarySearchWithFirstKey is used (#6621)
Summary:
Context: Index type `kBinarySearchWithFirstKey` added the ability for sst file iterator to sometimes report a key from index without reading the corresponding data block. This is useful when sst blocks are cut at some meaningful boundaries (e.g. one block per key prefix), and many seeks land between blocks (e.g. for each prefix, the ranges of keys in different sst files are nearly disjoint, so a typical seek needs to read a data block from only one file even if all files have the prefix). But this added a new error condition, which rocksdb code was really not equipped to deal with: `InternalIterator::value()` may fail with an IO error or Status::Incomplete, but it's just a method returning a Slice, with no way to report error instead. Before this PR, this type of error wasn't handled at all (an empty slice was returned), and kBinarySearchWithFirstKey implementation was considered a prototype.
Now that we (LogDevice) have experimented with kBinarySearchWithFirstKey for a while and confirmed that it's really useful, this PR is adding the missing error handling.
It's a pretty inconvenient situation implementation-wise. The error needs to be reported from InternalIterator when trying to access value. But there are ~700 call sites of `InternalIterator::value()`, most of which either can't hit the error condition (because the iterator is reading from memtable or from index or something) or wouldn't benefit from the deferred loading of the value (e.g. compaction iterator that reads all values anyway). Adding error handling to all these call sites would needlessly bloat the code. So instead I made the deferred value loading optional: only the call sites that may use deferred loading have to call the new method `PrepareValue()` before calling `value()`. The feature is enabled with a new bool argument `allow_unprepared_value` to a bunch of methods that create iterators (it wouldn't make sense to put it in ReadOptions because it's completely internal to iterators, with virtually no user-visible effect). Lmk if you have better ideas.
Note that the deferred value loading only happens for *internal* iterators. The user-visible iterator (DBIter) always prepares the value before returning from Seek/Next/etc. We could go further and add an API to defer that value loading too, but that's most likely not useful for LogDevice, so it doesn't seem worth the complexity for now.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6621
Test Plan: make -j5 check . Will also deploy to some logdevice test clusters and look at stats.
Reviewed By: siying
Differential Revision: D20786930
Pulled By: al13n321
fbshipit-source-id: 6da77d918bad3780522e918f17f4d5513d3e99ee
2020-04-16 00:37:23 +00:00
|
|
|
const bool allow_unprepared_value_;
|
2014-05-30 21:31:55 +00:00
|
|
|
MinIterHeap immutable_min_heap_;
|
|
|
|
|
|
|
|
SuperVersion* sv_;
|
2015-10-12 22:06:38 +00:00
|
|
|
InternalIterator* mutable_iter_;
|
|
|
|
std::vector<InternalIterator*> imm_iters_;
|
|
|
|
std::vector<InternalIterator*> l0_iters_;
|
2018-02-13 21:44:22 +00:00
|
|
|
std::vector<ForwardLevelIterator*> level_iters_;
|
2015-10-12 22:06:38 +00:00
|
|
|
InternalIterator* current_;
|
2015-08-31 23:44:34 +00:00
|
|
|
bool valid_;
|
|
|
|
|
|
|
|
// Internal iterator status; set only by one of the unsupported methods.
|
2014-05-30 21:31:55 +00:00
|
|
|
Status status_;
|
2015-08-31 23:44:34 +00:00
|
|
|
// Status of immutable iterators, maintained here to avoid iterating over
|
|
|
|
// all of them in status().
|
2014-09-26 21:20:24 +00:00
|
|
|
Status immutable_status_;
|
2015-08-31 23:44:34 +00:00
|
|
|
// Indicates that at least one of the immutable iterators pointed to a key
|
|
|
|
// larger than iterate_upper_bound and was therefore destroyed. Seek() may
|
|
|
|
// need to rebuild such iterators.
|
2015-08-19 23:05:51 +00:00
|
|
|
bool has_iter_trimmed_for_upper_bound_;
|
2015-08-31 23:44:34 +00:00
|
|
|
// Is current key larger than iterate_upper_bound? If so, makes Valid()
|
|
|
|
// return false.
|
|
|
|
bool current_over_upper_bound_;
|
|
|
|
|
|
|
|
// Left endpoint of the range of keys that immutable iterators currently
|
|
|
|
// cover. When Seek() is called with a key that's within that range, immutable
|
|
|
|
// iterators don't need to be moved; see NeedToSeekImmutable(). This key is
|
|
|
|
// included in the range after a Seek(), but excluded when advancing the
|
|
|
|
// iterator using Next().
|
2014-05-30 21:31:55 +00:00
|
|
|
IterKey prev_key_;
|
|
|
|
bool is_prev_set_;
|
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-22 22:20:03 +00:00
|
|
|
bool is_prev_inclusive_;
|
2015-08-31 23:44:34 +00:00
|
|
|
|
2016-08-12 02:10:16 +00:00
|
|
|
PinnedIteratorsManager* pinned_iters_mgr_;
|
2014-09-05 00:40:41 +00:00
|
|
|
Arena arena_;
|
2014-05-30 21:31:55 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|