From 175d4c55bd27c09d28541697148d9c91914201cc Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 28 Sep 2020 18:01:20 +0800 Subject: [PATCH] [td-225]add handle expired check --- src/query/src/qExecutor.c | 1 + src/util/inc/tcache.h | 4 ++++ src/util/src/tcache.c | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index d48d7d5ea1..c61e05551a 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6986,6 +6986,7 @@ void* qOpenQueryMgmt(int32_t vgId) { pthread_mutex_init(&pQueryMgmt->lock, NULL); + taosCacheCheckforExpire(pQueryMgmt->qinfoPool, 600); qDebug("vgId:%d, open querymgmt success", vgId); return pQueryMgmt; } diff --git a/src/util/inc/tcache.h b/src/util/inc/tcache.h index 3d6f9705ee..b37df100eb 100644 --- a/src/util/inc/tcache.h +++ b/src/util/inc/tcache.h @@ -75,6 +75,7 @@ typedef struct { uint8_t deleting; // set the deleting flag to stop refreshing ASAP. pthread_t refreshWorker; bool extendLifespan; // auto extend life span when one item is accessed. + int32_t itemExpireTime; // maximum live time in ms in cache for object, if -1, mean never expired. #if defined(LINUX) pthread_rwlock_t lock; #else @@ -169,6 +170,9 @@ void taosCacheCleanup(SCacheObj *pCacheObj); */ void taosCacheRefresh(SCacheObj *pCacheObj, __cache_free_fn_t fp); +// check the query handle not released, debug purpose only +void taosCacheCheckforExpire(SCacheObj *pCacheObj, int32_t expireTimeSec); + #ifdef __cplusplus } #endif diff --git a/src/util/src/tcache.c b/src/util/src/tcache.c index e5526647cb..ff2ae1d5e6 100644 --- a/src/util/src/tcache.c +++ b/src/util/src/tcache.c @@ -170,6 +170,7 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext pCacheObj->freeFp = fn; pCacheObj->refreshTime = refreshTimeInSeconds * 1000; pCacheObj->extendLifespan = extendLifespan; + pCacheObj->itemExpireTime = -1; if (__cache_lock_init(pCacheObj) != 0) { taosHashCleanup(pCacheObj->pHashTable); @@ -521,6 +522,8 @@ void taosAddToTrash(SCacheObj *pCacheObj, SCacheDataNode *pNode) { } void taosTrashCanEmpty(SCacheObj *pCacheObj, bool force) { + int64_t now = taosGetTimestampMs(); + __cache_wr_lock(pCacheObj); if (pCacheObj->numOfElemsInTrash == 0) { @@ -551,6 +554,12 @@ void taosTrashCanEmpty(SCacheObj *pCacheObj, bool force) { doRemoveElemInTrashcan(pCacheObj, p); doDestroyTrashcanElem(pCacheObj, p); } else { + // any item should not live longer than the specificed expired time + assert(now >= pElem->pData->addedTime); + if (pCacheObj->itemExpireTime > 0 && ((now - pElem->pData->addedTime) > pCacheObj->itemExpireTime)) { + assert(0); + } + pElem = pElem->next; } } @@ -653,3 +662,15 @@ void taosCacheRefresh(SCacheObj *pCacheObj, __cache_free_fn_t fp) { int64_t now = taosGetTimestampMs(); doCacheRefresh(pCacheObj, now, fp); } + +void taosCacheCheckforExpire(SCacheObj *pCacheObj, int32_t expireTimeSec) { + if (pCacheObj == NULL || expireTimeSec < 0) { + return; + } + + if (expireTimeSec < pCacheObj->refreshTime) { + expireTimeSec = pCacheObj->refreshTime * 20; + } + + pCacheObj->itemExpireTime = expireTimeSec*1000; +} -- GitLab