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

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

  int32_t len;
  int32_t code;
  tEncodeSize(tEncodeSMqDataRsp, pRsp, len, code);
  if (code < 0) {
    return -1;
  }
  int32_t tlen = sizeof(SMqRspHead) + len;
L
Liu Jicong 已提交
133 134 135 136 137 138 139 140 141 142 143
  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));

L
Liu Jicong 已提交
144
  SEncoder encoder;
L
Liu Jicong 已提交
145
  tEncoderInit(&encoder, abuf, len);
L
Liu Jicong 已提交
146 147 148 149
  tEncodeSMqDataRsp(&encoder, pRsp);
  /*tEncodeSMqDataBlkRsp(&abuf, pRsp);*/

  SRpcMsg rsp = {
L
Liu Jicong 已提交
150 151 152 153 154
      .info = pMsg->info,
      .pCont = buf,
      .contLen = tlen,
      .code = 0,
  };
L
Liu Jicong 已提交
155
  tmsgSendRsp(&rsp);
L
Liu Jicong 已提交
156

L
Liu Jicong 已提交
157 158 159 160 161 162
  char buf1[50];
  char buf2[50];
  tFormatOffset(buf1, 50, &pRsp->reqOffset);
  tFormatOffset(buf2, 50, &pRsp->rspOffset);
  tqDebug("vg %d from consumer %ld (epoch %d) send rsp, block num: %d, reqOffset: %s, rspOffset: %s",
          TD_VID(pTq->pVnode), pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2);
L
Liu Jicong 已提交
163 164 165 166

  return 0;
}

167 168 169 170 171 172 173 174 175 176
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 已提交
177
  if (offset.val.type == TMQ_OFFSET__SNAPSHOT_DATA) {
L
Liu Jicong 已提交
178
    tqDebug("receive offset commit msg to %s on vg %d, offset(type:snapshot) uid: %ld, ts: %ld", offset.subKey,
L
Liu Jicong 已提交
179 180
            TD_VID(pTq->pVnode), offset.val.uid, offset.val.ts);
  } else if (offset.val.type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
181
    tqDebug("receive offset commit msg to %s on vg %d, offset(type:log) version: %ld", offset.subKey,
L
Liu Jicong 已提交
182
            TD_VID(pTq->pVnode), offset.val.version);
183 184 185
  } else {
    ASSERT(0);
  }
L
Liu Jicong 已提交
186
  STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, offset.subKey);
L
Liu Jicong 已提交
187
  if (pOffset == NULL || pOffset->val.version < offset.val.version) {
L
Liu Jicong 已提交
188 189 190 191
    if (tqOffsetWrite(pTq->pOffsetStore, &offset) < 0) {
      ASSERT(0);
      return -1;
    }
192 193 194 195 196
  }

  return 0;
}

L
Liu Jicong 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
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;
}

static int32_t tqInitMetaRsp(SMqMetaRsp* pRsp, const SMqPollReq* pReq) { return 0; }

