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

taosdata / TDengine / #4142

20 May 2025 07:22AM UTC coverage: 62.238% (-0.9%) from 63.096%
#4142

push

travis-ci

web-flow
docs(datain): add topic meta options docs in tmq (#31147)

155113 of 318088 branches covered (48.76%)

Branch coverage included in aggregate %.

240242 of 317147 relevant lines covered (75.75%)

13602566.83 hits per line

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

60.4
/source/dnode/vnode/src/tq/tq.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
#include "osDef.h"
18
#include "taoserror.h"
19
#include "tqCommon.h"
20
#include "tstream.h"
21
#include "vnd.h"
22

23
// 0: not init
24
// 1: already inited
25
// 2: wait to be inited or cleanup
26
static int32_t tqInitialize(STQ* pTq);
27

28
static FORCE_INLINE bool tqIsHandleExec(STqHandle* pHandle) { return pHandle != NULL ? TMQ_HANDLE_STATUS_EXEC == pHandle->status : true; }
50,987!
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
48,431✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
48,442✔
31

32
void tqDestroyTqHandle(void* data) {
1,544✔
33
  if (data == NULL) return;
1,544!
34
  STqHandle* pData = (STqHandle*)data;
1,544✔
35
  qDestroyTask(pData->execHandle.task);
1,544✔
36

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,544✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
1,202!
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
342✔
40
    tqReaderClose(pData->execHandle.pTqReader);
270✔
41
    walCloseReader(pData->pWalReader);
270✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
270✔
43
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
72!
44
    walCloseReader(pData->pWalReader);
72✔
45
    tqReaderClose(pData->execHandle.pTqReader);
72✔
46
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
72!
47
    nodesDestroyNode(pData->execHandle.execTb.node);
72✔
48
  }
49
  if (pData->msg != NULL) {
1,543!
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
1,543!
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
1,543✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
1,497✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
1,544✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
4,066✔
64
  if (pLeft == NULL || pRight == NULL) {
4,066!
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
7,855✔
68
         pLeft->val.version == pRight->val.version;
3,789✔
69
}
70

71
int32_t tqOpen(const char* path, SVnode* pVnode) {
13,886✔
72
  if (path == NULL || pVnode == NULL) {
13,886!
73
    return TSDB_CODE_INVALID_PARA;
×
74
  }
75
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
13,899!
76
  if (pTq == NULL) {
13,900!
77
    return terrno;
×
78
  }
79

80
  pVnode->pTq = pTq;
13,900✔
81
  pTq->pVnode = pVnode;
13,900✔
82

83
  pTq->path = taosStrdup(path);
13,900!
84
  if (pTq->path == NULL) {
13,898!
85
    return terrno;
×
86
  }
87

88
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
13,898✔
89
  if (pTq->pHandle == NULL) {
13,899!
90
    return terrno;
×
91
  }
92
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
13,899✔
93

94
  taosInitRWLatch(&pTq->lock);
13,900✔
95

96
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
13,898✔
97
  if (pTq->pPushMgr == NULL) {
13,899!
98
    return terrno;
×
99
  }
100

101
  pTq->pCheckInfo = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
13,899✔
102
  if (pTq->pCheckInfo == NULL) {
13,903!
103
    return terrno;
×
104
  }
105
  taosHashSetFreeFp(pTq->pCheckInfo, (FDelete)tDeleteSTqCheckInfo);
13,903✔
106

107
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
13,903✔
108
  if (pTq->pOffset == NULL) {
13,902!
109
    return terrno;
×
110
  }
111
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
13,902✔
112

113
  return tqInitialize(pTq);
13,902✔
114
}
115

116
int32_t tqInitialize(STQ* pTq) {
13,902✔
117
  if (pTq == NULL) {
13,902!
118
    return TSDB_CODE_INVALID_PARA;
×
119
  }
120
  int32_t vgId = TD_VID(pTq->pVnode);
13,902✔
121
  int32_t code = streamMetaOpen(pTq->path, pTq, tqBuildStreamTask, tqExpandStreamTask, vgId, -1,
13,902✔
122
                                tqStartTaskCompleteCallback, &pTq->pStreamMeta);
123
  if (code != TSDB_CODE_SUCCESS) {
13,903!
124
    return code;
×
125
  }
126

127
  streamMetaLoadAllTasks(pTq->pStreamMeta);
13,903✔
128
  return tqMetaOpen(pTq);
13,903✔
129
}
130

131
void tqClose(STQ* pTq) {
13,904✔
132
  qDebug("start to close tq");
13,904✔
133
  if (pTq == NULL) {
13,904!
134
    return;
×
135
  }
136

137
  int32_t vgId = 0;
13,904✔
138
  if (pTq->pVnode != NULL) {
13,904✔
139
    vgId = TD_VID(pTq->pVnode);
13,903✔
140
  } else if (pTq->pStreamMeta != NULL) {
1!
141
    vgId = pTq->pStreamMeta->vgId;
×
142
  }
143

144
  // close the stream meta firstly
145
  streamMetaClose(pTq->pStreamMeta);
13,904✔
146

147
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
13,904✔
148
  while (pIter) {
13,939✔
149
    STqHandle* pHandle = *(STqHandle**)pIter;
35✔
150
    if (pHandle->msg != NULL) {
35!
151
      tqPushEmptyDataRsp(pHandle, vgId);
35✔
152
      rpcFreeCont(pHandle->msg->pCont);
35✔
153
      taosMemoryFree(pHandle->msg);
35!
154
      pHandle->msg = NULL;
35✔
155
    }
156
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
35✔
157
  }
158

159
  taosHashCleanup(pTq->pHandle);
13,904✔
160
  taosHashCleanup(pTq->pPushMgr);
13,904✔
161
  taosHashCleanup(pTq->pCheckInfo);
13,904✔
162
  taosHashCleanup(pTq->pOffset);
13,904✔
163
  taosMemoryFree(pTq->path);
13,904!
164
  tqMetaClose(pTq);
13,904✔
165
  qDebug("vgId:%d end to close tq", vgId);
13,904✔
166

167
#if 0
168
  streamMetaFreeTQDuringScanWalError(pTq);
169
#endif
170

171
  taosMemoryFree(pTq);
13,904!
172
}
173

174
void tqNotifyClose(STQ* pTq) {
21,254✔
175
  if (pTq == NULL) {
21,254!
176
    return;
×
177
  }
178

179
  if (pTq->pStreamMeta != NULL) {
21,254!
180
    streamMetaNotifyClose(pTq->pStreamMeta);
21,254✔
181
  }
182
}
183

184
void tqPushEmptyDataRsp(STqHandle* pHandle, int32_t vgId) {
601✔
185
  if (pHandle == NULL) {
601!
186
    return;
×
187
  }
188
  int32_t    code = 0;
601✔
189
  SMqPollReq req = {0};
601✔
190
  code = tDeserializeSMqPollReq(pHandle->msg->pCont, pHandle->msg->contLen, &req);
601✔
191
  if (code < 0) {
601!
192
    tqError("tDeserializeSMqPollReq %d failed, code:%d", pHandle->msg->contLen, code);
×
193
    return;
×
194
  }
195

196
  SMqDataRsp dataRsp = {0};
601✔
197
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
601✔
198
  if (code != 0) {
601!
199
    tqError("tqInitDataRsp failed, code:%d", code);
×
200
    return;
×
201
  }
202
  dataRsp.blockNum = 0;
601✔
203
  char buf[TSDB_OFFSET_LEN] = {0};
601✔
204
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.reqOffset);
601✔
205
  tqInfo("tqPushEmptyDataRsp to consumer:0x%" PRIx64 " vgId:%d, offset:%s, QID:0x%" PRIx64, req.consumerId, vgId, buf,
601!
206
         req.reqId);
207

208
  code = tqSendDataRsp(pHandle, pHandle->msg, &req, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
601✔
209
  if (code != 0) {
600!
210
    tqError("tqSendDataRsp failed, code:%d", code);
×
211
  }
212
  tDeleteMqDataRsp(&dataRsp);
600✔
213
}
214

215
int32_t tqSendDataRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, SMqDataRsp* pRsp, int32_t type,
31,677✔
216
                      int32_t vgId) {
217
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
31,677!
218
    return TSDB_CODE_INVALID_PARA;
×
219
  }
220
  int64_t sver = 0, ever = 0;
31,678✔
221
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
31,678✔
222

223
  char buf1[TSDB_OFFSET_LEN] = {0};
31,680✔
224
  char buf2[TSDB_OFFSET_LEN] = {0};
31,680✔
225
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
31,680✔
226
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
31,677✔
227

228
  tqDebug("tmq poll vgId:%d consumer:0x%" PRIx64 " (epoch %d) start to send rsp, block num:%d, req:%s, rsp:%s, QID:0x%" PRIx64,
31,680!
229
          vgId, pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
230

231
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
31,680✔
232
}
233

234
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
5,049✔
235
  if (pTq == NULL) {
5,049!
236
    return TSDB_CODE_INVALID_PARA;
×
237
  }
238
  SMqVgOffset vgOffset = {0};
5,049✔
239
  int32_t     vgId = TD_VID(pTq->pVnode);
5,049✔
240

241
  int32_t  code = 0;
5,049✔
242
  SDecoder decoder;
243
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
5,049✔
244
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
5,074!
245
    code = TSDB_CODE_INVALID_MSG;
×
246
    goto end;
×
247
  }
