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
/*
 * 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 "mndShow.h"
L
Liu Jicong 已提交
22
#include "mndSnode.h"
L
Liu Jicong 已提交
23
#include "mndStb.h"
L
Liu Jicong 已提交
24
#include "mndStream.h"
L
Liu Jicong 已提交
25 26 27 28 29
#include "mndSubscribe.h"
#include "mndTopic.h"
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
X
Xiaoyu Wang 已提交
30
#include "parser.h"
L
Liu Jicong 已提交
31 32
#include "tcompare.h"
#include "tname.h"
L
Liu Jicong 已提交
33 34
#include "tuuid.h"

L
Liu Jicong 已提交
35
extern bool tsDeployOnSnode;
L
Liu Jicong 已提交
36

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

44
int32_t mndConvertRsmaTask(char** pDst, int32_t* pDstLen, const char* ast, int64_t uid, int8_t triggerType,
45
                           int64_t watermark, int64_t deleteMark) {
L
Liu Jicong 已提交
46 47 48 49 50 51 52 53 54
  SNode*      pAst = NULL;
  SQueryPlan* pPlan = NULL;
  terrno = TSDB_CODE_SUCCESS;

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

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

L
Liu Jicong 已提交
60 61 62 63 64
  SPlanContext cxt = {
      .pAstRoot = pAst,
      .topicQuery = false,
      .streamQuery = true,
      .rSmaQuery = true,
L
Liu Jicong 已提交
65
      .triggerType = triggerType,
L
Liu Jicong 已提交
66
      .watermark = watermark,
67
      .deleteMark = deleteMark,
L
Liu Jicong 已提交
68
  };
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
  if (pStream->smaId != 0) {
102
    pTask->outputType = TASK_OUTPUT__SMA;
L
Liu Jicong 已提交
103 104
    pTask->smaSink.smaId = pStream->smaId;
  } else {
105
    pTask->outputType = TASK_OUTPUT__TABLE;
L
Liu Jicong 已提交
106 107 108 109 110 111 112
    pTask->tbSink.stbUid = pStream->targetStbUid;
    memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
    pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
  }
  return 0;
}

113
int32_t mndAddDispatcherToInnerTask(SMnode* pMnode, SStreamObj* pStream, SStreamTask* pTask) {
L
Liu Jicong 已提交
114 115
  bool isShuffle = false;

L
Liu Jicong 已提交
116 117
  if (pStream->fixedSinkVgId == 0) {
    SDbObj* pDb = mndAcquireDb(pMnode, pStream->targetDb);
L
Liu Jicong 已提交
118 119
    if (pDb->cfg.numOfVgroups > 1) {
      isShuffle = true;
120
      pTask->outputType = TASK_OUTPUT__SHUFFLE_DISPATCH;
L
Liu Jicong 已提交
121
      pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
L
Liu Jicong 已提交
122 123 124
      if (mndExtractDbInfo(pMnode, pDb, &pTask->shuffleDispatcher.dbInfo, NULL) < 0) {
        return -1;
      }
125
    }
L
Liu Jicong 已提交
126

127
    sdbRelease(pMnode->pSdb, pDb);
L
Liu Jicong 已提交
128
  }
129

L
Liu Jicong 已提交
130
  if (isShuffle) {
L
Liu Jicong 已提交
131
    memcpy(pTask->shuffleDispatcher.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
132 133 134 135 136 137 138 139 140 141 142
    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 已提交
143 144 145
        }
      }
    }
146
  } else {
147
    pTask->outputType = TASK_OUTPUT__FIXED_DISPATCH;
148 149 150 151 152 153 154
    pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
    SArray* pArray = taosArrayGetP(pStream->tasks, 0);
    // one sink only
    SStreamTask* lastLevelTask = taosArrayGetP(pArray, 0);
    pTask->fixedEpDispatcher.taskId = lastLevelTask->taskId;
    pTask->fixedEpDispatcher.nodeId = lastLevelTask->nodeId;
    pTask->fixedEpDispatcher.epSet = lastLevelTask->epSet;
L
Liu Jicong 已提交
155 156 157 158
  }
  return 0;
}

159
int32_t mndAssignTaskToVg(SMnode* pMnode, SStreamTask* pTask, SSubplan* plan, const SVgObj* pVgroup) {
L
Liu Jicong 已提交
160
  int32_t msgLen;
L
Liu Jicong 已提交
161 162 163
  pTask->nodeId = pVgroup->vgId;
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);

L
Liu Jicong 已提交
164
  plan->execNode.nodeId = pVgroup->vgId;
L
Liu Jicong 已提交
165
  plan->execNode.epSet = pTask->epSet;
L
Liu Jicong 已提交
166

L
Liu Jicong 已提交
167
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
L
Liu Jicong 已提交
168 169 170 171 172 173
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
  return 0;
}

L
Liu Jicong 已提交
174
SSnodeObj* mndSchedFetchOneSnode(SMnode* pMnode) {
L
Liu Jicong 已提交
175
  SSnodeObj* pObj = NULL;
L
Liu Jicong 已提交
176 177 178
  void*      pIter = NULL;
  // TODO random fetch
  pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void**)&pObj);
L
Liu Jicong 已提交
179 180 181
  return pObj;
}

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

L
Liu Jicong 已提交
185
  pTask->nodeId = SNODE_HANDLE;
L
Liu Jicong 已提交
186 187
  pTask->epSet = mndAcquireEpFromSnode(pMnode, pSnode);

L
Liu Jicong 已提交
188
  plan->execNode.nodeId = SNODE_HANDLE;
L
Liu Jicong 已提交
189
  plan->execNode.epSet = pTask->epSet;
L
Liu Jicong 已提交
190

L
Liu Jicong 已提交
191
  if (qSubPlanToString(plan, &pTask->exec.qmsg, &msgLen) < 0) {
L
Liu Jicong 已提交
192 193 194
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
195 196 197
  return 0;
}

L
Liu Jicong 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
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;
}

213
int32_t mndAddShuffleSinkTasksToStream(SMnode* pMnode, SStreamObj* pStream) {
L
Liu Jicong 已提交
214 215 216 217 218
  SSdb*   pSdb = pMnode->pSdb;
  void*   pIter = NULL;
  SArray* tasks = taosArrayGetP(pStream->tasks, 0);

  while (1) {
219
    SVgObj* pVgroup = NULL;
L
Liu Jicong 已提交
220 221
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
222
    if (!mndVgroupInDb(pVgroup, pStream->targetDbUid)) {
L
Liu Jicong 已提交
223 224 225
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
226

L
Liu Jicong 已提交
227
    SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
228
    if (pTask == NULL) {
229
      sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
230 231 232
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
L
Liu Jicong 已提交
233
    pTask->fillHistory = pStream->fillHistory;
L
Liu Jicong 已提交
234
    mndAddTaskToTaskSet(tasks, pTask);
L
Liu Jicong 已提交
235 236 237 238

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

239 240
    // type
    pTask->taskLevel = TASK_LEVEL__SINK;
L
Liu Jicong 已提交
241 242

    // sink
L
Liu Jicong 已提交
243
    if (pStream->smaId != 0) {
244
      pTask->outputType = TASK_OUTPUT__SMA;
L
Liu Jicong 已提交
245
      pTask->smaSink.smaId = pStream->smaId;
L
Liu Jicong 已提交
246
    } else {
247
      pTask->outputType = TASK_OUTPUT__TABLE;
L
Liu Jicong 已提交
248
      pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
249
      memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
250
      pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
251 252 253 254
      if (pTask->tbSink.pSchemaWrapper == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
255
    }
256
    sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
257 258 259 260
  }
  return 0;
}

261
int32_t mndAddFixedSinkTaskToStream(SMnode* pMnode, SStreamObj* pStream) {
L
Liu Jicong 已提交
262
  SArray*      tasks = taosArrayGetP(pStream->tasks, 0);
L
Liu Jicong 已提交
263
  SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
264 265 266 267
  if (pTask == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
268
  pTask->fillHistory = pStream->fillHistory;
L
Liu Jicong 已提交
269
  mndAddTaskToTaskSet(tasks, pTask);
L
Liu Jicong 已提交
270 271

  pTask->nodeId = pStream->fixedSinkVgId;
L
Liu Jicong 已提交
272
#if 0
L
Liu Jicong 已提交
273 274 275 276 277
  SVgObj* pVgroup = mndAcquireVgroup(pMnode, pStream->fixedSinkVgId);
  if (pVgroup == NULL) {
    return -1;
  }
  pTask->epSet = mndGetVgroupEpset(pMnode, pVgroup);
L
Liu Jicong 已提交
278 279
#endif
  pTask->epSet = mndGetVgroupEpset(pMnode, &pStream->fixedSinkVg);
L
Liu Jicong 已提交
280

281
  pTask->taskLevel = TASK_LEVEL__SINK;
L
Liu Jicong 已提交
282 283

  // sink
L
Liu Jicong 已提交
284
  if (pStream->smaId != 0) {
285
    pTask->outputType = TASK_OUTPUT__SMA;
L
Liu Jicong 已提交
286 287
    pTask->smaSink.smaId = pStream->smaId;
  } else {
288
    pTask->outputType = TASK_OUTPUT__TABLE;
L
Liu Jicong 已提交
289
    pTask->tbSink.stbUid = pStream->targetStbUid;
L
Liu Jicong 已提交
290
    memcpy(pTask->tbSink.stbFullName, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
L
Liu Jicong 已提交
291
    pTask->tbSink.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
L
Liu Jicong 已提交
292
  }
L
Liu Jicong 已提交
293

L
Liu Jicong 已提交
294 295 296
  return 0;
}

297
int32_t mndScheduleStream(SMnode* pMnode, SStreamObj* pStream) {
L
Liu Jicong 已提交
298 299 300 301 302 303
  SSdb*       pSdb = pMnode->pSdb;
  SQueryPlan* pPlan = qStringToQueryPlan(pStream->physicalPlan);
  if (pPlan == NULL) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
L
Liu Jicong 已提交
304
  int32_t planTotLevel = LIST_LENGTH(pPlan->pSubplans);
L
Liu Jicong 已提交
305

L
Liu Jicong 已提交
306
  pStream->tasks = taosArrayInit(planTotLevel, sizeof(void*));
L
Liu Jicong 已提交
307

308 309 310
  bool    hasExtraSink = false;
  bool    externalTargetDB = strcmp(pStream->sourceDb, pStream->targetDb) != 0;
  SDbObj* pDbObj = mndAcquireDb(pMnode, pStream->targetDb);
L
Liu Jicong 已提交
311 312 313 314
  if (pDbObj == NULL) {
    terrno = TSDB_CODE_QRY_INVALID_INPUT;
    return -1;
  }
315 316

  bool multiTarget = pDbObj->cfg.numOfVgroups > 1;
L
Liu Jicong 已提交
317
  sdbRelease(pSdb, pDbObj);
318

319
  if (planTotLevel == 2 || externalTargetDB || multiTarget || pStream->fixedSinkVgId) {
320
    /*if (true) {*/
L
Liu Jicong 已提交
321 322 323 324 325
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);
    // add extra sink
    hasExtraSink = true;
    if (pStream->fixedSinkVgId == 0) {
326 327 328 329
      if (mndAddShuffleSinkTasksToStream(pMnode, pStream) < 0) {
        // TODO free
        return -1;
      }
L
Liu Jicong 已提交
330
    } else {
331 332 333 334
      if (mndAddFixedSinkTaskToStream(pMnode, pStream) < 0) {
        // TODO free
        return -1;
      }
L
Liu Jicong 已提交
335 336
    }
  }
L
Liu Jicong 已提交
337
  pStream->totalLevel = planTotLevel + hasExtraSink;
338

L
Liu Jicong 已提交
339
  if (planTotLevel > 1) {
L
Liu Jicong 已提交
340 341
    SStreamTask* pInnerTask;
    // inner level
L
Liu Jicong 已提交
342 343 344 345
    {
      SArray* taskInnerLevel = taosArrayInit(0, sizeof(void*));
      taosArrayPush(pStream->tasks, &taskInnerLevel);

346 347
      SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 0);
      SSubplan*      plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
348 349 350 351
      if (plan->subplanType != SUBPLAN_TYPE_MERGE) {
        terrno = TSDB_CODE_QRY_INVALID_INPUT;
        return -1;
      }
L
Liu Jicong 已提交
352

L
Liu Jicong 已提交
353
      pInnerTask = tNewSStreamTask(pStream->uid);
354 355 356 357 358
      if (pInnerTask == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
359
      pInnerTask->fillHistory = pStream->fillHistory;
L
Liu Jicong 已提交
360
      mndAddTaskToTaskSet(taskInnerLevel, pInnerTask);
L
Liu Jicong 已提交
361 362 363

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

364
      pInnerTask->taskLevel = TASK_LEVEL__AGG;
L
Liu Jicong 已提交
365

366
      // trigger
L
Liu Jicong 已提交
367
      pInnerTask->triggerParam = pStream->triggerParam;
368

L
Liu Jicong 已提交
369
      // dispatch
370
      if (mndAddDispatcherToInnerTask(pMnode, pStream, pInnerTask) < 0) {
371 372 373
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
374

L
Liu Jicong 已提交
375
      if (tsDeployOnSnode) {
L
Liu Jicong 已提交
376 377 378
        SSnodeObj* pSnode = mndSchedFetchOneSnode(pMnode);
        if (pSnode == NULL) {
          SVgObj* pVgroup = mndSchedFetchOneVg(pMnode, pStream->sourceDbUid);
379
          if (mndAssignTaskToVg(pMnode, pInnerTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
380 381 382 383
            sdbRelease(pSdb, pVgroup);
            qDestroyQueryPlan(pPlan);
            return -1;
          }
384
          sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
385
        } else {
386
          if (mndAssignTaskToSnode(pMnode, pInnerTask, plan, pSnode) < 0) {
L
Liu Jicong 已提交
387 388 389 390 391 392 393
            sdbRelease(pSdb, pSnode);
            qDestroyQueryPlan(pPlan);
            return -1;
          }
        }
      } else {
        SVgObj* pVgroup = mndSchedFetchOneVg(pMnode, pStream->sourceDbUid);
394
        if (mndAssignTaskToVg(pMnode, pInnerTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
395 396 397 398
          sdbRelease(pSdb, pVgroup);
          qDestroyQueryPlan(pPlan);
          return -1;
        }
399
        sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
400 401 402
      }
    }

L
Liu Jicong 已提交
403
    // source level
L
Liu Jicong 已提交
404 405 406
    SArray* taskSourceLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskSourceLevel);

407 408
    SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 1);
    SSubplan*      plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
409 410 411 412
    if (plan->subplanType != SUBPLAN_TYPE_SCAN) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
L
Liu Jicong 已提交
413 414 415 416 417 418

    void* pIter = NULL;
    while (1) {
      SVgObj* pVgroup;
      pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
      if (pIter == NULL) break;
S
Shengliang Guan 已提交
419
      if (!mndVgroupInDb(pVgroup, pStream->sourceDbUid)) {
L
Liu Jicong 已提交
420 421 422
        sdbRelease(pSdb, pVgroup);
        continue;
      }
S
Shengliang Guan 已提交
423

L
Liu Jicong 已提交
424
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
L
Liu Jicong 已提交
425
      if (pTask == NULL) {
426 427 428 429 430
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
431
      pTask->fillHistory = pStream->fillHistory;
L
Liu Jicong 已提交
432 433
      mndAddTaskToTaskSet(taskSourceLevel, pTask);

L
Liu Jicong 已提交
434 435
      pTask->triggerParam = 0;

436
      // source
437
      pTask->taskLevel = TASK_LEVEL__SOURCE;
L
Liu Jicong 已提交
438

L
Liu Jicong 已提交
439 440
      // add fixed vg dispatch
      pTask->dispatchMsgType = TDMT_STREAM_TASK_DISPATCH;
441
      pTask->outputType = TASK_OUTPUT__FIXED_DISPATCH;
L
Liu Jicong 已提交
442

L
Liu Jicong 已提交
443 444 445
      pTask->fixedEpDispatcher.taskId = pInnerTask->taskId;
      pTask->fixedEpDispatcher.nodeId = pInnerTask->nodeId;
      pTask->fixedEpDispatcher.epSet = pInnerTask->epSet;
L
Liu Jicong 已提交
446

447
      if (mndAssignTaskToVg(pMnode, pTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
448 449 450 451
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464

      SStreamChildEpInfo* pEpInfo = taosMemoryMalloc(sizeof(SStreamChildEpInfo));
      if (pEpInfo == NULL) {
        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);
465
      sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
466 467 468
    }
  }

L
Liu Jicong 已提交
469
  if (planTotLevel == 1) {
L
Liu Jicong 已提交
470 471 472
    SArray* taskOneLevel = taosArrayInit(0, sizeof(void*));
    taosArrayPush(pStream->tasks, &taskOneLevel);

473
    SNodeListNode* inner = (SNodeListNode*)nodesListGetNode(pPlan->pSubplans, 0);
L
Liu Jicong 已提交
474 475 476 477
    if (LIST_LENGTH(inner->pNodeList) != 1) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
478
    SSubplan* plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
479 480 481 482
    if (plan->subplanType != SUBPLAN_TYPE_SCAN) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
L
Liu Jicong 已提交
483 484 485 486 487 488

    void* pIter = NULL;
    while (1) {
      SVgObj* pVgroup;
      pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
      if (pIter == NULL) break;
S
Shengliang Guan 已提交
489
      if (!mndVgroupInDb(pVgroup, pStream->sourceDbUid)) {
L
Liu Jicong 已提交
490 491 492
        sdbRelease(pSdb, pVgroup);
        continue;
      }
S
Shengliang Guan 已提交
493

L
Liu Jicong 已提交
494
      SStreamTask* pTask = tNewSStreamTask(pStream->uid);
495 496 497 498 499
      if (pTask == NULL) {
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
L
Liu Jicong 已提交
500
      pTask->fillHistory = pStream->fillHistory;
L
Liu Jicong 已提交
501 502
      mndAddTaskToTaskSet(taskOneLevel, pTask);

L
Liu Jicong 已提交
503
      // source
504
      pTask->taskLevel = TASK_LEVEL__SOURCE;
L
Liu Jicong 已提交
505

506 507 508
      // trigger
      pTask->triggerParam = pStream->triggerParam;

L
Liu Jicong 已提交
509 510
      // sink or dispatch
      if (hasExtraSink) {
511
        mndAddDispatcherToInnerTask(pMnode, pStream, pTask);
L
Liu Jicong 已提交
512
      } else {
513
        mndAddSinkToTask(pMnode, pStream, pTask);
L
Liu Jicong 已提交
514 515
      }

516
      if (mndAssignTaskToVg(pMnode, pTask, plan, pVgroup) < 0) {
L
Liu Jicong 已提交
517 518 519 520
        sdbRelease(pSdb, pVgroup);
        qDestroyQueryPlan(pPlan);
        return -1;
      }
521
      sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
522 523
    }
  }
L
Liu Jicong 已提交
524
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
525 526
  return 0;
}
L
Liu Jicong 已提交
527 528

int32_t mndSchedInitSubEp(SMnode* pMnode, const SMqTopicObj* pTopic, SMqSubscribeObj* pSub) {
L
Liu Jicong 已提交
529 530
  SSdb*       pSdb = pMnode->pSdb;
  SVgObj*     pVgroup = NULL;
L
Liu Jicong 已提交
531 532
  SQueryPlan* pPlan = NULL;
  SSubplan*   plan = NULL;
L
Liu Jicong 已提交
533

L
Liu Jicong 已提交
534
  if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
535 536 537 538 539
    pPlan = qStringToQueryPlan(pTopic->physicalPlan);
    if (pPlan == NULL) {
      terrno = TSDB_CODE_QRY_INVALID_INPUT;
      return -1;
    }
L
Liu Jicong 已提交
540

L
Liu Jicong 已提交
541 542 543
    int32_t levelNum = LIST_LENGTH(pPlan->pSubplans);
    if (levelNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
544
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
545 546
      return -1;
    }
L
Liu Jicong 已提交
547

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

L
Liu Jicong 已提交
550 551 552
    int32_t opNum = LIST_LENGTH(inner->pNodeList);
    if (opNum != 1) {
      qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
553
      terrno = TSDB_CODE_MND_INVALID_TOPIC_QUERY;
L
Liu Jicong 已提交
554 555
      return -1;
    }
556
    plan = (SSubplan*)nodesListGetNode(inner->pNodeList, 0);
L
Liu Jicong 已提交
557
  }
L
Liu Jicong 已提交
558 559 560 561 562

  void* pIter = NULL;
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
563
    if (!mndVgroupInDb(pVgroup, pTopic->dbUid)) {
L
Liu Jicong 已提交
564 565 566 567 568 569
      sdbRelease(pSdb, pVgroup);
      continue;
    }

    pSub->vgNum++;

570
    SMqVgEp* pVgEp = taosMemoryMalloc(sizeof(SMqVgEp));
L
Liu Jicong 已提交
571 572
    pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup);
    pVgEp->vgId = pVgroup->vgId;
573
    taosArrayPush(pSub->unassignedVgs, &pVgEp);
574

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

L
Liu Jicong 已提交
577
    if (pTopic->subType == TOPIC_SUB_TYPE__COLUMN) {
L
Liu Jicong 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590
      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 已提交
591
    }
S
Shengliang Guan 已提交
592 593

    sdbRelease(pSdb, pVgroup);
L
Liu Jicong 已提交
594 595
  }

X
Xiaoyu Wang 已提交
596
  qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
597

L
Liu Jicong 已提交
598 599
  return 0;
}