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

Fast path in SET if the expiration time is expired #865

Merged
merged 8 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ void restoreCommand(client *c) {
if (ttl && !absttl) ttl += commandTimeSnapshot();
if (ttl && checkAlreadyExpired(ttl)) {
if (deleted) {
/* Here we don't use deleteExpiredKeyFromOverwriteAndPropagate because
* strictly speaking, the `deleted` is triggered by `replace`. */
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
robj *aux = server.lazyfree_lazy_server_del ? shared.unlink : shared.del;
rewriteClientCommandVector(c, 2, aux, key);
signalModifiedKey(c, c->db, key);
Expand Down
15 changes: 15 additions & 0 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,21 @@ void deleteExpiredKeyAndPropagate(serverDb *db, robj *keyobj) {
server.stat_expiredkeys++;
}

/* Delete the specified expired key from overwriting and propagate the DEL or UNLINK. */
void deleteExpiredKeyFromOverwriteAndPropagate(client *c, robj *keyobj, int do_delete) {
zuiderkwast marked this conversation as resolved.
Show resolved Hide resolved
if (do_delete) {
int deleted = dbGenericDelete(c->db, keyobj, server.lazyfree_lazy_expire, DB_FLAG_KEY_EXPIRED);
serverAssertWithInfo(c, keyobj, deleted);
server.dirty++;
}

/* Replicate/AOF this as an explicit DEL or UNLINK. */
robj *aux = server.lazyfree_lazy_expire ? shared.unlink : shared.del;
rewriteClientCommandVector(c, 2, aux, keyobj);
signalModifiedKey(c, c->db, keyobj);
notifyKeyspaceEvent(NOTIFY_GENERIC, "del", keyobj, c->db->id);
}

/* Propagate an implicit key deletion into replicas and the AOF file.
* When a key was deleted in the primary by eviction, expiration or a similar
* mechanism a DEL/UNLINK operation for this key is sent
Expand Down
12 changes: 1 addition & 11 deletions src/expire.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,17 +668,7 @@ void expireGenericCommand(client *c, long long basetime, int unit) {
}

if (checkAlreadyExpired(when)) {
robj *aux;

int deleted = dbGenericDelete(c->db, key, server.lazyfree_lazy_expire, DB_FLAG_KEY_EXPIRED);
serverAssertWithInfo(c, key, deleted);
server.dirty++;

/* Replicate/AOF this as an explicit DEL or UNLINK. */
aux = server.lazyfree_lazy_expire ? shared.unlink : shared.del;
rewriteClientCommandVector(c, 2, aux, key);
signalModifiedKey(c, c->db, key);
notifyKeyspaceEvent(NOTIFY_GENERIC, "del", key, c->db->id);
deleteExpiredKeyFromOverwriteAndPropagate(c, key, 1);
addReply(c, shared.cone);
return;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,7 @@ int setModuleNumericConfig(ModuleConfig *config, long long val, const char **err
/* db.c -- Keyspace access API */
int removeExpire(serverDb *db, robj *key);
void deleteExpiredKeyAndPropagate(serverDb *db, robj *keyobj);
void deleteExpiredKeyFromOverwriteAndPropagate(client *c, robj *keyobj, int do_delete);
void propagateDeletion(serverDb *db, robj *key, int lazy);
int keyIsExpired(serverDb *db, robj *key);
long long getExpire(serverDb *db, robj *key);
Expand Down
17 changes: 10 additions & 7 deletions src/t_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ void setGenericCommand(client *c,
return;
}

/* If the `milliseconds` have expired, then we don't need to set it into the
* database, and then wait for the active expire to delete it, it is wasteful.
* If the key already exists, delete it. */
if (expire && checkAlreadyExpired(milliseconds)) {
if (found) deleteExpiredKeyFromOverwriteAndPropagate(c, key, 1);
if (!(flags & OBJ_SET_GET)) addReply(c, shared.ok);
return;
}

/* When expire is not NULL, we avoid deleting the TTL so it can be updated later instead of being deleted and then
* created again. */
setkey_flags |= ((flags & OBJ_KEEPTTL) || expire) ? SETKEY_KEEPTTL : 0;
Expand Down Expand Up @@ -395,13 +404,7 @@ void getexCommand(client *c) {
if (((flags & OBJ_PXAT) || (flags & OBJ_EXAT)) && checkAlreadyExpired(milliseconds)) {
/* When PXAT/EXAT absolute timestamp is specified, there can be a chance that timestamp
* has already elapsed so delete the key in that case. */
int deleted = dbGenericDelete(c->db, c->argv[1], server.lazyfree_lazy_expire, DB_FLAG_KEY_EXPIRED);
serverAssert(deleted);
robj *aux = server.lazyfree_lazy_expire ? shared.unlink : shared.del;
rewriteClientCommandVector(c,2,aux,c->argv[1]);
signalModifiedKey(c, c->db, c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC, "del", c->argv[1], c->db->id);
server.dirty++;
deleteExpiredKeyFromOverwriteAndPropagate(c, c->argv[1], 1);
} else if (expire) {
setExpire(c,c->db,c->argv[1],milliseconds);
/* Propagate as PXEXPIREAT millisecond-timestamp if there is
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/type/string.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,40 @@ if {[string match {*jemalloc*} [s mem_allocator]]} {
r set foo bar pxat [expr [clock milliseconds] + 10000]
assert_range [r ttl foo] 5 10
}

test "SET EXAT / PXAT Expiration time is expired" {
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
r debug set-active-expire 0
set repl [attach_to_replication_stream]

# Key exists.
r set foo bar
r set foo bar exat [expr [clock seconds] - 100]
assert_error {ERR no such key} {r debug object foo}
r set foo bar
r set foo bar pxat [expr [clock milliseconds] - 10000]
assert_error {ERR no such key} {r debug object foo}

# Key does not exist.
r del foo
r set foo bar exat [expr [clock seconds] - 100]
assert_error {ERR no such key} {r debug object foo}
r set foo bar pxat [expr [clock milliseconds] - 10000]
assert_error {ERR no such key} {r debug object foo}

r incr foo
assert_replication_stream $repl {
{select *}
{set foo bar}
{del foo}
{set foo bar}
{del foo}
{incr foo}
}

r debug set-active-expire 1
close_replication_stream $repl
} {} {needs:debug needs:repl}

test {Extended SET using multiple options at once} {
r set foo val
assert {[r set foo bar xx px 10000] eq {OK}}
Expand Down
Loading