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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

62.47
/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; }
3,517,243!
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
3,516,623✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
3,517,162✔
31

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

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

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
14,640✔
64
  if (pLeft == NULL || pRight == NULL) {
14,640!
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
28,886✔
68
         pLeft->val.version == pRight->val.version;
14,246✔
69
}
70

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

80
  pVnode->pTq = pTq;
10,684✔
81
  pTq->pVnode = pVnode;
10,684✔
82

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

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

94
  taosInitRWLatch(&pTq->lock);
10,685✔
95

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

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

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

113
  return tqInitialize(pTq);
10,686✔
114
}
115

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

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

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

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

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

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

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

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

171
  taosMemoryFree(pTq);
10,691!
172
}
173

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

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

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

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

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

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

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

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

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

249
  tDecoderClear(&decoder);
15,626✔
250

251
  STqOffset* pOffset = &vgOffset.offset;
15,619✔
252

253
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
15,619✔
254
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
456!
255
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
256
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
15,163!
257
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
15,163✔
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;
15,632✔
266
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
15,632✔
267
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
15,632✔
268
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
7!
269
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
270
    goto end;  // no need to update the offset value
7✔
271
  }
272

273
  // save the new offset value
274
  if (taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset))) {
15,625!
275
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
276
    return -1;
×
277
  }
278

279
  if (tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), msg,
15,625!
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;
15,624✔
286
end:
7✔
287
  tOffsetDestroy(&vgOffset.offset.val);
7✔
288
  return code;
7✔
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) {
25,179✔
370
  if (pTq == NULL) {
25,179!
371
    return TSDB_CODE_INVALID_PARA;
×
372
  }
373
  int32_t vgId = TD_VID(pTq->pVnode);
25,179✔
374
  taosWLockLatch(&pTq->lock);
25,179✔
375
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
25,179!
376
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
25,179✔
377

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

382
      if (pHandle->msg == NULL) {
27,292!
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,
27,292✔
388
                       .pCont = pHandle->msg->pCont,
27,292✔
389
                       .contLen = pHandle->msg->contLen,
27,292✔
390
                       .info = pHandle->msg->info};
27,292✔
391
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
27,292!
392
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
393
        }
394
        taosMemoryFree(pHandle->msg);
27,292!
395
        pHandle->msg = NULL;
27,292✔
396
      }
397

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

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

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

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

443
    // 2. check rebalance status
444
    if (pHandle->consumerId != consumerId) {
3,514,647✔
445
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
10!
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;
10✔
449
      taosWUnLockLatch(&pTq->lock);
10✔
450
      code = -1;
10✔
451
      goto END;
10✔
452
    }
453

454
    bool exec = tqIsHandleExec(pHandle);
3,514,637!
455
    if (!exec) {
3,514,637!
456
      tqSetHandleExec(pHandle);
3,516,623!
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,
3,516,623!
459
              req.subKey, pHandle);
460
      taosWUnLockLatch(&pTq->lock);
3,517,164✔
461
      break;
3,517,326✔
462
    }
463
    taosWUnLockLatch(&pTq->lock);
×
464

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

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

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

483
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
3,517,329✔
484
  tqSetHandleIdle(pHandle);
3,517,162✔
485

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

489
END:
14✔
490
  tDestroySMqPollReq(&req);
3,517,339✔
491
  return code;
3,517,316✔
492
}
493

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

501
  SMqVgOffset vgOffset = {0};
2✔
502

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

510
  tDecoderClear(&decoder);
2✔
511

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

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

524
  void* buf = rpcMallocCont(len);
1✔
525
  if (buf == NULL) {
1!
526
    return terrno;
×
527
  }
528
  SEncoder encoder = {0};
1✔
529
  tEncoderInit(&encoder, buf, len);
1✔
530
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
1✔
531
  tEncoderClear(&encoder);
1✔
532
  if (code < 0) {
1!
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};
1✔
538

539
  tmsgSendRsp(&rsp);
1✔
540
  return 0;
1✔
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) {
905✔
632
  if (pTq == NULL || msg == NULL) {
905!
633
    return TSDB_CODE_INVALID_PARA;
×
634
  }
635
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
906✔
636
  int32_t        vgId = TD_VID(pTq->pVnode);
906✔
637

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

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

647
      if (exec) {
904!
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);
904✔
655
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
904✔
656
      if (code != 0) {
903!
657
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
658
      }
659
      taosWUnLockLatch(&pTq->lock);
903✔
660
      break;
904✔
661
    }
662
  }
663

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

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

677
  return 0;
907✔
678
}
679

680
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
115✔
681
  if (pTq == NULL || msg == NULL) {
115!
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) {
115!
687
    return code;
×
688
  }
689

690
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
115✔
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,135✔
710
  if (pTq == NULL || msg == NULL) {
3,135!
711
    return TSDB_CODE_INVALID_PARA;
×
712
  }
713
  int         ret = 0;
3,138✔
714
  SMqRebVgReq req = {0};
3,138✔
715
  SDecoder    dc = {0};
3,138✔
716

717
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
3,138✔
718
  ret = tDecodeSMqRebVgReq(&dc, &req);
3,136✔
719
  if (ret < 0) {
3,136!
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,136!
724
         req.oldConsumerId, req.newConsumerId);
725

726
  taosRLockLatch(&pTq->lock);
3,141✔
727
  STqHandle* pHandle = NULL;
3,142✔
728
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
3,142✔
729
  if (code != 0){
3,140✔
730
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one", pTq->pVnode->config.vgId, req.subKey);
1,438!
731
  }
732
  taosRUnLockLatch(&pTq->lock);