248

249
  tDecoderClear(&decoder);
5,063✔
250

251
  STqOffset* pOffset = &vgOffset.offset;
5,049✔
252

253
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
5,049!
254
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
327!
255
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
256
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
4,722✔
257
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
4,717!
258
            pOffset->val.version);
259
  } else {
260
    tqError("invalid commit offset type:%d", pOffset->val.type);
5!
261
    code = TSDB_CODE_INVALID_MSG;
×
262
    goto end;
×
263
  }
264

265
  STqOffset* pSavedOffset = NULL;
5,065✔
266
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
5,065✔
267
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
5,080✔
268
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
4!
269
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
270
    goto end;  // no need to update the offset value
4✔
271
  }
272

273
  // save the new offset value
274
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
5,077✔
275
  if (code != 0) {
5,077!
276
    goto end;
×
277
  }
278

279
  return 0;
5,077✔
280
end:
4✔
281
  tOffsetDestroy(&vgOffset.offset.val);
4✔
282
  return code;
4✔
283
}
284

285
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
24✔
286
  if (pTq == NULL || pMsg == NULL) {
24!
287
    return TSDB_CODE_INVALID_PARA;
×
288
  }
289
  SMqSeekReq req = {0};
24✔
290
  int32_t    vgId = TD_VID(pTq->pVnode);
24✔
291
  SRpcMsg    rsp = {.info = pMsg->info};
24✔
292
  int        code = 0;
24✔
293

294
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
24!
295
    code = TSDB_CODE_OUT_OF_MEMORY;
×
296
    goto end;
×
297
  }
298

299
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
24!
300
  taosWLockLatch(&pTq->lock);
24✔
301

302
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
24✔
303
  if (pHandle == NULL) {
24!
304
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
305
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
306
    taosWUnLockLatch(&pTq->lock);
×
307
    goto end;
×
308
  }
309

310
  // 2. check consumer-vg assignment status
311
  if (pHandle->consumerId != req.consumerId) {
24!
312
    tqError("ERROR tmq seek, consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
313
            req.consumerId, vgId, req.subKey, pHandle->consumerId);
314
    taosWUnLockLatch(&pTq->lock);
×
315
    code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
316
    goto end;
×
317
  }
318

319
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
320
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
321
  tqUnregisterPushHandle(pTq, pHandle);
24✔
322
  taosWUnLockLatch(&pTq->lock);
24✔
323

324
end:
24✔
325
  rsp.code = code;
24✔
326
  tmsgSendRsp(&rsp);
24✔
327
  return 0;
24✔
328
}
329

330
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
234✔
331
  if (pTq == NULL) {
234!
332
    return TSDB_CODE_INVALID_PARA;
×
333
  }
334
  void* pIter = NULL;
234✔
335

336
  while (1) {
11✔
337
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
245✔
338
    if (pIter == NULL) {
245✔
339
      break;
199✔
340
    }
341

342
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
46✔
343

344
    if (pCheck->ntbUid == tbUid) {
46!
345
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
46✔
346
      for (int32_t i = 0; i < sz; i++) {
168✔
347
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
157✔
348
        if (pForbidColId == NULL) {
157!
349
          continue;
×
350
        }
351

352
        if ((*pForbidColId) == colId) {
157✔
353
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
35✔
354
          return -1;
35✔
355
        }
356
      }
357
    }
358
  }
359

360
  return 0;
199✔
361
}
362

363
int32_t tqProcessPollPush(STQ* pTq) {
15,099✔
364
  if (pTq == NULL) {
15,099!
365
    return TSDB_CODE_INVALID_PARA;
×
366
  }
367
  int32_t vgId = TD_VID(pTq->pVnode);
15,099✔
368
  taosWLockLatch(&pTq->lock);
15,099✔
369
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
15,099!
370
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
15,099✔
371

372
    while (pIter) {
30,424✔
373
      STqHandle* pHandle = *(STqHandle**)pIter;
15,325✔
374
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
15,325!
375

376
      if (pHandle->msg == NULL) {
15,325!
377
        tqError("pHandle->msg should not be null");
×
378
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
379
        break;
×
380
      } else {
381
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
15,325✔
382
                       .pCont = pHandle->msg->pCont,
15,325✔
383
                       .contLen = pHandle->msg->contLen,
15,325✔
384
                       .info = pHandle->msg->info};
15,325✔
385
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
15,325!
386
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
387
        }
388
        taosMemoryFree(pHandle->msg);
15,325!
389
        pHandle->msg = NULL;
15,325✔
390
      }
391

392
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
15,325✔
393
    }
394

395
    taosHashClear(pTq->pPushMgr);
15,099✔
396
  }
397
  taosWUnLockLatch(&pTq->lock);
15,099✔
398
  return 0;
15,099✔
399
}
400

401
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
48,438✔
402
  if (pTq == NULL || pMsg == NULL) {
48,438!
403
    return TSDB_CODE_INVALID_PARA;
×
404
  }
405
  SMqPollReq req = {0};
48,441✔
406
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
48,441✔
407
  if (code < 0) {
48,419!
408
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
409
    code = TSDB_CODE_INVALID_MSG;
×
410
    goto END;
×
411
  }
412
  if (req.rawData == 1){
48,419✔
413
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
90✔
414
    if (req.uidHash == NULL) {
120!
415
      tqError("tq poll rawData taosHashInit failed");
×
416
      code = terrno;
×
417
      goto END;
×
418
    }
419
  }
420
  int64_t      consumerId = req.consumerId;
48,449✔
421
  int32_t      reqEpoch = req.epoch;
48,449✔
422
  STqOffsetVal reqOffset = req.reqOffset;
48,449✔
423
  int32_t      vgId = TD_VID(pTq->pVnode);
48,449✔
424
  STqHandle*   pHandle = NULL;
48,449✔
425

