tq.c 15.6 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
 */

L
Liu Jicong 已提交
16
#include "tcompare.h"
L
Liu Jicong 已提交
17
#include "tdatablock.h"
L
Liu Jicong 已提交
18
#include "tqInt.h"
L
Liu Jicong 已提交
19
#include "tqMetaStore.h"
L
Liu Jicong 已提交
20
#include "tstream.h"
S
Shengliang Guan 已提交
21

L
Liu Jicong 已提交
22
int32_t tqInit() { return tqPushMgrInit(); }
L
Liu Jicong 已提交
23

L
Liu Jicong 已提交
24
void tqCleanUp() { tqPushMgrCleanUp(); }
L
Liu Jicong 已提交
25

L
Liu Jicong 已提交
26 27
STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal, SMeta* pVnodeMeta, STqCfg* tqConfig,
            SMemAllocatorFactory* allocFac) {
wafwerar's avatar
wafwerar 已提交
28
  STQ* pTq = taosMemoryMalloc(sizeof(STQ));
L
Liu Jicong 已提交
29
  if (pTq == NULL) {
L
Liu Jicong 已提交
30
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
31 32
    return NULL;
  }
H
Hongze Cheng 已提交
33
  pTq->path = strdup(path);
L
Liu Jicong 已提交
34
  pTq->tqConfig = tqConfig;
L
Liu Jicong 已提交
35
  pTq->pVnode = pVnode;
L
Liu Jicong 已提交
36
  pTq->pWal = pWal;
L
Liu Jicong 已提交
37
  pTq->pVnodeMeta = pVnodeMeta;
L
Liu Jicong 已提交
38
#if 0
L
Liu Jicong 已提交
39
  pTq->tqMemRef.pAllocatorFactory = allocFac;
L
Liu Jicong 已提交
40 41
  pTq->tqMemRef.pAllocator = allocFac->create(allocFac);
  if (pTq->tqMemRef.pAllocator == NULL) {
L
Liu Jicong 已提交
42
    // TODO: error code of buffer pool
L
Liu Jicong 已提交
43
  }
L
Liu Jicong 已提交
44
#endif
L
Liu Jicong 已提交
45 46
  pTq->tqMeta = tqStoreOpen(pTq, path, (FTqSerialize)tqSerializeConsumer, (FTqDeserialize)tqDeserializeConsumer,
                            (FTqDelete)taosMemoryFree, 0);
L
Liu Jicong 已提交
47
  if (pTq->tqMeta == NULL) {
wafwerar's avatar
wafwerar 已提交
48
    taosMemoryFree(pTq);
L
Liu Jicong 已提交
49 50 51
#if 0
    allocFac->destroy(allocFac, pTq->tqMemRef.pAllocator);
#endif
L
Liu Jicong 已提交
52 53
    return NULL;
  }
L
Liu Jicong 已提交
54

L
Liu Jicong 已提交
55
#if 0
L
Liu Jicong 已提交
56 57 58
  pTq->tqPushMgr = tqPushMgrOpen();
  if (pTq->tqPushMgr == NULL) {
    // free store
wafwerar's avatar
wafwerar 已提交
59
    taosMemoryFree(pTq);
L
Liu Jicong 已提交
60 61
    return NULL;
  }
L
Liu Jicong 已提交
62
#endif
L
Liu Jicong 已提交
63

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

L
Liu Jicong 已提交
66 67
  return pTq;
}
L
Liu Jicong 已提交
68

L
Liu Jicong 已提交
69
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
70
  if (pTq) {
wafwerar's avatar
wafwerar 已提交
71 72
    taosMemoryFreeClear(pTq->path);
    taosMemoryFree(pTq);
H
Hongze Cheng 已提交
73
  }
L
Liu Jicong 已提交
74 75
  // TODO
}
L
Liu Jicong 已提交
76

