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

taosdata / TDengine / #3599

08 Feb 2025 11:23AM UTC coverage: 1.77% (-61.6%) from 63.396%
#3599

push

travis-ci

web-flow
Merge pull request #29712 from taosdata/fix/TD-33652-3.0

fix: reduce write rows from 30w to 3w

3776 of 278949 branches covered (1.35%)

Branch coverage included in aggregate %.

6012 of 274147 relevant lines covered (2.19%)

1642.73 hits per line

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

0.0
/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) {
×
24
  int32_t    code = TDB_CODE_SUCCESS;
×
25
  int32_t    lino = 0;
×
26
  tqDebug("%s called", __FUNCTION__ );
×
27
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
×
28

29
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
×
30
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);
×
31

32
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
×
33
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
×
34

35
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
×
36
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
×
37
  pRsp->withTbName = 0;
×
38
  pRsp->withSchema = false;
×
39

40
END:
×
41
  if (code != 0){
×
42
    tqError("%s failed at:%d, code:%s", __FUNCTION__ , lino, tstrerror(code));
×
43
  }
44
  return code;
×
45
}
46

47
void tqUpdateNodeStage(STQ* pTq, bool isLeader) {
×
48
  SSyncState state = syncGetState(pTq->pVnode->sync);
×
49
  streamMetaUpdateStageRole(pTq->pStreamMeta, state.term, isLeader);
×
50
}
×
51

52
static int32_t tqInitTaosxRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
×
53
  int32_t    code = TDB_CODE_SUCCESS;
×
54
  int32_t    lino = 0;
×
55
  tqDebug("%s called", __FUNCTION__ );
×
56
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
×
57
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
×
58
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
×
59

60
  pRsp->withTbName = 1;
×
61
  pRsp->withSchema = 1;
×
62
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
×
63
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);\
×
64

65
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
×
66
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
×
67

68
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
×
69
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
×
70

71
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
×
72
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
×
73

74

75
END:
×
76
  if (code != 0){
×
77
    tqError("%s failed at:%d, code:%s", __FUNCTION__ , lino, tstrerror(code));
×
78
    taosArrayDestroy(pRsp->blockData);
×
79
    taosArrayDestroy(pRsp->blockDataLen);
×
80
    taosArrayDestroy(pRsp->blockTbName);
×
81
    taosArrayDestroy(pRsp->blockSchema);
×
82
  }
83
  return code;
×
84
}
85

86
static int32_t extractResetOffsetVal(STqOffsetVal* pOffsetVal, STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
×
87
                                     SRpcMsg* pMsg, bool* pBlockReturned) {
88
  if (pOffsetVal == NULL || pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL || pBlockReturned == NULL) {
×
89
    return TSDB_CODE_INVALID_PARA;
×
90
  }
91
  uint64_t   consumerId = pRequest->consumerId;
×
92
  STqOffset* pOffset = NULL;
×
93
  int32_t    code = tqMetaGetOffset(pTq, pRequest->subKey, &pOffset);
×
94
  int32_t    vgId = TD_VID(pTq->pVnode);
×
95

96
  *pBlockReturned = false;
×
97
  // In this vnode, data has been polled by consumer for this topic, so let's continue from the last offset value.
98
  if (code == 0) {
×
99
    tOffsetCopy(pOffsetVal, &pOffset->val);
×
100

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

128
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
×
129
      if (code != 0) {
×
130
        return code;
×
131
      }
132
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
×
133
              pHandle->subKey, vgId, dataRsp.rspOffset.version);
134
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
135
      tDeleteMqDataRsp(&dataRsp);
×
136

137
      *pBlockReturned = true;
×
138
      return code;
×
139
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_NONE) {
×
140
      tqError("tmq poll: subkey:%s, no offset committed for consumer:0x%" PRIx64
×
141
                  " in vg %d, subkey %s, reset none failed",
142
              pHandle->subKey, consumerId, vgId, pRequest->subKey);
143
      return TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
×
144
    }
145
  }
146

147
  return 0;
×
148
}
149