426
  while (1) {
5✔
427
    taosWLockLatch(&pTq->lock);
48,454✔
428
    // 1. find handle
429
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
48,449✔
430
    if (code != TDB_CODE_SUCCESS) {
48,442✔
431
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
10!
432
      taosWUnLockLatch(&pTq->lock);
10✔
433
      return code;
10✔
434
    }
435

436
    // 2. check rebalance status
437
    if (pHandle->consumerId != consumerId) {
48,432!
438
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
×
439
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
440
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
441
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
442
      taosWUnLockLatch(&pTq->lock);
×
443
      goto END;
×
444
    }
445

446
    bool exec = tqIsHandleExec(pHandle);
48,432✔
447
    if (!exec) {
48,432✔
448
      tqSetHandleExec(pHandle);
48,431!
449
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
450
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
48,431!
451
              req.subKey, pHandle);
452
      taosWUnLockLatch(&pTq->lock);
48,438✔
453
      break;
48,443✔
454
    }
455
    taosWUnLockLatch(&pTq->lock);
1✔
456

457
    tqDebug("tmq poll: consumer:0x%" PRIx64
5!
458
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
459
            consumerId, vgId, req.subKey, pHandle);
460
    taosMsleep(10);
5✔
461
  }
462

463
  // 3. update the epoch value
464
  if (pHandle->epoch < reqEpoch) {
48,443✔
465
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
1,365!
466
            reqEpoch);
467
    pHandle->epoch = reqEpoch;
1,365✔
468
  }
469

470
  char buf[TSDB_OFFSET_LEN] = {0};
48,443✔
471
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
48,443✔
472
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
48,443!
473
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
474

475
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
48,443✔
476
  tqSetHandleIdle(pHandle);
48,442!
477

478
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
48,442!
479
          req.subKey, pHandle);
480

481
END:
×
482
  tDestroySMqPollReq(&req);
48,443✔
483
  return code;
48,442✔
484
}
485

486
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
2✔
487
  if (pTq == NULL || pMsg == NULL) {
2!
488
    return TSDB_CODE_INVALID_PARA;
×
489
  }
490
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
2✔
491
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
2✔
492

493
  SMqVgOffset vgOffset = {0};
2✔
494

495
  SDecoder decoder;
496
  tDecoderInit(&decoder, (uint8_t*)data, len);
2✔
497
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
2!
498
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
499
    return terrno;
×
500
  }
501

502
  tDecoderClear(&decoder);
2✔
503

504
  STqOffset* pSavedOffset = NULL;
2✔
505
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
2✔
506
  if (code != 0) {
2✔
507
    return TSDB_CODE_TMQ_NO_COMMITTED;
1✔
508
  }
509
  vgOffset.offset = *pSavedOffset;
1✔
510

511
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
1!
512
  if (code < 0) {
1!
513
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
514
  }
515

516
  void* buf = rpcMallocCont(len);
1✔
517
  if (buf == NULL) {
1!
518
    return terrno;
×
519
  }
520
  SEncoder encoder = {0};
1✔
521
  tEncoderInit(&encoder, buf, len);
1✔
522
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
1✔
523
  tEncoderClear(&encoder);
1✔
524
  if (code < 0) {
1!
525
    rpcFreeCont(buf);
×
526
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
527
  }
528

529
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
1✔
530

531
  tmsgSendRsp(&rsp);
1✔
532
  return 0;
1✔
533
}
534

535
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
11✔
536
  if (pTq == NULL || pMsg == NULL) {
11!
537
    return TSDB_CODE_INVALID_PARA;
×
538
  }
539
  int32_t    code = 0;
11✔
540
  SMqPollReq req = {0};
11✔
541
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
11!
542
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
543
    return TSDB_CODE_INVALID_MSG;
×
544
  }
545

546
  int64_t      consumerId = req.consumerId;
11✔
547
  STqOffsetVal reqOffset = req.reqOffset;
11✔
548
  int32_t      vgId = TD_VID(pTq->pVnode);
11✔
549

550
  // 1. find handle
551
  taosRLockLatch(&pTq->lock);
11✔
552
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
11✔
553
  if (pHandle == NULL) {
11!
554
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
555
    taosRUnLockLatch(&pTq->lock);
×
556
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
557
  }
558

559
  // 2. check rebalance status
560
  if (pHandle->consumerId != consumerId) {
11!
561
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
562
            consumerId, vgId, req.subKey, pHandle->consumerId);
563
    taosRUnLockLatch(&pTq->lock);
×
564
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
565
  }
566

567
  int64_t sver = 0, ever = 0;
11✔
568
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
11✔
569
  taosRUnLockLatch(&pTq->lock);
11✔
570

571
  SMqDataRsp dataRsp = {0};
11✔
572
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
11✔
573
  if (code != 0) {
11!
574
    return code;
×
575
  }
576

577
  if (req.useSnapshot == true) {
11!
578
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
579
    code = TSDB_CODE_INVALID_PARA;
×
580
    goto END;
×
581
  }
582

583
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
11✔
584

585
  if (reqOffset.type == TMQ_OFFSET__LOG) {
11✔
586
    dataRsp.rspOffset.version = reqOffset.version;
2✔
587
  } else if (reqOffset.type < 0) {
9!
588
    STqOffset* pOffset = NULL;
9✔
589
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
9✔
590
    if (code == 0) {
9✔
591
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
1!
592
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
593
        code = TSDB_CODE_INVALID_PARA;
×
594
        goto END;
×
595
      }
596

597
      dataRsp.rspOffset.version = pOffset->val.version;
1✔
598
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
1!
599
             req.subKey, dataRsp.rspOffset.version);
600
    } else {
601
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
8!
602
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
8✔
603
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
604
        dataRsp.rspOffset.version = ever;
×
605
      }
606
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
8!
607
             dataRsp.rspOffset.version);
608
    }
609
  } else {
610
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
611
            reqOffset.type);
612
    code = TSDB_CODE_INVALID_PARA;
×
613
    goto END;
×
614
  }
615

616
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
11✔
617

618
END:
11✔
619
  tDeleteMqDataRsp(&dataRsp);
11✔
620
  return code;
11✔
621
}
622

623
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
851✔
624
  if (pTq == NULL || msg == NULL) {
851!
625
    return TSDB_CODE_INVALID_PARA;
×
626
  }
627
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
853✔
628
  int32_t        vgId = TD_VID(pTq->pVnode);
853✔
629

630
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
853!
631
  int32_t code = 0;
853✔
632

633
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
853✔
634
  if (pHandle) {
854✔
635
    while (1) {
×
636
      taosWLockLatch(&pTq->lock);
852✔
637
      bool exec = tqIsHandleExec(pHandle);
852✔
638

639
      if (exec) {
852!
640
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
641
               pHandle->subKey, pHandle);
642
        taosWUnLockLatch(&pTq->lock);
×
643
        taosMsleep(10);
×
644
        continue;
×
645
      }
646
      tqUnregisterPushHandle(pTq, pHandle);
852✔
647
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
852✔
648
      if (code != 0) {
851!
649
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
650
      }
651
      taosWUnLockLatch(&pTq->lock);
851✔
652
      break;
852✔
653
    }
654
  }
655

656
  taosWLockLatch(&pTq->lock);
854✔
657
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
854✔
658
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
268!
659
  }
660
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
854✔
661
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
803!
662
  }
663

664
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
852!
665
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
666
  }
667
  taosWUnLockLatch(&pTq->lock);
854✔
668

669
  return 0;
854✔
670
}
671

