streamDispatch.c 19.4 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#include "streamInc.h"
L
Liu Jicong 已提交
17

L
liuyao 已提交
18 19 20 21 22 23 24
#define MAX_BLOCK_NAME_NUM 1024

typedef struct SBlockName {
  uint32_t hashValue;
  char     parTbName[TSDB_TABLE_NAME_LEN];
} SBlockName;

L
Liu Jicong 已提交
25 26 27 28
int32_t tEncodeStreamDispatchReq(SEncoder* pEncoder, const SStreamDispatchReq* pReq) {
  if (tStartEncode(pEncoder) < 0) return -1;
  if (tEncodeI64(pEncoder, pReq->streamId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->taskId) < 0) return -1;
L
Liu Jicong 已提交
29 30 31
  if (tEncodeI32(pEncoder, pReq->upstreamTaskId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->dataSrcVgId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->upstreamChildId) < 0) return -1;
L
Liu Jicong 已提交
32
  if (tEncodeI32(pEncoder, pReq->upstreamNodeId) < 0) return -1;
L
Liu Jicong 已提交
33
  if (tEncodeI32(pEncoder, pReq->blockNum) < 0) return -1;
34
  if (tEncodeI64(pEncoder, pReq->totalLen) < 0) return -1;
L
Liu Jicong 已提交
35 36 37 38 39 40 41 42 43
  ASSERT(taosArrayGetSize(pReq->data) == pReq->blockNum);
  ASSERT(taosArrayGetSize(pReq->dataLen) == pReq->blockNum);
  for (int32_t i = 0; i < pReq->blockNum; i++) {
    int32_t len = *(int32_t*)taosArrayGet(pReq->dataLen, i);
    void*   data = taosArrayGetP(pReq->data, i);
    if (tEncodeI32(pEncoder, len) < 0) return -1;
    if (tEncodeBinary(pEncoder, data, len) < 0) return -1;
  }
  tEndEncode(pEncoder);
44
  return pEncoder->pos;
L
Liu Jicong 已提交
45 46 47 48 49 50
}

int32_t tDecodeStreamDispatchReq(SDecoder* pDecoder, SStreamDispatchReq* pReq) {
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeI64(pDecoder, &pReq->streamId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pReq->taskId) < 0) return -1;
L
Liu Jicong 已提交
51 52 53
  if (tDecodeI32(pDecoder, &pReq->upstreamTaskId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pReq->dataSrcVgId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pReq->upstreamChildId) < 0) return -1;
L
Liu Jicong 已提交
54
  if (tDecodeI32(pDecoder, &pReq->upstreamNodeId) < 0) return -1;
L
Liu Jicong 已提交
55
  if (tDecodeI32(pDecoder, &pReq->blockNum) < 0) return -1;
56 57
  if (tDecodeI64(pDecoder, &pReq->totalLen) < 0) return -1;

L
Liu Jicong 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  ASSERT(pReq->blockNum > 0);
  pReq->data = taosArrayInit(pReq->blockNum, sizeof(void*));
  pReq->dataLen = taosArrayInit(pReq->blockNum, sizeof(int32_t));
  for (int32_t i = 0; i < pReq->blockNum; i++) {
    int32_t  len1;
    uint64_t len2;
    void*    data;
    if (tDecodeI32(pDecoder, &len1) < 0) return -1;
    if (tDecodeBinaryAlloc(pDecoder, &data, &len2) < 0) return -1;
    ASSERT(len1 == len2);
    taosArrayPush(pReq->dataLen, &len1);
    taosArrayPush(pReq->data, &data);
  }
  tEndDecode(pDecoder);
  return 0;
}

L
Liu Jicong 已提交
75
void tDeleteStreamDispatchReq(SStreamDispatchReq* pReq) {
L
Liu Jicong 已提交
76 77 78 79
  taosArrayDestroyP(pReq->data, taosMemoryFree);
  taosArrayDestroy(pReq->dataLen);
}

L
Liu Jicong 已提交
80 81 82
int32_t tEncodeStreamRetrieveReq(SEncoder* pEncoder, const SStreamRetrieveReq* pReq) {
  if (tStartEncode(pEncoder) < 0) return -1;
  if (tEncodeI64(pEncoder, pReq->streamId) < 0) return -1;
L
Liu Jicong 已提交
83
  if (tEncodeI64(pEncoder, pReq->reqId) < 0) return -1;
L
Liu Jicong 已提交
84 85 86 87
  if (tEncodeI32(pEncoder, pReq->dstNodeId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->dstTaskId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->srcNodeId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->srcTaskId) < 0) return -1;
L
Liu Jicong 已提交
88
  if (tEncodeBinary(pEncoder, (const uint8_t*)pReq->pRetrieve, pReq->retrieveLen) < 0) return -1;
L
Liu Jicong 已提交
89 90 91 92 93 94 95
  tEndEncode(pEncoder);
  return pEncoder->pos;
}

int32_t tDecodeStreamRetrieveReq(SDecoder* pDecoder, SStreamRetrieveReq* pReq) {
  if (tStartDecode(pDecoder) < 0) return -1;
  if (tDecodeI64(pDecoder, &pReq->streamId) < 0) return -1;
L
Liu Jicong 已提交
96
  if (tDecodeI64(pDecoder, &pReq->reqId) < 0) return -1;
L
Liu Jicong 已提交
97 98 99 100
  if (tDecodeI32(pDecoder, &pReq->dstNodeId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pReq->dstTaskId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pReq->srcNodeId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pReq->srcTaskId) < 0) return -1;
L
Liu Jicong 已提交
101 102
  uint64_t len = 0;
  if (tDecodeBinaryAlloc(pDecoder, (void**)&pReq->pRetrieve, &len) < 0) return -1;
103
  pReq->retrieveLen = (int32_t)len;
L
Liu Jicong 已提交
104 105 106 107
  tEndDecode(pDecoder);
  return 0;
}

L
Liu Jicong 已提交
108 109
void tDeleteStreamRetrieveReq(SStreamRetrieveReq* pReq) { taosMemoryFree(pReq->pRetrieve); }

L
Liu Jicong 已提交
110
int32_t streamBroadcastToChildren(SStreamTask* pTask, const SSDataBlock* pBlock) {
L
Liu Jicong 已提交
111
  int32_t            code = -1;
L
Liu Jicong 已提交
112 113 114 115 116 117 118
  SRetrieveTableRsp* pRetrieve = NULL;
  void*              buf = NULL;
  int32_t            dataStrLen = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);

  pRetrieve = taosMemoryCalloc(1, dataStrLen);
  if (pRetrieve == NULL) return -1;

H
Haojun Liao 已提交
119
  int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
L
Liu Jicong 已提交
120 121 122 123 124
  pRetrieve->useconds = 0;
  pRetrieve->precision = TSDB_DEFAULT_PRECISION;
  pRetrieve->compressed = 0;
  pRetrieve->completed = 1;
  pRetrieve->streamBlockType = pBlock->info.type;
125
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
H
Haojun Liao 已提交
126
  pRetrieve->numOfCols = htonl(numOfCols);
127 128
  pRetrieve->skey = htobe64(pBlock->info.window.skey);
  pRetrieve->ekey = htobe64(pBlock->info.window.ekey);
129
  pRetrieve->version = htobe64(pBlock->info.version);
L
Liu Jicong 已提交
130

H
Haojun Liao 已提交
131
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data, numOfCols);
L
Liu Jicong 已提交
132 133

  SStreamRetrieveReq req = {
134
      .streamId = pTask->id.streamId,
L
Liu Jicong 已提交
135
      .srcNodeId = pTask->nodeId,
136
      .srcTaskId = pTask->id.taskId,
L
Liu Jicong 已提交
137
      .pRetrieve = pRetrieve,
5
54liuyao 已提交
138
      .retrieveLen = dataStrLen,
L
Liu Jicong 已提交
139 140 141 142 143
  };

  int32_t sz = taosArrayGetSize(pTask->childEpInfo);
  ASSERT(sz > 0);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
144
    req.reqId = tGenIdPI64();
L
Liu Jicong 已提交
145 146 147 148 149 150 151 152 153 154 155 156
    SStreamChildEpInfo* pEpInfo = taosArrayGetP(pTask->childEpInfo, i);
    req.dstNodeId = pEpInfo->nodeId;
    req.dstTaskId = pEpInfo->taskId;
    int32_t len;
    tEncodeSize(tEncodeStreamRetrieveReq, &req, len, code);
    if (code < 0) {
      ASSERT(0);
      return -1;
    }

    buf = rpcMallocCont(sizeof(SMsgHead) + len);
    if (buf == NULL) {
L
Liu Jicong 已提交
157
      goto CLEAR;
L
Liu Jicong 已提交
158 159 160 161 162 163 164
    }

    ((SMsgHead*)buf)->vgId = htonl(pEpInfo->nodeId);
    void*    abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
    SEncoder encoder;
    tEncoderInit(&encoder, abuf, len);
    tEncodeStreamRetrieveReq(&encoder, &req);
L
Liu Jicong 已提交
165
    tEncoderClear(&encoder);
L
Liu Jicong 已提交
166

dengyihao's avatar
dengyihao 已提交
167
    SRpcMsg rpcMsg = {.code = 0, .msgType = TDMT_STREAM_RETRIEVE, .pCont = buf, .contLen = sizeof(SMsgHead) + len};
L
Liu Jicong 已提交
168 169
    if (tmsgSendReq(&pEpInfo->epSet, &rpcMsg) < 0) {
      ASSERT(0);
L
Liu Jicong 已提交
170
      goto CLEAR;
L
Liu Jicong 已提交
171
    }
L
Liu Jicong 已提交
172

H
Haojun Liao 已提交
173 174
    buf = NULL;
    qDebug("s-task:%s (child %d) send retrieve req to task %d at node %d, reqId:0x%" PRIx64, pTask->id.idStr,
S
Shengliang Guan 已提交
175
           pTask->selfChildId, pEpInfo->taskId, pEpInfo->nodeId, req.reqId);
L
Liu Jicong 已提交
176
  }
L
Liu Jicong 已提交
177
  code = 0;
H
Haojun Liao 已提交
178

L
Liu Jicong 已提交
179 180 181 182
CLEAR:
  taosMemoryFree(pRetrieve);
  rpcFreeCont(buf);
  return code;
L
Liu Jicong 已提交
183 184
}

185
static int32_t streamAddBlockIntoDispatchMsg(const SSDataBlock* pBlock, SStreamDispatchReq* pReq) {
186 187 188 189 190 191 192 193 194
  int32_t dataStrLen = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  void*   buf = taosMemoryCalloc(1, dataStrLen);
  if (buf == NULL) return -1;

  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
  pRetrieve->useconds = 0;
  pRetrieve->precision = TSDB_DEFAULT_PRECISION;
  pRetrieve->compressed = 0;
  pRetrieve->completed = 1;
L
Liu Jicong 已提交
195
  pRetrieve->streamBlockType = pBlock->info.type;
196
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
197 198
  pRetrieve->skey = htobe64(pBlock->info.window.skey);
  pRetrieve->ekey = htobe64(pBlock->info.window.ekey);
199
  pRetrieve->version = htobe64(pBlock->info.version);
200
  pRetrieve->watermark = htobe64(pBlock->info.watermark);
201
  memcpy(pRetrieve->parTbName, pBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
202

L
Liu Jicong 已提交
203
  int32_t numOfCols = (int32_t)taosArrayGetSize(pBlock->pDataBlock);
204
  pRetrieve->numOfCols = htonl(numOfCols);
205

H
Haojun Liao 已提交
206
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data, numOfCols);
207 208 209 210 211
  actualLen += sizeof(SRetrieveTableRsp);
  ASSERT(actualLen <= dataStrLen);
  taosArrayPush(pReq->dataLen, &actualLen);
  taosArrayPush(pReq->data, &buf);

212
  pReq->totalLen += dataStrLen;
213 214 215
  return 0;
}

216
int32_t streamDispatchCheckMsg(SStreamTask* pTask, const SStreamTaskCheckReq* pReq, int32_t nodeId, SEpSet* pEpSet) {
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  void*   buf = NULL;
  int32_t code = -1;
  SRpcMsg msg = {0};

  int32_t tlen;
  tEncodeSize(tEncodeSStreamTaskCheckReq, pReq, tlen, code);
  if (code < 0) {
    return -1;
  }

  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
  if (buf == NULL) {
    return -1;
  }

  ((SMsgHead*)buf)->vgId = htonl(nodeId);
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));

  SEncoder encoder;
  tEncoderInit(&encoder, abuf, tlen);
  if ((code = tEncodeSStreamTaskCheckReq(&encoder, pReq)) < 0) {
238 239
    rpcFreeCont(buf);
    return code;
240
  }
241

242 243 244 245 246 247
  tEncoderClear(&encoder);

  msg.contLen = tlen + sizeof(SMsgHead);
  msg.pCont = buf;
  msg.msgType = TDMT_STREAM_TASK_CHECK;

248
  qDebug("s-task:%s dispatch check msg to downstream s-task:%" PRIx64 ":%d node %d: check msg", pTask->id.idStr,
249
         pReq->streamId, pReq->downstreamTaskId, nodeId);
250 251 252 253 254

  tmsgSendReq(pEpSet, &msg);
  return 0;
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268
int32_t streamDispatchOneRecoverFinishReq(SStreamTask* pTask, const SStreamRecoverFinishReq* pReq, int32_t vgId,
                                          SEpSet* pEpSet) {
  void*   buf = NULL;
  int32_t code = -1;
  SRpcMsg msg = {0};

  int32_t tlen;
  tEncodeSize(tEncodeSStreamRecoverFinishReq, pReq, tlen, code);
  if (code < 0) {
    return -1;
  }

  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
  if (buf == NULL) {
H
Haojun Liao 已提交
269
    terrno = TSDB_CODE_OUT_OF_MEMORY;
270 271 272 273 274 275 276 277 278
    return -1;
  }

  ((SMsgHead*)buf)->vgId = htonl(vgId);
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));

  SEncoder encoder;
  tEncoderInit(&encoder, abuf, tlen);
  if ((code = tEncodeSStreamRecoverFinishReq(&encoder, pReq)) < 0) {
H
Haojun Liao 已提交
279 280 281 282
    if (buf) {
      rpcFreeCont(buf);
    }
    return code;
283
  }
H
Haojun Liao 已提交
284

285 286 287 288
  tEncoderClear(&encoder);

  msg.contLen = tlen + sizeof(SMsgHead);
  msg.pCont = buf;
L
Liu Jicong 已提交
289
  msg.msgType = TDMT_STREAM_RECOVER_FINISH;
dengyihao's avatar
dengyihao 已提交
290
  msg.info.noResp = 1;
291 292

  tmsgSendReq(pEpSet, &msg);
293
  qDebug("s-task:%s dispatch recover finish msg to downstream taskId:0x%x node %d: recover finish msg", pTask->id.idStr,
H
Haojun Liao 已提交
294
         pReq->taskId, vgId);
295

296 297 298
  return 0;
}

299
int32_t doSendDispatchMsg(SStreamTask* pTask, const SStreamDispatchReq* pReq, int32_t vgId, SEpSet* pEpSet) {
L
Liu Jicong 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  void*   buf = NULL;
  int32_t code = -1;
  SRpcMsg msg = {0};

  // serialize
  int32_t tlen;
  tEncodeSize(tEncodeStreamDispatchReq, pReq, tlen, code);
  if (code < 0) goto FAIL;
  code = -1;
  buf = rpcMallocCont(sizeof(SMsgHead) + tlen);
  if (buf == NULL) {
    goto FAIL;
  }

  ((SMsgHead*)buf)->vgId = htonl(vgId);
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));

  SEncoder encoder;
  tEncoderInit(&encoder, abuf, tlen);
  if ((code = tEncodeStreamDispatchReq(&encoder, pReq)) < 0) {
    goto FAIL;
  }
  tEncoderClear(&encoder);

  msg.contLen = tlen + sizeof(SMsgHead);
  msg.pCont = buf;
  msg.msgType = pTask->dispatchMsgType;

328
  qDebug("s-task:%s dispatch msg to taskId:0x%x vgId:%d data msg", pTask->id.idStr, pReq->taskId, vgId);
L
Liu Jicong 已提交
329 330 331 332
  tmsgSendReq(pEpSet, &msg);

  code = 0;
  return 0;
333

334 335 336
FAIL:
  if (buf) rpcFreeCont(buf);
  return code;
L
Liu Jicong 已提交
337 338
}

L
Liu Jicong 已提交
339 340
int32_t streamSearchAndAddBlock(SStreamTask* pTask, SStreamDispatchReq* pReqs, SSDataBlock* pDataBlock, int32_t vgSz,
                                int64_t groupId) {
L
liuyao 已提交
341 342 343 344
  uint32_t   hashValue = 0;
  SArray* vgInfo = pTask->shuffleDispatcher.dbInfo.pVgroupInfos;
  if (pTask->pNameMap == NULL) {
    pTask->pNameMap = tSimpleHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
L
Liu Jicong 已提交
345 346
  }

L
liuyao 已提交
347 348 349 350 351 352 353
  void* pVal = tSimpleHashGet(pTask->pNameMap, &groupId, sizeof(int64_t));
  if (pVal) {
    SBlockName* pBln = (SBlockName*)pVal;
    hashValue = pBln->hashValue;
    if (!pDataBlock->info.parTbName[0]) {
      memcpy(pDataBlock->info.parTbName, pBln->parTbName, strlen(pBln->parTbName));
    }
354
  } else {
L
liuyao 已提交
355 356 357 358
    char* ctbName = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
    if (ctbName == NULL) {
      return -1;
    }
L
Liu Jicong 已提交
359

L
liuyao 已提交
360 361 362 363 364 365
    if (pDataBlock->info.parTbName[0]) {
      snprintf(ctbName, TSDB_TABLE_NAME_LEN, "%s.%s", pTask->shuffleDispatcher.dbInfo.db, pDataBlock->info.parTbName);
    } else {
      buildCtbNameByGroupIdImpl(pTask->shuffleDispatcher.stbFullName, groupId, pDataBlock->info.parTbName);
      snprintf(ctbName, TSDB_TABLE_NAME_LEN, "%s.%s", pTask->shuffleDispatcher.dbInfo.db, pDataBlock->info.parTbName);
    }
L
Liu Jicong 已提交
366

L
liuyao 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380
    SArray* vgInfo = pTask->shuffleDispatcher.dbInfo.pVgroupInfos;

    /*uint32_t hashValue = MurmurHash3_32(ctbName, strlen(ctbName));*/
    SUseDbRsp* pDbInfo = &pTask->shuffleDispatcher.dbInfo;
    hashValue =
        taosGetTbHashVal(ctbName, strlen(ctbName), pDbInfo->hashMethod, pDbInfo->hashPrefix, pDbInfo->hashSuffix);
    taosMemoryFree(ctbName);
    SBlockName bln = {0};
    bln.hashValue = hashValue;
    memcpy(bln.parTbName, pDataBlock->info.parTbName, strlen(pDataBlock->info.parTbName));
    if (tSimpleHashGetSize(pTask->pNameMap) < MAX_BLOCK_NAME_NUM) {
      tSimpleHashPut(pTask->pNameMap, &groupId, sizeof(int64_t), &bln, sizeof(SBlockName));
    }
  }
381

L
Liu Jicong 已提交
382 383 384 385 386 387 388
  bool found = false;
  // TODO: optimize search
  int32_t j;
  for (j = 0; j < vgSz; j++) {
    SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, j);
    ASSERT(pVgInfo->vgId > 0);
    if (hashValue >= pVgInfo->hashBegin && hashValue <= pVgInfo->hashEnd) {
389
      if (streamAddBlockIntoDispatchMsg(pDataBlock, &pReqs[j]) < 0) {
L
Liu Jicong 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403
        return -1;
      }
      if (pReqs[j].blockNum == 0) {
        atomic_add_fetch_32(&pTask->shuffleDispatcher.waitingRspCnt, 1);
      }
      pReqs[j].blockNum++;
      found = true;
      break;
    }
  }
  ASSERT(found);
  return 0;
}

L
Liu Jicong 已提交
404
int32_t streamDispatchAllBlocks(SStreamTask* pTask, const SStreamDataBlock* pData) {
405 406 407
  int32_t code = 0;
  int32_t numOfBlocks = taosArrayGetSize(pData->blocks);
  ASSERT(numOfBlocks != 0);
L
Liu Jicong 已提交
408

409
  if (pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH) {
L
Liu Jicong 已提交
410
    SStreamDispatchReq req = {
411
        .streamId = pTask->id.streamId,
L
Liu Jicong 已提交
412
        .dataSrcVgId = pData->srcVgId,
413
        .upstreamTaskId = pTask->id.taskId,
L
Liu Jicong 已提交
414 415
        .upstreamChildId = pTask->selfChildId,
        .upstreamNodeId = pTask->nodeId,
416
        .blockNum = numOfBlocks,
L
Liu Jicong 已提交
417 418
    };

419 420
    req.data = taosArrayInit(numOfBlocks, sizeof(void*));
    req.dataLen = taosArrayInit(numOfBlocks, sizeof(int32_t));
L
Liu Jicong 已提交
421
    if (req.data == NULL || req.dataLen == NULL) {
422 423
      taosArrayDestroyP(req.data, taosMemoryFree);
      taosArrayDestroy(req.dataLen);
H
Haojun Liao 已提交
424
      return TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
425 426
    }

427
    for (int32_t i = 0; i < numOfBlocks; i++) {
L
Liu Jicong 已提交
428
      SSDataBlock* pDataBlock = taosArrayGet(pData->blocks, i);
H
Haojun Liao 已提交
429 430 431
      code = streamAddBlockIntoDispatchMsg(pDataBlock, &req);

      if (code != TSDB_CODE_SUCCESS) {
432 433 434
        taosArrayDestroyP(req.data, taosMemoryFree);
        taosArrayDestroy(req.dataLen);
        return code;
L
Liu Jicong 已提交
435 436
      }
    }
437

L
Liu Jicong 已提交
438 439 440 441 442 443
    int32_t vgId = pTask->fixedEpDispatcher.nodeId;
    SEpSet* pEpSet = &pTask->fixedEpDispatcher.epSet;
    int32_t downstreamTaskId = pTask->fixedEpDispatcher.taskId;

    req.taskId = downstreamTaskId;

444
    qDebug("s-task:%s (child taskId:%d) fix-dispatch %d block(s) to down stream s-task:0x%x in vgId:%d", pTask->id.idStr,
445
           pTask->selfChildId, numOfBlocks, downstreamTaskId, vgId);
L
Liu Jicong 已提交
446

H
Haojun Liao 已提交
447 448 449 450
    code = doSendDispatchMsg(pTask, &req, vgId, pEpSet);
    taosArrayDestroyP(req.data, taosMemoryFree);
    taosArrayDestroy(req.dataLen);
    return code;
451
  } else if (pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {
L
Liu Jicong 已提交
452 453 454 455
    int32_t rspCnt = atomic_load_32(&pTask->shuffleDispatcher.waitingRspCnt);
    ASSERT(rspCnt == 0);

    SArray*             vgInfo = pTask->shuffleDispatcher.dbInfo.pVgroupInfos;
L
Liu Jicong 已提交
456 457 458
    int32_t             vgSz = taosArrayGetSize(vgInfo);
    SStreamDispatchReq* pReqs = taosMemoryCalloc(vgSz, sizeof(SStreamDispatchReq));
    if (pReqs == NULL) {
H
Haojun Liao 已提交
459
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
460 461 462 463
      return -1;
    }

    for (int32_t i = 0; i < vgSz; i++) {
464
      pReqs[i].streamId = pTask->id.streamId;
L
Liu Jicong 已提交
465
      pReqs[i].dataSrcVgId = pData->srcVgId;
466
      pReqs[i].upstreamTaskId = pTask->id.taskId;
L
Liu Jicong 已提交
467 468 469 470 471 472 473 474
      pReqs[i].upstreamChildId = pTask->selfChildId;
      pReqs[i].upstreamNodeId = pTask->nodeId;
      pReqs[i].blockNum = 0;
      pReqs[i].data = taosArrayInit(0, sizeof(void*));
      pReqs[i].dataLen = taosArrayInit(0, sizeof(int32_t));
      if (pReqs[i].data == NULL || pReqs[i].dataLen == NULL) {
        goto FAIL_SHUFFLE_DISPATCH;
      }
H
Haojun Liao 已提交
475

L
Liu Jicong 已提交
476 477
      SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
      pReqs[i].taskId = pVgInfo->taskId;
L
Liu Jicong 已提交
478
    }
L
Liu Jicong 已提交
479

480
    for (int32_t i = 0; i < numOfBlocks; i++) {
L
Liu Jicong 已提交
481
      SSDataBlock* pDataBlock = taosArrayGet(pData->blocks, i);
L
Liu Jicong 已提交
482

L
Liu Jicong 已提交
483 484 485
      // TODO: do not use broadcast
      if (pDataBlock->info.type == STREAM_DELETE_RESULT) {
        for (int32_t j = 0; j < vgSz; j++) {
486
          if (streamAddBlockIntoDispatchMsg(pDataBlock, &pReqs[j]) < 0) {
L
Liu Jicong 已提交
487 488
            goto FAIL_SHUFFLE_DISPATCH;
          }
L
Liu Jicong 已提交
489 490 491
          if (pReqs[j].blockNum == 0) {
            atomic_add_fetch_32(&pTask->shuffleDispatcher.waitingRspCnt, 1);
          }
L
Liu Jicong 已提交
492
          pReqs[j].blockNum++;
L
Liu Jicong 已提交
493
        }
L
Liu Jicong 已提交
494 495 496
        continue;
      }

H
Haojun Liao 已提交
497
      if (streamSearchAndAddBlock(pTask, pReqs, pDataBlock, vgSz, pDataBlock->info.id.groupId) < 0) {
L
Liu Jicong 已提交
498
        goto FAIL_SHUFFLE_DISPATCH;
L
Liu Jicong 已提交
499 500
      }
    }
L
Liu Jicong 已提交
501

H
Haojun Liao 已提交
502
    qDebug("s-task:%s (child taskId:%d) shuffle-dispatch blocks:%d to %d vgroups", pTask->id.idStr, pTask->selfChildId,
503
           numOfBlocks, vgSz);
H
Haojun Liao 已提交
504

L
Liu Jicong 已提交
505 506 507
    for (int32_t i = 0; i < vgSz; i++) {
      if (pReqs[i].blockNum > 0) {
        SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
H
Haojun Liao 已提交
508 509 510
        qDebug("s-task:%s (child taskId:%d) shuffle-dispatch blocks:%d to vgId:%d", pTask->id.idStr, pTask->selfChildId,
               pReqs[i].blockNum, pVgInfo->vgId);

511
        if (doSendDispatchMsg(pTask, &pReqs[i], pVgInfo->vgId, &pVgInfo->epSet) < 0) {
L
Liu Jicong 已提交
512 513 514 515
          goto FAIL_SHUFFLE_DISPATCH;
        }
      }
    }
H
Haojun Liao 已提交
516

L
Liu Jicong 已提交
517
    code = 0;
H
Haojun Liao 已提交
518

L
Liu Jicong 已提交
519
  FAIL_SHUFFLE_DISPATCH:
H
Haojun Liao 已提交
520 521 522
    for (int32_t i = 0; i < vgSz; i++) {
      taosArrayDestroyP(pReqs[i].data, taosMemoryFree);
      taosArrayDestroy(pReqs[i].dataLen);
L
Liu Jicong 已提交
523
    }
H
Haojun Liao 已提交
524
    taosMemoryFree(pReqs);
L
Liu Jicong 已提交
525
  }
H
Haojun Liao 已提交
526
  return code;
L
Liu Jicong 已提交
527 528
}

529
int32_t streamDispatchStreamBlock(SStreamTask* pTask) {
530
  ASSERT(pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH || pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH);
H
Haojun Liao 已提交
531 532 533 534 535
  int32_t numOfElems = taosQueueItemSize(pTask->outputQueue->queue);
  if (numOfElems > 0) {
    qDebug("s-task:%s try to dispatch intermediate result block to downstream, elem in outputQ:%d", pTask->id.idStr,
           numOfElems);
  }
L
Liu Jicong 已提交
536

L
Liu Jicong 已提交
537 538 539
  int8_t old =
      atomic_val_compare_exchange_8(&pTask->outputStatus, TASK_OUTPUT_STATUS__NORMAL, TASK_OUTPUT_STATUS__WAIT);
  if (old != TASK_OUTPUT_STATUS__NORMAL) {
540
    qDebug("s-task:%s task wait for dispatch rsp, not dispatch now, output status:%d", pTask->id.idStr, old);
L
Liu Jicong 已提交
541
    return 0;
L
Liu Jicong 已提交
542 543
  }

544
  qDebug("s-task:%s start to dispatch msg, set output status:%d", pTask->id.idStr, pTask->outputStatus);
545

546 547
  SStreamDataBlock* pDispatchedBlock = streamQueueNextItem(pTask->outputQueue);
  if (pDispatchedBlock == NULL) {
L
Liu Jicong 已提交
548
    atomic_store_8(&pTask->outputStatus, TASK_OUTPUT_STATUS__NORMAL);
549 550
    qDebug("s-task:%s stop dispatching since no output in output queue, output status:%d", pTask->id.idStr,
           pTask->outputStatus);
L
Liu Jicong 已提交
551 552
    return 0;
  }
L
Liu Jicong 已提交
553

554
  ASSERT(pDispatchedBlock->type == STREAM_INPUT__DATA_BLOCK);
L
Liu Jicong 已提交
555

H
Haojun Liao 已提交
556
  int32_t code = streamDispatchAllBlocks(pTask, pDispatchedBlock);
557
  if (code != TSDB_CODE_SUCCESS) {
558 559
    streamQueueProcessFail(pTask->outputQueue);
    atomic_store_8(&pTask->outputStatus, TASK_OUTPUT_STATUS__NORMAL);
560
    qDebug("s-task:%s failed to dispatch msg to downstream, output status:%d", pTask->id.idStr, pTask->outputStatus);
L
Liu Jicong 已提交
561
  }
562

563 564
  // this block can be freed only when it has been pushed to down stream.
  destroyStreamDataBlock(pDispatchedBlock);
L
Liu Jicong 已提交
565
  return code;
L
Liu Jicong 已提交
566
}