Skip to content

Commit 6706c9d

Browse files
Mixficsolwuxianrong
andcommitted
fix: The number of element updates in RedisCache is configurable (OpenAtomFoundation#3043)
* The number of element updates in RedisCache is configurable Co-authored-by: wuxianrong <[email protected]>
1 parent 4cca7bf commit 6706c9d

8 files changed

Lines changed: 36 additions & 10 deletions

File tree

conf/pika.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,12 @@ cache-model : 1
570570
# cache-type: string, set, zset, list, hash, bit
571571
cache-type: string, set, zset, list, hash, bit
572572

573+
# Set the maximum number of elements in the cache of the Set, list, Zset data types
574+
cache-value-item-max-size: 1024
575+
576+
# Sets the maximum number of bytes for Key when the String data type is updated in the cache
577+
max-key-size-in-cache: 1048576
578+
573579
# Maximum number of keys in the zset redis cache
574580
# On the disk DB, a zset field may have many fields. In the memory cache, we limit the maximum
575581
# number of keys that can exist in a zset, which is zset-zset-cache-field-num-per-key, with a

include/pika_command.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ class Cmd : public std::enable_shared_from_this<Cmd> {
539539
// used for execute multikey command into different slots
540540
virtual void Split(const HintKeys& hint_keys) = 0;
541541
virtual void Merge() = 0;
542-
virtual bool IsTooLargeKey(const int &max_sz) { return false; }
542+
virtual bool IsTooLargeKey(const size_t &max_sz) { return false; }
543543

544544
int8_t SubCmdIndex(const std::string& cmdName); // if the command no subCommand,return -1;
545545

include/pika_conf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ class PikaConf : public pstd::BaseConf {
959959
int zset_cache_start_direction() { return zset_cache_start_direction_; }
960960
int zset_cache_field_num_per_key() { return zset_cache_field_num_per_key_; }
961961
int max_key_size_in_cache() { return max_key_size_in_cache_; }
962+
int value_item_max_size_in_cache() { return cache_value_item_max_size_; }
962963
int cache_maxmemory_policy() { return cache_maxmemory_policy_; }
963964
int cache_maxmemory_samples() { return cache_maxmemory_samples_; }
964965
int cache_lfu_decay_time() { return cache_lfu_decay_time_; }
@@ -1120,7 +1121,8 @@ class PikaConf : public pstd::BaseConf {
11201121
std::atomic_int cache_bit_ = 1;
11211122
std::atomic_int zset_cache_start_direction_ = 0;
11221123
std::atomic_int zset_cache_field_num_per_key_ = 512;
1123-
std::atomic_int max_key_size_in_cache_ = 512;
1124+
std::atomic_int cache_value_item_max_size_ = 1024;
1125+
std::atomic_int max_key_size_in_cache_ = 1024 * 1024;
11241126
std::atomic_int cache_maxmemory_policy_ = 1;
11251127
std::atomic_int cache_maxmemory_samples_ = 5;
11261128
std::atomic_int cache_lfu_decay_time_ = 1;

include/pika_hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HGetCmd : public Cmd {
5454
void DoUpdateCache() override;
5555
void Split(const HintKeys& hint_keys) override {};
5656
void Merge() override {};
57-
bool IsTooLargeKey(const int &max_sz) override { return key_.size() > static_cast<uint32_t>(max_sz); }
57+
bool IsTooLargeKey(const size_t &max_sz) override { return key_.size() > max_sz; }
5858
Cmd* Clone() override { return new HGetCmd(*this); }
5959

6060
private:

include/pika_kv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SetCmd : public Cmd {
2929
void DoThroughDB() override;
3030
void Split(const HintKeys& hint_keys) override{};
3131
void Merge() override{};
32-
bool IsTooLargeKey(const int& max_sz) override { return key_.size() > static_cast<uint32_t>(max_sz); }
32+
bool IsTooLargeKey(const size_t &max_sz) override { return key_.size() > max_sz; }
3333
Cmd* Clone() override { return new SetCmd(*this); }
3434

3535
private:
@@ -65,7 +65,7 @@ class GetCmd : public Cmd {
6565
void ReadCache() override;
6666
void Split(const HintKeys& hint_keys) override{};
6767
void Merge() override{};
68-
bool IsTooLargeKey(const int &max_sz) override { return key_.size() > static_cast<uint32_t>(max_sz); }
68+
bool IsTooLargeKey(const size_t &max_sz) override { return key_.size() > max_sz; }
6969
Cmd* Clone() override { return new GetCmd(*this); }
7070

7171
private:

src/cache/include/config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ constexpr int CACHE_START_FROM_END = -1;
4040
#define DEFAULT_CACHE_ITEMS_PER_KEY 512
4141
#define DEFAULT_CACHE_MAX_KEY_SIZE 512
4242

43+
/*
44+
* cache value item default size
45+
*/
46+
#define DEFAULT_CACHE_ITEMS_SIZE 1024
47+
#define MAX_CACHE_ITEMS_SIZE 2048
48+
4349
struct CacheConfig {
4450
uint64_t maxmemory; /* Can used max memory */
4551
int32_t maxmemory_policy; /* Policy for key eviction */
4652
int32_t maxmemory_samples; /* Precision of random sampling */
4753
int32_t lfu_decay_time; /* LFU counter decay factor. */
4854
int32_t zset_cache_start_direction;
4955
int32_t zset_cache_field_num_per_key;
56+
int32_t cache_value_item_max_size;
57+
5058

5159
CacheConfig()
5260
: maxmemory(CACHE_DEFAULT_MAXMEMORY)

src/pika_cache_load_thread.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pstd/include/scope_record_lock.h"
1111

1212
extern PikaServer* g_pika_server;
13+
extern std::unique_ptr<PikaConf> g_pika_conf;
1314

1415
PikaCacheLoadThread::PikaCacheLoadThread(int zset_cache_start_direction, int zset_cache_field_num_per_key)
1516
: should_exit_(false)
@@ -70,7 +71,7 @@ bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>&
7071
db->storage()->HLen(key, &len);
7172
// If the Hash type contains more than 2048 data members,
7273
// it will not be updated to RedisCache
73-
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
74+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
7475
return false;
7576
}
7677

@@ -90,9 +91,9 @@ bool PikaCacheLoadThread::LoadList(std::string& key, const std::shared_ptr<DB>&
9091
db->storage()->LLen(key, &len);
9192
// If the List type contains more than 2048 data members,
9293
// it will not be updated to RedisCache
93-
if (len <= 0 || CACHE_VALUE_ITEM_MAX_SIZE < len) {
94+
if (len <= 0 || g_pika_conf->value_item_max_size_in_cache() < len) {
9495
LOG(WARNING) << "can not load key, because item size:" << len
95-
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
96+
<< " beyond max item size:" << g_pika_conf->value_item_max_size_in_cache();
9697
return false;
9798
}
9899

@@ -112,9 +113,9 @@ bool PikaCacheLoadThread::LoadSet(std::string& key, const std::shared_ptr<DB>& d
112113
db->storage()->SCard(key, &len);
113114
// If the Set type contains more than 2048 data members,
114115
// it will not be updated to RedisCache
115-
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
116+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
116117
LOG(WARNING) << "can not load key, because item size:" << len
117-
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
118+
<< " beyond max item size:" << g_pika_conf->value_item_max_size_in_cache();
118119
return false;
119120
}
120121

src/pika_conf.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,15 @@ int PikaConf::Load() {
683683
}
684684
zset_cache_field_num_per_key_ = zset_cache_field_num_per_key;
685685

686+
int cache_value_item_max_size = DEFAULT_CACHE_ITEMS_SIZE;
687+
GetConfInt("cache-value-item-max-size", &cache_value_item_max_size);
688+
if (cache_value_item_max_size <= 0) {
689+
cache_value_item_max_size = DEFAULT_CACHE_ITEMS_SIZE;
690+
} else if (cache_value_item_max_size > MAX_CACHE_ITEMS_SIZE) {
691+
cache_value_item_max_size = MAX_CACHE_ITEMS_SIZE;
692+
}
693+
cache_value_item_max_size_ = cache_value_item_max_size;
694+
686695
int max_key_size_in_cache = DEFAULT_CACHE_MAX_KEY_SIZE;
687696
GetConfInt("max-key-size-in-cache", &max_key_size_in_cache);
688697
if (max_key_size_in_cache <= 0) {

0 commit comments

Comments
 (0)