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

taosdata / TDengine / #3535

23 Nov 2024 02:07AM UTC coverage: 60.85% (+0.03%) from 60.825%
#3535

push

travis-ci

web-flow
Merge pull request #28893 from taosdata/doc/internal

refact: rename taos lib name

120252 of 252737 branches covered (47.58%)

Branch coverage included in aggregate %.

201187 of 275508 relevant lines covered (73.02%)

15886166.19 hits per line

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

73.57
/source/libs/stream/src/streamDispatch.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 "streamInt.h"
17
#include "tmisce.h"
18
#include "trpc.h"
19
#include "ttimer.h"
20

21
typedef struct SBlockName {
22
  uint32_t hashValue;
23
  char     parTbName[TSDB_TABLE_NAME_LEN];
24
} SBlockName;
25

26
static void    doMonitorDispatchData(void* param, void* tmrId);
27
static int32_t doSendDispatchMsg(SStreamTask* pTask, const SStreamDispatchReq* pReq, int32_t vgId, SEpSet* pEpSet);
28
static int32_t streamAddBlockIntoDispatchMsg(const SSDataBlock* pBlock, SStreamDispatchReq* pReq);
29
static int32_t streamSearchAndAddBlock(SStreamTask* pTask, SStreamDispatchReq* pReqs, SSDataBlock* pDataBlock,
30
                                       int64_t groupId, int64_t now);
31
static int32_t tInitStreamDispatchReq(SStreamDispatchReq* pReq, const SStreamTask* pTask, int32_t vgId,
32
                                      int32_t numOfBlocks, int64_t dstTaskId, int32_t type);
33
static int32_t getFailedDispatchInfo(SDispatchMsgInfo* pMsgInfo, int64_t now);
34
static bool    isDispatchRspTimeout(SDispatchEntry* pEntry, int64_t now);
35
static void    addDispatchEntry(SDispatchMsgInfo* pMsgInfo, int32_t nodeId, int64_t now, bool lock);
36

37
void initRpcMsg(SRpcMsg* pMsg, int32_t msgType, void* pCont, int32_t contLen) {
129,949✔
38
  pMsg->msgType = msgType;
129,949✔
39
  pMsg->pCont = pCont;
129,949✔
40
  pMsg->contLen = contLen;
129,949✔
41
}
129,949✔
42

43
static void initDispatchInfo(SDispatchMsgInfo* pInfo, int32_t msgId) {
29,446✔
44
  pInfo->startTs = taosGetTimestampMs();
29,448✔
45
  pInfo->rspTs = -1;
29,448✔
46
  pInfo->msgId = msgId;
29,448✔
47
}
29,448✔
48

49
static void clearDispatchInfo(SDispatchMsgInfo* pInfo) {
29,449✔
50
  pInfo->startTs = -1;
29,449✔
51
  pInfo->msgId = -1;
29,449✔
52
  pInfo->rspTs = -1;
29,449✔
53
}
29,449✔
54

55
static void updateDispatchInfo(SDispatchMsgInfo* pInfo, int64_t recvTs) { pInfo->rspTs = recvTs; }
×
56

57
static int32_t tInitStreamDispatchReq(SStreamDispatchReq* pReq, const SStreamTask* pTask, int32_t vgId,
76,801✔
58
                                      int32_t numOfBlocks, int64_t dstTaskId, int32_t type) {
59
  pReq->streamId = pTask->id.streamId;
76,801✔
60
  pReq->srcVgId = vgId;
76,801✔
61
  pReq->stage = pTask->pMeta->stage;
76,801✔
62
  pReq->msgId = pTask->msgInfo.msgId;
76,801✔
63
  pReq->upstreamTaskId = pTask->id.taskId;
76,801✔
64
  pReq->upstreamChildId = pTask->info.selfChildId;
76,801✔
65
  pReq->upstreamNodeId = pTask->info.nodeId;
76,801✔
66
  pReq->upstreamRelTaskId = pTask->streamTaskId.taskId;
76,801✔
67
  pReq->blockNum = numOfBlocks;
76,801✔
68
  pReq->taskId = dstTaskId;
76,801✔
69
  pReq->type = type;
76,801✔
70

71
  pReq->data = taosArrayInit(numOfBlocks, POINTER_BYTES);
76,801✔
72
  pReq->dataLen = taosArrayInit(numOfBlocks, sizeof(int32_t));
76,812✔
73
  if (pReq->data == NULL || pReq->dataLen == NULL) {
76,812!
74
    taosArrayDestroyP(pReq->data, taosMemoryFree);
1✔
75
    taosArrayDestroy(pReq->dataLen);
×
76
    return terrno;
×
77
  }
78

79
  return TSDB_CODE_SUCCESS;
76,811✔
80
}
81

82
void streamTaskSendRetrieveRsp(SStreamRetrieveReq* pReq, SRpcMsg* pRsp) {
547✔
83
  void* buf = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamRetrieveRsp));
547✔
84
  ((SMsgHead*)buf)->vgId = htonl(pReq->srcNodeId);
547✔
85

86
  SStreamRetrieveRsp* pCont = POINTER_SHIFT(buf, sizeof(SMsgHead));
547✔
87
  pCont->streamId = pReq->streamId;
547✔
88
  pCont->rspToTaskId = pReq->srcTaskId;
547✔
89
  pCont->rspFromTaskId = pReq->dstTaskId;
547✔
90
  pRsp->pCont = buf;
547✔
91
  pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamRetrieveRsp);
547✔
92
  tmsgSendRsp(pRsp);
547✔
93
}
547✔
94

95
int32_t streamTaskBroadcastRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* req) {
171✔
96
  int32_t code = 0;
171✔
97
  void*   buf = NULL;
171✔
98
  int32_t sz = taosArrayGetSize(pTask->upstreamInfo.pList);
171✔
99
  for (int32_t i = 0; i < sz; i++) {
719✔
100
    req->reqId = tGenIdPI64();
548✔
101
    SStreamUpstreamEpInfo* pEpInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
548✔
102
    req->dstNodeId = pEpInfo->nodeId;
548✔
103
    req->dstTaskId = pEpInfo->taskId;
548✔
104
    int32_t len;
105

106
    tEncodeSize(tEncodeStreamRetrieveReq, req, len, code);
548!
107
    if (code != 0) {
548!
108
      return code;
×
109
    }
110

111
    buf = rpcMallocCont(sizeof(SMsgHead) + len);
548✔
112
    if (buf == NULL) {
548!
113
      return terrno;
×
114
    }
115

116
    ((SMsgHead*)buf)->vgId = htonl(pEpInfo->nodeId);
548✔
117
    void*    abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
548✔
118
    SEncoder encoder;
119
    tEncoderInit(&encoder, abuf, len);
548✔
120
    code = tEncodeStreamRetrieveReq(&encoder, req);
548✔
121
    tEncoderClear(&encoder);
548✔
122

123
    if (code < 0) {
548!
124
      stError("s-task:%s failed encode stream retrieve req, code:%s", pTask->id.idStr, tstrerror(code));
×
125
      rpcFreeCont(buf);
×
126
      return code;
×
127
    }
128

129
    SRpcMsg rpcMsg = {0};
548✔
130
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE, buf, len + sizeof(SMsgHead));
548✔
131

132
    code = tmsgSendReq(&pEpInfo->epSet, &rpcMsg);
548✔
133
    if (code != 0) {
548!
134
      rpcFreeCont(buf);
×
135
      return code;
×
136
    }
137

138
    stDebug("s-task:%s (child %d) send retrieve req to task:0x%x (vgId:%d),QID:0x%" PRIx64, pTask->id.idStr,
548✔
139
            pTask->info.selfChildId, pEpInfo->taskId, pEpInfo->nodeId, req->reqId);
140
  }
141

142
  return code;
171✔
143
}
144

145
static int32_t buildStreamRetrieveReq(SStreamTask* pTask, const SSDataBlock* pBlock, SStreamRetrieveReq* req) {
165✔
146
  SRetrieveTableRsp* pRetrieve = NULL;
165✔
147

148
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
165✔
149
  int32_t len = sizeof(SRetrieveTableRsp) + dataEncodeSize + PAYLOAD_PREFIX_LEN;
165✔
150

151
  pRetrieve = taosMemoryCalloc(1, len);
165✔
152
  if (pRetrieve == NULL) return terrno;
165!
153

154
  int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
165✔
155
  pRetrieve->useconds = 0;
165✔
156
  pRetrieve->precision = TSDB_DEFAULT_PRECISION;
165✔
157
  pRetrieve->compressed = 0;
165✔
158
  pRetrieve->completed = 1;
165✔
159
  pRetrieve->streamBlockType = pBlock->info.type;
165✔
160
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
165✔
161
  pRetrieve->numOfCols = htonl(numOfCols);
165✔
162
  pRetrieve->skey = htobe64(pBlock->info.window.skey);
165✔
163
  pRetrieve->ekey = htobe64(pBlock->info.window.ekey);
165✔
164
  pRetrieve->version = htobe64(pBlock->info.version);
165✔
165

166
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data + PAYLOAD_PREFIX_LEN, dataEncodeSize, numOfCols);
165✔
167
  if (actualLen < 0) {
165!
168
    taosMemoryFree(pRetrieve);
×
169
    return terrno;
×
170
  }
171
  SET_PAYLOAD_LEN(pRetrieve->data, actualLen, actualLen);
165✔
172

173
  int32_t payloadLen = actualLen + PAYLOAD_PREFIX_LEN;
165✔
174
  pRetrieve->payloadLen = htonl(payloadLen);
165✔
175
  pRetrieve->compLen = htonl(payloadLen);
165✔
176
  pRetrieve->compressed = 0;
165✔
177

178
  req->streamId = pTask->id.streamId;
165✔
179
  req->srcNodeId = pTask->info.nodeId;
165✔
180
  req->srcTaskId = pTask->id.taskId;
165✔
181
  req->pRetrieve = pRetrieve;
165✔
182
  req->retrieveLen = len;
165✔
183
  return 0;
165✔
184
}
185

186
int32_t streamBroadcastToUpTasks(SStreamTask* pTask, const SSDataBlock* pBlock) {
165✔
187
  SStreamRetrieveReq req;
188
  int32_t            code = buildStreamRetrieveReq(pTask, pBlock, &req);
165✔
189
  if (code != 0) {
165!
190
    return code;
×
191
  }
192

193
  code = streamTaskBroadcastRetrieveReq(pTask, &req);
165✔
194
  taosMemoryFree(req.pRetrieve);
165✔
195

196
  return code;
165✔
197
}
198

199
// no need to do anything if failed
200
int32_t streamSendCheckMsg(SStreamTask* pTask, const SStreamTaskCheckReq* pReq, int32_t nodeId, SEpSet* pEpSet) {
21,899✔
201
  void*   buf = NULL;
21,899✔
202
  int32_t code = 0;
21,899✔
203
  SRpcMsg msg = {0};
21,899✔
204

205
  int32_t tlen;
206
  tEncodeSize(tEncodeStreamTaskCheckReq, pReq, tlen, code);
21,899!
207
  if (code < 0) {
21,900!
208
    return code;
×
209
  }
210

211
  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
21,900✔
212
  if (buf == NULL) {
21,900!
213
    return terrno;
×
214
  }
215

216
  ((SMsgHead*)buf)->vgId = htonl(nodeId);
21,900✔
217
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
21,900✔
218

219
  SEncoder encoder;
220
  tEncoderInit(&encoder, abuf, tlen);
21,900✔
221
  if ((code = tEncodeStreamTaskCheckReq(&encoder, pReq)) < 0) {
21,899!
222
    rpcFreeCont(buf);
×
223
    tEncoderClear(&encoder);
×
224
    return code;
×
225
  }
226

227
  tEncoderClear(&encoder);
21,901✔
228
  initRpcMsg(&msg, TDMT_VND_STREAM_TASK_CHECK, buf, tlen + sizeof(SMsgHead));
21,901✔
229
  stDebug("s-task:%s (level:%d) send check msg to s-task:0x%" PRIx64 ":0x%x (vgId:%d)", pTask->id.idStr,
21,900✔
230
          pTask->info.taskLevel, pReq->streamId, pReq->downstreamTaskId, nodeId);
231

232
  return tmsgSendReq(pEpSet, &msg);
21,900✔
233
}
234

235
void destroyDispatchMsg(SStreamDispatchReq* pReq, int32_t numOfVgroups) {
29,447✔
236
  for (int32_t i = 0; i < numOfVgroups; i++) {
106,254✔
237
    taosArrayDestroyP(pReq[i].data, taosMemoryFree);
76,804✔
238
    taosArrayDestroy(pReq[i].dataLen);
76,810✔
239
  }
240

241
  taosMemoryFree(pReq);
29,450✔
242
}
29,449✔
243