3,141✔
733
  if (pHandle == NULL) {
3,142✔
734
    if (req.oldConsumerId != -1) {
1,440✔
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,440✔
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,439✔
744
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,439✔
745
    if (ret < 0) {
1,438!
746
      tqDestroyTqHandle(&handle);
×
747
      goto end;
×
748
    }
749
    taosWLockLatch(&pTq->lock);
1,438✔
750
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,439✔
751
    taosWUnLockLatch(&pTq->lock);
1,439✔
752
  } else {
753
    while (1) {
×
754
      taosWLockLatch(&pTq->lock);
1,702✔
755
      bool exec = tqIsHandleExec(pHandle);
1,702!
756
      if (exec) {
1,702!
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,702✔
764
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
104!
765
      } else {
766
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,598!
767
               req.newConsumerId);
768

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

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

784
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
28,860!
785

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

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

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

798
  pTask->pBackend = NULL;
11,399✔
799

800
  // sink
801
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
11,399✔
802
  if (pOutputInfo->type == TASK_OUTPUT__SMA) {
11,399✔
803
    pOutputInfo->smaSink.vnode = pTq->pVnode;
58✔
804
    pOutputInfo->smaSink.smaSink = smaHandleRes;
58✔
805
  } else if (pOutputInfo->type == TASK_OUTPUT__TABLE) {
11,341✔
806
    pOutputInfo->tbSink.vnode = pTq->pVnode;
5,600✔
807
    pOutputInfo->tbSink.tbSinkFunc = tqSinkDataIntoDstTable;
5,600✔
808

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

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

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

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

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

841
  streamTaskResetUpstreamStageInfo(pTask);
11,398✔
842

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

846
  char*       p = streamTaskGetStatus(pTask).name;
11,396✔
847
  const char* pNext = streamTaskGetStatusStr(pTask->status.taskStatus);
11,397✔
848

849
  if (pTask->info.fillHistory) {
11,396✔
850
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
3,917!
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
7,479!
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) {
7,486!
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;
11,404✔
874
}
875

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

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

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

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

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

897
  if (done) {
1,887✔
898
    qDebug("s-task:%s scan wal(step 2) verRange:%" PRId64 "-%" PRId64 " ended, elapsed time:%.2fs", id,
891✔
899
           pStep2Range->minVer, pStep2Range->maxVer, 0.0);
900
    int32_t code = streamTaskPutTranstateIntoInputQ(pTask);  // todo: msg lost.
891✔
901
    if (code) {
891!
902
      qError("s-task:%s failed put trans-state into inputQ, code:%s", id, tstrerror(code));
×
903
    }
904
    (void)streamExecTask(pTask);  // exec directly
891✔
905
  } else {
906
    STimeWindow* pWindow = &pTask->dataRange.window;
996✔
907
    tqDebug("s-task:%s level:%d verRange:%" PRId64 "-%" PRId64 " window:%" PRId64 "-%" PRId64
996✔
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) {
996!
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);
996✔
916
    if (code) {
996!
917
      tqError("s-task:%s level:%d failed to set step2 param", id, pTask->info.taskLevel);
×
918
    }
919

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

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

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

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

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

943
  SStreamMeta* pMeta = pStreamTask->pMeta;
1,887✔
944
  STaskId      hId = pStreamTask->hTaskInfo.id;
1,887✔
945
  SStreamTask* pTask = NULL;
1,887✔
946
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
1,887✔
947
  if (pTask == NULL) {
1,887!
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);
1,887✔
953

954
  streamMetaReleaseTask(pMeta, pTask);
1,887✔
955
  return TSDB_CODE_SUCCESS;
1,887✔
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,004✔
960
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
2,004✔
961
  SStreamMeta*           pMeta = pTq->pStreamMeta;
2,004✔
962
  int32_t                code = TSDB_CODE_SUCCESS;
2,004✔
963
  SStreamTask*           pTask = NULL;
2,004✔
964
  SStreamTask*           pStreamTask = NULL;
2,004✔
965
  char*                  pStatus = NULL;
2,004✔
966

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

974
  // do recovery step1
975
  const char* id = pTask->id.idStr;
2,004✔
976
  streamMutexLock(&pTask->lock);
2,004✔
977

978
  SStreamTaskState s = streamTaskGetStatus(pTask);
2,004✔
979
  pStatus = s.name;
2,004✔
980

981
  if ((s.state != TASK_STATUS__SCAN_HISTORY) || (pTask->status.downstreamReady == 0)) {
2,004!
982
    tqError("s-task:%s vgId:%d status:%s downstreamReady:%d not allowed/ready for scan-history data, quit", id,
×
983
            pMeta->vgId, s.name, pTask->status.downstreamReady);
984

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

990
  if (pTask->exec.pExecutor == NULL) {
2,004!
991
    tqError("s-task:%s vgId:%d executor is null, not executor scan history", id, pMeta->vgId);
×
992

993
    streamMutexUnlock(&pTask->lock);
×
994
    streamMetaReleaseTask(pMeta, pTask);
×
995
    return 0;
×
996
  }
997

998
  streamMutexUnlock(&pTask->lock);
2,004✔
999

1000
  // avoid multi-thread exec
1001
  while (1) {
×
1002
    int32_t sentinel = atomic_val_compare_exchange_32(&pTask->status.inScanHistorySentinel, 0, 1);
2,004✔
1003
    if (sentinel != 0) {
2,004!
1004
      tqDebug("s-task:%s already in scan-history func, wait for 100ms, and try again", id);
×
1005
      taosMsleep(100);
×
1006
    } else {
1007
      break;
2,004✔
1008
    }
1009
  }
1010

1011
  // let's decide which step should be executed now
1012
  if (pTask->execInfo.step1Start == 0) {
2,004✔
1013
    int64_t ts = taosGetTimestampMs();
1,890✔
1014
    pTask->execInfo.step1Start = ts;
1,890✔
1015
    tqDebug("s-task:%s start scan-history stage(step 1), status:%s, step1 startTs:%" PRId64, id, pStatus, ts);
1,890✔
1016
  } else {
1017
    if (pTask->execInfo.step2Start == 0) {
114!
1018
      tqDebug("s-task:%s continue exec scan-history(step1), original step1 startTs:%" PRId64 ", already elapsed:%.2fs",
114!
1019
              id, pTask->execInfo.step1Start, pTask->execInfo.step1El);
1020
    } else {
1021
      tqDebug("s-task:%s already in step2, no need to scan-history data, step2 startTs:%" PRId64, id,
×
1022
              pTask->execInfo.step2Start);
1023

1024
      atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1025
      streamMetaReleaseTask(pMeta, pTask);
×
1026
      return 0;
×
1027
    }
1028
  }
1029

1030
  // we have to continue retrying to successfully execute the scan history task.
1031
  if (!streamTaskSetSchedStatusWait(pTask)) {
2,004!
1032
    tqError(
×
1033
        "s-task:%s failed to start scan-history in first stream time window since already started, unexpected "
1034
        "sched-status:%d",
1035
        id, pTask->status.schedStatus);
1036
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1037
    streamMetaReleaseTask(pMeta, pTask);
×
1038
    return 0;
×
1039
  }
1040

1041
  int64_t              st = taosGetTimestampMs();
2,004✔
1042
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
2,004✔
1043

1044
  double el = (taosGetTimestampMs() - st) / 1000.0;
2,004✔
1045
  pTask->execInfo.step1El += el;
2,004✔
1046

1047
  if (retInfo.ret == TASK_SCANHISTORY_QUIT || retInfo.ret == TASK_SCANHISTORY_REXEC) {
2,004✔
1048
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
115✔
1049
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
115✔
1050

1051
    if (retInfo.ret == TASK_SCANHISTORY_REXEC) {
115✔
1052
      streamExecScanHistoryInFuture(pTask, retInfo.idleTime);
114✔
1053
    } else {
1054
      SStreamTaskState p = streamTaskGetStatus(pTask);
1✔
1055
      ETaskStatus      s = p.state;
1✔
1056

1057
      if (s == TASK_STATUS__PAUSE) {
1!
1058
        tqDebug("s-task:%s is paused in the step1, elapsed time:%.2fs total:%.2fs, sched-status:%d", id, el,
×
1059
                pTask->execInfo.step1El, status);
1060
      } else if (s == TASK_STATUS__STOP || s == TASK_STATUS__DROPPING) {
1!
1061
        tqDebug("s-task:%s status:%p not continue scan-history data, total elapsed time:%.2fs quit", id, p.name,
1!
1062
                pTask->execInfo.step1El);
1063
      }
1064
    }
1065

1066
    streamMetaReleaseTask(pMeta, pTask);
115✔
1067
    return 0;
115✔
1068
  }
1069

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

1073
  if (pTask->info.fillHistory != 1) {
1,889!
1074
    tqError("s-task:%s fill-history is disabled, unexpected", id);
×
1075
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1076
  }
1077

1078
  // 1. get the related stream task
1079
  code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,889✔
1080
  if (pStreamTask == NULL) {
1,889✔
1081
    tqError("failed to find s-task:0x%" PRIx64 ", it may have been destroyed, drop related fill-history task:%s",
2!
1082
            pTask->streamTaskId.taskId, pTask->id.idStr);
1083

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

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

1092
  if (pStreamTask->info.taskLevel != TASK_LEVEL__SOURCE) {
1,887!
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
  code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
1,887✔
1098
  streamMetaReleaseTask(pMeta, pStreamTask);
1,887✔
1099

1100
  atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
1,887✔
1101
  streamMetaReleaseTask(pMeta, pTask);
1,887✔
1102
  return code;
1,887✔
1103
}
1104

1105
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
143,983✔
1106
  int32_t  code = 0;
143,983✔
1107
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
143,983✔
1108
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
143,983✔
1109
  SDecoder decoder;
1110

1111
  SStreamTaskRunReq req = {0};
143,983✔
1112
  tDecoderInit(&decoder, (uint8_t*)msg, len);
143,983✔
1113
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
144,065!
1114
    tqError("vgId:%d failed to decode task run req, code:%s", pTq->pStreamMeta->vgId, tstrerror(code));
×
1115
    tDecoderClear(&decoder);
×
1116
    return TSDB_CODE_SUCCESS;
×
1117
  }
1118

1119
  tDecoderClear(&decoder);
144,054✔
1120

1121
  // extracted submit data from wal files for all tasks
1122
  if (req.reqType == STREAM_EXEC_T_EXTRACT_WAL_DATA) {
144,051✔
1123
    return tqScanWal(pTq);
72,242✔
1124
  } else {
1125
    code = tqStreamTaskProcessRunReq(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
71,809✔
1126
    if (code) {
71,820✔
1127
      tqError("vgId:%d failed to create task run req, code:%s", TD_VID(pTq->pVnode), tstrerror(code));
4!
1128
    }
1129

1130
    return code;
71,819✔
1131
  }
1132
}
1133

1134
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
50,090✔
1135
  return tqStreamTaskProcessDispatchReq(pTq->pStreamMeta, pMsg);
50,090✔
1136
}
1137

1138
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
50,025✔
1139
  return tqStreamTaskProcessDispatchRsp(pTq->pStreamMeta, pMsg);
50,025✔
1140
}
1141

1142
int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
5,076✔
1143
  return tqStreamTaskProcessDropReq(pTq->pStreamMeta, msg, msgLen);
5,076✔
1144
}
1145

1146
int32_t tqProcessTaskUpdateCheckpointReq(STQ* pTq, char* msg, int32_t msgLen) {
2,759✔
1147
  return tqStreamTaskProcessUpdateCheckpointReq(pTq->pStreamMeta, pTq->pVnode->restored, msg);
2,759✔
1148
}
1149

1150
int32_t tqProcessTaskConsenChkptIdReq(STQ* pTq, SRpcMsg* pMsg) {
155✔
1151
  return tqStreamTaskProcessConsenChkptIdReq(pTq->pStreamMeta, pMsg);
155✔
1152
}
1153

1154
int32_t tqProcessTaskPauseReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
805✔
1155
  return tqStreamTaskProcessTaskPauseReq(pTq->pStreamMeta, msg);
805✔
1156
}
1157

1158
int32_t tqProcessTaskResumeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,470✔
1159
  return tqStreamTaskProcessTaskResumeReq(pTq, sversion, msg, true);
1,470✔
1160
}
1161

1162
int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
454✔
1163
  return tqStreamTaskProcessRetrieveReq(pTq->pStreamMeta, pMsg);
454✔
1164
}
1165

