From d067104ec1aade4fd2540f778b87f4fb26ef8aab Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 25 May 2022 20:38:25 +0800 Subject: [PATCH] fix: index memory error --- source/libs/index/src/index.c | 6 +++--- source/libs/index/src/indexTfile.c | 14 ++++++++++---- source/libs/index/test/indexTests.cc | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 5d44c063a5..1faf1decb4 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -558,17 +558,17 @@ static int64_t indexGetAvaialbleVer(SIndex* sIdx, IndexCache* cache) { ICacheKey key = {.suid = cache->suid, .colName = cache->colName, .nColName = strlen(cache->colName)}; int64_t ver = CACHE_VERSION(cache); - TFileReader* rd = tfileCacheGet(((IndexTFile*)sIdx->tindex)->cache, &key); - IndexTFile* tf = (IndexTFile*)(sIdx->tindex); + IndexTFile* tf = (IndexTFile*)(sIdx->tindex); taosThreadMutexLock(&tf->mtx); - tfileCacheGet(tf->cache, &key); + TFileReader* rd = tfileCacheGet(tf->cache, &key); taosThreadMutexUnlock(&tf->mtx); if (rd != NULL) { ver += MAX(ver, rd->header.version) + 1; indexInfo("header: %" PRId64 ", ver: %" PRId64 "", rd->header.version, ver); } + tfileReaderUnRef(rd); return ver; } static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) { diff --git a/source/libs/index/src/indexTfile.c b/source/libs/index/src/indexTfile.c index 73ef22faed..3de556e8b5 100644 --- a/source/libs/index/src/indexTfile.c +++ b/source/libs/index/src/indexTfile.c @@ -165,11 +165,11 @@ void tfileCachePut(TFileCache* tcache, ICacheKey* key, TFileReader* reader) { // remove last version index reader TFileReader** p = taosHashGet(tcache->tableCache, buf, sz); if (p != NULL && *p != NULL) { - TFileReader* oldReader = *p; + TFileReader* oldRdr = *p; taosHashRemove(tcache->tableCache, buf, sz); - indexInfo("found %s, remove file %s", buf, oldReader->ctx->file.buf); - oldReader->remove = true; - tfileReaderUnRef(oldReader); + indexInfo("found %s, should remove file %s", buf, oldRdr->ctx->file.buf); + oldRdr->remove = true; + tfileReaderUnRef(oldRdr); } taosHashPut(tcache->tableCache, buf, sz, &reader, sizeof(void*)); tfileReaderRef(reader); @@ -212,6 +212,12 @@ void tfileReaderDestroy(TFileReader* reader) { // T_REF_INC(reader); fstDestroy(reader->fst); writerCtxDestroy(reader->ctx, reader->remove); + if (reader->remove) { + indexInfo("%s is removed", reader->ctx->file.buf); + } else { + indexInfo("%s is not removed", reader->ctx->file.buf); + } + taosMemoryFree(reader); } static int32_t tfSearchTerm(void* reader, SIndexTerm* tem, SIdxTempResult* tr) { diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index a8e555c5b9..2d06002af8 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -953,8 +953,8 @@ TEST_F(IndexEnv2, testIndex_TrigeFlush) { } static void single_write_and_search(IndexObj* idx) { - int target = idx->SearchOne("tag1", "Hello"); - target = idx->SearchOne("tag2", "Test"); + // int target = idx->SearchOne("tag1", "Hello"); + // target = idx->SearchOne("tag2", "Test"); } static void multi_write_and_search(IndexObj* idx) { idx->PutOne("tag1", "Hello"); -- GitLab