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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

63.4
/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) {
46,149✔
38
  pMsg->msgType = msgType;
46,149✔
39
  pMsg->pCont = pCont;
46,149✔
40
  pMsg->contLen = contLen;
46,149✔
41
}
46,149✔
42

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

49
static void clearDispatchInfo(SDispatchMsgInfo* pInfo) {
11,969✔
50
  pInfo->startTs = -1;
11,969✔
51
  pInfo->msgId = -1;
11,969✔
52
  pInfo->rspTs = -1;
11,969✔
53
}
11,969✔
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,
27,775✔
58
                                      int32_t numOfBlocks, int64_t dstTaskId, int32_t type) {
59
  pReq->streamId = pTask->id.streamId;
27,775✔
60
  pReq->srcVgId = vgId;
27,775✔
61
  pReq->stage = pTask->pMeta->stage;
27,775✔
62
  pReq->msgId = pTask->msgInfo.msgId;
27,775✔
63
  pReq->upstreamTaskId = pTask->id.taskId;
27,775✔
64
  pReq->upstreamChildId = pTask->info.selfChildId;
27,775✔
65
  pReq->upstreamNodeId = pTask->info.nodeId;
27,775✔
66
  pReq->upstreamRelTaskId = pTask->streamTaskId.taskId;
27,775✔
67
  pReq->blockNum = numOfBlocks;
27,775✔
68
  pReq->taskId = dstTaskId;
27,775✔
69
  pReq->type = type;
27,775✔
70

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

79
  return TSDB_CODE_SUCCESS;
27,775✔
80
}
81

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

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

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

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

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

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

123
    if (code < 0) {
442!
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};
442✔
130
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE, buf, len + sizeof(SMsgHead));
442✔
131

132
    code = tmsgSendReq(&pEpInfo->epSet, &rpcMsg);
442✔
133
    if (code != 0) {
442!
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,
442!
139
            pTask->info.selfChildId, pEpInfo->taskId, pEpInfo->nodeId, req->reqId);
140
  }
141

142
  return code;
135✔
143
}
144

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

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

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

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

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

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

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

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

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

196
  return code;
129✔
197
}
198

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

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

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

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

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

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

232
  return tmsgSendReq(pEpSet, &msg);
11,522✔
233
}
234

235
void destroyDispatchMsg(SStreamDispatchReq* pReq, int32_t numOfVgroups) {
11,969✔
236
  for (int32_t i = 0; i < numOfVgroups; i++) {
39,744✔
237
    taosArrayDestroyP(pReq[i].data, taosMemoryFree);
27,775✔
238
    taosArrayDestroy(pReq[i].dataLen);
27,775✔
239
  }
240

241
  taosMemoryFree(pReq);
11,969✔
242
}
11,969✔
243

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

250
  streamMutexLock(&pMsgInfo->lock);
11,969✔
251

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

257
  clearDispatchInfo(pMsgInfo);
11,969✔
258

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

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

268
  if (type != TASK_OUTPUT__SHUFFLE_DISPATCH && type != TASK_OUTPUT__FIXED_DISPATCH) {
11,969!
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));
11,969✔
275
  if (pReqs == NULL) {
11,969!
276
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
277
    return NULL;
×
278
  }
279

280
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
11,969✔
281
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
8,674✔
282
    int32_t numOfVgroups = taosArrayGetSize(vgInfo);
8,674✔
283

284
    for (int32_t i = 0; i < numOfVgroups; i++) {
33,154✔
285
      SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
24,480✔
286
      if (pVgInfo == NULL) {
24,480!
287
        continue;
×
288
      }
289

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

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

309
  return pReqs;
11,969✔
310
}
311

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

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

323
  pTask->msgInfo.dispatchMsgType = pData->type;
11,969✔
324

325
  if (pData->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
11,969✔
326
    SSDataBlock* p = taosArrayGet(pData->blocks, 0);
1,384✔
327
    if (p == NULL) {
1,384!
328
      return terrno;
×
329
    }
330

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

335
  SStreamDispatchReq* pReqs = createDispatchDataReq(pTask, pData);
11,969✔
336
  if (pReqs == NULL) {
11,969!
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) {
11,969✔
342
    for (int32_t i = 0; i < numOfBlocks; i++) {
8,267✔
343
      SSDataBlock* pDataBlock = taosArrayGet(pData->blocks, i);
4,972✔
344
      if (pDataBlock == NULL) {
4,972!
345
        destroyDispatchMsg(pReqs, 1);
×
346
        return terrno;
×
347
      }
348

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

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

362
    for (int32_t i = 0; i < numOfBlocks; i++) {
23,859✔
363
      SSDataBlock* pDataBlock = taosArrayGet(pData->blocks, i);
15,185✔
364
      if (pDataBlock == NULL) {
15,185!
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 ||
15,185✔
371
          pDataBlock->info.type == STREAM_TRANS_STATE) {
13,469✔
372
        for (int32_t j = 0; j < numOfVgroups; j++) {
12,159✔
373
          code = streamAddBlockIntoDispatchMsg(pDataBlock, &pReqs[j]);
9,188✔
374
          if (code != 0) {
9,188!
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) {
9,188✔
381
            SVgroupInfo* pDstVgroupInfo = taosArrayGet(vgInfo, j);
9,036✔
382
            if (pDstVgroupInfo == NULL) {
9,036!
383
              destroyDispatchMsg(pReqs, numOfVgroups);
×
384
              return terrno;
×
385
            }
386
            addDispatchEntry(&pTask->msgInfo, pDstVgroupInfo->vgId, now, true);
9,036✔
387
          }
388

389
          pReqs[j].blockNum++;
9,188✔
390
        }
391

392
        continue;
2,971✔
393
      }
394

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

402
    pTask->msgInfo.pData = pReqs;
8,674✔
403
  }
404

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

414
  return code;
11,969✔
415
}
416

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

422
  if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
11,969✔
423
    int32_t vgId = pTask->outputInfo.fixedDispatcher.nodeId;
3,295✔
424
    SEpSet* pEpSet = &pTask->outputInfo.fixedDispatcher.epSet;
3,295✔
425
    int32_t downstreamTaskId = pTask->outputInfo.fixedDispatcher.taskId;
3,295✔
426

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

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

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

439
    int32_t numOfSend = 0;
8,674✔
440
    for (int32_t i = 0; i < numOfVgroups; i++) {
20,307!
441
      if (pDispatchMsg[i].blockNum > 0) {
20,307✔
442
        SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
15,679✔
443
        if (pVgInfo == NULL) {
15,679!
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,
15,679✔
449
                pDispatchMsg[i].blockNum, pVgInfo->vgId);
450

451
        code = doSendDispatchMsg(pTask, &pDispatchMsg[i], pVgInfo->vgId, &pVgInfo->epSet);
15,679✔
452
        if (code < 0) {
15,679✔
453
          break;
1✔
454
        }
455

456
        // no need to try remain, all already send.
457
        if (++numOfSend == numOfBranches) {
15,678✔
458
          break;
8,673✔
459
        }
460
      }
461
    }
462

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

466
  return code;
11,969✔
467
}
468

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

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

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

485
  if (lock) {
18,975✔
486
    streamMutexLock(&pMsgInfo->lock);
12,331✔
487
  }
488

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

494
  if (lock) {
18,975✔
495
    streamMutexUnlock(&pMsgInfo->lock);
12,331✔
496
  }
497
}
18,975✔
498

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

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

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

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

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

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

541
  pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
7,201✔
542
  if (pTask == NULL) {
7,201!
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;
7,201✔
549
  vgId = pTask->pMeta->vgId;
7,201✔
550
  pMsgInfo = &pTask->msgInfo;
7,201✔
551
  msgId = pMsgInfo->msgId;
7,201✔
552

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

555
  if (streamTaskShouldStop(pTask)) {
7,201!
UNCOV
556
    stDebug("s-task:%s should stop, abort from timer", pTask->id.idStr);
×
UNCOV
557
    setNotInDispatchMonitor(pMsgInfo);
×
UNCOV
558
    cleanupInMonitor(pTask->id.taskId, taskRefId, param);
×
UNCOV
559
    return;
×
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)) {
7,201!
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);
7,201✔
572
  if (pTask->outputq.status == TASK_OUTPUT_STATUS__NORMAL) {
7,201✔
573
    stDebug("s-task:%s not in dispatch procedure, abort from timer", pTask->id.idStr);
5,876✔
574
    pMsgInfo->inMonitor = 0;
5,876✔
575
    inDispatch = false;
5,876✔
576
  }
577

578
  streamMutexUnlock(&pMsgInfo->lock);
7,201✔
579

580
  if (!inDispatch) {
7,201✔
581
    cleanupInMonitor(pTask->id.taskId, taskRefId, param);
5,876✔
582
    return;
5,876✔
583
  }
584

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

