From d2ea41e0300e8b47789c336c853b3228ad0f6e26 Mon Sep 17 00:00:00 2001 From: Shungang Li Date: Thu, 15 Aug 2024 13:48:49 +0800 Subject: [PATCH] fix: ttlMgrFlush endless loop --- source/dnode/vnode/src/meta/metaTtl.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTtl.c b/source/dnode/vnode/src/meta/metaTtl.c index 6b203b146d..b5436af2bf 100644 --- a/source/dnode/vnode/src/meta/metaTtl.c +++ b/source/dnode/vnode/src/meta/metaTtl.c @@ -402,15 +402,20 @@ int32_t ttlMgrFlush(STtlManger *pTtlMgr, TXN *pTxn) { int32_t code = TSDB_CODE_SUCCESS; - void *pIter = taosHashIterate(pTtlMgr->pDirtyUids, NULL); - while (pIter != NULL) { + void *pIter = NULL; + while ((pIter = taosHashIterate(pTtlMgr->pDirtyUids, pIter)) != NULL) { STtlDirtyEntry *pEntry = (STtlDirtyEntry *)pIter; tb_uid_t *pUid = taosHashGetKey(pIter, NULL); STtlCacheEntry *cacheEntry = taosHashGet(pTtlMgr->pTtlCache, pUid, sizeof(*pUid)); if (cacheEntry == NULL) { - metaError("%s, ttlMgr flush failed to get ttl cache, uid: %" PRId64 ", type: %d", pTtlMgr->logPrefix, *pUid, - pEntry->type); + metaInfo("%s, ttlMgr flush failed to get ttl cache, might be restoring, uid: %" PRId64 ", type: %d", + pTtlMgr->logPrefix, *pUid, pEntry->type); + code = taosHashRemove(pTtlMgr->pDirtyUids, pUid, sizeof(*pUid)); + if (TSDB_CODE_SUCCESS != code) { + metaError("%s, ttlMgr flush failed to remove dirty uid since %s", pTtlMgr->logPrefix, tstrerror(code)); + goto _out; + } continue; } @@ -422,9 +427,9 @@ int32_t ttlMgrFlush(STtlManger *pTtlMgr, TXN *pTxn) { if (pEntry->type == ENTRY_TYPE_UPSERT) { // delete old key & upsert new key - (void)tdbTbDelete(pTtlMgr->pTtlIdx, &ttlKey, sizeof(ttlKey), pTxn); // maybe first insert, ignore error + (void)tdbTbDelete(pTtlMgr->pTtlIdx, &ttlKey, sizeof(ttlKey), pTxn); // maybe first insert, ignore error code = tdbTbUpsert(pTtlMgr->pTtlIdx, &ttlKeyDirty, sizeof(ttlKeyDirty), &cacheEntry->ttlDaysDirty, - sizeof(cacheEntry->ttlDaysDirty), pTxn); + sizeof(cacheEntry->ttlDaysDirty), pTxn); if (TSDB_CODE_SUCCESS != code) { metaError("%s, ttlMgr flush failed to upsert since %s", pTtlMgr->logPrefix, tstrerror(code)); goto _out; @@ -449,9 +454,11 @@ int32_t ttlMgrFlush(STtlManger *pTtlMgr, TXN *pTxn) { goto _out; } - void *pIterTmp = pIter; - pIter = taosHashIterate(pTtlMgr->pDirtyUids, pIterTmp); - (void)taosHashRemove(pTtlMgr->pDirtyUids, pUid, sizeof(tb_uid_t)); + code = taosHashRemove(pTtlMgr->pDirtyUids, pUid, sizeof(*pUid)); + if (TSDB_CODE_SUCCESS != code) { + metaError("%s, ttlMgr flush failed to remove dirty uid since %s", pTtlMgr->logPrefix, tstrerror(code)); + goto _out; + } } taosHashClear(pTtlMgr->pDirtyUids); @@ -459,6 +466,8 @@ int32_t ttlMgrFlush(STtlManger *pTtlMgr, TXN *pTxn) { code = TSDB_CODE_SUCCESS; _out: + taosHashCancelIterate(pTtlMgr->pDirtyUids, pIter); + endNs = taosGetTimestampNs(); metaTrace("%s, ttl mgr flush end, time consumed: %" PRId64 " ns", pTtlMgr->logPrefix, endNs - startNs);