L
Liu Jicong 已提交
77
int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t version) {
L
Liu Jicong 已提交
78
  if (msgType != TDMT_VND_SUBMIT) return 0;
wafwerar's avatar
wafwerar 已提交
79
  void* data = taosMemoryMalloc(msgLen);
L
Liu Jicong 已提交
80 81
  if (data == NULL) {
    return -1;
L
Liu Jicong 已提交
82
  }
L
Liu Jicong 已提交
83 84 85 86 87 88 89
  memcpy(data, msg, msgLen);
  SRpcMsg req = {
      .msgType = TDMT_VND_STREAM_TRIGGER,
      .pCont = data,
      .contLen = msgLen,
  };
  tmsgPutToQueue(&pTq->pVnode->msgCb, FETCH_QUEUE, &req);
L
Liu Jicong 已提交
90 91

#if 0
L
Liu Jicong 已提交
92 93 94 95 96 97
  void* pIter = taosHashIterate(pTq->tqPushMgr->pHash, NULL);
  while (pIter != NULL) {
    STqPusher* pusher = *(STqPusher**)pIter;
    if (pusher->type == TQ_PUSHER_TYPE__STREAM) {
      STqStreamPusher* streamPusher = (STqStreamPusher*)pusher;
      // repack
wafwerar's avatar
wafwerar 已提交
98
      STqStreamToken* token = taosMemoryMalloc(sizeof(STqStreamToken));
L
Liu Jicong 已提交
99 100 101 102 103 104 105 106 107 108 109 110
      if (token == NULL) {
        taosHashCancelIterate(pTq->tqPushMgr->pHash, pIter);
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
      token->type = TQ_STREAM_TOKEN__DATA;
      token->data = msg;
      // set input
      // exec
    }
    // send msg to ep
  }
L
Liu Jicong 已提交
111 112
  // iterate hash
  // process all msg
L
fix  
Liu Jicong 已提交
113 114
  // if waiting
  // memcpy and send msg to fetch thread
L
Liu Jicong 已提交
115 116 117 118
  // TODO: add reference
  // if handle waiting, launch query and response to consumer
  //
  // if no waiting handle, return
L
Liu Jicong 已提交
119
#endif
L
Liu Jicong 已提交
120 121 122
  return 0;
}

L
Liu Jicong 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
int tqCommit(STQ* pTq) { return tqStorePersist(pTq->tqMeta); }

int32_t tqGetTopicHandleSize(const STqTopic* pTopic) {
  return strlen(pTopic->topicName) + strlen(pTopic->sql) + strlen(pTopic->logicalPlan) + strlen(pTopic->physicalPlan) +
         strlen(pTopic->qmsg) + sizeof(int64_t) * 3;
}

int32_t tqGetConsumerHandleSize(const STqConsumer* pConsumer) {
  int     num = taosArrayGetSize(pConsumer->topics);
  int32_t sz = 0;
  for (int i = 0; i < num; i++) {
    STqTopic* pTopic = taosArrayGet(pConsumer->topics, i);
    sz += tqGetTopicHandleSize(pTopic);
  }
  return sz;
}

static FORCE_INLINE int32_t tEncodeSTqTopic(void** buf, const STqTopic* pTopic) {
  int32_t tlen = 0;
  tlen += taosEncodeString(buf, pTopic->topicName);
  /*tlen += taosEncodeString(buf, pTopic->sql);*/
  /*tlen += taosEncodeString(buf, pTopic->logicalPlan);*/
  /*tlen += taosEncodeString(buf, pTopic->physicalPlan);*/
  tlen += taosEncodeString(buf, pTopic->qmsg);
L
Liu Jicong 已提交
147 148 149
  /*tlen += taosEncodeFixedI64(buf, pTopic->persistedOffset);*/
  /*tlen += taosEncodeFixedI64(buf, pTopic->committedOffset);*/
  /*tlen += taosEncodeFixedI64(buf, pTopic->currentOffset);*/
L
Liu Jicong 已提交
150 151 152 153 154 155 156 157 158
  return tlen;
}

static FORCE_INLINE const void* tDecodeSTqTopic(const void* buf, STqTopic* pTopic) {
  buf = taosDecodeStringTo(buf, pTopic->topicName);
  /*buf = taosDecodeString(buf, &pTopic->sql);*/
  /*buf = taosDecodeString(buf, &pTopic->logicalPlan);*/
  /*buf = taosDecodeString(buf, &pTopic->physicalPlan);*/
  buf = taosDecodeString(buf, &pTopic->qmsg);
L
Liu Jicong 已提交
159 160 161
  /*buf = taosDecodeFixedI64(buf, &pTopic->persistedOffset);*/
  /*buf = taosDecodeFixedI64(buf, &pTopic->committedOffset);*/
  /*buf = taosDecodeFixedI64(buf, &pTopic->currentOffset);*/
L
Liu Jicong 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  return buf;
}

static FORCE_INLINE int32_t tEncodeSTqConsumer(void** buf, const STqConsumer* pConsumer) {
  int32_t sz;

  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pConsumer->consumerId);
  tlen += taosEncodeFixedI64(buf, pConsumer->epoch);
  tlen += taosEncodeString(buf, pConsumer->cgroup);
  sz = taosArrayGetSize(pConsumer->topics);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    STqTopic* pTopic = taosArrayGet(pConsumer->topics, i);
    tlen += tEncodeSTqTopic(buf, pTopic);
  }
  return tlen;
