Skip to content

Commit

Permalink
fixing inheritance bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
littlepig2013 committed Jan 17, 2024
1 parent e5f7d2d commit c474adc
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 42 deletions.
40 changes: 25 additions & 15 deletions db/bpk_alloc_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void BitsPerKeyAllocHelper::PrepareBpkAllocation(const Compaction* compaction) {
while (!level_states_pq_.empty() &&
std::log(level_states_pq_.top().num_entries) +
common_constant_in_bpk_optimization_ >
-std::exp(-log_2_squared)) {
-log_2_squared) {
tmp_num_entries_in_filter_by_file = level_states_pq_.top().num_entries;
temp_sum_in_bpk_optimization_ -=
std::log(tmp_num_entries_in_filter_by_file) *
Expand All @@ -170,8 +170,10 @@ void BitsPerKeyAllocHelper::PrepareBpkAllocation(const Compaction* compaction) {
monkey_num_entries_;
level_states_pq_.pop();
}
if (!level_states_pq_.empty())

if (!level_states_pq_.empty()) {
monkey_bpk_num_entries_threshold_ = level_states_pq_.top().num_entries;
}
} else if (bpk_alloc_type_ ==
BitsPerKeyAllocationType::kWorkloadAwareBpkAlloc) {
if (compaction == nullptr &&
Expand Down Expand Up @@ -298,7 +300,7 @@ void BitsPerKeyAllocHelper::PrepareBpkAllocation(const Compaction* compaction) {
!file_workload_state_pq_.empty() &&
std::log(file_workload_state_pq_.top().weight * total_empty_queries_) *
+common_constant_in_bpk_optimization_ >
-std::exp(-log_2_squared)) {
-log_2_squared) {
weight = file_workload_state_pq_.top().weight;
temp_sum_in_bpk_optimization_ -=
std::log(weight * total_empty_queries_) *
Expand All @@ -318,9 +320,11 @@ void BitsPerKeyAllocHelper::PrepareBpkAllocation(const Compaction* compaction) {
workload_aware_num_entries_with_empty_queries_;
file_workload_state_pq_.pop();
}
if (!file_workload_state_pq_.empty())

if (!file_workload_state_pq_.empty()) {
workload_aware_bpk_weight_threshold_ =
file_workload_state_pq_.top().weight;
}
}

bpk_optimization_prepared_flag_ = true;
Expand All @@ -333,7 +337,9 @@ bool BitsPerKeyAllocHelper::IfNeedAllocateBitsPerKey(
return false;
assert(bits_per_key);

if (bpk_alloc_type_ == BitsPerKeyAllocationType::kMonkeyBpkAlloc) {
if (bpk_alloc_type_ == BitsPerKeyAllocationType::kMonkeyBpkAlloc ||
(bpk_alloc_type_ == BitsPerKeyAllocationType::kWorkloadAwareBpkAlloc &&
vstorage_->GetAccumulatedNumPointReads() == 0)) {
if (!bpk_optimization_prepared_flag_) {
flush_flag_ = true;
temp_sum_in_bpk_optimization_ +=
Expand All @@ -346,7 +352,7 @@ bool BitsPerKeyAllocHelper::IfNeedAllocateBitsPerKey(
if (num_entries_in_output_level > monkey_bpk_num_entries_threshold_ ||
std::log(num_entries_in_output_level) +
common_constant_in_bpk_optimization_ >
-std::exp(-log_2_squared)) {
-log_2_squared) {
*bits_per_key = 0;
} else {
*bits_per_key = -(std::log(num_entries_in_output_level) +
Expand All @@ -355,35 +361,39 @@ bool BitsPerKeyAllocHelper::IfNeedAllocateBitsPerKey(
}
} else if (bpk_alloc_type_ ==
BitsPerKeyAllocationType::kWorkloadAwareBpkAlloc) {
if (total_empty_queries_ == 0) return false;
uint64_t num_entries = meta.num_entries - meta.num_range_deletions;
uint64_t num_point_reads =
meta.stats.num_point_reads.load(std::memory_order_relaxed);
if (num_point_reads == 0) return false;
uint64_t num_existing_point_reads =
meta.stats.num_existing_point_reads.load(std::memory_order_relaxed);
if (num_existing_point_reads >= num_point_reads &&
num_existing_point_reads > 0) {
*bits_per_key = 0;
return true;
}

if (!bpk_optimization_prepared_flag_) {
flush_flag_ = true;
total_empty_queries_ = num_point_reads - num_existing_point_reads;
if (num_point_reads > num_existing_point_reads) {
total_empty_queries_ = num_point_reads - num_existing_point_reads;
}
temp_sum_in_bpk_optimization_ +=
num_entries * std::log(num_entries * 1.0 / total_empty_queries_);
workload_aware_num_entries_ += num_entries;
workload_aware_num_entries_with_empty_queries_ += num_entries;
PrepareBpkAllocation();
}

if (total_empty_queries_ == 0) return false;

if (num_existing_point_reads >= num_point_reads &&
num_existing_point_reads > 0) {
*bits_per_key = 0;
return true;
}

double weight =
num_entries * 1.0 / (num_point_reads - num_existing_point_reads);
if (weight >= workload_aware_bpk_weight_threshold_ ||
if (weight > workload_aware_bpk_weight_threshold_ ||
std::log(weight * total_empty_queries_) +
common_constant_in_bpk_optimization_ >
-std::exp(-log_2_squared)) {
-log_2_squared) {
*bits_per_key = 0;
} else {
*bits_per_key = -(std::log(weight * total_empty_queries_) +
Expand Down
7 changes: 5 additions & 2 deletions db/bpk_alloc_helper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <limits>
#include <queue>

#include "compaction/compaction.h"
Expand Down Expand Up @@ -33,8 +34,10 @@ class BitsPerKeyAllocHelper {
BitsPerKeyAllocationType::kDefaultBpkAlloc;
bool flush_flag_ = false;
bool bpk_optimization_prepared_flag_ = false;
double workload_aware_bpk_weight_threshold_ = 0.0;
uint64_t monkey_bpk_num_entries_threshold_ = 0;
double workload_aware_bpk_weight_threshold_ =
std::numeric_limits<double>::max();
uint64_t monkey_bpk_num_entries_threshold_ =
std::numeric_limits<uint64_t>::max();
uint64_t monkey_num_entries_ = 0;
uint64_t workload_aware_num_entries_ = 0;
uint64_t workload_aware_num_entries_with_empty_queries_ = 0;
Expand Down
9 changes: 9 additions & 0 deletions db/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "file/filename.h"
#include "file/read_write_util.h"
#include "file/writable_file_writer.h"
#include "logging/logging.h"
#include "monitoring/iostats_context_imp.h"
#include "monitoring/thread_status_util.h"
#include "options/options_helper.h"
Expand Down Expand Up @@ -325,7 +326,15 @@ Status BuildTable(
&new_bits_per_key)) {
builder->ResetFilterBitsPerKey(new_bits_per_key);
meta->bpk = new_bits_per_key;
ROCKS_LOG_INFO(ioptions.info_log,
"[%s] Flushes generates new file %" PRIu64
" with reset bits-per-key %.4f",
tboptions.column_family_name.c_str(),
meta->fd.GetNumber(), new_bits_per_key);
}
version->storage_info()->SetBpkCommonConstant(
bpk_alloc_helper.bpk_alloc_type_,
bpk_alloc_helper.common_constant_in_bpk_optimization_);
}

s = builder->Finish();
Expand Down
3 changes: 3 additions & 0 deletions db/compaction/compaction_outputs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Status CompactionOutputs::Finish(
compaction_->column_family_data()->GetName().c_str(),
meta->fd.GetNumber(), new_bits_per_key);
}
compaction_->input_vstorage()->SetBpkCommonConstant(
bpk_alloc_helper_->bpk_alloc_type_,
bpk_alloc_helper_->common_constant_in_bpk_optimization_);

s = builder_->Finish();

Expand Down
3 changes: 2 additions & 1 deletion db/flush_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,8 @@ Status FlushJob::WriteLevel0Table() {
edit_->AddNumExistingPointReads(
meta_.stats.num_existing_point_reads.load(std::memory_order_relaxed));
edit_->SetBlobFileAdditions(std::move(blob_file_additions));
cfd_->current()->storage_info()->UpdateNumPointReadsAndExistingPointReads(
cfd_->current()->storage_info()->UpdateNumPointReadsStats(
meta_.stats.num_point_reads.load(std::memory_order_relaxed),
meta_.stats.num_existing_point_reads.load(std::memory_order_relaxed));
}
// Piggyback FlushJobInfo on the first first flushed memtable.
Expand Down
1 change: 1 addition & 0 deletions db/version_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once
#include <algorithm>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <utility>
Expand Down
Loading

0 comments on commit c474adc

Please sign in to comment.