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

taosdata / TDengine / #3616

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

push

travis-ci

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

fix:[TS-5928]add consumer parameters

148228 of 300186 branches covered (49.38%)

Branch coverage included in aggregate %.

232358 of 300909 relevant lines covered (77.22%)

17990742.15 hits per line

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

64.49
/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; }
71,341!
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
68,755✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
68,771✔
31

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

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

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
10,176✔
64
  if (pLeft == NULL || pRight == NULL) {
10,176!
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
20,037✔
68
         pLeft->val.version == pRight->val.version;
9,861✔
69
}
70

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

80
  pVnode->pTq = pTq;
12,043✔
81
  pTq->pVnode = pVnode;
12,043✔
82

83
  pTq->path = taosStrdup(path);
12,043!
84
  if (pTq->path == NULL) {
12,046!
85
    return terrno;
×
86
  }
87

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

94
  taosInitRWLatch(&pTq->lock);
12,043✔
95

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

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

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

113
  return tqInitialize(pTq);
12,043✔
114
}
115

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

127
  streamMetaLoadAllTasks(pTq->pStreamMeta);
12,048✔
128
  return tqMetaOpen(pTq);
12,049✔
129
}
130

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

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

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

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

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

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

171
  taosMemoryFree(pTq);
12,023!
172
}
173

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

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

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

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

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

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

223
  char buf1[TSDB_OFFSET_LEN] = {0};
43,178✔
224
  char buf2[TSDB_OFFSET_LEN] = {0};
43,178✔
225
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
43,178✔
226
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
43,177✔
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,
43,177!
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);
43,178✔
232
}
233

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

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

249
  tDecoderClear(&decoder);
11,167✔
250

251
  STqOffset* pOffset = &vgOffset.offset;
11,163✔
252

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

265
  STqOffset* pSavedOffset = NULL;
11,170✔
266
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
11,170✔
267
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
11,171✔
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
  if (taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset))) {
11,167!
275
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
276
    return -1;
×
277
  }
278

279
  if (tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), msg,
11,167!
280
                     msgLen >= sizeof(vgOffset.consumerId) ? msgLen - sizeof(vgOffset.consumerId) : 0) < 0) {
281
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
282
    return -1;
×
283
  }
284

285
  return 0;
11,162✔
286
end:
4✔
287
  tOffsetDestroy(&vgOffset.offset.val);
4✔
288
  return code;
4✔
289
}
290

291
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
24✔
292
  if (pTq == NULL || pMsg == NULL) {
24!
293
    return TSDB_CODE_INVALID_PARA;
×
294
  }
295
  SMqSeekReq req = {0};
24✔
296
  int32_t    vgId = TD_VID(pTq->pVnode);
24✔
297
  SRpcMsg    rsp = {.info = pMsg->info};
24✔
298
  int        code = 0;
24✔
299

300
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
24!
301
    code = TSDB_CODE_OUT_OF_MEMORY;
×
302
    goto end;
×
303
  }
304

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

308
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
24✔
309
  if (pHandle == NULL) {
24!
310
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
311
    code = 0;
×
312
    taosWUnLockLatch(&pTq->lock);
×
313
    goto end;
×
314
  }
315

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

325
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
326
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
327
  tqUnregisterPushHandle(pTq, pHandle);
24✔
328
  taosWUnLockLatch(&pTq->lock);
24✔
329

330
end:
24✔
331
  rsp.code = code;
24✔
332
  tmsgSendRsp(&rsp);
24✔
333
  return 0;
24✔
334
}
335

336
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
144✔
337
  if (pTq == NULL) {
144!
338
    return TSDB_CODE_INVALID_PARA;
×
339
  }
340
  void* pIter = NULL;
144✔
341

342
  while (1) {
11✔
343
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
155✔
344
    if (pIter == NULL) {
155✔
345
      break;
109✔
346
    }
347

348
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
46✔
349

350
    if (pCheck->ntbUid == tbUid) {
46!
351
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
46✔
352
      for (int32_t i = 0; i < sz; i++) {
168✔
353
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
157✔
354
        if (pForbidColId == NULL) {
157!
355
          continue;
×
356
        }
357

358
        if ((*pForbidColId) == colId) {
157✔
359
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
35✔
360
          return -1;
35✔
361
        }
362
      }
363
    }
364
  }
365

366
  return 0;
109✔
367
}
368

369
int32_t tqProcessPollPush(STQ* pTq, SRpcMsg* pMsg) {
23,126✔
370
  if (pTq == NULL) {
23,126!
371
    return TSDB_CODE_INVALID_PARA;
×
372
  }
373
  int32_t vgId = TD_VID(pTq->pVnode);
23,126✔
374
  taosWLockLatch(&pTq->lock);
23,126✔
375
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
23,126!
376
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
23,126✔
377

378
    while (pIter) {
47,166✔
379
      STqHandle* pHandle = *(STqHandle**)pIter;
24,040✔
380
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
24,040!
381

382
      if (pHandle->msg == NULL) {
24,040!
383
        tqError("pHandle->msg should not be null");
×
384
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
385
        break;
×
386
      } else {
387
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
24,040✔
388
                       .pCont = pHandle->msg->pCont,
24,040✔
389
                       .contLen = pHandle->msg->contLen,
24,040✔
390
                       .info = pHandle->msg->info};
24,040✔
391
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
24,040!
392
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
393
        }
394
        taosMemoryFree(pHandle->msg);
24,040!
395
        pHandle->msg = NULL;
24,040✔
396
      }
397

398
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
24,040✔
399
    }
400

401
    taosHashClear(pTq->pPushMgr);
23,126✔
402
  }
403
  taosWUnLockLatch(&pTq->lock);
23,126✔
404
  return 0;
23,126✔
405
}
406

407
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
68,763✔
408
  if (pTq == NULL || pMsg == NULL) {
68,763!
409
    return TSDB_CODE_INVALID_PARA;
×
410
  }
411
  SMqPollReq req = {0};
68,766✔
412
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
68,766✔
413
  if (code < 0) {
68,755!
414
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
415
    terrno = TSDB_CODE_INVALID_MSG;
×
416
    goto END;
×
417
  }
418
  if (req.rawData == 1){
68,755✔
419
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
101✔
420
    if (req.uidHash == NULL) {
124!
421
      tqError("tq poll rawData taosHashInit failed");
×
422
      code = terrno;
×
423
      goto END;
×
424
    }
425
  }
426
  int64_t      consumerId = req.consumerId;
68,778✔
427
  int32_t      reqEpoch = req.epoch;
68,778✔
428
  STqOffsetVal reqOffset = req.reqOffset;
68,778✔
429
  int32_t      vgId = TD_VID(pTq->pVnode);
68,778✔
430
  STqHandle*   pHandle = NULL;
68,778✔
431

432
  while (1) {
2✔
433
    taosWLockLatch(&pTq->lock);
68,780✔
434
    // 1. find handle
435
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
68,788✔
436
    if (code != TDB_CODE_SUCCESS) {
68,755✔
437
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", consumerId, vgId, req.subKey);
10!
438
      terrno = TSDB_CODE_INVALID_MSG;
10✔
439
      taosWUnLockLatch(&pTq->lock);
10✔
440
      return -1;
13✔
441
    }
442

443
    // 2. check rebalance status
444
    if (pHandle->consumerId != consumerId) {
68,745✔
445
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
6!
446
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
447
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
448
      terrno = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
6✔
449
      taosWUnLockLatch(&pTq->lock);
6✔
450
      code = -1;
6✔
451
      goto END;
6✔
452
    }
453

454
    bool exec = tqIsHandleExec(pHandle);
68,739!
455
    if (!exec) {
68,739!
456
      tqSetHandleExec(pHandle);
68,755!
457
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
458
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
68,755!
459
              req.subKey, pHandle);
460
      taosWUnLockLatch(&pTq->lock);
68,761✔
461
      break;
68,772✔
462
    }
463
    taosWUnLockLatch(&pTq->lock);
×
464

465
    tqDebug("tmq poll: consumer:0x%" PRIx64
2!
466
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
467
            consumerId, vgId, req.subKey, pHandle);
468
    taosMsleep(10);
2✔
469
  }
470

471
  // 3. update the epoch value
472
  if (pHandle->epoch < reqEpoch) {
68,772✔
473
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
1,428!
474
            reqEpoch);
475
    pHandle->epoch = reqEpoch;
1,428✔
476
  }
477

478
  char buf[TSDB_OFFSET_LEN] = {0};
68,772✔
479
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
68,772✔
480
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s,QID:0x%" PRIx64,
68,771!
481
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
482

483
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
68,771✔
484
  tqSetHandleIdle(pHandle);
68,771!
485

486
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
68,771!
487
          req.subKey, pHandle);
488

489
END:
×
490
  tDestroySMqPollReq(&req);
68,777✔
491
  return code;
68,778✔
492
}
493

494
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
4✔
495
  if (pTq == NULL || pMsg == NULL) {
4!
496
    return TSDB_CODE_INVALID_PARA;
×
497
  }
498
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
4✔
499
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
4✔
500

501
  SMqVgOffset vgOffset = {0};
4✔
502

503
  SDecoder decoder;
504
  tDecoderInit(&decoder, (uint8_t*)data, len);
4✔
505
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
4!
506
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
507
    return terrno;
×
508
  }
509

510
  tDecoderClear(&decoder);
4✔
511

512
  STqOffset* pSavedOffset = NULL;
4✔
513
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
4✔
514
  if (code != 0) {
4✔
515
    return TSDB_CODE_TMQ_NO_COMMITTED;
1✔
516
  }
517
  vgOffset.offset = *pSavedOffset;
3✔
518

519
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
3!
520
  if (code < 0) {
3!
521
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
522
  }
523

524
  void* buf = rpcMallocCont(len);
3✔
525
  if (buf == NULL) {
3!
526
    return terrno;
×
527
  }
528
  SEncoder encoder = {0};
3✔
529
  tEncoderInit(&encoder, buf, len);
3✔
530
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
3✔
531
  tEncoderClear(&encoder);
3✔
532
  if (code < 0) {
3!
533
    rpcFreeCont(buf);
×
534
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
535
  }
536

537
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
3✔
538

539
  tmsgSendRsp(&rsp);
3✔
540
  return 0;
3✔
541
}
542

543
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
13✔
544
  if (pTq == NULL || pMsg == NULL) {
13!
545
    return TSDB_CODE_INVALID_PARA;
×
546
  }
547
  int32_t    code = 0;
13✔
548
  SMqPollReq req = {0};
13✔
549
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
13!
550
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
551
    return TSDB_CODE_INVALID_MSG;
×
552
  }
553

554
  int64_t      consumerId = req.consumerId;
13✔
555
  STqOffsetVal reqOffset = req.reqOffset;
13✔
556
  int32_t      vgId = TD_VID(pTq->pVnode);
13✔
557

558
  // 1. find handle
559
  taosRLockLatch(&pTq->lock);
13✔
560
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
13✔
561
  if (pHandle == NULL) {
13!
562
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
563
    taosRUnLockLatch(&pTq->lock);
×
564
    return TSDB_CODE_INVALID_MSG;
×
565
  }
566

567
  // 2. check rebalance status
568
  if (pHandle->consumerId != consumerId) {
13!
569
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
570
            consumerId, vgId, req.subKey, pHandle->consumerId);
571
    taosRUnLockLatch(&pTq->lock);
×
572
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
573
  }
574

575
  int64_t sver = 0, ever = 0;
13✔
576
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
13✔
577
  taosRUnLockLatch(&pTq->lock);
13✔
578

579
  SMqDataRsp dataRsp = {0};
13✔
580
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
13✔
581
  if (code != 0) {
13!
582
    return code;
×
583
  }
584

585
  if (req.useSnapshot == true) {
13!
586
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
587
    code = TSDB_CODE_INVALID_PARA;
×
588
    goto END;
×
589
  }
590

591
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
13✔
592