1166
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
418✔
1167

1168
int32_t tqStreamProgressRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
×
1169
  char*               msgStr = pMsg->pCont;
×
1170
  char*               msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
1171
  int32_t             msgLen = pMsg->contLen - sizeof(SMsgHead);
×
1172
  int32_t             code = 0;
×
1173
  SStreamProgressReq  req;
1174
  char*               pRspBuf = taosMemoryCalloc(1, sizeof(SMsgHead) + sizeof(SStreamProgressRsp));
×
1175
  SStreamProgressRsp* pRsp = POINTER_SHIFT(pRspBuf, sizeof(SMsgHead));
×
1176
  if (!pRspBuf) {
×
1177
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1178
    code = -1;
×
1179
    goto _OVER;
×
1180
  }
1181

1182
  code = tDeserializeStreamProgressReq(msgBody, msgLen, &req);
×
1183
  if (code == TSDB_CODE_SUCCESS) {
×
1184
    code = tqGetStreamExecInfo(pTq->pVnode, req.streamId, &pRsp->progressDelay, &pRsp->fillHisFinished);
×
1185
  }
1186
  if (code == TSDB_CODE_SUCCESS) {
×
1187
    pRsp->fetchIdx = req.fetchIdx;
×
1188
    pRsp->subFetchIdx = req.subFetchIdx;
×
1189
    pRsp->vgId = req.vgId;
×
1190
    pRsp->streamId = req.streamId;
×
1191
    code = tSerializeStreamProgressRsp(pRsp, sizeof(SStreamProgressRsp) + sizeof(SMsgHead), pRsp);
×
1192
    if (code) {
×
1193
      goto _OVER;
×
1194
    }
1195

1196
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
1197
    rsp.pCont = pRspBuf;
×
1198
    pRspBuf = NULL;
×
1199
    rsp.contLen = sizeof(SMsgHead) + sizeof(SStreamProgressRsp);
×
1200
    tmsgSendRsp(&rsp);
×
1201
  }
