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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

81.63
/source/dnode/vnode/src/tq/tqScan.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 "tq.h"
17
static int32_t tqAddRawDataToRsp(const void* rawData, SMqDataRsp* pRsp, int8_t precision) {
×
18
  int32_t    code = TDB_CODE_SUCCESS;
×
19
  int32_t    lino = 0;
×
20
  void*      buf = NULL;
×
21

22
  int32_t dataStrLen = sizeof(SRetrieveTableRspForTmq) + *(uint32_t *)rawData + INT_BYTES;
×
23
  buf = taosMemoryCalloc(1, dataStrLen);
×
24
  TSDB_CHECK_NULL(buf, code, lino, END, terrno);
×
25

26
  SRetrieveTableRspForTmq* pRetrieve = (SRetrieveTableRspForTmq*)buf;
×
27
  pRetrieve->version = RETRIEVE_TABLE_RSP_TMQ_RAW_VERSION;
×
28
  pRetrieve->precision = precision;
×
29
  pRetrieve->compressed = 0;
×
30

31
  memcpy(pRetrieve->data, rawData, *(uint32_t *)rawData + INT_BYTES);
×
32
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockDataLen, &dataStrLen), code, lino, END, terrno);
×
33
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockData, &buf), code, lino, END, terrno);
×
34
  pRsp->blockDataElementFree = true;
×
35

36
  tqTrace("tqAddRawDataToRsp add block data to block array, blockDataLen:%d, blockData:%p", dataStrLen, buf);
×
37
  END:
×
38
  if (code != TSDB_CODE_SUCCESS) {
×
39
    taosMemoryFree(buf);
×
40
    tqError("%s failed at %d, failed to add block data to response:%s", __FUNCTION__, lino, tstrerror(code));
×
41
  }
42
  return code;
×
43
}
44

45
static int32_t tqAddBlockDataToRsp(const SSDataBlock* pBlock, SMqDataRsp* pRsp, const SSchemaWrapper* pSW, int8_t precision) {
50,036,222✔
46
  int32_t code = 0;
50,036,222✔
47
  int32_t lino = 0;
50,036,222✔
48
  SSchemaWrapper* pSchema = NULL;
50,036,222✔
49
  
50
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
50,052,010✔
51
  int32_t dataStrLen = sizeof(SRetrieveTableRspForTmq) + dataEncodeBufSize;
50,090,341✔
52
  void*   buf = taosMemoryCalloc(1, dataStrLen);
50,090,341✔
53
  TSDB_CHECK_NULL(buf, code, lino, END, terrno);
50,033,840✔
54

55
  SRetrieveTableRspForTmq* pRetrieve = (SRetrieveTableRspForTmq*)buf;
50,033,840✔
56
  pRetrieve->version = RETRIEVE_TABLE_RSP_TMQ_VERSION;
50,033,840✔
57
  pRetrieve->precision = precision;
50,046,586✔
58
  pRetrieve->compressed = 0;
50,060,405✔
59
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
50,039,857✔
60

61
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data, dataEncodeBufSize, pSW->nCols);
50,065,584✔
62
  TSDB_CHECK_CONDITION(actualLen >= 0, code, lino, END, terrno);
50,052,650✔
63

64
  actualLen += sizeof(SRetrieveTableRspForTmq);
50,052,650✔
65
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockDataLen, &actualLen), code, lino, END, terrno);
100,134,030✔
66
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockData, &buf), code, lino, END, terrno);
100,165,459✔
67
  pSchema = tCloneSSchemaWrapper(pSW);
50,051,285✔
68
  TSDB_CHECK_NULL(pSchema, code, lino, END, terrno);
50,051,285✔
69
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockSchema, &pSchema), code, lino, END, terrno);
100,131,240✔
70
  pSchema = NULL;
50,079,955✔
71
  pRsp->blockDataElementFree = true;
50,079,955✔
72
  tqTrace("tqAddBlockDataToRsp add block data to block array, blockDataLen:%d, blockData:%p", dataStrLen, buf);
50,068,335✔
73

74
END:
50,068,335✔
75
  tDeleteSchemaWrapper(pSchema);
50,054,418✔
76
  if (code != TSDB_CODE_SUCCESS){
50,032,439✔
77
    taosMemoryFree(buf);
×
78
    tqError("%s failed at line %d with msg:%s", __func__, lino, tstrerror(code));
×
79
  }
