tq.c 26.9 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
S
Shengliang Guan 已提交
14 15
 */

H
Hongze Cheng 已提交
16
#include "tq.h"
S
Shengliang Guan 已提交
17

L
Liu Jicong 已提交
18
int32_t tqInit() {
L
Liu Jicong 已提交
19 20 21 22 23 24
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&tqMgmt.inited, 0, 2);
    if (old != 2) break;
  }

25 26 27 28 29 30
  if (old == 0) {
    tqMgmt.timer = taosTmrInit(10000, 100, 10000, "TQ");
    if (tqMgmt.timer == NULL) {
      atomic_store_8(&tqMgmt.inited, 0);
      return -1;
    }
31 32 33
    if (streamInit() < 0) {
      return -1;
    }
L
Liu Jicong 已提交
34
    atomic_store_8(&tqMgmt.inited, 1);
35
  }
36

L
Liu Jicong 已提交
37 38
  return 0;
}
L
Liu Jicong 已提交
39

40
void tqCleanUp() {
L
Liu Jicong 已提交
41 42 43 44 45 46 47 48
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&tqMgmt.inited, 1, 2);
    if (old != 2) break;
  }

  if (old == 1) {
    taosTmrCleanUp(tqMgmt.timer);
L
Liu Jicong 已提交
49
    streamCleanUp();
L
Liu Jicong 已提交
50 51
    atomic_store_8(&tqMgmt.inited, 0);
  }
52
}
L
Liu Jicong 已提交
53

L
Liu Jicong 已提交
54
STQ* tqOpen(const char* path, SVnode* pVnode) {
55
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
L
Liu Jicong 已提交
56
  if (pTq == NULL) {
L
Liu Jicong 已提交
57
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
58 59
    return NULL;
  }
H
Hongze Cheng 已提交
60
  pTq->path = strdup(path);
L
Liu Jicong 已提交
61
  pTq->pVnode = pVnode;
62

63
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
64

65
  pTq->pPushMgr = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
66

67
  pTq->pCheckInfo = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
68

L
Liu Jicong 已提交
69
  if (tqMetaOpen(pTq) < 0) {
70 71 72
    ASSERT(0);
  }

73 74 75 76
  if (tqOffsetOpen(pTq) < 0) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
77 78 79 80 81
  pTq->pStreamMeta = streamMetaOpen(path, pTq, (FTaskExpand*)tqExpandTask);
  if (pTq->pStreamMeta == NULL) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
82 83
  return pTq;
}
L
Liu Jicong 已提交
84

L
Liu Jicong 已提交
85
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
86
  if (pTq) {
87
    tqOffsetClose(pTq->pOffsetStore);
88 89 90
    taosHashCleanup(pTq->pHandle);
    taosHashCleanup(pTq->pPushMgr);
    taosHashCleanup(pTq->pCheckInfo);
91
    taosMemoryFree(pTq->path);
L
Liu Jicong 已提交
92
    tqMetaClose(pTq);
L
Liu Jicong 已提交
93
    streamMetaClose(pTq->pStreamMeta);
wafwerar's avatar
wafwerar 已提交
94
    taosMemoryFree(pTq);
H
Hongze Cheng 已提交
95
  }
L
Liu Jicong 已提交
96
}
L
Liu Jicong 已提交
97

L
Liu Jicong 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
int32_t tqSendMetaPollRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp) {
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqMetaRsp(NULL, pRsp);
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    return -1;
  }

  ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_META_RSP;
  ((SMqRspHead*)buf)->epoch = pReq->epoch;
  ((SMqRspHead*)buf)->consumerId = pReq->consumerId;

  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
  tEncodeSMqMetaRsp(&abuf, pRsp);

  SRpcMsg resp = {
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
  tmsgSendRsp(&resp);

S
Shengliang Guan 已提交
120
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, res msg type %d, reqOffset:%" PRId64
L
Liu Jicong 已提交
121
          ", rspOffset:%" PRId64,
L
Liu Jicong 已提交
122 123 124 125 126
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->reqOffset, pRsp->rspOffset);

  return 0;
}

L
Liu Jicong 已提交
127 128 129 130 131 132 133 134 135 136
int32_t tqSendDataRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqDataRsp* pRsp) {
  ASSERT(taosArrayGetSize(pRsp->blockData) == pRsp->blockNum);
  ASSERT(taosArrayGetSize(pRsp->blockDataLen) == pRsp->blockNum);

  if (pRsp->withSchema) {
    ASSERT(taosArrayGetSize(pRsp->blockSchema) == pRsp->blockNum);
  } else {
    ASSERT(taosArrayGetSize(pRsp->blockSchema) == 0);
  }

137 138 139 140 141 142
  if (pRsp->reqOffset.type == TMQ_OFFSET__LOG) {
    if (pRsp->blockNum > 0) {
      ASSERT(pRsp->rspOffset.version > pRsp->reqOffset.version);
    } else {
      ASSERT(pRsp->rspOffset.version >= pRsp->reqOffset.version);
    }
L
Liu Jicong 已提交
143 144
  }

wmmhello's avatar
wmmhello 已提交
145 146
  int32_t len = 0;
  int32_t code = 0;
L
Liu Jicong 已提交
147 148 149 150 151
  tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
152 153 154 155 156 157 158 159 160 161 162
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    return -1;
  }

  ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
  ((SMqRspHead*)buf)->epoch = pReq->epoch;
  ((SMqRspHead*)buf)->consumerId = pReq->consumerId;

  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));

wmmhello's avatar
wmmhello 已提交
163
  SEncoder encoder = {0};
L
Liu Jicong 已提交
164
  tEncoderInit(&encoder, abuf, len);
L
Liu Jicong 已提交
165
  tEncodeSMqDataRsp(&encoder, pRsp);
wmmhello's avatar
wmmhello 已提交
166
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
167 168

  SRpcMsg rsp = {
L
Liu Jicong 已提交
169 170 171 172 173
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
L
Liu Jicong 已提交
174
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
175

wmmhello's avatar
wmmhello 已提交
176 177
  char buf1[80] = {0};
  char buf2[80] = {0};
L
Liu Jicong 已提交
178 179
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);
S
Shengliang Guan 已提交
180
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, block num: %d, reqOffset:%s, rspOffset:%s",
L
Liu Jicong 已提交
181
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2);
L
Liu Jicong 已提交
182 183 184 185

  return 0;
}

186 187 188 189 190 191
static FORCE_INLINE bool tqOffsetLessOrEqual(const STqOffset* pLeft, const STqOffset* pRight) {
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
         pLeft->val.version <= pRight->val.version;
}

int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
192 193 194 195 196 197 198 199 200
  STqOffset offset = {0};
  SDecoder  decoder;
  tDecoderInit(&decoder, msg, msgLen);
  if (tDecodeSTqOffset(&decoder, &offset) < 0) {
    ASSERT(0);
    return -1;
  }
  tDecoderClear(&decoder);

L
Liu Jicong 已提交
201
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA) {
L
Liu Jicong 已提交
202 203
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
            offset.subKey, TD_VID(pTq->pVnode), offset.val.uid, offset.val.ts);
L
Liu Jicong 已提交
204
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
S
Shengliang Guan 已提交
205
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, offset.subKey,
L
Liu Jicong 已提交
206
            TD_VID(pTq->pVnode), offset.val.version);
207 208 209
    if (offset.val.version + 1 == version) {
      offset.val.version += 1;
    }
210 211 212
  } else {
    ASSERT(0);
  }
213 214 215 216 217
  STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, offset.subKey);
  if (pOffset != NULL && tqOffsetLessOrEqual(&offset, pOffset)) {
    return 0;
  }

218 219 220
  if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
    ASSERT(0);
    return -1;
221
  }
222 223

  if (offset.val.type == TMQ_OFFSET__LOG) {
224
    STqHandle* pHandle = taosHashGet(pTq->pHandle, offset.subKey, strlen(offset.subKey));
L
Liu Jicong 已提交
225 226 227 228 229
    if (pHandle) {
      if (walRefVer(pHandle->pRef, offset.val.version) < 0) {
        ASSERT(0);
        return -1;
      }
230 231 232
    }
  }

233 234
  // rsp

235 236
  /*}*/
  /*}*/
237 238 239 240

  return 0;
}

L
Liu Jicong 已提交
241
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
L
Liu Jicong 已提交
242 243
  void* pIter = NULL;
  while (1) {
244
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
L
Liu Jicong 已提交
245
    if (pIter == NULL) break;
246
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
L
Liu Jicong 已提交
247 248
    if (pCheck->ntbUid == tbUid) {
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
L
Liu Jicong 已提交
249
      for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
250 251
        int16_t forbidColId = *(int16_t*)taosArrayGet(pCheck->colIdList, i);
        if (forbidColId == colId) {
252
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
L
Liu Jicong 已提交
253 254 255 256 257 258 259 260
          return -1;
        }
      }
    }
  }
  return 0;
}

L
Liu Jicong 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static int32_t tqInitDataRsp(SMqDataRsp* pRsp, const SMqPollReq* pReq, int8_t subType) {
  pRsp->reqOffset = pReq->reqOffset;

  pRsp->blockData = taosArrayInit(0, sizeof(void*));
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));

  if (pRsp->blockData == NULL || pRsp->blockDataLen == NULL) {
    return -1;
  }

  pRsp->withTbName = pReq->withTbName;
  if (pRsp->withTbName) {
    pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
    if (pRsp->blockTbName == NULL) {
      // TODO free
      return -1;
    }
  }

  if (subType == TOPIC_SUB_TYPE__COLUMN) {
    pRsp->withSchema = false;
  } else {
    pRsp->withSchema = true;
    pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
    if (pRsp->blockSchema == NULL) {
      // TODO free
      return -1;
    }
  }
  return 0;
}

L
Liu Jicong 已提交
293
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
294 295 296 297 298 299 300
  SMqPollReq*  pReq = pMsg->pCont;
  int64_t      consumerId = pReq->consumerId;
  int64_t      timeout = pReq->timeout;
  int32_t      reqEpoch = pReq->epoch;
  int32_t      code = 0;
  STqOffsetVal reqOffset = pReq->reqOffset;
  STqOffsetVal fetchOffsetNew;
L
Liu Jicong 已提交
301
  SWalCkHead*  pCkHead = NULL;
L
Liu Jicong 已提交
302 303

  // 1.find handle
304
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
305 306
  /*ASSERT(pHandle);*/
  if (pHandle == NULL) {
S
Shengliang Guan 已提交
307 308
    tqError("tmq poll: no consumer handle for consumer:%" PRId64 ", in vgId:%d, subkey %s", consumerId,
            TD_VID(pTq->pVnode), pReq->subKey);
L
Liu Jicong 已提交
309 310 311 312 313
    return -1;
  }

  // check rebalance
  if (pHandle->consumerId != consumerId) {
S
Shengliang Guan 已提交
314 315
    tqError("tmq poll: consumer handle mismatch for consumer:%" PRId64
            ", in vgId:%d, subkey %s, handle consumer id %" PRId64,
L
Liu Jicong 已提交
316
            consumerId, TD_VID(pTq->pVnode), pReq->subKey, pHandle->consumerId);
L
Liu Jicong 已提交
317
    terrno = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
L
Liu Jicong 已提交
318 319 320 321 322 323 324 325 326
    return -1;
  }

  // update epoch if need
  int32_t consumerEpoch = atomic_load_32(&pHandle->epoch);
  while (consumerEpoch < reqEpoch) {
    consumerEpoch = atomic_val_compare_exchange_32(&pHandle->epoch, consumerEpoch, reqEpoch);
  }

L
Liu Jicong 已提交
327 328
  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
S
Shengliang Guan 已提交
329
  tqDebug("tmq poll: consumer %" PRId64 " (epoch %d), subkey %s, recv poll req in vg %d, req offset %s", consumerId,
L
Liu Jicong 已提交
330 331
          pReq->epoch, pHandle->subKey, TD_VID(pTq->pVnode), buf);

L
Liu Jicong 已提交
332 333 334
  SMqDataRsp dataRsp = {0};
  tqInitDataRsp(&dataRsp, pReq, pHandle->execHandle.subType);

L
Liu Jicong 已提交
335 336 337 338 339 340 341
  // 2.reset offset if needed
  if (reqOffset.type > 0) {
    fetchOffsetNew = reqOffset;
  } else {
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, pReq->subKey);
    if (pOffset != NULL) {
      fetchOffsetNew = pOffset->val;
L
Liu Jicong 已提交
342 343
      char formatBuf[80];
      tFormatOffset(formatBuf, 80, &fetchOffsetNew);
L
Liu Jicong 已提交
344 345
      tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, offset reset to %s", consumerId, pHandle->subKey,
              TD_VID(pTq->pVnode), formatBuf);
L
Liu Jicong 已提交
346 347
    } else {
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEAST) {
348
        if (pReq->useSnapshot && pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
349 350 351 352 353 354 355 356 357 358
          if (!pHandle->fetchMeta) {
            tqOffsetResetToData(&fetchOffsetNew, 0, 0);
          } else {
            // reset to meta
            ASSERT(0);
          }
        } else {
          tqOffsetResetToLog(&fetchOffsetNew, walGetFirstVer(pTq->pVnode->pWal));
        }
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
L
Liu Jicong 已提交
359
        tqOffsetResetToLog(&dataRsp.rspOffset, walGetLastVer(pTq->pVnode->pWal));
S
Shengliang Guan 已提交
360 361
        tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, offset reset to %" PRId64, consumerId,
                pHandle->subKey, TD_VID(pTq->pVnode), dataRsp.rspOffset.version);
L
Liu Jicong 已提交
362 363 364
        if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
          code = -1;
        }
L
Liu Jicong 已提交
365
        goto OVER;
L
Liu Jicong 已提交
366
      } else if (reqOffset.type == TMQ_OFFSET__RESET_NONE) {
367 368
        tqError("tmq poll: subkey %s, no offset committed for consumer %" PRId64
                " in vg %d, subkey %s, reset none failed",
L
Liu Jicong 已提交
369
                pHandle->subKey, consumerId, TD_VID(pTq->pVnode), pReq->subKey);
L
Liu Jicong 已提交
370
        terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
L
Liu Jicong 已提交
371 372
        code = -1;
        goto OVER;
L
Liu Jicong 已提交
373 374 375 376 377
      }
    }
  }

  // 3.query
L
Liu Jicong 已提交
378
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
379 380 381
    /*if (fetchOffsetNew.type == TMQ_OFFSET__LOG) {*/
    /*fetchOffsetNew.version++;*/
    /*}*/
L
Liu Jicong 已提交
382
    if (tqScan(pTq, pHandle, &dataRsp, &fetchOffsetNew) < 0) {
L
Liu Jicong 已提交
383 384 385 386 387
      ASSERT(0);
      code = -1;
      goto OVER;
    }
    if (dataRsp.blockNum == 0) {
L
Liu Jicong 已提交
388
      // TODO add to async task pool
L
Liu Jicong 已提交
389
      /*dataRsp.rspOffset.version--;*/
L
Liu Jicong 已提交
390 391 392 393 394 395 396
    }
    if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
      code = -1;
    }
    goto OVER;
  }

L
Liu Jicong 已提交
397
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
398
    ASSERT(fetchOffsetNew.type == TMQ_OFFSET__LOG);
L
Liu Jicong 已提交
399 400
    int64_t fetchVer = fetchOffsetNew.version + 1;
    pCkHead = taosMemoryMalloc(sizeof(SWalCkHead) + 2048);
L
Liu Jicong 已提交
401
    if (pCkHead == NULL) {
L
Liu Jicong 已提交
402 403
      code = -1;
      goto OVER;
L
Liu Jicong 已提交
404 405 406 407 408 409 410
    }

    walSetReaderCapacity(pHandle->pWalReader, 2048);

    while (1) {
      consumerEpoch = atomic_load_32(&pHandle->epoch);
      if (consumerEpoch > reqEpoch) {
S
Shengliang Guan 已提交
411
        tqWarn("tmq poll: consumer %" PRId64 " (epoch %d), subkey %s, vg %d offset %" PRId64
S
Shengliang Guan 已提交
412
               ", found new consumer epoch %d, discard req epoch %d",
413
               consumerId, pReq->epoch, pHandle->subKey, TD_VID(pTq->pVnode), fetchVer, consumerEpoch, reqEpoch);
L
Liu Jicong 已提交
414 415 416
        break;
      }

L
Liu Jicong 已提交
417
      if (tqFetchLog(pTq, pHandle, &fetchVer, &pCkHead) < 0) {
L
Liu Jicong 已提交
418 419
        // TODO add push mgr

L
Liu Jicong 已提交
420 421
        tqOffsetResetToLog(&dataRsp.rspOffset, fetchVer);
        ASSERT(dataRsp.rspOffset.version >= dataRsp.reqOffset.version);
L
Liu Jicong 已提交
422 423 424 425 426 427
        if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
          code = -1;
        }
        goto OVER;
      }

L
Liu Jicong 已提交
428
      SWalCont* pHead = &pCkHead->head;
L
Liu Jicong 已提交
429

S
Shengliang Guan 已提交
430 431
      tqDebug("tmq poll: consumer:%" PRId64 ", (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %d", consumerId,
              pReq->epoch, TD_VID(pTq->pVnode), fetchVer, pHead->msgType);
L
Liu Jicong 已提交
432 433 434 435

      if (pHead->msgType == TDMT_VND_SUBMIT) {
        SSubmitReq* pCont = (SSubmitReq*)&pHead->body;

L
Liu Jicong 已提交
436
        if (tqLogScanExec(pTq, &pHandle->execHandle, pCont, &dataRsp) < 0) {
L
Liu Jicong 已提交
437
          /*ASSERT(0);*/
L
Liu Jicong 已提交
438 439 440 441 442
        }
        // TODO batch optimization:
        // TODO continue scan until meeting batch requirement
        if (dataRsp.blockNum > 0 /* threshold */) {
          tqOffsetResetToLog(&dataRsp.rspOffset, fetchVer);
L
Liu Jicong 已提交
443
          ASSERT(dataRsp.rspOffset.version >= dataRsp.reqOffset.version);
L
Liu Jicong 已提交
444 445 446 447

          if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
            code = -1;
          }
L
Liu Jicong 已提交
448
          goto OVER;
L
Liu Jicong 已提交
449 450 451 452 453 454 455
        } else {
          fetchVer++;
        }

      } else {
        ASSERT(pHandle->fetchMeta);
        ASSERT(IS_META_MSG(pHead->msgType));
L
Liu Jicong 已提交
456
        tqDebug("fetch meta msg, ver:%" PRId64 ", type:%d", pHead->version, pHead->msgType);
L
Liu Jicong 已提交
457
        SMqMetaRsp metaRsp = {0};
458
        /*metaRsp.reqOffset = pReq->reqOffset.version;*/
wmmhello's avatar
wmmhello 已提交
459
        metaRsp.rspOffset = fetchVer;
460 461 462
        /*metaRsp.rspOffsetNew.version = fetchVer;*/
        tqOffsetResetToLog(&metaRsp.reqOffsetNew, pReq->reqOffset.version);
        tqOffsetResetToLog(&metaRsp.rspOffsetNew, fetchVer);
L
Liu Jicong 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476
        metaRsp.resMsgType = pHead->msgType;
        metaRsp.metaRspLen = pHead->bodyLen;
        metaRsp.metaRsp = pHead->body;
        if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) {
          code = -1;
          goto OVER;
        }
        code = 0;
        goto OVER;
      }
    }
  }

OVER:
L
Liu Jicong 已提交
477
  if (pCkHead) taosMemoryFree(pCkHead);
L
Liu Jicong 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  // TODO wrap in destroy func
  taosArrayDestroy(dataRsp.blockDataLen);
  taosArrayDestroyP(dataRsp.blockData, (FDelete)taosMemoryFree);

  if (dataRsp.withSchema) {
    taosArrayDestroyP(dataRsp.blockSchema, (FDelete)tDeleteSSchemaWrapper);
  }

  if (dataRsp.withTbName) {
    taosArrayDestroyP(dataRsp.blockTbName, (FDelete)taosMemoryFree);
  }

  return code;
}

493
int32_t tqProcessVgDeleteReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
494
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
L
Liu Jicong 已提交
495

496
  int32_t code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
497
  ASSERT(code == 0);
498

L
Liu Jicong 已提交
499 500
  tqOffsetDelete(pTq->pOffsetStore, pReq->subKey);

L
Liu Jicong 已提交
501
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
502 503
    ASSERT(0);
  }
L
Liu Jicong 已提交
504
  return 0;
L
Liu Jicong 已提交
505 506
}

507 508 509
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
  STqCheckInfo info = {0};
  SDecoder     decoder;
L
Liu Jicong 已提交
510
  tDecoderInit(&decoder, msg, msgLen);
511
  if (tDecodeSTqCheckInfo(&decoder, &info) < 0) {
L
Liu Jicong 已提交
512 513 514 515
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  tDecoderClear(&decoder);
516 517 518 519 520
  if (taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  if (tqMetaSaveCheckInfo(pTq, info.topic, msg, msgLen) < 0) {
L
Liu Jicong 已提交
521 522 523 524 525 526
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

527 528 529 530 531 532 533 534 535 536 537 538 539
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  if (tqMetaDeleteCheckInfo(pTq, msg) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

int32_t tqProcessVgChangeReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
540
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
541 542
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
543
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
L
Liu Jicong 已提交
544
  if (pHandle == NULL) {
L
Liu Jicong 已提交
545 546 547 548
    if (req.oldConsumerId != -1) {
      tqError("vgId:%d, build new consumer handle %s for consumer %d, but old consumerId is %ld", req.vgId, req.subKey,
              req.newConsumerId, req.oldConsumerId);
    }
L
Liu Jicong 已提交
549
    ASSERT(req.newConsumerId != -1);
L
Liu Jicong 已提交
550 551
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
552 553
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
554 555 556
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
557

L
Liu Jicong 已提交
558
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
559
    pHandle->fetchMeta = req.withMeta;
560 561 562 563
    // TODO version should be assigned and refed during preprocess
    SWalRef* pRef = walRefCommittedVer(pTq->pVnode->pWal);
    if (pRef == NULL) {
      ASSERT(0);
L
Liu Jicong 已提交
564
      return -1;
565 566 567
    }
    int64_t ver = pRef->refVer;
    pHandle->pRef = pRef;
L
Liu Jicong 已提交
568

L
Liu Jicong 已提交
569
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
570
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
571
      pHandle->snapshotVer = ver;
L
Liu Jicong 已提交
572
      req.qmsg = NULL;
L
Liu Jicong 已提交
573 574 575 576 577 578 579 580
      SReadHandle handle = {
          .meta = pTq->pVnode->pMeta,
          .vnode = pTq->pVnode,
          .initTableReader = true,
          .initTqReader = true,
          .version = ver,
      };
      pHandle->execHandle.execCol.task =
581
          qCreateQueueExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle, &pHandle->execHandle.numOfCols,
L
Liu Jicong 已提交
582
                                   &pHandle->execHandle.pSchemaWrapper);
L
Liu Jicong 已提交
583 584 585 586 587 588
      ASSERT(pHandle->execHandle.execCol.task);
      void* scanner = NULL;
      qExtractStreamScanner(pHandle->execHandle.execCol.task, &scanner);
      ASSERT(scanner);
      pHandle->execHandle.pExecReader = qExtractReaderFromStreamScanner(scanner);
      ASSERT(pHandle->execHandle.pExecReader);
L
Liu Jicong 已提交
589
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
590 591
      pHandle->pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);

L
Liu Jicong 已提交
592
      pHandle->execHandle.pExecReader = tqOpenReader(pTq->pVnode);
L
Liu Jicong 已提交
593
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
594 595
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
596 597
      pHandle->pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);

L
Liu Jicong 已提交
598
      pHandle->execHandle.execTb.suid = req.suid;
L
Liu Jicong 已提交
599
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
H
Hongze Cheng 已提交
600
      vnodeGetCtbIdList(pTq->pVnode, req.suid, tbUidList);
601
      tqDebug("vgId:%d, tq try to get all ctb, suid:%" PRId64, pTq->pVnode->config.vgId, req.suid);
L
Liu Jicong 已提交
602 603
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
S
Shengliang Guan 已提交
604
        tqDebug("vgId:%d, idx %d, uid:%" PRId64, TD_VID(pTq->pVnode), i, tbUid);
L
Liu Jicong 已提交
605
      }
L
Liu Jicong 已提交
606 607
      pHandle->execHandle.pExecReader = tqOpenReader(pTq->pVnode);
      tqReaderSetTbUidList(pHandle->execHandle.pExecReader, tbUidList);
L
Liu Jicong 已提交
608
      taosArrayDestroy(tbUidList);
L
Liu Jicong 已提交
609
    }
610
    taosHashPut(pTq->pHandle, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
S
Shengliang Guan 已提交
611
    tqDebug("try to persist handle %s consumer %" PRId64, req.subKey, pHandle->consumerId);
L
Liu Jicong 已提交
612 613
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
P
plum-lihui 已提交
614
      ASSERT(0);
L
Liu Jicong 已提交
615
    }
L
Liu Jicong 已提交
616
  } else {
L
Liu Jicong 已提交
617
    /*ASSERT(pExec->consumerId == req.oldConsumerId);*/
L
Liu Jicong 已提交
618
    // TODO handle qmsg and exec modification
L
Liu Jicong 已提交
619 620 621
    atomic_store_32(&pHandle->epoch, -1);
    atomic_store_64(&pHandle->consumerId, req.newConsumerId);
    atomic_add_fetch_32(&pHandle->epoch, 1);
L
Liu Jicong 已提交
622 623 624
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
    }
L
Liu Jicong 已提交
625
  }
L
Liu Jicong 已提交
626

L
Liu Jicong 已提交
627
  return 0;
L
Liu Jicong 已提交
628
}
629

L
Liu Jicong 已提交
630
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask) {
631
  if (pTask->taskLevel == TASK_LEVEL__AGG) {
L
Liu Jicong 已提交
632 633
    ASSERT(taosArrayGetSize(pTask->childEpInfo) != 0);
  }
L
Liu Jicong 已提交
634

L
Liu Jicong 已提交
635
  pTask->schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
636 637 638

  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
L
Liu Jicong 已提交
639 640

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
L
Liu Jicong 已提交
641
    return -1;
L
Liu Jicong 已提交
642 643
  }

L
Liu Jicong 已提交
644 645 646
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

647 648
  pTask->pMsgCb = &pTq->pVnode->msgCb;

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
  // expand executor
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
    SReadHandle handle = {
        .meta = pTq->pVnode->pMeta,
        .vnode = pTq->pVnode,
        .initTqReader = 1,
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
    ASSERT(pTask->exec.executor);
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
    SReadHandle mgHandle = {
        .vnode = NULL,
        .numOfVgroups = (int32_t)taosArrayGetSize(pTask->childEpInfo),
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &mgHandle);
L
Liu Jicong 已提交
664
    ASSERT(pTask->exec.executor);
L
Liu Jicong 已提交
665
  }
L
Liu Jicong 已提交
666 667

  // sink
L
Liu Jicong 已提交
668
  /*pTask->ahandle = pTq->pVnode;*/
669
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
670
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
671
    pTask->smaSink.smaSink = smaHandleRes;
672
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
673 674 675
    pTask->tbSink.vnode = pTq->pVnode;
    pTask->tbSink.tbSinkFunc = tqTableSink;

L
Liu Jicong 已提交
676 677
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
678

L
Liu Jicong 已提交
679
    pTask->tbSink.pTSchema =
C
Cary Xu 已提交
680
        tdGetSTSChemaFromSSChema(pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols, 1);
L
Liu Jicong 已提交
681
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
682
  }
683 684 685

  streamSetupTrigger(pTask);

L
Liu Jicong 已提交
686
  tqInfo("expand stream task on vg %d, task id %d, child id %d", TD_VID(pTq->pVnode), pTask->taskId,
687
         pTask->selfChildId);
L
Liu Jicong 已提交
688
  return 0;
L
Liu Jicong 已提交
689
}
L
Liu Jicong 已提交
690

691
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
692
  //
693
  return streamMetaAddSerializedTask(pTq->pStreamMeta, version, msg, msgLen);
L
Liu Jicong 已提交
694
}
L
Liu Jicong 已提交
695

L
Liu Jicong 已提交
696
int32_t tqProcessStreamTrigger(STQ* pTq, SSubmitReq* pReq, int64_t ver) {
L
Liu Jicong 已提交
697 698 699
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
700

L
Liu Jicong 已提交
701
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
702
  if (pSubmit == NULL) {
L
Liu Jicong 已提交
703 704
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    qError("failed to create data submit for stream since out of memory");
L
Liu Jicong 已提交
705 706 707 708
    failed = true;
  }

  while (1) {
L
Liu Jicong 已提交
709
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
L
Liu Jicong 已提交
710
    if (pIter == NULL) break;
711
    SStreamTask* pTask = *(SStreamTask**)pIter;
712
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) continue;
L
Liu Jicong 已提交
713

S
Shengliang Guan 已提交
714
    qDebug("data submit enqueue stream task: %d, ver: %" PRId64, pTask->taskId, ver);
L
Liu Jicong 已提交
715

L
Liu Jicong 已提交
716 717
    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pSubmit) < 0) {
L
Liu Jicong 已提交
718
        qError("stream task input failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
719 720 721
        continue;
      }

L
Liu Jicong 已提交
722
      if (streamSchedExec(pTask) < 0) {
L
Liu Jicong 已提交
723
        qError("stream task launch failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
724 725
        continue;
      }
L
Liu Jicong 已提交
726
    } else {
L
Liu Jicong 已提交
727
      streamTaskInputFail(pTask);
L
Liu Jicong 已提交
728 729 730
    }
  }

L
Liu Jicong 已提交
731
  if (pSubmit) {
L
Liu Jicong 已提交
732
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
733
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
734
  }
L
Liu Jicong 已提交
735 736

  return failed ? -1 : 0;
L
Liu Jicong 已提交
737 738
}

L
Liu Jicong 已提交
739
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
740
  //
L
Liu Jicong 已提交
741 742
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
L
Liu Jicong 已提交
743 744 745
  SStreamTask*       pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
    streamProcessRunReq(pTask);
L
Liu Jicong 已提交
746
    return 0;
747 748
  } else {
    return -1;
L
Liu Jicong 已提交
749
  }
L
Liu Jicong 已提交
750 751
}

L
Liu Jicong 已提交
752
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
L
Liu Jicong 已提交
753
  ASSERT(0);
754 755 756 757 758
  char*              msgStr = pMsg->pCont;
  char*              msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t            msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamDispatchReq req;
  SDecoder           decoder;
L
Liu Jicong 已提交
759
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
760
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
761 762 763 764
  int32_t taskId = req.taskId;

  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
765 766 767 768
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
769
    streamProcessDispatchReq(pTask, &req, &rsp, exec);
L
Liu Jicong 已提交
770
    return 0;
771 772
  } else {
    return -1;
L
Liu Jicong 已提交
773
  }
L
Liu Jicong 已提交
774 775
}

L
Liu Jicong 已提交
776
#if 0
L
Liu Jicong 已提交
777 778 779
int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
L
Liu Jicong 已提交
780 781 782
  SStreamTask*           pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
    streamProcessRecoverReq(pTask, pReq, pMsg);
L
Liu Jicong 已提交
783
    return 0;
784 785
  } else {
    return -1;
L
Liu Jicong 已提交
786
  }
L
Liu Jicong 已提交
787 788
}

L
Liu Jicong 已提交
789 790 791 792 793
int32_t tqProcessTaskRecoverRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverRsp* pRsp = pMsg->pCont;
  int32_t                taskId = pRsp->rspTaskId;

  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
794
  if (pTask) {
L
Liu Jicong 已提交
795
    streamProcessRecoverRsp(pTask, pRsp);
L
Liu Jicong 已提交
796
    return 0;
797 798
  } else {
    return -1;
L
Liu Jicong 已提交
799
  }
L
Liu Jicong 已提交
800
}
L
Liu Jicong 已提交
801
#endif
L
Liu Jicong 已提交
802

L
Liu Jicong 已提交
803 804 805 806
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  int32_t             taskId = pRsp->taskId;
  SStreamTask*        pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
807
  if (pTask) {
L
Liu Jicong 已提交
808
    streamProcessDispatchRsp(pTask, pRsp);
L
Liu Jicong 已提交
809
    return 0;
810 811
  } else {
    return -1;
L
Liu Jicong 已提交
812
  }
L
Liu Jicong 已提交
813
}
L
Liu Jicong 已提交
814

815
int32_t tqProcessTaskDropReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
816
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
L
Liu Jicong 已提交
817

L
Liu Jicong 已提交
818
  return streamMetaRemoveTask(pTq->pStreamMeta, pReq->taskId);
L
Liu Jicong 已提交
819
}
L
Liu Jicong 已提交
820 821 822 823 824 825 826 827 828

int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
  char*              msgStr = pMsg->pCont;
  char*              msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t            msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamRetrieveReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  tDecodeStreamRetrieveReq(&decoder, &req);
L
Liu Jicong 已提交
829 830 831
  int32_t      taskId = req.dstTaskId;
  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
L
Liu Jicong 已提交
832 833 834 835
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
836 837
    streamProcessRetrieveReq(pTask, &req, &rsp);
    return 0;
L
Liu Jicong 已提交
838 839
  } else {
    return -1;
L
Liu Jicong 已提交
840 841 842 843 844 845 846
  }
}

int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}
L
Liu Jicong 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859

void vnodeEnqueueStreamMsg(SVnode* pVnode, SRpcMsg* pMsg) {
  STQ*    pTq = pVnode->pTq;
  char*   msgStr = pMsg->pCont;
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
  int32_t code = 0;

  SStreamDispatchReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
    code = TSDB_CODE_MSG_DECODE_ERROR;
L
Liu Jicong 已提交
860
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
861 862
    goto FAIL;
  }
L
Liu Jicong 已提交
863
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
864

L
Liu Jicong 已提交
865 866 867 868
  int32_t taskId = req.taskId;

  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
L
Liu Jicong 已提交
869 870 871 872
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
873
    streamProcessDispatchReq(pTask, &req, &rsp, false);
L
Liu Jicong 已提交
874 875
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
L
Liu Jicong 已提交
876 877
    return;
  }
L
Liu Jicong 已提交
878

L
Liu Jicong 已提交
879 880 881 882 883 884 885
FAIL:
  if (pMsg->info.handle == NULL) return;
  SRpcMsg rsp = {
      .code = code,
      .info = pMsg->info,
  };
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
886 887
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
L
Liu Jicong 已提交
888
}