int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) {
  SMqPollReq*  pReq = pMsg->pCont;
  int64_t      consumerId = pReq->consumerId;
  int64_t      timeout = pReq->timeout;
  int32_t      reqEpoch = pReq->epoch;
  int32_t      code = 0;
  STqOffsetVal reqOffset = pReq->reqOffset;
  STqOffsetVal fetchOffsetNew;

  // 1.find handle
  char buf[50];
  tFormatOffset(buf, 50, &reqOffset);
  tqDebug("tmq poll: consumer %ld (epoch %d) recv poll req in vg %d, req offset %s", consumerId, pReq->epoch,
          TD_VID(pTq->pVnode), buf);

  STqHandle* pHandle = taosHashGet(pTq->handles, pReq->subKey, strlen(pReq->subKey));
  /*ASSERT(pHandle);*/
  if (pHandle == NULL) {
    tqError("tmq poll: no consumer handle for consumer %ld in vg %d, subkey %s", consumerId, TD_VID(pTq->pVnode),
            pReq->subKey);
    return -1;
  }

  // check rebalance
  if (pHandle->consumerId != consumerId) {
    tqError("tmq poll: consumer handle mismatch for consumer %ld in vg %d, subkey %s, handle consumer id %ld",
            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);
  }

  // 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;
      char formatBuf[50];
      tFormatOffset(formatBuf, 50, &fetchOffsetNew);
      tqDebug("tmq poll: consumer %ld, offset reset to %s", consumerId, formatBuf);
    } else {
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEAST) {
        if (pReq->useSnapshot) {
          if (!pHandle->fetchMeta) {
            tqOffsetResetToData(&fetchOffsetNew, 0, 0);
          } else {
            // reset to meta
            ASSERT(0);
          }
        } else {
          tqOffsetResetToLog(&fetchOffsetNew, walGetFirstVer(pTq->pVnode->pWal));
        }
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
        tqOffsetResetToLog(&fetchOffsetNew, walGetLastVer(pTq->pVnode->pWal));
      } else if (reqOffset.type == TMQ_OFFSET__RESET_NONE) {
        tqError("tmq poll: no offset committed for consumer %ld in vg %d, subkey %s, reset none failed", consumerId,
                TD_VID(pTq->pVnode), pReq->subKey);
        terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
        return -1;
      }
    }
  }

  // 3.query
  SMqDataRsp dataRsp = {0};
  tqInitDataRsp(&dataRsp, pReq, pHandle->execHandle.subType);

  if (fetchOffsetNew.type == TMQ_OFFSET__LOG) {
    int64_t   fetchVer = fetchOffsetNew.version + 1;
    SWalHead* pHeadWithCkSum = taosMemoryMalloc(sizeof(SWalHead) + 2048);
    if (pHeadWithCkSum == NULL) {
      return -1;
    }

    walSetReaderCapacity(pHandle->pWalReader, 2048);

    while (1) {
      consumerEpoch = atomic_load_32(&pHandle->epoch);
      if (consumerEpoch > reqEpoch) {
        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), fetchVer, consumerEpoch, reqEpoch);
        break;
      }

      if (tqFetchLog(pTq, pHandle, &fetchVer, &pHeadWithCkSum) < 0) {
        // TODO add push mgr

L
Liu Jicong 已提交
324 325
        tqOffsetResetToLog(&dataRsp.rspOffset, fetchVer);
        ASSERT(dataRsp.rspOffset.version >= dataRsp.reqOffset.version);
L
Liu Jicong 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
        if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
          code = -1;
        }
        goto OVER;
      }

      SWalReadHead* pHead = &pHeadWithCkSum->head;

      tqDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d offset %ld msgType %d", consumerId, pReq->epoch,
              TD_VID(pTq->pVnode), fetchVer, pHead->msgType);

      if (pHead->msgType == TDMT_VND_SUBMIT) {
        SSubmitReq* pCont = (SSubmitReq*)&pHead->body;

        if (tqLogScanExec(pTq, &pHandle->execHandle, pCont, &dataRsp, workerId) < 0) {
L
Liu Jicong 已提交
341
          /*ASSERT(0);*/
L
Liu Jicong 已提交
342 343 344 345 346
        }
        // TODO batch optimization:
        // TODO continue scan until meeting batch requirement
        if (dataRsp.blockNum > 0 /* threshold */) {
          tqOffsetResetToLog(&dataRsp.rspOffset, fetchVer);
L
Liu Jicong 已提交
347
          ASSERT(dataRsp.rspOffset.version >= dataRsp.reqOffset.version);
L
Liu Jicong 已提交
348 349 350 351

          if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
            code = -1;
          }
L
Liu Jicong 已提交
352
          goto OVER;
L
Liu Jicong 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        } else {
          fetchVer++;
        }

      } else {
        ASSERT(pHandle->fetchMeta);
        ASSERT(IS_META_MSG(pHead->msgType));
        tqInfo("fetch meta msg, ver: %ld, type: %d", pHead->version, pHead->msgType);
        SMqMetaRsp metaRsp = {0};
        metaRsp.reqOffset = pReq->reqOffset.version;
        /*tqOffsetResetToLog(&metaR)*/
        metaRsp.rspOffset = fetchVer;
        metaRsp.resMsgType = pHead->msgType;
        metaRsp.metaRspLen = pHead->bodyLen;
        metaRsp.metaRsp = pHead->body;
        if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) {
          code = -1;
          goto OVER;
        }
        code = 0;
        goto OVER;
      }
    }

    taosMemoryFree(pHeadWithCkSum);
  } else if (fetchOffsetNew.type == TMQ_OFFSET__SNAPSHOT_DATA) {
    // 1. set uid and ts
    // 2. get data (rebuild reader if needed)
    // 3. get new uid and ts

    char formatBuf[50];
    tFormatOffset(formatBuf, 50, &dataRsp.reqOffset);
    tqInfo("retrieve using snapshot req offset %s", formatBuf);
L
Liu Jicong 已提交
386
    if (tqScanSnapshot(pTq, &pHandle->execHandle, &dataRsp, fetchOffsetNew, workerId) < 0) {
L
Liu Jicong 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
      ASSERT(0);
    }

    // 4. send rsp
    if (dataRsp.blockNum != 0) {
      if (tqSendDataRsp(pTq, pMsg, pReq, &dataRsp) < 0) {
        code = -1;
      }
    }
  } else if (fetchOffsetNew.type == TMQ_OFFSET__SNAPSHOT_META) {
    ASSERT(0);
  }

