From bad686568d5ed2d9cf8dc6ad22a4f6126113b3dc Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sun, 22 May 2022 23:19:35 +0800 Subject: [PATCH] fix: avoid invalid read/write --- source/libs/index/CMakeLists.txt | 6 +++--- source/libs/index/inc/indexInt.h | 2 ++ source/libs/index/src/index.c | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/source/libs/index/CMakeLists.txt b/source/libs/index/CMakeLists.txt index 7dc66e4789..d5fd574aad 100644 --- a/source/libs/index/CMakeLists.txt +++ b/source/libs/index/CMakeLists.txt @@ -31,7 +31,7 @@ if (${BUILD_WITH_INVERTEDINDEX}) endif(${BUILD_WITH_INVERTEDINDEX}) -if (${BUILD_TEST}) - add_subdirectory(test) -endif(${BUILD_TEST}) +#if (${BUILD_TEST}) +# add_subdirectory(test) +#endif(${BUILD_TEST}) diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 4bce792dd9..0bdcb131b6 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -58,6 +58,7 @@ struct SIndex { SIndexStat stat; TdThreadMutex mtx; + tsem_t sem; bool quit; }; @@ -70,6 +71,7 @@ struct SIndexOpts { int32_t cacheSize; // MB // add cache module later #endif + int32_t cacheOpt; // MB }; struct SIndexMultiTermQuery { diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index add59cb0b3..2141e90bbd 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -90,6 +90,15 @@ static void indexMergeCacheAndTFile(SArray* result, IterateValue* icache, Iterat // static int32_t indexSerialTermKey(SIndexTerm* itm, char* buf); // int32_t indexSerialKey(ICacheKey* key, char* buf); +static void indexPost(void* idx) { + SIndex* pIdx = idx; + tsem_post(&pIdx->sem); +} +static void indexWait(void* idx) { + SIndex* pIdx = idx; + tsem_wait(&pIdx->sem); +} + int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { taosThreadOnce(&isInit, indexInit); SIndex* sIdx = taosMemoryCalloc(1, sizeof(SIndex)); @@ -107,6 +116,8 @@ int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { sIdx->cVersion = 1; sIdx->path = tstrdup(path); taosThreadMutexInit(&sIdx->mtx, NULL); + tsem_init(&sIdx->sem, 0, 0); + // taosThreadCondInit(&sIdx->finished, NULL); sIdx->refId = indexAddRef(sIdx); indexAcquireRef(sIdx->refId); @@ -125,6 +136,7 @@ END: void indexDestroy(void* handle) { SIndex* sIdx = handle; taosThreadMutexDestroy(&sIdx->mtx); + tsem_destroy(&sIdx->sem); indexTFileDestroy(sIdx->tindex); taosMemoryFree(sIdx->path); taosMemoryFree(sIdx); @@ -138,7 +150,7 @@ void indexClose(SIndex* sIdx) { while (iter) { IndexCache** pCache = iter; indexCacheForceToMerge((void*)(*pCache)); - indexCacheWait((void*)(*pCache)); + indexWait((void*)(sIdx)); iter = taosHashIterate(sIdx->colObj, iter); indexCacheUnRef(*pCache); } @@ -461,7 +473,8 @@ int indexFlushCacheToTFile(SIndex* sIdx, void* cache) { indexCacheDestroyImm(pCache); tfileReaderUnRef(pReader); if (sIdx->quit) { - indexCacheBroadcast(pCache); + indexPost(sIdx); + // indexCacheBroadcast(pCache); } indexReleaseRef(sIdx->refId); return 0; @@ -509,9 +522,6 @@ int indexFlushCacheToTFile(SIndex* sIdx, void* cache) { indexDestroyFinalResult(result); indexCacheDestroyImm(pCache); - if (sIdx->quit) { - indexCacheBroadcast(pCache); - } indexCacheIteratorDestroy(cacheIter); tfileIteratorDestroy(tfileIter); @@ -525,6 +535,9 @@ int indexFlushCacheToTFile(SIndex* sIdx, void* cache) { } else { indexInfo("success to merge , time cost: %" PRId64 "ms", cost / 1000); } + if (sIdx->quit) { + indexPost(sIdx); + } indexReleaseRef(sIdx->refId); return ret; -- GitLab