672
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
119✔
673
  if (pTq == NULL || msg == NULL) {
119!
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  STqCheckInfo info = {0};
119✔
677
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
119✔
678
  if (code != 0) {
119!
679
    return code;
×
680
  }
681

682
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
119✔
683
  if (code != 0) {
119!
684
    tDeleteSTqCheckInfo(&info);
×
685
    return code;
×
686
  }
687

688
  return tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
119✔
689
}
690

691
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
10✔
692
  if (pTq == NULL || msg == NULL) {
10!
693
    return TSDB_CODE_INVALID_PARA;
×
694
  }
695
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
10!
696
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
697
  }
698
  return tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
10✔
699
}
700

701
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
3,017✔
702
  if (pTq == NULL || msg == NULL) {
3,017!
703
    return TSDB_CODE_INVALID_PARA;
×
704
  }
705
  int         ret = 0;
3,025✔
706
  SMqRebVgReq req = {0};
3,025✔
707
  SDecoder    dc = {0};
3,025✔
708

709
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
3,025✔
710
  ret = tDecodeSMqRebVgReq(&dc, &req);
3,018✔
711
  if (ret < 0) {
3,016!
712
    goto end;
×
713
  }
714

715
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
3,016!
716
         req.oldConsumerId, req.newConsumerId);
717

718
  taosRLockLatch(&pTq->lock);
3,025✔
719
  STqHandle* pHandle = NULL;
3,025✔
720
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
3,025✔
721
  if (code != 0){
3,022✔
722
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
1,319!
723
  }
724
  taosRUnLockLatch(&pTq->lock);
3,023✔
725
  if (pHandle == NULL) {
3,025✔
726
    if (req.oldConsumerId != -1) {
1,322!
727
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
728
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
729
    }
730
    if (req.newConsumerId == -1) {
1,322!
731
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
732
      ret = TSDB_CODE_INVALID_PARA;
×
733
      goto end;
×
734
    }
735
    STqHandle handle = {0};
1,322✔
736
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,322✔
737
    if (ret < 0) {
1,322!
738
      tqDestroyTqHandle(&handle);
×
739
      goto end;
×
740
    }
741
    taosWLockLatch(&pTq->lock);
1,322✔
742
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,322✔
743
    taosWUnLockLatch(&pTq->lock);
1,322✔
744
  } else {
745
    while (1) {
×
746
      taosWLockLatch(&pTq->lock);
1,703✔
747
      bool exec = tqIsHandleExec(pHandle);
1,703!
748
      if (exec) {
1,703!
749
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
×
750
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
751
        taosWUnLockLatch(&pTq->lock);
×
752
        taosMsleep(10);
×
753
        continue;
×
754
      }
755
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
1,703✔
756
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
162!
757
      } else {
758
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,541!
759
               req.newConsumerId);
760

761
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
1,541✔
762
        atomic_store_32(&pHandle->epoch, 0);
1,541✔
763
        tqUnregisterPushHandle(pTq, pHandle);
1,541✔
764
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
1,541✔
765
      }
766
      taosWUnLockLatch(&pTq->lock);
1,703✔
767
      break;
1,703✔
768
    }
769
  }
770

771
end:
3,025✔
772
  tDecoderClear(&dc);
3,025✔
773
  return ret;
3,025✔
774
}
775

776
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
10,353!
777

778
int32_t tqBuildStreamTask(void* pTqObj, SStreamTask* pTask, int64_t nextProcessVer) {
12,193✔
779
  STQ*             pTq = (STQ*)pTqObj;
12,193✔
780
  int32_t          vgId = TD_VID(pTq->pVnode);
12,193✔
781
  SCheckpointInfo* pChkInfo = NULL;
12,193✔
782

783
  tqDebug("s-task:0x%x start to build task", pTask->id.taskId);
12,193✔
784

785
  int32_t code = streamTaskInit(pTask, pTq->pStreamMeta, &pTq->pVnode->msgCb, nextProcessVer);
12,193✔
786
  if (code != TSDB_CODE_SUCCESS) {
12,202!
787
    return code;
×
788
  }
789

790
  pTask->pBackend = NULL;
12,202✔
791

792
  // sink
793
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
12,202✔
794
  if (pOutputInfo->type == TASK_OUTPUT__SMA) {
12,202!
795
    pOutputInfo->smaSink.vnode = pTq->pVnode;
×
796
    pOutputInfo->smaSink.smaSink = smaHandleRes;
×
797
  } else if (pOutputInfo->type == TASK_OUTPUT__TABLE) {
12,202✔
798
    pOutputInfo->tbSink.vnode = pTq->pVnode;
6,100✔
799
    pOutputInfo->tbSink.tbSinkFunc = tqSinkDataIntoDstTable;
6,100✔
800

801
    int32_t   ver1 = 1;
6,100✔
802
    SMetaInfo info = {0};
6,100✔
803
    code = metaGetInfo(pTq->pVnode->pMeta, pOutputInfo->tbSink.stbUid, &info, NULL);
6,100✔
804
    if (code == TSDB_CODE_SUCCESS) {
6,088✔
805
      ver1 = info.skmVer;
5,687✔
806
    }
807

808
    SSchemaWrapper* pschemaWrapper = pOutputInfo->tbSink.pSchemaWrapper;
6,088✔
809
    pOutputInfo->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
6,088✔
810
    if (pOutputInfo->tbSink.pTSchema == NULL) {
6,097!
811
      return terrno;
×
812
    }
813

814
    pOutputInfo->tbSink.pTbInfo = tSimpleHashInit(10240, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,097✔
815
    if (pOutputInfo->tbSink.pTbInfo == NULL) {
6,101!
816
      tqError("vgId:%d failed init sink tableInfo, code:%s", vgId, tstrerror(terrno));
×
817
      return terrno;
×
818
    }
819

820
    tSimpleHashSetFreeFp(pOutputInfo->tbSink.pTbInfo, freePtr);
6,101✔
821
  }
822

823
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
12,203✔
824
    bool scanDropCtb = pTask->subtableWithoutMd5 ? true : false;
6,194✔
825
    SWalFilterCond cond = {.deleteMsg = 1, .scanDropCtb = scanDropCtb};  // delete msg also extract from wal files
6,194✔
826
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, &cond, pTask->id.taskId);
6,194✔
827
    if (pTask->exec.pWalReader == NULL) {
6,195✔
828
      tqError("vgId:%d failed init wal reader, code:%s", vgId, tstrerror(terrno));
1!
829
      return terrno;
1✔
830
    }
831
  }
832

833
  streamTaskResetUpstreamStageInfo(pTask);
12,203✔
834

835
  pChkInfo = &pTask->chkInfo;
12,205✔
836
  tqSetRestoreVersionInfo(pTask);
12,205✔
837

838
  char*       p = streamTaskGetStatus(pTask).name;
12,204✔
839
  const char* pNext = streamTaskGetStatusStr(pTask->status.taskStatus);
12,202✔
840

841
  if (pTask->info.fillHistory) {
12,204✔
842
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
3,721!
843
           " nextProcessVer:%" PRId64
844
           " child id:%d, level:%d, cur-status:%s, next-status:%s taskType:%d, related stream task:0x%x "
845
           "delaySched:%" PRId64 " ms, inputVer:%" PRId64,
846
           vgId, pTask->id.idStr, pTask, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
847
           pTask->info.selfChildId, pTask->info.taskLevel, p, pNext, pTask->info.fillHistory,
848
           (int32_t)pTask->streamTaskId.taskId, pTask->info.delaySchedParam, nextProcessVer);
849
  } else {
850
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
8,483!
851
           " nextProcessVer:%" PRId64
852
           " child id:%d, level:%d, cur-status:%s next-status:%s taskType:%d, related helper-task:0x%x "
853
           "delaySched:%" PRId64 " ms, inputVer:%" PRId64,
854
           vgId, pTask->id.idStr, pTask, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
855
           pTask->info.selfChildId, pTask->info.taskLevel, p, pNext, pTask->info.fillHistory,
856
           (int32_t)pTask->hTaskInfo.id.taskId, pTask->info.delaySchedParam, nextProcessVer);