244
void clearBufferedDispatchMsg(SStreamTask* pTask) {
29,446✔
245
  SDispatchMsgInfo* pMsgInfo = &pTask->msgInfo;
29,446✔
246
  if (pMsgInfo->pData != NULL) {
29,446!
247
    destroyDispatchMsg(pMsgInfo->pData, streamTaskGetNumOfDownstream(pTask));
29,447✔
248
  }
249

250
  streamMutexLock(&pMsgInfo->lock);
29,448✔
251

252
  pMsgInfo->checkpointId = -1;
29,451✔
253
  pMsgInfo->transId = -1;
29,451✔
254
  pMsgInfo->pData = NULL;
29,451✔
255
  pMsgInfo->dispatchMsgType = 0;
29,451✔
256

257
  clearDispatchInfo(pMsgInfo);
29,451✔
258

259
  taosArrayClear(pTask->msgInfo.pSendInfo);
29,449✔
260
  streamMutexUnlock(&pMsgInfo->lock);
29,449✔
261
}
29,450✔
262

263
static SStreamDispatchReq* createDispatchDataReq(SStreamTask* pTask, const SStreamDataBlock* pData) {
29,442✔
264
  int32_t code = 0;
29,442✔
265
  int32_t type = pTask->outputInfo.type;
29,442✔
266
  int32_t num = streamTaskGetNumOfDownstream(pTask);
29,442✔
267

268
  if (type != TASK_OUTPUT__SHUFFLE_DISPATCH && type != TASK_OUTPUT__FIXED_DISPATCH) {
29,442!
269
    terrno = TSDB_CODE_INVALID_PARA;
×
270
    stError("s-task:%s invalid dispatch type:%d not dispatch data", pTask->id.idStr, type);
×
271
    return NULL;
×
272
  }
273

274
  SStreamDispatchReq* pReqs = taosMemoryCalloc(num, sizeof(SStreamDispatchReq));
29,442✔
275
  if (pReqs == NULL) {
29,442✔
276
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1✔
277
    return NULL;
×
278
  }
279

280
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
29,441✔
281
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
24,702✔
282
    int32_t numOfVgroups = taosArrayGetSize(vgInfo);
24,702✔
283

284
    for (int32_t i = 0; i < numOfVgroups; i++) {
96,770✔
285
      SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
72,057✔
286
      if (pVgInfo == NULL) {
72,056!
287
        continue;
×
288
      }
289

290
      code = tInitStreamDispatchReq(&pReqs[i], pTask, pData->srcVgId, 0, pVgInfo->taskId, pData->type);
72,056✔
291
      if (code != TSDB_CODE_SUCCESS) {
72,069✔
292
        destroyDispatchMsg(pReqs, numOfVgroups);
1✔
293
        terrno = code;
×
294
        return NULL;
×
295
      }
296
    }
297
  } else {  // shuffle dispatch
298
    int32_t numOfBlocks = taosArrayGetSize(pData->blocks);
4,739✔
299
    int32_t downstreamTaskId = pTask->outputInfo.fixedDispatcher.taskId;
4,739✔
300

301
    code = tInitStreamDispatchReq(pReqs, pTask, pData->srcVgId, numOfBlocks, downstreamTaskId, pData->type);
4,739✔
302
    if (code != TSDB_CODE_SUCCESS) {
4,736!
303
      taosMemoryFree(pReqs);
×
304
      terrno = code;
×
305
      return NULL;
×
306
    }
307
  }
308

309
  return pReqs;
29,449✔
310
}
311

312
static int32_t doBuildDispatchMsg(SStreamTask* pTask, const SStreamDataBlock* pData) {
29,443✔
313
  int32_t code = 0;
29,443✔
314
  int64_t now = taosGetTimestampMs();
29,448✔
315
  int32_t numOfBlocks = taosArrayGetSize(pData->blocks);
29,448✔
316

317
  if (!(numOfBlocks != 0 && pTask->msgInfo.pData == NULL)) {
29,447!
318
    stError("s-task:%s dispatch block number:%d, exist not rsp dispatch msg:%p, abort build new dispatch msg",
5!
319
            pTask->id.idStr, numOfBlocks, pTask->msgInfo.pData);
320
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
321
  }
322

323
  pTask->msgInfo.dispatchMsgType = pData->type;
29,442✔
324

325
  if (pData->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
29,442✔
326
    SSDataBlock* p = taosArrayGet(pData->blocks, 0);
3,379✔
327
    if (p == NULL) {
3,377!
328
      return terrno;
×
329
    }
330

331
    pTask->msgInfo.checkpointId = p->info.version;
3,382✔
332
    pTask->msgInfo.transId = p->info.window.ekey;
3,382✔
333
  }
334

335
  SStreamDispatchReq* pReqs = createDispatchDataReq(pTask, pData);
29,445✔
336
  if (pReqs == NULL) {
29,448!
337
    stError("s-task:%s failed to create dispatch req", pTask->id.idStr);
×
338
    return terrno;
×
339
  }
340

341
  if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
29,448✔
342
    for (int32_t i = 0; i < numOfBlocks; i++) {
12,914✔
343
      SSDataBlock* pDataBlock = taosArrayGet(pData->blocks, i);
8,177✔
344
      if (pDataBlock == NULL) {
8,177!
345
        destroyDispatchMsg(pReqs, 1);
×
346
        return terrno;
×
347
      }
348

349
      code = streamAddBlockIntoDispatchMsg(pDataBlock, pReqs);
8,177✔
350
      if (code != TSDB_CODE_SUCCESS) {
8,172!
351
        destroyDispatchMsg(pReqs, 1);
×
352
        return code;
×
353
      }
354
    }
355

356
    addDispatchEntry(&pTask->msgInfo, pTask->outputInfo.fixedDispatcher.nodeId, now, true);
4,737✔
357
    pTask->msgInfo.pData = pReqs;
4,741✔
358
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
24,707!
359
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
24,707✔
360
    int32_t numOfVgroups = taosArrayGetSize(vgInfo);
24,707✔
361

362
    for (int32_t i = 0; i < numOfBlocks; i++) {
157,595✔
363
      SSDataBlock* pDataBlock = taosArrayGet(pData->blocks, i);
132,885✔
364
      if (pDataBlock == NULL) {
132,880!
365
        destroyDispatchMsg(pReqs, numOfVgroups);
×
366
        return terrno;
×
367
      }
368

369
      // TODO: do not use broadcast
370
      if (pDataBlock->info.type == STREAM_DELETE_RESULT || pDataBlock->info.type == STREAM_CHECKPOINT ||
132,881✔
371
          pDataBlock->info.type == STREAM_TRANS_STATE) {
129,024✔
372
        for (int32_t j = 0; j < numOfVgroups; j++) {
24,224✔
373
          code = streamAddBlockIntoDispatchMsg(pDataBlock, &pReqs[j]);
18,132✔
374
          if (code != 0) {
18,122!
375
            destroyDispatchMsg(pReqs, numOfVgroups);
×
376
            return code;
×
377
          }
378

379
          // it's a new vnode to receive dispatch msg, so add one
380
          if (pReqs[j].blockNum == 0) {
18,122✔
381
            SVgroupInfo* pDstVgroupInfo = taosArrayGet(vgInfo, j);
17,636✔
382
            if (pDstVgroupInfo == NULL) {
17,635!
383
              destroyDispatchMsg(pReqs, numOfVgroups);
×
384
              return terrno;
×
385
            }
386
            addDispatchEntry(&pTask->msgInfo, pDstVgroupInfo->vgId, now, true);
17,635✔
387
          }
388

389
          pReqs[j].blockNum++;
18,141✔
390
        }
391

392
        continue;
6,092✔
393
      }
394

395
      code = streamSearchAndAddBlock(pTask, pReqs, pDataBlock, pDataBlock->info.id.groupId, now);
126,798✔
396
      if (code != 0) {
126,799✔
397
        destroyDispatchMsg(pReqs, numOfVgroups);
2✔
398
        return code;
×
399
      }
400
    }
401

402
    pTask->msgInfo.pData = pReqs;
24,710✔
403
  }
404

405
  if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
29,451✔
406
    stDebug("s-task:%s build dispatch msg success, msgId:%d, stage:%" PRId64 " %p", pTask->id.idStr,
4,740✔
407
            pTask->execInfo.dispatch, pTask->pMeta->stage, pTask->msgInfo.pData);
408
  } else {
409
    int32_t numOfBranches = taosArrayGetSize(pTask->msgInfo.pSendInfo);
24,711✔
410
    stDebug("s-task:%s build dispatch msg success, msgId:%d, stage:%" PRId64 " dstVgNum:%d %p", pTask->id.idStr,
24,708✔
411
            pTask->execInfo.dispatch, pTask->pMeta->stage, numOfBranches, pTask->msgInfo.pData);
412
  }
413

414
  return code;
29,447✔
415
}
416

417
static int32_t sendDispatchMsg(SStreamTask* pTask, SStreamDispatchReq* pDispatchMsg) {
29,447✔
418
  int32_t     code = 0;
29,447✔
419
  const char* id = pTask->id.idStr;
29,447✔
420
  int32_t     msgId = pTask->msgInfo.msgId;
29,447✔
421

422
  if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
29,447✔
423
    int32_t vgId = pTask->outputInfo.fixedDispatcher.nodeId;
4,740✔
424
    SEpSet* pEpSet = &pTask->outputInfo.fixedDispatcher.epSet;
4,740✔
425
    int32_t downstreamTaskId = pTask->outputInfo.fixedDispatcher.taskId;
4,740✔
426

427
    stDebug("s-task:%s (child taskId:%d) fix-dispatch %d block(s) to s-task:0x%x (vgId:%d), msgId:%d", id,
4,740✔
428
            pTask->info.selfChildId, 1, downstreamTaskId, vgId, msgId);
429

430
    code = doSendDispatchMsg(pTask, pDispatchMsg, vgId, pEpSet);
4,740✔
431
  } else {
432
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
24,707✔
433
    int32_t numOfVgroups = taosArrayGetSize(vgInfo);
24,707✔
434
    int32_t numOfBranches = taosArrayGetSize(pTask->msgInfo.pSendInfo);
24,708✔
435

436
    stDebug("s-task:%s (child taskId:%d) start to shuffle-dispatch blocks to %d/%d vgroup(s), msgId:%d", id,
24,708✔
437
            pTask->info.selfChildId, numOfBranches, numOfVgroups, msgId);
438

439
    int32_t numOfSend = 0;
24,706✔
440
    for (int32_t i = 0; i < numOfVgroups; i++) {
62,420!
441
      if (pDispatchMsg[i].blockNum > 0) {
62,420✔
442
        SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
50,985✔
443
        if (pVgInfo == NULL) {
50,984!
444
          code = terrno;
×
445
          break;
×
446
        }
447

448
        stDebug("s-task:%s (child taskId:%d) shuffle-dispatch blocks:%d to vgId:%d", id, pTask->info.selfChildId,
50,984✔
449
                pDispatchMsg[i].blockNum, pVgInfo->vgId);
450

451
        code = doSendDispatchMsg(pTask, &pDispatchMsg[i], pVgInfo->vgId, &pVgInfo->epSet);
50,984✔
452
        if (code < 0) {
50,987!
453
          break;
×
454
        }
455

456
        // no need to try remain, all already send.
457
        if (++numOfSend == numOfBranches) {
50,987✔
458
          break;
24,708✔
459
        }
460
      }
461
    }
462

463
    stDebug("s-task:%s complete shuffle-dispatch blocks to all %d vnodes, msgId:%d", id, numOfVgroups, msgId);
24,708✔
464
  }
465

466
  return code;
29,447✔
467
}
468

469
static void setNotInDispatchMonitor(SDispatchMsgInfo* pMsgInfo) {
7✔
470
  streamMutexLock(&pMsgInfo->lock);
7✔
471
  pMsgInfo->inMonitor = 0;
7✔
472
  streamMutexUnlock(&pMsgInfo->lock);
7✔
473
}
7✔
474

475
static void setResendInfo(SDispatchEntry* pEntry, int64_t now) {
6✔
476
  pEntry->sendTs = now;
6✔
477
  pEntry->rspTs = -1;
6✔
478
  pEntry->retryCount += 1;
6✔
479
  pEntry->status = TSDB_CODE_SUCCESS;
6✔
480
}
6✔
481

482
static void addDispatchEntry(SDispatchMsgInfo* pMsgInfo, int32_t nodeId, int64_t now, bool lock) {
55,709✔
483
  SDispatchEntry entry = {.nodeId = nodeId, .rspTs = -1, .status = 0, .sendTs = now};
55,709✔
484

485
  if (lock) {
55,709✔
486
    streamMutexLock(&pMsgInfo->lock);
22,375✔
487
  }
488

489
  void* p = taosArrayPush(pMsgInfo->pSendInfo, &entry);
55,732✔
490
  if (p == NULL) {
55,715!
491
    stError("failed to add dispatch info");
×
492
  }
493

494
  if (lock) {
55,715✔
495
    streamMutexUnlock(&pMsgInfo->lock);
22,381✔
496
  }
497
}
55,729✔
498

