tq.c 21.2 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) {
50
  STQ* pTq = taosMemoryCalloc(1, 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);
  }

69 70 71 72
  if (tqOffsetOpen(pTq) < 0) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
73 74
  return pTq;
}
L
Liu Jicong 已提交
75

L
Liu Jicong 已提交
76
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
77
  if (pTq) {
78
    tqOffsetClose(pTq->pOffsetStore);
L
Liu Jicong 已提交
79
    taosHashCleanup(pTq->handles);
L
Liu Jicong 已提交
80 81
    taosHashCleanup(pTq->pStreamTasks);
    taosHashCleanup(pTq->pushMgr);
82
    taosMemoryFree(pTq->path);
L
Liu Jicong 已提交
83
    tqMetaClose(pTq);
wafwerar's avatar
wafwerar 已提交
84
    taosMemoryFree(pTq);
H
Hongze Cheng 已提交
85
  }
L
Liu Jicong 已提交
86
}
L
Liu Jicong 已提交
87

L
Liu Jicong 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
int32_t tqSendMetaPollRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp) {
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqMetaRsp(NULL, pRsp);
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    return -1;
  }

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

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

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

  tqDebug("vg %d from consumer %ld (epoch %d) send rsp, res msg type %d, reqOffset: %ld, rspOffset: %ld",
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->reqOffset, pRsp->rspOffset);

  return 0;
}

L
Liu Jicong 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
int32_t tqSendPollRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqDataBlkRsp* pRsp) {
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqDataBlkRsp(NULL, pRsp);
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    return -1;
  }

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

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

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

  tqDebug("vg %d from consumer %ld (epoch %d) send rsp, block num: %d, reqOffset: %ld, rspOffset: %ld",
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, pRsp->reqOffset, pRsp->rspOffset);

  return 0;
}

144 145 146 147 148 149 150 151 152 153 154
int32_t tqProcessOffsetCommitReq(STQ* pTq, char* msg, int32_t msgLen) {
  STqOffset offset = {0};
  SDecoder  decoder;
  tDecoderInit(&decoder, msg, msgLen);
  if (tDecodeSTqOffset(&decoder, &offset) < 0) {
    ASSERT(0);
    return -1;
  }
  tDecoderClear(&decoder);

  if (offset.type == TMQ_OFFSET__SNAPSHOT) {
L
Liu Jicong 已提交
155
    tqDebug("receive offset commit msg to %s on vg %d, offset(type:snapshot) uid: %ld, ts: %ld", offset.subKey,
L
Liu Jicong 已提交
156
            TD_VID(pTq->pVnode), offset.uid, offset.ts);
157
  } else if (offset.type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
158
    tqDebug("receive offset commit msg to %s on vg %d, offset(type:log) version: %ld", offset.subKey,
L
Liu Jicong 已提交
159
            TD_VID(pTq->pVnode), offset.version);
160 161 162
  } else {
    ASSERT(0);
  }
L
Liu Jicong 已提交
163 164 165 166 167 168
  STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, offset.subKey);
  if (pOffset == NULL || pOffset->version < offset.version) {
    if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
      ASSERT(0);
      return -1;
    }
169 170 171 172 173
  }

  return 0;
}

L
fix  
Liu Jicong 已提交
174
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) {
L
Liu Jicong 已提交
175 176
  SMqPollReq* pReq = pMsg->pCont;
  int64_t     consumerId = pReq->consumerId;
L
Liu Jicong 已提交
177
  int64_t     timeout = pReq->timeout;
L
Liu Jicong 已提交
178 179
  int32_t     reqEpoch = pReq->epoch;
  int64_t     fetchOffset;
L
Liu Jicong 已提交
180
  int32_t     code = 0;
181

L
Liu Jicong 已提交
182
  // get offset to fetch message
L
Liu Jicong 已提交
183
  if (pReq->currentOffset >= 0) {
L
Liu Jicong 已提交
184
    fetchOffset = pReq->currentOffset + 1;
L
Liu Jicong 已提交
185 186 187 188
  } else {
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, pReq->subKey);
    if (pOffset != NULL) {
      ASSERT(pOffset->type == TMQ_OFFSET__LOG);
L
Liu Jicong 已提交
189
      tqDebug("consumer %ld, restore offset of %s on vg %d, offset(type:log) version: %ld", consumerId, pReq->subKey,
L
Liu Jicong 已提交
190
              TD_VID(pTq->pVnode), pOffset->version);
L
Liu Jicong 已提交
191 192 193 194 195 196 197
      fetchOffset = pOffset->version + 1;
    } else {
      if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__EARLIEAST) {
        fetchOffset = walGetFirstVer(pTq->pWal);
      } else if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__LATEST) {
        fetchOffset = walGetCommittedVer(pTq->pWal);
      } else if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__NONE) {
L
Liu Jicong 已提交
198 199
        tqError("tmq poll: no offset committed for consumer %ld in vg %d, subkey %s", consumerId, TD_VID(pTq->pVnode),
                pReq->subKey);
L
Liu Jicong 已提交
200
        terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
L
Liu Jicong 已提交
201 202
        return -1;
      }
L
Liu Jicong 已提交
203
      tqDebug("consumer %ld, restore offset of %s on vg %d failed, config is %ld, set to %ld", consumerId, pReq->subKey,
L
Liu Jicong 已提交
204
              TD_VID(pTq->pVnode), pReq->currentOffset, fetchOffset);
L
Liu Jicong 已提交
205
    }
L
Liu Jicong 已提交
206 207
  }

L
Liu Jicong 已提交
208 209
  tqDebug("tmq poll: consumer %ld (epoch %d) recv poll req in vg %d, req offset %ld fetch offset %ld", consumerId,
          pReq->epoch, TD_VID(pTq->pVnode), pReq->currentOffset, fetchOffset);
L
fix  
Liu Jicong 已提交
210

L
Liu Jicong 已提交
211
  STqHandle* pHandle = taosHashGet(pTq->handles, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
212 213
  /*ASSERT(pHandle);*/
  if (pHandle == NULL) {
L
Liu Jicong 已提交
214
    tqError("tmq poll: no consumer handle for consumer %ld in vg %d, subkey %s", consumerId, TD_VID(pTq->pVnode),
L
Liu Jicong 已提交
215 216 217
            pReq->subKey);
    return -1;
  }
218

L
Liu Jicong 已提交
219 220
  if (pHandle->consumerId != consumerId) {
    tqError("tmq poll: consumer handle mismatch for consumer %ld in vg %d, subkey %s, handle consumer id %ld",
L
Liu Jicong 已提交
221
            consumerId, TD_VID(pTq->pVnode), pReq->subKey, pHandle->consumerId);
L
Liu Jicong 已提交
222 223
    return -1;
  }
L
Liu Jicong 已提交
224

L
Liu Jicong 已提交
225
  int32_t consumerEpoch = atomic_load_32(&pHandle->epoch);
L
Liu Jicong 已提交
226
  while (consumerEpoch < reqEpoch) {
L
Liu Jicong 已提交
227
    consumerEpoch = atomic_val_compare_exchange_32(&pHandle->epoch, consumerEpoch, reqEpoch);
L
Liu Jicong 已提交
228 229
  }

L
Liu Jicong 已提交
230
  SMqDataBlkRsp rsp = {0};
L
Liu Jicong 已提交
231
  rsp.reqOffset = pReq->currentOffset;
L
Liu Jicong 已提交
232

L
Liu Jicong 已提交
233
  rsp.blockData = taosArrayInit(0, sizeof(void*));
L
Liu Jicong 已提交
234
  rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t));
235

L
Liu Jicong 已提交
236 237 238 239
  if (rsp.blockData == NULL || rsp.blockDataLen == NULL) {
    return -1;
  }

L
Liu Jicong 已提交
240
  rsp.withTbName = pReq->withTbName;
241 242 243
  if (rsp.withTbName) {
    rsp.blockTbName = taosArrayInit(0, sizeof(void*));
  }
L
Liu Jicong 已提交
244 245 246 247 248 249
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
    rsp.withSchema = false;
    rsp.withTag = false;
  } else {
    rsp.withSchema = true;
    rsp.withTag = false;
L
Liu Jicong 已提交
250
    rsp.blockSchema = taosArrayInit(0, sizeof(void*));
251
  }
L
Liu Jicong 已提交
252

L
Liu Jicong 已提交
253 254
#if 1
  if (pReq->useSnapshot) {
255
    // TODO set ver into snapshot
L
Liu Jicong 已提交
256 257
    int64_t lastVer = walGetCommittedVer(pTq->pWal);
    if (rsp.reqOffset < lastVer) {
258
      tqInfo("retrieve using snapshot req offset %ld last ver %ld", rsp.reqOffset, lastVer);
L
Liu Jicong 已提交
259 260 261 262 263
      tqScanSnapshot(pTq, &pHandle->execHandle, &rsp, workerId);

      if (rsp.blockNum != 0) {
        rsp.withTbName = false;
        rsp.rspOffset = lastVer;
264 265
        tqInfo("direct send by snapshot req offset %ld rsp offset %ld", rsp.reqOffset, rsp.rspOffset);
        fetchOffset = lastVer;
L
Liu Jicong 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278
        goto SEND_RSP;
      }
    }
  }
#endif

  SWalHead* pHeadWithCkSum = taosMemoryMalloc(sizeof(SWalHead) + 2048);
  if (pHeadWithCkSum == NULL) {
    return -1;
  }

  walSetReaderCapacity(pHandle->pWalReader, 2048);

279
  while (1) {
L
Liu Jicong 已提交
280
    consumerEpoch = atomic_load_32(&pHandle->epoch);
L
Liu Jicong 已提交
281
    if (consumerEpoch > reqEpoch) {
L
Liu Jicong 已提交
282 283
      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 已提交
284 285
      break;
    }
L
Liu Jicong 已提交
286

L
Liu Jicong 已提交
287 288
    if (tqFetchLog(pTq, pHandle, &fetchOffset, &pHeadWithCkSum) < 0) {
      // TODO add push mgr
289 290 291 292 293
      break;
    }

    SWalReadHead* pHead = &pHeadWithCkSum->head;

H
Hongze Cheng 已提交
294 295
    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 已提交
296

L
fix  
Liu Jicong 已提交
297 298
    if (pHead->msgType == TDMT_VND_SUBMIT) {
      SSubmitReq* pCont = (SSubmitReq*)&pHead->body;
L
Liu Jicong 已提交
299

L
Liu Jicong 已提交
300 301 302
      if (tqDataExec(pTq, &pHandle->execHandle, pCont, &rsp, workerId) < 0) {
        /*ASSERT(0);*/
      }
L
Liu Jicong 已提交
303
    } else {
L
Liu Jicong 已提交
304 305 306 307 308
      ASSERT(pHandle->fetchMeta);
      ASSERT(pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB ||
             pHead->msgType == TDMT_VND_DROP_STB || pHead->msgType == TDMT_VND_CREATE_TABLE ||
             pHead->msgType == TDMT_VND_ALTER_TABLE || pHead->msgType == TDMT_VND_DROP_TABLE ||
             pHead->msgType == TDMT_VND_DROP_TTL_TABLE);
309
      tqInfo("fetch meta msg, ver: %ld, type: %d", pHead->version, pHead->msgType);
L
Liu Jicong 已提交
310 311 312 313 314 315 316 317
      SMqMetaRsp metaRsp = {0};
      metaRsp.reqOffset = pReq->currentOffset;
      metaRsp.rspOffset = fetchOffset;
      metaRsp.resMsgType = pHead->msgType;
      metaRsp.metaRspLen = pHead->bodyLen;
      metaRsp.metaRsp = pHead->body;
      if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) {
        code = -1;
L
Liu Jicong 已提交
318
        goto OVER;
L
Liu Jicong 已提交
319 320 321
      }
      code = 0;
      goto OVER;
L
Liu Jicong 已提交
322
    }
323

L
Liu Jicong 已提交
324 325
    // TODO batch optimization:
    // TODO continue scan until meeting batch requirement
L
Liu Jicong 已提交
326 327
    if (rsp.blockNum > 0 /* threshold */) {
      break;
L
Liu Jicong 已提交
328 329
    } else {
      fetchOffset++;
L
Liu Jicong 已提交
330
    }
L
Liu Jicong 已提交
331
  }
332

L
Liu Jicong 已提交
333
  taosMemoryFree(pHeadWithCkSum);
L
Liu Jicong 已提交
334

L
Liu Jicong 已提交
335
SEND_RSP:
L
Liu Jicong 已提交
336 337
  ASSERT(taosArrayGetSize(rsp.blockData) == rsp.blockNum);
  ASSERT(taosArrayGetSize(rsp.blockDataLen) == rsp.blockNum);
L
Liu Jicong 已提交
338 339 340
  if (rsp.withSchema) {
    ASSERT(taosArrayGetSize(rsp.blockSchema) == rsp.blockNum);
  }
L
fix  
Liu Jicong 已提交
341

L
Liu Jicong 已提交
342
  rsp.rspOffset = fetchOffset;
343

L
Liu Jicong 已提交
344 345
  if (tqSendPollRsp(pTq, pMsg, pReq, &rsp) < 0) {
    code = -1;
346
  }
L
Liu Jicong 已提交
347
OVER:
L
Liu Jicong 已提交
348
  // TODO wrap in destroy func
L
Liu Jicong 已提交
349
  taosArrayDestroy(rsp.blockDataLen);
350
  taosArrayDestroyP(rsp.blockData, (FDelete)taosMemoryFree);
351 352 353 354 355 356 357 358

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

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

L
Liu Jicong 已提交
360
  return code;
361
}
L
Liu Jicong 已提交
362

L
Liu Jicong 已提交
363 364
int32_t tqProcessVgDeleteReq(STQ* pTq, char* msg, int32_t msgLen) {
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
L
Liu Jicong 已提交
365

L
Liu Jicong 已提交
366
  int32_t code = taosHashRemove(pTq->handles, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
367
  ASSERT(code == 0);
368

L
Liu Jicong 已提交
369
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
370 371
    ASSERT(0);
  }
L
Liu Jicong 已提交
372
  return 0;
L
Liu Jicong 已提交
373 374
}

L
Liu Jicong 已提交
375
int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
376
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
377 378
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
L
Liu Jicong 已提交
379 380
  STqHandle* pHandle = taosHashGet(pTq->handles, req.subKey, strlen(req.subKey));
  if (pHandle == NULL) {
L
Liu Jicong 已提交
381 382
    ASSERT(req.oldConsumerId == -1);
    ASSERT(req.newConsumerId != -1);
L
Liu Jicong 已提交
383 384
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
385 386
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
387 388 389
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
390

L
Liu Jicong 已提交
391
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
392
    pHandle->fetchMeta = req.withMeta;
L
Liu Jicong 已提交
393

L
Liu Jicong 已提交
394 395 396 397 398
    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) {
L
Liu Jicong 已提交
399
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
400
      req.qmsg = NULL;
401
      for (int32_t i = 0; i < 5; i++) {
L
Liu Jicong 已提交
402
        SReadHandle handle = {
L
Liu Jicong 已提交
403
            .reader = pHandle->execHandle.pExecReader[i],
L
Liu Jicong 已提交
404
            .meta = pTq->pVnode->pMeta,
L
Liu Jicong 已提交
405 406
            .vnode = pTq->pVnode,
            .initTsdbReader = 1,
L
Liu Jicong 已提交
407
        };
L
Liu Jicong 已提交
408 409
        pHandle->execHandle.execCol.task[i] = qCreateStreamExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle);
        ASSERT(pHandle->execHandle.execCol.task[i]);
410
      }
L
Liu Jicong 已提交
411
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
L
Liu Jicong 已提交
412
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
413 414
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
L
Liu Jicong 已提交
415
      pHandle->execHandle.execTb.suid = req.suid;
L
Liu Jicong 已提交
416 417
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
      tsdbGetCtbIdList(pTq->pVnode->pMeta, req.suid, tbUidList);
L
Liu Jicong 已提交
418
      tqDebug("vg %d, tq try get suid: %ld", TD_VID(pTq->pVnode), req.suid);
L
Liu Jicong 已提交
419 420
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
L
Liu Jicong 已提交
421
        tqDebug("vg %d, idx %d, uid: %ld", TD_VID(pTq->pVnode), i, tbUid);
L
Liu Jicong 已提交
422
      }
L
Liu Jicong 已提交
423 424 425
      for (int32_t i = 0; i < 5; i++) {
        tqReadHandleSetTbUidList(pHandle->execHandle.pExecReader[i], tbUidList);
      }
L
Liu Jicong 已提交
426
      taosArrayDestroy(tbUidList);
L
Liu Jicong 已提交
427
    }
L
Liu Jicong 已提交
428
    taosHashPut(pTq->handles, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
L
Liu Jicong 已提交
429 430 431
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
    }
L
Liu Jicong 已提交
432
  } else {
L
Liu Jicong 已提交
433
    /*ASSERT(pExec->consumerId == req.oldConsumerId);*/
L
Liu Jicong 已提交
434
    // TODO handle qmsg and exec modification
L
Liu Jicong 已提交
435 436 437
    atomic_store_32(&pHandle->epoch, -1);
    atomic_store_64(&pHandle->consumerId, req.newConsumerId);
    atomic_add_fetch_32(&pHandle->epoch, 1);
L
Liu Jicong 已提交
438 439 440
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
    }
L
Liu Jicong 已提交
441
  }
L
Liu Jicong 已提交
442

L
Liu Jicong 已提交
443
  return 0;
L
Liu Jicong 已提交
444
}
445

L
Liu Jicong 已提交
446
int32_t tqProcessTaskDeployReq(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
447 448 449 450 451 452 453 454 455 456
  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);
457
  ASSERT(pTask->isDataScan == 0 || pTask->isDataScan == 1);
L
Liu Jicong 已提交
458

L
Liu Jicong 已提交
459
  pTask->execStatus = TASK_EXEC_STATUS__IDLE;
L
Liu Jicong 已提交
460 461 462

  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
L
Liu Jicong 已提交
463 464 465
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

L
Liu Jicong 已提交
466
  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) goto FAIL;
L
Liu Jicong 已提交
467

468 469
  pTask->pMsgCb = &pTq->pVnode->msgCb;

L
Liu Jicong 已提交
470
  // exec
L
Liu Jicong 已提交
471 472
  if (pTask->execType != TASK_EXEC__NONE) {
    // expand runners
L
Liu Jicong 已提交
473
    if (pTask->isDataScan) {
L
Liu Jicong 已提交
474 475 476 477 478
      STqReadHandle* pStreamReader = tqInitSubmitMsgScanner(pTq->pVnode->pMeta);
      SReadHandle    handle = {
             .reader = pStreamReader,
             .meta = pTq->pVnode->pMeta,
             .vnode = pTq->pVnode,
L
Liu Jicong 已提交
479
             .initTsdbReader = 1,
L
Liu Jicong 已提交
480 481 482 483 484 485 486 487
      };
      /*pTask->exec.inputHandle = pStreamReader;*/
      pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
      ASSERT(pTask->exec.executor);
    } else {
      pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, NULL);
      ASSERT(pTask->exec.executor);
    }
L
Liu Jicong 已提交
488
  }
L
Liu Jicong 已提交
489 490

  // sink
L
Liu Jicong 已提交
491
  /*pTask->ahandle = pTq->pVnode;*/
L
Liu Jicong 已提交
492
  if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
493
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
494
    pTask->smaSink.smaSink = smaHandleRes;
L
Liu Jicong 已提交
495
  } else if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
496 497 498
    pTask->tbSink.vnode = pTq->pVnode;
    pTask->tbSink.tbSinkFunc = tqTableSink;

L
Liu Jicong 已提交
499 500
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
501

L
Liu Jicong 已提交
502 503 504
    pTask->tbSink.pTSchema =
        tdGetSTSChemaFromSSChema(&pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols);
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
505
  }
506 507 508

  streamSetupTrigger(pTask);

L
Liu Jicong 已提交
509
  tqInfo("deploy stream task id %d child id %d on vg %d", pTask->taskId, pTask->selfChildId, TD_VID(pTq->pVnode));
L
Liu Jicong 已提交
510

511
  taosHashPut(pTq->pStreamTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*));
L
Liu Jicong 已提交
512

L
Liu Jicong 已提交
513 514
  return 0;
FAIL:
L
Liu Jicong 已提交
515 516
  if (pTask->inputQueue) streamQueueClose(pTask->inputQueue);
  if (pTask->outputQueue) streamQueueClose(pTask->outputQueue);
L
Liu Jicong 已提交
517
  if (pTask) taosMemoryFree(pTask);
L
Liu Jicong 已提交
518 519
  return -1;
}
L
Liu Jicong 已提交
520

L
Liu Jicong 已提交
521
int32_t tqProcessStreamTrigger(STQ* pTq, SSubmitReq* pReq) {
L
Liu Jicong 已提交
522 523 524
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
525

L
Liu Jicong 已提交
526
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
527 528 529 530 531 532 533
  if (pSubmit == NULL) {
    failed = true;
  }

  while (1) {
    pIter = taosHashIterate(pTq->pStreamTasks, pIter);
    if (pIter == NULL) break;
534
    SStreamTask* pTask = *(SStreamTask**)pIter;
L
Liu Jicong 已提交
535 536 537
    if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
      continue;
    }
538
    if (!pTask->isDataScan) continue;
L
Liu Jicong 已提交
539

L
Liu Jicong 已提交
540 541
    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pSubmit) < 0) {
L
Liu Jicong 已提交
542 543 544
        continue;
      }

L
Liu Jicong 已提交
545
      if (streamLaunchByWrite(pTask, TD_VID(pTq->pVnode)) < 0) {
L
Liu Jicong 已提交
546 547
        continue;
      }
L
Liu Jicong 已提交
548
    } else {
L
Liu Jicong 已提交
549
      streamTaskInputFail(pTask);
L
Liu Jicong 已提交
550 551 552
    }
  }

L
Liu Jicong 已提交
553
  if (pSubmit) {
L
Liu Jicong 已提交
554
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
555
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
556
  }
L
Liu Jicong 已提交
557 558

  return failed ? -1 : 0;
L
Liu Jicong 已提交
559 560
}

L
Liu Jicong 已提交
561
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
562
  //
L
Liu Jicong 已提交
563 564
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
565
  SStreamTask*       pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
566
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
567 568
    return 0;
  }
L
Liu Jicong 已提交
569
  streamProcessRunReq(pTask);
L
Liu Jicong 已提交
570 571 572 573
  return 0;
}

int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
574 575 576 577 578 579 580 581
  char*              msgStr = pMsg->pCont;
  char*              msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t            msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamDispatchReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  tDecodeStreamDispatchReq(&decoder, &req);
  int32_t      taskId = req.taskId;
582
  SStreamTask* pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
583
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
584 585 586 587 588
    return 0;
  }
  SRpcMsg rsp = {
      .info = pMsg->info,
      .code = 0,
589
  };
L
Liu Jicong 已提交
590
  streamProcessDispatchReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
591 592 593 594 595 596
  return 0;
}

int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
597
  SStreamTask*           pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
598
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
599 600
    return 0;
  }
L
Liu Jicong 已提交
601
  streamProcessRecoverReq(pTask, pReq, pMsg);
L
Liu Jicong 已提交
602 603 604 605
  return 0;
}

int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
606
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
L
Liu Jicong 已提交
607
  int32_t             taskId = pRsp->taskId;
608
  SStreamTask*        pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
609
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
610 611
    return 0;
  }
L
Liu Jicong 已提交
612
  streamProcessDispatchRsp(pTask, pRsp);
L
Liu Jicong 已提交
613 614 615 616 617 618
  return 0;
}

int32_t tqProcessTaskRecoverRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverRsp* pRsp = pMsg->pCont;
  int32_t                taskId = pRsp->taskId;
619
  SStreamTask*           pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
620
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
621 622
    return 0;
  }
L
Liu Jicong 已提交
623
  streamProcessRecoverRsp(pTask, pRsp);
L
Liu Jicong 已提交
624 625
  return 0;
}
L
Liu Jicong 已提交
626 627 628

int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
L
Liu Jicong 已提交
629 630 631 632 633 634 635 636 637 638 639

  SStreamTask* pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &pReq->taskId, sizeof(int32_t));
  atomic_store_8(&pTask->taskStatus, TASK_STATUS__DROPPING);
  // todo
  // clear queue
  // push drop req into queue
  // launch exec to free memory
  // remove from hash
  return 0;

#if 0
L
Liu Jicong 已提交
640
  int32_t              code = taosHashRemove(pTq->pStreamTasks, &pReq->taskId, sizeof(int32_t));
L
Liu Jicong 已提交
641
  // set status dropping
L
Liu Jicong 已提交
642
  ASSERT(code == 0);
L
Liu Jicong 已提交
643 644 645 646
  if (code == 0) {
    // sendrsp
  }
  return code;
L
Liu Jicong 已提交
647
#endif
L
Liu Jicong 已提交
648
}
L
Liu Jicong 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
  char*              msgStr = pMsg->pCont;
  char*              msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t            msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamRetrieveReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  tDecodeStreamRetrieveReq(&decoder, &req);
  int32_t      taskId = req.dstTaskId;
  SStreamTask* pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
    return 0;
  }
  SRpcMsg rsp = {
      .info = pMsg->info,
      .code = 0,
  };
  streamProcessRetrieveReq(pTask, &req, &rsp);
  return 0;
}

int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}