593
  if (reqOffset.type == TMQ_OFFSET__LOG) {
13✔
594
    dataRsp.rspOffset.version = reqOffset.version;
3✔
595
  } else if (reqOffset.type < 0) {
10!
596
    STqOffset* pOffset = NULL;
10✔
597
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
10✔
598
    if (code == 0) {
10✔
599
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
1!
600
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
601
        code = TSDB_CODE_INVALID_PARA;
×
602
        goto END;
×
603
      }
604

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

624
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
13✔
625

626
END:
13✔
627
  tDeleteMqDataRsp(&dataRsp);
13✔
628
  return code;
13✔
629
}
630

631
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
869✔
632
  if (pTq == NULL || msg == NULL) {
869!
633
    return TSDB_CODE_INVALID_PARA;
×
634
  }
635
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
871✔
636
  int32_t        vgId = TD_VID(pTq->pVnode);
871✔
637

638
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
871✔
639
  int32_t code = 0;
873✔
640

641
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
873✔
642
  if (pHandle) {
872✔
643
    while (1) {
×
644
      taosWLockLatch(&pTq->lock);
869✔
645
      bool exec = tqIsHandleExec(pHandle);
869✔
646

647
      if (exec) {
869!
648
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
649
               pHandle->subKey, pHandle);
650
        taosWUnLockLatch(&pTq->lock);
×
651
        taosMsleep(10);
×
652
        continue;
×
653
      }
654
      tqUnregisterPushHandle(pTq, pHandle);
869✔
655
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
869✔
656
      if (code != 0) {
868!
657
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
658
      }
659
      taosWUnLockLatch(&pTq->lock);
868✔
660
      break;
869✔
661
    }
662
  }
663

664
  taosWLockLatch(&pTq->lock);
872✔
665
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
872✔
666
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
252!
667
  }
668
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
872✔
669
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
248!
670
  }
671

672
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
871!
673
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
674
  }
675
  taosWUnLockLatch(&pTq->lock);
872✔
676

677
  return 0;
872✔
678
}
679

680
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
116✔
681
  if (pTq == NULL || msg == NULL) {
116!
682
    return TSDB_CODE_INVALID_PARA;
×
683
  }
684
  STqCheckInfo info = {0};
116✔
685
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
116✔
686
  if (code != 0) {
116!
687
    return code;
×
688
  }
689

690
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
116✔
691
  if (code != 0) {
116!
692
    tDeleteSTqCheckInfo(&info);
×
693
    return code;
×
694
  }
695

696
  return tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
116✔
697
}
698

699
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
10✔
700
  if (pTq == NULL || msg == NULL) {
10!
701
    return TSDB_CODE_INVALID_PARA;
×
702
  }
703
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
10!
704
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
705
  }
706
  return tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
10✔
707
}
708

709
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
3,167✔
710
  if (pTq == NULL || msg == NULL) {
3,167!
711
    return TSDB_CODE_INVALID_PARA;
×
712
  }
713
  int         ret = 0;
3,171✔
714
  SMqRebVgReq req = {0};
3,171✔
715
  SDecoder    dc = {0};
3,171✔
716

717
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
3,171✔
718
  ret = tDecodeSMqRebVgReq(&dc, &req);
3,168✔
719
  if (ret < 0) {
3,172!
720
    goto end;
×
721
  }
722

723
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
3,172✔
724
         req.oldConsumerId, req.newConsumerId);
725

726
  taosRLockLatch(&pTq->lock);
3,178✔
727
  STqHandle* pHandle = NULL;
3,176✔
728
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
3,176✔
729
  if (code != 0){
3,174✔
730
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one", pTq->pVnode->config.vgId, req.subKey);
1,442!
731
  }
732
  taosRUnLockLatch(&pTq->lock);
3,175✔
733
  if (pHandle == NULL) {
3,176✔
734
    if (req.oldConsumerId != -1) {
1,443✔
735
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
1!
736
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
737
    }
738
    if (req.newConsumerId == -1) {
1,443✔
739
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64 "", req.vgId, req.newConsumerId);
1!
740
      ret = TSDB_CODE_INVALID_PARA;
1✔
741
      goto end;
1✔
742
    }
743
    STqHandle handle = {0};
1,442✔
744
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,442✔
745
    if (ret < 0) {
1,442!
746
      tqDestroyTqHandle(&handle);
×
747
      goto end;
×
748
    }
749
    taosWLockLatch(&pTq->lock);
1,442✔
750
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,442✔
751
    taosWUnLockLatch(&pTq->lock);
1,441✔
752
  } else {
753
    while (1) {
×
754
      taosWLockLatch(&pTq->lock);
1,733✔
755
      bool exec = tqIsHandleExec(pHandle);
1,733!
756
      if (exec) {
1,733!
757
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
×
758
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
759
        taosWUnLockLatch(&pTq->lock);
×
760
        taosMsleep(10);
×
761
        continue;
×
762
      }
763
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
1,733✔
764
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
105!
765
      } else {
766
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,628!
767
               req.newConsumerId);
768

769
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
1,628✔
770
        atomic_store_32(&pHandle->epoch, 0);
1,628✔
771
        tqUnregisterPushHandle(pTq, pHandle);
1,628✔
772
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
1,628✔
773
      }
774
      taosWUnLockLatch(&pTq->lock);
1,733✔
775
      break;
1,733✔
776
    }
777
  }
778

779
end:
3,176✔
780
  tDecoderClear(&dc);
3,176✔
781
  return ret;
3,176✔
782
}
783

784
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
31,399!
785