857

858
    if (pChkInfo->checkpointVer > pChkInfo->nextProcessVer) {
8,489!
859
      tqError("vgId:%d build stream task, s-task:%s, checkpointVer:%" PRId64 " > nextProcessVer:%" PRId64, vgId,
×
860
              pTask->id.idStr, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
861
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
862
    }
863
  }
864

865
  return 0;
12,210✔
866
}
867

868
int32_t tqProcessTaskCheckReq(STQ* pTq, SRpcMsg* pMsg) { return tqStreamTaskProcessCheckReq(pTq->pStreamMeta, pMsg); }
22,262✔
869

870
int32_t tqProcessTaskCheckRsp(STQ* pTq, SRpcMsg* pMsg) {
22,667✔
871
  return tqStreamTaskProcessCheckRsp(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
22,667✔
872
}
873

874
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
11,711✔
875
  return tqStreamTaskProcessDeployReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, sversion, msg, msgLen,
11,725✔
876
                                      vnodeIsRoleLeader(pTq->pVnode), pTq->pVnode->restored);
11,711✔
877
}
878

879
static void doStartFillhistoryStep2(SStreamTask* pTask, SStreamTask* pStreamTask, STQ* pTq) {
1,786✔
880
  const char*    id = pTask->id.idStr;
1,786✔
881
  int64_t        nextProcessedVer = pStreamTask->hTaskInfo.haltVer;
1,786✔
882
  SVersionRange* pStep2Range = &pTask->step2Range;
1,786✔
883
  int32_t        vgId = pTask->pMeta->vgId;
1,786✔
884

885
  // if it's an source task, extract the last version in wal.
886
  bool done = streamHistoryTaskSetVerRangeStep2(pTask, nextProcessedVer);
1,786✔
887
  pTask->execInfo.step2Start = taosGetTimestampMs();
1,786✔
888

889
  if (done) {
1,786✔
890
    qDebug("s-task:%s scan wal(step 2) verRange:%" PRId64 "-%" PRId64 " ended, elapsed time:%.2fs", id,
899✔
891
           pStep2Range->minVer, pStep2Range->maxVer, 0.0);
892
    int32_t code = streamTaskPutTranstateIntoInputQ(pTask);  // todo: msg lost.
899✔
893
    if (code) {
899!
894
      qError("s-task:%s failed put trans-state into inputQ, code:%s", id, tstrerror(code));
×
895
    }
896
    (void)streamExecTask(pTask);  // exec directly
899✔
897
  } else {
898
    STimeWindow* pWindow = &pTask->dataRange.window;
887✔
899
    tqDebug("s-task:%s level:%d verRange:%" PRId64 "-%" PRId64 " window:%" PRId64 "-%" PRId64
887✔
900
            ", do secondary scan-history from WAL after halt the related stream task:%s",
901
            id, pTask->info.taskLevel, pStep2Range->minVer, pStep2Range->maxVer, pWindow->skey, pWindow->ekey,
902
            pStreamTask->id.idStr);
903
    if (pTask->status.schedStatus != TASK_SCHED_STATUS__WAITING) {
887!
904
      tqError("s-task:%s level:%d unexpected sched-status:%d", id, pTask->info.taskLevel, pTask->status.schedStatus);
×
905
    }
906

907
    int32_t code = streamSetParamForStreamScannerStep2(pTask, pStep2Range, pWindow);
887✔
908
    if (code) {
886!
909
      tqError("s-task:%s level:%d failed to set step2 param", id, pTask->info.taskLevel);
×
910
    }
911

912
    int64_t dstVer = pStep2Range->minVer;
886✔
913
    pTask->chkInfo.nextProcessVer = dstVer;
886✔
914

915
    walReaderSetSkipToVersion(pTask->exec.pWalReader, dstVer);
886✔
916
    tqDebug("s-task:%s wal reader start scan WAL verRange:%" PRId64 "-%" PRId64 ", set sched-status:%d", id, dstVer,
887✔
917
            pStep2Range->maxVer, TASK_SCHED_STATUS__INACTIVE);
918

919
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
887✔
920

921
    // now the fill-history task starts to scan data from wal files.
922
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_SCANHIST_DONE);
887✔
923
  }
924
}
1,786✔
925

926
int32_t handleStep2Async(SStreamTask* pStreamTask, void* param) {
1,786✔
927
  STQ* pTq = param;
1,786✔
928

929
  SStreamMeta* pMeta = pStreamTask->pMeta;
1,786✔
930
  STaskId      hId = pStreamTask->hTaskInfo.id;
1,786✔
931
  SStreamTask* pTask = NULL;
1,786✔
932
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
1,786✔
933
  if (pTask == NULL) {
1,786!
934
    tqWarn("s-task:0x%x failed to acquired it to exec step 2, scan wal quit", (int32_t)hId.taskId);
×
935
    return TSDB_CODE_SUCCESS;
×
936
  }
937

938
  doStartFillhistoryStep2(pTask, pStreamTask, pTq);
1,786✔
939

940
  streamMetaReleaseTask(pMeta, pTask);
1,786✔
941
  return TSDB_CODE_SUCCESS;
1,786✔
942
}
943

