mndScheduler.c 17.5 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;

X
Xiaoyu Wang 已提交
38
int32_t mndConvertRSmaTask(const char* ast, int64_t uid, int8_t triggerType, int64_t watermark, char** pStr,
5
54liuyao 已提交
39
                           int32_t* pLen, double filesFactor) {
L
Liu Jicong 已提交
40 41 42 43 44 45 46 47 48
  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 已提交
49 50 51 52 53
  if (qSetSTableIdForRSma(pAst, uid) < 0) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

L
Liu Jicong 已提交
54 55 56 57 58 59 60
  SPlanContext cxt = {
      .pAstRoot = pAst,
      .topicQuery = false,
      .streamQuery = true,
      .rSmaQuery = true,
      .triggerType = triggerType,
      .watermark = watermark,
5
54liuyao 已提交
61
      .filesFactor = filesFactor,
L
Liu Jicong 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  };
  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 已提交
93
int32_t mndPersistTaskDeployReq(STrans* pTrans, SStreamTask* pTask, const SEpSet* pEpSet, tmsg_t type, int32_t nodeId) {
H
Hongze Cheng 已提交
94 95
  SEncoder encoder;
  tEncoderInit(&encoder, NULL, 0);
L
Liu Jicong 已提交
96
  tEncodeSStreamTask(&encoder, pTask);
L
Liu Jicong 已提交
97 98
  int32_t size = encoder.pos;
  int32_t tlen = sizeof(SMsgHead) + size;
H
Hongze Cheng 已提交
99
  tEncoderClear(&encoder);
wafwerar's avatar
wafwerar 已提交
100
  void* buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
101 102 103 104
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
105
  ((SMsgHead*)buf)->vgId = htonl(nodeId);
L
Liu Jicong 已提交
106
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
H
Hongze Cheng 已提交
107
  tEncoderInit(&encoder, abuf, size);
L
Liu Jicong 已提交
108
  tEncodeSStreamTask(&encoder, pTask);
H
Hongze Cheng 已提交
109
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
110 111 112 113 114

  STransAction action = {0};
  memcpy(&action.epSet, pEpSet, sizeof(SEpSet));
  action.pCont = buf;
  action.contLen = tlen;
L
Liu Jicong 已提交
115
  action.msgType = type;
L
Liu Jicong 已提交
116
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
117
    taosMemoryFree(buf);
L
Liu Jicong 已提交
118 119 120 121 122 123 124
    return -1;
  }
  return 0;
}

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

L
Liu Jicong 已提交
128
  plan->execNode.nodeId = pVgroup->vgId;
L
Liu Jicong 已提交
129
  plan->execNode.epSet = pTask->epSet;
L
Liu Jicong 已提交
130

L
Liu Jicong 已提交
131
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
L
Liu Jicong 已提交
132 133 134
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
135
  mndPersistTaskDeployReq(pTrans, pTask, &plan->execNode.epSet, TDMT_VND_TASK_DEPLOY, pVgroup->vgId);
L
Liu Jicong 已提交
136 137 138
  return 0;
}

L
Liu Jicong 已提交
139 140 141 142 143 144
SSnodeObj* mndSchedFetchSnode(SMnode* pMnode) {
  SSnodeObj* pObj = NULL;
  pObj = sdbFetch(pMnode->pSdb, SDB_SNODE, NULL, (void**)&pObj);
  return pObj;
}

L
Liu Jicong 已提交
145 146
int32_t mndAssignTaskToSnode(SMnode* pMnode, STrans* pTrans, SStreamTask* pTask, SSubplan* plan,
                             const SSnodeObj* pSnode) {
L
Liu Jicong 已提交
147
  int32_t msgLen;
L
Liu Jicong 已提交
148 149 150 151 152 153

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

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

L
Liu Jicong 已提交
155
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
L
Liu Jicong 已提交
156 157 158
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
159
  mndPersistTaskDeployReq(pTrans, pTask, &plan->execNode.epSet, TDMT_SND_TASK_DEPLOY, 0);
L
Liu Jicong 已提交
160 161 162
  return 0;
}

L
Liu Jicong 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
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 已提交
178
int32_t mndAddShuffledSinkToStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
L
Liu Jicong 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192
  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;
    if (pVgroup->dbUid != pStream->dbUid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
L
Liu Jicong 已提交
193
    SStreamTask* pTask = tNewSStreamTask(pStream->uid, 0);
L
Liu Jicong 已提交
194 195 196 197 198 199 200 201 202 203 204
    if (pTask == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    taosArrayPush(tasks, &pTask);

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

    // source
    pTask->sourceType = TASK_SOURCE__MERGE;
L
Liu Jicong 已提交
205
    pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
206 207 208 209 210

    // exec
    pTask->execType = TASK_EXEC__NONE;

    // sink
L
Liu Jicong 已提交
211
    if (pStream->createdBy == STREAM_CREATED_BY__SMA) {
L
Liu Jicong 已提交
212
      pTask->sinkType = TASK_SINK__SMA;
L
Liu Jicong 已提交
213
      pTask->smaSink.smaId = pStream->smaId;
L
Liu Jicong 已提交
214 215
    } else {
      pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
216
      pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
217
      memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
218 219
      pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
      ASSERT(pTask->tbSink.pSchemaWrapper);
L
Liu Jicong 已提交
220 221 222 223 224 225 226 227 228 229
    }

    // dispatch
    pTask->dispatchType = TASK_DISPATCH__NONE;

    mndPersistTaskDeployReq(pTrans, pTask, &pTask->epSet, TDMT_VND_TASK_DEPLOY, pVgroup->vgId);
  }
  return 0;
}

L
Liu Jicong 已提交
230 231 232
int32_t mndAddFixedSinkToStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
  ASSERT(pStream->fixedSinkVgId != 0);
  SArray*      tasks = taosArrayGetP(pStream->tasks, 0);
L
Liu Jicong 已提交
233
  SStreamTask* pTask = tNewSStreamTask(pStream->uid, 0);
L
Liu Jicong 已提交
234 235 236 237 238 239 240
  if (pTask == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  taosArrayPush(tasks, &pTask);

  pTask->nodeId = pStream->fixedSinkVgId;
L
Liu Jicong 已提交
241
#if 0
L
Liu Jicong 已提交
242 243 244 245 246
  SVgObj* pVgroup = mndAcquireVgroup(pMnode, pStream->fixedSinkVgId);
  if (pVgroup == NULL) {
    return -1;
  }
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);
L
Liu Jicong 已提交
247 248
#endif
  pTask->epSet = mndGetVgroupEpset(pMnode, &pStream->fixedSinkVg);
L
Liu Jicong 已提交
249 250
  // source
  pTask->sourceType = TASK_SOURCE__MERGE;
L
Liu Jicong 已提交
251
  pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
252 253 254 255 256 257 258 259 260 261

  // 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 已提交
262
    pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
263
    memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
264
    pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
265
  }
L
Liu Jicong 已提交
266

L
Liu Jicong 已提交
267 268 269
  // dispatch
  pTask->dispatchType = TASK_DISPATCH__NONE;

L
Liu Jicong 已提交
270 271
  /*mndPersistTaskDeployReq(pTrans, pTask, &pTask->epSet, TDMT_VND_TASK_DEPLOY, pVgroup->vgId);*/
  mndPersistTaskDeployReq(pTrans, pTask, &pTask->epSet, TDMT_VND_TASK_DEPLOY, pStream->fixedSinkVg.vgId);
L
Liu Jicong 已提交
272 273 274 275 276

  return 0;
}

int32_t mndScheduleStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) {
L
Liu Jicong 已提交
277 278 279 280 281 282 283 284
  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 已提交
285
  int32_t totLevel = LIST_LENGTH(pPlan->pSubplans);
L
Liu Jicong 已提交
286 287
  ASSERT(totLevel <= 2);
  pStream->tasks = taosArrayInit(totLevel, sizeof(void*));
L
Liu Jicong 已提交
288

L
Liu Jicong 已提交
289
  bool hasExtraSink = false;
L
Liu Jicong 已提交
290
  if (totLevel == 2 || strcmp(pStream->sourceDb, pStream->targetDb) != 0) {
L
Liu Jicong 已提交
291 292 293 294
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);
    // add extra sink
    hasExtraSink = true;
L
Liu Jicong 已提交
295 296 297 298 299
    if (pStream->fixedSinkVgId == 0) {
      mndAddShuffledSinkToStream(pMnode, pTrans, pStream);
    } else {
      mndAddFixedSinkToStream(pMnode, pTrans, pStream);
    }
L
Liu Jicong 已提交
300 301
  }

L
Liu Jicong 已提交
302 303 304 305
  for (int32_t level = 0; level < totLevel; level++) {
    SArray*        taskOneLevel = taosArrayInit(0, sizeof(void*));
    SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, level);
    ASSERT(LIST_LENGTH(inner->pNodeList) == 1);
L
Liu Jicong 已提交
306

L
Liu Jicong 已提交
307
    SSubplan* plan = nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
308 309 310 311 312

    // 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 已提交
313
      ASSERT(plan->subplanType == SUBPLAN_TYPE_SCAN);
L
Liu Jicong 已提交
314 315

      // replicate task to each vnode