L
Liu Jicong 已提交
179 180
}

L
Liu Jicong 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static FORCE_INLINE const void* tDecodeSTqConsumer(const void* buf, STqConsumer* pConsumer) {
  int32_t sz;

  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
  buf = taosDecodeFixedI64(buf, &pConsumer->epoch);
  buf = taosDecodeStringTo(buf, pConsumer->cgroup);
  buf = taosDecodeFixedI32(buf, &sz);
  pConsumer->topics = taosArrayInit(sz, sizeof(STqTopic));
  if (pConsumer->topics == NULL) return NULL;
  for (int32_t i = 0; i < sz; i++) {
    STqTopic pTopic;
    buf = tDecodeSTqTopic(buf, &pTopic);
    taosArrayPush(pConsumer->topics, &pTopic);
  }
  return buf;
}

int tqSerializeConsumer(const STqConsumer* pConsumer, STqSerializedHead** ppHead) {
  int32_t sz = tEncodeSTqConsumer(NULL, pConsumer);

L
Liu Jicong 已提交
201
  if (sz > (*ppHead)->ssize) {
wafwerar's avatar
wafwerar 已提交
202
    void* tmpPtr = taosMemoryRealloc(*ppHead, sizeof(STqSerializedHead) + sz);
L
Liu Jicong 已提交
203
    if (tmpPtr == NULL) {
wafwerar's avatar
wafwerar 已提交
204
      taosMemoryFree(*ppHead);
L
Liu Jicong 已提交
205
      terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
206 207 208 209 210 211 212
      return -1;
    }
    *ppHead = tmpPtr;
    (*ppHead)->ssize = sz;
  }

  void* ptr = (*ppHead)->content;
L
Liu Jicong 已提交
213 214
  void* abuf = ptr;
  tEncodeSTqConsumer(&abuf, pConsumer);
L
Liu Jicong 已提交
215

L
Liu Jicong 已提交
216 217 218
  return 0;
}

