2019-04-18 17:51:19 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
2013-10-29 03:34:02 +00: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.
|
|
|
|
|
|
|
|
#pragma once
|
2014-04-25 19:21:34 +00:00
|
|
|
|
2022-10-25 18:50:38 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
#include <memory>
|
2014-01-27 21:53:22 +00:00
|
|
|
#include <string>
|
2022-10-25 18:50:38 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2014-01-28 05:58:46 +00:00
|
|
|
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/random_access_file_reader.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "memory/arena.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2014-01-28 18:35:48 +00:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2014-01-28 05:58:46 +00:00
|
|
|
#include "rocksdb/table.h"
|
|
|
|
#include "rocksdb/table_properties.h"
|
2019-09-05 17:03:42 +00:00
|
|
|
#include "table/plain/plain_table_bloom.h"
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/plain/plain_table_factory.h"
|
|
|
|
#include "table/plain/plain_table_index.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "table/table_reader.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
class Block;
|
2014-07-20 14:56:40 +00:00
|
|
|
struct BlockContents;
|
2013-10-29 03:34:02 +00:00
|
|
|
class BlockHandle;
|
|
|
|
class Footer;
|
|
|
|
struct Options;
|
|
|
|
class RandomAccessFile;
|
|
|
|
struct ReadOptions;
|
|
|
|
class TableCache;
|
|
|
|
class TableReader;
|
2014-01-27 21:53:22 +00:00
|
|
|
class InternalKeyComparator;
|
2014-06-18 23:36:48 +00:00
|
|
|
class PlainTableKeyDecoder;
|
2014-09-29 18:09:09 +00:00
|
|
|
class GetContext;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2014-01-28 05:58:46 +00:00
|
|
|
extern const uint32_t kPlainTableVariableLength;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
struct PlainTableReaderFileInfo {
|
|
|
|
bool is_mmap_mode;
|
|
|
|
Slice file_data;
|
|
|
|
uint32_t data_end_offset;
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFileReader> file;
|
2015-09-16 23:57:43 +00:00
|
|
|
|
2019-03-27 17:18:56 +00:00
|
|
|
PlainTableReaderFileInfo(std::unique_ptr<RandomAccessFileReader>&& _file,
|
2015-09-16 23:57:43 +00:00
|
|
|
const EnvOptions& storage_options,
|
|
|
|
uint32_t _data_size_offset)
|
|
|
|
: is_mmap_mode(storage_options.use_mmap_reads),
|
|
|
|
data_end_offset(_data_size_offset),
|
|
|
|
file(std::move(_file)) {}
|
|
|
|
};
|
|
|
|
|
2019-05-23 23:22:13 +00:00
|
|
|
// The reader class of PlainTable. For description of PlainTable format
|
|
|
|
// See comments of class PlainTableFactory, where instances of
|
|
|
|
// PlainTableReader are created.
|
2022-10-25 18:50:38 +00:00
|
|
|
class PlainTableReader : public TableReader {
|
2019-05-23 23:22:13 +00:00
|
|
|
public:
|
2022-10-25 18:50:38 +00:00
|
|
|
// Based on following output file format shown in plain_table_factory.h
|
|
|
|
// When opening the output file, PlainTableReader creates a hash table
|
|
|
|
// from key prefixes to offset of the output file. PlainTable will decide
|
|
|
|
// whether it points to the data offset of the first key with the key prefix
|
|
|
|
// or the offset of it. If there are too many keys share this prefix, it will
|
|
|
|
// create a binary search-able index from the suffix to offset on disk.
|
2021-05-05 20:59:21 +00:00
|
|
|
static Status Open(const ImmutableOptions& ioptions,
|
2014-09-04 23:18:36 +00:00
|
|
|
const EnvOptions& env_options,
|
2017-07-27 21:17:10 +00:00
|
|
|
const InternalKeyComparator& internal_comparator,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFileReader>&& file,
|
|
|
|
uint64_t file_size, std::unique_ptr<TableReader>* table,
|
2014-02-08 00:25:38 +00:00
|
|
|
const int bloom_bits_per_key, double hash_table_ratio,
|
2014-06-18 23:36:48 +00:00
|
|
|
size_t index_sparseness, size_t huge_page_tlb_size,
|
2019-01-26 01:07:00 +00:00
|
|
|
bool full_scan_mode, const bool immortal_table = false,
|
2018-05-21 21:33:55 +00:00
|
|
|
const SliceTransform* prefix_extractor = nullptr);
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2019-06-19 21:07:36 +00:00
|
|
|
// Returns new iterator over table contents
|
|
|
|
// compaction_readahead_size: its value will only be used if for_compaction =
|
|
|
|
// true
|
2017-05-05 22:01:04 +00:00
|
|
|
InternalIterator* NewIterator(const ReadOptions&,
|
2018-05-21 21:33:55 +00:00
|
|
|
const SliceTransform* prefix_extractor,
|
2019-06-20 21:28:22 +00:00
|
|
|
Arena* arena, bool skip_filters,
|
2019-09-20 19:00:55 +00:00
|
|
|
TableReaderCaller caller,
|
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
|
|
|
size_t compaction_readahead_size = 0,
|
|
|
|
bool allow_unprepared_value = false) override;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Prepare(const Slice& target) override;
|
2014-06-12 17:06:18 +00:00
|
|
|
|
2018-05-21 21:33:55 +00:00
|
|
|
Status Get(const ReadOptions& readOptions, const Slice& key,
|
|
|
|
GetContext* get_context, const SliceTransform* prefix_extractor,
|
Skip bottom-level filter block caching when hit-optimized
Summary:
When Get() or NewIterator() trigger file loads, skip caching the filter block if
(1) optimize_filters_for_hits is set and (2) the file is on the bottommost
level. Also skip checking filters under the same conditions, which means that
for a preloaded file or a file that was trivially-moved to the bottom level, its
filter block will eventually expire from the cache.
- added parameters/instance variables in various places in order to propagate the config ("skip_filters") from version_set to block_based_table_reader
- in BlockBasedTable::Rep, this optimization prevents filter from being loaded when the file is opened simply by setting filter_policy = nullptr
- in BlockBasedTable::Get/BlockBasedTable::NewIterator, this optimization prevents filter from being used (even if it was loaded already) by setting filter = nullptr
Test Plan:
updated unit test:
$ ./db_test --gtest_filter=DBTest.OptimizeFiltersForHits
will also run 'make check'
Reviewers: sdong, igor, paultuckfield, anthony, rven, kradhakrishnan, IslamAbdelRahman, yhchiang
Reviewed By: yhchiang
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D51633
2015-12-23 18:15:07 +00:00
|
|
|
bool skip_filters = false) override;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2023-04-21 16:07:18 +00:00
|
|
|
uint64_t ApproximateOffsetOf(const ReadOptions& read_options,
|
|
|
|
const Slice& key,
|
2019-06-20 21:28:22 +00:00
|
|
|
TableReaderCaller caller) override;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2023-04-21 16:07:18 +00:00
|
|
|
uint64_t ApproximateSize(const ReadOptions& read_options, const Slice& start,
|
|
|
|
const Slice& end, TableReaderCaller caller) override;
|
2019-08-16 21:16:49 +00:00
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
uint32_t GetIndexSize() const { return index_.GetIndexSize(); }
|
2015-02-26 19:28:41 +00:00
|
|
|
void SetupForCompaction() override;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
std::shared_ptr<const TableProperties> GetTableProperties() const override {
|
2014-02-08 03:26:49 +00:00
|
|
|
return table_properties_;
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2014-08-05 18:27:34 +00:00
|
|
|
virtual size_t ApproximateMemoryUsage() const override {
|
|
|
|
return arena_.MemoryAllocatedBytes();
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:59:21 +00:00
|
|
|
PlainTableReader(const ImmutableOptions& ioptions,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFileReader>&& file,
|
2014-09-04 23:18:36 +00:00
|
|
|
const EnvOptions& env_options,
|
2014-01-27 21:53:22 +00:00
|
|
|
const InternalKeyComparator& internal_comparator,
|
2014-06-18 23:36:48 +00:00
|
|
|
EncodingType encoding_type, uint64_t file_size,
|
2018-05-21 21:33:55 +00:00
|
|
|
const TableProperties* table_properties,
|
|
|
|
const SliceTransform* prefix_extractor);
|
2014-02-08 00:25:38 +00:00
|
|
|
virtual ~PlainTableReader();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Check bloom filter to see whether it might contain this prefix.
|
|
|
|
// The hash of the prefix is given, since it can be reused for index lookup
|
|
|
|
// too.
|
|
|
|
virtual bool MatchBloom(uint32_t hash) const;
|
|
|
|
|
|
|
|
// PopulateIndex() builds index of keys. It must be called before any query
|
|
|
|
// to the table.
|
|
|
|
//
|
2014-04-23 01:31:55 +00:00
|
|
|
// props: the table properties object that need to be stored. Ownership of
|
|
|
|
// the object will be passed.
|
|
|
|
//
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
Status PopulateIndex(TableProperties* props, int bloom_bits_per_key,
|
|
|
|
double hash_table_ratio, size_t index_sparseness,
|
|
|
|
size_t huge_page_tlb_size);
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
Status MmapDataIfNeeded();
|
2014-02-08 00:25:38 +00:00
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
private:
|
2014-01-27 21:53:22 +00:00
|
|
|
const InternalKeyComparator internal_comparator_;
|
2014-06-18 23:36:48 +00:00
|
|
|
EncodingType encoding_type_;
|
2014-02-13 23:27:59 +00:00
|
|
|
// represents plain table's current status.
|
2013-10-29 03:34:02 +00:00
|
|
|
Status status_;
|
2013-12-20 17:35:24 +00:00
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
PlainTableIndex index_;
|
|
|
|
bool full_scan_mode_;
|
|
|
|
|
2014-02-13 23:27:59 +00:00
|
|
|
// data_start_offset_ and data_end_offset_ defines the range of the
|
|
|
|
// sst file that stores data.
|
2014-01-25 05:10:19 +00:00
|
|
|
const uint32_t data_start_offset_ = 0;
|
2014-11-11 21:47:22 +00:00
|
|
|
const uint32_t user_key_len_;
|
2014-06-09 19:30:19 +00:00
|
|
|
const SliceTransform* prefix_extractor_;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
static const size_t kNumInternalBytes = 8;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
// Bloom filter is used to rule out non-existent key
|
|
|
|
bool enable_bloom_;
|
2019-09-05 17:03:42 +00:00
|
|
|
PlainTableBloomV1 bloom_;
|
2015-09-16 23:57:43 +00:00
|
|
|
PlainTableReaderFileInfo file_info_;
|
2014-06-09 19:30:19 +00:00
|
|
|
Arena arena_;
|
2018-10-03 00:21:54 +00:00
|
|
|
CacheAllocationPtr index_block_alloc_;
|
|
|
|
CacheAllocationPtr bloom_block_alloc_;
|
2014-06-09 19:30:19 +00:00
|
|
|
|
2021-05-05 20:59:21 +00:00
|
|
|
const ImmutableOptions& ioptions_;
|
2019-01-26 01:07:00 +00:00
|
|
|
std::unique_ptr<Cleanable> dummy_cleanable_;
|
2014-11-11 21:47:22 +00:00
|
|
|
uint64_t file_size_;
|
2022-10-25 18:50:38 +00:00
|
|
|
|
|
|
|
protected: // for testing
|
2014-06-09 19:30:19 +00:00
|
|
|
std::shared_ptr<const TableProperties> table_properties_;
|
|
|
|
|
2022-10-25 18:50:38 +00:00
|
|
|
private:
|
2014-01-25 05:10:19 +00:00
|
|
|
bool IsFixedLength() const {
|
2014-01-28 05:58:46 +00:00
|
|
|
return user_key_len_ != kPlainTableVariableLength;
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
size_t GetFixedInternalKeyLength() const {
|
2013-12-20 17:35:24 +00:00
|
|
|
return user_key_len_ + kNumInternalBytes;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
Slice GetPrefix(const Slice& target) const {
|
|
|
|
assert(target.size() >= 8); // target is internal key
|
2022-03-15 17:02:33 +00:00
|
|
|
return GetPrefixFromUserKey(ExtractUserKey(target));
|
2014-07-18 23:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Slice GetPrefix(const ParsedInternalKey& target) const {
|
|
|
|
return GetPrefixFromUserKey(target.user_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice GetPrefixFromUserKey(const Slice& user_key) const {
|
|
|
|
if (!IsTotalOrderMode()) {
|
|
|
|
return prefix_extractor_->Transform(user_key);
|
|
|
|
} else {
|
|
|
|
// Use empty slice as prefix if prefix_extractor is not set.
|
|
|
|
// In that case,
|
|
|
|
// it falls back to pure binary search and
|
|
|
|
// total iterator seek is supported.
|
|
|
|
return Slice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
friend class TableCache;
|
|
|
|
friend class PlainTableIterator;
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
// Internal helper function to generate an IndexRecordList object from all
|
|
|
|
// the rows, which contains index records as a list.
|
2014-02-08 00:25:38 +00:00
|
|
|
// If bloom_ is not null, all the keys' full-key hash will be added to the
|
|
|
|
// bloom filter.
|
2014-07-18 23:58:13 +00:00
|
|
|
Status PopulateIndexRecordList(PlainTableIndexBuilder* index_builder,
|
2019-03-27 17:18:56 +00:00
|
|
|
std::vector<uint32_t>* prefix_hashes);
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2019-09-13 17:24:38 +00:00
|
|
|
// Internal helper function to allocate memory for bloom filter
|
|
|
|
void AllocateBloom(int bloom_bits_per_key, int num_prefixes,
|
|
|
|
size_t huge_page_tlb_size);
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2019-09-13 17:24:38 +00:00
|
|
|
void FillBloom(const std::vector<uint32_t>& prefix_hashes);
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
// Read the key and value at `offset` to parameters for keys, the and
|
|
|
|
// `seekable`.
|
2014-02-13 23:27:59 +00:00
|
|
|
// On success, `offset` will be updated as the offset for the next key.
|
2014-06-18 23:36:48 +00:00
|
|
|
// `parsed_key` will be key in parsed format.
|
|
|
|
// if `internal_key` is not empty, it will be filled with key with slice
|
|
|
|
// format.
|
|
|
|
// if `seekable` is not null, it will return whether we can directly read
|
|
|
|
// data using this offset.
|
|
|
|
Status Next(PlainTableKeyDecoder* decoder, uint32_t* offset,
|
|
|
|
ParsedInternalKey* parsed_key, Slice* internal_key, Slice* value,
|
|
|
|
bool* seekable = nullptr) const;
|
2013-11-21 19:11:02 +00:00
|
|
|
// Get file offset for key target.
|
|
|
|
// return value prefix_matched is set to true if the offset is confirmed
|
|
|
|
// for a key with the same prefix as target.
|
2015-11-18 02:29:40 +00:00
|
|
|
Status GetOffset(PlainTableKeyDecoder* decoder, const Slice& target,
|
|
|
|
const Slice& prefix, uint32_t prefix_hash,
|
|
|
|
bool& prefix_matched, uint32_t* offset) const;
|
2014-02-08 00:25:38 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
bool IsTotalOrderMode() const { return (prefix_extractor_ == nullptr); }
|
2014-01-27 21:53:22 +00:00
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
// No copying allowed
|
|
|
|
explicit PlainTableReader(const TableReader&) = delete;
|
|
|
|
void operator=(const TableReader&) = delete;
|
|
|
|
};
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|