Skip to content

Commit 9d27dc7

Browse files
author
wuxianrong
committed
Added the large Key detection function
1 parent cc86143 commit 9d27dc7

8 files changed

Lines changed: 77 additions & 44 deletions

File tree

conf/pika.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ max-rsync-parallel-num : 4
522522
# The synchronization mode of Pika primary/secondary replication is determined by ReplicationID. ReplicationID in one replication_cluster are the same
523523
# replication-id :
524524

525+
keys-analysis: no
526+
525527
###################
526528
## Cache Settings
527529
###################

include/pika_conf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class PikaConf : public pstd::BaseConf {
4949
std::shared_lock l(rwlock_);
5050
return write_binlog_;
5151
}
52+
bool keys_analysis() {
53+
std::shared_lock l(rwlock_);
54+
return keys_analysis_;
55+
}
5256
int thread_num() {
5357
std::shared_lock l(rwlock_);
5458
return thread_num_;
@@ -498,6 +502,11 @@ class PikaConf : public pstd::BaseConf {
498502
TryPushDiffCommands("write-binlog", value);
499503
write_binlog_ = value == "yes";
500504
}
505+
void SetKeysAnalysis(const bool value) {
506+
std::lock_guard l(rwlock_);
507+
TryPushDiffCommands("keys-analysis", value ? "yes" : "no");
508+
keys_analysis_.store(value);
509+
}
501510
void SetMaxCacheStatisticKeys(const int value) {
502511
std::lock_guard l(rwlock_);
503512
TryPushDiffCommands("max-cache-statistic-keys", std::to_string(value));
@@ -941,6 +950,7 @@ class PikaConf : public pstd::BaseConf {
941950
std::atomic<int> slowlog_log_slower_than_;
942951
std::atomic<bool> slotmigrate_;
943952
std::atomic<int> binlog_writer_num_;
953+
std::atomic<bool> keys_analysis_ = false;
944954
int slowlog_max_len_ = 0;
945955
int expire_logs_days_ = 0;
946956
int expire_logs_nums_ = 0;

src/pika_admin.cc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,10 @@ void InfoCmd::InfoData(std::string& info) {
14041404
uint64_t total_background_errors = 0;
14051405
uint64_t total_memtable_usage = 0;
14061406
uint64_t total_table_reader_usage = 0;
1407+
uint64_t total_big_key_count = 0;;
14071408
uint64_t memtable_usage = 0;
14081409
uint64_t table_reader_usage = 0;
1409-
uint64_t total_big_key_count = 0;
1410+
uint64_t big_key_count = 0;
14101411
std::shared_lock db_rwl(g_pika_server->dbs_rw_);
14111412
for (const auto& db_item : g_pika_server->dbs_) {
14121413
if (!db_item.second) {
@@ -1418,8 +1419,7 @@ void InfoCmd::InfoData(std::string& info) {
14181419
db_item.second->storage()->GetUsage(storage::PROPERTY_TYPE_ROCKSDB_CUR_SIZE_ALL_MEM_TABLES, &memtable_usage);
14191420
db_item.second->storage()->GetUsage(storage::PROPERTY_TYPE_ROCKSDB_ESTIMATE_TABLE_READER_MEM, &table_reader_usage);
14201421
db_item.second->storage()->GetUsage(storage::PROPERTY_TYPE_ROCKSDB_BACKGROUND_ERRORS, &background_errors);
1421-
1422-
uint64_t big_key_count = db_item.second->storage()->GetBigKeyStatistics(db_item.first, "bigkey_property");
1422+
db_item.second->storage()->GetBigKeyStatistics();
14231423
total_big_key_count += big_key_count;
14241424
db_item.second->DBUnlockShared();
14251425
total_memtable_usage += memtable_usage;
@@ -2293,6 +2293,12 @@ void ConfigCmd::ConfigGet(std::string& ret) {
22932293
g_pika_conf->acl_pubsub_default() ? EncodeString(&config_body, "allchannels")
22942294
: EncodeString(&config_body, "resetchannels");
22952295
}
2296+
2297+
if (pstd::stringmatch(pattern.data(), "keys-analysis", 1)) {
2298+
elements += 2;
2299+
EncodeString(&config_body, "keys-analysis");
2300+
EncodeNumber(&config_body, g_pika_conf->keys_analysis());
2301+
}
22962302

22972303
std::stringstream resp;
22982304
resp << "*" << std::to_string(elements) << "\r\n" << config_body;
@@ -2483,6 +2489,17 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
24832489
g_pika_conf->SetSlowCmdPool(SlowCmdPool);
24842490
g_pika_server->SetSlowCmdThreadPoolFlag(SlowCmdPool);
24852491
res_.AppendStringRaw("+OK\r\n");
2492+
} else if (set_item == "keys-analysis") {
2493+
bool KeysAnalysis;
2494+
if (value == "yes") {
2495+
KeysAnalysis = true;
2496+
} else if (value == "no") {
2497+
KeysAnalysis = false;
2498+
} else {
2499+
res_.AppendStringRaw( "-ERR Invalid argument \'" + value + "\' for CONFIG SET 'keys-analysis'\r\n");
2500+
return;
2501+
}
2502+
g_pika_conf->SetKeysAnalysis(KeysAnalysis);
24862503
} else if (set_item == "slowlog-log-slower-than") {
24872504
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival < 0) {
24882505
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'slowlog-log-slower-than'\r\n");

src/pika_conf.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ int PikaConf::Load() {
529529
}
530530
GetConfStr("pidfile", &pidfile_);
531531

532+
// keys-analysis
533+
std::string ka;
534+
GetConfStr("keys-analysis", &ka);
535+
keys_analysis_ = wb != "no";
536+
532537
// db sync
533538
GetConfStr("db-sync-path", &db_sync_path_);
534539
db_sync_path_ = db_sync_path_.empty() ? "./dbsync/" : db_sync_path_;
@@ -766,6 +771,7 @@ int PikaConf::ConfigRewrite() {
766771
SetConfInt("slave-priority", slave_priority_);
767772
SetConfStr("log-net-activities", log_net_activities_ ? "yes" : "no");
768773
SetConfStr("write-binlog", write_binlog_ ? "yes" : "no");
774+
SetConfStr("keys-analysis", keys_analysis_ ? "yes" : "no");
769775
SetConfStr("run-id", run_id_);
770776
SetConfStr("replication-id", replication_id_);
771777
SetConfInt("max-cache-statistic-keys", max_cache_statistic_keys_);

src/storage/include/storage/storage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ class Storage {
11001100
Status EnableAutoCompaction(const OptionType& option_type,
11011101
const std::string& db_type, const std::unordered_map<std::string, std::string>& options);
11021102
void GetRocksDBInfo(std::string& info);
1103-
uint64_t GetBigKeyStatistics(const std::string& db_type, const std::string& property);
1103+
size_t GetBigKeyStatistics();
11041104

11051105
private:
11061106
std::unique_ptr<RedisStrings> strings_db_;

src/storage/src/redis.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,30 @@ void Redis::SetCompactRangeOptions(const bool is_canceled) {
199199
}
200200
}
201201

202+
void Redis::CheckBigKeyAndLog(const std::string& key, uint64_t size) {
203+
thread_local uint64_t last_log_time = 0;
204+
auto current_time = pstd::NowMicros();
205+
206+
static const uint64_t kLogInterval = 60 * 1000 * 1000;
207+
static const uint64_t kBigKeyThreshold = 10000;
208+
209+
if (current_time - last_log_time >= kLogInterval) {
210+
last_log_time = current_time;
211+
212+
if (size > kBigKeyThreshold) {
213+
std::unique_lock<std::shared_mutex> write_lock(big_key_access_mutex_);
214+
big_key_access_count_[key]++;
215+
216+
LOG(INFO) << "[BIGKEY DETECTED] Key: " << key
217+
<< ", Size: " << size
218+
<< ", Access Count: " << big_key_access_count_[key];
219+
}
220+
}
221+
}
222+
223+
size_t Redis::GetBigKeyStatistics() {
224+
std::shared_lock<std::shared_mutex> read_lock(big_key_access_mutex_);
225+
return big_key_access_count_.size();
226+
}
227+
202228
} // namespace storage

src/storage/src/redis.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <vector>
1212
#include <iostream>
1313

14+
#include <glog/logging.h>
15+
1416
#include "rocksdb/db.h"
1517
#include "rocksdb/slice.h"
1618
#include "rocksdb/status.h"
@@ -116,21 +118,8 @@ class Redis {
116118
Status SetSmallCompactionDurationThreshold(uint64_t small_compaction_duration_threshold);
117119
std::vector<rocksdb::ColumnFamilyHandle*> GetHandles(){ return handles_;};
118120
void GetRocksDBInfo(std::string &info, const char *prefix);
119-
void CheckBigKeyAndLog(const std::string& key, uint64_t size) {
120-
static const uint64_t kBigKeyThreshold = 10000;
121-
if (size > kBigKeyThreshold) {
122-
std::lock_guard<std::mutex> lock(big_key_access_mutex_);
123-
big_key_access_count_[key]++;
124-
std::cerr << "[BIGKEY DETECTED] Key: " << key
125-
<< ", Size: " << size
126-
<< ", Access Count: " << big_key_access_count_[key] << std::endl;
127-
}
128-
}
129-
130-
std::unordered_map<std::string, int> GetBigKeyStatistics() {
131-
std::lock_guard<std::mutex> lock(big_key_access_mutex_);
132-
return big_key_access_count_;
133-
}
121+
void CheckBigKeyAndLog(const std::string& key, uint64_t size);
122+
size_t GetBigKeyStatistics();
134123

135124
protected:
136125
Storage* const storage_;
@@ -158,7 +147,7 @@ class Redis {
158147
Status UpdateSpecificKeyDuration(const std::string& key, uint64_t duration);
159148
Status AddCompactKeyTaskIfNeeded(const std::string& key, uint64_t count, uint64_t duration);
160149
std::unordered_map<std::string, int> big_key_access_count_;
161-
std::mutex big_key_access_mutex_;
150+
std::shared_mutex big_key_access_mutex_;
162151
};
163152

164153
} // namespace storage

src/storage/src/storage.cc

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,30 +1855,13 @@ uint64_t Storage::GetProperty(const std::string& db_type, const std::string& pro
18551855
return result;
18561856
}
18571857

1858-
uint64_t Storage::GetBigKeyStatistics(const std::string& db_type, const std::string& property) {
1859-
uint64_t out = 0;
1860-
uint64_t result = 0;
1861-
if (db_type == ALL_DB || db_type == HASHES_DB) {
1862-
hashes_db_->GetBigKeyStatistics();
1863-
result += out;
1864-
}
1865-
1866-
if (db_type == ALL_DB || db_type == LISTS_DB) {
1867-
lists_db_->GetBigKeyStatistics();
1868-
result += out;
1869-
}
1870-
if (db_type == ALL_DB || db_type == ZSETS_DB) {
1871-
zsets_db_->GetBigKeyStatistics();
1872-
result += out;
1873-
}
1874-
if (db_type == ALL_DB || db_type == SETS_DB) {
1875-
sets_db_->GetBigKeyStatistics();
1876-
result += out;
1877-
}
1878-
if (db_type == ALL_DB || db_type == STREAMS_DB) {
1879-
streams_db_->GetBigKeyStatistics();
1880-
result += out;
1881-
}
1858+
size_t Storage::GetBigKeyStatistics() {
1859+
size_t result = 0;
1860+
result += hashes_db_->GetBigKeyStatistics();
1861+
result += lists_db_->GetBigKeyStatistics();
1862+
result += zsets_db_->GetBigKeyStatistics();
1863+
result += sets_db_->GetBigKeyStatistics();
1864+
result += streams_db_->GetBigKeyStatistics();
18821865
return result;
18831866
}
18841867

0 commit comments

Comments
 (0)