944
// this function should be executed by only one thread, so we set a sentinel to protect this function
945
int32_t tqProcessTaskScanHistory(STQ* pTq, SRpcMsg* pMsg) {
1,882✔
946
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
1,882✔
947
  SStreamMeta*           pMeta = pTq->pStreamMeta;
1,882✔
948
  int32_t                code = TSDB_CODE_SUCCESS;
1,882✔
949
  SStreamTask*           pTask = NULL;
1,882✔
950
  SStreamTask*           pStreamTask = NULL;
1,882✔
951
  char*                  pStatus = NULL;
1,882✔
952
  int32_t                taskType = 0;
1,882✔
953

954
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,882✔
955
  if (pTask == NULL) {
1,882✔
956
    tqError("vgId:%d failed to acquire stream task:0x%x during scan history data, task may have been destroyed",
1!
957
            pMeta->vgId, pReq->taskId);
958
    return code;
1✔
959
  }
960

961
  // do recovery step1
962
  const char* id = pTask->id.idStr;
1,881✔
963
  streamMutexLock(&pTask->lock);
1,881✔
964

965
  SStreamTaskState s = streamTaskGetStatus(pTask);
1,881✔
966
  pStatus = s.name;
1,881✔
967
  taskType = pTask->info.fillHistory;
1,881✔
968

969
  if ((s.state != TASK_STATUS__SCAN_HISTORY && taskType == STREAM_HISTORY_TASK) ||
1,881!
970
      (s.state != TASK_STATUS__READY && taskType == STREAM_RECALCUL_TASK) ||
1,881!
971
      (pTask->status.downstreamReady == 0)) {
1,881!
972
    tqError("s-task:%s vgId:%d status:%s downstreamReady:%d not allowed/ready for scan-history data, quit", id,
×
973
            pMeta->vgId, s.name, pTask->status.downstreamReady);
974

975
    streamMutexUnlock(&pTask->lock);
×
976
    streamMetaReleaseTask(pMeta, pTask);
×
977
    return 0;
×
978
  }
979

980
  if (pTask->exec.pExecutor == NULL) {
1,881!
981
    tqError("s-task:%s vgId:%d executor is null, not executor scan history", id, pMeta->vgId);
×
982

983
    streamMutexUnlock(&pTask->lock);
×
984
    streamMetaReleaseTask(pMeta, pTask);
×
985
    return 0;
×
986
  }
987

988
  streamMutexUnlock(&pTask->lock);
1,881✔
989

990
  // avoid multi-thread exec
991
  while (1) {
×
992
    int32_t sentinel = atomic_val_compare_exchange_32(&pTask->status.inScanHistorySentinel, 0, 1);
1,881✔
993
    if (sentinel != 0) {
1,881!
994
      tqDebug("s-task:%s already in scan-history func, wait for 100ms, and try again", id);
×
995
      taosMsleep(100);
×
996
    } else {
997
      break;
1,881✔
998
    }
999
  }
1000

1001
  // let's decide which step should be executed now
1002
  if (pTask->execInfo.step1Start == 0) {
1,881✔
1003
    int64_t ts = taosGetTimestampMs();
1,788✔
1004
    pTask->execInfo.step1Start = ts;
1,788✔
1005
    tqDebug("s-task:%s start scan-history stage(step 1), status:%s, step1 startTs:%" PRId64, id, pStatus, ts);
1,788✔
1006
  } else {
1007
    if (pTask->execInfo.step2Start == 0) {
93!
1008
      tqDebug("s-task:%s continue exec scan-history(step1), original step1 startTs:%" PRId64 ", already elapsed:%.2fs",
93!
1009
              id, pTask->execInfo.step1Start, pTask->execInfo.step1El);
1010
    } else {
1011
      tqDebug("s-task:%s already in step2, no need to scan-history data, step2 startTs:%" PRId64, id,
×
1012
              pTask->execInfo.step2Start);
1013

1014
      atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1015
      streamMetaReleaseTask(pMeta, pTask);
×
1016
      return 0;
×
1017
    }
1018
  }
1019

1020
  // we have to continue retrying to successfully execute the scan history task.
1021
  if (!streamTaskSetSchedStatusWait(pTask)) {
1,881!
1022
    tqError(
×
1023
        "s-task:%s failed to start scan-history in first stream time window since already started, unexpected "
1024
        "sched-status:%d",
1025
        id, pTask->status.schedStatus);
1026
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1027
    streamMetaReleaseTask(pMeta, pTask);
×
1028
    return 0;
×
1029
  }
1030

1031
  int64_t              st = taosGetTimestampMs();
1,881✔
1032
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
1,881✔
1033

1034
  double el = (taosGetTimestampMs() - st) / 1000.0;
1,881✔
1035
  pTask->execInfo.step1El += el;
1,881✔
1036

1037
  if (retInfo.ret == TASK_SCANHISTORY_QUIT || retInfo.ret == TASK_SCANHISTORY_REXEC) {
1,881✔
1038
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
95✔
1039
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
95✔
1040

1041
    if (retInfo.ret == TASK_SCANHISTORY_REXEC) {
95✔
1042
      streamExecScanHistoryInFuture(pTask, retInfo.idleTime);
93✔
1043
    } else {
1044
      SStreamTaskState p = streamTaskGetStatus(pTask);
2✔
1045
      ETaskStatus      localStatus = p.state;
2✔
1046

1047
      if (localStatus == TASK_STATUS__PAUSE) {
2!
1048
        tqDebug("s-task:%s is paused in the step1, elapsed time:%.2fs total:%.2fs, sched-status:%d", id, el,
×
1049
                pTask->execInfo.step1El, status);
1050
      } else if (localStatus == TASK_STATUS__STOP || localStatus == TASK_STATUS__DROPPING) {
2!
1051
        tqDebug("s-task:%s status:%p not continue scan-history data, total elapsed time:%.2fs quit", id, p.name,
2!
1052
                pTask->execInfo.step1El);
1053
      }
1054
    }
1055

1056
    streamMetaReleaseTask(pMeta, pTask);
95✔
1057
    return 0;
95✔
1058
  }
1059

1060
  // the following procedure should be executed, no matter status is stop/pause or not
1061
  if (taskType == STREAM_HISTORY_TASK) {
1,786!
1062
    tqDebug("s-task:%s scan-history(step 1) ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
1,786✔
1063
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1064
    tqDebug("s-task:%s recalculate ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
×
1065
  } else {
1066
    tqError("s-task:%s fill-history is disabled, unexpected", id);
×
1067
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1068
  }
1069

1070
  // 1. get the related stream task
1071
  code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,786✔
1072
  if (pStreamTask == NULL) {
1,786!
1073

1074
    int32_t ret = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
1075
    if (ret == 0 && pStreamTask != NULL) {
×
1076
      tqWarn("s-task:0x%" PRIx64 " stopped, not ready for related task:%s scan-history work, do nothing",
×
1077
             pTask->streamTaskId.taskId, pTask->id.idStr);
1078
      streamMetaReleaseTask(pMeta, pStreamTask);
×
1079
    } else {
1080
      tqError("failed to find s-task:0x%" PRIx64 ", it may have been destroyed, drop related fill-history task:%s",
×
1081
              pTask->streamTaskId.taskId, pTask->id.idStr);
1082

1083
      tqDebug("s-task:%s fill-history task set status to be dropping", id);
×
1084
      code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
×
1085
    }
1086

1087
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1088
    streamMetaReleaseTask(pMeta, pTask);
×
1089
    return code;
×
1090
  }
1091

1092
  if (pStreamTask->info.taskLevel != TASK_LEVEL__SOURCE) {
1,786!
1093
    tqError("s-task:%s fill-history task related stream task level:%d, unexpected", id, pStreamTask->info.taskLevel);
×
1094
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1095
  }
1096

1097
  if (taskType == STREAM_HISTORY_TASK) {
1,786!
1098
    code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
1,786✔
1099
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1100
    // send recalculate end block
1101
    code = streamCreateAddRecalculateEndBlock(pStreamTask);
×
1102
    if (code) {
×
1103
      tqError("s-task:%s failed to create-add recalculate end block, code:%s", id, tstrerror(code));
×
1104
    }
1105
    streamTaskSetSchedStatusInactive(pTask);
×
1106
  }
1107

1108
  streamMetaReleaseTask(pMeta, pStreamTask);
1,786✔
1109

1110
  atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
1,786✔
1111
  streamMetaReleaseTask(pMeta, pTask);
1,786✔
1112
  return code;
1,786✔
1113
}
1114

1115
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
151,189✔
1116
  int32_t  code = 0;
151,189✔
1117
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
151,189✔
1118
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
151,189✔
1119
  SDecoder decoder;
1120

1121
  SStreamTaskRunReq req = {0};
151,189✔
1122
  tDecoderInit(&decoder, (uint8_t*)msg, len);
151,189✔
1123
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
151,274!
1124
    tqError("vgId:%d failed to decode task run req, code:%s", pTq->pStreamMeta->vgId, tstrerror(code));
×
1125
    tDecoderClear(&decoder);
×
1126
    return TSDB_CODE_SUCCESS;
×
1127
  }
1128

1129
  tDecoderClear(&decoder);
151,069✔
1130

1131
  // extracted submit data from wal files for all tasks
1132
  if (req.reqType == STREAM_EXEC_T_EXTRACT_WAL_DATA) {
150,881✔
1133
    return tqScanWal(pTq);
82,663✔
1134
  } else {
1135
    code = tqStreamTaskProcessRunReq(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
68,218✔
1136
    if (code) {
68,306✔
1137
      tqError("vgId:%d failed to create task run req, code:%s", TD_VID(pTq->pVnode), tstrerror(code));
21!
1138
    }
1139

1140
    return code;
68,300✔
1141
  }
1142
}
1143

1144
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
22,446✔
1145
  return tqStreamTaskProcessDispatchReq(pTq->pStreamMeta, pMsg);
22,446✔
1146
}
1147

