Skip to content

Commit

Permalink
expose more rocksdb optionos for tuning (OpenAtomFoundation#358)
Browse files Browse the repository at this point in the history
* expose more rocksdb optionos for tuning

* codestyle change

* add some options
  • Loading branch information
fancy-rabbit authored and Axlgrep committed Sep 19, 2018
1 parent 2d57907 commit bb7deb4
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 26 deletions.
10 changes: 10 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ max-background-compactions : 2
max-cache-files : 5000
# max_bytes_for_level_multiplier: default is 10, you can change it to 5
max-bytes-for-level-multiplier : 10
# BlockBasedTable block_size, default 4k
# block-size: 4096
# block LRU cache, default 8M
# block-cache: 8388608
# whether or not index and filter blocks is stored in block cache
# cache-index-and-filter-blocks: no
# when set to yes, bloomfilter of the last level will not be built
# optimize-filters-for-hits: no
# https://github.com/facebook/rocksdb/wiki/Leveled-Compaction#levels-target-size
# level-compaction-dynamic-level-bytes: no
11 changes: 11 additions & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class PikaConf : public slash::BaseConf {
int max_background_compactions() { RWLock l(&rwlock_, false); return max_background_compactions_; }
int max_cache_files() { RWLock l(&rwlock_, false); return max_cache_files_; }
int max_bytes_for_level_multiplier() {RWLock l(&rwlock_, false); return max_bytes_for_level_multiplier_; }
int block_size() {RWLock l(&rwlock_, false); return block_size_; }
int block_cache() {RWLock l(&rwlock_, false); return block_cache_; }
bool cache_index_and_filter_blocks() {RWLock l(&rwlock_, false); return cache_index_and_filter_blocks_; }
bool optimize_filters_for_hits() {RWLock l(&rwlock_, false); return optimize_filters_for_hits_; }
bool level_compaction_dynamic_level_bytes() {RWLock l(&rwlock_, false); return level_compaction_dynamic_level_bytes_; }
int expire_logs_nums() { RWLock l(&rwlock_, false); return expire_logs_nums_; }
int expire_logs_days() { RWLock l(&rwlock_, false); return expire_logs_days_; }
std::string conf_path() { RWLock l(&rwlock_, false); return conf_path_; }
Expand Down Expand Up @@ -232,6 +237,12 @@ class PikaConf : public slash::BaseConf {
int max_background_compactions_;
int max_cache_files_;
int max_bytes_for_level_multiplier_;
int block_size_;
int block_cache_;
bool cache_index_and_filter_blocks_;
bool optimize_filters_for_hits_;
bool level_compaction_dynamic_level_bytes_;

std::string network_interface_;

//char username_[30];
Expand Down
1 change: 1 addition & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class PikaServer {
* Blackwidow options init
*/
void RocksdbOptionInit(rocksdb::Options* option);
void RocksdbTableOptionInit(rocksdb::BlockBasedTableOptions* table_option);

/*
* Binlog
Expand Down
32 changes: 31 additions & 1 deletion src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,26 @@ void ConfigCmd::ConfigGet(std::string &ret) {
ret = "*2\r\n";
EncodeString(&ret, "max-bytes-for-level-multiplier");
EncodeInt32(&ret, g_pika_conf->max_bytes_for_level_multiplier());
} else if (get_item == "block-size") {
ret = "*2\r\n";
EncodeString(&ret, "block-size");
EncodeInt32(&ret, g_pika_conf->block_size());
} else if (get_item == "block-cache") {
ret = "*2\r\n";
EncodeString(&ret, "block-cache");
EncodeInt32(&ret, g_pika_conf->block_cache());
} else if (get_item == "cache-index-and-filter-blocks") {
ret = "*2\r\n";
EncodeString(&ret, "cache-index-and-filter-blocks");
EncodeString(&ret, g_pika_conf->cache_index_and_filter_blocks() ? "yes" : "no");
} else if (get_item == "optimize-filters-for-hits") {
ret = "*2\r\n";
EncodeString(&ret, "optimize-filters-for-hits");
EncodeString(&ret, g_pika_conf->optimize_filters_for_hits() ? "yes" : "no");
} else if (get_item == "level-compaction-dynamic-level-bytes") {
ret = "*2\r\n";
EncodeString(&ret, "level-compaction-dynamic-level-bytes");
EncodeString(&ret, g_pika_conf->level_compaction_dynamic_level_bytes() ? "yes" : "no");
} else if (get_item == "expire-logs-days") {
ret = "*2\r\n";
EncodeString(&ret, "expire-logs-days");
Expand Down Expand Up @@ -1133,7 +1153,7 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeString(&ret, "slave-priority");
EncodeInt32(&ret, g_pika_conf->slave_priority());
} else if (get_item == "*") {
ret = "*92\r\n";
ret = "*102\r\n";
EncodeString(&ret, "port");
EncodeInt32(&ret, g_pika_conf->port());
EncodeString(&ret, "double-master-ip");
Expand Down Expand Up @@ -1190,6 +1210,16 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeInt32(&ret, g_pika_conf->max_cache_files());
EncodeString(&ret, "max-bytes-for-level-multiplier");
EncodeInt32(&ret, g_pika_conf->max_bytes_for_level_multiplier());
EncodeString(&ret, "block-size");
EncodeInt32(&ret, g_pika_conf->block_size());
EncodeString(&ret, "block-cache");
EncodeInt32(&ret, g_pika_conf->block_cache());
EncodeString(&ret, "cache-index-and-filter-blocks");
EncodeString(&ret, g_pika_conf->cache_index_and_filter_blocks() ? "yes" : "no");
EncodeString(&ret, "optimize-filters-for-hits");
EncodeString(&ret, g_pika_conf->optimize_filters_for_hits() ? "yes" : "no");
EncodeString(&ret, "level-compaction-dynamic-level-bytes");
EncodeString(&ret, g_pika_conf->level_compaction_dynamic_level_bytes() ? "yes" : "no");
EncodeString(&ret, "expire-logs-days");
EncodeInt32(&ret, g_pika_conf->expire_logs_days());
EncodeString(&ret, "expire-logs-nums");
Expand Down
29 changes: 29 additions & 0 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ int PikaConf::Load()
max_bytes_for_level_multiplier_ = 5;
}

block_size_ = 4 * 1024;
GetConfInt("block-size", &block_size_);
if (block_size_ <= 0) {
block_size_ = 4 * 1024;
}

block_cache_ = 8 * 1024 * 1024;
GetConfInt("block-cache", &block_cache_);
if (block_cache_ <= 0) {
block_cache_ = 8 * 1024 * 1024;
}

std::string ciafb;
GetConfStr("cache-index-and-filter-blocks", &ciafb);
cache_index_and_filter_blocks_ = (ciafb == "yes") ? true : false;

std::string offh;
GetConfStr("optimize-filters-for-hits", &offh);
optimize_filters_for_hits_ = (offh == "yes") ? true : false;

std::string lcdlb;
GetConfStr("level-compaction-dynamic-level-bytes", &lcdlb);
level_compaction_dynamic_level_bytes_ = (lcdlb == "yes") ? true : false;

// daemonize
std::string dmz;
GetConfStr("daemonize", &dmz);
Expand Down Expand Up @@ -292,6 +316,11 @@ int PikaConf::ConfigRewrite() {
SetConfInt("max-background-compactions", max_background_compactions_);
SetConfInt("max-cache-files", max_cache_files_);
SetConfInt("max-bytes-for-level-multiplier", max_bytes_for_level_multiplier_);
SetConfInt("block-size", block_size_);
SetConfInt("block-cache", block_cache_);
SetConfStr("cache-index-and-filter-blocks", cache_index_and_filter_blocks_ ? "yes" : "no");
SetConfStr("optimize-filters-for-hits", optimize_filters_for_hits_ ? "yes" : "no");
SetConfStr("level-compaction-dynamic-level-bytes", level_compaction_dynamic_level_bytes_ ? "yes" : "no");

return WriteBack();
}
47 changes: 23 additions & 24 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ PikaServer::PikaServer() :

//Create blackwidow handle
rocksdb::Options rocksdb_option;
rocksdb::BlockBasedTableOptions rocksdb_table_option;
RocksdbOptionInit(&rocksdb_option);
RocksdbTableOptionInit(&rocksdb_table_option);

std::string db_path = g_pika_conf->db_path();
LOG(INFO) << "Prepare Blackwidow DB...";
db_ = std::shared_ptr<blackwidow::BlackWidow>(new blackwidow::BlackWidow());
rocksdb::Status s = db_->Open(rocksdb_option, db_path);
rocksdb::Status s = db_->Open(rocksdb_option, rocksdb_table_option, db_path);
assert(db_);
assert(s.ok());
LOG(INFO) << "DB Success";
Expand Down Expand Up @@ -237,6 +239,13 @@ bool PikaServer::ServerInit() {

}

void PikaServer::RocksdbTableOptionInit(rocksdb::BlockBasedTableOptions* table_option) {
table_option->block_size = g_pika_conf->block_size();
table_option->block_cache = rocksdb::NewLRUCache(g_pika_conf->block_cache());
table_option->cache_index_and_filter_blocks = g_pika_conf->cache_index_and_filter_blocks();
}


void PikaServer::RocksdbOptionInit(rocksdb::Options* option) {
option->create_if_missing = true;
option->keep_log_file_num = 10;
Expand All @@ -249,6 +258,8 @@ void PikaServer::RocksdbOptionInit(rocksdb::Options* option) {
option->max_background_compactions = g_pika_conf->max_background_compactions();
option->max_open_files = g_pika_conf->max_cache_files();
option->max_bytes_for_level_multiplier = g_pika_conf->max_bytes_for_level_multiplier();
option->optimize_filters_for_hits = g_pika_conf->optimize_filters_for_hits();
option->level_compaction_dynamic_level_bytes = g_pika_conf->level_compaction_dynamic_level_bytes();

if (g_pika_conf->compression() == "none") {
option->compression = rocksdb::CompressionType::kNoCompression;
Expand Down Expand Up @@ -402,26 +413,10 @@ void PikaServer::DeleteSlave(int fd) {
*/
bool PikaServer::ChangeDb(const std::string& new_path) {

rocksdb::Options option;
option.create_if_missing = true;
option.keep_log_file_num = 10;
option.max_manifest_file_size = 64 * 1024 * 1024;
option.max_log_file_size = 512 * 1024 * 1024;

option.write_buffer_size = g_pika_conf->write_buffer_size();
option.target_file_size_base = g_pika_conf->target_file_size_base();
option.max_background_flushes = g_pika_conf->max_background_flushes();
option.max_background_compactions = g_pika_conf->max_background_compactions();
option.max_open_files = g_pika_conf->max_cache_files();
option.max_bytes_for_level_multiplier = g_pika_conf->max_bytes_for_level_multiplier();

if (g_pika_conf->compression() == "none") {
option.compression = rocksdb::CompressionType::kNoCompression;
} else if (g_pika_conf->compression() == "snappy") {
option.compression = rocksdb::CompressionType::kSnappyCompression;
} else if (g_pika_conf->compression() == "zlib") {
option.compression = rocksdb::CompressionType::kZlibCompression;
}
rocksdb::Options rocksdb_option;
rocksdb::BlockBasedTableOptions rocksdb_table_option;
RocksdbOptionInit(&rocksdb_option);
RocksdbTableOptionInit(&rocksdb_table_option);

std::string db_path = g_pika_conf->db_path();
std::string tmp_path(db_path);
Expand All @@ -445,7 +440,7 @@ bool PikaServer::ChangeDb(const std::string& new_path) {
}

db_.reset(new blackwidow::BlackWidow());
rocksdb::Status s = db_->Open(option, db_path);
rocksdb::Status s = db_->Open(rocksdb_option, rocksdb_table_option, db_path);
assert(db_);
assert(s.ok());
slash::DeleteDirIfExist(tmp_path);
Expand Down Expand Up @@ -1410,11 +1405,13 @@ bool PikaServer::FlushAll() {

//Create blackwidow handle
rocksdb::Options rocksdb_option;
rocksdb::BlockBasedTableOptions rocksdb_table_option;
RocksdbOptionInit(&rocksdb_option);
RocksdbTableOptionInit(&rocksdb_table_option);

LOG(INFO) << "Prepare open new db...";
db_ = std::shared_ptr<blackwidow::BlackWidow>(new blackwidow::BlackWidow());
rocksdb::Status s = db_->Open(rocksdb_option, g_pika_conf->db_path());
rocksdb::Status s = db_->Open(rocksdb_option, rocksdb_table_option, g_pika_conf->db_path());
assert(db_);
assert(s.ok());
LOG(INFO) << "open new db success";
Expand Down Expand Up @@ -1448,11 +1445,13 @@ bool PikaServer::FlushDb(const std::string& db_name) {
slash::RenameFile(sub_dbpath, del_dbpath);

rocksdb::Options rocksdb_option;
rocksdb::BlockBasedTableOptions rocksdb_table_option;
RocksdbOptionInit(&rocksdb_option);
RocksdbTableOptionInit(&rocksdb_table_option);

LOG(INFO) << "Prepare open new " + db_name + " db...";
db_ = std::shared_ptr<blackwidow::BlackWidow>(new blackwidow::BlackWidow());
rocksdb::Status s = db_->Open(rocksdb_option, g_pika_conf->db_path());
rocksdb::Status s = db_->Open(rocksdb_option, rocksdb_table_option, g_pika_conf->db_path());
assert(db_);
assert(s.ok());
LOG(INFO) << "open new " + db_name + " db success";
Expand Down

0 comments on commit bb7deb4

Please sign in to comment.