786
int32_t tqBuildStreamTask(void* pTqObj, SStreamTask* pTask, int64_t nextProcessVer) {
14,531✔
787
  STQ*             pTq = (STQ*)pTqObj;
14,531✔
788
  int32_t          vgId = TD_VID(pTq->pVnode);
14,531✔
789
  SCheckpointInfo* pChkInfo = NULL;
14,531✔
790

791
  tqDebug("s-task:0x%x start to build task", pTask->id.taskId);
14,531✔
792

793
  int32_t code = streamTaskInit(pTask, pTq->pStreamMeta, &pTq->pVnode->msgCb, nextProcessVer);
14,531✔
794
  if (code != TSDB_CODE_SUCCESS) {
14,534!
795
    return code;
×
796
  }
797

798
  pTask->pBackend = NULL;
14,534✔
799

800
  // sink
801
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,534✔
802
  if (pOutputInfo->type == TASK_OUTPUT__SMA) {
14,534✔
803
    pOutputInfo->smaSink.vnode = pTq->pVnode;
66✔
804
    pOutputInfo->smaSink.smaSink = smaHandleRes;
66✔
805
  } else if (pOutputInfo->type == TASK_OUTPUT__TABLE) {
14,468✔
806
    pOutputInfo->tbSink.vnode = pTq->pVnode;
7,158✔
807
    pOutputInfo->tbSink.tbSinkFunc = tqSinkDataIntoDstTable;
7,158✔
808

809
    int32_t   ver1 = 1;
7,158✔
810
    SMetaInfo info = {0};
7,158✔
811
    code = metaGetInfo(pTq->pVnode->pMeta, pOutputInfo->tbSink.stbUid, &info, NULL);
7,158✔
812
    if (code == TSDB_CODE_SUCCESS) {
7,152✔
813
      ver1 = info.skmVer;
6,572✔
814
    }
815

816
    SSchemaWrapper* pschemaWrapper = pOutputInfo->tbSink.pSchemaWrapper;
7,152✔
817
    pOutputInfo->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
7,152✔
818
    if (pOutputInfo->tbSink.pTSchema == NULL) {
7,158!
819
      return terrno;
×
820
    }
821

822
    pOutputInfo->tbSink.pTbInfo = tSimpleHashInit(10240, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
7,158✔
823
    if (pOutputInfo->tbSink.pTbInfo == NULL) {
7,161!
824
      tqError("vgId:%d failed init sink tableInfo, code:%s", vgId, tstrerror(terrno));
×
825
      return terrno;
×
826
    }
827

828
    tSimpleHashSetFreeFp(pOutputInfo->tbSink.pTbInfo, freePtr);
7,161✔
829
  }
830

831
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,537✔
832
    bool scanDropCtb = pTask->subtableWithoutMd5 ? true : false;
7,371✔
833
    SWalFilterCond cond = {.deleteMsg = 1, .scanDropCtb = scanDropCtb};  // delete msg also extract from wal files
7,371✔
834
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, &cond, pTask->id.taskId);
7,371✔
835
    if (pTask->exec.pWalReader == NULL) {
7,372✔
836
      tqError("vgId:%d failed init wal reader, code:%s", vgId, tstrerror(terrno));
2!
837
      return terrno;
2✔
838
    }
839
  }
840

841
  streamTaskResetUpstreamStageInfo(pTask);
14,536✔
842

843
  pChkInfo = &pTask->chkInfo;
14,532✔
844
  tqSetRestoreVersionInfo(pTask);
14,532✔
845

846
  char*       p = streamTaskGetStatus(pTask).name;
14,529✔
847
  const char* pNext = streamTaskGetStatusStr(pTask->status.taskStatus);
14,530✔
848

849
  if (pTask->info.fillHistory) {
14,530✔
850
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
5,000!
851
           " nextProcessVer:%" PRId64
852
           " child id:%d, level:%d, cur-status:%s, next-status:%s fill-history:%d, related stream 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->streamTaskId.taskId, pTask->info.delaySchedParam, nextProcessVer);
857
  } else {
858
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
9,530✔
859
           " nextProcessVer:%" PRId64
860
           " child id:%d, level:%d, cur-status:%s next-status:%s fill-history:%d, related fill-task:0x%x "
861
           "delaySched:%" PRId64 " ms, inputVer:%" PRId64,
862
           vgId, pTask->id.idStr, pTask, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
863
           pTask->info.selfChildId, pTask->info.taskLevel, p, pNext, pTask->info.fillHistory,
864
           (int32_t)pTask->hTaskInfo.id.taskId, pTask->info.delaySchedParam, nextProcessVer);
865

866
    if (pChkInfo->checkpointVer > pChkInfo->nextProcessVer) {
9,539!
867
      tqError("vgId:%d build stream task, s-task:%s, checkpointVer:%" PRId64 " > nextProcessVer:%" PRId64, vgId,
×
868
              pTask->id.idStr, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
869
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
870
    }
871
  }
872

873
  return 0;
14,539✔
874
}
875

876
int32_t tqProcessTaskCheckReq(STQ* pTq, SRpcMsg* pMsg) { return tqStreamTaskProcessCheckReq(pTq->pStreamMeta, pMsg); }
22,243✔
877

878
int32_t tqProcessTaskCheckRsp(STQ* pTq, SRpcMsg* pMsg) {
22,523✔
879
  return tqStreamTaskProcessCheckRsp(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
22,523✔
880
}
881

882
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
14,209✔
883
  return tqStreamTaskProcessDeployReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, sversion, msg, msgLen,
14,222✔
884
                                      vnodeIsRoleLeader(pTq->pVnode), pTq->pVnode->restored);
14,209✔
885
}
886

887
static void doStartFillhistoryStep2(SStreamTask* pTask, SStreamTask* pStreamTask, STQ* pTq) {
2,350✔
888
  const char*    id = pTask->id.idStr;
2,350✔
889
  int64_t        nextProcessedVer = pStreamTask->hTaskInfo.haltVer;
2,350✔
890
  SVersionRange* pStep2Range = &pTask->step2Range;
2,350✔
891
  int32_t        vgId = pTask->pMeta->vgId;
2,350✔
892

893
  // if it's an source task, extract the last version in wal.
894
  bool done = streamHistoryTaskSetVerRangeStep2(pTask, nextProcessedVer);
2,350✔
895
  pTask->execInfo.step2Start = taosGetTimestampMs();
2,350✔
896

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

915
    int32_t code = streamSetParamForStreamScannerStep2(pTask, pStep2Range, pWindow);
882✔
916
    if (code) {
882!
917
      tqError("s-task:%s level:%d failed to set step2 param", id, pTask->info.taskLevel);
×
918
    }
919

920
    int64_t dstVer = pStep2Range->minVer;
882✔
921
    pTask->chkInfo.nextProcessVer = dstVer;
882✔
922

923
    walReaderSetSkipToVersion(pTask->exec.pWalReader, dstVer);
882✔
924
    tqDebug("s-task:%s wal reader start scan WAL verRange:%" PRId64 "-%" PRId64 ", set sched-status:%d", id, dstVer,
882✔
925
            pStep2Range->maxVer, TASK_SCHED_STATUS__INACTIVE);
926

927
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
882✔
928

929
    // now the fill-history task starts to scan data from wal files.
930
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_SCANHIST_DONE);
882✔
931
    if (code == TSDB_CODE_SUCCESS) {
882!
932
      code = tqScanWalAsync(pTq, false);
882✔
933
      if (code) {
882!
934
        tqError("vgId:%d failed to start scan wal file, code:%s", vgId, tstrerror(code));
×
935
      }
936
    }
