diff --git a/src/aof.cpp b/src/aof.cpp index 0a15204d0..35402cd25 100644 --- a/src/aof.cpp +++ b/src/aof.cpp @@ -1432,8 +1432,6 @@ int rewriteAppendOnlyFileRio(rio *aof) { initStaticStringObject(key,(sds)keystr); - expireEntry *pexpire = db->getExpire(&key); - /* Save the key and associated value */ if (o->type == OBJ_STRING) { /* Emit a SET command */ @@ -1458,6 +1456,8 @@ int rewriteAppendOnlyFileRio(rio *aof) { serverPanic("Unknown object type"); } /* Save the expire time */ + std::unique_lock ul(g_expireLock); + expireEntry *pexpire = db->getExpire(&key); if (pexpire != nullptr) { for (auto &subExpire : *pexpire) { if (subExpire.subkey() == nullptr) @@ -1476,6 +1476,8 @@ int rewriteAppendOnlyFileRio(rio *aof) { if (rioWriteBulkLongLong(aof,subExpire.when()) == 0) return false; // common } } + ul.unlock(); + /* Read some diff from the parent process from time to time. */ if (aof->processed_bytes > processed+AOF_READ_DIFF_INTERVAL_BYTES) { processed = aof->processed_bytes; diff --git a/src/cluster.cpp b/src/cluster.cpp index 389c6dd98..e0ddab80c 100644 --- a/src/cluster.cpp +++ b/src/cluster.cpp @@ -5324,6 +5324,7 @@ try_again: /* Create RESTORE payload and generate the protocol to call the command. */ for (j = 0; j < num_keys; j++) { long long ttl = 0; + std::unique_lock ul(g_expireLock); expireEntry *pexpire = c->db->getExpire(kv[j]); long long expireat = -1; if (pexpire != nullptr) diff --git a/src/db.cpp b/src/db.cpp index 156784634..f0df31f5a 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -1241,6 +1241,7 @@ void renameGenericCommand(client *c, int nx) { std::unique_ptr spexpire; { // scope pexpireOld since it will be invalid soon + std::unique_lock ul(g_expireLock); expireEntry *pexpireOld = c->db->getExpire(c->argv[1]); if (pexpireOld != nullptr) spexpire = std::make_unique(std::move(*pexpireOld)); @@ -1319,6 +1320,7 @@ void moveCommand(client *c) { std::unique_ptr spexpire; { // scope pexpireOld + std::unique_lock ul(g_expireLock); expireEntry *pexpireOld = c->db->getExpire(c->argv[1]); if (pexpireOld != nullptr) spexpire = std::make_unique(std::move(*pexpireOld)); @@ -1441,6 +1443,7 @@ int redisDbPersistentData::removeExpire(robj *key, dict_iter itr) { /* An expire may only be removed if there is a corresponding entry in the * main dict. Otherwise, the key will never be freed. */ serverAssertWithInfo(NULL,key,itr != nullptr); + std::unique_lock ul(g_expireLock); robj *val = itr.val(); if (!val->FExpires()) @@ -1457,7 +1460,8 @@ int redisDbPersistentData::removeExpire(robj *key, dict_iter itr) { int redisDbPersistentData::removeSubkeyExpire(robj *key, robj *subkey) { auto de = find(szFromObj(key)); serverAssertWithInfo(NULL,key,de != nullptr); - + std::unique_lock ul(g_expireLock); + robj *val = de.val(); if (!val->FExpires()) return 0; @@ -1489,6 +1493,7 @@ int redisDbPersistentData::removeSubkeyExpire(robj *key, robj *subkey) { void redisDbPersistentData::resortExpire(expireEntry &e) { + std::unique_lock ul(g_expireLock); auto itr = m_setexpire->find(e.key()); expireEntry eT = std::move(e); m_setexpire->erase(itr); @@ -1648,6 +1653,7 @@ void propagateSubkeyExpire(redisDb *db, int type, robj *key, robj *subkey) /* Check if the key is expired. Note, this does not check subexpires */ int keyIsExpired(const redisDbPersistentDataSnapshot *db, robj *key) { + std::unique_lock ul(g_expireLock); const expireEntry *pexpire = db->getExpire(key); mstime_t now; @@ -2273,6 +2279,7 @@ void redisDbPersistentData::clear(void(callback)(void*)) void redisDbPersistentData::setExpire(robj *key, robj *subkey, long long when) { /* Reuse the sds from the main dict in the expire dict */ + std::unique_lock ul(g_expireLock); dictEntry *kde = dictFind(m_pdict,ptrFromObj(key)); serverAssertWithInfo(NULL,key,kde != NULL); trackkey(key, true /* fUpdate */); @@ -2302,12 +2309,14 @@ void redisDbPersistentData::setExpire(robj *key, robj *subkey, long long when) void redisDbPersistentData::setExpire(expireEntry &&e) { + std::unique_lock ul(g_expireLock); trackkey(e.key(), true /* fUpdate */); m_setexpire->insert(e); } bool redisDb::FKeyExpires(const char *key) { + std::unique_lock ul(g_expireLock); return setexpireUnsafe()->find(key) != setexpire()->end(); } @@ -2327,6 +2336,7 @@ void redisDbPersistentData::ensure(const char *sdsKey, dictEntry **pde) { serverAssert(sdsKey != nullptr); serverAssert(FImplies(*pde != nullptr, dictGetVal(*pde) != nullptr)); // early versions set a NULL object, this is no longer valid + std::unique_lock ul(g_expireLock); // First see if the key can be obtained from a snapshot if (*pde == nullptr && m_pdbSnapshot != nullptr) diff --git a/src/debug.cpp b/src/debug.cpp index 9aebe8885..fc565d436 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -127,6 +127,7 @@ void mixStringObjectDigest(unsigned char *digest, robj_roptr o) { void xorObjectDigest(redisDb *db, robj_roptr keyobj, unsigned char *digest, robj_roptr o) { uint32_t aux = htonl(o->type); mixDigest(digest,&aux,sizeof(aux)); + std::unique_lock ul(g_expireLock); expireEntry *pexpire = db->getExpire(keyobj); long long expiretime = -1; char buf[128]; diff --git a/src/defrag.cpp b/src/defrag.cpp index e60705a3e..65aecfa34 100644 --- a/src/defrag.cpp +++ b/src/defrag.cpp @@ -811,6 +811,7 @@ long defragStream(redisDb *db, dictEntry *kde) { * all the various pointers it has. Returns a stat of how many pointers were * moved. */ long defragKey(redisDb *db, dictEntry *de) { + std::unique_lock ul(g_expireLock); sds keysds = (sds)dictGetKey(de); robj *newob, *ob; unsigned char *newzl; diff --git a/src/evict.cpp b/src/evict.cpp index a6dfd9ba9..da17314ea 100644 --- a/src/evict.cpp +++ b/src/evict.cpp @@ -262,6 +262,7 @@ int evictionPoolPopulate(int dbid, redisDb *db, expireset *setexpire, struct evi { if (setexpire != nullptr) { + std::unique_lock ul(g_expireLock); visitFunctor visitor { dbid, db->dictUnsafeKeyOnly(), pool, 0 }; setexpire->random_visit(visitor); return visitor.count; diff --git a/src/expire.cpp b/src/expire.cpp index 14c65449d..2aa398a76 100644 --- a/src/expire.cpp +++ b/src/expire.cpp @@ -33,6 +33,8 @@ #include "server.h" #include "cron.h" +fastlock g_expireLock {"Expire"}; + /* Helper function for the activeExpireCycle() function. * This function will try to expire the key that is stored in the hash table * entry 'de' of the 'expires' hash table of a Redis database. @@ -372,6 +374,7 @@ void activeExpireCycle(int type) { continue; } + std::unique_lock ul(g_expireLock); size_t expired = 0; size_t tried = 0; long long check = ACTIVE_EXPIRE_CYCLE_FAST_DURATION; // assume a check is roughly 1us. It isn't but good enough @@ -661,6 +664,7 @@ void ttlGenericCommand(client *c, int output_ms) { /* The key exists. Return -1 if it has no expire, or the actual * TTL value otherwise. */ + std::unique_lock ul(g_expireLock); expireEntry *pexpire = c->db->getExpire(c->argv[1]); if (c->argc == 2) { diff --git a/src/lazyfree.cpp b/src/lazyfree.cpp index 6bca4481b..7cb3efbd9 100644 --- a/src/lazyfree.cpp +++ b/src/lazyfree.cpp @@ -118,6 +118,7 @@ void freeObjAsync(robj *o) { * create a new empty set of hash tables and scheduling the old ones for * lazy freeing. */ void redisDbPersistentData::emptyDbAsync() { + std::unique_lock ul(g_expireLock); dict *oldht1 = m_pdict; auto *set = m_setexpire; m_setexpire = new (MALLOC_LOCAL) expireset(); diff --git a/src/module.cpp b/src/module.cpp index 4f79b9f95..a772c0cbb 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -2167,6 +2167,7 @@ int RM_UnlinkKey(RedisModuleKey *key) { * If no TTL is associated with the key or if the key is empty, * REDISMODULE_NO_EXPIRE is returned. */ mstime_t RM_GetExpire(RedisModuleKey *key) { + std::unique_lock ul(g_expireLock); expireEntry *pexpire = key->db->getExpire(key->key); mstime_t expire = -1; if (pexpire != nullptr) diff --git a/src/object.cpp b/src/object.cpp index 72ac1b961..a45a91db8 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -1078,8 +1078,10 @@ struct redisMemOverhead *getMemoryOverheadData(void) { db->size() * sizeof(robj); mh->db[mh->num_dbs].overhead_ht_main = mem; mem_total+=mem; - + + std::unique_lock ul(g_expireLock); mem = db->setexpire()->bytes_used(); + mh->db[mh->num_dbs].overhead_ht_expires = mem; mem_total+=mem; diff --git a/src/rdb.cpp b/src/rdb.cpp index befb43430..4feb1d6a7 100644 --- a/src/rdb.cpp +++ b/src/rdb.cpp @@ -1150,8 +1150,11 @@ int saveKey(rio *rdb, const redisDbPersistentDataSnapshot *db, int flags, size_t robj key; initStaticStringObject(key,(char*)keystr); + std::unique_lock ul(g_expireLock); const expireEntry *pexpire = db->getExpire(&key); serverAssert((o->FExpires() && pexpire != nullptr) || (!o->FExpires() && pexpire == nullptr)); + if (pexpire == nullptr) + ul.unlock(); // no need to hold the lock if we're not saving the expire if (rdbSaveKeyValuePair(rdb,&key,o,pexpire) == -1) return 0; diff --git a/src/server.cpp b/src/server.cpp index f5f2a93bd..ce817947d 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2853,6 +2853,7 @@ bool getCommandAsync(client *c) } // Are we expired? + std::unique_lock ul(g_expireLock); const expireEntry *expire = serverTL->rgdbSnapshot[idb]->getExpire(c->argv[1]); long long when; if (expire && expire->FGetPrimaryExpire(&when) && when > 0) { diff --git a/src/server.h b/src/server.h index ad15c319a..8e21f6aa5 100644 --- a/src/server.h +++ b/src/server.h @@ -1171,6 +1171,7 @@ public: explicit operator long long() const noexcept { return when(); } }; typedef semiorderedset expireset; +extern fastlock g_expireLock; /* The a string name for an object's type as listed above * Native types are checked against the OBJ_STRING, OBJ_LIST, OBJ_* defines, diff --git a/src/snapshot.cpp b/src/snapshot.cpp index cb65eb30a..f863a2b1d 100644 --- a/src/snapshot.cpp +++ b/src/snapshot.cpp @@ -49,8 +49,10 @@ const redisDbPersistentDataSnapshot *redisDbPersistentData::createSnapshot(uint6 dictForceRehash(m_spdbSnapshotHOLDER->m_pdictTombstone); dictMerge(m_pdbSnapshot->m_pdict, m_pdict); dictEmpty(m_pdictTombstone, nullptr); - delete m_spdbSnapshotHOLDER->m_setexpire; - m_spdbSnapshotHOLDER->m_setexpire = new (MALLOC_LOCAL) expireset(*m_setexpire); + { + std::unique_lock ul(g_expireLock); + (*m_spdbSnapshotHOLDER->m_setexpire) = *m_setexpire; + } m_pdbSnapshotASYNC = nullptr; serverAssert(m_pdbSnapshot->m_pdict->iterators == 1); @@ -79,6 +81,7 @@ const redisDbPersistentDataSnapshot *redisDbPersistentData::createSnapshot(uint6 spdb->m_mvccCheckpoint = getMvccTstamp(); if (m_setexpire != nullptr) { + std::unique_lock ul(g_expireLock); spdb->m_setexpire = new (MALLOC_LOCAL) expireset(*m_setexpire); spdb->m_setexpire->pause_rehash(); // needs to be const } @@ -158,8 +161,11 @@ void redisDbPersistentData::restoreSnapshot(const redisDbPersistentDataSnapshot size_t expectedSize = psnapshot->size(); dictEmpty(m_pdict, nullptr); dictEmpty(m_pdictTombstone, nullptr); + { + std::unique_lock ul(g_expireLock); delete m_setexpire; m_setexpire = new (MALLOC_LOCAL) expireset(*psnapshot->m_setexpire); + } endSnapshot(psnapshot); serverAssert(size() == expectedSize); } @@ -504,6 +510,11 @@ void redisDbPersistentDataSnapshot::consolidate_children(redisDbPersistentData * return true; }, true /*fKeyOnly*/, true /*fCacheOnly*/); spdb->m_spstorage = m_pdbSnapshot->m_spstorage; + { + std::unique_lock ul(g_expireLock); + delete spdb->m_setexpire; + spdb->m_setexpire = new (MALLOC_LOCAL) expireset(*m_pdbSnapshot->m_setexpire); + } spdb->m_pdict->iterators++;