tq.c 26.9 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 "tstream.h"
H
Hongze Cheng 已提交
19
#include "vnodeInt.h"
S
Shengliang Guan 已提交
20

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

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

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

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

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

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

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

L
Liu Jicong 已提交
76
int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t version) {
L
Liu Jicong 已提交
77
  if (msgType != TDMT_VND_SUBMIT) return 0;
wafwerar's avatar
wafwerar 已提交
78
  void* data = taosMemoryMalloc(msgLen);
L
Liu Jicong 已提交
79 80
  if (data == NULL) {
    return -1;
L
Liu Jicong 已提交
81
  }
L
Liu Jicong 已提交
82
  memcpy(data, msg, msgLen);
C
Cary Xu 已提交
83 84

  if (msgType == TDMT_VND_SUBMIT) {
C
Cary Xu 已提交
85 86 87
    if (tsdbUpdateSmaWindow(pTq->pVnode->pTsdb, msg) != 0) {
      return -1;
    }
C
Cary Xu 已提交
88 89
  }

L
Liu Jicong 已提交
90 91 92 93 94 95
  SRpcMsg req = {
      .msgType = TDMT_VND_STREAM_TRIGGER,
      .pCont = data,
      .contLen = msgLen,
  };
  tmsgPutToQueue(&pTq->pVnode->msgCb, FETCH_QUEUE, &req);
L
Liu Jicong 已提交
96 97

#if 0
L
Liu Jicong 已提交
98 99 100 101 102 103
  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 已提交
104
      STqStreamToken* token = taosMemoryMalloc(sizeof(STqStreamToken));
L
Liu Jicong 已提交
105 106 107 108 109 110 111 112 113 114 115 116
      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 已提交
117 118
  // iterate hash
  // process all msg
L
fix  
Liu Jicong 已提交
119 120
  // if waiting
  // memcpy and send msg to fetch thread
L
Liu Jicong 已提交
121 122 123 124
  // TODO: add reference
  // if handle waiting, launch query and response to consumer
  //
  // if no waiting handle, return
L
Liu Jicong 已提交
125
#endif
L
Liu Jicong 已提交
126 127 128
  return 0;
}

L
Liu Jicong 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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 已提交
153 154 155
  /*tlen += taosEncodeFixedI64(buf, pTopic->persistedOffset);*/
  /*tlen += taosEncodeFixedI64(buf, pTopic->committedOffset);*/
  /*tlen += taosEncodeFixedI64(buf, pTopic->currentOffset);*/
L
Liu Jicong 已提交
156 157 158 159 160 161 162 163 164
  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 已提交
165 166 167
  /*buf = taosDecodeFixedI64(buf, &pTopic->persistedOffset);*/
  /*buf = taosDecodeFixedI64(buf, &pTopic->committedOffset);*/
  /*buf = taosDecodeFixedI64(buf, &pTopic->currentOffset);*/
L
Liu Jicong 已提交
168 169 170 171 172 173 174 175
  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);
L
Liu Jicong 已提交
176
  tlen += taosEncodeFixedI32(buf, pConsumer->epoch);
L
Liu Jicong 已提交
177 178 179 180 181 182 183 184
  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 已提交
185 186
}

L
Liu Jicong 已提交
187 188 189 190
static FORCE_INLINE const void* tDecodeSTqConsumer(const void* buf, STqConsumer* pConsumer) {
  int32_t sz;

  buf = taosDecodeFixedI64(buf, &pConsumer->consumerId);
L
Liu Jicong 已提交
191
  buf = taosDecodeFixedI32(buf, &pConsumer->epoch);
L
Liu Jicong 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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 已提交
207
  if (sz > (*ppHead)->ssize) {
wafwerar's avatar
wafwerar 已提交
208
    void* tmpPtr = taosMemoryRealloc(*ppHead, sizeof(STqSerializedHead) + sz);
L
Liu Jicong 已提交
209
    if (tmpPtr == NULL) {
wafwerar's avatar
wafwerar 已提交
210
      taosMemoryFree(*ppHead);
L
Liu Jicong 已提交
211
      terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
212 213 214 215 216 217 218
      return -1;
    }
    *ppHead = tmpPtr;
    (*ppHead)->ssize = sz;
  }

  void* ptr = (*ppHead)->content;
L
Liu Jicong 已提交
219 220
  void* abuf = ptr;
  tEncodeSTqConsumer(&abuf, pConsumer);
L
Liu Jicong 已提交
221

L
Liu Jicong 已提交
222 223 224
  return 0;
}

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

  return 0;
