Skip to content

Commit 1668357

Browse files
Mixficsolwuxianrong
andauthored
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 274b1b7 commit 1668357

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
@@ -533,6 +533,12 @@ cache-model : 1
533533
# cache-type: string, set, zset, list, hash, bit
534534
cache-type: string, set, zset, list, hash, bit
535535

536+
# Set the maximum number of elements in the cache of the Set, list, Zset data types
537+
cache-value-item-max-size: 1024
538+
539+
# Sets the maximum number of bytes for Key when the String data type is updated in the cache
540+
max-key-size-in-cache: 1048576
541+
536542
# Maximum number of keys in the zset redis cache
537543
# On the disk DB, a zset field may have many fields. In the memory cache, we limit the maximum
538544
# 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
@@ -531,7 +531,7 @@ class Cmd : public std::enable_shared_from_this<Cmd> {
531531
// used for execute multikey command into different slots
532532
virtual void Split(const HintKeys& hint_keys) = 0;
533533
virtual void Merge() = 0;
534-
virtual bool IsTooLargeKey(const int &max_sz) { return false; }
534+
virtual bool IsTooLargeKey(const size_t &max_sz) { return false; }
535535

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

include/pika_conf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ class PikaConf : public pstd::BaseConf {
845845
int zset_cache_start_direction() { return zset_cache_start_direction_; }
846846
int zset_cache_field_num_per_key() { return zset_cache_field_num_per_key_; }
847847
int max_key_size_in_cache() { return max_key_size_in_cache_; }
848+
int value_item_max_size_in_cache() { return cache_value_item_max_size_; }
848849
int cache_maxmemory_policy() { return cache_maxmemory_policy_; }
849850
int cache_maxmemory_samples() { return cache_maxmemory_samples_; }
850851
int cache_lfu_decay_time() { return cache_lfu_decay_time_; }
@@ -982,7 +983,8 @@ class PikaConf : public pstd::BaseConf {
982983
std::atomic_int cache_bit_ = 1;
983984
std::atomic_int zset_cache_start_direction_ = 0;
984985
std::atomic_int zset_cache_field_num_per_key_ = 512;
985-
std::atomic_int max_key_size_in_cache_ = 512;
986+
std::atomic_int cache_value_item_max_size_ = 1024;
987+
std::atomic_int max_key_size_in_cache_ = 1024 * 1024;
986988
std::atomic_int cache_maxmemory_policy_ = 1;
987989
std::atomic_int cache_maxmemory_samples_ = 5;
988990
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)
@@ -71,7 +72,7 @@ bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>&
7172
db->storage()->HLen(key, &len);
7273
// If the Hash type contains more than 2048 data members,
7374
// it will not be updated to RedisCache
74-
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
75+
if (0 >= len || g_pika_conf->value_item_max_size_in_cache() < len) {
7576
return false;
7677
}
7778

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

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

src/pika_conf.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,15 @@ int PikaConf::Load() {
609609
}
610610
zset_cache_field_num_per_key_ = zset_cache_field_num_per_key;
611611

612+
int cache_value_item_max_size = DEFAULT_CACHE_ITEMS_SIZE;
613+
GetConfInt("cache-value-item-max-size", &cache_value_item_max_size);
614+
if (cache_value_item_max_size <= 0) {
615+
cache_value_item_max_size = DEFAULT_CACHE_ITEMS_SIZE;
616+
} else if (cache_value_item_max_size > MAX_CACHE_ITEMS_SIZE) {
617+
cache_value_item_max_size = MAX_CACHE_ITEMS_SIZE;
618+
}
619+
cache_value_item_max_size_ = cache_value_item_max_size;
620+
612621
int max_key_size_in_cache = DEFAULT_CACHE_MAX_KEY_SIZE;
613622
GetConfInt("max-key-size-in-cache", &max_key_size_in_cache);
614623
if (max_key_size_in_cache <= 0) {

0 commit comments

Comments
 (0)