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

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

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

6.31
/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; }
×
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
×
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
×
31

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

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

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

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

80
  pVnode->pTq = pTq;
21✔
81
  pTq->pVnode = pVnode;
21✔
82

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

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

94
  taosInitRWLatch(&pTq->lock);
21✔
95

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

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

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

113
  return tqInitialize(pTq);
21✔
114
}
115

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

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

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

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

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

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

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

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

171
  taosMemoryFree(pTq);
22!
172
}
173

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

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

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

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

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

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

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

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

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

249
  tDecoderClear(&decoder);
×
250

251
  STqOffset* pOffset = &vgOffset.offset;
×
252

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

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

279
  if (tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), msg,
×
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;
×
286
end:
×
287
  tOffsetDestroy(&vgOffset.offset.val);
×
288
  return code;
×
289
}
290

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

300
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
×
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);
×
306
  taosWLockLatch(&pTq->lock);
×
307

308
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
×
309
  if (pHandle == NULL) {
×
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) {
×
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);
×
328
  taosWUnLockLatch(&pTq->lock);
×
329

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

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

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

348
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
×
349

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

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

366
  return 0;
×
367
}
368

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

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

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

398
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
×
399
    }
400

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

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

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

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

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

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

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

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

483
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
×
484
  tqSetHandleIdle(pHandle);
×
485

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

489
END:
×
490
  tDestroySMqPollReq(&req);
×
491
  return code;
×
492
}
493

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

501
  SMqVgOffset vgOffset = {0};
×
502

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

510
  tDecoderClear(&decoder);
×
511

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

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

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

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

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

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

558
  // 1. find handle
559
  taosRLockLatch(&pTq->lock);
×
560
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
×
561
  if (pHandle == NULL) {
×
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) {
×
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;
×
576
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
577
  taosRUnLockLatch(&pTq->lock);
×
578

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

585
  if (req.useSnapshot == true) {
×
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;
×
592

593
  if (reqOffset.type == TMQ_OFFSET__LOG) {
×
594
    dataRsp.rspOffset.version = reqOffset.version;
×
595
  } else if (reqOffset.type < 0) {
×
596
    STqOffset* pOffset = NULL;
×
597
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
×
598
    if (code == 0) {
×
599
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
×
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;
×
606
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
×
607
             req.subKey, dataRsp.rspOffset.version);
608
    } else {
609
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
×
610
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
×
611
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
612
        dataRsp.rspOffset.version = ever;
×
613
      }
614
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
×
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);
×
625

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

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

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

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

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

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

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

677
  return 0;
×
678
}
679

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

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

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

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

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

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

726
  taosRLockLatch(&pTq->lock);
×
727
  STqHandle* pHandle = NULL;
×
728
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
×
729
  if (code != 0){
×
730
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one", pTq->pVnode->config.vgId, req.subKey);
×
731
  }
732
  taosRUnLockLatch(&pTq->lock);
×
733
  if (pHandle == NULL) {
×
734
    if (req.oldConsumerId != -1) {
×
735
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
736
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
737
    }
738
    if (req.newConsumerId == -1) {
×
739
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
740
      ret = TSDB_CODE_INVALID_PARA;
×
741
      goto end;
×
742
    }
743
    STqHandle handle = {0};
×
744
    ret = tqMetaCreateHandle(pTq, &req, &handle);
×
745
    if (ret < 0) {
×
746
      tqDestroyTqHandle(&handle);
×
747
      goto end;
×
748
    }
749
    taosWLockLatch(&pTq->lock);
×
750
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
×
751
    taosWUnLockLatch(&pTq->lock);
×
752
  } else {
753
    while (1) {
×
754
      taosWLockLatch(&pTq->lock);
×
755
      bool exec = tqIsHandleExec(pHandle);
×
756
      if (exec) {
×
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
×
764
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
×
765
      } else {
766
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
×
767
               req.newConsumerId);
768

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

779
end:
×
780
  tDecoderClear(&dc);
×
781
  return ret;
×
782
}
783

784
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
×
785

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

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

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

798
  pTask->pBackend = NULL;
×
799

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

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

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

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

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

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

841
  streamTaskResetUpstreamStageInfo(pTask);
×
842

843
  pChkInfo = &pTask->chkInfo;
×
844
  tqSetRestoreVersionInfo(pTask);
×
845

846
  char*       p = streamTaskGetStatus(pTask).name;
×
847
  const char* pNext = streamTaskGetStatusStr(pTask->status.taskStatus);
×
848

849
  if (pTask->info.fillHistory) {
×
850
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
851
           " nextProcessVer:%" PRId64
852
           " child id:%d, level:%d, cur-status:%s, next-status:%s taskType:%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
×
859
           " nextProcessVer:%" PRId64
860
           " child id:%d, level:%d, cur-status:%s next-status:%s taskType:%d, related helper-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) {
×
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;
×
874
}
875

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

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

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

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

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

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

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

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

927
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
928

929
    // now the fill-history task starts to scan data from wal files.
930
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_SCANHIST_DONE);
×
931
  }