L
Liu Jicong 已提交
257
}
L
Liu Jicong 已提交
258
#if 0
L
fix  
Liu Jicong 已提交
259
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) {
L
Liu Jicong 已提交
260 261 262 263
  SMqPollReq* pReq = pMsg->pCont;
  int64_t     consumerId = pReq->consumerId;
  int64_t     fetchOffset;
  int64_t     blockingTime = pReq->blockingTime;
L
Liu Jicong 已提交
264
  int32_t     reqEpoch = pReq->epoch;
265

L
Liu Jicong 已提交
266 267 268 269 270 271 272 273
  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;
  }

H
Hongze Cheng 已提交
274 275
  vDebug("tmq poll: consumer %ld (epoch %d) recv poll req in vg %d, req %ld %ld", consumerId, pReq->epoch,
         pTq->pVnode->vgId, pReq->currentOffset, fetchOffset);
L
fix  
Liu Jicong 已提交
276

L
Liu Jicong 已提交
277
  SMqPollRsp rsp = {
L
Liu Jicong 已提交
278
      /*.consumerId = consumerId,*/
L
Liu Jicong 已提交
279 280 281
      .numOfTopics = 0,
      .pBlockData = NULL,
  };
282 283 284

  STqConsumer* pConsumer = tqHandleGet(pTq->tqMeta, consumerId);
  if (pConsumer == NULL) {
L
Liu Jicong 已提交
285
    vWarn("tmq poll: consumer %ld (epoch %d) not found in vg %d", consumerId, pReq->epoch, pTq->pVnode->vgId);
286 287 288
    pMsg->pCont = NULL;
    pMsg->contLen = 0;
    pMsg->code = -1;
S
shm  
Shengliang Guan 已提交
289
    tmsgSendRsp(pMsg);
290 291
    return 0;
  }
L
Liu Jicong 已提交
292

L
Liu Jicong 已提交
293 294 295 296 297 298
  int32_t consumerEpoch = atomic_load_32(&pConsumer->epoch);
  while (consumerEpoch < reqEpoch) {
    consumerEpoch = atomic_val_compare_exchange_32(&pConsumer->epoch, consumerEpoch, reqEpoch);
  }

  STqTopic* pTopic = NULL;
L
Liu Jicong 已提交
299
  int32_t   sz = taosArrayGetSize(pConsumer->topics);
L
Liu Jicong 已提交
300 301
  for (int32_t i = 0; i < sz; i++) {
    STqTopic* topic = taosArrayGet(pConsumer->topics, i);
H
Hongze Cheng 已提交
302
    // TODO race condition
L
Liu Jicong 已提交
303 304 305 306 307 308 309
    ASSERT(pConsumer->consumerId == consumerId);
    if (strcmp(topic->topicName, pReq->topic) == 0) {
      pTopic = topic;
      break;
    }
  }
  if (pTopic == NULL) {
H
Hongze Cheng 已提交
310 311
    vWarn("tmq poll: consumer %ld (epoch %d) topic %s not found in vg %d", consumerId, pReq->epoch, pReq->topic,
          pTq->pVnode->vgId);
L
Liu Jicong 已提交
312 313 314 315 316 317 318
    pMsg->pCont = NULL;
    pMsg->contLen = 0;
    pMsg->code = -1;
    tmsgSendRsp(pMsg);
    return 0;
  }

L
Liu Jicong 已提交
319 320
  vDebug("poll topic %s from consumer %ld (epoch %d) vg %d", pTopic->topicName, consumerId, pReq->epoch,
         pTq->pVnode->vgId);
321

L
Liu Jicong 已提交
322
  rsp.reqOffset = pReq->currentOffset;
