Skip to content

Commit 26b2ff7

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

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
@@ -18,6 +18,7 @@
1818
#include "pstd/include/pstd_string.h"
1919

2020
#include "acl.h"
21+
#include "cache/include/config.h"
2122
#include "include/pika_define.h"
2223
#include "rocksdb/compression_type.h"
2324

@@ -862,6 +863,30 @@ class PikaConf : public pstd::BaseConf {
862863
return rocksdb_perf_level_.load();
863864
}
864865

866+
int CacheValueItemMaxSize() const {
867+
return cache_value_item_max_size_.load();
868+
}
869+
870+
bool UpdateCacheValueItemMaxSize(int size) {
871+
if (size >= MAX_CACHE_ITEMS_SIZE || size <= 0) {
872+
return false;
873+
}
874+
cache_value_item_max_size_.store(size);
875+
return true;
876+
}
877+
878+
size_t MaxKeySizeInCache() const {
879+
return max_key_size_in_cache_.load();
880+
}
881+
882+
bool UpdateMaxKeySizeInCache(size_t size) {
883+
if (size >= MAX_CACHE_MAX_KEY_SIZE || size <= 0) {
884+
return false;
885+
}
886+
max_key_size_in_cache_.store(size);
887+
return true;
888+
}
889+
865890
bool UpdateRocksDBPerfLevel(int perf_level) {
866891
if (perf_level >= 6 || perf_level < 0) {
867892
return false;
@@ -1122,7 +1147,7 @@ class PikaConf : public pstd::BaseConf {
11221147
std::atomic_int zset_cache_start_direction_ = 0;
11231148
std::atomic_int zset_cache_field_num_per_key_ = 512;
11241149
std::atomic_int cache_value_item_max_size_ = 1024;
1125-
std::atomic_int max_key_size_in_cache_ = 1024 * 1024;
1150+
std::atomic_size_t max_key_size_in_cache_ = 1024 * 1024;
11261151
std::atomic_int cache_maxmemory_policy_ = 1;
11271152
std::atomic_int cache_maxmemory_samples_ = 5;
11281153
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
@@ -2132,6 +2132,18 @@ void ConfigCmd::ConfigGet(std::string& ret) {
21322132
EncodeNumber(&config_body, g_pika_conf->blob_file_size());
21332133
}
21342134

2135+
if (pstd::stringmatch(pattern.data(), "cache-value-item-max-size", 1) != 0) {
2136+
elements += 2;
2137+
EncodeString(&config_body, "cache-value-item-max-size");
2138+
EncodeNumber(&config_body, g_pika_conf->CacheValueItemMaxSize());
2139+
}
2140+
2141+
if (pstd::stringmatch(pattern.data(), "max-key-size-in-cache", 1) != 0) {
2142+
elements += 2;
2143+
EncodeString(&config_body, "max-key-size-in-cache");
2144+
EncodeNumber(&config_body, g_pika_conf->MaxKeySizeInCache());
2145+
}
2146+
21352147
if (pstd::stringmatch(pattern.data(), "blob-garbage-collection-age-cutoff", 1) != 0) {
21362148
elements += 2;
21372149
EncodeString(&config_body, "blob-garbage-collection-age-cutoff");
@@ -2902,6 +2914,32 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
29022914
return;
29032915
}
29042916
res_.AppendStringRaw("+OK\r\n");
2917+
} else if (set_item == "cache-value-item-max-size") {
2918+
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival <= 0) {
2919+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'cache-value-item-max-size'\r\n");
2920+
return;
2921+
}
2922+
bool success = g_pika_conf->UpdateCacheValueItemMaxSize(int(ival));
2923+
LOG(INFO) << "update cache-value-item-max-size to " << ival
2924+
<< (success ? " success" : " failed");
2925+
if (!success) {
2926+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'cache-value-item-max-size', should between 1 and 2048\r\n");
2927+
return;
2928+
}
2929+
res_.AppendStringRaw("+OK\r\n");
2930+
} else if (set_item == "max-key-size-in-cache") {
2931+
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival <= 0) {
2932+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'max-key-size-in-cache'\r\n");
2933+
return;
2934+
}
2935+
bool success = g_pika_conf->UpdateMaxKeySizeInCache(size_t(ival));
2936+
LOG(INFO) << "update max-key-size-in-cache to " << ival
2937+
<< (success ? " success" : " failed");
2938+
if (!success) {
2939+
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'max-key-size-in-cache', should between 1 and 2097152 \r\n");
2940+
return;
2941+
}
2942+
res_.AppendStringRaw("+OK\r\n");
29052943
} else if (set_item == "throttle-bytes-per-second") {
29062944
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival <= 0) {
29072945
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
}
@@ -71,7 +71,7 @@ bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>&
7171
db->storage()->HLen(key, &len);
7272
// If the Hash type contains more than 2048 data members,
7373
// it will not be updated to RedisCache
74-
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
74+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len || key.size() > g_pika_conf->max_key_size_in_cache()) {
7575
return false;
7676
}
7777

@@ -91,7 +91,7 @@ bool PikaCacheLoadThread::LoadList(std::string& key, const std::shared_ptr<DB>&
9191
db->storage()->LLen(key, &len);
9292
// If the List type contains more than 2048 data members,
9393
// it will not be updated to RedisCache
94-
if (len <= 0 || g_pika_conf->value_item_max_size_in_cache() < len) {
94+
if (len <= 0 || g_pika_conf->value_item_max_size_in_cache() < len || key.size() > g_pika_conf->max_key_size_in_cache()) {
9595
LOG(WARNING) << "can not load key, because item size:" << len
9696
<< " beyond max item size:" << g_pika_conf->value_item_max_size_in_cache();
9797
return false;
@@ -113,7 +113,7 @@ bool PikaCacheLoadThread::LoadSet(std::string& key, const std::shared_ptr<DB>& d
113113
db->storage()->SCard(key, &len);
114114
// If the Set type contains more than 2048 data members,
115115
// it will not be updated to RedisCache
116-
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
116+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len || key.size() > g_pika_conf->max_key_size_in_cache()) {
117117
LOG(WARNING) << "can not load key, because item size:" << len
118118
<< " beyond max item size:" << g_pika_conf->value_item_max_size_in_cache();
119119
return false;
@@ -135,7 +135,7 @@ bool PikaCacheLoadThread::LoadZset(std::string& key, const std::shared_ptr<DB>&
135135
int start_index = 0;
136136
int stop_index = -1;
137137
db->storage()->ZCard(key, &len);
138-
if (0 >= len) {
138+
if (0 >= len || key.size() > g_pika_conf->max_key_size_in_cache()) {
139139
return false;
140140
}
141141

src/pika_conf.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ int PikaConf::Load() {
696696
GetConfInt("max-key-size-in-cache", &max_key_size_in_cache);
697697
if (max_key_size_in_cache <= 0) {
698698
max_key_size_in_cache = DEFAULT_CACHE_MAX_KEY_SIZE;
699+
} else if (max_key_size_in_cache > MAX_CACHE_MAX_KEY_SIZE) {
700+
max_key_size_in_cache = MAX_CACHE_MAX_KEY_SIZE;
699701
}
700702
max_key_size_in_cache_ = max_key_size_in_cache;
701703

0 commit comments

Comments
 (0)