• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.94
/source/dnode/vnode/src/tsdb/tsdbMergeTree.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdb.h"
17
#include "tsdbFSet2.h"
18
#include "tsdbMerge.h"
19
#include "tsdbReadUtil.h"
20
#include "tsdbSttFileRW.h"
21
#include "tsdbUtil2.h"
22

23
static void tLDataIterClose2(SLDataIter *pIter);
24

25
// SLDataIter =================================================
26
int32_t tCreateSttBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols, SSttBlockLoadInfo **pInfo) {
6,867,933✔
27
  *pInfo = NULL;
6,867,933✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
6,867,933✔
30
  if (pLoadInfo == NULL) {
6,871,844!
UNCOV
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
6,871,844✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
6,871,844✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
6,871,844✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
6,871,844✔
40
  if (code) {
6,870,953!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
6,870,953✔
46
  if (code) {
6,872,481!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
6,872,481✔
52
  if (pLoadInfo->aSttBlk == NULL) {
6,871,872!
UNCOV
53
    taosMemoryFreeClear(pLoadInfo);
×
UNCOV
54
    return terrno;
×
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
6,872,331✔
58
  pLoadInfo->colIds = colList;
6,872,331✔
59
  pLoadInfo->numOfCols = numOfCols;
6,872,331✔
60

61
  *pInfo = pLoadInfo;
6,872,331✔
62
  return code;
6,872,331✔
63
}
64

65
static void freeItem(void *pValue) {
38,330,154✔
66
  SValue *p = (SValue *)pValue;
38,330,154✔
67
  if (IS_VAR_DATA_TYPE(p->type)) {
38,330,154!
68
    taosMemoryFree(p->pData);
530,504✔
69
  }
70
}
38,347,997✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
6,875,156✔
73
  if (pLoadInfo == NULL) {
6,875,156!
UNCOV
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
6,875,156✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
6,875,156✔
80
  tBlockDataDestroy(&pInfo->data);
6,875,156✔
81
  pInfo->sttBlockIndex = -1;
6,875,049✔
82
  pInfo->pin = false;
6,875,049✔
83

84
  pInfo = &pLoadInfo->blockData[1];
6,875,049✔
85
  tBlockDataDestroy(&pInfo->data);
6,875,049✔
86
  pInfo->sttBlockIndex = -1;
6,874,601✔
87
  pInfo->pin = false;
6,874,601✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
6,874,601✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
6,873,973✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
6,874,090✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
6,874,253✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
6,874,100✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
6,874,120✔
95

96
  pLoadInfo->info.pUid = NULL;
6,873,990✔
97
  pLoadInfo->info.pFirstKey = NULL;
6,873,990✔
98
  pLoadInfo->info.pLastKey = NULL;
6,873,990✔
99
  pLoadInfo->info.pCount = NULL;
6,873,990✔
100
  pLoadInfo->info.pFirstTs = NULL;
6,873,990✔
101
  pLoadInfo->info.pLastTs = NULL;
6,873,990✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
6,873,990✔
104
  taosMemoryFree(pLoadInfo);
6,874,987✔
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
6,870,339✔
108
  tLDataIterClose2(pIter);
6,870,339✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
6,875,528✔
110
  taosMemoryFree(pIter);
6,875,438✔
111
}
6,875,909✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
12,689,317✔
114
  if (pLDataIterArray == NULL) {
12,689,317✔
115
    return;
45,155✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
12,644,162✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
19,514,290✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
6,867,378✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
13,742,025✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
6,871,313✔
123
      if (pIter->pBlockLoadInfo != NULL) {
6,869,841!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
6,870,131✔
125
        if (pLoadCost != NULL) {
6,870,131✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
6,862,997✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
6,862,997✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
6,862,997✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
6,862,997✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
6,869,841✔
134
    }
135

136
    taosArrayDestroy(pList);
6,864,346✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
12,646,912✔
140
}
141

142
// choose the unpinned slot to load next data block
143
static void updateBlockLoadSlot(SSttBlockLoadInfo *pLoadInfo) {
4,472,650✔
144
  int32_t nextSlotIndex = pLoadInfo->currentLoadBlockIndex ^ 1;
4,472,650✔
145
  if (pLoadInfo->blockData[nextSlotIndex].pin) {
4,472,650!
146
    nextSlotIndex = nextSlotIndex ^ 1;
×
147
  }
148

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
4,472,650✔
150
}
4,472,650✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
896,285,932✔
153
  if (pResBlock != NULL) {
896,285,932!
154
    *pResBlock = NULL;
896,604,522✔
155
  }
156

157
  int32_t            code = 0;
896,285,932✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
896,285,932✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
896,285,932✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
621,439,382✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
636✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
636✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
621,439,382✔
169
    return code;
621,439,382✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
274,846,550!
173
    if (pInfo->currentLoadBlockIndex != 1) {
289,740,951✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
615✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
615✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
289,740,951✔
181
    return code;
289,740,951✔
182
  }
183

UNCOV
184
  if (pIter->pSttBlk == NULL || pInfo->pSchema == NULL) {
×
185
    return code;
×
186
  }
187

188
  updateBlockLoadSlot(pInfo);
4,473,002✔
189
  int64_t st = taosGetTimestampUs();
4,475,065✔
190

191
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,475,065✔
192
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
4,475,065✔
193
                                          pInfo->numOfCols - 1);
4,475,065✔
194
  if (code != TSDB_CODE_SUCCESS) {
4,477,753!
UNCOV
195
    return code;
×
196
  }
197

198
  double el = (taosGetTimestampUs() - st) / 1000.0;
4,477,741✔
199
  pInfo->cost.blockElapsedTime += el;
4,477,741✔
200
  pInfo->cost.loadBlocks += 1;
4,477,741✔
201

202
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
4,477,741✔
203
            ", last block index:%d, entry:%d, rows:%d, uidRange:%" PRId64 "-%" PRId64 " tsRange:%" PRId64 "-%" PRId64
204
            " %p, elapsed time:%.2f ms, %s",
205
            pInfo->cost.loadBlocks, pIter->uid, pIter->cid, pIter->iSttBlk, pInfo->currentLoadBlockIndex, pBlock->nRow,
206
            pIter->pSttBlk->minUid, pIter->pSttBlk->maxUid, pIter->pSttBlk->minKey, pIter->pSttBlk->maxKey, pBlock, el,
207
            idStr);
208

209
  pInfo->blockData[pInfo->currentLoadBlockIndex].sttBlockIndex = pIter->iSttBlk;
4,477,672✔
210
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
4,477,672✔
211

212
  tsdbDebug("last block index list:%d, %d, rowIndex:%d %s", pInfo->blockData[0].sttBlockIndex,
4,477,672✔
213
            pInfo->blockData[1].sttBlockIndex, pIter->iRow, idStr);
214

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,477,600✔
216
  return code;
4,477,600✔
217
}
218

219
// find the earliest block that contains the required records
220
static FORCE_INLINE int32_t findEarliestIndex(int32_t index, uint64_t uid, const SSttBlk *pBlockList, int32_t num,
221
                                              int32_t backward) {
222
  int32_t i = index;
5,540,026✔
223
  int32_t step = backward ? 1 : -1;
5,540,026✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
11,124,213!
225
    i += step;
5,584,187✔
226
  }
227
  return i - step;
5,540,026✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
14,448,445✔
231
  int32_t midPos = -1;
14,448,445✔
232
  if (num <= 0) {
14,448,445✔
233
    return -1;
3,814,573✔
234
  }
235

236
  int32_t firstPos = 0;
10,633,872✔
237
  int32_t lastPos = num - 1;
10,633,872✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
10,633,872✔
241
    return -1;
5,093,571✔
242
  }
243

244
  while (1) {
504,099✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
6,044,400✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
5,422,853✔
247
    }
248

249
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
621,547✔
250
      return -1;
275✔
251
    }