932
}
×
933

934
int32_t handleStep2Async(SStreamTask* pStreamTask, void* param) {
×
935
  STQ* pTq = param;
×
936

937
  SStreamMeta* pMeta = pStreamTask->pMeta;
×
938
  STaskId      hId = pStreamTask->hTaskInfo.id;
×
939
  SStreamTask* pTask = NULL;
×
940
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
×
941
  if (pTask == NULL) {
×
942
    tqWarn("s-task:0x%x failed to acquired it to exec step 2, scan wal quit", (int32_t)hId.taskId);
×
943
    return TSDB_CODE_SUCCESS;
×
944
  }
945

946
  doStartFillhistoryStep2(pTask, pStreamTask, pTq);
×
947

948
  streamMetaReleaseTask(pMeta, pTask);
×
949
  return TSDB_CODE_SUCCESS;
×
950
}
951

952
// this function should be executed by only one thread, so we set a sentinel to protect this function
953
int32_t tqProcessTaskScanHistory(STQ* pTq, SRpcMsg* pMsg) {
×
954
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
×
955
  SStreamMeta*           pMeta = pTq->pStreamMeta;
×
956
  int32_t                code = TSDB_CODE_SUCCESS;
×
957
  SStreamTask*           pTask = NULL;
×
958
  SStreamTask*           pStreamTask = NULL;
×
959
  char*                  pStatus = NULL;
×
960
  int32_t                taskType = 0;
×
961

962
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
963
  if (pTask == NULL) {
×
964
    tqError("vgId:%d failed to acquire stream task:0x%x during scan history data, task may have been destroyed",
×
965
            pMeta->vgId, pReq->taskId);
966
    return code;
×
967
  }
968

969
  // do recovery step1
970
  const char* id = pTask->id.idStr;
×
971
  streamMutexLock(&pTask->lock);
×
972

973
  SStreamTaskState s = streamTaskGetStatus(pTask);
×
974
  pStatus = s.name;
×
975
  taskType = pTask->info.fillHistory;
×
976

977
  if ((s.state != TASK_STATUS__SCAN_HISTORY && taskType == STREAM_HISTORY_TASK) ||
×
978
      (s.state != TASK_STATUS__READY && taskType == STREAM_RECALCUL_TASK) ||
×
979
      (pTask->status.downstreamReady == 0)) {
×
980
    tqError("s-task:%s vgId:%d status:%s downstreamReady:%d not allowed/ready for scan-history data, quit", id,
×
981
            pMeta->vgId, s.name, pTask->status.downstreamReady);
982

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

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

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

996
  streamMutexUnlock(&pTask->lock);
×
997

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

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

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

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

1039
  int64_t              st = taosGetTimestampMs();
×
1040
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
×
1041

1042
  double el = (taosGetTimestampMs() - st) / 1000.0;
×
1043
  pTask->execInfo.step1El += el;
×
1044

1045
  if (retInfo.ret == TASK_SCANHISTORY_QUIT || retInfo.ret == TASK_SCANHISTORY_REXEC) {
×
1046
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
1047
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1048

1049
    if (retInfo.ret == TASK_SCANHISTORY_REXEC) {
×
1050
      streamExecScanHistoryInFuture(pTask, retInfo.idleTime);
×
1051
    } else {
1052
      SStreamTaskState p = streamTaskGetStatus(pTask);
×
1053
      ETaskStatus      localStatus = p.state;
×
1054

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

1064
    streamMetaReleaseTask(pMeta, pTask);
×
1065
    return 0;
×
1066
  }
1067

1068
  // the following procedure should be executed, no matter status is stop/pause or not
1069
  if (taskType == STREAM_HISTORY_TASK) {
×
1070
    tqDebug("s-task:%s scan-history(step 1) ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
×
1071
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1072
    tqDebug("s-task:%s recalculate ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
×
1073
  } else {
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);
×
1080
  if (pStreamTask == NULL) {
×
1081

1082
    int32_t ret = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
1083
    if (ret == 0 && pStreamTask != NULL) {
×
1084
      tqWarn("s-task:0x%" PRIx64 " stopped, not ready for related task:%s scan-history work, do nothing",
×
1085
             pTask->streamTaskId.taskId, pTask->id.idStr);
1086
      streamMetaReleaseTask(pMeta, pStreamTask);
×
1087
    } else {
1088
      tqError("failed to find s-task:0x%" PRIx64 ", it may have been destroyed, drop related fill-history task:%s",
×
1089
              pTask->streamTaskId.taskId, pTask->id.idStr);
1090

1091
      tqDebug("s-task:%s fill-history task set status to be dropping", id);
×
1092
      code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
×
1093
    }
1094

1095
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1096
    streamMetaReleaseTask(pMeta, pTask);
×
1097
    return code;
×
1098
  }
1099

1100
  if (pStreamTask->info.taskLevel != TASK_LEVEL__SOURCE) {
×
1101
    tqError("s-task:%s fill-history task related stream task level:%d, unexpected", id, pStreamTask->info.taskLevel);
×
1102
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1103
  }
1104

1105
  if (taskType == STREAM_HISTORY_TASK) {
×
1106
    code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
×
1107
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1108
    // send recalculate end block
1109
    code = streamCreateAddRecalculateEndBlock(pStreamTask);
×
1110
    if (code) {
×
1111
      tqError("s-task:%s failed to create-add recalculate end block, code:%s", id, tstrerror(code));
×
1112
    }
1113
    streamTaskSetSchedStatusInactive(pTask);
×
1114
  }
1115

1116
  streamMetaReleaseTask(pMeta, pStreamTask);
×
1117

1118
  atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1119
  streamMetaReleaseTask(pMeta, pTask);
×
1120
  return code;
×
1121
}
1122

1123
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
21✔
1124
  int32_t  code = 0;
21✔
1125
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
21✔
1126
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
21✔
1127
  SDecoder decoder;
1128

1129
  SStreamTaskRunReq req = {0};
21✔
1130
  tDecoderInit(&decoder, (uint8_t*)msg, len);
21✔
1131
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
21!
1132
    tqError("vgId:%d failed to decode task run req, code:%s", pTq->pStreamMeta->vgId, tstrerror(code));
×
1133
    tDecoderClear(&decoder);
×
1134
    return TSDB_CODE_SUCCESS;
×
1135
  }
1136

1137
  tDecoderClear(&decoder);
21✔
1138

1139
  // extracted submit data from wal files for all tasks
1140
  if (req.reqType == STREAM_EXEC_T_EXTRACT_WAL_DATA) {
21!
1141
    return tqScanWal(pTq);
×
1142
  } else {
1143
    code = tqStreamTaskProcessRunReq(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
21✔
1144
    if (code) {
21!
1145
      tqError("vgId:%d failed to create task run req, code:%s", TD_VID(pTq->pVnode), tstrerror(code));
×
1146
    }
1147

1148
    return code;
21✔
1149
  }
1150
}
1151

1152
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
×
1153
  return tqStreamTaskProcessDispatchReq(pTq->pStreamMeta, pMsg);
×
1154
}
1155

1156
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1157
  return tqStreamTaskProcessDispatchRsp(pTq->pStreamMeta, pMsg);
×
1158
}
1159

1160
int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
×
1161
  return tqStreamTaskProcessDropReq(pTq->pStreamMeta, msg, msgLen);
×
1162
}
1163

