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

taosdata / TDengine / #3616

19 Feb 2025 11:22AM UTC coverage: 63.315% (+0.4%) from 62.953%
#3616

push

travis-ci

web-flow
Merge pull request #29823 from taosdata/feat/TS-5928

fix:[TS-5928]add consumer parameters

148228 of 300186 branches covered (49.38%)

Branch coverage included in aggregate %.

232358 of 300909 relevant lines covered (77.22%)

17990742.15 hits per line

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

64.82
/source/dnode/vnode/src/tq/tqRead.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 "tmsg.h"
17
#include "tq.h"
18

19
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
677✔
20
  if (pHandle == NULL || pHead == NULL) {
677!
21
    return false;
×
22
  }
23
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
677✔
24
    return true;
673✔
25
  }
26

27
  int16_t msgType = pHead->msgType;
4✔
28
  char*   body = pHead->body;
4✔
29
  int32_t bodyLen = pHead->bodyLen;
4✔
30

31
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
4✔
32
  int64_t  realTbSuid = 0;
4✔
33
  SDecoder dcoder = {0};
4✔
34
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
4✔
35
  int32_t  len = bodyLen - sizeof(SMsgHead);
4✔
36
  tDecoderInit(&dcoder, data, len);
4✔
37

38
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
4!
39
    SVCreateStbReq req = {0};
2✔
40
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
2!
41
      goto end;
×
42
    }
43
    realTbSuid = req.suid;
2✔
44
  } else if (msgType == TDMT_VND_DROP_STB) {
2!
45
    SVDropStbReq req = {0};
×
46
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
47
      goto end;
×
48
    }
49
    realTbSuid = req.suid;
×
50
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
2!
51
    SVCreateTbBatchReq req = {0};
2✔
52
    if (tDecodeSVCreateTbBatchReq(&dcoder, &req) < 0) {
2!
53
      goto end;
×
54
    }
55

56
    int32_t        needRebuild = 0;
2✔
57
    SVCreateTbReq* pCreateReq = NULL;
2✔
58
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
4✔
59
      pCreateReq = req.pReqs + iReq;
2✔
60
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid) {
2!
61
        needRebuild++;
×
62
      }
63
    }
64
    if (needRebuild == 0) {
2!
65
      // do nothing
66
    } else if (needRebuild == req.nReqs) {
×
67
      realTbSuid = tbSuid;
×
68
    } else {
69
      realTbSuid = tbSuid;
×
70
      SVCreateTbBatchReq reqNew = {0};
×
71
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
×
72
      if (reqNew.pArray == NULL) {
×
73
        tDeleteSVCreateTbBatchReq(&req);
×
74
        goto end;
×
75
      }
76
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
77
        pCreateReq = req.pReqs + iReq;
×
78
        if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid) {
×
79
          reqNew.nReqs++;
×
80
          if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
×
81
            taosArrayDestroy(reqNew.pArray);
×
82
            tDeleteSVCreateTbBatchReq(&req);
×
83
            goto end;
×
84
          }
85
        }
86
      }
87

88
      int     tlen = 0;
×
89
      int32_t ret = 0;
×
90
      tEncodeSize(tEncodeSVCreateTbBatchReq, &reqNew, tlen, ret);
×
91
      void* buf = taosMemoryMalloc(tlen);
×
92
      if (NULL == buf) {
×
93
        taosArrayDestroy(reqNew.pArray);
×
94
        tDeleteSVCreateTbBatchReq(&req);
×
95
        goto end;
×
96
      }
97
      SEncoder coderNew = {0};
×
98
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
99
      ret = tEncodeSVCreateTbBatchReq(&coderNew, &reqNew);
×
100
      tEncoderClear(&coderNew);
×
101
      if (ret < 0) {
×
102
        taosMemoryFree(buf);
×
103
        taosArrayDestroy(reqNew.pArray);
×
104
        tDeleteSVCreateTbBatchReq(&req);
×
105
        goto end;
×
106
      }
107
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
108
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
109
      taosMemoryFree(buf);
×
110
      taosArrayDestroy(reqNew.pArray);
×
111
    }
112

113
    tDeleteSVCreateTbBatchReq(&req);
2✔
114
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
×
115
    SVAlterTbReq req = {0};
×
116

117
    if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
×
118
      goto end;
×
119
    }
120

121
    SMetaReader mr = {0};
×
122
    metaReaderDoInit(&mr, pHandle->execHandle.pTqReader->pVnodeMeta, META_READER_LOCK);
×
123

124
    if (metaGetTableEntryByName(&mr, req.tbName) < 0) {
×
125
      metaReaderClear(&mr);
×
126
      goto end;
×
127
    }
128
    realTbSuid = mr.me.ctbEntry.suid;
×
129
    metaReaderClear(&mr);
×
130
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
131
    SVDropTbBatchReq req = {0};
×
132

133
    if (tDecodeSVDropTbBatchReq(&dcoder, &req) < 0) {
×
134
      goto end;
×
135
    }
136

137
    int32_t      needRebuild = 0;
×
138
    SVDropTbReq* pDropReq = NULL;
×
139
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
140
      pDropReq = req.pReqs + iReq;
×
141

142
      if (pDropReq->suid == tbSuid) {
×
143
        needRebuild++;
×
144
      }
145
    }
146
    if (needRebuild == 0) {
×
147
      // do nothing
148
    } else if (needRebuild == req.nReqs) {
×
149
      realTbSuid = tbSuid;
×
150
    } else {
151
      realTbSuid = tbSuid;
×
152
      SVDropTbBatchReq reqNew = {0};
×
153
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbReq));
×
154
      if (reqNew.pArray == NULL) {
×
155
        goto end;
×
156
      }
157
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
158
        pDropReq = req.pReqs + iReq;
×
159
        if (pDropReq->suid == tbSuid) {
×
160
          reqNew.nReqs++;
×
161
          if (taosArrayPush(reqNew.pArray, pDropReq) == NULL) {
×
162
            taosArrayDestroy(reqNew.pArray);
×
163
            goto end;
×
164
          }
165
        }
166
      }
167

168
      int     tlen = 0;
×
169
      int32_t ret = 0;
×
170
      tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, ret);
×
171
      void* buf = taosMemoryMalloc(tlen);
×
172
      if (NULL == buf) {
×
173
        taosArrayDestroy(reqNew.pArray);
×
174
        goto end;
×
175
      }
176
      SEncoder coderNew = {0};
×
177
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
178
      ret = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
×
179
      tEncoderClear(&coderNew);
×
180
      if (ret != 0) {
×
181
        taosMemoryFree(buf);
×
182
        taosArrayDestroy(reqNew.pArray);
×
183
        goto end;
×
184
      }
185
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
186
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
187
      taosMemoryFree(buf);
×
188
      taosArrayDestroy(reqNew.pArray);
×
189
    }
190
  } else if (msgType == TDMT_VND_DELETE) {
×
191
    SDeleteRes req = {0};
×
192
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
193
      goto end;
×
194
    }
195
    realTbSuid = req.suid;
×
196
  }
197

198
end:
×
199
  tDecoderClear(&dcoder);
4✔
200
  bool tmp = tbSuid == realTbSuid;
4✔
201
  tqDebug("%s suid:%"PRId64" realSuid:%"PRId64" return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
4!
202
  return tmp;
4✔
203
}
204

205
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
133,562✔
206
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
133,562!
207
    return -1;
×
208
  }
209
  int32_t code = -1;
133,658✔
210
  int32_t vgId = TD_VID(pTq->pVnode);
133,658✔
211
  int64_t id = pHandle->pWalReader->readerId;
133,658✔
212

213
  int64_t offset = *fetchOffset;
133,658✔
214
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
133,658✔
215
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
133,677✔
216
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
133,712✔
217

218
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
133,761!
219
          ", 0x%" PRIx64,
220
          vgId, offset, lastVer, committedVer, appliedVer, id);
221

222
  while (offset <= appliedVer) {
143,151✔
223
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
131,521!
224
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
225
              ", no more log to return,QID:0x%" PRIx64 " 0x%" PRIx64,
226
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
227
      goto END;
×
228
    }
229

230
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type: %s,QID:0x%" PRIx64 " 0x%" PRIx64,
131,506!
231
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
232

233
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
131,515✔
234
      code = walFetchBody(pHandle->pWalReader);
121,632✔
235
      goto END;
121,627✔
236
    } else {
237
      if (pHandle->fetchMeta != WITH_DATA) {
9,883✔
238
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
837✔
239
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
837✔
240
          code = walFetchBody(pHandle->pWalReader);
677✔
241
          if (code < 0) {
677!
242
            goto END;
×
243
          }
244

245
          pHead = &(pHandle->pWalReader->pHead->head);
677✔
246
          if (isValValidForTable(pHandle, pHead)) {
677✔
247
            code = 0;
674✔
248
            goto END;
674✔
249
          } else {
250
            offset++;
3✔
251
            code = -1;
3✔
252
            continue;
3✔
253
          }
254
        }
255
      }
256
      code = walSkipFetchBody(pHandle->pWalReader);
9,206✔
257
      if (code < 0) {
9,207!
258
        goto END;
×
259
      }
260
      offset++;
9,207✔
261
    }
262
    code = -1;
9,207✔
263
  }
264

265
END:
11,630✔
266
  *fetchOffset = offset;
133,931✔
267
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64 ", 0x%" PRIx64,
133,931✔
268
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
269
  return code;
133,938✔
270
}
271

272
bool tqGetTablePrimaryKey(STqReader* pReader) {
20,314✔
273
  if (pReader == NULL) {
20,314!
274
    return false;
×
275
  }
276
  return pReader->hasPrimaryKey;
20,314✔
277
}
278

279
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
179✔
280
  tqDebug("%s:%p uid:%"PRId64, __FUNCTION__ , pReader, uid);
179!
281

282
  if (pReader == NULL) {
179!
283
    return;
×
284
  }
285
  bool            ret = false;
179✔
286
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnodeMeta, uid, -1, 1);
179✔
287
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
178!
288
    ret = true;
×
289
  }
290
  tDeleteSchemaWrapper(schema);
291
  pReader->hasPrimaryKey = ret;
179✔
292
}
293

294
STqReader* tqReaderOpen(SVnode* pVnode) {
8,722✔
295
  tqDebug("%s:%p", __FUNCTION__ , pVnode);
8,722✔
296
  if (pVnode == NULL) {
8,729!
297
    return NULL;
×
298
  }
299
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
8,729!
300
  if (pReader == NULL) {
8,728!
301
    return NULL;
×
302
  }
303

304
  pReader->pWalReader = walOpenReader(pVnode->pWal, NULL, 0);
8,728✔
305
  if (pReader->pWalReader == NULL) {
8,729!
306
    taosMemoryFree(pReader);
×
307
    return NULL;
×
308
  }
309

310
  pReader->pVnodeMeta = pVnode->pMeta;
8,729✔
311
  pReader->pColIdList = NULL;
8,729✔
312
  pReader->cachedSchemaVer = 0;
8,729✔
313
  pReader->cachedSchemaSuid = 0;
8,729✔
314
  pReader->pSchemaWrapper = NULL;
8,729✔
315
  pReader->tbIdHash = NULL;
8,729✔
316
  pReader->pResBlock = NULL;
8,729✔
317

318
  int32_t code = createDataBlock(&pReader->pResBlock);
8,729✔
319
  if (code) {
8,729!
320
    terrno = code;
×
321
  }
322

323
  return pReader;
8,729✔
324
}
325

326
void tqReaderClose(STqReader* pReader) {
8,763✔
327
  tqDebug("%s:%p", __FUNCTION__ , pReader);
8,763✔
328
  if (pReader == NULL) return;
8,766✔
329

330
  // close wal reader
331
  if (pReader->pWalReader) {
8,727!
332
    walCloseReader(pReader->pWalReader);
8,727✔
333
  }
334

335
  if (pReader->pSchemaWrapper) {
8,727✔
336
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
2,066!
337
  }
338

339
  if (pReader->pColIdList) {
8,727✔
340
    taosArrayDestroy(pReader->pColIdList);
8,404✔
341
  }
342

343
  // free hash
344
  blockDataDestroy(pReader->pResBlock);
8,728✔
345
  taosHashCleanup(pReader->tbIdHash);
8,727✔
346
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
8,727✔
347
  taosMemoryFree(pReader);
8,727!
348
}
349