150
static int32_t extractDataAndRspForNormalSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
×
151
                                                   SRpcMsg* pMsg, STqOffsetVal* pOffset) {
152
  int32_t    code = TDB_CODE_SUCCESS;
×
153
  int32_t    lino = 0;
×
154
  tqDebug("%s called", __FUNCTION__ );
×
155
  uint64_t consumerId = pRequest->consumerId;
×
156
  int32_t  vgId = TD_VID(pTq->pVnode);
×
157
  terrno = 0;
×
158

159
  SMqDataRsp dataRsp = {0};
×
160
  code = tqInitDataRsp(&dataRsp, *pOffset);
×
161
  TSDB_CHECK_CODE(code, lino, end);
×
162

163
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
×
164
  TSDB_CHECK_CODE(code, lino, end);
×
165

166
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
×
167
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
×
168
    goto end;
×
169
  }
170

171
  //   till now, all data has been transferred to consumer, new data needs to push client once arrived.
172
  if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST && dataRsp.blockNum == 0) {
×
173
    // lock
174
    taosWLockLatch(&pTq->lock);
×
175
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
×
176
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
×
177
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
×
178
      taosWUnLockLatch(&pTq->lock);
×
179
      goto end;
×
180
    }
181
    taosWUnLockLatch(&pTq->lock);
×
182
  }
183

184
  // reqOffset represents the current date offset, may be changed if wal not exists
185
  tOffsetCopy(&dataRsp.reqOffset, pOffset);
×
186
  code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
187

188
end:
×
189
  {
190
    char buf[TSDB_OFFSET_LEN] = {0};
×
191
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
×
192
    if (code != 0){
×
193
      tqError("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s,QID:0x%" PRIx64 " error msg:%s, line:%d",
×
194
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, tstrerror(code), lino);
195
    } else {
196
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s,QID:0x%" PRIx64 " success",
×
197
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
198
    }
199

200
    tDeleteMqDataRsp(&dataRsp);
×
201
    return code;
×
202
  }
203
}
204

205
#define PROCESS_EXCLUDED_MSG(TYPE, DECODE_FUNC, DELETE_FUNC)                                               \
206
  SDecoder decoder = {0};                                                                                  \
207
  TYPE     req = {0};                                                                                      \
208
  void*    data = POINTER_SHIFT(pHead->body, sizeof(SMsgHead));                                            \
209
  int32_t  len = pHead->bodyLen - sizeof(SMsgHead);                                                        \
210
  tDecoderInit(&decoder, data, len);                                                                       \
211
  if (DECODE_FUNC(&decoder, &req) == 0 && (req.source & TD_REQ_FROM_TAOX) != 0) {                          \
212
    tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, jump meta for, vgId:%d offset %" PRId64 \
213
            " msgType %d",                                                                                 \
214
            pRequest->consumerId, pRequest->epoch, vgId, fetchVer, pHead->msgType);                        \
215
    fetchVer++;                                                                                            \
216
    DELETE_FUNC(&req);                                                                                     \
217
    tDecoderClear(&decoder);                                                                               \
218
    continue;                                                                                              \
219
  }                                                                                                        \
220
  DELETE_FUNC(&req);                                                                                       \
221
  tDecoderClear(&decoder);
222

223
static void tDeleteCommon(void* parm) {}
×
224

225
static int32_t extractDataAndRspForDbStbSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
×
226
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
227
  int32_t         vgId = TD_VID(pTq->pVnode);
×
228
  SMqDataRsp      taosxRsp = {0};
×
229
  SMqBatchMetaRsp btMetaRsp = {0};
×
230
  int32_t         code = 0;
×
231

232
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
×
233
  if (offset->type != TMQ_OFFSET__LOG) {
×
234
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest->timeout));
×
235

236
    if (taosArrayGetSize(btMetaRsp.batchMetaReq) > 0) {
×
237
      code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
238
      tqDebug("tmq poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send meta offset type:%d,uid:%" PRId64 ",ts:%" PRId64,
×
239
              pRequest->consumerId, pHandle->subKey, vgId, btMetaRsp.rspOffset.type, btMetaRsp.rspOffset.uid,btMetaRsp.rspOffset.ts);
240
      goto END;
×
241
    }
242

243
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64",ts:%" PRId64,
×
244
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type, taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
245
    if (taosxRsp.blockNum > 0) {
×
246
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
247
      goto END;
×
248
    } else {
249
      tOffsetCopy(offset, &taosxRsp.rspOffset);
×
250
    }