937
  }
938
}
2,349✔
939

940
int32_t handleStep2Async(SStreamTask* pStreamTask, void* param) {
2,350✔
941
  STQ* pTq = param;
2,350✔
942

943
  SStreamMeta* pMeta = pStreamTask->pMeta;
2,350✔
944
  STaskId      hId = pStreamTask->hTaskInfo.id;
2,350✔
945
  SStreamTask* pTask = NULL;
2,350✔
946
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
2,350✔
947
  if (pTask == NULL) {
2,350!
948
    tqWarn("s-task:0x%x failed to acquired it to exec step 2, scan wal quit", (int32_t)hId.taskId);
×
949
    return TSDB_CODE_SUCCESS;
×
950
  }
951

952
  doStartFillhistoryStep2(pTask, pStreamTask, pTq);
2,350✔
953

954
  streamMetaReleaseTask(pMeta, pTask);
2,349✔
955
  return TSDB_CODE_SUCCESS;
2,350✔
956
}
957

958
// this function should be executed by only one thread, so we set an sentinel to protect this function
959
int32_t tqProcessTaskScanHistory(STQ* pTq, SRpcMsg* pMsg) {
2,587✔
960
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
2,587✔
961
  SStreamMeta*           pMeta = pTq->pStreamMeta;
2,587✔
962
  int32_t                code = TSDB_CODE_SUCCESS;
2,587✔
963
  SStreamTask*           pTask = NULL;
2,587✔
964
  SStreamTask*           pStreamTask = NULL;
2,587✔
965

966
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
2,587✔
967
  if (pTask == NULL) {
2,587!
968
    tqError("vgId:%d failed to acquire stream task:0x%x during scan history data, task may have been destroyed",
×
969
            pMeta->vgId, pReq->taskId);
970
    return code;
×
971
  }
972

973
  // do recovery step1
974
  const char* id = pTask->id.idStr;
2,587✔
975
  char*       pStatus = streamTaskGetStatus(pTask).name;
2,587✔
976

977
  // avoid multi-thread exec
978
  while (1) {
×
979
    int32_t sentinel = atomic_val_compare_exchange_32(&pTask->status.inScanHistorySentinel, 0, 1);
2,587✔
980
    if (sentinel != 0) {
2,587✔
981
      tqDebug("s-task:%s already in scan-history func, wait for 100ms, and try again", id);
1!
982
      taosMsleep(100);
1✔
983
    } else {
984
      break;
2,586✔
985
    }
986
  }
987

988
  // let's decide which step should be executed now
989
  if (pTask->execInfo.step1Start == 0) {
2,586✔
990
    int64_t ts = taosGetTimestampMs();
2,359✔
991
    pTask->execInfo.step1Start = ts;
2,359✔
992
    tqDebug("s-task:%s start scan-history stage(step 1), status:%s, step1 startTs:%" PRId64, id, pStatus, ts);
2,359✔
993
  } else {
994
    if (pTask->execInfo.step2Start == 0) {
227✔
995
      tqDebug("s-task:%s continue exec scan-history(step1), original step1 startTs:%" PRId64 ", already elapsed:%.2fs",
183!
996
              id, pTask->execInfo.step1Start, pTask->execInfo.step1El);
997
    } else {
998
      tqDebug("s-task:%s already in step2, no need to scan-history data, step2 startTs:%" PRId64, id,
44!
999
              pTask->execInfo.step2Start);
1000

1001
      atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
44✔
1002
      streamMetaReleaseTask(pMeta, pTask);
45✔
1003
      return 0;
45✔
1004
    }
1005
  }
1006

1007
  // we have to continue retrying to successfully execute the scan history task.
1008
  if (!streamTaskSetSchedStatusWait(pTask)) {
2,542!
1009
    tqError(
×
1010
        "s-task:%s failed to start scan-history in first stream time window since already started, unexpected "
1011
        "sched-status:%d",
1012
        id, pTask->status.schedStatus);
1013
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1014
    streamMetaReleaseTask(pMeta, pTask);
×
1015
    return 0;
×
1016
  }
1017

1018
  int64_t              st = taosGetTimestampMs();
2,542✔
1019
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
2,542✔
1020

1021
  double el = (taosGetTimestampMs() - st) / 1000.0;
2,542✔
1022
  pTask->execInfo.step1El += el;
2,542✔
1023

1024
  if (retInfo.ret == TASK_SCANHISTORY_QUIT || retInfo.ret == TASK_SCANHISTORY_REXEC) {
2,542✔
1025
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
186✔
1026
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
186✔
1027

1028
    if (retInfo.ret == TASK_SCANHISTORY_REXEC) {
186✔
1029
      streamExecScanHistoryInFuture(pTask, retInfo.idleTime);
183✔
1030
    } else {
1031
      SStreamTaskState p = streamTaskGetStatus(pTask);
3✔
1032
      ETaskStatus      s = p.state;
3✔
1033

1034
      if (s == TASK_STATUS__PAUSE) {
3!
1035
        tqDebug("s-task:%s is paused in the step1, elapsed time:%.2fs total:%.2fs, sched-status:%d", id, el,
×
1036
                pTask->execInfo.step1El, status);
1037
      } else if (s == TASK_STATUS__STOP || s == TASK_STATUS__DROPPING) {
3!
1038
        tqDebug("s-task:%s status:%p not continue scan-history data, total elapsed time:%.2fs quit", id, p.name,
3!
1039
                pTask->execInfo.step1El);
1040
      }
1041
    }
1042

1043
    streamMetaReleaseTask(pMeta, pTask);
186✔
1044
    return 0;
186✔
1045
  }
1046

1047
  // the following procedure should be executed, no matter status is stop/pause or not
1048
  tqDebug("s-task:%s scan-history(step 1) ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
2,356✔
1049

1050
  if (pTask->info.fillHistory != 1) {
2,356!
1051
    tqError("s-task:%s fill-history is disabled, unexpected", id);
×
1052
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1053
  }
1054

1055
  // 1. get the related stream task
1056
  code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
2,356✔
1057
  if (pStreamTask == NULL) {
2,356✔
1058
    tqError("failed to find s-task:0x%" PRIx64 ", it may have been destroyed, drop related fill-history task:%s",
6!
1059
            pTask->streamTaskId.taskId, pTask->id.idStr);
1060

1061
    tqDebug("s-task:%s fill-history task set status to be dropping", id);
6!
1062
    code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
6✔
1063

1064
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
6✔
1065
    streamMetaReleaseTask(pMeta, pTask);
6✔
1066
    return code;
6✔
1067
  }
1068

1069
  if (pStreamTask->info.taskLevel != TASK_LEVEL__SOURCE) {
2,350!
1070
    tqError("s-task:%s fill-history task related stream task level:%d, unexpected", id, pStreamTask->info.taskLevel);
×
1071
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1072
  }
1073

1074
  code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
2,350✔
1075
  streamMetaReleaseTask(pMeta, pStreamTask);
2,349✔
1076

1077
  atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
2,350✔
1078
  streamMetaReleaseTask(pMeta, pTask);
2,350✔
1079
  return code;
2,350✔
1080
}
1081

1082
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
161,900✔
1083
  int32_t  code = 0;
161,900✔
1084
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
161,900✔
1085
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
161,900✔
1086
  SDecoder decoder;
1087

1088
  SStreamTaskRunReq req = {0};
161,900✔
1089
  tDecoderInit(&decoder, (uint8_t*)msg, len);
161,900✔
1090
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
161,948!
1091
    tqError("vgId:%d failed to decode task run req, code:%s", pTq->pStreamMeta->vgId, tstrerror(code));
×
1092
    tDecoderClear(&decoder);
×
1093
    return TSDB_CODE_SUCCESS;
×
1094
  }
