From 4a742cb93b4b48a4cafa1ee011f032e630ae3830 Mon Sep 17 00:00:00 2001 From: Binbin Date: Thu, 8 Aug 2024 11:51:34 +0800 Subject: [PATCH] adding deleteExpiredKeyFromOverwriteAndPropagate Signed-off-by: Binbin --- src/cluster.c | 5 +---- src/db.c | 15 +++++++++++++++ src/expire.c | 12 +----------- src/server.h | 1 + src/t_string.c | 25 +++---------------------- 5 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 162004d703..bfed1de0a9 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -262,10 +262,7 @@ void restoreCommand(client *c) { if (ttl && !absttl) ttl += commandTimeSnapshot(); if (ttl && checkAlreadyExpired(ttl)) { if (deleted) { - robj *aux = server.lazyfree_lazy_server_del ? 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, 0); server.dirty++; } decrRefCount(obj); diff --git a/src/db.c b/src/db.c index d0a6640f57..ab0af1df69 100644 --- a/src/db.c +++ b/src/db.c @@ -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) { + 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 diff --git a/src/expire.c b/src/expire.c index 05abb9580a..f6a711b7db 100644 --- a/src/expire.c +++ b/src/expire.c @@ -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 { diff --git a/src/server.h b/src/server.h index ccdece20dd..996f6e4364 100644 --- a/src/server.h +++ b/src/server.h @@ -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); diff --git a/src/t_string.c b/src/t_string.c index f9a770ce89..c6e3ffc1c0 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -113,21 +113,8 @@ void setGenericCommand(client *c, * 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) { - 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. */ - robj *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); - } - - if (!(flags & OBJ_SET_GET)) { - addReply(c, ok_reply ? ok_reply : shared.ok); - } + if (found) deleteExpiredKeyFromOverwriteAndPropagate(c, key, 1); + if (!(flags & OBJ_SET_GET)) addReply(c, shared.ok); return; } @@ -417,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