tq.c 27.3 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 84 85
  if (streamLoadTasks(pTq->pStreamMeta) < 0) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
86 87
  return pTq;
}
L
Liu Jicong 已提交
88

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

L
Liu Jicong 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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 已提交
124
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, res msg type %d, reqOffset:%" PRId64
L
Liu Jicong 已提交
125
          ", rspOffset:%" PRId64,
L
Liu Jicong 已提交
126 127 128 129 130
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->reqOffset, pRsp->rspOffset);

  return 0;
}

L
Liu Jicong 已提交
131 132 133 134 135 136 137 138 139 140
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);
  }

141 142 143 144 145 146
  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 已提交
147 148
  }

wmmhello's avatar
wmmhello 已提交
149 150
  int32_t len = 0;
  int32_t code = 0;
L
Liu Jicong 已提交
151 152 153 154 155
  tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
156 157 158 159 160 161 162 163 164 165 166
  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 已提交
167
  SEncoder encoder = {0};
L
Liu Jicong 已提交
168
  tEncoderInit(&encoder, abuf, len);
L
Liu Jicong 已提交
169
  tEncodeSMqDataRsp(&encoder, pRsp);
wmmhello's avatar
wmmhello 已提交
170
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
171 172

  SRpcMsg rsp = {
L
Liu Jicong 已提交
173 174 175 176 177
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
L
Liu Jicong 已提交
178
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
179

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

  return 0;
}

190 191 192 193 194 195
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) {
196 197 198 199 200 201 202 203 204
  STqOffset offset = {0};
  SDecoder  decoder;
  tDecoderInit(&decoder, msg, msgLen);
  if (tDecodeSTqOffset(&decoder, &offset) < 0) {
    ASSERT(0);
    return -1;
  }
  tDecoderClear(&decoder);

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

222 223 224
  if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
    ASSERT(0);
    return -1;
225
  }
226 227

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

237 238
  // rsp

239 240
  /*}*/
  /*}*/
241 242 243 244

  return 0;
}

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

L
Liu Jicong 已提交
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 293 294 295 296
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 已提交
297
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
298 299 300 301 302 303 304
  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 已提交
305
  SWalCkHead*  pCkHead = NULL;
L
Liu Jicong 已提交
306 307

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

  // check rebalance
  if (pHandle->consumerId != consumerId) {
S
Shengliang Guan 已提交
318 319
    tqError("tmq poll: consumer handle mismatch for consumer:%" PRId64
            ", in vgId:%d, subkey %s, handle consumer id %" PRId64,
L
Liu Jicong 已提交
320
            consumerId, TD_VID(pTq->pVnode), pReq->subKey, pHandle->consumerId);
L
Liu Jicong 已提交
321
    terrno = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
L
Liu Jicong 已提交
322 323 324 325 326 327 328 329 330
    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 已提交
331 332
  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
S
Shengliang Guan 已提交
333
  tqDebug("tmq poll: consumer %" PRId64 " (epoch %d), subkey %s, recv poll req in vg %d, req offset %s", consumerId,
L
Liu Jicong 已提交
334 335
          pReq->epoch, pHandle->subKey, TD_VID(pTq->pVnode), buf);

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

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

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

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

    walSetReaderCapacity(pHandle->pWalReader, 2048);

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

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

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

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

S
Shengliang Guan 已提交
434 435
      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 已提交
436 437 438 439

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

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

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

      } else {
        ASSERT(pHandle->fetchMeta);
        ASSERT(IS_META_MSG(pHead->msgType));
L
Liu Jicong 已提交
460
        tqDebug("fetch meta msg, ver:%" PRId64 ", type:%d", pHead->version, pHead->msgType);
L
Liu Jicong 已提交
461
        SMqMetaRsp metaRsp = {0};
462
        /*metaRsp.reqOffset = pReq->reqOffset.version;*/
wmmhello's avatar
wmmhello 已提交
463
        metaRsp.rspOffset = fetchVer;
464 465 466
        /*metaRsp.rspOffsetNew.version = fetchVer;*/
        tqOffsetResetToLog(&metaRsp.reqOffsetNew, pReq->reqOffset.version);
        tqOffsetResetToLog(&metaRsp.rspOffsetNew, fetchVer);
L
Liu Jicong 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480
        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 已提交
481
  if (pCkHead) taosMemoryFree(pCkHead);
L
Liu Jicong 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  // 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;
}

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

500
  int32_t code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
501
  ASSERT(code == 0);
502

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

L
Liu Jicong 已提交
505
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
506 507
    ASSERT(0);
  }
L
Liu Jicong 已提交
508
  return 0;
L
Liu Jicong 已提交
509 510
}

511 512 513
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
  STqCheckInfo info = {0};
  SDecoder     decoder;
L
Liu Jicong 已提交
514
  tDecoderInit(&decoder, msg, msgLen);