1095

1096
  tDecoderClear(&decoder);
161,948✔
1097

1098
  // extracted submit data from wal files for all tasks
1099
  if (req.reqType == STREAM_EXEC_T_EXTRACT_WAL_DATA) {
161,929✔
1100
    return tqScanWal(pTq);
63,299✔
1101
  }
1102

1103
  code = tqStreamTaskProcessRunReq(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
98,630✔
1104
  if (code) {
98,630✔
1105
    tqError("vgId:%d failed to create task run req, code:%s", TD_VID(pTq->pVnode), tstrerror(code));
18!
1106
    return code;
18✔
1107
  }
1108

1109
  // let's continue scan data in the wal files
1110
  if (req.reqType >= 0 || req.reqType == STREAM_EXEC_T_RESUME_TASK) {
98,612✔
1111
    code = tqScanWalAsync(pTq, false);  // it's ok to failed
74,423✔
1112
    if (code) {
74,465✔
1113
      tqError("vgId:%d failed to start scan wal file, code:%s", pTq->pStreamMeta->vgId, tstrerror(code));
4!
1114
    }
1115
  }
1116

1117
  return code;
98,649✔
1118
}
1119

1120
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
57,611✔
1121
  return tqStreamTaskProcessDispatchReq(pTq->pStreamMeta, pMsg);
57,611✔
1122
}
1123

1124
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
57,570✔
1125
  return tqStreamTaskProcessDispatchRsp(pTq->pStreamMeta, pMsg);
57,570✔
1126
}
1127

1128
int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
6,956✔
1129
  return tqStreamTaskProcessDropReq(pTq->pStreamMeta, msg, msgLen);
6,956✔
1130
}
1131

1132
int32_t tqProcessTaskUpdateCheckpointReq(STQ* pTq, char* msg, int32_t msgLen) {
5,395✔
1133
  return tqStreamTaskProcessUpdateCheckpointReq(pTq->pStreamMeta, pTq->pVnode->restored, msg);
5,395✔
1134
}
1135

1136
int32_t tqProcessTaskConsenChkptIdReq(STQ* pTq, SRpcMsg* pMsg) {
207✔
1137
  return tqStreamTaskProcessConsenChkptIdReq(pTq->pStreamMeta, pMsg);
207✔
1138
}
1139

1140
int32_t tqProcessTaskPauseReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,393✔
1141
  return tqStreamTaskProcessTaskPauseReq(pTq->pStreamMeta, msg);
1,393✔
1142
}
1143

1144
int32_t tqProcessTaskResumeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
2,568✔
1145
  return tqStreamTaskProcessTaskResumeReq(pTq, sversion, msg, true);
2,568✔
1146
}
1147

1148
int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
557✔
1149
  return tqStreamTaskProcessRetrieveReq(pTq->pStreamMeta, pMsg);
557✔
1150
}
1151

1152
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
446✔
1153

1154
int32_t tqStreamProgressRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
×
1155
  char*               msgStr = pMsg->pCont;
×
1156
  char*               msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
1157
  int32_t             msgLen = pMsg->contLen - sizeof(SMsgHead);
×
1158
  int32_t             code = 0;
×
1159
  SStreamProgressReq  req;
1160
  char*               pRspBuf = taosMemoryCalloc(1, sizeof(SMsgHead) + sizeof(SStreamProgressRsp));
×
1161
  SStreamProgressRsp* pRsp = POINTER_SHIFT(pRspBuf, sizeof(SMsgHead));
