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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

67.44
/source/dnode/vnode/src/tq/tqUtil.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

18
static int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
19
                                 const SMqMetaRsp* pRsp, int32_t vgId);
20
static int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
21
                                      const SMqBatchMetaRsp* pRsp, int32_t vgId);
22

23
int32_t tqInitDataRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
49,898✔
24
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
49,898✔
25
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
49,898✔
26

27
  if (pRsp->blockData == NULL || pRsp->blockDataLen == NULL) {
49,899!
28
    return terrno;
×
29
  }
30

31
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
49,899✔
32
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
49,899✔
33
  pRsp->withTbName = 0;
49,897✔
34
  pRsp->withSchema = false;
49,897✔
35
  return 0;
49,897✔
36
}
37

38
void tqUpdateNodeStage(STQ* pTq, bool isLeader) {
8,120✔
39
  SSyncState state = syncGetState(pTq->pVnode->sync);
8,120✔
40
  streamMetaUpdateStageRole(pTq->pStreamMeta, state.term, isLeader);
8,124✔
41
}
8,125✔
42

43
static int32_t tqInitTaosxRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
7,726✔
44
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
7,726✔
45
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
7,726✔
46

47
  pRsp->withTbName = 1;
7,726✔
48
  pRsp->withSchema = 1;
7,726✔
49
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
7,726✔
50
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
7,726✔
51
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
7,726✔
52
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
7,725✔
53

54
  if (pRsp->blockData == NULL || pRsp->blockDataLen == NULL ||
7,726!
55
      pRsp->blockTbName == NULL || pRsp->blockSchema == NULL) {
7,726!
56
    if (pRsp->blockData != NULL) {
×
57
      taosArrayDestroy(pRsp->blockData);
×
58
      pRsp->blockData = NULL;
×
59
    }
60

61
    if (pRsp->blockDataLen != NULL) {
×
62
      taosArrayDestroy(pRsp->blockDataLen);
×
63
      pRsp->blockDataLen = NULL;
×
64
    }
65

66
    if (pRsp->blockTbName != NULL) {
×
67
      taosArrayDestroy(pRsp->blockTbName);
×
68
      pRsp->blockTbName = NULL;
×
69
    }
70

71
    if (pRsp->blockSchema != NULL) {
×
72
      taosArrayDestroy(pRsp->blockSchema);
×
73
      pRsp->blockSchema = NULL;
×
74
    }
75

76
    return terrno;
×
77
  }
78

79
  return 0;
7,726✔
80
}
81

82
static int32_t extractResetOffsetVal(STqOffsetVal* pOffsetVal, STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
1,599✔
83
                                     SRpcMsg* pMsg, bool* pBlockReturned) {
84
  uint64_t   consumerId = pRequest->consumerId;
1,599✔
85
  STqOffset* pOffset = NULL;
1,599✔
86
  int32_t    code = tqMetaGetOffset(pTq, pRequest->subKey, &pOffset);
1,599✔
87
  int32_t    vgId = TD_VID(pTq->pVnode);
1,595✔
88

89
  *pBlockReturned = false;
1,595✔
90
  // In this vnode, data has been polled by consumer for this topic, so let's continue from the last offset value.
91
  if (code == 0) {
1,595✔
92
    tOffsetCopy(pOffsetVal, &pOffset->val);
227✔
93

94
    char formatBuf[TSDB_OFFSET_LEN] = {0};
226✔
95
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
226✔
96
    tqDebug("tmq poll: consumer:0x%" PRIx64
226!
97
            ", subkey %s, vgId:%d, existed offset found, offset reset to %s and continue.QID:0x%" PRIx64,
98
            consumerId, pHandle->subKey, vgId, formatBuf, pRequest->reqId);
99
    return 0;
226✔
100
  } else {
101
    // no poll occurs in this vnode for this topic, let's seek to the right offset value.
102
    if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
1,368✔
103
      if (pRequest->useSnapshot) {
830✔
104
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
331!
105
                consumerId, pHandle->subKey, vgId);
106
        if (pHandle->fetchMeta) {
331✔
107
          tqOffsetResetToMeta(pOffsetVal, 0);
108
        } else {
109
          SValue val = {0};
313!
110
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
313✔
111
        }
112
      } else {
113
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
499✔
114
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
500✔
115
      }
116
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
538✔
117
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
34✔
118
      SMqDataRsp dataRsp = {0};
34✔
119
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
34✔
120

121
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
34✔
122
      if (code != 0) {
34!
123
        return code;
×
124
      }
125
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
34!
126
              pHandle->subKey, vgId, dataRsp.rspOffset.version);
127
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
34✔
128
      tDeleteMqDataRsp(&dataRsp);
34✔
129

130
      *pBlockReturned = true;
34✔
131
      return code;
34✔
132
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_NONE) {
504!
133
      tqError("tmq poll: subkey:%s, no offset committed for consumer:0x%" PRIx64
506!
134
              " in vg %d, subkey %s, reset none failed",
135
              pHandle->subKey, consumerId, vgId, pRequest->subKey);
136
      return TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
508✔
137
    }
138
  }
139

140
  return 0;
829✔
141
}
142

143
static int32_t extractDataAndRspForNormalSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
49,433✔
144
                                                   SRpcMsg* pMsg, STqOffsetVal* pOffset) {
145
  uint64_t consumerId = pRequest->consumerId;
49,433✔
146
  int32_t  vgId = TD_VID(pTq->pVnode);
49,433✔
147
  terrno = 0;
49,433✔
148

149
  SMqDataRsp dataRsp = {0};
49,433✔
150
  int code = tqInitDataRsp(&dataRsp, *pOffset);
49,433✔
151
  if (code != 0) {
49,432!
152
    goto end;
×
153
  }
154

155
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
49,432✔
156
  if (code != 0) {
49,433!
157
    goto end;
×
158
  }
159

160
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
49,433✔
161
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
49,433✔
162
    goto end;
255✔
163
  }
164

165
  //   till now, all data has been transferred to consumer, new data needs to push client once arrived.
166
  if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST && dataRsp.blockNum == 0) {
49,178✔
167
    // lock
168
    taosWLockLatch(&pTq->lock);
23,878✔
169
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
23,878✔
170
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
23,878✔
171
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
23,178✔
172
      taosWUnLockLatch(&pTq->lock);
23,178✔
173
      goto end;
23,178✔
174
    }
175
    taosWUnLockLatch(&pTq->lock);
700✔
176
  }
177

178
  // reqOffset represents the current date offset, may be changed if wal not exists
179
  tOffsetCopy(&dataRsp.reqOffset, pOffset);
26,000✔
180
  code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
26,000✔
181

182
end : {
49,432✔
183
  char buf[TSDB_OFFSET_LEN] = {0};
49,432✔
184
  tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
49,432✔
185
  tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s,QID:0x%" PRIx64
49,431!
186
          " code:%d",
187
          consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, code);
188
  tDeleteMqDataRsp(&dataRsp);
49,433✔
189
  return code;
49,433✔
190
}
191
}
192

193
#define PROCESS_EXCLUDED_MSG(TYPE, DECODE_FUNC, DELETE_FUNC)                                               \
194
  SDecoder decoder = {0};                                                                                  \
195
  TYPE     req = {0};                                                                                      \
196
  void*    data = POINTER_SHIFT(pHead->body, sizeof(SMsgHead));                                            \
197
  int32_t  len = pHead->bodyLen - sizeof(SMsgHead);                                                        \
198
  tDecoderInit(&decoder, data, len);                                                                       \
199
  if (DECODE_FUNC(&decoder, &req) == 0 && (req.source & TD_REQ_FROM_TAOX) != 0) {                          \
200
    tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, jump meta for, vgId:%d offset %" PRId64 \
201
            " msgType %d",                                                                                 \
202
            pRequest->consumerId, pRequest->epoch, vgId, fetchVer, pHead->msgType);                        \
203
    fetchVer++;                                                                                            \
204
    DELETE_FUNC(&req);                                                                                     \
205
    tDecoderClear(&decoder);                                                                               \
206
    continue;                                                                                              \
207
  }                                                                                                        \
208
  DELETE_FUNC(&req);                                                                                       \
209
  tDecoderClear(&decoder);
210

211
static void tDeleteCommon(void* parm) {}
321✔
212

213
static int32_t extractDataAndRspForDbStbSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
7,725✔
214
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
215
  int32_t         vgId = TD_VID(pTq->pVnode);
7,725✔
216
  SMqDataRsp      taosxRsp = {0};
7,725✔
217
  SMqBatchMetaRsp btMetaRsp = {0};
7,725✔
218
  int32_t         code = 0;