499
static void doSendFailedDispatch(SStreamTask* pTask, SDispatchEntry* pEntry, int64_t now, const char* pMsg) {
6✔
500
  SStreamDispatchReq* pReq = pTask->msgInfo.pData;
6✔
501

502
  int32_t msgId = pTask->msgInfo.msgId;
6✔
503
  SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
6✔
504
  int32_t numOfVgroups = taosArrayGetSize(vgInfo);
6✔
505

506
  setResendInfo(pEntry, now);
6✔
507
  for (int32_t j = 0; j < numOfVgroups; ++j) {
12!
508
    SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, j);
12✔
509
    if (pVgInfo == NULL) {
12!
510
      continue;
×
511
    }
512

513
    if (pVgInfo->vgId == pEntry->nodeId) {
12✔
514
      int32_t code = doSendDispatchMsg(pTask, &pReq[j], pVgInfo->vgId, &pVgInfo->epSet);
6✔
515
      stDebug("s-task:%s (child taskId:%d) shuffle-dispatch blocks:%d to vgId:%d for %s, msgId:%d, code:%s",
6✔
516
              pTask->id.idStr, pTask->info.selfChildId, pReq[j].blockNum, pVgInfo->vgId, pMsg, msgId, tstrerror(code));
517
      break;
6✔
518
    }
519
  }
520
}
6✔
521

522
static void cleanupInMonitor(int32_t taskId, int64_t taskRefId, void* param) {
14,192✔
523
  int32_t ret = taosReleaseRef(streamTaskRefPool, taskRefId);
14,192✔
524
  if (ret) {
14,192!
525
    stError("s-task:0x%x failed to release task refId:%" PRId64, taskId, taskRefId);
×
526
  }
527
  streamTaskFreeRefId(param);
14,192✔
528
}
14,192✔
529

530
static void doMonitorDispatchData(void* param, void* tmrId) {
14,192✔
531
  int32_t           code = 0;
14,192✔
532
  int64_t           now = taosGetTimestampMs();
14,192✔
533
  bool              inDispatch = true;
14,192✔
534
  SStreamTask*      pTask = NULL;
14,192✔
535
  int64_t           taskRefId = *(int64_t*)param;
14,192✔
536
  const char*       id = NULL;
14,192✔
537
  int32_t           vgId = -1;
14,192✔
538
  SDispatchMsgInfo* pMsgInfo = NULL;
14,192✔
539
  int32_t           msgId = -1;
14,192✔
540

541
  pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
14,192✔
542
  if (pTask == NULL) {
14,192!
543
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
×
544
    streamTaskFreeRefId(param);
×
545
    return;
×
546
  }
547

548
  id = pTask->id.idStr;
14,192✔
549
  vgId = pTask->pMeta->vgId;
14,192✔
550
  pMsgInfo = &pTask->msgInfo;
14,192✔
551
  msgId = pMsgInfo->msgId;
14,192✔
552

553
  stDebug("s-task:%s start to monitor dispatch data", id);
14,192✔
554

555
  if (streamTaskShouldStop(pTask)) {
14,192✔
556
    stDebug("s-task:%s should stop, abort from timer", pTask->id.idStr);
7✔
557
    setNotInDispatchMonitor(pMsgInfo);
7✔
558
    cleanupInMonitor(pTask->id.taskId, taskRefId, param);
7✔
559
    return;
7✔
560
  }
561

562
  // slave task not handle the dispatch, downstream not ready will break the monitor timer
563
  // follower not handle the dispatch rsp
564
  if ((pTask->pMeta->role == NODE_ROLE_FOLLOWER) || (pTask->status.downstreamReady != 1)) {
14,185!
565
    stError("s-task:%s vgId:%d follower or downstream not ready, jump out of monitor tmr", id, vgId);
×
566
    setNotInDispatchMonitor(pMsgInfo);
×
567
    cleanupInMonitor(pTask->id.taskId, taskRefId, param);
×
568
    return;
×
569
  }
570

571
  streamMutexLock(&pMsgInfo->lock);
14,185✔
572
  if (pTask->outputq.status == TASK_OUTPUT_STATUS__NORMAL) {
14,185✔
573
    stDebug("s-task:%s not in dispatch procedure, abort from timer", pTask->id.idStr);
12,211✔
574
    pMsgInfo->inMonitor = 0;
12,211✔
575
    inDispatch = false;
12,211✔
576
  }
577

578
  streamMutexUnlock(&pMsgInfo->lock);
14,185✔
579

580
  if (!inDispatch) {
14,185✔
581
    cleanupInMonitor(pTask->id.taskId, taskRefId, param);
12,211✔
582
    return;
12,211✔
583
  }
584

585
  int32_t numOfFailed = getFailedDispatchInfo(pMsgInfo, now);
1,974✔
586
  if (numOfFailed == 0) {
1,974✔
587
    stDebug("s-task:%s no error occurs, check again in %dms", id, DISPATCH_RETRY_INTERVAL_MS);
1,968✔
588
    streamStartMonitorDispatchData(pTask, DISPATCH_RETRY_INTERVAL_MS);
1,968✔
589
    cleanupInMonitor(pTask->id.taskId, taskRefId, param);
1,968✔
590
    return;
1,968✔
591
  }
592

593
  {
594
    SStreamDispatchReq* pReq = pTask->msgInfo.pData;
6✔
595

596
    if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
6!
597
      stDebug("s-task:%s (child taskId:%d) retry shuffle-dispatch to down streams, msgId:%d", id,
6✔
598
              pTask->info.selfChildId, msgId);
599

600
      int32_t numOfRetry = 0;
6✔
601
      for (int32_t i = 0; i < taosArrayGetSize(pTask->msgInfo.pSendInfo); ++i) {
21✔
602
        SDispatchEntry* pEntry = taosArrayGet(pTask->msgInfo.pSendInfo, i);
15✔
603
        if (pEntry == NULL) {
15!
604
          continue;
×
605
        }
606

607
        if (pEntry->status == TSDB_CODE_SUCCESS && pEntry->rspTs > 0) {
15✔
608
          continue;
9✔
609
        }
610

611
        // downstream not rsp yet beyond threshold that is 10s
612
        if (isDispatchRspTimeout(pEntry, now)) {  // not respond yet beyonds 30s, re-send data
6✔
613
          doSendFailedDispatch(pTask, pEntry, now, "timeout");
5✔
614
          numOfRetry += 1;
5✔
615
          continue;
5✔
616
        }
617

618
        // downstream inputQ is closed
619
        if (pEntry->status == TASK_INPUT_STATUS__BLOCKED) {
1!
620
          doSendFailedDispatch(pTask, pEntry, now, "downstream inputQ blocked");
1✔
621
          numOfRetry += 1;
1✔
622
          continue;
1✔
623
        }
624

625
        // handle other errors
626
        if (pEntry->status != TSDB_CODE_SUCCESS) {
×
627
          doSendFailedDispatch(pTask, pEntry, now, "downstream error");
×
628
          numOfRetry += 1;
×
629
        }
630
      }
631

632
      stDebug("s-task:%s complete retry shuffle-dispatch blocks to all %d vnodes, msgId:%d", pTask->id.idStr,
6✔
633
              numOfRetry, msgId);
634
    } else {
635
      int32_t dstVgId = pTask->outputInfo.fixedDispatcher.nodeId;
×
636
      SEpSet* pEpSet = &pTask->outputInfo.fixedDispatcher.epSet;
×
637
      int32_t downstreamTaskId = pTask->outputInfo.fixedDispatcher.taskId;
×
638

639
      int32_t         s = taosArrayGetSize(pTask->msgInfo.pSendInfo);
×
640
      SDispatchEntry* pEntry = taosArrayGet(pTask->msgInfo.pSendInfo, 0);
×
641
      if (pEntry != NULL) {
×
642
        setResendInfo(pEntry, now);
×
643
        code = doSendDispatchMsg(pTask, pReq, dstVgId, pEpSet);
×
644

645
        stDebug("s-task:%s (child taskId:%d) fix-dispatch %d block(s) to s-task:0x%x (vgId:%d), msgId:%d, code:%s", id,
×
646
                pTask->info.selfChildId, 1, downstreamTaskId, dstVgId, msgId, tstrerror(code));
647
      } else {
648
        stError("s-task:%s invalid index 0, size:%d", id, s);
×
649
      }
650
    }
651
  }
652

653
  if (streamTaskShouldStop(pTask)) {
6!
654
    stDebug("s-task:%s should stop, abort from timer", pTask->id.idStr);
×
655
    setNotInDispatchMonitor(pMsgInfo);
×
656
  } else {
657
    streamStartMonitorDispatchData(pTask, DISPATCH_RETRY_INTERVAL_MS);
6✔
658
  }
659

660
  cleanupInMonitor(pTask->id.taskId, taskRefId, param);
6✔
661
}
662

663
void streamStartMonitorDispatchData(SStreamTask* pTask, int64_t waitDuration) {
14,976✔
664
  int32_t  vgId = pTask->pMeta->vgId;
14,976✔
665
  int64_t* pTaskRefId = NULL;
14,976✔
666
  int32_t  code = streamTaskAllocRefId(pTask, &pTaskRefId);
14,976✔
667
  if (code == 0) {
14,984!
668
    streamTmrStart(doMonitorDispatchData, waitDuration, pTaskRefId, streamTimer, &pTask->msgInfo.pRetryTmr, vgId,
14,984✔
669
                   "dispatch-monitor");
670
  }
671
}
14,984✔
672

673
static int32_t doAddDispatchBlock(SStreamTask* pTask, SStreamDispatchReq* pReqs, SSDataBlock* pDataBlock,
126,797✔
674
                                  SArray* vgInfo, uint32_t hashValue, int64_t now, bool* pFound) {
675
  size_t  numOfVgroups = taosArrayGetSize(vgInfo);
126,797✔
676
  int32_t code = 0;
126,798✔
677

678
  *pFound = false;
126,798✔
679

680
  for (int32_t j = 0; j < numOfVgroups; j++) {
254,047✔
681
    SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, j);
254,043✔
682
    if (pVgInfo == NULL) {
254,043!
683
      continue;
×
684
    }
685

686
    if (hashValue >= pVgInfo->hashBegin && hashValue <= pVgInfo->hashEnd) {
254,043✔
687
      if ((code = streamAddBlockIntoDispatchMsg(pDataBlock, &pReqs[j])) < 0) {
126,794!
688
        stError("s-task:%s failed to add dispatch block, code:%s", pTask->id.idStr, tstrerror(terrno));
×
689
        return code;
×
690
      }
691

692
      if (pReqs[j].blockNum == 0) {
126,798✔
693
        SVgroupInfo* pDstVgroupInfo = taosArrayGet(vgInfo, j);
33,335✔
694
        if (pDstVgroupInfo != NULL) {
33,337!
695
          addDispatchEntry(&pTask->msgInfo, pDstVgroupInfo->vgId, now, false);
33,337✔
696
        }
697
      }
698

699
      pReqs[j].blockNum++;
126,797✔
700
      *pFound = true;
126,797✔
701
      break;
126,797✔
702
    }
703
  }
704

705
  return code;
126,801✔
706
}
707

