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.
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/plain/plain_table_reader.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2014-01-27 21:53:22 +00:00
|
|
|
#include <string>
|
2014-02-13 23:27:59 +00:00
|
|
|
#include <vector>
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
2022-10-25 18:50:38 +00:00
|
|
|
#include "memory/arena.h"
|
|
|
|
#include "monitoring/histogram.h"
|
|
|
|
#include "monitoring/perf_context_imp.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
#include "rocksdb/cache.h"
|
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/filter_policy.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/statistics.h"
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/block_based/block.h"
|
|
|
|
#include "table/block_based/filter_block.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
#include "table/format.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "table/get_context.h"
|
2015-10-12 22:06:38 +00:00
|
|
|
#include "table/internal_iterator.h"
|
2013-12-06 00:51:26 +00:00
|
|
|
#include "table/meta_blocks.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_key_coding.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "table/two_level_iterator.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
#include "util/coding.h"
|
2013-12-20 17:35:24 +00:00
|
|
|
#include "util/dynamic_bloom.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
#include "util/hash.h"
|
|
|
|
#include "util/stop_watch.h"
|
2014-11-25 04:44:49 +00:00
|
|
|
#include "util/string_util.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
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
namespace {
|
2013-12-20 17:35:24 +00:00
|
|
|
|
2014-02-13 23:27:59 +00:00
|
|
|
// Safely getting a uint32_t element from a char array, where, starting from
|
|
|
|
// `base`, every 4 bytes are considered as an fixed 32 bit integer.
|
|
|
|
inline uint32_t GetFixed32Element(const char* base, size_t offset) {
|
|
|
|
return DecodeFixed32(base + offset * sizeof(uint32_t));
|
|
|
|
}
|
2014-01-25 05:10:19 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Iterator to iterate IndexedTable
|
2015-10-12 22:06:38 +00:00
|
|
|
class PlainTableIterator : public InternalIterator {
|
2014-01-25 05:10:19 +00:00
|
|
|
public:
|
2014-02-08 00:25:38 +00:00
|
|
|
explicit PlainTableIterator(PlainTableReader* table, bool use_prefix_seek);
|
2019-09-12 01:07:12 +00:00
|
|
|
// No copying allowed
|
|
|
|
PlainTableIterator(const PlainTableIterator&) = delete;
|
|
|
|
void operator=(const Iterator&) = delete;
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~PlainTableIterator() override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
bool Valid() const override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void SeekToFirst() override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void SeekToLast() override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Seek(const Slice& target) override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2016-09-28 01:20:57 +00:00
|
|
|
void SeekForPrev(const Slice& target) override;
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Next() override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Prev() override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
Slice key() const override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
Slice value() const override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
Status status() const override;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
PlainTableReader* table_;
|
2014-06-18 23:36:48 +00:00
|
|
|
PlainTableKeyDecoder decoder_;
|
2014-02-08 00:25:38 +00:00
|
|
|
bool use_prefix_seek_;
|
2014-01-25 05:10:19 +00:00
|
|
|
uint32_t offset_;
|
|
|
|
uint32_t next_offset_;
|
2014-06-18 23:36:48 +00:00
|
|
|
Slice key_;
|
2014-01-25 05:10:19 +00:00
|
|
|
Slice value_;
|
|
|
|
Status status_;
|
|
|
|
};
|
|
|
|
|
2018-11-09 19:17:34 +00:00
|
|
|
PlainTableReader::PlainTableReader(
|
2021-05-05 20:59:21 +00:00
|
|
|
const ImmutableOptions& ioptions,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFileReader>&& file,
|
|
|
|
const EnvOptions& storage_options, const InternalKeyComparator& icomparator,
|
|
|
|
EncodingType encoding_type, uint64_t file_size,
|
|
|
|
const TableProperties* table_properties,
|
|
|
|
const SliceTransform* prefix_extractor)
|
2014-06-09 19:30:19 +00:00
|
|
|
: internal_comparator_(icomparator),
|
2014-06-18 23:36:48 +00:00
|
|
|
encoding_type_(encoding_type),
|
2014-07-18 23:58:13 +00:00
|
|
|
full_scan_mode_(false),
|
2014-11-11 21:47:22 +00:00
|
|
|
user_key_len_(static_cast<uint32_t>(table_properties->fixed_key_len)),
|
2018-05-21 21:33:55 +00:00
|
|
|
prefix_extractor_(prefix_extractor),
|
2014-06-09 19:30:19 +00:00
|
|
|
enable_bloom_(false),
|
2019-01-24 18:11:19 +00:00
|
|
|
bloom_(6),
|
2015-09-16 23:57:43 +00:00
|
|
|
file_info_(std::move(file), storage_options,
|
|
|
|
static_cast<uint32_t>(table_properties->data_size)),
|
2014-09-04 23:18:36 +00:00
|
|
|
ioptions_(ioptions),
|
2014-06-09 19:30:19 +00:00
|
|
|
file_size_(file_size),
|
|
|
|
table_properties_(nullptr) {}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
PlainTableReader::~PlainTableReader() {
|
2020-09-29 16:47:33 +00:00
|
|
|
// Should fix?
|
|
|
|
status_.PermitUncheckedError();
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:33:55 +00:00
|
|
|
Status PlainTableReader::Open(
|
2021-05-05 20:59:21 +00:00
|
|
|
const ImmutableOptions& ioptions, const EnvOptions& env_options,
|
2018-05-21 21:33:55 +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_reader, const int bloom_bits_per_key,
|
2018-05-21 21:33:55 +00:00
|
|
|
double hash_table_ratio, 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,
|
|
|
|
const SliceTransform* prefix_extractor) {
|
2014-07-18 23:58:13 +00:00
|
|
|
if (file_size > PlainTableIndex::kMaxFileSize) {
|
2013-11-21 19:11:02 +00:00
|
|
|
return Status::NotSupported("File is too large for PlainTableReader!");
|
|
|
|
}
|
|
|
|
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 19:42:12 +00:00
|
|
|
std::unique_ptr<TableProperties> props;
|
Group SST write in flush, compaction and db open with new stats (#11910)
Summary:
## Context/Summary
Similar to https://github.com/facebook/rocksdb/pull/11288, https://github.com/facebook/rocksdb/pull/11444, categorizing SST/blob file write according to different io activities allows more insight into the activity.
For that, this PR does the following:
- Tag different write IOs by passing down and converting WriteOptions to IOOptions
- Add new SST_WRITE_MICROS histogram in WritableFileWriter::Append() and breakdown FILE_WRITE_{FLUSH|COMPACTION|DB_OPEN}_MICROS
Some related code refactory to make implementation cleaner:
- Blob stats
- Replace high-level write measurement with low-level WritableFileWriter::Append() measurement for BLOB_DB_BLOB_FILE_WRITE_MICROS. This is to make FILE_WRITE_{FLUSH|COMPACTION|DB_OPEN}_MICROS include blob file. As a consequence, this introduces some behavioral changes on it, see HISTORY and db bench test plan below for more info.
- Fix bugs where BLOB_DB_BLOB_FILE_SYNCED/BLOB_DB_BLOB_FILE_BYTES_WRITTEN include file failed to sync and bytes failed to write.
- Refactor WriteOptions constructor for easier construction with io_activity and rate_limiter_priority
- Refactor DBImpl::~DBImpl()/BlobDBImpl::Close() to bypass thread op verification
- Build table
- TableBuilderOptions now includes Read/WriteOpitons so BuildTable() do not need to take these two variables
- Replace the io_priority passed into BuildTable() with TableBuilderOptions::WriteOpitons::rate_limiter_priority. Similar for BlobFileBuilder.
This parameter is used for dynamically changing file io priority for flush, see https://github.com/facebook/rocksdb/pull/9988?fbclid=IwAR1DtKel6c-bRJAdesGo0jsbztRtciByNlvokbxkV6h_L-AE9MACzqRTT5s for more
- Update ThreadStatus::FLUSH_BYTES_WRITTEN to use io_activity to track flush IO in flush job and db open instead of io_priority
## Test
### db bench
Flush
```
./db_bench --statistics=1 --benchmarks=fillseq --num=100000 --write_buffer_size=100
rocksdb.sst.write.micros P50 : 1.830863 P95 : 4.094720 P99 : 6.578947 P100 : 26.000000 COUNT : 7875 SUM : 20377
rocksdb.file.write.flush.micros P50 : 1.830863 P95 : 4.094720 P99 : 6.578947 P100 : 26.000000 COUNT : 7875 SUM : 20377
rocksdb.file.write.compaction.micros P50 : 0.000000 P95 : 0.000000 P99 : 0.000000 P100 : 0.000000 COUNT : 0 SUM : 0
rocksdb.file.write.db.open.micros P50 : 0.000000 P95 : 0.000000 P99 : 0.000000 P100 : 0.000000 COUNT : 0 SUM : 0
```
compaction, db oopen
```
Setup: ./db_bench --statistics=1 --benchmarks=fillseq --num=10000 --disable_auto_compactions=1 -write_buffer_size=100 --db=../db_bench
Run:./db_bench --statistics=1 --benchmarks=compact --db=../db_bench --use_existing_db=1
rocksdb.sst.write.micros P50 : 2.675325 P95 : 9.578788 P99 : 18.780000 P100 : 314.000000 COUNT : 638 SUM : 3279
rocksdb.file.write.flush.micros P50 : 0.000000 P95 : 0.000000 P99 : 0.000000 P100 : 0.000000 COUNT : 0 SUM : 0
rocksdb.file.write.compaction.micros P50 : 2.757353 P95 : 9.610687 P99 : 19.316667 P100 : 314.000000 COUNT : 615 SUM : 3213
rocksdb.file.write.db.open.micros P50 : 2.055556 P95 : 3.925000 P99 : 9.000000 P100 : 9.000000 COUNT : 23 SUM : 66
```
blob stats - just to make sure they aren't broken by this PR
```
Integrated Blob DB
Setup: ./db_bench --enable_blob_files=1 --statistics=1 --benchmarks=fillseq --num=10000 --disable_auto_compactions=1 -write_buffer_size=100 --db=../db_bench
Run:./db_bench --enable_blob_files=1 --statistics=1 --benchmarks=compact --db=../db_bench --use_existing_db=1
pre-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 7.298246 P95 : 9.771930 P99 : 9.991813 P100 : 16.000000 COUNT : 235 SUM : 1600
rocksdb.blobdb.blob.file.synced COUNT : 1
rocksdb.blobdb.blob.file.bytes.written COUNT : 34842
post-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 2.000000 P95 : 2.829360 P99 : 2.993779 P100 : 9.000000 COUNT : 707 SUM : 1614
- COUNT is higher and values are smaller as it includes header and footer write
- COUNT is 3X higher due to each Append() count as one post-PR, while in pre-PR, 3 Append()s counts as one. See https://github.com/facebook/rocksdb/pull/11910/files#diff-32b811c0a1c000768cfb2532052b44dc0b3bf82253f3eab078e15ff201a0dabfL157-L164
rocksdb.blobdb.blob.file.synced COUNT : 1 (stay the same)
rocksdb.blobdb.blob.file.bytes.written COUNT : 34842 (stay the same)
```
```
Stacked Blob DB
Run: ./db_bench --use_blob_db=1 --statistics=1 --benchmarks=fillseq --num=10000 --disable_auto_compactions=1 -write_buffer_size=100 --db=../db_bench
pre-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 12.808042 P95 : 19.674497 P99 : 28.539683 P100 : 51.000000 COUNT : 10000 SUM : 140876
rocksdb.blobdb.blob.file.synced COUNT : 8
rocksdb.blobdb.blob.file.bytes.written COUNT : 1043445
post-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 1.657370 P95 : 2.952175 P99 : 3.877519 P100 : 24.000000 COUNT : 30001 SUM : 67924
- COUNT is higher and values are smaller as it includes header and footer write
- COUNT is 3X higher due to each Append() count as one post-PR, while in pre-PR, 3 Append()s counts as one. See https://github.com/facebook/rocksdb/pull/11910/files#diff-32b811c0a1c000768cfb2532052b44dc0b3bf82253f3eab078e15ff201a0dabfL157-L164
rocksdb.blobdb.blob.file.synced COUNT : 8 (stay the same)
rocksdb.blobdb.blob.file.bytes.written COUNT : 1043445 (stay the same)
```
### Rehearsal CI stress test
Trigger 3 full runs of all our CI stress tests
### Performance
Flush
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_pre_pr --benchmark_filter=ManualFlush/key_num:524288/per_key_size:256 --benchmark_repetitions=1000
-- default: 1 thread is used to run benchmark; enable_statistics = true
Pre-pr: avg 507515519.3 ns
497686074,499444327,500862543,501389862,502994471,503744435,504142123,504224056,505724198,506610393,506837742,506955122,507695561,507929036,508307733,508312691,508999120,509963561,510142147,510698091,510743096,510769317,510957074,511053311,511371367,511409911,511432960,511642385,511691964,511730908,
Post-pr: avg 511971266.5 ns, regressed 0.88%
502744835,506502498,507735420,507929724,508313335,509548582,509994942,510107257,510715603,511046955,511352639,511458478,512117521,512317380,512766303,512972652,513059586,513804934,513808980,514059409,514187369,514389494,514447762,514616464,514622882,514641763,514666265,514716377,514990179,515502408,
```
Compaction
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_{pre|post}_pr --benchmark_filter=ManualCompaction/comp_style:0/max_data:134217728/per_key_size:256/enable_statistics:1 --benchmark_repetitions=1000
-- default: 1 thread is used to run benchmark
Pre-pr: avg 495346098.30 ns
492118301,493203526,494201411,494336607,495269217,495404950,496402598,497012157,497358370,498153846
Post-pr: avg 504528077.20, regressed 1.85%. "ManualCompaction" include flush so the isolated regression for compaction should be around 1.85-0.88 = 0.97%
502465338,502485945,502541789,502909283,503438601,504143885,506113087,506629423,507160414,507393007
```
Put with WAL (in case passing WriteOptions slows down this path even without collecting SST write stats)
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_pre_pr --benchmark_filter=DBPut/comp_style:0/max_data:107374182400/per_key_size:256/enable_statistics:1/wal:1 --benchmark_repetitions=1000
-- default: 1 thread is used to run benchmark
Pre-pr: avg 3848.10 ns
3814,3838,3839,3848,3854,3854,3854,3860,3860,3860
Post-pr: avg 3874.20 ns, regressed 0.68%
3863,3867,3871,3874,3875,3877,3877,3877,3880,3881
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/11910
Reviewed By: ajkr
Differential Revision: D49788060
Pulled By: hx235
fbshipit-source-id: 79e73699cda5be3b66461687e5147c2484fc5eff
2023-12-29 23:29:23 +00:00
|
|
|
// TODO: plumb Env::IOActivity, Env::IOPriority
|
2023-04-21 16:07:18 +00:00
|
|
|
const ReadOptions read_options;
|
2014-01-25 05:10:19 +00:00
|
|
|
auto s = ReadTableProperties(file.get(), file_size, kPlainTableMagicNumber,
|
2023-04-21 16:07:18 +00:00
|
|
|
ioptions, read_options, &props);
|
2013-12-06 00:51:26 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
assert(hash_table_ratio >= 0.0);
|
2014-06-18 23:36:48 +00:00
|
|
|
auto& user_props = props->user_collected_properties;
|
2016-08-26 18:46:32 +00:00
|
|
|
auto prefix_extractor_in_file = props->prefix_extractor_name;
|
2014-06-18 23:36:48 +00:00
|
|
|
|
2016-08-26 18:46:32 +00:00
|
|
|
if (!full_scan_mode &&
|
|
|
|
!prefix_extractor_in_file.empty() /* old version sst file*/
|
|
|
|
&& prefix_extractor_in_file != "nullptr") {
|
2018-05-21 21:33:55 +00:00
|
|
|
if (!prefix_extractor) {
|
2014-06-18 23:36:48 +00:00
|
|
|
return Status::InvalidArgument(
|
|
|
|
"Prefix extractor is missing when opening a PlainTable built "
|
|
|
|
"using a prefix extractor");
|
2021-09-27 14:42:36 +00:00
|
|
|
} else if (prefix_extractor_in_file != prefix_extractor->AsString()) {
|
2014-06-18 23:36:48 +00:00
|
|
|
return Status::InvalidArgument(
|
|
|
|
"Prefix extractor given doesn't match the one used to build "
|
|
|
|
"PlainTable");
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 00:51:26 +00:00
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
EncodingType encoding_type = kPlain;
|
|
|
|
auto encoding_type_prop =
|
|
|
|
user_props.find(PlainTablePropertyNames::kEncodingType);
|
|
|
|
if (encoding_type_prop != user_props.end()) {
|
|
|
|
encoding_type = static_cast<EncodingType>(
|
|
|
|
DecodeFixed32(encoding_type_prop->second.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PlainTableReader> new_reader(new PlainTableReader(
|
2014-09-04 23:18:36 +00:00
|
|
|
ioptions, std::move(file), env_options, internal_comparator,
|
2019-10-21 23:51:19 +00:00
|
|
|
encoding_type, file_size, props.get(), prefix_extractor));
|
2014-06-18 23:36:48 +00:00
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
s = new_reader->MmapDataIfNeeded();
|
2013-10-29 03:34:02 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-06 00:51:26 +00:00
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
if (!full_scan_mode) {
|
2019-10-21 23:51:19 +00:00
|
|
|
s = new_reader->PopulateIndex(props.get(), bloom_bits_per_key,
|
|
|
|
hash_table_ratio, index_sparseness,
|
|
|
|
huge_page_tlb_size);
|
2014-06-18 23:36:48 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Flag to indicate it is a full scan mode so that none of the indexes
|
|
|
|
// can be used.
|
2014-07-18 23:58:13 +00:00
|
|
|
new_reader->full_scan_mode_ = true;
|
2014-06-18 23:36:48 +00:00
|
|
|
}
|
2019-10-18 21:43:17 +00:00
|
|
|
// PopulateIndex can add to the props, so don't store them until now
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 19:42:12 +00:00
|
|
|
new_reader->table_properties_ = std::move(props);
|
2014-06-18 23:36:48 +00:00
|
|
|
|
2019-01-26 01:07:00 +00:00
|
|
|
if (immortal_table && new_reader->file_info_.is_mmap_mode) {
|
|
|
|
new_reader->dummy_cleanable_.reset(new Cleanable());
|
|
|
|
}
|
|
|
|
|
2013-12-06 00:51:26 +00:00
|
|
|
*table_reader = std::move(new_reader);
|
2013-10-29 03:34:02 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:50:38 +00:00
|
|
|
void PlainTableReader::SetupForCompaction() {}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2018-05-21 21:33:55 +00:00
|
|
|
InternalIterator* PlainTableReader::NewIterator(
|
|
|
|
const ReadOptions& options, const SliceTransform* /* prefix_extractor */,
|
2019-06-20 21:28:22 +00:00
|
|
|
Arena* arena, bool /*skip_filters*/, TableReaderCaller /*caller*/,
|
2022-10-25 18:50:38 +00:00
|
|
|
size_t /*compaction_readahead_size*/, bool /* allow_unprepared_value */) {
|
2019-10-18 21:43:17 +00:00
|
|
|
// Not necessarily used here, but make sure this has been initialized
|
|
|
|
assert(table_properties_);
|
|
|
|
|
2020-01-28 22:42:21 +00:00
|
|
|
// Auto prefix mode is not implemented in PlainTable.
|
|
|
|
bool use_prefix_seek = !IsTotalOrderMode() && !options.total_order_seek &&
|
|
|
|
!options.auto_prefix_mode;
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
if (arena == nullptr) {
|
Fix interaction between CompactionFilter::Decision::kRemoveAndSkipUnt…
Summary:
Fixes the following scenario:
1. Set prefix extractor. Enable bloom filters, with `whole_key_filtering = false`. Use compaction filter that sometimes returns `kRemoveAndSkipUntil`.
2. Do a compaction.
3. Compaction creates an iterator with `total_order_seek = false`, calls `SeekToFirst()` on it, then repeatedly calls `Next()`.
4. At some point compaction filter returns `kRemoveAndSkipUntil`.
5. Compaction calls `Seek(skip_until)` on the iterator. The key that it seeks to happens to have prefix that doesn't match the bloom filter. Since `total_order_seek = false`, iterator becomes invalid, and compaction thinks that it has reached the end. The rest of the compaction input is silently discarded.
The fix is to make compaction iterator use `total_order_seek = true`.
The implementation for PlainTable is quite awkward. I've made `kRemoveAndSkipUntil` officially incompatible with PlainTable. If you try to use them together, compaction will fail, and DB will enter read-only mode (`bg_error_`). That's not a very graceful way to communicate a misconfiguration, but the alternatives don't seem worth the implementation time and complexity. To be able to check in advance that `kRemoveAndSkipUntil` is not going to be used with PlainTable, we'd need to extend the interface of either `CompactionFilter` or `InternalIterator`. It seems unlikely that anyone will ever want to use `kRemoveAndSkipUntil` with PlainTable: PlainTable probably has very few users, and `kRemoveAndSkipUntil` has only one user so far: us (logdevice).
Closes https://github.com/facebook/rocksdb/pull/2349
Differential Revision: D5110388
Pulled By: lightmark
fbshipit-source-id: ec29101a99d9dcd97db33923b87f72bce56cc17a
2017-06-02 21:56:31 +00:00
|
|
|
return new PlainTableIterator(this, use_prefix_seek);
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
} else {
|
|
|
|
auto mem = arena->AllocateAligned(sizeof(PlainTableIterator));
|
Fix interaction between CompactionFilter::Decision::kRemoveAndSkipUnt…
Summary:
Fixes the following scenario:
1. Set prefix extractor. Enable bloom filters, with `whole_key_filtering = false`. Use compaction filter that sometimes returns `kRemoveAndSkipUntil`.
2. Do a compaction.
3. Compaction creates an iterator with `total_order_seek = false`, calls `SeekToFirst()` on it, then repeatedly calls `Next()`.
4. At some point compaction filter returns `kRemoveAndSkipUntil`.
5. Compaction calls `Seek(skip_until)` on the iterator. The key that it seeks to happens to have prefix that doesn't match the bloom filter. Since `total_order_seek = false`, iterator becomes invalid, and compaction thinks that it has reached the end. The rest of the compaction input is silently discarded.
The fix is to make compaction iterator use `total_order_seek = true`.
The implementation for PlainTable is quite awkward. I've made `kRemoveAndSkipUntil` officially incompatible with PlainTable. If you try to use them together, compaction will fail, and DB will enter read-only mode (`bg_error_`). That's not a very graceful way to communicate a misconfiguration, but the alternatives don't seem worth the implementation time and complexity. To be able to check in advance that `kRemoveAndSkipUntil` is not going to be used with PlainTable, we'd need to extend the interface of either `CompactionFilter` or `InternalIterator`. It seems unlikely that anyone will ever want to use `kRemoveAndSkipUntil` with PlainTable: PlainTable probably has very few users, and `kRemoveAndSkipUntil` has only one user so far: us (logdevice).
Closes https://github.com/facebook/rocksdb/pull/2349
Differential Revision: D5110388
Pulled By: lightmark
fbshipit-source-id: ec29101a99d9dcd97db33923b87f72bce56cc17a
2017-06-02 21:56:31 +00:00
|
|
|
return new (mem) PlainTableIterator(this, use_prefix_seek);
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
Status PlainTableReader::PopulateIndexRecordList(
|
2019-03-27 17:18:56 +00:00
|
|
|
PlainTableIndexBuilder* index_builder,
|
|
|
|
std::vector<uint32_t>* prefix_hashes) {
|
2013-10-29 03:34:02 +00:00
|
|
|
Slice prev_key_prefix_slice;
|
2015-09-16 23:57:43 +00:00
|
|
|
std::string prev_key_prefix_buf;
|
2013-11-21 19:11:02 +00:00
|
|
|
uint32_t pos = data_start_offset_;
|
2013-12-20 17:35:24 +00:00
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
bool is_first_record = true;
|
|
|
|
Slice key_prefix_slice;
|
2015-09-16 23:57:43 +00:00
|
|
|
PlainTableKeyDecoder decoder(&file_info_, encoding_type_, user_key_len_,
|
2018-05-21 21:33:55 +00:00
|
|
|
prefix_extractor_);
|
2015-09-16 23:57:43 +00:00
|
|
|
while (pos < file_info_.data_end_offset) {
|
2013-11-21 19:11:02 +00:00
|
|
|
uint32_t key_offset = pos;
|
2014-01-27 21:53:22 +00:00
|
|
|
ParsedInternalKey key;
|
2014-01-25 05:10:19 +00:00
|
|
|
Slice value_slice;
|
2014-06-18 23:36:48 +00:00
|
|
|
bool seekable = false;
|
|
|
|
Status s = Next(&decoder, &pos, &key, nullptr, &value_slice, &seekable);
|
2014-02-08 00:25:38 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-07-18 23:58:13 +00:00
|
|
|
|
|
|
|
key_prefix_slice = GetPrefix(key);
|
2014-06-09 19:30:19 +00:00
|
|
|
if (enable_bloom_) {
|
|
|
|
bloom_.AddHash(GetSliceHash(key.user_key));
|
2014-07-18 23:58:13 +00:00
|
|
|
} else {
|
|
|
|
if (is_first_record || prev_key_prefix_slice != key_prefix_slice) {
|
|
|
|
if (!is_first_record) {
|
|
|
|
prefix_hashes->push_back(GetSliceHash(prev_key_prefix_slice));
|
|
|
|
}
|
2015-09-16 23:57:43 +00:00
|
|
|
if (file_info_.is_mmap_mode) {
|
|
|
|
prev_key_prefix_slice = key_prefix_slice;
|
|
|
|
} else {
|
|
|
|
prev_key_prefix_buf = key_prefix_slice.ToString();
|
|
|
|
prev_key_prefix_slice = prev_key_prefix_buf;
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
index_builder->AddKeyPrefix(GetPrefix(key), key_offset);
|
2014-06-18 23:36:48 +00:00
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
if (!seekable && is_first_record) {
|
|
|
|
return Status::Corruption("Key for a prefix is not seekable");
|
2013-11-21 19:11:02 +00:00
|
|
|
}
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
is_first_record = false;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
prefix_hashes->push_back(GetSliceHash(key_prefix_slice));
|
2014-07-21 17:31:33 +00:00
|
|
|
auto s = index_.InitFromRawData(index_builder->Finish());
|
|
|
|
return s;
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 17:24:38 +00:00
|
|
|
void PlainTableReader::AllocateBloom(int bloom_bits_per_key, int num_keys,
|
|
|
|
size_t huge_page_tlb_size) {
|
|
|
|
uint32_t bloom_total_bits = num_keys * bloom_bits_per_key;
|
|
|
|
if (bloom_total_bits > 0) {
|
|
|
|
enable_bloom_ = true;
|
|
|
|
bloom_.SetTotalBits(&arena_, bloom_total_bits, ioptions_.bloom_locality,
|
2021-04-26 19:43:02 +00:00
|
|
|
huge_page_tlb_size, ioptions_.logger);
|
2014-02-08 00:25:38 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
2013-11-21 19:11:02 +00:00
|
|
|
|
2019-09-13 17:24:38 +00:00
|
|
|
void PlainTableReader::FillBloom(const std::vector<uint32_t>& prefix_hashes) {
|
2014-07-18 23:58:13 +00:00
|
|
|
assert(bloom_.IsInitialized());
|
2019-09-13 17:24:38 +00:00
|
|
|
for (const auto prefix_hash : prefix_hashes) {
|
2014-07-18 23:58:13 +00:00
|
|
|
bloom_.AddHash(prefix_hash);
|
2013-11-21 19:11:02 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
Status PlainTableReader::MmapDataIfNeeded() {
|
|
|
|
if (file_info_.is_mmap_mode) {
|
|
|
|
// Get mmapped memory.
|
Group rocksdb.sst.read.micros stat by different user read IOActivity + misc (#11444)
Summary:
**Context/Summary:**
- Similar to https://github.com/facebook/rocksdb/pull/11288 but for user read such as `Get(), MultiGet(), DBIterator::XXX(), Verify(File)Checksum()`.
- For this, I refactored some user-facing `MultiGet` calls in `TransactionBase` and various types of `DB` so that it does not call a user-facing `Get()` but `GetImpl()` for passing the `ReadOptions::io_activity` check (see PR conversation)
- New user read stats breakdown are guarded by `kExceptDetailedTimers` since measurement shows they have 4-5% regression to the upstream/main.
- Misc
- More refactoring: with https://github.com/facebook/rocksdb/pull/11288, we complete passing `ReadOptions/IOOptions` to FS level. So we can now replace the previously [added](https://github.com/facebook/rocksdb/pull/9424) `rate_limiter_priority` parameter in `RandomAccessFileReader`'s `Read/MultiRead/Prefetch()` with `IOOptions::rate_limiter_priority`
- Also, `ReadAsync()` call time is measured in `SST_READ_MICRO` now
Pull Request resolved: https://github.com/facebook/rocksdb/pull/11444
Test Plan:
- CI fake db crash/stress test
- Microbenchmarking
**Build** `make clean && ROCKSDB_NO_FBCODE=1 DEBUG_LEVEL=0 make -jN db_basic_bench`
- google benchmark version: https://github.com/google/benchmark/commit/604f6fd3f4b34a84ec4eb4db81d842fa4db829cd
- db_basic_bench_base: upstream
- db_basic_bench_pr: db_basic_bench_base + this PR
- asyncread_db_basic_bench_base: upstream + [db basic bench patch for IteratorNext](https://github.com/facebook/rocksdb/compare/main...hx235:rocksdb:micro_bench_async_read)
- asyncread_db_basic_bench_pr: asyncread_db_basic_bench_base + this PR
**Test**
Get
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_{null_stat|base|pr} --benchmark_filter=DBGet/comp_style:0/max_data:134217728/per_key_size:256/enable_statistics:1/negative_query:0/enable_filter:0/mmap:1/threads:1 --benchmark_repetitions=1000
```
Result
```
Coming soon
```
AsyncRead
```
TEST_TMPDIR=/dev/shm ./asyncread_db_basic_bench_{base|pr} --benchmark_filter=IteratorNext/comp_style:0/max_data:134217728/per_key_size:256/enable_statistics:1/async_io:1/include_detailed_timers:0 --benchmark_repetitions=1000 > syncread_db_basic_bench_{base|pr}.out
```
Result
```
Base:
1956,1956,1968,1977,1979,1986,1988,1988,1988,1990,1991,1991,1993,1993,1993,1993,1994,1996,1997,1997,1997,1998,1999,2001,2001,2002,2004,2007,2007,2008,
PR (2.3% regression, due to measuring `SST_READ_MICRO` that wasn't measured before):
1993,2014,2016,2022,2024,2027,2027,2028,2028,2030,2031,2031,2032,2032,2038,2039,2042,2044,2044,2047,2047,2047,2048,2049,2050,2052,2052,2052,2053,2053,
```
Reviewed By: ajkr
Differential Revision: D45918925
Pulled By: hx235
fbshipit-source-id: 58a54560d9ebeb3a59b6d807639692614dad058a
2023-08-09 00:26:50 +00:00
|
|
|
return file_info_.file->Read(IOOptions(), 0,
|
|
|
|
static_cast<size_t>(file_size_),
|
|
|
|
&file_info_.file_data, nullptr, nullptr);
|
2015-09-16 23:57:43 +00:00
|
|
|
}
|
|
|
|
return Status::OK();
|
2014-06-18 23:36:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
Status PlainTableReader::PopulateIndex(TableProperties* props,
|
|
|
|
int bloom_bits_per_key,
|
|
|
|
double hash_table_ratio,
|
|
|
|
size_t index_sparseness,
|
|
|
|
size_t huge_page_tlb_size) {
|
2014-04-23 01:31:55 +00:00
|
|
|
assert(props != nullptr);
|
|
|
|
|
2019-03-01 23:41:55 +00:00
|
|
|
BlockContents index_block_contents;
|
2023-04-21 16:07:18 +00:00
|
|
|
|
Group SST write in flush, compaction and db open with new stats (#11910)
Summary:
## Context/Summary
Similar to https://github.com/facebook/rocksdb/pull/11288, https://github.com/facebook/rocksdb/pull/11444, categorizing SST/blob file write according to different io activities allows more insight into the activity.
For that, this PR does the following:
- Tag different write IOs by passing down and converting WriteOptions to IOOptions
- Add new SST_WRITE_MICROS histogram in WritableFileWriter::Append() and breakdown FILE_WRITE_{FLUSH|COMPACTION|DB_OPEN}_MICROS
Some related code refactory to make implementation cleaner:
- Blob stats
- Replace high-level write measurement with low-level WritableFileWriter::Append() measurement for BLOB_DB_BLOB_FILE_WRITE_MICROS. This is to make FILE_WRITE_{FLUSH|COMPACTION|DB_OPEN}_MICROS include blob file. As a consequence, this introduces some behavioral changes on it, see HISTORY and db bench test plan below for more info.
- Fix bugs where BLOB_DB_BLOB_FILE_SYNCED/BLOB_DB_BLOB_FILE_BYTES_WRITTEN include file failed to sync and bytes failed to write.
- Refactor WriteOptions constructor for easier construction with io_activity and rate_limiter_priority
- Refactor DBImpl::~DBImpl()/BlobDBImpl::Close() to bypass thread op verification
- Build table
- TableBuilderOptions now includes Read/WriteOpitons so BuildTable() do not need to take these two variables
- Replace the io_priority passed into BuildTable() with TableBuilderOptions::WriteOpitons::rate_limiter_priority. Similar for BlobFileBuilder.
This parameter is used for dynamically changing file io priority for flush, see https://github.com/facebook/rocksdb/pull/9988?fbclid=IwAR1DtKel6c-bRJAdesGo0jsbztRtciByNlvokbxkV6h_L-AE9MACzqRTT5s for more
- Update ThreadStatus::FLUSH_BYTES_WRITTEN to use io_activity to track flush IO in flush job and db open instead of io_priority
## Test
### db bench
Flush
```
./db_bench --statistics=1 --benchmarks=fillseq --num=100000 --write_buffer_size=100
rocksdb.sst.write.micros P50 : 1.830863 P95 : 4.094720 P99 : 6.578947 P100 : 26.000000 COUNT : 7875 SUM : 20377
rocksdb.file.write.flush.micros P50 : 1.830863 P95 : 4.094720 P99 : 6.578947 P100 : 26.000000 COUNT : 7875 SUM : 20377
rocksdb.file.write.compaction.micros P50 : 0.000000 P95 : 0.000000 P99 : 0.000000 P100 : 0.000000 COUNT : 0 SUM : 0
rocksdb.file.write.db.open.micros P50 : 0.000000 P95 : 0.000000 P99 : 0.000000 P100 : 0.000000 COUNT : 0 SUM : 0
```
compaction, db oopen
```
Setup: ./db_bench --statistics=1 --benchmarks=fillseq --num=10000 --disable_auto_compactions=1 -write_buffer_size=100 --db=../db_bench
Run:./db_bench --statistics=1 --benchmarks=compact --db=../db_bench --use_existing_db=1
rocksdb.sst.write.micros P50 : 2.675325 P95 : 9.578788 P99 : 18.780000 P100 : 314.000000 COUNT : 638 SUM : 3279
rocksdb.file.write.flush.micros P50 : 0.000000 P95 : 0.000000 P99 : 0.000000 P100 : 0.000000 COUNT : 0 SUM : 0
rocksdb.file.write.compaction.micros P50 : 2.757353 P95 : 9.610687 P99 : 19.316667 P100 : 314.000000 COUNT : 615 SUM : 3213
rocksdb.file.write.db.open.micros P50 : 2.055556 P95 : 3.925000 P99 : 9.000000 P100 : 9.000000 COUNT : 23 SUM : 66
```
blob stats - just to make sure they aren't broken by this PR
```
Integrated Blob DB
Setup: ./db_bench --enable_blob_files=1 --statistics=1 --benchmarks=fillseq --num=10000 --disable_auto_compactions=1 -write_buffer_size=100 --db=../db_bench
Run:./db_bench --enable_blob_files=1 --statistics=1 --benchmarks=compact --db=../db_bench --use_existing_db=1
pre-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 7.298246 P95 : 9.771930 P99 : 9.991813 P100 : 16.000000 COUNT : 235 SUM : 1600
rocksdb.blobdb.blob.file.synced COUNT : 1
rocksdb.blobdb.blob.file.bytes.written COUNT : 34842
post-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 2.000000 P95 : 2.829360 P99 : 2.993779 P100 : 9.000000 COUNT : 707 SUM : 1614
- COUNT is higher and values are smaller as it includes header and footer write
- COUNT is 3X higher due to each Append() count as one post-PR, while in pre-PR, 3 Append()s counts as one. See https://github.com/facebook/rocksdb/pull/11910/files#diff-32b811c0a1c000768cfb2532052b44dc0b3bf82253f3eab078e15ff201a0dabfL157-L164
rocksdb.blobdb.blob.file.synced COUNT : 1 (stay the same)
rocksdb.blobdb.blob.file.bytes.written COUNT : 34842 (stay the same)
```
```
Stacked Blob DB
Run: ./db_bench --use_blob_db=1 --statistics=1 --benchmarks=fillseq --num=10000 --disable_auto_compactions=1 -write_buffer_size=100 --db=../db_bench
pre-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 12.808042 P95 : 19.674497 P99 : 28.539683 P100 : 51.000000 COUNT : 10000 SUM : 140876
rocksdb.blobdb.blob.file.synced COUNT : 8
rocksdb.blobdb.blob.file.bytes.written COUNT : 1043445
post-PR:
rocksdb.blobdb.blob.file.write.micros P50 : 1.657370 P95 : 2.952175 P99 : 3.877519 P100 : 24.000000 COUNT : 30001 SUM : 67924
- COUNT is higher and values are smaller as it includes header and footer write
- COUNT is 3X higher due to each Append() count as one post-PR, while in pre-PR, 3 Append()s counts as one. See https://github.com/facebook/rocksdb/pull/11910/files#diff-32b811c0a1c000768cfb2532052b44dc0b3bf82253f3eab078e15ff201a0dabfL157-L164
rocksdb.blobdb.blob.file.synced COUNT : 8 (stay the same)
rocksdb.blobdb.blob.file.bytes.written COUNT : 1043445 (stay the same)
```
### Rehearsal CI stress test
Trigger 3 full runs of all our CI stress tests
### Performance
Flush
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_pre_pr --benchmark_filter=ManualFlush/key_num:524288/per_key_size:256 --benchmark_repetitions=1000
-- default: 1 thread is used to run benchmark; enable_statistics = true
Pre-pr: avg 507515519.3 ns
497686074,499444327,500862543,501389862,502994471,503744435,504142123,504224056,505724198,506610393,506837742,506955122,507695561,507929036,508307733,508312691,508999120,509963561,510142147,510698091,510743096,510769317,510957074,511053311,511371367,511409911,511432960,511642385,511691964,511730908,
Post-pr: avg 511971266.5 ns, regressed 0.88%
502744835,506502498,507735420,507929724,508313335,509548582,509994942,510107257,510715603,511046955,511352639,511458478,512117521,512317380,512766303,512972652,513059586,513804934,513808980,514059409,514187369,514389494,514447762,514616464,514622882,514641763,514666265,514716377,514990179,515502408,
```
Compaction
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_{pre|post}_pr --benchmark_filter=ManualCompaction/comp_style:0/max_data:134217728/per_key_size:256/enable_statistics:1 --benchmark_repetitions=1000
-- default: 1 thread is used to run benchmark
Pre-pr: avg 495346098.30 ns
492118301,493203526,494201411,494336607,495269217,495404950,496402598,497012157,497358370,498153846
Post-pr: avg 504528077.20, regressed 1.85%. "ManualCompaction" include flush so the isolated regression for compaction should be around 1.85-0.88 = 0.97%
502465338,502485945,502541789,502909283,503438601,504143885,506113087,506629423,507160414,507393007
```
Put with WAL (in case passing WriteOptions slows down this path even without collecting SST write stats)
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_pre_pr --benchmark_filter=DBPut/comp_style:0/max_data:107374182400/per_key_size:256/enable_statistics:1/wal:1 --benchmark_repetitions=1000
-- default: 1 thread is used to run benchmark
Pre-pr: avg 3848.10 ns
3814,3838,3839,3848,3854,3854,3854,3860,3860,3860
Post-pr: avg 3874.20 ns, regressed 0.68%
3863,3867,3871,3874,3875,3877,3877,3877,3880,3881
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/11910
Reviewed By: ajkr
Differential Revision: D49788060
Pulled By: hx235
fbshipit-source-id: 79e73699cda5be3b66461687e5147c2484fc5eff
2023-12-29 23:29:23 +00:00
|
|
|
// TODO: plumb Env::IOActivity, Env::IOPriority
|
2023-04-21 16:07:18 +00:00
|
|
|
const ReadOptions read_options;
|
|
|
|
Status s =
|
|
|
|
ReadMetaBlock(file_info_.file.get(), nullptr /* prefetch_buffer */,
|
|
|
|
file_size_, kPlainTableMagicNumber, ioptions_, read_options,
|
|
|
|
PlainTableIndexBuilder::kPlainTableIndexBlock,
|
|
|
|
BlockType::kIndex, &index_block_contents);
|
2019-03-01 23:41:55 +00:00
|
|
|
|
|
|
|
bool index_in_file = s.ok();
|
|
|
|
|
|
|
|
BlockContents bloom_block_contents;
|
|
|
|
bool bloom_in_file = false;
|
|
|
|
// We only need to read the bloom block if index block is in file.
|
|
|
|
if (index_in_file) {
|
|
|
|
s = ReadMetaBlock(file_info_.file.get(), nullptr /* prefetch_buffer */,
|
|
|
|
file_size_, kPlainTableMagicNumber, ioptions_,
|
2023-04-21 16:07:18 +00:00
|
|
|
read_options, BloomBlockBuilder::kBloomBlock,
|
|
|
|
BlockType::kFilter, &bloom_block_contents);
|
2019-03-01 23:41:55 +00:00
|
|
|
bloom_in_file = s.ok() && bloom_block_contents.data.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice* bloom_block;
|
|
|
|
if (bloom_in_file) {
|
|
|
|
// If bloom_block_contents.allocation is not empty (which will be the case
|
|
|
|
// for non-mmap mode), it holds the alloated memory for the bloom block.
|
|
|
|
// It needs to be kept alive to keep `bloom_block` valid.
|
|
|
|
bloom_block_alloc_ = std::move(bloom_block_contents.allocation);
|
|
|
|
bloom_block = &bloom_block_contents.data;
|
|
|
|
} else {
|
|
|
|
bloom_block = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice* index_block;
|
|
|
|
if (index_in_file) {
|
|
|
|
// If index_block_contents.allocation is not empty (which will be the case
|
|
|
|
// for non-mmap mode), it holds the alloated memory for the index block.
|
|
|
|
// It needs to be kept alive to keep `index_block` valid.
|
|
|
|
index_block_alloc_ = std::move(index_block_contents.allocation);
|
|
|
|
index_block = &index_block_contents.data;
|
|
|
|
} else {
|
|
|
|
index_block = nullptr;
|
|
|
|
}
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2018-05-21 21:33:55 +00:00
|
|
|
if ((prefix_extractor_ == nullptr) && (hash_table_ratio != 0)) {
|
|
|
|
// moptions.prefix_extractor is requried for a hash-based look-up.
|
2014-02-08 00:25:38 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"PlainTable requires a prefix extractor enable prefix hash mode.");
|
|
|
|
}
|
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
// First, read the whole file, for every kIndexIntervalForSamePrefixKeys rows
|
|
|
|
// for a prefix (starting from the first one), generate a record of (hash,
|
|
|
|
// offset) and append it to IndexRecordList, which is a data structure created
|
|
|
|
// to store them.
|
2014-02-08 00:25:38 +00:00
|
|
|
|
2019-03-01 23:41:55 +00:00
|
|
|
if (!index_in_file) {
|
|
|
|
// Allocate bloom filter here for total order mode.
|
|
|
|
if (IsTotalOrderMode()) {
|
2019-09-13 17:24:38 +00:00
|
|
|
AllocateBloom(bloom_bits_per_key,
|
2019-10-18 21:43:17 +00:00
|
|
|
static_cast<uint32_t>(props->num_entries),
|
2019-09-13 17:24:38 +00:00
|
|
|
huge_page_tlb_size);
|
2014-02-08 00:25:38 +00:00
|
|
|
}
|
2019-03-01 23:41:55 +00:00
|
|
|
} else if (bloom_in_file) {
|
|
|
|
enable_bloom_ = true;
|
|
|
|
auto num_blocks_property = props->user_collected_properties.find(
|
|
|
|
PlainTablePropertyNames::kNumBloomBlocks);
|
|
|
|
|
|
|
|
uint32_t num_blocks = 0;
|
|
|
|
if (num_blocks_property != props->user_collected_properties.end()) {
|
|
|
|
Slice temp_slice(num_blocks_property->second);
|
|
|
|
if (!GetVarint32(&temp_slice, &num_blocks)) {
|
|
|
|
num_blocks = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cast away const qualifier, because bloom_ won't be changed
|
2019-09-16 23:15:18 +00:00
|
|
|
bloom_.SetRawData(const_cast<char*>(bloom_block->data()),
|
|
|
|
static_cast<uint32_t>(bloom_block->size()) * 8,
|
|
|
|
num_blocks);
|
2019-03-01 23:41:55 +00:00
|
|
|
} else {
|
|
|
|
// Index in file but no bloom in file. Disable bloom filter in this case.
|
|
|
|
enable_bloom_ = false;
|
|
|
|
bloom_bits_per_key = 0;
|
2014-02-08 00:25:38 +00:00
|
|
|
}
|
2019-03-01 23:41:55 +00:00
|
|
|
|
2018-05-21 21:33:55 +00:00
|
|
|
PlainTableIndexBuilder index_builder(&arena_, ioptions_, prefix_extractor_,
|
|
|
|
index_sparseness, hash_table_ratio,
|
|
|
|
huge_page_tlb_size);
|
2014-07-18 23:58:13 +00:00
|
|
|
|
|
|
|
std::vector<uint32_t> prefix_hashes;
|
2019-03-01 23:41:55 +00:00
|
|
|
if (!index_in_file) {
|
2019-09-13 17:24:38 +00:00
|
|
|
// Populates _bloom if enabled (total order mode)
|
2019-03-01 23:41:55 +00:00
|
|
|
s = PopulateIndexRecordList(&index_builder, &prefix_hashes);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s = index_.InitFromRawData(*index_block);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index_in_file) {
|
2019-09-13 17:24:38 +00:00
|
|
|
if (!IsTotalOrderMode()) {
|
|
|
|
// Calculated bloom filter size and allocate memory for
|
|
|
|
// bloom filter based on the number of prefixes, then fill it.
|
|
|
|
AllocateBloom(bloom_bits_per_key, index_.GetNumPrefixes(),
|
|
|
|
huge_page_tlb_size);
|
|
|
|
if (enable_bloom_) {
|
|
|
|
FillBloom(prefix_hashes);
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 00:25:38 +00:00
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2014-04-23 01:31:55 +00:00
|
|
|
// Fill two table properties.
|
2019-03-01 23:41:55 +00:00
|
|
|
if (!index_in_file) {
|
|
|
|
props->user_collected_properties["plain_table_hash_table_size"] =
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(index_.GetIndexSize() * PlainTableIndex::kOffsetLen);
|
2019-03-01 23:41:55 +00:00
|
|
|
props->user_collected_properties["plain_table_sub_index_size"] =
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(index_.GetSubIndexSize());
|
2019-03-01 23:41:55 +00:00
|
|
|
} else {
|
|
|
|
props->user_collected_properties["plain_table_hash_table_size"] =
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(0);
|
2019-03-01 23:41:55 +00:00
|
|
|
props->user_collected_properties["plain_table_sub_index_size"] =
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(0);
|
2019-03-01 23:41:55 +00:00
|
|
|
}
|
2014-04-23 01:31:55 +00:00
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:29:40 +00:00
|
|
|
Status PlainTableReader::GetOffset(PlainTableKeyDecoder* decoder,
|
|
|
|
const Slice& target, const Slice& prefix,
|
2013-12-20 17:35:24 +00:00
|
|
|
uint32_t prefix_hash, bool& prefix_matched,
|
2014-02-13 23:27:59 +00:00
|
|
|
uint32_t* offset) const {
|
2013-11-21 19:11:02 +00:00
|
|
|
prefix_matched = false;
|
2014-07-18 23:58:13 +00:00
|
|
|
uint32_t prefix_index_offset;
|
|
|
|
auto res = index_.GetOffset(prefix_hash, &prefix_index_offset);
|
|
|
|
if (res == PlainTableIndex::kNoPrefixForBucket) {
|
2015-09-16 23:57:43 +00:00
|
|
|
*offset = file_info_.data_end_offset;
|
2013-12-20 17:35:24 +00:00
|
|
|
return Status::OK();
|
2014-07-18 23:58:13 +00:00
|
|
|
} else if (res == PlainTableIndex::kDirectToFile) {
|
|
|
|
*offset = prefix_index_offset;
|
2013-12-20 17:35:24 +00:00
|
|
|
return Status::OK();
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
// point to sub-index, need to do a binary search
|
2020-06-26 18:12:06 +00:00
|
|
|
uint32_t upper_bound = 0;
|
2014-07-18 23:58:13 +00:00
|
|
|
const char* base_ptr =
|
|
|
|
index_.GetSubIndexBasePtrAndUpperBound(prefix_index_offset, &upper_bound);
|
2013-10-29 03:34:02 +00:00
|
|
|
uint32_t low = 0;
|
2013-11-21 19:11:02 +00:00
|
|
|
uint32_t high = upper_bound;
|
2014-01-27 21:53:22 +00:00
|
|
|
ParsedInternalKey mid_key;
|
|
|
|
ParsedInternalKey parsed_target;
|
2020-10-28 17:11:13 +00:00
|
|
|
Status s = ParseInternalKey(target, &parsed_target,
|
|
|
|
false /* log_err_key */); // TODO
|
2023-12-01 19:15:17 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2013-11-21 19:11:02 +00:00
|
|
|
// The key is between [low, high). Do a binary search between it.
|
2013-10-29 03:34:02 +00:00
|
|
|
while (high - low > 1) {
|
|
|
|
uint32_t mid = (high + low) / 2;
|
2014-02-13 23:27:59 +00:00
|
|
|
uint32_t file_offset = GetFixed32Element(base_ptr, mid);
|
2015-09-16 23:57:43 +00:00
|
|
|
uint32_t tmp;
|
2020-10-01 02:15:42 +00:00
|
|
|
s = decoder->NextKeyNoValue(file_offset, &mid_key, nullptr, &tmp);
|
2013-12-20 17:35:24 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-01-27 21:53:22 +00:00
|
|
|
int cmp_result = internal_comparator_.Compare(mid_key, parsed_target);
|
|
|
|
if (cmp_result < 0) {
|
2013-10-29 03:34:02 +00:00
|
|
|
low = mid;
|
|
|
|
} else {
|
|
|
|
if (cmp_result == 0) {
|
|
|
|
// Happen to have found the exact key or target is smaller than the
|
|
|
|
// first key after base_offset.
|
2013-11-21 19:11:02 +00:00
|
|
|
prefix_matched = true;
|
2014-02-13 23:27:59 +00:00
|
|
|
*offset = file_offset;
|
2013-12-20 17:35:24 +00:00
|
|
|
return Status::OK();
|
2013-10-29 03:34:02 +00:00
|
|
|
} else {
|
|
|
|
high = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
// Both of the key at the position low or low+1 could share the same
|
|
|
|
// prefix as target. We need to rule out one of them to avoid to go
|
|
|
|
// to the wrong prefix.
|
2014-01-27 21:53:22 +00:00
|
|
|
ParsedInternalKey low_key;
|
2015-09-16 23:57:43 +00:00
|
|
|
uint32_t tmp;
|
2014-02-13 23:27:59 +00:00
|
|
|
uint32_t low_key_offset = GetFixed32Element(base_ptr, low);
|
2020-10-01 02:15:42 +00:00
|
|
|
s = decoder->NextKeyNoValue(low_key_offset, &low_key, nullptr, &tmp);
|
2014-06-18 23:36:48 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
if (GetPrefix(low_key) == prefix) {
|
|
|
|
prefix_matched = true;
|
2014-02-13 23:27:59 +00:00
|
|
|
*offset = low_key_offset;
|
2013-12-20 17:35:24 +00:00
|
|
|
} else if (low + 1 < upper_bound) {
|
|
|
|
// There is possible a next prefix, return it
|
2013-11-21 19:11:02 +00:00
|
|
|
prefix_matched = false;
|
2014-02-13 23:27:59 +00:00
|
|
|
*offset = GetFixed32Element(base_ptr, low + 1);
|
2013-12-20 17:35:24 +00:00
|
|
|
} else {
|
|
|
|
// target is larger than a key of the last prefix in this bucket
|
|
|
|
// but with a different prefix. Key does not exist.
|
2015-09-16 23:57:43 +00:00
|
|
|
*offset = file_info_.data_end_offset;
|
2013-11-21 19:11:02 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
return Status::OK();
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 00:25:38 +00:00
|
|
|
bool PlainTableReader::MatchBloom(uint32_t hash) const {
|
2015-10-07 18:23:20 +00:00
|
|
|
if (!enable_bloom_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bloom_.MayContainHash(hash)) {
|
|
|
|
PERF_COUNTER_ADD(bloom_sst_hit_count, 1);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
PERF_COUNTER_ADD(bloom_sst_miss_count, 1);
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-21 23:13:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
Status PlainTableReader::Next(PlainTableKeyDecoder* decoder, uint32_t* offset,
|
|
|
|
ParsedInternalKey* parsed_key,
|
|
|
|
Slice* internal_key, Slice* value,
|
|
|
|
bool* seekable) const {
|
2015-09-16 23:57:43 +00:00
|
|
|
if (*offset == file_info_.data_end_offset) {
|
|
|
|
*offset = file_info_.data_end_offset;
|
2013-11-21 19:11:02 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
if (*offset > file_info_.data_end_offset) {
|
2013-11-21 19:11:02 +00:00
|
|
|
return Status::Corruption("Offset is out of file size");
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
uint32_t bytes_read;
|
|
|
|
Status s = decoder->NextKey(*offset, parsed_key, internal_key, value,
|
|
|
|
&bytes_read, seekable);
|
2014-02-26 22:36:54 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2015-09-16 23:57:43 +00:00
|
|
|
*offset = *offset + bytes_read;
|
2013-11-21 19:11:02 +00:00
|
|
|
return Status::OK();
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 17:06:18 +00:00
|
|
|
void PlainTableReader::Prepare(const Slice& target) {
|
|
|
|
if (enable_bloom_) {
|
|
|
|
uint32_t prefix_hash = GetSliceHash(GetPrefix(target));
|
|
|
|
bloom_.Prefetch(prefix_hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
Status PlainTableReader::Get(const ReadOptions& /*ro*/, const Slice& target,
|
2018-05-21 21:33:55 +00:00
|
|
|
GetContext* get_context,
|
|
|
|
const SliceTransform* /* prefix_extractor */,
|
|
|
|
bool /*skip_filters*/) {
|
2013-11-21 19:11:02 +00:00
|
|
|
// Check bloom filter first.
|
2014-02-08 00:25:38 +00:00
|
|
|
Slice prefix_slice;
|
|
|
|
uint32_t prefix_hash;
|
|
|
|
if (IsTotalOrderMode()) {
|
2014-07-18 23:58:13 +00:00
|
|
|
if (full_scan_mode_) {
|
2014-06-18 23:36:48 +00:00
|
|
|
status_ =
|
|
|
|
Status::InvalidArgument("Get() is not allowed in full scan mode.");
|
|
|
|
}
|
2014-02-08 00:25:38 +00:00
|
|
|
// Match whole user key for bloom filter check.
|
2022-03-15 17:02:33 +00:00
|
|
|
if (!MatchBloom(GetSliceHash(ExtractUserKey(target)))) {
|
2014-02-08 00:25:38 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
// in total order mode, there is only one bucket 0, and we always use empty
|
|
|
|
// prefix.
|
|
|
|
prefix_slice = Slice();
|
|
|
|
prefix_hash = 0;
|
|
|
|
} else {
|
|
|
|
prefix_slice = GetPrefix(target);
|
|
|
|
prefix_hash = GetSliceHash(prefix_slice);
|
|
|
|
if (!MatchBloom(prefix_hash)) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
2013-11-21 19:11:02 +00:00
|
|
|
uint32_t offset;
|
|
|
|
bool prefix_match;
|
2015-11-18 02:29:40 +00:00
|
|
|
PlainTableKeyDecoder decoder(&file_info_, encoding_type_, user_key_len_,
|
2018-05-21 21:33:55 +00:00
|
|
|
prefix_extractor_);
|
2015-11-18 02:29:40 +00:00
|
|
|
Status s = GetOffset(&decoder, target, prefix_slice, prefix_hash,
|
|
|
|
prefix_match, &offset);
|
2015-09-16 23:57:43 +00:00
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-01-27 21:53:22 +00:00
|
|
|
ParsedInternalKey found_key;
|
|
|
|
ParsedInternalKey parsed_target;
|
2020-10-28 17:11:13 +00:00
|
|
|
s = ParseInternalKey(target, &parsed_target,
|
|
|
|
false /* log_err_key */); // TODO
|
2023-12-01 19:15:17 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2020-10-01 02:15:42 +00:00
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
Slice found_value;
|
2015-09-16 23:57:43 +00:00
|
|
|
while (offset < file_info_.data_end_offset) {
|
2014-10-31 18:59:54 +00:00
|
|
|
s = Next(&decoder, &offset, &found_key, nullptr, &found_value);
|
2013-11-21 19:11:02 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (!prefix_match) {
|
|
|
|
// Need to verify prefix for the first key found if it is not yet
|
|
|
|
// checked.
|
2013-12-20 17:35:24 +00:00
|
|
|
if (GetPrefix(found_key) != prefix_slice) {
|
|
|
|
return Status::OK();
|
2013-11-21 19:11:02 +00:00
|
|
|
}
|
|
|
|
prefix_match = true;
|
|
|
|
}
|
2014-09-29 18:09:09 +00:00
|
|
|
// TODO(ljin): since we know the key comparison result here,
|
|
|
|
// can we enable the fast path?
|
2014-01-27 21:53:22 +00:00
|
|
|
if (internal_comparator_.Compare(found_key, parsed_target) >= 0) {
|
2018-04-05 22:54:24 +00:00
|
|
|
bool dont_care __attribute__((__unused__));
|
Fix kBlockCacheTier read when merge-chain base value is in a blob file (#12462)
Summary:
The original goal is to propagate failures from `GetContext::SaveValue()` -> `GetContext::GetBlobValue()` -> `BlobFetcher::FetchBlob()` up to the user. This call sequence happens when a merge chain ends with a base value in a blob file.
There's also fixes for bugs encountered along the way where non-ok statuses were ignored/overwritten, and a bit of plumbing work for functions that had no capability to return a status.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12462
Test Plan:
A repro command
```
db=/dev/shm/dbstress_db ; exp=/dev/shm/dbstress_exp ; rm -rf $db $exp ; mkdir -p $db $exp
./db_stress \
--clear_column_family_one_in=0 \
--test_batches_snapshots=0 \
--write_fault_one_in=0 \
--use_put_entity_one_in=0 \
--prefixpercent=0 \
--read_fault_one_in=0 \
--readpercent=0 \
--reopen=0 \
--set_options_one_in=10000 \
--delpercent=0 \
--delrangepercent=0 \
--open_metadata_write_fault_one_in=0 \
--open_read_fault_one_in=0 \
--open_write_fault_one_in=0 \
--destroy_db_initially=0 \
--ingest_external_file_one_in=0 \
--iterpercent=0 \
--nooverwritepercent=0 \
--db=$db \
--enable_blob_files=1 \
--expected_values_dir=$exp \
--max_background_compactions=20 \
--max_bytes_for_level_base=2097152 \
--max_key=100000 \
--min_blob_size=0 \
--open_files=-1 \
--ops_per_thread=100000000 \
--prefix_size=-1 \
--target_file_size_base=524288 \
--use_merge=1 \
--value_size_mult=32 \
--write_buffer_size=524288 \
--writepercent=100
```
It used to fail like:
```
...
frame https://github.com/facebook/rocksdb/issues/9: 0x00007fc63903bc93 libc.so.6`__GI___assert_fail(assertion="HasDefaultColumn(columns)", file="fbcode/internal_repo_rocksdb/repo/db/wide/wide_columns_helper.h", line=33, function="static const rocksdb::Slice &rocksdb::WideColumnsHelper::GetDefaultColumn(const rocksdb::WideColumns &)") at assert.c:101:3
frame https://github.com/facebook/rocksdb/issues/10: 0x00000000006f7e92 db_stress`rocksdb::Version::Get(rocksdb::ReadOptions const&, rocksdb::LookupKey const&, rocksdb::PinnableSlice*, rocksdb::PinnableWideColumns*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, rocksdb::Status*, rocksdb::MergeContext*, unsigned long*, rocksdb::PinnedIteratorsManager*, bool*, bool*, unsigned long*, rocksdb::ReadCallback*, bool*, bool) [inlined] rocksdb::WideColumnsHelper::GetDefaultColumn(columns=size=0) at wide_columns_helper.h:33
frame https://github.com/facebook/rocksdb/issues/11: 0x00000000006f7e76 db_stress`rocksdb::Version::Get(this=0x00007fc5ec763000, read_options=<unavailable>, k=<unavailable>, value=0x0000000000000000, columns=0x00007fc6035fd1d8, timestamp=<unavailable>, status=0x00007fc6035fd250, merge_context=0x00007fc6035fce40, max_covering_tombstone_seq=0x00007fc6035fce90, pinned_iters_mgr=0x00007fc6035fcdf0, value_found=0x0000000000000000, key_exists=0x0000000000000000, seq=0x0000000000000000, callback=0x0000000000000000, is_blob=0x0000000000000000, do_merge=<unavailable>) at version_set.cc:2492
frame https://github.com/facebook/rocksdb/issues/12: 0x000000000051e245 db_stress`rocksdb::DBImpl::GetImpl(this=0x00007fc637a86000, read_options=0x00007fc6035fcf60, key=<unavailable>, get_impl_options=0x00007fc6035fd000) at db_impl.cc:2408
frame https://github.com/facebook/rocksdb/issues/13: 0x000000000050cec2 db_stress`rocksdb::DBImpl::GetEntity(this=0x00007fc637a86000, _read_options=<unavailable>, column_family=<unavailable>, key=0x00007fc6035fd3c8, columns=0x00007fc6035fd1d8) at db_impl.cc:2109
frame https://github.com/facebook/rocksdb/issues/14: 0x000000000074f688 db_stress`rocksdb::(anonymous namespace)::MemTableInserter::MergeCF(this=0x00007fc6035fd450, column_family_id=2, key=0x00007fc6035fd3c8, value=0x00007fc6035fd3a0) at write_batch.cc:2656
frame https://github.com/facebook/rocksdb/issues/15: 0x00000000007476fc db_stress`rocksdb::WriteBatchInternal::Iterate(wb=0x00007fc6035fe698, handler=0x00007fc6035fd450, begin=12, end=<unavailable>) at write_batch.cc:607
frame https://github.com/facebook/rocksdb/issues/16: 0x000000000074d7dd db_stress`rocksdb::WriteBatchInternal::InsertInto(rocksdb::WriteThread::WriteGroup&, unsigned long, rocksdb::ColumnFamilyMemTables*, rocksdb::FlushScheduler*, rocksdb::TrimHistoryScheduler*, bool, unsigned long, rocksdb::DB*, bool, bool, bool) [inlined] rocksdb::WriteBatch::Iterate(this=<unavailable>, handler=0x00007fc6035fd450) const at write_batch.cc:505
frame https://github.com/facebook/rocksdb/issues/17: 0x000000000074d77b db_stress`rocksdb::WriteBatchInternal::InsertInto(write_group=<unavailable>, sequence=<unavailable>, memtables=<unavailable>, flush_scheduler=<unavailable>, trim_history_scheduler=<unavailable>, ignore_missing_column_families=<unavailable>, recovery_log_number=0, db=0x00007fc637a86000, concurrent_memtable_writes=<unavailable>, seq_per_batch=false, batch_per_txn=<unavailable>) at write_batch.cc:3084
frame https://github.com/facebook/rocksdb/issues/18: 0x0000000000631d77 db_stress`rocksdb::DBImpl::PipelinedWriteImpl(this=0x00007fc637a86000, write_options=<unavailable>, my_batch=0x00007fc6035fe698, callback=0x0000000000000000, log_used=<unavailable>, log_ref=0, disable_memtable=<unavailable>, seq_used=0x0000000000000000) at db_impl_write.cc:807
frame https://github.com/facebook/rocksdb/issues/19: 0x000000000062ceeb db_stress`rocksdb::DBImpl::WriteImpl(this=<unavailable>, write_options=<unavailable>, my_batch=0x00007fc6035fe698, callback=0x0000000000000000, log_used=<unavailable>, log_ref=0, disable_memtable=<unavailable>, seq_used=0x0000000000000000, batch_cnt=0, pre_release_callback=0x0000000000000000, post_memtable_callback=0x0000000000000000) at db_impl_write.cc:312
frame https://github.com/facebook/rocksdb/issues/20: 0x000000000062c8ec db_stress`rocksdb::DBImpl::Write(this=0x00007fc637a86000, write_options=0x00007fc6035feca8, my_batch=0x00007fc6035fe698) at db_impl_write.cc:157
frame https://github.com/facebook/rocksdb/issues/21: 0x000000000062b847 db_stress`rocksdb::DB::Merge(this=0x00007fc637a86000, opt=0x00007fc6035feca8, column_family=0x00007fc6370bf140, key=0x00007fc6035fe8d8, value=0x00007fc6035fe830) at db_impl_write.cc:2544
frame https://github.com/facebook/rocksdb/issues/22: 0x000000000062b6ef db_stress`rocksdb::DBImpl::Merge(this=0x00007fc637a86000, o=<unavailable>, column_family=0x00007fc6370bf140, key=0x00007fc6035fe8d8, val=0x00007fc6035fe830) at db_impl_write.cc:72
frame https://github.com/facebook/rocksdb/issues/23: 0x00000000004d6397 db_stress`rocksdb::NonBatchedOpsStressTest::TestPut(this=0x00007fc637041000, thread=0x00007fc6370dbc00, write_opts=0x00007fc6035feca8, read_opts=0x00007fc6035fe9c8, rand_column_families=<unavailable>, rand_keys=size=1, value={P\xe9_\x03\xc6\x7f\0\0}) at no_batched_ops_stress.cc:1317
frame https://github.com/facebook/rocksdb/issues/24: 0x000000000049361d db_stress`rocksdb::StressTest::OperateDb(this=0x00007fc637041000, thread=0x00007fc6370dbc00) at db_stress_test_base.cc:1148
...
```
Reviewed By: ltamasi
Differential Revision: D55157795
Pulled By: ajkr
fbshipit-source-id: 5f7c1380ead5794c29d41680028e34b839744764
2024-03-21 19:38:53 +00:00
|
|
|
bool ret = get_context->SaveValue(found_key, found_value, &dont_care, &s,
|
|
|
|
dummy_cleanable_.get());
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (!ret) {
|
2014-01-27 21:53:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-21 19:11:02 +00:00
|
|
|
return Status::OK();
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 16:07:18 +00:00
|
|
|
uint64_t PlainTableReader::ApproximateOffsetOf(
|
|
|
|
const ReadOptions& /*read_options*/, const Slice& /*key*/,
|
|
|
|
TableReaderCaller /*caller*/) {
|
2013-10-29 03:34:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-21 16:07:18 +00:00
|
|
|
uint64_t PlainTableReader::ApproximateSize(const ReadOptions& /* read_options*/,
|
|
|
|
const Slice& /*start*/,
|
2019-08-16 21:16:49 +00:00
|
|
|
const Slice& /*end*/,
|
|
|
|
TableReaderCaller /*caller*/) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:25:38 +00:00
|
|
|
PlainTableIterator::PlainTableIterator(PlainTableReader* table,
|
|
|
|
bool use_prefix_seek)
|
2014-06-18 23:36:48 +00:00
|
|
|
: table_(table),
|
2015-09-16 23:57:43 +00:00
|
|
|
decoder_(&table_->file_info_, table_->encoding_type_,
|
|
|
|
table_->user_key_len_, table_->prefix_extractor_),
|
2014-06-18 23:36:48 +00:00
|
|
|
use_prefix_seek_(use_prefix_seek) {
|
2015-09-16 23:57:43 +00:00
|
|
|
next_offset_ = offset_ = table_->file_info_.data_end_offset;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 19:15:17 +00:00
|
|
|
PlainTableIterator::~PlainTableIterator() = default;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
bool PlainTableIterator::Valid() const {
|
2015-09-16 23:57:43 +00:00
|
|
|
return offset_ < table_->file_info_.data_end_offset &&
|
|
|
|
offset_ >= table_->data_start_offset_;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::SeekToFirst() {
|
Change and clarify the relationship between Valid(), status() and Seek*() for all iterators. Also fix some bugs
Summary:
Before this PR, Iterator/InternalIterator may simultaneously have non-ok status() and Valid() = true. That state means that the last operation failed, but the iterator is nevertheless positioned on some unspecified record. Likely intended uses of that are:
* If some sst files are corrupted, a normal iterator can be used to read the data from files that are not corrupted.
* When using read_tier = kBlockCacheTier, read the data that's in block cache, skipping over the data that is not.
However, this behavior wasn't documented well (and until recently the wiki on github had misleading incorrect information). In the code there's a lot of confusion about the relationship between status() and Valid(), and about whether Seek()/SeekToLast()/etc reset the status or not. There were a number of bugs caused by this confusion, both inside rocksdb and in the code that uses rocksdb (including ours).
This PR changes the convention to:
* If status() is not ok, Valid() always returns false.
* Any seek operation resets status. (Before the PR, it depended on iterator type and on particular error.)
This does sacrifice the two use cases listed above, but siying said it's ok.
Overview of the changes:
* A commit that adds missing status checks in MergingIterator. This fixes a bug that actually affects us, and we need it fixed. `DBIteratorTest.NonBlockingIterationBugRepro` explains the scenario.
* Changes to lots of iterator types to make all of them conform to the new convention. Some bug fixes along the way. By far the biggest changes are in DBIter, which is a big messy piece of code; I tried to make it less big and messy but mostly failed.
* A stress-test for DBIter, to gain some confidence that I didn't break it. It does a few million random operations on the iterator, while occasionally modifying the underlying data (like ForwardIterator does) and occasionally returning non-ok status from internal iterator.
To find the iterator types that needed changes I searched for "public .*Iterator" in the code. Here's an overview of all 27 iterator types:
Iterators that didn't need changes:
* status() is always ok(), or Valid() is always false: MemTableIterator, ModelIter, TestIterator, KVIter (2 classes with this name anonymous namespaces), LoggingForwardVectorIterator, VectorIterator, MockTableIterator, EmptyIterator, EmptyInternalIterator.
* Thin wrappers that always pass through Valid() and status(): ArenaWrappedDBIter, TtlIterator, InternalIteratorFromIterator.
Iterators with changes (see inline comments for details):
* DBIter - an overhaul:
- It used to silently skip corrupted keys (`FindParseableKey()`), which seems dangerous. This PR makes it just stop immediately after encountering a corrupted key, just like it would for other kinds of corruption. Let me know if there was actually some deeper meaning in this behavior and I should put it back.
- It had a few code paths silently discarding subiterator's status. The stress test caught a few.
- The backwards iteration code path was expecting the internal iterator's set of keys to be immutable. It's probably always true in practice at the moment, since ForwardIterator doesn't support backwards iteration, but this PR fixes it anyway. See added DBIteratorTest.ReverseToForwardBug for an example.
- Some parts of backwards iteration code path even did things like `assert(iter_->Valid())` after a seek, which is never a safe assumption.
- It used to not reset status on seek for some types of errors.
- Some simplifications and better comments.
- Some things got more complicated from the added error handling. I'm open to ideas for how to make it nicer.
* MergingIterator - check status after every operation on every subiterator, and in some places assert that valid subiterators have ok status.
* ForwardIterator - changed to the new convention, also slightly simplified.
* ForwardLevelIterator - fixed some bugs and simplified.
* LevelIterator - simplified.
* TwoLevelIterator - changed to the new convention. Also fixed a bug that would make SeekForPrev() sometimes silently ignore errors from first_level_iter_.
* BlockBasedTableIterator - minor changes.
* BlockIter - replaced `SetStatus()` with `Invalidate()` to make sure non-ok BlockIter is always invalid.
* PlainTableIterator - some seeks used to not reset status.
* CuckooTableIterator - tiny code cleanup.
* ManagedIterator - fixed some bugs.
* BaseDeltaIterator - changed to the new convention and fixed a bug.
* BlobDBIterator - seeks used to not reset status.
* KeyConvertingIterator - some small change.
Closes https://github.com/facebook/rocksdb/pull/3810
Differential Revision: D7888019
Pulled By: al13n321
fbshipit-source-id: 4aaf6d3421c545d16722a815b2fa2e7912bc851d
2018-05-17 09:44:14 +00:00
|
|
|
status_ = Status::OK();
|
2013-11-21 19:11:02 +00:00
|
|
|
next_offset_ = table_->data_start_offset_;
|
2015-09-16 23:57:43 +00:00
|
|
|
if (next_offset_ >= table_->file_info_.data_end_offset) {
|
|
|
|
next_offset_ = offset_ = table_->file_info_.data_end_offset;
|
2013-12-20 17:35:24 +00:00
|
|
|
} else {
|
|
|
|
Next();
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::SeekToLast() {
|
|
|
|
assert(false);
|
2014-02-08 00:25:38 +00:00
|
|
|
status_ = Status::NotSupported("SeekToLast() is not supported in PlainTable");
|
Change and clarify the relationship between Valid(), status() and Seek*() for all iterators. Also fix some bugs
Summary:
Before this PR, Iterator/InternalIterator may simultaneously have non-ok status() and Valid() = true. That state means that the last operation failed, but the iterator is nevertheless positioned on some unspecified record. Likely intended uses of that are:
* If some sst files are corrupted, a normal iterator can be used to read the data from files that are not corrupted.
* When using read_tier = kBlockCacheTier, read the data that's in block cache, skipping over the data that is not.
However, this behavior wasn't documented well (and until recently the wiki on github had misleading incorrect information). In the code there's a lot of confusion about the relationship between status() and Valid(), and about whether Seek()/SeekToLast()/etc reset the status or not. There were a number of bugs caused by this confusion, both inside rocksdb and in the code that uses rocksdb (including ours).
This PR changes the convention to:
* If status() is not ok, Valid() always returns false.
* Any seek operation resets status. (Before the PR, it depended on iterator type and on particular error.)
This does sacrifice the two use cases listed above, but siying said it's ok.
Overview of the changes:
* A commit that adds missing status checks in MergingIterator. This fixes a bug that actually affects us, and we need it fixed. `DBIteratorTest.NonBlockingIterationBugRepro` explains the scenario.
* Changes to lots of iterator types to make all of them conform to the new convention. Some bug fixes along the way. By far the biggest changes are in DBIter, which is a big messy piece of code; I tried to make it less big and messy but mostly failed.
* A stress-test for DBIter, to gain some confidence that I didn't break it. It does a few million random operations on the iterator, while occasionally modifying the underlying data (like ForwardIterator does) and occasionally returning non-ok status from internal iterator.
To find the iterator types that needed changes I searched for "public .*Iterator" in the code. Here's an overview of all 27 iterator types:
Iterators that didn't need changes:
* status() is always ok(), or Valid() is always false: MemTableIterator, ModelIter, TestIterator, KVIter (2 classes with this name anonymous namespaces), LoggingForwardVectorIterator, VectorIterator, MockTableIterator, EmptyIterator, EmptyInternalIterator.
* Thin wrappers that always pass through Valid() and status(): ArenaWrappedDBIter, TtlIterator, InternalIteratorFromIterator.
Iterators with changes (see inline comments for details):
* DBIter - an overhaul:
- It used to silently skip corrupted keys (`FindParseableKey()`), which seems dangerous. This PR makes it just stop immediately after encountering a corrupted key, just like it would for other kinds of corruption. Let me know if there was actually some deeper meaning in this behavior and I should put it back.
- It had a few code paths silently discarding subiterator's status. The stress test caught a few.
- The backwards iteration code path was expecting the internal iterator's set of keys to be immutable. It's probably always true in practice at the moment, since ForwardIterator doesn't support backwards iteration, but this PR fixes it anyway. See added DBIteratorTest.ReverseToForwardBug for an example.
- Some parts of backwards iteration code path even did things like `assert(iter_->Valid())` after a seek, which is never a safe assumption.
- It used to not reset status on seek for some types of errors.
- Some simplifications and better comments.
- Some things got more complicated from the added error handling. I'm open to ideas for how to make it nicer.
* MergingIterator - check status after every operation on every subiterator, and in some places assert that valid subiterators have ok status.
* ForwardIterator - changed to the new convention, also slightly simplified.
* ForwardLevelIterator - fixed some bugs and simplified.
* LevelIterator - simplified.
* TwoLevelIterator - changed to the new convention. Also fixed a bug that would make SeekForPrev() sometimes silently ignore errors from first_level_iter_.
* BlockBasedTableIterator - minor changes.
* BlockIter - replaced `SetStatus()` with `Invalidate()` to make sure non-ok BlockIter is always invalid.
* PlainTableIterator - some seeks used to not reset status.
* CuckooTableIterator - tiny code cleanup.
* ManagedIterator - fixed some bugs.
* BaseDeltaIterator - changed to the new convention and fixed a bug.
* BlobDBIterator - seeks used to not reset status.
* KeyConvertingIterator - some small change.
Closes https://github.com/facebook/rocksdb/pull/3810
Differential Revision: D7888019
Pulled By: al13n321
fbshipit-source-id: 4aaf6d3421c545d16722a815b2fa2e7912bc851d
2018-05-17 09:44:14 +00:00
|
|
|
next_offset_ = offset_ = table_->file_info_.data_end_offset;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::Seek(const Slice& target) {
|
Fix interaction between CompactionFilter::Decision::kRemoveAndSkipUnt…
Summary:
Fixes the following scenario:
1. Set prefix extractor. Enable bloom filters, with `whole_key_filtering = false`. Use compaction filter that sometimes returns `kRemoveAndSkipUntil`.
2. Do a compaction.
3. Compaction creates an iterator with `total_order_seek = false`, calls `SeekToFirst()` on it, then repeatedly calls `Next()`.
4. At some point compaction filter returns `kRemoveAndSkipUntil`.
5. Compaction calls `Seek(skip_until)` on the iterator. The key that it seeks to happens to have prefix that doesn't match the bloom filter. Since `total_order_seek = false`, iterator becomes invalid, and compaction thinks that it has reached the end. The rest of the compaction input is silently discarded.
The fix is to make compaction iterator use `total_order_seek = true`.
The implementation for PlainTable is quite awkward. I've made `kRemoveAndSkipUntil` officially incompatible with PlainTable. If you try to use them together, compaction will fail, and DB will enter read-only mode (`bg_error_`). That's not a very graceful way to communicate a misconfiguration, but the alternatives don't seem worth the implementation time and complexity. To be able to check in advance that `kRemoveAndSkipUntil` is not going to be used with PlainTable, we'd need to extend the interface of either `CompactionFilter` or `InternalIterator`. It seems unlikely that anyone will ever want to use `kRemoveAndSkipUntil` with PlainTable: PlainTable probably has very few users, and `kRemoveAndSkipUntil` has only one user so far: us (logdevice).
Closes https://github.com/facebook/rocksdb/pull/2349
Differential Revision: D5110388
Pulled By: lightmark
fbshipit-source-id: ec29101a99d9dcd97db33923b87f72bce56cc17a
2017-06-02 21:56:31 +00:00
|
|
|
if (use_prefix_seek_ != !table_->IsTotalOrderMode()) {
|
|
|
|
// This check is done here instead of NewIterator() to permit creating an
|
|
|
|
// iterator with total_order_seek = true even if we won't be able to Seek()
|
|
|
|
// it. This is needed for compaction: it creates iterator with
|
|
|
|
// total_order_seek = true but usually never does Seek() on it,
|
|
|
|
// only SeekToFirst().
|
2022-10-25 18:50:38 +00:00
|
|
|
status_ = Status::InvalidArgument(
|
|
|
|
"total_order_seek not implemented for PlainTable.");
|
Fix interaction between CompactionFilter::Decision::kRemoveAndSkipUnt…
Summary:
Fixes the following scenario:
1. Set prefix extractor. Enable bloom filters, with `whole_key_filtering = false`. Use compaction filter that sometimes returns `kRemoveAndSkipUntil`.
2. Do a compaction.
3. Compaction creates an iterator with `total_order_seek = false`, calls `SeekToFirst()` on it, then repeatedly calls `Next()`.
4. At some point compaction filter returns `kRemoveAndSkipUntil`.
5. Compaction calls `Seek(skip_until)` on the iterator. The key that it seeks to happens to have prefix that doesn't match the bloom filter. Since `total_order_seek = false`, iterator becomes invalid, and compaction thinks that it has reached the end. The rest of the compaction input is silently discarded.
The fix is to make compaction iterator use `total_order_seek = true`.
The implementation for PlainTable is quite awkward. I've made `kRemoveAndSkipUntil` officially incompatible with PlainTable. If you try to use them together, compaction will fail, and DB will enter read-only mode (`bg_error_`). That's not a very graceful way to communicate a misconfiguration, but the alternatives don't seem worth the implementation time and complexity. To be able to check in advance that `kRemoveAndSkipUntil` is not going to be used with PlainTable, we'd need to extend the interface of either `CompactionFilter` or `InternalIterator`. It seems unlikely that anyone will ever want to use `kRemoveAndSkipUntil` with PlainTable: PlainTable probably has very few users, and `kRemoveAndSkipUntil` has only one user so far: us (logdevice).
Closes https://github.com/facebook/rocksdb/pull/2349
Differential Revision: D5110388
Pulled By: lightmark
fbshipit-source-id: ec29101a99d9dcd97db33923b87f72bce56cc17a
2017-06-02 21:56:31 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:25:38 +00:00
|
|
|
// If the user doesn't set prefix seek option and we are not able to do a
|
|
|
|
// total Seek(). assert failure.
|
Fix interaction between CompactionFilter::Decision::kRemoveAndSkipUnt…
Summary:
Fixes the following scenario:
1. Set prefix extractor. Enable bloom filters, with `whole_key_filtering = false`. Use compaction filter that sometimes returns `kRemoveAndSkipUntil`.
2. Do a compaction.
3. Compaction creates an iterator with `total_order_seek = false`, calls `SeekToFirst()` on it, then repeatedly calls `Next()`.
4. At some point compaction filter returns `kRemoveAndSkipUntil`.
5. Compaction calls `Seek(skip_until)` on the iterator. The key that it seeks to happens to have prefix that doesn't match the bloom filter. Since `total_order_seek = false`, iterator becomes invalid, and compaction thinks that it has reached the end. The rest of the compaction input is silently discarded.
The fix is to make compaction iterator use `total_order_seek = true`.
The implementation for PlainTable is quite awkward. I've made `kRemoveAndSkipUntil` officially incompatible with PlainTable. If you try to use them together, compaction will fail, and DB will enter read-only mode (`bg_error_`). That's not a very graceful way to communicate a misconfiguration, but the alternatives don't seem worth the implementation time and complexity. To be able to check in advance that `kRemoveAndSkipUntil` is not going to be used with PlainTable, we'd need to extend the interface of either `CompactionFilter` or `InternalIterator`. It seems unlikely that anyone will ever want to use `kRemoveAndSkipUntil` with PlainTable: PlainTable probably has very few users, and `kRemoveAndSkipUntil` has only one user so far: us (logdevice).
Closes https://github.com/facebook/rocksdb/pull/2349
Differential Revision: D5110388
Pulled By: lightmark
fbshipit-source-id: ec29101a99d9dcd97db33923b87f72bce56cc17a
2017-06-02 21:56:31 +00:00
|
|
|
if (table_->IsTotalOrderMode()) {
|
2014-07-18 23:58:13 +00:00
|
|
|
if (table_->full_scan_mode_) {
|
2014-06-18 23:36:48 +00:00
|
|
|
status_ =
|
|
|
|
Status::InvalidArgument("Seek() is not allowed in full scan mode.");
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2014-06-18 23:36:48 +00:00
|
|
|
return;
|
2014-07-18 23:58:13 +00:00
|
|
|
} else if (table_->GetIndexSize() > 1) {
|
2014-06-18 23:36:48 +00:00
|
|
|
assert(false);
|
|
|
|
status_ = Status::NotSupported(
|
|
|
|
"PlainTable cannot issue non-prefix seek unless in total order "
|
|
|
|
"mode.");
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2014-06-18 23:36:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-08 00:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Slice prefix_slice = table_->GetPrefix(target);
|
2014-04-01 22:00:48 +00:00
|
|
|
uint32_t prefix_hash = 0;
|
|
|
|
// Bloom filter is ignored in total-order mode.
|
|
|
|
if (!table_->IsTotalOrderMode()) {
|
2014-02-08 00:25:38 +00:00
|
|
|
prefix_hash = GetSliceHash(prefix_slice);
|
2014-04-01 22:00:48 +00:00
|
|
|
if (!table_->MatchBloom(prefix_hash)) {
|
Change and clarify the relationship between Valid(), status() and Seek*() for all iterators. Also fix some bugs
Summary:
Before this PR, Iterator/InternalIterator may simultaneously have non-ok status() and Valid() = true. That state means that the last operation failed, but the iterator is nevertheless positioned on some unspecified record. Likely intended uses of that are:
* If some sst files are corrupted, a normal iterator can be used to read the data from files that are not corrupted.
* When using read_tier = kBlockCacheTier, read the data that's in block cache, skipping over the data that is not.
However, this behavior wasn't documented well (and until recently the wiki on github had misleading incorrect information). In the code there's a lot of confusion about the relationship between status() and Valid(), and about whether Seek()/SeekToLast()/etc reset the status or not. There were a number of bugs caused by this confusion, both inside rocksdb and in the code that uses rocksdb (including ours).
This PR changes the convention to:
* If status() is not ok, Valid() always returns false.
* Any seek operation resets status. (Before the PR, it depended on iterator type and on particular error.)
This does sacrifice the two use cases listed above, but siying said it's ok.
Overview of the changes:
* A commit that adds missing status checks in MergingIterator. This fixes a bug that actually affects us, and we need it fixed. `DBIteratorTest.NonBlockingIterationBugRepro` explains the scenario.
* Changes to lots of iterator types to make all of them conform to the new convention. Some bug fixes along the way. By far the biggest changes are in DBIter, which is a big messy piece of code; I tried to make it less big and messy but mostly failed.
* A stress-test for DBIter, to gain some confidence that I didn't break it. It does a few million random operations on the iterator, while occasionally modifying the underlying data (like ForwardIterator does) and occasionally returning non-ok status from internal iterator.
To find the iterator types that needed changes I searched for "public .*Iterator" in the code. Here's an overview of all 27 iterator types:
Iterators that didn't need changes:
* status() is always ok(), or Valid() is always false: MemTableIterator, ModelIter, TestIterator, KVIter (2 classes with this name anonymous namespaces), LoggingForwardVectorIterator, VectorIterator, MockTableIterator, EmptyIterator, EmptyInternalIterator.
* Thin wrappers that always pass through Valid() and status(): ArenaWrappedDBIter, TtlIterator, InternalIteratorFromIterator.
Iterators with changes (see inline comments for details):
* DBIter - an overhaul:
- It used to silently skip corrupted keys (`FindParseableKey()`), which seems dangerous. This PR makes it just stop immediately after encountering a corrupted key, just like it would for other kinds of corruption. Let me know if there was actually some deeper meaning in this behavior and I should put it back.
- It had a few code paths silently discarding subiterator's status. The stress test caught a few.
- The backwards iteration code path was expecting the internal iterator's set of keys to be immutable. It's probably always true in practice at the moment, since ForwardIterator doesn't support backwards iteration, but this PR fixes it anyway. See added DBIteratorTest.ReverseToForwardBug for an example.
- Some parts of backwards iteration code path even did things like `assert(iter_->Valid())` after a seek, which is never a safe assumption.
- It used to not reset status on seek for some types of errors.
- Some simplifications and better comments.
- Some things got more complicated from the added error handling. I'm open to ideas for how to make it nicer.
* MergingIterator - check status after every operation on every subiterator, and in some places assert that valid subiterators have ok status.
* ForwardIterator - changed to the new convention, also slightly simplified.
* ForwardLevelIterator - fixed some bugs and simplified.
* LevelIterator - simplified.
* TwoLevelIterator - changed to the new convention. Also fixed a bug that would make SeekForPrev() sometimes silently ignore errors from first_level_iter_.
* BlockBasedTableIterator - minor changes.
* BlockIter - replaced `SetStatus()` with `Invalidate()` to make sure non-ok BlockIter is always invalid.
* PlainTableIterator - some seeks used to not reset status.
* CuckooTableIterator - tiny code cleanup.
* ManagedIterator - fixed some bugs.
* BaseDeltaIterator - changed to the new convention and fixed a bug.
* BlobDBIterator - seeks used to not reset status.
* KeyConvertingIterator - some small change.
Closes https://github.com/facebook/rocksdb/pull/3810
Differential Revision: D7888019
Pulled By: al13n321
fbshipit-source-id: 4aaf6d3421c545d16722a815b2fa2e7912bc851d
2018-05-17 09:44:14 +00:00
|
|
|
status_ = Status::OK();
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2014-04-01 22:00:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-21 23:13:45 +00:00
|
|
|
}
|
2013-11-21 19:11:02 +00:00
|
|
|
bool prefix_match;
|
2015-11-18 02:29:40 +00:00
|
|
|
status_ = table_->GetOffset(&decoder_, target, prefix_slice, prefix_hash,
|
|
|
|
prefix_match, &next_offset_);
|
2013-12-20 17:35:24 +00:00
|
|
|
if (!status_.ok()) {
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2013-12-20 17:35:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-21 19:11:02 +00:00
|
|
|
|
2015-09-16 23:57:43 +00:00
|
|
|
if (next_offset_ < table_->file_info_.data_end_offset) {
|
2013-11-21 19:11:02 +00:00
|
|
|
for (Next(); status_.ok() && Valid(); Next()) {
|
|
|
|
if (!prefix_match) {
|
|
|
|
// Need to verify the first key's prefix
|
2013-12-20 17:35:24 +00:00
|
|
|
if (table_->GetPrefix(key()) != prefix_slice) {
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2013-11-21 19:11:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
prefix_match = true;
|
|
|
|
}
|
2014-01-27 21:53:22 +00:00
|
|
|
if (table_->internal_comparator_.Compare(key(), target) >= 0) {
|
2013-11-21 19:11:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
2013-11-21 19:11:02 +00:00
|
|
|
} else {
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = table_->file_info_.data_end_offset;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
void PlainTableIterator::SeekForPrev(const Slice& /*target*/) {
|
2016-09-28 01:20:57 +00:00
|
|
|
assert(false);
|
|
|
|
status_ =
|
|
|
|
Status::NotSupported("SeekForPrev() is not supported in PlainTable");
|
Change and clarify the relationship between Valid(), status() and Seek*() for all iterators. Also fix some bugs
Summary:
Before this PR, Iterator/InternalIterator may simultaneously have non-ok status() and Valid() = true. That state means that the last operation failed, but the iterator is nevertheless positioned on some unspecified record. Likely intended uses of that are:
* If some sst files are corrupted, a normal iterator can be used to read the data from files that are not corrupted.
* When using read_tier = kBlockCacheTier, read the data that's in block cache, skipping over the data that is not.
However, this behavior wasn't documented well (and until recently the wiki on github had misleading incorrect information). In the code there's a lot of confusion about the relationship between status() and Valid(), and about whether Seek()/SeekToLast()/etc reset the status or not. There were a number of bugs caused by this confusion, both inside rocksdb and in the code that uses rocksdb (including ours).
This PR changes the convention to:
* If status() is not ok, Valid() always returns false.
* Any seek operation resets status. (Before the PR, it depended on iterator type and on particular error.)
This does sacrifice the two use cases listed above, but siying said it's ok.
Overview of the changes:
* A commit that adds missing status checks in MergingIterator. This fixes a bug that actually affects us, and we need it fixed. `DBIteratorTest.NonBlockingIterationBugRepro` explains the scenario.
* Changes to lots of iterator types to make all of them conform to the new convention. Some bug fixes along the way. By far the biggest changes are in DBIter, which is a big messy piece of code; I tried to make it less big and messy but mostly failed.
* A stress-test for DBIter, to gain some confidence that I didn't break it. It does a few million random operations on the iterator, while occasionally modifying the underlying data (like ForwardIterator does) and occasionally returning non-ok status from internal iterator.
To find the iterator types that needed changes I searched for "public .*Iterator" in the code. Here's an overview of all 27 iterator types:
Iterators that didn't need changes:
* status() is always ok(), or Valid() is always false: MemTableIterator, ModelIter, TestIterator, KVIter (2 classes with this name anonymous namespaces), LoggingForwardVectorIterator, VectorIterator, MockTableIterator, EmptyIterator, EmptyInternalIterator.
* Thin wrappers that always pass through Valid() and status(): ArenaWrappedDBIter, TtlIterator, InternalIteratorFromIterator.
Iterators with changes (see inline comments for details):
* DBIter - an overhaul:
- It used to silently skip corrupted keys (`FindParseableKey()`), which seems dangerous. This PR makes it just stop immediately after encountering a corrupted key, just like it would for other kinds of corruption. Let me know if there was actually some deeper meaning in this behavior and I should put it back.
- It had a few code paths silently discarding subiterator's status. The stress test caught a few.
- The backwards iteration code path was expecting the internal iterator's set of keys to be immutable. It's probably always true in practice at the moment, since ForwardIterator doesn't support backwards iteration, but this PR fixes it anyway. See added DBIteratorTest.ReverseToForwardBug for an example.
- Some parts of backwards iteration code path even did things like `assert(iter_->Valid())` after a seek, which is never a safe assumption.
- It used to not reset status on seek for some types of errors.
- Some simplifications and better comments.
- Some things got more complicated from the added error handling. I'm open to ideas for how to make it nicer.
* MergingIterator - check status after every operation on every subiterator, and in some places assert that valid subiterators have ok status.
* ForwardIterator - changed to the new convention, also slightly simplified.
* ForwardLevelIterator - fixed some bugs and simplified.
* LevelIterator - simplified.
* TwoLevelIterator - changed to the new convention. Also fixed a bug that would make SeekForPrev() sometimes silently ignore errors from first_level_iter_.
* BlockBasedTableIterator - minor changes.
* BlockIter - replaced `SetStatus()` with `Invalidate()` to make sure non-ok BlockIter is always invalid.
* PlainTableIterator - some seeks used to not reset status.
* CuckooTableIterator - tiny code cleanup.
* ManagedIterator - fixed some bugs.
* BaseDeltaIterator - changed to the new convention and fixed a bug.
* BlobDBIterator - seeks used to not reset status.
* KeyConvertingIterator - some small change.
Closes https://github.com/facebook/rocksdb/pull/3810
Differential Revision: D7888019
Pulled By: al13n321
fbshipit-source-id: 4aaf6d3421c545d16722a815b2fa2e7912bc851d
2018-05-17 09:44:14 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2016-09-28 01:20:57 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
void PlainTableIterator::Next() {
|
|
|
|
offset_ = next_offset_;
|
2015-09-16 23:57:43 +00:00
|
|
|
if (offset_ < table_->file_info_.data_end_offset) {
|
2014-01-27 21:53:22 +00:00
|
|
|
Slice tmp_slice;
|
|
|
|
ParsedInternalKey parsed_key;
|
2014-06-18 23:36:48 +00:00
|
|
|
status_ =
|
|
|
|
table_->Next(&decoder_, &next_offset_, &parsed_key, &key_, &value_);
|
|
|
|
if (!status_.ok()) {
|
2015-09-16 23:57:43 +00:00
|
|
|
offset_ = next_offset_ = table_->file_info_.data_end_offset;
|
2014-01-27 21:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:50:38 +00:00
|
|
|
void PlainTableIterator::Prev() { assert(false); }
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
Slice PlainTableIterator::key() const {
|
2014-01-27 21:53:22 +00:00
|
|
|
assert(Valid());
|
2014-06-18 23:36:48 +00:00
|
|
|
return key_;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Slice PlainTableIterator::value() const {
|
2014-01-27 21:53:22 +00:00
|
|
|
assert(Valid());
|
2013-10-29 03:34:02 +00:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:50:38 +00:00
|
|
|
Status PlainTableIterator::status() const { return status_; }
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|