Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic resize block and sst #120

Merged
merged 2 commits into from
Sep 3, 2020
Merged
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
4 changes: 2 additions & 2 deletions kvrocks.conf
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ rocksdb.write_buffer_size 64
# Target file size for compaction, target file size for Leve N can be caculated
# by target_file_size_base * (target_file_size_multiplier ^ (L-1))
#
# Default: 256MB
rocksdb.target_file_size_base 256
# Default: 128MB
rocksdb.target_file_size_base 128

# The maximum number of write buffers that are built up in memory.
# The default and the minimum number is 2, so that when 1 write buffer
Expand Down
2 changes: 1 addition & 1 deletion src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Config::Config() {
{"rocksdb.max_open_files", false, new IntField(&RocksDB.max_open_files, 4096, -1, INT_MAX)},
{"rocksdb.write_buffer_size", false, new IntField(&RocksDB.write_buffer_size, 64, 0, 4096)},
{"rocksdb.max_write_buffer_number", false, new IntField(&RocksDB.max_write_buffer_number, 4, 0, 256)},
{"rocksdb.target_file_size_base", true, new IntField(&RocksDB.target_file_size_base, 256, 1, 1024)},
{"rocksdb.target_file_size_base", true, new IntField(&RocksDB.target_file_size_base, 128, 1, 1024)},
{"rocksdb.max_background_compactions", false, new IntField(&RocksDB.max_background_compactions, 2, 0, 32)},
{"rocksdb.max_background_flushes", true, new IntField(&RocksDB.max_background_flushes, 2, 0, 32)},
{"rocksdb.max_sub_compactions", false, new IntField(&RocksDB.max_sub_compactions, 1, 0, 16)},
Expand Down
51 changes: 51 additions & 0 deletions src/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "redis_request.h"
#include "redis_connection.h"
#include "compaction_checker.h"
#include "config.h"

std::atomic<int>Server::unix_time_ = {0};

Expand Down Expand Up @@ -480,6 +481,11 @@ void Server::cron() {
Status s = AsyncPurgeOldBackups();
LOG(INFO) << "[server] Schedule to purge old backups, result: " << s.Msg();
}
// check every 30 minutes
if (counter != 0 && counter % 18000 == 0) {
Status s = dynamicResizeBlockAndSST();
LOG(INFO) << "[server] Schedule to dynamic resize block and sst, result: " << s.Msg();
}
cleanupExitedSlaves();
counter++;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand Down Expand Up @@ -872,6 +878,51 @@ Status Server::AsyncScanDBSize(const std::string &ns) {
return task_runner_.Publish(task);
}

Status Server::dynamicResizeBlockAndSST() {
auto total_size = storage_->GetTotalSize(kDefaultNamespace);
uint64_t total_keys = 0, estimate_keys = 0;
for (const auto &cf_handle : *storage_->GetCFHandles()) {
storage_->GetDB()->GetIntProperty(cf_handle, "rocksdb.estimate-num-keys", &estimate_keys);
total_keys += estimate_keys;
}
if (total_size == 0 || total_keys == 0) {
return Status::OK();
}
auto average_kv_size = total_size / total_keys;
int target_file_size_base = 0;
if (average_kv_size > 512 * KiB) {
target_file_size_base = 1024;
} else if (average_kv_size > 256 * KiB) {
target_file_size_base = 512;
} else if (average_kv_size > 32 * KiB) {
target_file_size_base = 256;
} else if (average_kv_size > 1 * KiB) {
target_file_size_base = 128;
} else if (average_kv_size > 128) {
target_file_size_base = 64;
} else {
target_file_size_base = 16;
}
if (target_file_size_base == config_->RocksDB.target_file_size_base) {
return Status::OK();
}
auto s = storage_->SetOption("target_file_size_base", std::to_string(target_file_size_base * MiB));
LOG(INFO) << "[server] Resize rocksdb.target_file_size_base from "
<< config_->RocksDB.target_file_size_base
<< " to " << target_file_size_base
<< ", average_kv_size: " << average_kv_size
<< ", total_size: " << total_size
<< ", total_keys: " << total_keys
<< ", result: " << s.Msg();
if (!s.IsOK()) {
return s;
}
config_->RocksDB.target_file_size_base = target_file_size_base;
s = config_->Rewrite();
LOG(INFO) << "[server] rewrite config, result: " << s.Msg();
return Status::OK();
}

void Server::GetLastestKeyNumStats(const std::string &ns, KeyNumStats *stats) {
auto iter = db_scan_infos_.find(ns);
if (iter != db_scan_infos_.end()) {
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Server {
void cron();
void delConnContext(ConnContext *c);
void updateCachedTime();
Status dynamicResizeBlockAndSST();

bool stop_ = false;
bool is_loading_ = false;
Expand Down
6 changes: 6 additions & 0 deletions src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ Status Storage::SetColumnFamilyOption(const std::string &key, const std::string
return Status::OK();
}

Status Storage::SetOption(const std::string &key, const std::string &value) {
auto s = db_->SetOptions({{key, value}});
if (!s.ok()) return Status(Status::NotOK, s.ToString());
return Status::OK();
}

Status Storage::SetDBOption(const std::string &key, const std::string &value) {
auto s = db_->SetDBOptions({{key, value}});
if (!s.ok()) return Status(Status::NotOK, s.ToString());
Expand Down
1 change: 1 addition & 0 deletions src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Storage {
void CloseDB();
void InitOptions(rocksdb::Options *options);
Status SetColumnFamilyOption(const std::string &key, const std::string &value);
Status SetOption(const std::string &key, const std::string &value);
Status SetDBOption(const std::string &key, const std::string &value);
Status CreateColumnFamilies(const rocksdb::Options &options);
Status CreateBackup();
Expand Down