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

dengyihao's avatar
dengyihao 已提交
18 19 20
// 0: not init
// 1: already inited
// 2: wait to be inited or cleaup
21
#define WAL_READ_TASKS_ID       (-1)
dengyihao's avatar
dengyihao 已提交
22

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

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

L
Liu Jicong 已提交
42 43
  return 0;
}
L
Liu Jicong 已提交
44

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

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

L
Liu Jicong 已提交
74
static void tqPushEntryFree(void* data) {
L
Liu Jicong 已提交
75
  STqPushEntry* p = *(void**)data;
D
dapan1121 已提交
76 77 78 79 80 81 82
  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 已提交
83 84 85
  taosMemoryFree(p);
}

86 87 88 89 90
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 已提交
91
STQ* tqOpen(const char* path, SVnode* pVnode) {
92
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
L
Liu Jicong 已提交
93
  if (pTq == NULL) {
S
Shengliang Guan 已提交
94
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
95 96
    return NULL;
  }
97

98
  pTq->path = taosStrdup(path);
L
Liu Jicong 已提交
99
  pTq->pVnode = pVnode;
L
Liu Jicong 已提交
100
  pTq->walLogLastVer = pVnode->pWal->vers.lastVer;
101

102
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
103
  taosHashSetFreeFp(pTq->pHandle, destroyTqHandle);
104

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

109
  pTq->pCheckInfo = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
110
  taosHashSetFreeFp(pTq->pCheckInfo, (FDelete)tDeleteSTqCheckInfo);
L
Liu Jicong 已提交
111

L
Liu Jicong 已提交
112
  if (tqMetaOpen(pTq) < 0) {
L
Liu Jicong 已提交
113
    return NULL;
114 115
  }

L
Liu Jicong 已提交
116 117
  pTq->pOffsetStore = tqOffsetOpen(pTq);
  if (pTq->pOffsetStore == NULL) {
L
Liu Jicong 已提交
118
    return NULL;
119 120
  }

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

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

L
Liu Jicong 已提交
130 131
  return pTq;
}
L
Liu Jicong 已提交
132

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

  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 已提交
146
}
L
Liu Jicong 已提交
147

D
dapan1121 已提交
148 149
static int32_t doSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, const SMqDataRsp* pRsp, int32_t epoch,
                             int64_t consumerId, int32_t type) {
L
Liu Jicong 已提交
150 151
  int32_t len = 0;
  int32_t code = 0;
D
dapan1121 已提交
152 153 154 155 156 157

  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 已提交
158 159 160 161 162 163 164 165 166 167 168

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

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

D
dapan1121 已提交
169 170 171
  ((SMqRspHead*)buf)->mqMsgType = type;
  ((SMqRspHead*)buf)->epoch = epoch;
  ((SMqRspHead*)buf)->consumerId = consumerId;
L
Liu Jicong 已提交
172 173 174 175 176 177

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

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

D
dapan1121 已提交
178 179 180
  if (type == TMQ_MSG_TYPE__POLL_RSP) {
    tEncodeSMqDataRsp(&encoder, pRsp);
  } else if (type == TMQ_MSG_TYPE__TAOSX_RSP) {
X
Xiaoyu Wang 已提交
181
    tEncodeSTaosxRsp(&encoder, (STaosxRsp*)pRsp);
dengyihao's avatar
dengyihao 已提交
182
  }
L
Liu Jicong 已提交
183

wmmhello's avatar
wmmhello 已提交
184
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
185 186

  SRpcMsg rsp = {
D
dapan1121 已提交
187
      .info = *pRpcHandleInfo,
L
Liu Jicong 已提交
188 189 190 191
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
L
Liu Jicong 已提交
192

L
Liu Jicong 已提交
193
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
194 195
  return 0;
}
L
Liu Jicong 已提交
196

D
dapan1121 已提交
197 198 199 200 201 202 203 204 205 206 207
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);

  char buf1[80] = {0};
  char buf2[80] = {0};
  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 已提交
208 209 210
  return 0;
}

D
dapan1121 已提交
211 212
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);
213

D
dapan1121 已提交
214 215 216 217
  char buf1[80] = {0};
  char buf2[80] = {0};
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);
218

X
Xiaoyu Wang 已提交
219
  tqDebug("vgId:%d consumer:0x%" PRIx64 " (epoch %d) send rsp, block num:%d, req:%s, rsp:%s, reqId:0x%" PRIx64,
D
dapan1121 已提交
220
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
221 222 223 224

  return 0;
}

225
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
226
  STqOffset offset = {0};
X
Xiaoyu Wang 已提交
227
  int32_t   vgId = TD_VID(pTq->pVnode);
228

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

235 236
  tDecoderClear(&decoder);

wmmhello's avatar
wmmhello 已提交
237
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA || offset.val.type == TMQ_OFFSET__SNAPSHOT_META) {
L
Liu Jicong 已提交
238
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
D
dapan1121 已提交
239
            offset.subKey, vgId, offset.val.uid, offset.val.ts);
L
Liu Jicong 已提交
240
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
X
Xiaoyu Wang 已提交
241 242
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, offset.subKey, vgId,
            offset.val.version);
243
    if (offset.val.version + 1 == sversion) {
244 245
      offset.val.version += 1;
    }
246
  } else {
247 248
    tqError("invalid commit offset type:%d", offset.val.type);
    return -1;
249
  }
250 251 252 253

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

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

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

268 269 270
  return 0;
}

L
Liu Jicong 已提交
271
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
L
Liu Jicong 已提交
272
  void* pIter = NULL;
273

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

280
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
281

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

L
Liu Jicong 已提交
294 295 296
  return 0;
}

D
dapan1121 已提交
297
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
X
Xiaoyu Wang 已提交
298
  SMqPollReq req = {0};
D
dapan1121 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  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;
  }

  // 2. check re-balance status
319
  taosRLockLatch(&pTq->lock);
D
dapan1121 已提交
320 321 322 323
  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;
324
    taosRUnLockLatch(&pTq->lock);
D
dapan1121 已提交
325 326
    return -1;
  }
327
  taosRUnLockLatch(&pTq->lock);
D
dapan1121 已提交
328 329

  // 3. update the epoch value
330
  taosWLockLatch(&pTq->lock);
D
dapan1121 已提交
331 332
  int32_t savedEpoch = pHandle->epoch;
  if (savedEpoch < reqEpoch) {
X
Xiaoyu Wang 已提交
333 334
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, savedEpoch,
            reqEpoch);
D
dapan1121 已提交
335 336
    pHandle->epoch = reqEpoch;
  }
337
  taosWUnLockLatch(&pTq->lock);
D
dapan1121 已提交
338 339 340 341 342 343

  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
  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);

344
  return tqExtractDataForMq(pTq, pHandle, &req, pMsg);
D
dapan1121 已提交
345 346
}

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

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

352
  taosWLockLatch(&pTq->lock);
L
Liu Jicong 已提交
353 354 355 356
  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);
  }
357
  taosWUnLockLatch(&pTq->lock);
L
Liu Jicong 已提交
358

L
Liu Jicong 已提交
359 360
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
  if (pHandle) {
X
Xiaoyu Wang 已提交
361
    // walCloseRef(pHandle->pWalReader->pWal, pHandle->pRef->refId);
L
Liu Jicong 已提交
362 363 364 365 366 367 368
    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 已提交
369
  }
370

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

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

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

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

414
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
415
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
416
  tDecodeSMqRebVgReq(msg, &req);
L
Liu Jicong 已提交
417

D
dapan1121 已提交
418 419 420
  SVnode* pVnode = pTq->pVnode;
  int32_t vgId = TD_VID(pVnode);

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

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

L
Liu Jicong 已提交
431
    if (req.newConsumerId == -1) {
432
      tqError("vgId:%d, tq invalid re-balance request, new consumerId %" PRId64 "", req.vgId, req.newConsumerId);
L
Liu Jicong 已提交
433
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
434 435
      return 0;
    }
D
dapan1121 已提交
436

L
Liu Jicong 已提交
437 438
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
439

H
Haojun Liao 已提交
440
    uint64_t oldConsumerId = pHandle->consumerId;
L
Liu Jicong 已提交
441 442 443
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
444

L
Liu Jicong 已提交
445
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
446
    pHandle->fetchMeta = req.withMeta;
wmmhello's avatar
wmmhello 已提交
447

448
    // TODO version should be assigned and refed during preprocess
D
dapan1121 已提交
449
    SWalRef* pRef = walRefCommittedVer(pVnode->pWal);
450
    if (pRef == NULL) {
D
dapan1121 已提交
451
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
452
      return -1;
453
    }
D
dapan1121 已提交
454

455 456
    int64_t ver = pRef->refVer;
    pHandle->pRef = pRef;
L
Liu Jicong 已提交
457

458
    SReadHandle handle = {
459
        .meta = pVnode->pMeta, .vnode = pVnode, .initTableReader = true, .initTqReader = true, .version = ver};
wmmhello's avatar
wmmhello 已提交
460
    pHandle->snapshotVer = ver;
461

L
Liu Jicong 已提交
462
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
463
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
464
      req.qmsg = NULL;
465

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

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

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

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

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

501
    taosHashPut(pTq->pHandle, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
dengyihao's avatar
dengyihao 已提交
502 503
    tqDebug("try to persist handle %s consumer:0x%" PRIx64 " , old consumer:0x%" PRIx64, req.subKey,
            pHandle->consumerId, oldConsumerId);
L
Liu Jicong 已提交
504
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
D
dapan1121 已提交
505
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
506
      return -1;
L
Liu Jicong 已提交
507
    }
L
Liu Jicong 已提交
508
  } else {
D
dapan1121 已提交
509 510 511 512 513 514
    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);
      taosMemoryFree(req.qmsg);
      return tqMetaSaveHandle(pTq, req.subKey, pHandle);
515 516 517
    } else {
      tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
             req.newConsumerId);
H
Haojun Liao 已提交
518

519 520 521
      // kill executing task
      qTaskInfo_t pTaskInfo = pHandle->execHandle.task;
      if (pTaskInfo != NULL) {
522
        qKillTask(pTaskInfo, TSDB_CODE_SUCCESS);
523
      }
D
dapan1121 已提交
524

525 526
      taosWLockLatch(&pTq->lock);
      atomic_store_32(&pHandle->epoch, -1);
D
dapan1121 已提交
527

528
      // remove if it has been register in the push manager, and return one empty block to consumer
529
      tqUnregisterPushHandle(pTq, req.subKey, (int32_t)strlen(req.subKey), pHandle->consumerId, true);
D
dapan1121 已提交
530

531 532
      atomic_store_64(&pHandle->consumerId, req.newConsumerId);
      atomic_add_fetch_32(&pHandle->epoch, 1);
D
dapan1121 已提交
533

534
      if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
535 536
        qStreamCloseTsdbReader(pTaskInfo);
      }
H
Haojun Liao 已提交
537

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

D
dapan1121 已提交
546
  taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
547
  return 0;
L
Liu Jicong 已提交
548
}
549

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

D
dapan1121 已提交
555
  int32_t vgId = TD_VID(pTq->pVnode);
556
  pTask->id.idStr = taosStrdup(buf);
L
Liu Jicong 已提交
557
  pTask->refCnt = 1;
558
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
dengyihao's avatar
dengyihao 已提交
559 560
  pTask->inputQueue = streamQueueOpen(512 << 10);
  pTask->outputQueue = streamQueueOpen(512 << 10);
L
Liu Jicong 已提交
561 562

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
L
Liu Jicong 已提交
563
    return -1;
L
Liu Jicong 已提交
564 565
  }

L
Liu Jicong 已提交
566 567
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;
568
  pTask->pMsgCb = &pTq->pVnode->msgCb;
569
  pTask->pMeta = pTq->pStreamMeta;
570
  pTask->chkInfo.version = ver;
571

572
  // expand executor
573
  if (pTask->fillHistory) {
574
    pTask->status.taskStatus = TASK_STATUS__WAIT_DOWNSTREAM;
575
  } else {
576
    pTask->status.taskStatus = TASK_STATUS__RESTORE;
577 578
  }

579
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
580
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
581 582 583 584
    if (pTask->pState == NULL) {
      return -1;
    }

585
    SReadHandle handle = {
586
        .meta = pTq->pVnode->pMeta, .vnode = pTq->pVnode, .initTqReader = 1, .pStateBackend = pTask->pState};
587

588 589
    pTask->exec.pExecutor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle, vgId);
    if (pTask->exec.pExecutor == NULL) {
L
Liu Jicong 已提交
590 591
      return -1;
    }
592

593
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
594
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
595 596 597
    if (pTask->pState == NULL) {
      return -1;
    }
598

599 600 601 602 603
    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 已提交
604 605
      return -1;
    }
L
Liu Jicong 已提交
606
  }
L
Liu Jicong 已提交
607 608

  // sink
L
Liu Jicong 已提交
609
  /*pTask->ahandle = pTq->pVnode;*/
610
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
611
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
612
    pTask->smaSink.smaSink = smaHandleRes;