350
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
6,130✔
351
  if (pReader == NULL) {
6,130!
352
    return TSDB_CODE_INVALID_PARA;
×
353
  }
354
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
6,130✔
355
    return terrno;
140✔
356
  }
357
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
5,990!
358
  return 0;
5,990✔
359
}
360

361
int32_t extractMsgFromWal(SWalReader* pReader, void** pItem, int64_t maxVer, const char* id) {
532,657✔
362
  int32_t code = 0;
532,657✔
363

364
  while (1) {
8,907✔
365
    TAOS_CHECK_RETURN(walNextValidMsg(pReader));
541,564✔
366

367
    SWalCont* pCont = &pReader->pHead->head;
471,414✔
368
    int64_t   ver = pCont->version;
471,414✔
369
    if (ver > maxVer) {
471,414✔
370
      tqDebug("maxVer in WAL:%" PRId64 " reached, current:%" PRId64 ", do not scan wal anymore, %s", maxVer, ver, id);
327✔
371
      return TSDB_CODE_SUCCESS;
327✔
372
    }
373

374
    if (pCont->msgType == TDMT_VND_SUBMIT) {
471,087✔
375
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SSubmitReq2Msg));
459,610✔
376
      int32_t len = pCont->bodyLen - sizeof(SSubmitReq2Msg);
459,610✔
377

378
      void* data = taosMemoryMalloc(len);
459,610!
379
      if (data == NULL) {
459,609!
380
        // todo: for all stream in this vnode, keep this offset in the offset files, and wait for a moment, and then
381
        // retry
382
        tqError("vgId:%d, failed to copy submit data for stream processing, since out of memory", 0);
×
383
        return terrno;
×
384
      }
385

386
      (void)memcpy(data, pBody, len);
459,609✔
387
      SPackedData data1 = (SPackedData){.ver = ver, .msgLen = len, .msgStr = data};
459,609✔
388

389
      code = streamDataSubmitNew(&data1, STREAM_INPUT__DATA_SUBMIT, (SStreamDataSubmit**)pItem);
459,609✔
390
      if (code != 0) {
459,602!
391
        tqError("%s failed to create data submit for stream since out of memory", id);
×
392
        return code;
×
393
      }
394
    } else if (pCont->msgType == TDMT_VND_DELETE) {
11,477✔
395
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SMsgHead));
11,313✔
396
      int32_t len = pCont->bodyLen - sizeof(SMsgHead);
11,313✔
397
      EStreamType blockType = STREAM_DELETE_DATA;
11,313✔
398
      code = tqExtractDelDataBlock(pBody, len, ver, (void**)pItem, 0, blockType);
11,313✔
399
      if (code == TSDB_CODE_SUCCESS) {
11,308!
400
        if (*pItem == NULL) {
11,308✔
401
          tqDebug("s-task:%s empty delete msg, discard it, len:%d, ver:%" PRId64, id, len, ver);
8,907✔
402
          // we need to continue check next data in the wal files.
403
          continue;
8,907✔
404
        } else {
405
          tqDebug("s-task:%s delete msg extract from WAL, len:%d, ver:%" PRId64, id, len, ver);
2,401✔
406
        }
407
      } else {
408
        terrno = code;
×
409
        tqError("s-task:%s extract delete msg from WAL failed, code:%s", id, tstrerror(code));
×
410
        return code;
×
411
      }
412

413
    } else if (pCont->msgType == TDMT_VND_DROP_TABLE && pReader->cond.scanDropCtb) {
349!
414
      void* pBody = POINTER_SHIFT(pCont->body, sizeof(SMsgHead));
185✔
415
      int32_t len = pCont->bodyLen - sizeof(SMsgHead);
185✔
416
      code = tqExtractDropCtbDataBlock(pBody, len, ver, (void**)pItem, 0);
185✔
417
      if (TSDB_CODE_SUCCESS == code) {
185!
418
        if (!*pItem) {
185!
419
          continue;
×
420
        } else {
421
          tqDebug("s-task:%s drop ctb msg extract from WAL, len:%d, ver:%"PRId64, id, len, ver);
185!
422
        }
423
      } else {
424
        terrno = code;
×
425
        return code;
×
426
      }
427
    } else {
428
      tqError("s-task:%s invalid msg type:%d, ver:%" PRId64, id, pCont->msgType, ver);
×
429
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
430
    }
431

432
    return code;
462,180✔
433
  }
434
}
435

436
bool tqNextBlockInWal(STqReader* pReader, const char* id, int sourceExcluded) {
510,745✔
437
  if (pReader == NULL) {
510,745!
438
    return false;
×
439
  }
440
  SWalReader* pWalReader = pReader->pWalReader;
510,745✔
441

442
  int64_t st = taosGetTimestampMs();
510,743✔
443
  while (1) {
599,781✔
444
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
1,110,524✔
445
    while (pReader->nextBlk < numOfBlocks) {
1,453,038✔
446
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
801,285✔
447
              pReader->msg.ver);
448

449
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
801,285✔
450
      if (pSubmitTbData == NULL) {
801,239✔
451
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
2!
452
                pReader->msg.ver);
453
        return false;
×
454
      }
455
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
801,237✔
456
        pReader->nextBlk += 1;
132✔
457
        continue;
132✔
458
      }
459
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
801,102!
460
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
458,696✔
461
        SSDataBlock* pRes = NULL;
458,696✔
462
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
458,696✔
463
        if (code == TSDB_CODE_SUCCESS) {
458,757!
464
          return true;
458,760✔
465
        }
466
      } else {
467
        pReader->nextBlk += 1;
342,519✔
468
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
342,519!
469
      }
470
    }
471

472
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
651,753✔
473
    pReader->msg.msgStr = NULL;
651,814✔
474

475
    int64_t elapsed = taosGetTimestampMs() - st;
651,801✔
476
    if (elapsed > 1000 || elapsed < 0) {
651,801!
477
      return false;
2✔
478
    }
479

480
    // try next message in wal file