7,725✔
219

220
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
7,725!
221
  if (offset->type != TMQ_OFFSET__LOG) {
7,726✔
222
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset));
41!
223

224
    if (taosArrayGetSize(btMetaRsp.batchMetaReq) > 0) {
41✔
225
      code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
16✔
226
      tqDebug("tmq poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send meta offset type:%d,uid:%" PRId64
16!
227
              ",ts:%" PRId64,
228
              pRequest->consumerId, pHandle->subKey, vgId, btMetaRsp.rspOffset.type, btMetaRsp.rspOffset.uid,
229
              btMetaRsp.rspOffset.ts);
230
      goto END;
16✔
231
    }
232

233
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64
25!
234
            ",ts:%" PRId64,
235
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type,
236
            taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
237
    if (taosxRsp.blockNum > 0) {
25✔
238
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
19✔
239
      goto END;
19✔
240
    } else {
241
      tOffsetCopy(offset, &taosxRsp.rspOffset);
6✔
242
    }
243
  }
244

245
  if (offset->type == TMQ_OFFSET__LOG) {
7,691!
246
    walReaderVerifyOffset(pHandle->pWalReader, offset);
7,691✔
247
    int64_t fetchVer = offset->version;
7,691✔
248

249
    uint64_t st = taosGetTimestampMs();
7,691✔
250
    int      totalRows = 0;
7,691✔
251
    int32_t  totalMetaRows = 0;
7,691✔
252
    while (1) {
63,381✔
253
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
71,072✔
254
      if (savedEpoch > pRequest->epoch) {
71,030!
255
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
256
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
257
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
258
        goto END;
7,691✔
259
      }
260

261
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
71,030✔
262
        if (totalMetaRows > 0) {
7,180✔
263
          tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
2✔
264
          code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
2✔
265
          if (totalRows != 0) {
2!
266
            tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, totalRows error, vgId:%d offset %" PRId64,
×
267
                    pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
268
            code = code == 0 ? TSDB_CODE_TQ_INTERNAL_ERROR : code;
×
269
          }
270
          goto END;
2✔
271
        }
272
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);
7,178✔
273
        code = tqSendDataRsp(
7,178✔
274
            pHandle, pMsg, pRequest, &taosxRsp,
275
            taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
7,178✔
276
        goto END;
7,178✔
277
      }
278

279
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
63,936✔
280
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %d",
63,936✔
281
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, pHead->msgType);
282

283
      // process meta
284
      if (pHead->msgType != TDMT_VND_SUBMIT) {
63,934✔
285
        if (totalRows > 0) {
558✔
286
          tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);
26✔
287
          code = tqSendDataRsp(
26✔
288
              pHandle, pMsg, pRequest, &taosxRsp,
289
              taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
26✔
290
          goto END;
215✔
291
        }
292

293
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
532✔
294
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
529✔
295
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
170!
296
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
359✔
297
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteCommon)
44!
298
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
396✔
299
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
272!
300
          } else if (pHead->msgType == TDMT_VND_DELETE) {
43✔
301
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
5!
302
          }
303
        }
304

305
        tqDebug("fetch meta msg, ver:%" PRId64 ", vgId:%d, type:%s, enable batch meta:%d", pHead->version, vgId,
191!
306
                TMSG_INFO(pHead->msgType), pRequest->enableBatchMeta);
307
        if (!pRequest->enableBatchMeta && !pRequest->useSnapshot) {
191!
308
          SMqMetaRsp metaRsp = {0};
189✔
309
          tqOffsetResetToLog(&metaRsp.rspOffset, fetchVer + 1);
189✔
310
          metaRsp.resMsgType = pHead->msgType;
189✔
311
          metaRsp.metaRspLen = pHead->bodyLen;
189✔
312
          metaRsp.metaRsp = pHead->body;
189✔
313
          code = tqSendMetaPollRsp(pHandle, pMsg, pRequest, &metaRsp, vgId);
189✔
314
          goto END;
189✔
315
        }
316

317
        if (!btMetaRsp.batchMetaReq) {
2!
318
          btMetaRsp.batchMetaReq = taosArrayInit(4, POINTER_BYTES);
2✔
319
          if (btMetaRsp.batchMetaReq == NULL) {
2!
320
            code = TAOS_GET_TERRNO(terrno);
×
321
            goto END;
×
322
          }
323
          btMetaRsp.batchMetaLen = taosArrayInit(4, sizeof(int32_t));
2✔
324
          if (btMetaRsp.batchMetaLen == NULL) {
2!
325
            code = TAOS_GET_TERRNO(terrno);
×
326
            goto END;
×
327
          }
328
        }