251
  }
252

253
  if (offset->type == TMQ_OFFSET__LOG) {
×
254
    walReaderVerifyOffset(pHandle->pWalReader, offset);
×
255
    int64_t fetchVer = offset->version;
×
256

257
    uint64_t st = taosGetTimestampMs();
×
258
    int      totalRows = 0;
×
259
    int32_t  totalMetaRows = 0;
×
260
    while (1) {
×
261
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
×
262
      if (savedEpoch > pRequest->epoch) {
×
263
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
264
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
265
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
266
        goto END;
×
267
      }
268

269
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
×
270
        if (totalMetaRows > 0) {
×
271
          tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
×
272
          code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
273
          goto END;
×
274
        }
275
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);
×
276
        code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp,
×
277
                             taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
278
        goto END;
×
279
      }
280

281
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
×
282
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %s",
×
283
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, TMSG_INFO(pHead->msgType));
284

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

294
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
×
295
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
×
296
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
×
297
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
×
298
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteCommon)
×
299
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
×
300
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
×
301
          } else if (pHead->msgType == TDMT_VND_DELETE) {
×
302
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
×
303
          }
304
        }
305

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

318
        if (!btMetaRsp.batchMetaReq) {
×
319
          btMetaRsp.batchMetaReq = taosArrayInit(4, POINTER_BYTES);
×
320
          TQ_NULL_GO_TO_END(btMetaRsp.batchMetaReq);
×
321
          btMetaRsp.batchMetaLen = taosArrayInit(4, sizeof(int32_t));
×
322
          TQ_NULL_GO_TO_END(btMetaRsp.batchMetaLen);
×
323
        }
324
        fetchVer++;
×
325

326
        SMqMetaRsp tmpMetaRsp = {0};
×
327
        tmpMetaRsp.resMsgType = pHead->msgType;
×
328
        tmpMetaRsp.metaRspLen = pHead->bodyLen;
×
329
        tmpMetaRsp.metaRsp = pHead->body;
×
330
        uint32_t len = 0;
×
331
        tEncodeSize(tEncodeMqMetaRsp, &tmpMetaRsp, len, code);
×
332
        if (TSDB_CODE_SUCCESS != code) {
×
333
          tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
334
          continue;
×
335
        }
336
        int32_t tLen = sizeof(SMqRspHead) + len;
×
337
        void*   tBuf = taosMemoryCalloc(1, tLen);
×
338
        TQ_NULL_GO_TO_END(tBuf);
×
339
        void*    metaBuff = POINTER_SHIFT(tBuf, sizeof(SMqRspHead));
×
340
        SEncoder encoder = {0};
×
341
        tEncoderInit(&encoder, metaBuff, len);
×
342
        code = tEncodeMqMetaRsp(&encoder, &tmpMetaRsp);
×
343
        tEncoderClear(&encoder);
×
344

345
        if (code < 0) {
×
346
          tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
347
          continue;
×
348
        }
349
        TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp.batchMetaReq, &tBuf));
×
350
        TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp.batchMetaLen, &tLen));
×
351
        totalMetaRows++;
×
352
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= tmqRowSize) || (taosGetTimestampMs() - st > TMIN(TQ_POLL_MAX_TIME, pRequest->timeout))) {
×
353
          tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
×
354
          code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
355
          goto END;
×
356
        }
357
        continue;
×
358
      }
359

360
      if (totalMetaRows > 0) {
×
361
        tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
×
362
        code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
363
        goto END;
×
364
      }
365

366
      // process data
367
      SPackedData submit = {
×
368
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
×
369
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
×
370
          .ver = pHead->version,
×
371
      };
372

373
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest->sourceExcluded));
×
374

375
      if (totalRows >= tmqRowSize || (taosGetTimestampMs() - st > TMIN(TQ_POLL_MAX_TIME, pRequest->timeout))) {
×
376
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer + 1);
×
377
        code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp,
×
378
                             taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
379
        goto END;
×
380
      } else {
381
        fetchVer++;
×
382
      }
383
    }
384
  }
385

386
END:
×
387
  if (code != 0){
×
388
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
389
            pRequest->subKey);