323 324 325
  rsp.skipLogNum = 0;

  while (1) {
L
Liu Jicong 已提交
326
    /*if (fetchOffset > walGetLastVer(pTq->pWal) || walReadWithHandle(pTopic->pReadhandle, fetchOffset) < 0) {*/
H
Hongze Cheng 已提交
327
    // TODO
L
Liu Jicong 已提交
328
    consumerEpoch = atomic_load_32(&pConsumer->epoch);
L
Liu Jicong 已提交
329
    if (consumerEpoch > reqEpoch) {
L
Liu Jicong 已提交
330 331
      vDebug("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, found new consumer epoch %d discard req epoch %d",
             consumerId, pReq->epoch, pTq->pVnode->vgId, fetchOffset, consumerEpoch, reqEpoch);
L
Liu Jicong 已提交
332 333
      break;
    }
L
fix  
Liu Jicong 已提交
334 335
    SWalReadHead* pHead;
    if (walReadWithHandle_s(pTopic->pReadhandle, fetchOffset, &pHead) < 0) {
L
Liu Jicong 已提交
336 337
      // TODO: no more log, set timer to wait blocking time
      // if data inserted during waiting, launch query and
L
Liu Jicong 已提交
338
      // response to user
L
Liu Jicong 已提交
339 340
      vDebug("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, no more log to return", consumerId, pReq->epoch,
             pTq->pVnode->vgId, fetchOffset);
341 342
      break;
    }
L
Liu Jicong 已提交
343 344
    vDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d offset %ld msgType %d", consumerId, pReq->epoch,
           pTq->pVnode->vgId, fetchOffset, pHead->msgType);
L
fix  
Liu Jicong 已提交
345 346 347 348 349 350
    /*int8_t pos = fetchOffset % TQ_BUFFER_SIZE;*/
    /*pHead = pTopic->pReadhandle->pHead;*/
    if (pHead->msgType == TDMT_VND_SUBMIT) {
      SSubmitReq* pCont = (SSubmitReq*)&pHead->body;
      qTaskInfo_t task = pTopic->buffer.output[workerId].task;
      ASSERT(task);
351
      qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK);
352 353
      SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
      while (1) {
L
fix  
Liu Jicong 已提交
354
        SSDataBlock* pDataBlock = NULL;
355 356 357 358 359
        uint64_t     ts;
        if (qExecTask(task, &pDataBlock, &ts) < 0) {
          ASSERT(false);
        }
        if (pDataBlock == NULL) {
L
fix  
Liu Jicong 已提交
360
          /*pos = fetchOffset % TQ_BUFFER_SIZE;*/
361 362 363 364
          break;
        }

        taosArrayPush(pRes, pDataBlock);
L
fix  
Liu Jicong 已提交
365 366 367
      }

      if (taosArrayGetSize(pRes) == 0) {
L
Liu Jicong 已提交
368 369
        vDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d skip log %ld since not wanted", consumerId,
               pReq->epoch, pTq->pVnode->vgId, fetchOffset);
L
fix  
Liu Jicong 已提交
370 371 372 373 374 375 376
        fetchOffset++;
        rsp.skipLogNum++;
        taosArrayDestroy(pRes);
        continue;
      }
      rsp.schema = pTopic->buffer.output[workerId].pReadHandle->pSchemaWrapper;
      rsp.rspOffset = fetchOffset;
377

L
fix  
Liu Jicong 已提交
378 379
      rsp.numOfTopics = 1;
      rsp.pBlockData = pRes;
380

L
fix  
Liu Jicong 已提交
381 382 383 384 385 386
      int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqPollRsp(NULL, &rsp);
      void*   buf = rpcMallocCont(tlen);
      if (buf == NULL) {
        pMsg->code = -1;
        taosMemoryFree(pHead);
        return -1;
387
      }
L
fix  
Liu Jicong 已提交
388 389 390 391 392 393 394 395 396 397
      ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
      ((SMqRspHead*)buf)->epoch = pReq->epoch;
      ((SMqRspHead*)buf)->consumerId = consumerId;

      void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
      tEncodeSMqPollRsp(&abuf, &rsp);
      /*taosArrayDestroyEx(rsp.pBlockData, (void (*)(void*))tDeleteSSDataBlock);*/
      pMsg->pCont = buf;
      pMsg->contLen = tlen;
      pMsg->code = 0;
L
Liu Jicong 已提交
398 399
      vDebug("vg %d offset %ld msgType %d from consumer %ld (epoch %d) actual rsp", pTq->pVnode->vgId, fetchOffset,
             pHead->msgType, consumerId, pReq->epoch);