708
int32_t streamSearchAndAddBlock(SStreamTask* pTask, SStreamDispatchReq* pReqs, SSDataBlock* pDataBlock, int64_t groupId,
126,795✔
709
                                int64_t now) {
710
  bool     found = false;
126,795✔
711
  uint32_t hashValue = 0;
126,795✔
712
  int32_t  code = 0;
126,795✔
713
  SArray*  vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
126,795✔
714

715
  if (pTask->pNameMap == NULL) {
126,795✔
716
    pTask->pNameMap = tSimpleHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
2,287✔
717
    if (pTask->pNameMap == NULL) {
2,287!
718
      stError("s-task:%s failed to init the name map, code:%s", pTask->id.idStr, tstrerror(terrno));
×
719
      return terrno;
×
720
    }
721
  }
722

723
  void* pVal = tSimpleHashGet(pTask->pNameMap, &groupId, sizeof(int64_t));
126,795✔
724
  if (pVal) {
126,796✔
725
    SBlockName* pBln = (SBlockName*)pVal;
42,527✔
726
    hashValue = pBln->hashValue;
42,527✔
727
    if (!pDataBlock->info.parTbName[0]) {
42,527✔
728
      memset(pDataBlock->info.parTbName, 0, TSDB_TABLE_NAME_LEN);
31,746✔
729
      memcpy(pDataBlock->info.parTbName, pBln->parTbName, strlen(pBln->parTbName));
31,746✔
730
    }
731
  } else {
732
    char ctbName[TSDB_TABLE_FNAME_LEN] = {0};
84,269✔
733
    if (pDataBlock->info.parTbName[0]) {
84,269✔
734
      if (pTask->subtableWithoutMd5 != 1 && !isAutoTableName(pDataBlock->info.parTbName) &&
5,625!
735
          !alreadyAddGroupId(pDataBlock->info.parTbName, groupId) && groupId != 0) {
3,464!
736
        if (pTask->ver == SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
3,435!
737
          code = buildCtbNameAddGroupId(NULL, pDataBlock->info.parTbName, groupId, sizeof(pDataBlock->info.parTbName));
×
738
        } else if (pTask->ver > SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
3,435!
739
          code = buildCtbNameAddGroupId(pTask->outputInfo.shuffleDispatcher.stbFullName, pDataBlock->info.parTbName,
3,435✔
740
                                        groupId, sizeof(pDataBlock->info.parTbName));
741
        }
742
        if (code != TSDB_CODE_SUCCESS) {
3,433!
743
          return code;
×
744
        }
745
      }
746
    } else {
747
      code = buildCtbNameByGroupIdImpl(pTask->outputInfo.shuffleDispatcher.stbFullName, groupId,
78,644✔
748
                                       pDataBlock->info.parTbName);
78,644✔
749
      if (code) {
78,644!
750
        stError("s-task:%s failed to build child table name for group:%" PRId64 ", code:%s", pTask->id.idStr, groupId,
×
751
                tstrerror(code));
752
      }
753
    }
754

755
    snprintf(ctbName, TSDB_TABLE_NAME_LEN, "%s.%s", pTask->outputInfo.shuffleDispatcher.dbInfo.db,
84,270✔
756
             pDataBlock->info.parTbName);
84,270✔
757
    /*uint32_t hashValue = MurmurHash3_32(ctbName, strlen(ctbName));*/
758
    SUseDbRsp* pDbInfo = &pTask->outputInfo.shuffleDispatcher.dbInfo;
84,270✔
759
    hashValue =
84,269✔
760
        taosGetTbHashVal(ctbName, strlen(ctbName), pDbInfo->hashMethod, pDbInfo->hashPrefix, pDbInfo->hashSuffix);
84,270✔
761
    SBlockName bln = {0};
84,269✔
762
    bln.hashValue = hashValue;
84,269✔
763
    memcpy(bln.parTbName, pDataBlock->info.parTbName, strlen(pDataBlock->info.parTbName));
84,269✔
764

765
    // failed to put into name buffer, no need to do anything
766
    if (tSimpleHashGetSize(pTask->pNameMap) < MAX_BLOCK_NAME_NUM) {  // allow error, and do nothing
84,269✔
767
      code = tSimpleHashPut(pTask->pNameMap, &groupId, sizeof(int64_t), &bln, sizeof(SBlockName));
27,679✔
768
    }
769
  }
770

771
  streamMutexLock(&pTask->msgInfo.lock);
126,797✔
772
  code = doAddDispatchBlock(pTask, pReqs, pDataBlock, vgInfo, hashValue, now, &found);
126,798✔
773
  streamMutexUnlock(&pTask->msgInfo.lock);
126,798✔
774

775
  if (code) {
126,800!
776
    return code;
×
777
  }
778

779
  if (!found) {
126,800!
780
    stError("s-task:%s not found req hash value:%u, failed to add dispatch block", pTask->id.idStr, hashValue);
×
781
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
782
  } else {
783
    return 0;
126,800✔
784
  }
785
}
786

787
int32_t streamDispatchStreamBlock(SStreamTask* pTask) {
53,155✔
788
  const char*            id = pTask->id.idStr;
53,155✔
789
  int32_t                code = 0;
53,155✔
790
  SStreamDataBlock*      pBlock = NULL;
53,155✔
791
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
53,155✔
792
  int32_t                old = 0;
53,155✔
793

794
  int32_t numOfElems = streamQueueGetNumOfItems(pTask->outputq.queue);
53,155✔
795
  if (numOfElems > 0) {
53,155!
796
    double  size = SIZE_IN_MiB(taosQueueMemorySize(pTask->outputq.queue->pQueue));
53,156✔
797
    int32_t numOfUnAccessed = streamQueueGetNumOfUnAccessedItems(pTask->outputq.queue);
53,158✔
798
    stDebug("s-task:%s start to dispatch intermediate block to downstream, elem in outputQ:%d/%d, size:%.2fMiB", id,
53,158✔
799
            numOfUnAccessed, numOfElems, size);
800
  }
801

802
  // to make sure only one dispatch is running
803
  old = atomic_val_compare_exchange_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL, TASK_OUTPUT_STATUS__WAIT);
53,157✔
804
  if (old != TASK_OUTPUT_STATUS__NORMAL) {
53,156✔
805
    stDebug("s-task:%s wait for dispatch rsp, not dispatch now, output status:%d", id, old);
3,549✔
806
    return 0;
3,549✔
807
  }
808

809
  if (pInfo->dispatchTrigger) {
49,607!
810
    if ((pInfo->activeId != 0) && (pInfo->failedId < pInfo->activeId)) {
×
811
      stDebug("s-task:%s already send checkpoint-trigger, no longer dispatch any other data", id);
×
812
      atomic_store_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL);
×
813
      return 0;
×
814
    } else {
815
      stDebug("s-task:%s dispatch trigger set, and ignore since current active checkpointId:%" PRId64 " failed", id,
×
816
              pInfo->activeId);
817
    }
818
  }
819

820
  if (pTask->msgInfo.pData != NULL) {
49,600!
821
    stFatal("s-task:%s not rsp data:%p exist, should not dispatch msg now", id, pTask->msgInfo.pData);
×
822
  } else {
823
    stDebug("s-task:%s start to dispatch msg, set output status:%d", id, pTask->outputq.status);
49,600✔
824
  }
825

826
  while (1) {
×
827
    streamQueueNextItem(pTask->outputq.queue, (SStreamQueueItem**)&pBlock);
49,600✔
828
    if (pBlock == NULL) {
49,609✔
829
      atomic_store_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL);
20,160✔
830
      stDebug("s-task:%s not dispatch since no elems in outputQ, output status:%d", id, pTask->outputq.status);
20,160✔
831
      return 0;
20,160✔
832
    }
833

834
    int32_t type = pBlock->type;
29,449✔
835
    if (!(type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
29,449!
836
          type == STREAM_INPUT__TRANS_STATE)) {
837
      stError("s-task:%s invalid dispatch block type:%d", id, type);
×
838
      return TSDB_CODE_INTERNAL_ERROR;
×
839
    }
840

841
    pTask->execInfo.dispatch += 1;
29,449✔
842

843
    streamMutexLock(&pTask->msgInfo.lock);
29,449✔
844
    initDispatchInfo(&pTask->msgInfo, pTask->execInfo.dispatch);
29,448✔
845
    streamMutexUnlock(&pTask->msgInfo.lock);
29,448✔
846

847
    code = doBuildDispatchMsg(pTask, pBlock);
29,451✔
848
    if (code == 0) {
29,441!
849
      destroyStreamDataBlock(pBlock);
29,443✔
850
    } else {  // todo handle build dispatch msg failed
851
    }
852

853
    if (type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
29,449✔
854
      // outputQ should be empty here, otherwise, set the checkpoint failed due to the retrieve req happens
855
      if (streamQueueGetNumOfUnAccessedItems(pTask->outputq.queue) > 0) {
3,388!
856
        stError(
×
857
            "s-task:%s items are still in outputQ due to downstream retrieve, failed to init and discard "
858
            "checkpoint-trigger dispatch",
859
            pTask->id.idStr);
860
        streamTaskSetCheckpointFailed(pTask);
×
861
        clearBufferedDispatchMsg(pTask);
×
862
        continue;
×
863
      }
864

865
      code = streamTaskInitTriggerDispatchInfo(pTask);
3,388✔
866
      if (code != TSDB_CODE_SUCCESS) {  // todo handle error
867
      }
868
    }
869

870
    break;
29,449✔
871
  }
872

873
  code = sendDispatchMsg(pTask, pTask->msgInfo.pData);
29,449✔
874

875
  // todo: secure the timerActive and start timer in after lock pTask->lock
876
  streamMutexLock(&pTask->lock);
29,445✔
877
  bool shouldStop = streamTaskShouldStop(pTask);
29,450✔
878
  streamMutexUnlock(&pTask->lock);
29,450✔
879

880
  if (shouldStop) {
29,447✔
881
    stDebug("s-task:%s in stop/dropping status, not start dispatch monitor tmr", id);
22✔
882
  } else {
883
    streamMutexLock(&pTask->msgInfo.lock);
29,425✔
884
    if (pTask->msgInfo.inMonitor == 0) {
29,428✔
885
//      int32_t ref = atomic_add_fetch_32(&pTask->status.timerActive, 1);
886
      stDebug("s-task:%s start dispatch monitor tmr in %dms, dispatch code:%s", id, DISPATCH_RETRY_INTERVAL_MS,
13,004✔
887
              tstrerror(code));
888
      streamStartMonitorDispatchData(pTask, DISPATCH_RETRY_INTERVAL_MS);
13,004✔
889
      pTask->msgInfo.inMonitor = 1;
13,010✔
890
    } else {
891
      stDebug("s-task:%s already in dispatch monitor tmr", id);
16,424✔
892
    }
893

894
    streamMutexUnlock(&pTask->msgInfo.lock);
29,434✔
895
  }
896

897
  // this block can not be deleted until it has been sent to downstream task successfully.
898
  return TSDB_CODE_SUCCESS;
29,451✔
899
}
900

901
int32_t initCheckpointReadyMsg(SStreamTask* pTask, int32_t upstreamNodeId, int32_t upstreamTaskId, int32_t childId,
8,732✔
902
                               int64_t checkpointId, SRpcMsg* pMsg) {
903
  int32_t code = 0;
8,732✔
904
  int32_t tlen = 0;
8,732✔
905
  void*   buf = NULL;
8,732✔
906

907
  SStreamCheckpointReadyMsg req = {0};
8,732✔
908
  req.downstreamNodeId = pTask->pMeta->vgId;
8,732✔
909
  req.downstreamTaskId = pTask->id.taskId;
8,732✔
910
  req.streamId = pTask->id.streamId;
8,732✔
911
  req.checkpointId = checkpointId;
8,732✔
912
  req.childId = childId;
8,732✔
913
  req.upstreamNodeId = upstreamNodeId;
8,732✔
914
  req.upstreamTaskId = upstreamTaskId;
8,732✔
915

916
  tEncodeSize(tEncodeStreamCheckpointReadyMsg, &req, tlen, code);
8,732!
917
  if (code < 0) {
8,720!
918
    return -1;
×
919
  }
920

921
  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
8,720✔
922
  if (buf == NULL) {
8,734!
923
    return terrno;
×
924
  }
925

926
  ((SMsgHead*)buf)->vgId = htonl(req.upstreamNodeId);
8,734✔
927
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
8,734✔
928

929
  SEncoder encoder;
930
  tEncoderInit(&encoder, abuf, tlen);
8,734✔
931
  if ((code = tEncodeStreamCheckpointReadyMsg(&encoder, &req)) < 0) {
8,729!
932
    rpcFreeCont(buf);
×
933
    tEncoderClear(&encoder);
×
934
    return code;
×
935
  }
936
  tEncoderClear(&encoder);
8,737✔
937

938
  initRpcMsg(pMsg, TDMT_STREAM_TASK_CHECKPOINT_READY, buf, tlen + sizeof(SMsgHead));
8,744✔
939
  return TSDB_CODE_SUCCESS;
8,723✔
940
}
941

942
static int32_t doTaskChkptStatusCheck(SStreamTask* pTask, void* param, int32_t num) {
1✔
943
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
944
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptReadyMsgTmr;
1✔
945
  const char*            id = pTask->id.idStr;
1✔
946
  int32_t                vgId = pTask->pMeta->vgId;
1✔
947

948
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
1!
949
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
950
    stWarn("s-task:%s vgId:%d ready-msg send tmr launched by previous checkpoint procedure, checkpointId:%" PRId64
×
951
           ", quit",
952
           id, vgId, pTmrInfo->launchChkptId);
953
    return -1;
×
954
  }
955

956
  // active checkpoint info is cleared for now
957
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (num == 0) || (pTask->chkInfo.startTs == 0)) {
1!
958
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
959
    stWarn("s-task:%s vgId:%d active checkpoint may be cleared, quit from readyMsg send tmr", id, vgId);
×
960
    return -1;
×
961
  }
962

963
  if (taosArrayGetSize(pTask->upstreamInfo.pList) != num) {
1!
964
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
965
    stWarn("s-task:%s vgId:%d upstream number:%d not equals sent readyMsg:%d, quit from readyMsg send tmr", id,
×
966
           vgId, (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList), num);
967
    return -1;
×
968
  }
969

970
  return 0;
1✔
971
}
972

973
static int32_t doFindNotConfirmUpstream(SArray** ppNotRspList, SArray* pList, int32_t num, int32_t vgId, int32_t level,
1✔
974
                                        const char* id) {
975
  SArray* pTmp = taosArrayInit(4, sizeof(int32_t));
1✔
976
  if (pTmp == NULL) {
1!
977
    return terrno;
×
978
  }
979

980
  for (int32_t i = 0; i < num; ++i) {
3✔
981
    STaskCheckpointReadyInfo* pInfo = taosArrayGet(pList, i);
2✔
982
    if (pInfo == NULL) {
2!
983
      continue;
×
984
    }
985

986
    if (pInfo->sendCompleted == 1) {
2!
987
      continue;
2✔
988
    }
989

990
    void* p = taosArrayPush(pTmp, &pInfo->upstreamTaskId);
×
991
    if (p == NULL) {
×
992
      stError("s-task:%s vgId:%d failed to record not rsp task, code: out of memory", id, vgId);
×
993
      return terrno;
×
994
    } else {
995
      stDebug("s-task:%s vgId:%d level:%d checkpoint-ready rsp from upstream:0x%x not confirmed yet", id, vgId, level,
×
996
              pInfo->upstreamTaskId);
997
    }
998
  }
999

1000
  *ppNotRspList = pTmp;
1✔
1001
  return 0;
1✔
1002
}
1003

1004
static void doSendChkptReadyMsg(SStreamTask* pTask, SArray* pNotRspList, int64_t checkpointId, SArray* pReadyList) {
×
1005
  int32_t     code = 0;
×
1006
  int32_t     num = taosArrayGetSize(pReadyList);
×
1007
  const char* id = pTask->id.idStr;
×
1008

1009
  for (int32_t i = 0; i < taosArrayGetSize(pNotRspList); ++i) {
×
1010
    int32_t* pTaskId = taosArrayGet(pNotRspList, i);
×
1011
    if (pTaskId == NULL) {
×
1012
      continue;
×
1013
    }
1014

1015
    for (int32_t j = 0; j < num; ++j) {
×
1016
      STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pReadyList, j);
×
1017
      if (pReadyInfo == NULL) {
×
1018
        continue;
×
1019
      }
1020

1021
      if (*pTaskId == pReadyInfo->upstreamTaskId) {  // send msg again
×
1022

1023
        SRpcMsg msg = {0};
×
1024
        code = initCheckpointReadyMsg(pTask, pReadyInfo->upstreamNodeId, pReadyInfo->upstreamTaskId,
×
1025
                                      pReadyInfo->childId, checkpointId, &msg);
1026
        if (code == TSDB_CODE_SUCCESS) {
×
1027
          code = tmsgSendReq(&pReadyInfo->upstreamNodeEpset, &msg);
×
1028
          if (code == TSDB_CODE_SUCCESS) {
×
1029
            stDebug("s-task:%s level:%d checkpoint-ready msg sent to upstream:0x%x again", id, pTask->info.taskLevel,
×
1030
                    pReadyInfo->upstreamTaskId);
1031
          } else {
1032
            stError("s-task:%s failed to send checkpoint-ready msg, try nex time in 10s", id);
×
1033
          }
1034
        } else {
1035
          stError("s-task:%s failed to prepare the checkpoint-ready msg, try nex time in 10s", id);
×
1036
        }
1037
      }
1038
    }
1039
  }
1040
}
×
1041

1042
static int32_t chkptReadyMsgSendHelper(SStreamTask* pTask, void* param, SArray* pNotRspList) {
1✔
1043
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
1044
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptReadyMsgTmr;
1✔
1045
  SArray*                pList = pActiveInfo->pReadyMsgList;
1✔
1046
  int32_t                num = taosArrayGetSize(pList);
1✔
1047
  int32_t                vgId = pTask->pMeta->vgId;
1✔
1048
  int32_t                checkpointId = pActiveInfo->activeId;
1✔
1049
  const char*            id = pTask->id.idStr;
1✔
1050
  int32_t                notRsp = 0;
1✔
1051

1052
  int32_t code = doTaskChkptStatusCheck(pTask, param, num);
1✔
1053
  if (code) {
1!
1054
    return code;
×
1055
  }
1056

1057
  code = doFindNotConfirmUpstream(&pNotRspList, pList, num, vgId, pTask->info.taskLevel, id);
1✔
1058
  if (code) {
1!
1059
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1060
    stError("s-task:%s failed to find not rsp checkpoint-ready downstream, code:%s, out of tmr", id, tstrerror(code));
×
1061
    return code;
×
1062
  }
1063

1064
  notRsp = taosArrayGetSize(pNotRspList);
1✔
1065
  if (notRsp == 0) {
1!
1066
    streamClearChkptReadyMsg(pActiveInfo);
1✔
1067
  } else {
1068
    doSendChkptReadyMsg(pTask, pNotRspList, checkpointId, pList);
×
1069
  }
1070

1071
  return code;
1✔
1072
}
1073

1074
static void chkptReadyMsgSendMonitorFn(void* param, void* tmrId) {
110,467✔
1075
  SArray*                pNotRspList = NULL;
110,467✔
1076
  int32_t                code = 0;
110,467✔
1077
  int32_t                notRsp = 0;
110,467✔
1078
  int64_t                taskRefId = *(int64_t*)param;
110,467✔
1079
  int32_t                vgId = -1;
110,467✔
1080
  const char*            id = NULL;
110,467✔
1081
  SActiveCheckpointInfo* pActiveInfo = NULL;
110,467✔
1082
  SStreamTmrInfo*        pTmrInfo = NULL;
110,467✔
1083

1084
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
110,467✔
1085
  if (pTask == NULL) {
110,467✔
1086
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
9!
1087
    streamTaskFreeRefId(param);
9✔
1088
    return;
110,466✔
1089
  }
1090

1091
  vgId = pTask->pMeta->vgId;
110,458✔
1092
  id = pTask->id.idStr;
110,458✔
1093
  pActiveInfo = pTask->chkInfo.pActiveInfo;
110,458✔
1094
  pTmrInfo = &pActiveInfo->chkptReadyMsgTmr;
110,458✔
1095

1096
  // check the status every 100ms
1097
  if (streamTaskShouldStop(pTask)) {
110,458✔
1098
    streamCleanBeforeQuitTmr(pTmrInfo, param);
7✔
1099
    stDebug("s-task:%s vgId:%d status:stop, quit from monitor checkpoint-trigger", id, vgId);
7!
1100
    streamMetaReleaseTask(pTask->pMeta, pTask);
7✔
1101
    taosArrayDestroy(pNotRspList);
7✔
1102
    return;
7✔
1103
  }
1104

1105
  if (++pTmrInfo->activeCounter < 50) {
110,451✔
1106
    streamTmrStart(chkptReadyMsgSendMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
108,884✔
1107
                   "chkpt-ready-monitor");
1108
    streamMetaReleaseTask(pTask->pMeta, pTask);
108,884✔
1109
    taosArrayDestroy(pNotRspList);
108,884✔
1110
    return;
108,884✔
1111
  }
1112

1113
  // reset tmr
1114
  pTmrInfo->activeCounter = 0;
1,567✔
1115
  stDebug("s-task:%s in sending checkpoint-ready msg monitor tmr", id);
1,567✔
1116

1117
  streamMutexLock(&pTask->lock);
1,567✔
1118
  SStreamTaskState state = streamTaskGetStatus(pTask);
1,567✔
1119
  streamMutexUnlock(&pTask->lock);
1,567✔
1120

1121
  // 1. check status in the first place
1122
  if (state.state != TASK_STATUS__CK) {
1,567✔
1123
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1,566✔
1124
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint, quit from monitor checkpoint-ready", id, vgId,
1,566✔
1125
            state.name);
1126
    streamMetaReleaseTask(pTask->pMeta, pTask);
1,566✔
1127
    taosArrayDestroy(pNotRspList);
1,566✔
1128
    return;
1,566✔
1129
  }
1130

1131
  streamMutexLock(&pActiveInfo->lock);
1✔
1132
  code = chkptReadyMsgSendHelper(pTask, param, pNotRspList);
1✔
1133
  streamMutexUnlock(&pActiveInfo->lock);
1✔
1134

1135
  if (code != TSDB_CODE_SUCCESS) {
1!
1136
    streamMetaReleaseTask(pTask->pMeta, pTask);
×
1137
    taosArrayDestroy(pNotRspList);
×
1138
    return;
×
1139
  }
1140

1141
  notRsp = taosArrayGetSize(pNotRspList);
1✔
1142
  if (notRsp > 0) {  // send checkpoint-ready msg again
1!
1143
    stDebug("s-task:%s start to monitor checkpoint-ready msg recv status in 10s", id);
×
1144
    streamTmrStart(chkptReadyMsgSendMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1145
                   "chkpt-ready-monitor");
1146
  } else {
1147
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1✔
1148
    stDebug(
1!
1149
        "s-task:%s vgId:%d checkpoint-ready msg confirmed by all upstream task(s), clear checkpoint-ready msg and quit "
1150
        "from timer",
1151
        id, vgId);
1152
  }
1153

1154
  // release should be the last execution, since pTask may be destroyed after it immediately.
1155
  streamMetaReleaseTask(pTask->pMeta, pTask);
1✔
1156
  taosArrayDestroy(pNotRspList);
1✔
1157
}
1158

1159
// this function is usually invoked by sink/agg task
1160
int32_t streamTaskSendCheckpointReadyMsg(SStreamTask* pTask) {
3,332✔
1161
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
3,332✔
1162

1163
  const char* id = pTask->id.idStr;
3,332✔
1164
  int32_t     vgId = pTask->pMeta->vgId;
3,332✔
1165
  SArray*     pList = pActiveInfo->pReadyMsgList;
3,332✔
1166

1167
  streamMutexLock(&pActiveInfo->lock);
3,332✔
1168

1169
  int32_t num = taosArrayGetSize(pList);
3,344✔
1170
  if (taosArrayGetSize(pTask->upstreamInfo.pList) != num) {
3,344!
1171
    stError("s-task:%s invalid number of sent readyMsg:%d to upstream:%d", id, num,
×
1172
            (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList));
1173
    streamMutexUnlock(&pActiveInfo->lock);
×
1174
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1175
  }
1176

1177
  for (int32_t i = 0; i < num; ++i) {
12,077✔
1178
    STaskCheckpointReadyInfo* pInfo = taosArrayGet(pList, i);
8,732✔
1179
    if (pInfo == NULL) {
8,728!
1180
      continue;
×
1181
    }
1182

1183
    SRpcMsg msg = {0};
8,728✔
1184
    int32_t code = initCheckpointReadyMsg(pTask, pInfo->upstreamNodeId, pInfo->upstreamTaskId, pInfo->childId,
8,728✔
1185
                                          pInfo->checkpointId, &msg);
1186
    if (code == TSDB_CODE_SUCCESS) {
8,722!
1187
      code = tmsgSendReq(&pInfo->upstreamNodeEpset, &msg);
8,722✔
1188
      if (code == TSDB_CODE_SUCCESS) {
8,745!
1189
        stDebug("s-task:%s level:%d checkpoint-ready msg sent to upstream:0x%x", id, pTask->info.taskLevel,
8,745✔
1190
                pInfo->upstreamTaskId);
1191
      } else {
1192
        stError("s-task:%s failed to send checkpoint-ready msg, try nex time in 10s", id);
×
1193
      }
1194
    } else {
1195
      stError("s-task:%s failed to prepare the checkpoint-ready msg, try nex time in 10s", id);
×
1196
    }
1197
  }
1198

1199
  stDebug("s-task:%s level:%d checkpoint-ready msg sent to all %d upstreams", id, pTask->info.taskLevel, num);
3,345✔
1200

1201
  // start to check if checkpoint ready msg has successfully received by upstream tasks.
1202
  if (pTask->info.taskLevel == TASK_LEVEL__SINK || pTask->info.taskLevel == TASK_LEVEL__AGG) {
3,345✔
1203
    SStreamTmrInfo* pTmrInfo = &pActiveInfo->chkptReadyMsgTmr;
3,331✔
1204

1205
    int8_t old = atomic_val_compare_exchange_8(&pTmrInfo->isActive, 0, 1);
3,331✔
1206
    if (old == 0) {
3,331!
1207
      stDebug("s-task:%s start checkpoint-ready monitor in 10s", pTask->id.idStr);
3,331✔
1208

1209
      int64_t* pTaskRefId = NULL;
3,331✔
1210
      int32_t  code = streamTaskAllocRefId(pTask, &pTaskRefId);
3,331✔
1211
      if (code == 0) {
3,332!
1212
        streamTmrStart(chkptReadyMsgSendMonitorFn, 200, pTaskRefId, streamTimer, &pTmrInfo->tmrHandle, vgId,
3,332✔
1213
                       "chkpt-ready-monitor");
1214

1215
        // mark the timer monitor checkpointId
1216
        pTmrInfo->launchChkptId = pActiveInfo->activeId;
3,332✔
1217
      }
1218
    } else {
1219
      stError("s-task:%s previous checkpoint-ready monitor tmr is set, not start new one", pTask->id.idStr);
×
1220
    }
1221
  }
1222

1223
  streamMutexUnlock(&pActiveInfo->lock);
3,346✔
1224
  return TSDB_CODE_SUCCESS;
3,346✔
1225
}
1226