390
  }
391
  tDeleteMqBatchMetaRsp(&btMetaRsp);
×
392
  tDeleteSTaosxRsp(&taosxRsp);
×
393
  return code;
×
394
}
395

396
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
×
397
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
×
398
    return TSDB_CODE_TMQ_INVALID_MSG;
×
399
  }
400
  int32_t      code = 0;
×
401
  STqOffsetVal reqOffset = {0};
×
402
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
×
403

404
  // reset the offset if needed
405
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
×
406
    bool blockReturned = false;
×
407
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
×
408
    if (code != 0) {
×
409
      goto END;
×
410
    }
411

412
    // empty block returned, quit
413
    if (blockReturned) {
×
414
      goto END;
×
415
    }
416
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
×
417
    uError("req offset type is 0");
×
418
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
419
    goto END;
×
420
  }
421

422
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
×
423
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
×
424
  } else {
425
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
×
426
  }
427

428
END:
×
429
  if (code != 0){
×
430
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
×
431
  }
432
  tOffsetDestroy(&reqOffset);
×
433
  return code;
×
434
}
435

436
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
×
437
                          int64_t ever) {
438
  if (pMsgHead == NULL) {
×
439
    return;
×
440
  }
441
  pMsgHead->consumerId = consumerId;
×
442
  pMsgHead->epoch = epoch;
×
443
  pMsgHead->mqMsgType = type;
×
444
  pMsgHead->walsver = sver;
×
445
  pMsgHead->walever = ever;
×
446
}
447

448
int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
×
449
                               const SMqBatchMetaRsp* pRsp, int32_t vgId) {
450
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
×
451
    return TSDB_CODE_TMQ_INVALID_MSG;
×
452
  }
453
  int32_t len = 0;
×
454
  int32_t code = 0;
×
455
  tEncodeSize(tEncodeMqBatchMetaRsp, pRsp, len, code);
×
456
  if (code < 0) {
×
457
    return TAOS_GET_TERRNO(code);
×
458
  }
459
  int32_t tlen = sizeof(SMqRspHead) + len;
×
460
  void*   buf = rpcMallocCont(tlen);
×
461
  if (buf == NULL) {
×
462
    return TAOS_GET_TERRNO(terrno);
×
463
  }
464

465
  int64_t sver = 0, ever = 0;
×
466
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
467
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
×
468

469
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
470

471
  SEncoder encoder = {0};
×
472
  tEncoderInit(&encoder, abuf, len);
×
473
  code = tEncodeMqBatchMetaRsp(&encoder, pRsp);
×
474
  tEncoderClear(&encoder);
×
475
  if (code < 0) {
×
476
    rpcFreeCont(buf);
×
477
    return TAOS_GET_TERRNO(code);
×
478
  }
479
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
×
480

481
  tmsgSendRsp(&resp);
×
482
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
×
483
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
484

485
  return 0;
×
486
}
487

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

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

509
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
510

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

520
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
×
521

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

526
  return 0;
×
527
}
528

529
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, const SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
×
530
                        int32_t type, int64_t sver, int64_t ever) {
531
  if (pRpcHandleInfo == NULL || pRsp == NULL) {
×
532
    return TSDB_CODE_TMQ_INVALID_MSG;
×
533
  }
534
  int32_t len = 0;
×
535
  int32_t code = 0;
×
536

537
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP || type == TMQ_MSG_TYPE__WALINFO_RSP) {
×
538
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
×
539
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
540
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
×
541
  }
542

543
  if (code < 0) {
×
544
    return TAOS_GET_TERRNO(code);
×
545
  }
546

547
  int32_t tlen = sizeof(SMqRspHead) + len;
×
548
  void*   buf = rpcMallocCont(tlen);
×
549
  if (buf == NULL) {
×
550
    return terrno;
×
551
  }
552

553
  SMqRspHead* pHead = (SMqRspHead*)buf;
×
554
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
×
555

556
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
557

558
  SEncoder encoder = {0};
×
559
  tEncoderInit(&encoder, abuf, len);
×
560

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

573
  tmsgSendRsp(&rsp);
×
574
  return 0;
×
575
}
576

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

583
  *pRefBlock = NULL;
