From 89c81042389b568186f0a5af28f406e26ae50c69 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 31 Jul 2020 10:59:57 +0800 Subject: [PATCH] add lock when read --- src/tsdb/src/tsdbRead.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 3a89ead4a8..4494b86209 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -555,17 +555,6 @@ static int32_t binarySearchForBlock(SCompBlock* pBlock, int32_t numOfBlocks, TSK } static int32_t getFileCompInfo(STsdbQueryHandle* pQueryHandle, int32_t* numOfBlocks) { - SFileGroup* fileGroup = pQueryHandle->pFileGroup; - assert(fileGroup->files[TSDB_FILE_TYPE_HEAD].fname > 0); - - if (tsdbSetAndOpenHelperFile(&pQueryHandle->rhelper, fileGroup) < 0) { - return terrno; - } - - if (tsdbLoadCompIdx(&pQueryHandle->rhelper, NULL) < 0) { - return terrno; - } - // load all the comp offset value for all tables in this file *numOfBlocks = 0; size_t numOfTables = taosArrayGetSize(pQueryHandle->pTableCheckInfo); @@ -1462,12 +1451,20 @@ static int32_t getDataBlocksInFilesImpl(STsdbQueryHandle* pQueryHandle, bool* ex STsdbCfg* pCfg = &pQueryHandle->pTsdb->config; STimeWindow win = TSWINDOW_INITIALIZER; - while ((pQueryHandle->pFileGroup = tsdbGetFileGroupNext(&pQueryHandle->fileIter)) != NULL) { + while (true) { + pthread_rwlock_rdlock(&pQueryHandle->pTsdb->tsdbFileH->fhlock); + + if ((pQueryHandle->pFileGroup = tsdbGetFileGroupNext(&pQueryHandle->fileIter)) == NULL) { + pthread_rwlock_unlock(&pQueryHandle->pTsdb->tsdbFileH->fhlock); + break; + } + tsdbGetFidKeyRange(pCfg->daysPerFile, pCfg->precision, pQueryHandle->pFileGroup->fileId, &win.skey, &win.ekey); // current file are not overlapped with query time window, ignore remain files if ((ASCENDING_TRAVERSE(pQueryHandle->order) && win.skey > pQueryHandle->window.ekey) || (!ASCENDING_TRAVERSE(pQueryHandle->order) && win.ekey < pQueryHandle->window.ekey)) { + pthread_rwlock_unlock(&pQueryHandle->pTsdb->tsdbFileH->fhlock); tsdbDebug("%p remain files are not qualified for qrange:%" PRId64 "-%" PRId64 ", ignore, %p", pQueryHandle, pQueryHandle->window.skey, pQueryHandle->window.ekey, pQueryHandle->qinfo); pQueryHandle->pFileGroup = NULL; @@ -1475,6 +1472,19 @@ static int32_t getDataBlocksInFilesImpl(STsdbQueryHandle* pQueryHandle, bool* ex break; } + if (tsdbSetAndOpenHelperFile(&pQueryHandle->rhelper, pQueryHandle->pFileGroup) < 0) { + pthread_rwlock_unlock(&pQueryHandle->pTsdb->tsdbFileH->fhlock); + code = terrno; + break; + } + + pthread_rwlock_unlock(&pQueryHandle->pTsdb->tsdbFileH->fhlock); + + if (tsdbLoadCompIdx(&pQueryHandle->rhelper, NULL) < 0) { + code = terrno; + break; + } + if ((code = getFileCompInfo(pQueryHandle, &numOfBlocks)) != TSDB_CODE_SUCCESS) { break; } -- GitLab