OVER:
  // 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);
  }

  return code;
}

#if 0
L
fix  
Liu Jicong 已提交
417
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) {
L
Liu Jicong 已提交
418 419
  SMqPollReq* pReq = pMsg->pCont;
  int64_t     consumerId = pReq->consumerId;
L
Liu Jicong 已提交
420
  int64_t     timeout = pReq->timeout;
L
Liu Jicong 已提交
421 422
  int32_t     reqEpoch = pReq->epoch;
  int64_t     fetchOffset;
L
Liu Jicong 已提交
423
  int32_t     code = 0;
424

L
Liu Jicong 已提交
425
  // get offset to fetch message
L
Liu Jicong 已提交
426
  if (pReq->currentOffset >= 0) {
L
Liu Jicong 已提交
427
    fetchOffset = pReq->currentOffset + 1;
L
Liu Jicong 已提交
428 429 430
  } else {
    STqOffset* pOffset = tqOffsetRead(pTq->pOffsetStore, pReq->subKey);
    if (pOffset != NULL) {
L
Liu Jicong 已提交
431
      ASSERT(pOffset->val.type == TMQ_OFFSET__LOG);
L
Liu Jicong 已提交
432
      tqDebug("consumer %ld, restore offset of %s on vg %d, offset(type:log) version: %ld", consumerId, pReq->subKey,
L
Liu Jicong 已提交
433 434
              TD_VID(pTq->pVnode), pOffset->val.version);
      fetchOffset = pOffset->val.version + 1;
L
Liu Jicong 已提交
435 436 437 438 439 440
    } 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 已提交
441 442
        tqError("tmq poll: no offset committed for consumer %ld in vg %d, subkey %s", consumerId, TD_VID(pTq->pVnode),
                pReq->subKey);
L
Liu Jicong 已提交
443
        terrno = TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
L
Liu Jicong 已提交
444 445
        return -1;
      }
L
Liu Jicong 已提交
446
      tqDebug("consumer %ld, restore offset of %s on vg %d failed, config is %ld, set to %ld", consumerId, pReq->subKey,
L
Liu Jicong 已提交
447
              TD_VID(pTq->pVnode), pReq->currentOffset, fetchOffset);
L
Liu Jicong 已提交
448
    }
L
Liu Jicong 已提交
449 450
  }

L
Liu Jicong 已提交
451 452
  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 已提交
453

L
Liu Jicong 已提交
454
  STqHandle* pHandle = taosHashGet(pTq->handles, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
455 456
  /*ASSERT(pHandle);*/
  if (pHandle == NULL) {
L
Liu Jicong 已提交
457
    tqError("tmq poll: no consumer handle for consumer %ld in vg %d, subkey %s", consumerId, TD_VID(pTq->pVnode),
L
Liu Jicong 已提交
458 459 460
            pReq->subKey);
    return -1;
  }
461

L
Liu Jicong 已提交
462 463
  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 已提交
464
            consumerId, TD_VID(pTq->pVnode), pReq->subKey, pHandle->consumerId);
L
Liu Jicong 已提交
465 466
    return -1;
  }
L
Liu Jicong 已提交
467

L
Liu Jicong 已提交
468
  int32_t consumerEpoch = atomic_load_32(&pHandle->epoch);
L
Liu Jicong 已提交
469
  while (consumerEpoch < reqEpoch) {
L
Liu Jicong 已提交
470
    consumerEpoch = atomic_val_compare_exchange_32(&pHandle->epoch, consumerEpoch, reqEpoch);
L
Liu Jicong 已提交
471 472
  }

L
Liu Jicong 已提交
473
  SMqDataBlkRsp rsp = {0};
L
Liu Jicong 已提交
474
  rsp.reqOffset = pReq->currentOffset;
L
Liu Jicong 已提交
475

L
Liu Jicong 已提交
476
  rsp.blockData = taosArrayInit(0, sizeof(void*));
L
Liu Jicong 已提交
477
  rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t));
478

L
Liu Jicong 已提交
479 480 481 482
  if (rsp.blockData == NULL || rsp.blockDataLen == NULL) {
    return -1;
  }

L
Liu Jicong 已提交
483
  rsp.withTbName = pReq->withTbName;
484 485 486
  if (rsp.withTbName) {
    rsp.blockTbName = taosArrayInit(0, sizeof(void*));
  }
L
Liu Jicong 已提交
487

L
Liu Jicong 已提交
488 489 490 491
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
    rsp.withSchema = false;
  } else {
    rsp.withSchema = true;
L
Liu Jicong 已提交
492
    rsp.blockSchema = taosArrayInit(0, sizeof(void*));
493
  }
L
Liu Jicong 已提交
494

L
Liu Jicong 已提交
495 496
#if 1
  if (pReq->useSnapshot) {
497
    // TODO set ver into snapshot
L
Liu Jicong 已提交
498 499
    int64_t lastVer = walGetCommittedVer(pTq->pWal);
    if (rsp.reqOffset < lastVer) {
500
      tqInfo("retrieve using snapshot req offset %ld last ver %ld", rsp.reqOffset, lastVer);
L
Liu Jicong 已提交
501 502 503 504 505
      tqScanSnapshot(pTq, &pHandle->execHandle, &rsp, workerId);

      if (rsp.blockNum != 0) {
        rsp.withTbName = false;
        rsp.rspOffset = lastVer;
506 507
        tqInfo("direct send by snapshot req offset %ld rsp offset %ld", rsp.reqOffset, rsp.rspOffset);
        fetchOffset = lastVer;
L
Liu Jicong 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520
        goto SEND_RSP;
      }
    }
  }
#endif

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

  walSetReaderCapacity(pHandle->pWalReader, 2048);

521
  while (1) {
L
Liu Jicong 已提交
522
    consumerEpoch = atomic_load_32(&pHandle->epoch);
L
Liu Jicong 已提交
523
    if (consumerEpoch > reqEpoch) {
L
Liu Jicong 已提交
524 525
      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 已提交
526 527
      break;
    }
L
Liu Jicong 已提交
528

L
Liu Jicong 已提交
529 530
    if (tqFetchLog(pTq, pHandle, &fetchOffset, &pHeadWithCkSum) < 0) {
      // TODO add push mgr
531 532 533 534 535
      break;
    }

    SWalReadHead* pHead = &pHeadWithCkSum->head;

H
Hongze Cheng 已提交
536 537
    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 已提交
538

L
fix  
Liu Jicong 已提交
539 540
    if (pHead->msgType == TDMT_VND_SUBMIT) {
      SSubmitReq* pCont = (SSubmitReq*)&pHead->body;
L
Liu Jicong 已提交
541

L
Liu Jicong 已提交
542 543 544
      if (tqDataExec(pTq, &pHandle->execHandle, pCont, &rsp, workerId) < 0) {
        /*ASSERT(0);*/
      }
L
Liu Jicong 已提交
545
    } else {
L
Liu Jicong 已提交
546
      ASSERT(pHandle->fetchMeta);
L
Liu Jicong 已提交
547
      ASSERT(IS_META_MSG(pHead->msgType));
548
      tqInfo("fetch meta msg, ver: %ld, type: %d", pHead->version, pHead->msgType);
L
Liu Jicong 已提交
549 550 551 552 553 554 555 556
      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 已提交
557
        goto OVER;
L
Liu Jicong 已提交
558 559 560
      }
      code = 0;
      goto OVER;
L
Liu Jicong 已提交
561
    }
562

L
Liu Jicong 已提交
563 564
    // TODO batch optimization:
    // TODO continue scan until meeting batch requirement
L
Liu Jicong 已提交
565 566
    if (rsp.blockNum > 0 /* threshold */) {
      break;
L
Liu Jicong 已提交
567 568
    } else {
      fetchOffset++;
L
Liu Jicong 已提交
569
    }
L
Liu Jicong 已提交
570
  }
571

L
Liu Jicong 已提交
572
  taosMemoryFree(pHeadWithCkSum);
L
Liu Jicong 已提交
573

L
Liu Jicong 已提交
574
SEND_RSP:
L
Liu Jicong 已提交
575 576
  ASSERT(taosArrayGetSize(rsp.blockData) == rsp.blockNum);
  ASSERT(taosArrayGetSize(rsp.blockDataLen) == rsp.blockNum);
L
Liu Jicong 已提交
577 578 579
  if (rsp.withSchema) {
    ASSERT(taosArrayGetSize(rsp.blockSchema) == rsp.blockNum);
  }
L
fix  
Liu Jicong 已提交
580

L
Liu Jicong 已提交
581
  rsp.rspOffset = fetchOffset;
582

L
Liu Jicong 已提交
583
  if (tqSendDataRsp(pTq, pMsg, pReq, &rsp) < 0) {
L
Liu Jicong 已提交
584
    code = -1;
585
  }
L
Liu Jicong 已提交
586
OVER:
L
Liu Jicong 已提交
587
  // TODO wrap in destroy func
L
Liu Jicong 已提交
588
  taosArrayDestroy(rsp.blockDataLen);
589
  taosArrayDestroyP(rsp.blockData, (FDelete)taosMemoryFree);
590 591 592 593 594 595 596 597

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

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

L
Liu Jicong 已提交
599
  return code;
600
}
L
Liu Jicong 已提交
601
#endif
L
Liu Jicong 已提交
602

L
Liu Jicong 已提交
603 604
int32_t tqProcessVgDeleteReq(STQ* pTq, char* msg, int32_t msgLen) {
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
L
Liu Jicong 已提交
605

L
Liu Jicong 已提交
606
  int32_t code = taosHashRemove(pTq->handles, pReq->subKey, strlen(pReq->subKey));
L
Liu Jicong 已提交
607
  ASSERT(code == 0);
608

L
Liu Jicong 已提交
609
  if (tqMetaDeleteHandle(pTq, pReq->subKey) < 0) {
610 611
    ASSERT(0);
  }
L
Liu Jicong 已提交
612
  return 0;
L
Liu Jicong 已提交
613 614
}

L
Liu Jicong 已提交
615
int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
616
  SMqRebVgReq req = {0};
L
Liu Jicong 已提交
617 618
  tDecodeSMqRebVgReq(msg, &req);
  // todo lock
L
Liu Jicong 已提交
619 620
  STqHandle* pHandle = taosHashGet(pTq->handles, req.subKey, strlen(req.subKey));
  if (pHandle == NULL) {
L
Liu Jicong 已提交
621 622
    ASSERT(req.oldConsumerId == -1);
    ASSERT(req.newConsumerId != -1);
L
Liu Jicong 已提交
623 624
    STqHandle tqHandle = {0};
    pHandle = &tqHandle;
L
Liu Jicong 已提交
625 626
    /*taosInitRWLatch(&pExec->lock);*/

L
Liu Jicong 已提交
627 628 629
    memcpy(pHandle->subKey, req.subKey, TSDB_SUBSCRIBE_KEY_LEN);
    pHandle->consumerId = req.newConsumerId;
    pHandle->epoch = -1;
L
Liu Jicong 已提交
630

L
Liu Jicong 已提交
631
    pHandle->execHandle.subType = req.subType;
L
Liu Jicong 已提交
632
    pHandle->fetchMeta = req.withMeta;
L
Liu Jicong 已提交
633

L
Liu Jicong 已提交
634 635 636 637 638
    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 已提交
639
      pHandle->execHandle.execCol.qmsg = req.qmsg;
L
Liu Jicong 已提交
640
      req.qmsg = NULL;
641
      for (int32_t i = 0; i < 5; i++) {
L
Liu Jicong 已提交
642
        SReadHandle handle = {
L
Liu Jicong 已提交
643
            .reader = pHandle->execHandle.pExecReader[i],
L
Liu Jicong 已提交
644
            .meta = pTq->pVnode->pMeta,
L
Liu Jicong 已提交
645
            .vnode = pTq->pVnode,
L
Liu Jicong 已提交
646
            .tqReader = true,
L
Liu Jicong 已提交
647
        };
L
Liu Jicong 已提交
648 649
        pHandle->execHandle.execCol.task[i] = qCreateStreamExecTaskInfo(pHandle->execHandle.execCol.qmsg, &handle);
        ASSERT(pHandle->execHandle.execCol.task[i]);
650
      }
L
Liu Jicong 已提交
651
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
L
Liu Jicong 已提交
652
      pHandle->execHandle.execDb.pFilterOutTbUid =
L
Liu Jicong 已提交
653 654
          taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
    } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
L
Liu Jicong 已提交
655
      pHandle->execHandle.execTb.suid = req.suid;
L
Liu Jicong 已提交
656 657
      SArray* tbUidList = taosArrayInit(0, sizeof(int64_t));
      tsdbGetCtbIdList(pTq->pVnode->pMeta, req.suid, tbUidList);
L
Liu Jicong 已提交
658
      tqDebug("vg %d, tq try get suid: %ld", TD_VID(pTq->pVnode), req.suid);
L
Liu Jicong 已提交
659 660
      for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
        int64_t tbUid = *(int64_t*)taosArrayGet(tbUidList, i);
L
Liu Jicong 已提交
661
        tqDebug("vg %d, idx %d, uid: %ld", TD_VID(pTq->pVnode), i, tbUid);
L
Liu Jicong 已提交
662
      }
L
Liu Jicong 已提交
663 664 665
      for (int32_t i = 0; i < 5; i++) {
        tqReadHandleSetTbUidList(pHandle->execHandle.pExecReader[i], tbUidList);
      }
L
Liu Jicong 已提交
666
      taosArrayDestroy(tbUidList);
L
Liu Jicong 已提交
667
    }
L
Liu Jicong 已提交
668
    taosHashPut(pTq->handles, req.subKey, strlen(req.subKey), pHandle, sizeof(STqHandle));
L
Liu Jicong 已提交
669 670 671
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
    }
L
Liu Jicong 已提交
672
  } else {
L
Liu Jicong 已提交
673
    /*ASSERT(pExec->consumerId == req.oldConsumerId);*/
L
Liu Jicong 已提交
674
    // TODO handle qmsg and exec modification
L
Liu Jicong 已提交
675 676 677
    atomic_store_32(&pHandle->epoch, -1);
    atomic_store_64(&pHandle->consumerId, req.newConsumerId);
    atomic_add_fetch_32(&pHandle->epoch, 1);
L
Liu Jicong 已提交
678 679 680
    if (tqMetaSaveHandle(pTq, req.subKey, pHandle) < 0) {
      // TODO
    }
L
Liu Jicong 已提交
681
  }
L
Liu Jicong 已提交
682

L
Liu Jicong 已提交
683
  return 0;
L
Liu Jicong 已提交
684
}
685

L
Liu Jicong 已提交
686
int32_t tqProcessTaskDeployReq(STQ* pTq, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
687 688 689 690 691 692 693 694 695 696
  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);
697
  ASSERT(pTask->isDataScan == 0 || pTask->isDataScan == 1);
L
Liu Jicong 已提交
698 699 700
  if (pTask->isDataScan == 0 && pTask->sinkType == TASK_SINK__NONE) {
    ASSERT(taosArrayGetSize(pTask->childEpInfo) != 0);
  }
L
Liu Jicong 已提交
701

L
Liu Jicong 已提交
702
  pTask->execStatus = TASK_EXEC_STATUS__IDLE;
L
Liu Jicong 已提交
703 704 705

  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
L
Liu Jicong 已提交
706 707 708
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

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

711 712
  pTask->pMsgCb = &pTq->pVnode->msgCb;

L
Liu Jicong 已提交
713
  // exec
L
Liu Jicong 已提交
714 715
  if (pTask->execType != TASK_EXEC__NONE) {
    // expand runners
L
Liu Jicong 已提交
716
    if (pTask->isDataScan) {
L
Liu Jicong 已提交
717
      SStreamReader* pStreamReader = tqInitSubmitMsgScanner(pTq->pVnode->pMeta);
L
Liu Jicong 已提交
718 719 720 721 722 723 724 725 726 727 728 729
      SReadHandle    handle = {
             .reader = pStreamReader,
             .meta = pTq->pVnode->pMeta,
             .vnode = pTq->pVnode,
      };
      /*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 已提交
730
  }
L
Liu Jicong 已提交
731 732

  // sink
L
Liu Jicong 已提交
733
  /*pTask->ahandle = pTq->pVnode;*/
L
Liu Jicong 已提交
734
  if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
735
    pTask->smaSink.vnode = pTq->pVnode;
L
Liu Jicong 已提交
736
    pTask->smaSink.smaSink = smaHandleRes;
L
Liu Jicong 已提交
737
  } else if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
738 739 740
    pTask->tbSink.vnode = pTq->pVnode;
    pTask->tbSink.tbSinkFunc = tqTableSink;

L
Liu Jicong 已提交
741 742
    ASSERT(pTask->tbSink.pSchemaWrapper);
    ASSERT(pTask->tbSink.pSchemaWrapper->pSchema);
L
Liu Jicong 已提交
743

L
Liu Jicong 已提交
744 745 746
    pTask->tbSink.pTSchema =
        tdGetSTSChemaFromSSChema(&pTask->tbSink.pSchemaWrapper->pSchema, pTask->tbSink.pSchemaWrapper->nCols);
    ASSERT(pTask->tbSink.pTSchema);
L
Liu Jicong 已提交
747
  }
748 749 750

  streamSetupTrigger(pTask);

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

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

L
Liu Jicong 已提交
755 756
  return 0;
FAIL:
L
Liu Jicong 已提交
757 758
  if (pTask->inputQueue) streamQueueClose(pTask->inputQueue);
  if (pTask->outputQueue) streamQueueClose(pTask->outputQueue);
L
Liu Jicong 已提交
759
  if (pTask) taosMemoryFree(pTask);
L
Liu Jicong 已提交
760 761
  return -1;
}
L
Liu Jicong 已提交
762

L
Liu Jicong 已提交
763
int32_t tqProcessStreamTrigger(STQ* pTq, SSubmitReq* pReq) {
L
Liu Jicong 已提交
764 765 766
  void*              pIter = NULL;
  bool               failed = false;
  SStreamDataSubmit* pSubmit = NULL;
L
Liu Jicong 已提交
767

L
Liu Jicong 已提交
768
  pSubmit = streamDataSubmitNew(pReq);
L
Liu Jicong 已提交
769 770 771 772 773 774 775
  if (pSubmit == NULL) {
    failed = true;
  }

  while (1) {
    pIter = taosHashIterate(pTq->pStreamTasks, pIter);
    if (pIter == NULL) break;
776
    SStreamTask* pTask = *(SStreamTask**)pIter;
L
Liu Jicong 已提交
777 778 779
    if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
      continue;
    }
780
    if (!pTask->isDataScan) continue;
L
Liu Jicong 已提交
781

L
Liu Jicong 已提交
782 783
    if (!failed) {
      if (streamTaskInput(pTask, (SStreamQueueItem*)pSubmit) < 0) {
L
Liu Jicong 已提交
784 785 786
        continue;
      }

L
Liu Jicong 已提交
787
      if (streamLaunchByWrite(pTask, TD_VID(pTq->pVnode)) < 0) {
L
Liu Jicong 已提交
788 789
        continue;
      }
L
Liu Jicong 已提交
790
    } else {
L
Liu Jicong 已提交
791
      streamTaskInputFail(pTask);
L
Liu Jicong 已提交
792 793 794
    }
  }

L
Liu Jicong 已提交
795
  if (pSubmit) {
L
Liu Jicong 已提交
796
    streamDataSubmitRefDec(pSubmit);
L
Liu Jicong 已提交
797
    taosFreeQitem(pSubmit);
L
Liu Jicong 已提交
798
  }
L
Liu Jicong 已提交
799 800

  return failed ? -1 : 0;
L
Liu Jicong 已提交
801 802
}

L
Liu Jicong 已提交
803
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
804
  //
L
Liu Jicong 已提交
805 806
  SStreamTaskRunReq* pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
807
  SStreamTask*       pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
808
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
809 810
    return 0;
  }
L
Liu Jicong 已提交
811
  streamProcessRunReq(pTask);
L
Liu Jicong 已提交
812 813 814 815
  return 0;
}

int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
816 817 818 819 820 821 822 823
  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;
824
  SStreamTask* pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
825
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
826 827 828 829 830
    return 0;
  }
  SRpcMsg rsp = {
      .info = pMsg->info,
      .code = 0,
831
  };
L
Liu Jicong 已提交
832
  streamProcessDispatchReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
833 834 835 836 837 838
  return 0;
}

int32_t tqProcessTaskRecoverReq(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverReq* pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
839
  SStreamTask*           pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
840
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
841 842
    return 0;
  }
L
Liu Jicong 已提交
843
  streamProcessRecoverReq(pTask, pReq, pMsg);
L
Liu Jicong 已提交
844 845 846 847
  return 0;
}

int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
848
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
L
Liu Jicong 已提交
849
  int32_t             taskId = pRsp->taskId;
850
  SStreamTask*        pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
851
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
852 853
    return 0;
  }
L
Liu Jicong 已提交
854
  streamProcessDispatchRsp(pTask, pRsp);
L
Liu Jicong 已提交
855 856 857 858 859 860
  return 0;
}

int32_t tqProcessTaskRecoverRsp(STQ* pTq, SRpcMsg* pMsg) {
  SStreamTaskRecoverRsp* pRsp = pMsg->pCont;
  int32_t                taskId = pRsp->taskId;
861
  SStreamTask*           pTask = *(SStreamTask**)taosHashGet(pTq->pStreamTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
862
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
L
Liu Jicong 已提交
863 864
    return 0;
  }
L
Liu Jicong 已提交
865
  streamProcessRecoverRsp(pTask, pRsp);
L
Liu Jicong 已提交
866 867
  return 0;
}
L
Liu Jicong 已提交
868 869 870

int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
L
Liu Jicong 已提交
871 872 873 874 875 876 877 878 879 880 881

  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 已提交
882
  int32_t              code = taosHashRemove(pTq->pStreamTasks, &pReq->taskId, sizeof(int32_t));
L
Liu Jicong 已提交
883
  // set status dropping
L
Liu Jicong 已提交
884
  ASSERT(code == 0);
L
Liu Jicong 已提交
885 886 887 888
  if (code == 0) {
    // sendrsp
  }
  return code;
L
Liu Jicong 已提交
889
#endif
L
Liu Jicong 已提交
890
}
L
Liu Jicong 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916

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