1227
// this function is only invoked by source task, and send rsp to mnode
1228
int32_t streamTaskSendCheckpointSourceRsp(SStreamTask* pTask) {
9,148✔
1229
  SArray* pList = pTask->chkInfo.pActiveInfo->pReadyMsgList;
9,148✔
1230

1231
  streamMutexLock(&pTask->chkInfo.pActiveInfo->lock);
9,148✔
1232
  if (taosArrayGetSize(pList) == 1) {
9,151✔
1233
    STaskCheckpointReadyInfo* pInfo = taosArrayGet(pList, 0);
3,350✔
1234
    if (pInfo != NULL) {
3,350!
1235
      tmsgSendRsp(&pInfo->msg);
3,350✔
1236
      taosArrayClear(pList);
3,350✔
1237
      stDebug("s-task:%s level:%d checkpoint-source rsp completed msg sent to mnode", pTask->id.idStr,
3,350✔
1238
              pTask->info.taskLevel);
1239
    } else {
1240
      // todo
1241
    }
1242
  } else {
1243
    stDebug("s-task:%s level:%d already send checkpoint-source rsp success to mnode", pTask->id.idStr,
5,801✔
1244
            pTask->info.taskLevel);
1245
  }
1246

1247
  streamMutexUnlock(&pTask->chkInfo.pActiveInfo->lock);
9,151✔
1248
  return TSDB_CODE_SUCCESS;
9,151✔
1249
}
1250

1251
int32_t streamAddBlockIntoDispatchMsg(const SSDataBlock* pBlock, SStreamDispatchReq* pReq) {
153,099✔
1252
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
153,099✔
1253
  int32_t dataStrLen = sizeof(SRetrieveTableRsp) + dataEncodeSize + PAYLOAD_PREFIX_LEN;
153,086✔
1254
  void*   buf = taosMemoryCalloc(1, dataStrLen);
153,086✔
1255
  if (buf == NULL) {
153,094!
1256
    return terrno;
×
1257
  }
1258

1259
  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
153,094✔
1260
  pRetrieve->useconds = 0;
153,094✔
1261
  pRetrieve->precision = TSDB_DEFAULT_PRECISION;
153,094✔
1262
  pRetrieve->compressed = 0;
153,094✔
1263
  pRetrieve->completed = 1;
153,094✔
1264
  pRetrieve->streamBlockType = pBlock->info.type;
153,094✔
1265
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
153,094✔
1266
  pRetrieve->skey = htobe64(pBlock->info.window.skey);
153,093✔
1267
  pRetrieve->ekey = htobe64(pBlock->info.window.ekey);
153,094✔
1268
  pRetrieve->version = htobe64(pBlock->info.version);
153,095✔
1269
  pRetrieve->watermark = htobe64(pBlock->info.watermark);
153,094✔
1270
  memcpy(pRetrieve->parTbName, pBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
153,091✔
1271

1272
  int32_t numOfCols = (int32_t)taosArrayGetSize(pBlock->pDataBlock);
153,091✔
1273
  pRetrieve->numOfCols = htonl(numOfCols);
153,099✔
1274

1275
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data + PAYLOAD_PREFIX_LEN, dataEncodeSize, numOfCols);
153,099✔
1276
  if (actualLen < 0) {
153,094!
1277
    taosMemoryFree(buf);
×
1278
    return terrno;
×
1279
  }
1280

1281
  SET_PAYLOAD_LEN(pRetrieve->data, actualLen, actualLen);
153,094✔
1282

1283
  int32_t payloadLen = actualLen + PAYLOAD_PREFIX_LEN;
153,094✔
1284
  pRetrieve->payloadLen = htonl(payloadLen);
153,094✔
1285
  pRetrieve->compLen = htonl(payloadLen);
153,094✔
1286

1287
  payloadLen += sizeof(SRetrieveTableRsp);
153,094✔
1288

1289
  void* px = taosArrayPush(pReq->dataLen, &payloadLen);
153,094✔
1290
  if (px == NULL) {
153,100!
1291
    taosMemoryFree(buf);
×
1292
    return terrno;
×
1293
  }
1294

1295
  px = taosArrayPush(pReq->data, &buf);
153,100✔
1296
  if (px == NULL) {
153,093!
1297
    taosMemoryFree(buf);
×
1298
    return terrno;
×
1299
  }
1300

1301
  pReq->totalLen += dataStrLen;
153,093✔
1302
  return 0;
153,093✔
1303
}
1304

1305
int32_t doSendDispatchMsg(SStreamTask* pTask, const SStreamDispatchReq* pReq, int32_t vgId, SEpSet* pEpSet) {
55,725✔
1306
  void*   buf = NULL;
55,725✔
1307
  int32_t code = 0;
55,725✔
1308
  SRpcMsg msg = {0};
55,725✔
1309

1310
  // serialize
1311
  int32_t tlen;
1312
  tEncodeSize(tEncodeStreamDispatchReq, pReq, tlen, code);
55,725!
1313
  if (code < 0) {
55,703!
1314
    goto FAIL;
×
1315
  }
1316

1317
  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
55,703✔
1318
  if (buf == NULL) {
55,718!
1319
    code = terrno;
×
1320
    goto FAIL;
×
1321
  }
1322

1323
  ((SMsgHead*)buf)->vgId = htonl(vgId);
55,718✔
1324
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
55,718✔
1325

1326
  SEncoder encoder;
1327
  tEncoderInit(&encoder, abuf, tlen);
55,718✔
1328
  if ((code = tEncodeStreamDispatchReq(&encoder, pReq)) < 0) {
55,720!
1329
    tEncoderClear(&encoder);
×
1330
    goto FAIL;
×
1331
  }
1332
  tEncoderClear(&encoder);
55,726✔
1333

1334
  initRpcMsg(&msg, pTask->msgInfo.msgType, buf, tlen + sizeof(SMsgHead));
55,737✔
1335
  stDebug("s-task:%s dispatch msg to taskId:0x%x vgId:%d data msg", pTask->id.idStr, pReq->taskId, vgId);
55,722✔
1336

1337
  return tmsgSendReq(pEpSet, &msg);
55,722✔
1338

1339
FAIL:
×
1340
  if (buf) {
×
1341
    rpcFreeCont(buf);
×
1342
  }
1343

1344
  if (code == -1) {
×
1345
    code = TSDB_CODE_INVALID_MSG;
×
1346
  }
1347

1348
  return code;
×
1349
}
1350

1351
int32_t streamTaskBuildCheckpointSourceRsp(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo, SRpcMsg* pMsg,
4,330✔
1352
                                           int32_t setCode) {
1353
  int32_t  len = 0;
4,330✔
1354
  int32_t  code = 0;
4,330✔
1355
  SEncoder encoder;
1356

1357
  SStreamCheckpointSourceRsp rsp = {
4,330✔
1358
      .checkpointId = pReq->checkpointId,
4,330✔
1359
      .taskId = pReq->taskId,
4,330✔
1360
      .nodeId = pReq->nodeId,
4,330✔
1361
      .streamId = pReq->streamId,
4,330✔
1362
      .expireTime = pReq->expireTime,
4,330✔
1363
      .mnodeId = pReq->mnodeId,
4,330✔
1364
      .success = (setCode == TSDB_CODE_SUCCESS) ? 1 : 0,
4,330✔
1365
  };
1366

1367
  tEncodeSize(tEncodeStreamCheckpointSourceRsp, &rsp, len, code);
4,330!
1368
  if (code < 0) {
4,332!
1369
    return code;
×
1370
  }
1371

1372
  void* pBuf = rpcMallocCont(sizeof(SMsgHead) + len);
4,332✔
1373
  if (pBuf == NULL) {
4,332!
1374
    return terrno;
×
1375
  }
1376

1377
  ((SMsgHead*)pBuf)->vgId = htonl(pReq->mnodeId);
4,332✔
1378
  void* abuf = POINTER_SHIFT(pBuf, sizeof(SMsgHead));
4,332✔
1379

1380
  tEncoderInit(&encoder, (uint8_t*)abuf, len);
4,332✔
1381
  code = tEncodeStreamCheckpointSourceRsp(&encoder, &rsp);
4,332✔
1382
  tEncoderClear(&encoder);
4,329✔
1383

1384
  if (code < 0) {
4,333!
1385
    rpcFreeCont(pBuf);
×
1386
    return code;
×
1387
  }
1388

1389
  code = TMIN(code, 0);
4,333✔
1390
  initRpcMsg(pMsg, 0, pBuf, sizeof(SMsgHead) + len);
4,333✔
1391

1392
  pMsg->code = setCode;
4,331✔
1393
  pMsg->info = *pRpcInfo;
4,331✔
1394
  return code;
4,331✔
1395
}
1396

1397
int32_t streamAddCheckpointSourceRspMsg(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo,
3,354✔
1398
                                        SStreamTask* pTask) {
1399
  STaskCheckpointReadyInfo info = {
6,710✔
1400
      .recvTs = taosGetTimestampMs(), .transId = pReq->transId, .checkpointId = pReq->checkpointId};
3,354✔
1401

1402
  // todo retry until it success
1403
  int32_t code = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &info.msg, TSDB_CODE_SUCCESS);
3,356✔
1404
  if (code) {
3,356!
1405
    stError("s-task:%s failed to build checkpoint-source rsp, code:%s", pTask->id.idStr, tstrerror(code));
×
1406
    return code;
×
1407
  }
1408

1409
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
3,356✔
1410
  streamMutexLock(&pActiveInfo->lock);
3,356✔
1411

1412
  int32_t size = taosArrayGetSize(pActiveInfo->pReadyMsgList);
3,356✔
1413
  if (size > 0) {
3,354!
1414
    STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, 0);
×
1415
    if (pReady == NULL) {
×
1416
      streamMutexUnlock(&pActiveInfo->lock);
×
1417
      return terrno;
×
1418
    }
1419

1420
    if (pReady->transId == pReq->transId) {
×
1421
      stWarn("s-task:%s repeatly recv checkpoint source msg from mnode, checkpointId:%" PRId64 ", ignore",
×
1422
             pTask->id.idStr, pReq->checkpointId);
1423
    } else {
1424
      stError("s-task:%s checkpointId:%" PRId64 " transId:%d not completed, new transId:%d checkpointId:%" PRId64
×
1425
              " recv from mnode",
1426
              pTask->id.idStr, pReady->checkpointId, pReady->transId, pReq->transId, pReq->checkpointId);
1427
    }
1428
  } else {
1429
    void* px = taosArrayPush(pActiveInfo->pReadyMsgList, &info);
3,354✔
1430
    if (px != NULL) {
3,354!
1431
      stDebug("s-task:%s add checkpoint source rsp msg, total:%d", pTask->id.idStr, size + 1);
3,354✔
1432
    } else {
1433
      stError("s-task:%s failed to add readyMsg, code: out of memory", pTask->id.idStr);
×
1434
    }
1435
  }
1436

1437
  streamMutexUnlock(&pActiveInfo->lock);
3,354✔
1438
  return TSDB_CODE_SUCCESS;
3,356✔
1439
}
1440

1441
void initCheckpointReadyInfo(STaskCheckpointReadyInfo* pReadyInfo, int32_t upstreamNodeId, int32_t upstreamTaskId,
8,754✔
1442
                             int32_t childId, SEpSet* pEpset, int64_t checkpointId) {
1443
  pReadyInfo->upstreamTaskId = upstreamTaskId;
8,754✔
1444
  pReadyInfo->upstreamNodeEpset = *pEpset;
8,754✔
1445
  pReadyInfo->upstreamNodeId = upstreamNodeId;
8,754✔
1446
  pReadyInfo->recvTs = taosGetTimestampMs();
8,759✔
1447
  pReadyInfo->checkpointId = checkpointId;
8,759✔
1448
  pReadyInfo->childId = childId;
8,759✔
1449
}
8,759✔
1450

