mndScheduler.c 24.0 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#include "mndScheduler.h"
#include "mndConsumer.h"
#include "mndDb.h"
#include "mndDnode.h"
#include "mndMnode.h"
#include "mndOffset.h"
#include "mndShow.h"
L
Liu Jicong 已提交
23
#include "mndSnode.h"
L
Liu Jicong 已提交
24
#include "mndStb.h"
L
Liu Jicong 已提交
25
#include "mndStream.h"
L
Liu Jicong 已提交
26 27 28 29 30
#include "mndSubscribe.h"
#include "mndTopic.h"
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
X
Xiaoyu Wang 已提交
31
#include "parser.h"
L
Liu Jicong 已提交
32 33
#include "tcompare.h"
#include "tname.h"
L
Liu Jicong 已提交
34 35
#include "tuuid.h"

L
Liu Jicong 已提交
36 37
extern bool tsStreamSchedV;

L
Liu Jicong 已提交
38 39 40 41 42 43 44
static int32_t mndAddTaskToTaskSet(SArray* pArray, SStreamTask* pTask) {
  int32_t childId = taosArrayGetSize(pArray);
  pTask->childId = childId;
  taosArrayPush(pArray, &pTask);
  return 0;
}

X
Xiaoyu Wang 已提交
45
int32_t mndConvertRSmaTask(const char* ast, int64_t uid, int8_t triggerType, int64_t watermark, char** pStr,
5
54liuyao 已提交
46
                           int32_t* pLen, double filesFactor) {
L
Liu Jicong 已提交
47 48 49 50 51 52 53 54 55
  SNode*      pAst = NULL;
  SQueryPlan* pPlan = NULL;
  terrno = TSDB_CODE_SUCCESS;

  if (nodesStringToNode(ast, &pAst) < 0) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

X
Xiaoyu Wang 已提交
56 57 58 59 60
  if (qSetSTableIdForRSma(pAst, uid) < 0) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

L
Liu Jicong 已提交
61 62 63 64 65 66 67
  SPlanContext cxt = {
      .pAstRoot = pAst,
      .topicQuery = false,
      .streamQuery = true,
      .rSmaQuery = true,
      .triggerType = triggerType,
      .watermark = watermark,
5
54liuyao 已提交
68
      .filesFactor = filesFactor,
L
Liu Jicong 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  };
  if (qCreateQueryPlan(&cxt, &pPlan, NULL) < 0) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

  int32_t levelNum = LIST_LENGTH(pPlan->pSubplans);
  if (levelNum != 1) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }
  SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 0);

  int32_t opNum = LIST_LENGTH(inner->pNodeList);
  if (opNum != 1) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

  SSubplan* plan = nodesListGetNode(inner->pNodeList, 0);
  if (qSubPlanToString(plan, pStr, pLen) < 0) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

END:
  if (pAst) nodesDestroyNode(pAst);
  if (pPlan) nodesDestroyNode(pPlan);
  return terrno;
}

L
Liu Jicong 已提交
100
int32_t mndPersistTaskDeployReq(STrans* pTrans, SStreamTask* pTask, const SEpSet* pEpSet, tmsg_t type, int32_t nodeId) {
H
Hongze Cheng 已提交
101 102
  SEncoder encoder;
  tEncoderInit(&encoder, NULL, 0);
L
Liu Jicong 已提交
103
  tEncodeSStreamTask(&encoder, pTask);
L
Liu Jicong 已提交
104 105
  int32_t size = encoder.pos;
  int32_t tlen = sizeof(SMsgHead) + size;
H
Hongze Cheng 已提交
106
  tEncoderClear(&encoder);
wafwerar's avatar
wafwerar 已提交
107
  void* buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
108 109 110 111
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
112
  ((SMsgHead*)buf)->vgId = htonl(nodeId);
L
Liu Jicong 已提交
113
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
H
Hongze Cheng 已提交
114
  tEncoderInit(&encoder, abuf, size);
L
Liu Jicong 已提交
115
  tEncodeSStreamTask(&encoder, pTask);
H
Hongze Cheng 已提交
116
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
117 118 119 120 121

  STransAction action = {0};
  memcpy(&action.epSet, pEpSet, sizeof(SEpSet));
  action.pCont = buf;
  action.contLen = tlen;
L
Liu Jicong 已提交
122
  action.msgType = type;
L
Liu Jicong 已提交
123
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
124
    taosMemoryFree(buf);
L
Liu Jicong 已提交
125 126 127 128 129
    return -1;
  }
  return 0;
}

L
Liu Jicong 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
int32_t mndAddSinkToTask(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream, SStreamTask* pTask) {
  pTask->dispatchType = TASK_DISPATCH__NONE;
  // sink
  if (pStream->createdBy == STREAM_CREATED_BY__SMA) {
    pTask->sinkType = TASK_SINK__SMA;
    pTask->smaSink.smaId = pStream->smaId;
  } else {
    pTask->sinkType = TASK_SINK__TABLE;
    pTask->tbSink.stbUid = pStream->targetStbUid;
    memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
    pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
  }
  return 0;
}

int32_t mndAddDispatcherToInnerTask(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream, SStreamTask* pTask) {
  pTask->sinkType = TASK_SINK__NONE;
  if (pStream->fixedSinkVgId == 0) {
    pTask->dispatchType = TASK_DISPATCH__SHUFFLE;
    pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
    SDbObj* pDb = mndAcquireDb(pMnode, pStream->targetDb);
    ASSERT(pDb);

    if (mndExtractDbInfo(pMnode, pDb, &pTask->shuffleDispatcher.dbInfo, NULL) < 0) {
      sdbRelease(pMnode->pSdb, pDb);

      SArray* pVgs = pTask->shuffleDispatcher.dbInfo.pVgroupInfos;
      int32_t sz = taosArrayGetSize(pVgs);
      SArray* sinkLv = taosArrayGetP(pStream->tasks, 0);
      int32_t sinkLvSize = taosArrayGetSize(sinkLv);
      for (int32_t i = 0; i < sz; i++) {
        SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
        for (int32_t j = 0; j < sinkLvSize; j++) {
          SStreamTask* pLastLevelTask = taosArrayGetP(sinkLv, j);
          if (pLastLevelTask->nodeId == pVgInfo->vgId) {
            pVgInfo->taskId = pLastLevelTask->taskId;
            break;
          }
        }
      }
    } else {
      pTask->dispatchType = TASK_DISPATCH__FIXED;
      pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
      SArray* pArray = taosArrayGetP(pStream->tasks, 0);
      // one sink only
      ASSERT(taosArrayGetSize(pArray) == 1);
      SStreamTask* lastLevelTask = taosArrayGetP(pArray, 0);
      pTask->fixedEpDispatcher.taskId = lastLevelTask->taskId;
      pTask->fixedEpDispatcher.nodeId = lastLevelTask->nodeId;
      pTask->fixedEpDispatcher.epSet = lastLevelTask->epSet;
    }
  }
  return 0;
}

L
Liu Jicong 已提交
185 186
int32_t mndAssignTaskToVg(SMnode* pMnode, STrans* pTrans, SStreamTask* pTask, SSubplan* plan, const SVgObj* pVgroup) {
  int32_t msgLen;
L
Liu Jicong 已提交
187 188 189
  pTask->nodeId = pVgroup->vgId;
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);

L
Liu Jicong 已提交
190
  plan->execNode.nodeId = pVgroup->vgId;
L
Liu Jicong 已提交
191
  plan->execNode.epSet = pTask->epSet;
L
Liu Jicong 已提交
192

L
Liu Jicong 已提交
193
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
L
Liu Jicong 已提交
194 195 196
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
197
  ASSERT(pTask->dispatchType != TASK_DISPATCH__NONE || pTask->sinkType != TASK_SINK__NONE);
198
  mndPersistTaskDeployReq(pTrans, pTask, &plan->execNode.epSet, TDMT_STREAM_TASK_DEPLOY, pVgroup->vgId);
L
Liu Jicong 已提交
199 200 201
  return 0;
}

L
Liu Jicong 已提交
202 203 204 205 206 207
SSnodeObj* mndSchedFetchSnode(SMnode* pMnode) {
  SSnodeObj* pObj = NULL;
  pObj = sdbFetch(pMnode->pSdb, SDB_SNODE, NULL, (void**)&pObj);
  return pObj;
}

L
Liu Jicong 已提交
208 209
int32_t mndAssignTaskToSnode(SMnode* pMnode, STrans* pTrans, SStreamTask* pTask, SSubplan* plan,
                             const SSnodeObj* pSnode) {
L
Liu Jicong 已提交
210
  int32_t msgLen;
L
Liu Jicong 已提交
211 212 213 214 215 216

  pTask->nodeId = 0;
  pTask->epSet = mndAcquireEpFromSnode(pMnode, pSnode);

  plan->execNode.nodeId = 0;
  plan->execNode.epSet = pTask->epSet;
L
Liu Jicong 已提交
217

L
Liu Jicong 已提交
218
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
L
Liu Jicong 已提交
219 220 221
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
222
  mndPersistTaskDeployReq(pTrans, pTask, &plan->execNode.epSet, TDMT_STREAM_TASK_DEPLOY, 0);
L
Liu Jicong 已提交
223 224 225
  return 0;
}

L
Liu Jicong 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
SVgObj* mndSchedFetchOneVg(SMnode* pMnode, int64_t dbUid) {
  void*   pIter = NULL;
  SVgObj* pVgroup = NULL;
  while (1) {
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != dbUid) {
      sdbRelease(pMnode->pSdb, pVgroup);
      continue;
    }
    return pVgroup;
  }
  return pVgroup;
}

L
Liu Jicong 已提交
241
int32_t mndAddShuffleSinkTasksToStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
L
Liu Jicong 已提交
242 243 244 245 246 247 248 249 250 251
  SSdb*   pSdb = pMnode->pSdb;
  void*   pIter = NULL;
  SArray* tasks = taosArrayGetP(pStream->tasks, 0);

  ASSERT(taosArrayGetSize(pStream->tasks) == 1);

  while (1) {
    SVgObj* pVgroup;
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;
252
    if (strcmp(pVgroup->dbName, pStream->targetDb) != 0) {
L
Liu Jicong 已提交
253 254 255
      sdbRelease(pSdb, pVgroup);
      continue;
    }
L
Liu Jicong 已提交
256
    SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
257 258 259 260
    if (pTask == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
261
    mndAddTaskToTaskSet(tasks, pTask);
L
Liu Jicong 已提交
262 263 264 265 266 267

    pTask->nodeId = pVgroup->vgId;
    pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);

    // source
    pTask->sourceType = TASK_SOURCE__MERGE;
L
Liu Jicong 已提交
268
    pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
269 270 271 272 273

    // exec
    pTask->execType = TASK_EXEC__NONE;

    // sink
L
Liu Jicong 已提交
274
    if (pStream->createdBy == STREAM_CREATED_BY__SMA) {
L
Liu Jicong 已提交
275
      pTask->sinkType = TASK_SINK__SMA;
L
Liu Jicong 已提交
276
      pTask->smaSink.smaId = pStream->smaId;
L
Liu Jicong 已提交
277 278
    } else {
      pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
279
      pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
280
      memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
281 282
      pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
      ASSERT(pTask->tbSink.pSchemaWrapper);
L
Liu Jicong 已提交
283 284 285 286 287
    }

    // dispatch
    pTask->dispatchType = TASK_DISPATCH__NONE;

288
    mndPersistTaskDeployReq(pTrans, pTask, &pTask->epSet, TDMT_STREAM_TASK_DEPLOY, pVgroup->vgId);
L
Liu Jicong 已提交
289 290 291 292
  }
  return 0;
}

L
Liu Jicong 已提交
293
int32_t mndAddFixedSinkTaskToStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
L
Liu Jicong 已提交
294 295
  ASSERT(pStream->fixedSinkVgId != 0);
  SArray*      tasks = taosArrayGetP(pStream->tasks, 0);