80
  return code;
50,032,439✔
81
}
82

83
static int32_t tqAddTbNameToRsp(const STQ* pTq, int64_t uid, SMqDataRsp* pRsp, int32_t n) {
46,098,204✔
84
  int32_t    code = TDB_CODE_SUCCESS;
46,098,204✔
85
  int32_t    lino = 0;
46,098,204✔
86
  SMetaReader mr = {0};
46,098,204✔
87

88
  TSDB_CHECK_NULL(pTq, code, lino, END, TSDB_CODE_INVALID_PARA);
46,116,193✔
89
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
46,116,193✔
90

91
  metaReaderDoInit(&mr, pTq->pVnode->pMeta, META_READER_LOCK);
46,116,193✔
92

93
  code = metaReaderGetTableEntryByUidCache(&mr, uid);
46,093,607✔
94
  TSDB_CHECK_CODE(code, lino, END);
46,095,697✔
95

96
  for (int32_t i = 0; i < n; i++) {
92,230,793✔
97
    char* tbName = taosStrdup(mr.me.name);
46,063,680✔
98
    TSDB_CHECK_NULL(tbName, code, lino, END, terrno);
46,116,401✔
99
    if(taosArrayPush(pRsp->blockTbName, &tbName) == NULL){
92,250,519✔
100
      tqError("failed to push tbName to blockTbName:%s, uid:%"PRId64, tbName, uid);
×
101
      continue;
×
102
    }
103
    tqTrace("add tbName to response success tbname:%s, uid:%"PRId64, tbName, uid);
46,134,118✔
104
  }
105

106
END:
46,166,765✔
107
  if (code != TSDB_CODE_SUCCESS) {
46,109,810✔
108
    tqError("%s failed at %d, failed to add tbName to response:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
109
  }
110
  metaReaderClear(&mr);
46,109,810✔
111
  return code;
46,123,540✔
112
}
113

114
int32_t getDataBlock(qTaskInfo_t task, const STqHandle* pHandle, int32_t vgId, SSDataBlock** res) {
10,596,825✔
115
  if (task == NULL || pHandle == NULL || res == NULL) {
10,596,825✔
116
    return TSDB_CODE_INVALID_PARA;
×
117
  }
118
  uint64_t ts = 0;
10,602,633✔
119
  qStreamSetOpen(task);
10,600,417✔
120

121
  tqDebug("consumer:0x%" PRIx64 " vgId:%d, tmq one task start execute", pHandle->consumerId, vgId);
10,586,016✔
122
  int32_t code = qExecTask(task, res, &ts);
10,597,285✔
123
  if (code != TSDB_CODE_SUCCESS) {
10,605,108✔
124
    tqError("consumer:0x%" PRIx64 " vgId:%d, task exec error since %s", pHandle->consumerId, vgId, tstrerror(code));
×
125
  }
126

127
  tqDebug("consumer:0x%" PRIx64 " vgId:%d tmq one task end executed, pDataBlock:%p", pHandle->consumerId, vgId, *res);
10,605,108✔
128
  return code;
10,605,108✔
129
}
130

131
static int32_t tqProcessReplayRsp(STQ* pTq, STqHandle* pHandle, SMqDataRsp* pRsp, const SMqPollReq* pRequest, SSDataBlock* pDataBlock, qTaskInfo_t task){
15,236✔
132
  int32_t code = 0;
15,236✔
133
  int32_t lino = 0;
15,236✔
134

135
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type) && pHandle->block != NULL) {
15,236✔
136
    blockDataDestroy(pHandle->block);
293✔
137
    pHandle->block = NULL;
293✔
138
  }
139
  if (pHandle->block == NULL) {
15,236✔
140
    if (pDataBlock == NULL) {
11,720✔
141
      goto END;
10,255✔
142
    }
143

144
    STqOffsetVal offset = {0};
1,465✔
145
    code = qStreamExtractOffset(task, &offset);
1,465✔
146
    TSDB_CHECK_CODE(code, lino, END);
1,465✔
147

148
    pHandle->block = NULL;
1,465✔
149

150
    code = createOneDataBlock(pDataBlock, true, &pHandle->block);
1,465✔
151
    TSDB_CHECK_CODE(code, lino, END);
1,465✔
152

153
    pHandle->blockTime = offset.ts;
1,465✔
154
    tOffsetDestroy(&offset);
1,465✔
155
    int32_t vgId = TD_VID(pTq->pVnode);
1,465✔
156
    code = getDataBlock(task, pHandle, vgId, &pDataBlock);
1,465✔
157
    TSDB_CHECK_CODE(code, lino, END);
1,465✔
158
  }
159

160
  const STqExecHandle* pExec = &pHandle->execHandle;
4,981✔
161
  code = tqAddBlockDataToRsp(pHandle->block, pRsp, &pExec->execCol.pSW, pTq->pVnode->config.tsdbCfg.precision);
4,981✔
162
  TSDB_CHECK_CODE(code, lino, END);
4,981✔
163

164
  pRsp->blockNum++;
4,981✔
165
  if (pDataBlock == NULL) {
4,981✔
166
    blockDataDestroy(pHandle->block);
1,172✔
167
    pHandle->block = NULL;
1,172✔
168
  } else {
169
    code = copyDataBlock(pHandle->block, pDataBlock);
3,809✔
170
    TSDB_CHECK_CODE(code, lino, END);
3,809✔
171

172
    STqOffsetVal offset = {0};
3,809✔
173
    code = qStreamExtractOffset(task, &offset);
3,809✔
174
    TSDB_CHECK_CODE(code, lino, END);
3,809✔
175

176
    pRsp->sleepTime = offset.ts - pHandle->blockTime;
3,809✔
177
    pHandle->blockTime = offset.ts;
3,809✔
178
    tOffsetDestroy(&offset);
3,809✔
179
  }
180

181
END:
15,236✔
182
  if (code != TSDB_CODE_SUCCESS) {
15,236✔
183
    tqError("%s failed at %d, failed to process replay response:%s", __FUNCTION__, lino, tstrerror(code));
×
184
  }
185
  return code;
15,236✔
186
}
187

188
int32_t tqScanData(STQ* pTq, STqHandle* pHandle, SMqDataRsp* pRsp, STqOffsetVal* pOffset, const SMqPollReq* pRequest) {
10,883,484✔
189
  int32_t code = 0;
10,883,484✔
190
  int32_t lino = 0;
10,883,484✔
191
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
10,883,484✔
192
  TSDB_CHECK_NULL(pTq, code, lino, END, TSDB_CODE_INVALID_PARA);
10,883,484✔
193
  TSDB_CHECK_NULL(pHandle, code, lino, END, TSDB_CODE_INVALID_PARA);
10,883,484✔
194
  TSDB_CHECK_NULL(pOffset, code, lino, END, TSDB_CODE_INVALID_PARA);
10,883,484✔
195
  TSDB_CHECK_NULL(pRequest, code, lino, END, TSDB_CODE_INVALID_PARA);
10,883,484✔
196

197
  int32_t vgId = TD_VID(pTq->pVnode);
10,883,484✔
198
  int32_t totalRows = 0;
10,884,483✔
199

200
  const STqExecHandle* pExec = &pHandle->execHandle;
10,884,483✔
201
  qTaskInfo_t          task = pExec->task;
10,883,101✔
202

203
  code = qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
10,881,189✔
204
  TSDB_CHECK_CODE(code, lino, END);
10,878,530✔
205

206
  qStreamSetParams(task, pRequest->sourceExcluded, pRequest->minPollRows, pRequest->timeout, pRequest->enableReplay);
10,596,461✔
207
  do {
208
    SSDataBlock* pDataBlock = NULL;
10,595,965✔
209
    code = getDataBlock(task, pHandle, vgId, &pDataBlock);
10,595,965✔
210
    TSDB_CHECK_CODE(code, lino, END);
10,603,278✔
211

212
    if (pRequest->enableReplay) {
10,603,278✔
213
      code = tqProcessReplayRsp(pTq, pHandle, pRsp, pRequest, pDataBlock, task);
15,236✔
214
      TSDB_CHECK_CODE(code, lino, END);
15,236✔
215
      break;
15,236✔
216
    }
217
    if (pDataBlock == NULL) {
10,588,042✔
218
      break;
7,393,539✔
219
    }
220
    code = tqAddBlockDataToRsp(pDataBlock, pRsp, &pExec->execCol.pSW, pTq->pVnode->config.tsdbCfg.precision);
3,194,503✔
221
    TSDB_CHECK_CODE(code, lino, END);
3,194,503✔
222

223
    pRsp->blockNum++;
3,194,503✔
224
    totalRows += pDataBlock->info.rows;
3,194,503✔
225
  } while(0);
226

227
  tqDebug("consumer:0x%" PRIx64 " vgId:%d tmq task executed finished, total blocks:%d, totalRows:%d", pHandle->consumerId, vgId, pRsp->blockNum, totalRows);
10,603,278✔
228
  code = qStreamExtractOffset(task, &pRsp->rspOffset);
10,603,643✔
229

230
END:
10,885,712✔
231
  if (code != 0) {
10,885,712✔
232
    tqError("%s failed at %d, tmq task executed error msg:%s", __FUNCTION__, lino, tstrerror(code));
282,069✔
233
  }
234
  return code;
10,885,712✔
235
}
236

237
int32_t tqScanTaosx(STQ* pTq, const STqHandle* pHandle, SMqDataRsp* pRsp, SMqBatchMetaRsp* pBatchMetaRsp, STqOffsetVal* pOffset, const SMqPollReq* pRequest) {
41,643✔
238
  int32_t code = 0;
41,643✔
239
  int32_t lino = 0;
41,643✔
240
  char* tbName = NULL;
41,643✔
241
  const STqExecHandle* pExec = &pHandle->execHandle;
41,643✔
242
  qTaskInfo_t          task = pExec->task;
41,643✔
243
  code = qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
41,643✔
244
  TSDB_CHECK_CODE(code, lino, END);
41,643✔
245

246
  qStreamSetParams(task, pRequest->sourceExcluded, pRequest->minPollRows, pRequest->timeout, false);
41,643✔
247

248
  int32_t rowCnt = 0;
41,643✔
249
  int64_t st = taosGetTimestampMs();
41,643✔
250
  while (1) {
1,504,167✔
251
    SSDataBlock* pDataBlock = NULL;
1,545,810✔
252
    uint64_t     ts = 0;
1,544,862✔
253
    tqDebug("tmqsnap task start to execute");
1,545,492✔
254
    code = qExecTask(task, &pDataBlock, &ts);
1,545,807✔
255
    TSDB_CHECK_CODE(code, lino, END);
1,545,810✔
256
    tqDebug("tmqsnap task execute end, get %p", pDataBlock);
1,545,810✔
257

258
    if (pDataBlock != NULL && pDataBlock->info.rows > 0) {
1,545,810✔
259
      if (pRsp->withTbName) {
750,127✔
260
        tbName = taosStrdup(qExtractTbnameFromTask(task));
750,127✔
261
        TSDB_CHECK_NULL(tbName, code, lino, END, terrno);
749,812✔
262
        TSDB_CHECK_NULL(taosArrayPush(pRsp->blockTbName, &tbName), code, lino, END, terrno);
1,499,939✔
263
        tqDebug("vgId:%d, add tbname:%s to rsp msg", pTq->pVnode->config.vgId, tbName);
750,127✔
264
        tbName = NULL;
750,127✔
265
      }
266

267
      code = tqAddBlockDataToRsp(pDataBlock, pRsp, qExtractSchemaFromTask(task), pTq->pVnode->config.tsdbCfg.precision);
750,127✔
268
      TSDB_CHECK_CODE(code, lino, END);
750,127✔
269

270
      pRsp->blockNum++;
750,127✔
271
      rowCnt += pDataBlock->info.rows;
750,127✔
272
      if (rowCnt <= pRequest->minPollRows && (taosGetTimestampMs() - st <= pRequest->timeout)) {
1,469,203✔
273
        continue;
719,391✔
274
      }
275
    }
276

277
    // get meta
278
    SMqBatchMetaRsp* tmp = qStreamExtractMetaMsg(task);
826,734✔
279
    if (taosArrayGetSize(tmp->batchMetaReq) > 0) {
826,419✔
280
      code = qStreamExtractOffset(task, &tmp->rspOffset);
2,411✔
281
      TSDB_CHECK_CODE(code, lino, END);
2,411✔
282
      *pBatchMetaRsp = *tmp;
2,411✔
283
      tqDebug("tmqsnap task get meta");
2,411✔
284
      break;
2,411✔
285
    }
286

287
    if (pDataBlock == NULL) {
824,008✔
288
      code = qStreamExtractOffset(task, pOffset);
793,272✔
289
      TSDB_CHECK_CODE(code, lino, END);
793,272✔
290

291
      if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
793,272✔
292
        continue;
784,776✔
293
      }
294

295
      tqDebug("tmqsnap vgId: %d, tsdb consume over, switch to wal, ver %" PRId64, TD_VID(pTq->pVnode), pHandle->snapshotVer + 1);
8,496✔
296
      code = qStreamExtractOffset(task, &pRsp->rspOffset);
8,496✔
297
      break;
8,496✔
298
    }
299

300
    if (pRsp->blockNum > 0) {
30,736✔
301
      tqDebug("tmqsnap task exec exited, get data");
30,736✔
302
      code = qStreamExtractOffset(task, &pRsp->rspOffset);
30,736✔
303
      break;
30,736✔
304
    }
305
  }
306
  tqDebug("%s:%d success", __FUNCTION__, lino);
41,643✔
307
END:
41,643✔
308
  if (code != 0){
41,643✔
309
    tqError("%s failed at %d, vgId:%d, task exec error since %s", __FUNCTION__ , lino, pTq->pVnode->config.vgId, tstrerror(code));
×
310
  }
311
  taosMemoryFree(tbName);
41,643✔
312
  return code;
41,643✔
313
}
314

315
static void tqProcessSubData(STQ* pTq, STqHandle* pHandle, SMqDataRsp* pRsp, int32_t* totalRows,
46,142,438✔
316
                             const SMqPollReq* pRequest, SArray* rawList){
317
  int32_t code = 0;
46,142,438✔
318
  int32_t lino = 0;
46,142,438✔
319
  SArray* pBlocks = NULL;
46,142,438✔
320
  SArray* pSchemas = NULL;
46,142,438✔
321

322
  STqExecHandle* pExec = &pHandle->execHandle;
46,142,438✔
323
  STqReader* pReader = pExec->pTqReader;
46,147,366✔
324

325
  pBlocks = taosArrayInit(0, sizeof(SSDataBlock));
46,143,651✔
326
  TSDB_CHECK_NULL(pBlocks, code, lino, END, terrno);
46,142,686✔
327
  pSchemas = taosArrayInit(0, sizeof(void*));
46,142,686✔
328
  TSDB_CHECK_NULL(pSchemas, code, lino, END, terrno);
46,145,536✔
329

330
  SSubmitTbData* pSubmitTbData = NULL;
46,145,536✔
331
  code = tqRetrieveTaosxBlock(pReader, pRsp, pBlocks, pSchemas, &pSubmitTbData, rawList, pHandle->fetchMeta);
46,142,227✔
332
  TSDB_CHECK_CODE(code, lino, END);
46,104,848✔
333
  bool tmp = (pSubmitTbData->flags & pRequest->sourceExcluded) != 0;
46,096,898✔
334
  TSDB_CHECK_CONDITION(!tmp, code, lino, END, TSDB_CODE_SUCCESS);
46,120,186✔
335

336
  if (pHandle->fetchMeta == ONLY_META){
46,120,186✔
337
    goto END;
945✔
338
  }
339

340
  int32_t blockNum = taosArrayGetSize(pBlocks) == 0 ? 1 : taosArrayGetSize(pBlocks);
46,138,195✔
341
  if (pRsp->withTbName) {
46,135,378✔
342
    int64_t uid = pExec->pTqReader->lastBlkUid;
46,134,451✔
343
    code = tqAddTbNameToRsp(pTq, uid, pRsp, blockNum);
46,137,080✔
344
    TSDB_CHECK_CODE(code, lino, END);
46,088,732✔
345
  }
346

347
  TSDB_CHECK_CONDITION(!tmp, code, lino, END, TSDB_CODE_SUCCESS);
46,086,418✔
348
  for (int32_t i = 0; i < blockNum; i++) {
92,215,803✔
349
    if (taosArrayGetSize(pBlocks) == 0){
46,069,834✔
350
      void* rawData = taosArrayGetP(rawList, pReader->nextBlk - 1);
×
351
      if (rawData == NULL) {
×
352
        continue;
×
353
      }
354
      if (tqAddRawDataToRsp(rawData, pRsp, pTq->pVnode->config.tsdbCfg.precision) != 0){
×
355
        tqError("vgId:%d, failed to add block to rsp msg", pTq->pVnode->config.vgId);
×
356
        continue;
×
357
      }
358
      *totalRows += *(uint32_t *)rawData + INT_BYTES; // bytes actually
×
359
    } else {
360
      SSDataBlock* pBlock = taosArrayGet(pBlocks, i);
46,109,451✔
361
      if (pBlock == NULL) {
46,096,942✔
362
        continue;
×
363
      }
364

365
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(pSchemas, i);
46,096,942✔
366
      if (tqAddBlockDataToRsp(pBlock, pRsp, pSW, pTq->pVnode->config.tsdbCfg.precision) != 0){
46,130,942✔
367
        tqError("vgId:%d, failed to add block to rsp msg", pTq->pVnode->config.vgId);
×
368
        continue;
×
369
      }
370
      *totalRows += pBlock->info.rows;
46,093,118✔
371
    }
372

373
    pRsp->blockNum++;
46,116,188✔
374
  }
375
  tqTrace("vgId:%d, process sub data success, response blocknum:%d, rows:%d", pTq->pVnode->config.vgId, pRsp->blockNum, *totalRows);
46,145,969✔
376
END:
46,154,864✔
377
  if (code != 0) {
46,080,343✔
378
    tqError("%s failed at %d, failed to process sub data:%s", __FUNCTION__, lino, tstrerror(code));
7,950✔
379
  }
380
  taosArrayDestroyEx(pBlocks, (FDelete)blockDataFreeRes);
46,080,343✔
381
  taosArrayDestroyP(pSchemas, (FDelete)tDeleteSchemaWrapper);
46,091,039✔
382
}
46,132,810✔
383