252

253
    int32_t numOfRows = lastPos - firstPos + 1;
621,272✔
254
    midPos = (numOfRows >> 1u) + firstPos;
621,272✔
255

256
    if (uid < pBlockList[midPos].minUid) {
621,272✔
257
      lastPos = midPos - 1;
256,108✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
365,164✔
259
      firstPos = midPos + 1;
247,991✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
117,173✔
262
    }
263
  }
264
}
265

266
static FORCE_INLINE int32_t findEarliestRow(int32_t index, uint64_t uid, const uint64_t *uidList, int32_t num,
267
                                            int32_t backward) {
268
  int32_t i = index;
5,410,153✔
269
  int32_t step = backward ? 1 : -1;
5,410,153✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
101,766,030!
271
    i += step;
96,355,877✔
272
  }
273
  return i - step;
5,410,153✔
274
}
275

276
static int32_t binarySearchForStartRowIndex(uint64_t *uidList, int32_t num, uint64_t uid, int32_t backward) {
5,486,184✔
277
  int32_t firstPos = 0;
5,486,184✔
278
  int32_t lastPos = num - 1;
5,486,184✔
279

280
  // find the first position which is bigger than the key
281
  if ((uid > uidList[lastPos]) || (uid < uidList[firstPos])) {
5,486,184!
282
    return -1;
×
283
  }
284

285
  while (1) {
1,627,724✔
286
    if (uid == uidList[firstPos]) {
7,113,908✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
4,311,811✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
2,802,097✔
291
      return -1;
76,031✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
2,726,066✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
2,726,066✔
296

297
    if (uid < uidList[midPos]) {
2,726,066✔
298
      lastPos = midPos - 1;
664,755✔
299
    } else if (uid > uidList[midPos]) {
2,061,311✔
300
      firstPos = midPos + 1;
962,969✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,098,342✔
303
    }
304
  }
305
}
306

307
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
6,862,075✔
308
                                   uint64_t suid) {
309
  void   *px = NULL;
6,862,075✔
310
  int32_t code = TSDB_CODE_SUCCESS;
6,862,075✔
311
  if (TARRAY2_SIZE(pArray) <= 0) {
6,862,075✔
312
    return code;
10,117✔
313
  }
314

315
  SSttBlk *pStart = &pArray->data[0];
6,851,958✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
6,851,958✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
6,851,958✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,355,193✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,509,852✔
322
      pIter->iSttBlk = -1;
1,509,628✔
323
      return TSDB_CODE_SUCCESS;
1,509,628✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,845,341✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,845,736✔
327
      if (px == NULL) {
1,845,627!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
3,496,765✔
333
    if (pTmp == NULL) {
3,500,967!
UNCOV
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
11,920,447✔
338
      SSttBlk *p = &pArray->data[i];
9,900,113✔
339
      if (p->suid < suid) {
9,900,113✔
340
        continue;
5,388,252✔
341
      }
342

343
      if (p->suid == suid) {
4,511,861✔
344
        void *px = taosArrayPush(pTmp, p);
3,032,116✔
345
        if (px == NULL) {
3,032,116!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
1,479,947!
350
        break;
1,480,918✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
3,501,252✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
3,502,458✔
356
  }
357

358
  return code;
5,348,085✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
3,473,962✔
362
  if (IS_VAR_DATA_TYPE(pVal->type)) {
3,473,962!
363
    char *p = (char *)pVal->pData;
547,941✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
547,941✔
365
    if (pBuf == NULL) {
548,155!
366
      return terrno;
×
367
    }
368

369
    memcpy(pBuf, p, pVal->nData);
548,198✔
370
    pVal->pData = (uint8_t *)pBuf;
548,198✔
371
  }
372

373
  return TSDB_CODE_SUCCESS;
3,474,219✔
374
}
375

376
static int32_t loadSttStatisticsBlockData(SSttFileReader *pSttFileReader, SSttBlockLoadInfo *pBlockLoadInfo,
6,862,447✔
377
                                          TStatisBlkArray *pStatisBlkArray, uint64_t suid, const char *id) {
378
  int32_t code = TSDB_CODE_SUCCESS;
6,862,447✔
379
  int32_t lino = 0;
6,862,447✔
380
  void   *px = NULL;
6,862,447✔
381
  int32_t startIndex = 0;
6,862,447✔
382
  int32_t ret = 0;
6,862,447✔
383

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
6,862,447✔
385
  if (numOfBlocks <= 0) {
6,862,447✔
386
    return code;
10,117✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
7,948,047✔
390
    ++startIndex;
1,095,717✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
6,852,330✔
394
    return 0;
1,698,589✔
395
  }
396

397
  int32_t endIndex = startIndex;
5,153,741✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
10,325,728✔
399
    ++endIndex;
5,171,987✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
5,153,741✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
5,153,741✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
5,153,741✔
407
  QUERY_CHECK_CODE(code, lino, _end);
5,153,676!
408

409
  int64_t st = taosGetTimestampUs();
5,153,933✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
10,320,952✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
5,167,531✔
413
    QUERY_CHECK_CODE(code, lino, _end);
5,172,751!
414

415
    int32_t i = 0;
5,172,751✔
416
    int32_t rows = block.numOfRecords;
5,172,751✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
10,659,666✔
418
      ++i;
5,486,915✔
419
    }
420

421
    // existed
422
    if (i < rows) {
5,172,751✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
4,830,849✔
424

425
      if (pInfo->pUid == NULL) {
4,830,849✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
4,812,545✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
4,812,593✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
4,812,674✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
4,812,742✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
4,812,603✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
4,812,567✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
4,812,549!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
4,812,589!
436
          code = terrno;
379✔
UNCOV
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
4,830,474✔
442
        int32_t size = rows - i;
3,781,511✔
443
        int32_t offset = i * sizeof(int64_t);
3,781,511✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
3,781,511✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,781,227!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
3,781,227✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,781,160!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
3,781,160✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,780,379!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
3,780,379✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,780,725✔
456

457
        if (block.numOfPKs > 0) {
3,780,205✔
458
          SValue vFirst = {0}, vLast = {0};
341,735✔
459
          for (int32_t f = i; f < rows; ++f) {
2,000,835✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
1,658,988✔
461
            TSDB_CHECK_CODE(code, lino, _end);
1,659,188!
462

463
            code = tValueDupPayload(&vFirst);
1,659,188✔
464
            TSDB_CHECK_CODE(code, lino, _end);
1,659,227!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
1,659,227✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,659,171!
468

469
            // todo add api to clone the original data
470
            code = tValueColumnGet(&block.lastKeyPKs[0], f, &vLast);
1,659,171✔
471
            TSDB_CHECK_CODE(code, lino, _end);
1,659,225!
472

473
            code = tValueDupPayload(&vLast);
1,659,225✔
474
            TSDB_CHECK_CODE(code, lino, _end);
1,659,196!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
1,659,196✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,659,100!
478
          }
479
        } else {
480
          SValue vFirst = {0};
3,438,470✔
481
          for (int32_t j = 0; j < size; ++j) {
19,437,791✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
16,003,494✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,998,009!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
15,998,009✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,999,321!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
1,048,963✔
491
        while (i < rows) {
2,191,135✔
492
          code = tStatisBlockGet(&block, i, &record);
2,191,126✔
493
          TSDB_CHECK_CODE(code, lino, _end);
2,191,066!
494

495
          if (record.suid != suid) {
2,191,066✔
496
            break;
1,048,964✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
1,142,102✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,142,134!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
1,142,134✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,142,127!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
1,142,127✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,142,124!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
1,142,124✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,142,120!
510

511
          if (record.firstKey.numOfPKs > 0) {
1,142,120✔
512
            SValue s = record.firstKey.pks[0];
78,358✔
513
            code = tValueDupPayload(&s);
78,358✔
514
            TSDB_CHECK_CODE(code, lino, _end);
78,362!
515

516
            px = taosArrayPush(pInfo->pFirstKey, &s);
78,362✔
517
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
78,363!
518

519
            s = record.lastKey.pks[0];
78,363✔
520
            code = tValueDupPayload(&s);
78,363✔
521
            TSDB_CHECK_CODE(code, lino, _end);
78,367!
522

523
            px = taosArrayPush(pInfo->pLastKey, &s);
78,367✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
78,366!
525
          } else {
526
            SValue v = {0};
1,063,762✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
1,063,762✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,063,815!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
1,063,815✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,063,806!
532
          }
533

534
          i += 1;
1,142,172✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
5,153,421✔
541
  tStatisBlockDestroy(&block);
5,153,421✔
542
  if (code != 0) {
5,154,400!
UNCOV
543
    tsdbError("%s error happens at:%s line number: %d, code:%s", id, __func__, lino, tstrerror(code));
×
544
  } else {
545
    double el = (taosGetTimestampUs() - st) / 1000.0;
5,154,321✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
5,154,321✔
547

548
    tsdbDebug("%s load %d statis blocks into buf, elapsed time:%.2fms", id, num, el);
5,154,321✔
549
  }
550
  return code;
5,154,594✔
551
}
552

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
6,863,444✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
6,866,112✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
6,866,112✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
6,866,112✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
6,866,112✔
562
  if (code != TSDB_CODE_SUCCESS) {
6,863,642!
UNCOV
563
    tsdbError("load stt blk failed, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
564
    return code;
×
565
  }
566

567
  // load the stt block info for each stt file block
568
  code = extractSttBlockInfo(pIter, pSttBlkArray, pBlockLoadInfo, suid);
6,863,642✔
569
  if (code != TSDB_CODE_SUCCESS) {
6,866,564!
UNCOV
570
    tsdbError("load stt block info failed, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
571
    return code;
×
572
  }
573

574
  // load stt statistics block for all stt-blocks, to decide if the data of queried table exists in current stt file
575
  TStatisBlkArray *pStatisBlkArray = NULL;
6,866,564✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
6,866,564✔
577
  if (code != TSDB_CODE_SUCCESS) {
6,866,229!
UNCOV
578
    tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
579
    return code;
×
580
  }
581

582
  // load statistics block for all tables in current stt file
583
  code = loadSttStatisticsBlockData(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, suid, idStr);
6,866,229✔
584
  if (code != TSDB_CODE_SUCCESS) {
6,865,354!
UNCOV
585
    tsdbError("failed to load stt statistics block data, code:%s, %s", tstrerror(code), idStr);
×
UNCOV
586
    return code;
×
587
  }
588

589
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
6,865,354✔
590

591
  double el = (taosGetTimestampUs() - st) / 1000.0;
6,864,658✔
592
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
6,864,658✔
593
  return code;
6,864,887✔
594
}
595

596
static int32_t uidComparFn(const void *p1, const void *p2) {
13,615,668✔
597
  const uint64_t *pFirst = p1;
13,615,668✔
598
  const uint64_t *pVal = p2;
13,615,668✔
599

600
  if (*pFirst == *pVal) {
13,615,668✔
601
    return 0;
5,470,373✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
8,145,295✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
14,454,517✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
14,454,517!
610
    return;
3,815,159✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
10,637,972✔
614
  if (index >= 0) {
10,642,276✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
5,472,729✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
5,468,944✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
5,470,089✔
619

620
    if (pRange->skey.numOfPKs > 0) {
5,470,325✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
613,422✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
613,277✔
623
    }
624
  }
625
}
626

627
int32_t tLDataIterOpen2(SLDataIter *pIter, SSttFileReader *pSttFileReader, int32_t cid, int8_t backward,
14,457,453✔
628
                        SMergeTreeConf *pConf, SSttBlockLoadInfo *pBlockLoadInfo, SSttKeyRange *pKeyRange,
629
                        int64_t *numOfRows, const char *idStr) {
630
  int32_t code = TSDB_CODE_SUCCESS;
14,457,453✔
631

632
  pIter->uid = pConf->uid;
14,457,453✔
633
  pIter->cid = cid;
14,457,453✔
634
  pIter->backward = backward;
14,457,453✔
635
  pIter->verRange.minVer = pConf->verRange.minVer;
14,457,453✔
636
  pIter->verRange.maxVer = pConf->verRange.maxVer;
14,457,453✔
637
  pIter->timeWindow.skey = pConf->timewindow.skey;
14,457,453✔
638
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
14,457,453✔
639

640
  pIter->pStartRowKey = pConf->pCurRowKey;
14,457,453✔
641
  pIter->pReader = pSttFileReader;
14,457,453✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
14,457,453✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
14,457,453!
UNCOV
646
    tsdbError("stt file reader is null, %s", idStr);
×
UNCOV
647
    pIter->pSttBlk = NULL;
×
UNCOV
648
    pIter->iSttBlk = -1;
×
UNCOV
649
    return TSDB_CODE_SUCCESS;
×
650
  }
651

652
  if (!pBlockLoadInfo->sttBlockLoaded) {
14,457,453✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
6,865,800✔
654
    if (code != TSDB_CODE_SUCCESS) {
6,864,052!
UNCOV
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
14,455,705✔
660

661
  // find the start block, actually we could load the position to avoid repeatly searching for the start position when
662
  // the skey is updated.
663
  size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
14,451,412✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
14,449,672✔
665
  if (pIter->iSttBlk != -1) {
14,449,800✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
5,546,988✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
5,545,742✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
5,545,742!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
4,526,870✔
671
      pIter->pSttBlk = NULL;
51✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
5,545,742!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
1,018,849!
676
      pIter->pSttBlk = NULL;
494✔
677
      pIter->ignoreEarlierTs = true;
494✔
678
    }
679
  }
680

681
  return code;
14,448,554✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
6,870,086✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
6,870,086✔
686
  pIter->pReader = NULL;
6,875,565✔
687
}
6,875,565✔
688

689
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
5,592,880✔
690
  int32_t step = pIter->backward ? -1 : 1;
5,592,880✔
691
  int32_t oldIndex = pIter->iSttBlk;
5,592,880✔
692

693
  pIter->iSttBlk += step;
5,592,880✔
694

695
  int32_t index = -1;
5,592,880✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
5,592,880✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
5,606,647!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
292,856✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
292,844✔
700
      break;
184,200✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
108,644✔
704
      break;
16,340✔
705
    }
706

707
    // check uid firstly
708
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
92,304!
709
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
92,317✔
710
        break;
857✔
711
      }
712

713
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
91,460✔
714
        break;
146✔
715
      }
716

717
      // check time range secondly
718
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
91,314✔
719
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
77,536!
UNCOV
720
          break;
×
721
        }
722

723
        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
77,536!
724
          break;
×
725
        }
726

727
        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
77,536!
728
          index = i;
77,534✔
729
          break;
77,534✔
730
        }
731
      }
732
    }
733
  }
734

735
  pIter->pSttBlk = NULL;
5,592,868✔
736
  if (index != -1) {
5,592,868✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
77,534✔
738

739
    pIter->iSttBlk = index;
77,534✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
77,534✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
77,535✔
742
              ", uidRange:%" PRId64 "-%" PRId64 " %s",
743
              pIter->iSttBlk, oldIndex, pIter->uid, pIter->cid, p->minUid, p->maxUid, idStr);
744
  } else {
745
    tsdbDebug("no more last block qualified, uid:%" PRIu64 ", stt-file block:%d, %s", pIter->uid, oldIndex, idStr);
5,515,334✔
746
  }