613
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
614
    pTask->tbSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
615
    pTask->tbSink.tbSinkFunc = tqSinkToTablePipeline2;
L
Liu Jicong 已提交
616

X
Xiaoyu Wang 已提交
617
    int32_t   ver1 = 1;
5
54liuyao 已提交
618
    SMetaInfo info = {0};
dengyihao's avatar
dengyihao 已提交
619
    int32_t   code = metaGetInfo(pTq->pVnode->pMeta, pTask->tbSink.stbUid, &info, NULL);
5
54liuyao 已提交
620
    if (code == TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
621
      ver1 = info.skmVer;
5
54liuyao 已提交
622
    }
L
Liu Jicong 已提交
623

624 625
    SSchemaWrapper* pschemaWrapper = pTask->tbSink.pSchemaWrapper;
    pTask->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
wmmhello's avatar
wmmhello 已提交
626
    if(pTask->tbSink.pTSchema == NULL) {
D
dapan1121 已提交
627 628
      return -1;
    }
L
Liu Jicong 已提交
629
  }
630

631
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
632
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);
633 634
  }

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

641 642 643 644 645 646
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 已提交
647
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
648 649 650 651 652 653 654 655 656 657 658 659
  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,
  };
660

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

    tqDebug("tq recv task check req(reqId:0x%" PRIx64
            ") %d at node %d task status:%d, check req from task %d at node %d, rsp status %d",
            rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, pTask->status.taskStatus, rsp.upstreamTaskId,
            rsp.upstreamNodeId, rsp.status);
670 671
  } else {
    rsp.status = 0;
672 673 674 675
    tqDebug("tq recv task check(taskId:%d not built yet) req(reqId:0x%" PRIx64
            ") %d at node %d, check req from task %d at node %d, rsp status %d",
            taskId, rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.upstreamTaskId, rsp.upstreamNodeId,
            rsp.status);
676 677 678 679 680 681 682
  }

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

687 688 689 690 691 692 693 694
  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);

695
  SRpcMsg rspMsg = { .code = 0, .pCont = buf, .contLen = sizeof(SMsgHead) + len, .info = pMsg->info };
696 697 698 699
  tmsgSendRsp(&rspMsg);
  return 0;
}

700
int32_t tqProcessStreamTaskCheckRsp(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
701 702 703 704 705 706 707 708 709 710 711
  int32_t             code;
  SStreamTaskCheckRsp rsp;

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

712
  tDecoderClear(&decoder);
713
  tqDebug("tq recv task check rsp(reqId:0x%" PRIx64 ") %d at node %d check req from task %d at node %d, status %d",
714 715
          rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.upstreamTaskId, rsp.upstreamNodeId, rsp.status);

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

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

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

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

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

751 752 753
  tDecoderClear(&decoder);

  // 2.save task
754
  code = streamMetaAddDeployedTask(pTq->pStreamMeta, sversion, pTask);
755
  if (code < 0) {
756 757
    tqError("vgId:%d failed to add s-task:%s, total:%d", TD_VID(pTq->pVnode), pTask->id.idStr,
            streamMetaGetNumOfTasks(pTq->pStreamMeta));
758 759 760 761 762
    return -1;
  }

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

766 767
  tqDebug("vgId:%d s-task:%s is deployed and add meta from mnd, status:%d, total:%d", TD_VID(pTq->pVnode),
          pTask->id.idStr, pTask->status.taskStatus, streamMetaGetNumOfTasks(pTq->pStreamMeta));
768 769 770
  return 0;
}

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

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

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

  // do recovery step 1
  streamSourceRecoverScanStep1(pTask);

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

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

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

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

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

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

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

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

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

  return 0;
}

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

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

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

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

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

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

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

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

880 881 882
  return 0;
}

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

  // deserialize
888 889 890
  SStreamRecoverFinishReq req;

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

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

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

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

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

951 952 953
    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 已提交
954 955
  }

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

  // remove the offset, if all functions are completed successfully.
  if (code == TSDB_CODE_SUCCESS) {
    tqOffsetDelete(pOffsetStore, key);
  }
L
Liu Jicong 已提交
1040

1041
  return code;
1042 1043
}

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

1055 1056
  SArray* pInputQueueFullTasks = taosArrayInit(4, POINTER_BYTES);

L
Liu Jicong 已提交
1057
  while (1) {
L
Liu Jicong 已提交
1058
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
1059 1060 1061 1062
    if (pIter == NULL) {
      break;
    }

1063
    SStreamTask* pTask = *(SStreamTask**)pIter;
1064
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) {
L
Liu Jicong 已提交
1065 1066
      continue;
    }
L
Liu Jicong 已提交
1067

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

1074 1075 1076
    // check if offset value exists
    char key[128] = {0};
    createStreamTaskOffsetKey(key, pTask->id.streamId, pTask->id.taskId);
L
Liu Jicong 已提交
1077

1078 1079 1080 1081 1082 1083 1084 1085
    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 已提交
1086
      }
L
Liu Jicong 已提交
1087

1088 1089
      tqDebug("s-task:%s input queue is full, discard submit block, ver:%" PRId64, pTask->id.idStr, ver);
      taosArrayPush(pInputQueueFullTasks, &pTask);
1090
      continue;
L
Liu Jicong 已提交
1091 1092
    }

1093 1094
    // check if offset value exists
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, key);
1095
    ASSERT(pOffset == NULL);
1096

1097
    addSubmitBlockNLaunchTask(pTq->pOffsetStore, pTask, pSubmit, key, submit.ver);
L
Liu Jicong 已提交
1098
  }
L
Liu Jicong 已提交
1099

1100 1101
  streamDataSubmitDestroy(pSubmit);
  taosFreeQitem(pSubmit);
1102
#endif
L
Liu Jicong 已提交
1103

1104
  tqStartStreamTasks(pTq);
1105
  return 0;
L
Liu Jicong 已提交
1106 1107
}

L
Liu Jicong 已提交
1108 1109
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRunReq* pReq = pMsg->pCont;
1110 1111 1112 1113

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

1114 1115
  if (taskId == WAL_READ_TASKS_ID) {  // all tasks are extracted submit data from the wal
    tqStreamTasksScanWal(pTq);
L
Liu Jicong 已提交
1116
    return 0;
1117
  }
1118

1119
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
1120 1121 1122 1123 1124 1125 1126 1127
  if (pTask != NULL) {
    if (pTask->status.taskStatus == TASK_STATUS__NORMAL) {
      tqDebug("vgId:%d s-task:%s start to process run req", vgId, pTask->id.idStr);
      streamProcessRunReq(pTask);
    } else if (pTask->status.taskStatus == TASK_STATUS__RESTORE) {
      tqDebug("vgId:%d s-task:%s start to process block from wal, last chk point:%" PRId64, vgId,
              pTask->id.idStr, pTask->chkInfo.version);
      streamProcessRunReq(pTask);
1128
    } else {
1129
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state", vgId, pTask->id.idStr);
1130
    }
1131

L
Liu Jicong 已提交
1132
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
1133
    tqStartStreamTasks(pTq);
L
Liu Jicong 已提交
1134
    return 0;
1135
  } else {
1136
    tqError("vgId:%d failed to found s-task, taskId:%d", vgId, taskId);
1137
    return -1;
L
Liu Jicong 已提交
1138
  }
L
Liu Jicong 已提交
1139 1140
}

L
Liu Jicong 已提交
1141
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
1142 1143 1144 1145 1146
  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 已提交
1147
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
1148
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
1149

1150
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, req.taskId);
L
Liu Jicong 已提交
1151
  if (pTask) {
1152
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1153
    streamProcessDispatchReq(pTask, &req, &rsp, exec);
L
Liu Jicong 已提交
1154
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1155
    return 0;
1156 1157
  } else {
    return -1;
L
Liu Jicong 已提交
1158
  }
L
Liu Jicong 已提交
1159 1160
}

L
Liu Jicong 已提交
1161 1162
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1163
  int32_t             taskId = ntohl(pRsp->upstreamTaskId);
L
Liu Jicong 已提交
1164
  SStreamTask*        pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
1165
  tqDebug("recv dispatch rsp, code:%x", pMsg->code);
L
Liu Jicong 已提交
1166
  if (pTask) {
1167
    streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
L
Liu Jicong 已提交
1168
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1169
    return 0;
1170 1171
  } else {
    return -1;
L
Liu Jicong 已提交
1172
  }
L
Liu Jicong 已提交
1173
}
L
Liu Jicong 已提交
1174

1175
int32_t tqProcessTaskDropReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
1176
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
1177
  streamMetaRemoveTask(pTq->pStreamMeta, pReq->taskId);
L
Liu Jicong 已提交
1178
  return 0;
L
Liu Jicong 已提交
1179
}
L
Liu Jicong 已提交
1180 1181 1182 1183 1184 1185 1186

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;
1187
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
L
Liu Jicong 已提交
1188
  tDecodeStreamRetrieveReq(&decoder, &req);
L
Liu Jicong 已提交
1189
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1190
  int32_t      taskId = req.dstTaskId;
L
Liu Jicong 已提交
1191
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1192
  if (pTask) {
1193
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1194
    streamProcessRetrieveReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
1195
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1196
    tDeleteStreamRetrieveReq(&req);
L
Liu Jicong 已提交
1197
    return 0;
L
Liu Jicong 已提交
1198 1199
  } else {
    return -1;
L
Liu Jicong 已提交
1200 1201 1202 1203 1204 1205 1206
  }
}

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

1208 1209 1210 1211 1212 1213
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 已提交
1214 1215 1216

  SStreamDispatchReq req;
  SDecoder           decoder;
1217
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
L
Liu Jicong 已提交
1218 1219
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
    code = TSDB_CODE_MSG_DECODE_ERROR;
L
Liu Jicong 已提交
1220
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1221 1222
    goto FAIL;
  }
L
Liu Jicong 已提交
1223
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1224

L
Liu Jicong 已提交
1225
  int32_t taskId = req.taskId;
L
Liu Jicong 已提交
1226

L
Liu Jicong 已提交
1227
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1228
  if (pTask) {
1229
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1230
    streamProcessDispatchReq(pTask, &req, &rsp, false);
L
Liu Jicong 已提交
1231
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1232 1233
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
1234
    return 0;
5
54liuyao 已提交
1235 1236
  } else {
    tDeleteStreamDispatchReq(&req);
L
Liu Jicong 已提交
1237
  }
L
Liu Jicong 已提交
1238

1239 1240
  code = TSDB_CODE_STREAM_TASK_NOT_EXIST;

L
Liu Jicong 已提交
1241
FAIL:
1242 1243 1244 1245
  if (pMsg->info.handle == NULL) return -1;

  SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
  if (pRspHead == NULL) {
1246
    SRpcMsg rsp = { .code = TSDB_CODE_OUT_OF_MEMORY, .info = pMsg->info };
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
    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 已提交
1263
  SRpcMsg rsp = {
1264
      .code = code, .info = pMsg->info, .contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp), .pCont = pRspHead};
1265
  tqDebug("send dispatch error rsp, code: %x", code);
L
Liu Jicong 已提交
1266
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
1267 1268
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
1269
  return -1;
L
Liu Jicong 已提交
1270
}
L
Liu Jicong 已提交
1271

1272
int32_t tqCheckLogInWal(STQ* pTq, int64_t sversion) { return sversion <= pTq->walLogLastVer; }
1273

1274
int32_t tqStartStreamTasks(STQ* pTq) {
1275 1276
  int32_t vgId = TD_VID(pTq->pVnode);

1277 1278 1279 1280 1281 1282
  SStreamMeta* pMeta = pTq->pStreamMeta;
  taosWLockLatch(&pMeta->lock);
  pMeta->walScan += 1;

  if (pMeta->walScan > 1) {
    tqDebug("vgId:%d wal read task has been launched, remain scan times:%d", vgId, pMeta->walScan);
H
Haojun Liao 已提交
1283
    taosWUnLockLatch(&pMeta->lock);
1284 1285 1286
    return 0;
  }

1287 1288 1289 1290
  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));
1291
    taosWUnLockLatch(&pTq->pStreamMeta->lock);
1292 1293 1294
    return -1;
  }

1295 1296
  int32_t numOfTasks = taosHashGetSize(pTq->pStreamMeta->pTasks);

H
Haojun Liao 已提交
1297
  tqDebug("vgId:%d start wal scan stream tasks, tasks:%d", vgId, numOfTasks);
1298 1299
  initOffsetForAllRestoreTasks(pTq);

1300 1301
  pRunReq->head.vgId = vgId;
  pRunReq->streamId = 0;
1302
  pRunReq->taskId = WAL_READ_TASKS_ID;
1303 1304 1305

  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_RUN, .pCont = pRunReq, .contLen = sizeof(SStreamTaskRunReq)};
  tmsgPutToQueue(&pTq->pVnode->msgCb, STREAM_QUEUE, &msg);
1306
  taosWUnLockLatch(&pTq->pStreamMeta->lock);
1307 1308 1309

  return 0;
}