1451
static int32_t doAddChkptReadyMsg(SStreamTask* pTask, STaskCheckpointReadyInfo* pInfo) {
8,757✔
1452
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
8,757✔
1453

1454
  void* px = taosArrayPush(pActiveInfo->pReadyMsgList, pInfo);
8,757✔
1455
  if (px == NULL) {
8,700!
1456
    stError("s-task:%s failed to add readyMsg info, code: out of memory", pTask->id.idStr);
×
1457
    return terrno;
×
1458
  }
1459

1460
  int32_t numOfRecv = taosArrayGetSize(pActiveInfo->pReadyMsgList);
8,700✔
1461
  int32_t total = streamTaskGetNumOfUpstream(pTask);
8,700✔
1462
  if (numOfRecv == total) {
8,732✔
1463
    stDebug("s-task:%s recv checkpoint-trigger from all upstream, continue", pTask->id.idStr);
3,320✔
1464
    pActiveInfo->allUpstreamTriggerRecv = 1;
3,320✔
1465
  } else {
1466
    stDebug("s-task:%s %d/%d checkpoint-trigger recv", pTask->id.idStr, numOfRecv, total);
5,412✔
1467
  }
1468

1469
  return 0;
8,719✔
1470
}
1471

1472
int32_t streamAddCheckpointReadyMsg(SStreamTask* pTask, int32_t upstreamTaskId, int32_t index, int64_t checkpointId) {
8,745✔
1473
  int32_t                  code = 0;
8,745✔
1474
  STaskCheckpointReadyInfo info = {0};
8,745✔
1475

1476
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
8,745!
1477
    return TSDB_CODE_SUCCESS;
×
1478
  }
1479

1480
  SStreamUpstreamEpInfo* pInfo = NULL;
8,745✔
1481
  streamTaskGetUpstreamTaskEpInfo(pTask, upstreamTaskId, &pInfo);
8,745✔
1482
  if (pInfo == NULL) {
8,757!
1483
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1484
  }
1485

1486
  initCheckpointReadyInfo(&info, pInfo->nodeId, pInfo->taskId, pInfo->childId, &pInfo->epSet, checkpointId);
8,757✔
1487

1488
  stDebug("s-task:%s (level:%d) prepare checkpoint-ready msg to upstream s-task:0x%" PRIx64 "-0x%x (vgId:%d) idx:%d",
8,759✔
1489
          pTask->id.idStr, pTask->info.taskLevel, pTask->id.streamId, pInfo->taskId, pInfo->nodeId, index);
1490

1491
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
8,759✔
1492

1493
  streamMutexLock(&pActiveInfo->lock);
8,759✔
1494
  code = doAddChkptReadyMsg(pTask, &info);
8,759✔
1495
  streamMutexUnlock(&pActiveInfo->lock);
8,720✔
1496
  return code;
8,766✔
1497
}
1498

1499
void streamClearChkptReadyMsg(SActiveCheckpointInfo* pActiveInfo) {
65,562✔
1500
  if (pActiveInfo == NULL) {
65,562✔
1501
    return;
45,854✔
1502
  }
1503

1504
  for (int i = 0; i < taosArrayGetSize(pActiveInfo->pReadyMsgList); i++) {
28,464✔
1505
    STaskCheckpointReadyInfo* pInfo = taosArrayGet(pActiveInfo->pReadyMsgList, i);
8,755✔
1506
    if (pInfo != NULL) {
8,752!
1507
      rpcFreeCont(pInfo->msg.pCont);
8,753✔
1508
    }
1509
  }
1510

1511
  taosArrayClear(pActiveInfo->pReadyMsgList);
19,707✔
1512
}
1513

1514
// this message has been sent successfully, let's try next one.
1515
static int32_t handleDispatchSuccessRsp(SStreamTask* pTask, int32_t downstreamId, int32_t downstreamNodeId) {
26,986✔
1516
  stDebug("s-task:%s destroy dispatch msg:%p", pTask->id.idStr, pTask->msgInfo.pData);
26,986✔
1517

1518
  int64_t el = taosGetTimestampMs() - pTask->msgInfo.startTs;
26,986✔
1519
  bool    delayDispatch = (pTask->msgInfo.dispatchMsgType == STREAM_INPUT__CHECKPOINT_TRIGGER);
26,986✔
1520

1521
  clearBufferedDispatchMsg(pTask);
26,986✔
1522

1523
  // put data into inputQ of current task is also allowed
1524
  if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
26,988✔
1525
    pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
1✔
1526
    stDebug("s-task:%s downstream task:0x%x resume to normal from inputQ blocking, blocking time:%" PRId64 "ms",
1!
1527
            pTask->id.idStr, downstreamId, el);
1528
  } else {
1529
    stDebug("s-task:%s dispatch completed, elapsed time:%" PRId64 "ms", pTask->id.idStr, el);
26,987✔
1530
  }
1531

1532
  // now ready for next data output
1533
  atomic_store_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL);
26,988✔
1534

1535
  // otherwise, continue dispatch the first block to down stream task in pipeline
1536
  if (delayDispatch) {
26,988✔
1537
    return 0;
3,362✔
1538
  } else {
1539
    return streamDispatchStreamBlock(pTask);
23,626✔
1540
  }
1541
}
1542

1543
static bool setDispatchRspInfo(SDispatchMsgInfo* pMsgInfo, int32_t vgId, int32_t code, int64_t now, int32_t* pNotRsp,
55,661✔
1544
                               int32_t* pFailed, const char* id) {
1545
  int32_t numOfRsp = 0;
55,661✔
1546
  int32_t numOfFailed = 0;
55,661✔
1547

1548
  bool    allRsp = false;
55,661✔
1549
  int32_t numOfDispatchBranch = taosArrayGetSize(pMsgInfo->pSendInfo);
55,661✔
1550

1551
  *pNotRsp = 0;
55,660✔
1552
  *pFailed = 0;
55,660✔
1553

1554
  for (int32_t i = 0; i < numOfDispatchBranch; ++i) {
191,079✔
1555
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, i);
135,429✔
1556
    if (pEntry == NULL) {
135,419!
1557
      continue;
×
1558
    }
1559

1560
    if (pEntry->rspTs != -1) {
135,419✔
1561
      numOfRsp += 1;
39,900✔
1562
    }
1563
  }
1564

1565
  for (int32_t j = 0; j < numOfDispatchBranch; ++j) {
95,540✔
1566
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, j);
95,521✔
1567
    if (pEntry == NULL) {
95,514!
1568
      continue;
×
1569
    }
1570

1571
    if (pEntry->nodeId == vgId) {
95,514✔
1572
      if (pEntry->rspTs != -1) {
55,624!
1573
        stDebug("s-task:%s dispatch rsp has already recved at:%" PRId64 ", ignore this rsp, msgId:%d", id,
×
1574
                pEntry->rspTs, pMsgInfo->msgId);
1575
        allRsp = false;
×
1576
      } else {
1577
        pEntry->rspTs = now;
55,624✔
1578
        pEntry->status = code;
55,624✔
1579
        numOfRsp += 1;
55,624✔
1580
        allRsp = (numOfRsp == numOfDispatchBranch);
55,624✔
1581

1582
        stDebug("s-task:%s record the rsp recv, ts:%" PRId64 " code:%d, idx:%d, total recv:%d/%d", id, now, code, j,
55,624✔
1583
                numOfRsp, numOfDispatchBranch);
1584
      }
1585
      break;
55,628✔
1586
    }
1587
  }
1588

1589
  // this code may be error code.
1590
  for (int32_t i = 0; i < numOfDispatchBranch; ++i) {
191,016✔
1591
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, i);
135,363✔
1592
    if (pEntry->status != TSDB_CODE_SUCCESS || isDispatchRspTimeout(pEntry, now)) {
135,357!
1593
      numOfFailed += 1;
1✔
1594
    }
1595
  }
1596

1597
  *pFailed = numOfFailed;
55,653✔
1598
  *pNotRsp = numOfDispatchBranch - numOfRsp;
55,653✔
1599

1600
  return allRsp;
55,653✔
1601
}
1602

1603
bool isDispatchRspTimeout(SDispatchEntry* pEntry, int64_t now) {
140,092✔
1604
  return (pEntry->rspTs == -1) && (now - pEntry->sendTs) > 30 * 1000;
140,092✔
1605
}
1606

1607
int32_t getFailedDispatchInfo(SDispatchMsgInfo* pMsgInfo, int64_t now) {
1,974✔
1608
  int32_t numOfFailed = 0;
1,974✔
1609
  streamMutexLock(&pMsgInfo->lock);
1,974✔
1610

1611
  for (int32_t j = 0; j < taosArrayGetSize(pMsgInfo->pSendInfo); ++j) {
6,698✔
1612
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, j);
4,724✔
1613
    if (pEntry == NULL) {
4,724!
1614
      continue;
×
1615
    }
1616

1617
    if (pEntry->status != TSDB_CODE_SUCCESS || isDispatchRspTimeout(pEntry, now)) {
4,724✔
1618
      numOfFailed += 1;
6✔
1619
    }
1620
  }
1621
  streamMutexUnlock(&pMsgInfo->lock);
1,974✔
1622
  return numOfFailed;
1,974✔
1623
}
1624

