2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
//
|
|
|
|
// Decodes the blocks generated by block_builder.cc.
|
|
|
|
|
2019-05-30 23:47:29 +02:00
|
|
|
#include "table/block_based/block.h"
|
2022-05-04 02:37:19 +02:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <algorithm>
|
2014-04-10 23:19:43 +02:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/perf_context_imp.h"
|
2016-08-27 03:55:58 +02:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "port/stack_trace.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/comparator.h"
|
2019-05-30 23:47:29 +02:00
|
|
|
#include "table/block_based/block_prefix_index.h"
|
|
|
|
#include "table/block_based/data_block_footer.h"
|
2016-08-27 03:55:58 +02:00
|
|
|
#include "table/format.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// Helper routine: decode the next block entry starting at "p",
|
|
|
|
// storing the number of shared key bytes, non_shared key bytes,
|
|
|
|
// and the length of the value in "*shared", "*non_shared", and
|
|
|
|
// "*value_length", respectively. Will not derefence past "limit".
|
|
|
|
//
|
2013-03-01 03:04:58 +01:00
|
|
|
// If any errors are detected, returns nullptr. Otherwise, returns a
|
2011-03-18 23:37:00 +01:00
|
|
|
// pointer to the key delta (just past the three decoded values).
|
2018-08-10 01:49:45 +02:00
|
|
|
struct DecodeEntry {
|
|
|
|
inline const char* operator()(const char* p, const char* limit,
|
|
|
|
uint32_t* shared, uint32_t* non_shared,
|
|
|
|
uint32_t* value_length) {
|
|
|
|
// We need 2 bytes for shared and non_shared size. We also need one more
|
|
|
|
// byte either for value size or the actual value in case of value delta
|
|
|
|
// encoding.
|
|
|
|
assert(limit - p >= 3);
|
|
|
|
*shared = reinterpret_cast<const unsigned char*>(p)[0];
|
|
|
|
*non_shared = reinterpret_cast<const unsigned char*>(p)[1];
|
|
|
|
*value_length = reinterpret_cast<const unsigned char*>(p)[2];
|
|
|
|
if ((*shared | *non_shared | *value_length) < 128) {
|
|
|
|
// Fast path: all three values are encoded in one byte each
|
|
|
|
p += 3;
|
|
|
|
} else {
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, shared)) == nullptr) return nullptr;
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, non_shared)) == nullptr) return nullptr;
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, value_length)) == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2018-08-10 01:49:45 +02:00
|
|
|
// Using an assert in place of "return null" since we should not pay the
|
|
|
|
// cost of checking for corruption on every single key decoding
|
|
|
|
assert(!(static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-11 20:37:07 +01:00
|
|
|
// Helper routine: similar to DecodeEntry but does not have assertions.
|
|
|
|
// Instead, returns nullptr so that caller can detect and report failure.
|
|
|
|
struct CheckAndDecodeEntry {
|
|
|
|
inline const char* operator()(const char* p, const char* limit,
|
|
|
|
uint32_t* shared, uint32_t* non_shared,
|
|
|
|
uint32_t* value_length) {
|
|
|
|
// We need 2 bytes for shared and non_shared size. We also need one more
|
|
|
|
// byte either for value size or the actual value in case of value delta
|
|
|
|
// encoding.
|
|
|
|
if (limit - p < 3) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
*shared = reinterpret_cast<const unsigned char*>(p)[0];
|
|
|
|
*non_shared = reinterpret_cast<const unsigned char*>(p)[1];
|
|
|
|
*value_length = reinterpret_cast<const unsigned char*>(p)[2];
|
|
|
|
if ((*shared | *non_shared | *value_length) < 128) {
|
|
|
|
// Fast path: all three values are encoded in one byte each
|
|
|
|
p += 3;
|
|
|
|
} else {
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, shared)) == nullptr) return nullptr;
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, non_shared)) == nullptr) return nullptr;
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, value_length)) == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-10 01:49:45 +02:00
|
|
|
struct DecodeKey {
|
|
|
|
inline const char* operator()(const char* p, const char* limit,
|
|
|
|
uint32_t* shared, uint32_t* non_shared) {
|
|
|
|
uint32_t value_length;
|
|
|
|
return DecodeEntry()(p, limit, shared, non_shared, &value_length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// In format_version 4, which is used by index blocks, the value size is not
|
|
|
|
// encoded before the entry, as the value is known to be the handle with the
|
|
|
|
// known size.
|
|
|
|
struct DecodeKeyV4 {
|
|
|
|
inline const char* operator()(const char* p, const char* limit,
|
|
|
|
uint32_t* shared, uint32_t* non_shared) {
|
|
|
|
// We need 2 bytes for shared and non_shared size. We also need one more
|
|
|
|
// byte either for value size or the actual value in case of value delta
|
|
|
|
// encoding.
|
|
|
|
if (limit - p < 3) return nullptr;
|
|
|
|
*shared = reinterpret_cast<const unsigned char*>(p)[0];
|
|
|
|
*non_shared = reinterpret_cast<const unsigned char*>(p)[1];
|
|
|
|
if ((*shared | *non_shared) < 128) {
|
|
|
|
// Fast path: all three values are encoded in one byte each
|
|
|
|
p += 2;
|
|
|
|
} else {
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, shared)) == nullptr) return nullptr;
|
|
|
|
if ((p = GetVarint32Ptr(p, limit, non_shared)) == nullptr) return nullptr;
|
|
|
|
}
|
|
|
|
return p;
|
2011-04-21 00:48:11 +02:00
|
|
|
}
|
2018-08-10 01:49:45 +02:00
|
|
|
};
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
struct DecodeEntryV4 {
|
|
|
|
inline const char* operator()(const char* p, const char* limit,
|
|
|
|
uint32_t* shared, uint32_t* non_shared,
|
|
|
|
uint32_t* value_length) {
|
|
|
|
assert(value_length);
|
|
|
|
|
|
|
|
*value_length = 0;
|
|
|
|
return DecodeKeyV4()(p, limit, shared, non_shared);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void DataBlockIter::NextImpl() {
|
|
|
|
bool is_shared = false;
|
|
|
|
ParseNextDataKey(&is_shared);
|
|
|
|
}
|
2019-02-11 20:37:07 +01:00
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
void MetaBlockIter::NextImpl() {
|
|
|
|
bool is_shared = false;
|
|
|
|
ParseNextKey<CheckAndDecodeEntry>(&is_shared);
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void IndexBlockIter::NextImpl() { ParseNextIndexKey(); }
|
2018-07-17 02:10:44 +02:00
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void IndexBlockIter::PrevImpl() {
|
2018-07-17 02:10:44 +02:00
|
|
|
assert(Valid());
|
|
|
|
// Scan backwards to a restart point before current_
|
|
|
|
const uint32_t original = current_;
|
|
|
|
while (GetRestartPoint(restart_index_) >= original) {
|
|
|
|
if (restart_index_ == 0) {
|
|
|
|
// No more entries
|
|
|
|
current_ = restarts_;
|
|
|
|
restart_index_ = num_restarts_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
restart_index_--;
|
|
|
|
}
|
|
|
|
SeekToRestartPoint(restart_index_);
|
2020-02-22 00:07:55 +01:00
|
|
|
// Loop until end of current entry hits the start of original entry
|
|
|
|
while (ParseNextIndexKey() && NextEntryOffset() < original) {
|
|
|
|
}
|
2018-07-17 02:10:44 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
void MetaBlockIter::PrevImpl() {
|
|
|
|
assert(Valid());
|
|
|
|
// Scan backwards to a restart point before current_
|
|
|
|
const uint32_t original = current_;
|
|
|
|
while (GetRestartPoint(restart_index_) >= original) {
|
|
|
|
if (restart_index_ == 0) {
|
|
|
|
// No more entries
|
|
|
|
current_ = restarts_;
|
|
|
|
restart_index_ = num_restarts_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
restart_index_--;
|
|
|
|
}
|
|
|
|
SeekToRestartPoint(restart_index_);
|
|
|
|
bool is_shared = false;
|
|
|
|
// Loop until end of current entry hits the start of original entry
|
|
|
|
while (ParseNextKey<CheckAndDecodeEntry>(&is_shared) &&
|
|
|
|
NextEntryOffset() < original) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
// Similar to IndexBlockIter::PrevImpl but also caches the prev entries
|
|
|
|
void DataBlockIter::PrevImpl() {
|
2014-07-31 01:34:35 +02:00
|
|
|
assert(Valid());
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2016-06-14 21:27:46 +02:00
|
|
|
assert(prev_entries_idx_ == -1 ||
|
|
|
|
static_cast<size_t>(prev_entries_idx_) < prev_entries_.size());
|
|
|
|
// Check if we can use cached prev_entries_
|
|
|
|
if (prev_entries_idx_ > 0 &&
|
|
|
|
prev_entries_[prev_entries_idx_].offset == current_) {
|
|
|
|
// Read cached CachedPrevEntry
|
|
|
|
prev_entries_idx_--;
|
|
|
|
const CachedPrevEntry& current_prev_entry =
|
|
|
|
prev_entries_[prev_entries_idx_];
|
|
|
|
|
2016-07-09 02:50:51 +02:00
|
|
|
const char* key_ptr = nullptr;
|
2020-07-08 02:25:08 +02:00
|
|
|
bool raw_key_cached;
|
2016-06-14 21:27:46 +02:00
|
|
|
if (current_prev_entry.key_ptr != nullptr) {
|
|
|
|
// The key is not delta encoded and stored in the data block
|
|
|
|
key_ptr = current_prev_entry.key_ptr;
|
2020-07-08 02:25:08 +02:00
|
|
|
raw_key_cached = false;
|
2016-06-14 21:27:46 +02:00
|
|
|
} else {
|
|
|
|
// The key is delta encoded and stored in prev_entries_keys_buff_
|
|
|
|
key_ptr = prev_entries_keys_buff_.data() + current_prev_entry.key_offset;
|
2020-07-08 02:25:08 +02:00
|
|
|
raw_key_cached = true;
|
2016-06-14 21:27:46 +02:00
|
|
|
}
|
|
|
|
const Slice current_key(key_ptr, current_prev_entry.key_size);
|
|
|
|
|
|
|
|
current_ = current_prev_entry.offset;
|
2020-07-08 02:25:08 +02:00
|
|
|
// TODO(ajkr): the copy when `raw_key_cached` is done here for convenience,
|
|
|
|
// not necessity. It is convenient since this class treats keys as pinned
|
|
|
|
// when `raw_key_` points to an outside buffer. So we cannot allow
|
|
|
|
// `raw_key_` point into Prev cache as it is a transient outside buffer
|
|
|
|
// (i.e., keys in it are not actually pinned).
|
|
|
|
raw_key_.SetKey(current_key, raw_key_cached /* copy */);
|
2016-06-14 21:27:46 +02:00
|
|
|
value_ = current_prev_entry.value;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear prev entries cache
|
|
|
|
prev_entries_idx_ = -1;
|
|
|
|
prev_entries_.clear();
|
|
|
|
prev_entries_keys_buff_.clear();
|
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
// Scan backwards to a restart point before current_
|
|
|
|
const uint32_t original = current_;
|
|
|
|
while (GetRestartPoint(restart_index_) >= original) {
|
|
|
|
if (restart_index_ == 0) {
|
|
|
|
// No more entries
|
|
|
|
current_ = restarts_;
|
|
|
|
restart_index_ = num_restarts_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
restart_index_--;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
SeekToRestartPoint(restart_index_);
|
2016-06-14 21:27:46 +02:00
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
do {
|
2021-12-21 20:29:52 +01:00
|
|
|
bool is_shared = false;
|
|
|
|
if (!ParseNextDataKey(&is_shared)) {
|
2016-06-14 21:27:46 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-05-28 19:49:02 +02:00
|
|
|
Slice current_key = raw_key_.GetKey();
|
2016-06-14 21:27:46 +02:00
|
|
|
|
2020-05-28 19:49:02 +02:00
|
|
|
if (raw_key_.IsKeyPinned()) {
|
2016-06-14 21:27:46 +02:00
|
|
|
// The key is not delta encoded
|
|
|
|
prev_entries_.emplace_back(current_, current_key.data(), 0,
|
|
|
|
current_key.size(), value());
|
|
|
|
} else {
|
|
|
|
// The key is delta encoded, cache decoded key in buffer
|
|
|
|
size_t new_key_offset = prev_entries_keys_buff_.size();
|
|
|
|
prev_entries_keys_buff_.append(current_key.data(), current_key.size());
|
|
|
|
|
|
|
|
prev_entries_.emplace_back(current_, nullptr, new_key_offset,
|
|
|
|
current_key.size(), value());
|
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
// Loop until end of current entry hits the start of original entry
|
2016-06-14 21:27:46 +02:00
|
|
|
} while (NextEntryOffset() < original);
|
2016-06-15 09:24:33 +02:00
|
|
|
prev_entries_idx_ = static_cast<int32_t>(prev_entries_.size()) - 1;
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void DataBlockIter::SeekImpl(const Slice& target) {
|
2018-07-13 02:19:57 +02:00
|
|
|
Slice seek_key = target;
|
|
|
|
PERF_TIMER_GUARD(block_seek_nanos);
|
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t index = 0;
|
2020-06-10 22:56:25 +02:00
|
|
|
bool skip_linear_scan = false;
|
2020-07-09 21:25:40 +02:00
|
|
|
bool ok = BinarySeek<DecodeKey>(seek_key, &index, &skip_linear_scan);
|
2018-07-13 02:19:57 +02:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-08 02:25:08 +02:00
|
|
|
FindKeyAfterBinarySeek(seek_key, index, skip_linear_scan);
|
2018-07-13 02:19:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
void MetaBlockIter::SeekImpl(const Slice& target) {
|
|
|
|
Slice seek_key = target;
|
|
|
|
PERF_TIMER_GUARD(block_seek_nanos);
|
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t index = 0;
|
|
|
|
bool skip_linear_scan = false;
|
|
|
|
bool ok = BinarySeek<DecodeKey>(seek_key, &index, &skip_linear_scan);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FindKeyAfterBinarySeek(seek_key, index, skip_linear_scan);
|
|
|
|
}
|
|
|
|
|
2018-08-15 23:27:47 +02:00
|
|
|
// Optimized Seek for point lookup for an internal key `target`
|
|
|
|
// target = "seek_user_key @ type | seqno".
|
|
|
|
//
|
|
|
|
// For any type other than kTypeValue, kTypeDeletion, kTypeSingleDeletion,
|
|
|
|
// or kTypeBlobIndex, this function behaves identically as Seek().
|
|
|
|
//
|
|
|
|
// For any type in kTypeValue, kTypeDeletion, kTypeSingleDeletion,
|
|
|
|
// or kTypeBlobIndex:
|
|
|
|
//
|
|
|
|
// If the return value is FALSE, iter location is undefined, and it means:
|
|
|
|
// 1) there is no key in this block falling into the range:
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
// ["seek_user_key @ type | seqno", "seek_user_key @ kTypeDeletion | 0"],
|
2018-08-15 23:27:47 +02:00
|
|
|
// inclusive; AND
|
|
|
|
// 2) the last key of this block has a greater user_key from seek_user_key
|
|
|
|
//
|
|
|
|
// If the return value is TRUE, iter location has two possibilies:
|
|
|
|
// 1) If iter is valid, it is set to a location as if set by BinarySeek. In
|
2020-05-28 19:49:02 +02:00
|
|
|
// this case, it points to the first key with a larger user_key or a matching
|
|
|
|
// user_key with a seqno no greater than the seeking seqno.
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
// 2) If the iter is invalid, it means that either all the user_key is less
|
|
|
|
// than the seek_user_key, or the block ends with a matching user_key but
|
|
|
|
// with a smaller [ type | seqno ] (i.e. a larger seqno, or the same seqno
|
|
|
|
// but larger type).
|
2018-08-15 23:27:47 +02:00
|
|
|
bool DataBlockIter::SeekForGetImpl(const Slice& target) {
|
2019-04-16 20:32:03 +02:00
|
|
|
Slice target_user_key = ExtractUserKey(target);
|
2018-08-15 23:27:47 +02:00
|
|
|
uint32_t map_offset = restarts_ + num_restarts_ * sizeof(uint32_t);
|
2019-04-16 20:32:03 +02:00
|
|
|
uint8_t entry =
|
|
|
|
data_block_hash_index_->Lookup(data_, map_offset, target_user_key);
|
2018-08-15 23:27:47 +02:00
|
|
|
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
if (entry == kCollision) {
|
|
|
|
// HashSeek not effective, falling back
|
2020-07-08 02:25:08 +02:00
|
|
|
SeekImpl(target);
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-15 23:27:47 +02:00
|
|
|
if (entry == kNoEntry) {
|
|
|
|
// Even if we cannot find the user_key in this block, the result may
|
2020-04-23 21:26:56 +02:00
|
|
|
// exist in the next block. Consider this example:
|
2018-08-15 23:27:47 +02:00
|
|
|
//
|
|
|
|
// Block N: [aab@100, ... , app@120]
|
2020-09-03 05:45:32 +02:00
|
|
|
// boundary key: axy@50 (we make minimal assumption about a boundary key)
|
2018-08-15 23:27:47 +02:00
|
|
|
// Block N+1: [axy@10, ... ]
|
|
|
|
//
|
|
|
|
// If seek_key = axy@60, the search will starts from Block N.
|
|
|
|
// Even if the user_key is not found in the hash map, the caller still
|
2020-04-23 21:26:56 +02:00
|
|
|
// have to continue searching the next block.
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
//
|
|
|
|
// In this case, we pretend the key is the the last restart interval.
|
|
|
|
// The while-loop below will search the last restart interval for the
|
|
|
|
// key. It will stop at the first key that is larger than the seek_key,
|
|
|
|
// or to the end of the block if no one is larger.
|
|
|
|
entry = static_cast<uint8_t>(num_restarts_ - 1);
|
2018-08-15 23:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t restart_index = entry;
|
|
|
|
|
|
|
|
// check if the key is in the restart_interval
|
|
|
|
assert(restart_index < num_restarts_);
|
|
|
|
SeekToRestartPoint(restart_index);
|
2021-12-21 20:29:52 +01:00
|
|
|
current_ = GetRestartPoint(restart_index);
|
2018-08-15 23:27:47 +02:00
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
uint32_t limit = restarts_;
|
|
|
|
if (restart_index + 1 < num_restarts_) {
|
|
|
|
limit = GetRestartPoint(restart_index + 1);
|
2018-08-15 23:27:47 +02:00
|
|
|
}
|
2021-12-21 20:29:52 +01:00
|
|
|
while (current_ < limit) {
|
|
|
|
bool shared;
|
2018-08-15 23:27:47 +02:00
|
|
|
// Here we only linear seek the target key inside the restart interval.
|
|
|
|
// If a key does not exist inside a restart interval, we avoid
|
2021-12-21 20:29:52 +01:00
|
|
|
// further searching the block content across restart interval boundary.
|
2018-08-15 23:27:47 +02:00
|
|
|
//
|
2021-12-21 20:29:52 +01:00
|
|
|
// TODO(fwu): check the left and right boundary of the restart interval
|
2018-08-15 23:27:47 +02:00
|
|
|
// to avoid linear seek a target key that is out of range.
|
2021-12-21 20:29:52 +01:00
|
|
|
if (!ParseNextDataKey(&shared) || CompareCurrentKey(target) >= 0) {
|
2018-08-15 23:27:47 +02:00
|
|
|
// we stop at the first potential matching user key.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_ == restarts_) {
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
// Search reaches to the end of the block. There are three possibilites:
|
2018-08-15 23:27:47 +02:00
|
|
|
// 1) there is only one user_key match in the block (otherwise collsion).
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
// the matching user_key resides in the last restart interval, and it
|
|
|
|
// is the last key of the restart interval and of the block as well.
|
2021-12-21 20:29:52 +01:00
|
|
|
// ParseNextKey() skiped it as its [ type | seqno ] is smaller.
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
//
|
|
|
|
// 2) The seek_key is not found in the HashIndex Lookup(), i.e. kNoEntry,
|
|
|
|
// AND all existing user_keys in the restart interval are smaller than
|
|
|
|
// seek_user_key.
|
2018-08-15 23:27:47 +02:00
|
|
|
//
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
// 3) The seek_key is a false positive and happens to be hashed to the
|
|
|
|
// last restart interval, AND all existing user_keys in the restart
|
|
|
|
// interval are smaller than seek_user_key.
|
2018-08-15 23:27:47 +02:00
|
|
|
//
|
DataBlockHashIndex: avoiding expensive iiter->Next when handling hash kNoEntry (#4296)
Summary:
When returning `kNoEntry` from HashIndex lookup, previously we invalidate the
`biter` by set `current_=restarts_`, so that the search can continue to the next
block in case the search result may reside in the next block.
There is one problem: when we are searching for a missing key, if the search
finds a `kNoEntry` and continue the search to the next block, there is also a
non-trivial possibility that the HashIndex return `kNoEntry` too, and the
expensive index iterator `Next()` will happen several times for nothing.
The solution is that if the hash table returns `kNoEntry`, `SeekForGetImpl()` just search the last restart interval for the key. It will stop at the first key that is large than the seek_key, or to the end of the block, and each case will be handled correctly.
Microbenchmark script:
```
TEST_TMPDIR=/dev/shm ./db_bench --benchmarks=fillseq,readtocache,readmissing \
--cache_size=20000000000 --use_data_block_hash_index={true|false}
```
`readmissing` performance (lower is better):
```
binary: 3.6098 micros/op
hash (before applying diff): 4.1048 micros/op
hash (after applying diff): 3.3502 micros/op
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4296
Differential Revision: D9419159
Pulled By: fgwu
fbshipit-source-id: 21e3eedcccbc47a249aa8eb4bf405c9def0b8a05
2018-08-23 19:12:15 +02:00
|
|
|
// The result may exist in the next block each case, so we return true.
|
2018-08-15 23:27:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-04 02:37:19 +02:00
|
|
|
if (icmp_->user_comparator()->Compare(raw_key_.GetUserKey(),
|
|
|
|
target_user_key) != 0) {
|
2018-08-15 23:27:47 +02:00
|
|
|
// the key is not in this block and cannot be at the next block either.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we are conservative and only support a limited set of cases
|
2020-07-08 02:25:08 +02:00
|
|
|
ValueType value_type = ExtractValueType(raw_key_.GetInternalKey());
|
2018-08-15 23:27:47 +02:00
|
|
|
if (value_type != ValueType::kTypeValue &&
|
|
|
|
value_type != ValueType::kTypeDeletion &&
|
|
|
|
value_type != ValueType::kTypeSingleDeletion &&
|
|
|
|
value_type != ValueType::kTypeBlobIndex) {
|
2020-07-08 02:25:08 +02:00
|
|
|
SeekImpl(target);
|
2018-08-15 23:27:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result found, and the iter is correctly set.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void IndexBlockIter::SeekImpl(const Slice& target) {
|
2019-05-01 23:23:48 +02:00
|
|
|
TEST_SYNC_POINT("IndexBlockIter::Seek:0");
|
2015-07-09 01:34:48 +02:00
|
|
|
PERF_TIMER_GUARD(block_seek_nanos);
|
2014-07-31 01:34:35 +02:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2020-04-21 01:30:34 +02:00
|
|
|
Slice seek_key = target;
|
2020-07-08 02:25:08 +02:00
|
|
|
if (raw_key_.IsUserKey()) {
|
2020-04-21 01:30:34 +02:00
|
|
|
seek_key = ExtractUserKey(target);
|
|
|
|
}
|
2020-01-16 19:46:05 +01:00
|
|
|
status_ = Status::OK();
|
2014-07-31 01:34:35 +02:00
|
|
|
uint32_t index = 0;
|
2020-06-10 22:56:25 +02:00
|
|
|
bool skip_linear_scan = false;
|
2014-07-31 01:34:35 +02:00
|
|
|
bool ok = false;
|
|
|
|
if (prefix_index_) {
|
2020-01-15 23:03:18 +01:00
|
|
|
bool prefix_may_exist = true;
|
|
|
|
ok = PrefixSeek(target, &index, &prefix_may_exist);
|
|
|
|
if (!prefix_may_exist) {
|
|
|
|
// This is to let the caller to distinguish between non-existing prefix,
|
|
|
|
// and when key is larger than the last key, which both set Valid() to
|
|
|
|
// false.
|
|
|
|
current_ = restarts_;
|
|
|
|
status_ = Status::NotFound();
|
|
|
|
}
|
2020-06-10 22:56:25 +02:00
|
|
|
// restart interval must be one when hash search is enabled so the binary
|
|
|
|
// search simply lands at the right place.
|
|
|
|
skip_linear_scan = true;
|
2018-08-10 01:49:45 +02:00
|
|
|
} else if (value_delta_encoded_) {
|
2020-07-09 21:25:40 +02:00
|
|
|
ok = BinarySeek<DecodeKeyV4>(seek_key, &index, &skip_linear_scan);
|
2014-07-31 01:34:35 +02:00
|
|
|
} else {
|
2020-07-09 21:25:40 +02:00
|
|
|
ok = BinarySeek<DecodeKey>(seek_key, &index, &skip_linear_scan);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
if (!ok) {
|
|
|
|
return;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2020-07-08 02:25:08 +02:00
|
|
|
FindKeyAfterBinarySeek(seek_key, index, skip_linear_scan);
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void DataBlockIter::SeekForPrevImpl(const Slice& target) {
|
2016-09-28 03:20:57 +02:00
|
|
|
PERF_TIMER_GUARD(block_seek_nanos);
|
2018-05-26 03:41:31 +02:00
|
|
|
Slice seek_key = target;
|
2016-09-28 03:20:57 +02:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t index = 0;
|
2020-06-10 22:56:25 +02:00
|
|
|
bool skip_linear_scan = false;
|
2020-07-09 21:25:40 +02:00
|
|
|
bool ok = BinarySeek<DecodeKey>(seek_key, &index, &skip_linear_scan);
|
2016-09-28 03:20:57 +02:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-08 02:25:08 +02:00
|
|
|
FindKeyAfterBinarySeek(seek_key, index, skip_linear_scan);
|
2016-09-28 03:20:57 +02:00
|
|
|
|
|
|
|
if (!Valid()) {
|
2020-07-08 02:25:08 +02:00
|
|
|
SeekToLastImpl();
|
2016-09-28 03:20:57 +02:00
|
|
|
} else {
|
2020-07-08 02:25:08 +02:00
|
|
|
while (Valid() && CompareCurrentKey(seek_key) > 0) {
|
|
|
|
PrevImpl();
|
2016-09-28 03:20:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
void MetaBlockIter::SeekForPrevImpl(const Slice& target) {
|
|
|
|
PERF_TIMER_GUARD(block_seek_nanos);
|
|
|
|
Slice seek_key = target;
|
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t index = 0;
|
|
|
|
bool skip_linear_scan = false;
|
|
|
|
bool ok = BinarySeek<DecodeKey>(seek_key, &index, &skip_linear_scan);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FindKeyAfterBinarySeek(seek_key, index, skip_linear_scan);
|
|
|
|
|
|
|
|
if (!Valid()) {
|
|
|
|
SeekToLastImpl();
|
|
|
|
} else {
|
|
|
|
while (Valid() && CompareCurrentKey(seek_key) > 0) {
|
|
|
|
PrevImpl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void DataBlockIter::SeekToFirstImpl() {
|
2018-07-17 02:10:44 +02:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SeekToRestartPoint(0);
|
2021-12-21 20:29:52 +01:00
|
|
|
bool is_shared = false;
|
|
|
|
ParseNextDataKey(&is_shared);
|
2019-02-11 20:37:07 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
void MetaBlockIter::SeekToFirstImpl() {
|
2019-02-11 20:37:07 +01:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SeekToRestartPoint(0);
|
2021-12-21 20:29:52 +01:00
|
|
|
bool is_shared = false;
|
|
|
|
ParseNextKey<CheckAndDecodeEntry>(&is_shared);
|
2018-07-17 02:10:44 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void IndexBlockIter::SeekToFirstImpl() {
|
2014-07-31 01:34:35 +02:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2020-01-16 19:46:05 +01:00
|
|
|
status_ = Status::OK();
|
2014-07-31 01:34:35 +02:00
|
|
|
SeekToRestartPoint(0);
|
2018-07-17 02:10:44 +02:00
|
|
|
ParseNextIndexKey();
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void DataBlockIter::SeekToLastImpl() {
|
2014-07-31 01:34:35 +02:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SeekToRestartPoint(num_restarts_ - 1);
|
2021-12-21 20:29:52 +01:00
|
|
|
bool is_shared = false;
|
|
|
|
while (ParseNextDataKey(&is_shared) && NextEntryOffset() < restarts_) {
|
|
|
|
// Keep skipping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetaBlockIter::SeekToLastImpl() {
|
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SeekToRestartPoint(num_restarts_ - 1);
|
|
|
|
bool is_shared = false;
|
|
|
|
while (ParseNextKey<CheckAndDecodeEntry>(&is_shared) &&
|
|
|
|
NextEntryOffset() < restarts_) {
|
2018-07-17 02:10:44 +02:00
|
|
|
// Keep skipping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:25:08 +02:00
|
|
|
void IndexBlockIter::SeekToLastImpl() {
|
2018-07-17 02:10:44 +02:00
|
|
|
if (data_ == nullptr) { // Not init yet
|
|
|
|
return;
|
|
|
|
}
|
2020-01-16 19:46:05 +01:00
|
|
|
status_ = Status::OK();
|
2018-07-17 02:10:44 +02:00
|
|
|
SeekToRestartPoint(num_restarts_ - 1);
|
|
|
|
while (ParseNextIndexKey() && NextEntryOffset() < restarts_) {
|
2014-07-31 01:34:35 +02:00
|
|
|
// Keep skipping
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 01:49:45 +02:00
|
|
|
template <class TValue>
|
|
|
|
void BlockIter<TValue>::CorruptionError() {
|
2014-07-31 01:34:35 +02:00
|
|
|
current_ = restarts_;
|
|
|
|
restart_index_ = num_restarts_;
|
|
|
|
status_ = Status::Corruption("bad entry in block");
|
2020-05-28 19:49:02 +02:00
|
|
|
raw_key_.Clear();
|
2014-07-31 01:34:35 +02:00
|
|
|
value_.clear();
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
template <class TValue>
|
2019-02-11 20:37:07 +01:00
|
|
|
template <typename DecodeEntryFunc>
|
2021-12-21 20:29:52 +01:00
|
|
|
bool BlockIter<TValue>::ParseNextKey(bool* is_shared) {
|
2016-09-28 03:20:57 +02:00
|
|
|
current_ = NextEntryOffset();
|
|
|
|
const char* p = data_ + current_;
|
2021-12-21 20:29:52 +01:00
|
|
|
const char* limit = data_ + restarts_; // Restarts come right after data
|
2018-08-15 23:27:47 +02:00
|
|
|
|
2016-09-28 03:20:57 +02:00
|
|
|
if (p >= limit) {
|
|
|
|
// No more entries to return. Mark as invalid.
|
|
|
|
current_ = restarts_;
|
|
|
|
restart_index_ = num_restarts_;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Decode next entry
|
|
|
|
uint32_t shared, non_shared, value_length;
|
2019-02-11 20:37:07 +01:00
|
|
|
p = DecodeEntryFunc()(p, limit, &shared, &non_shared, &value_length);
|
2020-05-28 19:49:02 +02:00
|
|
|
if (p == nullptr || raw_key_.Size() < shared) {
|
2016-09-28 03:20:57 +02:00
|
|
|
CorruptionError();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (shared == 0) {
|
2021-12-21 20:29:52 +01:00
|
|
|
*is_shared = false;
|
2020-02-28 23:10:51 +01:00
|
|
|
// If this key doesn't share any bytes with prev key then we don't need
|
|
|
|
// to decode it and can use its address in the block directly.
|
2020-05-28 19:49:02 +02:00
|
|
|
raw_key_.SetKey(Slice(p, non_shared), false /* copy */);
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
2016-09-28 03:20:57 +02:00
|
|
|
// This key share `shared` bytes with prev key, we need to decode it
|
2021-12-21 20:29:52 +01:00
|
|
|
*is_shared = true;
|
2020-05-28 19:49:02 +02:00
|
|
|
raw_key_.TrimAppend(shared, p, non_shared);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2021-12-21 20:29:52 +01:00
|
|
|
value_ = Slice(p + non_shared, value_length);
|
|
|
|
if (shared == 0) {
|
|
|
|
while (restart_index_ + 1 < num_restarts_ &&
|
|
|
|
GetRestartPoint(restart_index_ + 1) < current_) {
|
|
|
|
++restart_index_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else we are in the middle of a restart interval and the restart_index_
|
|
|
|
// thus has not changed
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 01:59:37 +02:00
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
bool DataBlockIter::ParseNextDataKey(bool* is_shared) {
|
|
|
|
if (ParseNextKey<DecodeEntry>(is_shared)) {
|
2020-05-28 19:49:02 +02:00
|
|
|
#ifndef NDEBUG
|
2016-10-19 01:59:37 +02:00
|
|
|
if (global_seqno_ != kDisableGlobalSequenceNumber) {
|
|
|
|
// If we are reading a file with a global sequence number we should
|
2017-05-26 21:05:19 +02:00
|
|
|
// expect that all encoded sequence numbers are zeros and any value
|
2021-09-10 03:57:01 +02:00
|
|
|
// type is kTypeValue, kTypeMerge, kTypeDeletion,
|
|
|
|
// kTypeDeletionWithTimestamp, or kTypeRangeDeletion.
|
2020-05-28 19:49:02 +02:00
|
|
|
uint64_t packed = ExtractInternalKeyFooter(raw_key_.GetKey());
|
|
|
|
SequenceNumber seqno;
|
|
|
|
ValueType value_type;
|
|
|
|
UnPackSequenceAndType(packed, &seqno, &value_type);
|
|
|
|
assert(value_type == ValueType::kTypeValue ||
|
|
|
|
value_type == ValueType::kTypeMerge ||
|
|
|
|
value_type == ValueType::kTypeDeletion ||
|
2021-09-10 03:57:01 +02:00
|
|
|
value_type == ValueType::kTypeDeletionWithTimestamp ||
|
2020-05-28 19:49:02 +02:00
|
|
|
value_type == ValueType::kTypeRangeDeletion);
|
|
|
|
assert(seqno == 0);
|
2016-10-19 01:59:37 +02:00
|
|
|
}
|
2020-05-28 19:49:02 +02:00
|
|
|
#endif // NDEBUG
|
2016-09-28 03:20:57 +02:00
|
|
|
return true;
|
2021-12-21 20:29:52 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2016-09-28 03:20:57 +02:00
|
|
|
}
|
2014-06-13 04:03:22 +02:00
|
|
|
|
2018-07-17 02:10:44 +02:00
|
|
|
bool IndexBlockIter::ParseNextIndexKey() {
|
2021-12-21 20:29:52 +01:00
|
|
|
bool is_shared = false;
|
|
|
|
bool ok = (value_delta_encoded_) ? ParseNextKey<DecodeEntryV4>(&is_shared)
|
|
|
|
: ParseNextKey<DecodeEntry>(&is_shared);
|
|
|
|
if (ok) {
|
|
|
|
if (value_delta_encoded_ || global_seqno_state_ != nullptr) {
|
|
|
|
DecodeCurrentValue(is_shared);
|
2018-08-10 01:49:45 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-21 20:29:52 +01:00
|
|
|
return ok;
|
2018-07-17 02:10:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 01:49:45 +02:00
|
|
|
// The format:
|
|
|
|
// restart_point 0: k, v (off, sz), k, v (delta-sz), ..., k, v (delta-sz)
|
|
|
|
// restart_point 1: k, v (off, sz), k, v (delta-sz), ..., k, v (delta-sz)
|
|
|
|
// ...
|
|
|
|
// restart_point n-1: k, v (off, sz), k, v (delta-sz), ..., k, v (delta-sz)
|
|
|
|
// where, k is key, v is value, and its encoding is in parenthesis.
|
|
|
|
// The format of each key is (shared_size, non_shared_size, shared, non_shared)
|
2020-04-23 21:26:56 +02:00
|
|
|
// The format of each value, i.e., block handle, is (offset, size) whenever the
|
2021-12-21 20:29:52 +01:00
|
|
|
// is_shared is false, which included the first entry in each restart point.
|
2018-08-10 01:49:45 +02:00
|
|
|
// Otherwise the format is delta-size = block handle size - size of last block
|
|
|
|
// handle.
|
2021-12-21 20:29:52 +01:00
|
|
|
void IndexBlockIter::DecodeCurrentValue(bool is_shared) {
|
Add an option to put first key of each sst block in the index (#5289)
Summary:
The first key is used to defer reading the data block until this file gets to the top of merging iterator's heap. For short range scans, most files never make it to the top of the heap, so this change can reduce read amplification by a lot sometimes.
Consider the following workload. There are a few data streams (we'll be calling them "logs"), each stream consisting of a sequence of blobs (we'll be calling them "records"). Each record is identified by log ID and a sequence number within the log. RocksDB key is concatenation of log ID and sequence number (big endian). Reads are mostly relatively short range scans, each within a single log. Writes are mostly sequential for each log, but writes to different logs are randomly interleaved. Compactions are disabled; instead, when we accumulate a few tens of sst files, we create a new column family and start writing to it.
So, a typical sst file consists of a few ranges of blocks, each range corresponding to one log ID (we use FlushBlockPolicy to cut blocks at log boundaries). A typical read would go like this. First, iterator Seek() reads one block from each sst file. Then a series of Next()s move through one sst file (since writes to each log are mostly sequential) until the subiterator reaches the end of this log in this sst file; then Next() switches to the next sst file and reads sequentially from that, and so on. Often a range scan will only return records from a small number of blocks in small number of sst files; in this case, the cost of initial Seek() reading one block from each file may be bigger than the cost of reading the actually useful blocks.
Neither iterate_upper_bound nor bloom filters can prevent reading one block from each file in Seek(). But this PR can: if the index contains first key from each block, we don't have to read the block until this block actually makes it to the top of merging iterator's heap, so for short range scans we won't read any blocks from most of the sst files.
This PR does the deferred block loading inside value() call. This is not ideal: there's no good way to report an IO error from inside value(). As discussed with siying offline, it would probably be better to change InternalIterator's interface to explicitly fetch deferred value and get status. I'll do it in a separate PR.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5289
Differential Revision: D15256423
Pulled By: al13n321
fbshipit-source-id: 750e4c39ce88e8d41662f701cf6275d9388ba46a
2019-06-25 05:50:35 +02:00
|
|
|
Slice v(value_.data(), data_ + restarts_ - value_.data());
|
|
|
|
// Delta encoding is used if `shared` != 0.
|
|
|
|
Status decode_s __attribute__((__unused__)) = decoded_value_.DecodeFrom(
|
|
|
|
&v, have_first_key_,
|
2021-12-21 20:29:52 +01:00
|
|
|
(value_delta_encoded_ && is_shared) ? &decoded_value_.handle : nullptr);
|
Add an option to put first key of each sst block in the index (#5289)
Summary:
The first key is used to defer reading the data block until this file gets to the top of merging iterator's heap. For short range scans, most files never make it to the top of the heap, so this change can reduce read amplification by a lot sometimes.
Consider the following workload. There are a few data streams (we'll be calling them "logs"), each stream consisting of a sequence of blobs (we'll be calling them "records"). Each record is identified by log ID and a sequence number within the log. RocksDB key is concatenation of log ID and sequence number (big endian). Reads are mostly relatively short range scans, each within a single log. Writes are mostly sequential for each log, but writes to different logs are randomly interleaved. Compactions are disabled; instead, when we accumulate a few tens of sst files, we create a new column family and start writing to it.
So, a typical sst file consists of a few ranges of blocks, each range corresponding to one log ID (we use FlushBlockPolicy to cut blocks at log boundaries). A typical read would go like this. First, iterator Seek() reads one block from each sst file. Then a series of Next()s move through one sst file (since writes to each log are mostly sequential) until the subiterator reaches the end of this log in this sst file; then Next() switches to the next sst file and reads sequentially from that, and so on. Often a range scan will only return records from a small number of blocks in small number of sst files; in this case, the cost of initial Seek() reading one block from each file may be bigger than the cost of reading the actually useful blocks.
Neither iterate_upper_bound nor bloom filters can prevent reading one block from each file in Seek(). But this PR can: if the index contains first key from each block, we don't have to read the block until this block actually makes it to the top of merging iterator's heap, so for short range scans we won't read any blocks from most of the sst files.
This PR does the deferred block loading inside value() call. This is not ideal: there's no good way to report an IO error from inside value(). As discussed with siying offline, it would probably be better to change InternalIterator's interface to explicitly fetch deferred value and get status. I'll do it in a separate PR.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5289
Differential Revision: D15256423
Pulled By: al13n321
fbshipit-source-id: 750e4c39ce88e8d41662f701cf6275d9388ba46a
2019-06-25 05:50:35 +02:00
|
|
|
assert(decode_s.ok());
|
|
|
|
value_ = Slice(value_.data(), v.data() - value_.data());
|
|
|
|
|
|
|
|
if (global_seqno_state_ != nullptr) {
|
|
|
|
// Overwrite sequence number the same way as in DataBlockIter.
|
|
|
|
|
|
|
|
IterKey& first_internal_key = global_seqno_state_->first_internal_key;
|
|
|
|
first_internal_key.SetInternalKey(decoded_value_.first_internal_key,
|
|
|
|
/* copy */ true);
|
|
|
|
|
|
|
|
assert(GetInternalKeySeqno(first_internal_key.GetInternalKey()) == 0);
|
|
|
|
|
|
|
|
ValueType value_type = ExtractValueType(first_internal_key.GetKey());
|
|
|
|
assert(value_type == ValueType::kTypeValue ||
|
|
|
|
value_type == ValueType::kTypeMerge ||
|
|
|
|
value_type == ValueType::kTypeDeletion ||
|
|
|
|
value_type == ValueType::kTypeRangeDeletion);
|
|
|
|
|
|
|
|
first_internal_key.UpdateInternalKey(global_seqno_state_->global_seqno,
|
|
|
|
value_type);
|
|
|
|
decoded_value_.first_internal_key = first_internal_key.GetKey();
|
2018-08-10 01:49:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 22:56:25 +02:00
|
|
|
template <class TValue>
|
|
|
|
void BlockIter<TValue>::FindKeyAfterBinarySeek(const Slice& target,
|
|
|
|
uint32_t index,
|
2020-07-08 02:25:08 +02:00
|
|
|
bool skip_linear_scan) {
|
2020-06-10 22:56:25 +02:00
|
|
|
// SeekToRestartPoint() only does the lookup in the restart block. We need
|
2020-07-08 02:25:08 +02:00
|
|
|
// to follow it up with NextImpl() to position the iterator at the restart
|
|
|
|
// key.
|
2020-06-10 22:56:25 +02:00
|
|
|
SeekToRestartPoint(index);
|
2020-07-08 02:25:08 +02:00
|
|
|
NextImpl();
|
2020-06-10 22:56:25 +02:00
|
|
|
|
|
|
|
if (!skip_linear_scan) {
|
|
|
|
// Linear search (within restart block) for first key >= target
|
|
|
|
uint32_t max_offset;
|
|
|
|
if (index + 1 < num_restarts_) {
|
|
|
|
// We are in a non-last restart interval. Since `BinarySeek()` guarantees
|
|
|
|
// the next restart key is strictly greater than `target`, we can
|
|
|
|
// terminate upon reaching it without any additional key comparison.
|
|
|
|
max_offset = GetRestartPoint(index + 1);
|
|
|
|
} else {
|
|
|
|
// We are in the last restart interval. The while-loop will terminate by
|
|
|
|
// `Valid()` returning false upon advancing past the block's last key.
|
|
|
|
max_offset = port::kMaxUint32;
|
|
|
|
}
|
|
|
|
while (true) {
|
2020-07-08 02:25:08 +02:00
|
|
|
NextImpl();
|
2020-06-10 22:56:25 +02:00
|
|
|
if (!Valid()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (current_ == max_offset) {
|
2020-07-08 02:25:08 +02:00
|
|
|
assert(CompareCurrentKey(target) > 0);
|
2020-06-10 22:56:25 +02:00
|
|
|
break;
|
2020-07-08 02:25:08 +02:00
|
|
|
} else if (CompareCurrentKey(target) >= 0) {
|
2020-06-10 22:56:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary searches in restart array to find the starting restart point for the
|
|
|
|
// linear scan, and stores it in `*index`. Assumes restart array does not
|
|
|
|
// contain duplicate keys. It is guaranteed that the restart key at `*index + 1`
|
|
|
|
// is strictly greater than `target` or does not exist (this can be used to
|
|
|
|
// elide a comparison when linear scan reaches all the way to the next restart
|
|
|
|
// key). Furthermore, `*skip_linear_scan` is set to indicate whether the
|
|
|
|
// `*index`th restart key is the final result so that key does not need to be
|
|
|
|
// compared again later.
|
2018-08-10 01:49:45 +02:00
|
|
|
template <class TValue>
|
|
|
|
template <typename DecodeKeyFunc>
|
2020-07-09 21:25:40 +02:00
|
|
|
bool BlockIter<TValue>::BinarySeek(const Slice& target, uint32_t* index,
|
2020-07-08 02:25:08 +02:00
|
|
|
bool* skip_linear_scan) {
|
2020-06-10 22:56:25 +02:00
|
|
|
if (restarts_ == 0) {
|
|
|
|
// SST files dedicated to range tombstones are written with index blocks
|
|
|
|
// that have no keys while also having `num_restarts_ == 1`. This would
|
|
|
|
// cause a problem for `BinarySeek()` as it'd try to access the first key
|
|
|
|
// which does not exist. We identify such blocks by the offset at which
|
|
|
|
// their restarts are stored, and return false to prevent any attempted
|
|
|
|
// key accesses.
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-10 23:19:43 +02:00
|
|
|
|
2020-06-10 22:56:25 +02:00
|
|
|
*skip_linear_scan = false;
|
2020-07-09 21:25:40 +02:00
|
|
|
// Loop invariants:
|
|
|
|
// - Restart key at index `left` is less than or equal to the target key. The
|
|
|
|
// sentinel index `-1` is considered to have a key that is less than all
|
|
|
|
// keys.
|
|
|
|
// - Any restart keys after index `right` are strictly greater than the target
|
|
|
|
// key.
|
|
|
|
int64_t left = -1, right = num_restarts_ - 1;
|
|
|
|
while (left != right) {
|
|
|
|
// The `mid` is computed by rounding up so it lands in (`left`, `right`].
|
|
|
|
int64_t mid = left + (right - left + 1) / 2;
|
|
|
|
uint32_t region_offset = GetRestartPoint(static_cast<uint32_t>(mid));
|
2018-08-10 01:49:45 +02:00
|
|
|
uint32_t shared, non_shared;
|
|
|
|
const char* key_ptr = DecodeKeyFunc()(
|
|
|
|
data_ + region_offset, data_ + restarts_, &shared, &non_shared);
|
Block::Iter::PrefixSeek() to have an extra check to filter out some false matches
Summary:
In block based table's hash index checking, when looking for a key that doesn't exist, there is a high chance that a false block is returned because of hash bucket conflicts. In this revision, another check is done to filter out some of those cases: comparing previous key of the block boundary to see whether the target block is what we are looking for.
In a favored test setting (bloom filter disabled, 8 L0 files), I saw about 80% improvements. In a non-favored test setting (bloom filter enabled, files are all in L1, files are all cached), I see the performance penalty is less than 3%.
Test Plan: make all check
Reviewers: haobo, ljin
Reviewed By: ljin
Subscribers: wuj, leveldb, zagfox, yhchiang
Differential Revision: https://reviews.facebook.net/D20595
2014-07-25 23:38:18 +02:00
|
|
|
if (key_ptr == nullptr || (shared != 0)) {
|
|
|
|
CorruptionError();
|
2014-07-31 01:34:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Slice mid_key(key_ptr, non_shared);
|
2020-05-28 19:49:02 +02:00
|
|
|
raw_key_.SetKey(mid_key, false /* copy */);
|
2020-07-08 02:25:08 +02:00
|
|
|
int cmp = CompareCurrentKey(target);
|
2014-07-31 01:34:35 +02:00
|
|
|
if (cmp < 0) {
|
|
|
|
// Key at "mid" is smaller than "target". Therefore all
|
|
|
|
// blocks before "mid" are uninteresting.
|
|
|
|
left = mid;
|
|
|
|
} else if (cmp > 0) {
|
|
|
|
// Key at "mid" is >= "target". Therefore all blocks at or
|
|
|
|
// after "mid" are uninteresting.
|
|
|
|
right = mid - 1;
|
|
|
|
} else {
|
2020-06-10 22:56:25 +02:00
|
|
|
*skip_linear_scan = true;
|
2014-07-31 01:34:35 +02:00
|
|
|
left = right = mid;
|
Block::Iter::PrefixSeek() to have an extra check to filter out some false matches
Summary:
In block based table's hash index checking, when looking for a key that doesn't exist, there is a high chance that a false block is returned because of hash bucket conflicts. In this revision, another check is done to filter out some of those cases: comparing previous key of the block boundary to see whether the target block is what we are looking for.
In a favored test setting (bloom filter disabled, 8 L0 files), I saw about 80% improvements. In a non-favored test setting (bloom filter enabled, files are all in L1, files are all cached), I see the performance penalty is less than 3%.
Test Plan: make all check
Reviewers: haobo, ljin
Reviewed By: ljin
Subscribers: wuj, leveldb, zagfox, yhchiang
Differential Revision: https://reviews.facebook.net/D20595
2014-07-25 23:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:25:40 +02:00
|
|
|
if (left == -1) {
|
|
|
|
// All keys in the block were strictly greater than `target`. So the very
|
|
|
|
// first key in the block is the final seek result.
|
|
|
|
*skip_linear_scan = true;
|
|
|
|
*index = 0;
|
|
|
|
} else {
|
|
|
|
*index = static_cast<uint32_t>(left);
|
2020-06-10 22:56:25 +02:00
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-13 04:03:22 +02:00
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
// Compare target key and the block key of the block of `block_index`.
|
2018-07-17 02:10:44 +02:00
|
|
|
// Return -1 if error.
|
2018-07-13 02:19:57 +02:00
|
|
|
int IndexBlockIter::CompareBlockKey(uint32_t block_index, const Slice& target) {
|
2014-07-31 01:34:35 +02:00
|
|
|
uint32_t region_offset = GetRestartPoint(block_index);
|
2018-08-10 01:49:45 +02:00
|
|
|
uint32_t shared, non_shared;
|
|
|
|
const char* key_ptr =
|
|
|
|
value_delta_encoded_
|
|
|
|
? DecodeKeyV4()(data_ + region_offset, data_ + restarts_, &shared,
|
|
|
|
&non_shared)
|
|
|
|
: DecodeKey()(data_ + region_offset, data_ + restarts_, &shared,
|
|
|
|
&non_shared);
|
2014-07-31 01:34:35 +02:00
|
|
|
if (key_ptr == nullptr || (shared != 0)) {
|
|
|
|
CorruptionError();
|
|
|
|
return 1; // Return target is smaller
|
|
|
|
}
|
|
|
|
Slice block_key(key_ptr, non_shared);
|
2020-05-28 19:49:02 +02:00
|
|
|
raw_key_.SetKey(block_key, false /* copy */);
|
2020-07-08 02:25:08 +02:00
|
|
|
return CompareCurrentKey(target);
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
Block::Iter::PrefixSeek() to have an extra check to filter out some false matches
Summary:
In block based table's hash index checking, when looking for a key that doesn't exist, there is a high chance that a false block is returned because of hash bucket conflicts. In this revision, another check is done to filter out some of those cases: comparing previous key of the block boundary to see whether the target block is what we are looking for.
In a favored test setting (bloom filter disabled, 8 L0 files), I saw about 80% improvements. In a non-favored test setting (bloom filter enabled, files are all in L1, files are all cached), I see the performance penalty is less than 3%.
Test Plan: make all check
Reviewers: haobo, ljin
Reviewed By: ljin
Subscribers: wuj, leveldb, zagfox, yhchiang
Differential Revision: https://reviews.facebook.net/D20595
2014-07-25 23:38:18 +02:00
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
// Binary search in block_ids to find the first block
|
|
|
|
// with a key >= target
|
2018-07-13 02:19:57 +02:00
|
|
|
bool IndexBlockIter::BinaryBlockIndexSeek(const Slice& target,
|
|
|
|
uint32_t* block_ids, uint32_t left,
|
2020-01-15 23:03:18 +01:00
|
|
|
uint32_t right, uint32_t* index,
|
|
|
|
bool* prefix_may_exist) {
|
2014-07-31 01:34:35 +02:00
|
|
|
assert(left <= right);
|
2020-01-15 23:03:18 +01:00
|
|
|
assert(index);
|
|
|
|
assert(prefix_may_exist);
|
|
|
|
*prefix_may_exist = true;
|
2014-07-31 01:34:35 +02:00
|
|
|
uint32_t left_bound = left;
|
2014-06-13 04:03:22 +02:00
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
while (left <= right) {
|
2016-09-28 03:20:57 +02:00
|
|
|
uint32_t mid = (right + left) / 2;
|
Block::Iter::PrefixSeek() to have an extra check to filter out some false matches
Summary:
In block based table's hash index checking, when looking for a key that doesn't exist, there is a high chance that a false block is returned because of hash bucket conflicts. In this revision, another check is done to filter out some of those cases: comparing previous key of the block boundary to see whether the target block is what we are looking for.
In a favored test setting (bloom filter disabled, 8 L0 files), I saw about 80% improvements. In a non-favored test setting (bloom filter enabled, files are all in L1, files are all cached), I see the performance penalty is less than 3%.
Test Plan: make all check
Reviewers: haobo, ljin
Reviewed By: ljin
Subscribers: wuj, leveldb, zagfox, yhchiang
Differential Revision: https://reviews.facebook.net/D20595
2014-07-25 23:38:18 +02:00
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
int cmp = CompareBlockKey(block_ids[mid], target);
|
|
|
|
if (!status_.ok()) {
|
2014-06-13 04:03:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
if (cmp < 0) {
|
|
|
|
// Key at "target" is larger than "mid". Therefore all
|
|
|
|
// blocks before or at "mid" are uninteresting.
|
|
|
|
left = mid + 1;
|
|
|
|
} else {
|
|
|
|
// Key at "target" is <= "mid". Therefore all blocks
|
|
|
|
// after "mid" are uninteresting.
|
|
|
|
// If there is only one block left, we found it.
|
|
|
|
if (left == right) break;
|
|
|
|
right = mid;
|
|
|
|
}
|
2014-06-13 04:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
if (left == right) {
|
|
|
|
// In one of the two following cases:
|
|
|
|
// (1) left is the first one of block_ids
|
|
|
|
// (2) there is a gap of blocks between block of `left` and `left-1`.
|
|
|
|
// we can further distinguish the case of key in the block or key not
|
|
|
|
// existing, by comparing the target key and the key of the previous
|
|
|
|
// block to the left of the block found.
|
|
|
|
if (block_ids[left] > 0 &&
|
|
|
|
(left == left_bound || block_ids[left - 1] != block_ids[left] - 1) &&
|
|
|
|
CompareBlockKey(block_ids[left] - 1, target) > 0) {
|
2014-04-10 23:19:43 +02:00
|
|
|
current_ = restarts_;
|
2020-01-15 23:03:18 +01:00
|
|
|
*prefix_may_exist = false;
|
2014-06-13 04:03:22 +02:00
|
|
|
return false;
|
2014-04-10 23:19:43 +02:00
|
|
|
}
|
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
*index = block_ids[left];
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
assert(left > right);
|
2020-01-15 23:03:18 +01:00
|
|
|
|
|
|
|
// If the next block key is larger than seek key, it is possible that
|
|
|
|
// no key shares the prefix with `target`, or all keys with the same
|
|
|
|
// prefix as `target` are smaller than prefix. In the latter case,
|
|
|
|
// we are mandated to set the position the same as the total order.
|
|
|
|
// In the latter case, either:
|
|
|
|
// (1) `target` falls into the range of the next block. In this case,
|
|
|
|
// we can place the iterator to the next block, or
|
|
|
|
// (2) `target` is larger than all block keys. In this case we can
|
|
|
|
// keep the iterator invalidate without setting `prefix_may_exist`
|
|
|
|
// to false.
|
|
|
|
// We might sometimes end up with setting the total order position
|
|
|
|
// while there is no key sharing the prefix as `target`, but it
|
|
|
|
// still follows the contract.
|
|
|
|
uint32_t right_index = block_ids[right];
|
|
|
|
assert(right_index + 1 <= num_restarts_);
|
|
|
|
if (right_index + 1 < num_restarts_) {
|
|
|
|
if (CompareBlockKey(right_index + 1, target) >= 0) {
|
|
|
|
*index = right_index + 1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// We have to set the flag here because we are not positioning
|
|
|
|
// the iterator to the total order position.
|
|
|
|
*prefix_may_exist = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
// Mark iterator invalid
|
|
|
|
current_ = restarts_;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 23:03:18 +01:00
|
|
|
bool IndexBlockIter::PrefixSeek(const Slice& target, uint32_t* index,
|
|
|
|
bool* prefix_may_exist) {
|
|
|
|
assert(index);
|
|
|
|
assert(prefix_may_exist);
|
2014-07-31 01:34:35 +02:00
|
|
|
assert(prefix_index_);
|
2020-01-15 23:03:18 +01:00
|
|
|
*prefix_may_exist = true;
|
2018-05-26 03:41:31 +02:00
|
|
|
Slice seek_key = target;
|
2020-07-08 02:25:08 +02:00
|
|
|
if (raw_key_.IsUserKey()) {
|
2018-05-26 03:41:31 +02:00
|
|
|
seek_key = ExtractUserKey(target);
|
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
uint32_t* block_ids = nullptr;
|
|
|
|
uint32_t num_blocks = prefix_index_->GetBlocks(target, &block_ids);
|
2014-06-13 04:03:22 +02:00
|
|
|
|
2014-07-31 01:34:35 +02:00
|
|
|
if (num_blocks == 0) {
|
|
|
|
current_ = restarts_;
|
2020-01-15 23:03:18 +01:00
|
|
|
*prefix_may_exist = false;
|
2014-07-31 01:34:35 +02:00
|
|
|
return false;
|
2019-03-28 00:13:08 +01:00
|
|
|
} else {
|
2020-01-15 23:03:18 +01:00
|
|
|
assert(block_ids);
|
|
|
|
return BinaryBlockIndexSeek(seek_key, block_ids, 0, num_blocks - 1, index,
|
|
|
|
prefix_may_exist);
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Block::NumRestarts() const {
|
2019-03-28 00:13:08 +01:00
|
|
|
assert(size_ >= 2 * sizeof(uint32_t));
|
2018-08-15 23:27:47 +02:00
|
|
|
uint32_t block_footer = DecodeFixed32(data_ + size_ - sizeof(uint32_t));
|
|
|
|
uint32_t num_restarts = block_footer;
|
|
|
|
if (size_ > kMaxBlockSizeSupportedByHashIndex) {
|
2018-08-22 02:06:44 +02:00
|
|
|
// In BlockBuilder, we have ensured a block with HashIndex is less than
|
|
|
|
// kMaxBlockSizeSupportedByHashIndex (64KiB).
|
|
|
|
//
|
|
|
|
// Therefore, if we encounter a block with a size > 64KiB, the block
|
|
|
|
// cannot have HashIndex. So the footer will directly interpreted as
|
2018-08-15 23:27:47 +02:00
|
|
|
// num_restarts.
|
2018-08-22 02:06:44 +02:00
|
|
|
//
|
|
|
|
// Such check is for backward compatibility. We can ensure legacy block
|
|
|
|
// with a vary large num_restarts i.e. >= 0x80000000 can be interpreted
|
|
|
|
// correctly as no HashIndex even if the MSB of num_restarts is set.
|
2018-08-15 23:27:47 +02:00
|
|
|
return num_restarts;
|
|
|
|
}
|
|
|
|
BlockBasedTableOptions::DataBlockIndexType index_type;
|
|
|
|
UnPackIndexTypeAndNumRestarts(block_footer, &index_type, &num_restarts);
|
|
|
|
return num_restarts;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockBasedTableOptions::DataBlockIndexType Block::IndexType() const {
|
|
|
|
assert(size_ >= 2 * sizeof(uint32_t));
|
|
|
|
if (size_ > kMaxBlockSizeSupportedByHashIndex) {
|
|
|
|
// The check is for the same reason as that in NumRestarts()
|
|
|
|
return BlockBasedTableOptions::kDataBlockBinarySearch;
|
|
|
|
}
|
|
|
|
uint32_t block_footer = DecodeFixed32(data_ + size_ - sizeof(uint32_t));
|
|
|
|
uint32_t num_restarts = block_footer;
|
|
|
|
BlockBasedTableOptions::DataBlockIndexType index_type;
|
|
|
|
UnPackIndexTypeAndNumRestarts(block_footer, &index_type, &num_restarts);
|
|
|
|
return index_type;
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 09:05:08 +02:00
|
|
|
Block::~Block() {
|
2018-09-17 20:44:36 +02:00
|
|
|
// This sync point can be re-enabled if RocksDB can control the
|
|
|
|
// initialization order of any/all static options created by the user.
|
2018-09-15 09:05:08 +02:00
|
|
|
// TEST_SYNC_POINT("Block::~Block");
|
|
|
|
}
|
2018-08-14 02:31:58 +02:00
|
|
|
|
2020-02-26 00:29:17 +01:00
|
|
|
Block::Block(BlockContents&& contents, size_t read_amp_bytes_per_bit,
|
|
|
|
Statistics* statistics)
|
2014-09-18 01:45:58 +02:00
|
|
|
: contents_(std::move(contents)),
|
|
|
|
data_(contents_.data.data()),
|
2016-10-19 01:59:37 +02:00
|
|
|
size_(contents_.data.size()),
|
2017-12-07 20:50:49 +01:00
|
|
|
restart_offset_(0),
|
2020-02-26 00:29:17 +01:00
|
|
|
num_restarts_(0) {
|
2018-08-14 02:31:58 +02:00
|
|
|
TEST_SYNC_POINT("Block::Block:0");
|
2014-07-31 01:34:35 +02:00
|
|
|
if (size_ < sizeof(uint32_t)) {
|
|
|
|
size_ = 0; // Error marker
|
|
|
|
} else {
|
2018-06-16 04:24:21 +02:00
|
|
|
// Should only decode restart points for uncompressed blocks
|
2018-11-14 02:00:49 +01:00
|
|
|
num_restarts_ = NumRestarts();
|
|
|
|
switch (IndexType()) {
|
|
|
|
case BlockBasedTableOptions::kDataBlockBinarySearch:
|
|
|
|
restart_offset_ = static_cast<uint32_t>(size_) -
|
|
|
|
(1 + num_restarts_) * sizeof(uint32_t);
|
|
|
|
if (restart_offset_ > size_ - sizeof(uint32_t)) {
|
|
|
|
// The size is too small for NumRestarts() and therefore
|
|
|
|
// restart_offset_ wrapped around.
|
|
|
|
size_ = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BlockBasedTableOptions::kDataBlockBinaryAndHash:
|
|
|
|
if (size_ < sizeof(uint32_t) /* block footer */ +
|
|
|
|
sizeof(uint16_t) /* NUM_BUCK */) {
|
|
|
|
size_ = 0;
|
2018-08-15 23:27:47 +02:00
|
|
|
break;
|
2018-11-14 02:00:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t map_offset;
|
|
|
|
data_block_hash_index_.Initialize(
|
|
|
|
contents.data.data(),
|
|
|
|
static_cast<uint16_t>(contents.data.size() -
|
|
|
|
sizeof(uint32_t)), /*chop off
|
|
|
|
NUM_RESTARTS*/
|
|
|
|
&map_offset);
|
|
|
|
|
|
|
|
restart_offset_ = map_offset - num_restarts_ * sizeof(uint32_t);
|
|
|
|
|
|
|
|
if (restart_offset_ > map_offset) {
|
|
|
|
// map_offset is too small for NumRestarts() and
|
|
|
|
// therefore restart_offset_ wrapped around.
|
|
|
|
size_ = 0;
|
2018-08-15 23:27:47 +02:00
|
|
|
break;
|
2018-11-14 02:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
size_ = 0; // Error marker
|
|
|
|
}
|
2019-03-28 00:13:08 +01:00
|
|
|
}
|
2016-08-27 03:55:58 +02:00
|
|
|
if (read_amp_bytes_per_bit != 0 && statistics && size_ != 0) {
|
|
|
|
read_amp_bitmap_.reset(new BlockReadAmpBitmap(
|
|
|
|
restart_offset_, read_amp_bytes_per_bit, statistics));
|
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 20:29:52 +01:00
|
|
|
MetaBlockIter* Block::NewMetaIterator(bool block_contents_pinned) {
|
|
|
|
MetaBlockIter* iter = new MetaBlockIter();
|
|
|
|
if (size_ < 2 * sizeof(uint32_t)) {
|
|
|
|
iter->Invalidate(Status::Corruption("bad block contents"));
|
|
|
|
return iter;
|
|
|
|
} else if (num_restarts_ == 0) {
|
|
|
|
// Empty block.
|
|
|
|
iter->Invalidate(Status::OK());
|
|
|
|
} else {
|
|
|
|
iter->Initialize(data_, restart_offset_, num_restarts_,
|
|
|
|
block_contents_pinned);
|
|
|
|
}
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:05:21 +02:00
|
|
|
DataBlockIter* Block::NewDataIterator(const Comparator* raw_ucmp,
|
2020-02-26 00:29:17 +01:00
|
|
|
SequenceNumber global_seqno,
|
Add an option to put first key of each sst block in the index (#5289)
Summary:
The first key is used to defer reading the data block until this file gets to the top of merging iterator's heap. For short range scans, most files never make it to the top of the heap, so this change can reduce read amplification by a lot sometimes.
Consider the following workload. There are a few data streams (we'll be calling them "logs"), each stream consisting of a sequence of blobs (we'll be calling them "records"). Each record is identified by log ID and a sequence number within the log. RocksDB key is concatenation of log ID and sequence number (big endian). Reads are mostly relatively short range scans, each within a single log. Writes are mostly sequential for each log, but writes to different logs are randomly interleaved. Compactions are disabled; instead, when we accumulate a few tens of sst files, we create a new column family and start writing to it.
So, a typical sst file consists of a few ranges of blocks, each range corresponding to one log ID (we use FlushBlockPolicy to cut blocks at log boundaries). A typical read would go like this. First, iterator Seek() reads one block from each sst file. Then a series of Next()s move through one sst file (since writes to each log are mostly sequential) until the subiterator reaches the end of this log in this sst file; then Next() switches to the next sst file and reads sequentially from that, and so on. Often a range scan will only return records from a small number of blocks in small number of sst files; in this case, the cost of initial Seek() reading one block from each file may be bigger than the cost of reading the actually useful blocks.
Neither iterate_upper_bound nor bloom filters can prevent reading one block from each file in Seek(). But this PR can: if the index contains first key from each block, we don't have to read the block until this block actually makes it to the top of merging iterator's heap, so for short range scans we won't read any blocks from most of the sst files.
This PR does the deferred block loading inside value() call. This is not ideal: there's no good way to report an IO error from inside value(). As discussed with siying offline, it would probably be better to change InternalIterator's interface to explicitly fetch deferred value and get status. I'll do it in a separate PR.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5289
Differential Revision: D15256423
Pulled By: al13n321
fbshipit-source-id: 750e4c39ce88e8d41662f701cf6275d9388ba46a
2019-06-25 05:50:35 +02:00
|
|
|
DataBlockIter* iter, Statistics* stats,
|
|
|
|
bool block_contents_pinned) {
|
2018-07-13 02:19:57 +02:00
|
|
|
DataBlockIter* ret_iter;
|
|
|
|
if (iter != nullptr) {
|
|
|
|
ret_iter = iter;
|
|
|
|
} else {
|
|
|
|
ret_iter = new DataBlockIter;
|
|
|
|
}
|
|
|
|
if (size_ < 2 * sizeof(uint32_t)) {
|
|
|
|
ret_iter->Invalidate(Status::Corruption("bad block contents"));
|
|
|
|
return ret_iter;
|
|
|
|
}
|
|
|
|
if (num_restarts_ == 0) {
|
|
|
|
// Empty block.
|
|
|
|
ret_iter->Invalidate(Status::OK());
|
|
|
|
return ret_iter;
|
|
|
|
} else {
|
2018-08-15 23:27:47 +02:00
|
|
|
ret_iter->Initialize(
|
2020-07-20 23:05:21 +02:00
|
|
|
raw_ucmp, data_, restart_offset_, num_restarts_, global_seqno,
|
2018-11-14 02:00:49 +01:00
|
|
|
read_amp_bitmap_.get(), block_contents_pinned,
|
2018-08-15 23:27:47 +02:00
|
|
|
data_block_hash_index_.Valid() ? &data_block_hash_index_ : nullptr);
|
2016-08-27 03:55:58 +02:00
|
|
|
if (read_amp_bitmap_) {
|
|
|
|
if (read_amp_bitmap_->GetStatistics() != stats) {
|
|
|
|
// DB changed the Statistics pointer, we need to notify read_amp_bitmap_
|
|
|
|
read_amp_bitmap_->SetStatistics(stats);
|
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2014-07-31 01:34:35 +02:00
|
|
|
|
2018-01-25 23:43:43 +01:00
|
|
|
return ret_iter;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
Add an option to put first key of each sst block in the index (#5289)
Summary:
The first key is used to defer reading the data block until this file gets to the top of merging iterator's heap. For short range scans, most files never make it to the top of the heap, so this change can reduce read amplification by a lot sometimes.
Consider the following workload. There are a few data streams (we'll be calling them "logs"), each stream consisting of a sequence of blobs (we'll be calling them "records"). Each record is identified by log ID and a sequence number within the log. RocksDB key is concatenation of log ID and sequence number (big endian). Reads are mostly relatively short range scans, each within a single log. Writes are mostly sequential for each log, but writes to different logs are randomly interleaved. Compactions are disabled; instead, when we accumulate a few tens of sst files, we create a new column family and start writing to it.
So, a typical sst file consists of a few ranges of blocks, each range corresponding to one log ID (we use FlushBlockPolicy to cut blocks at log boundaries). A typical read would go like this. First, iterator Seek() reads one block from each sst file. Then a series of Next()s move through one sst file (since writes to each log are mostly sequential) until the subiterator reaches the end of this log in this sst file; then Next() switches to the next sst file and reads sequentially from that, and so on. Often a range scan will only return records from a small number of blocks in small number of sst files; in this case, the cost of initial Seek() reading one block from each file may be bigger than the cost of reading the actually useful blocks.
Neither iterate_upper_bound nor bloom filters can prevent reading one block from each file in Seek(). But this PR can: if the index contains first key from each block, we don't have to read the block until this block actually makes it to the top of merging iterator's heap, so for short range scans we won't read any blocks from most of the sst files.
This PR does the deferred block loading inside value() call. This is not ideal: there's no good way to report an IO error from inside value(). As discussed with siying offline, it would probably be better to change InternalIterator's interface to explicitly fetch deferred value and get status. I'll do it in a separate PR.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5289
Differential Revision: D15256423
Pulled By: al13n321
fbshipit-source-id: 750e4c39ce88e8d41662f701cf6275d9388ba46a
2019-06-25 05:50:35 +02:00
|
|
|
IndexBlockIter* Block::NewIndexIterator(
|
2020-07-20 23:05:21 +02:00
|
|
|
const Comparator* raw_ucmp, SequenceNumber global_seqno,
|
|
|
|
IndexBlockIter* iter, Statistics* /*stats*/, bool total_order_seek,
|
|
|
|
bool have_first_key, bool key_includes_seq, bool value_is_full,
|
|
|
|
bool block_contents_pinned, BlockPrefixIndex* prefix_index) {
|
2018-07-13 02:19:57 +02:00
|
|
|
IndexBlockIter* ret_iter;
|
|
|
|
if (iter != nullptr) {
|
|
|
|
ret_iter = iter;
|
|
|
|
} else {
|
|
|
|
ret_iter = new IndexBlockIter;
|
|
|
|
}
|
|
|
|
if (size_ < 2 * sizeof(uint32_t)) {
|
|
|
|
ret_iter->Invalidate(Status::Corruption("bad block contents"));
|
|
|
|
return ret_iter;
|
|
|
|
}
|
|
|
|
if (num_restarts_ == 0) {
|
|
|
|
// Empty block.
|
|
|
|
ret_iter->Invalidate(Status::OK());
|
|
|
|
return ret_iter;
|
|
|
|
} else {
|
|
|
|
BlockPrefixIndex* prefix_index_ptr =
|
|
|
|
total_order_seek ? nullptr : prefix_index;
|
2020-07-20 23:05:21 +02:00
|
|
|
ret_iter->Initialize(raw_ucmp, data_, restart_offset_, num_restarts_,
|
2020-02-26 00:29:17 +01:00
|
|
|
global_seqno, prefix_index_ptr, have_first_key,
|
Add an option to put first key of each sst block in the index (#5289)
Summary:
The first key is used to defer reading the data block until this file gets to the top of merging iterator's heap. For short range scans, most files never make it to the top of the heap, so this change can reduce read amplification by a lot sometimes.
Consider the following workload. There are a few data streams (we'll be calling them "logs"), each stream consisting of a sequence of blobs (we'll be calling them "records"). Each record is identified by log ID and a sequence number within the log. RocksDB key is concatenation of log ID and sequence number (big endian). Reads are mostly relatively short range scans, each within a single log. Writes are mostly sequential for each log, but writes to different logs are randomly interleaved. Compactions are disabled; instead, when we accumulate a few tens of sst files, we create a new column family and start writing to it.
So, a typical sst file consists of a few ranges of blocks, each range corresponding to one log ID (we use FlushBlockPolicy to cut blocks at log boundaries). A typical read would go like this. First, iterator Seek() reads one block from each sst file. Then a series of Next()s move through one sst file (since writes to each log are mostly sequential) until the subiterator reaches the end of this log in this sst file; then Next() switches to the next sst file and reads sequentially from that, and so on. Often a range scan will only return records from a small number of blocks in small number of sst files; in this case, the cost of initial Seek() reading one block from each file may be bigger than the cost of reading the actually useful blocks.
Neither iterate_upper_bound nor bloom filters can prevent reading one block from each file in Seek(). But this PR can: if the index contains first key from each block, we don't have to read the block until this block actually makes it to the top of merging iterator's heap, so for short range scans we won't read any blocks from most of the sst files.
This PR does the deferred block loading inside value() call. This is not ideal: there's no good way to report an IO error from inside value(). As discussed with siying offline, it would probably be better to change InternalIterator's interface to explicitly fetch deferred value and get status. I'll do it in a separate PR.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5289
Differential Revision: D15256423
Pulled By: al13n321
fbshipit-source-id: 750e4c39ce88e8d41662f701cf6275d9388ba46a
2019-06-25 05:50:35 +02:00
|
|
|
key_includes_seq, value_is_full,
|
|
|
|
block_contents_pinned);
|
2018-07-13 02:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret_iter;
|
2014-06-13 04:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 20:27:34 +02:00
|
|
|
size_t Block::ApproximateMemoryUsage() const {
|
Use malloc_usable_size() for accounting block cache size
Summary:
Currently, when we insert something into block cache, we say that the block cache capacity decreased by the size of the block. However, size of the block might be less than the actual memory used by this object. For example, 4.5KB block will actually use 8KB of memory. So even if we configure block cache to 10GB, our actually memory usage of block cache will be 20GB!
This problem showed up a lot in testing and just recently also showed up in MongoRocks production where we were using 30GB more memory than expected.
This diff will fix the problem. Instead of counting the block size, we will count memory used by the block. That way, a block cache configured to be 10GB will actually use only 10GB of memory.
I'm using non-portable function and I couldn't find info on portability on Google. However, it seems to work on Linux, which will cover majority of our use-cases.
Test Plan:
1. fill up mongo instance with 80GB of data
2. restart mongo with block cache size configured to 10GB
3. do a table scan in mongo
4. memory usage before the diff: 12GB. memory usage after the diff: 10.5GB
Reviewers: sdong, MarkCallaghan, rven, yhchiang
Reviewed By: yhchiang
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D40635
2015-06-26 20:48:09 +02:00
|
|
|
size_t usage = usable_size();
|
2018-06-29 17:55:33 +02:00
|
|
|
#ifdef ROCKSDB_MALLOC_USABLE_SIZE
|
|
|
|
usage += malloc_usable_size((void*)this);
|
|
|
|
#else
|
|
|
|
usage += sizeof(*this);
|
|
|
|
#endif // ROCKSDB_MALLOC_USABLE_SIZE
|
|
|
|
if (read_amp_bitmap_) {
|
|
|
|
usage += read_amp_bitmap_->ApproximateMemoryUsage();
|
|
|
|
}
|
2014-08-05 20:27:34 +02:00
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|