mndScheduler.c 17.8 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
extern bool tsSchedStreamToSnode;
L
Liu Jicong 已提交
37

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

45
int32_t mndConvertRsmaTask(char** pDst, int32_t* pDstLen, const char* ast, int64_t uid, int8_t triggerType,
46
                           int64_t watermark) {
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;
  }

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

L
Liu Jicong 已提交
61 62 63 64 65
  SPlanContext cxt = {
      .pAstRoot = pAst,
      .topicQuery = false,
      .streamQuery = true,
      .rSmaQuery = true,
L
Liu Jicong 已提交
66
      .triggerType = triggerType,
L
Liu Jicong 已提交
67 68
      .watermark = watermark,
  };
L
Liu Jicong 已提交
69

L
Liu Jicong 已提交
70 71 72 73 74 75 76 77 78 79
  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;
  }
80
  SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 0);
L
Liu Jicong 已提交
81 82 83 84 85 86 87

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

88 89
  SSubplan* plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
  if (qSubPlanToString(plan, pDst, pDstLen) < 0) {
L
Liu Jicong 已提交
90 91 92 93 94 95
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    goto END;
  }

END:
  if (pAst) nodesDestroyNode(pAst);
96
  if (pPlan) nodesDestroyNode((SNode*)pPlan);
L
Liu Jicong 已提交
97 98 99
  return terrno;
}

100
int32_t mndAddSinkToTask(SMnode* pMnode, SStreamObj* pStream, SStreamTask* pTask) {
L
Liu Jicong 已提交
101 102
  pTask->dispatchType = TASK_DISPATCH__NONE;
  // sink
L
Liu Jicong 已提交
103
  if (pStream->smaId != 0) {
L
Liu Jicong 已提交
104 105 106 107 108 109 110 111 112 113 114
    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;
}

115
int32_t mndAddDispatcherToInnerTask(SMnode* pMnode, SStreamObj* pStream, SStreamTask* pTask) {
L
Liu Jicong 已提交
116
  pTask->sinkType = TASK_SINK__NONE;
L
Liu Jicong 已提交
117 118 119

  bool isShuffle = false;

L
Liu Jicong 已提交
120 121 122
  if (pStream->fixedSinkVgId == 0) {
    SDbObj* pDb = mndAcquireDb(pMnode, pStream->targetDb);
    ASSERT(pDb);
L
Liu Jicong 已提交
123 124
    if (pDb->cfg.numOfVgroups > 1) {
      isShuffle = true;
L
Liu Jicong 已提交
125 126
      pTask->dispatchType = TASK_DISPATCH__SHUFFLE;
      pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
L
Liu Jicong 已提交
127 128 129 130
      if (mndExtractDbInfo(pMnode, pDb, &pTask->shuffleDispatcher.dbInfo, NULL) < 0) {
        ASSERT(0);
        return -1;
      }
131
    }
L
Liu Jicong 已提交
132

133
    sdbRelease(pMnode->pSdb, pDb);
L
Liu Jicong 已提交
134
  }
135

L
Liu Jicong 已提交
136
  if (isShuffle) {
L
Liu Jicong 已提交
137
    memcpy(pTask->shuffleDispatcher.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
138 139 140 141 142 143 144 145 146
    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) {
L
Liu Jicong 已提交
147
          ASSERT(pVgInfo->vgId > 0);
148 149 150
          pVgInfo->taskId = pLastLevelTask->taskId;
          ASSERT(pVgInfo->taskId != 0);
          break;
L
Liu Jicong 已提交
151 152 153
        }
      }
    }
154 155 156 157 158 159 160 161 162 163
  } 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;
L
Liu Jicong 已提交
164 165 166 167
  }
  return 0;
}

168
int32_t mndAssignTaskToVg(SMnode* pMnode, SStreamTask* pTask, SSubplan* plan, const SVgObj* pVgroup) {
L
Liu Jicong 已提交
169
  int32_t msgLen;
L
Liu Jicong 已提交
170 171 172
  pTask->nodeId = pVgroup->vgId;
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);