329
        fetchVer++;
2✔
330

331
        SMqMetaRsp tmpMetaRsp = {0};
2✔
332
        tmpMetaRsp.resMsgType = pHead->msgType;
2✔
333
        tmpMetaRsp.metaRspLen = pHead->bodyLen;
2✔
334
        tmpMetaRsp.metaRsp = pHead->body;
2✔
335
        uint32_t len = 0;
2✔
336
        tEncodeSize(tEncodeMqMetaRsp, &tmpMetaRsp, len, code);
2!
337
        if (TSDB_CODE_SUCCESS != code) {
2!
338
          tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
339
          continue;
×
340
        }
341
        int32_t tLen = sizeof(SMqRspHead) + len;
2✔
342
        void*   tBuf = taosMemoryCalloc(1, tLen);
2✔
343
        if (tBuf == NULL) {
2!
344
          code = TAOS_GET_TERRNO(terrno);
×
345
          goto END;
×
346
        }
347
        void*    metaBuff = POINTER_SHIFT(tBuf, sizeof(SMqRspHead));
2✔
348
        SEncoder encoder = {0};
2✔
349
        tEncoderInit(&encoder, metaBuff, len);
2✔
350
        code = tEncodeMqMetaRsp(&encoder, &tmpMetaRsp);
2✔
351
        tEncoderClear(&encoder);
2✔
352

353
        if (code < 0) {
2!
354
          tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
355
          continue;
×
356
        }
357
        if (taosArrayPush(btMetaRsp.batchMetaReq, &tBuf) == NULL) {
4!
358
          code = TAOS_GET_TERRNO(terrno);
×
359
          goto END;
×
360
        }
361
        if (taosArrayPush(btMetaRsp.batchMetaLen, &tLen) == NULL) {
4!
362
          code = TAOS_GET_TERRNO(terrno);
×
363
          goto END;
×
364
        }
365
        totalMetaRows++;
2✔
366
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= tmqRowSize) || (taosGetTimestampMs() - st > 1000)) {
4!
367
          tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
×
368
          code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
369
          goto END;
×
370
        }
371
        continue;
2✔
372
      }
373

374
      if (totalMetaRows > 0) {
63,376!
375
        tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
×
376
        code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
377
        goto END;
×
378
      }
379

380
      // process data
381
      SPackedData submit = {
63,376✔
382
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
63,376✔
383
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
63,376✔
384
          .ver = pHead->version,
63,376✔
385
      };
386

387
      code = tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest->sourceExcluded);
63,376✔
388
      if (code < 0) {
63,321!
389
        tqError("tmq poll: tqTaosxScanLog error %" PRId64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
390
                pRequest->subKey);
391
        goto END;
×
392
      }
393

394
      if (totalRows >= tmqRowSize || (taosGetTimestampMs() - st > 1000)) {
126,367!
395
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer + 1);
295✔
396
        code = tqSendDataRsp(
295✔
397
            pHandle, pMsg, pRequest, &taosxRsp,
398
            taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
295!
399
        goto END;
296✔
400
      } else {
401
        fetchVer++;
63,038✔
402
      }
403
    }
404
  }
405

406
END:
×
407
  tDeleteMqBatchMetaRsp(&btMetaRsp);
7,726✔
408
  tDeleteSTaosxRsp(&taosxRsp);
7,726✔
409
  return code;
7,726✔
410
}
411

412
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
57,701✔
413
  int32_t      code = 0;
57,701✔
414
  STqOffsetVal reqOffset = {0};
57,701✔
415
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
57,701✔
416

417
  // reset the offset if needed
418
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
57,700✔
419
    bool blockReturned = false;
1,598✔
420
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
1,598✔
421
    if (code != 0) {
1,599✔
422
      goto END;
542✔
423
    }
424

425
    // empty block returned, quit
426
    if (blockReturned) {
1,091✔
427
      goto END;
34✔
428
    }
429
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
56,102!
430
    uError("req offset type is 0");
×
431
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
432
    goto END;
×
433
  }
434

435
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
57,159✔
436
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
49,433✔
437
  } else {
438
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
7,726✔
439
  }
440

441
END:
57,701✔
442
  tOffsetDestroy(&reqOffset);
57,701✔
443
  return code;
57,701✔
444
}
445

446
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
34,192✔
447
                          int64_t ever) {
448
  pMsgHead->consumerId = consumerId;
34,192✔
449
  pMsgHead->epoch = epoch;
34,192✔
450
  pMsgHead->mqMsgType = type;
34,192✔
451
  pMsgHead->walsver = sver;
34,192✔
452
  pMsgHead->walever = ever;
34,192✔
453
}
34,192✔
454

455
int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
18✔
456
                               const SMqBatchMetaRsp* pRsp, int32_t vgId) {
457
  int32_t len = 0;
18✔
458
  int32_t code = 0;
18✔
459
  tEncodeSize(tEncodeMqBatchMetaRsp, pRsp, len, code);
18!
460
  if (code < 0) {
18!
461
    return TAOS_GET_TERRNO(code);
×
462
  }
463
  int32_t tlen = sizeof(SMqRspHead) + len;
18✔
464
  void*   buf = rpcMallocCont(tlen);
18✔
465
  if (buf == NULL) {
18!
466
    return TAOS_GET_TERRNO(terrno);
×
467
  }
468

469
  int64_t sver = 0, ever = 0;
18✔
470
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
18✔
471
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
18✔
472

473
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
18✔
474

475
  SEncoder encoder = {0};
18✔
476
  tEncoderInit(&encoder, abuf, len);
18✔
477
  code = tEncodeMqBatchMetaRsp(&encoder, pRsp);
18✔
478
  tEncoderClear(&encoder);
18✔
479
  if (code < 0) {
18!
480
    rpcFreeCont(buf);
×
481
    return TAOS_GET_TERRNO(code);
×
482
  }
483
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
18✔
484

485
  tmsgSendRsp(&resp);
18✔
486
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
18!
487
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
488

489
  return 0;
18✔
490
}
491

492
int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp,
189✔
493
                          int32_t vgId) {
494
  int32_t len = 0;
189✔
495
  int32_t code = 0;
189✔
496
  tEncodeSize(tEncodeMqMetaRsp, pRsp, len, code);
189!
497
  if (code < 0) {
189!
498
    return TAOS_GET_TERRNO(code);
×
499
  }
500
  int32_t tlen = sizeof(SMqRspHead) + len;
189✔
501
  void*   buf = rpcMallocCont(tlen);
189✔
502
  if (buf == NULL) {
189!
503
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
504
  }
505

506
  int64_t sver = 0, ever = 0;
189✔
507
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
189✔
508
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
189✔
509

510
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
189✔
511

512
  SEncoder encoder = {0};
189✔
513
  tEncoderInit(&encoder, abuf, len);
189✔
514
  code = tEncodeMqMetaRsp(&encoder, pRsp);
189✔
515
  tEncoderClear(&encoder);
189✔
516
  if (code < 0) {
189!
517
    rpcFreeCont(buf);
×
518
    return TAOS_GET_TERRNO(code);
×
519
  }
520

521
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
189✔
522

523
  tmsgSendRsp(&resp);
189✔
524
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type %d, offset type:%d", vgId,
189!
525
          pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
526

527
  return 0;
189✔
528
}
529

530
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, const SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
33,985✔
531
                        int32_t type, int64_t sver, int64_t ever) {
532
  int32_t len = 0;
33,985✔
533
  int32_t code = 0;
33,985✔
534

535
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP || type == TMQ_MSG_TYPE__WALINFO_RSP) {
33,985✔
536
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
33,973!
537
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
12!
538
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
12!
539
  }
540

541
  if (code < 0) {
33,982!
542
    return TAOS_GET_TERRNO(code);
×
543
  }
544

545
  int32_t tlen = sizeof(SMqRspHead) + len;
33,982✔
546
  void*   buf = rpcMallocCont(tlen);
33,982✔
547
  if (buf == NULL) {
33,984!
548
    return terrno;
×
549
  }
550

551
  SMqRspHead* pHead = (SMqRspHead*)buf;
33,984✔
552
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
33,984✔
553

554
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
33,984✔
555

556
  SEncoder encoder = {0};
33,984✔
557
  tEncoderInit(&encoder, abuf, len);
33,984✔
558

559
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP || type == TMQ_MSG_TYPE__WALINFO_RSP) {
33,984✔
560
    code = tEncodeMqDataRsp(&encoder, pRsp);
33,972✔
561
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
12!
562
    code = tEncodeSTaosxRsp(&encoder, pRsp);
12✔
563
  }
564
  tEncoderClear(&encoder);
33,984✔
565
  if (code < 0) {
33,984!
566
    rpcFreeCont(buf);
×
567
    return TAOS_GET_TERRNO(code);
×
568
  }
569
  SRpcMsg rsp = {.info = *pRpcHandleInfo, .pCont = buf, .contLen = tlen, .code = 0};
33,984✔
570

571
  tmsgSendRsp(&rsp);
33,984✔
572
  return 0;
33,983✔
573
}
574

575
int32_t tqExtractDelDataBlock(const void* pData, int32_t len, int64_t ver, void** pRefBlock, int32_t type, EStreamType blockType) {
1,884✔
576
  int32_t     code = 0;
1,884✔
577
  int32_t     line = 0;
1,884✔
578
  SDecoder*   pCoder = &(SDecoder){0};
1,884✔
579
  SDeleteRes* pRes = &(SDeleteRes){0};
1,884✔
580

581
  *pRefBlock = NULL;
1,884✔
582

583
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
1,884✔
584
  TSDB_CHECK_NULL(pRes->uidList, code, line, END, terrno)
1,884!
585

586
  tDecoderInit(pCoder, (uint8_t*)pData, len);
1,884✔
587
  code = tDecodeDeleteRes(pCoder, pRes);
1,884✔
588
  TSDB_CHECK_CODE(code, line, END);
1,884!
589

590
  int32_t numOfTables = taosArrayGetSize(pRes->uidList);
1,884✔
591
  if (numOfTables == 0 || pRes->affectedRows == 0) {
1,884✔
592
    goto END;
1,201✔
593
  }
594

595
  SSDataBlock* pDelBlock = NULL;
683✔
596
  code = createSpecialDataBlock(blockType, &pDelBlock);
683✔
597
  TSDB_CHECK_CODE(code, line, END);
683!
598

599
  code = blockDataEnsureCapacity(pDelBlock, numOfTables);
683✔
600
  TSDB_CHECK_CODE(code, line, END);
683!
601

602
  pDelBlock->info.rows = numOfTables;
683✔
603
  pDelBlock->info.version = ver;
683✔
604

605
  for (int32_t i = 0; i < numOfTables; i++) {
1,370✔
606
    // start key column
607
    SColumnInfoData* pStartCol = taosArrayGet(pDelBlock->pDataBlock, START_TS_COLUMN_INDEX);
687✔
608
    TSDB_CHECK_NULL(pStartCol, code, line, END, terrno)
687!
609
    code = colDataSetVal(pStartCol, i, (const char*)&pRes->skey, false);  // end key column
687✔
610
    TSDB_CHECK_CODE(code, line, END);
687!
611
    SColumnInfoData* pEndCol = taosArrayGet(pDelBlock->pDataBlock, END_TS_COLUMN_INDEX);
687✔
612
    TSDB_CHECK_NULL(pEndCol, code, line, END, terrno)
687!
613
    code = colDataSetVal(pEndCol, i, (const char*)&pRes->ekey, false);
687✔
614
    TSDB_CHECK_CODE(code, line, END);
687!
615
    // uid column
616
    SColumnInfoData* pUidCol = taosArrayGet(pDelBlock->pDataBlock, UID_COLUMN_INDEX);
687✔
617
    TSDB_CHECK_NULL(pUidCol, code, line, END, terrno)
687!
618

619
    int64_t* pUid = taosArrayGet(pRes->uidList, i);
687✔
620
    code = colDataSetVal(pUidCol, i, (const char*)pUid, false);
687✔
621
    TSDB_CHECK_CODE(code, line, END);
687!
622
    void* tmp = taosArrayGet(pDelBlock->pDataBlock, GROUPID_COLUMN_INDEX);
687✔
623
    TSDB_CHECK_NULL(tmp, code, line, END, terrno)
687!
624
    colDataSetNULL(tmp, i);
687!
625
    tmp = taosArrayGet(pDelBlock->pDataBlock, CALCULATE_START_TS_COLUMN_INDEX);
687✔
626
    TSDB_CHECK_NULL(tmp, code, line, END, terrno)
687!
627
    colDataSetNULL(tmp, i);
687!
628
    tmp = taosArrayGet(pDelBlock->pDataBlock, CALCULATE_END_TS_COLUMN_INDEX);
687✔
629
    TSDB_CHECK_NULL(tmp, code, line, END, terrno)
687!
630
    colDataSetNULL(tmp, i);
687!
631
    tmp = taosArrayGet(pDelBlock->pDataBlock, TABLE_NAME_COLUMN_INDEX);
687✔
632
    TSDB_CHECK_NULL(tmp, code, line, END, terrno)
687!
633
    colDataSetNULL(tmp, i);
687!
634
  }
635

636
  if (type == 0) {
683✔
637
    code = taosAllocateQitem(sizeof(SStreamRefDataBlock), DEF_QITEM, 0, pRefBlock);
676✔
638
    if (code) {
676!
639
      blockDataCleanup(pDelBlock);
×
640
      taosMemoryFree(pDelBlock);
×
641
      return code;
×
642
    }
643

644
    ((SStreamRefDataBlock*)(*pRefBlock))->type = STREAM_INPUT__REF_DATA_BLOCK;
676✔
645
    ((SStreamRefDataBlock*)(*pRefBlock))->pBlock = pDelBlock;
676✔
646
  } else if (type == 1) {
7!
647
    *pRefBlock = pDelBlock;
7✔
648
  } else {
649
    tqError("unknown type:%d", type);
×
650
    code = TSDB_CODE_TMQ_CONSUMER_ERROR;
×
651
  }
652

653
END:
1,884✔
654
  if (code != 0) {
1,884!
655
    tqError("failed to extract delete data block, line:%d code:%d", line, code);
×
656
  }
657
  tDecoderClear(pCoder);
1,884✔
658
  taosArrayDestroy(pRes->uidList);
1,884✔
659
  return code;
1,884✔
660
}
661

662
int32_t tqGetStreamExecInfo(SVnode* pVnode, int64_t streamId, int64_t* pDelay, bool* fhFinished) {
4✔
663
  SStreamMeta* pMeta = pVnode->pTq->pStreamMeta;
4✔
664
  int32_t      numOfTasks = taosArrayGetSize(pMeta->pTaskList);
4✔
665
  int32_t      code = TSDB_CODE_SUCCESS;
4✔
666

667
  if (pDelay != NULL) {
4!
668
    *pDelay = 0;
4✔
669
  }
670

671
  *fhFinished = false;
4✔
672

673
  if (numOfTasks <= 0) {
4!
674
    return code;
×
675
  }
676

677
  // extract the required source task for a given stream, identified by streamId
678
  streamMetaRLock(pMeta);
4✔
679

680
  numOfTasks = taosArrayGetSize(pMeta->pTaskList);
4✔
681

682
  for (int32_t i = 0; i < numOfTasks; ++i) {
20✔
683
    SStreamTaskId* pId = taosArrayGet(pMeta->pTaskList, i);
16✔
684
    if (pId == NULL) {
16!
685
      continue;
12✔
686
    }
687
    if (pId->streamId != streamId) {
16✔
688
      continue;
8✔
689
    }
690

691
    STaskId      id = {.streamId = pId->streamId, .taskId = pId->taskId};
8✔
692
    SStreamTask* pTask = NULL;
8✔
693

694
    code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
8✔
695
    if (code != 0) {
8!
696
      tqError("vgId:%d failed to acquire task:0x%x in retrieving progress", pMeta->vgId, pId->taskId);
×
697
      continue;
×
698
    }
699

700
    if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
8✔
701
      streamMetaReleaseTask(pMeta, pTask);
4✔
702
      continue;
4✔
703
    }
704

705
    // here we get the required stream source task
706
    *fhFinished = !HAS_RELATED_FILLHISTORY_TASK(pTask);
4✔
707

708
    int64_t ver = walReaderGetCurrentVer(pTask->exec.pWalReader);
4✔
709
    if (ver == -1) {
4!
710
      ver = pTask->chkInfo.processedVer;
4✔
711
    } else {
UNCOV
712
      ver--;
×
713
    }
714

715
    SVersionRange verRange = {0};
4✔
716
    walReaderValidVersionRange(pTask->exec.pWalReader, &verRange.minVer, &verRange.maxVer);
4✔
717

718
    SWalReader* pReader = walOpenReader(pTask->exec.pWalReader->pWal, NULL, 0);
4✔
719
    if (pReader == NULL) {
4!
720
      tqError("failed to open wal reader to extract exec progress, vgId:%d", pMeta->vgId);
×
721
      streamMetaReleaseTask(pMeta, pTask);
×
722
      continue;
×
723
    }
724

725
    int64_t cur = 0;
4✔
726
    int64_t latest = 0;
4✔
727

728
    code = walFetchHead(pReader, ver);
4✔
729
    if (code == TSDB_CODE_SUCCESS) {
4!
730
      cur = pReader->pHead->head.ingestTs;
4✔
731
    }
732

733
    if (ver == verRange.maxVer) {
4!
UNCOV
734
      latest = cur;
×
735
    } else {
736
      code = walFetchHead(pReader, verRange.maxVer);
4✔
737
      if (code == TSDB_CODE_SUCCESS) {
4!
738
        latest = pReader->pHead->head.ingestTs;
4✔
739
      }
740
    }
741

742
    if (pDelay != NULL) {  // delay in ms
4!
743
      *pDelay = (latest - cur) / 1000;
4✔
744
    }
745

746
    walCloseReader(pReader);
4✔
747
    streamMetaReleaseTask(pMeta, pTask);
4✔
748
  }
749

750
  streamMetaRUnLock(pMeta);
4✔
751

752
  return TSDB_CODE_SUCCESS;
4✔
753
}
754

UNCOV
755
int32_t tqExtractDropCtbDataBlock(const void* data, int32_t len, int64_t ver, void** pRefBlock, int32_t type) {
×
UNCOV
756
  int32_t          code = 0;
×
UNCOV
757
  int32_t          lino = 0;
×
UNCOV
758
  SDecoder         dc = {0};
×
UNCOV
759
  SVDropTbBatchReq batchReq = {0};
×
UNCOV
760
  tDecoderInit(&dc, (uint8_t*)data, len);
×
UNCOV
761
  code = tDecodeSVDropTbBatchReq(&dc, &batchReq);
×
UNCOV
762
  TSDB_CHECK_CODE(code, lino, _exit);
×
UNCOV
763
  if (batchReq.nReqs <= 0) goto _exit;
×
764

UNCOV
765
  SSDataBlock* pBlock = NULL;
×
UNCOV
766
  code = createSpecialDataBlock(STREAM_DROP_CHILD_TABLE, &pBlock);
×
UNCOV
767
  TSDB_CHECK_CODE(code, lino, _exit);
×
768

UNCOV
769
  code = blockDataEnsureCapacity(pBlock, batchReq.nReqs);
×
UNCOV
770
  TSDB_CHECK_CODE(code, lino, _exit);
×
771

UNCOV
772
  pBlock->info.rows = batchReq.nReqs;
×
UNCOV
773
  pBlock->info.version = ver;
×
UNCOV
774
  for (int32_t i = 0; i < batchReq.nReqs; ++i) {
×
UNCOV
775
    SVDropTbReq* pReq = batchReq.pReqs + i;
×
UNCOV
776
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, UID_COLUMN_INDEX);
×
UNCOV
777
    TSDB_CHECK_NULL(pCol, code, lino, _exit, terrno);
×
UNCOV
778
    code = colDataSetVal(pCol, i, (const char* )&pReq->uid, false);
×
UNCOV
779
    TSDB_CHECK_CODE(code, lino, _exit);
×
780
  }
781

UNCOV
782
  code = taosAllocateQitem(sizeof(SStreamRefDataBlock), DEF_QITEM, 0, pRefBlock);
×
UNCOV
783
  TSDB_CHECK_CODE(code, lino, _exit);
×
UNCOV
784
  ((SStreamRefDataBlock*)(*pRefBlock))->type = STREAM_INPUT__REF_DATA_BLOCK;
×
UNCOV
785
  ((SStreamRefDataBlock*)(*pRefBlock))->pBlock = pBlock;
×
786

UNCOV
787
_exit:
×
UNCOV
788
  tDecoderClear(&dc);
×
UNCOV
789
  if (TSDB_CODE_SUCCESS != code) {
×
790
    tqError("faled to extract drop ctb data block, line:%d code:%s", lino, tstrerror(code));
×
791
    blockDataCleanup(pBlock);
×
792
    taosMemoryFree(pBlock);
×
793
  }
UNCOV
794
  return code;
×
795
}
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