1202

1203
_OVER:
×
1204
  if (pRspBuf) {
×
1205
    taosMemoryFree(pRspBuf);
×
1206
  }
1207
  return code;
×
1208
}
1209

1210
// always return success to mnode
1211
//todo: handle failure of build and send msg to mnode
1212
static void doSendChkptSourceRsp(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo, int32_t code,
23✔
1213
                                 int32_t taskId) {
1214
  SRpcMsg rsp = {0};
23✔
1215
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
23✔
1216
  if (ret) {  // suppress the error in build checkpoint source rsp
23!
1217
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1218
  }
1219
  tmsgSendRsp(&rsp);  // error occurs
23✔
1220
}
23✔
1221

1222
// no matter what kinds of error happened, make sure the mnode will receive the success execution code.
1223
int32_t tqProcessTaskCheckPointSourceReq(STQ* pTq, SRpcMsg* pMsg, SRpcMsg* pRsp) {
1,815✔
1224
  int32_t                    vgId = TD_VID(pTq->pVnode);
1,815✔
1225
  SStreamMeta*               pMeta = pTq->pStreamMeta;
1,815✔
1226
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1,815✔
1227
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
1,815✔
1228
  int32_t                    code = 0;
1,815✔
1229
  SStreamCheckpointSourceReq req = {0};
1,815✔
1230
  SDecoder                   decoder = {0};
1,815✔
1231
  SStreamTask*               pTask = NULL;
1,815✔
1232
  int64_t                    checkpointId = 0;
1,815✔
1233

1234
  // disable auto rsp to mnode
1235
  pRsp->info.handle = NULL;
1,815✔
1236

1237
  tDecoderInit(&decoder, (uint8_t*)msg, len);
1,815✔
1238
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
1,814!
1239
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
1240
    tDecoderClear(&decoder);
×
1241
    tqError("vgId:%d failed to decode checkpoint-source msg, code:%s", vgId, tstrerror(code));
×
1242
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1243
    return TSDB_CODE_SUCCESS;  // always return success to mnode,
×
1244
  }
1245

1246
  tDecoderClear(&decoder);
1,818✔
1247

1248
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
1,817✔
1249
    tqDebug("vgId:%d not leader, ignore checkpoint-source msg, s-task:0x%x", vgId, req.taskId);
15!
1250
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
15✔
1251
    return TSDB_CODE_SUCCESS;  // always return success to mnode
15✔
1252
  }
1253

1254
  if (!pTq->pVnode->restored) {
1,804✔
1255
    tqDebug("vgId:%d checkpoint-source msg received during restoring, checkpointId:%" PRId64
8!
1256
            ", transId:%d s-task:0x%x ignore it",
1257
            vgId, req.checkpointId, req.transId, req.taskId);
1258
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
8✔
1259
    return TSDB_CODE_SUCCESS;  // always return success to mnode
8✔
1260
  }
1261

1262
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
1,796✔
1263
  if (pTask == NULL || code != 0) {
1,797!
1264
    tqError("vgId:%d failed to find s-task:0x%x, ignore checkpoint msg. checkpointId:%" PRId64
3!
1265
            " transId:%d it may have been destroyed",
1266
            vgId, req.taskId, req.checkpointId, req.transId);
1267
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
3✔
1268
    return TSDB_CODE_SUCCESS;
×
1269
  }
1270

1271
  if (pTask->status.downstreamReady != 1) {
1,794!
1272
    // record the latest failed checkpoint id
1273
    streamTaskSetFailedChkptInfo(pTask, req.transId, req.checkpointId);
×
1274
    tqError("s-task:%s not ready for checkpoint, since downstream not ready, ignore this checkpointId:%" PRId64
×
1275
            ", transId:%d set it failed",
1276
            pTask->id.idStr, req.checkpointId, req.transId);
1277

1278
    streamMetaReleaseTask(pMeta, pTask);
×
1279
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1280
    return TSDB_CODE_SUCCESS;  // todo retry handle error
×
1281
  }
