Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions db/db_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,8 @@ class DBTestBase : public testing::Test {
ResetTableProperties(tp);
int count = sscanf(
tp_string.c_str(),
"# data blocks %" SCNu64 " # entries %" SCNu64 " # deletions %" SCNu64
"# data blocks %" SCNu64 " # uniform blocks %" SCNu64
" # entries %" SCNu64 " # deletions %" SCNu64
" # merge operands %" SCNu64 " # range deletions %" SCNu64
" raw key size %" SCNu64
" raw average key size %lf "
Expand All @@ -1455,12 +1456,12 @@ class DBTestBase : public testing::Test {
" data block size %" SCNu64 " data uncompressed size %" SCNu64
" index block size (user-key? %" SCNu64 ", delta-value? %" SCNu64
") %" SCNu64 " filter block size %" SCNu64,
&tp->num_data_blocks, &tp->num_entries, &tp->num_deletions,
&tp->num_merge_operands, &tp->num_range_deletions, &tp->raw_key_size,
&dummy_double, &tp->raw_value_size, &dummy_double, &tp->data_size,
&tp->uncompressed_data_size, &tp->index_key_is_user_key,
&tp->num_data_blocks, &tp->num_uniform_blocks, &tp->num_entries,
&tp->num_deletions, &tp->num_merge_operands, &tp->num_range_deletions,
&tp->raw_key_size, &dummy_double, &tp->raw_value_size, &dummy_double,
&tp->data_size, &tp->uncompressed_data_size, &tp->index_key_is_user_key,
&tp->index_value_is_delta_encoded, &tp->index_size, &tp->filter_size);
ASSERT_EQ(count, 15);
ASSERT_EQ(count, 16);
}

private: // Prone to error on direct use
Expand Down
3 changes: 3 additions & 0 deletions include/rocksdb/table_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct TablePropertiesNames {
static const std::string kRawKeySize;
static const std::string kRawValueSize;
static const std::string kNumDataBlocks;
static const std::string kNumUniformBlocks;
static const std::string kNumEntries;
static const std::string kNumFilterEntries;
static const std::string kDeletedKeys;
Expand Down Expand Up @@ -245,6 +246,8 @@ struct TableProperties {
uint64_t raw_value_size = 0;
// the number of blocks in this table
uint64_t num_data_blocks = 0;
// the number of uniform blocks in this table
uint64_t num_uniform_blocks = 0;
// the number of entries in this table
uint64_t num_entries = 0;
// the number of unique entries (keys or prefixes) added to filters
Expand Down
2 changes: 1 addition & 1 deletion options/options_settable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ TEST_F(OptionsSettableTest, TablePropertiesAllFieldsSettable) {
"external_sst_file_global_seqno_offset=0;num_merge_operands=0;index_key_"
"is_user_key=0;key_largest_seqno=18446744073709551615;key_smallest_seqno="
"18;data_block_restart_interval=16;index_block_restart_interval=1;"
"separate_key_value_in_data_block=0;",
"separate_key_value_in_data_block=0;num_uniform_blocks=0;",
new_tp));

// All bytes are set from the parse
Expand Down
4 changes: 4 additions & 0 deletions table/block_based/block_based_table_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,10 @@ void BlockBasedTableBuilder::WriteIndexBlock(
// The last index_block_handle will be for the partition index block
}
}
if (LIKELY(ok())) {
rep_->props.num_uniform_blocks =
rep_->index_builder->NumUniformIndexBlocks();
}
// If success and need to record in metaindex rather than footer...
if (LIKELY(ok()) && !FormatVersionUsesIndexHandleInFooter(
rep_->table_options.format_version)) {
Expand Down
6 changes: 4 additions & 2 deletions table/block_based/block_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ BlockBuilder::BlockBuilder(
restarts_(1, 0), // First restart point is at offset 0
counter_(0),
finished_(false),
is_uniform_(false),
uniform_cv_threshold_(uniform_cv_threshold),
statistics_(statistics),
use_separated_kv_storage_(use_separated_kv_storage) {
Expand All @@ -131,6 +132,7 @@ void BlockBuilder::Reset() {
(use_separated_kv_storage_ ? sizeof(uint32_t) : 0);
counter_ = 0;
finished_ = false;
is_uniform_ = false;
last_key_.clear();
if (data_block_hash_index_builder_.Valid()) {
data_block_hash_index_builder_.Reset();
Expand Down Expand Up @@ -185,7 +187,7 @@ size_t BlockBuilder::EstimateSizeAfterKV(const Slice& key,
}

Slice BlockBuilder::Finish() {
bool is_uniform = ScanForUniformity();
is_uniform_ = ScanForUniformity();

// Append restart array
size_t values_buffer_offset = buffer_.size();
Expand All @@ -201,7 +203,7 @@ Slice BlockBuilder::Finish() {
DataBlockFooter footer;
footer.num_restarts = static_cast<uint32_t>(restarts_.size());
footer.index_type = BlockBasedTableOptions::kDataBlockBinarySearch;
footer.is_uniform = is_uniform;
footer.is_uniform = is_uniform_;
if (data_block_hash_index_builder_.Valid() &&
CurrentSizeEstimate() <= kMaxBlockSizeSupportedByHashIndex) {
data_block_hash_index_builder_.Finish(buffer_);
Expand Down
5 changes: 5 additions & 0 deletions table/block_based/block_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class BlockBuilder {

std::string& MutableBuffer() { return buffer_; }

// Returns true if the most recently Finish()'d block was marked uniform.
// REQUIRES: Finish() has been called.
bool IsUniform() const { return is_uniform_; }

private:
inline void AddWithLastKeyImpl(const Slice& key, const Slice& value,
const Slice& last_key,
Expand Down Expand Up @@ -128,6 +132,7 @@ class BlockBuilder {
size_t estimate_;
int counter_; // Number of entries emitted since restart
bool finished_; // Has Finish() been called?
bool is_uniform_; // Was the last Finish()'d block uniform?
std::string last_key_;
DataBlockHashIndexBuilder data_block_hash_index_builder_;
const double uniform_cv_threshold_;
Expand Down
1 change: 1 addition & 0 deletions table/block_based/block_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ TEST_P(IndexBlockTest, IndexValueEncodingTest) {
bool expect_uniform = reader.NumRestarts() >= 3 &&
keyDistribution() == KeyDistribution::kUniform;
EXPECT_EQ(reader.IsUniform(), expect_uniform);
EXPECT_EQ(builder.IsUniform(), expect_uniform);
}

// read block contents randomly
Expand Down
4 changes: 4 additions & 0 deletions table/block_based/index_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,12 @@ Status PartitionedIndexBuilder::Finish(
if (UNLIKELY(entries_.empty())) {
if (must_use_separator_with_seq_.LoadRelaxed()) {
index_blocks->index_block_contents = index_block_builder_.Finish();
num_uniform_index_blocks_ += index_block_builder_.IsUniform() ? 1 : 0;
} else {
index_blocks->index_block_contents =
index_block_builder_without_seq_.Finish();
num_uniform_index_blocks_ +=
index_block_builder_without_seq_.IsUniform() ? 1 : 0;
}
top_level_index_size_ = index_blocks->index_block_contents.size();
index_size_ += top_level_index_size_;
Expand All @@ -361,6 +364,7 @@ Status PartitionedIndexBuilder::Finish(
entry.value->must_use_separator_with_seq_.StoreRelaxed(
must_use_separator_with_seq_.LoadRelaxed());
auto s = entry.value->Finish(index_blocks);
num_uniform_index_blocks_ += entry.value->NumUniformIndexBlocks();
index_size_ += index_blocks->index_block_contents.size();
finishing_indexes_ = true;
return s.ok() ? Status::Incomplete() : s;
Expand Down
20 changes: 20 additions & 0 deletions table/block_based/index_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ class IndexBuilder {
// Get the size for index block. Must be called after ::Finish.
virtual size_t IndexSize() const = 0;

// Get the number of uniform index blocks. Must be called after ::Finish.
virtual uint64_t NumUniformIndexBlocks() const { return 0; }

// Returns an estimate of the current index size based on the builder's state.
// Implementations should cache the estimate and update it via
// UpdateIndexSizeEstimate() to avoid recalculating on every key add,
Expand Down Expand Up @@ -434,16 +437,22 @@ class ShortenedIndexBuilder : public IndexBuilder {
const BlockHandle& /*last_partition_block_handle*/) override {
if (must_use_separator_with_seq_.LoadRelaxed()) {
index_blocks->index_block_contents = index_block_builder_.Finish();
is_uniform_ = index_block_builder_.IsUniform();
} else {
index_blocks->index_block_contents =
index_block_builder_without_seq_.Finish();
is_uniform_ = index_block_builder_without_seq_.IsUniform();
}
index_size_ = index_blocks->index_block_contents.size();
return Status::OK();
}

size_t IndexSize() const override { return index_size_; }

uint64_t NumUniformIndexBlocks() const override {
return is_uniform_ ? 1 : 0;
}

uint64_t CurrentIndexSizeEstimate() const override {
return estimated_index_size_.LoadRelaxed();
}
Expand Down Expand Up @@ -480,6 +489,7 @@ class ShortenedIndexBuilder : public IndexBuilder {
const bool include_first_key_;
BlockBasedTableOptions::IndexShorteningMode shortening_mode_;
BlockHandle last_encoded_handle_ = BlockHandle::NullBlockHandle();
bool is_uniform_ = false;
std::string current_block_first_internal_key_;
uint64_t num_index_entries_ = 0;
// Cache for index size estimate to avoid recalculating in hot path
Expand Down Expand Up @@ -607,6 +617,10 @@ class HashIndexBuilder : public IndexBuilder {
prefix_meta_block_.size();
}

uint64_t NumUniformIndexBlocks() const override {
return primary_index_builder_.NumUniformIndexBlocks();
}

uint64_t CurrentIndexSizeEstimate() const override { return 0; }

bool separator_is_key_plus_seq() override {
Expand Down Expand Up @@ -685,6 +699,10 @@ class PartitionedIndexBuilder : public IndexBuilder {
size_t TopLevelIndexSize(uint64_t) const { return top_level_index_size_; }
size_t NumPartitions() const;

uint64_t NumUniformIndexBlocks() const override {
return num_uniform_index_blocks_;
}

// Returns a cached estimate of the current index size. This
// estimate is updated when data blocks are added.
uint64_t CurrentIndexSizeEstimate() const override {
Expand Down Expand Up @@ -724,6 +742,8 @@ class PartitionedIndexBuilder : public IndexBuilder {
size_t top_level_index_size_ = 0;
// Set after ::Finish is called
size_t partition_cnt_ = 0;
// Accumulated across all Finish() calls
uint64_t num_uniform_index_blocks_ = 0;

void MakeNewSubIndexBuilder();
void UpdateIndexSizeEstimate() override;
Expand Down
3 changes: 3 additions & 0 deletions table/meta_blocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void PropertyBlockBuilder::AddTableProperty(const TableProperties& props) {
Add(TablePropertiesNames::kMergeOperands, props.num_merge_operands);
Add(TablePropertiesNames::kNumRangeDeletions, props.num_range_deletions);
Add(TablePropertiesNames::kNumDataBlocks, props.num_data_blocks);
Add(TablePropertiesNames::kNumUniformBlocks, props.num_uniform_blocks);
Add(TablePropertiesNames::kFilterSize, props.filter_size);
Add(TablePropertiesNames::kFormatVersion, props.format_version);
Add(TablePropertiesNames::kFixedKeyLen, props.fixed_key_len);
Expand Down Expand Up @@ -291,6 +292,8 @@ Status ParsePropertiesBlock(
&new_table_properties->raw_value_size},
{TablePropertiesNames::kNumDataBlocks,
&new_table_properties->num_data_blocks},
{TablePropertiesNames::kNumUniformBlocks,
&new_table_properties->num_uniform_blocks},
{TablePropertiesNames::kNumEntries, &new_table_properties->num_entries},
{TablePropertiesNames::kNumFilterEntries,
&new_table_properties->num_filter_entries},
Expand Down
10 changes: 10 additions & 0 deletions table/table_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
// Basic Info
AppendProperty(result, "# data blocks", num_data_blocks, prop_delim,
kv_delim);
AppendProperty(result, "# uniform blocks", num_uniform_blocks, prop_delim,
kv_delim);
AppendProperty(result, "# entries", num_entries, prop_delim, kv_delim);
AppendProperty(result, "# deletions", num_deletions, prop_delim, kv_delim);
AppendProperty(result, "# merge operands", num_merge_operands, prop_delim,
Expand Down Expand Up @@ -192,6 +194,7 @@
raw_key_size += tp.raw_key_size;
raw_value_size += tp.raw_value_size;
num_data_blocks += tp.num_data_blocks;
num_uniform_blocks += tp.num_uniform_blocks;
num_entries += tp.num_entries;
num_filter_entries += tp.num_filter_entries;
num_deletions += tp.num_deletions;
Expand All @@ -215,6 +218,7 @@
rv["raw_key_size"] = raw_key_size;
rv["raw_value_size"] = raw_value_size;
rv["num_data_blocks"] = num_data_blocks;
rv["num_uniform_blocks"] = num_uniform_blocks;
rv["num_entries"] = num_entries;
rv["num_filter_entries"] = num_filter_entries;
rv["num_deletions"] = num_deletions;
Expand Down Expand Up @@ -280,6 +284,8 @@
"rocksdb.raw.value.size";
const std::string TablePropertiesNames::kNumDataBlocks =
"rocksdb.num.data.blocks";
const std::string TablePropertiesNames::kNumUniformBlocks =

Check warning on line 287 in table/table_properties.cc

View workflow job for this annotation

GitHub Actions / clang-tidy

initialization of 'kNumUniformBlocks' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
"rocksdb.num.uniform.blocks";
const std::string TablePropertiesNames::kNumEntries = "rocksdb.num.entries";
const std::string TablePropertiesNames::kNumFilterEntries =
"rocksdb.num.filter_entries";
Expand Down Expand Up @@ -381,6 +387,10 @@
{offsetof(struct TableProperties, num_data_blocks),
OptionType::kUInt64T, OptionVerificationType::kNormal,
OptionTypeFlags::kNone}},
{"num_uniform_blocks",
{offsetof(struct TableProperties, num_uniform_blocks),
OptionType::kUInt64T, OptionVerificationType::kNormal,
OptionTypeFlags::kNone}},
{"num_entries",
{offsetof(struct TableProperties, num_entries), OptionType::kUInt64T,
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
Expand Down
Loading