From 837c8fb276f82ee40641de7c18c146cf3a4601a6 Mon Sep 17 00:00:00 2001 From: wanghenshui Date: Sat, 8 Apr 2023 21:14:47 +0800 Subject: [PATCH] feat: ratelimiter support auto tune --- include/pika_conf.h | 8 ++++++++ src/pika_admin.cc | 18 ++++++++++++++++++ src/pika_conf.cc | 16 ++++++++++++++++ src/pika_server.cc | 9 +++++++-- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/include/pika_conf.h b/include/pika_conf.h index 91d6b50f88..074b64fc48 100644 --- a/include/pika_conf.h +++ b/include/pika_conf.h @@ -95,6 +95,10 @@ class PikaConf : public pstd::BaseConf { int consensus_level() { return consensus_level_.load(); } int replication_num() { return replication_num_.load(); } int64_t rate_limiter_bandwidth() { RWLock l(&rwlock_, false); return rate_limiter_bandwidth_; } + int64_t rate_limiter_refill_period_us() { RWLock l(&rwlock_, false); return rate_limiter_refill_period_us_; } + int64_t rate_limiter_fairness() { RWLock l(&rwlock_, false); return rate_limiter_fairness_; } + bool rate_limiter_auto_tuned() { RWLock l(&rwlock_, false); return rate_limiter_auto_tuned_; } + // Immutable config items, we don't use lock. bool daemonize() { return daemonize_; } @@ -355,6 +359,10 @@ class PikaConf : public pstd::BaseConf { bool optimize_filters_for_hits_; bool level_compaction_dynamic_level_bytes_; int64_t rate_limiter_bandwidth_ = 200 * 1024 * 1024; // 200M + int64_t rate_limiter_refill_period_us_ = 100 * 1000; + int64_t rate_limiter_fairness_ = 10; + bool rate_limiter_auto_tuned_ = true; + std::atomic sync_window_size_; std::atomic max_conn_rbuf_size_; std::atomic consensus_level_; diff --git a/src/pika_admin.cc b/src/pika_admin.cc index 6ded04edc1..8e97e0d602 100644 --- a/src/pika_admin.cc +++ b/src/pika_admin.cc @@ -1745,6 +1745,24 @@ void ConfigCmd::ConfigGet(std::string& ret) { EncodeInt64(&config_body, g_pika_conf->rate_limiter_bandwidth()); } + if (pstd::stringmatch(pattern.data(), "rate-limiter-refill-period-us", 1)) { + elements += 2; + EncodeString(&config_body, "rate-limiter-refill-period-us"); + EncodeInt64(&config_body, g_pika_conf->rate_limiter_refill_period_us()); + } + + if (pstd::stringmatch(pattern.data(), "rate-limiter-fairness", 1)) { + elements += 2; + EncodeString(&config_body, "rate-limiter-fairness"); + EncodeInt64(&config_body, g_pika_conf->rate_limiter_fairness()); + } + + if (pstd::stringmatch(pattern.data(), "rate-limiter-auto-tuned", 1)) { + elements += 2; + EncodeString(&config_body, "rate-limiter-auto-tuned"); + EncodeString(&config_body, g_pika_conf->rate_limiter_auto_tuned() ? "yes" : "no"); + } + std::stringstream resp; resp << "*" << std::to_string(elements) << "\r\n" << config_body; ret = resp.str(); diff --git a/src/pika_conf.cc b/src/pika_conf.cc index fbcf9bd558..fd293f26c6 100644 --- a/src/pika_conf.cc +++ b/src/pika_conf.cc @@ -382,6 +382,22 @@ int PikaConf::Load() rate_limiter_bandwidth_ = 200 * 1024 * 1024; // 200MB } + // rate-limiter-refill-period-us + GetConfInt64("rate-limiter-refill-period-us", &rate_limiter_refill_period_us_); + if (rate_limiter_refill_period_us_ <= 0 ) { + rate_limiter_refill_period_us_ = 100 * 1000; + } + + // rate-limiter-fairness + GetConfInt64("rate-limiter-fairness", &rate_limiter_fairness_); + if (rate_limiter_fairness_ <= 0 ) { + rate_limiter_fairness_ = 10; + } + + std::string at; + GetConfStr("rate-limiter-auto-tuned", &at); + rate_limiter_auto_tuned_ = (at == "yes" || at.empty()) ? true : false; + // max_write_buffer_num max_write_buffer_num_ = 2; GetConfInt("max-write-buffer-num", &max_write_buffer_num_); diff --git a/src/pika_server.cc b/src/pika_server.cc index 4a4f9119c2..595fc11b1b 100644 --- a/src/pika_server.cc +++ b/src/pika_server.cc @@ -1681,8 +1681,13 @@ void PikaServer::InitStorageOptions() { } storage_options_.options.rate_limiter = - std::shared_ptr(rocksdb::NewGenericRateLimiter(g_pika_conf->rate_limiter_bandwidth())); - + std::shared_ptr( + rocksdb::NewGenericRateLimiter( + g_pika_conf->rate_limiter_bandwidth(), + g_pika_conf->rate_limiter_refill_period_us(), + g_pika_conf->rate_limiter_fairness(), + g_pika_conf->rate_limiter_auto_tuned(), + )); // For Storage small compaction storage_options_.statistics_max_size = g_pika_conf->max_cache_statistic_keys(); storage_options_.small_compaction_threshold =