tq.c 41.8 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)
22

23 24
static int32_t tqInitialize(STQ* pTq);

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

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

L
Liu Jicong 已提交
44 45
  return 0;
}
L
Liu Jicong 已提交
46

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

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

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

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

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

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

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

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

114
  tqInitialize(pTq);
115 116 117 118
  return pTq;
}

int32_t tqInitialize(STQ* pTq) {
L
Liu Jicong 已提交
119
  if (tqMetaOpen(pTq) < 0) {
120
    return -1;
121 122
  }

L
Liu Jicong 已提交
123 124
  pTq->pOffsetStore = tqOffsetOpen(pTq);
  if (pTq->pOffsetStore == NULL) {
125
    return -1;
126 127
  }

128
  pTq->pStreamMeta = streamMetaOpen(pTq->path, pTq, (FTaskExpand*)tqExpandTask, pTq->pVnode->config.vgId);
L
Liu Jicong 已提交
129
  if (pTq->pStreamMeta == NULL) {
130
    return -1;
L
Liu Jicong 已提交
131 132
  }

133 134
  // the version is kept in task's meta data
  // todo check if this version is required or not
135 136
  if (streamLoadTasks(pTq->pStreamMeta, walGetCommittedVer(pTq->pVnode->pWal)) < 0) {
    return -1;
L
Liu Jicong 已提交
137 138
  }

139
  return 0;
L
Liu Jicong 已提交
140
}
L
Liu Jicong 已提交
141

L
Liu Jicong 已提交
142
void tqClose(STQ* pTq) {
143 144
  if (pTq == NULL) {
    return;
H
Hongze Cheng 已提交
145
  }
146 147 148 149 150 151 152 153 154

  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 已提交
155
}
L
Liu Jicong 已提交
156

H
Haojun Liao 已提交
157 158
static int32_t doSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, const SMqDataRsp* pRsp, int32_t epoch,
                             int64_t consumerId, int32_t type) {
L
Liu Jicong 已提交
159 160
  int32_t len = 0;
  int32_t code = 0;
H
Haojun Liao 已提交
161 162 163 164 165 166

  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 已提交
167 168 169 170 171 172 173 174 175 176 177

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

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

H
Haojun Liao 已提交
178 179 180
  ((SMqRspHead*)buf)->mqMsgType = type;
  ((SMqRspHead*)buf)->epoch = epoch;
  ((SMqRspHead*)buf)->consumerId = consumerId;
L
Liu Jicong 已提交
181 182 183 184 185

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

  SEncoder encoder = {0};
  tEncoderInit(&encoder, abuf, len);
H
Haojun Liao 已提交
186 187 188 189

  if (type == TMQ_MSG_TYPE__POLL_RSP) {
    tEncodeSMqDataRsp(&encoder, pRsp);
  } else if (type == TMQ_MSG_TYPE__TAOSX_RSP) {
X
Xiaoyu Wang 已提交
190
    tEncodeSTaosxRsp(&encoder, (STaosxRsp*)pRsp);
H
Haojun Liao 已提交
191 192
  }

L
Liu Jicong 已提交
193 194 195
  tEncoderClear(&encoder);

  SRpcMsg rsp = {
H
Haojun Liao 已提交
196
      .info = *pRpcHandleInfo,
L
Liu Jicong 已提交
197 198 199 200 201 202 203 204 205
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };

  tmsgSendRsp(&rsp);
  return 0;
}

H
Haojun Liao 已提交
206 207 208 209
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 已提交
210

wmmhello's avatar
wmmhello 已提交
211 212
  char buf1[80] = {0};
  char buf2[80] = {0};
H
Haojun Liao 已提交
213 214 215 216
  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 已提交
217 218 219
  return 0;
}

H
Haojun Liao 已提交
220 221
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);
222 223 224 225 226 227

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

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

231 232 233
  return 0;
}

234
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
235
  STqOffset offset = {0};
X
Xiaoyu Wang 已提交
236
  int32_t   vgId = TD_VID(pTq->pVnode);
237

X
Xiaoyu Wang 已提交
238 239
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
240 241 242
  if (tDecodeSTqOffset(&decoder, &offset) < 0) {
    return -1;
  }
243

244 245
  tDecoderClear(&decoder);

wmmhello's avatar
wmmhello 已提交
246
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA || offset.val.type == TMQ_OFFSET__SNAPSHOT_META) {
L
Liu Jicong 已提交
247
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
H
Haojun Liao 已提交
248
            offset.subKey, vgId, offset.val.uid, offset.val.ts);
L
Liu Jicong 已提交
249
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
X
Xiaoyu Wang 已提交
250 251
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, offset.subKey, vgId,
            offset.val.version);
252
    if (offset.val.version + 1 == sversion) {
253 254
      offset.val.version += 1;
    }
255
  } else {
256 257
    tqError("invalid commit offset type:%d", offset.val.type);
    return -1;
258
  }
259 260 261 262

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

265
  // save the new offset value
266 267
  if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
    return -1;
268
  }
269 270

  if (offset.val.type == TMQ_OFFSET__LOG) {
271
    STqHandle* pHandle = taosHashGet(pTq->pHandle, offset.subKey, strlen(offset.subKey));
272 273
    if (pHandle && (walRefVer(pHandle->pRef, offset.val.version) < 0)) {
      return -1;
274 275 276
    }
  }

277 278 279
  return 0;
}

L
Liu Jicong 已提交
280
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
L
Liu Jicong 已提交
281
  void* pIter = NULL;
282

L
Liu Jicong 已提交
283
  while (1) {
284
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
285 286 287 288
    if (pIter == NULL) {
      break;
    }

289
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
290

L
Liu Jicong 已提交
291 292
    if (pCheck->ntbUid == tbUid) {
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
L
Liu Jicong 已提交
293
      for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
294 295
        int16_t forbidColId = *(int16_t*)taosArrayGet(pCheck->colIdList, i);
        if (forbidColId == colId) {
296
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
L
Liu Jicong 已提交
297 298 299 300 301
          return -1;
        }
      }
    }
  }
302

L
Liu Jicong 已提交
303 304 305
  return 0;
}

306
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
X
Xiaoyu Wang 已提交
307
  SMqPollReq req = {0};
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  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;
  }

327
  // 2. check re-balance status
328
  taosRLockLatch(&pTq->lock);
329 330 331 332
  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;
333
    taosRUnLockLatch(&pTq->lock);
334 335
    return -1;
  }
336
  taosRUnLockLatch(&pTq->lock);
337

338
  // 3. update the epoch value
339
  taosWLockLatch(&pTq->lock);
H
Haojun Liao 已提交
340 341
  int32_t savedEpoch = pHandle->epoch;
  if (savedEpoch < reqEpoch) {
X
Xiaoyu Wang 已提交
342 343
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, savedEpoch,
            reqEpoch);
344
    pHandle->epoch = reqEpoch;
H
Haojun Liao 已提交
345
  }
346
  taosWUnLockLatch(&pTq->lock);
347 348 349

  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
H
Haojun Liao 已提交
350 351
  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);
352

353
  return tqExtractDataForMq(pTq, pHandle, &req, pMsg);
354 355
}

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

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

361
  taosWLockLatch(&pTq->lock);
L
Liu Jicong 已提交
362 363 364 365
  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);
  }
366
  taosWUnLockLatch(&pTq->lock);
L
Liu Jicong 已提交
367

L
Liu Jicong 已提交
368 369
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
  if (pHandle) {
X
Xiaoyu Wang 已提交
370
    // walCloseRef(pHandle->pWalReader->pWal, pHandle->pRef->refId);
L
Liu Jicong 已提交
371 372 373 374 375 376 377
    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 已提交
378
  }
379

L
Liu Jicong 已提交
380 381
  code = tqOffsetDelete(pTq->pOffsetStore, pReq->subKey);
  if (code != 0) {
382
    tqError("cannot process tq delete req %s, since no such offset in cache", pReq->subKey);
L
Liu Jicong 已提交
383
  }
L
Liu Jicong 已提交
384

L
Liu Jicong 已提交
385
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
L
Liu Jicong 已提交
386
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
387
  }
L
Liu Jicong 已提交
388
  return 0;
L
Liu Jicong 已提交
389 390
}

391
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
392 393
  STqCheckInfo info = {0};
  SDecoder     decoder;
X
Xiaoyu Wang 已提交
394
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
395
  if (tDecodeSTqCheckInfo(&decoder, &info) < 0) {
L
Liu Jicong 已提交
396 397 398 399
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  tDecoderClear(&decoder);
400 401 402 403 404
  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 已提交
405 406 407 408 409 410
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

411
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
412 413 414 415 416 417 418 419 420 421 422
  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;
}

423
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
424
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
425
  tDecodeSMqRebVgReq(msg, &req);
L
Liu Jicong 已提交
426

427 428 429
  SVnode* pVnode = pTq->pVnode;
  int32_t vgId = TD_VID(pVnode);

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

433
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
L
Liu Jicong 已提交
434
  if (pHandle == NULL) {
L
Liu Jicong 已提交
435
    if (req.oldConsumerId != -1) {
436
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
437
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
L
Liu Jicong 已提交
438
    }
439

L
Liu Jicong 已提交
440
    if (req.newConsumerId == -1) {
441
      tqError("vgId:%d, tq invalid re-balance request, new consumerId %" PRId64 "", req.vgId, req.newConsumerId);
L
Liu Jicong 已提交
442
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
443 444
      return 0;
    }
445

L
Liu Jicong 已提交
446 447
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
448

H
Haojun Liao 已提交
449
    uint64_t oldConsumerId = pHandle->consumerId;
L
Liu Jicong 已提交
450 451 452
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
453

L
Liu Jicong 已提交
454
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
455
    pHandle->fetchMeta = req.withMeta;
wmmhello's avatar
wmmhello 已提交
456

457
    // TODO version should be assigned and refed during preprocess
458
    SWalRef* pRef = walRefCommittedVer(pVnode->pWal);
459
    if (pRef == NULL) {
H
Haojun Liao 已提交
460
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
461
      return -1;
462
    }
H
Haojun Liao 已提交
463

464 465
    int64_t ver = pRef->refVer;
    pHandle->pRef = pRef;
L
Liu Jicong 已提交
466

467
    SReadHandle handle = {
468
        .meta = pVnode->pMeta, .vnode = pVnode, .initTableReader = true, .initTqReader = true, .version = ver};
wmmhello's avatar
wmmhello 已提交
469
    pHandle->snapshotVer = ver;
470

L
Liu Jicong 已提交
471
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
472
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
473
      req.qmsg = NULL;
474

X
Xiaoyu Wang 已提交
475 476
      pHandle->execHandle.task = qCreateQueueExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle, vgId,
                                                          &pHandle->execHandle.numOfCols, req.newConsumerId);
L
Liu Jicong 已提交
477
      void* scanner = NULL;
478
      qExtractStreamScanner(pHandle->execHandle.task, &scanner);
479
      pHandle->execHandle.pTqReader = qExtractReaderFromStreamScanner(scanner);
L
Liu Jicong 已提交
480
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
481
      pHandle->pWalReader = walOpenReader(pVnode->pWal, NULL);
482
      pHandle->execHandle.pTqReader = tqOpenReader(pVnode);
483

L
Liu Jicong 已提交
484
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
485
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
486 487
      buildSnapContext(handle.meta, handle.version, 0, pHandle->execHandle.subType, pHandle->fetchMeta,
                       (SSnapContext**)(&handle.sContext));
488

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

L
Liu Jicong 已提交
494
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
495 496
      vnodeGetCtbIdList(pVnode, req.suid, tbUidList);
      tqDebug("vgId:%d, tq try to get all ctb, suid:%" PRId64, pVnode->config.vgId, req.suid);
L
Liu Jicong 已提交
497 498
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
499
        tqDebug("vgId:%d, idx %d, uid:%" PRId64, vgId, i, tbUid);
L
Liu Jicong 已提交
500
      }
501 502
      pHandle->execHandle.pTqReader = tqOpenReader(pVnode);
      tqReaderSetTbUidList(pHandle->execHandle.pTqReader, tbUidList);
L
Liu Jicong 已提交
503
      taosArrayDestroy(tbUidList);
wmmhello's avatar
wmmhello 已提交
504

L
Liu Jicong 已提交
505 506
      buildSnapContext(handle.meta, handle.version, req.suid, pHandle->execHandle.subType, pHandle->fetchMeta,
                       (SSnapContext**)(&handle.sContext));
507
      pHandle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &handle, vgId, NULL, req.newConsumerId);
L
Liu Jicong 已提交
508
    }
H
Haojun Liao 已提交
509

510
    taosHashPut(pTq->pHandle, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
511 512
    tqDebug("try to persist handle %s consumer:0x%" PRIx64 " , old consumer:0x%" PRIx64, req.subKey,
            pHandle->consumerId, oldConsumerId);
L
Liu Jicong 已提交
513
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
H
Haojun Liao 已提交
514
      taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
515
      return -1;
L
Liu Jicong 已提交
516
    }
L
Liu Jicong 已提交
517
  } else {
518 519 520 521
    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 已提交
522
      taosMemoryFree(req.qmsg);
523
      return tqMetaSaveHandle(pTq, req.subKey, pHandle);
524 525 526
    } else {
      tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
             req.newConsumerId);
527

528 529 530
      // kill executing task
      qTaskInfo_t pTaskInfo = pHandle->execHandle.task;
      if (pTaskInfo != NULL) {
531
        qKillTask(pTaskInfo, TSDB_CODE_SUCCESS);
532 533
      }

534 535
      taosWLockLatch(&pTq->lock);
      atomic_store_32(&pHandle->epoch, -1);
536

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

540 541
      atomic_store_64(&pHandle->consumerId, req.newConsumerId);
      atomic_add_fetch_32(&pHandle->epoch, 1);
542

543
      if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
544 545 546
        qStreamCloseTsdbReader(pTaskInfo);
      }

547 548 549 550 551
      taosWUnLockLatch(&pTq->lock);
      if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
        taosMemoryFree(req.qmsg);
        return -1;
      }
L
Liu Jicong 已提交
552
    }
L
Liu Jicong 已提交
553
  }
L
Liu Jicong 已提交
554

H
Haojun Liao 已提交
555
  taosMemoryFree(req.qmsg);
L
Liu Jicong 已提交
556
  return 0;
L
Liu Jicong 已提交
557
}
558

559
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask, int64_t ver) {
560
  int32_t vgId = TD_VID(pTq->pVnode);
561
  pTask->id.idStr = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId);
L
Liu Jicong 已提交
562
  pTask->refCnt = 1;
563
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
564 565
  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
L
Liu Jicong 已提交
566 567

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
L
Liu Jicong 已提交
568
    return -1;
L
Liu Jicong 已提交
569 570
  }

L
Liu Jicong 已提交
571 572
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;
573
  pTask->pMsgCb = &pTq->pVnode->msgCb;
574
  pTask->pMeta = pTq->pStreamMeta;
575
  pTask->chkInfo.version = ver;
576

577
  // expand executor
578
  if (pTask->fillHistory) {
579
    pTask->status.taskStatus = TASK_STATUS__WAIT_DOWNSTREAM;
580
  } else {
581
    pTask->status.taskStatus = TASK_STATUS__RESTORE;
582 583
  }

584
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
585
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
586 587 588 589
    if (pTask->pState == NULL) {
      return -1;
    }

590
    SReadHandle handle = {
591
        .meta = pTq->pVnode->pMeta, .vnode = pTq->pVnode, .initTqReader = 1, .pStateBackend = pTask->pState};
592

593 594
    pTask->exec.pExecutor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle, vgId);
    if (pTask->exec.pExecutor == NULL) {
L
Liu Jicong 已提交
595 596
      return -1;
    }
597

598
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
599
    pTask->pState = streamStateOpen(pTq->pStreamMeta->path, pTask, false, -1, -1);
600 601 602
    if (pTask->pState == NULL) {
      return -1;
    }
603

604 605 606 607 608
    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 已提交
609 610
      return -1;
    }
L
Liu Jicong 已提交
611
  }
L
Liu Jicong 已提交
612 613

  // sink
L
Liu Jicong 已提交
614
  /*pTask->ahandle = pTq->pVnode;*/
615
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
616
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
617
    pTask->smaSink.smaSink = smaHandleRes;
618
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
619
    pTask->tbSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
620
    pTask->tbSink.tbSinkFunc = tqSinkToTablePipeline2;
L
Liu Jicong 已提交
621

X
Xiaoyu Wang 已提交
622
    int32_t   ver1 = 1;
5
54liuyao 已提交
623
    SMetaInfo info = {0};
dengyihao's avatar
dengyihao 已提交
624
    int32_t   code = metaGetInfo(pTq->pVnode->pMeta, pTask->tbSink.stbUid, &info, NULL);
5
54liuyao 已提交
625
    if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
626
      ver1 = info.skmVer;
5
54liuyao 已提交
627
    }
L
Liu Jicong 已提交
628

629 630
    SSchemaWrapper* pschemaWrapper = pTask->tbSink.pSchemaWrapper;
    pTask->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
wmmhello's avatar
wmmhello 已提交
631
    if(pTask->tbSink.pTSchema == NULL) {
wmmhello's avatar
wmmhello 已提交
632
      return -1;
wmmhello's avatar
wmmhello 已提交
633
    }
L
Liu Jicong 已提交
634
  }
635

636
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
637
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, NULL);
638 639
  }

640
  streamSetupTrigger(pTask);
641
  tqInfo("vgId:%d expand stream task, s-task:%s, checkpoint ver:%" PRId64 " child id:%d, level:%d", vgId, pTask->id.idStr,
642
         pTask->chkInfo.version, pTask->selfChildId, pTask->taskLevel);
643 644 645

  // next valid version will add one
  pTask->chkInfo.version += 1;
L
Liu Jicong 已提交
646
  return 0;
L
Liu Jicong 已提交
647
}
L
Liu Jicong 已提交
648

649 650 651 652 653 654
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 已提交
655
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
656 657 658 659 660 661 662 663 664 665 666 667
  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,
  };
668

L
Liu Jicong 已提交
669
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
670 671 672 673 674 675 676 677
  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);
678 679
  } else {
    rsp.status = 0;
680 681 682 683
    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);
684 685 686 687 688 689 690
  }

  SEncoder encoder;
  int32_t  code;
  int32_t  len;
  tEncodeSize(tEncodeSStreamTaskCheckRsp, &rsp, len, code);
  if (code < 0) {
L
Liu Jicong 已提交
691
    tqError("unable to encode rsp %d", __LINE__);
L
Liu Jicong 已提交
692
    return -1;
693
  }
L
Liu Jicong 已提交
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);

703
  SRpcMsg rspMsg = { .code = 0, .pCont = buf, .contLen = sizeof(SMsgHead) + len, .info = pMsg->info };
704 705 706 707
  tmsgSendRsp(&rspMsg);
  return 0;
}

708
int32_t tqProcessStreamTaskCheckRsp(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
709 710 711 712 713 714 715 716 717 718 719
  int32_t             code;
  SStreamTaskCheckRsp rsp;

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

720
  tDecoderClear(&decoder);
721
  tqDebug("tq recv task check rsp(reqId:0x%" PRIx64 ") %d at node %d check req from task %d at node %d, status %d",
722 723
          rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.upstreamTaskId, rsp.upstreamNodeId, rsp.status);

L
Liu Jicong 已提交
724
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, rsp.upstreamTaskId);
725 726 727 728
  if (pTask == NULL) {
    return -1;
  }

729
  code = streamProcessTaskCheckRsp(pTask, &rsp, sversion);
L
Liu Jicong 已提交
730 731
  streamMetaReleaseTask(pTq->pStreamMeta, pTask);
  return code;
732 733
}

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

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

750 751
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
752
  code = tDecodeStreamTask(&decoder, pTask);
753 754 755 756 757
  if (code < 0) {
    tDecoderClear(&decoder);
    taosMemoryFree(pTask);
    return -1;
  }
758

759 760
  tDecoderClear(&decoder);

761
  // 2.save task, use the newest commit version as the initial start version of stream task.
762
  code = streamMetaAddDeployedTask(pTq->pStreamMeta, sversion, pTask);
763
  if (code < 0) {
764 765
    tqError("vgId:%d failed to add s-task:%s, total:%d", TD_VID(pTq->pVnode), pTask->id.idStr,
            streamMetaGetNumOfTasks(pTq->pStreamMeta));
766 767 768 769 770
    return -1;
  }

  // 3.go through recover steps to fill history
  if (pTask->fillHistory) {
771
    streamTaskCheckDownstream(pTask, sversion);
772 773
  }

774 775
  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));
776 777 778
  return 0;
}

L
Liu Jicong 已提交
779 780 781 782 783
int32_t tqProcessTaskRecover1Req(STQ* pTq, SRpcMsg* pMsg) {
  int32_t code;
  char*   msg = pMsg->pCont;
  int32_t msgLen = pMsg->contLen;

784
  SStreamRecoverStep1Req* pReq = (SStreamRecoverStep1Req*)msg;
L
Liu Jicong 已提交
785
  SStreamTask*            pTask = streamMetaAcquireTask(pTq->pStreamMeta, pReq->taskId);
786 787 788 789 790
  if (pTask == NULL) {
    return -1;
  }

  // check param
791
  int64_t fillVer1 = pTask->chkInfo.version;
792
  if (fillVer1 <= 0) {
L
Liu Jicong 已提交
793
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
794 795 796 797 798 799
    return -1;
  }

  // do recovery step 1
  streamSourceRecoverScanStep1(pTask);

800
  if (atomic_load_8(&pTask->status.taskStatus) == TASK_STATUS__DROPPING) {
L
Liu Jicong 已提交
801 802 803 804
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
    return 0;
  }

805 806 807 808
  // build msg to launch next step
  SStreamRecoverStep2Req req;
  code = streamBuildSourceRecover2Req(pTask, &req);
  if (code < 0) {
L
Liu Jicong 已提交
809
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
810 811 812
    return -1;
  }

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

815
  if (atomic_load_8(&pTask->status.taskStatus) == TASK_STATUS__DROPPING) {
L
Liu Jicong 已提交
816 817 818
    return 0;
  }

819
  // serialize msg
L
Liu Jicong 已提交
820 821 822 823 824 825 826 827
  int32_t len = sizeof(SStreamRecoverStep1Req);

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

  memcpy(serializedReq, &req, len);
828 829 830 831 832

  // dispatch msg
  SRpcMsg rpcMsg = {
      .code = 0,
      .contLen = len,
L
Liu Jicong 已提交
833
      .msgType = TDMT_VND_STREAM_RECOVER_BLOCKING_STAGE,
L
Liu Jicong 已提交
834
      .pCont = serializedReq,
835 836 837 838 839 840 841
  };

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

  return 0;
}

842
int32_t tqProcessTaskRecover2Req(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
843 844
  int32_t                 code;
  SStreamRecoverStep2Req* pReq = (SStreamRecoverStep2Req*)msg;
L
Liu Jicong 已提交
845
  SStreamTask*            pTask = streamMetaAcquireTask(pTq->pStreamMeta, pReq->taskId);
846 847 848 849 850
  if (pTask == NULL) {
    return -1;
  }

  // do recovery step 2
851
  code = streamSourceRecoverScanStep2(pTask, sversion);
852
  if (code < 0) {
L
Liu Jicong 已提交
853
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
854 855 856
    return -1;
  }

857
  if (atomic_load_8(&pTask->status.taskStatus) == TASK_STATUS__DROPPING) {
L
Liu Jicong 已提交
858 859 860 861
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
    return 0;
  }

862 863 864
  // restore param
  code = streamRestoreParam(pTask);
  if (code < 0) {
L
Liu Jicong 已提交
865
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
866 867 868 869 870 871
    return -1;
  }

  // set status normal
  code = streamSetStatusNormal(pTask);
  if (code < 0) {
L
Liu Jicong 已提交
872
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
873 874 875 876 877 878
    return -1;
  }

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

L
Liu Jicong 已提交
883 884 885
  atomic_store_8(&pTask->fillHistory, 0);
  streamMetaSaveTask(pTq->pStreamMeta, pTask);

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

888 889 890
  return 0;
}

L
Liu Jicong 已提交
891 892 893
int32_t tqProcessTaskRecoverFinishReq(STQ* pTq, SRpcMsg* pMsg) {
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
894 895

  // deserialize
896 897 898
  SStreamRecoverFinishReq req;

  SDecoder decoder;
X
Xiaoyu Wang 已提交
899
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
900 901 902
  tDecodeSStreamRecoverFinishReq(&decoder, &req);
  tDecoderClear(&decoder);

903
  // find task
L
Liu Jicong 已提交
904
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, req.taskId);
905 906 907
  if (pTask == NULL) {
    return -1;
  }
908
  // do process request
909
  if (streamProcessRecoverFinishReq(pTask, req.childId) < 0) {
L
Liu Jicong 已提交
910
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
911 912 913
    return -1;
  }

L
Liu Jicong 已提交
914
  streamMetaReleaseTask(pTq->pStreamMeta, pTask);
915
  return 0;
L
Liu Jicong 已提交
916
}
L
Liu Jicong 已提交
917

L
Liu Jicong 已提交
918 919 920 921 922
int32_t tqProcessTaskRecoverFinishRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}

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

959 960 961
    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 已提交
962 963
  }

L
Liu Jicong 已提交
964 965
  taosArrayDestroy(pRes->uidList);

L
Liu Jicong 已提交
966 967 968
  int32_t* pRef = taosMemoryMalloc(sizeof(int32_t));
  *pRef = 1;

L
Liu Jicong 已提交
969 970 971 972 973 974 975
  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;

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

L
Liu Jicong 已提交
978
    if (!failed) {
S
Shengliang Guan 已提交
979
      SStreamRefDataBlock* pRefBlock = taosAllocateQitem(sizeof(SStreamRefDataBlock), DEF_QITEM, 0);
L
Liu Jicong 已提交
980 981 982 983 984
      pRefBlock->type = STREAM_INPUT__REF_DATA_BLOCK;
      pRefBlock->pBlock = pDelBlock;
      pRefBlock->dataRef = pRef;
      atomic_add_fetch_32(pRefBlock->dataRef, 1);

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

L
Liu Jicong 已提交
988
        atomic_sub_fetch_32(pRef, 1);
L
Liu Jicong 已提交
989
        taosFreeQitem(pRefBlock);
L
Liu Jicong 已提交
990 991
        continue;
      }
L
Liu Jicong 已提交
992

L
Liu Jicong 已提交
993
      if (streamSchedExec(pTask) < 0) {
994
        qError("stream task launch failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
995 996
        continue;
      }
L
Liu Jicong 已提交
997

L
Liu Jicong 已提交
998 999 1000 1001
    } else {
      streamTaskInputFail(pTask);
    }
  }
L
Liu Jicong 已提交
1002

L
Liu Jicong 已提交
1003
  int32_t ref = atomic_sub_fetch_32(pRef, 1);
L
Liu Jicong 已提交
1004
  /*A(ref >= 0);*/
L
Liu Jicong 已提交
1005
  if (ref == 0) {
L
Liu Jicong 已提交
1006
    blockDataDestroy(pDelBlock);
L
Liu Jicong 已提交
1007 1008 1009 1010
    taosMemoryFree(pRef);
  }

#if 0
S
Shengliang Guan 已提交
1011
    SStreamDataBlock* pStreamBlock = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
L
Liu Jicong 已提交
1012 1013 1014 1015 1016 1017 1018 1019
    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) {
1020
      if (tAppendDataToInputQueue(pTask, (SStreamQueueItem*)pStreamBlock) < 0) {
1021
        qError("stream task input del failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
1022 1023 1024 1025
        continue;
      }

      if (streamSchedExec(pTask) < 0) {
1026
        qError("stream task launch failed, task id %d", pTask->id.taskId);
L
Liu Jicong 已提交
1027 1028 1029 1030 1031 1032
        continue;
      }
    } else {
      streamTaskInputFail(pTask);
    }
  }
L
Liu Jicong 已提交
1033
  blockDataDestroy(pDelBlock);
L
Liu Jicong 已提交
1034
#endif
L
Liu Jicong 已提交
1035 1036 1037 1038

  return 0;
}

1039 1040 1041
static int32_t addSubmitBlockNLaunchTask(STqOffsetStore* pOffsetStore, SStreamTask* pTask, SStreamDataSubmit2* pSubmit,
                                         const char* key, int64_t ver) {
  doSaveTaskOffset(pOffsetStore, key, ver);
1042
  int32_t code = tqAddInputBlockNLaunchTask(pTask, (SStreamQueueItem*)pSubmit, ver);
1043 1044 1045 1046 1047

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

  return code;
1050 1051
}

L
Liu Jicong 已提交
1052
int32_t tqProcessSubmitReq(STQ* pTq, SPackedData submit) {
1053
#if 0
1054
  void* pIter = NULL;
1055
  SStreamDataSubmit2* pSubmit = streamDataSubmitNew(submit, STREAM_INPUT__DATA_SUBMIT);
L
Liu Jicong 已提交
1056
  if (pSubmit == NULL) {
L
Liu Jicong 已提交
1057
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1058
    tqError("failed to create data submit for stream since out of memory");
1059
    saveOffsetForAllTasks(pTq, submit.ver);
1060
    return -1;
L
Liu Jicong 已提交
1061 1062
  }

1063 1064
  SArray* pInputQueueFullTasks = taosArrayInit(4, POINTER_BYTES);

L
Liu Jicong 已提交
1065
  while (1) {
L
Liu Jicong 已提交
1066
    pIter = taosHashIterate(pTq->pStreamMeta->pTasks, pIter);
1067 1068 1069 1070
    if (pIter == NULL) {
      break;
    }

1071
    SStreamTask* pTask = *(SStreamTask**)pIter;
1072 1073 1074 1075
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) {
      continue;
    }

1076
    if (pTask->status.taskStatus == TASK_STATUS__RECOVER_PREPARE || pTask->status.taskStatus == TASK_STATUS__WAIT_DOWNSTREAM) {
1077
      tqDebug("stream task:%d skip push data, not ready for processing, status %d", pTask->id.taskId,
1078
              pTask->status.taskStatus);
L
Liu Jicong 已提交
1079 1080
      continue;
    }
L
Liu Jicong 已提交
1081

1082 1083 1084
    // check if offset value exists
    char key[128] = {0};
    createStreamTaskOffsetKey(key, pTask->id.streamId, pTask->id.taskId);
1085

1086 1087 1088 1089 1090 1091 1092 1093
    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 已提交
1094 1095
      }

1096 1097
      tqDebug("s-task:%s input queue is full, discard submit block, ver:%" PRId64, pTask->id.idStr, ver);
      taosArrayPush(pInputQueueFullTasks, &pTask);
1098 1099 1100 1101 1102
      continue;
    }

    // check if offset value exists
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, key);
1103
    ASSERT(pOffset == NULL);
1104

1105
    addSubmitBlockNLaunchTask(pTq->pOffsetStore, pTask, pSubmit, key, submit.ver);
L
Liu Jicong 已提交
1106 1107
  }

1108 1109
  streamDataSubmitDestroy(pSubmit);
  taosFreeQitem(pSubmit);
1110
#endif
L
Liu Jicong 已提交
1111

1112
  tqStartStreamTasks(pTq);
1113
  return 0;
L
Liu Jicong 已提交
1114 1115
}

L
Liu Jicong 已提交
1116 1117
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRunReq* pReq = pMsg->pCont;
1118 1119 1120 1121

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

1122 1123
  if (taskId == WAL_READ_TASKS_ID) {  // all tasks are extracted submit data from the wal
    tqStreamTasksScanWal(pTq);
L
Liu Jicong 已提交
1124
    return 0;
1125
  }
1126

1127
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
1128 1129 1130 1131 1132 1133 1134 1135
  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);
1136
    } else {
1137
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state", vgId, pTask->id.idStr);
1138
    }
1139 1140 1141 1142 1143 1144 1145

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

L
Liu Jicong 已提交
1149
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
1150 1151 1152 1153 1154
  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 已提交
1155
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
1156
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
1157

1158
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, req.taskId);
L
Liu Jicong 已提交
1159
  if (pTask) {
1160
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1161
    streamProcessDispatchReq(pTask, &req, &rsp, exec);
L
Liu Jicong 已提交
1162
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1163
    return 0;
1164
  } else {
L
liuyao 已提交
1165
    tDeleteStreamDispatchReq(&req);
1166
    return -1;
L
Liu Jicong 已提交
1167
  }
L
Liu Jicong 已提交
1168 1169
}

L
Liu Jicong 已提交
1170 1171
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
1172
  int32_t             taskId = ntohl(pRsp->upstreamTaskId);
L
Liu Jicong 已提交
1173
  SStreamTask*        pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
1174
  tqDebug("recv dispatch rsp, code:%x", pMsg->code);
L
Liu Jicong 已提交
1175
  if (pTask) {
1176
    streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
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
}
L
Liu Jicong 已提交
1183

1184
int32_t tqProcessTaskDropReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
1185
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
1186
  streamMetaRemoveTask(pTq->pStreamMeta, pReq->taskId);
L
Liu Jicong 已提交
1187
  return 0;
L
Liu Jicong 已提交
1188
}
L
Liu Jicong 已提交
1189 1190 1191 1192 1193 1194 1195

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;
1196
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
L
Liu Jicong 已提交
1197
  tDecodeStreamRetrieveReq(&decoder, &req);
L
Liu Jicong 已提交
1198
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1199
  int32_t      taskId = req.dstTaskId;
L
Liu Jicong 已提交
1200
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1201
  if (pTask) {
1202
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1203
    streamProcessRetrieveReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
1204
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1205
    tDeleteStreamRetrieveReq(&req);
L
Liu Jicong 已提交
1206
    return 0;
L
Liu Jicong 已提交
1207
  } else {
L
liuyao 已提交
1208
    tDeleteStreamRetrieveReq(&req);
L
Liu Jicong 已提交
1209
    return -1;
L
Liu Jicong 已提交
1210 1211 1212 1213 1214 1215 1216
  }
}

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

1218 1219 1220 1221 1222 1223
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 已提交
1224 1225 1226

  SStreamDispatchReq req;
  SDecoder           decoder;
1227
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
L
Liu Jicong 已提交
1228 1229
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
    code = TSDB_CODE_MSG_DECODE_ERROR;
L
Liu Jicong 已提交
1230
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
1231 1232
    goto FAIL;
  }
L
Liu Jicong 已提交
1233
  tDecoderClear(&decoder);
L
Liu Jicong 已提交
1234

L
Liu Jicong 已提交
1235
  int32_t taskId = req.taskId;
L
Liu Jicong 已提交
1236

L
Liu Jicong 已提交
1237
  SStreamTask* pTask = streamMetaAcquireTask(pTq->pStreamMeta, taskId);
L
Liu Jicong 已提交
1238
  if (pTask) {
1239
    SRpcMsg rsp = { .info = pMsg->info, .code = 0 };
L
Liu Jicong 已提交
1240
    streamProcessDispatchReq(pTask, &req, &rsp, false);
L
Liu Jicong 已提交
1241
    streamMetaReleaseTask(pTq->pStreamMeta, pTask);
L
Liu Jicong 已提交
1242 1243
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
1244
    return 0;
5
54liuyao 已提交
1245 1246
  } else {
    tDeleteStreamDispatchReq(&req);
L
Liu Jicong 已提交
1247
  }
L
Liu Jicong 已提交
1248

1249 1250
  code = TSDB_CODE_STREAM_TASK_NOT_EXIST;

L
Liu Jicong 已提交
1251
FAIL:
1252 1253 1254 1255
  if (pMsg->info.handle == NULL) return -1;

  SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
  if (pRspHead == NULL) {
1256
    SRpcMsg rsp = { .code = TSDB_CODE_OUT_OF_MEMORY, .info = pMsg->info };
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
    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 已提交
1273
  SRpcMsg rsp = {
1274
      .code = code, .info = pMsg->info, .contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp), .pCont = pRspHead};
1275
  tqDebug("send dispatch error rsp, code: %x", code);
L
Liu Jicong 已提交
1276
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
1277 1278
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
1279
  return -1;
L
Liu Jicong 已提交
1280
}
L
Liu Jicong 已提交
1281

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

1284
int32_t tqStartStreamTasks(STQ* pTq) {
1285 1286
  int32_t vgId = TD_VID(pTq->pVnode);

1287 1288
  SStreamMeta* pMeta = pTq->pStreamMeta;
  taosWLockLatch(&pMeta->lock);
1289 1290 1291 1292 1293 1294 1295
  int32_t numOfTasks = taosHashGetSize(pTq->pStreamMeta->pTasks);
  if (numOfTasks == 0) {
    tqInfo("vgId:%d no stream tasks exists", vgId);
    taosWUnLockLatch(&pTq->pStreamMeta->lock);
    return 0;
  }

1296 1297 1298 1299 1300 1301 1302 1303
  pMeta->walScan += 1;

  if (pMeta->walScan > 1) {
    tqDebug("vgId:%d wal read task has been launched, remain scan times:%d", vgId, pMeta->walScan);
    taosWUnLockLatch(&pTq->pStreamMeta->lock);
    return 0;
  }

1304 1305 1306 1307
  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));
1308
    taosWUnLockLatch(&pTq->pStreamMeta->lock);
1309 1310 1311
    return -1;
  }

1312
  tqInfo("vgId:%d start wal scan stream tasks, tasks:%d", vgId, numOfTasks);
1313 1314
  initOffsetForAllRestoreTasks(pTq);

1315 1316
  pRunReq->head.vgId = vgId;
  pRunReq->streamId = 0;
1317
  pRunReq->taskId = WAL_READ_TASKS_ID;
1318 1319 1320

  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_RUN, .pCont = pRunReq, .contLen = sizeof(SStreamTaskRunReq)};
  tmsgPutToQueue(&pTq->pVnode->msgCb, STREAM_QUEUE, &msg);
1321
  taosWUnLockLatch(&pTq->pStreamMeta->lock);
1322 1323 1324

  return 0;
}