515
  if (tDecodeSTqCheckInfo(&decoder, &info) < 0) {
L
Liu Jicong 已提交
516 517 518 519
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  tDecoderClear(&decoder);
520 521 522 523 524
  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 已提交
525 526 527 528 529 530
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

531 532 533 534 535 536 537 538 539 540 541 542 543
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 已提交
544
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
545 546
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
547
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
L
Liu Jicong 已提交
548
  if (pHandle == NULL) {
L
Liu Jicong 已提交
549 550 551 552
    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 已提交
553
    ASSERT(req.newConsumerId != -1);
L
Liu Jicong 已提交
554 555
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
556 557
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
558 559 560
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
561

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

L
Liu Jicong 已提交
573
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
574
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
575
      pHandle->snapshotVer = ver;
L
Liu Jicong 已提交
576
      req.qmsg = NULL;
L
Liu Jicong 已提交
577 578 579 580 581 582 583 584
      SReadHandle handle = {
          .meta = pTq->pVnode->pMeta,
          .vnode = pTq->pVnode,
          .initTableReader = true,
          .initTqReader = true,
          .version = ver,
      };
      pHandle->execHandle.execCol.task =
585
          qCreateQueueExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle, &pHandle->execHandle.numOfCols,
L
Liu Jicong 已提交
586
                                   &pHandle->execHandle.pSchemaWrapper);
L
Liu Jicong 已提交
587 588 589 590 591 592
      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 已提交
593
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
594 595
      pHandle->pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);

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

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

L
Liu Jicong 已提交
631
  return 0;
L
Liu Jicong 已提交
632
}
633

L
Liu Jicong 已提交
634
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask) {
635
  if (pTask->taskLevel == TASK_LEVEL__AGG) {
L
Liu Jicong 已提交
636 637
    ASSERT(taosArrayGetSize(pTask->childEpInfo) != 0);
  }
L
Liu Jicong 已提交
638

L
Liu Jicong 已提交
639
  pTask->schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
640 641 642

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

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
L
Liu Jicong 已提交
645
    return -1;
L
Liu Jicong 已提交
646 647
  }

L
Liu Jicong 已提交
648 649 650
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

651 652
  pTask->pMsgCb = &pTq->pVnode->msgCb;

653 654
  // expand executor
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
655 656 657 658 659
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask);
    if (pTask->pState == NULL) {
      return -1;
    }

660 661 662 663
    SReadHandle handle = {
        .meta = pTq->pVnode->pMeta,
        .vnode = pTq->pVnode,
        .initTqReader = 1,
664
        .pStateBackend = pTask->pState,
665 666 667 668
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
    ASSERT(pTask->exec.executor);
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
669 670 671 672
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask);
    if (pTask->pState == NULL) {
      return -1;
    }
673 674 675
    SReadHandle mgHandle = {
        .vnode = NULL,
        .numOfVgroups = (int32_t)taosArrayGetSize(pTask->childEpInfo),
676
        .pStateBackend = pTask->pState,
677 678
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &mgHandle);
L
Liu Jicong 已提交
679
    ASSERT(pTask->exec.executor);
L
Liu Jicong 已提交
680
  }
L
Liu Jicong 已提交
681 682

  // sink
L
Liu Jicong 已提交
683
  /*pTask->ahandle = pTq->pVnode;*/
684
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
685
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
686
    pTask->smaSink.smaSink = smaHandleRes;
687
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
688 689 690
    pTask->tbSink.vnode = pTq->pVnode;
    pTask->tbSink.tbSinkFunc = tqTableSink;

L
Liu Jicong 已提交
691 692
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
693

L
Liu Jicong 已提交
694
    pTask->tbSink.pTSchema =
C
Cary Xu 已提交
695
        tdGetSTSChemaFromSSChema(pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols, 1);
L
Liu Jicong 已提交
696
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
697
  }
698 699 700

  streamSetupTrigger(pTask);

L
Liu Jicong 已提交
701
  tqInfo("expand stream task on vg %d, task id %d, child id %d", TD_VID(pTq->pVnode), pTask->taskId,
702
         pTask->selfChildId);
L
Liu Jicong 已提交
703
  return 0;
L
Liu Jicong 已提交
704
}
L
Liu Jicong 已提交
705

706
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
707
  //
708
  return streamMetaAddSerializedTask(pTq->pStreamMeta, version, msg, msgLen);
L
Liu Jicong 已提交
709
}
L
Liu Jicong 已提交
710

L
Liu Jicong 已提交
711
int32_t tqProcessStreamTrigger(STQ* pTq, SSubmitReq* pReq, int64_t ver) {
L
Liu Jicong 已提交
712 713 714
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
715

L
Liu Jicong 已提交
716
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
717
  if (pSubmit == NULL) {
L
Liu Jicong 已提交
718 719
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    qError("failed to create data submit for stream since out of memory");
L
Liu Jicong 已提交
720 721 722 723
    failed = true;
  }

  while (1) {
L
Liu Jicong 已提交
724
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
L
Liu Jicong 已提交
725
    if (pIter == NULL) break;
726
    SStreamTask* pTask = *(SStreamTask**)pIter;
727
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) continue;
L
Liu Jicong 已提交
728

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

L
Liu Jicong 已提交
731 732
    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pSubmit) < 0) {
L
Liu Jicong 已提交
733
        qError("stream task input failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
734 735 736
        continue;
      }

L
Liu Jicong 已提交
737
      if (streamSchedExec(pTask) < 0) {
L
Liu Jicong 已提交
738
        qError("stream task launch failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
739 740
        continue;
      }
L
Liu Jicong 已提交
741
    } else {
L
Liu Jicong 已提交
742
      streamTaskInputFail(pTask);
L
Liu Jicong 已提交
743 744 745
    }
  }

L
Liu Jicong 已提交
746
  if (pSubmit) {
L
Liu Jicong 已提交
747
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
748
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
749
  }
L
Liu Jicong 已提交
750 751

  return failed ? -1 : 0;
L
Liu Jicong 已提交
752 753
}

L
Liu Jicong 已提交
754
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
755
  //
L
Liu Jicong 已提交
756 757
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
L
Liu Jicong 已提交
758 759 760
  SStreamTask*       pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
    streamProcessRunReq(pTask);
L
Liu Jicong 已提交
761
    return 0;
762 763
  } else {
    return -1;
L
Liu Jicong 已提交
764
  }
L
Liu Jicong 已提交
765 766
}

L
Liu Jicong 已提交
767
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
L
Liu Jicong 已提交
768
  ASSERT(0);
769 770 771 772 773
  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 已提交
774
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
775
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
776 777 778 779
  int32_t taskId = req.taskId;

  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
780 781 782 783
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
784
    streamProcessDispatchReq(pTask, &req, &rsp, exec);
L
Liu Jicong 已提交
785
    return 0;
786 787
  } else {
    return -1;
L
Liu Jicong 已提交
788
  }
L
Liu Jicong 已提交
789 790
}

L
Liu Jicong 已提交
791
#if 0
L
Liu Jicong 已提交
792 793 794
int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
L
Liu Jicong 已提交
795 796 797
  SStreamTask*           pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
    streamProcessRecoverReq(pTask, pReq, pMsg);
L
Liu Jicong 已提交
798
    return 0;
799 800
  } else {
    return -1;
L
Liu Jicong 已提交
801
  }
L
Liu Jicong 已提交
802 803
}

L
Liu Jicong 已提交
804 805 806 807 808
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 已提交
809
  if (pTask) {
L
Liu Jicong 已提交
810
    streamProcessRecoverRsp(pTask, pRsp);
L
Liu Jicong 已提交
811
    return 0;
812 813
  } else {
    return -1;
L
Liu Jicong 已提交
814
  }
L
Liu Jicong 已提交
815
}
L
Liu Jicong 已提交
816
#endif
L
Liu Jicong 已提交
817

L
Liu Jicong 已提交
818 819 820 821
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 已提交
822
  if (pTask) {
L
Liu Jicong 已提交
823
    streamProcessDispatchRsp(pTask, pRsp);
L
Liu Jicong 已提交
824
    return 0;
825 826
  } else {
    return -1;
L
Liu Jicong 已提交
827
  }
L
Liu Jicong 已提交
828
}
L
Liu Jicong 已提交
829

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

L
Liu Jicong 已提交
833
  return streamMetaRemoveTask(pTq->pStreamMeta, pReq->taskId);
L
Liu Jicong 已提交
834
}
L
Liu Jicong 已提交
835 836 837 838 839 840 841 842 843

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 已提交
844 845 846
  int32_t      taskId = req.dstTaskId;
  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
L
Liu Jicong 已提交
847 848 849 850
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
851 852
    streamProcessRetrieveReq(pTask, &req, &rsp);
    return 0;
L
Liu Jicong 已提交
853 854
  } else {
    return -1;
L
Liu Jicong 已提交
855 856 857 858 859 860 861
  }
}

int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}
L
Liu Jicong 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874

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 已提交
875
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
876 877
    goto FAIL;
  }
L
Liu Jicong 已提交
878
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
879

L
Liu Jicong 已提交
880 881 882 883
  int32_t taskId = req.taskId;

  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
L
Liu Jicong 已提交
884 885 886 887
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
888
    streamProcessDispatchReq(pTask, &req, &rsp, false);
L
Liu Jicong 已提交
889 890
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
L
Liu Jicong 已提交
891 892
    return;
  }
L
Liu Jicong 已提交
893

L
Liu Jicong 已提交
894 895 896 897 898 899 900
FAIL:
  if (pMsg->info.handle == NULL) return;
  SRpcMsg rsp = {
      .code = code,
      .info = pMsg->info,
  };
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
901 902
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
L
Liu Jicong 已提交
903
}