Skip to content

Commit

Permalink
Add cron to recalculate dbsize
Browse files Browse the repository at this point in the history
  • Loading branch information
kinoute committed Apr 2, 2024
1 parent c588a6d commit 8654ab8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions kvrocks.conf
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ compaction-checker-range 0-7
# e.g. bgsave-cron 0 3 * * * 0 4 * * *
# would bgsave the db at 3am and 4am every day

# Kvrocks doesn't store the key number directly. It needs to scan the DB and
# then retrieve the key number by using the dbsize scan command.
# The Dbsize scan scheduler auto-recalculates the estimated keys at scheduled time.
# Time expression format is the same as crontab (currently only support * and int)
# e.g. dbsize-scan-cron 0 * * * *
# would recalculate the keyspace infos of the db every hour.

# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Config::Config() {
{"slaveof", true, new StringField(&slaveof_, "")},
{"compact-cron", false, new StringField(&compact_cron_str_, "")},
{"bgsave-cron", false, new StringField(&bgsave_cron_str_, "")},
{"dbsize-scan-cron", false, new StringField(&dbsize_scan_cron_str_, "")},
{"replica-announce-ip", false, new StringField(&replica_announce_ip, "")},
{"replica-announce-port", false, new UInt32Field(&replica_announce_port, 0, 0, PORT_LIMIT)},
{"compaction-checker-range", false, new StringField(&compaction_checker_range_str_, "")},
Expand Down Expand Up @@ -292,6 +293,11 @@ void Config::initFieldValidator() {
std::vector<std::string> args = util::Split(v, " \t");
return bgsave_cron.SetScheduleTime(args);
}},
{"dbsize-scan-cron",
[this](const std::string &k, const std::string &v) -> Status {
std::vector<std::string> args = util::Split(v, " \t");
return dbsize_scan_cron.SetScheduleTime(args);
}},
{"compaction-checker-range",
[this](const std::string &k, const std::string &v) -> Status {
if (v.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ struct Config {
uint32_t master_port = 0;
Cron compact_cron;
Cron bgsave_cron;
Cron dbsize_scan_cron;
CompactionCheckerRange compaction_checker_range{-1, -1};
int64_t force_compact_file_age;
int force_compact_file_min_deleted_percentage;
Expand Down Expand Up @@ -246,6 +247,7 @@ struct Config {
std::string slaveof_;
std::string compact_cron_str_;
std::string bgsave_cron_str_;
std::string dbsize_scan_cron_str_;
std::string compaction_checker_range_str_;
std::string profiling_sample_commands_str_;
std::map<std::string, std::unique_ptr<ConfigField>> fields_;
Expand Down
17 changes: 17 additions & 0 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,23 @@ void Server::cron() {
Status s = AsyncBgSaveDB();
LOG(INFO) << "[server] Schedule to bgsave the db, result: " << s.Msg();
}
if (config_->dbsize_scan_cron.IsEnabled() && config_->dbsize_scan_cron.IsTimeMatch(&now)) {
std::vector<std::string> namespaces;

auto tokens = namespace_.List();
for (auto &token : tokens) {
namespaces.emplace_back(token.second); // namespace
}

// add default namespace as fallback
namespaces.emplace_back(kDefaultNamespace);

for (auto &ns : namespaces) {
Status s = AsyncScanDBSize(ns);
LOG(INFO) << "[server] Schedule to recalculate the db size on namespace: " << ns << ", result: " << s.Msg();
}

}
}
// check every 10s
if (counter != 0 && counter % 100 == 0) {
Expand Down
1 change: 1 addition & 0 deletions tests/cppunit/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TEST(Config, GetAndSet) {
{"masterauth", "mytest_masterauth"},
{"compact-cron", "1 2 3 4 5"},
{"bgsave-cron", "5 4 3 2 1"},
{"dbsize-scan-cron", "1 2 3 2 1"},
{"max-io-mb", "5000"},
{"max-db-size", "6000"},
{"max-replication-mb", "7000"},
Expand Down

0 comments on commit 8654ab8

Please sign in to comment.