tq.c 42.7 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

54 55 56 57 58 59 60 61
static void destroySTqHandle(void* data) {
  STqHandle* pData = (STqHandle*)data;
  qDestroyTask(pData->execHandle.task);
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
    tqCloseReader(pData->execHandle.pExecReader);
    walCloseReader(pData->pWalReader);
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
L
Liu Jicong 已提交
62
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
63 64 65 66 67
    walCloseReader(pData->pWalReader);
    tqCloseReader(pData->execHandle.pExecReader);
  }
}

L
Liu Jicong 已提交
68
static void tqPushEntryFree(void* data) {
L
Liu Jicong 已提交
69
  STqPushEntry* p = *(void**)data;
L
Liu Jicong 已提交
70 71 72
  taosMemoryFree(p);
}

L
Liu Jicong 已提交
73
STQ* tqOpen(const char* path, SVnode* pVnode) {
74
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
L
Liu Jicong 已提交
75
  if (pTq == NULL) {
L
Liu Jicong 已提交
76
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
77 78
    return NULL;
  }
H
Hongze Cheng 已提交
79
  pTq->path = strdup(path);
L
Liu Jicong 已提交
80
  pTq->pVnode = pVnode;
81

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

84 85
  taosHashSetFreeFp(pTq->pHandle, destroySTqHandle);

L
Liu Jicong 已提交
86 87
  taosInitRWLatch(&pTq->pushLock);
  pTq->pPushMgr = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
L
Liu Jicong 已提交
88
  taosHashSetFreeFp(pTq->pPushMgr, tqPushEntryFree);
L
Liu Jicong 已提交
89

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

L
Liu Jicong 已提交
92
  if (tqMetaOpen(pTq) < 0) {
93 94 95
    ASSERT(0);
  }

L
Liu Jicong 已提交
96 97
  pTq->pOffsetStore = tqOffsetOpen(pTq);
  if (pTq->pOffsetStore == NULL) {
98 99 100
    ASSERT(0);
  }

101
  pTq->pStreamMeta = streamMetaOpen(path, pTq, (FTaskExpand*)tqExpandTask, pTq->pVnode->config.vgId);
L
Liu Jicong 已提交
102 103 104 105
  if (pTq->pStreamMeta == NULL) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
106 107 108 109
  if (streamLoadTasks(pTq->pStreamMeta) < 0) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
110 111
  return pTq;
}
L
Liu Jicong 已提交
112

L
Liu Jicong 已提交
113
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
114
  if (pTq) {
115
    tqOffsetClose(pTq->pOffsetStore);
116 117 118
    taosHashCleanup(pTq->pHandle);
    taosHashCleanup(pTq->pPushMgr);
    taosHashCleanup(pTq->pCheckInfo);
119
    taosMemoryFree(pTq->path);
L
Liu Jicong 已提交
120
    tqMetaClose(pTq);
L
Liu Jicong 已提交
121
    streamMetaClose(pTq->pStreamMeta);
wafwerar's avatar
wafwerar 已提交
122
    taosMemoryFree(pTq);
H
Hongze Cheng 已提交
123
  }
L
Liu Jicong 已提交
124
}
L
Liu Jicong 已提交
125

L
Liu Jicong 已提交
126
int32_t tqSendMetaPollRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp) {
127 128 129 130 131 132 133
  int32_t len = 0;
  int32_t code = 0;
  tEncodeSize(tEncodeSMqMetaRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
134 135 136 137 138 139 140 141 142 143
  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));
144 145 146 147 148

  SEncoder encoder = {0};
  tEncoderInit(&encoder, abuf, len);
  tEncodeSMqMetaRsp(&encoder, pRsp);
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
149 150 151 152 153 154 155 156 157

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

158 159
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, res msg type %d, offset type:%d",
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
L
Liu Jicong 已提交
160 161 162 163

  return 0;
}

L
Liu Jicong 已提交
164 165 166 167 168 169 170 171 172 173
int32_t tqPushDataRsp(STQ* pTq, STqPushEntry* pPushEntry) {
  SMqDataRsp* pRsp = &pPushEntry->dataRsp;

  ASSERT(taosArrayGetSize(pRsp->blockData) == pRsp->blockNum);
  ASSERT(taosArrayGetSize(pRsp->blockDataLen) == pRsp->blockNum);

  ASSERT(!pRsp->withSchema);
  ASSERT(taosArrayGetSize(pRsp->blockSchema) == 0);

  if (pRsp->reqOffset.type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
174 175 176 177 178
    /*if (pRsp->blockNum > 0) {*/
    /*ASSERT(pRsp->rspOffset.version > pRsp->reqOffset.version);*/
    /*} else {*/
    ASSERT(pRsp->rspOffset.version > pRsp->reqOffset.version);
    /*}*/
L
Liu Jicong 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  }

  int32_t len = 0;
  int32_t code = 0;
  tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);

  if (code < 0) {
    return -1;
  }

  int32_t tlen = sizeof(SMqRspHead) + len;
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    return -1;
  }

L
Liu Jicong 已提交
195
  memcpy(buf, &pPushEntry->dataRsp.head, sizeof(SMqRspHead));
L
Liu Jicong 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

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

  SEncoder encoder = {0};
  tEncoderInit(&encoder, abuf, len);
  tEncodeSMqDataRsp(&encoder, pRsp);
  tEncoderClear(&encoder);

  SRpcMsg rsp = {
      .info = pPushEntry->pInfo,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };

  tmsgSendRsp(&rsp);

  char buf1[80] = {0};
  char buf2[80] = {0};
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) push rsp, block num: %d, reqOffset:%s, rspOffset:%s",
L
Liu Jicong 已提交
218
          TD_VID(pTq->pVnode), pRsp->head.consumerId, pRsp->head.epoch, pRsp->blockNum, buf1, buf2);
L
Liu Jicong 已提交
219 220 221 222

  return 0;
}

L
Liu Jicong 已提交
223 224 225 226
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);

L
Liu Jicong 已提交
227 228
  ASSERT(!pRsp->withSchema);
  ASSERT(taosArrayGetSize(pRsp->blockSchema) == 0);
L
Liu Jicong 已提交
229

230 231 232 233 234 235
  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 已提交
236 237
  }

wmmhello's avatar
wmmhello 已提交
238 239
  int32_t len = 0;
  int32_t code = 0;
