Skip to content

Commit

Permalink
Incr expired_keys if the unix-time is already expired for EXPIREAT an…
Browse files Browse the repository at this point in the history
…d other commands(valkey-io#1517)

Some commands that use unix-time, such as `EXPIREAT` and `SET EXAT`, should include the deleted keys in the `expired_keys` statistics if the specified time has already expired, and notifications should be sent in the manner of expired.

---------

Signed-off-by: Ray Cao <[email protected]>
  • Loading branch information
RayaCoo authored and proost committed Jan 17, 2025
1 parent f752df6 commit 50d6d24
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,8 @@ void deleteExpiredKeyFromOverwriteAndPropagate(client *c, robj *keyobj) {
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);
notifyKeyspaceEvent(NOTIFY_EXPIRED, "expired", keyobj, c->db->id);
server.stat_expiredkeys++;
}

/* Propagate an implicit key deletion into replicas and the AOF file.
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/expire.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ start_server {tags {"expire"}} {
list $e $f
} {somevalue {}}

test {EXPIRE / EXPIREAT / PEXPIRE / PEXPIREAT Expiration time is already expired} {
r flushall
r config resetstat

r set x somevalue
r expire x -1
assert_equal {0} [r exists x]
assert_equal {1} [s expired_keys]

r set x somevalue
r expireat x [expr [clock seconds] - 1]
assert_equal {0} [r exists x]
assert_equal {2} [s expired_keys]

r set x somevalue
r pexpire x -1000
assert_equal {0} [r exists x]
assert_equal {3} [s expired_keys]

r set x somevalue
r pexpireat x [expr [clock milliseconds] - 1000]
assert_equal {0} [r exists x]
assert_equal {4} [s expired_keys]
}

test {TTL returns time to live in seconds} {
r del x
r setex x 10 somevalue
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/pubsub.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ start_server {tags {"pubsub network"}} {
$rd1 close
}

test "Keyspace notification: expired event (Expiration time is already expired)" {
r config set notify-keyspace-events Ex
r del foo
set rd1 [valkey_deferring_client]
assert_equal {1} [psubscribe $rd1 *]
r set foo 1
r expire foo -1
assert_equal "pmessage * __keyevent@${db}__:expired foo" [$rd1 read]
$rd1 close
}

test "Keyspace notifications: lazy expired events" {
# disable active expire
r debug set-active-expire 0
Expand Down

0 comments on commit 50d6d24

Please sign in to comment.