L
fix  
Liu Jicong 已提交
400 401 402
      tmsgSendRsp(pMsg);
      taosMemoryFree(pHead);
      return 0;
403
    } else {
L
fix  
Liu Jicong 已提交
404
      taosMemoryFree(pHead);
405 406 407 408 409
      fetchOffset++;
      rsp.skipLogNum++;
    }
  }

L
Liu Jicong 已提交
410 411 412 413
  /*if (blockingTime != 0) {*/
  /*tqAddClientPusher(pTq->tqPushMgr, pMsg, consumerId, blockingTime);*/
  /*} else {*/
  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqPollRsp(NULL, &rsp);
414 415 416 417 418
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    pMsg->code = -1;
    return -1;
  }
L
Liu Jicong 已提交
419 420
  ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
  ((SMqRspHead*)buf)->epoch = pReq->epoch;
L
temp  
Liu Jicong 已提交
421
  rsp.rspOffset = fetchOffset - 1;
422

L
Liu Jicong 已提交
423
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
L
Liu Jicong 已提交
424
  tEncodeSMqPollRsp(&abuf, &rsp);
425 426 427 428
  rsp.pBlockData = NULL;
  pMsg->pCont = buf;
  pMsg->contLen = tlen;
  pMsg->code = 0;
S
shm  
Shengliang Guan 已提交
429
  tmsgSendRsp(pMsg);
L
Liu Jicong 已提交
430 431
  vDebug("vg %d offset %ld from consumer %ld (epoch %d) not rsp", pTq->pVnode->vgId, fetchOffset, consumerId,
         pReq->epoch);
L
Liu Jicong 已提交
432 433
  /*}*/

434 435
  return 0;
}
L
Liu Jicong 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
#endif

int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) {
  SMqPollReq* pReq = pMsg->pCont;
  int64_t     consumerId = pReq->consumerId;
  int64_t     fetchOffset;
  int64_t     blockingTime = pReq->blockingTime;
  int32_t     reqEpoch = pReq->epoch;

  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;
  }

  vDebug("tmq poll: consumer %ld (epoch %d) recv poll req in vg %d, req %ld %ld", consumerId, pReq->epoch,
         pTq->pVnode->vgId, pReq->currentOffset, fetchOffset);

  SMqPollRspV2 rspV2 = {0};
  rspV2.dataLen = 0;

  STqConsumer* pConsumer = tqHandleGet(pTq->tqMeta, consumerId);
  if (pConsumer == NULL) {
    vWarn("tmq poll: consumer %ld (epoch %d) not found in vg %d", consumerId, pReq->epoch, pTq->pVnode->vgId);
    pMsg->pCont = NULL;
    pMsg->contLen = 0;
    pMsg->code = -1;
    tmsgSendRsp(pMsg);
    return 0;
  }

  int32_t consumerEpoch = atomic_load_32(&pConsumer->epoch);
  while (consumerEpoch < reqEpoch) {
    consumerEpoch = atomic_val_compare_exchange_32(&pConsumer->epoch, consumerEpoch, reqEpoch);
  }

  STqTopic* pTopic = NULL;
  int32_t   topicSz = taosArrayGetSize(pConsumer->topics);
  for (int32_t i = 0; i < topicSz; i++) {
    STqTopic* topic = taosArrayGet(pConsumer->topics, i);
    // TODO race condition
    ASSERT(pConsumer->consumerId == consumerId);
    if (strcmp(topic->topicName, pReq->topic) == 0) {
      pTopic = topic;
      break;
    }
  }
  if (pTopic == NULL) {
    vWarn("tmq poll: consumer %ld (epoch %d) topic %s not found in vg %d", consumerId, pReq->epoch, pReq->topic,
          pTq->pVnode->vgId);
    pMsg->pCont = NULL;
    pMsg->contLen = 0;
    pMsg->code = -1;
    tmsgSendRsp(pMsg);
    return 0;
  }

  vDebug("poll topic %s from consumer %ld (epoch %d) vg %d", pTopic->topicName, consumerId, pReq->epoch,
         pTq->pVnode->vgId);

  rspV2.reqOffset = pReq->currentOffset;
  rspV2.skipLogNum = 0;

  while (1) {
    /*if (fetchOffset > walGetLastVer(pTq->pWal) || walReadWithHandle(pTopic->pReadhandle, fetchOffset) < 0) {*/
    // TODO
    consumerEpoch = atomic_load_32(&pConsumer->epoch);
    if (consumerEpoch > reqEpoch) {
      vDebug("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, found new consumer epoch %d discard req epoch %d",
             consumerId, pReq->epoch, pTq->pVnode->vgId, fetchOffset, consumerEpoch, reqEpoch);
      break;
    }
    SWalReadHead* pHead;
    if (walReadWithHandle_s(pTopic->pReadhandle, fetchOffset, &pHead) < 0) {
      // TODO: no more log, set timer to wait blocking time
      // if data inserted during waiting, launch query and
      // response to user
      vDebug("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, no more log to return", consumerId, pReq->epoch,
             pTq->pVnode->vgId, fetchOffset);
      break;
    }
    vDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d offset %ld msgType %d", consumerId, pReq->epoch,
           pTq->pVnode->vgId, fetchOffset, pHead->msgType);
    /*int8_t pos = fetchOffset % TQ_BUFFER_SIZE;*/
    /*pHead = pTopic->pReadhandle->pHead;*/
    if (pHead->msgType == TDMT_VND_SUBMIT) {
      SSubmitReq* pCont = (SSubmitReq*)&pHead->body;
      qTaskInfo_t task = pTopic->buffer.output[workerId].task;
      ASSERT(task);
      qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK);
      SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
      while (1) {
        SSDataBlock* pDataBlock = NULL;
        uint64_t     ts;
        if (qExecTask(task, &pDataBlock, &ts) < 0) {
          ASSERT(false);
        }
        if (pDataBlock == NULL) {
          /*pos = fetchOffset % TQ_BUFFER_SIZE;*/
          break;
        }

        taosArrayPush(pRes, pDataBlock);
      }

      if (taosArrayGetSize(pRes) == 0) {
        vDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d skip log %ld since not wanted", consumerId,
               pReq->epoch, pTq->pVnode->vgId, fetchOffset);
        fetchOffset++;
        rspV2.skipLogNum++;
        taosArrayDestroy(pRes);
        continue;
      }
      rspV2.rspOffset = fetchOffset;

      int32_t blockSz = taosArrayGetSize(pRes);
      int32_t tlen = 0;
      for (int32_t i = 0; i < blockSz; i++) {
        SSDataBlock* pBlock = taosArrayGet(pRes, i);
        tlen += sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
      }

      void* data = taosMemoryMalloc(tlen);
      if (data == NULL) {
        pMsg->code = -1;
        taosMemoryFree(pHead);
      }

      rspV2.blockData = data;

      void*   dataBlockBuf = data;
      int32_t pos;
      for (int32_t i = 0; i < blockSz; i++) {
        pos = 0;
        SSDataBlock* pBlock = taosArrayGet(pRes, i);
        blockCompressEncode(pBlock, dataBlockBuf, &pos, pBlock->info.numOfCols, false);
        taosArrayPush(rspV2.blockPos, &rspV2.dataLen);
        rspV2.dataLen += pos;
        dataBlockBuf = POINTER_SHIFT(dataBlockBuf, pos);
      }

      int32_t msgLen = sizeof(SMqRspHead) + tEncodeSMqPollRspV2(NULL, &rspV2);
      void*   buf = rpcMallocCont(msgLen);

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

      void* msgBodyBuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
      tEncodeSMqPollRspV2(&msgBodyBuf, &rspV2);

      /*rsp.pBlockData = pRes;*/

      /*taosArrayDestroyEx(rsp.pBlockData, (void (*)(void*))tDeleteSSDataBlock);*/
      pMsg->pCont = buf;
      pMsg->contLen = tlen;
      pMsg->code = 0;
      vDebug("vg %d offset %ld msgType %d from consumer %ld (epoch %d) actual rsp", pTq->pVnode->vgId, fetchOffset,
             pHead->msgType, consumerId, pReq->epoch);
      tmsgSendRsp(pMsg);
      taosMemoryFree(pHead);
      return 0;
    } else {
      taosMemoryFree(pHead);
      fetchOffset++;
      rspV2.skipLogNum++;
    }
  }

  /*if (blockingTime != 0) {*/
  /*tqAddClientPusher(pTq->tqPushMgr, pMsg, consumerId, blockingTime);*/
  /*} else {*/

  rspV2.rspOffset = fetchOffset - 1;

  int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqPollRspV2(NULL, &rspV2);
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    pMsg->code = -1;
    return -1;
  }
  ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP;
  ((SMqRspHead*)buf)->epoch = pReq->epoch;
  ((SMqRspHead*)buf)->consumerId = consumerId;

  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
  tEncodeSMqPollRspV2(&abuf, &rspV2);
  pMsg->pCont = buf;
  pMsg->contLen = tlen;
  pMsg->code = 0;
  tmsgSendRsp(pMsg);
  vDebug("vg %d offset %ld from consumer %ld (epoch %d) not rsp", pTq->pVnode->vgId, fetchOffset, consumerId,
         pReq->epoch);
  /*}*/

  return 0;
}
635

L
Liu Jicong 已提交
636 637
int32_t tqProcessRebReq(STQ* pTq, char* msg) {
  SMqMVRebReq req = {0};
L
Liu Jicong 已提交
638
  terrno = TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
639 640
  tDecodeSMqMVRebReq(msg, &req);

L
Liu Jicong 已提交
641
  vDebug("vg %d set from consumer %ld to consumer %ld", req.vgId, req.oldConsumerId, req.newConsumerId);
L
Liu Jicong 已提交
642
  STqConsumer* pConsumer = tqHandleGet(pTq->tqMeta, req.oldConsumerId);
L
Liu Jicong 已提交
643
  ASSERT(pConsumer);
L
Liu Jicong 已提交
644 645 646 647 648 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 675 676 677 678 679 680 681 682 683 684 685 686
  ASSERT(pConsumer->consumerId == req.oldConsumerId);
  int32_t numOfTopics = taosArrayGetSize(pConsumer->topics);
  if (numOfTopics == 1) {
    STqTopic* pTopic = taosArrayGet(pConsumer->topics, 0);
    ASSERT(strcmp(pTopic->topicName, req.topic) == 0);
    STqConsumer* pNewConsumer = tqHandleGet(pTq->tqMeta, req.newConsumerId);
    if (pNewConsumer == NULL) {
      pConsumer->consumerId = req.newConsumerId;
      tqHandleMovePut(pTq->tqMeta, req.newConsumerId, pConsumer);
      tqHandleCommit(pTq->tqMeta, req.newConsumerId);
      tqHandlePurge(pTq->tqMeta, req.oldConsumerId);
      return 0;
    } else {
      taosArrayPush(pNewConsumer->topics, pTopic);
    }
  } else {
    for (int32_t i = 0; i < numOfTopics; i++) {
      STqTopic* pTopic = taosArrayGet(pConsumer->topics, i);
      if (strcmp(pTopic->topicName, req.topic) == 0) {
        STqConsumer* pNewConsumer = tqHandleGet(pTq->tqMeta, req.newConsumerId);
        if (pNewConsumer == NULL) {
          pNewConsumer = taosMemoryCalloc(1, sizeof(STqConsumer));
          if (pNewConsumer == NULL) {
            terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
            return -1;
          }
          strcpy(pNewConsumer->cgroup, pConsumer->cgroup);
          pNewConsumer->topics = taosArrayInit(0, sizeof(STqTopic));
          pNewConsumer->consumerId = req.newConsumerId;
          pNewConsumer->epoch = 0;

          taosArrayPush(pNewConsumer->topics, pTopic);
          tqHandleMovePut(pTq->tqMeta, req.newConsumerId, pConsumer);
          tqHandleCommit(pTq->tqMeta, req.newConsumerId);
          return 0;
        }
        ASSERT(pNewConsumer->consumerId == req.newConsumerId);
        taosArrayPush(pNewConsumer->topics, pTopic);
        break;
      }
    }
    //
  }
L
Liu Jicong 已提交
687 688 689
  return 0;
}

L
Liu Jicong 已提交
690
int32_t tqProcessSetConnReq(STQ* pTq, char* msg) {
L
Liu Jicong 已提交
691
  SMqSetCVgReq req = {0};
L
Liu Jicong 已提交
692
  tDecodeSMqSetCVgReq(msg, &req);
L
Liu Jicong 已提交
693
  bool create = false;
L
Liu Jicong 已提交
694

L
Liu Jicong 已提交
695 696
  vDebug("vg %d set to consumer %ld", req.vgId, req.consumerId);
  STqConsumer* pConsumer = tqHandleGet(pTq->tqMeta, req.consumerId);
L
Liu Jicong 已提交
697
  if (pConsumer == NULL) {
L
Liu Jicong 已提交
698 699 700 701 702 703 704 705 706
    pConsumer = taosMemoryCalloc(1, sizeof(STqConsumer));
    if (pConsumer == NULL) {
      terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
      return -1;
    }
    strcpy(pConsumer->cgroup, req.cgroup);
    pConsumer->topics = taosArrayInit(0, sizeof(STqTopic));
    pConsumer->consumerId = req.consumerId;
    pConsumer->epoch = 0;
L
Liu Jicong 已提交
707
    create = true;
L
Liu Jicong 已提交
708
  }
L
Liu Jicong 已提交
709

wafwerar's avatar
wafwerar 已提交
710
  STqTopic* pTopic = taosMemoryCalloc(1, sizeof(STqTopic));
L
Liu Jicong 已提交
711
  if (pTopic == NULL) {
L
Liu Jicong 已提交
712
    taosArrayDestroy(pConsumer->topics);
wafwerar's avatar
wafwerar 已提交
713
    taosMemoryFree(pConsumer);
L
Liu Jicong 已提交
714 715
    return -1;
  }
L
Liu Jicong 已提交
716
  strcpy(pTopic->topicName, req.topicName);
L
Liu Jicong 已提交
717 718 719
  pTopic->sql = req.sql;
  pTopic->logicalPlan = req.logicalPlan;
  pTopic->physicalPlan = req.physicalPlan;
L
Liu Jicong 已提交
720
  pTopic->qmsg = req.qmsg;
L
Liu Jicong 已提交
721 722
  /*pTopic->committedOffset = -1;*/
  /*pTopic->currentOffset = -1;*/
723

L
Liu Jicong 已提交
724 725
  pTopic->buffer.firstOffset = -1;
  pTopic->buffer.lastOffset = -1;
L
Liu Jicong 已提交
726 727
  pTopic->pReadhandle = walOpenReadHandle(pTq->pWal);
  if (pTopic->pReadhandle == NULL) {
L
Liu Jicong 已提交
728
    ASSERT(false);
L
Liu Jicong 已提交
729
  }
L
Liu Jicong 已提交
730 731
  for (int i = 0; i < TQ_BUFFER_SIZE; i++) {
    pTopic->buffer.output[i].status = 0;
L
Liu Jicong 已提交
732
    STqReadHandle* pReadHandle = tqInitSubmitMsgScanner(pTq->pVnodeMeta);
L
Liu Jicong 已提交
733 734 735 736
    SReadHandle    handle = {
           .reader = pReadHandle,
           .meta = pTq->pVnodeMeta,
    };
L
Liu Jicong 已提交
737
    pTopic->buffer.output[i].pReadHandle = pReadHandle;
L
Liu Jicong 已提交
738
    pTopic->buffer.output[i].task = qCreateStreamExecTaskInfo(req.qmsg, &handle);
L
fix  
Liu Jicong 已提交
739
    ASSERT(pTopic->buffer.output[i].task);
L
Liu Jicong 已提交
740
  }
L
Liu Jicong 已提交
741
  vDebug("set topic %s to consumer %ld on vg %d", pTopic->topicName, req.consumerId, pTq->pVnode->vgId);
L
Liu Jicong 已提交
742
  taosArrayPush(pConsumer->topics, pTopic);
L
Liu Jicong 已提交
743 744 745 746
  if (create) {
    tqHandleMovePut(pTq->tqMeta, req.consumerId, pConsumer);
    tqHandleCommit(pTq->tqMeta, req.consumerId);
  }
L
Liu Jicong 已提交
747
  terrno = TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
748 749
  return 0;
}
L
Liu Jicong 已提交
750

L
Liu Jicong 已提交
751 752 753 754 755
int32_t tqProcessCancelConnReq(STQ* pTq, char* msg) {
  terrno = TSDB_CODE_SUCCESS;
  return 0;
}

L
Liu Jicong 已提交
756
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask, int32_t parallel) {
L
Liu Jicong 已提交
757 758 759
  if (pTask->execType == TASK_EXEC__NONE) return 0;

  pTask->exec.numOfRunners = parallel;
L
Liu Jicong 已提交
760
  pTask->exec.runners = taosMemoryCalloc(parallel, sizeof(SStreamRunner));
L
Liu Jicong 已提交
761 762 763
  if (pTask->exec.runners == NULL) {
    return -1;
  }
L
Liu Jicong 已提交
764 765 766 767 768 769
  for (int32_t i = 0; i < parallel; i++) {
    STqReadHandle* pReadHandle = tqInitSubmitMsgScanner(pTq->pVnodeMeta);
    SReadHandle    handle = {
           .reader = pReadHandle,
           .meta = pTq->pVnodeMeta,
    };
L
Liu Jicong 已提交
770 771
    pTask->exec.runners[i].inputHandle = pReadHandle;
    pTask->exec.runners[i].executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
L
Liu Jicong 已提交
772
    ASSERT(pTask->exec.runners[i].executor);
L
Liu Jicong 已提交
773 774 775 776
  }
  return 0;
}

L
Liu Jicong 已提交
777
int32_t tqProcessTaskDeploy(STQ* pTq, char* msg, int32_t msgLen) {
wafwerar's avatar
wafwerar 已提交
778
  SStreamTask* pTask = taosMemoryMalloc(sizeof(SStreamTask));
L
Liu Jicong 已提交
779 780 781 782 783
  if (pTask == NULL) {
    return -1;
  }
  SCoder decoder;
  tCoderInit(&decoder, TD_LITTLE_ENDIAN, (uint8_t*)msg, msgLen, TD_DECODER);
L
Liu Jicong 已提交
784 785 786
  if (tDecodeSStreamTask(&decoder, pTask) < 0) {
    ASSERT(0);
  }
L
Liu Jicong 已提交
787 788
  tCoderClear(&decoder);

L
Liu Jicong 已提交
789
  // exec
L
Liu Jicong 已提交
790 791 792
  if (tqExpandTask(pTq, pTask, 4) < 0) {
    ASSERT(0);
  }
L
Liu Jicong 已提交
793 794

  // sink
L
Liu Jicong 已提交
795
  pTask->ahandle = pTq->pVnode;
L
Liu Jicong 已提交
796 797 798
  if (pTask->sinkType == TASK_SINK__SMA) {
    pTask->smaSink.smaHandle = smaHandleRes;
  }
L
Liu Jicong 已提交
799

L
Liu Jicong 已提交
800 801 802 803
  taosHashPut(pTq->pStreamTasks, &pTask->taskId, sizeof(int32_t), pTask, sizeof(SStreamTask));

  return 0;
}
L
Liu Jicong 已提交
804

L
Liu Jicong 已提交
805
int32_t tqProcessStreamTrigger(STQ* pTq, void* data, int32_t dataLen, int32_t workerId) {
L
Liu Jicong 已提交
806 807 808 809 810 811 812
  void* pIter = NULL;

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

L
Liu Jicong 已提交
813
    if (streamExecTask(pTask, &pTq->pVnode->msgCb, data, STREAM_DATA_TYPE_SUBMIT_BLOCK, workerId) < 0) {
L
Liu Jicong 已提交
814
      // TODO
L
Liu Jicong 已提交
815 816 817 818 819
    }
  }
  return 0;
}

L
Liu Jicong 已提交
820
int32_t tqProcessTaskExec(STQ* pTq, char* msg, int32_t msgLen, int32_t workerId) {
L
Liu Jicong 已提交
821
  SStreamTaskExecReq req;
L
Liu Jicong 已提交
822
  tDecodeSStreamTaskExecReq(msg, &req);
L
Liu Jicong 已提交
823

L
Liu Jicong 已提交
824 825 826
  int32_t taskId = req.taskId;
  ASSERT(taskId);

L
Liu Jicong 已提交
827
  SStreamTask* pTask = taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
828
  ASSERT(pTask);
L
Liu Jicong 已提交
829

L
Liu Jicong 已提交
830
  if (streamExecTask(pTask, &pTq->pVnode->msgCb, req.data, STREAM_DATA_TYPE_SSDATA_BLOCK, workerId) < 0) {
L
Liu Jicong 已提交
831
    // TODO
L
Liu Jicong 已提交
832
  }
L
Liu Jicong 已提交
833 834
  return 0;
}