L
Liu Jicong 已提交
219 220
int32_t tqDeserializeConsumer(STQ* pTq, const STqSerializedHead* pHead, STqConsumer** ppConsumer) {
  const void* str = pHead->content;
wafwerar's avatar
wafwerar 已提交
221
  *ppConsumer = taosMemoryCalloc(1, sizeof(STqConsumer));
L
Liu Jicong 已提交
222 223 224 225 226 227 228 229 230 231
  if (*ppConsumer == NULL) {
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
    return -1;
  }
  if (tDecodeSTqConsumer(str, *ppConsumer) == NULL) {
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
    return -1;
  }
  STqConsumer* pConsumer = *ppConsumer;
  int32_t      sz = taosArrayGetSize(pConsumer->topics);
L
Liu Jicong 已提交
232
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
233 234 235 236 237
    STqTopic* pTopic = taosArrayGet(pConsumer->topics, i);
    pTopic->pReadhandle = walOpenReadHandle(pTq->pWal);
    if (pTopic->pReadhandle == NULL) {
      ASSERT(false);
    }
L
Liu Jicong 已提交
238 239
    for (int j = 0; j < TQ_BUFFER_SIZE; j++) {
      pTopic->buffer.output[j].status = 0;
L
Liu Jicong 已提交
240
      STqReadHandle* pReadHandle = tqInitSubmitMsgScanner(pTq->pVnodeMeta);
L
Liu Jicong 已提交
241 242 243 244
      SReadHandle    handle = {
             .reader = pReadHandle,
             .meta = pTq->pVnodeMeta,
      };
L
Liu Jicong 已提交
245 246
      pTopic->buffer.output[j].pReadHandle = pReadHandle;
      pTopic->buffer.output[j].task = qCreateStreamExecTaskInfo(pTopic->qmsg, &handle);
L
Liu Jicong 已提交
247
    }
L
Liu Jicong 已提交
248
  }
L
Liu Jicong 已提交
249 250

  return 0;
L
Liu Jicong 已提交
251 252
}

L
Liu Jicong 已提交
253 254 255 256 257
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
  SMqPollReq* pReq = pMsg->pCont;
  int64_t     consumerId = pReq->consumerId;
  int64_t     fetchOffset;
  int64_t     blockingTime = pReq->blockingTime;
258

L
Liu Jicong 已提交
259 260 261 262 263 264 265 266
  if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__EARLIEAST) {
    fetchOffset = 0;
  } else if (pReq->currentOffset == TMQ_CONF__RESET_OFFSET__LATEST) {
    fetchOffset = walGetLastVer(pTq->pWal);
  } else {
    fetchOffset = pReq->currentOffset + 1;
  }

L
Liu Jicong 已提交
267
  SMqPollRsp rsp = {
L
Liu Jicong 已提交
268
      /*.consumerId = consumerId,*/
L
Liu Jicong 已提交
269 270 271
      .numOfTopics = 0,
      .pBlockData = NULL,
  };
272 273 274 275 276 277 278 279 280

  STqConsumer* pConsumer = tqHandleGet(pTq->tqMeta, consumerId);
  if (pConsumer == NULL) {
    pMsg->pCont = NULL;
    pMsg->contLen = 0;
    pMsg->code = -1;
    rpcSendResponse(pMsg);
    return 0;
  }
L
Liu Jicong 已提交
281

282 283 284 285 286 287
  int sz = taosArrayGetSize(pConsumer->topics);
  ASSERT(sz == 1);
  STqTopic* pTopic = taosArrayGet(pConsumer->topics, 0);
  ASSERT(strcmp(pTopic->topicName, pReq->topic) == 0);
  ASSERT(pConsumer->consumerId == consumerId);

L
Liu Jicong 已提交
288
  rsp.reqOffset = pReq->currentOffset;
289 290 291 292
  rsp.skipLogNum = 0;

  SWalHead* pHead;
  while (1) {
L
Liu Jicong 已提交
293
    /*if (fetchOffset > walGetLastVer(pTq->pWal) || walReadWithHandle(pTopic->pReadhandle, fetchOffset) < 0) {*/
294
    if (walReadWithHandle(pTopic->pReadhandle, fetchOffset) < 0) {
L
Liu Jicong 已提交
295 296
      // TODO: no more log, set timer to wait blocking time
      // if data inserted during waiting, launch query and
L
Liu Jicong 已提交
297
      // response to user
298 299
      break;
    }
L
Liu Jicong 已提交
300
    int8_t pos = fetchOffset % TQ_BUFFER_SIZE;
301 302
    pHead = pTopic->pReadhandle->pHead;
    if (pHead->head.msgType == TDMT_VND_SUBMIT) {
S
Shengliang Guan 已提交
303
      SSubmitReq* pCont = (SSubmitReq*)&pHead->head.body;
304
      qTaskInfo_t task = pTopic->buffer.output[pos].task;
305
      qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK);
306 307 308 309 310 311 312 313 314
      SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
      while (1) {
        SSDataBlock* pDataBlock;
        uint64_t     ts;
        if (qExecTask(task, &pDataBlock, &ts) < 0) {
          ASSERT(false);
        }
        if (pDataBlock == NULL) {
          fetchOffset++;
L
Liu Jicong 已提交
315
          pos = fetchOffset % TQ_BUFFER_SIZE;
316 317 318 319 320
          rsp.skipLogNum++;
          break;
        }

        taosArrayPush(pRes, pDataBlock);
L
Liu Jicong 已提交
321
        rsp.schema = pTopic->buffer.output[pos].pReadHandle->pSchemaWrapper;
322 323 324 325 326
        rsp.rspOffset = fetchOffset;

        rsp.numOfTopics = 1;
        rsp.pBlockData = pRes;

L
Liu Jicong 已提交
327
        int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqPollRsp(NULL, &rsp);
328 329 330 331 332
        void*   buf = rpcMallocCont(tlen);
        if (buf == NULL) {
          pMsg->code = -1;
          return -1;
        }
L
fix  
Liu Jicong 已提交
333 334
        ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
        ((SMqRspHead*)buf)->epoch = pReq->epoch;
L
Liu Jicong 已提交
335
        ((SMqRspHead*)buf)->consumerId = consumerId;
336

L
fix  
Liu Jicong 已提交
337
        void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
L
Liu Jicong 已提交
338
        tEncodeSMqPollRsp(&abuf, &rsp);
L
Liu Jicong 已提交
339
        /*taosArrayDestroyEx(rsp.pBlockData, (void (*)(void*))tDeleteSSDataBlock);*/
340 341 342 343 344 345 346 347 348 349 350 351
        pMsg->pCont = buf;
        pMsg->contLen = tlen;
        pMsg->code = 0;
        rpcSendResponse(pMsg);
        return 0;
      }
    } else {
      fetchOffset++;
      rsp.skipLogNum++;
    }
  }

L
Liu Jicong 已提交
352 353 354 355
  /*if (blockingTime != 0) {*/
  /*tqAddClientPusher(pTq->tqPushMgr, pMsg, consumerId, blockingTime);*/
  /*} else {*/
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqPollRsp(NULL, &rsp);
356 357 358 359 360
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    pMsg->code = -1;
    return -1;
  }
L
Liu Jicong 已提交
361 362
  ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
  ((SMqRspHead*)buf)->epoch = pReq->epoch;
363

L
Liu Jicong 已提交
364
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
L
Liu Jicong 已提交
365
  tEncodeSMqPollRsp(&abuf, &rsp);
366 367 368 369 370
  rsp.pBlockData = NULL;
  pMsg->pCont = buf;
  pMsg->contLen = tlen;
  pMsg->code = 0;
  rpcSendResponse(pMsg);
L
Liu Jicong 已提交
371 372
  /*}*/

373 374 375
  return 0;
}

L
Liu Jicong 已提交
376 377 378 379
int32_t tqProcessRebReq(STQ* pTq, char* msg) {
  SMqMVRebReq req = {0};
  tDecodeSMqMVRebReq(msg, &req);

L
Liu Jicong 已提交
380
  STqConsumer* pConsumer = tqHandleGet(pTq->tqMeta, req.oldConsumerId);
L
Liu Jicong 已提交
381
  ASSERT(pConsumer);
L
Liu Jicong 已提交
382
  pConsumer->consumerId = req.newConsumerId;
L
Liu Jicong 已提交
383 384 385 386 387 388 389
  tqHandleMovePut(pTq->tqMeta, req.newConsumerId, pConsumer);
  tqHandleCommit(pTq->tqMeta, req.newConsumerId);
  tqHandlePurge(pTq->tqMeta, req.oldConsumerId);
  terrno = TSDB_CODE_SUCCESS;
  return 0;
}

L
Liu Jicong 已提交
390
int32_t tqProcessSetConnReq(STQ* pTq, char* msg) {
L
Liu Jicong 已提交
391
  SMqSetCVgReq req = {0};
L
Liu Jicong 已提交
392
  tDecodeSMqSetCVgReq(msg, &req);
L
Liu Jicong 已提交
393

394
  /*printf("vg %d set to consumer from %ld to %ld\n", req.vgId, req.oldConsumerId, req.newConsumerId);*/
wafwerar's avatar
wafwerar 已提交
395
  STqConsumer* pConsumer = taosMemoryCalloc(1, sizeof(STqConsumer));
L
Liu Jicong 已提交
396
  if (pConsumer == NULL) {
L
Liu Jicong 已提交
397 398
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
    return -1;
L
Liu Jicong 已提交
399
  }
L
Liu Jicong 已提交
400

L
Liu Jicong 已提交
401
  strcpy(pConsumer->cgroup, req.cgroup);
L
Liu Jicong 已提交
402
  pConsumer->topics = taosArrayInit(0, sizeof(STqTopic));
L
Liu Jicong 已提交
403
  pConsumer->consumerId = req.consumerId;
L
Liu Jicong 已提交
404
  pConsumer->epoch = 0;
L
Liu Jicong 已提交
405

wafwerar's avatar
wafwerar 已提交
406
  STqTopic* pTopic = taosMemoryCalloc(1, sizeof(STqTopic));
L
Liu Jicong 已提交
407
  if (pTopic == NULL) {
L
Liu Jicong 已提交
408
    taosArrayDestroy(pConsumer->topics);
wafwerar's avatar
wafwerar 已提交
409
    taosMemoryFree(pConsumer);
L
Liu Jicong 已提交
410 411
    return -1;
  }
L
Liu Jicong 已提交
412
  strcpy(pTopic->topicName, req.topicName);
L
Liu Jicong 已提交
413 414 415
  pTopic->sql = req.sql;
  pTopic->logicalPlan = req.logicalPlan;
  pTopic->physicalPlan = req.physicalPlan;
L
Liu Jicong 已提交
416
  pTopic->qmsg = req.qmsg;
L
Liu Jicong 已提交
417 418
  /*pTopic->committedOffset = -1;*/
  /*pTopic->currentOffset = -1;*/
419

L
Liu Jicong 已提交
420 421
  pTopic->buffer.firstOffset = -1;
  pTopic->buffer.lastOffset = -1;
L
Liu Jicong 已提交
422 423
  pTopic->pReadhandle = walOpenReadHandle(pTq->pWal);
  if (pTopic->pReadhandle == NULL) {
L
Liu Jicong 已提交
424
    ASSERT(false);
L
Liu Jicong 已提交
425
  }
L
Liu Jicong 已提交
426 427
  for (int i = 0; i < TQ_BUFFER_SIZE; i++) {
    pTopic->buffer.output[i].status = 0;
L
Liu Jicong 已提交
428
    STqReadHandle* pReadHandle = tqInitSubmitMsgScanner(pTq->pVnodeMeta);
L
Liu Jicong 已提交
429 430 431 432
    SReadHandle    handle = {
           .reader = pReadHandle,
           .meta = pTq->pVnodeMeta,
    };
L
Liu Jicong 已提交
433
    pTopic->buffer.output[i].pReadHandle = pReadHandle;
L
Liu Jicong 已提交
434
    pTopic->buffer.output[i].task = qCreateStreamExecTaskInfo(req.qmsg, &handle);
L
Liu Jicong 已提交
435
  }
L
Liu Jicong 已提交
436
  taosArrayPush(pConsumer->topics, pTopic);
L
Liu Jicong 已提交
437 438
  tqHandleMovePut(pTq->tqMeta, req.consumerId, pConsumer);
  tqHandleCommit(pTq->tqMeta, req.consumerId);
L
Liu Jicong 已提交
439
  terrno = TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
440 441
  return 0;
}
L
Liu Jicong 已提交
442

L
Liu Jicong 已提交
443
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask, int32_t parallel) {
L
Liu Jicong 已提交
444 445 446
  if (pTask->execType == TASK_EXEC__NONE) return 0;

  pTask->exec.numOfRunners = parallel;
L
Liu Jicong 已提交
447
  pTask->exec.runners = taosMemoryCalloc(parallel, sizeof(SStreamRunner));
L
Liu Jicong 已提交
448 449 450
  if (pTask->exec.runners == NULL) {
    return -1;
  }
L
Liu Jicong 已提交
451 452 453 454 455 456
  for (int32_t i = 0; i < parallel; i++) {
    STqReadHandle* pReadHandle = tqInitSubmitMsgScanner(pTq->pVnodeMeta);
    SReadHandle    handle = {
           .reader = pReadHandle,
           .meta = pTq->pVnodeMeta,
    };
L
Liu Jicong 已提交
457 458
    pTask->exec.runners[i].inputHandle = pReadHandle;
    pTask->exec.runners[i].executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
L
Liu Jicong 已提交
459
    ASSERT(pTask->exec.runners[i].executor);
L
Liu Jicong 已提交
460 461 462 463
  }
  return 0;
}

L
Liu Jicong 已提交
464
int32_t tqProcessTaskDeploy(STQ* pTq, char* msg, int32_t msgLen) {
wafwerar's avatar
wafwerar 已提交
465
  SStreamTask* pTask = taosMemoryMalloc(sizeof(SStreamTask));
L
Liu Jicong 已提交
466 467 468 469 470
  if (pTask == NULL) {
    return -1;
  }
  SCoder decoder;
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, (uint8_t*)msg, msgLen, TD_DECODER);
L
Liu Jicong 已提交
471 472 473
  if (tDecodeSStreamTask(&decoder, pTask) < 0) {
    ASSERT(0);
  }
L
Liu Jicong 已提交
474 475
  tCoderClear(&decoder);

L
Liu Jicong 已提交
476 477 478
  if (tqExpandTask(pTq, pTask, 4) < 0) {
    ASSERT(0);
  }
L
Liu Jicong 已提交
479
  pTask->ahandle = pTq->pVnode;
L
Liu Jicong 已提交
480

L
Liu Jicong 已提交
481 482 483 484
  taosHashPut(pTq->pStreamTasks, &pTask->taskId, sizeof(int32_t), pTask, sizeof(SStreamTask));

  return 0;
}
L
Liu Jicong 已提交
485

L
Liu Jicong 已提交
486 487 488 489 490 491 492 493
int32_t tqProcessStreamTrigger(STQ* pTq, void* data, int32_t dataLen) {
  void* pIter = NULL;

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

L
Liu Jicong 已提交
494 495
    if (streamExecTask(pTask, &pTq->pVnode->msgCb, data, STREAM_DATA_TYPE_SUBMIT_BLOCK, 0) < 0) {
      // TODO
L
Liu Jicong 已提交
496 497 498 499 500
    }
  }
  return 0;
}

L
Liu Jicong 已提交
501
int32_t tqProcessTaskExec(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
502
  SStreamTaskExecReq req;
L
Liu Jicong 已提交
503
  tDecodeSStreamTaskExecReq(msg, &req);
L
Liu Jicong 已提交
504 505 506

  int32_t      taskId = req.taskId;
  SStreamTask* pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
507
  ASSERT(pTask);
L
Liu Jicong 已提交
508

L
Liu Jicong 已提交
509
  if (streamExecTask(pTask, &pTq->pVnode->msgCb, req.data, STREAM_DATA_TYPE_SSDATA_BLOCK, 0) < 0) {
L
Liu Jicong 已提交
510
    // TODO
L
Liu Jicong 已提交
511
  }
L
Liu Jicong 已提交
512 513
  return 0;
}