747
}
5,592,871✔
748

749
static int32_t findNextValidRow(SLDataIter *pIter, const char *idStr) {
457,575,308✔
750
  bool        hasVal = false;
457,575,308✔
751
  int32_t     step = pIter->backward ? -1 : 1;
457,575,308✔
752
  int32_t     i = pIter->iRow;
457,575,308✔
753
  SBlockData *pData = NULL;
457,575,308✔
754

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
457,575,308✔
756
  if (code) {
453,179,827!
757
    tsdbError("failed to load stt block, code:%s, %s", tstrerror(code), idStr);
×
758
    return code;
×
759
  }
760

761
  // mostly we only need to find the start position for a given table
762
  if ((((i == 0) && (!pIter->backward)) || (i == pData->nRow - 1 && pIter->backward)) && pData->aUid != NULL) {
453,179,827✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
5,486,466✔
764
    if (i == -1) {
5,486,974!
UNCOV
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
76,192✔
767
      return code;
76,192✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
469,135,818!
772
    if (pData->aUid != NULL) {
466,905,851✔
773
      if (!pIter->backward) {
462,857,255✔
774
        if (pData->aUid[i] > pIter->uid) {
422,870,884✔
775
          break;
985,942✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
39,986,371✔
779
          break;
172,069✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
465,747,840✔
785
    if (!pIter->backward) {               // asc
465,747,840✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
425,439,840✔
787
        break;
231,078✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
425,208,762✔
790
          continue;
5,971,829✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
419,236,933✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
100,249!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
100,249✔
797
          if (ret < 0) {
100,249!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
40,308,000✔
804
        break;
275,149✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
40,032,851✔
807
          continue;
9,706,840✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
30,326,011!
811
          SRowKey key;
812
          tColRowGetKey(pData, i, &key);
×
813
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
×
814
          if (ret > 0) {
×
815
            continue;
×
816
          }
817
        }
818
      }
819
    }
820

821
    int64_t ver = pData->aVersion[i];
449,562,944✔
822
    if (ver < pIter->verRange.minVer) {
449,562,944!
823
      continue;
×
824
    }
825

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
449,562,944✔
828
      continue;
24✔
829
    }
830

831
    hasVal = true;
449,562,920✔
832
    break;
449,562,920✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
453,457,125✔
836
  return code;
453,457,125✔
837
}
838

839
int32_t tLDataIterNextRow(SLDataIter *pIter, const char *idStr, bool *hasNext) {
466,774,628✔
840
  int32_t     step = pIter->backward ? -1 : 1;
466,774,628✔
841
  int32_t     code = 0;
466,774,628✔
842
  int32_t     iBlockL = pIter->iSttBlk;
466,774,628✔
843
  SBlockData *pBlockData = NULL;
466,774,628✔
844
  int32_t     lino = 0;
466,774,628✔
845

846
  *hasNext = false;
466,774,628✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
466,774,628✔
850
    return code;
8,908,592✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
457,866,036✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
457,421,315!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
457,820,674✔
860

861
  while (1) {
77,133✔
862
    bool skipBlock = false;
457,897,807✔
863
    code = findNextValidRow(pIter, idStr);
457,897,807✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
454,255,821!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
454,255,821!
867
      skipBlock = true;
×
868
      int16_t *aCols = pIter->pBlockLoadInfo->colIds;
×
869
      int      nCols = pIter->pBlockLoadInfo->numOfCols;
×
870
      bool     isLast = pIter->pBlockLoadInfo->isLast;
×
871
      for (int inputColIndex = 0; inputColIndex < nCols; ++inputColIndex) {
×
872
        for (int colIndex = 0; colIndex < pBlockData->nColData; ++colIndex) {
×
873
          SColData *pColData = &pBlockData->aColData[colIndex];
×
874
          int16_t   cid = pColData->cid;
×
875

876
          if (cid == aCols[inputColIndex]) {
×
877
            if (isLast && (pColData->flag & HAS_VALUE)) {
×
878
              skipBlock = false;
×
879
              break;
×
880
            } else if (pColData->flag & (HAS_VALUE | HAS_NULL)) {
×
881
              skipBlock = false;
×
882
              break;
×
883
            }
884
          }
885
        }
886
      }
887
    }
888

889
    if (skipBlock || pIter->iRow >= pBlockData->nRow || pIter->iRow < 0) {
454,255,821!
890
      tLDataIterNextBlock(pIter, idStr);
5,434,531✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
5,592,489✔
892
        goto _exit;
5,515,358✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
77,131!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
77,534✔
900
      if ((pBlockData == NULL) || (code != 0)) {
77,535!
UNCOV
901
        lino = __LINE__;
×
UNCOV
902
        goto _exit;
×
903
      }
904

905
      // set start row index
906
      pIter->iRow = pIter->backward ? pBlockData->nRow - 1 : 0;
77,536✔
907
    }
908
  }
909

910
  pIter->rInfo.suid = pBlockData->suid;
448,821,290✔
911
  pIter->rInfo.uid = pBlockData->uid;
448,821,290✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
448,821,290✔
913

914
_exit:
453,937,288✔
915
  if (code) {
453,937,288!
UNCOV
916
    tsdbError("failed to exec stt-file nextIter, lino:%d, code:%s, %s", lino, tstrerror(code), idStr);
×
917
  }
918

919
  *hasNext = (code == TSDB_CODE_SUCCESS) && (pIter->pSttBlk != NULL) && (pBlockData != NULL);
454,075,767!
920
  return code;
454,075,767✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
41,633,814✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
41,729,848✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
41,729,848✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
41,729,848✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
41,729,848!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
41,902,388!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
41,835,338✔
933
  if (ret < 0) {
41,812,461✔
934
    return -1;
23,423,173✔
935
  } else if (ret > 0) {
18,389,288✔
936
    return 1;
7,621,398✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
10,767,890!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
10,767,890!
940

941
    if (ver1 < ver2) {
10,767,890✔
942
      return -1;
4,356,708✔
943
    } else if (ver1 > ver2) {
6,411,182!
944
      return 1;
6,891,827✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

951
static FORCE_INLINE int32_t tLDataIterDescCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
96,034!
952
  return -1 * tLDataIterCmprFn(p1, p2);
96,034✔
953
}
954

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
14,951,024✔
956
  int32_t code = TSDB_CODE_SUCCESS;
14,951,024✔
957

958
  pMTree->pIter = NULL;
14,951,024✔
959
  pMTree->backward = pConf->backward;
14,951,024✔
960
  pMTree->idStr = pConf->idstr;
14,951,024✔
961
  int32_t lino = 0;
14,951,024✔
962

963
  if (!pMTree->backward) {  // asc
14,951,024✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
11,572,583✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
3,378,441✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
14,952,452✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
14,952,452✔
973
  if (numOfLevels == 0) {
14,952,452✔
974
    goto _end;
700,725✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
14,251,727✔
978
  if (code) {
14,248,032!
UNCOV
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
28,677,556✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
14,427,786✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
14,427,786✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
28,877,641✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
14,448,117✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
14,443,213✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
14,443,213✔
991

992
      // open stt file reader if not opened yet
993
      // if failed to open this stt file, ignore the error and try next one
994
      if (pSttFileReader == NULL) {
14,443,213✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
6,865,474✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
6,865,474✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
6,865,474✔
999
        if (code != TSDB_CODE_SUCCESS) {
6,864,062!
UNCOV
1000
          tsdbError("open stt file reader error. file name %s, code %s, %s", pSttLevel->fobjArr->data[i]->fname,
×
1001
                    tstrerror(code), pMTree->idStr);
1002
        }
1003
      }
1004

1005
      if (pLoadInfo == NULL) {
14,442,353✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
6,865,091✔
1007
        if (code != TSDB_CODE_SUCCESS) {
6,865,620!
UNCOV
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
14,442,882✔
1013

1014
      SSttKeyRange range = {.skey.numOfPKs = pConf->pCurRowKey->numOfPKs, .ekey.numOfPKs = pConf->pCurRowKey->numOfPKs};
14,442,882✔
1015
      int64_t      numOfRows = 0;
14,442,882✔
1016
      int64_t      cid = pSttLevel->fobjArr->data[i]->f->cid;
14,442,882✔
1017

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
14,442,882✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
14,448,614!
UNCOV
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
14,448,614✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
14,448,614✔
1026
      if (code) {
14,454,078!
UNCOV
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
14,454,078✔
1031
        tMergeTreeAddIter(pMTree, pIter);
5,466,651✔
1032

1033
        // let's record the time window for current table of uid in the stt files
1034
        if (pSttDataInfo != NULL && numOfRows > 0) {
5,465,996!
1035
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
5,465,463✔
1036
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
5,464,720!
1037

1038
          pSttDataInfo->numOfRows += numOfRows;
5,464,720✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
8,987,427!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
8,990,084✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
14,249,770✔
1049

1050
_end:
700,725✔
1051
  tMergeTreeClose(pMTree);
700,725✔
1052
  return code;
700,618✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
5,466,267✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
5,466,267✔
1057
}
5,466,020✔
1058

1059
bool tMergeTreeIgnoreEarlierTs(SMergeTree *pMTree) { return pMTree->ignoreEarlierTs; }
×
1060

1061
static void tLDataIterPinSttBlock(SLDataIter *pIter, const char *id) {
291,674,346✔
1062
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
291,674,346✔
1063

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
291,674,346✔
1065
    pInfo->blockData[0].pin = true;
175,982,743✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
175,982,743✔
1067
    return;
176,026,362✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
115,691,603!
1071
    pInfo->blockData[1].pin = true;
120,040,808✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
120,040,808✔
1073
    return;
119,925,084✔
1074
  }
1075

1076
  tsdbError("failed to pin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1077
}
1078

1079
static void tLDataIterUnpinSttBlock(SLDataIter *pIter, const char *id) {
284,737,581✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
284,737,581✔
1081
  if (pInfo->blockData[0].pin) {
284,737,581✔
1082
    pInfo->blockData[0].pin = false;
173,797,175✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
173,797,175✔
1084
    return;
173,803,323✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
110,940,406!
1088
    pInfo->blockData[1].pin = false;
118,170,546✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
118,170,546✔
1090
    return;
118,172,241✔
1091
  }
1092

1093
  tsdbError("failed to unpin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1094
}
1095

1096
void tMergeTreePinSttBlock(SMergeTree *pMTree) {
291,727,997✔
1097
  if (pMTree->pIter == NULL) {
291,727,997!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
291,727,997✔
1102
  pMTree->pPinnedBlockIter = pIter;
291,727,997✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
291,727,997✔
1104
}
1105

1106
void tMergeTreeUnpinSttBlock(SMergeTree *pMTree) {
284,929,546✔
1107
  if (pMTree->pPinnedBlockIter == NULL) {
284,929,546!
1108
    return;
×
1109
  }
1110

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
284,929,546✔
1112
  pMTree->pPinnedBlockIter = NULL;
284,929,546✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
284,929,546✔
1114
}
1115

1116
int32_t tMergeTreeNext(SMergeTree *pMTree, bool *pHasNext) {
466,113,498✔
1117
  int32_t code = 0;
466,113,498✔
1118
  if (pHasNext == NULL) {
466,113,498!
1119
    return TSDB_CODE_INVALID_PARA;
×
1120
  }
1121

1122
  *pHasNext = false;
466,113,498✔
1123
  if (pMTree->pIter) {
466,113,498✔
1124
    SLDataIter *pIter = pMTree->pIter;
451,734,711✔
1125
    bool        hasVal = false;
451,734,711✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
451,734,711✔
1127
    if (!hasVal || (code != 0)) {
448,071,368✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
5,432,831!
1129
        code = 0;  // suppress the file corrupt error to enable all queries within this cluster can run without failed.
×
1130
      }
1131

1132
      pMTree->pIter = NULL;
5,432,831✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
448,071,368✔
1137
    if (pMTree->pIter && pIter) {
448,071,368✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
23,113,834✔
1139
      if (c > 0) {
23,002,540✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,246,398✔
1141
        pMTree->pIter = NULL;
4,344,413✔
1142
      } else if (!c) {
18,756,142!
1143
        return TSDB_CODE_INTERNAL_ERROR;
×
1144
      }
1145
    }
1146
  }
1147

1148
  if (pMTree->pIter == NULL) {
462,436,876✔
1149
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
24,686,795✔
1150
    if (pMTree->pIter) {
24,686,795✔
1151
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
9,776,309✔
1152
    }
1153
  }
1154

1155
  *pHasNext = (pMTree->pIter != NULL);
462,843,824✔
1156
  return code;
462,843,824✔
1157
}
1158

1159
void tMergeTreeClose(SMergeTree *pMTree) {
26,030,290✔
1160
  pMTree->pIter = NULL;
26,030,290✔
1161
  pMTree->pPinnedBlockIter = NULL;
26,030,290✔
1162
}
26,030,290✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc