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

taosdata / TDengine / #5009

29 Mar 2026 04:32AM UTC coverage: 72.26% (+0.02%) from 72.241%
#5009

push

travis-ci

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

253662 of 351039 relevant lines covered (72.26%)

131649114.75 hits per line

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

81.93
/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) {
49,637,641✔
46
  int32_t code = 0;
49,637,641✔
47
  int32_t lino = 0;
49,637,641✔
48
  SSchemaWrapper* pSchema = NULL;
49,637,641✔
49
  
50
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
49,654,023✔
51
  int32_t dataStrLen = sizeof(SRetrieveTableRspForTmq) + dataEncodeBufSize;
49,699,753✔
52
  void*   buf = taosMemoryCalloc(1, dataStrLen);
49,699,753✔
53
  TSDB_CHECK_NULL(buf, code, lino, END, terrno);
49,624,256✔
54

55
  SRetrieveTableRspForTmq* pRetrieve = (SRetrieveTableRspForTmq*)buf;
49,624,256✔
56
  pRetrieve->version = RETRIEVE_TABLE_RSP_TMQ_VERSION;
49,624,256✔
57
  pRetrieve->precision = precision;
49,629,743✔
58
  pRetrieve->compressed = 0;
49,645,995✔
59
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
49,635,569✔
60

61
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data, dataEncodeBufSize, pSW->nCols);
49,675,873✔
62
  TSDB_CHECK_CONDITION(actualLen >= 0, code, lino, END, terrno);
49,638,501✔
63

64
  actualLen += sizeof(SRetrieveTableRspForTmq);
49,638,501✔
65
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockDataLen, &actualLen), code, lino, END, terrno);
99,327,645✔
66
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockData, &buf), code, lino, END, terrno);
99,382,841✔
67
  pSchema = tCloneSSchemaWrapper(pSW);
49,665,614✔
68
  TSDB_CHECK_NULL(pSchema, code, lino, END, terrno);
49,665,614✔
69
  TSDB_CHECK_NULL(taosArrayPush(pRsp->blockSchema, &pSchema), code, lino, END, terrno);
99,357,246✔
70
  pSchema = NULL;
49,691,632✔
71
  pRsp->blockDataElementFree = true;
49,691,632✔
72
  tqTrace("tqAddBlockDataToRsp add block data to block array, blockDataLen:%d, blockData:%p", dataStrLen, buf);
49,680,341✔
73

74
END:
49,680,341✔
75
  tDeleteSchemaWrapper(pSchema);
49,657,605✔
76
  if (code != TSDB_CODE_SUCCESS){
49,635,483✔
77
    taosMemoryFree(buf);
×
78
    tqError("%s failed at line %d with msg:%s", __func__, lino, tstrerror(code));
×
79
  }
80
  return code;
49,635,483✔
81
}
82

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

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

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

93
  code = metaReaderGetTableEntryByUidCache(&mr, uid);
46,239,634✔
94
  TSDB_CHECK_CODE(code, lino, END);
46,227,062✔
95

96
  for (int32_t i = 0; i < n; i++) {
92,499,840✔
97
    char* tbName = taosStrdup(mr.me.name);
46,187,636✔
98
    TSDB_CHECK_NULL(tbName, code, lino, END, terrno);
46,238,698✔
99
    if(taosArrayPush(pRsp->blockTbName, &tbName) == NULL){
92,510,216✔
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,271,518✔
104
  }
105

106
END:
46,312,565✔
107
  if (code != TSDB_CODE_SUCCESS) {
46,242,943✔
108
    tqError("%s failed at %d, failed to add tbName to response:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
676✔
109
  }
110
  metaReaderClear(&mr);
46,242,943✔
111
  return code;
46,240,266✔
112
}
113

114
int32_t getDataBlock(qTaskInfo_t task, const STqHandle* pHandle, int32_t vgId, SSDataBlock** res) {
9,500,030✔
115
  if (task == NULL || pHandle == NULL || res == NULL) {
9,500,030✔
116
    return TSDB_CODE_INVALID_PARA;
×
117
  }
118
  uint64_t ts = 0;
9,505,340✔
119
  qStreamSetOpen(task);
9,503,762✔
120

121
  tqDebug("consumer:0x%" PRIx64 " vgId:%d, tmq one task start execute", pHandle->consumerId, vgId);
9,483,126✔
122
  int32_t code = qExecTask(task, res, &ts);
9,501,524✔
123
  if (code != TSDB_CODE_SUCCESS) {
9,509,309✔
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);
9,509,309✔
128
  return code;
9,509,230✔
129
}
130

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

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

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

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

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

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

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

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

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

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

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

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

197
  int32_t vgId = TD_VID(pTq->pVnode);
9,774,905✔
198
  int32_t totalRows = 0;
9,777,971✔
199

200
  const STqExecHandle* pExec = &pHandle->execHandle;
9,777,971✔
201
  qTaskInfo_t          task = pExec->task;
9,771,431✔
202

203
  code = qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
9,774,683✔
204
  TSDB_CHECK_CODE(code, lino, END);
9,770,206✔
205

206
  qStreamSetParams(task, pRequest->sourceExcluded, pRequest->minPollRows, pRequest->timeout, pRequest->enableReplay);
9,497,803✔
207
  do {
208
    SSDataBlock* pDataBlock = NULL;
9,496,117✔
209
    code = getDataBlock(task, pHandle, vgId, &pDataBlock);
9,496,169✔
210
    TSDB_CHECK_CODE(code, lino, END);
9,508,111✔
211

212
    if (pRequest->enableReplay) {
9,508,111✔
213
      code = tqProcessReplayRsp(pTq, pHandle, pRsp, pRequest, pDataBlock, task);
15,688✔
214
      TSDB_CHECK_CODE(code, lino, END);
15,688✔
215
      break;
15,688✔
216
    }
217
    if (pDataBlock == NULL) {
9,492,423✔
218
      break;
6,840,072✔
219
    }
220
    code = tqAddBlockDataToRsp(pDataBlock, pRsp, &pExec->execCol.pSW, pTq->pVnode->config.tsdbCfg.precision);
2,652,351✔
221
    TSDB_CHECK_CODE(code, lino, END);
2,652,351✔
222

223
    pRsp->blockNum++;
2,652,351✔
224
    totalRows += pDataBlock->info.rows;
2,652,351✔
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);
9,508,111✔
228
  code = qStreamExtractOffset(task, &pRsp->rspOffset);
9,508,431✔
229

230
END:
9,780,514✔
231
  if (code != 0) {
9,780,514✔
232
    tqError("%s failed at %d, tmq task executed error msg:%s", __FUNCTION__, lino, tstrerror(code));
272,403✔
233
  }
234
  return code;
9,780,514✔
235
}
236

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

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

248
  int32_t rowCnt = 0;
41,250✔
249
  int64_t st = taosGetTimestampMs();
41,250✔
250
  while (1) {
1,491,044✔
251
    SSDataBlock* pDataBlock = NULL;
1,532,294✔
252
    uint64_t     ts = 0;
1,532,294✔
253
    tqDebug("tmqsnap task start to execute");
1,532,294✔
254
    code = qExecTask(task, &pDataBlock, &ts);
1,532,609✔
255
    TSDB_CHECK_CODE(code, lino, END);
1,532,294✔
256
    tqDebug("tmqsnap task execute end, get %p", pDataBlock);
1,532,294✔
257

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

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

270
      pRsp->blockNum++;
761,285✔
271
      rowCnt += pDataBlock->info.rows;
761,285✔
272
      if (rowCnt <= pRequest->minPollRows && (taosGetTimestampMs() - st <= pRequest->timeout)) {
1,489,593✔
273
        continue;
728,308✔
274
      }
275
    }
276

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

287
    if (pDataBlock == NULL) {
802,273✔
288
      code = qStreamExtractOffset(task, pOffset);
769,296✔
289
      TSDB_CHECK_CODE(code, lino, END);
769,296✔
290

291
      if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
769,296✔
292
        continue;
762,736✔
293
      }
294

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

300
    if (pRsp->blockNum > 0) {
32,977✔
301
      tqDebug("tmqsnap task exec exited, get data");
32,977✔
302
      code = qStreamExtractOffset(task, &pRsp->rspOffset);
32,977✔
303
      break;
32,977✔
304
    }
305
  }
306
  tqDebug("%s:%d success", __FUNCTION__, lino);
41,250✔
307
END:
41,250✔
308
  if (code != 0){
41,250✔
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,250✔
312
  return code;
41,250✔
313
}
314

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

322
  STqExecHandle* pExec = &pHandle->execHandle;
46,276,229✔
323
  STqReader* pReader = pExec->pTqReader;
46,286,068✔
324

325
  pBlocks = taosArrayInit(0, sizeof(SSDataBlock));
46,274,654✔
326
  TSDB_CHECK_NULL(pBlocks, code, lino, END, terrno);
46,280,026✔
327
  pSchemas = taosArrayInit(0, sizeof(void*));
46,280,026✔
328
  TSDB_CHECK_NULL(pSchemas, code, lino, END, terrno);
46,286,205✔
329

330
  SSubmitTbData* pSubmitTbData = NULL;
46,286,205✔
331
  code = tqRetrieveTaosxBlock(pReader, pRsp, pBlocks, pSchemas, &pSubmitTbData, rawList, pHandle->fetchMeta);
46,283,273✔
332
  TSDB_CHECK_CODE(code, lino, END);
46,240,920✔
333
  bool tmp = (pSubmitTbData->flags & pRequest->sourceExcluded) != 0;
46,232,795✔
334
  TSDB_CHECK_CONDITION(!tmp, code, lino, END, TSDB_CODE_SUCCESS);
46,263,806✔
335

336
  if (pHandle->fetchMeta == ONLY_META){
46,263,806✔
337
    goto END;
951✔
338
  }
339

340
  int32_t blockNum = taosArrayGetSize(pBlocks) == 0 ? 1 : taosArrayGetSize(pBlocks);
46,278,928✔
341
  if (pRsp->withTbName) {
46,279,745✔
342
    int64_t uid = pExec->pTqReader->lastBlkUid;
46,273,444✔
343
    code = tqAddTbNameToRsp(pTq, uid, pRsp, blockNum);
46,276,214✔
344
    TSDB_CHECK_CODE(code, lino, END);
46,189,577✔
345
  }
346

347
  TSDB_CHECK_CONDITION(!tmp, code, lino, END, TSDB_CODE_SUCCESS);
46,192,321✔
348
  for (int32_t i = 0; i < blockNum; i++) {
92,450,676✔
349
    if (taosArrayGetSize(pBlocks) == 0){
46,184,186✔
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,244,044✔
361
      if (pBlock == NULL) {
46,232,428✔
362
        continue;
×
363
      }
364

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

373
    pRsp->blockNum++;
46,252,898✔
374
  }
375
  tqTrace("vgId:%d, process sub data success, response blocknum:%d, rows:%d", pTq->pVnode->config.vgId, pRsp->blockNum, *totalRows);
46,266,490✔
376
END:
46,276,242✔
377
  if (code != 0) {
46,205,426✔
378
    tqError("%s failed at %d, failed to process sub data:%s", __FUNCTION__, lino, tstrerror(code));
8,801✔
379
  }
380
  taosArrayDestroyEx(pBlocks, (FDelete)blockDataFreeRes);
46,205,426✔
381
  taosArrayDestroyP(pSchemas, (FDelete)tDeleteSchemaWrapper);
46,222,787✔
382
}
46,269,354✔
383

384
static void preProcessSubmitMsg(STqHandle* pHandle, const SMqPollReq* pRequest, SArray** rawList){
47,698,614✔
385
  STqExecHandle* pExec = &pHandle->execHandle;
47,698,614✔
386
  STqReader* pReader = pExec->pTqReader;
47,704,872✔
387
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
47,706,148✔
388
  for (int32_t i = 0; i < blockSz; i++){
95,412,181✔
389
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, i);
47,702,740✔
390
    if (pSubmitTbData== NULL){
47,705,760✔
391
      taosArrayDestroy(*rawList);
×
392
      *rawList = NULL;
×
393
      return;
×
394
    }
395

396
    int64_t uid = pSubmitTbData->uid;
47,705,760✔
397
    if (pRequest->rawData) {
47,709,612✔
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,709,535✔
411
      continue;
46,937,947✔
412
    }
413

414
    int64_t createTime = INT64_MAX;
768,988✔
415
    int64_t *cTime = (int64_t*)taosHashGet(pHandle->tableCreateTimeHash, &uid, LONG_BYTES);
768,988✔
416
    if (cTime != NULL){
769,326✔
417
      createTime = *cTime;
2,703✔
418
    } else{
419
      createTime = metaGetTableCreateTime(pReader->pVnode->pMeta, uid, 1);
766,623✔
420
      if (createTime != INT64_MAX){
765,948✔
421
        int32_t code = taosHashPut(pHandle->tableCreateTimeHash, &uid, LONG_BYTES, &createTime, LONG_BYTES);
766,623✔
422
        if (code != 0){
766,623✔
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){
768,651✔
428
      tDestroySVSubmitCreateTbReq(pSubmitTbData->pCreateTbReq, TSDB_MSG_FLG_DECODE);
766,681✔
429
      taosMemoryFreeClear(pSubmitTbData->pCreateTbReq);
766,344✔
430
    } else{
431
      taosArrayDestroy(*rawList);
2,645✔
432
      *rawList = NULL;
2,645✔
433
    }
434
  }
435
}
436

437
int32_t tqTaosxScanLog(STQ* pTq, STqHandle* pHandle, SPackedData submit, SMqDataRsp* pRsp, int32_t* totalRows, const SMqPollReq* pRequest) {
47,709,172✔
438
  int32_t code = 0;
47,709,172✔
439
  int32_t lino = 0;
47,709,172✔
440
  SDecoder decoder = {0};
47,709,172✔
441
  STqExecHandle* pExec = &pHandle->execHandle;
47,710,478✔
442
  STqReader* pReader = pExec->pTqReader;
47,710,478✔
443
  SArray *rawList = NULL;
47,710,117✔
444
  if (pRequest->rawData){
47,710,163✔
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,708,903✔
449
  TSDB_CHECK_CODE(code, lino, END);
47,704,854✔
450
  preProcessSubmitMsg(pHandle, pRequest, &rawList);
47,704,854✔
451
  // data could not contains same uid data in rawdata mode
452
  if (pRequest->rawData != 0 && terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
47,686,657✔
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,706,807✔
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,694,113✔
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,693,114✔
471
    while (tqNextBlockImpl(pReader, NULL)) {
24,599,170✔
472
      tqProcessSubData(pTq, pHandle, pRsp, totalRows, pRequest, rawList);
11,691,176✔
473
    }
474
  } else if (pExec->subType == TOPIC_SUB_TYPE__DB) {
34,785,320✔
475
    while (tqNextDataBlockFilterOut(pReader, pExec->execDb.pFilterOutTbUid)) {
69,322,279✔
476
      tqProcessSubData(pTq, pHandle, pRsp, totalRows, pRequest, rawList);
34,591,991✔
477
    }
478
  }
479

480
END:
47,626,982✔
481
  tDecoderClear(&decoder);
47,668,612✔
482
  tqReaderClearSubmitMsg(pReader);
47,696,249✔
483
  taosArrayDestroy(rawList);
47,704,804✔
484
  if (code != 0){
47,703,275✔
485
    tqError("%s failed at %d, failed to scan log:%s", __FUNCTION__, lino, tstrerror(code));
×
486
  }
487
  return code;
47,703,275✔
488
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc