Skip to content

Commit

Permalink
Unshare object to avoid LRU/LFU being messed up.
Browse files Browse the repository at this point in the history
Signed-off-by: chentianjie.ctj <[email protected]>
  • Loading branch information
chentianjie.ctj committed Apr 7, 2024
1 parent 717ec1e commit 73395f3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ robj *lookupKey(serverDb *db, robj *key, int flags) {
server.current_client->cmd->proc != touchCommand)
flags |= LOOKUP_NOTOUCH;
if (!hasActiveChildProcess() && !(flags & LOOKUP_NOTOUCH)){
if (!canUseSharedObject() && val->refcount == OBJ_SHARED_REFCOUNT) {
val = dupStringObject(val);
kvstoreDictSetVal(db->keys, getKeySlot(key->ptr), de, val);
}
if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
updateLFU(val);
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ void trimStringObjectIfNeeded(robj *o, int trim_small_values) {
}
}

int canUseSharedObject(void) {
return server.maxmemory == 0 || !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS);
}

/* Try to encode a string object in order to save space */
robj *tryObjectEncodingEx(robj *o, int try_trim) {
long value;
Expand Down Expand Up @@ -662,8 +666,7 @@ robj *tryObjectEncodingEx(robj *o, int try_trim) {
* Note that we avoid using shared integers when maxmemory is used
* because every object needs to have a private LRU field for the LRU
* algorithm to work well. */
if ((server.maxmemory == 0 ||
!(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS)) &&
if (canUseSharedObject() &&
value >= 0 &&
value < OBJ_SHARED_INTEGERS)
{
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,7 @@ int collateStringObjects(const robj *a, const robj *b);
int equalStringObjects(robj *a, robj *b);
unsigned long long estimateObjectIdleTime(robj *o);
void trimStringObjectIfNeeded(robj *o, int trim_small_values);
int canUseSharedObject(void);
#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)

/* Synchronous I/O with timeout */
Expand Down

0 comments on commit 73395f3

Please sign in to comment.