593
  {
UNCOV
594
    SStreamDispatchReq* pReq = pTask->msgInfo.pData;
×
595

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

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

607
        if (pEntry->status == TSDB_CODE_SUCCESS && pEntry->rspTs > 0) {
×
608
          continue;
×
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
×
613
          doSendFailedDispatch(pTask, pEntry, now, "timeout");
×
614
          numOfRetry += 1;
×
615
          continue;
×
616
        }
617

618
        // downstream inputQ is closed
619
        if (pEntry->status == TASK_INPUT_STATUS__BLOCKED) {
×
620
          doSendFailedDispatch(pTask, pEntry, now, "downstream inputQ blocked");
×
621
          numOfRetry += 1;
×
622
          continue;
×
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,
×
633
              numOfRetry, msgId);
634
    } else {
UNCOV
635
      int32_t dstVgId = pTask->outputInfo.fixedDispatcher.nodeId;
×
UNCOV
636
      SEpSet* pEpSet = &pTask->outputInfo.fixedDispatcher.epSet;
×
UNCOV
637
      int32_t downstreamTaskId = pTask->outputInfo.fixedDispatcher.taskId;
×
638

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

UNCOV
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

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

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

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

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

678
  *pFound = false;
12,214✔
679

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

686
    if (hashValue >= pVgInfo->hashBegin && hashValue <= pVgInfo->hashEnd) {
24,431!
687
      if ((code = streamAddBlockIntoDispatchMsg(pDataBlock, &pReqs[j])) < 0) {
12,214!
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) {
12,214✔
693
        SVgroupInfo* pDstVgroupInfo = taosArrayGet(vgInfo, j);
6,644✔
694
        if (pDstVgroupInfo != NULL) {
6,644!
695
          addDispatchEntry(&pTask->msgInfo, pDstVgroupInfo->vgId, now, false);
6,644✔
696
        }
697
      }
698

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

705
  return code;
12,213✔
706
}
707

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

715
  if (pTask->pNameMap == NULL) {
12,214✔
716
    pTask->pNameMap = tSimpleHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1,404✔
717
    if (pTask->pNameMap == NULL) {
1,404!
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));
12,214✔
724
  if (pVal) {
12,214✔
725
    SBlockName* pBln = (SBlockName*)pVal;
9,123✔
726
    hashValue = pBln->hashValue;
9,123✔
727
    if (!pDataBlock->info.parTbName[0]) {
9,123✔
728
      memset(pDataBlock->info.parTbName, 0, TSDB_TABLE_NAME_LEN);
3,711✔
729
      memcpy(pDataBlock->info.parTbName, pBln->parTbName, strlen(pBln->parTbName));
3,711✔
730
    }
731
  } else {
732
    char ctbName[TSDB_TABLE_FNAME_LEN] = {0};
3,091✔
733
    if (pDataBlock->info.parTbName[0]) {
3,091✔
734
      if (pTask->subtableWithoutMd5 != 1 && !isAutoTableName(pDataBlock->info.parTbName) &&
2,621!
735
          !alreadyAddGroupId(pDataBlock->info.parTbName, groupId) && groupId != 0) {
693!
736
        if (pTask->ver == SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
689!
737
          code = buildCtbNameAddGroupId(NULL, pDataBlock->info.parTbName, groupId, sizeof(pDataBlock->info.parTbName));
×
738
        } else if (pTask->ver > SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
689!
739
          code = buildCtbNameAddGroupId(pTask->outputInfo.shuffleDispatcher.stbFullName, pDataBlock->info.parTbName,
689✔
740
                                        groupId, sizeof(pDataBlock->info.parTbName));
741
        }
742
        if (code != TSDB_CODE_SUCCESS) {
689!
743
          return code;
×
744
        }
745
      }
746
    } else {
747
      code = buildCtbNameByGroupIdImpl(pTask->outputInfo.shuffleDispatcher.stbFullName, groupId,
470✔
748
                                       pDataBlock->info.parTbName);
470✔
749
      if (code) {
470!
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,
3,091✔
756
             pDataBlock->info.parTbName);
3,091✔
757
    /*uint32_t hashValue = MurmurHash3_32(ctbName, strlen(ctbName));*/
758
    SUseDbRsp* pDbInfo = &pTask->outputInfo.shuffleDispatcher.dbInfo;
3,091✔
759
    hashValue =
3,091✔
760
        taosGetTbHashVal(ctbName, strlen(ctbName), pDbInfo->hashMethod, pDbInfo->hashPrefix, pDbInfo->hashSuffix);
3,091✔
761
    SBlockName bln = {0};
3,091✔
762
    bln.hashValue = hashValue;
3,091✔
763
    memcpy(bln.parTbName, pDataBlock->info.parTbName, strlen(pDataBlock->info.parTbName));
3,091✔
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
3,091!
767
      code = tSimpleHashPut(pTask->pNameMap, &groupId, sizeof(int64_t), &bln, sizeof(SBlockName));
3,091✔
768
    }
769
  }
770

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

775
  if (code) {
12,214!
776
    return code;
×
777
  }
778

779
  if (!found) {
12,214!
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;
12,214✔
784
  }
785
}
786

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

794
  int32_t numOfElems = streamQueueGetNumOfItems(pTask->outputq.queue);
21,181✔
795
  if (numOfElems > 0) {
21,181!
796
    double  size = SIZE_IN_MiB(taosQueueMemorySize(pTask->outputq.queue->pQueue));
21,181✔
797
    int32_t numOfUnAccessed = streamQueueGetNumOfUnAccessedItems(pTask->outputq.queue);
21,181✔
798
    stDebug("s-task:%s start to dispatch intermediate block to downstream, elem in outputQ:%d/%d, size:%.2fMiB", id,
21,181✔
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);
21,181✔
804
  if (old != TASK_OUTPUT_STATUS__NORMAL) {
21,181✔
805
    stDebug("s-task:%s wait for dispatch rsp, not dispatch now, output status:%d", id, old);
1,765✔
806
    return 0;
1,765✔
807
  }
808

809
  if (pInfo->dispatchTrigger) {
19,416!
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) {
19,416!
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);
19,416✔
824
  }
825

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

834
    int32_t type = pBlock->type;
11,969✔
835
    if (!(type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__CHECKPOINT_TRIGGER ||
11,969!
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;
11,969✔
842

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

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

853
    if (type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
11,969✔
854
      // outputQ should be empty here, otherwise, set the checkpoint failed due to the retrieve req happens
855
      if (streamQueueGetNumOfUnAccessedItems(pTask->outputq.queue) > 0) {
1,384!
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);
1,384✔
866
      if (code != TSDB_CODE_SUCCESS) {  // todo handle error
867
      }
868
    }
869

870
    break;
11,969✔
871
  }
872

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

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

880
  if (shouldStop) {
11,969✔
881
    stDebug("s-task:%s in stop/dropping status, not start dispatch monitor tmr", id);
20!
882
  } else {
883
    streamMutexLock(&pTask->msgInfo.lock);
11,949✔
884
    if (pTask->msgInfo.inMonitor == 0) {
11,949✔
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,
6,212✔
887
              tstrerror(code));
888
      streamStartMonitorDispatchData(pTask, DISPATCH_RETRY_INTERVAL_MS);
6,212✔
889
      pTask->msgInfo.inMonitor = 1;
6,212✔
890
    } else {
891
      stDebug("s-task:%s already in dispatch monitor tmr", id);
5,737✔
892
    }
893

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

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

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

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

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

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

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

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

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

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

948
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
×
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)) {
×
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) {
×
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;
×
971
}
972

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

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

986
    if (pInfo->sendCompleted == 1) {
×
987
      continue;
×
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;
×
1001
  return 0;
×
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) {
×
1043
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1044
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptReadyMsgTmr;
×
1045
  SArray*                pList = pActiveInfo->pReadyMsgList;
×
1046
  int32_t                num = taosArrayGetSize(pList);
×
1047
  int32_t                vgId = pTask->pMeta->vgId;
×
1048
  int32_t                checkpointId = pActiveInfo->activeId;
×
1049
  const char*            id = pTask->id.idStr;
×
1050
  int32_t                notRsp = 0;
×
1051

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

1057
  code = doFindNotConfirmUpstream(&pNotRspList, pList, num, vgId, pTask->info.taskLevel, id);
×
1058
  if (code) {
×
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);
×
1065
  if (notRsp == 0) {
×
1066
    streamClearChkptReadyMsg(pActiveInfo);
×
1067
  } else {
1068
    doSendChkptReadyMsg(pTask, pNotRspList, checkpointId, pList);
×
1069
  }
1070

1071
  return code;
×
1072
}
1073

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

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

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

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

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

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

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

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

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

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

1141
  notRsp = taosArrayGetSize(pNotRspList);
×
1142
  if (notRsp > 0) {  // send checkpoint-ready msg again
×
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);
×
1148
    stDebug(
×
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);
×
1156
  taosArrayDestroy(pNotRspList);
×
1157
}
1158

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

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

1167
  streamMutexLock(&pActiveInfo->lock);
1,352✔
1168

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

1176
  for (int32_t i = 0; i < num; ++i) {
5,559✔
1177
    STaskCheckpointReadyInfo* pInfo = taosArrayGet(pList, i);
4,207✔
1178
    if (pInfo == NULL) {
4,207!
1179
      continue;
×
1180
    }
1181

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

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

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

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

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

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

1222
  streamMutexUnlock(&pActiveInfo->lock);
1,352✔
1223
  return TSDB_CODE_SUCCESS;
1,352✔
1224
}
1225

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

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

1246
  streamMutexUnlock(&pTask->chkInfo.pActiveInfo->lock);
4,305✔
1247
  return TSDB_CODE_SUCCESS;
4,305✔
1248
}
1249

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

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

1271
  int32_t numOfCols = (int32_t)taosArrayGetSize(pBlock->pDataBlock);
26,374✔
1272
  pRetrieve->numOfCols = htonl(numOfCols);
26,374✔
1273

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

1280
  SET_PAYLOAD_LEN(pRetrieve->data, actualLen, actualLen);
26,374✔
1281

1282
  int32_t payloadLen = actualLen + PAYLOAD_PREFIX_LEN;
26,374✔
1283
  pRetrieve->payloadLen = htonl(payloadLen);
26,374✔
1284
  pRetrieve->compLen = htonl(payloadLen);
26,374✔
1285

1286
  payloadLen += sizeof(SRetrieveTableRsp);
26,374✔
1287

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

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

1300
  pReq->totalLen += dataStrLen;
26,374✔
1301
  return 0;
26,374✔
1302
}
1303

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

1309
  // serialize
1310
  int32_t tlen;
1311
  tEncodeSize(tEncodeStreamDispatchReq, pReq, tlen, code);
18,974!
1312
  if (code < 0) {
18,974!
1313
    goto FAIL;
×
1314
  }
1315

1316
  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
18,974✔
1317
  if (buf == NULL) {
18,974!
1318
    code = terrno;
×
1319
    goto FAIL;
×
1320
  }
1321

1322
  ((SMsgHead*)buf)->vgId = htonl(vgId);
18,974✔
1323
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
18,974✔
1324

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

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

1336
  return tmsgSendReq(pEpSet, &msg);
18,974✔
1337

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

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

1347
  return code;
×
1348
}
1349

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

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

1366
  tEncodeSize(tEncodeStreamCheckpointSourceRsp, &rsp, len, code);
1,378!
1367
  if (code < 0) {
1,378!
1368
    return code;
×
1369
  }
1370

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

1376
  ((SMsgHead*)pBuf)->vgId = htonl(pReq->mnodeId);
1,378✔
1377
  void* abuf = POINTER_SHIFT(pBuf, sizeof(SMsgHead));
1,378✔
1378

1379
  tEncoderInit(&encoder, (uint8_t*)abuf, len);
1,378✔
1380
  code = tEncodeStreamCheckpointSourceRsp(&encoder, &rsp);
1,378✔
1381
  tEncoderClear(&encoder);
1,378✔
1382

1383
  if (code < 0) {
1,378!
1384
    rpcFreeCont(pBuf);
×
1385
    return code;
×
1386
  }
1387

1388
  code = TMIN(code, 0);
1,378✔
1389
  initRpcMsg(pMsg, 0, pBuf, sizeof(SMsgHead) + len);
1,378✔
1390

1391
  pMsg->code = setCode;
1,378✔
1392
  pMsg->info = *pRpcInfo;
1,378✔
1393
  return code;
1,378✔
1394
}
1395

1396
int32_t streamAddCheckpointSourceRspMsg(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo,
1,375✔
1397
                                        SStreamTask* pTask) {
1398
  STaskCheckpointReadyInfo info = {
2,750✔
1399
      .recvTs = taosGetTimestampMs(), .transId = pReq->transId, .checkpointId = pReq->checkpointId};
1,375✔
1400

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

1408
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1,375✔
1409
  streamMutexLock(&pActiveInfo->lock);
1,375✔
1410

1411
  int32_t size = taosArrayGetSize(pActiveInfo->pReadyMsgList);
1,375✔
1412
  if (size > 0) {
1,375!
1413
    STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, 0);
×
1414
    if (pReady == NULL) {
×
1415
      return terrno;
×
1416
    }
1417

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

1435
  streamMutexUnlock(&pActiveInfo->lock);
1,375✔
1436
  return TSDB_CODE_SUCCESS;
1,375✔
1437
}
1438

1439
void initCheckpointReadyInfo(STaskCheckpointReadyInfo* pReadyInfo, int32_t upstreamNodeId, int32_t upstreamTaskId,
4,213✔
1440
                             int32_t childId, SEpSet* pEpset, int64_t checkpointId) {
1441
  pReadyInfo->upstreamTaskId = upstreamTaskId;
4,213✔
1442
  pReadyInfo->upstreamNodeEpset = *pEpset;
4,213✔
1443
  pReadyInfo->upstreamNodeId = upstreamNodeId;
4,213✔
1444
  pReadyInfo->recvTs = taosGetTimestampMs();
4,213✔
1445
  pReadyInfo->checkpointId = checkpointId;
4,213✔
1446
  pReadyInfo->childId = childId;
4,213✔
1447
}
4,213✔
1448

1449
static int32_t doAddChkptReadyMsg(SStreamTask* pTask, STaskCheckpointReadyInfo* pInfo) {
4,213✔
1450
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
4,213✔
1451

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

1458
  int32_t numOfRecv = taosArrayGetSize(pActiveInfo->pReadyMsgList);
4,213✔
1459
  int32_t total = streamTaskGetNumOfUpstream(pTask);
4,213✔
1460
  if (numOfRecv == total) {
4,213✔
1461
    stDebug("s-task:%s recv checkpoint-trigger from all upstream, continue", pTask->id.idStr);
1,338✔
1462
    pActiveInfo->allUpstreamTriggerRecv = 1;
1,338✔
1463
  } else {
1464
    stDebug("s-task:%s %d/%d checkpoint-trigger recv", pTask->id.idStr, numOfRecv, total);
2,875✔
1465
  }
1466

1467
  return 0;
4,213✔
1468
}
1469

1470
int32_t streamAddCheckpointReadyMsg(SStreamTask* pTask, int32_t upstreamTaskId, int32_t index, int64_t checkpointId) {
4,213✔
1471
  int32_t                  code = 0;
4,213✔
1472
  STaskCheckpointReadyInfo info = {0};
4,213✔
1473

1474
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
4,213!
1475
    return TSDB_CODE_SUCCESS;
×
1476
  }
1477

1478
  SStreamUpstreamEpInfo* pInfo = NULL;
4,213✔
1479
  streamTaskGetUpstreamTaskEpInfo(pTask, upstreamTaskId, &pInfo);
4,213✔
1480
  if (pInfo == NULL) {
4,213!
1481
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1482
  }
1483

1484
  initCheckpointReadyInfo(&info, pInfo->nodeId, pInfo->taskId, pInfo->childId, &pInfo->epSet, checkpointId);
4,213✔
1485

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

1489
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
4,213✔
1490

1491
  streamMutexLock(&pActiveInfo->lock);
4,213✔
1492
  code = doAddChkptReadyMsg(pTask, &info);
4,213✔
1493
  streamMutexUnlock(&pActiveInfo->lock);
4,213✔
1494
  return code;
4,213✔
1495
}
1496

1497
void streamClearChkptReadyMsg(SActiveCheckpointInfo* pActiveInfo) {
32,748✔
1498
  if (pActiveInfo == NULL) {
32,748✔
1499
    return;
22,824✔
1500
  }
1501

1502
  for (int i = 0; i < taosArrayGetSize(pActiveInfo->pReadyMsgList); i++) {
14,137✔
1503
    STaskCheckpointReadyInfo* pInfo = taosArrayGet(pActiveInfo->pReadyMsgList, i);
4,213✔
1504
    if (pInfo != NULL) {
4,213!
1505
      rpcFreeCont(pInfo->msg.pCont);
4,213✔
1506
    }
1507
  }
1508

1509
  taosArrayClear(pActiveInfo->pReadyMsgList);
9,924✔
1510
}
1511

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

1516
  int64_t el = taosGetTimestampMs() - pTask->msgInfo.startTs;
10,569✔
1517
  bool    delayDispatch = (pTask->msgInfo.dispatchMsgType == STREAM_INPUT__CHECKPOINT_TRIGGER);
10,569✔
1518

1519
  clearBufferedDispatchMsg(pTask);
10,569✔
1520

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

1530
  // now ready for next data output
1531
  atomic_store_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL);
10,569✔
1532

1533
  // otherwise, continue dispatch the first block to down stream task in pipeline
1534
  if (delayDispatch) {
10,569✔
1535
    return 0;
1,357✔
1536
  } else {
1537
    return streamDispatchStreamBlock(pTask);
9,212✔
1538
  }
1539
}
1540

1541
static bool setDispatchRspInfo(SDispatchMsgInfo* pMsgInfo, int32_t vgId, int32_t code, int64_t now, int32_t* pNotRsp,
18,915✔
1542
                               int32_t* pFailed, const char* id) {
1543
  int32_t numOfRsp = 0;
18,915✔
1544
  int32_t numOfFailed = 0;
18,915✔
1545

1546
  bool    allRsp = false;
18,915✔
1547
  int32_t numOfDispatchBranch = taosArrayGetSize(pMsgInfo->pSendInfo);
18,915✔
1548

1549
  *pNotRsp = 0;
18,915✔
1550
  *pFailed = 0;
18,915✔
1551

1552
  for (int32_t i = 0; i < numOfDispatchBranch; ++i) {
62,147✔
1553
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, i);
43,232✔
1554
    if (pEntry == NULL) {
43,232!
1555
      continue;
×
1556
    }
1557

1558
    if (pEntry->rspTs != -1) {
43,232✔
1559
      numOfRsp += 1;
12,158✔
1560
    }
1561
  }
1562

1563
  for (int32_t j = 0; j < numOfDispatchBranch; ++j) {
31,073!
1564
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, j);
31,073✔
1565
    if (pEntry == NULL) {
31,073!
1566
      continue;
×
1567
    }
1568

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

1580
        stDebug("s-task:%s record the rsp recv, ts:%" PRId64 " code:%d, idx:%d, total recv:%d/%d", id, now, code, j,
18,915✔
1581
                numOfRsp, numOfDispatchBranch);
1582
      }
1583
      break;
18,915✔
1584
    }
1585
  }
1586

1587
  // this code may be error code.
1588
  for (int32_t i = 0; i < numOfDispatchBranch; ++i) {
62,147✔
1589
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, i);
43,232✔
1590
    if (pEntry->status != TSDB_CODE_SUCCESS || isDispatchRspTimeout(pEntry, now)) {
43,232!
UNCOV
1591
      numOfFailed += 1;
×
1592
    }
1593
  }
1594

1595
  *pFailed = numOfFailed;
18,915✔
1596
  *pNotRsp = numOfDispatchBranch - numOfRsp;
18,915✔
1597

1598
  return allRsp;
18,915✔
1599
}
1600

1601
bool isDispatchRspTimeout(SDispatchEntry* pEntry, int64_t now) {
46,190✔
1602
  return (pEntry->rspTs == -1) && (now - pEntry->sendTs) > 30 * 1000;
46,190!
1603
}
1604

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

1609
  for (int32_t j = 0; j < taosArrayGetSize(pMsgInfo->pSendInfo); ++j) {
4,283✔
1610
    SDispatchEntry* pEntry = taosArrayGet(pMsgInfo->pSendInfo, j);
2,958✔
1611
    if (pEntry == NULL) {
2,958!
1612
      continue;
×
1613
    }
1614

1615
    if (pEntry->status != TSDB_CODE_SUCCESS || isDispatchRspTimeout(pEntry, now)) {
2,958!
UNCOV
1616
      numOfFailed += 1;
×
1617
    }
1618
  }
1619
  streamMutexUnlock(&pMsgInfo->lock);
1,325✔
1620
  return numOfFailed;
1,325✔
1621
}
1622

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

1637
  // we only set the dispatch msg info for current checkpoint trans
1638
  streamMutexLock(&pTask->lock);
18,915✔
1639
  SStreamTaskState s = streamTaskGetStatus(pTask);
18,915✔
1640
  triggerDispatchRsp = (s.state == TASK_STATUS__CK) && (pInfo->activeId == pMsgInfo->checkpointId) &&
23,137!
1641
                       (pInfo->transId == pMsgInfo->transId);
4,222!
1642
  tmpCheckpointId = pInfo->activeId;
18,915✔
1643
  tmpTranId = pInfo->transId;
18,915✔
1644
  pStatus = s.name;
18,915✔
1645
  streamMutexUnlock(&pTask->lock);
18,915✔
1646

1647
  streamMutexLock(&pMsgInfo->lock);
18,915✔
1648
  int32_t msgId = pMsgInfo->msgId;
18,915✔
1649

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

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

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

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

1697
      allRsp = setDispatchRspInfo(pMsgInfo, pRsp->downstreamNodeId, TSDB_CODE_SUCCESS, now, &notRsp, &numOfFailed, id);
18,913✔
1698

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

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

1719
  streamMutexUnlock(&pMsgInfo->lock);
18,915✔
1720

1721
  if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
18,915✔
1722
    if (!allRsp) {
15,627✔
1723
      stDebug(
6,979✔
1724
          "s-task:%s recv dispatch rsp, msgId:%d from 0x%x(vgId:%d), downstream task input status:%d code:%s, "
1725
          "waiting for %d rsp",
1726
          id, msgId, pRsp->downstreamTaskId, pRsp->downstreamNodeId, pRsp->inputStatus, tstrerror(code), notRsp);
1727
    } else {
1728
      stDebug(
8,648✔
1729
          "s-task:%s recv dispatch rsp, msgId:%d from 0x%x(vgId:%d), downstream task input status:%d code:%s, all "
1730
          "rsp",
1731
          id, msgId, pRsp->downstreamTaskId, pRsp->downstreamNodeId, pRsp->inputStatus, tstrerror(code));
1732
    }