1282

1283
  // todo save the checkpoint failed info
1284
  streamMutexLock(&pTask->lock);
1,794✔
1285
  ETaskStatus status = streamTaskGetStatus(pTask).state;
1,796✔
1286

1287
  if (req.mndTrigger == 1) {
1,795✔
1288
    if (status == TASK_STATUS__HALT || status == TASK_STATUS__PAUSE) {
85!
1289
      tqError("s-task:%s not ready for checkpoint, since it is halt, ignore checkpointId:%" PRId64 ", set it failure",
×
1290
              pTask->id.idStr, req.checkpointId);
1291

1292
      streamMutexUnlock(&pTask->lock);
×
1293
      streamMetaReleaseTask(pMeta, pTask);
×
1294
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1295
      return TSDB_CODE_SUCCESS;
×
1296
    }
1297
  } else {
1298
    if (status != TASK_STATUS__HALT) {
1,710!
1299
      tqError("s-task:%s should in halt status, let's halt it directly", pTask->id.idStr);
×
1300
      //      streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_HALT);
1301
    }
1302
  }
1303

1304
  // check if the checkpoint msg already sent or not.
1305
  if (status == TASK_STATUS__CK) {
1,793!
1306
    streamTaskGetActiveCheckpointInfo(pTask, NULL, &checkpointId);
×
1307

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

1312
    streamMutexUnlock(&pTask->lock);
×
1313
    streamMetaReleaseTask(pMeta, pTask);
×
1314
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SYN_PROPOSE_NOT_READY, req.taskId);
×
1315
    return TSDB_CODE_SUCCESS;
×
1316
  } else {  // checkpoint already finished, and not in checkpoint status
1317
    if (req.checkpointId <= pTask->chkInfo.checkpointId) {
1,793!
1318
      tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1319
             " transId:%d already handled, return success",
1320
             pTask->id.idStr, req.checkpointId, req.transId);
1321

1322
      streamMutexUnlock(&pTask->lock);
×
1323
      streamMetaReleaseTask(pMeta, pTask);
×
1324
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1325
      return TSDB_CODE_SUCCESS;
×
1326
    }
1327
  }
1328

1329
  code = streamProcessCheckpointSourceReq(pTask, &req);
1,793✔
1330
  streamMutexUnlock(&pTask->lock);
1,796✔
1331

1332
  if (code) {
1,795!
1333
    qError("s-task:%s (vgId:%d) failed to process checkpoint-source req, code:%s", pTask->id.idStr, vgId,
×
1334
           tstrerror(code));
1335
    streamMetaReleaseTask(pMeta, pTask);
×
1336
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1337
    return TSDB_CODE_SUCCESS;
×
1338
  }
1339

1340
  if (req.mndTrigger) {
1,795✔
1341
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64 ", transId:%d, ",
85!
1342
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId);
1343
  } else {
1344
    const char* pPrevStatus = streamTaskGetStatusStr(streamTaskGetPrevStatus(pTask));
1,710✔
1345
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64
1,712!
1346
           ", transId:%d after transfer-state, prev status:%s",
1347
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId, pPrevStatus);
1348
  }
1349

1350
  code = streamAddCheckpointSourceRspMsg(&req, &pMsg->info, pTask);