384
static void preProcessSubmitMsg(STqHandle* pHandle, const SMqPollReq* pRequest, SArray** rawList){
47,558,539✔
385
  STqExecHandle* pExec = &pHandle->execHandle;
47,558,539✔
386
  STqReader* pReader = pExec->pTqReader;
47,564,159✔
387
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
47,564,539✔
388
  for (int32_t i = 0; i < blockSz; i++){
95,126,093✔
389
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, i);
47,563,753✔
390
    if (pSubmitTbData== NULL){
47,563,610✔
391
      taosArrayDestroy(*rawList);
×
392
      *rawList = NULL;
×
393
      return;
×
394
    }
395

396
    int64_t uid = pSubmitTbData->uid;
47,563,610✔
397
    if (pRequest->rawData) {
47,564,693✔
398
      if (taosHashGet(pRequest->uidHash, &uid, LONG_BYTES) != NULL) {
×
399
        tqDebug("poll rawdata split,uid:%" PRId64 " is already exists", uid);
×
400
        terrno = TSDB_CODE_TMQ_RAW_DATA_SPLIT;
×
401
        return;
×
402
      } else {
403
        int32_t code = taosHashPut(pRequest->uidHash, &uid, LONG_BYTES, &uid, LONG_BYTES);
×
404
        if (code != 0) {
×
405
          tqError("failed to add table uid to hash, code:%d, uid:%" PRId64, code, uid);
×
406
        }
407
      }
408
    }
409

410
    if (pSubmitTbData->pCreateTbReq == NULL){
47,565,026✔
411
      continue;
46,919,939✔
412
    }
413

414
    int64_t createTime = INT64_MAX;
641,703✔
415
    int64_t *cTime = (int64_t*)taosHashGet(pHandle->tableCreateTimeHash, &uid, LONG_BYTES);
642,036✔
416
    if (cTime != NULL){
642,369✔
417
      createTime = *cTime;
2,961✔
418
    } else{
419
      createTime = metaGetTableCreateTime(pReader->pVnode->pMeta, uid, 1);
639,408✔
420
      if (createTime != INT64_MAX){
639,408✔
421
        int32_t code = taosHashPut(pHandle->tableCreateTimeHash, &uid, LONG_BYTES, &createTime, LONG_BYTES);
639,408✔
422
        if (code != 0){
639,408✔
423
          tqError("failed to add table create time to hash,code:%d, uid:%"PRId64, code, uid);
×
424
        }
425
      }
426
    }
427
    if (pHandle->fetchMeta == WITH_DATA || pSubmitTbData->ctimeMs > createTime){
642,369✔
428
      tDestroySVSubmitCreateTbReq(pSubmitTbData->pCreateTbReq, TSDB_MSG_FLG_DECODE);
637,605✔
429
      taosMemoryFreeClear(pSubmitTbData->pCreateTbReq);
636,606✔
430
    } else{
431
      taosArrayDestroy(*rawList);
4,764✔
432
      *rawList = NULL;
4,764✔
433
    }
434
  }
