From 52babbd1381cfe7b3e2e3d7ce74372434644545a Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Wed, 6 Apr 2022 19:59:07 +0800 Subject: [PATCH] [TS-510]: fix missing last_row results --- src/tsdb/src/tsdbRead.c | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 4f47d9d7b2..e559839e3e 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -189,6 +189,8 @@ static void* destroyTableCheckInfo(SArray* pTableCheckInfo); static bool tsdbGetExternalRow(TsdbQueryHandleT pHandle); static int32_t tsdbQueryTableList(STable* pTable, SArray* pRes, void* filterInfo); static STableBlockInfo* moveToNextDataBlockInCurrentFile(STsdbQueryHandle* pQueryHandle); +static bool initTableMemIterator(STsdbQueryHandle* pHandle, STableCheckInfo* pCheckInfo); +static SMemRow getSMemRowInTableMem(STableCheckInfo* pCheckInfo, int32_t order, int32_t update, SMemRow* extraRow); static void tsdbInitDataBlockLoadInfo(SDataBlockLoadInfo* pBlockLoadInfo) { pBlockLoadInfo->slot = -1; @@ -632,12 +634,53 @@ static int32_t lazyLoadCacheLast(STsdbQueryHandle* pQueryHandle) { if (pTable->cacheLastConfigVersion == pRepo->cacheLastConfigVersion) { continue; } + + if (!pCheckInfo->initBuf) { + initTableMemIterator(pQueryHandle, pCheckInfo); + } + code = tsdbLoadLastCache(pRepo, pTable); if (code != 0) { tsdbError("%p uid:%" PRId64 ", tid:%d, failed to load last cache since %s", pQueryHandle, pTable->tableId.uid, pTable->tableId.tid, tstrerror(terrno)); break; } + + STsdbCfg *pCfg = &pQueryHandle->pTsdb->config; + bool cacheLastRow = CACHE_LAST_ROW(&(pRepo->config)); + if (!cacheLastRow) continue; + + SMemRow row = getSMemRowInTableMem(pCheckInfo, pQueryHandle->order, pCfg->update, NULL); + if (row == NULL) continue; + TSKEY key = memRowKey(row); + + if (!pTable->lastRow) { + STSchema *pSchema = tsdbGetTableSchema(pTable); + SMemRow lastRow = taosTMalloc(memRowMaxBytesFromSchema(pSchema)); + if (lastRow == NULL) { + return TSDB_CODE_TDB_OUT_OF_MEMORY; + } + + memRowCpy(lastRow, row); + pTable->lastRow = lastRow; + pTable->lastKey = key; + } else { + TSKEY lastRowKey = memRowKey(pTable->lastRow); + if (key <= lastRowKey) continue; + + if (memRowTLen(pTable->lastRow) < memRowTLen(row)) { + SMemRow lastRow = taosTRealloc(pTable->lastRow, memRowTLen(row)); + if (lastRow == NULL) { + taosTZfree(pTable->lastRow); + pTable->lastRow = NULL; + + return TSDB_CODE_TDB_OUT_OF_MEMORY; + } + pTable->lastRow = lastRow; + } + memRowCpy(pTable->lastRow, row); + pTable->lastKey = key; + } } return code; -- GitLab