481
    if (walNextValidMsg(pWalReader) < 0) {
651,799✔
482
      return false;
51,984✔
483
    }
484

485
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
599,779✔
486
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
599,779✔
487
    int64_t ver = pWalReader->pHead->head.version;
599,779✔
488
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL) != 0) {
599,779!
489
      return false;
×
490
    }
491
    pReader->nextBlk = 0;
599,781✔
492
  }
493
}
494

495
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList) {
1,181,001✔
496
  if (pReader == NULL) {
1,181,001!
497
    return TSDB_CODE_INVALID_PARA;
×
498
  }
499
  pReader->msg.msgStr = msgStr;
1,181,001✔
500
  pReader->msg.msgLen = msgLen;
1,181,001✔
501
  pReader->msg.ver = ver;
1,181,001✔
502

503
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
1,181,001✔
504
  SDecoder decoder = {0};
1,181,001✔
505

506
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
1,181,001✔
507
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit, rawList);
1,180,556✔
508
  tDecoderClear(&decoder);
1,180,687✔
509

510
  if (code != 0) {
1,181,177!
511
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
512
  }
513

514
  return code;
1,181,184✔
515
}
516

517
void tqReaderClearSubmitMsg(STqReader *pReader) {
702,108✔
518
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
702,108✔
519
  pReader->nextBlk = 0;
702,553✔
520
  pReader->msg.msgStr = NULL;
702,553✔
521
}
702,553✔
522

523

524
SWalReader* tqGetWalReader(STqReader* pReader) {
570,325✔
525
  if (pReader == NULL) {
570,325!
526
    return NULL;
×
527
  }
528
  return pReader->pWalReader;
570,325✔
529
}
530

531
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
510,728✔
532
  if (pReader == NULL) {
510,728!
533
    return NULL;
×
534
  }
535
  return pReader->pResBlock;
510,728✔
536
}
537

538
int64_t tqGetResultBlockTime(STqReader* pReader) {
510,705✔
539
  if (pReader == NULL) {
510,705!
540
    return 0;
×
541
  }
542
  return pReader->lastTs;
510,705✔
543
}
544

545
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
913,195✔
546
  int32_t code = false;
913,195✔
547
  int32_t lino = 0;
913,195✔
548
  int64_t uid = 0;
913,195✔
549
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
913,195!
550
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
913,195!
551
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
913,195!
552

553
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
913,195✔
554
  while (pReader->nextBlk < blockSz) {
1,034,110✔
555
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
537,947✔
556
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
537,939!
557
    uid = pSubmitTbData->uid;
537,939✔
558
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
537,939✔
559
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
537,975✔
560

561
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64 "", pReader->nextBlk, blockSz, uid);
120,491✔
562
    pReader->nextBlk++;
120,504✔
563
  }
564

565
  tqReaderClearSubmitMsg(pReader);
496,163✔
566
  tqTrace("iterator data block end, total block num:%d, uid:%"PRId64, blockSz, uid);
496,309✔
567

568
END:
490,539✔
569
  tqTrace("%s:%d return:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
913,793✔
570
  return code;
913,541✔
571
}
572

573
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
169,850✔
574
  int32_t code = false;
169,850✔
575
  int32_t lino = 0;
169,850✔
576
  int64_t uid = 0;
169,850✔
577

578
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
169,850!
579
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
169,850!
580
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
169,850!
581

582
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
169,850✔
583
  while (pReader->nextBlk < blockSz) {
169,837✔
584
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
85,071✔
585
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
85,068!
586
    uid = pSubmitTbData->uid;
85,068✔
587
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
85,068✔
588
    TSDB_CHECK_NULL(ret, code, lino, END, true);
85,066!
589
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64 "", pReader->nextBlk, blockSz, uid);
×
590
    pReader->nextBlk++;
×
591
  }
592
  tqReaderClearSubmitMsg(pReader);
84,766✔
593
  tqTrace("iterator data block end, total block num:%d, uid:%"PRId64, blockSz, uid);
84,913!
594

595
END:
84,913✔
596
  tqTrace("%s:%d get data:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
169,980!
597
  return code;
169,834✔
598
}
599

600
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask) {
117,563✔
601
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
117,563!
602
    return TSDB_CODE_INVALID_PARA;
×
603
  }
604
  int32_t code = 0;
117,614✔
605

606
  int32_t cnt = 0;
117,614✔
607
  for (int32_t i = 0; i < pSrc->nCols; i++) {
700,909✔
608
    cnt += mask[i];
583,295✔
609
  }
610

611
  pDst->nCols = cnt;
117,614✔
612
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
117,614!
613
  if (pDst->pSchema == NULL) {
117,555!
614
    return TAOS_GET_TERRNO(terrno);
×
615
  }
616

617
  int32_t j = 0;
117,555✔
618
  for (int32_t i = 0; i < pSrc->nCols; i++) {
699,672✔
619
    if (mask[i]) {
582,154✔
620
      pDst->pSchema[j++] = pSrc->pSchema[i];
582,128✔
621
      SColumnInfoData colInfo =
622
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
582,128✔
623
      code = blockDataAppendColInfo(pBlock, &colInfo);
583,095✔
624
      if (code != 0) {
582,091!
625
        return code;
×
626
      }
627
    }
628
  }
629
  return 0;
117,518✔
630
}
631

632
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
1,894✔
633
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
1,894!
634
    return TSDB_CODE_INVALID_PARA;
×
635
  }
636
  SSDataBlock* pBlock = pReader->pResBlock;
1,894✔
637
  if (blockDataGetNumOfCols(pBlock) > 0) {
1,894✔
638
      blockDataDestroy(pBlock);
2✔
639
      int32_t code = createDataBlock(&pReader->pResBlock);
2✔
640
      if (code) {
2!
641
        return code;
×
642
      }
643
      pBlock = pReader->pResBlock;
2✔
644

645
      pBlock->info.id.uid = pReader->cachedSchemaUid;
2✔
646
      pBlock->info.version = pReader->msg.ver;
2✔
647
  }
648

649
  int32_t numOfCols = taosArrayGetSize(pColIdList);
1,894✔
650