1164
int32_t tqProcessTaskUpdateCheckpointReq(STQ* pTq, char* msg, int32_t msgLen) {
×
1165
  return tqStreamTaskProcessUpdateCheckpointReq(pTq->pStreamMeta, pTq->pVnode->restored, msg);
×
1166
}
1167

1168
int32_t tqProcessTaskConsenChkptIdReq(STQ* pTq, SRpcMsg* pMsg) {
×
1169
  return tqStreamTaskProcessConsenChkptIdReq(pTq->pStreamMeta, pMsg);
×
1170
}
1171

1172
int32_t tqProcessTaskPauseReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
×
1173
  return tqStreamTaskProcessTaskPauseReq(pTq->pStreamMeta, msg);
×
1174
}
1175

1176
int32_t tqProcessTaskResumeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
×
1177
  return tqStreamTaskProcessTaskResumeReq(pTq, sversion, msg, true);
×
1178
}
1179

1180
int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
×
1181
  return tqStreamTaskProcessRetrieveReq(pTq->pStreamMeta, pMsg);
×
1182
}
1183

1184
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
×
1185

1186
int32_t tqStreamProgressRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
×
1187
  char*               msgStr = pMsg->pCont;
×
1188
  char*               msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
1189
  int32_t             msgLen = pMsg->contLen - sizeof(SMsgHead);
