tq.c 25.0 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;
    }
31 32 33
    if (streamInit() < 0) {
      return -1;
    }
L
Liu Jicong 已提交
34
    atomic_store_8(&tqMgmt.inited, 1);
35
  }
36

L
Liu Jicong 已提交
37 38
  return 0;
}
L
Liu Jicong 已提交
39

40
void tqCleanUp() {
L
Liu Jicong 已提交
41 42 43 44 45 46 47 48
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&tqMgmt.inited, 1, 2);
    if (old != 2) break;
  }

  if (old == 1) {
    taosTmrCleanUp(tqMgmt.timer);
L
Liu Jicong 已提交
49
    streamCleanUp();
L
Liu Jicong 已提交
50 51
    atomic_store_8(&tqMgmt.inited, 0);
  }
52
}
L
Liu Jicong 已提交
53

L
Liu Jicong 已提交
54
STQ* tqOpen(const char* path, SVnode* pVnode) {
55
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
L
Liu Jicong 已提交
56
  if (pTq == NULL) {
L
Liu Jicong 已提交
57
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
58 59
    return NULL;
  }
H
Hongze Cheng 已提交
60
  pTq->path = strdup(path);
L
Liu Jicong 已提交
61
  pTq->pVnode = pVnode;
62

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

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

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

L
Liu Jicong 已提交
69 70
  pTq->pAlterInfo = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);

L
Liu Jicong 已提交
71
  if (tqMetaOpen(pTq) < 0) {
72 73 74
    ASSERT(0);
  }

75 76 77 78
  if (tqOffsetOpen(pTq) < 0) {
    ASSERT(0);
  }

L
Liu Jicong 已提交
79 80
  return pTq;
}
L
Liu Jicong 已提交
81

L
Liu Jicong 已提交
82
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
83
  if (pTq) {
84
    tqOffsetClose(pTq->pOffsetStore);
L
Liu Jicong 已提交
85
    taosHashCleanup(pTq->handles);
86 87 88 89 90 91 92
    void* pIter = NULL;
    while (1) {
      pIter = taosHashIterate(pTq->pStreamTasks, pIter);
      if (pIter == NULL) break;
      SStreamTask* pTask = *(SStreamTask**)pIter;
      tFreeSStreamTask(pTask);
    }
L
Liu Jicong 已提交
93 94
    taosHashCleanup(pTq->pStreamTasks);
    taosHashCleanup(pTq->pushMgr);
L
Liu Jicong 已提交
95
    taosHashCleanup(pTq->pAlterInfo);
96
    taosMemoryFree(pTq->path);
L
Liu Jicong 已提交
97
    tqMetaClose(pTq);
wafwerar's avatar
wafwerar 已提交
98
    taosMemoryFree(pTq);
H
Hongze Cheng 已提交
99
  }
L
Liu Jicong 已提交
100
}
L
Liu Jicong 已提交
101

L
Liu Jicong 已提交
102
int32_t tqSendMetaPollRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp) {
103 104 105 106 107 108 109
  int32_t len = 0;
  int32_t code = 0;
  tEncodeSize(tEncodeSMqMetaRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
110 111 112 113 114 115 116 117 118 119
  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));
120 121 122 123 124

  SEncoder encoder = {0};
  tEncoderInit(&encoder, abuf, len);
  tEncodeSMqMetaRsp(&encoder, pRsp);
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
125 126 127 128 129 130 131 132 133

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

134 135
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, res msg type %d, offset type:%d",
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
L
Liu Jicong 已提交
136 137 138 139

  return 0;
}