×
584

585
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
586
  TSDB_CHECK_NULL(pRes->uidList, code, line, END, terrno)
×
587

588
  tDecoderInit(pCoder, (uint8_t*)pData, len);
×
589
  code = tDecodeDeleteRes(pCoder, pRes);
×
590
  TSDB_CHECK_CODE(code, line, END);
×
591

592
  int32_t numOfTables = taosArrayGetSize(pRes->uidList);
×
593
  if (numOfTables == 0 || pRes->affectedRows == 0) {
×
594
    goto END;
×
595
  }
596

597
  SSDataBlock* pDelBlock = NULL;
×
598
  code = createSpecialDataBlock(blockType, &pDelBlock);
×
599
  TSDB_CHECK_CODE(code, line, END);
×
600

601
  code = blockDataEnsureCapacity(pDelBlock, numOfTables);
×
602
  TSDB_CHECK_CODE(code, line, END);
×
603

604
  pDelBlock->info.rows = numOfTables;
×
605
  pDelBlock->info.version = ver;
×
606

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

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

638
  if (type == 0) {
×
639
    code = taosAllocateQitem(sizeof(SStreamRefDataBlock), DEF_QITEM, 0, pRefBlock);
×
640
    if (code) {
×
641
      blockDataCleanup(pDelBlock);
×
642
      taosMemoryFree(pDelBlock);
×
643
      return code;
×
644
    }
645

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

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

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

669
  if (pDelay != NULL) {
×
670
    *pDelay = 0;
×
671
  }
672

673
  *fhFinished = false;
×
674

675
  if (numOfTasks <= 0) {
×
676
    return code;
×
677
  }
678

679
  // extract the required source task for a given stream, identified by streamId
680
  streamMetaRLock(pMeta);
×
681

682
  numOfTasks = taosArrayGetSize(pMeta->pTaskList);
×
683

684
  for (int32_t i = 0; i < numOfTasks; ++i) {
×
685
    SStreamTaskId* pId = taosArrayGet(pMeta->pTaskList, i);
×
686
    if (pId == NULL) {
×
687
      continue;
×
688
    }
689
    if (pId->streamId != streamId) {
×
690
      continue;
×
691
    }
692

693
    STaskId      id = {.streamId = pId->streamId, .taskId = pId->taskId};
×
694
    SStreamTask* pTask = NULL;
×
695

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

702
    if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
×
703
      streamMetaReleaseTask(pMeta, pTask);
×
704
      continue;
×
705
    }
706

707
    // here we get the required stream source task
708
    *fhFinished = !HAS_RELATED_FILLHISTORY_TASK(pTask);
×
709

710
    int64_t ver = walReaderGetCurrentVer(pTask->exec.pWalReader);
×
711
    if (ver == -1) {
×
712
      ver = pTask->chkInfo.processedVer;
×
713
    } else {
714
      ver--;
×
715
    }
716

717
    SVersionRange verRange = {0};
×
718
    walReaderValidVersionRange(pTask->exec.pWalReader, &verRange.minVer, &verRange.maxVer);
×
719

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

727
    int64_t cur = 0;
×
728
    int64_t latest = 0;
×
729

730
    code = walFetchHead(pReader, ver);
×
731
    if (code == TSDB_CODE_SUCCESS) {
×
732
      cur = pReader->pHead->head.ingestTs;
×
733
    }
734

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

744
    if (pDelay != NULL) {  // delay in ms
×
745
      *pDelay = (latest - cur) / 1000;
×
746
    }
747

748
    walCloseReader(pReader);
×
749
    streamMetaReleaseTask(pMeta, pTask);
×
750
  }
751

752
  streamMetaRUnLock(pMeta);
×
753

754
  return TSDB_CODE_SUCCESS;
×
755
}
756

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

767
  SSDataBlock* pBlock = NULL;
×
768
  code = createSpecialDataBlock(STREAM_DROP_CHILD_TABLE, &pBlock);
×
769
  TSDB_CHECK_CODE(code, lino, _exit);
×
770

771
  code = blockDataEnsureCapacity(pBlock, batchReq.nReqs);
×
772
  TSDB_CHECK_CODE(code, lino, _exit);
×
773

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

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

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