651
  if (numOfCols == 0) {  // all columns are required
1,894!
652
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
653
      SSchema*        pColSchema = &pSchema->pSchema[i];
×
654
      SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
×
655

656
      int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
×
657
      if (code != TSDB_CODE_SUCCESS) {
×
658
        blockDataFreeRes(pBlock);
×
659
        return terrno;
×
660
      }
661
    }
662
  } else {
663
    if (numOfCols > pSchema->nCols) {
1,894✔
664
      numOfCols = pSchema->nCols;
2✔
665
    }
666

667
    int32_t i = 0;
1,894✔
668
    int32_t j = 0;
1,894✔
669
    while (i < pSchema->nCols && j < numOfCols) {
15,087✔
670
      SSchema* pColSchema = &pSchema->pSchema[i];
13,192✔
671
      col_id_t colIdSchema = pColSchema->colId;
13,192✔
672

673
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
13,192✔
674
      if (pColIdNeed == NULL) {
13,191!
675
        break;
×
676
      }
677
      if (colIdSchema < *pColIdNeed) {
13,191✔
678
        i++;
1,652✔
679
      } else if (colIdSchema > *pColIdNeed) {
11,539!
680
        j++;
×
681
      } else {
682
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
11,539✔
683
        int32_t         code = blockDataAppendColInfo(pBlock, &colInfo);
11,560✔
684
        if (code != TSDB_CODE_SUCCESS) {
11,541!
685
          return -1;
×
686
        }
687
        i++;
11,541✔
688
        j++;
11,541✔
689
      }
690
    }
691
  }
692

693
  return TSDB_CODE_SUCCESS;
1,895✔
694
}
695

696
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
169,775,801✔
697
  int32_t code = TSDB_CODE_SUCCESS;
169,775,801✔
698

699
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
178,828,606!
700
    char val[65535 + 2] = {0};
8,671,105✔
701
    if (COL_VAL_IS_VALUE(pColVal)) {
8,671,105!
702
      if (pColVal->value.pData != NULL) {
9,066,713!
703
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
9,068,954✔
704
      }
705
      varDataSetLen(val, pColVal->value.nData);
9,066,713✔
706
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
9,066,713✔
707
    } else {
708
      colDataSetNULL(pColumnInfoData, rowIndex);
×
709
    }
710
  } else {
711
    code = colDataSetVal(pColumnInfoData, rowIndex, (void*)&pColVal->value.val, !COL_VAL_IS_VALUE(pColVal));
161,104,696✔
712
  }
713

714
  return code;
169,935,008✔
715
}
716

717
int32_t tqRetrieveDataBlock(STqReader* pReader, SSDataBlock** pRes, const char* id) {
842,951✔
718
  if (pReader == NULL || pRes == NULL) {
842,951!
719
    return TSDB_CODE_INVALID_PARA;
×
720
  }
721
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
843,046✔
722
  int32_t        code = 0;
843,103✔
723
  int32_t        line = 0;
843,103✔
724
  STSchema*      pTSchema = NULL;
843,103✔
725
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
843,103✔
726
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
843,143!
727
  SSDataBlock* pBlock = pReader->pResBlock;
843,143✔
728
  *pRes = pBlock;
843,143✔
729

730
  blockDataCleanup(pBlock);
843,143✔
731

732
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
843,063✔
733
  int32_t sversion = pSubmitTbData->sver;
843,063✔
734
  int64_t suid = pSubmitTbData->suid;
843,063✔
735
  int64_t uid = pSubmitTbData->uid;
843,063✔
736
  pReader->lastTs = pSubmitTbData->ctimeMs;
843,063✔
737

738
  pBlock->info.id.uid = uid;
843,063✔
739
  pBlock->info.version = pReader->msg.ver;
843,063✔
740

741
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
843,063✔
742
      (pReader->cachedSchemaVer != sversion)) {
841,162!
743
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
1,896✔
744

745
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1);
1,904✔
746
    if (pReader->pSchemaWrapper == NULL) {
1,904✔
747
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
9!
748
             "version %d, possibly dropped table",
749
             vgId, suid, uid, pReader->cachedSchemaVer);
750
      pReader->cachedSchemaSuid = 0;
9✔
751
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
9✔
752
    }
753

754
    pReader->cachedSchemaUid = uid;
1,895✔
755
    pReader->cachedSchemaSuid = suid;
1,895✔
756
    pReader->cachedSchemaVer = sversion;
1,895✔
757

758
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
1,895!
759
      tqError("vgId:%d, schema version mismatch, suid:%" PRId64 ", uid:%" PRId64 ", version:%d, cached version:%d",
×
760
              vgId, suid, uid, sversion, pReader->pSchemaWrapper->version);
761
      return TSDB_CODE_TQ_INTERNAL_ERROR;
×
762
    }
763
    code = buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
1,895✔
764
    TSDB_CHECK_CODE(code, line, END);
1,894!
765
    pBlock = pReader->pResBlock;
1,894✔
766
    *pRes = pBlock;
1,894✔
767
  }
768

769
  int32_t numOfRows = 0;
843,061✔
770
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
843,061✔
771
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
4✔
772
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
4!
773
    numOfRows = pCol->nVal;
4✔
774
  } else {
775
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
843,057✔
776
  }
777

778
  code = blockDataEnsureCapacity(pBlock, numOfRows);
843,063✔
779
  TSDB_CHECK_CODE(code, line, END);
843,066!
780
  pBlock->info.rows = numOfRows;
843,066✔
781
  int32_t colActual = blockDataGetNumOfCols(pBlock);
843,066✔
782

783
  // convert and scan one block
784
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
843,063✔
785
    SArray* pCols = pSubmitTbData->aCol;
4✔
786
    int32_t numOfCols = taosArrayGetSize(pCols);
4✔
787
    int32_t targetIdx = 0;
4✔
788
    int32_t sourceIdx = 0;
4✔
789
    while (targetIdx < colActual) {
18✔
790
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14✔
791
      TSDB_CHECK_NULL(pColData, code, line, END, terrno);
14!
792
      if (sourceIdx >= numOfCols) {
14✔
793
        tqError("lostdata tqRetrieveDataBlock sourceIdx:%d >= numOfCols:%d", sourceIdx, numOfCols);
4!
794
        colDataSetNNULL(pColData, 0, numOfRows);
4!
795
        targetIdx++;
4✔
796
        continue;
4✔
797
      }
798

799
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
10✔
800
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
10!
801
      SColVal colVal = {0};
10✔
802
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
10!
803
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
804
      if (pCol->cid < pColData->info.colId) {
10✔
805
        sourceIdx++;
4✔
806
      } else if (pCol->cid == pColData->info.colId) {
6✔
807
        for (int32_t i = 0; i < pCol->nVal; i++) {
12✔
808
          code = tColDataGetValue(pCol, i, &colVal);
8✔
809
          TSDB_CHECK_CODE(code, line, END);
8!
810
          code = doSetVal(pColData, i, &colVal);
8✔
811
          TSDB_CHECK_CODE(code, line, END);
8!
812
        }
813
        sourceIdx++;
4✔
814
        targetIdx++;
4✔
815
      } else {
816
        colDataSetNNULL(pColData, 0, numOfRows);
2!
817
        targetIdx++;
2✔
818
      }
819
    }
820
  } else {
821
    SArray*         pRows = pSubmitTbData->aRowP;
843,059✔
822
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
843,059✔
823
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
843,059✔
824
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
843,107✔
825

826
    for (int32_t i = 0; i < numOfRows; i++) {
54,083,274✔
827
      SRow* pRow = taosArrayGetP(pRows, i);
53,370,940✔
828
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
53,334,616✔
829
      int32_t sourceIdx = 0;
53,324,334✔
830
      for (int32_t j = 0; j < colActual; j++) {
205,184,219✔
831
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
151,944,051✔
832
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
151,761,079!
833

834
        while (1) {
10,014,439✔
835
          SColVal colVal = {0};
161,775,518✔
836
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
161,775,518✔
837
          TSDB_CHECK_CODE(code, line, END);
161,512,443!
838

839
          if (colVal.cid < pColData->info.colId) {
161,512,443✔
840
            sourceIdx++;
10,014,439✔
841
            continue;
10,014,439✔
842
          } else if (colVal.cid == pColData->info.colId) {
151,498,004!
843
            code = doSetVal(pColData, i, &colVal);
151,988,380✔
844
            TSDB_CHECK_CODE(code, line, END);
152,350,261!
845
            sourceIdx++;
152,350,261✔
846
            break;
151,859,885✔
847
          } else {
848
            colDataSetNULL(pColData, i);
×
849
            break;
×
850
          }
851
        }
852
      }
853
    }
854
  }
855

856
END:
712,334✔
857
  if (code != 0) {
712,338!
858
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
859
  }
860
  taosMemoryFreeClear(pTSchema);
843,005!
861
  return code;
843,113✔
862
}
863

864
#define PROCESS_VAL                                      \
865
  if (curRow == 0) {                                     \
866
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
867
    buildNew = true;                                     \
868
  } else {                                               \
869
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
870
    if (currentRowAssigned != assigned[j]) {             \
871
      assigned[j] = currentRowAssigned;                  \
872
      buildNew = true;                                   \
873
    }                                                    \
874
  }
875

876
#define SET_DATA                                                     \
877
  if (colVal.cid < pColData->info.colId) {                           \
878
    sourceIdx++;                                                     \
879
  } else if (colVal.cid == pColData->info.colId) {                   \
880
    TQ_ERR_GO_TO_END(doSetVal(pColData, curRow - lastRow, &colVal)); \
881
    sourceIdx++;                                                     \
882
    targetIdx++;                                                     \
883
  }
884

885
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
117,408✔
886
                               SSchemaWrapper* pSchemaWrapper, char* assigned, int32_t numOfRows, int32_t curRow,
887
                               int32_t* lastRow) {
888
  int32_t         code = 0;
117,408✔
889
  SSchemaWrapper* pSW = NULL;
117,408✔
890
  SSDataBlock*    block = NULL;
117,408✔
891
  if (taosArrayGetSize(blocks) > 0) {
117,408!
892
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
893
    TQ_NULL_GO_TO_END(pLastBlock);
×
894
    pLastBlock->info.rows = curRow - *lastRow;
×
895
    *lastRow = curRow;
×
896
  }
897

898
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
117,450!
899
  TQ_NULL_GO_TO_END(block);
117,609!
900

901
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
117,609!
902
  TQ_NULL_GO_TO_END(pSW);
117,632!
903

904
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pSchemaWrapper, assigned));
117,632!
905
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
117,513!
906
          (int32_t)taosArrayGetSize(block->pDataBlock));
907

908
  block->info.id.uid = pSubmitTbData->uid;
117,513✔
909
  block->info.version = pReader->msg.ver;
117,513✔
910
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
117,513!
911
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
117,573!
912
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
117,549!
913
  pSW = NULL;
117,549✔
914
  taosMemoryFreeClear(block);
117,549!
915

916
END:
7✔
917
  if (code != 0) {
117,672!
918
    tqError("processBuildNew failed, code:%d", code);
×
919
  }
920
  tDeleteSchemaWrapper(pSW);
117,672!
921
  blockDataFreeRes(block);
117,627✔
922
  taosMemoryFree(block);
117,608!
923
  return code;
117,616✔
924
}
925
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
118✔
926
  int32_t code = 0;
118✔
927
  int32_t curRow = 0;
118✔
928
  int32_t lastRow = 0;
118✔
929

930
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
118✔
931
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
118!
932
  TQ_NULL_GO_TO_END(assigned);
118!
933

934
  SArray*   pCols = pSubmitTbData->aCol;
118✔
935
  SColData* pCol = taosArrayGet(pCols, 0);
118✔
936
  TQ_NULL_GO_TO_END(pCol);
118!
937
  int32_t numOfRows = pCol->nVal;
118✔
938
  int32_t numOfCols = taosArrayGetSize(pCols);
118✔
939
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols, numOfRows);
118!
940
  for (int32_t i = 0; i < numOfRows; i++) {
325✔
941
    bool buildNew = false;
208✔
942

943
    for (int32_t j = 0; j < numOfCols; j++) {
1,064✔
944
      pCol = taosArrayGet(pCols, j);
857✔
945
      TQ_NULL_GO_TO_END(pCol);
857!
946
      SColVal colVal = {0};
857✔
947
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
857!
948
      PROCESS_VAL
856!
949
    }
950

951
    if (buildNew) {
207✔
952
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
118!
953
                                       curRow, &lastRow));
