Skip to content

Commit

Permalink
Add test case and Address Ping Comment
Browse files Browse the repository at this point in the history
Signed-off-by: hwware <[email protected]>
  • Loading branch information
hwware committed Oct 18, 2024
1 parent 8ffdd55 commit d6b15e1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,7 @@ static int updateMaxmemoryReserved(const char **err) {
server.maxmemory_reserved_scale = 60;
}
}
calculateMaxAvailableMemory();
updateMaxAvailableMemory();
return 1;
}

Expand All @@ -2526,7 +2526,7 @@ static int updateMaxmemory(const char **err) {
"depending on the maxmemory-policy.",
server.maxmemory, used);
}
calculateMaxAvailableMemory();
updateMaxAvailableMemory();
startEvictionTimeProc();
}
return 1;
Expand Down
8 changes: 2 additions & 6 deletions src/evict.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2623,7 +2623,7 @@ void initServer(void) {
resetReplicationBuffer();

if (server.maxmemory) {
calculateMaxAvailableMemory();
updateMaxAvailableMemory();
}

/* Make sure the locale is set on startup based on the config file. */
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3014,7 +3014,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)
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/maxmemory.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d6b15e1

Please sign in to comment.