L
Liu Jicong 已提交
140 141 142 143 144 145 146 147 148 149
int32_t tqSendDataRsp(STQ* pTq, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqDataRsp* pRsp) {
  ASSERT(taosArrayGetSize(pRsp->blockData) == pRsp->blockNum);
  ASSERT(taosArrayGetSize(pRsp->blockDataLen) == pRsp->blockNum);

  if (pRsp->withSchema) {
    ASSERT(taosArrayGetSize(pRsp->blockSchema) == pRsp->blockNum);
  } else {
    ASSERT(taosArrayGetSize(pRsp->blockSchema) == 0);
  }

150 151 152 153 154 155
  if (pRsp->reqOffset.type == TMQ_OFFSET__LOG) {
    if (pRsp->blockNum > 0) {
      ASSERT(pRsp->rspOffset.version > pRsp->reqOffset.version);
    } else {
      ASSERT(pRsp->rspOffset.version >= pRsp->reqOffset.version);
    }
L
Liu Jicong 已提交
156 157
  }

wmmhello's avatar
wmmhello 已提交
158 159
  int32_t len = 0;
  int32_t code = 0;
L
Liu Jicong 已提交
160 161 162 163 164
  tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
165 166 167 168 169 170 171 172 173 174 175
  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));

wmmhello's avatar
wmmhello 已提交
176
  SEncoder encoder = {0};
L
Liu Jicong 已提交
177
  tEncoderInit(&encoder, abuf, len);
L
Liu Jicong 已提交
178
  tEncodeSMqDataRsp(&encoder, pRsp);
wmmhello's avatar
wmmhello 已提交
179
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
180 181

  SRpcMsg rsp = {
L
Liu Jicong 已提交
182 183 184 185 186
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
L
Liu Jicong 已提交
187
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
188

wmmhello's avatar
wmmhello 已提交
189 190
  char buf1[80] = {0};
  char buf2[80] = {0};
L
Liu Jicong 已提交
191 192
  tFormatOffset(buf1, 80, &pRsp->reqOffset);
  tFormatOffset(buf2, 80, &pRsp->rspOffset);
S
Shengliang Guan 已提交
193
  tqDebug("vgId:%d, from consumer:%" PRId64 ", (epoch %d) send rsp, block num: %d, reqOffset:%s, rspOffset:%s",
L
Liu Jicong 已提交
194
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2);
L
Liu Jicong 已提交
195 196 197 198

  return 0;
}

199 200 201 202 203 204 205 206 207 208
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);

L
Liu Jicong 已提交
209
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA) {
L
Liu Jicong 已提交
210 211
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
            offset.subKey, TD_VID(pTq->pVnode), offset.val.uid, offset.val.ts);
L
Liu Jicong 已提交
212
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
S
Shengliang Guan 已提交
213
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, offset.subKey,
L
Liu Jicong 已提交
214
            TD_VID(pTq->pVnode), offset.val.version);
215 216 217
  } else {
    ASSERT(0);
  }
218 219 220 221 222 223
  /*STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, offset.subKey);*/
  /*if (pOffset != NULL) {*/
  /*if (pOffset->val.type == TMQ_OFFSET__LOG && pOffset->val.version < offset.val.version) {*/
  if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
    ASSERT(0);
    return -1;
224
  }
225 226 227

  if (offset.val.type == TMQ_OFFSET__LOG) {
    STqHandle* pHandle = taosHashGet(pTq->handles, offset.subKey, strlen(offset.subKey));
L
Liu Jicong 已提交
228 229 230 231 232
    if (pHandle) {
      if (walRefVer(pHandle->pRef, offset.val.version) < 0) {
        ASSERT(0);
        return -1;
      }
233 234 235
    }
  }

236 237
  /*}*/
  /*}*/
238 239 240 241

  return 0;
}

L
Liu Jicong 已提交
242
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
L
Liu Jicong 已提交
243 244
  void* pIter = NULL;
  while (1) {
L
Liu Jicong 已提交
245
    pIter = taosHashIterate(pTq->pAlterInfo, pIter);
L
Liu Jicong 已提交
246
    if (pIter == NULL) break;
L
Liu Jicong 已提交
247 248 249
    SCheckAlterInfo* pCheck = (SCheckAlterInfo*)pIter;
    if (pCheck->ntbUid == tbUid) {
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
L
Liu Jicong 已提交
250
      for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
251 252 253
        int16_t forbidColId = *(int16_t*)taosArrayGet(pCheck->colIdList, i);
        if (forbidColId == colId) {
          taosHashCancelIterate(pTq->pAlterInfo, pIter);
L
Liu Jicong 已提交
254 255 256 257 258 259 260 261
          return -1;
        }
      }
    }
  }
  return 0;
}

L
Liu Jicong 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static int32_t tqInitDataRsp(SMqDataRsp* pRsp, const SMqPollReq* pReq, int8_t subType) {
  pRsp->reqOffset = pReq->reqOffset;

  pRsp->blockData = taosArrayInit(0, sizeof(void*));
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));

  if (pRsp->blockData == NULL || pRsp->blockDataLen == NULL) {
    return -1;
  }

  pRsp->withTbName = pReq->withTbName;
  if (pRsp->withTbName) {
    pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
    if (pRsp->blockTbName == NULL) {
      // TODO free
      return -1;
    }
  }

  if (subType == TOPIC_SUB_TYPE__COLUMN) {
    pRsp->withSchema = false;
  } else {
    pRsp->withSchema = true;
    pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
    if (pRsp->blockSchema == NULL) {
      // TODO free
      return -1;
    }
  }
  return 0;
}

L
Liu Jicong 已提交
294
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
295 296 297 298 299 300 301 302 303 304 305
  SMqPollReq*  pReq = pMsg->pCont;
  int64_t      consumerId = pReq->consumerId;
  int32_t      reqEpoch = pReq->epoch;
  int32_t      code = 0;
  STqOffsetVal reqOffset = pReq->reqOffset;
  STqOffsetVal fetchOffsetNew;

  // 1.find handle
  STqHandle* pHandle = taosHashGet(pTq->handles, pReq->subKey, strlen(pReq->subKey));
  /*ASSERT(pHandle);*/
  if (pHandle == NULL) {
S
Shengliang Guan 已提交
306 307
    tqError("tmq poll: no consumer handle for consumer:%" PRId64 ", in vgId:%d, subkey %s", consumerId,
            TD_VID(pTq->pVnode), pReq->subKey);
L
Liu Jicong 已提交
308 309 310 311 312
    return -1;
  }

  // check rebalance
  if (pHandle->consumerId != consumerId) {
S
Shengliang Guan 已提交
313 314
    tqError("tmq poll: consumer handle mismatch for consumer:%" PRId64
            ", in vgId:%d, subkey %s, handle consumer id %" PRId64,
L
Liu Jicong 已提交
315 316 317 318 319 320 321 322 323 324
            consumerId, TD_VID(pTq->pVnode), pReq->subKey, pHandle->consumerId);
    return -1;
  }

  // update epoch if need
  int32_t consumerEpoch = atomic_load_32(&pHandle->epoch);
  while (consumerEpoch < reqEpoch) {
    consumerEpoch = atomic_val_compare_exchange_32(&pHandle->epoch, consumerEpoch, reqEpoch);
  }

L
Liu Jicong 已提交
325 326
  char buf[80];
  tFormatOffset(buf, 80, &reqOffset);
S
Shengliang Guan 已提交
327
  tqDebug("tmq poll: consumer %" PRId64 " (epoch %d), subkey %s, recv poll req in vg %d, req offset %s", consumerId,
L
Liu Jicong 已提交
328 329
          pReq->epoch, pHandle->subKey, TD_VID(pTq->pVnode), buf);

wmmhello's avatar
wmmhello 已提交
330
  SMqMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
331 332 333
  SMqDataRsp dataRsp = {0};
  tqInitDataRsp(&dataRsp, pReq, pHandle->execHandle.subType);

L
Liu Jicong 已提交
334 335 336 337 338 339 340
  // 2.reset offset if needed
  if (reqOffset.type > 0) {
    fetchOffsetNew = reqOffset;
  } else {
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, pReq->subKey);
    if (pOffset != NULL) {
      fetchOffsetNew = pOffset->val;
L
Liu Jicong 已提交
341 342
      char formatBuf[80];
      tFormatOffset(formatBuf, 80, &fetchOffsetNew);
L
Liu Jicong 已提交
343 344
      tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, offset reset to %s", consumerId, pHandle->subKey,
              TD_VID(pTq->pVnode), formatBuf);
L
Liu Jicong 已提交
345 346
    } else {
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEAST) {
347 348
        if (pReq->useSnapshot){
          if (pHandle->fetchMeta){
wmmhello's avatar
wmmhello 已提交
349
            tqOffsetResetToMeta(&fetchOffsetNew, 0);
L
Liu Jicong 已提交
350
          } else {
351
            tqOffsetResetToData(&fetchOffsetNew, 0, 0);
L
Liu Jicong 已提交
352 353 354 355 356
          }
        } else {
          tqOffsetResetToLog(&fetchOffsetNew, walGetFirstVer(pTq->pVnode->pWal));
        }
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
L
Liu Jicong 已提交
357
        tqOffsetResetToLog(&dataRsp.rspOffset, walGetLastVer(pTq->pVnode->pWal));
S
Shengliang Guan 已提交
358 359
        tqDebug("tmq poll: consumer %" PRId64 ", subkey %s, vg %d, offset reset to %" PRId64, consumerId,
                pHandle->subKey, TD_VID(pTq->pVnode), dataRsp.rspOffset.version);
L
Liu Jicong 已提交
360 361 362
        if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
          code = -1;
        }
L
Liu Jicong 已提交
363
        goto OVER;
L
Liu Jicong 已提交
364
      } else if (reqOffset.type == TMQ_OFFSET__RESET_NONE) {
365 366
        tqError("tmq poll: subkey %s, no offset committed for consumer %" PRId64
                " in vg %d, subkey %s, reset none failed",
L
Liu Jicong 已提交
367
                pHandle->subKey, consumerId, TD_VID(pTq->pVnode), pReq->subKey);
L
Liu Jicong 已提交
368
        terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
L
Liu Jicong 已提交
369 370
        code = -1;
        goto OVER;
L
Liu Jicong 已提交
371 372 373 374
      }
    }
  }

wmmhello's avatar
wmmhello 已提交
375
  tqScan(pTq, pHandle, &dataRsp, &metaRsp, &fetchOffsetNew);
L
Liu Jicong 已提交
376

wmmhello's avatar
wmmhello 已提交
377
  if(dataRsp.blockNum != 0){
378 379 380 381 382 383
    if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
      code = -1;
    }
    goto OVER;
  }

wmmhello's avatar
wmmhello 已提交
384
  if(metaRsp.metaRspLen > 0){
385 386 387 388 389 390
    if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) {
      code = -1;
    }
    goto OVER;
  }

wmmhello's avatar
wmmhello 已提交
391 392
  tqDebug("tmq poll: consumer %ld, subkey %s, vg %d, no data", consumerId, pHandle->subKey,
          TD_VID(pTq->pVnode));
L
Liu Jicong 已提交
393
OVER:
wmmhello's avatar
wmmhello 已提交
394

L
Liu Jicong 已提交
395 396 397 398 399 400 401 402 403 404 405 406
  // TODO wrap in destroy func
  taosArrayDestroy(dataRsp.blockDataLen);
  taosArrayDestroyP(dataRsp.blockData, (FDelete)taosMemoryFree);

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

  if (dataRsp.withTbName) {
    taosArrayDestroyP(dataRsp.blockTbName, (FDelete)taosMemoryFree);
  }

wmmhello's avatar
wmmhello 已提交
407 408
  taosMemoryFreeClear(metaRsp.metaRsp);

L
Liu Jicong 已提交
409 410 411
  return code;
}

L
Liu Jicong 已提交
412 413
int32_t tqProcessVgDeleteReq(STQ* pTq, char* msg, int32_t msgLen) {
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
L
Liu Jicong 已提交
414

L
Liu Jicong 已提交
415
  int32_t code = taosHashRemove(pTq->handles, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
416
  ASSERT(code == 0);
417

L
Liu Jicong 已提交
418 419
  tqOffsetDelete(pTq->pOffsetStore, pReq->subKey);

L
Liu Jicong 已提交
420
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
421 422
    ASSERT(0);
  }
L
Liu Jicong 已提交
423
  return 0;
L
Liu Jicong 已提交
424 425
}

L
Liu Jicong 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
int32_t tqProcessCheckAlterInfoReq(STQ* pTq, char* msg, int32_t msgLen) {
  SCheckAlterInfo info = {0};
  SDecoder        decoder;
  tDecoderInit(&decoder, msg, msgLen);
  if (tDecodeSCheckAlterInfo(&decoder, &info) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  tDecoderClear(&decoder);
  if (taosHashPut(pTq->pAlterInfo, info.topic, strlen(info.topic), &info, sizeof(SCheckAlterInfo)) < 0) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

L
Liu Jicong 已提交
442
int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
443
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
444 445
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
L
Liu Jicong 已提交
446 447
  STqHandle* pHandle = taosHashGet(pTq->handles, req.subKey, strlen(req.subKey));
  if (pHandle == NULL) {
L
Liu Jicong 已提交
448 449 450 451
    if (req.oldConsumerId != -1) {
      tqError("vgId:%d, build new consumer handle %s for consumer %d, but old consumerId is %ld", req.vgId, req.subKey,
              req.newConsumerId, req.oldConsumerId);
    }
L
Liu Jicong 已提交
452
    ASSERT(req.newConsumerId != -1);
L
Liu Jicong 已提交
453 454
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
455 456
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
457 458 459
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
460

L
Liu Jicong 已提交
461
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
462
    pHandle->fetchMeta = req.withMeta;
463 464 465 466
    // TODO version should be assigned and refed during preprocess
    SWalRef* pRef = walRefCommittedVer(pTq->pVnode->pWal);
    if (pRef == NULL) {
      ASSERT(0);
L
Liu Jicong 已提交
467
      return -1;
468 469 470
    }
    int64_t ver = pRef->refVer;
    pHandle->pRef = pRef;
L
Liu Jicong 已提交
471

472 473 474 475 476 477 478 479
    SReadHandle handle = {
        .meta = pTq->pVnode->pMeta,
        .vnode = pTq->pVnode,
        .initTableReader = true,
        .initTqReader = true,
        .version = ver,
    };

L
Liu Jicong 已提交
480
    if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
481
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
482
      pHandle->snapshotVer = ver;
L
Liu Jicong 已提交
483
      req.qmsg = NULL;
484 485

      pHandle->execHandle.task =
wmmhello's avatar
wmmhello 已提交
486
          qCreateQueueExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle, NULL,
L
Liu Jicong 已提交
487
                                   &pHandle->execHandle.pSchemaWrapper);
488
      ASSERT(pHandle->execHandle.task);
L
Liu Jicong 已提交
489
      void* scanner = NULL;
490
      qExtractStreamScanner(pHandle->execHandle.task, &scanner);
L
Liu Jicong 已提交
491 492 493
      ASSERT(scanner);
      pHandle->execHandle.pExecReader = qExtractReaderFromStreamScanner(scanner);
      ASSERT(pHandle->execHandle.pExecReader);
L
Liu Jicong 已提交
494
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
L
Liu Jicong 已提交
495
      pHandle->execHandle.pExecReader = tqOpenReader(pTq->pVnode);
L
Liu Jicong 已提交
496
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
497
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
498 499 500 501
      buildSnapContext(handle.meta, handle.version, 0, pHandle->execHandle.subType, pHandle->fetchMeta, (SSnapContext **)(&handle.sContext));
      handle.tqReader = pHandle->execHandle.pExecReader;
      handle.pWalReader = ((STqReader*)handle.tqReader)->pWalReader;
      handle.pFilterOutTbUid = pHandle->execHandle.execDb.pFilterOutTbUid;
502 503 504

      pHandle->execHandle.task =
          qCreateQueueExecTaskInfo(NULL, &handle, NULL, NULL);
L
Liu Jicong 已提交
505
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
L
Liu Jicong 已提交
506
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
H
Hongze Cheng 已提交
507
      vnodeGetCtbIdList(pTq->pVnode, req.suid, tbUidList);
508
      tqDebug("vgId:%d, tq try to get all ctb, suid:%" PRId64, pTq->pVnode->config.vgId, req.suid);
L
Liu Jicong 已提交
509 510
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
S
Shengliang Guan 已提交
511
        tqDebug("vgId:%d, idx %d, uid:%" PRId64, TD_VID(pTq->pVnode), i, tbUid);
L
Liu Jicong 已提交
512
      }
L
Liu Jicong 已提交
513 514
      pHandle->execHandle.pExecReader = tqOpenReader(pTq->pVnode);
      tqReaderSetTbUidList(pHandle->execHandle.pExecReader, tbUidList);
L
Liu Jicong 已提交
515
      taosArrayDestroy(tbUidList);
wmmhello's avatar
wmmhello 已提交
516 517 518 519 520 521

      buildSnapContext(handle.meta, handle.version, req.suid, pHandle->execHandle.subType, pHandle->fetchMeta, (SSnapContext **)(&handle.sContext));
      handle.tqReader = pHandle->execHandle.pExecReader;
      handle.pWalReader = ((STqReader*)handle.tqReader)->pWalReader;
      pHandle->execHandle.task =
          qCreateQueueExecTaskInfo(NULL, &handle, NULL, NULL);
L
Liu Jicong 已提交
522
    }
L
Liu Jicong 已提交
523
    taosHashPut(pTq->handles, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
S
Shengliang Guan 已提交
524
    tqDebug("try to persist handle %s consumer %" PRId64, req.subKey, pHandle->consumerId);
L
Liu Jicong 已提交
525 526
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
P
plum-lihui 已提交
527
      ASSERT(0);
L
Liu Jicong 已提交
528
    }
L
Liu Jicong 已提交
529
  } else {
L
Liu Jicong 已提交
530
    /*ASSERT(pExec->consumerId == req.oldConsumerId);*/
L
Liu Jicong 已提交
531
    // TODO handle qmsg and exec modification
L
Liu Jicong 已提交
532 533 534
    atomic_store_32(&pHandle->epoch, -1);
    atomic_store_64(&pHandle->consumerId, req.newConsumerId);
    atomic_add_fetch_32(&pHandle->epoch, 1);
L
Liu Jicong 已提交
535 536 537
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
    }
L
Liu Jicong 已提交
538
  }
L
Liu Jicong 已提交
539

L
Liu Jicong 已提交
540
  return 0;
L
Liu Jicong 已提交
541
}
542

L
Liu Jicong 已提交
543 544
int32_t tqExpandTask(STQ* pTq, SStreamTask* pTask) {
  int32_t code = 0;
545 546

  if (pTask->taskLevel == TASK_LEVEL__AGG) {
L
Liu Jicong 已提交
547 548
    ASSERT(taosArrayGetSize(pTask->childEpInfo) != 0);
  }
L
Liu Jicong 已提交
549

L
Liu Jicong 已提交
550
  pTask->schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
551 552 553

  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
L
Liu Jicong 已提交
554 555 556 557 558 559

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) {
    code = -1;
    goto FAIL;
  }

L
Liu Jicong 已提交
560 561 562
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

563 564
  pTask->pMsgCb = &pTq->pVnode->msgCb;

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  // expand executor
  if (pTask->taskLevel == TASK_LEVEL__SOURCE) {
    SReadHandle handle = {
        .meta = pTq->pVnode->pMeta,
        .vnode = pTq->pVnode,
        .initTqReader = 1,
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle);
    ASSERT(pTask->exec.executor);
  } else if (pTask->taskLevel == TASK_LEVEL__AGG) {
    SReadHandle mgHandle = {
        .vnode = NULL,
        .numOfVgroups = (int32_t)taosArrayGetSize(pTask->childEpInfo),
    };
    pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &mgHandle);
L
Liu Jicong 已提交
580
    ASSERT(pTask->exec.executor);
L
Liu Jicong 已提交
581
  }
L
Liu Jicong 已提交
582 583

  // sink
L
Liu Jicong 已提交
584
  /*pTask->ahandle = pTq->pVnode;*/
585
  if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
586
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
587
    pTask->smaSink.smaSink = smaHandleRes;
588
  } else if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
589 590 591
    pTask->tbSink.vnode = pTq->pVnode;
    pTask->tbSink.tbSinkFunc = tqTableSink;

L
Liu Jicong 已提交
592 593
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
594

L
Liu Jicong 已提交
595
    pTask->tbSink.pTSchema =
C
Cary Xu 已提交
596
        tdGetSTSChemaFromSSChema(pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols, 1);
L
Liu Jicong 已提交
597
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
598
  }
599 600 601

  streamSetupTrigger(pTask);

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

L
Liu Jicong 已提交
605 606 607 608 609 610
FAIL:
  if (pTask->inputQueue) streamQueueClose(pTask->inputQueue);
  if (pTask->outputQueue) streamQueueClose(pTask->outputQueue);
  // TODO free executor
  return code;
}
L
Liu Jicong 已提交
611

L
Liu Jicong 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
int32_t tqProcessTaskDeployReq(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);
    goto FAIL;
  }
  tDecoderClear(&decoder);

  if (tqExpandTask(pTq, pTask) < 0) {
    goto FAIL;
  }

  taosHashPut(pTq->pStreamTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*));
630

L
Liu Jicong 已提交
631
  return 0;
L
Liu Jicong 已提交
632

L
Liu Jicong 已提交
633
FAIL:
L
Liu Jicong 已提交
634
  if (pTask) taosMemoryFree(pTask);
L
Liu Jicong 已提交
635 636
  return -1;
}
L
Liu Jicong 已提交
637

L
Liu Jicong 已提交
638
int32_t tqProcessStreamTrigger(STQ* pTq, SSubmitReq* pReq, int64_t ver) {
L
Liu Jicong 已提交
639 640 641
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
642

L
Liu Jicong 已提交
643
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
644
  if (pSubmit == NULL) {
L
Liu Jicong 已提交
645 646
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    qError("failed to create data submit for stream since out of memory");
L
Liu Jicong 已提交
647 648 649 650 651 652
    failed = true;
  }

  while (1) {
    pIter = taosHashIterate(pTq->pStreamTasks, pIter);
    if (pIter == NULL) break;
653
    SStreamTask* pTask = *(SStreamTask**)pIter;
654
    if (pTask->taskLevel != TASK_LEVEL__SOURCE) continue;
L
Liu Jicong 已提交
655

S
Shengliang Guan 已提交
656
    qDebug("data submit enqueue stream task: %d, ver: %" PRId64, pTask->taskId, ver);
L
Liu Jicong 已提交
657

L
Liu Jicong 已提交
658 659
    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pSubmit) < 0) {
L
Liu Jicong 已提交
660
        qError("stream task input failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
661 662 663
        continue;
      }

L
Liu Jicong 已提交
664
      if (streamSchedExec(pTask) < 0) {
L
Liu Jicong 已提交
665
        qError("stream task launch failed, task id %d", pTask->taskId);
L
Liu Jicong 已提交
666 667
        continue;
      }
L
Liu Jicong 已提交
668
    } else {
L
Liu Jicong 已提交
669
      streamTaskInputFail(pTask);
L
Liu Jicong 已提交
670 671 672
    }
  }

L
Liu Jicong 已提交
673
  if (pSubmit) {
L
Liu Jicong 已提交
674
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
675
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
676
  }
L
Liu Jicong 已提交
677 678

  return failed ? -1 : 0;
L
Liu Jicong 已提交
679 680
}

L
Liu Jicong 已提交
681
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
682
  //
L
Liu Jicong 已提交
683 684
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
L
Liu Jicong 已提交
685 686 687
  SStreamTask**      ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    streamProcessRunReq(*ppTask);
L
Liu Jicong 已提交
688
    return 0;
689 690
  } else {
    return -1;
L
Liu Jicong 已提交
691
  }
L
Liu Jicong 已提交
692 693
}

L
Liu Jicong 已提交
694
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg, bool exec) {
L
Liu Jicong 已提交
695
  ASSERT(0);
696 697 698 699 700
  char*              msgStr = pMsg->pCont;
  char*              msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t            msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamDispatchReq req;
  SDecoder           decoder;
L
Liu Jicong 已提交
701
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
702
  tDecodeStreamDispatchReq(&decoder, &req);
L
Liu Jicong 已提交
703 704 705
  int32_t       taskId = req.taskId;
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
706 707 708 709
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
L
Liu Jicong 已提交
710
    streamProcessDispatchReq(*ppTask, &req, &rsp, exec);
L
Liu Jicong 已提交
711
    return 0;
712 713
  } else {
    return -1;
L
Liu Jicong 已提交
714
  }
L
Liu Jicong 已提交
715 716 717 718 719
}

int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
L
Liu Jicong 已提交
720 721 722
  SStreamTask**          ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    streamProcessRecoverReq(*ppTask, pReq, pMsg);
L
Liu Jicong 已提交
723
    return 0;
724 725
  } else {
    return -1;
L
Liu Jicong 已提交
726
  }
L
Liu Jicong 已提交
727 728 729
}

int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
730
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
L
Liu Jicong 已提交
731
  int32_t             taskId = pRsp->taskId;
L
Liu Jicong 已提交
732 733 734
  SStreamTask**       ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    streamProcessDispatchRsp(*ppTask, pRsp);
L
Liu Jicong 已提交
735
    return 0;
736 737
  } else {
    return -1;
L
Liu Jicong 已提交
738
  }
L
Liu Jicong 已提交
739 740 741 742
}

int32_t tqProcessTaskRecoverRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverRsp* pRsp = pMsg->pCont;
L
Liu Jicong 已提交
743
  int32_t                taskId = pRsp->rspTaskId;
L
Liu Jicong 已提交
744 745 746
  SStreamTask**          ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    streamProcessRecoverRsp(*ppTask, pRsp);
L
Liu Jicong 已提交
747
    return 0;
748 749
  } else {
    return -1;
L
Liu Jicong 已提交
750
  }
L
Liu Jicong 已提交
751
}
L
Liu Jicong 已提交
752 753 754

int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
L
Liu Jicong 已提交
755

L
Liu Jicong 已提交
756 757
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &pReq->taskId, sizeof(int32_t));
  if (ppTask) {
L
Liu Jicong 已提交
758
    SStreamTask* pTask = *ppTask;
759
    taosHashRemove(pTq->pStreamTasks, &pReq->taskId, sizeof(int32_t));
L
Liu Jicong 已提交
760
    atomic_store_8(&pTask->taskStatus, TASK_STATUS__DROPPING);
761
  }
L
Liu Jicong 已提交
762 763 764 765 766 767
  // todo
  // clear queue
  // push drop req into queue
  // launch exec to free memory
  // remove from hash
  return 0;
L
Liu Jicong 已提交
768
}
L
Liu Jicong 已提交
769 770 771 772 773 774 775 776 777

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);
L
Liu Jicong 已提交
778 779 780 781 782 783 784 785 786 787
  int32_t       taskId = req.dstTaskId;
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
    streamProcessRetrieveReq(*ppTask, &req, &rsp);
  } else {
    return -1;
L
Liu Jicong 已提交
788 789 790 791 792 793 794 795
  }
  return 0;
}

int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) {
  //
  return 0;
}
L
Liu Jicong 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

void vnodeEnqueueStreamMsg(SVnode* pVnode, SRpcMsg* pMsg) {
  STQ*    pTq = pVnode->pTq;
  char*   msgStr = pMsg->pCont;
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
  int32_t code = 0;

  SStreamDispatchReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
    code = TSDB_CODE_MSG_DECODE_ERROR;
    goto FAIL;
  }

  int32_t       taskId = req.taskId;
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    SRpcMsg rsp = {
        .info = pMsg->info,
        .code = 0,
    };
    streamProcessDispatchReq(*ppTask, &req, &rsp, false);
    return;
  }
FAIL:
  if (pMsg->info.handle == NULL) return;
  SRpcMsg rsp = {
      .code = code,
      .info = pMsg->info,
  };
  tmsgSendRsp(&rsp);
}