954
    }
955

956
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
207✔
957
    TQ_NULL_GO_TO_END(pBlock);
208!
958

959
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
208!
960
            (int32_t)taosArrayGetSize(blocks));
961

962
    int32_t targetIdx = 0;
208✔
963
    int32_t sourceIdx = 0;
208✔
964
    int32_t colActual = blockDataGetNumOfCols(pBlock);
208✔
965
    while (targetIdx < colActual) {
1,059✔
966
      pCol = taosArrayGet(pCols, sourceIdx);
852✔
967
      TQ_NULL_GO_TO_END(pCol);
852!
968
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
852✔
969
      TQ_NULL_GO_TO_END(pColData);
852!
970
      SColVal colVal = {0};
852✔
971
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
852!
972
      SET_DATA
852!
973
    }
974

975
    curRow++;
207✔
976
  }
977
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
117✔
978
  pLastBlock->info.rows = curRow - lastRow;
118✔
979
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
118!
980
END:
118✔
981
  if (code != TSDB_CODE_SUCCESS) {
118!
982
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
983
  }
984
  taosMemoryFree(assigned);
118!
985
  return code;
118✔
986
}
987

988
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
117,449✔
989
  int32_t   code = 0;
117,449✔
990
  STSchema* pTSchema = NULL;
117,449✔
991

992
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
117,449✔
993
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
117,449!
994
  TQ_NULL_GO_TO_END(assigned);
117,528!
995

996
  int32_t curRow = 0;
117,528✔
997
  int32_t lastRow = 0;
117,528✔
998
  SArray* pRows = pSubmitTbData->aRowP;
117,528✔
999
  int32_t numOfRows = taosArrayGetSize(pRows);
117,528✔
1000
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
117,485✔
1001
  TQ_NULL_GO_TO_END(pTSchema);
117,475!
1002
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
117,475!
1003

1004
  for (int32_t i = 0; i < numOfRows; i++) {
3,878,778✔
1005
    bool  buildNew = false;
3,753,048✔
1006
    SRow* pRow = taosArrayGetP(pRows, i);
3,753,048✔
1007
    TQ_NULL_GO_TO_END(pRow);
3,751,116!
1008

1009
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
21,290,838✔
1010
      SColVal colVal = {0};
17,567,895✔
1011
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
17,567,895✔
1012
      PROCESS_VAL
17,541,800!
1013
    }
1014

1015
    if (buildNew) {
3,722,943✔
1016
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
117,390!
1017
                                       curRow, &lastRow));
1018
    }
1019

1020
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,723,027✔
1021
    TQ_NULL_GO_TO_END(pBlock);
3,746,207!
1022

1023
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
3,746,207!
1024
            (int32_t)taosArrayGetSize(blocks));
1025

1026
    int32_t targetIdx = 0;
3,746,207✔
1027
    int32_t sourceIdx = 0;
3,746,207✔
1028
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,746,207✔
1029
    while (targetIdx < colActual) {
21,202,967✔
1030
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
17,441,629✔
1031
      SColVal          colVal = {0};
17,420,205✔
1032
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
17,420,205!
1033
      SET_DATA
17,334,338!
1034
    }
1035

1036
    curRow++;
3,761,338✔
1037
  }
1038
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
125,730✔
1039
  pLastBlock->info.rows = curRow - lastRow;
117,335✔
1040

1041
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows, (int)taosArrayGetSize(blocks));
117,335!
1042
END:
117,335✔
1043
  if (code != TSDB_CODE_SUCCESS) {
117,331✔
1044
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
5!
1045
  }
1046
  taosMemoryFreeClear(pTSchema);
117,331!
1047
  taosMemoryFree(assigned);
117,520!
1048
  return code;
117,505✔
1049
}
1050

1051
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq){
1,071✔
1052
  int32_t code = 0;
1,071✔
1053
  int32_t lino = 0;
1,071✔
1054
  void*   createReq = NULL;
1,071✔
1055
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
1,071!
1056
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
1,071!
1057

1058
  if (pRsp->createTableNum == 0) {
1,071✔
1059
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
88✔
1060
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
88!
1061
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
88✔
1062
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
88!
1063
  }
1064

1065
  uint32_t len = 0;
1,071✔
1066
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
1,071!
1067
  TSDB_CHECK_CODE(code, lino, END);
1,070!
1068
  createReq = taosMemoryCalloc(1, len);
1,070!
1069
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
1,071!
1070

1071
  SEncoder encoder = {0};
1,071✔
1072
  tEncoderInit(&encoder, createReq, len);
1,071✔
1073
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
1,071✔
1074
  tEncoderClear(&encoder);
1,071✔
1075
  TSDB_CHECK_CODE(code, lino, END);
1,071!
1076
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
2,142!
1077
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
2,142!
1078
  pRsp->createTableNum++;
1,071✔
1079
  tqTrace("build create table info msg success");
1,071!
1080

1081
  END:
1,071✔
1082
  if (code != 0){
1,071!
1083
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1084
    taosMemoryFree(createReq);
×
1085
  }
1086
  return code;
1,071✔
1087
}
1088

1089
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas, SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
118,184✔
1090
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
118,184!
1091
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
118,184✔
1092
  if (pSubmitTbData == NULL) {
118,176!
1093
    return terrno;
×
1094
  }
1095
  pReader->nextBlk++;
118,176✔
1096

1097
  if (pSubmitTbDataRet) {
118,176!
1098
    *pSubmitTbDataRet = pSubmitTbData;
118,180✔
1099
  }
1100

1101
  int32_t sversion = pSubmitTbData->sver;
118,176✔
1102
  int64_t uid = pSubmitTbData->uid;
118,176✔
1103
  pReader->lastBlkUid = uid;
118,176✔
1104

1105
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
118,176✔
1106
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1);
118,198✔
1107
  if (pReader->pSchemaWrapper == NULL) {
118,139✔
1108
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
487!
1109
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1110
    pReader->cachedSchemaSuid = 0;
501✔
1111
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
501✔
1112
  }
