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

18
#define ALL_STREAM_TASKS_ID       (-1)
19

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

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

L
Liu Jicong 已提交
39 40
  return 0;
}
L
Liu Jicong 已提交
41

42
void tqCleanUp() {
L
Liu Jicong 已提交
43 44 45 46 47 48 49 50
  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 已提交
51
    streamCleanUp();
L
Liu Jicong 已提交
52 53
    atomic_store_8(&tqMgmt.inited, 0);
  }
54
}
L
Liu Jicong 已提交
55

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

L
Liu Jicong 已提交
71
static void tqPushEntryFree(void* data) {
L
Liu Jicong 已提交
72
  STqPushEntry* p = *(void**)data;
H
Haojun Liao 已提交
73 74 75 76 77 78 79
  if (p->pDataRsp->head.mqMsgType == TMQ_MSG_TYPE__POLL_RSP) {
    tDeleteSMqDataRsp(p->pDataRsp);
  } else if (p->pDataRsp->head.mqMsgType == TMQ_MSG_TYPE__TAOSX_RSP) {
    tDeleteSTaosxRsp((STaosxRsp*)p->pDataRsp);
  }

  taosMemoryFree(p->pDataRsp);
L
Liu Jicong 已提交
80 81 82
  taosMemoryFree(p);
}

83 84 85 86 87
static 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;
}

L
Liu Jicong 已提交
88
STQ* tqOpen(const char* path, SVnode* pVnode) {
89
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
L
Liu Jicong 已提交
90
  if (pTq == NULL) {
S
Shengliang Guan 已提交
91
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
92 93
    return NULL;
  }
94

95
  pTq->path = taosStrdup(path);
L
Liu Jicong 已提交
96
  pTq->pVnode = pVnode;
L
Liu Jicong 已提交
97
  pTq->walLogLastVer = pVnode->pWal->vers.lastVer;
98

99
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
100
  taosHashSetFreeFp(pTq->pHandle, destroyTqHandle);
101

102
  taosInitRWLatch(&pTq->lock);
L
Liu Jicong 已提交
103
  pTq->pPushMgr = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
L
Liu Jicong 已提交
104
  taosHashSetFreeFp(pTq->pPushMgr, tqPushEntryFree);
L
Liu Jicong 已提交
105

106
  pTq->pCheckInfo = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
107
  taosHashSetFreeFp(pTq->pCheckInfo, (FDelete)tDeleteSTqCheckInfo);
L
Liu Jicong 已提交
108

L
Liu Jicong 已提交
109
  if (tqMetaOpen(pTq) < 0) {
L
Liu Jicong 已提交
110
    return NULL;
111 112
  }

L
Liu Jicong 已提交
113 114
  pTq->pOffsetStore = tqOffsetOpen(pTq);
  if (pTq->pOffsetStore == NULL) {
L
Liu Jicong 已提交
115
    return NULL;
116 117
  }

118
  pTq->pStreamMeta = streamMetaOpen(path, pTq, (FTaskExpand*)tqExpandTask, pTq->pVnode->config.vgId);
L
Liu Jicong 已提交
119
  if (pTq->pStreamMeta == NULL) {
L
Liu Jicong 已提交
120
    return NULL;
L
Liu Jicong 已提交
121 122
  }

L
Liu Jicong 已提交
123
  if (streamLoadTasks(pTq->pStreamMeta, walGetCommittedVer(pVnode->pWal)) < 0) {
L
Liu Jicong 已提交
124
    return NULL;
L
Liu Jicong 已提交
125 126
  }

L
Liu Jicong 已提交
127 128
  return pTq;
}
L
Liu Jicong 已提交
129

L
Liu Jicong 已提交
130
void tqClose(STQ* pTq) {
131 132
  if (pTq == NULL) {
    return;
H
Hongze Cheng 已提交
133
  }
134 135 136 137 138 139 140 141 142

  tqOffsetClose(pTq->pOffsetStore);
  taosHashCleanup(pTq->pHandle);
  taosHashCleanup(pTq->pPushMgr);
  taosHashCleanup(pTq->pCheckInfo);
  taosMemoryFree(pTq->path);
  tqMetaClose(pTq);
  streamMetaClose(pTq->pStreamMeta);
  taosMemoryFree(pTq);
L
Liu Jicong 已提交
143
}
L
Liu Jicong 已提交
144

H
Haojun Liao 已提交
145 146
static int32_t doSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, const SMqDataRsp* pRsp, int32_t epoch,
                             int64_t consumerId, int32_t type) {
L
Liu Jicong 已提交
147 148
  int32_t len = 0;
  int32_t code = 0;
H
Haojun Liao 已提交
149 150 151 152 153 154

  if (type == TMQ_MSG_TYPE__POLL_RSP) {
    tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);
  } else if (type == TMQ_MSG_TYPE__TAOSX_RSP) {
    tEncodeSize(tEncodeSTaosxRsp, (STaosxRsp*)pRsp, len, code);
  }
L
Liu Jicong 已提交
155 156 157 158 159 160 161 162 163 164 165

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

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

H
Haojun Liao 已提交
166 167 168
  ((SMqRspHead*)buf)->mqMsgType = type;
  ((SMqRspHead*)buf)->epoch = epoch;
  ((SMqRspHead*)buf)->consumerId = consumerId;
L
Liu Jicong 已提交
169 170 171 172 173

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

  SEncoder encoder = {0};
  tEncoderInit(&encoder, abuf, len);
H
Haojun Liao 已提交
174 175 176 177 178 179 180

  if (type == TMQ_MSG_TYPE__POLL_RSP) {
    tEncodeSMqDataRsp(&encoder, pRsp);
  } else if (type == TMQ_MSG_TYPE__TAOSX_RSP) {
    tEncodeSTaosxRsp(&encoder, (STaosxRsp*) pRsp);
  }

L
Liu Jicong 已提交
181 182 183
  tEncoderClear(&encoder);

  SRpcMsg rsp = {
H
Haojun Liao 已提交
184
      .info = *pRpcHandleInfo,
L
Liu Jicong 已提交
185 186 187 188 189 190 191 192 193
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };

  tmsgSendRsp(&rsp);
  return 0;
}

