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
5 changes: 5 additions & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
"db/blob/blob_log_format.cc",
"db/blob/blob_log_sequential_reader.cc",
"db/blob/blob_log_writer.cc",
"db/blob/blob_reference_census.cc",
"db/blob/blob_reference_metadata.cc",
"db/blob/blob_relocation_file.cc",
"db/blob/blob_relocation_gc.cc",
"db/blob/blob_relocation_manifest.cc",
"db/blob/blob_source.cc",
"db/blob/blob_write_batch_transformer.cc",
"db/blob/prefetch_buffer_collection.cc",
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,11 @@ set(SOURCES
db/blob/blob_log_format.cc
db/blob/blob_log_sequential_reader.cc
db/blob/blob_log_writer.cc
db/blob/blob_relocation_file.cc
db/blob/blob_relocation_gc.cc
db/blob/blob_relocation_manifest.cc
db/blob/blob_reference_census.cc
db/blob/blob_reference_metadata.cc
db/blob/blob_source.cc
db/blob/blob_write_batch_transformer.cc
db/blob/prefetch_buffer_collection.cc
Expand Down
40 changes: 40 additions & 0 deletions c_api_gen/c_generated_option_structs_auto.cc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,36 @@ unsigned char rocksdb_options_get_enable_blob_files(rocksdb_options_t* opt) {
return opt->rep.enable_blob_files;
}

void rocksdb_options_set_enable_blob_indirection(rocksdb_options_t* opt,
unsigned char v) {
opt->rep.enable_blob_indirection = v;
}

unsigned char rocksdb_options_get_enable_blob_indirection(
rocksdb_options_t* opt) {
return opt->rep.enable_blob_indirection;
}

void rocksdb_options_set_pin_blob_relocation_location_blocks(
rocksdb_options_t* opt, unsigned char v) {
opt->rep.pin_blob_relocation_location_blocks = v;
}

unsigned char rocksdb_options_get_pin_blob_relocation_location_blocks(
rocksdb_options_t* opt) {
return opt->rep.pin_blob_relocation_location_blocks;
}

void rocksdb_options_set_blob_indirection_max_references_per_sst(
rocksdb_options_t* opt, uint64_t v) {
opt->rep.blob_indirection_max_references_per_sst = v;
}

uint64_t rocksdb_options_get_blob_indirection_max_references_per_sst(
rocksdb_options_t* opt) {
return opt->rep.blob_indirection_max_references_per_sst;
}

void rocksdb_options_set_min_blob_size(rocksdb_options_t* opt, uint64_t v) {
opt->rep.min_blob_size = v;
}
Expand Down Expand Up @@ -1410,6 +1440,16 @@ double rocksdb_options_get_blob_gc_force_threshold(rocksdb_options_t* opt) {
return opt->rep.blob_garbage_collection_force_threshold;
}

void rocksdb_options_set_indirect_blob_gc_read_coalesce_bytes(
rocksdb_options_t* opt, uint64_t v) {
opt->rep.indirect_blob_gc_read_coalesce_bytes = v;
}

uint64_t rocksdb_options_get_indirect_blob_gc_read_coalesce_bytes(
rocksdb_options_t* opt) {
return opt->rep.indirect_blob_gc_read_coalesce_bytes;
}

void rocksdb_options_set_blob_compaction_readahead_size(rocksdb_options_t* opt,
uint64_t v) {
opt->rep.blob_compaction_readahead_size = v;
Expand Down
29 changes: 29 additions & 0 deletions c_api_gen/c_generated_option_structs_auto.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,27 @@ rocksdb_options_set_preserve_internal_time_seconds(rocksdb_options_t* opt,
extern ROCKSDB_LIBRARY_API uint64_t
rocksdb_options_get_preserve_internal_time_seconds(rocksdb_options_t* opt);

extern ROCKSDB_LIBRARY_API void rocksdb_options_set_enable_blob_indirection(
rocksdb_options_t* opt, unsigned char v);

extern ROCKSDB_LIBRARY_API unsigned char
rocksdb_options_get_enable_blob_indirection(rocksdb_options_t* opt);

extern ROCKSDB_LIBRARY_API void
rocksdb_options_set_pin_blob_relocation_location_blocks(rocksdb_options_t* opt,
unsigned char v);

extern ROCKSDB_LIBRARY_API unsigned char
rocksdb_options_get_pin_blob_relocation_location_blocks(rocksdb_options_t* opt);

extern ROCKSDB_LIBRARY_API void
rocksdb_options_set_blob_indirection_max_references_per_sst(
rocksdb_options_t* opt, uint64_t v);

extern ROCKSDB_LIBRARY_API uint64_t
rocksdb_options_get_blob_indirection_max_references_per_sst(
rocksdb_options_t* opt);

extern ROCKSDB_LIBRARY_API void
rocksdb_options_set_blob_file_writable_file_max_buffer_size(
rocksdb_options_t* opt, uint64_t v);
Expand All @@ -439,6 +460,14 @@ extern ROCKSDB_LIBRARY_API uint64_t
rocksdb_options_get_blob_file_writable_file_max_buffer_size(
rocksdb_options_t* opt);

extern ROCKSDB_LIBRARY_API void
rocksdb_options_set_indirect_blob_gc_read_coalesce_bytes(rocksdb_options_t* opt,
uint64_t v);

extern ROCKSDB_LIBRARY_API uint64_t
rocksdb_options_get_indirect_blob_gc_read_coalesce_bytes(
rocksdb_options_t* opt);

extern ROCKSDB_LIBRARY_API void rocksdb_options_set_enable_blob_direct_write(
rocksdb_options_t* opt, unsigned char v);

Expand Down
22 changes: 22 additions & 0 deletions c_api_gen/c_generated_roundtrip_tests.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ static void rocksdb_roundtrip_test_rocksdb_options(void) {
rocksdb_options_set_blob_file_writable_file_max_buffer_size(obj, 0);
CheckCondition(
rocksdb_options_get_blob_file_writable_file_max_buffer_size(obj) == 0);
rocksdb_options_set_blob_indirection_max_references_per_sst(obj, 1);
CheckCondition(
rocksdb_options_get_blob_indirection_max_references_per_sst(obj) == 1);
rocksdb_options_set_blob_indirection_max_references_per_sst(obj, 0);
CheckCondition(
rocksdb_options_get_blob_indirection_max_references_per_sst(obj) == 0);
rocksdb_options_set_block_protection_bytes_per_key(obj, 1);
CheckCondition(rocksdb_options_get_block_protection_bytes_per_key(obj) == 1);
rocksdb_options_set_block_protection_bytes_per_key(obj, 0);
Expand Down Expand Up @@ -755,6 +761,10 @@ static void rocksdb_roundtrip_test_rocksdb_options(void) {
CheckCondition(rocksdb_options_get_enable_blob_direct_write(obj) == 1);
rocksdb_options_set_enable_blob_direct_write(obj, 0);
CheckCondition(rocksdb_options_get_enable_blob_direct_write(obj) == 0);
rocksdb_options_set_enable_blob_indirection(obj, 1);
CheckCondition(rocksdb_options_get_enable_blob_indirection(obj) == 1);
rocksdb_options_set_enable_blob_indirection(obj, 0);
CheckCondition(rocksdb_options_get_enable_blob_indirection(obj) == 0);
rocksdb_options_set_enable_thread_tracking(obj, 1);
CheckCondition(rocksdb_options_get_enable_thread_tracking(obj) == 1);
rocksdb_options_set_enable_thread_tracking(obj, 0);
Expand Down Expand Up @@ -797,6 +807,12 @@ static void rocksdb_roundtrip_test_rocksdb_options(void) {
CheckCondition(rocksdb_options_get_force_consistency_checks(obj) == 1);
rocksdb_options_set_force_consistency_checks(obj, 0);
CheckCondition(rocksdb_options_get_force_consistency_checks(obj) == 0);
rocksdb_options_set_indirect_blob_gc_read_coalesce_bytes(obj, 1);
CheckCondition(
rocksdb_options_get_indirect_blob_gc_read_coalesce_bytes(obj) == 1);
rocksdb_options_set_indirect_blob_gc_read_coalesce_bytes(obj, 0);
CheckCondition(
rocksdb_options_get_indirect_blob_gc_read_coalesce_bytes(obj) == 0);
rocksdb_options_set_last_level_temperature(obj, 1);
CheckCondition(rocksdb_options_get_last_level_temperature(obj) == 1);
rocksdb_options_set_last_level_temperature(obj, 0);
Expand Down Expand Up @@ -875,6 +891,12 @@ static void rocksdb_roundtrip_test_rocksdb_options(void) {
CheckCondition(rocksdb_options_get_persist_user_defined_timestamps(obj) == 1);
rocksdb_options_set_persist_user_defined_timestamps(obj, 0);
CheckCondition(rocksdb_options_get_persist_user_defined_timestamps(obj) == 0);
rocksdb_options_set_pin_blob_relocation_location_blocks(obj, 1);
CheckCondition(rocksdb_options_get_pin_blob_relocation_location_blocks(obj) ==
1);
rocksdb_options_set_pin_blob_relocation_location_blocks(obj, 0);
CheckCondition(rocksdb_options_get_pin_blob_relocation_location_blocks(obj) ==
0);
rocksdb_options_set_preclude_last_level_data_seconds(obj, 1);
CheckCondition(rocksdb_options_get_preclude_last_level_data_seconds(obj) ==
1);
Expand Down
6 changes: 6 additions & 0 deletions cache/cache_entry_roles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ std::array<std::string, kNumCacheEntryRoles> kCacheEntryRoleToCamelString{{
"BlobValue",
"BlobCache",
"ReadaheadBuffer",
"BlobReferenceMetadataBuildingBuffer",
"BlobReferenceCensus",
"IndirectBlobGcWorkingMemory",
"Misc",
}};

Expand All @@ -44,6 +47,9 @@ std::array<std::string, kNumCacheEntryRoles> kCacheEntryRoleToHyphenString{{
"blob-value",
"blob-cache",
"readahead-buffer",
"blob-reference-metadata-building-buffer",
"blob-reference-census",
"indirect-blob-gc-working-memory",
"misc",
}};

Expand Down
19 changes: 19 additions & 0 deletions cache/cache_reservation_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cassert>
#include <cstddef>
#include <cstring>
#include <limits>
#include <memory>

#include "rocksdb/cache.h"
Expand Down Expand Up @@ -39,6 +40,18 @@ CacheReservationManagerImpl<
s.PermitUncheckedError();
}

template <CacheEntryRole R>
Status CacheReservationManagerImpl<R>::CacheReservationHandle::Resize(
std::size_t memory_used) {
const std::size_t others =
cache_res_mgr_->GetTotalMemoryUsed() - incremental_memory_used_;
if (memory_used > std::numeric_limits<std::size_t>::max() - others) {
return Status::MemoryLimit("Cache reservation size overflow");
}
incremental_memory_used_ = memory_used;
return cache_res_mgr_->UpdateCacheReservation(others + memory_used);
}

template <CacheEntryRole R>
CacheReservationManagerImpl<R>::CacheReservationManagerImpl(
std::shared_ptr<Cache> cache, bool delayed_decrease)
Expand Down Expand Up @@ -201,6 +214,12 @@ template class CacheReservationManagerImpl<CacheEntryRole::kFilterConstruction>;
template class CacheReservationManagerImpl<CacheEntryRole::kMisc>;
template class CacheReservationManagerImpl<CacheEntryRole::kWriteBuffer>;
template class CacheReservationManagerImpl<CacheEntryRole::kFileMetadata>;
template class CacheReservationManagerImpl<
CacheEntryRole::kBlobReferenceMetadataBuildingBuffer>;
template class CacheReservationManagerImpl<
CacheEntryRole::kBlobReferenceCensus>;
template class CacheReservationManagerImpl<
CacheEntryRole::kIndirectBlobGcWorkingMemory>;
template class CacheReservationManagerImpl<CacheEntryRole::kBlobCache>;
template class CacheReservationManagerImpl<CacheEntryRole::kReadaheadBuffer>;

Expand Down
16 changes: 16 additions & 0 deletions cache/cache_reservation_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ class CacheReservationManager {
class CacheReservationHandle {
public:
virtual ~CacheReservationHandle() = default;

// Sets the memory that this handle owns to `memory_used` bytes. The other
// handles of the same manager keep the memory they own. The manager's new
// total is its old total minus this handle's old size plus `memory_used`.
// When the cache cannot reserve the new total, Resize() returns an error,
// but the handle still owns `memory_used`, the same as after a failed
// MakeCacheReservation(). A later Resize() or the handle's destructor
// releases it. After an error, the caller must not allocate the memory.
// When the new total would overflow size_t, Resize() returns MemoryLimit
// and the handle keeps its old size.
virtual Status Resize(std::size_t memory_used) = 0;
};
virtual ~CacheReservationManager() = default;
virtual Status UpdateCacheReservation(std::size_t new_memory_used) = 0;
Expand Down Expand Up @@ -76,6 +87,7 @@ class CacheReservationManagerImpl
std::size_t incremental_memory_used,
std::shared_ptr<CacheReservationManagerImpl> cache_res_mgr);
~CacheReservationHandle() override;
Status Resize(std::size_t memory_used) override;

private:
std::size_t incremental_memory_used_;
Expand Down Expand Up @@ -255,6 +267,10 @@ class ConcurrentCacheReservationManager
std::lock_guard<std::mutex> lock(cache_res_mgr_->cache_res_mgr_mu_);
cache_res_handle_.reset();
}
Status Resize(std::size_t memory_used) override {
std::lock_guard<std::mutex> lock(cache_res_mgr_->cache_res_mgr_mu_);
return cache_res_handle_->Resize(memory_used);
}

private:
std::shared_ptr<ConcurrentCacheReservationManager> cache_res_mgr_;
Expand Down
66 changes: 66 additions & 0 deletions cache/cache_reservation_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include <vector>

#include "cache/cache_entry_roles.h"
#include "db/blob/blob_reference_metadata.h"
#include "rocksdb/cache.h"
#include "rocksdb/slice.h"
#include "rocksdb/table.h"
#include "test_util/mock_time_env.h"
#include "test_util/sync_point.h"
#include "test_util/testharness.h"
Expand All @@ -43,6 +45,70 @@ class CacheReservationManagerTest : public ::testing::Test {
}
};

TEST_F(CacheReservationManagerTest, ResizeKeepsOtherOwnersCharged) {
std::shared_ptr<CacheReservationManager> manager =
std::make_shared<ConcurrentCacheReservationManager>(test_cache_rev_mng);
std::unique_ptr<CacheReservationManager::CacheReservationHandle> first;
std::unique_ptr<CacheReservationManager::CacheReservationHandle> second;
ASSERT_OK(manager->MakeCacheReservation(100, &first));
ASSERT_OK(manager->MakeCacheReservation(200, &second));
ASSERT_OK(first->Resize(400));
ASSERT_EQ(manager->GetTotalMemoryUsed(), 600U);
ASSERT_OK(second->Resize(0));
ASSERT_EQ(manager->GetTotalMemoryUsed(), 400U);
first.reset();
ASSERT_EQ(manager->GetTotalMemoryUsed(), 0U);
second.reset();
}

TEST_F(CacheReservationManagerTest, BlobSummariesShareOneReservationQuantum) {
// BlockBasedTableFactory makes one manager of this form and gives it to
// every blob reference collector. A BlobReferenceSstSummary takes over the
// handle of its collector, so it uses the same manager. This test checks
// that 1000 handles of 100 bytes each share one dummy cache entry
// (kSizeDummyEntry) instead of reserving one entry each, and that the
// reservation goes back to 0 when all handles are released.
std::shared_ptr<CacheReservationManager> first =
std::make_shared<ConcurrentCacheReservationManager>(
std::make_shared<CacheReservationManagerImpl<
CacheEntryRole::kBlobReferenceMetadataBuildingBuffer>>(cache));
std::shared_ptr<CacheReservationManager> second = first;
std::vector<std::unique_ptr<CacheReservationManager::CacheReservationHandle>>
owners;
for (size_t i = 0; i < 1000; ++i) {
std::unique_ptr<CacheReservationManager::CacheReservationHandle> owner;
ASSERT_OK(first->MakeCacheReservation(100, &owner));
owners.push_back(std::move(owner));
}
ASSERT_EQ(first->GetTotalMemoryUsed(), 100000U);
ASSERT_EQ(first->GetTotalReservedCacheSize(), kSizeDummyEntry);
owners.front().reset();
ASSERT_EQ(second->GetTotalMemoryUsed(), 99900U);
owners.clear();
ASSERT_EQ(first->GetTotalMemoryUsed(), 0U);
ASSERT_EQ(first->GetTotalReservedCacheSize(), 0U);
first.reset();
second.reset();
ASSERT_EQ(cache->GetPinnedUsage(), 0U);
}

TEST_F(CacheReservationManagerTest, FailedResizeRemainsLocallyOwned) {
cache->SetCapacity(2 * kSizeDummyEntry);
cache->SetStrictCapacityLimit(true);
std::shared_ptr<CacheReservationManager> manager =
std::make_shared<ConcurrentCacheReservationManager>(test_cache_rev_mng);
std::unique_ptr<CacheReservationManager::CacheReservationHandle> first;
std::unique_ptr<CacheReservationManager::CacheReservationHandle> second;
ASSERT_OK(manager->MakeCacheReservation(10, &first));
ASSERT_OK(manager->MakeCacheReservation(20, &second));
ASSERT_TRUE(second->Resize(3 * kSizeDummyEntry).IsMemoryLimit());
ASSERT_EQ(manager->GetTotalMemoryUsed(), 10 + 3 * kSizeDummyEntry);
second.reset();
ASSERT_EQ(manager->GetTotalMemoryUsed(), 10U);
first.reset();
ASSERT_EQ(manager->GetTotalMemoryUsed(), 0U);
}

TEST_F(CacheReservationManagerTest, GenerateCacheKey) {
std::size_t new_mem_used = 1 * kSizeDummyEntry;
Status s = test_cache_rev_mng->UpdateCacheReservation(new_mem_used);
Expand Down
2 changes: 2 additions & 0 deletions db/blob/blob_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ROCKSDB_NAMESPACE {

inline constexpr size_t kBlobGcMaxSelectedFiles = 8;

// WARNING: This value is == kCurrentFileBlobIndexFileNumber.
// Use this name only where file number zero means "no valid blob file" or
// "current file" is not understood/supported.
Expand Down
Loading
Loading