×
1190
  int32_t             code = 0;
×
1191
  SStreamProgressReq  req;
1192
  char*               pRspBuf = taosMemoryCalloc(1, sizeof(SMsgHead) + sizeof(SStreamProgressRsp));
×
1193
  SStreamProgressRsp* pRsp = POINTER_SHIFT(pRspBuf, sizeof(SMsgHead));
×
1194
  if (!pRspBuf) {
×
1195
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1196
    code = -1;
×
1197
    goto _OVER;
×
1198
  }
1199

1200
  code = tDeserializeStreamProgressReq(msgBody, msgLen, &req);
×
1201
  if (code == TSDB_CODE_SUCCESS) {
×
1202
    code = tqGetStreamExecInfo(pTq->pVnode, req.streamId, &pRsp->progressDelay, &pRsp->fillHisFinished);
×
1203
  }
1204
  if (code == TSDB_CODE_SUCCESS) {
×
1205
    pRsp->fetchIdx = req.fetchIdx;
×
1206
    pRsp->subFetchIdx = req.subFetchIdx;
×
1207
    pRsp->vgId = req.vgId;
×
1208
    pRsp->streamId = req.streamId;
×
1209
    code = tSerializeStreamProgressRsp(pRsp, sizeof(SStreamProgressRsp) + sizeof(SMsgHead), pRsp);
×
1210
    if (code) {
×
1211
      goto _OVER;
×
1212
    }
1213

1214
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
1215
    rsp.pCont = pRspBuf;
×
1216
    pRspBuf = NULL;
×
1217
    rsp.contLen = sizeof(SMsgHead) + sizeof(SStreamProgressRsp);
×
1218
    tmsgSendRsp(&rsp);
×
1219
  }
1220

1221
_OVER:
×
1222
  if (pRspBuf) {
×
1223
    taosMemoryFree(pRspBuf);
×
1224
  }
1225
  return code;
×
1226
}
1227

1228
// always return success to mnode
1229
//todo: handle failure of build and send msg to mnode
1230
static void doSendChkptSourceRsp(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo, int32_t code,
×
1231
                                 int32_t taskId) {
1232
  SRpcMsg rsp = {0};
×
1233
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
×
1234
  if (ret) {  // suppress the error in build checkpoint source rsp
×
1235
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1236
  }
1237
  tmsgSendRsp(&rsp);  // error occurs
×
1238
}
×
1239

1240
// no matter what kinds of error happened, make sure the mnode will receive the success execution code.
1241
int32_t tqProcessTaskCheckPointSourceReq(STQ* pTq, SRpcMsg* pMsg, SRpcMsg* pRsp) {
×
1242
  int32_t                    vgId = TD_VID(pTq->pVnode);
×
1243
  SStreamMeta*               pMeta = pTq->pStreamMeta;
×
1244
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1245
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
×
1246
  int32_t                    code = 0;
×
1247
  SStreamCheckpointSourceReq req = {0};
×
1248
  SDecoder                   decoder = {0};
×
1249
  SStreamTask*               pTask = NULL;
×
1250
  int64_t                    checkpointId = 0;
×
1251

1252
  // disable auto rsp to mnode
1253
  pRsp->info.handle = NULL;
×
1254

1255
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1256
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
×
1257
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
1258
    tDecoderClear(&decoder);
×
1259
    tqError("vgId:%d failed to decode checkpoint-source msg, code:%s", vgId, tstrerror(code));
×
1260
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1261
    return TSDB_CODE_SUCCESS;  // always return success to mnode,
×
1262
  }
1263