1625
int32_t streamProcessDispatchRsp(SStreamTask* pTask, SStreamDispatchRsp* pRsp, int32_t code) {
55,644✔
1626
  const char*            id = pTask->id.idStr;
55,644✔
1627
  int32_t                vgId = pTask->pMeta->vgId;
55,644✔
1628
  SDispatchMsgInfo*      pMsgInfo = &pTask->msgInfo;
55,644✔
1629
  int64_t                now = taosGetTimestampMs();
55,644✔
1630
  bool                   allRsp = false;
55,644✔
1631
  int32_t                notRsp = 0;
55,644✔
1632
  int32_t                numOfFailed = 0;
55,644✔
1633
  bool                   triggerDispatchRsp = false;
55,644✔
1634
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
55,644✔
1635
  int64_t                tmpCheckpointId = -1;
55,644✔
1636
  int32_t                tmpTranId = -1;
55,644✔
1637
  const char*            pStatus = NULL;
55,644✔
1638

1639
  // we only set the dispatch msg info for current checkpoint trans
1640
  streamMutexLock(&pTask->lock);
55,644✔
1641
  SStreamTaskState s = streamTaskGetStatus(pTask);
55,668✔
1642
  triggerDispatchRsp = (s.state == TASK_STATUS__CK) && (pInfo->activeId == pMsgInfo->checkpointId) &&
64,440!
1643
                       (pInfo->transId == pMsgInfo->transId);
8,773!
1644
  tmpCheckpointId = pInfo->activeId;
55,667✔
1645
  tmpTranId = pInfo->transId;
55,667✔
1646
  pStatus = s.name;
55,667✔
1647
  streamMutexUnlock(&pTask->lock);
55,667✔
1648

1649
  streamMutexLock(&pMsgInfo->lock);
55,670✔
1650
  int32_t msgId = pMsgInfo->msgId;
55,668✔
1651

1652
  // follower not handle the dispatch rsp
1653
  if ((pTask->pMeta->role == NODE_ROLE_FOLLOWER) || (pTask->status.downstreamReady != 1)) {
55,668!
1654
    stError("s-task:%s vgId:%d is follower or just re-launched, not handle the dispatch rsp, discard it", id, vgId);
×
1655
    streamMutexUnlock(&pMsgInfo->lock);
×
1656
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1657
  }
1658

1659
  // discard invalid dispatch rsp msg
1660
  if ((pRsp->msgId != msgId) || (pRsp->stage != pTask->pMeta->stage)) {
55,669✔
1661
    stError("s-task:%s vgId:%d not expect rsp, expected: msgId:%d, stage:%" PRId64 " actual msgId:%d, stage:%" PRId64
3!
1662
            " discard it",
1663
            id, vgId, msgId, pTask->pMeta->stage, pRsp->msgId, pRsp->stage);
1664
    streamMutexUnlock(&pMsgInfo->lock);
3✔
1665
    return TSDB_CODE_INVALID_MSG;
×
1666
  }
1667

1668
  if (code != TSDB_CODE_SUCCESS) {
55,666✔
1669
    // dispatch message failed: network error, or node not available.
1670
    // in case of the input queue is full, the code will be TSDB_CODE_SUCCESS, the and pRsp->inputStatus will be set
1671
    // flag. Here we need to retry dispatch this message to downstream task immediately. handle the case the failure
1672
    // happened too fast.
1673
    if (code == TSDB_CODE_STREAM_TASK_NOT_EXIST) {  // destination task does not exist, not retry anymore
91!
1674
      stError("s-task:%s failed to dispatch msg to task:0x%x(vgId:%d), msgId:%d no retry, since task destroyed already",
91!
1675
              id, pRsp->downstreamTaskId, pRsp->downstreamNodeId, msgId);
1676
      allRsp = setDispatchRspInfo(pMsgInfo, pRsp->downstreamNodeId, TSDB_CODE_SUCCESS, now, &notRsp, &numOfFailed, id);
91✔
1677
    } else {
1678
      stError("s-task:%s failed to dispatch msgId:%d to task:0x%x(vgId:%d), code:%s, add to retry list", id, msgId,
×
1679
              pRsp->downstreamTaskId, pRsp->downstreamNodeId, tstrerror(code));
1680
      allRsp = setDispatchRspInfo(pMsgInfo, pRsp->downstreamNodeId, code, now, &notRsp, &numOfFailed, id);
×
1681
    }
1682

1683
  } else {  // code == 0
1684
    if (pRsp->inputStatus == TASK_INPUT_STATUS__BLOCKED) {
55,575✔
1685
      pTask->inputq.status = TASK_INPUT_STATUS__BLOCKED;
1✔
1686
      // block the input of current task, to push pressure to upstream
1687
      allRsp = setDispatchRspInfo(pMsgInfo, pRsp->downstreamNodeId, pRsp->inputStatus, now, &notRsp, &numOfFailed, id);
1✔
1688
      stTrace("s-task:%s inputQ of downstream task:0x%x(vgId:%d) is full, wait for retry dispatch", id,
1!
1689
              pRsp->downstreamTaskId, pRsp->downstreamNodeId);
1690
    } else {
1691
      if (pRsp->inputStatus == TASK_INPUT_STATUS__REFUSED) {
55,574✔
1692
        // todo handle the role-changed during checkpoint generation, add test case
1693
        stError(
170!
1694
            "s-task:%s downstream task:0x%x(vgId:%d) refused the dispatch msg, downstream may become follower or "
1695
            "restart already, treat it as success",
1696
            id, pRsp->downstreamTaskId, pRsp->downstreamNodeId);
1697
      }
1698

1699
      allRsp = setDispatchRspInfo(pMsgInfo, pRsp->downstreamNodeId, TSDB_CODE_SUCCESS, now, &notRsp, &numOfFailed, id);
55,574✔
1700

1701
      {
1702
        bool delayDispatch = (pMsgInfo->dispatchMsgType == STREAM_INPUT__CHECKPOINT_TRIGGER);
55,539✔
1703
        if (delayDispatch) {
55,539✔
1704
          // we only set the dispatch msg info for current checkpoint trans
1705
          if (triggerDispatchRsp) {
8,744!
1706
            stDebug("s-task:%s checkpoint-trigger msg to 0x%x rsp for checkpointId:%" PRId64 " transId:%d confirmed",
8,744✔
1707
                    pTask->id.idStr, pRsp->downstreamTaskId, pMsgInfo->checkpointId, pMsgInfo->transId);
1708

1709
            streamTaskSetTriggerDispatchConfirmed(pTask, pRsp->downstreamNodeId);
8,744✔
1710
          } else {
1711
            stWarn("s-task:%s checkpoint-trigger msg rsp for checkpointId:%" PRId64
×
1712
                   " transId:%d discard, current status:%s, active checkpointId:%" PRId64
1713
                   " active transId:%d, since expired",
1714
                   pTask->id.idStr, pMsgInfo->checkpointId, pMsgInfo->transId, pStatus, tmpCheckpointId, tmpTranId);
1715
          }
1716
        }
1717
      }
1718
    }
1719
  }
1720

1721
  streamMutexUnlock(&pMsgInfo->lock);
55,660✔
1722

1723
  if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
55,668✔
1724
    if (!allRsp) {
50,930✔
1725
      stDebug(
26,253✔
1726
          "s-task:%s recv dispatch rsp, msgId:%d from 0x%x(vgId:%d), downstream task input status:%d code:%s, "
1727
          "waiting for %d rsp",
1728
          id, msgId, pRsp->downstreamTaskId, pRsp->downstreamNodeId, pRsp->inputStatus, tstrerror(code), notRsp);
1729
    } else {
1730
      stDebug(
24,677✔
1731
          "s-task:%s recv dispatch rsp, msgId:%d from 0x%x(vgId:%d), downstream task input status:%d code:%s, all "
1732
          "rsp",
1733
          id, msgId, pRsp->downstreamTaskId, pRsp->downstreamNodeId, pRsp->inputStatus, tstrerror(code));
1734
    }
1735
  } else {
1736
    stDebug("s-task:%s recv fix-dispatch rsp, msgId:%d from 0x%x(vgId:%d), downstream task input status:%d code:%s", id,
4,738✔
1737
            msgId, pRsp->downstreamTaskId, pRsp->downstreamNodeId, pRsp->inputStatus, tstrerror(code));
1738
  }
1739

1740
  // all msg rsp already, continue
1741
  // we need to re-try send dispatch msg to downstream tasks
1742
  if (allRsp && (numOfFailed == 0)) {
55,667✔
1743
    // trans-state msg has been sent to downstream successfully. let's transfer the fill-history task state
1744
    if (pMsgInfo->dispatchMsgType == STREAM_INPUT__TRANS_STATE) {
29,414✔
1745
      stDebug("s-task:%s dispatch trans-state msgId:%d to downstream successfully, start to prepare transfer state", id,
2,428✔
1746
              msgId);
1747
      if (pTask->info.fillHistory != 1) {
2,428!
1748
        stFatal("s-task:%s unexpected dispatch rsp, not scan-history task, not recv this dispatch rsp", id);
×
1749
      }
1750

1751
      code = streamTransferStatePrepare(pTask);
2,428✔
1752
      if (code != TSDB_CODE_SUCCESS) {  // todo: do nothing if error happens
1753
      }
1754

1755
      clearBufferedDispatchMsg(pTask);
2,428✔
1756

1757
      // now ready for next data output
1758
      atomic_store_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL);
2,428✔
1759
    } else {
1760
      // this message has been sent successfully, let's try next one.
1761
      code = handleDispatchSuccessRsp(pTask, pRsp->downstreamTaskId, pRsp->downstreamNodeId);
26,986✔
1762
    }
1763
  }
1764

1765
  return code;
55,668✔
1766
}
1767

1768
static int32_t buildDispatchRsp(const SStreamTask* pTask, const SStreamDispatchReq* pReq, int32_t status, void** pBuf) {
55,528✔
1769
  *pBuf = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
55,528✔
1770
  if (*pBuf == NULL) {
55,558✔
1771
    return terrno;
7✔
1772
  }
1773

1774
  ((SMsgHead*)(*pBuf))->vgId = htonl(pReq->upstreamNodeId);
55,551✔
1775

1776
  if (((SMsgHead*)(*pBuf))->vgId == 0) {
55,551!
1777
    return TSDB_CODE_INVALID_MSG;
×
1778
  }
1779

1780
  SStreamDispatchRsp* pDispatchRsp = POINTER_SHIFT((*pBuf), sizeof(SMsgHead));
55,551✔
1781

1782
  pDispatchRsp->stage = htobe64(pReq->stage);
55,551✔
1783
  pDispatchRsp->msgId = htonl(pReq->msgId);
55,525✔
1784
  pDispatchRsp->inputStatus = status;
55,525✔
1785
  pDispatchRsp->streamId = htobe64(pReq->streamId);
55,525✔
1786
  pDispatchRsp->upstreamNodeId = htonl(pReq->upstreamNodeId);
55,556✔
1787
  pDispatchRsp->upstreamTaskId = htonl(pReq->upstreamTaskId);
55,556✔
1788
  pDispatchRsp->downstreamNodeId = htonl(pTask->info.nodeId);
55,556✔
1789
  pDispatchRsp->downstreamTaskId = htonl(pTask->id.taskId);
55,556✔
1790

1791
  return TSDB_CODE_SUCCESS;
55,556✔
1792
}
1793

1794
static int32_t streamTaskAppendInputBlocks(SStreamTask* pTask, const SStreamDispatchReq* pReq) {
55,355✔
1795
  int8_t status = 0;
55,355✔
1796

1797
  SStreamDataBlock* pBlock = NULL;
55,355✔
1798

1799
  int32_t code = createStreamBlockFromDispatchMsg(pReq, pReq->type, pReq->srcVgId, &pBlock);
55,355✔
1800
  if (code) {
55,361!
1801
    streamTaskInputFail(pTask);
×
1802
    status = TASK_INPUT_STATUS__FAILED;
×
1803
    stError("vgId:%d, s-task:%s failed to receive dispatch msg, reason: out of memory", pTask->pMeta->vgId,
×
1804
            pTask->id.idStr);
1805
  } else {
1806
    if (pBlock->type == STREAM_INPUT__TRANS_STATE) {
55,361✔
1807
      pTask->status.appendTranstateBlock = true;
7,220✔
1808
    }
1809

1810
    code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pBlock);
55,361✔
1811
    // input queue is full, upstream is blocked now
1812
    status = (code == TSDB_CODE_SUCCESS) ? TASK_INPUT_STATUS__NORMAL : TASK_INPUT_STATUS__BLOCKED;
55,374!
1813
  }
1814

1815
  return status;
55,387✔
1816
}
1817

1818
int32_t streamProcessDispatchMsg(SStreamTask* pTask, SStreamDispatchReq* pReq, SRpcMsg* pRsp) {
55,540✔
1819
  int32_t      status = 0;
55,540✔
1820
  SStreamMeta* pMeta = pTask->pMeta;
55,540✔
1821
  const char*  id = pTask->id.idStr;
55,540✔
1822

1823
  stDebug("s-task:%s receive dispatch msg from taskId:0x%x(vgId:%d), msgLen:%" PRId64 ", msgId:%d", id,
55,540✔
1824
          pReq->upstreamTaskId, pReq->upstreamNodeId, pReq->totalLen, pReq->msgId);
1825

1826
  SStreamUpstreamEpInfo* pInfo = NULL;
55,540✔
1827
  streamTaskGetUpstreamTaskEpInfo(pTask, pReq->upstreamTaskId, &pInfo);
55,540✔
1828
  if (pInfo == NULL) {
55,538!
1829
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1830
  }
1831

1832
  if (pMeta->role == NODE_ROLE_FOLLOWER) {
55,538!
1833
    stError("s-task:%s task on follower received dispatch msgs, dispatch msg rejected", id);
×
1834
    status = TASK_INPUT_STATUS__REFUSED;
×
1835
  } else {
1836
    if (pReq->stage > pInfo->stage) {
55,538✔
1837
      // upstream task has restarted/leader-follower switch/transferred to other dnodes
1838
      stError("s-task:%s upstream task:0x%x (vgId:%d) has restart/leader-switch/vnode-transfer, prev stage:%" PRId64
170!
1839
              ", current:%" PRId64 " dispatch msg rejected",
1840
              id, pReq->upstreamTaskId, pReq->upstreamNodeId, pInfo->stage, pReq->stage);
1841
      status = TASK_INPUT_STATUS__REFUSED;
170✔
1842
    } else {
1843
      if (!pInfo->dataAllowed) {
55,368✔
1844
        stWarn("s-task:%s data from task:0x%x is denied, since inputQ is closed for it", id, pReq->upstreamTaskId);
1!
1845
        status = TASK_INPUT_STATUS__BLOCKED;
1✔
1846
      } else {
1847
        // This task has received the checkpoint req from the upstream task, from which all the messages should be
1848
        // blocked. Note that there is no race condition here.
1849
        if (pReq->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
55,367✔
1850
          streamTaskCloseUpstreamInput(pTask, pReq->upstreamTaskId);
8,749✔
1851
          stDebug("s-task:%s close inputQ for upstream:0x%x, msgId:%d", id, pReq->upstreamTaskId, pReq->msgId);
8,773✔
1852
        } else if (pReq->type == STREAM_INPUT__TRANS_STATE) {
46,618✔
1853
          stDebug("s-task:%s recv trans-state msgId:%d from upstream:0x%x", id, pReq->msgId, pReq->upstreamTaskId);
7,230✔
1854
        }
1855

1856
        status = streamTaskAppendInputBlocks(pTask, pReq);
55,391✔
1857
      }
1858
    }
1859
  }
1860

1861
  {
1862
    // do send response with the input status
1863
    int32_t code = buildDispatchRsp(pTask, pReq, status, &pRsp->pCont);
55,542✔
1864
    if (code != TSDB_CODE_SUCCESS) {
55,562!
1865
      stError("s-task:%s failed to build dispatch rsp, msgId:%d, code:%s", id, pReq->msgId, tstrerror(code));
×
1866
      return code;
×
1867
    }
1868

1869
    pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp);
55,562✔
1870
    tmsgSendRsp(pRsp);
55,562✔
1871
  }
1872

1873
  return streamTrySchedExec(pTask);
55,557✔
1874
}
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