From f7cc8c8f374cbb128175d61b6457b268f2791983 Mon Sep 17 00:00:00 2001 From: hwware Date: Mon, 7 Oct 2024 09:29:56 +0000 Subject: [PATCH] Add test case and Address Ping Comment --- src/config.c | 4 ++-- src/evict.c | 8 ++------ src/server.c | 2 +- src/server.h | 2 +- tests/unit/maxmemory.tcl | 19 +++++++++++++++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/config.c b/src/config.c index 1a3ccdae89..69a3a8e269 100644 --- a/src/config.c +++ b/src/config.c @@ -2509,7 +2509,7 @@ static int updateMaxmemoryReserved(const char **err) { server.maxmemory_reserved_scale = 60; } } - calculateMaxAvailableMemory(); + updateMaxAvailableMemory(); return 1; } @@ -2524,7 +2524,7 @@ static int updateMaxmemory(const char **err) { "depending on the maxmemory-policy.", server.maxmemory, used); } - calculateMaxAvailableMemory(); + updateMaxAvailableMemory(); startEvictionTimeProc(); } return 1; diff --git a/src/evict.c b/src/evict.c index eed9fc15c4..09b017b118 100644 --- a/src/evict.c +++ b/src/evict.c @@ -398,16 +398,12 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev if (total) *total = mem_reported; /* We may return ASAP if there is no need to compute the level. */ - if (!server.maxmemory) { + if (!server.maxmemory_available) { if (level) *level = 0; return C_OK; } - if (server.maxmemory_reserved_scale) { - if (mem_reported <= server.maxmemory_available && !level) return C_OK; - } else if (mem_reported <= server.maxmemory && !level) { - return C_OK; - } + if (mem_reported <= server.maxmemory_available && !level) return C_OK; /* Remove the size of replicas output buffers and AOF buffer from the * count of used memory. */ diff --git a/src/server.c b/src/server.c index 11716ee219..a24d48cfa3 100644 --- a/src/server.c +++ b/src/server.c @@ -2622,7 +2622,7 @@ void initServer(void) { resetReplicationBuffer(); if (server.maxmemory) { - calculateMaxAvailableMemory(); + updateMaxAvailableMemory(); } /* Make sure the locale is set on startup based on the config file. */ diff --git a/src/server.h b/src/server.h index 0514ee50f8..a7277b2696 100644 --- a/src/server.h +++ b/src/server.h @@ -3012,7 +3012,7 @@ void trimStringObjectIfNeeded(robj *o, int trim_small_values); static inline int canUseSharedObject(void) { return server.maxmemory == 0 || !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS); } -static inline void calculateMaxAvailableMemory(void) { +static inline void updateMaxAvailableMemory(void) { server.maxmemory_available = (unsigned long long)server.maxmemory / 100.0 * (100 - server.maxmemory_reserved_scale); } #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR) diff --git a/tests/unit/maxmemory.tcl b/tests/unit/maxmemory.tcl index d4e62246f1..72855b0e8b 100644 --- a/tests/unit/maxmemory.tcl +++ b/tests/unit/maxmemory.tcl @@ -145,6 +145,25 @@ start_server {tags {"maxmemory" "external:skip"}} { } start_server {tags {"maxmemory external:skip"}} { + + test "maxmemory-reserved-scale" { + r flushdb + r config set maxmemory 10mb + set current_dbsize 0 + + foreach scale_value {30 10 0} { + r config set maxmemory-reserved-scale $scale_value + # fill 20mb using 200 keys of 100kb + catch { for {set j 0} {$j < 200} {incr j} { + r setrange $j 100000 x + }} e + assert_match {*OOM*} $e + assert_lessthan $current_dbsize [r dbsize] + set current_dbsize [r dbsize] + } + r flushdb + } + test "Without maxmemory small integers are shared" { r config set maxmemory 0 r set a 1