1148
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
22,763✔
1149
  return tqStreamTaskProcessDispatchRsp(pTq->pStreamMeta, pMsg);
22,763✔
1150
}
1151

1152
int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
5,980✔
1153
  return tqStreamTaskProcessDropReq(pTq->pStreamMeta, msg, msgLen);
5,980✔
1154
}
1155

1156
int32_t tqProcessTaskUpdateCheckpointReq(STQ* pTq, char* msg, int32_t msgLen) {
2,267✔
1157
  return tqStreamTaskProcessUpdateCheckpointReq(pTq->pStreamMeta, pTq->pVnode->restored, msg);
2,267✔
1158
}
1159

1160
int32_t tqProcessTaskConsenChkptIdReq(STQ* pTq, SRpcMsg* pMsg) {
97✔
1161
  return tqStreamTaskProcessConsenChkptIdReq(pTq->pStreamMeta, pMsg);
97✔
1162
}
1163

1164
int32_t tqProcessTaskPauseReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
729✔
1165
  return tqStreamTaskProcessTaskPauseReq(pTq->pStreamMeta, msg);
729✔
1166
}
1167

1168
int32_t tqProcessTaskResumeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,220✔
1169
  return tqStreamTaskProcessTaskResumeReq(pTq, sversion, msg, true);
1,220✔
1170
}
1171

1172
int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
522✔
1173
  return tqStreamTaskProcessRetrieveReq(pTq->pStreamMeta, pMsg);
522✔
1174
}
1175

1176
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
402✔
1177

1178
int32_t tqStreamProgressRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
×
1179
  char*               msgStr = pMsg->pCont;
×
1180
  char*               msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
1181
  int32_t             msgLen = pMsg->contLen - sizeof(SMsgHead);
×
1182
  int32_t             code = 0;
×
1183
  SStreamProgressReq  req;
1184
  char*               pRspBuf = taosMemoryCalloc(1, sizeof(SMsgHead) + sizeof(SStreamProgressRsp));
×
1185
  SStreamProgressRsp* pRsp = POINTER_SHIFT(pRspBuf, sizeof(SMsgHead));
×
1186
  if (!pRspBuf) {
×
1187
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1188
    code = -1;
×
1189
    goto _OVER;
×
1190
  }
1191

1192
  code = tDeserializeStreamProgressReq(msgBody, msgLen, &req);
×
1193
  if (code == TSDB_CODE_SUCCESS) {
×
1194
    code = tqGetStreamExecInfo(pTq->pVnode, req.streamId, &pRsp->progressDelay, &pRsp->fillHisFinished);
×
1195
  }
1196
  if (code == TSDB_CODE_SUCCESS) {
×
1197
    pRsp->fetchIdx = req.fetchIdx;
×
1198
    pRsp->subFetchIdx = req.subFetchIdx;
×
1199
    pRsp->vgId = req.vgId;
×
1200
    pRsp->streamId = req.streamId;
×
1201
    code = tSerializeStreamProgressRsp(pRsp, sizeof(SStreamProgressRsp) + sizeof(SMsgHead), pRsp);
×
1202
    if (code) {
×
1203
      goto _OVER;
×
1204
    }
1205

1206
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
1207
    rsp.pCont = pRspBuf;
×
1208
    pRspBuf = NULL;
×
1209
    rsp.contLen = sizeof(SMsgHead) + sizeof(SStreamProgressRsp);
×
1210
    tmsgSendRsp(&rsp);
×
1211
  }
1212

1213
_OVER:
×
1214
  if (pRspBuf) {
×
1215
    taosMemoryFree(pRspBuf);
×
1216
  }
1217
  return code;
×
1218
}
1219

1220
// always return success to mnode
1221
//todo: handle failure of build and send msg to mnode
1222
static void doSendChkptSourceRsp(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo, int32_t code,
16✔
1223
                                 int32_t taskId) {
1224
  SRpcMsg rsp = {0};
16✔
1225
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
16✔
1226
  if (ret) {  // suppress the error in build checkpoint source rsp
16!
1227
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1228
  }
1229
  tmsgSendRsp(&rsp);  // error occurs
16✔
1230
}
16✔
1231

1232
// no matter what kinds of error happened, make sure the mnode will receive the success execution code.
1233
int32_t tqProcessTaskCheckPointSourceReq(STQ* pTq, SRpcMsg* pMsg, SRpcMsg* pRsp) {
1,580✔
1234
  int32_t                    vgId = TD_VID(pTq->pVnode);
1,580✔
1235
  SStreamMeta*               pMeta = pTq->pStreamMeta;
1,580✔
1236
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,580✔
1237
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
1,580✔
1238
  int32_t                    code = 0;
1,580✔
1239
  SStreamCheckpointSourceReq req = {0};
1,580✔
1240
  SDecoder                   decoder = {0};
1,580✔
1241
  SStreamTask*               pTask = NULL;
1,580✔
1242
  int64_t                    checkpointId = 0;
1,580✔
1243

1244
  // disable auto rsp to mnode
1245
  pRsp->info.handle = NULL;
1,580✔
1246

1247
  tDecoderInit(&decoder, (uint8_t*)msg, len);
1,580✔
1248
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
1,580!
1249
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
1250
    tDecoderClear(&decoder);
×
1251
    tqError("vgId:%d failed to decode checkpoint-source msg, code:%s", vgId, tstrerror(code));
×
1252
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1253
    return TSDB_CODE_SUCCESS;  // always return success to mnode,
×
1254
  }
1255

1256
  tDecoderClear(&decoder);
1,576✔
1257

1258
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
1,581✔
1259
    tqDebug("vgId:%d not leader, ignore checkpoint-source msg, checkpontId:%" PRId64 ", s-task:0x%x", vgId,
9!
1260
            req.checkpointId, req.taskId);
1261
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
9✔
1262
    return TSDB_CODE_SUCCESS;  // always return success to mnode
9✔
1263
  }
1264

1265
  if (!pTq->pVnode->restored) {
1,571✔
1266
    tqDebug("vgId:%d checkpoint-source msg received during restoring, checkpointId:%" PRId64
1!
1267
            ", transId:%d s-task:0x%x ignore it",
1268
            vgId, req.checkpointId, req.transId, req.taskId);
1269
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
1✔
1270
    return TSDB_CODE_SUCCESS;  // always return success to mnode
1✔
1271
  }
1272

1273
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
1,570✔
1274
  if (pTask == NULL || code != 0) {
1,570✔
1275
    tqError("vgId:%d failed to find s-task:0x%x, ignore checkpoint msg. checkpointId:%" PRId64
3!
1276
            " transId:%d it may have been destroyed or stopped",
1277
            vgId, req.taskId, req.checkpointId, req.transId);
1278
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
3✔
1279
    return TSDB_CODE_SUCCESS;
×
1280
  }
1281

1282
  if (pTask->status.downstreamReady != 1) {
1,567!
1283
    // record the latest failed checkpoint id
1284
    streamTaskSetFailedChkptInfo(pTask, req.transId, req.checkpointId);
×
1285
    tqError("s-task:%s not ready for checkpoint, since downstream not ready, ignore this checkpointId:%" PRId64
×
1286
            ", transId:%d set it failed",
1287
            pTask->id.idStr, req.checkpointId, req.transId);
1288

1289
    streamMetaReleaseTask(pMeta, pTask);
×
1290
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1291
    return TSDB_CODE_SUCCESS;  // todo retry handle error
×
1292
  }
1293

1294
  // todo save the checkpoint failed info
1295
  streamMutexLock(&pTask->lock);