L
Liu Jicong 已提交
173
  plan->execNode.nodeId = pVgroup->vgId;
L
Liu Jicong 已提交
174
  plan->execNode.epSet = pTask->epSet;
L
Liu Jicong 已提交
175

L
Liu Jicong 已提交
176
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
177
    ASSERT(0);
L
Liu Jicong 已提交
178 179 180
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
181
  ASSERT(pTask->dispatchType != TASK_DISPATCH__NONE || pTask->sinkType != TASK_SINK__NONE);
L
Liu Jicong 已提交
182 183 184
  return 0;
}

L
Liu Jicong 已提交
185
SSnodeObj* mndSchedFetchOneSnode(SMnode* pMnode) {
L
Liu Jicong 已提交
186
  SSnodeObj* pObj = NULL;
L
Liu Jicong 已提交
187 188 189
  void*      pIter = NULL;
  // TODO random fetch
  pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void**)&pObj);
L
Liu Jicong 已提交
190 191 192
  return pObj;
}

193
int32_t mndAssignTaskToSnode(SMnode* pMnode, SStreamTask* pTask, SSubplan* plan, const SSnodeObj* pSnode) {
L
Liu Jicong 已提交
194
  int32_t msgLen;
L
Liu Jicong 已提交
195

L
Liu Jicong 已提交
196
  pTask->nodeId = SNODE_HANDLE;
L
Liu Jicong 已提交
197 198 199 200
  pTask->epSet = mndAcquireEpFromSnode(pMnode, pSnode);

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

L
Liu Jicong 已提交
202
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
203
    ASSERT(0);
L
Liu Jicong 已提交
204 205 206
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
207 208 209
  return 0;
}

L
Liu Jicong 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
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;
}

225
int32_t mndAddShuffleSinkTasksToStream(SMnode* pMnode, SStreamObj* pStream) {
L
Liu Jicong 已提交
226 227 228 229 230 231 232 233 234 235
  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;
S
Shengliang Guan 已提交
236
    if (!mndVgroupInDb(pVgroup, pStream->targetDbUid)) {
L
Liu Jicong 已提交
237 238 239
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
240

L
Liu Jicong 已提交
241
    SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
242
    if (pTask == NULL) {
243
      sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
244 245 246
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
247
    mndAddTaskToTaskSet(tasks, pTask);
L
Liu Jicong 已提交
248 249 250 251 252

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

    // source
253
    pTask->isDataScan = 0;
L
Liu Jicong 已提交
254 255 256 257 258

    // exec
    pTask->execType = TASK_EXEC__NONE;

    // sink
L
Liu Jicong 已提交
259
    if (pStream->smaId != 0) {
L
Liu Jicong 已提交
260
      pTask->sinkType = TASK_SINK__SMA;
L
Liu Jicong 已提交
261
      pTask->smaSink.smaId = pStream->smaId;
L
Liu Jicong 已提交
262 263
    } else {
      pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
264
      pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
265
      memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
266 267
      pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
      ASSERT(pTask->tbSink.pSchemaWrapper);
L
Liu Jicong 已提交
268 269 270 271 272 273 274 275
    }

    // dispatch
    pTask->dispatchType = TASK_DISPATCH__NONE;
  }
  return 0;
}

276
int32_t mndAddFixedSinkTaskToStream(SMnode* pMnode, SStreamObj* pStream) {
L
Liu Jicong 已提交
277 278
  ASSERT(pStream->fixedSinkVgId != 0);
  SArray*      tasks = taosArrayGetP(pStream->tasks, 0);
L
Liu Jicong 已提交
279
  SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
280 281 282 283
  if (pTask == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
284
  mndAddTaskToTaskSet(tasks, pTask);
L
Liu Jicong 已提交
285

L
Liu Jicong 已提交
286 287
  ASSERT(pStream->fixedSinkVg.vgId == pStream->fixedSinkVgId);

L
Liu Jicong 已提交
288
  pTask->nodeId = pStream->fixedSinkVgId;
L
Liu Jicong 已提交
289
#if 0
L
Liu Jicong 已提交
290 291 292 293 294
  SVgObj* pVgroup = mndAcquireVgroup(pMnode, pStream->fixedSinkVgId);
  if (pVgroup == NULL) {
    return -1;
  }
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);
L
Liu Jicong 已提交
295 296
#endif
  pTask->epSet = mndGetVgroupEpset(pMnode, &pStream->fixedSinkVg);
L
Liu Jicong 已提交
297

L
Liu Jicong 已提交
298
  // source
299
  pTask->isDataScan = 0;
L
Liu Jicong 已提交
300 301 302 303 304

  // exec
  pTask->execType = TASK_EXEC__NONE;

  // sink
L
Liu Jicong 已提交
305
  if (pStream->smaId != 0) {
L
Liu Jicong 已提交
306 307 308 309
    pTask->sinkType = TASK_SINK__SMA;
    pTask->smaSink.smaId = pStream->smaId;
  } else {
    pTask->sinkType = TASK_SINK__TABLE;
L
Liu Jicong 已提交
310
    pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
311
    memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
312
    pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
313
  }
L
Liu Jicong 已提交
314

L
Liu Jicong 已提交
315 316 317 318 319
  // dispatch
  pTask->dispatchType = TASK_DISPATCH__NONE;
  return 0;
}

320
int32_t mndScheduleStream(SMnode* pMnode, SStreamObj* pStream) {
L
Liu Jicong 已提交
321 322 323 324 325 326 327 328 329
  SSdb*       pSdb = pMnode->pSdb;
  SQueryPlan* pPlan = qStringToQueryPlan(pStream->physicalPlan);
  if (pPlan == NULL) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
  int32_t totLevel = LIST_LENGTH(pPlan->pSubplans);
  ASSERT(totLevel <= 2);
  pStream->tasks = taosArrayInit(totLevel, sizeof(void*));
L
Liu Jicong 已提交
330
  pStream->isDistributed = totLevel == 2;
L
Liu Jicong 已提交
331

332 333 334 335 336 337 338 339 340
  bool    hasExtraSink = false;
  bool    externalTargetDB = strcmp(pStream->sourceDb, pStream->targetDb) != 0;
  SDbObj* pDbObj = mndAcquireDb(pMnode, pStream->targetDb);
  ASSERT(pDbObj != NULL);
  sdbRelease(pSdb, pDbObj);

  bool multiTarget = pDbObj->cfg.numOfVgroups > 1;

  if (totLevel == 2 || externalTargetDB || multiTarget) {
L
Liu Jicong 已提交
341 342 343 344 345
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);
    // add extra sink
    hasExtraSink = true;
    if (pStream->fixedSinkVgId == 0) {
346 347 348 349
      if (mndAddShuffleSinkTasksToStream(pMnode, pStream) < 0) {
        // TODO free
        return -1;
      }
L
Liu Jicong 已提交
350
    } else {
351 352 353 354
      if (mndAddFixedSinkTaskToStream(pMnode, pStream) < 0) {
        // TODO free
        return -1;
      }
L
Liu Jicong 已提交
355 356
    }
  }
357

L
Liu Jicong 已提交
358
  if (totLevel > 1) {
L
Liu Jicong 已提交
359 360
    SStreamTask* pInnerTask;
    // inner level
L
Liu Jicong 已提交
361 362 363 364
    {
      SArray* taskInnerLevel = taosArrayInit(0, sizeof(void*));
      taosArrayPush(pStream->tasks, &taskInnerLevel);

365 366
      SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 0);
      SSubplan*      plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
367 368
      ASSERT(plan->subplanType == SUBPLAN_TYPE_MERGE);

L
Liu Jicong 已提交
369
      pInnerTask = tNewSStreamTask(pStream->uid);
370 371 372 373 374
      if (pInnerTask == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
375
      mndAddTaskToTaskSet(taskInnerLevel, pInnerTask);
L
Liu Jicong 已提交
376 377 378

      pInnerTask->childEpInfo = taosArrayInit(0, sizeof(void*));

379 380
      // source
      pInnerTask->isDataScan = 0;
L
Liu Jicong 已提交
381

382
      // trigger
L
Liu Jicong 已提交
383
      pInnerTask->triggerParam = pStream->triggerParam;
384

L
Liu Jicong 已提交
385
      // dispatch
386
      if (mndAddDispatcherToInnerTask(pMnode, pStream, pInnerTask) < 0) {
387 388 389
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
390 391

      // exec
L
Liu Jicong 已提交
392 393
      pInnerTask->execType = TASK_EXEC__PIPE;

L
Liu Jicong 已提交
394
#if 0
5
54liuyao 已提交
395 396 397 398
      SDbObj* pSourceDb = mndAcquireDb(pMnode, pStream->sourceDb);
      ASSERT(pDbObj != NULL);
      sdbRelease(pSdb, pSourceDb);
      pInnerTask->numOfVgroups = pSourceDb->cfg.numOfVgroups;
L
Liu Jicong 已提交
399
#endif
5
54liuyao 已提交
400

L
Liu Jicong 已提交
401 402 403 404
      if (tsSchedStreamToSnode) {
        SSnodeObj* pSnode = mndSchedFetchOneSnode(pMnode);
        if (pSnode == NULL) {
          SVgObj* pVgroup = mndSchedFetchOneVg(pMnode, pStream->sourceDbUid);
405
          if (mndAssignTaskToVg(pMnode, pInnerTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
406 407 408 409 410
            sdbRelease(pSdb, pVgroup);
            qDestroyQueryPlan(pPlan);
            return -1;
          }
        } else {
411
          if (mndAssignTaskToSnode(pMnode, pInnerTask, plan, pSnode) < 0) {
L
Liu Jicong 已提交
412 413 414 415 416 417 418
            sdbRelease(pSdb, pSnode);
            qDestroyQueryPlan(pPlan);
            return -1;
          }
        }
      } else {
        SVgObj* pVgroup = mndSchedFetchOneVg(pMnode, pStream->sourceDbUid);
419
        if (mndAssignTaskToVg(pMnode, pInnerTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
420 421 422 423
          sdbRelease(pSdb, pVgroup);
          qDestroyQueryPlan(pPlan);
          return -1;
        }
L
Liu Jicong 已提交
424 425 426
      }
    }

L
Liu Jicong 已提交
427
    // source level
L
Liu Jicong 已提交
428 429 430
    SArray* taskSourceLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskSourceLevel);

431 432
    SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 1);
    SSubplan*      plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
433 434 435 436 437 438 439
    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;
S
Shengliang Guan 已提交
440
      if (!mndVgroupInDb(pVgroup, pStream->sourceDbUid)) {
L
Liu Jicong 已提交
441 442 443
        sdbRelease(pSdb, pVgroup);
        continue;
      }
S
Shengliang Guan 已提交
444

L
Liu Jicong 已提交
445
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
446 447 448 449 450 451
      if (pInnerTask == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
452 453
      mndAddTaskToTaskSet(taskSourceLevel, pTask);

454
      // source
L
Liu Jicong 已提交
455
      pTask->isDataScan = 1;
L
Liu Jicong 已提交
456

L
Liu Jicong 已提交
457 458 459 460 461
      // add fixed vg dispatch
      pTask->sinkType = TASK_SINK__NONE;
      pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
      pTask->dispatchType = TASK_DISPATCH__FIXED;

L
Liu Jicong 已提交
462 463 464
      pTask->fixedEpDispatcher.taskId = pInnerTask->taskId;
      pTask->fixedEpDispatcher.nodeId = pInnerTask->nodeId;
      pTask->fixedEpDispatcher.epSet = pInnerTask->epSet;
L
Liu Jicong 已提交
465 466 467

      // exec
      pTask->execType = TASK_EXEC__PIPE;
468
      if (mndAssignTaskToVg(pMnode, pTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
469 470 471 472
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486

      SStreamChildEpInfo* pEpInfo = taosMemoryMalloc(sizeof(SStreamChildEpInfo));
      if (pEpInfo == NULL) {
        ASSERT(0);
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
      pEpInfo->childId = pTask->selfChildId;
      pEpInfo->epSet = pTask->epSet;
      pEpInfo->nodeId = pTask->nodeId;
      pEpInfo->taskId = pTask->taskId;
      taosArrayPush(pInnerTask->childEpInfo, &pEpInfo);
L
Liu Jicong 已提交
487 488 489 490 491 492 493
    }
  }

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

494
    SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 0);
L
Liu Jicong 已提交
495
    ASSERT(LIST_LENGTH(inner->pNodeList) == 1);
496
    SSubplan* plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
497 498 499 500 501 502 503
    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;
S
Shengliang Guan 已提交
504
      if (!mndVgroupInDb(pVgroup, pStream->sourceDbUid)) {
L
Liu Jicong 已提交
505 506 507
        sdbRelease(pSdb, pVgroup);
        continue;
      }
S
Shengliang Guan 已提交
508

L
Liu Jicong 已提交
509
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
510 511 512 513 514
      if (pTask == NULL) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
515 516
      mndAddTaskToTaskSet(taskOneLevel, pTask);

L
Liu Jicong 已提交
517
      // source
518
      pTask->isDataScan = 1;
L
Liu Jicong 已提交
519

520 521 522
      // trigger
      pTask->triggerParam = pStream->triggerParam;

L
Liu Jicong 已提交
523 524
      // sink or dispatch
      if (hasExtraSink) {
525
        mndAddDispatcherToInnerTask(pMnode, pStream, pTask);
L
Liu Jicong 已提交
526
      } else {
527
        mndAddSinkToTask(pMnode, pStream, pTask);
L
Liu Jicong 已提交
528 529 530 531
      }

      // exec
      pTask->execType = TASK_EXEC__PIPE;
532
      if (mndAssignTaskToVg(pMnode, pTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
533 534 535 536 537 538
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
    }
  }
L
Liu Jicong 已提交
539
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
540 541
  return 0;
}
L
Liu Jicong 已提交
542 543

int32_t mndSchedInitSubEp(SMnode* pMnode, const SMqTopicObj* pTopic, SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
544 545
  SSdb*       pSdb = pMnode->pSdb;
  SVgObj*     pVgroup = NULL;
L
Liu Jicong 已提交
546 547
  SQueryPlan* pPlan = NULL;
  SSubplan*   plan = NULL;
L
Liu Jicong 已提交
548

L
Liu Jicong 已提交
549
  if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
550 551 552 553 554
    pPlan = qStringToQueryPlan(pTopic->physicalPlan);
    if (pPlan == NULL) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
L
Liu Jicong 已提交
555

L
Liu Jicong 已提交
556 557 558
    int32_t levelNum = LIST_LENGTH(pPlan->pSubplans);
    if (levelNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
559
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
560 561
      return -1;
    }
L
Liu Jicong 已提交
562

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

L
Liu Jicong 已提交
565 566 567
    int32_t opNum = LIST_LENGTH(inner->pNodeList);
    if (opNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
568
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
569 570
      return -1;
    }
571
    plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
572
  }
L
Liu Jicong 已提交
573

574 575
  ASSERT(pSub->unassignedVgs);
  ASSERT(taosHashGetSize(pSub->consumerHash) == 0);
576

L
Liu Jicong 已提交
577 578 579 580
  void* pIter = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
581
    if (!mndVgroupInDb(pVgroup, pTopic->dbUid)) {
L
Liu Jicong 已提交
582 583 584 585 586 587
      sdbRelease(pSdb, pVgroup);
      continue;
    }

    pSub->vgNum++;

588
    SMqVgEp* pVgEp = taosMemoryMalloc(sizeof(SMqVgEp));
L
Liu Jicong 已提交
589 590
    pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
    pVgEp->vgId = pVgroup->vgId;
591
    taosArrayPush(pSub->unassignedVgs, &pVgEp);
592

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

L
Liu Jicong 已提交
595
    if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608
      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 已提交
609
    }
L
Liu Jicong 已提交
610 611
  }

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

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

X
Xiaoyu Wang 已提交
616
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
617

L
Liu Jicong 已提交
618 619
  return 0;
}