435
}
436

437
int32_t tqTaosxScanLog(STQ* pTq, STqHandle* pHandle, SPackedData submit, SMqDataRsp* pRsp, int32_t* totalRows, const SMqPollReq* pRequest) {
47,551,624✔
438
  int32_t code = 0;
47,551,624✔
439
  int32_t lino = 0;
47,551,624✔
440
  SDecoder decoder = {0};
47,551,624✔
441
  STqExecHandle* pExec = &pHandle->execHandle;
47,566,429✔
442
  STqReader* pReader = pExec->pTqReader;
47,566,114✔
443
  SArray *rawList = NULL;
47,566,744✔
444
  if (pRequest->rawData){
47,566,114✔
445
    rawList = taosArrayInit(0, POINTER_BYTES);
×
446
    TSDB_CHECK_NULL(rawList, code, lino, END, terrno);
×
447
  }
448
  code = tqReaderSetSubmitMsg(pReader, submit.msgStr, submit.msgLen, submit.ver, rawList, &decoder);
47,566,429✔
449
  TSDB_CHECK_CODE(code, lino, END);
47,560,915✔
450
  preProcessSubmitMsg(pHandle, pRequest, &rawList);
47,560,915✔
451
  // data could not contains same uid data in rawdata mode
452
  if (pRequest->rawData != 0 && terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
47,554,621✔
453
    goto END;
×
454
  }
455

456
  // this submit data is metadata and previous data is rawdata
457
  if (pRequest->rawData != 0 && *totalRows > 0 && pRsp->createTableNum == 0 && rawList == NULL){
47,562,823✔
458
    tqDebug("poll rawdata split,vgId:%d, this wal submit data contains metadata and previous data is data", pTq->pVnode->config.vgId);
×
459
    terrno = TSDB_CODE_TMQ_RAW_DATA_SPLIT;
×
460
    goto END;
×
461
  }
462

463
  // this submit data is rawdata and previous data is metadata
464
  if (pRequest->rawData != 0 && pRsp->createTableNum > 0 && rawList != NULL){
47,553,173✔
465
    tqDebug("poll rawdata split,vgId:%d, this wal submit data is data and previous data is metadata", pTq->pVnode->config.vgId);
×
466
    terrno = TSDB_CODE_TMQ_RAW_DATA_SPLIT;
×
467
    goto END;
×
468
  }
469

470
  if (pExec->subType == TOPIC_SUB_TYPE__TABLE) {
47,552,981✔
471
    while (tqNextBlockImpl(pReader, NULL)) {
24,647,980✔
472
      tqProcessSubData(pTq, pHandle, pRsp, totalRows, pRequest, rawList);
11,717,314✔
473
    }
474
  } else if (pExec->subType == TOPIC_SUB_TYPE__DB) {
34,620,974✔
475
    while (tqNextDataBlockFilterOut(pReader, pExec->execDb.pFilterOutTbUid)) {
69,006,104✔
476
      tqProcessSubData(pTq, pHandle, pRsp, totalRows, pRequest, rawList);
34,429,690✔
477
    }
478
  }
479

480
END:
47,494,771✔
481
  tDecoderClear(&decoder);
47,523,434✔
482
  tqReaderClearSubmitMsg(pReader);
47,552,312✔
483
  taosArrayDestroy(rawList);
47,561,595✔
484
  if (code != 0){
47,560,965✔
485
    tqError("%s failed at %d, failed to scan log:%s", __FUNCTION__, lino, tstrerror(code));
×
486
  }
487
  return code;
47,560,965✔
488
}
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

© 2026 Coveralls, Inc