From aafe746d90eb22683f632325ce1ddf22050a0f24 Mon Sep 17 00:00:00 2001 From: Ray Cao Date: Mon, 6 Jan 2025 18:59:50 +0800 Subject: [PATCH 1/2] add expired_keys statistic if the expiration time is already expired Signed-off-by: Ray Cao --- src/db.c | 3 ++- tests/unit/expire.tcl | 25 +++++++++++++++++++++++++ tests/unit/pubsub.tcl | 11 +++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/db.c b/src/db.c index 9a53e6b4d1..e84cc4526b 100644 --- a/src/db.c +++ b/src/db.c @@ -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. diff --git a/tests/unit/expire.tcl b/tests/unit/expire.tcl index e1dcc9203b..a34d403dc5 100644 --- a/tests/unit/expire.tcl +++ b/tests/unit/expire.tcl @@ -142,6 +142,31 @@ start_server {tags {"expire"}} { list $e $f } {somevalue {}} + test {EXPIRE / EXPIREAT / PEXPIRE / PEXPIREAT Expiration time is already expired} { + set prev_expired [s expired_keys] + r del x + + r set x somevalue + r expire x -1 + assert_equal {0} [r exists x] + assert_equal {1} [expr {[s expired_keys] - $prev_expired}] + + r set x somevalue + r expireat x [expr [clock seconds] - 1] + assert_equal {0} [r exists x] + assert_equal {2} [expr {[s expired_keys] - $prev_expired}] + + r set x somevalue + r pexpire x -1 + assert_equal {0} [r exists x] + assert_equal {3} [expr {[s expired_keys] - $prev_expired}] + + r set x somevalue + r pexpireat x [expr [clock milliseconds] - 1] + assert_equal {0} [r exists x] + assert_equal {4} [expr {[s expired_keys] - $prev_expired}] + } + test {TTL returns time to live in seconds} { r del x r setex x 10 somevalue diff --git a/tests/unit/pubsub.tcl b/tests/unit/pubsub.tcl index 24b78b6e5a..482052dc3d 100644 --- a/tests/unit/pubsub.tcl +++ b/tests/unit/pubsub.tcl @@ -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 0 + assert_equal "pmessage * __keyevent@${db}__:expired foo" [$rd1 read] + $rd1 close + } + test "Keyspace notifications: evicted events" { r config set notify-keyspace-events Ee r config set maxmemory-policy allkeys-lru From 11348fe51e7501b72916b19335d009a05eadc6e9 Mon Sep 17 00:00:00 2001 From: Ray Cao Date: Tue, 7 Jan 2025 15:38:33 +0800 Subject: [PATCH 2/2] make test more stable Signed-off-by: Ray Cao --- tests/unit/expire.tcl | 16 ++++++++-------- tests/unit/pubsub.tcl | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/unit/expire.tcl b/tests/unit/expire.tcl index a34d403dc5..3736538105 100644 --- a/tests/unit/expire.tcl +++ b/tests/unit/expire.tcl @@ -143,28 +143,28 @@ start_server {tags {"expire"}} { } {somevalue {}} test {EXPIRE / EXPIREAT / PEXPIRE / PEXPIREAT Expiration time is already expired} { - set prev_expired [s expired_keys] - r del x + r flushall + r config resetstat r set x somevalue r expire x -1 assert_equal {0} [r exists x] - assert_equal {1} [expr {[s expired_keys] - $prev_expired}] + 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} [expr {[s expired_keys] - $prev_expired}] + assert_equal {2} [s expired_keys] r set x somevalue - r pexpire x -1 + r pexpire x -1000 assert_equal {0} [r exists x] - assert_equal {3} [expr {[s expired_keys] - $prev_expired}] + assert_equal {3} [s expired_keys] r set x somevalue - r pexpireat x [expr [clock milliseconds] - 1] + r pexpireat x [expr [clock milliseconds] - 1000] assert_equal {0} [r exists x] - assert_equal {4} [expr {[s expired_keys] - $prev_expired}] + assert_equal {4} [s expired_keys] } test {TTL returns time to live in seconds} { diff --git a/tests/unit/pubsub.tcl b/tests/unit/pubsub.tcl index 482052dc3d..53b70bf552 100644 --- a/tests/unit/pubsub.tcl +++ b/tests/unit/pubsub.tcl @@ -431,7 +431,7 @@ start_server {tags {"pubsub network"}} { set rd1 [valkey_deferring_client] assert_equal {1} [psubscribe $rd1 *] r set foo 1 - r expire foo 0 + r expire foo -1 assert_equal "pmessage * __keyevent@${db}__:expired foo" [$rd1 read] $rd1 close }