H
Haojun Liao 已提交
194 195 196 197
int32_t tqPushDataRsp(STQ* pTq, STqPushEntry* pPushEntry) {
  SMqDataRsp* pRsp = pPushEntry->pDataRsp;
  SMqRspHead* pHeader = &pPushEntry->pDataRsp->head;
  doSendDataRsp(&pPushEntry->info, pRsp, pHeader->epoch, pHeader->consumerId, pHeader->mqMsgType);
L
Liu Jicong 已提交
198

wmmhello's avatar
wmmhello 已提交
199 200
  char buf1[80] = {0};
  char buf2[80] = {0};
H
Haojun Liao 已提交
201 202 203 204
  tFormatOffset(buf1, tListLen(buf1), &pRsp->reqOffset);
  tFormatOffset(buf2, tListLen(buf2), &pRsp->rspOffset);
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) push rsp, block num: %d, req:%s, rsp:%s",
          TD_VID(pTq->pVnode), pRsp->head.consumerId, pRsp->head.epoch, pRsp->blockNum, buf1, buf2);
L
Liu Jicong 已提交
205 206 207
  return 0;
}

H
Haojun Liao 已提交
208 209
int32_t tqSendDataRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqDataRsp* pRsp, int32_t type) {
  doSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type);
210 211 212 213 214 215

  char buf1[80] = {0};
  char buf2[80] = {0};
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);

H
Haojun Liao 已提交
216 217
  tqDebug("vgId:%d consumer:0x%" PRIx64 " (epoch %d) send rsp, block num:%d, req:%s, rsp:%s, reqId:0x%"PRIx64,
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
H
Haojun Liao 已提交
218

219 220 221
  return 0;
}

222
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
223
  STqOffset offset = {0};
H
Haojun Liao 已提交
224
  int32_t vgId = TD_VID(pTq->pVnode);
225

X
Xiaoyu Wang 已提交
226 227
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
228 229 230
  if (tDecodeSTqOffset(&decoder, &offset) < 0) {
    return -1;
  }
231

232 233
  tDecoderClear(&decoder);

wmmhello's avatar
wmmhello 已提交
234
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA || offset.val.type == TMQ_OFFSET__SNAPSHOT_META) {
L
Liu Jicong 已提交
235
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
H
Haojun Liao 已提交
236
            offset.subKey, vgId, offset.val.uid, offset.val.ts);
L
Liu Jicong 已提交
237
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
S
Shengliang Guan 已提交
238
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, offset.subKey,
H
Haojun Liao 已提交
239
            vgId, offset.val.version);
240
    if (offset.val.version + 1 == sversion) {
241 242
      offset.val.version += 1;
    }
243
  } else {
244 245
    tqError("invalid commit offset type:%d", offset.val.type);
    return -1;
246
  }
247 248 249 250

  STqOffset* pSavedOffset = tqOffsetRead(pTq->pOffsetStore, offset.subKey);
  if (pSavedOffset != NULL && tqOffsetLessOrEqual(&offset, pSavedOffset)) {
    return 0;  // no need to update the offset value
251 252
  }

253
  // save the new offset value
254 255
  if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
    return -1;
256
  }
257 258

  if (offset.val.type == TMQ_OFFSET__LOG) {
259
    STqHandle* pHandle = taosHashGet(pTq->pHandle, offset.subKey, strlen(offset.subKey));
260 261
    if (pHandle && (walRefVer(pHandle->pRef, offset.val.version) < 0)) {
      return -1;
262 263 264
    }
  }

265 266 267
  return 0;
}

L
Liu Jicong 已提交
268
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
L
Liu Jicong 已提交
269
  void* pIter = NULL;
270

L
Liu Jicong 已提交
271
  while (1) {
272
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
273 274 275 276
    if (pIter == NULL) {
      break;
    }

277
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
278

L
Liu Jicong 已提交
279 280
    if (pCheck->ntbUid == tbUid) {
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
L
Liu Jicong 已提交
281
      for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
282 283
        int16_t forbidColId = *(int16_t*)taosArrayGet(pCheck->colIdList, i);
        if (forbidColId == colId) {
284
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
L
Liu Jicong 已提交
285 286 287 288 289
          return -1;
        }
      }
    }
  }
290

L
Liu Jicong 已提交
291 292 293
  return 0;
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
  SMqPollReq   req = {0};
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  int64_t      consumerId = req.consumerId;
  int32_t      reqEpoch = req.epoch;
  STqOffsetVal reqOffset = req.reqOffset;
  int32_t      vgId = TD_VID(pTq->pVnode);

  // 1. find handle
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
  if (pHandle == NULL) {
    tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", consumerId, vgId, req.subKey);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

315
  // 2. check re-balance status
316
  taosRLockLatch(&pTq->lock);
317 318 319 320
  if (pHandle->consumerId != consumerId) {
    tqDebug("ERROR tmq poll: consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
            consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
    terrno = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
321
    taosRUnLockLatch(&pTq->lock);
322 323
    return -1;
  }
324
  taosRUnLockLatch(&pTq->lock);
325

326
  // 3. update the epoch value
327
  taosWLockLatch(&pTq->lock);
H
Haojun Liao 已提交
328 329
  int32_t savedEpoch = pHandle->epoch;
  if (savedEpoch < reqEpoch) {
330 331
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, savedEpoch, reqEpoch);
    pHandle->epoch = reqEpoch;
H
Haojun Liao 已提交
332
  }
333
  taosWUnLockLatch(&pTq->lock);
334 335 336

  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
H
Haojun Liao 已提交
337 338
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, reqId:0x%" PRIx64,
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
339

340
  return tqExtractDataForMq(pTq, pHandle, &req, pMsg);
341 342
}

343
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
344
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
L
Liu Jicong 已提交
345

L
Liu Jicong 已提交
346
  tqDebug("vgId:%d, tq process delete sub req %s", pTq->pVnode->config.vgId, pReq->subKey);
L
Liu Jicong 已提交
347

348
  taosWLockLatch(&pTq->lock);
L
Liu Jicong 已提交
349 350 351 352
  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);
  }
353
  taosWUnLockLatch(&pTq->lock);
L
Liu Jicong 已提交
354

L
Liu Jicong 已提交
355 356
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
  if (pHandle) {
X
Xiaoyu Wang 已提交
357
    // walCloseRef(pHandle->pWalReader->pWal, pHandle->pRef->refId);
L
Liu Jicong 已提交
358 359 360 361 362 363 364
    if (pHandle->pRef) {
      walCloseRef(pTq->pVnode->pWal, pHandle->pRef->refId);
    }
    code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
    if (code != 0) {
      tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
    }
L
Liu Jicong 已提交
365
  }
366