1,799✔
1351
  if (code != TSDB_CODE_SUCCESS) {
1,798!
1352
    streamTaskSetCheckpointFailed(pTask);  // set the checkpoint failed
×
1353
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1354
  }
1355

1356
  streamMetaReleaseTask(pMeta, pTask);
1,798✔
1357
  return TSDB_CODE_SUCCESS;
1,798✔
1358
}
1359

1360
// downstream task has complete the stream task checkpoint procedure, let's start the handle the rsp by execute task
1361
int32_t tqProcessTaskCheckpointReadyMsg(STQ* pTq, SRpcMsg* pMsg) {
5,418✔
1362
  int32_t vgId = TD_VID(pTq->pVnode);
5,418✔
1363

1364
  SStreamCheckpointReadyMsg* pReq = (SStreamCheckpointReadyMsg*)pMsg->pCont;
5,418✔
1365
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
5,418!
1366
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from 0x%x", vgId,
×
1367
            (int32_t)pReq->downstreamTaskId);
1368
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1369
  }
1370

1371
  return tqStreamTaskProcessCheckpointReadyMsg(pTq->pStreamMeta, pMsg);
5,418✔
1372
}
1373

1374
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
50✔
1375
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg,
100✔
1376
                                      pTq->pVnode->restored, (pTq->pStreamMeta->role == NODE_ROLE_LEADER));
50✔
1377
}
1378

1379
int32_t tqProcessTaskResetReq(STQ* pTq, SRpcMsg* pMsg) {
×
1380
  return tqStreamTaskProcessTaskResetReq(pTq->pStreamMeta, pMsg->pCont);
×
1381
}
1382

1383
int32_t tqProcessAllTaskStopReq(STQ* pTq, SRpcMsg* pMsg) {
3,027✔
1384
  return tqStreamTaskProcessAllTaskStopReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg);
3,027✔
1385
}
1386

1387
int32_t tqProcessTaskRetrieveTriggerReq(STQ* pTq, SRpcMsg* pMsg) {
×
1388
  int32_t vgId = TD_VID(pTq->pVnode);
×
1389

1390
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1391
    SRetrieveChkptTriggerReq req = {0};
×
1392

1393
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1394
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1395
    SDecoder decoder = {0};
×
1396

1397
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1398
    if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1399
      tDecoderClear(&decoder);
×
1400
      tqError("vgId:%d invalid retrieve checkpoint-trigger req received", vgId);
×
1401
      return TSDB_CODE_INVALID_MSG;
×
1402
    }
1403
    tDecoderClear(&decoder);
×
1404

1405
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from s-task:0x%" PRId64, vgId,
×
1406
            req.downstreamTaskId);
1407
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1408
  }
1409

1410
  return tqStreamTaskProcessRetrieveTriggerReq(pTq->pStreamMeta, pMsg);
×
1411
}
1412

1413
int32_t tqProcessTaskRetrieveTriggerRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1414
  return tqStreamTaskProcessRetrieveTriggerRsp(pTq->pStreamMeta, pMsg);
×
1415
}
1416

1417
// this function is needed, do not try to remove it.
1418
int32_t tqProcessStreamHbRsp(STQ* pTq, SRpcMsg* pMsg) { return tqStreamProcessStreamHbRsp(pTq->pStreamMeta, pMsg); }
6,296✔
1419

1420
int32_t tqProcessStreamReqCheckpointRsp(STQ* pTq, SRpcMsg* pMsg) {
3,485✔
1421
  return tqStreamProcessReqCheckpointRsp(pTq->pStreamMeta, pMsg);
3,485✔
1422
}
1423

1424
int32_t tqProcessTaskCheckpointReadyRsp(STQ* pTq, SRpcMsg* pMsg) {
5,415✔
1425
  return tqStreamProcessCheckpointReadyRsp(pTq->pStreamMeta, pMsg);
5,415✔
1426
}
1427

1428
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
3,490✔
1429
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
3,490✔
1430
}
1431

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