From 299fe0ae90bbc90da1f0ee8f54076e429f58cfbb Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 10 Nov 2025 15:45:50 +0800 Subject: [PATCH] fix(query): release after opening iter completed. --- source/dnode/vnode/src/tsdb/tsdbMergeTree.c | 52 ++++++++++++--------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMergeTree.c b/source/dnode/vnode/src/tsdb/tsdbMergeTree.c index c527babb702..42a7fb4e4a1 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMergeTree.c +++ b/source/dnode/vnode/src/tsdb/tsdbMergeTree.c @@ -48,8 +48,8 @@ static void clearStatisInfoCache(SLRUCache *pCache, SSttStatisCacheKey *pKey) static int32_t putStatisInfoIntoCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue *pValue, const char *id); static int32_t getStatisInfoFromCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue **pValue, - const char *id); - + LRUHandle **pHandle, const char *id); +static void releaseCacheHandle(SLRUCache* pCache, LRUHandle** pHandle); static void tLDataIterClose2(SLDataIter *pIter); // SLDataIter ================================================= @@ -1002,6 +1002,7 @@ int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoF int32_t lino = 0; int32_t numOfLevels = pFset->lvlArr->size; SSttStatisCacheValue* pValue = NULL; + LRUHandle* pHandle = NULL; SSttStatisCacheKey key = {.suid = pConf->suid, .fid = pFset->fid, .vgId = TD_VID(pConf->pTsdb->pVnode)}; (void)taosThreadOnce(&tsCacheInit, initTableRowsInfoCache); @@ -1029,15 +1030,16 @@ int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoF } if (pConf->cacheStatis) { - int32_t ret = getStatisInfoFromCache(statisCacheInfo.pStatisFileCache, &key, &pValue, pConf->idstr); - if (ret == TSDB_CODE_SUCCESS) { // use cached statis info + int32_t ret = getStatisInfoFromCache(statisCacheInfo.pStatisFileCache, &key, &pValue, &pHandle, pConf->idstr); + if (ret == TSDB_CODE_SUCCESS) { // use cached statis info if (pValue->commitTs == pFset->lastCommit) { loadStatisFromDisk = false; - } else { + } else { // release the handle ref, and then remove it from lru cache + releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle); clearStatisInfoCache(statisCacheInfo.pStatisFileCache, &key); - tsdbDebug( + tsdbInfo( "cache expired since new commit occurs, remove the cache and load from disk, vgId:%d, fid:%d, suid:%" PRId64 - ", commitTs:%" PRId64 ", new commitTs:%" PRId64 , + ", commitTs:%" PRId64 ", new commitTs:%" PRId64, key.vgId, key.fid, key.suid, pValue->commitTs, pFset->lastCommit); } } @@ -1122,19 +1124,17 @@ int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoF code = buildSttTableRowsInfoKV(pConf, TD_VID(pConf->pTsdb->pVnode), &k, &pVal); if (code == TSDB_CODE_SUCCESS) { - // SSttStatisCacheValue *vx = NULL; - // int32_t ret = getStatisInfoFromCache(statisCacheInfo.pStatisFileCache, &k, &vx, pConf->idstr); - // if (ret != 0) { - code = putStatisInfoIntoCache(statisCacheInfo.pStatisFileCache, &k, pVal, pConf->idstr); - // } else { - // taosMemoryFree(pVal); // already exists - // } + code = putStatisInfoIntoCache(statisCacheInfo.pStatisFileCache, &k, pVal, pConf->idstr); } } return code; _end: + if (pHandle != NULL && pConf->cacheStatis) { + releaseCacheHandle(statisCacheInfo.pStatisFileCache, &pHandle); + } + tMergeTreeClose(pMTree); return code; } @@ -1309,30 +1309,38 @@ int32_t buildSttTableRowsInfoKV(SMergeTreeConf *pConf, int32_t vgId, SSttStatisC return TSDB_CODE_SUCCESS; } -int32_t getStatisInfoFromCache(SLRUCache* pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue **pValue, - const char *id) { +int32_t getStatisInfoFromCache(SLRUCache *pCache, SSttStatisCacheKey *pKey, SSttStatisCacheValue **pValue, + LRUHandle **pHandle, const char *id) { *pValue = NULL; + *pHandle = NULL; - (void) taosThreadMutexLock(&statisCacheInfo.lock); - LRUHandle *pHandle = taosLRUCacheLookup(pCache, pKey, sizeof(SSttStatisCacheKey)); - if (pHandle == NULL) { + (void)taosThreadMutexLock(&statisCacheInfo.lock); + LRUHandle *pItemHandle = taosLRUCacheLookup(pCache, pKey, sizeof(SSttStatisCacheKey)); + if (pItemHandle == NULL) { (void)taosThreadMutexUnlock(&statisCacheInfo.lock); return TSDB_CODE_NOT_FOUND; } - void* p = taosLRUCacheValue(pCache, pHandle); + void *p = taosLRUCacheValue(pCache, pItemHandle); + *pValue = p; + *pHandle = pItemHandle; tsdbDebug("get statis info from cache suid:%" PRId64 ", vgId:%d, fid:%d, %s, commitTs:%" PRId64, pKey->suid, pKey->vgId, pKey->fid, id, (*pValue)->commitTs); - bool ret = taosLRUCacheRelease(pCache, pHandle, false); - // (*pEntry)->hitTimes += 1; (void)taosThreadMutexUnlock(&statisCacheInfo.lock); return TSDB_CODE_SUCCESS; } +void releaseCacheHandle(SLRUCache* pCache, LRUHandle** pHandle) { + (void) taosThreadMutexLock(&statisCacheInfo.lock); + bool ret = taosLRUCacheRelease(pCache, *pHandle, false); + *pHandle = NULL; + (void)taosThreadMutexUnlock(&statisCacheInfo.lock); +} + static void freeStatisFileItems(const void* key, size_t keyLen, void* value, void* ud) { (void)ud;