1,567✔
1296
  ETaskStatus status = streamTaskGetStatus(pTask).state;
1,575✔
1297

1298
  if (req.mndTrigger == 1) {
1,569✔
1299
    if (status == TASK_STATUS__HALT || status == TASK_STATUS__PAUSE) {
47!
1300
      tqError("s-task:%s not ready for checkpoint, since it is halt, ignore checkpointId:%" PRId64 ", set it failure",
6!
1301
              pTask->id.idStr, req.checkpointId);
1302

1303
      streamMutexUnlock(&pTask->lock);
6✔
1304
      streamMetaReleaseTask(pMeta, pTask);
6✔
1305
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
6✔
1306
      return TSDB_CODE_SUCCESS;
6✔
1307
    }
1308
  } else {
1309
    if (status != TASK_STATUS__HALT) {
1,522!
1310
      tqError("s-task:%s should in halt status, let's halt it directly", pTask->id.idStr);
×
1311
      //      streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_HALT);
1312
    }
1313
  }
1314

1315
  // check if the checkpoint msg already sent or not.
1316
  if (status == TASK_STATUS__CK) {
1,561!
1317
    streamTaskGetActiveCheckpointInfo(pTask, NULL, &checkpointId);
×
1318

1319
    tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1320
           " transId:%d already handled, ignore msg and continue process checkpoint",
1321
           pTask->id.idStr, checkpointId, req.transId);
1322

1323
    streamMutexUnlock(&pTask->lock);
×
1324
    streamMetaReleaseTask(pMeta, pTask);
×
1325
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SYN_PROPOSE_NOT_READY, req.taskId);
×
1326
    return TSDB_CODE_SUCCESS;
×
1327
  } else {  // checkpoint already finished, and not in checkpoint status
1328
    if (req.checkpointId <= pTask->chkInfo.checkpointId) {
1,561!
1329
      tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1330
             " transId:%d already handled, return success",
1331
             pTask->id.idStr, req.checkpointId, req.transId);
1332

1333
      streamMutexUnlock(&pTask->lock);
×
1334
      streamMetaReleaseTask(pMeta, pTask);
×
1335
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1336
      return TSDB_CODE_SUCCESS;
×
1337
    }
1338
  }
1339

1340
  code = streamProcessCheckpointSourceReq(pTask, &req);
1,561✔
1341
  streamMutexUnlock(&pTask->lock);
1,564✔
1342

1343
  if (code) {
1,561!
1344
    qError("s-task:%s (vgId:%d) failed to process checkpoint-source req, code:%s", pTask->id.idStr, vgId,
×
1345
           tstrerror(code));
1346
    streamMetaReleaseTask(pMeta, pTask);
×
1347
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1348
    return TSDB_CODE_SUCCESS;
×
1349
  }
1350

1351
  if (req.mndTrigger) {
1,561✔
1352
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64 ", transId:%d, ",
41!
1353
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId);
1354
  } else {
1355
    const char* pPrevStatus = streamTaskGetStatusStr(streamTaskGetPrevStatus(pTask));
1,520✔
1356
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64
1,525!
1357
           ", transId:%d after transfer-state, prev status:%s",
1358
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId, pPrevStatus);
1359
  }
1360

1361
  code = streamAddCheckpointSourceRspMsg(&req, &pMsg->info, pTask);
1,568✔
1362
  if (code != TSDB_CODE_SUCCESS) {
1,570!
1363
    streamTaskSetCheckpointFailed(pTask);  // set the checkpoint failed
×
1364
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1365
  }
1366

1367
  streamMetaReleaseTask(pMeta, pTask);
1,570✔
1368
  return TSDB_CODE_SUCCESS;
1,571✔
1369
}
1370

1371
// downstream task has complete the stream task checkpoint procedure, let's start the handle the rsp by execute task
1372
int32_t tqProcessTaskCheckpointReadyMsg(STQ* pTq, SRpcMsg* pMsg) {
4,388✔
1373
  int32_t vgId = TD_VID(pTq->pVnode);
4,388✔
1374

1375
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
4,388!
1376
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1377
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1378
    int32_t  code = 0;
×
1379
    SDecoder decoder;
1380

1381
    SStreamCheckpointReadyMsg req = {0};
×
1382
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1383
    if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
×
1384
      code = TSDB_CODE_MSG_DECODE_ERROR;
×
1385
      tDecoderClear(&decoder);
×
1386
      return code;
×
1387
    }
1388
    tDecoderClear(&decoder);
×
1389

1390
    tqError("vgId:%d not leader, s-task:0x%x ignore the retrieve checkpoint-trigger msg from s-task:0x%x vgId:%d", vgId,
×
1391
            req.upstreamTaskId, req.downstreamTaskId, req.downstreamNodeId);
1392

1393
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1394
  }
1395

1396
  return tqStreamTaskProcessCheckpointReadyMsg(pTq->pStreamMeta, pMsg);
4,389✔
1397
}
1398

1399
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
70✔
1400
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg,
140✔
1401
                                      pTq->pVnode->restored, (pTq->pStreamMeta->role == NODE_ROLE_LEADER));
70✔
1402
}
1403

1404
int32_t tqProcessTaskResetReq(STQ* pTq, SRpcMsg* pMsg) {
×
1405
  return tqStreamTaskProcessTaskResetReq(pTq->pStreamMeta, pMsg->pCont);
×
1406
}
1407

1408
int32_t tqProcessAllTaskStopReq(STQ* pTq, SRpcMsg* pMsg) {
4,259✔
1409
  return tqStreamTaskProcessAllTaskStopReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg);
4,259✔
1410
}
1411

1412
int32_t tqProcessTaskRetrieveTriggerReq(STQ* pTq, SRpcMsg* pMsg) {
×
1413
  int32_t vgId = TD_VID(pTq->pVnode);
×
1414

1415
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1416
    SRetrieveChkptTriggerReq req = {0};
×
1417

1418
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1419
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1420
    SDecoder decoder = {0};
×
1421

1422
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1423
    if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1424
      tDecoderClear(&decoder);
×
1425
      tqError("vgId:%d invalid retrieve checkpoint-trigger req received", vgId);
×
1426
      return TSDB_CODE_INVALID_MSG;
×
1427
    }
1428
    tDecoderClear(&decoder);
×
1429

1430
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from s-task:0x%" PRId64, vgId,
×
1431
            req.downstreamTaskId);
1432
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1433
  }
1434

1435
  return tqStreamTaskProcessRetrieveTriggerReq(pTq->pStreamMeta, pMsg);
×
1436
}
1437

1438
int32_t tqProcessTaskRetrieveTriggerRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1439
  return tqStreamTaskProcessRetrieveTriggerRsp(pTq->pStreamMeta, pMsg);
×
1440
}
1441

1442
// this function is needed, do not try to remove it.
1443
int32_t tqProcessStreamHbRsp(STQ* pTq, SRpcMsg* pMsg) { return tqStreamProcessStreamHbRsp(pTq->pStreamMeta, pMsg); }
8,005✔
1444

1445
int32_t tqProcessStreamReqCheckpointRsp(STQ* pTq, SRpcMsg* pMsg) {
3,144✔
1446
  return tqStreamProcessReqCheckpointRsp(pTq->pStreamMeta, pMsg);
3,144✔
1447
}
1448

1449
int32_t tqProcessTaskCheckpointReadyRsp(STQ* pTq, SRpcMsg* pMsg) {
4,392✔
1450
  return tqStreamProcessCheckpointReadyRsp(pTq->pStreamMeta, pMsg);
4,392✔
1451
}
1452

1453
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
3,068✔
1454
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
3,068✔
1455
}
1456

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