Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix randomKey pathological case #13214

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from

Conversation

moticless
Copy link
Collaborator

@moticless moticless commented Apr 16, 2024

Randomkey implemented such that if number of keys with expiration is equal to number of keys, then it only retries up-to 100 times to get a random, non-expired, key. If it fails, then it returns expired key. However, if the number of keys with expiration differed from the total number of keys, the function continue retrying until it success. This approach posed potential delays, particularly when a significant portion of keys were expired.

With this change, it will retry unconditionally 100 times.

Comment on lines 345 to 347
/* If the DB is composed only of keys with an expire set,
* it could happen that all the keys are already logically
* expired in the slave, so the function cannot stop because
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's update this comment.

@sundb
Copy link
Collaborator

sundb commented Apr 16, 2024

and i found that dbRandomKey may return NULL, but RM_RandomKey doesn't check it.
maybe we can fix it together this PR.

RedisModuleString *RM_RandomKey(RedisModuleCtx *ctx) {
    robj *key = dbRandomKey(ctx->client->db);
    autoMemoryAdd(ctx,REDISMODULE_AM_STRING,key);
    return key;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants