Skip to content

Commit 61c760c

Browse files
Mixficsolwuxianrong
andauthored
Example Modify the policy for updating big keys except String to Redis-Cache (OpenAtomFoundation#3047)
Co-authored-by: wuxianrong <[email protected]>
1 parent 29c8311 commit 61c760c

5 files changed

Lines changed: 73 additions & 7 deletions

File tree

include/pika_conf.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "pstd/include/pstd_string.h"
1717

1818
#include "acl.h"
19+
#include "cache/include/config.h"
1920
#include "include/pika_define.h"
2021
#include "include/pika_meta.h"
2122
#include "rocksdb/compression_type.h"
@@ -748,6 +749,30 @@ class PikaConf : public pstd::BaseConf {
748749
return rocksdb_perf_level_.load();
749750
}
750751

752+
int CacheValueItemMaxSize() const {
753+
return cache_value_item_max_size_.load();
754+
}
755+
756+
bool UpdateCacheValueItemMaxSize(int size) {
757+
if (size >= MAX_CACHE_ITEMS_SIZE || size <= 0) {
758+
return false;
759+
}
760+
cache_value_item_max_size_.store(size);
761+
return true;
762+
}
763+
764+
size_t MaxKeySizeInCache() const {
765+
return max_key_size_in_cache_.load();
766+
}
767+
768+
bool UpdateMaxKeySizeInCache(size_t size) {
769+
if (size >= MAX_CACHE_MAX_KEY_SIZE || size <= 0) {
770+
return false;
771+
}
772+
max_key_size_in_cache_.store(size);
773+
return true;
774+
}
775+
751776
bool UpdateRocksDBPerfLevel(int perf_level) {
752777
if (perf_level >= 6 || perf_level < 0) {
753778
return false;
@@ -984,7 +1009,7 @@ class PikaConf : public pstd::BaseConf {
9841009
std::atomic_int zset_cache_start_direction_ = 0;
9851010
std::atomic_int zset_cache_field_num_per_key_ = 512;
9861011
std::atomic_int cache_value_item_max_size_ = 1024;
987-
std::atomic_int max_key_size_in_cache_ = 1024 * 1024;
1012+
std::atomic_size_t max_key_size_in_cache_ = 1024 * 1024;
9881013
std::atomic_int cache_maxmemory_policy_ = 1;
9891014
std::atomic_int cache_maxmemory_samples_ = 5;
9901015
std::atomic_int cache_lfu_decay_time_ = 1;

src/cache/include/config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ constexpr int CACHE_START_FROM_END = -1;
3838
* cache items per key
3939
*/
4040
#define DEFAULT_CACHE_ITEMS_PER_KEY 512
41-
#define DEFAULT_CACHE_MAX_KEY_SIZE 512
41+
#define DEFAULT_CACHE_MAX_KEY_SIZE 1048576 // 1M
42+
#define MAX_CACHE_MAX_KEY_SIZE 2097152 // 2M
4243

4344
/*
4445
* cache value item default size

src/pika_admin.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,18 @@ void ConfigCmd::ConfigGet(std::string& ret) {
22132213
EncodeNumber(&config_body, g_pika_conf->blob_file_size());
22142214
}
22152215

2216+
if (pstd::stringmatch(pattern.data(), "cache-value-item-max-size", 1) != 0) {
2217+
elements += 2;
2218+
EncodeString(&config_body, "cache-value-item-max-size");
2219+
EncodeNumber(&config_body, g_pika_conf->CacheValueItemMaxSize());
2220+
}
2221+
2222+
if (pstd::stringmatch(pattern.data(), "max-key-size-in-cache", 1) != 0) {
2223+
elements += 2;
2224+
EncodeString(&config_body, "max-key-size-in-cache");
2225+
EncodeNumber(&config_body, g_pika_conf->MaxKeySizeInCache());
2226+
}
2227+
22162228
if (pstd::stringmatch(pattern.data(), "blob-garbage-collection-age-cutoff", 1) != 0) {
22172229
elements += 2;
22182230
EncodeString(&config_body, "blob-garbage-collection-age-cutoff");
@@ -2861,6 +2873,32 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
28612873
return;
28622874
}
28632875
res_.AppendStringRaw("+OK\r\n");
2876+
} else if (set_item == "cache-value-item-max-size") {
2877+
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival <= 0) {
2878+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'cache-value-item-max-size'\r\n");
2879+
return;
2880+
}
2881+
bool success = g_pika_conf->UpdateCacheValueItemMaxSize(int(ival));
2882+
LOG(INFO) << "update cache-value-item-max-size to " << ival
2883+
<< (success ? " success" : " failed");
2884+
if (!success) {
2885+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'cache-value-item-max-size', should between 1 and 2048\r\n");
2886+
return;
2887+
}
2888+
res_.AppendStringRaw("+OK\r\n");
2889+
} else if (set_item == "max-key-size-in-cache") {
2890+
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival <= 0) {
2891+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'max-key-size-in-cache'\r\n");
2892+
return;
2893+
}
2894+
bool success = g_pika_conf->UpdateMaxKeySizeInCache(size_t(ival));
2895+
LOG(INFO) << "update max-key-size-in-cache to " << ival
2896+
<< (success ? " success" : " failed");
2897+
if (!success) {
2898+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'max-key-size-in-cache', should between 1 and 2097152 \r\n");
2899+
return;
2900+
}
2901+
res_.AppendStringRaw("+OK\r\n");
28642902
} else if (set_item == "throttle-bytes-per-second") {
28652903
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival <= 0) {
28662904
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'throttle-bytes-per-second'\r\n");

src/pika_cache_load_thread.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool PikaCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr<DB>& db
5858
std::string value;
5959
int64_t ttl = -1;
6060
rocksdb::Status s = db->storage()->GetWithTTL(key, &value, &ttl);
61-
if (!s.ok()) {
61+
if (!s.ok() || key.size() > g_pika_conf->max_key_size_in_cache()) {
6262
LOG(WARNING) << "load kv failed, key=" << key;
6363
return false;
6464
}
@@ -72,7 +72,7 @@ bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>&
7272
db->storage()->HLen(key, &len);
7373
// If the Hash type contains more than 2048 data members,
7474
// it will not be updated to RedisCache
75-
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
75+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len || key.size() > g_pika_conf->max_key_size_in_cache()) {
7676
return false;
7777
}
7878

@@ -93,7 +93,7 @@ bool PikaCacheLoadThread::LoadList(std::string& key, const std::shared_ptr<DB>&
9393
db->storage()->LLen(key, &len);
9494
// If the List type contains more than 2048 data members,
9595
// it will not be updated to RedisCache
96-
if (len <= 0 || g_pika_conf->value_item_max_size_in_cache() < len) {
96+
if (len <= 0 || g_pika_conf->value_item_max_size_in_cache() < len || key.size() > g_pika_conf->max_key_size_in_cache()) {
9797
LOG(WARNING) << "can not load key, because item size:" << len
9898
<< " beyond max item size:" << g_pika_conf->value_item_max_size_in_cache();
9999
return false;
@@ -116,7 +116,7 @@ bool PikaCacheLoadThread::LoadSet(std::string& key, const std::shared_ptr<DB>& d
116116
db->storage()->SCard(key, &len);
117117
// If the Set type contains more than 2048 data members,
118118
// it will not be updated to RedisCache
119-
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
119+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len || key.size() > g_pika_conf->max_key_size_in_cache()) {
120120
LOG(WARNING) << "can not load key, because item size:" << len
121121
<< " beyond max item size:" << g_pika_conf->value_item_max_size_in_cache();
122122
return false;
@@ -139,7 +139,7 @@ bool PikaCacheLoadThread::LoadZset(std::string& key, const std::shared_ptr<DB>&
139139
int start_index = 0;
140140
int stop_index = -1;
141141
db->storage()->ZCard(key, &len);
142-
if (0 >= len) {
142+
if (0 >= len || key.size() > g_pika_conf->max_key_size_in_cache()) {
143143
return false;
144144
}
145145

src/pika_conf.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ int PikaConf::Load() {
622622
GetConfInt("max-key-size-in-cache", &max_key_size_in_cache);
623623
if (max_key_size_in_cache <= 0) {
624624
max_key_size_in_cache = DEFAULT_CACHE_MAX_KEY_SIZE;
625+
} else if (max_key_size_in_cache > MAX_CACHE_MAX_KEY_SIZE) {
626+
max_key_size_in_cache = MAX_CACHE_MAX_KEY_SIZE;
625627
}
626628
max_key_size_in_cache_ = max_key_size_in_cache;
627629

0 commit comments

Comments
 (0)