1733
  } else {
1734
    stDebug("s-task:%s recv fix-dispatch rsp, msgId:%d from 0x%x(vgId:%d), downstream task input status:%d code:%s", id,
3,288✔
1735
            msgId, pRsp->downstreamTaskId, pRsp->downstreamNodeId, pRsp->inputStatus, tstrerror(code));
1736
  }
1737

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

1749
      code = streamTransferStatePrepare(pTask);
1,367✔
1750
      if (code != TSDB_CODE_SUCCESS) {  // todo: do nothing if error happens
1751
      }
1752

1753
      clearBufferedDispatchMsg(pTask);
1,367✔
1754

1755
      // now ready for next data output
1756
      atomic_store_8(&pTask->outputq.status, TASK_OUTPUT_STATUS__NORMAL);
1,367✔
1757
    } else {
1758
      // this message has been sent successfully, let's try next one.
1759
      code = handleDispatchSuccessRsp(pTask, pRsp->downstreamTaskId, pRsp->downstreamNodeId);
10,569✔
1760
    }
1761
  }
1762

1763
  return code;
18,915✔
1764
}
1765

1766
static int32_t buildDispatchRsp(const SStreamTask* pTask, const SStreamDispatchReq* pReq, int32_t status, void** pBuf) {
18,919✔
1767
  *pBuf = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
18,919✔
1768
  if (*pBuf == NULL) {
18,919!
1769
    return terrno;
×
1770
  }
1771

1772
  ((SMsgHead*)(*pBuf))->vgId = htonl(pReq->upstreamNodeId);
18,919✔
1773

1774
  if (((SMsgHead*)(*pBuf))->vgId == 0) {
18,919!
1775
    return TSDB_CODE_INVALID_MSG;
×
1776
  }
1777

1778
  SStreamDispatchRsp* pDispatchRsp = POINTER_SHIFT((*pBuf), sizeof(SMsgHead));
18,919✔
1779

1780
  pDispatchRsp->stage = htobe64(pReq->stage);
18,919✔
1781
  pDispatchRsp->msgId = htonl(pReq->msgId);
18,919✔
1782
  pDispatchRsp->inputStatus = status;
18,919✔
1783
  pDispatchRsp->streamId = htobe64(pReq->streamId);
18,919✔
1784
  pDispatchRsp->upstreamNodeId = htonl(pReq->upstreamNodeId);
18,919✔
1785
  pDispatchRsp->upstreamTaskId = htonl(pReq->upstreamTaskId);
18,919✔
1786
  pDispatchRsp->downstreamNodeId = htonl(pTask->info.nodeId);
18,919✔
1787
  pDispatchRsp->downstreamTaskId = htonl(pTask->id.taskId);
18,919✔
1788

1789
  return TSDB_CODE_SUCCESS;
18,919✔
1790
}
1791

1792
static int32_t streamTaskAppendInputBlocks(SStreamTask* pTask, const SStreamDispatchReq* pReq) {
18,893✔
1793
  int8_t status = 0;
18,893✔
1794

1795
  SStreamDataBlock* pBlock = NULL;
18,893✔
1796

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

1808
    code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pBlock);
18,893✔
1809
    // input queue is full, upstream is blocked now
1810
    status = (code == TSDB_CODE_SUCCESS) ? TASK_INPUT_STATUS__NORMAL : TASK_INPUT_STATUS__BLOCKED;
18,893!
1811
  }
1812

1813
  return status;
18,893✔
1814
}
1815

1816
int32_t streamProcessDispatchMsg(SStreamTask* pTask, SStreamDispatchReq* pReq, SRpcMsg* pRsp) {
18,919✔
1817
  int32_t      status = 0;
18,919✔
1818
  SStreamMeta* pMeta = pTask->pMeta;
18,919✔
1819
  const char*  id = pTask->id.idStr;
18,919✔
1820

1821
  stDebug("s-task:%s receive dispatch msg from taskId:0x%x(vgId:%d), msgLen:%" PRId64 ", msgId:%d", id,
18,919✔
1822
          pReq->upstreamTaskId, pReq->upstreamNodeId, pReq->totalLen, pReq->msgId);
1823

1824
  SStreamUpstreamEpInfo* pInfo = NULL;
18,919✔
1825
  streamTaskGetUpstreamTaskEpInfo(pTask, pReq->upstreamTaskId, &pInfo);
18,919✔
1826
  if (pInfo == NULL) {
18,919!
1827
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1828
  }
1829

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

1854
        status = streamTaskAppendInputBlocks(pTask, pReq);
18,893✔
1855
      }
1856
    }
1857
  }
1858

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

1867
    pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp);
18,919✔
1868
    tmsgSendRsp(pRsp);
18,919✔
1869
  }
1870

1871
  return streamTrySchedExec(pTask);
18,919✔
1872
}
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