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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

42.68
/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) {
652✔
24
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
652✔
25
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
652✔
26

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

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

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

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

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

54
  if (pRsp->blockData == NULL || pRsp->blockDataLen == NULL ||
59!
55
      pRsp->blockTbName == NULL || pRsp->blockSchema == NULL) {
59!
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;
59✔
80
}
81

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

89
  *pBlockReturned = false;
136✔
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) {
136✔
92
    tOffsetCopy(pOffsetVal, &pOffset->val);
19✔
93

94
    char formatBuf[TSDB_OFFSET_LEN] = {0};
19✔
95
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
19✔
96
    tqDebug("tmq poll: consumer:0x%" PRIx64
19!
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;
19✔
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) {
117!
103
      if (pRequest->useSnapshot) {
117✔
104
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
14!
105
                consumerId, pHandle->subKey, vgId);
106
        if (pHandle->fetchMeta) {
14✔
107
          tqOffsetResetToMeta(pOffsetVal, 0);
108
        } else {
109
          SValue val = {0};
12!
110
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
12✔
111
        }
112
      } else {
113
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
103✔
114
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
103✔
115
      }
UNCOV
116
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
UNCOV
117
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
×
UNCOV
118
      SMqDataRsp dataRsp = {0};
×
UNCOV
119
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
×
120

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

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

140
  return 0;
117✔
141
}
142

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

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

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

160
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
555✔
161
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
555!
162
    goto end;
×
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) {
555✔
167
    // lock
168
    taosWLockLatch(&pTq->lock);
217✔
169
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
217✔
170
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
217✔
171
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
214✔
172
      taosWUnLockLatch(&pTq->lock);
214✔
173
      goto end;
214✔
174
    }
175
    taosWUnLockLatch(&pTq->lock);
3✔
176
  }
177

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

182
end : {
555✔
183
  char buf[TSDB_OFFSET_LEN] = {0};
555✔
184
  tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
555✔
185
  tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s,QID:0x%" PRIx64
555!
186
          " code:%d",
187
          consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, code);
188
  tDeleteMqDataRsp(&dataRsp);
555✔
189
  return code;
555✔
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) {}
×
212

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

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

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

233
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64
9!
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) {
9✔
238
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
6✔
239
      goto END;
6✔
240
    } else {
241
      tOffsetCopy(offset, &taosxRsp.rspOffset);
3✔
242
    }
243
  }
244

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

249
    uint64_t st = taosGetTimestampMs();
51✔
250
    int      totalRows = 0;
51✔
251
    int32_t  totalMetaRows = 0;
51✔
252
    while (1) {
50✔
253
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
101✔
254
      if (savedEpoch > pRequest->epoch) {
101!
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;
51✔
259
      }
260

261
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
101✔
262
        if (totalMetaRows > 0) {
19!
263
          tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);
×
264
          code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
265
          if (totalRows != 0) {
×
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;
×
271
        }
272
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);
19✔
273
        code = tqSendDataRsp(
19✔
274
            pHandle, pMsg, pRequest, &taosxRsp,
275
            taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
19!
276
        goto END;
19✔
277
      }
278

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

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

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

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

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

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

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

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

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

387
      code = tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest->sourceExcluded);
82✔
388
      if (code < 0) {
82!
UNCOV
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)) {
132!
395
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer + 1);
32✔
396
        code = tqSendDataRsp(
32✔
397
            pHandle, pMsg, pRequest, &taosxRsp,
398
            taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
32!
399
        goto END;
32✔
400
      } else {
401
        fetchVer++;
50✔
402
      }
403
    }
404
  }
405

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

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

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

425
    // empty block returned, quit
426
    if (blockReturned) {
136!
UNCOV
427
      goto END;
×
428
    }
429
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
478!
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) {
614✔
436
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
555✔
437
  } else {
438
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
59✔
439
  }
440

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

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

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

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

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

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

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

489
  return 0;
2✔
490
}
491

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

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

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

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

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

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

527
  return 0;
×
528
}
529

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

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

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

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

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

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

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

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

571
  tmsgSendRsp(&rsp);
495✔
572
  return 0;
495✔
573
}
574

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

581
  *pRefBlock = NULL;
22,149✔
582

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

586
  tDecoderInit(pCoder, (uint8_t*)pData, len);
22,150✔
587
  code = tDecodeDeleteRes(pCoder, pRes);
22,149✔
588
  TSDB_CHECK_CODE(code, line, END);
22,116!
589

590
  int32_t numOfTables = taosArrayGetSize(pRes->uidList);
22,116✔
591
  if (numOfTables == 0 || pRes->affectedRows == 0) {
22,114✔
592
    goto END;
21,194✔
593
  }
594

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

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

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

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

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

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

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

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

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

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

671
  *fhFinished = false;
×
672

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

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

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

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

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

694
    code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
695
    if (code != 0) {
×
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) {
×
701
      streamMetaReleaseTask(pMeta, pTask);
×
702
      continue;
×
703
    }
704

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

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

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

718
    SWalReader* pReader = walOpenReader(pTask->exec.pWalReader->pWal, NULL, 0);
×
719
    if (pReader == NULL) {
×
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;
×
726
    int64_t latest = 0;
×
727

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

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

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

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

750
  streamMetaRUnLock(pMeta);
×
751

752
  return TSDB_CODE_SUCCESS;
×
753
}
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