Skip to content

Commit

Permalink
Cache dumper exit early due to deadline or max_dumped_size (facebook#…
Browse files Browse the repository at this point in the history
…12491)

Summary:
In production, we need to control the duration time or max size of cache dumper to get better performance.
Fixes facebook#12494

Pull Request resolved: facebook#12491

Reviewed By: hx235

Differential Revision: D55905826

Pulled By: ajkr

fbshipit-source-id: 9196a5e852c344d6783f7a8234e997c87215bd19
  • Loading branch information
lhsoft authored and facebook-github-bot committed Apr 12, 2024
1 parent 70d3fc3 commit b7f1eeb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions include/rocksdb/utilities/cache_dump_load.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class CacheDumpReader {
// dump or load process related control variables can be added here.
struct CacheDumpOptions {
SystemClock* clock;
// Deadline for dumper or loader in microseconds
std::chrono::microseconds deadline = std::chrono::microseconds::zero();
// Max size bytes for dumper or loader
uint64_t max_size_bytes = 0;
};

// NOTE that: this class is EXPERIMENTAL! May be changed in the future!
Expand Down
1 change: 1 addition & 0 deletions unreleased_history/new_features/cache_dumper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added two options `deadline` and `max_size_bytes` for CacheDumper to exit early
35 changes: 27 additions & 8 deletions utilities/cache_dump_load_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).

#include "cache/cache_key.h"
#include "table/block_based/block_based_table_reader.h"
#include "utilities/cache_dump_load_impl.h"

#include <limits>

#include "cache/cache_entry_roles.h"
#include "cache/cache_key.h"
#include "file/writable_file_writer.h"
#include "port/lang.h"
#include "rocksdb/env.h"
#include "rocksdb/file_system.h"
#include "rocksdb/utilities/ldb_cmd.h"
#include "table/block_based/block_based_table_reader.h"
#include "table/format.h"
#include "util/crc32c.h"
#include "utilities/cache_dump_load_impl.h"

namespace ROCKSDB_NAMESPACE {

Expand Down Expand Up @@ -67,6 +69,8 @@ IOStatus CacheDumperImpl::DumpCacheEntriesToWriter() {
}
clock_ = options_.clock;

deadline_ = options_.deadline;

// Set the sequence number
sequence_num_ = 0;

Expand Down Expand Up @@ -117,6 +121,19 @@ CacheDumperImpl::DumpOneBlockCallBack(std::string& buf) {
return;
}

if (options_.max_size_bytes > 0 &&
dumped_size_bytes_ > options_.max_size_bytes) {
return;
}

uint64_t timestamp = clock_->NowMicros();
if (deadline_.count()) {
std::chrono::microseconds now = std::chrono::microseconds(timestamp);
if (now >= deadline_) {
return;
}
}

CacheEntryRole role = helper->role;
CacheDumpUnitType type = CacheDumpUnitType::kBlockTypeMax;

Expand Down Expand Up @@ -154,7 +171,8 @@ CacheDumperImpl::DumpOneBlockCallBack(std::string& buf) {

if (s.ok()) {
// Write it out
WriteBlock(type, key, buf).PermitUncheckedError();
WriteBlock(type, key, buf, timestamp).PermitUncheckedError();
dumped_size_bytes_ += len;
}
};
}
Expand All @@ -168,8 +186,7 @@ CacheDumperImpl::DumpOneBlockCallBack(std::string& buf) {
// First, we write the metadata first, which is a fixed size string. Then, we
// Append the dump unit string to the writer.
IOStatus CacheDumperImpl::WriteBlock(CacheDumpUnitType type, const Slice& key,
const Slice& value) {
uint64_t timestamp = clock_->NowMicros();
const Slice& value, uint64_t timestamp) {
uint32_t value_checksum = crc32c::Value(value.data(), value.size());

// First, serialize the block information in a string
Expand Down Expand Up @@ -219,15 +236,17 @@ IOStatus CacheDumperImpl::WriteHeader() {
"block_size, block_data, block_checksum> cache_value\n";
std::string header_value(s.str());
CacheDumpUnitType type = CacheDumpUnitType::kHeader;
return WriteBlock(type, header_key, header_value);
uint64_t timestamp = clock_->NowMicros();
return WriteBlock(type, header_key, header_value, timestamp);
}

// Write the footer after all the blocks are stored to indicate the ending.
IOStatus CacheDumperImpl::WriteFooter() {
std::string footer_key = "footer";
std::string footer_value("cache dump completed");
CacheDumpUnitType type = CacheDumpUnitType::kFooter;
return WriteBlock(type, footer_key, footer_value);
uint64_t timestamp = clock_->NowMicros();
return WriteBlock(type, footer_key, footer_value, timestamp);
}

// This is the main function to restore the cache entries to secondary cache.
Expand Down
9 changes: 7 additions & 2 deletions utilities/cache_dump_load_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ class CacheDumperImpl : public CacheDumper {
CacheDumperImpl(const CacheDumpOptions& dump_options,
const std::shared_ptr<Cache>& cache,
std::unique_ptr<CacheDumpWriter>&& writer)
: options_(dump_options), cache_(cache), writer_(std::move(writer)) {}
: options_(dump_options), cache_(cache), writer_(std::move(writer)) {
dumped_size_bytes_ = 0;
}
~CacheDumperImpl() { writer_.reset(); }
Status SetDumpFilter(std::vector<DB*> db_list) override;
IOStatus DumpCacheEntriesToWriter() override;

private:
IOStatus WriteBlock(CacheDumpUnitType type, const Slice& key,
const Slice& value);
const Slice& value, uint64_t timestamp);
IOStatus WriteHeader();
IOStatus WriteFooter();
bool ShouldFilterOut(const Slice& key);
Expand All @@ -121,6 +123,9 @@ class CacheDumperImpl : public CacheDumper {
// improvement can be applied like BloomFilter or others to speedup the
// filtering.
std::set<std::string> prefix_filter_;
// Deadline for dumper in microseconds.
std::chrono::microseconds deadline_;
uint64_t dumped_size_bytes_;
};

// The default implementation of CacheDumpedLoader
Expand Down

0 comments on commit b7f1eeb

Please sign in to comment.