L
Liu Jicong 已提交
316 317
      void* pIter = NULL;
      while (1) {
L
Liu Jicong 已提交
318
        SVgObj* pVgroup;
L
Liu Jicong 已提交
319 320 321 322 323 324
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
        if (pIter == NULL) break;
        if (pVgroup->dbUid != pStream->dbUid) {
          sdbRelease(pSdb, pVgroup);
          continue;
        }
L
Liu Jicong 已提交
325
        SStreamTask* pTask = tNewSStreamTask(pStream->uid, 0);
L
Liu Jicong 已提交
326 327
        // source part
        pTask->sourceType = TASK_SOURCE__SCAN;
L
Liu Jicong 已提交
328
        pTask->inputType = TASK_INPUT_TYPE__SUMBIT_BLOCK;
L
Liu Jicong 已提交
329

L
Liu Jicong 已提交
330 331 332
        // sink part
        if (level == 0) {
          // only for inplace
L
Liu Jicong 已提交
333
          pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
334
          if (!hasExtraSink) {
L
Liu Jicong 已提交
335 336
#if 1
            if (pStream->createdBy == STREAM_CREATED_BY__SMA) {
L
Liu Jicong 已提交
337
              pTask->sinkType = TASK_SINK__SMA;
L
Liu Jicong 已提交
338
              pTask->smaSink.smaId = pStream->smaId;
L
Liu Jicong 已提交
339 340
            } else {
              pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
341
              pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
342
              memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
343
              pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
344
            }
L
Liu Jicong 已提交
345
#endif
L
Liu Jicong 已提交
346
          }
L
Liu Jicong 已提交
347 348 349
        } else {
          pTask->sinkType = TASK_SINK__NONE;
        }
L
Liu Jicong 已提交
350

L
Liu Jicong 已提交
351 352 353 354 355 356 357
        // dispatch part
        if (level == 0) {
          pTask->dispatchType = TASK_DISPATCH__NONE;
        } else {
          // add fixed ep dispatcher
          int32_t lastLevel = level - 1;
          ASSERT(lastLevel == 0);
L
Liu Jicong 已提交
358
          if (hasExtraSink) lastLevel++;
L
Liu Jicong 已提交
359 360 361
          SArray* pArray = taosArrayGetP(pStream->tasks, lastLevel);
          // one merge only
          ASSERT(taosArrayGetSize(pArray) == 1);
L
Liu Jicong 已提交
362
          SStreamTask* lastLevelTask = taosArrayGetP(pArray, 0);
363 364
          /*pTask->dispatchMsgType = TDMT_VND_TASK_MERGE_EXEC;*/
          pTask->dispatchMsgType = TDMT_VND_TASK_DISPATCH;
L
Liu Jicong 已提交
365 366
          pTask->dispatchType = TASK_DISPATCH__FIXED;

L
Liu Jicong 已提交
367
          pTask->fixedEpDispatcher.taskId = lastLevelTask->taskId;
L
Liu Jicong 已提交
368 369 370 371 372 373
          pTask->fixedEpDispatcher.nodeId = lastLevelTask->nodeId;
          pTask->fixedEpDispatcher.epSet = lastLevelTask->epSet;
        }

        // exec part
        pTask->execType = TASK_EXEC__PIPE;
L
Liu Jicong 已提交
374
        pTask->exec.parallelizable = 1;
L
Liu Jicong 已提交
375
        if (mndAssignTaskToVg(pMnode, pTrans, pTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
376 377 378 379
          sdbRelease(pSdb, pVgroup);
          qDestroyQueryPlan(pPlan);
          return -1;
        }
L
Liu Jicong 已提交
380 381
        sdbRelease(pSdb, pVgroup);
        taosArrayPush(taskOneLevel, &pTask);
L
Liu Jicong 已提交
382
      }
L
Liu Jicong 已提交
383
    } else {
L
Liu Jicong 已提交
384 385 386 387 388 389
      // merge plan

      // TODO if has snode, assign to snode

      // else, assign to vnode
      ASSERT(plan->subplanType == SUBPLAN_TYPE_MERGE);
L
Liu Jicong 已提交
390
      SStreamTask* pTask = tNewSStreamTask(pStream->uid, 0);
L
Liu Jicong 已提交
391 392 393

      // source part, currently only support multi source
      pTask->sourceType = TASK_SOURCE__PIPE;
L
Liu Jicong 已提交
394
      pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
395 396

      // sink part
L
Liu Jicong 已提交
397
      pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
398 399

      // dispatch part
L
Liu Jicong 已提交
400 401 402 403 404 405
      ASSERT(hasExtraSink);
      /*pTask->dispatchType = TASK_DISPATCH__NONE;*/
#if 1

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

409 410
          /*pTask->dispatchMsgType = TDMT_VND_TASK_WRITE_EXEC;*/
          pTask->dispatchMsgType = TDMT_VND_TASK_DISPATCH;
L
Liu Jicong 已提交
411
          SDbObj* pDb = mndAcquireDb(pMnode, pStream->targetDb);
L
Liu Jicong 已提交
412 413 414 415 416 417
          ASSERT(pDb);
          if (mndExtractDbInfo(pMnode, pDb, &pTask->shuffleDispatcher.dbInfo, NULL) < 0) {
            sdbRelease(pSdb, pDb);
            qDestroyQueryPlan(pPlan);
            return -1;
          }
L
Liu Jicong 已提交
418
          sdbRelease(pSdb, pDb);
L
Liu Jicong 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

          // 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);
              /*printf("vgid %d node id %d\n", pVgInfo->vgId, pTask->nodeId);*/
              if (pLastLevelTask->nodeId == pVgInfo->vgId) {
                pVgInfo->taskId = pLastLevelTask->taskId;
                /*printf("taskid %d set to %d\n", pVgInfo->taskId, pTask->taskId);*/
                break;
              }
L
Liu Jicong 已提交
436 437
            }
          }
L
Liu Jicong 已提交
438 439
        } else {
          pTask->dispatchType = TASK_DISPATCH__FIXED;
440 441
          /*pTask->dispatchMsgType = TDMT_VND_TASK_WRITE_EXEC;*/
          pTask->dispatchMsgType = TDMT_VND_TASK_DISPATCH;
L
Liu Jicong 已提交
442 443 444 445 446 447 448
          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 已提交
449
        }
L
Liu Jicong 已提交
450 451
      }
#endif
L
Liu Jicong 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

      // exec part
      pTask->execType = TASK_EXEC__MERGE;
      pTask->exec.parallelizable = 0;
      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);
      taosArrayPush(taskOneLevel, &pTask);
    }

    taosArrayPush(pStream->tasks, &taskOneLevel);
  }

  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 已提交
479
      }
L
Liu Jicong 已提交
480
      SStreamTask* pTask = tNewSStreamTask(pStream->uid, 0);
L
Liu Jicong 已提交
481

L
Liu Jicong 已提交
482 483
      // source part
      pTask->sourceType = TASK_SOURCE__MERGE;
L
Liu Jicong 已提交
484
      pTask->inputType = TASK_INPUT_TYPE__DATA_BLOCK;
L
Liu Jicong 已提交
485 486

      // sink part
L
Liu Jicong 已提交
487
      pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
488 489 490 491 492 493 494

      // dispatch part
      pTask->dispatchType = TASK_DISPATCH__NONE;

      // exec part
      pTask->execType = TASK_EXEC__NONE;
      pTask->exec.parallelizable = 0;
L
Liu Jicong 已提交
495 496
    }
  }
L
Liu Jicong 已提交
497 498

  // free memory
L
Liu Jicong 已提交
499
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
500

L
Liu Jicong 已提交
501 502
  return 0;
}
L
Liu Jicong 已提交
503 504

int32_t mndSchedInitSubEp(SMnode* pMnode, const SMqTopicObj* pTopic, SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
505 506
  SSdb*       pSdb = pMnode->pSdb;
  SVgObj*     pVgroup = NULL;
L
Liu Jicong 已提交
507 508
  SQueryPlan* pPlan = NULL;
  SSubplan*   plan = NULL;
L
Liu Jicong 已提交
509

L
Liu Jicong 已提交
510
  if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
511 512 513 514 515
    pPlan = qStringToQueryPlan(pTopic->physicalPlan);
    if (pPlan == NULL) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
L
Liu Jicong 已提交
516

L
Liu Jicong 已提交
517 518 519
    int32_t levelNum = LIST_LENGTH(pPlan->pSubplans);
    if (levelNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
520
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
521 522
      return -1;
    }
L
Liu Jicong 已提交
523

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

L
Liu Jicong 已提交
526 527 528
    int32_t opNum = LIST_LENGTH(inner->pNodeList);
    if (opNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
529
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
530 531 532
      return -1;
    }
    plan = nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
533
  }
L
Liu Jicong 已提交
534

535 536
  ASSERT(pSub->unassignedVgs);
  ASSERT(taosHashGetSize(pSub->consumerHash) == 0);
537

L
Liu Jicong 已提交
538 539 540 541 542 543 544 545 546 547 548
  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++;

549
    SMqVgEp* pVgEp = taosMemoryMalloc(sizeof(SMqVgEp));
L
Liu Jicong 已提交
550 551
    pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
    pVgEp->vgId = pVgroup->vgId;
552
    taosArrayPush(pSub->unassignedVgs, &pVgEp);
553

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

L
Liu Jicong 已提交
556
    if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569
      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 已提交
570
    }
L
Liu Jicong 已提交
571 572
  }

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

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

X
Xiaoyu Wang 已提交
577
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
578

L
Liu Jicong 已提交
579 580
  return 0;
}