1113

1114
  if (pSubmitTbData->pCreateTbReq != NULL) {
117,652✔
1115
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
1,071✔
1116
    if (code != 0) {
1,071!
1117
      return code;
×
1118
    }
1119
  } else if (rawList != NULL) {
116,581✔
1120
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL){
36!
1121
      return terrno;
×
1122
    }
1123
    pReader->pSchemaWrapper = NULL;
18✔
1124
    return 0;
18✔
1125
  }
1126

1127
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
117,634✔
1128
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
118✔
1129
  } else {
1130
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
117,516✔
1131
  }
1132
}
1133

1134
void tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList) {
8,407✔
1135
  if (pReader == NULL){
8,407!
1136
    return;
×
1137
  }
1138
  pReader->pColIdList = pColIdList;
8,407✔
1139
}
1140

1141
void tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
8,483✔
1142
  if (pReader == NULL || tbUidList == NULL) {
8,483!
1143
    return;
1✔
1144
  }
1145
  if (pReader->tbIdHash) {
8,482✔
1146
    taosHashClear(pReader->tbIdHash);
14✔
1147
  } else {
1148
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
8,468✔
1149
    if (pReader->tbIdHash == NULL) {
8,470!
1150
      tqError("s-task:%s failed to init hash table", id);
×
1151
      return;
×
1152
    }
1153
  }
1154

1155
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
177,861✔
1156
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
169,356✔
1157
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
169,262!
1158
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1159
      continue;
×
1160
    }
1161
  }
1162

1163
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
8,484✔
1164
}
1165

1166
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
20,791✔
1167
  if (pReader == NULL || pTableUidList == NULL) {
20,791!
1168
    return;
×
1169
  }
1170
  if (pReader->tbIdHash == NULL) {
20,791!
1171
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1172
    if (pReader->tbIdHash == NULL) {
×
1173
      tqError("failed to init hash table");
×
1174
      return;
×
1175
    }
1176
  }
1177

1178
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
20,791✔
1179
  for (int i = 0; i < numOfTables; i++) {
21,680✔
1180
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
890✔
1181
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
890!
1182
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1183
      continue;
×
1184
    }
1185
  }
1186
}
1187

1188
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
2,423✔
1189
  if (pReader == NULL) {
2,423!
1190
    return false;
×
1191
  }
1192
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
2,423✔
1193
}
1194

1195
bool tqCurrentBlockConsumed(const STqReader* pReader) {
845,700✔
1196
  if (pReader == NULL) {
845,700!
1197
    return false;
×
1198
  }
1199
  return pReader->msg.msgStr == NULL;
845,700✔
1200
}
1201

1202
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
775✔
1203
  if (pReader == NULL || tbUidList == NULL) {
775!
1204
    return;
×
1205
  }
1206
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
781✔
1207
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
6✔
1208
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
6!
1209
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
1!
1210
    }
1211
  }
1212
}
1213

1214
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
107,712✔
1215
  if (pTq == NULL || tbUidList == NULL) {
107,712!
1216
    return TSDB_CODE_INVALID_PARA;
×
1217
  }
1218
  void*   pIter = NULL;
107,715✔
1219
  int32_t vgId = TD_VID(pTq->pVnode);
107,715✔
1220

1221
  // update the table list for each consumer handle
1222
  taosWLockLatch(&pTq->lock);
107,715✔
1223
  while (1) {
4,154✔
1224
    pIter = taosHashIterate(pTq->pHandle, pIter);
111,872✔
1225
    if (pIter == NULL) {
111,872✔
1226
      break;
107,718✔
1227
    }
1228

1229
    STqHandle* pTqHandle = (STqHandle*)pIter;
4,154✔
1230
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,154✔
1231
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
985✔
1232
      if (code != 0) {
985!
1233
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1234
        continue;
×
1235
      }
1236
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,169✔
1237
      if (!isAdd) {
3,155✔
1238
        int32_t sz = taosArrayGetSize(tbUidList);
1,062✔
1239
        for (int32_t i = 0; i < sz; i++) {
1,062!
1240
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1241
          if (tbUid &&
×
1242
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1243
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1244
            continue;
×
1245
          }
1246
        }
1247
      }
1248
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
14!
1249
      if (isAdd) {
14!
1250
        SArray* list = NULL;
14✔
1251
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
14✔
1252
                                    &list, pTqHandle->execHandle.task);
1253
        if (ret != TDB_CODE_SUCCESS) {
14!
1254
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1255
                  pTqHandle->consumerId);
1256
          taosArrayDestroy(list);
×
1257
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1258
          taosWUnLockLatch(&pTq->lock);
×
1259

1260
          return ret;
×
1261
        }
1262
        tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
14✔
1263
        taosArrayDestroy(list);
14✔
1264
      } else {
1265
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1266
      }
1267
    }
1268
  }
1269
  taosWUnLockLatch(&pTq->lock);
107,718✔
1270

1271
  // update the table list handle for each stream scanner/wal reader
1272
  streamMetaWLock(pTq->pStreamMeta);
107,717✔
1273
  while (1) {
42,114✔
1274
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
149,832✔
1275
    if (pIter == NULL) {
149,831✔
1276
      break;
107,718✔
1277
    }
1278

1279
    int64_t      refId = *(int64_t*)pIter;
42,113✔
1280
    SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, refId);
42,113✔
1281
    if (pTask != NULL) {
42,115!
1282
      int32_t taskId = pTask->id.taskId;
42,115✔
1283

1284
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
42,115✔
1285
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
20,587✔
1286
        if (code != 0) {
20,580✔
1287
          tqError("vgId:%d, s-task:0x%x update qualified table error for stream task", vgId, taskId);
20!
1288
        }
1289
      }
1290
      int32_t ret = taosReleaseRef(streamTaskRefPool, refId);
42,108✔
1291
      if (ret) {
42,114!
1292
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
×
1293
      }
1294
    }
1295
  }
1296

1297
  streamMetaWUnLock(pTq->pStreamMeta);
107,718✔
1298
  return 0;
107,718✔
1299
}
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