From 4d76eaefc6db5ddd0df3599fe649e9ed66c510d5 Mon Sep 17 00:00:00 2001 From: karelrooted Date: Mon, 24 Aug 2020 18:55:16 +0800 Subject: [PATCH 1/2] ADD: dynamic resize block and sst --- src/server.cc | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/server.h | 1 + src/storage.cc | 6 ++++++ src/storage.h | 1 + 4 files changed, 59 insertions(+) diff --git a/src/server.cc b/src/server.cc index 0e0e585ac2e..db318c14c05 100644 --- a/src/server.cc +++ b/src/server.cc @@ -16,6 +16,7 @@ #include "redis_request.h" #include "redis_connection.h" #include "compaction_checker.h" +#include "config.h" std::atomicServer::unix_time_ = {0}; @@ -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)); @@ -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()) { diff --git a/src/server.h b/src/server.h index ed533c25509..9a14cd8ea67 100644 --- a/src/server.h +++ b/src/server.h @@ -116,6 +116,7 @@ class Server { void cron(); void delConnContext(ConnContext *c); void updateCachedTime(); + Status dynamicResizeBlockAndSST(); bool stop_ = false; bool is_loading_ = false; diff --git a/src/storage.cc b/src/storage.cc index ea349ceb5fa..e5f4327e340 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -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()); diff --git a/src/storage.h b/src/storage.h index 6cf5b20601e..67fba48753b 100644 --- a/src/storage.h +++ b/src/storage.h @@ -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(); From b2476933df75514443f877ee60a6277434dcfff2 Mon Sep 17 00:00:00 2001 From: karelrooted Date: Thu, 3 Sep 2020 15:50:08 +0800 Subject: [PATCH 2/2] MOD: rocksdb.target_file_size_base default to 128 --- kvrocks.conf | 4 ++-- src/config.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kvrocks.conf b/kvrocks.conf index f4f39c3aa40..2062d091e53 100644 --- a/kvrocks.conf +++ b/kvrocks.conf @@ -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 diff --git a/src/config.cc b/src/config.cc index 1129a87858b..0621e29d615 100644 --- a/src/config.cc +++ b/src/config.cc @@ -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)},