L
Liu Jicong 已提交
240 241 242 243 244
  tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
245 246 247 248 249 250 251 252 253 254 255
  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 已提交
256
  SEncoder encoder = {0};
L
Liu Jicong 已提交
257
  tEncoderInit(&encoder, abuf, len);
L
Liu Jicong 已提交
258
  tEncodeSMqDataRsp(&encoder, pRsp);
wmmhello's avatar
wmmhello 已提交
259
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
260 261

  SRpcMsg rsp = {
L
Liu Jicong 已提交
262 263 264 265 266
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
L
Liu Jicong 已提交
267
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
268

wmmhello's avatar
wmmhello 已提交
269 270
  char buf1[80] = {0};
  char buf2[80] = {0};
L
Liu Jicong 已提交
271 272
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);
S
Shengliang Guan 已提交
273
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, block num: %d, reqOffset:%s, rspOffset:%s",
L
Liu Jicong 已提交
274
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2);
L
Liu Jicong 已提交
275 276 277 278

  return 0;
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 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 328 329 330 331 332 333 334 335 336 337 338
int32_t tqSendTaosxRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const STaosxRsp* 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);
  }

  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);
    }
  }

  int32_t len = 0;
  int32_t code = 0;
  tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    return -1;
  }

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

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

  SEncoder encoder = {0};
  tEncoderInit(&encoder, abuf, len);
  tEncodeSTaosxRsp(&encoder, pRsp);
  tEncoderClear(&encoder);

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

  char buf1[80] = {0};
  char buf2[80] = {0};
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);
  tqDebug("taosx rsp, vgId:%d, from consumer:%" PRId64
          ", (epoch %d) send rsp, block num: %d, reqOffset:%s, rspOffset:%s",
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2);

  return 0;
}

339 340 341 342 343 344
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) {
345 346 347 348 349 350 351 352 353
  STqOffset offset = {0};
  SDecoder  decoder;
  tDecoderInit(&decoder, msg, msgLen);
  if (tDecodeSTqOffset(&decoder, &offset) < 0) {
    ASSERT(0);
    return -1;
  }
  tDecoderClear(&decoder);

wmmhello's avatar
wmmhello 已提交
354
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA || offset.val.type == TMQ_OFFSET__SNAPSHOT_META) {
L
Liu Jicong 已提交
355 356
    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 已提交
357
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
S
Shengliang Guan 已提交
358
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, offset.subKey,
L
Liu Jicong 已提交
359
            TD_VID(pTq->pVnode), offset.val.version);
360 361 362
    if (offset.val.version + 1 == version) {
      offset.val.version += 1;
    }
363 364 365
  } else {
    ASSERT(0);
  }
366 367 368 369 370
  STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, offset.subKey);
  if (pOffset != NULL && tqOffsetLessOrEqual(&offset, pOffset)) {
    return 0;
  }

371 372 373
  if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
    ASSERT(0);
    return -1;
374
  }
375 376

  if (offset.val.type == TMQ_OFFSET__LOG) {
377
    STqHandle* pHandle = taosHashGet(pTq->pHandle, offset.subKey, strlen(offset.subKey));
L
Liu Jicong 已提交
378 379 380 381 382
    if (pHandle) {
      if (walRefVer(pHandle->pRef, offset.val.version) < 0) {
        ASSERT(0);
        return -1;
      }
383 384 385
    }
  }

386 387
  // rsp

388 389
  /*}*/
  /*}*/
390 391 392 393

  return 0;
}

L
Liu Jicong 已提交
394
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
L
Liu Jicong 已提交
395 396
  void* pIter = NULL;
  while (1) {
397
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
L
Liu Jicong 已提交
398
    if (pIter == NULL) break;
399
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
L
Liu Jicong 已提交
400 401
    if (pCheck->ntbUid == tbUid) {
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
L
Liu Jicong 已提交
402
      for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
403 404
        int16_t forbidColId = *(int16_t*)taosArrayGet(pCheck->colIdList, i);
        if (forbidColId == colId) {
405
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
L
Liu Jicong 已提交
406 407 408 409 410 411 412 413
          return -1;
        }
      }
    }
  }
  return 0;
}

L
Liu Jicong 已提交
414 415 416 417 418 419 420 421 422 423
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;
  }

L
Liu Jicong 已提交
424 425
  pRsp->withTbName = 0;
#if 0
L
Liu Jicong 已提交
426 427 428 429 430 431 432 433
  pRsp->withTbName = pReq->withTbName;
  if (pRsp->withTbName) {
    pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
    if (pRsp->blockTbName == NULL) {
      // TODO free
      return -1;
    }
  }
L
Liu Jicong 已提交
434
#endif
L
Liu Jicong 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448

  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;
}

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
static int32_t tqInitTaosxRsp(STaosxRsp* pRsp, const SMqPollReq* pReq) {
  pRsp->reqOffset = pReq->reqOffset;

  pRsp->withTbName = 1;
  pRsp->withSchema = 1;
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));

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

L
Liu Jicong 已提交
465
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
466 467 468 469 470 471
  SMqPollReq*  pReq = pMsg->pCont;
  int64_t      consumerId = pReq->consumerId;
  int32_t      reqEpoch = pReq->epoch;
  int32_t      code = 0;
  STqOffsetVal reqOffset = pReq->reqOffset;
  STqOffsetVal fetchOffsetNew;
wmmhello's avatar
wmmhello 已提交
472
  SWalCkHead*  pCkHead = NULL;
L
Liu Jicong 已提交
473 474

  // 1.find handle
475
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
476 477
  /*ASSERT(pHandle);*/
  if (pHandle == NULL) {
S
Shengliang Guan 已提交
478 479
    tqError("tmq poll: no consumer handle for consumer:%" PRId64 ", in vgId:%d, subkey %s", consumerId,
            TD_VID(pTq->pVnode), pReq->subKey);
L
Liu Jicong 已提交
480 481 482 483 484
    return -1;
  }

  // check rebalance
  if (pHandle->consumerId != consumerId) {
S
Shengliang Guan 已提交
485 486
    tqError("tmq poll: consumer handle mismatch for consumer:%" PRId64
            ", in vgId:%d, subkey %s, handle consumer id %" PRId64,
L
Liu Jicong 已提交
487
            consumerId, TD_VID(pTq->pVnode), pReq->subKey, pHandle->consumerId);
L
Liu Jicong 已提交
488
    terrno = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
L
Liu Jicong 已提交
489 490 491 492 493 494 495 496 497
    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 已提交
498 499
  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
S
Shengliang Guan 已提交
500
  tqDebug("tmq poll: consumer %" PRId64 " (epoch %d), subkey %s, recv poll req in vg %d, req offset %s", consumerId,
L
Liu Jicong 已提交
501 502
          pReq->epoch, pHandle->subKey, TD_VID(pTq->pVnode), buf);

L
Liu Jicong 已提交
503 504 505 506 507 508 509
  // 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 已提交
510 511
      char formatBuf[80];
      tFormatOffset(formatBuf, 80, &fetchOffsetNew);
L
Liu Jicong 已提交
512 513
      tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, offset reset to %s", consumerId, pHandle->subKey,
              TD_VID(pTq->pVnode), formatBuf);
L
Liu Jicong 已提交
514 515
    } else {
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEAST) {
L
Liu Jicong 已提交
516 517
        if (pReq->useSnapshot) {
          if (pHandle->fetchMeta) {
wmmhello's avatar
wmmhello 已提交
518
            tqOffsetResetToMeta(&fetchOffsetNew, 0);
L
Liu Jicong 已提交
519
          } else {
520
            tqOffsetResetToData(&fetchOffsetNew, 0, 0);
L
Liu Jicong 已提交
521 522 523 524 525
          }
        } else {
          tqOffsetResetToLog(&fetchOffsetNew, walGetFirstVer(pTq->pVnode->pWal));
        }
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
526 527 528
        SMqDataRsp dataRsp = {0};
        tqInitDataRsp(&dataRsp, pReq, pHandle->execHandle.subType);

L
Liu Jicong 已提交
529
        tqOffsetResetToLog(&dataRsp.rspOffset, walGetLastVer(pTq->pVnode->pWal));
S
Shengliang Guan 已提交
530 531
        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 已提交
532 533 534
        if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
          code = -1;
        }
L
Liu Jicong 已提交
535 536
        tDeleteSMqDataRsp(&dataRsp);
        return code;
L
Liu Jicong 已提交
537
      } else if (reqOffset.type == TMQ_OFFSET__RESET_NONE) {
538 539
        tqError("tmq poll: subkey %s, no offset committed for consumer %" PRId64
                " in vg %d, subkey %s, reset none failed",
L
Liu Jicong 已提交
540
                pHandle->subKey, consumerId, TD_VID(pTq->pVnode), pReq->subKey);
L
Liu Jicong 已提交
541
        terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
542
        return -1;
L
Liu Jicong 已提交
543 544 545 546
      }
    }
  }

547 548 549
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
    SMqDataRsp dataRsp = {0};
    tqInitDataRsp(&dataRsp, pReq, pHandle->execHandle.subType);
L
Liu Jicong 已提交
550 551
    // lock
    taosWLockLatch(&pTq->pushLock);
552 553
    tqScanData(pTq, pHandle, &dataRsp, &fetchOffsetNew);

L
Liu Jicong 已提交
554
#if 1
L
Liu Jicong 已提交
555
    if (dataRsp.blockNum == 0 && dataRsp.reqOffset.type == TMQ_OFFSET__LOG &&
556
        dataRsp.reqOffset.version == dataRsp.rspOffset.version) {
L
Liu Jicong 已提交
557 558 559
      STqPushEntry* pPushEntry = taosMemoryCalloc(1, sizeof(STqPushEntry));
      if (pPushEntry != NULL) {
        pPushEntry->pInfo = pMsg->info;
L
Liu Jicong 已提交
560
        memcpy(pPushEntry->subKey, pHandle->subKey, TSDB_SUBSCRIBE_KEY_LEN);
L
Liu Jicong 已提交
561
        dataRsp.withTbName = 0;
L
Liu Jicong 已提交
562
        memcpy(&pPushEntry->dataRsp, &dataRsp, sizeof(SMqDataRsp));
L
Liu Jicong 已提交
563 564 565
        pPushEntry->dataRsp.head.consumerId = consumerId;
        pPushEntry->dataRsp.head.epoch = reqEpoch;
        pPushEntry->dataRsp.head.mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
L
Liu Jicong 已提交
566
        taosHashPut(pTq->pPushMgr, pHandle->subKey, strlen(pHandle->subKey) + 1, &pPushEntry, sizeof(void*));
567
        tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d save handle to push mgr", consumerId, pHandle->subKey,
L
Liu Jicong 已提交
568 569 570 571 572 573 574
                TD_VID(pTq->pVnode));
        // unlock
        taosWUnLockLatch(&pTq->pushLock);
        return 0;
      }
    }
    taosWUnLockLatch(&pTq->pushLock);
L
Liu Jicong 已提交
575
#endif
L
Liu Jicong 已提交
576

577 578 579 580
    if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
      code = -1;
    }

581 582
    tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, send data blockNum:%d, offset type:%d, uid:%" PRId64
            ", version:%" PRId64 "",
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
            consumerId, pHandle->subKey, TD_VID(pTq->pVnode), dataRsp.blockNum, dataRsp.rspOffset.type,
            dataRsp.rspOffset.uid, dataRsp.rspOffset.version);

    tDeleteSMqDataRsp(&dataRsp);
    return code;
  }

  // for taosx
  ASSERT(pHandle->execHandle.subType != TOPIC_SUB_TYPE__COLUMN);

  SMqMetaRsp metaRsp = {0};

  STaosxRsp taosxRsp = {0};
  tqInitTaosxRsp(&taosxRsp, pReq);

  if (fetchOffsetNew.type != TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
599
    tqScanTaosx(pTq, pHandle, &taosxRsp, &metaRsp, &fetchOffsetNew);
L
Liu Jicong 已提交
600

L
Liu Jicong 已提交
601
    if (metaRsp.metaRspLen > 0) {
wmmhello's avatar
wmmhello 已提交
602 603 604
      if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) {
        code = -1;
      }
605 606 607
      tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, send meta offset type:%d,uid:%" PRId64
              ",version:%" PRId64 "",
              consumerId, pHandle->subKey, TD_VID(pTq->pVnode), metaRsp.rspOffset.type, metaRsp.rspOffset.uid,
L
Liu Jicong 已提交
608
              metaRsp.rspOffset.version);
wmmhello's avatar
wmmhello 已提交
609
      taosMemoryFree(metaRsp.metaRsp);
610 611
      tDeleteSTaosxRsp(&taosxRsp);
      return code;
wmmhello's avatar
wmmhello 已提交
612 613
    }

614 615
    if (taosxRsp.blockNum > 0) {
      if (tqSendTaosxRsp(pTq, pMsg, pReq, &taosxRsp) < 0) {
wmmhello's avatar
wmmhello 已提交
616 617
        code = -1;
      }
618 619
      tDeleteSTaosxRsp(&taosxRsp);
      return code;
L
Liu Jicong 已提交
620
    } else {
621
      fetchOffsetNew = taosxRsp.rspOffset;
622
    }
wmmhello's avatar
wmmhello 已提交
623

624 625
    tqDebug("taosx poll: consumer %" PRId64 ", subkey %s, vg %d, send data blockNum:%d, offset type:%d,uid:%" PRId64
            ",version:%" PRId64 "",
626 627
            consumerId, pHandle->subKey, TD_VID(pTq->pVnode), taosxRsp.blockNum, taosxRsp.rspOffset.type,
            taosxRsp.rspOffset.uid, taosxRsp.rspOffset.version);
628 629
  }

630
  if (fetchOffsetNew.type == TMQ_OFFSET__LOG) {
wmmhello's avatar
wmmhello 已提交
631 632 633
    int64_t fetchVer = fetchOffsetNew.version + 1;
    pCkHead = taosMemoryMalloc(sizeof(SWalCkHead) + 2048);
    if (pCkHead == NULL) {
634
      tDeleteSTaosxRsp(&taosxRsp);
635
      return -1;
636 637
    }

wmmhello's avatar
wmmhello 已提交
638 639 640 641 642 643
    walSetReaderCapacity(pHandle->pWalReader, 2048);

    while (1) {
      consumerEpoch = atomic_load_32(&pHandle->epoch);
      if (consumerEpoch > reqEpoch) {
        tqWarn("tmq poll: consumer %" PRId64 " (epoch %d), subkey %s, vg %d offset %" PRId64
L
Liu Jicong 已提交
644
               ", found new consumer epoch %d, discard req epoch %d",
wmmhello's avatar
wmmhello 已提交
645 646 647 648 649
               consumerId, pReq->epoch, pHandle->subKey, TD_VID(pTq->pVnode), fetchVer, consumerEpoch, reqEpoch);
        break;
      }

      if (tqFetchLog(pTq, pHandle, &fetchVer, &pCkHead) < 0) {
650 651
        tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);
        if (tqSendTaosxRsp(pTq, pMsg, pReq, &taosxRsp) < 0) {
wmmhello's avatar
wmmhello 已提交
652 653
          code = -1;
        }
654
        tDeleteSTaosxRsp(&taosxRsp);
L
Liu Jicong 已提交
655
        taosMemoryFreeClear(pCkHead);
656
        return code;
wmmhello's avatar
wmmhello 已提交
657 658 659
      }

      SWalCont* pHead = &pCkHead->head;
wmmhello's avatar
wmmhello 已提交
660

wmmhello's avatar
wmmhello 已提交
661 662 663 664 665 666
      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);

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

667
        if (tqTaosxScanLog(pTq, pHandle, pCont, &taosxRsp) < 0) {
wmmhello's avatar
wmmhello 已提交
668 669 670 671
          /*ASSERT(0);*/
        }
        // TODO batch optimization:
        // TODO continue scan until meeting batch requirement
672 673 674
        if (taosxRsp.blockNum > 0 /* threshold */) {
          tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);
          if (tqSendTaosxRsp(pTq, pMsg, pReq, &taosxRsp) < 0) {
wmmhello's avatar
wmmhello 已提交
675 676
            code = -1;
          }
677
          tDeleteSTaosxRsp(&taosxRsp);
L
Liu Jicong 已提交
678
          taosMemoryFreeClear(pCkHead);
679
          return code;
wmmhello's avatar
wmmhello 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693
        } else {
          fetchVer++;
        }

      } else {
        ASSERT(pHandle->fetchMeta);
        ASSERT(IS_META_MSG(pHead->msgType));
        tqDebug("fetch meta msg, ver:%" PRId64 ", type:%d", pHead->version, pHead->msgType);
        tqOffsetResetToLog(&metaRsp.rspOffset, fetchVer);
        metaRsp.resMsgType = pHead->msgType;
        metaRsp.metaRspLen = pHead->bodyLen;
        metaRsp.metaRsp = pHead->body;
        if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) {
          code = -1;
L
Liu Jicong 已提交
694
          taosMemoryFreeClear(pCkHead);
695
          tDeleteSTaosxRsp(&taosxRsp);
696
          return code;
wmmhello's avatar
wmmhello 已提交
697 698
        }
        code = 0;
L
Liu Jicong 已提交
699
        taosMemoryFreeClear(pCkHead);
700
        tDeleteSTaosxRsp(&taosxRsp);
701
        return code;
wmmhello's avatar
wmmhello 已提交
702 703 704
      }
    }
  }
705
  tDeleteSTaosxRsp(&taosxRsp);
L
Liu Jicong 已提交
706
  taosMemoryFreeClear(pCkHead);
707
  return 0;
L
Liu Jicong 已提交
708 709
}

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

L
Liu Jicong 已提交
713 714 715 716 717 718 719 720
  taosWLockLatch(&pTq->pushLock);
  int32_t code = taosHashRemove(pTq->pPushMgr, pReq->subKey, strlen(pReq->subKey));
  if (code != 0) {
    tqDebug("vgId:%d, tq remove push handle %s", pTq->pVnode->config.vgId, pReq->subKey);
  }
  taosWUnLockLatch(&pTq->pushLock);

  code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
721 722 723
  if (code != 0) {
    tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
  }
724

L
Liu Jicong 已提交
725 726 727 728
  code = tqOffsetDelete(pTq->pOffsetStore, pReq->subKey);
  if (code != 0) {
    tqError("cannot process tq delete req %s, since no such offset", pReq->subKey);
  }
L
Liu Jicong 已提交
729

L
Liu Jicong 已提交
730
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
731 732
    ASSERT(0);
  }
L
Liu Jicong 已提交
733
  return 0;
L
Liu Jicong 已提交
734 735
}

736 737 738
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
  STqCheckInfo info = {0};
  SDecoder     decoder;
L
Liu Jicong 已提交
739
  tDecoderInit(&decoder, msg, msgLen);
740
  if (tDecodeSTqCheckInfo(&decoder, &info) < 0) {
L
Liu Jicong 已提交
741 742 743 744
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  tDecoderClear(&decoder);
745 746 747 748 749
  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 已提交
750 751 752 753 754 755
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

756 757 758 759 760 761 762 763 764 765 766 767 768
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 已提交
769
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
770 771
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
772
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
L
Liu Jicong 已提交
773
  if (pHandle == NULL) {
L
Liu Jicong 已提交
774
    if (req.oldConsumerId != -1) {
775 776
      tqError("vgId:%d, build new consumer handle %s for consumer %" PRId64 ", but old consumerId is %" PRId64 "",
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
L
Liu Jicong 已提交
777
    }
L
Liu Jicong 已提交
778
    if (req.newConsumerId == -1) {
779
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64 "", req.vgId, req.newConsumerId);
L
Liu Jicong 已提交
780 781
      return 0;
    }
L
Liu Jicong 已提交
782 783
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
784 785
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
786 787 788
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
789

L
Liu Jicong 已提交
790
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
791
    pHandle->fetchMeta = req.withMeta;
wmmhello's avatar
wmmhello 已提交
792

793 794 795 796
    // TODO version should be assigned and refed during preprocess
    SWalRef* pRef = walRefCommittedVer(pTq->pVnode->pWal);
    if (pRef == NULL) {
      ASSERT(0);
L
Liu Jicong 已提交
797
      return -1;
798 799 800
    }
    int64_t ver = pRef->refVer;
    pHandle->pRef = pRef;
L
Liu Jicong 已提交
801

802 803 804 805 806 807 808
    SReadHandle handle = {
        .meta = pTq->pVnode->pMeta,
        .vnode = pTq->pVnode,
        .initTableReader = true,
        .initTqReader = true,
        .version = ver,
    };
wmmhello's avatar
wmmhello 已提交
809
    pHandle->snapshotVer = ver;
810

L
Liu Jicong 已提交
811
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
812
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
813
      req.qmsg = NULL;
814 815

      pHandle->execHandle.task =
wmmhello's avatar
wmmhello 已提交
816
          qCreateQueueExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle, &pHandle->execHandle.numOfCols,
L
Liu Jicong 已提交
817
                                   &pHandle->execHandle.pSchemaWrapper);
818
      ASSERT(pHandle->execHandle.task);
L
Liu Jicong 已提交
819
      void* scanner = NULL;
820
      qExtractStreamScanner(pHandle->execHandle.task, &scanner);
L
Liu Jicong 已提交
821 822 823
      ASSERT(scanner);
      pHandle->execHandle.pExecReader = qExtractReaderFromStreamScanner(scanner);
      ASSERT(pHandle->execHandle.pExecReader);
L
Liu Jicong 已提交
824
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
wmmhello's avatar
wmmhello 已提交
825
      pHandle->pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);
L
Liu Jicong 已提交
826
      pHandle->execHandle.pExecReader = tqOpenReader(pTq->pVnode);
L
Liu Jicong 已提交
827
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
828
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
829 830
      buildSnapContext(handle.meta, handle.version, 0, pHandle->execHandle.subType, pHandle->fetchMeta,
                       (SSnapContext**)(&handle.sContext));
831

832
      pHandle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &handle, NULL, NULL);
L
Liu Jicong 已提交
833
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
wmmhello's avatar
wmmhello 已提交
834 835 836 837
      pHandle->pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);

      pHandle->execHandle.execTb.suid = req.suid;

L
Liu Jicong 已提交
838
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
H
Hongze Cheng 已提交
839
      vnodeGetCtbIdList(pTq->pVnode, req.suid, tbUidList);
840
      tqDebug("vgId:%d, tq try to get all ctb, suid:%" PRId64, pTq->pVnode->config.vgId, req.suid);
L
Liu Jicong 已提交
841 842
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
S
Shengliang Guan 已提交
843
        tqDebug("vgId:%d, idx %d, uid:%" PRId64, TD_VID(pTq->pVnode), i, tbUid);
L
Liu Jicong 已提交
844
      }
L
Liu Jicong 已提交
845 846
      pHandle->execHandle.pExecReader = tqOpenReader(pTq->pVnode);
      tqReaderSetTbUidList(pHandle->execHandle.pExecReader, tbUidList);
L
Liu Jicong 已提交
847
      taosArrayDestroy(tbUidList);
wmmhello's avatar
wmmhello 已提交
848

L
Liu Jicong 已提交
849 850 851
      buildSnapContext(handle.meta, handle.version, req.suid, pHandle->execHandle.subType, pHandle->fetchMeta,
                       (SSnapContext**)(&handle.sContext));
      pHandle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &handle, NULL, NULL);
L
Liu Jicong 已提交
852
    }
853
    taosHashPut(pTq->pHandle, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
S
Shengliang Guan 已提交
854
    tqDebug("try to persist handle %s consumer %" PRId64, req.subKey, pHandle->consumerId);
L
Liu Jicong 已提交
855 856
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
P
plum-lihui 已提交
857
      ASSERT(0);
L
Liu Jicong 已提交
858
    }
L
Liu Jicong 已提交
859
  } else {
L
Liu Jicong 已提交
860
    /*ASSERT(pExec->consumerId == req.oldConsumerId);*/
L
Liu Jicong 已提交
861
    // TODO handle qmsg and exec modification
L
Liu Jicong 已提交
862 863 864
    atomic_store_32(&pHandle->epoch, -1);
    atomic_store_64(&pHandle->consumerId, req.newConsumerId);
    atomic_add_fetch_32(&pHandle->epoch, 1);
L
Liu Jicong 已提交
865 866
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
L
Liu Jicong 已提交
867
      ASSERT(0);
L
Liu Jicong 已提交
868
    }
L
Liu Jicong 已提交
869
    // close handle
L
Liu Jicong 已提交
870
  }
L
Liu Jicong 已提交
871

L
Liu Jicong 已提交
872
  return 0;
L
Liu Jicong 已提交
873
}
874

875
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask, int64_t ver) {
876
  if (pTask->taskLevel == TASK_LEVEL__AGG) {
L
Liu Jicong 已提交
877 878
    ASSERT(taosArrayGetSize(pTask->childEpInfo) != 0);
  }
L
Liu Jicong 已提交
879

L
Liu Jicong 已提交
880
  pTask->schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
881 882 883

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

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
L
Liu Jicong 已提交
886
    return -1;
L
Liu Jicong 已提交
887 888
  }

L
Liu Jicong 已提交
889 890 891
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

892 893
  pTask->pMsgCb = &pTq->pVnode->msgCb;

894 895
  pTask->startVer = ver;

896 897
  // expand executor
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
898
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
899 900 901 902
    if (pTask->pState == NULL) {
      return -1;
    }

903 904 905 906
    SReadHandle handle = {
        .meta = pTq->pVnode->pMeta,
        .vnode = pTq->pVnode,
        .initTqReader = 1,
907
        .pStateBackend = pTask->pState,
908 909 910
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
    ASSERT(pTask->exec.executor);
911 912 913 914

    if (pTask->fillHistory) {
      pTask->taskStatus = TASK_STATUS__RECOVER_PREPARE;
    }
915
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
916
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
917 918 919
    if (pTask->pState == NULL) {
      return -1;
    }
920 921 922
    SReadHandle mgHandle = {
        .vnode = NULL,
        .numOfVgroups = (int32_t)taosArrayGetSize(pTask->childEpInfo),
923
        .pStateBackend = pTask->pState,
924 925
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &mgHandle);
L
Liu Jicong 已提交
926
    ASSERT(pTask->exec.executor);
L
Liu Jicong 已提交
927
  }
L
Liu Jicong 已提交
928 929

  // sink
L
Liu Jicong 已提交
930
  /*pTask->ahandle = pTq->pVnode;*/
931
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
932
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
933
    pTask->smaSink.smaSink = smaHandleRes;
934
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
935
    pTask->tbSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
936
    pTask->tbSink.tbSinkFunc = tqSinkToTablePipeline;
L
Liu Jicong 已提交
937

L
Liu Jicong 已提交
938 939
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
940

L
Liu Jicong 已提交
941
    pTask->tbSink.pTSchema =
C
Cary Xu 已提交
942
        tdGetSTSChemaFromSSChema(pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols, 1);
L
Liu Jicong 已提交
943
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
944
  }
945 946 947

  streamSetupTrigger(pTask);

L
Liu Jicong 已提交
948
  tqInfo("expand stream task on vg %d, task id %d, child id %d", TD_VID(pTq->pVnode), pTask->taskId,
949
         pTask->selfChildId);
L
Liu Jicong 已提交
950
  return 0;
L
Liu Jicong 已提交
951
}
L
Liu Jicong 已提交
952

953
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
  int32_t code;
#if 0
  code = streamMetaAddSerializedTask(pTq->pStreamMeta, version, msg, msgLen);
  if (code < 0) return code;
#endif

  // 1.deserialize msg and build task
  SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
  code = tDecodeSStreamTask(&decoder, pTask);
  if (code < 0) {
    tDecoderClear(&decoder);
    taosMemoryFree(pTask);
    return -1;
  }
  tDecoderClear(&decoder);

  // 2.save task
  code = streamMetaAddTask(pTq->pStreamMeta, version, pTask);
  if (code < 0) {
    return -1;
  }

  // 3.go through recover steps to fill history
  if (pTask->fillHistory) {
    streamSetParamForRecover(pTask);
    if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
      streamSourceRecoverPrepareStep1(pTask, version);

      SStreamRecoverStep1Req req;
      streamBuildSourceRecover1Req(pTask, &req);

      void*   serialziedReq = (void*)&req;
      int32_t len = sizeof(SStreamRecoverStep1Req);

      SRpcMsg rpcMsg = {
          .contLen = len,
          .pCont = serialziedReq,
          .msgType = TDMT_VND_STREAM_RECOVER_STEP1,
      };

      tmsgPutToQueue(&pTq->pVnode->msgCb, STREAM_QUEUE, &rpcMsg);

    } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
      streamAggRecoverPrepare(pTask);
    } else if (pTask->taskLevel == TASK_LEVEL__SINK) {
      // do nothing
    }
  }

  return 0;
}

int32_t tqProcessTaskRecover1Req(STQ* pTq, char* msg, int32_t msgLen) {
  int32_t                 code;
  SStreamRecoverStep1Req* pReq = (SStreamRecoverStep1Req*)msg;
  SStreamTask*            pTask = streamMetaGetTask(pTq->pStreamMeta, pReq->taskId);
  if (pTask == NULL) {
    return -1;
  }

  // check param
  int64_t fillVer1 = pTask->startVer;
  if (fillVer1 <= 0) {
    ASSERT(0);
    return -1;
  }

  // do recovery step 1
  streamSourceRecoverScanStep1(pTask);

  // build msg to launch next step
  SStreamRecoverStep2Req req;
  code = streamBuildSourceRecover2Req(pTask, &req);
  if (code < 0) {
    return -1;
  }

  // serialize msg
  int32_t len = sizeof(SStreamRecoverStep2Req);
  void*   serializedReq = (void*)&req;

  // dispatch msg
  SRpcMsg rpcMsg = {
      .code = 0,
      .contLen = len,
      .msgType = TDMT_VND_STREAM_RECOVER_STEP2,
      .pCont = (void*)serializedReq,
  };

  tmsgPutToQueue(&pTq->pVnode->msgCb, WRITE_QUEUE, &rpcMsg);

  return 0;
}

int32_t tqProcessTaskRecover2Req(STQ* pTq, int64_t version, char* msg, int32_t msgLen) {
  int32_t                 code;
  SStreamRecoverStep2Req* pReq = (SStreamRecoverStep2Req*)msg;
  SStreamTask*            pTask = streamMetaGetTask(pTq->pStreamMeta, pReq->taskId);
  if (pTask == NULL) {
    return -1;
  }

  // do recovery step 2
  code = streamSourceRecoverScanStep2(pTask, version);
  if (code < 0) {
    return -1;
  }

  // restore param
  code = streamRestoreParam(pTask);
  if (code < 0) {
    return -1;
  }

  // set status normal
  code = streamSetStatusNormal(pTask);
  if (code < 0) {
    return -1;
  }

  // dispatch recover finish req to all related downstream task
  code = streamDispatchRecoverFinishReq(pTask);
  if (code < 0) {
    return -1;
  }

  return 0;
}

int32_t tqProcessTaskRecoverFinishReq(STQ* pTq, char* msg, int32_t msgLen) {
  int32_t code;

  // deserialize
1092 1093 1094 1095 1096 1097 1098 1099
  int32_t                 len;
  SStreamRecoverFinishReq req;

  SDecoder decoder;
  tDecoderInit(&decoder, msg, sizeof(SStreamRecoverFinishReq));
  tDecodeSStreamRecoverFinishReq(&decoder, &req);
  tDecoderClear(&decoder);

1100
  // find task
1101 1102 1103 1104
  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, req.taskId);
  if (pTask == NULL) {
    return -1;
  }
1105
  // do process request
1106 1107 1108 1109
  if (streamProcessRecoverFinishReq(pTask, req.childId) < 0) {
    return -1;
  }

1110
  return 0;
L
Liu Jicong 已提交
1111
}
L
Liu Jicong 已提交
1112

L
Liu Jicong 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
int32_t tqProcessDelReq(STQ* pTq, void* pReq, int32_t len, int64_t ver) {
  bool        failed = false;
  SDecoder*   pCoder = &(SDecoder){0};
  SDeleteRes* pRes = &(SDeleteRes){0};

  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
  if (pRes->uidList == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    failed = true;
  }

  tDecoderInit(pCoder, pReq, len);
  tDecodeDeleteRes(pCoder, pRes);
  tDecoderClear(pCoder);

  int32_t sz = taosArrayGetSize(pRes->uidList);
L
Liu Jicong 已提交
1129
  if (sz == 0 || pRes->affectedRows == 0) {
L
Liu Jicong 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    taosArrayDestroy(pRes->uidList);
    return 0;
  }
  SSDataBlock* pDelBlock = createSpecialDataBlock(STREAM_DELETE_DATA);
  blockDataEnsureCapacity(pDelBlock, sz);
  pDelBlock->info.rows = sz;
  pDelBlock->info.version = ver;

  for (int32_t i = 0; i < sz; i++) {
    // start key column
    SColumnInfoData* pStartCol = taosArrayGet(pDelBlock->pDataBlock, START_TS_COLUMN_INDEX);
    colDataAppend(pStartCol, i, (const char*)&pRes->skey, false);  // end key column
    SColumnInfoData* pEndCol = taosArrayGet(pDelBlock->pDataBlock, END_TS_COLUMN_INDEX);
    colDataAppend(pEndCol, i, (const char*)&pRes->ekey, false);
    // uid column
    SColumnInfoData* pUidCol = taosArrayGet(pDelBlock->pDataBlock, UID_COLUMN_INDEX);
    int64_t*         pUid = taosArrayGet(pRes->uidList, i);
    colDataAppend(pUidCol, i, (const char*)pUid, false);

    colDataAppendNULL(taosArrayGet(pDelBlock->pDataBlock, GROUPID_COLUMN_INDEX), i);
    colDataAppendNULL(taosArrayGet(pDelBlock->pDataBlock, CALCULATE_START_TS_COLUMN_INDEX), i);
    colDataAppendNULL(taosArrayGet(pDelBlock->pDataBlock, CALCULATE_END_TS_COLUMN_INDEX), i);
  }

L
Liu Jicong 已提交
1154 1155
  taosArrayDestroy(pRes->uidList);

L
Liu Jicong 已提交
1156 1157 1158
  int32_t* pRef = taosMemoryMalloc(sizeof(int32_t));
  *pRef = 1;

L
Liu Jicong 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167
  void* pIter = NULL;
  while (1) {
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
    if (pIter == NULL) break;
    SStreamTask* pTask = *(SStreamTask**)pIter;
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) continue;

    qDebug("delete req enqueue stream task: %d, ver: %" PRId64, pTask->taskId, ver);

L
Liu Jicong 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176
    if (!failed) {
      SStreamRefDataBlock* pRefBlock = taosAllocateQitem(sizeof(SStreamRefDataBlock), DEF_QITEM);
      pRefBlock->type = STREAM_INPUT__REF_DATA_BLOCK;
      pRefBlock->pBlock = pDelBlock;
      pRefBlock->dataRef = pRef;
      atomic_add_fetch_32(pRefBlock->dataRef, 1);

      if (streamTaskInput(pTask, (SStreamQueueItem*)pRefBlock) < 0) {
        qError("stream task input del failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
1177 1178

        taosFreeQitem(pRefBlock);
L
Liu Jicong 已提交
1179 1180
        continue;
      }
L
Liu Jicong 已提交
1181

L
Liu Jicong 已提交
1182 1183 1184 1185
      if (streamSchedExec(pTask) < 0) {
        qError("stream task launch failed, task id %d", pTask->taskId);
        continue;
      }
L
Liu Jicong 已提交
1186

L
Liu Jicong 已提交
1187 1188 1189 1190
    } else {
      streamTaskInputFail(pTask);
    }
  }
L
Liu Jicong 已提交
1191

L
Liu Jicong 已提交
1192 1193 1194 1195 1196 1197 1198 1199
  int32_t ref = atomic_sub_fetch_32(pRef, 1);
  ASSERT(ref >= 0);
  if (ref == 0) {
    taosMemoryFree(pDelBlock);
    taosMemoryFree(pRef);
  }

#if 0
L
Liu Jicong 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    SStreamDataBlock* pStreamBlock = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM);
    pStreamBlock->type = STREAM_INPUT__DATA_BLOCK;
    pStreamBlock->blocks = taosArrayInit(0, sizeof(SSDataBlock));
    SSDataBlock block = {0};
    assignOneDataBlock(&block, pDelBlock);
    block.info.type = STREAM_DELETE_DATA;
    taosArrayPush(pStreamBlock->blocks, &block);

    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pStreamBlock) < 0) {
        qError("stream task input del failed, task id %d", pTask->taskId);
        continue;
      }

      if (streamSchedExec(pTask) < 0) {
        qError("stream task launch failed, task id %d", pTask->taskId);
        continue;
      }
    } else {
      streamTaskInputFail(pTask);
    }
  }
L
Liu Jicong 已提交
1222
  blockDataDestroy(pDelBlock);
L
Liu Jicong 已提交
1223
#endif
L
Liu Jicong 已提交
1224 1225 1226 1227 1228

  return 0;
}

int32_t tqProcessSubmitReq(STQ* pTq, SSubmitReq* pReq, int64_t ver) {
L
Liu Jicong 已提交
1229 1230 1231
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
1232

L
Liu Jicong 已提交
1233
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
1234
  if (pSubmit == NULL) {
L
Liu Jicong 已提交
1235 1236
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    qError("failed to create data submit for stream since out of memory");
L
Liu Jicong 已提交
1237 1238 1239 1240
    failed = true;
  }

  while (1) {
L
Liu Jicong 已提交
1241
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
L
Liu Jicong 已提交
1242
    if (pIter == NULL) break;
1243
    SStreamTask* pTask = *(SStreamTask**)pIter;
1244
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) continue;
1245
    if (pTask->taskStatus == TASK_STATUS__RECOVER_PREPARE || pTask->taskStatus == TASK_STATUS__RECOVER1) continue;
L
Liu Jicong 已提交
1246

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

L
Liu Jicong 已提交
1249 1250
    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pSubmit) < 0) {
L
Liu Jicong 已提交
1251
        qError("stream task input failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
1252 1253 1254
        continue;
      }

L
Liu Jicong 已提交
1255
      if (streamSchedExec(pTask) < 0) {
L
Liu Jicong 已提交
1256
        qError("stream task launch failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
1257 1258
        continue;
      }
L
Liu Jicong 已提交
1259
    } else {
L
Liu Jicong 已提交
1260
      streamTaskInputFail(pTask);
L
Liu Jicong 已提交
1261 1262 1263
    }
  }

L
Liu Jicong 已提交
1264
  if (pSubmit) {
L
Liu Jicong 已提交
1265
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
1266
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
1267
  }
L
Liu Jicong 已提交
1268 1269

  return failed ? -1 : 0;
L
Liu Jicong 已提交
1270 1271
}

L
Liu Jicong 已提交
1272
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
1273
  //
L
Liu Jicong 已提交
1274 1275
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
L
Liu Jicong 已提交
1276 1277 1278
  SStreamTask*       pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
    streamProcessRunReq(pTask);
L
Liu Jicong 已提交
1279
    return 0;
1280 1281
  } else {
    return -1;
L
Liu Jicong 已提交
1282
  }
L
Liu Jicong 已提交
1283 1284
}

L
Liu Jicong 已提交
1285
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
L
Liu Jicong 已提交
1286
  ASSERT(0);
1287 1288 1289 1290 1291
  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 已提交
1292
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
1293
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
1294 1295 1296 1297
  int32_t taskId = req.taskId;

  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
1298 1299 1300 1301
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
1302
    streamProcessDispatchReq(pTask, &req, &rsp, exec);
L
Liu Jicong 已提交
1303
    return 0;
1304 1305
  } else {
    return -1;
L
Liu Jicong 已提交
1306
  }
L
Liu Jicong 已提交
1307 1308
}

L
Liu Jicong 已提交
1309
#if 0
L
Liu Jicong 已提交
1310 1311 1312
int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
L
Liu Jicong 已提交
1313 1314 1315
  SStreamTask*           pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
    streamProcessRecoverReq(pTask, pReq, pMsg);
L
Liu Jicong 已提交
1316
    return 0;
1317 1318
  } else {
    return -1;
L
Liu Jicong 已提交
1319
  }
L
Liu Jicong 已提交
1320 1321
}

L
Liu Jicong 已提交
1322 1323 1324 1325 1326
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 已提交
1327
  if (pTask) {
L
Liu Jicong 已提交
1328
    streamProcessRecoverRsp(pTask, pRsp);
L
Liu Jicong 已提交
1329
    return 0;
1330 1331
  } else {
    return -1;
L
Liu Jicong 已提交
1332
  }
L
Liu Jicong 已提交
1333
}
L
Liu Jicong 已提交
1334
#endif
L
Liu Jicong 已提交
1335

L
Liu Jicong 已提交
1336 1337 1338 1339
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 已提交
1340
  if (pTask) {
L
Liu Jicong 已提交
1341
    streamProcessDispatchRsp(pTask, pRsp);
L
Liu Jicong 已提交
1342
    return 0;
1343 1344
  } else {
    return -1;
L
Liu Jicong 已提交
1345
  }
L
Liu Jicong 已提交
1346
}
L
Liu Jicong 已提交
1347

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

L
Liu Jicong 已提交
1351
  return streamMetaRemoveTask(pTq->pStreamMeta, pReq->taskId);
L
Liu Jicong 已提交
1352
}
L
Liu Jicong 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361

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 已提交
1362
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1363 1364 1365
  int32_t      taskId = req.dstTaskId;
  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
L
Liu Jicong 已提交
1366 1367 1368 1369
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
1370
    streamProcessRetrieveReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
1371
    tDeleteStreamRetrieveReq(&req);
L
Liu Jicong 已提交
1372
    return 0;
L
Liu Jicong 已提交
1373 1374
  } else {
    return -1;
L
Liu Jicong 已提交
1375 1376 1377 1378 1379 1380 1381
  }
}

int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}
L
Liu Jicong 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

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 已提交
1395
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1396 1397
    goto FAIL;
  }
L
Liu Jicong 已提交
1398
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1399

L
Liu Jicong 已提交
1400
  int32_t taskId = req.taskId;
L
Liu Jicong 已提交
1401

L
Liu Jicong 已提交
1402 1403
  SStreamTask* pTask = streamMetaGetTask(pTq->pStreamMeta, taskId);
  if (pTask) {
L
Liu Jicong 已提交
1404 1405 1406 1407
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
1408
    streamProcessDispatchReq(pTask, &req, &rsp, false);
L
Liu Jicong 已提交
1409 1410
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
L
Liu Jicong 已提交
1411 1412
    return;
  }
L
Liu Jicong 已提交
1413

L
Liu Jicong 已提交
1414 1415 1416 1417 1418 1419 1420
FAIL:
  if (pMsg->info.handle == NULL) return;
  SRpcMsg rsp = {
      .code = code,
      .info = pMsg->info,
  };
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
1421 1422
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
L
Liu Jicong 已提交
1423
}