L
Liu Jicong 已提交
296
  SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
297 298 299 300
  if (pTask == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
301
  mndAddTaskToTaskSet(tasks, pTask);
L
Liu Jicong 已提交
302 303

  pTask->nodeId = pStream->fixedSinkVgId;
L
Liu Jicong 已提交
304
#if 0
L
Liu Jicong 已提交
305 306 307 308 309
  SVgObj* pVgroup = mndAcquireVgroup(pMnode, pStream->fixedSinkVgId);
  if (pVgroup == NULL) {
    return -1;
  }
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);
L
Liu Jicong 已提交
310 311
#endif
  pTask->epSet = mndGetVgroupEpset(pMnode, &pStream->fixedSinkVg);
L
Liu Jicong 已提交
312 313
  // source
  pTask->sourceType = TASK_SOURCE__MERGE;
L
Liu Jicong 已提交
314
  pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
315 316 317 318 319 320 321 322 323 324

  // exec
  pTask->execType = TASK_EXEC__NONE;

  // sink
  if (pStream->createdBy == STREAM_CREATED_BY__SMA) {
    pTask->sinkType = TASK_SINK__SMA;
    pTask->smaSink.smaId = pStream->smaId;
  } else {
    pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
325
    pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
326
    memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
327
    pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
328
  }
L
Liu Jicong 已提交
329

L
Liu Jicong 已提交
330 331 332
  // dispatch
  pTask->dispatchType = TASK_DISPATCH__NONE;

333
  mndPersistTaskDeployReq(pTrans, pTask, &pTask->epSet, TDMT_STREAM_TASK_DEPLOY, pStream->fixedSinkVg.vgId);
L
Liu Jicong 已提交
334 335 336 337

  return 0;
}

L
Liu Jicong 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 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 386 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 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
int32_t mndScheduleStream1(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
  SSdb*       pSdb = pMnode->pSdb;
  SQueryPlan* pPlan = qStringToQueryPlan(pStream->physicalPlan);
  if (pPlan == NULL) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
  ASSERT(pStream->vgNum == 0);

  int32_t totLevel = LIST_LENGTH(pPlan->pSubplans);
  ASSERT(totLevel <= 2);
  pStream->tasks = taosArrayInit(totLevel, sizeof(void*));

  bool hasExtraSink = false;
  bool externalTargetDB = strcmp(pStream->sourceDb, pStream->targetDb) != 0;
  if (totLevel == 2 || externalTargetDB) {
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);
    // add extra sink
    hasExtraSink = true;
    if (pStream->fixedSinkVgId == 0) {
      mndAddShuffleSinkTasksToStream(pMnode, pTrans, pStream);
    } else {
      mndAddFixedSinkTaskToStream(pMnode, pTrans, pStream);
    }
  }
  if (totLevel > 1) {
    SStreamTask* pFinalTask;
    // inner plan
    {
      SArray* taskInnerLevel = taosArrayInit(0, sizeof(void*));
      taosArrayPush(pStream->tasks, &taskInnerLevel);

      SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 0);
      SSubplan*      plan = nodesListGetNode(inner->pNodeList, 0);
      ASSERT(plan->subplanType == SUBPLAN_TYPE_MERGE);

      pFinalTask = tNewSStreamTask(pStream->uid);
      mndAddTaskToTaskSet(taskInnerLevel, pFinalTask);
      // input
      pFinalTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;

      // dispatch
      mndAddDispatcherToInnerTask(pMnode, pTrans, pStream, pFinalTask);

      // exec
      pFinalTask->execType = TASK_EXEC__PIPE;
      SVgObj* pVgroup = mndSchedFetchOneVg(pMnode, pStream->dbUid);
      if (mndAssignTaskToVg(pMnode, pTrans, pFinalTask, plan, pVgroup) < 0) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
    }

    // source plan
    SArray* taskSourceLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskSourceLevel);

    SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 1);
    SSubplan*      plan = nodesListGetNode(inner->pNodeList, 0);
    ASSERT(plan->subplanType == SUBPLAN_TYPE_SCAN);

    void* pIter = NULL;
    while (1) {
      SVgObj* pVgroup;
      pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
      if (pIter == NULL) break;
      if (pVgroup->dbUid != pStream->dbUid) {
        sdbRelease(pSdb, pVgroup);
        continue;
      }
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
      mndAddTaskToTaskSet(taskSourceLevel, pTask);

      // input
      pTask->inputType = TASK_INPUT_TYPE__SUMBIT_BLOCK;

      // add fixed vg dispatch
      pTask->sinkType = TASK_SINK__NONE;
      pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
      pTask->dispatchType = TASK_DISPATCH__FIXED;

      pTask->fixedEpDispatcher.taskId = pFinalTask->taskId;
      pTask->fixedEpDispatcher.nodeId = pFinalTask->nodeId;
      pTask->fixedEpDispatcher.epSet = pFinalTask->epSet;

      // exec
      pTask->execType = TASK_EXEC__PIPE;
      if (mndAssignTaskToVg(pMnode, pTrans, pTask, plan, pVgroup) < 0) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
    }
  }

  if (totLevel == 1) {
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);

    SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 0);
    ASSERT(LIST_LENGTH(inner->pNodeList) == 1);
    SSubplan* plan = nodesListGetNode(inner->pNodeList, 0);
    ASSERT(plan->subplanType == SUBPLAN_TYPE_SCAN);

    void* pIter = NULL;
    while (1) {
      SVgObj* pVgroup;
      pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
      if (pIter == NULL) break;
      if (pVgroup->dbUid != pStream->dbUid) {
        sdbRelease(pSdb, pVgroup);
        continue;
      }
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
      mndAddTaskToTaskSet(taskOneLevel, pTask);

      // input
      pTask->inputType = TASK_INPUT_TYPE__SUMBIT_BLOCK;

      // sink or dispatch
      if (hasExtraSink) {
        mndAddDispatcherToInnerTask(pMnode, pTrans, pStream, pTask);
      } else {
        mndAddSinkToTask(pMnode, pTrans, pStream, pTask);
      }

      // exec
      pTask->execType = TASK_EXEC__PIPE;
      if (mndAssignTaskToVg(pMnode, pTrans, pTask, plan, pVgroup) < 0) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
    }
  }
  return 0;
}

L
Liu Jicong 已提交
478
int32_t mndScheduleStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
L
Liu Jicong 已提交
479 480 481 482 483 484 485 486
  SSdb*       pSdb = pMnode->pSdb;
  SQueryPlan* pPlan = qStringToQueryPlan(pStream->physicalPlan);
  if (pPlan == NULL) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
  ASSERT(pStream->vgNum == 0);

L
Liu Jicong 已提交
487
  int32_t totLevel = LIST_LENGTH(pPlan->pSubplans);
L
Liu Jicong 已提交
488 489
  ASSERT(totLevel <= 2);
  pStream->tasks = taosArrayInit(totLevel, sizeof(void*));
L
Liu Jicong 已提交
490

L
Liu Jicong 已提交
491
  bool hasExtraSink = false;
492 493
  bool externalTargetDB = strcmp(pStream->sourceDb, pStream->targetDb) != 0;
  if (totLevel == 2 || externalTargetDB) {
L
Liu Jicong 已提交
494 495 496 497
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);
    // add extra sink
    hasExtraSink = true;
L
Liu Jicong 已提交
498
    if (pStream->fixedSinkVgId == 0) {
L
Liu Jicong 已提交
499
      mndAddShuffleSinkTasksToStream(pMnode, pTrans, pStream);
L
Liu Jicong 已提交
500
    } else {
L
Liu Jicong 已提交
501
      mndAddFixedSinkTaskToStream(pMnode, pTrans, pStream);
L
Liu Jicong 已提交
502
    }
L
Liu Jicong 已提交
503 504
  }

L
Liu Jicong 已提交
505
  for (int32_t level = 0; level < totLevel; level++) {
L
Liu Jicong 已提交
506 507
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);
L
Liu Jicong 已提交
508 509
    SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, level);
    ASSERT(LIST_LENGTH(inner->pNodeList) == 1);
L
Liu Jicong 已提交
510

L
Liu Jicong 已提交
511
    SSubplan* plan = nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
512 513 514 515 516

    // if (level == totLevel - 1 /* or no snode */) {
    if (level == totLevel - 1) {
      // last level, source, must assign to vnode
      // must be scan type
L
Liu Jicong 已提交
517
      ASSERT(plan->subplanType == SUBPLAN_TYPE_SCAN);
L
Liu Jicong 已提交
518 519

      // replicate task to each vnode
L
Liu Jicong 已提交
520 521
      void* pIter = NULL;
      while (1) {
L
Liu Jicong 已提交
522
        SVgObj* pVgroup;
L
Liu Jicong 已提交
523 524 525 526 527 528
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
        if (pIter == NULL) break;
        if (pVgroup->dbUid != pStream->dbUid) {
          sdbRelease(pSdb, pVgroup);
          continue;
        }
L
Liu Jicong 已提交
529 530
        SStreamTask* pTask = tNewSStreamTask(pStream->uid);
        mndAddTaskToTaskSet(taskOneLevel, pTask);
L
Liu Jicong 已提交
531 532
        // source part
        pTask->sourceType = TASK_SOURCE__SCAN;
L
Liu Jicong 已提交
533
        pTask->inputType = TASK_INPUT_TYPE__SUMBIT_BLOCK;
L
Liu Jicong 已提交
534

L
Liu Jicong 已提交
535 536 537
        // sink part
        if (level == 0) {
          // only for inplace
L
Liu Jicong 已提交
538
          pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
539
          if (!hasExtraSink) {
L
Liu Jicong 已提交
540 541
#if 1
            if (pStream->createdBy == STREAM_CREATED_BY__SMA) {
L
Liu Jicong 已提交
542
              pTask->sinkType = TASK_SINK__SMA;
L
Liu Jicong 已提交
543
              pTask->smaSink.smaId = pStream->smaId;
L
Liu Jicong 已提交
544 545
            } else {
              pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
546
              pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
547
              memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
548
              pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
549
            }
L
Liu Jicong 已提交
550
#endif
L
Liu Jicong 已提交
551
          }
L
Liu Jicong 已提交
552 553 554
        } else {
          pTask->sinkType = TASK_SINK__NONE;
        }
L
Liu Jicong 已提交
555

L
Liu Jicong 已提交
556
        // dispatch part
L
Liu Jicong 已提交
557
        if (level == 0 && !hasExtraSink) {
L
Liu Jicong 已提交
558 559 560 561
          pTask->dispatchType = TASK_DISPATCH__NONE;
        } else {
          // add fixed ep dispatcher
          int32_t lastLevel = level - 1;
L
Liu Jicong 已提交
562
          if (hasExtraSink) lastLevel++;
L
Liu Jicong 已提交
563
          ASSERT(lastLevel == 0);
L
Liu Jicong 已提交
564 565 566
          SArray* pArray = taosArrayGetP(pStream->tasks, lastLevel);
          // one merge only
          ASSERT(taosArrayGetSize(pArray) == 1);
L
Liu Jicong 已提交
567
          SStreamTask* lastLevelTask = taosArrayGetP(pArray, 0);
568
          pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
L
Liu Jicong 已提交
569 570
          pTask->dispatchType = TASK_DISPATCH__FIXED;

L
Liu Jicong 已提交
571
          pTask->fixedEpDispatcher.taskId = lastLevelTask->taskId;
L
Liu Jicong 已提交
572 573 574 575 576 577
          pTask->fixedEpDispatcher.nodeId = lastLevelTask->nodeId;
          pTask->fixedEpDispatcher.epSet = lastLevelTask->epSet;
        }

        // exec part
        pTask->execType = TASK_EXEC__PIPE;
L
Liu Jicong 已提交
578
        if (mndAssignTaskToVg(pMnode, pTrans, pTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
579 580 581 582
          sdbRelease(pSdb, pVgroup);
          qDestroyQueryPlan(pPlan);
          return -1;
        }
L
Liu Jicong 已提交
583
        sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
584
      }
L
Liu Jicong 已提交
585
    } else {
L
Liu Jicong 已提交
586 587 588 589 590 591
      // merge plan

      // TODO if has snode, assign to snode

      // else, assign to vnode
      ASSERT(plan->subplanType == SUBPLAN_TYPE_MERGE);
L
Liu Jicong 已提交
592 593
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
      mndAddTaskToTaskSet(taskOneLevel, pTask);
L
Liu Jicong 已提交
594 595 596

      // source part, currently only support multi source
      pTask->sourceType = TASK_SOURCE__PIPE;
L
Liu Jicong 已提交
597
      pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
598 599

      // sink part
L
Liu Jicong 已提交
600
      pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
601 602

      // dispatch part
L
Liu Jicong 已提交
603 604 605 606 607 608
      ASSERT(hasExtraSink);
      /*pTask->dispatchType = TASK_DISPATCH__NONE;*/
#if 1

      if (hasExtraSink) {
        // add dispatcher
L
Liu Jicong 已提交
609 610 611
        if (pStream->fixedSinkVgId == 0) {
          pTask->dispatchType = TASK_DISPATCH__SHUFFLE;

612
          pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
L
Liu Jicong 已提交
613
          SDbObj* pDb = mndAcquireDb(pMnode, pStream->targetDb);
L
Liu Jicong 已提交
614 615 616 617 618 619
          ASSERT(pDb);
          if (mndExtractDbInfo(pMnode, pDb, &pTask->shuffleDispatcher.dbInfo, NULL) < 0) {
            sdbRelease(pSdb, pDb);
            qDestroyQueryPlan(pPlan);
            return -1;
          }
L
Liu Jicong 已提交
620
          sdbRelease(pSdb, pDb);
L
Liu Jicong 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

          // put taskId to useDbRsp
          // TODO: optimize
          SArray* pVgs = pTask->shuffleDispatcher.dbInfo.pVgroupInfos;
          int32_t sz = taosArrayGetSize(pVgs);
          SArray* sinkLv = taosArrayGetP(pStream->tasks, 0);
          int32_t sinkLvSize = taosArrayGetSize(sinkLv);
          for (int32_t i = 0; i < sz; i++) {
            SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
            for (int32_t j = 0; j < sinkLvSize; j++) {
              SStreamTask* pLastLevelTask = taosArrayGetP(sinkLv, j);
              if (pLastLevelTask->nodeId == pVgInfo->vgId) {
                pVgInfo->taskId = pLastLevelTask->taskId;
                break;
              }
L
Liu Jicong 已提交
636 637
            }
          }
L
Liu Jicong 已提交
638 639
        } else {
          pTask->dispatchType = TASK_DISPATCH__FIXED;
640
          /*pTask->dispatchMsgType = TDMT_VND_TASK_WRITE_EXEC;*/
641
          pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
L
Liu Jicong 已提交
642 643 644 645 646 647 648
          SArray* pArray = taosArrayGetP(pStream->tasks, 0);
          // one sink only
          ASSERT(taosArrayGetSize(pArray) == 1);
          SStreamTask* lastLevelTask = taosArrayGetP(pArray, 0);
          pTask->fixedEpDispatcher.taskId = lastLevelTask->taskId;
          pTask->fixedEpDispatcher.nodeId = lastLevelTask->nodeId;
          pTask->fixedEpDispatcher.epSet = lastLevelTask->epSet;
L
Liu Jicong 已提交
649
        }
L
Liu Jicong 已提交
650 651
      }
#endif
L
Liu Jicong 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665

      // exec part
      pTask->execType = TASK_EXEC__MERGE;
      SVgObj* pVgroup = mndSchedFetchOneVg(pMnode, pStream->dbUid);
      ASSERT(pVgroup);
      if (mndAssignTaskToVg(pMnode, pTrans, pTask, plan, pVgroup) < 0) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
      sdbRelease(pSdb, pVgroup);
    }
  }

L
Liu Jicong 已提交
666
#if 0
L
Liu Jicong 已提交
667 668 669 670 671 672 673 674 675
  if (totLevel == 2) {
    void* pIter = NULL;
    while (1) {
      SVgObj* pVgroup;
      pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
      if (pIter == NULL) break;
      if (pVgroup->dbUid != pStream->dbUid) {
        sdbRelease(pSdb, pVgroup);
        continue;
L
Liu Jicong 已提交
676
      }
L
Liu Jicong 已提交
677
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
678

L
Liu Jicong 已提交
679 680
      // source part
      pTask->sourceType = TASK_SOURCE__MERGE;
L
Liu Jicong 已提交
681
      pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
682 683

      // sink part
L
Liu Jicong 已提交
684
      pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
685 686 687 688 689 690

      // dispatch part
      pTask->dispatchType = TASK_DISPATCH__NONE;

      // exec part
      pTask->execType = TASK_EXEC__NONE;
L
Liu Jicong 已提交
691 692
    }
  }
L
Liu Jicong 已提交
693
#endif
L
Liu Jicong 已提交
694 695

  // free memory
L
Liu Jicong 已提交
696
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
697

L
Liu Jicong 已提交
698 699
  return 0;
}
L
Liu Jicong 已提交
700 701

int32_t mndSchedInitSubEp(SMnode* pMnode, const SMqTopicObj* pTopic, SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
702 703
  SSdb*       pSdb = pMnode->pSdb;
  SVgObj*     pVgroup = NULL;
L
Liu Jicong 已提交
704 705
  SQueryPlan* pPlan = NULL;
  SSubplan*   plan = NULL;
L
Liu Jicong 已提交
706

L
Liu Jicong 已提交
707
  if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
708 709 710 711 712
    pPlan = qStringToQueryPlan(pTopic->physicalPlan);
    if (pPlan == NULL) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
L
Liu Jicong 已提交
713

L
Liu Jicong 已提交
714 715 716
    int32_t levelNum = LIST_LENGTH(pPlan->pSubplans);
    if (levelNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
717
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
718 719
      return -1;
    }
L
Liu Jicong 已提交
720

L
Liu Jicong 已提交
721
    SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 0);
L
Liu Jicong 已提交
722

L
Liu Jicong 已提交
723 724 725
    int32_t opNum = LIST_LENGTH(inner->pNodeList);
    if (opNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
726
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
727 728 729
      return -1;
    }
    plan = nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
730
  }
L
Liu Jicong 已提交
731

732 733
  ASSERT(pSub->unassignedVgs);
  ASSERT(taosHashGetSize(pSub->consumerHash) == 0);
734

L
Liu Jicong 已提交
735 736 737 738 739 740 741 742 743 744 745
  void* pIter = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pTopic->dbUid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }

    pSub->vgNum++;

746
    SMqVgEp* pVgEp = taosMemoryMalloc(sizeof(SMqVgEp));
L
Liu Jicong 已提交
747 748
    pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
    pVgEp->vgId = pVgroup->vgId;
749
    taosArrayPush(pSub->unassignedVgs, &pVgEp);
750

L
Liu Jicong 已提交
751
    mDebug("init subscription %s, assign vg: %d", pSub->key, pVgEp->vgId);
L
Liu Jicong 已提交
752

L
Liu Jicong 已提交
753
    if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766
      int32_t msgLen;

      plan->execNode.epSet = pVgEp->epSet;
      plan->execNode.nodeId = pVgEp->vgId;

      if (qSubPlanToString(plan, &pVgEp->qmsg, &msgLen) < 0) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        terrno = TSDB_CODE_QRY_INVALID_INPUT;
        return -1;
      }
    } else {
      pVgEp->qmsg = strdup("");
L
Liu Jicong 已提交
767
    }
L
Liu Jicong 已提交
768 769
  }

770
  ASSERT(pSub->unassignedVgs->size > 0);
L
Liu Jicong 已提交
771

772
  ASSERT(taosHashGetSize(pSub->consumerHash) == 0);
L
Liu Jicong 已提交
773

X
Xiaoyu Wang 已提交
774
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
775

L
Liu Jicong 已提交
776 777
  return 0;
}