L
Liu Jicong 已提交
367 368
  code = tqOffsetDelete(pTq->pOffsetStore, pReq->subKey);
  if (code != 0) {
369
    tqError("cannot process tq delete req %s, since no such offset in cache", pReq->subKey);
L
Liu Jicong 已提交
370
  }
L
Liu Jicong 已提交
371

L
Liu Jicong 已提交
372
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
L
Liu Jicong 已提交
373
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
374
  }
L
Liu Jicong 已提交
375
  return 0;
L
Liu Jicong 已提交
376 377
}

378
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
379 380
  STqCheckInfo info = {0};
  SDecoder     decoder;
X
Xiaoyu Wang 已提交
381
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
382
  if (tDecodeSTqCheckInfo(&decoder, &info) < 0) {
L
Liu Jicong 已提交
383 384 385 386
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  tDecoderClear(&decoder);
387 388 389 390 391
  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 已提交
392 393 394 395 396 397
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

398
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
399 400 401 402 403 404 405 406 407 408 409
  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;
}

410
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
411
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
412
  tDecodeSMqRebVgReq(msg, &req);
L
Liu Jicong 已提交
413

414 415 416
  SVnode* pVnode = pTq->pVnode;
  int32_t vgId = TD_VID(pVnode);

417
  tqDebug("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pVnode->config.vgId, req.subKey,
418
          req.oldConsumerId, req.newConsumerId);
L
Liu Jicong 已提交
419

420
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
L
Liu Jicong 已提交
421
  if (pHandle == NULL) {
L
Liu Jicong 已提交
422
    if (req.oldConsumerId != -1) {
423
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
424
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
L
Liu Jicong 已提交
425
    }
426

L
Liu Jicong 已提交
427
    if (req.newConsumerId == -1) {
428
      tqError("vgId:%d, tq invalid re-balance request, new consumerId %" PRId64 "", req.vgId, req.newConsumerId);
L
Liu Jicong 已提交
429
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
430 431
      return 0;
    }
432

L
Liu Jicong 已提交
433 434
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
435

H
Haojun Liao 已提交
436
    uint64_t oldConsumerId = pHandle->consumerId;
L
Liu Jicong 已提交
437 438 439
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
440

L
Liu Jicong 已提交
441
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
442
    pHandle->fetchMeta = req.withMeta;
wmmhello's avatar
wmmhello 已提交
443

444
    // TODO version should be assigned and refed during preprocess
445
    SWalRef* pRef = walRefCommittedVer(pVnode->pWal);
446
    if (pRef == NULL) {
H
Haojun Liao 已提交
447
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
448
      return -1;
449
    }
H
Haojun Liao 已提交
450

451 452
    int64_t ver = pRef->refVer;
    pHandle->pRef = pRef;
L
Liu Jicong 已提交
453

454
    SReadHandle handle = {
455
        .meta = pVnode->pMeta, .vnode = pVnode, .initTableReader = true, .initTqReader = true, .version = ver};
wmmhello's avatar
wmmhello 已提交
456
    pHandle->snapshotVer = ver;
457

L
Liu Jicong 已提交
458
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
459
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
460
      req.qmsg = NULL;
461 462

      pHandle->execHandle.task =
463
          qCreateQueueExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle, vgId, &pHandle->execHandle.numOfCols, req.newConsumerId);
L
Liu Jicong 已提交
464
      void* scanner = NULL;
465
      qExtractStreamScanner(pHandle->execHandle.task, &scanner);
466
      pHandle->execHandle.pTqReader = qExtractReaderFromStreamScanner(scanner);
L
Liu Jicong 已提交
467
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
468
      pHandle->pWalReader = walOpenReader(pVnode->pWal, NULL);
469
      pHandle->execHandle.pTqReader = tqOpenReader(pVnode);
470

L
Liu Jicong 已提交
471
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
472
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
473 474
      buildSnapContext(handle.meta, handle.version, 0, pHandle->execHandle.subType, pHandle->fetchMeta,
                       (SSnapContext**)(&handle.sContext));
475

476
      pHandle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &handle, vgId, NULL, req.newConsumerId);
L
Liu Jicong 已提交
477
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
478
      pHandle->pWalReader = walOpenReader(pVnode->pWal, NULL);
wmmhello's avatar
wmmhello 已提交
479 480
      pHandle->execHandle.execTb.suid = req.suid;

L
Liu Jicong 已提交
481
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
482 483
      vnodeGetCtbIdList(pVnode, req.suid, tbUidList);
      tqDebug("vgId:%d, tq try to get all ctb, suid:%" PRId64, pVnode->config.vgId, req.suid);
L
Liu Jicong 已提交
484 485
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
486
        tqDebug("vgId:%d, idx %d, uid:%" PRId64, vgId, i, tbUid);
L
Liu Jicong 已提交
487
      }
488 489
      pHandle->execHandle.pTqReader = tqOpenReader(pVnode);
      tqReaderSetTbUidList(pHandle->execHandle.pTqReader, tbUidList);
L
Liu Jicong 已提交
490
      taosArrayDestroy(tbUidList);
wmmhello's avatar
wmmhello 已提交
491

L
Liu Jicong 已提交
492 493
      buildSnapContext(handle.meta, handle.version, req.suid, pHandle->execHandle.subType, pHandle->fetchMeta,
                       (SSnapContext**)(&handle.sContext));
494
      pHandle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &handle, vgId, NULL, req.newConsumerId);
L
Liu Jicong 已提交
495
    }
H
Haojun Liao 已提交
496

497
    taosHashPut(pTq->pHandle, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
498 499
    tqDebug("try to persist handle %s consumer:0x%" PRIx64 " , old consumer:0x%" PRIx64, req.subKey,
            pHandle->consumerId, oldConsumerId);
L
Liu Jicong 已提交
500
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
H
Haojun Liao 已提交
501
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
502
      return -1;
L
Liu Jicong 已提交
503
    }
L
Liu Jicong 已提交
504
  } else {
505 506 507 508
    if (pHandle->consumerId == req.newConsumerId) {  // do nothing
      tqInfo("vgId:%d consumer:0x%" PRIx64 " remains, no switch occurs", req.vgId, req.newConsumerId);
      atomic_store_32(&pHandle->epoch, -1);
      atomic_add_fetch_32(&pHandle->epoch, 1);
H
Haojun Liao 已提交
509
      taosMemoryFree(req.qmsg);
510
      return tqMetaSaveHandle(pTq, req.subKey, pHandle);
511 512 513
    } else {
      tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
             req.newConsumerId);
514

515 516 517
      // kill executing task
      qTaskInfo_t pTaskInfo = pHandle->execHandle.task;
      if (pTaskInfo != NULL) {
518
        qKillTask(pTaskInfo, TSDB_CODE_SUCCESS);
519 520
      }

521 522
      taosWLockLatch(&pTq->lock);
      atomic_store_32(&pHandle->epoch, -1);
523

524
      // remove if it has been register in the push manager, and return one empty block to consumer
525
      tqUnregisterPushHandle(pTq, req.subKey, (int32_t)strlen(req.subKey), pHandle->consumerId, true);
526

527 528
      atomic_store_64(&pHandle->consumerId, req.newConsumerId);
      atomic_add_fetch_32(&pHandle->epoch, 1);
529

530
      if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
531 532 533
        qStreamCloseTsdbReader(pTaskInfo);
      }

534 535 536 537 538
      taosWUnLockLatch(&pTq->lock);
      if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
        taosMemoryFree(req.qmsg);
        return -1;
      }
L
Liu Jicong 已提交
539
    }
L
Liu Jicong 已提交
540
  }
L
Liu Jicong 已提交
541

H
Haojun Liao 已提交
542
  taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
543
  return 0;
L
Liu Jicong 已提交
544
}
545

546
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask, int64_t ver) {
547 548 549
  // todo extract method
  char buf[128] = {0};
  sprintf(buf, "0x%"PRIx64"-%d", pTask->id.streamId, pTask->id.taskId);
L
Liu Jicong 已提交
550

551
  int32_t vgId = TD_VID(pTq->pVnode);
552
  pTask->id.idStr = taosStrdup(buf);
L
Liu Jicong 已提交
553
  pTask->refCnt = 1;
L
Liu Jicong 已提交
554
  pTask->schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
555 556
  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
L
Liu Jicong 已提交
557 558

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
L
Liu Jicong 已提交
559
    return -1;
L
Liu Jicong 已提交
560 561
  }

L
Liu Jicong 已提交
562 563
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;
564
  pTask->pMsgCb = &pTq->pVnode->msgCb;
565
  pTask->pMeta = pTq->pStreamMeta;
566

567
  // expand executor
568 569
  if (pTask->fillHistory) {
    pTask->taskStatus = TASK_STATUS__WAIT_DOWNSTREAM;
570
  } else {
571
    pTask->taskStatus = TASK_STATUS__RESTORE;
572 573
  }

574
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
575
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
576 577 578 579
    if (pTask->pState == NULL) {
      return -1;
    }

580
    SReadHandle handle = {
581
        .meta = pTq->pVnode->pMeta, .vnode = pTq->pVnode, .initTqReader = 1, .pStateBackend = pTask->pState};
582

583 584
    pTask->exec.pExecutor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle, vgId);
    if (pTask->exec.pExecutor == NULL) {
L
Liu Jicong 已提交
585 586
      return -1;
    }
587

588
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
589
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
590 591 592
    if (pTask->pState == NULL) {
      return -1;
    }
593

594 595 596 597 598
    int32_t numOfVgroups = (int32_t)taosArrayGetSize(pTask->childEpInfo);
    SReadHandle mgHandle = { .vnode = NULL, .numOfVgroups = numOfVgroups, .pStateBackend = pTask->pState};

    pTask->exec.pExecutor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &mgHandle, vgId);
    if (pTask->exec.pExecutor == NULL) {
L
Liu Jicong 已提交
599 600
      return -1;
    }
L
Liu Jicong 已提交
601
  }
L
Liu Jicong 已提交
602 603

  // sink
L
Liu Jicong 已提交
604
  /*pTask->ahandle = pTq->pVnode;*/
605
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
606
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
607
    pTask->smaSink.smaSink = smaHandleRes;
608
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
609
    pTask->tbSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
610
    pTask->tbSink.tbSinkFunc = tqSinkToTablePipeline2;
L
Liu Jicong 已提交
611

H
Haojun Liao 已提交
612
    int32_t ver1 = 1;
5
54liuyao 已提交
613 614 615
    SMetaInfo info = {0};
    int32_t code = metaGetInfo(pTq->pVnode->pMeta, pTask->tbSink.stbUid, &info, NULL);
    if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
616
      ver1 = info.skmVer;
5
54liuyao 已提交
617
    }
L
Liu Jicong 已提交
618

619 620
    SSchemaWrapper* pschemaWrapper = pTask->tbSink.pSchemaWrapper;
    pTask->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
wmmhello's avatar
wmmhello 已提交
621
    if(pTask->tbSink.pTSchema == NULL) {
wmmhello's avatar
wmmhello 已提交
622
      return -1;
wmmhello's avatar
wmmhello 已提交
623
    }
L
Liu Jicong 已提交
624
  }
625

626 627 628 629 630 631 632 633 634 635 636
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
    pTask->exec.pTqReader = tqOpenReader(pTq->pVnode);
    if (pTask->exec.pTqReader == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    SArray* pList = qGetQueriedTableListInfo(pTask->exec.pExecutor);
    tqReaderAddTbUidList(pTask->exec.pTqReader, pList);
  }

637
  streamSetupTrigger(pTask);
638
  tqInfo("vgId:%d expand stream task, s-task:%s, ver:%" PRId64 " child id:%d, level:%d", vgId, pTask->id.idStr,
639
         pTask->chkInfo.version, pTask->selfChildId, pTask->taskLevel);
L
Liu Jicong 已提交
640
  return 0;
L
Liu Jicong 已提交
641
}
L
Liu Jicong 已提交
642

643 644 645 646 647 648
int32_t tqProcessStreamTaskCheckReq(STQ* pTq, SRpcMsg* pMsg) {
  char*               msgStr = pMsg->pCont;
  char*               msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t             msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamTaskCheckReq req;
  SDecoder            decoder;
X
Xiaoyu Wang 已提交
649
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
650 651 652 653 654 655 656 657 658 659 660 661
  tDecodeSStreamTaskCheckReq(&decoder, &req);
  tDecoderClear(&decoder);
  int32_t             taskId = req.downstreamTaskId;
  SStreamTaskCheckRsp rsp = {
      .reqId = req.reqId,
      .streamId = req.streamId,
      .childId = req.childId,
      .downstreamNodeId = req.downstreamNodeId,
      .downstreamTaskId = req.downstreamTaskId,
      .upstreamNodeId = req.upstreamNodeId,
      .upstreamTaskId = req.upstreamTaskId,
  };
662

L
Liu Jicong 已提交
663
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
664 665 666 667 668 669
  if (pTask && atomic_load_8(&pTask->taskStatus) == TASK_STATUS__NORMAL) {
    rsp.status = 1;
  } else {
    rsp.status = 0;
  }

L
Liu Jicong 已提交
670
  if (pTask) streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
671

672
  tqDebug("tq recv task check req(reqId:0x%" PRIx64 ") %d at node %d check req from task %d at node %d, status %d",
673 674 675 676 677 678 679
          rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.upstreamTaskId, rsp.upstreamNodeId, rsp.status);

  SEncoder encoder;
  int32_t  code;
  int32_t  len;
  tEncodeSize(tEncodeSStreamTaskCheckRsp, &rsp, len, code);
  if (code < 0) {
L
Liu Jicong 已提交
680
    tqError("unable to encode rsp %d", __LINE__);
L
Liu Jicong 已提交
681
    return -1;
682
  }
L
Liu Jicong 已提交
683

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
  void* buf = rpcMallocCont(sizeof(SMsgHead) + len);
  ((SMsgHead*)buf)->vgId = htonl(req.upstreamNodeId);

  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  tEncoderInit(&encoder, (uint8_t*)abuf, len);
  tEncodeSStreamTaskCheckRsp(&encoder, &rsp);
  tEncoderClear(&encoder);

  SRpcMsg rspMsg = {
      .code = 0,
      .pCont = buf,
      .contLen = sizeof(SMsgHead) + len,
      .info = pMsg->info,
  };

  tmsgSendRsp(&rspMsg);
  return 0;
}

703
int32_t tqProcessStreamTaskCheckRsp(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
704 705 706 707 708 709 710 711 712 713 714 715
  int32_t             code;
  SStreamTaskCheckRsp rsp;

  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
  code = tDecodeSStreamTaskCheckRsp(&decoder, &rsp);
  if (code < 0) {
    tDecoderClear(&decoder);
    return -1;
  }
  tDecoderClear(&decoder);

716
  tqDebug("tq recv task check rsp(reqId:0x%" PRIx64 ") %d at node %d check req from task %d at node %d, status %d",
717 718
          rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.upstreamTaskId, rsp.upstreamNodeId, rsp.status);

L
Liu Jicong 已提交
719
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, rsp.upstreamTaskId);
720 721 722 723
  if (pTask == NULL) {
    return -1;
  }

724
  code = streamProcessTaskCheckRsp(pTask, &rsp, sversion);
L
Liu Jicong 已提交
725 726
  streamMetaReleaseTask(pTq->pStreamMeta, pTask);
  return code;
727 728
}

729
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
730 731 732 733 734
  int32_t code;
#if 0
  code = streamMetaAddSerializedTask(pTq->pStreamMeta, version, msg, msgLen);
  if (code < 0) return code;
#endif
5
54liuyao 已提交
735 736 737
  if (tsDisableStream) {
    return 0;
  }
738 739 740 741 742 743

  // 1.deserialize msg and build task
  SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
744

745 746
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
747
  code = tDecodeStreamTask(&decoder, pTask);
748 749 750 751 752
  if (code < 0) {
    tDecoderClear(&decoder);
    taosMemoryFree(pTask);
    return -1;
  }
753

754 755 756
  tDecoderClear(&decoder);

  // 2.save task
757
  code = streamMetaAddTask(pTq->pStreamMeta, sversion, pTask);
758 759 760 761 762 763
  if (code < 0) {
    return -1;
  }

  // 3.go through recover steps to fill history
  if (pTask->fillHistory) {
764
    streamTaskCheckDownstream(pTask, sversion);
765 766 767 768 769
  }

  return 0;
}

L
Liu Jicong 已提交
770 771 772 773 774
int32_t tqProcessTaskRecover1Req(STQ* pTq, SRpcMsg* pMsg) {
  int32_t code;
  char*   msg = pMsg->pCont;
  int32_t msgLen = pMsg->contLen;

775
  SStreamRecoverStep1Req* pReq = (SStreamRecoverStep1Req*)msg;
L
Liu Jicong 已提交
776
  SStreamTask*            pTask = streamMetaAcquireTask(pTq->pStreamMeta, pReq->taskId);
777 778 779 780 781
  if (pTask == NULL) {
    return -1;
  }

  // check param
782
  int64_t fillVer1 = pTask->chkInfo.version;
783
  if (fillVer1 <= 0) {
L
Liu Jicong 已提交
784
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
785 786 787 788 789 790
    return -1;
  }

  // do recovery step 1
  streamSourceRecoverScanStep1(pTask);

L
Liu Jicong 已提交
791 792 793 794 795
  if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
    return 0;
  }

796 797 798 799
  // build msg to launch next step
  SStreamRecoverStep2Req req;
  code = streamBuildSourceRecover2Req(pTask, &req);
  if (code < 0) {
L
Liu Jicong 已提交
800
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
801 802 803
    return -1;
  }

L
Liu Jicong 已提交
804
  streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
805

L
Liu Jicong 已提交
806 807 808 809
  if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
    return 0;
  }

810
  // serialize msg
L
Liu Jicong 已提交
811 812 813 814 815 816 817 818
  int32_t len = sizeof(SStreamRecoverStep1Req);

  void* serializedReq = rpcMallocCont(len);
  if (serializedReq == NULL) {
    return -1;
  }

  memcpy(serializedReq, &req, len);
819 820 821 822 823

  // dispatch msg
  SRpcMsg rpcMsg = {
      .code = 0,
      .contLen = len,
L
Liu Jicong 已提交
824
      .msgType = TDMT_VND_STREAM_RECOVER_BLOCKING_STAGE,
L
Liu Jicong 已提交
825
      .pCont = serializedReq,
826 827 828 829 830 831 832
  };

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

  return 0;
}

833
int32_t tqProcessTaskRecover2Req(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
834 835
  int32_t                 code;
  SStreamRecoverStep2Req* pReq = (SStreamRecoverStep2Req*)msg;
L
Liu Jicong 已提交
836
  SStreamTask*            pTask = streamMetaAcquireTask(pTq->pStreamMeta, pReq->taskId);
837 838 839 840 841
  if (pTask == NULL) {
    return -1;
  }

  // do recovery step 2
842
  code = streamSourceRecoverScanStep2(pTask, sversion);
843
  if (code < 0) {
L
Liu Jicong 已提交
844
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
845 846 847
    return -1;
  }

L
Liu Jicong 已提交
848 849 850 851 852
  if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
    return 0;
  }

