From ca559819f7dcd97ba9ef667bf38360a9527d62f6 Mon Sep 17 00:00:00 2001 From: menwen Date: Thu, 5 Aug 2021 16:09:24 +0800 Subject: [PATCH] Add latency monitor sample when key is deleted via lazy expire (#9317) Fix that there is no sample latency after the key expires via expireIfNeeded(). Some refactoring for shared code. --- src/db.c | 28 ++++++++++++++++++---------- src/expire.c | 15 +-------------- src/server.h | 1 + 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/db.c b/src/db.c index 3926bd33f..2e3009d7a 100644 --- a/src/db.c +++ b/src/db.c @@ -30,6 +30,7 @@ #include "server.h" #include "cluster.h" #include "atomicvar.h" +#include "latency.h" #include #include @@ -1437,6 +1438,22 @@ long long getExpire(redisDb *db, robj *key) { return dictGetSignedIntegerVal(de); } +/* Delete the specified expired key and propagate expire. */ +void deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj) { + mstime_t expire_latency; + latencyStartMonitor(expire_latency); + if (server.lazyfree_lazy_expire) + dbAsyncDelete(db,keyobj); + else + dbSyncDelete(db,keyobj); + latencyEndMonitor(expire_latency); + latencyAddSampleIfNeeded("expire-del",expire_latency); + notifyKeyspaceEvent(NOTIFY_EXPIRED,"expired",keyobj,db->id); + signalModifiedKey(NULL, db, keyobj); + propagateExpire(db,keyobj,server.lazyfree_lazy_expire); + server.stat_expiredkeys++; +} + /* Propagate expires into slaves and the AOF file. * When a key expires in the master, a DEL operation for this key is sent * to all the slaves and the AOF file if enabled. @@ -1541,16 +1558,7 @@ int expireIfNeeded(redisDb *db, robj *key) { if (checkClientPauseTimeoutAndReturnIfPaused()) return 1; /* Delete the key */ - if (server.lazyfree_lazy_expire) { - dbAsyncDelete(db,key); - } else { - dbSyncDelete(db,key); - } - server.stat_expiredkeys++; - propagateExpire(db,key,server.lazyfree_lazy_expire); - notifyKeyspaceEvent(NOTIFY_EXPIRED, - "expired",key,db->id); - signalModifiedKey(NULL,db,key); + deleteExpiredKeyAndPropagate(db,key); return 1; } diff --git a/src/expire.c b/src/expire.c index 858c13549..798651255 100644 --- a/src/expire.c +++ b/src/expire.c @@ -53,24 +53,11 @@ * to the function to avoid too many gettimeofday() syscalls. */ int activeExpireCycleTryExpire(redisDb *db, dictEntry *de, long long now) { long long t = dictGetSignedIntegerVal(de); - mstime_t expire_latency; if (now > t) { sds key = dictGetKey(de); robj *keyobj = createStringObject(key,sdslen(key)); - - propagateExpire(db,keyobj,server.lazyfree_lazy_expire); - latencyStartMonitor(expire_latency); - if (server.lazyfree_lazy_expire) - dbAsyncDelete(db,keyobj); - else - dbSyncDelete(db,keyobj); - latencyEndMonitor(expire_latency); - latencyAddSampleIfNeeded("expire-del",expire_latency); - notifyKeyspaceEvent(NOTIFY_EXPIRED, - "expired",keyobj,db->id); - signalModifiedKey(NULL, db, keyobj); + deleteExpiredKeyAndPropagate(db,keyobj); decrRefCount(keyobj); - server.stat_expiredkeys++; return 1; } else { return 0; diff --git a/src/server.h b/src/server.h index 5516dab29..dbef3c2de 100644 --- a/src/server.h +++ b/src/server.h @@ -2377,6 +2377,7 @@ void initConfigValues(); /* db.c -- Keyspace access API */ int removeExpire(redisDb *db, robj *key); +void deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj); void propagateExpire(redisDb *db, robj *key, int lazy); int keyIsExpired(redisDb *db, robj *key); int expireIfNeeded(redisDb *db, robj *key);