×
1162
  if (!pRspBuf) {
×
1163
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1164
    code = -1;
×
1165
    goto _OVER;
×
1166
  }
1167

1168
  code = tDeserializeStreamProgressReq(msgBody, msgLen, &req);
×
1169
  if (code == TSDB_CODE_SUCCESS) {
×
1170
    code = tqGetStreamExecInfo(pTq->pVnode, req.streamId, &pRsp->progressDelay, &pRsp->fillHisFinished);
×
1171
  }
1172
  if (code == TSDB_CODE_SUCCESS) {
×
1173
    pRsp->fetchIdx = req.fetchIdx;
×
1174
    pRsp->subFetchIdx = req.subFetchIdx;
×
1175
    pRsp->vgId = req.vgId;
×
1176
    pRsp->streamId = req.streamId;
×
1177
    code = tSerializeStreamProgressRsp(pRsp, sizeof(SStreamProgressRsp) + sizeof(SMsgHead), pRsp);
×
1178
    if (code) {
×
1179
      goto _OVER;
×
1180
    }
1181

1182
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
1183
    rsp.pCont = pRspBuf;
×
1184
    pRspBuf = NULL;
×
1185
    rsp.contLen = sizeof(SMsgHead) + sizeof(SStreamProgressRsp);
×
1186
    tmsgSendRsp(&rsp);
×
1187
  }
1188

1189
_OVER:
×
1190
  if (pRspBuf) {
×
1191
    taosMemoryFree(pRspBuf);
×
1192
  }
1193
  return code;
×
1194
}
1195

1196
// always return success to mnode
1197
//todo: handle failure of build and send msg to mnode
1198
static void doSendChkptSourceRsp(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo, int32_t code,
128✔
1199
                                 int32_t taskId) {
1200
  SRpcMsg rsp = {0};
128✔
1201
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
128✔
1202
  if (ret) {  // suppress the error in build checkpoint source rsp
128!
1203
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1204
  }
1205
  tmsgSendRsp(&rsp);  // error occurs
128✔
1206
}
128✔
1207

1208
// no matter what kinds of error happened, make sure the mnode will receive the success execution code.
1209
int32_t tqProcessTaskCheckPointSourceReq(STQ* pTq, SRpcMsg* pMsg, SRpcMsg* pRsp) {
3,218✔
1210
  int32_t                    vgId = TD_VID(pTq->pVnode);
3,218✔
1211
  SStreamMeta*               pMeta = pTq->pStreamMeta;
3,218✔
1212
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
3,218✔
1213
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
3,218✔
1214
  int32_t                    code = 0;
3,218✔
1215
  SStreamCheckpointSourceReq req = {0};
3,218✔
1216
  SDecoder                   decoder = {0};
3,218✔
1217
  SStreamTask*               pTask = NULL;
3,218✔
1218
  int64_t                    checkpointId = 0;
3,218✔
1219

1220
  // disable auto rsp to mnode
1221
  pRsp->info.handle = NULL;
3,218✔
1222

1223
  tDecoderInit(&decoder, (uint8_t*)msg, len);
3,218✔
1224
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
3,222!
1225
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
1226
    tDecoderClear(&decoder);
×
1227
    tqError("vgId:%d failed to decode checkpoint-source msg, code:%s", vgId, tstrerror(code));
×
1228
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1229
    return TSDB_CODE_SUCCESS;  // always return success to mnode,
×
1230
  }
1231

1232
  tDecoderClear(&decoder);
3,214✔
1233

1234
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
3,219✔
1235
    tqDebug("vgId:%d not leader, ignore checkpoint-source msg, s-task:0x%x", vgId, req.taskId);
15!
1236
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
15✔
1237
    return TSDB_CODE_SUCCESS;  // always return success to mnode
15✔
1238
  }
1239

1240
  if (!pTq->pVnode->restored) {
3,202✔
1241
    tqDebug("vgId:%d checkpoint-source msg received during restoring, checkpointId:%" PRId64
113✔
1242
            ", transId:%d s-task:0x%x ignore it",
1243
            vgId, req.checkpointId, req.transId, req.taskId);
1244
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
113✔
1245
    return TSDB_CODE_SUCCESS;  // always return success to mnode
113✔
1246
  }
1247

1248
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
3,089✔
1249
  if (pTask == NULL || code != 0) {
3,090!
1250
    tqError("vgId:%d failed to find s-task:0x%x, ignore checkpoint msg. checkpointId:%" PRId64
2!
1251
            " transId:%d it may have been destroyed",
1252
            vgId, req.taskId, req.checkpointId, req.transId);
1253
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
2✔
1254
    return TSDB_CODE_SUCCESS;
×
1255
  }
1256

1257
  if (pTask->status.downstreamReady != 1) {
3,088!
1258
    // record the latest failed checkpoint id
1259
    streamTaskSetFailedChkptInfo(pTask, req.transId, req.checkpointId);
×
1260
    tqError("s-task:%s not ready for checkpoint, since downstream not ready, ignore this checkpointId:%" PRId64
×
1261
            ", transId:%d set it failed",
1262
            pTask->id.idStr, req.checkpointId, req.transId);
1263

1264
    streamMetaReleaseTask(pMeta, pTask);
×
1265
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1266
    return TSDB_CODE_SUCCESS;  // todo retry handle error
×
1267
  }
1268

1269
  // todo save the checkpoint failed info
1270
  streamMutexLock(&pTask->lock);
3,088✔
1271
  ETaskStatus status = streamTaskGetStatus(pTask).state;
3,096✔
1272

1273
  if (req.mndTrigger == 1) {
3,089✔
1274
    if (status == TASK_STATUS__HALT || status == TASK_STATUS__PAUSE) {
959!
1275
      tqError("s-task:%s not ready for checkpoint, since it is halt, ignore checkpointId:%" PRId64 ", set it failure",
×
1276
              pTask->id.idStr, req.checkpointId);
1277

1278
      streamMutexUnlock(&pTask->lock);
×
1279
      streamMetaReleaseTask(pMeta, pTask);
×
1280
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1281
      return TSDB_CODE_SUCCESS;
×
1282
    }
1283
  } else {
1284
    if (status != TASK_STATUS__HALT) {
2,130!
1285
      tqError("s-task:%s should in halt status, let's halt it directly", pTask->id.idStr);
×
1286
      //      streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_HALT);
1287
    }
1288
  }
1289

1290
  // check if the checkpoint msg already sent or not.
1291
  if (status == TASK_STATUS__CK) {
3,078!
1292
    streamTaskGetActiveCheckpointInfo(pTask, NULL, &checkpointId);
×
1293

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

1298
    streamMutexUnlock(&pTask->lock);
×
1299
    streamMetaReleaseTask(pMeta, pTask);
×
1300
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SYN_PROPOSE_NOT_READY, req.taskId);
×
1301
    return TSDB_CODE_SUCCESS;
×
1302
  } else {  // checkpoint already finished, and not in checkpoint status
1303
    if (req.checkpointId <= pTask->chkInfo.checkpointId) {
3,078!
1304
      tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1305
             " transId:%d already handled, return success",
1306
             pTask->id.idStr, req.checkpointId, req.transId);
1307

1308
      streamMutexUnlock(&pTask->lock);
×
1309
      streamMetaReleaseTask(pMeta, pTask);
×
1310
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1311
      return TSDB_CODE_SUCCESS;
×
1312
    }
1313
  }
1314

1315
  code = streamProcessCheckpointSourceReq(pTask, &req);
3,078✔
1316
  streamMutexUnlock(&pTask->lock);
3,095✔
1317

1318
  if (code) {
3,095!
1319
    qError("s-task:%s (vgId:%d) failed to process checkpoint-source req, code:%s", pTask->id.idStr, vgId,
×
1320
           tstrerror(code));
1321
    streamMetaReleaseTask(pMeta, pTask);
×
1322
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1323
    return TSDB_CODE_SUCCESS;
×
1324
  }
1325

1326
  if (req.mndTrigger) {
3,095✔
1327
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64 ", transId:%d, ",
960!
1328
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId);
1329
  } else {
1330
    const char* pPrevStatus = streamTaskGetStatusStr(streamTaskGetPrevStatus(pTask));
2,135✔
1331
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64
2,134!
1332
           ", transId:%d after transfer-state, prev status:%s",
1333
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId, pPrevStatus);
1334
  }
1335

1336
  code = streamAddCheckpointSourceRspMsg(&req, &pMsg->info, pTask);
3,099✔
1337
  if (code != TSDB_CODE_SUCCESS) {
3,096!
1338
    streamTaskSetCheckpointFailed(pTask);  // set the checkpoint failed
×
1339
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1340
  }
1341

1342
  streamMetaReleaseTask(pMeta, pTask);
3,096✔
1343
  return TSDB_CODE_SUCCESS;
3,099✔
1344
}
1345

1346
// downstream task has complete the stream task checkpoint procedure, let's start the handle the rsp by execute task
1347
int32_t tqProcessTaskCheckpointReadyMsg(STQ* pTq, SRpcMsg* pMsg) {
7,706✔
1348
  int32_t vgId = TD_VID(pTq->pVnode);
7,706✔
1349

1350
  SStreamCheckpointReadyMsg* pReq = (SStreamCheckpointReadyMsg*)pMsg->pCont;
7,706✔
1351
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
7,706!
1352
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from 0x%x", vgId,
×
1353
            (int32_t)pReq->downstreamTaskId);
1354
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1355
  }
1356

1357
  return tqStreamTaskProcessCheckpointReadyMsg(pTq->pStreamMeta, pMsg);
7,705✔
1358
}
1359

1360
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
81✔
1361
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg, pTq->pVnode->restored);
81✔
1362
}
1363

1364
int32_t tqProcessTaskResetReq(STQ* pTq, SRpcMsg* pMsg) {
×
1365
  return tqStreamTaskProcessTaskResetReq(pTq->pStreamMeta, pMsg->pCont);
×
1366
}
1367

1368
int32_t tqProcessTaskRetrieveTriggerReq(STQ* pTq, SRpcMsg* pMsg) {
×
1369
  int32_t vgId = TD_VID(pTq->pVnode);
×
1370

1371
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1372
    SRetrieveChkptTriggerReq req = {0};
×
1373

1374
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1375
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1376
    SDecoder decoder = {0};
×
1377

1378
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1379
    if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1380
      tDecoderClear(&decoder);
×
1381
      tqError("vgId:%d invalid retrieve checkpoint-trigger req received", vgId);
×
1382
      return TSDB_CODE_INVALID_MSG;
×
1383
    }
1384
    tDecoderClear(&decoder);
×
1385

1386
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from s-task:0x%" PRId64, vgId,
×
1387
            req.downstreamTaskId);
1388
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1389
  }
1390

1391
  return tqStreamTaskProcessRetrieveTriggerReq(pTq->pStreamMeta, pMsg);
×
1392
}
1393

1394
int32_t tqProcessTaskRetrieveTriggerRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1395
  return tqStreamTaskProcessRetrieveTriggerRsp(pTq->pStreamMeta, pMsg);
×
1396
}
1397

1398
// this function is needed, do not try to remove it.
1399
int32_t tqProcessStreamHbRsp(STQ* pTq, SRpcMsg* pMsg) { return tqStreamProcessStreamHbRsp(pTq->pStreamMeta, pMsg); }
23,557✔
1400

1401
int32_t tqProcessStreamReqCheckpointRsp(STQ* pTq, SRpcMsg* pMsg) {
4,368✔
1402
  return tqStreamProcessReqCheckpointRsp(pTq->pStreamMeta, pMsg);
4,368✔
1403
}
1404

1405
int32_t tqProcessTaskCheckpointReadyRsp(STQ* pTq, SRpcMsg* pMsg) {
7,707✔
1406
  return tqStreamProcessCheckpointReadyRsp(pTq->pStreamMeta, pMsg);
7,707✔
1407
}
1408

1409
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
5,931✔
1410
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
5,931✔
1411
}
1412

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