1264
  tDecoderClear(&decoder);
×
1265

1266
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1267
    tqDebug("vgId:%d not leader, ignore checkpoint-source msg, s-task:0x%x", vgId, req.taskId);
×
1268
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1269
    return TSDB_CODE_SUCCESS;  // always return success to mnode
×
1270
  }
1271

1272
  if (!pTq->pVnode->restored) {
×
1273
    tqDebug("vgId:%d checkpoint-source msg received during restoring, checkpointId:%" PRId64
×
1274
            ", transId:%d s-task:0x%x ignore it",
1275
            vgId, req.checkpointId, req.transId, req.taskId);
1276
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1277
    return TSDB_CODE_SUCCESS;  // always return success to mnode
×
1278
  }
1279

1280
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
1281
  if (pTask == NULL || code != 0) {
×
1282
    tqError("vgId:%d failed to find s-task:0x%x, ignore checkpoint msg. checkpointId:%" PRId64
×
1283
            " transId:%d it may have been destroyed",
1284
            vgId, req.taskId, req.checkpointId, req.transId);
1285
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1286
    return TSDB_CODE_SUCCESS;
×
1287
  }
1288

1289
  if (pTask->status.downstreamReady != 1) {
×
1290
    // record the latest failed checkpoint id
1291
    streamTaskSetFailedChkptInfo(pTask, req.transId, req.checkpointId);
×
1292
    tqError("s-task:%s not ready for checkpoint, since downstream not ready, ignore this checkpointId:%" PRId64
×
1293
            ", transId:%d set it failed",
1294
            pTask->id.idStr, req.checkpointId, req.transId);
1295

1296
    streamMetaReleaseTask(pMeta, pTask);
×
1297
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1298
    return TSDB_CODE_SUCCESS;  // todo retry handle error
×
1299
  }
1300

1301
  // todo save the checkpoint failed info
1302
  streamMutexLock(&pTask->lock);
×
1303
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
1304

1305
  if (req.mndTrigger == 1) {
×
1306
    if (status == TASK_STATUS__HALT || status == TASK_STATUS__PAUSE) {
×
1307
      tqError("s-task:%s not ready for checkpoint, since it is halt, ignore checkpointId:%" PRId64 ", set it failure",
×
1308
              pTask->id.idStr, req.checkpointId);
1309

1310
      streamMutexUnlock(&pTask->lock);
×
1311
      streamMetaReleaseTask(pMeta, pTask);
×
1312
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1313
      return TSDB_CODE_SUCCESS;
×
1314
    }
1315
  } else {
1316
    if (status != TASK_STATUS__HALT) {
×
1317
      tqError("s-task:%s should in halt status, let's halt it directly", pTask->id.idStr);
×
1318
      //      streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_HALT);
1319
    }
1320
  }
1321

1322
  // check if the checkpoint msg already sent or not.
1323
  if (status == TASK_STATUS__CK) {
×
1324
    streamTaskGetActiveCheckpointInfo(pTask, NULL, &checkpointId);
×
1325

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

1330
    streamMutexUnlock(&pTask->lock);
×
1331
    streamMetaReleaseTask(pMeta, pTask);
×
1332
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SYN_PROPOSE_NOT_READY, req.taskId);
×
1333
    return TSDB_CODE_SUCCESS;
×
1334
  } else {  // checkpoint already finished, and not in checkpoint status
1335
    if (req.checkpointId <= pTask->chkInfo.checkpointId) {
×
1336
      tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1337
             " transId:%d already handled, return success",
1338
             pTask->id.idStr, req.checkpointId, req.transId);
1339

1340
      streamMutexUnlock(&pTask->lock);
×
1341
      streamMetaReleaseTask(pMeta, pTask);
×
1342
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1343
      return TSDB_CODE_SUCCESS;
×
1344
    }
1345
  }
1346

1347
  code = streamProcessCheckpointSourceReq(pTask, &req);
×
1348
  streamMutexUnlock(&pTask->lock);
×
1349

1350
  if (code) {
×
1351
    qError("s-task:%s (vgId:%d) failed to process checkpoint-source req, code:%s", pTask->id.idStr, vgId,
×
1352
           tstrerror(code));
1353
    streamMetaReleaseTask(pMeta, pTask);
×
1354
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1355
    return TSDB_CODE_SUCCESS;
×
1356
  }
1357

1358
  if (req.mndTrigger) {
×
1359
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64 ", transId:%d, ",
×
1360
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId);
1361
  } else {
1362
    const char* pPrevStatus = streamTaskGetStatusStr(streamTaskGetPrevStatus(pTask));
×
1363
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64
×
1364
           ", transId:%d after transfer-state, prev status:%s",
1365
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId, pPrevStatus);
1366
  }
1367

1368
  code = streamAddCheckpointSourceRspMsg(&req, &pMsg->info, pTask);
×
1369
  if (code != TSDB_CODE_SUCCESS) {
×
1370
    streamTaskSetCheckpointFailed(pTask);  // set the checkpoint failed
×
1371
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1372
  }
1373

1374
  streamMetaReleaseTask(pMeta, pTask);
×
1375
  return TSDB_CODE_SUCCESS;
×
1376
}
1377

1378
// downstream task has complete the stream task checkpoint procedure, let's start the handle the rsp by execute task
1379
int32_t tqProcessTaskCheckpointReadyMsg(STQ* pTq, SRpcMsg* pMsg) {
×
1380
  int32_t vgId = TD_VID(pTq->pVnode);
×
1381

1382
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1383
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1384
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1385
    int32_t  code = 0;
×
1386
    SDecoder decoder;
1387

1388
    SStreamCheckpointReadyMsg req = {0};
×
1389
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1390
    if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
×
1391
      code = TSDB_CODE_MSG_DECODE_ERROR;
×
1392
      tDecoderClear(&decoder);
×
1393
      return code;
×
1394
    }
1395
    tDecoderClear(&decoder);
×
1396

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

1400
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1401
  }
1402

1403
  return tqStreamTaskProcessCheckpointReadyMsg(pTq->pStreamMeta, pMsg);
×
1404
}
1405

1406
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
×
1407
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg,
×
1408
                                      pTq->pVnode->restored, (pTq->pStreamMeta->role == NODE_ROLE_LEADER));
×
1409
}
1410

1411
int32_t tqProcessTaskResetReq(STQ* pTq, SRpcMsg* pMsg) {
×
1412
  return tqStreamTaskProcessTaskResetReq(pTq->pStreamMeta, pMsg->pCont);
×
1413
}
1414

1415
int32_t tqProcessAllTaskStopReq(STQ* pTq, SRpcMsg* pMsg) {
16✔
1416
  return tqStreamTaskProcessAllTaskStopReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg);
16✔
1417
}
1418

1419
int32_t tqProcessTaskRetrieveTriggerReq(STQ* pTq, SRpcMsg* pMsg) {
×
1420
  int32_t vgId = TD_VID(pTq->pVnode);
×
1421

1422
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1423
    SRetrieveChkptTriggerReq req = {0};
×
1424

1425
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1426
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1427
    SDecoder decoder = {0};
×
1428

1429
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1430
    if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1431
      tDecoderClear(&decoder);
×
1432
      tqError("vgId:%d invalid retrieve checkpoint-trigger req received", vgId);
×
1433
      return TSDB_CODE_INVALID_MSG;
×
1434
    }
1435
    tDecoderClear(&decoder);
×
1436

1437
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from s-task:0x%" PRId64, vgId,
×
1438
            req.downstreamTaskId);
1439
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1440
  }
1441

1442
  return tqStreamTaskProcessRetrieveTriggerReq(pTq->pStreamMeta, pMsg);
×
1443
}
1444

1445
int32_t tqProcessTaskRetrieveTriggerRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1446
  return tqStreamTaskProcessRetrieveTriggerRsp(pTq->pStreamMeta, pMsg);
×
1447
}
1448

1449
// this function is needed, do not try to remove it.
1450
int32_t tqProcessStreamHbRsp(STQ* pTq, SRpcMsg* pMsg) { return tqStreamProcessStreamHbRsp(pTq->pStreamMeta, pMsg); }
×
1451

1452
int32_t tqProcessStreamReqCheckpointRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1453
  return tqStreamProcessReqCheckpointRsp(pTq->pStreamMeta, pMsg);
×
1454
}
1455

1456
int32_t tqProcessTaskCheckpointReadyRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1457
  return tqStreamProcessCheckpointReadyRsp(pTq->pStreamMeta, pMsg);
×
1458
}
1459

1460
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1461
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
×
1462
}
1463

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