853 854 855
  // restore param
  code = streamRestoreParam(pTask);
  if (code < 0) {
L
Liu Jicong 已提交
856
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
857 858 859 860 861 862
    return -1;
  }

  // set status normal
  code = streamSetStatusNormal(pTask);
  if (code < 0) {
L
Liu Jicong 已提交
863
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
864 865 866 867 868 869
    return -1;
  }

  // dispatch recover finish req to all related downstream task
  code = streamDispatchRecoverFinishReq(pTask);
  if (code < 0) {
L
Liu Jicong 已提交
870
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
871 872 873
    return -1;
  }

L
Liu Jicong 已提交
874 875 876
  atomic_store_8(&pTask->fillHistory, 0);
  streamMetaSaveTask(pTq->pStreamMeta, pTask);

L
Liu Jicong 已提交
877 878
  streamMetaReleaseTask(pTq->pStreamMeta, pTask);

879 880 881
  return 0;
}

L
Liu Jicong 已提交
882 883 884
int32_t tqProcessTaskRecoverFinishReq(STQ* pTq, SRpcMsg* pMsg) {
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
885 886

  // deserialize
887 888 889
  SStreamRecoverFinishReq req;

  SDecoder decoder;
X
Xiaoyu Wang 已提交
890
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
891 892 893
  tDecodeSStreamRecoverFinishReq(&decoder, &req);
  tDecoderClear(&decoder);

894
  // find task
L
Liu Jicong 已提交
895
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, req.taskId);
896 897 898
  if (pTask == NULL) {
    return -1;
  }
899
  // do process request
900
  if (streamProcessRecoverFinishReq(pTask, req.childId) < 0) {
L
Liu Jicong 已提交
901
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
902 903 904
    return -1;
  }

L
Liu Jicong 已提交
905
  streamMetaReleaseTask(pTq->pStreamMeta, pTask);
906
  return 0;
L
Liu Jicong 已提交
907
}
L
Liu Jicong 已提交
908

L
Liu Jicong 已提交
909 910 911 912 913
int32_t tqProcessTaskRecoverFinishRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}

L
Liu Jicong 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
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 已提交
930
  if (sz == 0 || pRes->affectedRows == 0) {
L
Liu Jicong 已提交
931 932 933 934 935 936 937 938 939 940 941
    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);
942
    colDataSetVal(pStartCol, i, (const char*)&pRes->skey, false);  // end key column
L
Liu Jicong 已提交
943
    SColumnInfoData* pEndCol = taosArrayGet(pDelBlock->pDataBlock, END_TS_COLUMN_INDEX);
944
    colDataSetVal(pEndCol, i, (const char*)&pRes->ekey, false);
L
Liu Jicong 已提交
945 946 947
    // uid column
    SColumnInfoData* pUidCol = taosArrayGet(pDelBlock->pDataBlock, UID_COLUMN_INDEX);
    int64_t*         pUid = taosArrayGet(pRes->uidList, i);
948
    colDataSetVal(pUidCol, i, (const char*)pUid, false);
L
Liu Jicong 已提交
949

950 951 952
    colDataSetNULL(taosArrayGet(pDelBlock->pDataBlock, GROUPID_COLUMN_INDEX), i);
    colDataSetNULL(taosArrayGet(pDelBlock->pDataBlock, CALCULATE_START_TS_COLUMN_INDEX), i);
    colDataSetNULL(taosArrayGet(pDelBlock->pDataBlock, CALCULATE_END_TS_COLUMN_INDEX), i);
L
Liu Jicong 已提交
953 954
  }

L
Liu Jicong 已提交
955 956
  taosArrayDestroy(pRes->uidList);

L
Liu Jicong 已提交
957 958 959
  int32_t* pRef = taosMemoryMalloc(sizeof(int32_t));
  *pRef = 1;

L
Liu Jicong 已提交
960 961 962 963 964 965 966
  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;

967
    qDebug("delete req enqueue stream task: %d, ver: %" PRId64, pTask->id.taskId, ver);
L
Liu Jicong 已提交
968

L
Liu Jicong 已提交
969
    if (!failed) {
S
Shengliang Guan 已提交
970
      SStreamRefDataBlock* pRefBlock = taosAllocateQitem(sizeof(SStreamRefDataBlock), DEF_QITEM, 0);
L
Liu Jicong 已提交
971 972 973 974 975
      pRefBlock->type = STREAM_INPUT__REF_DATA_BLOCK;
      pRefBlock->pBlock = pDelBlock;
      pRefBlock->dataRef = pRef;
      atomic_add_fetch_32(pRefBlock->dataRef, 1);

976
      if (tAppendDataForStream(pTask, (SStreamQueueItem*)pRefBlock) < 0) {
977
        qError("stream task input del failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
978

L
Liu Jicong 已提交
979
        atomic_sub_fetch_32(pRef, 1);
L
Liu Jicong 已提交
980
        taosFreeQitem(pRefBlock);
L
Liu Jicong 已提交
981 982
        continue;
      }
L
Liu Jicong 已提交
983

L
Liu Jicong 已提交
984
      if (streamSchedExec(pTask) < 0) {
985
        qError("stream task launch failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
986 987
        continue;
      }
L
Liu Jicong 已提交
988

L
Liu Jicong 已提交
989 990 991 992
    } else {
      streamTaskInputFail(pTask);
    }
  }
L
Liu Jicong 已提交
993

L
Liu Jicong 已提交
994
  int32_t ref = atomic_sub_fetch_32(pRef, 1);
L
Liu Jicong 已提交
995
  /*A(ref >= 0);*/
L
Liu Jicong 已提交
996
  if (ref == 0) {
L
Liu Jicong 已提交
997
    blockDataDestroy(pDelBlock);
L
Liu Jicong 已提交
998 999 1000 1001
    taosMemoryFree(pRef);
  }

#if 0
S
Shengliang Guan 已提交
1002
    SStreamDataBlock* pStreamBlock = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
L
Liu Jicong 已提交
1003 1004 1005 1006 1007 1008 1009 1010
    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) {
1011
      if (tAppendDataForStream(pTask, (SStreamQueueItem*)pStreamBlock) < 0) {
1012
        qError("stream task input del failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
1013 1014 1015 1016
        continue;
      }

      if (streamSchedExec(pTask) < 0) {
1017
        qError("stream task launch failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
1018 1019 1020 1021 1022 1023
        continue;
      }
    } else {
      streamTaskInputFail(pTask);
    }
  }
L
Liu Jicong 已提交
1024
  blockDataDestroy(pDelBlock);
L
Liu Jicong 已提交
1025
#endif
L
Liu Jicong 已提交
1026 1027 1028 1029

  return 0;
}

1030 1031 1032
static int32_t addSubmitBlockNLaunchTask(STqOffsetStore* pOffsetStore, SStreamTask* pTask, SStreamDataSubmit2* pSubmit,
                                         const char* key, int64_t ver) {
  doSaveTaskOffset(pOffsetStore, key, ver);
1033
  int32_t code = tqAddInputBlockNLaunchTask(pTask, (SStreamQueueItem*)pSubmit, ver);
1034 1035 1036 1037 1038 1039 1040 1041

  // remove the offset, if all functions are completed successfully.
  if (code == TSDB_CODE_SUCCESS) {
    tqOffsetDelete(pOffsetStore, key);
  }
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
1042
int32_t tqProcessSubmitReq(STQ* pTq, SPackedData submit) {
1043
  void* pIter = NULL;
L
Liu Jicong 已提交
1044

1045
  SStreamDataSubmit2* pSubmit = streamDataSubmitNew(submit, STREAM_INPUT__DATA_SUBMIT);
L
Liu Jicong 已提交
1046
  if (pSubmit == NULL) {
L
Liu Jicong 已提交
1047
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1048
    tqError("failed to create data submit for stream since out of memory");
1049
    saveOffsetForAllTasks(pTq, submit.ver);
1050
    return -1;
L
Liu Jicong 已提交
1051 1052 1053
  }

  while (1) {
L
Liu Jicong 已提交
1054
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
1055 1056 1057 1058
    if (pIter == NULL) {
      break;
    }

1059
    SStreamTask* pTask = *(SStreamTask**)pIter;
1060 1061 1062 1063
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) {
      continue;
    }

1064
    if (pTask->taskStatus == TASK_STATUS__RECOVER_PREPARE || pTask->taskStatus == TASK_STATUS__WAIT_DOWNSTREAM) {
1065 1066
      tqDebug("stream task:%d skip push data, not ready for processing, status %d", pTask->id.taskId,
              pTask->taskStatus);
L
Liu Jicong 已提交
1067 1068
      continue;
    }
L
Liu Jicong 已提交
1069

1070 1071 1072
    // check if offset value exists
    char key[128] = {0};
    createStreamTaskOffsetKey(key, pTask->id.streamId, pTask->id.taskId);
1073

1074 1075 1076 1077 1078 1079 1080 1081
    if (tInputQueueIsFull(pTask)) {
      STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, key);

      int64_t ver = submit.ver;
      if (pOffset == NULL) {
        doSaveTaskOffset(pTq->pOffsetStore, key, submit.ver);
      } else {
        ver = pOffset->val.version;
L
Liu Jicong 已提交
1082 1083
      }

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
      tqDebug("s-task:%s input queue is full, do nothing, start ver:%" PRId64, pTask->id.idStr, ver);
      continue;
    }

    // check if offset value exists
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, key);
    if (pOffset != NULL) {
      // seek the stored version and extract data from WAL
      int32_t code = tqSeekVer(pTask->exec.pTqReader, pOffset->val.version, "");

      // all data has been retrieved from WAL, let's try submit block directly.
      if (code == TSDB_CODE_SUCCESS) {  // all data retrieved, abort
        // append the data for the stream
1097
        SFetchRet ret = {.data.info.type = STREAM_NORMAL};
1098 1099 1100 1101
        terrno = 0;

        tqNextBlock(pTask->exec.pTqReader, &ret);
        if (ret.fetchType == FETCH_TYPE__DATA) {
1102 1103
          code = launchTaskForWalBlock(pTask, &ret, pOffset);
          if (code != TSDB_CODE_SUCCESS) {
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
            continue;
          }
        } else {  // FETCH_TYPE__NONE, let's try submit block directly
          tqDebug("s-task:%s data in WAL are all consumed, try data in submit message", pTask->id.idStr);
          addSubmitBlockNLaunchTask(pTq->pOffsetStore, pTask, pSubmit, key, submit.ver);
        }

        // do nothing if failed, since the offset value is kept already
      } else {  // failed to seek to the WAL version
        // todo handle the case where offset has been deleted in WAL, due to stream computing too slow
        tqDebug("s-task:%s data in WAL are all consumed, try data in submit msg", pTask->id.idStr);
        addSubmitBlockNLaunchTask(pTq->pOffsetStore, pTask, pSubmit, key, submit.ver);
L
Liu Jicong 已提交
1116
      }
L
Liu Jicong 已提交
1117
    } else {
1118
      addSubmitBlockNLaunchTask(pTq->pOffsetStore, pTask, pSubmit, key, submit.ver);
L
Liu Jicong 已提交
1119 1120 1121
    }
  }

1122 1123
  streamDataSubmitDestroy(pSubmit);
  taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
1124

1125
  return 0;
L
Liu Jicong 已提交
1126 1127
}

L
Liu Jicong 已提交
1128 1129
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRunReq* pReq = pMsg->pCont;
1130 1131 1132 1133 1134 1135

  int32_t taskId = pReq->taskId;
  int32_t vgId = TD_VID(pTq->pVnode);

  if (taskId == ALL_STREAM_TASKS_ID) {  // all tasks are restored from the wal
    tqDoRestoreSourceStreamTasks(pTq);
L
Liu Jicong 已提交
1136
    return 0;
1137
  } else {
1138
    SStreamTask* pTask = streamMetaAcquireTaskEx(pTq->pStreamMeta, taskId);
1139 1140 1141 1142
    if (pTask != NULL) {
      if (pTask->taskStatus == TASK_STATUS__NORMAL) {
        tqDebug("vgId:%d s-task:%s start to process run req", vgId, pTask->id.idStr);
        streamProcessRunReq(pTask);
1143 1144 1145
      } else if (pTask->taskStatus == TASK_STATUS__RESTORE) {
        tqDebug("vgId:%d s-task:%s start to process in restore procedure from last chk point:%" PRId64, vgId,
                pTask->id.idStr, pTask->chkInfo.version);
1146
        streamProcessRunReq(pTask);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
      } else {
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state", vgId, pTask->id.idStr);
      }

      streamMetaReleaseTask(pTq->pStreamMeta, pTask);
      return 0;
    } else {
      tqError("vgId:%d failed to found s-task, taskId:%d", vgId, taskId);
      return -1;
    }
L
Liu Jicong 已提交
1157
  }
L
Liu Jicong 已提交
1158 1159
}

L
Liu Jicong 已提交
1160
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
1161 1162 1163 1164 1165
  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 已提交
1166
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
1167
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
1168 1169
  int32_t taskId = req.taskId;

L
Liu Jicong 已提交
1170
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1171
  if (pTask) {
1172 1173 1174 1175
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
1176
    streamProcessDispatchReq(pTask, &req, &rsp, exec);
L
Liu Jicong 已提交
1177
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1178
    return 0;
1179 1180
  } else {
    return -1;
L
Liu Jicong 已提交
1181
  }
L
Liu Jicong 已提交
1182 1183
}

L
Liu Jicong 已提交
1184 1185
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1186
  int32_t             taskId = ntohl(pRsp->upstreamTaskId);
L
Liu Jicong 已提交
1187
  SStreamTask*        pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
1188
  tqDebug("recv dispatch rsp, code:%x", pMsg->code);
L
Liu Jicong 已提交
1189
  if (pTask) {
1190
    streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
L
Liu Jicong 已提交
1191
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1192
    return 0;
1193 1194
  } else {
    return -1;
L
Liu Jicong 已提交
1195
  }
L
Liu Jicong 已提交
1196
}
L
Liu Jicong 已提交
1197

1198
int32_t tqProcessTaskDropReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
1199
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
1200
  streamMetaRemoveTask(pTq->pStreamMeta, pReq->taskId);
L
Liu Jicong 已提交
1201
  return 0;
L
Liu Jicong 已提交
1202
}
L
Liu Jicong 已提交
1203 1204 1205 1206 1207 1208 1209

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;
1210
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
L
Liu Jicong 已提交
1211
  tDecodeStreamRetrieveReq(&decoder, &req);
L
Liu Jicong 已提交
1212
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1213
  int32_t      taskId = req.dstTaskId;
L
Liu Jicong 已提交
1214
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1215
  if (pTask) {
1216
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1217
    streamProcessRetrieveReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
1218
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1219
    tDeleteStreamRetrieveReq(&req);
L
Liu Jicong 已提交
1220
    return 0;
L
Liu Jicong 已提交
1221 1222
  } else {
    return -1;
L
Liu Jicong 已提交
1223 1224 1225 1226 1227 1228 1229
  }
}

int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}
L
Liu Jicong 已提交
1230

1231 1232 1233 1234 1235 1236
int32_t vnodeEnqueueStreamMsg(SVnode* pVnode, SRpcMsg* pMsg) {
  STQ*      pTq = pVnode->pTq;
  SMsgHead* msgStr = pMsg->pCont;
  char*     msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t   msgLen = pMsg->contLen - sizeof(SMsgHead);
  int32_t   code = 0;
L
Liu Jicong 已提交
1237 1238 1239

  SStreamDispatchReq req;
  SDecoder           decoder;
1240
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
L
Liu Jicong 已提交
1241 1242
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
    code = TSDB_CODE_MSG_DECODE_ERROR;
L
Liu Jicong 已提交
1243
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1244 1245
    goto FAIL;
  }
L
Liu Jicong 已提交
1246
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1247

L
Liu Jicong 已提交
1248
  int32_t taskId = req.taskId;
L
Liu Jicong 已提交
1249

L
Liu Jicong 已提交
1250
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1251
  if (pTask) {
1252
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1253
    streamProcessDispatchReq(pTask, &req, &rsp, false);
L
Liu Jicong 已提交
1254
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1255 1256
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
1257
    return 0;
5
54liuyao 已提交
1258 1259
  } else {
    tDeleteStreamDispatchReq(&req);
L
Liu Jicong 已提交
1260
  }
L
Liu Jicong 已提交
1261

1262 1263
  code = TSDB_CODE_STREAM_TASK_NOT_EXIST;

L
Liu Jicong 已提交
1264
FAIL:
1265 1266 1267 1268
  if (pMsg->info.handle == NULL) return -1;

  SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
  if (pRspHead == NULL) {
1269
    SRpcMsg rsp = { .code = TSDB_CODE_OUT_OF_MEMORY, .info = pMsg->info };
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    tqDebug("send dispatch error rsp, code: %x", code);
    tmsgSendRsp(&rsp);
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
    return -1;
  }

  pRspHead->vgId = htonl(req.upstreamNodeId);
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pRspHead, sizeof(SMsgHead));
  pRsp->streamId = htobe64(req.streamId);
  pRsp->upstreamTaskId = htonl(req.upstreamTaskId);
  pRsp->upstreamNodeId = htonl(req.upstreamNodeId);
  pRsp->downstreamNodeId = htonl(pVnode->config.vgId);
  pRsp->downstreamTaskId = htonl(req.taskId);
  pRsp->inputStatus = TASK_OUTPUT_STATUS__NORMAL;

L
Liu Jicong 已提交
1286
  SRpcMsg rsp = {
1287
      .code = code, .info = pMsg->info, .contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp), .pCont = pRspHead};
1288
  tqDebug("send dispatch error rsp, code: %x", code);
L
Liu Jicong 已提交
1289
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
1290 1291
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
1292
  return -1;
L
Liu Jicong 已提交
1293
}
L
Liu Jicong 已提交
1294

1295
int32_t tqCheckLogInWal(STQ* pTq, int64_t sversion) { return sversion <= pTq->walLogLastVer; }
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

int32_t tqRestoreStreamTasks(STQ* pTq) {
  int32_t vgId = TD_VID(pTq->pVnode);

  SStreamTaskRunReq* pRunReq = rpcMallocCont(sizeof(SStreamTaskRunReq));
  if (pRunReq == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    tqError("vgId:%d failed restore stream tasks, code:%s", vgId, terrstr(terrno));
    return -1;
  }

1307 1308 1309 1310
  int32_t numOfTasks = taosHashGetSize(pTq->pStreamMeta->pRestoreTasks);
  tqInfo("vgId:%d start restoring stream tasks, total tasks:%d", vgId, numOfTasks);
  initOffsetForAllRestoreTasks(pTq);

1311 1312 1313 1314 1315 1316 1317 1318 1319
  pRunReq->head.vgId = vgId;
  pRunReq->streamId = 0;
  pRunReq->taskId = ALL_STREAM_TASKS_ID;

  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_RUN, .pCont = pRunReq, .contLen = sizeof(SStreamTaskRunReq)};
  tmsgPutToQueue(&pTq->pVnode->msgCb, STREAM_QUEUE, &msg);

  return 0;
}