tq.c 14.1 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
S
Shengliang Guan 已提交
14 15
 */

H
Hongze Cheng 已提交
16
#include "tq.h"
S
Shengliang Guan 已提交
17

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

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

36
void tqCleanUp() {
L
Liu Jicong 已提交
37 38 39 40 41 42 43 44 45 46
  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);
    atomic_store_8(&tqMgmt.inited, 0);
  }
47
}
L
Liu Jicong 已提交
48

L
Liu Jicong 已提交
49
STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal) {
wafwerar's avatar
wafwerar 已提交
50
  STQ* pTq = taosMemoryMalloc(sizeof(STQ));
L
Liu Jicong 已提交
51
  if (pTq == NULL) {
L
Liu Jicong 已提交
52
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
53 54
    return NULL;
  }
H
Hongze Cheng 已提交
55
  pTq->path = strdup(path);
L
Liu Jicong 已提交
56
  pTq->pVnode = pVnode;
L
Liu Jicong 已提交
57
  pTq->pWal = pWal;
58

L
Liu Jicong 已提交
59
  pTq->handles = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
60

L
Liu Jicong 已提交
61 62
  pTq->pStreamTasks = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);

L
Liu Jicong 已提交
63 64
  pTq->pushMgr = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);

L
Liu Jicong 已提交
65
  if (tqMetaOpen(pTq) < 0) {
66 67 68
    ASSERT(0);
  }

L
Liu Jicong 已提交
69 70
  return pTq;
}
L
Liu Jicong 已提交
71

L
Liu Jicong 已提交
72
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
73
  if (pTq) {
wafwerar's avatar
wafwerar 已提交
74
    taosMemoryFreeClear(pTq->path);
L
Liu Jicong 已提交
75
    taosHashCleanup(pTq->handles);
L
Liu Jicong 已提交
76 77
    taosHashCleanup(pTq->pStreamTasks);
    taosHashCleanup(pTq->pushMgr);
L
Liu Jicong 已提交
78
    tqMetaClose(pTq);
wafwerar's avatar
wafwerar 已提交
79
    taosMemoryFree(pTq);
H
Hongze Cheng 已提交
80
  }
L
Liu Jicong 已提交
81 82
  // TODO
}
L
Liu Jicong 已提交
83

L
fix  
Liu Jicong 已提交
84
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) {
L
Liu Jicong 已提交
85 86
  SMqPollReq* pReq = pMsg->pCont;
  int64_t     consumerId = pReq->consumerId;
L
Liu Jicong 已提交
87
  int64_t     timeout = pReq->timeout;
L
Liu Jicong 已提交
88 89
  int32_t     reqEpoch = pReq->epoch;
  int64_t     fetchOffset;
90

L
Liu Jicong 已提交
91
  // get offset to fetch message
L
Liu Jicong 已提交
92
  if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__EARLIEAST) {
L
Liu Jicong 已提交
93
    fetchOffset = walGetFirstVer(pTq->pWal);
L
Liu Jicong 已提交
94
  } else if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__LATEST) {
L
Liu Jicong 已提交
95
    fetchOffset = walGetCommittedVer(pTq->pWal);
L
Liu Jicong 已提交
96 97 98 99
  } else {
    fetchOffset = pReq->currentOffset + 1;
  }

H
Hongze Cheng 已提交
100 101
  tqDebug("tmq poll: consumer %ld (epoch %d) recv poll req in vg %d, req %ld %ld", consumerId, pReq->epoch,
          TD_VID(pTq->pVnode), pReq->currentOffset, fetchOffset);
L
fix  
Liu Jicong 已提交
102

L
Liu Jicong 已提交
103 104
  STqHandle* pHandle = taosHashGet(pTq->handles, pReq->subKey, strlen(pReq->subKey));
  ASSERT(pHandle);
L
Liu Jicong 已提交
105

L
Liu Jicong 已提交
106
  int32_t consumerEpoch = atomic_load_32(&pHandle->epoch);
L
Liu Jicong 已提交
107
  while (consumerEpoch < reqEpoch) {
L
Liu Jicong 已提交
108
    consumerEpoch = atomic_val_compare_exchange_32(&pHandle->epoch, consumerEpoch, reqEpoch);
L
Liu Jicong 已提交
109 110
  }

111 112 113 114 115
  SWalHead* pHeadWithCkSum = taosMemoryMalloc(sizeof(SWalHead) + 2048);
  if (pHeadWithCkSum == NULL) {
    return -1;
  }

L
Liu Jicong 已提交
116
  walSetReaderCapacity(pHandle->pWalReader, 2048);
117

L
Liu Jicong 已提交
118
  SMqDataBlkRsp rsp = {0};
L
Liu Jicong 已提交
119
  rsp.reqOffset = pReq->currentOffset;
L
Liu Jicong 已提交
120

L
Liu Jicong 已提交
121
  rsp.blockData = taosArrayInit(0, sizeof(void*));
L
Liu Jicong 已提交
122
  rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t));
123

L
Liu Jicong 已提交
124
  rsp.withTbName = pReq->withTbName;
125 126 127
  if (rsp.withTbName) {
    rsp.blockTbName = taosArrayInit(0, sizeof(void*));
  }
L
Liu Jicong 已提交
128 129 130 131 132 133
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
    rsp.withSchema = false;
    rsp.withTag = false;
  } else {
    rsp.withSchema = true;
    rsp.withTag = false;
L
Liu Jicong 已提交
134
    rsp.blockSchema = taosArrayInit(0, sizeof(void*));
135
  }
L
Liu Jicong 已提交
136

137
  while (1) {
L
Liu Jicong 已提交
138
    consumerEpoch = atomic_load_32(&pHandle->epoch);
L
Liu Jicong 已提交
139
    if (consumerEpoch > reqEpoch) {
L
Liu Jicong 已提交
140 141
      tqWarn("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, found new consumer epoch %d, discard req epoch %d",
             consumerId, pReq->epoch, TD_VID(pTq->pVnode), fetchOffset, consumerEpoch, reqEpoch);
L
Liu Jicong 已提交
142 143
      break;
    }
L
Liu Jicong 已提交
144

L
Liu Jicong 已提交
145 146
    if (tqFetchLog(pTq, pHandle, &fetchOffset, &pHeadWithCkSum) < 0) {
      // TODO add push mgr
147 148 149 150 151
      break;
    }

    SWalReadHead* pHead = &pHeadWithCkSum->head;

H
Hongze Cheng 已提交
152 153
    tqDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d offset %ld msgType %d", consumerId, pReq->epoch,
            TD_VID(pTq->pVnode), fetchOffset, pHead->msgType);
L
Liu Jicong 已提交
154

L
fix  
Liu Jicong 已提交
155 156
    if (pHead->msgType == TDMT_VND_SUBMIT) {
      SSubmitReq* pCont = (SSubmitReq*)&pHead->body;
L
Liu Jicong 已提交
157 158 159 160 161

      tqDataExec(pTq, &pHandle->execHandle, pCont, &rsp, workerId);
    } else {
      // TODO
      ASSERT(0);
L
Liu Jicong 已提交
162
    }
163

L
Liu Jicong 已提交
164 165
    // TODO batch optimization:
    // TODO continue scan until meeting batch requirement
L
Liu Jicong 已提交
166 167
    if (rsp.blockNum > 0 /* threshold */) {
      break;
L
Liu Jicong 已提交
168 169
    } else {
      fetchOffset++;
L
Liu Jicong 已提交
170
    }
L
Liu Jicong 已提交
171
  }
172

L
Liu Jicong 已提交
173
  taosMemoryFree(pHeadWithCkSum);
L
Liu Jicong 已提交
174

L
Liu Jicong 已提交
175 176
  ASSERT(taosArrayGetSize(rsp.blockData) == rsp.blockNum);
  ASSERT(taosArrayGetSize(rsp.blockDataLen) == rsp.blockNum);
L
Liu Jicong 已提交
177 178 179
  if (rsp.withSchema) {
    ASSERT(taosArrayGetSize(rsp.blockSchema) == rsp.blockNum);
  }
L
fix  
Liu Jicong 已提交
180

L
Liu Jicong 已提交
181
  rsp.rspOffset = fetchOffset;
182

L
Liu Jicong 已提交
183
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqDataBlkRsp(NULL, &rsp);
184 185 186 187 188
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    pMsg->code = -1;
    return -1;
  }
L
Liu Jicong 已提交
189

L
Liu Jicong 已提交
190 191
  ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
  ((SMqRspHead*)buf)->epoch = pReq->epoch;
L
Liu Jicong 已提交
192
  ((SMqRspHead*)buf)->consumerId = consumerId;
193

L
Liu Jicong 已提交
194
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
L
Liu Jicong 已提交
195
  tEncodeSMqDataBlkRsp(&abuf, &rsp);
dengyihao's avatar
dengyihao 已提交
196

L
Liu Jicong 已提交
197 198 199 200 201 202
  SRpcMsg resp = {
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
dengyihao's avatar
dengyihao 已提交
203
  tmsgSendRsp(&resp);
L
Liu Jicong 已提交
204

H
Hongze Cheng 已提交
205 206
  tqDebug("vg %d offset %ld from consumer %ld (epoch %d) send rsp, block num: %d, reqOffset: %ld, rspOffset: %ld",
          TD_VID(pTq->pVnode), fetchOffset, consumerId, pReq->epoch, rsp.blockNum, rsp.reqOffset, rsp.rspOffset);
L
Liu Jicong 已提交
207

L
Liu Jicong 已提交
208
  // TODO wrap in destroy func
L
Liu Jicong 已提交
209 210
  taosArrayDestroy(rsp.blockData);
  taosArrayDestroy(rsp.blockDataLen);
211 212 213 214 215 216 217 218

  if (rsp.withSchema) {
    taosArrayDestroyP(rsp.blockSchema, (FDelete)tDeleteSSchemaWrapper);
  }

  if (rsp.withTbName) {
    taosArrayDestroyP(rsp.blockTbName, (FDelete)taosMemoryFree);
  }
L
Liu Jicong 已提交
219

220 221
  return 0;
}
L
Liu Jicong 已提交
222

L
Liu Jicong 已提交
223 224
int32_t tqProcessVgDeleteReq(STQ* pTq, char* msg, int32_t msgLen) {
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
L
Liu Jicong 已提交
225

L
Liu Jicong 已提交
226
  int32_t code = taosHashRemove(pTq->handles, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
227
  ASSERT(code == 0);
228

L
Liu Jicong 已提交
229
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
230 231
    ASSERT(0);
  }
L
Liu Jicong 已提交
232
  return 0;
L
Liu Jicong 已提交
233 234
}

L
Liu Jicong 已提交
235 236
// TODO: persist meta into tdb
int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
237
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
238 239
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
L
Liu Jicong 已提交
240 241
  STqHandle* pHandle = taosHashGet(pTq->handles, req.subKey, strlen(req.subKey));
  if (pHandle == NULL) {
L
Liu Jicong 已提交
242 243
    ASSERT(req.oldConsumerId == -1);
    ASSERT(req.newConsumerId != -1);
L
Liu Jicong 已提交
244 245
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
246 247
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
248 249 250
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
251

L
Liu Jicong 已提交
252 253 254 255
    pHandle->execHandle.subType = req.subType;
    /*pExec->withTbName = req.withTbName;*/
    /*pExec->withSchema = req.withSchema;*/
    /*pExec->withTag = req.withTag;*/
L
Liu Jicong 已提交
256

L
Liu Jicong 已提交
257
    pHandle->execHandle.exec.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
258
    req.qmsg = NULL;
L
Liu Jicong 已提交
259

L
Liu Jicong 已提交
260 261 262 263 264
    pHandle->pWalReader = walOpenReadHandle(pTq->pVnode->pWal);
    for (int32_t i = 0; i < 5; i++) {
      pHandle->execHandle.pExecReader[i] = tqInitSubmitMsgScanner(pTq->pVnode->pMeta);
    }
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
265
      for (int32_t i = 0; i < 5; i++) {
L
Liu Jicong 已提交
266
        SReadHandle handle = {
L
Liu Jicong 已提交
267
            .reader = pHandle->execHandle.pExecReader[i],
L
Liu Jicong 已提交
268
            .meta = pTq->pVnode->pMeta,
269
            .pMsgCb = &pTq->pVnode->msgCb,
L
Liu Jicong 已提交
270
        };
L
Liu Jicong 已提交
271 272 273
        pHandle->execHandle.exec.execCol.task[i] =
            qCreateStreamExecTaskInfo(pHandle->execHandle.exec.execCol.qmsg, &handle);
        ASSERT(pHandle->execHandle.exec.execCol.task[i]);
274
      }
L
Liu Jicong 已提交
275 276 277 278
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
      pHandle->execHandle.exec.execDb.pFilterOutTbUid =
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
L
Liu Jicong 已提交
279
    }
L
Liu Jicong 已提交
280
    taosHashPut(pTq->handles, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
L
Liu Jicong 已提交
281
  } else {
L
Liu Jicong 已提交
282
    /*ASSERT(pExec->consumerId == req.oldConsumerId);*/
L
Liu Jicong 已提交
283
    // TODO handle qmsg and exec modification
L
Liu Jicong 已提交
284 285 286 287
    atomic_store_32(&pHandle->epoch, -1);
    atomic_store_64(&pHandle->consumerId, req.newConsumerId);
    atomic_add_fetch_32(&pHandle->epoch, 1);
  }
L
Liu Jicong 已提交
288

L
Liu Jicong 已提交
289
  if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
L
Liu Jicong 已提交
290
    // TODO
L
Liu Jicong 已提交
291
  }
L
Liu Jicong 已提交
292
  return 0;
L
Liu Jicong 已提交
293
}
294

L
Liu Jicong 已提交
295 296 297 298 299 300 301 302 303 304 305 306
int32_t tqProcessTaskDeploy(STQ* pTq, char* msg, int32_t msgLen) {
  SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
  if (tDecodeSStreamTask(&decoder, pTask) < 0) {
    ASSERT(0);
  }
  tDecoderClear(&decoder);

L
Liu Jicong 已提交
307 308 309 310 311 312 313 314 315 316 317 318
  pTask->status = TASK_STATUS__IDLE;
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

  pTask->inputQ = taosOpenQueue();
  pTask->outputQ = taosOpenQueue();
  pTask->inputQAll = taosAllocateQall();
  pTask->outputQAll = taosAllocateQall();

  if (pTask->inputQ == NULL || pTask->outputQ == NULL || pTask->inputQAll == NULL || pTask->outputQAll == NULL)
    goto FAIL;

L
Liu Jicong 已提交
319
  // exec
L
Liu Jicong 已提交
320 321
  if (pTask->execType != TASK_EXEC__NONE) {
    // expand runners
L
Liu Jicong 已提交
322 323 324 325 326 327 328 329 330 331
    STqReadHandle* pStreamReader = tqInitSubmitMsgScanner(pTq->pVnode->pMeta);
    SReadHandle    handle = {
           .reader = pStreamReader,
           .meta = pTq->pVnode->pMeta,
           .pMsgCb = &pTq->pVnode->msgCb,
           .vnode = pTq->pVnode,
    };
    pTask->exec.inputHandle = pStreamReader;
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
    ASSERT(pTask->exec.executor);
L
Liu Jicong 已提交
332
  }
L
Liu Jicong 已提交
333 334

  // sink
L
Liu Jicong 已提交
335
  pTask->ahandle = pTq->pVnode;
L
Liu Jicong 已提交
336
  if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
337
    pTask->smaSink.smaSink = smaHandleRes;
L
Liu Jicong 已提交
338
  } else if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
339 340 341
    pTask->tbSink.vnode = pTq->pVnode;
    pTask->tbSink.tbSinkFunc = tqTableSink;

L
Liu Jicong 已提交
342 343
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
344

L
Liu Jicong 已提交
345 346 347
    pTask->tbSink.pTSchema =
        tdGetSTSChemaFromSSChema(&pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols);
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
348
  }
L
Liu Jicong 已提交
349

L
Liu Jicong 已提交
350 351
  taosHashPut(pTq->pStreamTasks, &pTask->taskId, sizeof(int32_t), pTask, sizeof(SStreamTask));

L
Liu Jicong 已提交
352 353
  return 0;
FAIL:
L
Liu Jicong 已提交
354 355 356 357 358
  if (pTask->inputQ) taosCloseQueue(pTask->inputQ);
  if (pTask->outputQ) taosCloseQueue(pTask->outputQ);
  if (pTask->inputQAll) taosFreeQall(pTask->inputQAll);
  if (pTask->outputQAll) taosFreeQall(pTask->outputQAll);
  if (pTask) taosMemoryFree(pTask);
L
Liu Jicong 已提交
359 360
  return -1;
}
L
Liu Jicong 已提交
361

L
Liu Jicong 已提交
362
int32_t tqProcessStreamTrigger(STQ* pTq, SSubmitReq* pReq) {
L
Liu Jicong 已提交
363 364 365
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
366

L
Liu Jicong 已提交
367
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  if (pSubmit == NULL) {
    failed = true;
  }

  while (1) {
    pIter = taosHashIterate(pTq->pStreamTasks, pIter);
    if (pIter == NULL) break;
    SStreamTask* pTask = (SStreamTask*)pIter;
    if (pTask->inputType != STREAM_INPUT__DATA_SUBMIT) continue;

    int8_t inputStatus = atomic_load_8(&pTask->inputStatus);
    if (inputStatus == TASK_INPUT_STATUS__NORMAL) {
      if (failed) {
        atomic_store_8(&pTask->inputStatus, TASK_INPUT_STATUS__FAILED);
        continue;
      }

L
Liu Jicong 已提交
385 386 387 388 389
      SStreamDataSubmit* pSubmitClone = streamSubmitRefClone(pSubmit);
      if (pSubmitClone == NULL) {
        atomic_store_8(&pTask->inputStatus, TASK_INPUT_STATUS__FAILED);
        continue;
      }
L
Liu Jicong 已提交
390
      taosWriteQitem(pTask->inputQ, pSubmitClone);
L
Liu Jicong 已提交
391 392 393

      int8_t execStatus = atomic_load_8(&pTask->status);
      if (execStatus == TASK_STATUS__IDLE || execStatus == TASK_STATUS__CLOSING) {
L
Liu Jicong 已提交
394 395 396 397 398 399 400 401 402 403 404 405
        SStreamTaskRunReq* pRunReq = taosMemoryMalloc(sizeof(SStreamTaskRunReq));
        if (pRunReq == NULL) continue;
        // TODO: do we need htonl?
        pRunReq->head.vgId = pTq->pVnode->config.vgId;
        pRunReq->streamId = pTask->streamId;
        pRunReq->taskId = pTask->taskId;
        SRpcMsg msg = {
            .msgType = TDMT_VND_TASK_RUN,
            .pCont = pRunReq,
            .contLen = sizeof(SStreamTaskRunReq),
        };
        tmsgPutToQueue(&pTq->pVnode->msgCb, FETCH_QUEUE, &msg);
L
Liu Jicong 已提交
406 407 408 409 410 411 412
      }

    } else {
      // blocked or stopped, do nothing
    }
  }

L
Liu Jicong 已提交
413
  if (pSubmit) {
L
Liu Jicong 已提交
414
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
415
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
416
  }
L
Liu Jicong 已提交
417 418

  return failed ? -1 : 0;
L
Liu Jicong 已提交
419 420
}

L
Liu Jicong 已提交
421
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
422
  //
L
Liu Jicong 已提交
423 424 425 426 427 428 429 430 431 432 433
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
  SStreamTask*       pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  streamTaskProcessRunReq(pTask, &pTq->pVnode->msgCb);
  return 0;
}

int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamDispatchReq* pReq = pMsg->pCont;
  int32_t             taskId = pReq->taskId;
  SStreamTask*        pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
434
  streamProcessDispatchReq(pTask, &pTq->pVnode->msgCb, pReq, pMsg);
L
Liu Jicong 已提交
435 436 437 438 439 440 441
  return 0;
}

int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
  SStreamTask*           pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
442
  streamProcessRecoverReq(pTask, &pTq->pVnode->msgCb, pReq, pMsg);
L
Liu Jicong 已提交
443 444 445 446 447 448 449
  return 0;
}

int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamDispatchRsp* pRsp = pMsg->pCont;
  int32_t             taskId = pRsp->taskId;
  SStreamTask*        pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
450
  streamProcessDispatchRsp(pTask, &pTq->pVnode->msgCb, pRsp);
L
Liu Jicong 已提交
451 452 453 454 455 456 457
  return 0;
}

int32_t tqProcessTaskRecoverRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverRsp* pRsp = pMsg->pCont;
  int32_t                taskId = pRsp->taskId;
  SStreamTask*           pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
458
  streamProcessRecoverRsp(pTask, pRsp);
L
Liu Jicong 已提交
459 460
  return 0;
}