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

taosdata / TDengine / #4180

27 May 2025 10:49AM UTC coverage: 63.267% (+0.4%) from 62.885%
#4180

push

travis-ci

web-flow
TD-35056 (#31227)

158426 of 318644 branches covered (49.72%)

Branch coverage included in aggregate %.

243945 of 317346 relevant lines covered (76.87%)

18276423.81 hits per line

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

59.67
/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; }
57,251!
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
53,488✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
53,487✔
31

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

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,657✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
1,294!
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
363✔
40
    tqReaderClose(pData->execHandle.pTqReader);
291✔
41
    walCloseReader(pData->pWalReader);
291✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
291✔
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,657!
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
1,657!
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
1,657✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
1,610✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
1,657✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
8,750✔
64
  if (pLeft == NULL || pRight == NULL) {
8,750!
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
17,127✔
68
         pLeft->val.version == pRight->val.version;
8,377✔
69
}
70

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

80
  pVnode->pTq = pTq;
15,326✔
81
  pTq->pVnode = pVnode;
15,326✔
82

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

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

94
  taosInitRWLatch(&pTq->lock);
15,329✔
95

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

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

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

113
  return tqInitialize(pTq);
15,329✔
114
}
115

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

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

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

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

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

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

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

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

171
  taosMemoryFree(pTq);
15,332!
172
}
173

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

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

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

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

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

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

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

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

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

249
  tDecoderClear(&decoder);
9,858✔
250

251
  STqOffset* pOffset = &vgOffset.offset;
9,848✔
252

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

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

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

279
  return 0;
9,865✔
280
end:
2✔
281
  tOffsetDestroy(&vgOffset.offset.val);
2✔
282
  return code;
2✔
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) {
666✔
331
  if (pTq == NULL) {
666!
332
    return TSDB_CODE_INVALID_PARA;
×
333
  }
334
  void* pIter = NULL;
666✔
335

336
  while (1) {
11✔
337
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
677✔
338
    if (pIter == NULL) {
677✔
339
      break;
631✔
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;
631✔
361
}
362

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

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

376
      if (pHandle->msg == NULL) {
15,321!
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,321✔
382
                       .pCont = pHandle->msg->pCont,
15,321✔
383
                       .contLen = pHandle->msg->contLen,
15,321✔
384
                       .info = pHandle->msg->info};
15,321✔
385
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
15,321!
386
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
387
        }
388
        taosMemoryFree(pHandle->msg);
15,321!
389
        pHandle->msg = NULL;
15,321✔
390
      }
391

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

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

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

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

436
    // 2. check rebalance status
437
    if (pHandle->consumerId != consumerId) {
53,474✔
438
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
8!
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;
8✔
442
      taosWUnLockLatch(&pTq->lock);
8✔
443
      goto END;
8✔
444
    }
445

446
    bool exec = tqIsHandleExec(pHandle);
53,466!
447
    if (!exec) {
53,466!
448
      tqSetHandleExec(pHandle);
53,488!
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,
53,488!
451
              req.subKey, pHandle);
452
      taosWUnLockLatch(&pTq->lock);
53,494✔
453
      break;
53,496✔
454
    }
455
    taosWUnLockLatch(&pTq->lock);
×
456

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

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

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

475
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
53,496✔
476
  tqSetHandleIdle(pHandle);
53,487!
477

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

481
END:
×
482
  tDestroySMqPollReq(&req);
53,501✔
483
  return code;
53,504✔
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) {
931✔
624
  if (pTq == NULL || msg == NULL) {
931!
625
    return TSDB_CODE_INVALID_PARA;
×
626
  }
627
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
934✔
628
  int32_t        vgId = TD_VID(pTq->pVnode);
934✔
629

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

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

639
      if (exec) {
932!
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);
932✔
647
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
932✔
648
      if (code != 0) {
931!
649
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
650
      }
651
      taosWUnLockLatch(&pTq->lock);
931✔
652
      break;
932✔
653
    }
654
  }
655

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

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

669
  return 0;
934✔
670
}
671

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

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

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

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

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

709
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
4,276✔
710
  ret = tDecodeSMqRebVgReq(&dc, &req);
4,272✔
711
  if (ret < 0) {
4,274!
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,
4,274!
716
         req.oldConsumerId, req.newConsumerId);
717

718
  taosRLockLatch(&pTq->lock);
4,279✔
719
  STqHandle* pHandle = NULL;
4,280✔
720
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
4,280✔
721
  if (code != 0){
4,277✔
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,427!
723
  }
724
  taosRUnLockLatch(&pTq->lock);
4,278✔
725
  if (pHandle == NULL) {
4,280✔
726
    if (req.oldConsumerId != -1) {
1,429!
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,429!
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,429✔
736
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,429✔
737
    if (ret < 0) {
1,429!
738
      tqDestroyTqHandle(&handle);
×
739
      goto end;
×
740
    }
741
    taosWLockLatch(&pTq->lock);
1,429✔
742
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,429✔
743
    taosWUnLockLatch(&pTq->lock);
1,428✔
744
  } else {
745
    while (1) {
2✔
746
      taosWLockLatch(&pTq->lock);
2,853✔
747
      bool exec = tqIsHandleExec(pHandle);
2,853!
748
      if (exec) {
2,853✔
749
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
2!
750
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
751
        taosWUnLockLatch(&pTq->lock);
2✔
752
        taosMsleep(10);
2✔
753
        continue;
2✔
754
      }
755
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
2,851✔
756
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
163!
757
      } else {
758
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
2,688!
759
               req.newConsumerId);
760

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

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

776
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
14,947!
777

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

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

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

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

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

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

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

814
    pOutputInfo->tbSink.pTbInfo = tSimpleHashInit(10240, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,274✔
815
    if (pOutputInfo->tbSink.pTbInfo == NULL) {
6,281!
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,281✔
821
  }
822

823
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
12,556✔
824
    bool scanDropCtb = pTask->subtableWithoutMd5 ? true : false;
6,370✔
825
    SWalFilterCond cond = {.deleteMsg = 1, .scanDropCtb = scanDropCtb};  // delete msg also extract from wal files
6,370✔
826
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, &cond, pTask->id.taskId);
6,370✔
827
    if (pTask->exec.pWalReader == NULL) {
6,370✔
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,555✔
834

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

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

841
  if (pTask->info.fillHistory) {
12,557✔
842
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
3,835!
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,722✔
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,725!
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,560✔
866
}
867

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

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

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

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

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

889
  if (done) {
1,791✔
890
    qDebug("s-task:%s scan wal(step 2) verRange:%" PRId64 "-%" PRId64 " ended, elapsed time:%.2fs", id,
953✔
891
           pStep2Range->minVer, pStep2Range->maxVer, 0.0);
892
    int32_t code = streamTaskPutTranstateIntoInputQ(pTask);  // todo: msg lost.
953✔
893
    if (code) {
954!
894
      qError("s-task:%s failed put trans-state into inputQ, code:%s", id, tstrerror(code));
×
895
    }
896
    (void)streamExecTask(pTask);  // exec directly
954✔
897
  } else {
898
    STimeWindow* pWindow = &pTask->dataRange.window;
838✔
899
    tqDebug("s-task:%s level:%d verRange:%" PRId64 "-%" PRId64 " window:%" PRId64 "-%" PRId64
838✔
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) {
838!
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);
838✔
908
    if (code) {
838!
909
      tqError("s-task:%s level:%d failed to set step2 param", id, pTask->info.taskLevel);
×
910
    }
911

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

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

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

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

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

929
  SStreamMeta* pMeta = pStreamTask->pMeta;
1,792✔
930
  STaskId      hId = pStreamTask->hTaskInfo.id;
1,792✔
931
  SStreamTask* pTask = NULL;
1,792✔
932
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
1,792✔
933
  if (pTask == NULL) {
1,791!
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,791✔
939

940
  streamMetaReleaseTask(pMeta, pTask);
1,792✔
941
  return TSDB_CODE_SUCCESS;
1,792✔
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,942✔
946
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
1,942✔
947
  SStreamMeta*           pMeta = pTq->pStreamMeta;
1,942✔
948
  int32_t                code = TSDB_CODE_SUCCESS;
1,942✔
949
  SStreamTask*           pTask = NULL;
1,942✔
950
  SStreamTask*           pStreamTask = NULL;
1,942✔
951
  char*                  pStatus = NULL;
1,942✔
952
  int32_t                taskType = 0;
1,942✔
953

954
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,942✔
955
  if (pTask == NULL) {
1,942✔
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,941✔
963
  streamMutexLock(&pTask->lock);
1,941✔
964

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

969
  if ((s.state != TASK_STATUS__SCAN_HISTORY && taskType == STREAM_HISTORY_TASK) ||
1,941!
970
      (s.state != TASK_STATUS__READY && taskType == STREAM_RECALCUL_TASK) ||
1,941!
971
      (pTask->status.downstreamReady == 0)) {
1,941!
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,941!
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,941✔
989

990
  // avoid multi-thread exec
991
  while (1) {
×
992
    int32_t sentinel = atomic_val_compare_exchange_32(&pTask->status.inScanHistorySentinel, 0, 1);
1,941✔
993
    if (sentinel != 0) {
1,941!
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,941✔
998
    }
999
  }
1000

1001
  // let's decide which step should be executed now
1002
  if (pTask->execInfo.step1Start == 0) {
1,941✔
1003
    int64_t ts = taosGetTimestampMs();
1,797✔
1004
    pTask->execInfo.step1Start = ts;
1,797✔
1005
    tqDebug("s-task:%s start scan-history stage(step 1), status:%s, step1 startTs:%" PRId64, id, pStatus, ts);
1,797✔
1006
  } else {
1007
    if (pTask->execInfo.step2Start == 0) {
144!
1008
      tqDebug("s-task:%s continue exec scan-history(step1), original step1 startTs:%" PRId64 ", already elapsed:%.2fs",
144!
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,941!
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,941✔
1032
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
1,941✔
1033

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

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

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

1047
      if (localStatus == TASK_STATUS__PAUSE) {
5!
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) {
5!
1051
        tqDebug("s-task:%s status:%p not continue scan-history data, total elapsed time:%.2fs quit", id, p.name,
5!
1052
                pTask->execInfo.step1El);
1053
      }
1054
    }
1055

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

1060
  // the following procedure should be executed, no matter status is stop/pause or not
1061
  if (taskType == STREAM_HISTORY_TASK) {
1,792!
1062
    tqDebug("s-task:%s scan-history(step 1) ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
1,792✔
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,792✔
1072
  if (pStreamTask == NULL) {
1,791!
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,791!
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,791!
1098
    code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
1,791✔
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,792✔
1109

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

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

1121
  SStreamTaskRunReq req = {0};
283,789✔
1122
  tDecoderInit(&decoder, (uint8_t*)msg, len);
283,789✔
1123
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
283,826!
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);
283,751✔
1130

1131
  // extracted submit data from wal files for all tasks
1132
  if (req.reqType == STREAM_EXEC_T_EXTRACT_WAL_DATA) {
283,580✔
1133
    return tqScanWal(pTq);
206,448✔
1134
  } else {
1135
    code = tqStreamTaskProcessRunReq(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
77,132✔
1136
    if (code) {
77,350✔
1137
      tqError("vgId:%d failed to create task run req, code:%s", TD_VID(pTq->pVnode), tstrerror(code));
17!
1138
    }
1139

1140
    return code;
77,351✔
1141
  }
1142
}
1143

1144
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
25,276✔
1145
  return tqStreamTaskProcessDispatchReq(pTq->pStreamMeta, pMsg, &pTq->pVnode->msgCb);
25,276✔
1146
}
1147

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

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

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

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

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

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

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

1176
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
386✔
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,
12✔
1223
                                 int32_t taskId) {
1224
  SRpcMsg rsp = {0};
12✔
1225
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
12✔
1226
  if (ret) {  // suppress the error in build checkpoint source rsp
12!
1227
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1228
  }
1229
  tmsgSendRsp(&rsp);  // error occurs
12✔
1230
}
12✔
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) {
2,280✔
1234
  int32_t                    vgId = TD_VID(pTq->pVnode);
2,280✔
1235
  SStreamMeta*               pMeta = pTq->pStreamMeta;
2,280✔
1236
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
2,280✔
1237
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
2,280✔
1238
  int32_t                    code = 0;
2,280✔
1239
  SStreamCheckpointSourceReq req = {0};
2,280✔
1240
  SDecoder                   decoder = {0};
2,280✔
1241
  SStreamTask*               pTask = NULL;
2,280✔
1242
  int64_t                    checkpointId = 0;
2,280✔
1243

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

1247
  tDecoderInit(&decoder, (uint8_t*)msg, len);
2,280✔
1248
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
2,277!
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);
2,273✔
1257

1258
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
2,279✔
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) {
2,272!
1266
    tqDebug("vgId:%d checkpoint-source msg received during restoring, checkpointId:%" PRId64
×
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);
×
1270
    return TSDB_CODE_SUCCESS;  // always return success to mnode
×
1271
  }
1272

1273
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
2,272✔
1274
  if (pTask == NULL || code != 0) {
2,272!
1275
    tqError("vgId:%d failed to find s-task:0x%x, ignore checkpoint msg. checkpointId:%" PRId64
×
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);
×
1279
    return TSDB_CODE_SUCCESS;
×
1280
  }
1281

1282
  if (pTask->status.downstreamReady != 1) {
2,272!
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);
2,272✔
1296
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,271✔
1297

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

1303
      streamMutexUnlock(&pTask->lock);
3✔
1304
      streamMetaReleaseTask(pMeta, pTask);
3✔
1305
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
3✔
1306
      return TSDB_CODE_SUCCESS;
3✔
1307
    }
1308
  } else {
1309
    if (status != TASK_STATUS__HALT) {
1,572!
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) {
2,261!
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) {
2,261!
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);
2,261✔
1341
  streamMutexUnlock(&pTask->lock);
2,269✔
1342

1343
  if (code) {
2,270!
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) {
2,270✔
1352
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64 ", transId:%d, ",
697!
1353
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId);
1354
  } else {
1355
    const char* pPrevStatus = streamTaskGetStatusStr(streamTaskGetPrevStatus(pTask));
1,573✔
1356
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64
1,574!
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);
2,273✔
1362
  if (code != TSDB_CODE_SUCCESS) {
2,271!
1363
    streamTaskSetCheckpointFailed(pTask);  // set the checkpoint failed
×
1364
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1365
  }
1366

1367
  streamMetaReleaseTask(pMeta, pTask);
2,271✔
1368
  return TSDB_CODE_SUCCESS;
2,272✔
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) {
5,260✔
1373
  int32_t vgId = TD_VID(pTq->pVnode);
5,260✔
1374

1375
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
5,260!
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);
5,261✔
1397
}
1398

1399
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
34✔
1400
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg,
68✔
1401
                                      pTq->pVnode->restored, (pTq->pStreamMeta->role == NODE_ROLE_LEADER));
34✔
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,766✔
1409
  return tqStreamTaskProcessAllTaskStopReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg);
4,766✔
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); }
19,940✔
1444

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

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

1453
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
4,348✔
1454
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
4,348✔
1455
}
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