scheduler.c 50.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 15
 */

D
dapan1121 已提交
16
#include "schedulerInt.h"
H
Hongze Cheng 已提交
17
#include "tmsg.h"
18
#include "query.h"
D
dapan 已提交
19
#include "catalog.h"
20

21
static SSchedulerMgmt schMgmt = {0};
D
dapan1121 已提交
22

D
dapan1121 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
uint64_t schGenTaskId(void) {
  return atomic_add_fetch_64(&schMgmt.taskId, 1);
}

uint64_t schGenUUID(void) {
  static uint64_t hashId = 0;
  static int32_t requestSerialId = 0;

  if (hashId == 0) {
    char    uid[64];
    int32_t code = taosGetSystemUUID(uid, tListLen(uid));
    if (code != TSDB_CODE_SUCCESS) {
      qError("Failed to get the system uid, reason:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    } else {
      hashId = MurmurHash3_32(uid, strlen(uid));
    }
  }

  int64_t ts      = taosGetTimestampMs();
  uint64_t pid    = taosGetPId();
  int32_t val     = atomic_add_fetch_32(&requestSerialId, 1);

  uint64_t id = ((hashId & 0x0FFF) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
  return id;
}

D
dapan1121 已提交
49 50 51 52 53

int32_t schInitTask(SSchJob* pJob, SSchTask *pTask, SSubplan* pPlan, SSchLevel *pLevel) {
  pTask->plan   = pPlan;
  pTask->level  = pLevel;
  SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_NOT_START);
D
dapan1121 已提交
54
  pTask->taskId = schGenTaskId();
55
  pTask->execAddrs = taosArrayInit(SCH_MAX_CANDIDATE_EP_NUM, sizeof(SQueryNodeAddr));
D
dapan1121 已提交
56
  if (NULL == pTask->execAddrs) {
57
    SCH_TASK_ELOG("taosArrayInit %d exec addrs failed", SCH_MAX_CANDIDATE_EP_NUM);
D
dapan1121 已提交
58 59 60 61 62 63 64 65 66 67 68
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  return TSDB_CODE_SUCCESS;
}

void schFreeTask(SSchTask* pTask) {
  if (pTask->candidateAddrs) {
    taosArrayDestroy(pTask->candidateAddrs);
  }

H
Haojun Liao 已提交
69
  tfree(pTask->msg);
D
dapan1121 已提交
70 71 72 73 74 75 76 77

  if (pTask->children) {
    taosArrayDestroy(pTask->children);
  }

  if (pTask->parents) {
    taosArrayDestroy(pTask->parents);
  }
H
Haojun Liao 已提交
78 79 80 81

  if (pTask->execAddrs) {
    taosArrayDestroy(pTask->execAddrs);
  }
D
dapan1121 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
}


int32_t schValidateTaskReceivedMsgType(SSchJob *pJob, SSchTask *pTask, int32_t msgType) {
  int32_t lastMsgType = atomic_load_32(&pTask->lastMsgType);
  
  switch (msgType) {
    case TDMT_VND_CREATE_TABLE_RSP:
    case TDMT_VND_SUBMIT_RSP:
    case TDMT_VND_QUERY_RSP:
    case TDMT_VND_RES_READY_RSP:
    case TDMT_VND_FETCH_RSP:
    case TDMT_VND_DROP_TASK:
      if (lastMsgType != (msgType - 1)) {
        SCH_TASK_ELOG("rsp msg type mis-match, last sent msgType:%d, rspType:%d", lastMsgType, msgType);
        SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
      }
D
dapan1121 已提交
99

D
dapan1121 已提交
100
      if (SCH_GET_TASK_STATUS(pTask) != JOB_TASK_STATUS_EXECUTING && SCH_GET_TASK_STATUS(pTask) != JOB_TASK_STATUS_PARTIAL_SUCCEED) {
D
dapan1121 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        SCH_TASK_ELOG("rsp msg conflicted with task status, status:%d, rspType:%d", SCH_GET_TASK_STATUS(pTask), msgType);
        SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
      }

      break;
    default:
      SCH_TASK_ELOG("unknown rsp msg, type:%d, status:%d", msgType, SCH_GET_TASK_STATUS(pTask));
      
      SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  return TSDB_CODE_SUCCESS;
}


int32_t schCheckAndUpdateJobStatus(SSchJob *pJob, int8_t newStatus) {
D
dapan 已提交
117 118
  int32_t code = 0;

D
dapan1121 已提交
119
  int8_t oriStatus = 0;
D
dapan1121 已提交
120

D
dapan1121 已提交
121 122 123 124 125 126 127 128 129 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
  while (true) {
    oriStatus = SCH_GET_JOB_STATUS(pJob);

    if (oriStatus == newStatus) {
      SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
    }
    
    switch (oriStatus) {
      case JOB_TASK_STATUS_NULL:
        if (newStatus != JOB_TASK_STATUS_NOT_START) {
          SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
        }
        
        break;
      case JOB_TASK_STATUS_NOT_START:
        if (newStatus != JOB_TASK_STATUS_EXECUTING) {
          SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
        }
        
        break;
      case JOB_TASK_STATUS_EXECUTING:
        if (newStatus != JOB_TASK_STATUS_PARTIAL_SUCCEED 
         && newStatus != JOB_TASK_STATUS_FAILED 
         && newStatus != JOB_TASK_STATUS_CANCELLING 
         && newStatus != JOB_TASK_STATUS_CANCELLED 
         && newStatus != JOB_TASK_STATUS_DROPPING) {
          SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
        }
        
        break;
      case JOB_TASK_STATUS_PARTIAL_SUCCEED:
        if (newStatus != JOB_TASK_STATUS_FAILED 
         && newStatus != JOB_TASK_STATUS_SUCCEED
         && newStatus != JOB_TASK_STATUS_DROPPING) {
          SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
        }
        
        break;
      case JOB_TASK_STATUS_SUCCEED:
      case JOB_TASK_STATUS_FAILED:
      case JOB_TASK_STATUS_CANCELLING:
        if (newStatus != JOB_TASK_STATUS_DROPPING) {
          SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
        }
        
        break;
      case JOB_TASK_STATUS_CANCELLED:
      case JOB_TASK_STATUS_DROPPING:
D
dapan1121 已提交
169
        SCH_ERR_JRET(TSDB_CODE_QRY_JOB_FREED);
D
dapan1121 已提交
170 171 172 173
        break;
        
      default:
        SCH_JOB_ELOG("invalid job status:%d", oriStatus);
D
dapan 已提交
174
        SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
175 176 177 178 179
    }

    if (oriStatus != atomic_val_compare_exchange_8(&pJob->status, oriStatus, newStatus)) {
      continue;
    }
D
dapan 已提交
180

D
dapan1121 已提交
181
    SCH_JOB_DLOG("job status updated from %d to %d", oriStatus, newStatus);
D
dapan1121 已提交
182

D
dapan1121 已提交
183 184
    break;
  }
D
dapan1121 已提交
185

D
dapan 已提交
186 187 188 189 190 191 192 193 194
  return TSDB_CODE_SUCCESS;

_return:

  SCH_JOB_ELOG("invalid job status update, from %d to %d", oriStatus, newStatus);
  SCH_ERR_RET(code);
}


D
dapan1121 已提交
195
int32_t schBuildTaskRalation(SSchJob *pJob, SHashObj *planToTask) {
D
dapan1121 已提交
196 197
  for (int32_t i = 0; i < pJob->levelNum; ++i) {
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);
D
dapan 已提交
198
    
D
dapan1121 已提交
199 200 201 202 203
    for (int32_t m = 0; m < pLevel->taskNum; ++m) {
      SSchTask *pTask = taosArrayGet(pLevel->subTasks, m);
      SSubplan *pPlan = pTask->plan;
      int32_t childNum = pPlan->pChildren ? (int32_t)taosArrayGetSize(pPlan->pChildren) : 0;
      int32_t parentNum = pPlan->pParents ? (int32_t)taosArrayGetSize(pPlan->pParents) : 0;
D
dapan1121 已提交
204 205

      if (childNum > 0) {
D
dapan1121 已提交
206 207 208 209 210
        if (pJob->levelIdx == pLevel->level) {
          SCH_JOB_ELOG("invalid query plan, lowest level, childNum:%d", childNum);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }
        
D
dapan1121 已提交
211 212 213
        pTask->children = taosArrayInit(childNum, POINTER_BYTES);
        if (NULL == pTask->children) {
          SCH_TASK_ELOG("taosArrayInit %d children failed", childNum);
D
dapan1121 已提交
214 215 216 217 218
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
      }

      for (int32_t n = 0; n < childNum; ++n) {
D
dapan1121 已提交
219
        SSubplan **child = taosArrayGet(pPlan->pChildren, n);
D
dapan 已提交
220
        SSchTask **childTask = taosHashGet(planToTask, child, POINTER_BYTES);
D
dapan 已提交
221
        if (NULL == childTask || NULL == *childTask) {
D
dapan1121 已提交
222
          SCH_TASK_ELOG("subplan children relationship error, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
223 224 225
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

D
dapan1121 已提交
226 227
        if (NULL == taosArrayPush(pTask->children, childTask)) {
          SCH_TASK_ELOG("taosArrayPush childTask failed, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
228 229 230 231 232
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
      }

      if (parentNum > 0) {
D
dapan1121 已提交
233 234 235 236 237
        if (0 == pLevel->level) {
          SCH_TASK_ELOG("invalid task info, level:0, parentNum:%d", parentNum);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }
        
D
dapan1121 已提交
238 239 240
        pTask->parents = taosArrayInit(parentNum, POINTER_BYTES);
        if (NULL == pTask->parents) {
          SCH_TASK_ELOG("taosArrayInit %d parents failed", parentNum);
D
dapan1121 已提交
241 242
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
D
dapan1121 已提交
243 244 245 246 247
      } else {
        if (0 != pLevel->level) {
          SCH_TASK_ELOG("invalid task info, level:%d, parentNum:%d", pLevel->level, parentNum);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }
D
dapan1121 已提交
248 249 250
      }

      for (int32_t n = 0; n < parentNum; ++n) {
D
dapan1121 已提交
251
        SSubplan **parent = taosArrayGet(pPlan->pParents, n);
D
dapan 已提交
252
        SSchTask **parentTask = taosHashGet(planToTask, parent, POINTER_BYTES);
D
dapan 已提交
253
        if (NULL == parentTask || NULL == *parentTask) {
D
dapan1121 已提交
254
          SCH_TASK_ELOG("subplan parent relationship error, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
255 256 257
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

D
dapan1121 已提交
258 259
        if (NULL == taosArrayPush(pTask->parents, parentTask)) {
          SCH_TASK_ELOG("taosArrayPush parentTask failed, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
260 261
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
D
dapan1121 已提交
262 263 264
      }  

      SCH_TASK_DLOG("level:%d, parentNum:%d, childNum:%d", i, parentNum, childNum);
D
dapan1121 已提交
265 266 267
    }
  }

D
dapan1121 已提交
268 269 270
  SSchLevel *pLevel = taosArrayGet(pJob->levels, 0);
  if (pJob->attr.queryJob && pLevel->taskNum > 1) {
    SCH_JOB_ELOG("invalid query plan, level:0, taskNum:%d", pLevel->taskNum);
D
dapan 已提交
271 272 273
    SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }

D
dapan1121 已提交
274 275 276
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
277 278 279 280 281 282 283

int32_t schRecordTaskSucceedNode(SSchTask *pTask) {
  SQueryNodeAddr *addr = taosArrayGet(pTask->candidateAddrs, atomic_load_8(&pTask->candidateIdx));

  assert(NULL != addr);

  pTask->succeedAddr = *addr;
284

D
dapan1121 已提交
285
  return TSDB_CODE_SUCCESS;
286 287
}

D
dapan1121 已提交
288 289 290 291 292 293 294 295

int32_t schRecordTaskExecNode(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr) {
  if (NULL == taosArrayPush(pTask->execAddrs, addr)) {
    SCH_TASK_ELOG("taosArrayPush addr to execAddr list failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  return TSDB_CODE_SUCCESS;
296
}
D
dapan1121 已提交
297

D
dapan1121 已提交
298

D
dapan1121 已提交
299
int32_t schValidateAndBuildJob(SQueryDag *pDag, SSchJob *pJob) {
D
dapan1121 已提交
300
  int32_t code = 0;
D
dapan1121 已提交
301
  pJob->queryId = pDag->queryId;
D
dapan1121 已提交
302
  
D
dapan1121 已提交
303 304
  if (pDag->numOfSubplans <= 0) {
    SCH_JOB_ELOG("invalid subplan num:%d", pDag->numOfSubplans);
D
dapan 已提交
305 306 307
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
  
D
dapan1121 已提交
308
  int32_t levelNum = (int32_t)taosArrayGetSize(pDag->pSubplans);
309
  if (levelNum <= 0) {
D
dapan1121 已提交
310
    SCH_JOB_ELOG("invalid level num:%d", levelNum);
D
dapan1121 已提交
311
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
312 313
  }

D
dapan1121 已提交
314 315
  SHashObj *planToTask = taosHashInit(SCHEDULE_DEFAULT_TASK_NUMBER, taosGetDefaultHashFunction(POINTER_BYTES == sizeof(int64_t) ? TSDB_DATA_TYPE_BIGINT : TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
  if (NULL == planToTask) {
D
dapan1121 已提交
316
    SCH_JOB_ELOG("taosHashInit %d failed", SCHEDULE_DEFAULT_TASK_NUMBER);
D
dapan1121 已提交
317 318
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
319

D
dapan1121 已提交
320 321
  pJob->levels = taosArrayInit(levelNum, sizeof(SSchLevel));
  if (NULL == pJob->levels) {
D
dapan1121 已提交
322
    SCH_JOB_ELOG("taosArrayInit %d failed", levelNum);
D
dapan1121 已提交
323
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
324 325
  }

D
dapan1121 已提交
326 327
  pJob->levelNum = levelNum;
  pJob->levelIdx = levelNum - 1;
328

D
dapan1121 已提交
329
  pJob->subPlans = pDag->pSubplans;
330

D
dapan 已提交
331
  SSchLevel level = {0};
D
dapan1121 已提交
332 333
  SArray *plans = NULL;
  int32_t taskNum = 0;
D
dapan 已提交
334
  SSchLevel *pLevel = NULL;
335

D
dapan1121 已提交
336
  level.status = JOB_TASK_STATUS_NOT_START;
D
dapan1121 已提交
337

338
  for (int32_t i = 0; i < levelNum; ++i) {
D
dapan1121 已提交
339
    if (NULL == taosArrayPush(pJob->levels, &level)) {
D
dapan1121 已提交
340
      SCH_JOB_ELOG("taosArrayPush level failed, level:%d", i);
D
dapan1121 已提交
341 342 343
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

D
dapan1121 已提交
344
    pLevel = taosArrayGet(pJob->levels, i);
D
dapan1121 已提交
345 346
  
    pLevel->level = i;
D
dapan1121 已提交
347
    
D
dapan1121 已提交
348
    plans = taosArrayGetP(pDag->pSubplans, i);
D
dapan1121 已提交
349 350
    if (NULL == plans) {
      SCH_JOB_ELOG("empty level plan, level:%d", i);
D
dapan1121 已提交
351
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
352 353
    }

D
dapan1121 已提交
354 355 356
    taskNum = (int32_t)taosArrayGetSize(plans);
    if (taskNum <= 0) {
      SCH_JOB_ELOG("invalid level plan number:%d, level:%d", taskNum, i);
D
dapan1121 已提交
357
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
358 359
    }

D
dapan1121 已提交
360
    pLevel->taskNum = taskNum;
D
dapan1121 已提交
361
    
D
dapan1121 已提交
362
    pLevel->subTasks = taosArrayInit(taskNum, sizeof(SSchTask));
D
dapan1121 已提交
363
    if (NULL == pLevel->subTasks) {
D
dapan1121 已提交
364
      SCH_JOB_ELOG("taosArrayInit %d failed", taskNum);
D
dapan1121 已提交
365
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
366 367
    }
    
D
dapan1121 已提交
368 369
    for (int32_t n = 0; n < taskNum; ++n) {
      SSubplan *plan = taosArrayGetP(plans, n);
D
dapan 已提交
370

D
dapan1121 已提交
371 372 373 374 375
      SCH_SET_JOB_TYPE(&pJob->attr, plan->type);

      SSchTask  task = {0};
      SSchTask *pTask = &task;
      
D
dapan1121 已提交
376
      SCH_ERR_JRET(schInitTask(pJob, &task, plan, pLevel));
D
dapan1121 已提交
377
      
D
dapan1121 已提交
378
      void *p = taosArrayPush(pLevel->subTasks, &task);
D
dapan1121 已提交
379
      if (NULL == p) {
D
dapan1121 已提交
380
        SCH_TASK_ELOG("taosArrayPush task to level failed, level:%d, taskIdx:%d", pLevel->level, n);
D
dapan1121 已提交
381 382 383 384
        SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
      
      if (0 != taosHashPut(planToTask, &plan, POINTER_BYTES, &p, POINTER_BYTES)) {
D
dapan1121 已提交
385
        SCH_TASK_ELOG("taosHashPut to planToTaks failed, taskIdx:%d", n);
D
dapan1121 已提交
386 387
        SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
388

D
dapan1121 已提交
389 390
      SCH_TASK_DLOG("task initialized, level:%d", pLevel->level);
    }
D
dapan1121 已提交
391

D
dapan1121 已提交
392
    SCH_JOB_DLOG("level initialized, taskNum:%d", taskNum);
D
dapan1121 已提交
393
  }
D
dapan1121 已提交
394 395

  SCH_ERR_JRET(schBuildTaskRalation(pJob, planToTask));
D
dapan1121 已提交
396 397

_return:
D
dapan1121 已提交
398 399 400 401
  if (planToTask) {
    taosHashCleanup(planToTask);
  }

D
dapan1121 已提交
402
  SCH_RET(code);
403 404
}

D
dapan1121 已提交
405 406
int32_t schSetTaskCandidateAddrs(SSchJob *pJob, SSchTask *pTask) {
  if (NULL != pTask->candidateAddrs) {
D
dapan 已提交
407 408 409
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
410
  pTask->candidateIdx = 0;
411
  pTask->candidateAddrs = taosArrayInit(SCH_MAX_CANDIDATE_EP_NUM, sizeof(SQueryNodeAddr));
D
dapan1121 已提交
412
  if (NULL == pTask->candidateAddrs) {
413
    SCH_TASK_ELOG("taosArrayInit %d condidate addrs failed", SCH_MAX_CANDIDATE_EP_NUM);
D
dapan1121 已提交
414 415 416
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
417 418 419
  if (pTask->plan->execNode.numOfEps > 0) {
    if (NULL == taosArrayPush(pTask->candidateAddrs, &pTask->plan->execNode)) {
      SCH_TASK_ELOG("taosArrayPush execNode to candidate addrs failed, errno:%d", errno);
D
dapan1121 已提交
420 421 422
      SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

D
dapan1121 已提交
423
    SCH_TASK_DLOG("use execNode from plan as candidate addr, numOfEps:%d", pTask->plan->execNode.numOfEps);
D
dapan1121 已提交
424

D
dapan1121 已提交
425 426 427 428
    return TSDB_CODE_SUCCESS;
  }

  int32_t addNum = 0;
D
dapan 已提交
429
  int32_t nodeNum = 0;
430
  if (pJob->nodeList) {
D
dapan 已提交
431
    nodeNum = taosArrayGetSize(pJob->nodeList);
D
dapan1121 已提交
432
    
433
    for (int32_t i = 0; i < nodeNum && addNum < SCH_MAX_CANDIDATE_EP_NUM; ++i) {
D
dapan 已提交
434 435
      SQueryNodeAddr *naddr = taosArrayGet(pJob->nodeList, i);
      
436
      if (NULL == taosArrayPush(pTask->candidateAddrs, naddr)) {
D
dapan 已提交
437 438 439
        SCH_TASK_ELOG("taosArrayPush execNode to candidate addrs failed, addNum:%d, errno:%d", addNum, errno);
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
D
dapan1121 已提交
440 441

      ++addNum;
D
dapan1121 已提交
442
    }
D
dapan1121 已提交
443 444
  }

D
dapan1121 已提交
445
  if (addNum <= 0) {
446
    SCH_TASK_ELOG("no available execNode as candidate addr, nodeNum:%d", nodeNum);
D
dapan1121 已提交
447 448 449
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
450
/*
451
  for (int32_t i = 0; i < job->dataSrcEps.numOfEps && addNum < SCH_MAX_CANDIDATE_EP_NUM; ++i) {
D
dapan1121 已提交
452 453 454 455
    strncpy(epSet->fqdn[epSet->numOfEps], job->dataSrcEps.fqdn[i], sizeof(job->dataSrcEps.fqdn[i]));
    epSet->port[epSet->numOfEps] = job->dataSrcEps.port[i];
    
    ++epSet->numOfEps;
D
dapan 已提交
456
  }
D
dapan1121 已提交
457
*/
D
dapan 已提交
458 459

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
460
}
D
dapan1121 已提交
461

D
dapan1121 已提交
462
int32_t schPushTaskToExecList(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
463 464 465
  int32_t code = taosHashPut(pJob->execTasks, &pTask->taskId, sizeof(pTask->taskId), &pTask, POINTER_BYTES);
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
466
      SCH_TASK_ELOG("task already in execTask list, code:%x", code);
D
dapan1121 已提交
467 468 469
      SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
    }
    
D
dapan1121 已提交
470
    SCH_TASK_ELOG("taosHashPut task to execTask list failed, errno:%d", errno);
D
dapan 已提交
471 472 473
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
474
  SCH_TASK_DLOG("task added to execTask list, numOfTasks:%d", taosHashGetSize(pJob->execTasks));
475

D
dapan 已提交
476 477 478
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
479 480 481
int32_t schMoveTaskToSuccList(SSchJob *pJob, SSchTask *pTask, bool *moved) {
  if (0 != taosHashRemove(pJob->execTasks, &pTask->taskId, sizeof(pTask->taskId))) {
    SCH_TASK_WLOG("remove task from execTask list failed, may not exist, status:%d", SCH_GET_TASK_STATUS(pTask));
D
dapan 已提交
482 483
  }

D
dapan1121 已提交
484 485 486 487 488 489 490 491 492 493
  int32_t code = taosHashPut(pJob->succTasks, &pTask->taskId, sizeof(pTask->taskId), &pTask, POINTER_BYTES);
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
      *moved = true;
      
      SCH_TASK_ELOG("task already in succTask list, status:%d", SCH_GET_TASK_STATUS(pTask));
      SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
    }
  
    SCH_TASK_ELOG("taosHashPut task to succTask list failed, errno:%d", errno);
D
dapan1121 已提交
494 495 496 497
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  *moved = true;
D
dapan1121 已提交
498 499

  SCH_TASK_DLOG("task moved to succTask list, numOfTasks:%d", taosHashGetSize(pJob->succTasks));
D
dapan1121 已提交
500 501 502 503
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
504 505 506 507 508
int32_t schMoveTaskToFailList(SSchJob *pJob, SSchTask *pTask, bool *moved) {
  *moved = false;
  
  if (0 != taosHashRemove(pJob->execTasks, &pTask->taskId, sizeof(pTask->taskId))) {
    SCH_TASK_WLOG("remove task from execTask list failed, may not exist, status:%d", SCH_GET_TASK_STATUS(pTask));
D
dapan1121 已提交
509 510
  }

D
dapan1121 已提交
511 512 513 514 515 516 517 518 519 520
  int32_t code = taosHashPut(pJob->failTasks, &pTask->taskId, sizeof(pTask->taskId), &pTask, POINTER_BYTES);
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
      *moved = true;
      
      SCH_TASK_WLOG("task already in failTask list, status:%d", SCH_GET_TASK_STATUS(pTask));
      SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
    }
    
    SCH_TASK_ELOG("taosHashPut task to failTask list failed, errno:%d", errno);
D
dapan 已提交
521 522 523 524
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  *moved = true;
D
dapan1121 已提交
525 526

  SCH_TASK_DLOG("task moved to failTask list, numOfTasks:%d", taosHashGetSize(pJob->failTasks));
D
dapan 已提交
527 528 529 530
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

int32_t schMoveTaskToExecList(SSchJob *pJob, SSchTask *pTask, bool *moved) {
  if (0 != taosHashRemove(pJob->succTasks, &pTask->taskId, sizeof(pTask->taskId))) {
    SCH_TASK_WLOG("remove task from succTask list failed, may not exist, status:%d", SCH_GET_TASK_STATUS(pTask));
  }

  int32_t code = taosHashPut(pJob->execTasks, &pTask->taskId, sizeof(pTask->taskId), &pTask, POINTER_BYTES);
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
      *moved = true;
      
      SCH_TASK_ELOG("task already in execTask list, status:%d", SCH_GET_TASK_STATUS(pTask));
      SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
    }
  
    SCH_TASK_ELOG("taosHashPut task to execTask list failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  *moved = true;

  SCH_TASK_DLOG("task moved to execTask list, numOfTasks:%d", taosHashGetSize(pJob->execTasks));
  
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
558
int32_t schTaskCheckAndSetRetry(SSchJob *job, SSchTask *task, int32_t errCode, bool *needRetry) {
D
dapan1121 已提交
559 560
  // TODO set retry or not based on task type/errCode/retry times/job status/available eps...
  // TODO if needRetry, set task retry info
D
dapan1121 已提交
561 562
  // TODO set condidateIdx
  // TODO record failed but tried task
D
dapan 已提交
563

D
dapan1121 已提交
564 565 566
  *needRetry = false;

  return TSDB_CODE_SUCCESS;
D
dapan 已提交
567 568
}

D
dapan1121 已提交
569
int32_t schProcessOnJobFailureImpl(SSchJob *pJob, int32_t status, int32_t errCode) {
D
dapan1121 已提交
570
  // if already FAILED, no more processing
D
dapan1121 已提交
571
  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, status));
D
dapan1121 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  
  if (errCode) {
    atomic_store_32(&pJob->errCode, errCode);
  }

  if (atomic_load_8(&pJob->userFetch) || ((!SCH_JOB_NEED_FETCH(&pJob->attr)) && pJob->attr.syncSchedule)) {
    tsem_post(&pJob->rspSem);
  }

  SCH_ERR_RET(atomic_load_32(&pJob->errCode));

  assert(0);
}



D
dapan1121 已提交
588 589 590 591 592 593 594 595 596 597 598
// Note: no more error processing, handled in function internal
int32_t schProcessOnJobFailure(SSchJob *pJob, int32_t errCode) {
  SCH_RET(schProcessOnJobFailureImpl(pJob, JOB_TASK_STATUS_FAILED, errCode));
}

// Note: no more error processing, handled in function internal
int32_t schProcessOnJobDropped(SSchJob *pJob, int32_t errCode) {
  SCH_RET(schProcessOnJobFailureImpl(pJob, JOB_TASK_STATUS_DROPPING, errCode));
}


D
dapan1121 已提交
599
// Note: no more error processing, handled in function internal
D
dapan1121 已提交
600
int32_t schFetchFromRemote(SSchJob *pJob) {
D
dapan 已提交
601 602
  int32_t code = 0;
  
D
dapan1121 已提交
603 604
  if (atomic_val_compare_exchange_32(&pJob->remoteFetch, 0, 1) != 0) {
    SCH_JOB_ELOG("prior fetching not finished, remoteFetch:%d", atomic_load_32(&pJob->remoteFetch));
D
dapan 已提交
605 606 607
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
608 609 610 611 612 613 614
  void *res = atomic_load_ptr(&pJob->res);
  if (res) {
    atomic_val_compare_exchange_32(&pJob->remoteFetch, 1, 0);

    SCH_JOB_DLOG("res already fetched, res:%p", res);
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
615 616

  SCH_ERR_JRET(schBuildAndSendMsg(pJob, pJob->fetchTask, &pJob->resNode, TDMT_VND_FETCH));
D
dapan 已提交
617 618 619 620

  return TSDB_CODE_SUCCESS;
  
_return:
D
dapan1121 已提交
621 622

  atomic_val_compare_exchange_32(&pJob->remoteFetch, 1, 0);
D
dapan 已提交
623

D
dapan1121 已提交
624 625
  schProcessOnJobFailure(pJob, code);

D
dapan 已提交
626
  return code;
D
dapan 已提交
627 628
}

D
dapan 已提交
629

D
dapan1121 已提交
630 631 632
// Note: no more error processing, handled in function internal
int32_t schProcessOnJobPartialSuccess(SSchJob *pJob) {
  int32_t code = 0;
D
dapan 已提交
633
  
D
dapan1121 已提交
634
  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_PARTIAL_SUCCEED));
D
dapan 已提交
635

D
dapan1121 已提交
636
  if ((!SCH_JOB_NEED_FETCH(&pJob->attr)) && pJob->attr.syncSchedule) {
D
dapan 已提交
637
    tsem_post(&pJob->rspSem);
D
dapan 已提交
638
  }
D
dapan1121 已提交
639 640 641 642
  
  if (atomic_load_8(&pJob->userFetch)) {
    SCH_ERR_JRET(schFetchFromRemote(pJob));
  }
D
dapan 已提交
643

D
dapan 已提交
644
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
645 646 647 648 649 650

_return:

  SCH_ERR_RET(schProcessOnJobFailure(pJob, code));

  SCH_RET(code);
D
dapan 已提交
651 652
}

D
dapan1121 已提交
653

D
dapan1121 已提交
654
int32_t schProcessOnDataFetched(SSchJob *job) {
D
dapan 已提交
655 656 657
  atomic_val_compare_exchange_32(&job->remoteFetch, 1, 0);

  tsem_post(&job->rspSem);
D
dapan 已提交
658 659 660
}


D
dapan1121 已提交
661 662 663 664

// Note: no more error processing, handled in function internal
int32_t schProcessOnTaskFailure(SSchJob *pJob, SSchTask *pTask, int32_t errCode) {
  bool needRetry = false;
D
dapan 已提交
665
  bool moved = false;
D
dapan1121 已提交
666
  int32_t taskDone = 0;
D
dapan1121 已提交
667
  int32_t code = 0;
D
dapan1121 已提交
668 669

  SCH_TASK_DLOG("taskOnFailure, code:%x", errCode);
D
dapan 已提交
670
  
D
dapan1121 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
  SCH_ERR_JRET(schTaskCheckAndSetRetry(pJob, pTask, errCode, &needRetry));
  
  if (!needRetry) {
    SCH_TASK_ELOG("task failed and no more retry, code:%x", errCode);

    if (SCH_GET_TASK_STATUS(pTask) == JOB_TASK_STATUS_EXECUTING) {
      code = schMoveTaskToFailList(pJob, pTask, &moved);
      if (code && moved) {
        SCH_ERR_RET(errCode);
      }
    }

    SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_FAILED);
    
    if (SCH_TASK_NEED_WAIT_ALL(pTask)) {
      SCH_LOCK(SCH_WRITE, &pTask->level->lock);
      pTask->level->taskFailed++;
      taskDone = pTask->level->taskSucceed + pTask->level->taskFailed;
      SCH_UNLOCK(SCH_WRITE, &pTask->level->lock);

      atomic_store_32(&pJob->errCode, errCode);
      
      if (taskDone < pTask->level->taskNum) {
        SCH_TASK_DLOG("not all tasks done, done:%d, all:%d", taskDone, pTask->level->taskNum);
        SCH_ERR_RET(errCode);
      }
    }
  } else {
    // Note: no more error processing, already handled
    SCH_ERR_RET(schLaunchTask(pJob, pTask));
    
D
dapan 已提交
702 703
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
704

D
dapan1121 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
_return:

  SCH_ERR_RET(schProcessOnJobFailure(pJob, errCode));

  SCH_ERR_RET(errCode);
}


// Note: no more error processing, handled in function internal
int32_t schProcessOnTaskSuccess(SSchJob *pJob, SSchTask *pTask) {
  bool moved = false;
  int32_t code = 0;
  SSchTask *pErrTask = pTask;

  SCH_TASK_DLOG("taskOnSuccess, status:%d", SCH_GET_TASK_STATUS(pTask));
  
  code = schMoveTaskToSuccList(pJob, pTask, &moved);
  if (code && moved) {
    SCH_ERR_RET(code);
  }

D
dapan1121 已提交
726
  SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_PARTIAL_SUCCEED);
D
dapan1121 已提交
727 728

  SCH_ERR_JRET(schRecordTaskSucceedNode(pTask));
D
dapan 已提交
729
  
D
dapan1121 已提交
730
  int32_t parentNum = pTask->parents ? (int32_t)taosArrayGetSize(pTask->parents) : 0;
D
dapan 已提交
731
  if (parentNum == 0) {
D
dapan1121 已提交
732 733
    int32_t taskDone = 0;
    
D
dapan1121 已提交
734 735 736 737 738
    if (SCH_TASK_NEED_WAIT_ALL(pTask)) {
      SCH_LOCK(SCH_WRITE, &pTask->level->lock);
      pTask->level->taskSucceed++;
      taskDone = pTask->level->taskSucceed + pTask->level->taskFailed;
      SCH_UNLOCK(SCH_WRITE, &pTask->level->lock);
D
dapan1121 已提交
739
      
D
dapan1121 已提交
740 741
      if (taskDone < pTask->level->taskNum) {
        SCH_TASK_ELOG("wait all tasks, done:%d, all:%d", taskDone, pTask->level->taskNum);
D
dapan1121 已提交
742
        
D
dapan1121 已提交
743
        return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
744 745
      } else if (taskDone > pTask->level->taskNum) {
        assert(0);
D
dapan1121 已提交
746 747
      }

D
dapan1121 已提交
748
      if (pTask->level->taskFailed > 0) {
D
dapan1121 已提交
749 750 751
        SCH_RET(schProcessOnJobFailure(pJob, 0));
      } else {
        SCH_RET(schProcessOnJobPartialSuccess(pJob));
D
dapan1121 已提交
752 753
      }
    } else {
D
dapan1121 已提交
754
      pJob->resNode = pTask->succeedAddr;
D
dapan1121 已提交
755
    }
D
dapan 已提交
756

D
dapan1121 已提交
757
    pJob->fetchTask = pTask;
D
dapan1121 已提交
758 759 760 761 762

    code = schMoveTaskToExecList(pJob, pTask, &moved);
    if (code && moved) {
      SCH_ERR_RET(code);
    }
D
dapan1121 已提交
763
    
D
dapan1121 已提交
764
    SCH_ERR_RET(schProcessOnJobPartialSuccess(pJob));
D
dapan 已提交
765 766 767 768

    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
769
/*
770
  if (SCH_IS_DATA_SRC_TASK(task) && job->dataSrcEps.numOfEps < SCH_MAX_CANDIDATE_EP_NUM) {
D
dapan 已提交
771 772 773 774 775
    strncpy(job->dataSrcEps.fqdn[job->dataSrcEps.numOfEps], task->execAddr.fqdn, sizeof(task->execAddr.fqdn));
    job->dataSrcEps.port[job->dataSrcEps.numOfEps] = task->execAddr.port;

    ++job->dataSrcEps.numOfEps;
  }
D
dapan1121 已提交
776
*/
D
dapan 已提交
777

D
dapan 已提交
778
  for (int32_t i = 0; i < parentNum; ++i) {
D
dapan1121 已提交
779
    SSchTask *par = *(SSchTask **)taosArrayGet(pTask->parents, i);
D
dapan1121 已提交
780 781
    pErrTask = par;
    
D
dapan1121 已提交
782
    atomic_add_fetch_32(&par->childReady, 1);
D
dapan 已提交
783

D
dapan1121 已提交
784
    SCH_LOCK(SCH_WRITE, &par->lock);
H
Haojun Liao 已提交
785 786
    SDownstreamSource source = {.taskId = pTask->taskId, .schedId = schMgmt.sId, .addr = pTask->succeedAddr};
    qSetSubplanExecutionNode(par->plan, pTask->plan->id.templateId, &source);
D
dapan1121 已提交
787
    SCH_UNLOCK(SCH_WRITE, &par->lock);
D
dapan 已提交
788 789
    
    if (SCH_TASK_READY_TO_LUNCH(par)) {
D
dapan1121 已提交
790
      SCH_ERR_RET(schLaunchTask(pJob, par));
D
dapan 已提交
791 792 793 794 795
    }
  }

  return TSDB_CODE_SUCCESS;

D
dapan1121 已提交
796
_return:
D
dapan 已提交
797

D
dapan1121 已提交
798
  SCH_ERR_RET(schProcessOnTaskFailure(pJob, pErrTask, code));
D
dapan 已提交
799

D
dapan1121 已提交
800
  SCH_ERR_RET(code);
D
dapan 已提交
801 802
}

D
dapan1121 已提交
803
int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, char *msg, int32_t msgSize, int32_t rspCode) {
D
dapan1121 已提交
804
  int32_t code = 0;
H
Haojun Liao 已提交
805

D
dapan1121 已提交
806 807
  SCH_ERR_JRET(schValidateTaskReceivedMsgType(pJob, pTask, msgType));

D
dapan1121 已提交
808
  switch (msgType) {
H
Haojun Liao 已提交
809
    case TDMT_VND_CREATE_TABLE_RSP: {
D
dapan1121 已提交
810 811
        if (rspCode != TSDB_CODE_SUCCESS) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
H
Haojun Liao 已提交
812
        }
D
dapan1121 已提交
813 814
        
        SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
815

D
dapan1121 已提交
816 817
        break;
      }
D
dapan1121 已提交
818
    case TDMT_VND_SUBMIT_RSP: {
D
dapan1121 已提交
819 820 821 822 823 824 825 826 827 828 829
        #if 0 //TODO OPEN THIS
        SShellSubmitRspMsg *rsp = (SShellSubmitRspMsg *)msg;

        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg || rsp->code != TSDB_CODE_SUCCESS) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
        }

        pJob->resNumOfRows += rsp->affectedRows;
        #else
        if (rspCode != TSDB_CODE_SUCCESS) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
D
dapan1121 已提交
830
        }
D
dapan1121 已提交
831 832 833 834 835

        SShellSubmitRsp *rsp = (SShellSubmitRsp *)msg;
        if (rsp) {
          pJob->resNumOfRows += rsp->affectedRows;
        }
D
dapan1121 已提交
836 837 838 839
        #endif

        SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));

D
dapan1121 已提交
840
        break;
D
dapan1121 已提交
841
      }
D
dapan1121 已提交
842
    case TDMT_VND_QUERY_RSP: {
D
dapan1121 已提交
843 844
        SQueryTableRsp *rsp = (SQueryTableRsp *)msg;
        
D
dapan1121 已提交
845
        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg || rsp->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
846
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
D
dapan1121 已提交
847
        }
D
dapan1121 已提交
848 849 850
        
        SCH_ERR_JRET(schBuildAndSendMsg(pJob, pTask, NULL, TDMT_VND_RES_READY));
        
D
dapan1121 已提交
851 852
        break;
      }
D
dapan1121 已提交
853
    case TDMT_VND_RES_READY_RSP: {
D
dapan1121 已提交
854 855
        SResReadyRsp *rsp = (SResReadyRsp *)msg;
        
D
dapan1121 已提交
856
        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg || rsp->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
857
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
D
dapan1121 已提交
858
        }
D
dapan1121 已提交
859 860 861
        
        SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));
        
D
dapan1121 已提交
862 863
        break;
      }
D
dapan1121 已提交
864
    case TDMT_VND_FETCH_RSP: {
D
dapan1121 已提交
865 866
        SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg;

D
dapan1121 已提交
867 868
        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
D
dapan1121 已提交
869
        }
D
dapan1121 已提交
870

D
dapan1121 已提交
871 872 873 874 875 876
        if (pJob->res) {
          SCH_TASK_ELOG("got fetch rsp while res already exists, res:%p", pJob->res);
          tfree(rsp);
          SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
        }

D
dapan1121 已提交
877 878
        atomic_store_ptr(&pJob->res, rsp);
        atomic_store_32(&pJob->resNumOfRows, rsp->numOfRows);
D
dapan1121 已提交
879 880 881 882

        if (rsp->completed) {
          SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_SUCCEED);
        }
H
Haojun Liao 已提交
883

D
dapan1121 已提交
884
        SCH_ERR_JRET(schProcessOnDataFetched(pJob));
D
dapan1121 已提交
885 886
        
        break;
D
dapan1121 已提交
887
      }
D
dapan1121 已提交
888
    case TDMT_VND_DROP_TASK: {
D
dapan1121 已提交
889 890 891
        // SHOULD NEVER REACH HERE
        assert(0);
        break;
D
dapan1121 已提交
892
      }
D
dapan1121 已提交
893
    default:
D
dapan1121 已提交
894 895 896
      SCH_TASK_ELOG("unknown rsp msg, type:%d, status:%d", msgType, SCH_GET_TASK_STATUS(pTask));
      
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
897 898 899 900 901
  }

  return TSDB_CODE_SUCCESS;

_return:
D
dapan1121 已提交
902 903 904 905

  SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, code));
  
  SCH_RET(code);
D
dapan1121 已提交
906 907
}

D
dapan 已提交
908

D
dapan1121 已提交
909
int32_t schHandleCallback(void* param, const SDataBuf* pMsg, int32_t msgType, int32_t rspCode) {
D
dapan1121 已提交
910 911
  int32_t code = 0;
  SSchCallbackParam *pParam = (SSchCallbackParam *)param;
D
dapan1121 已提交
912 913
  SSchJob *pJob = NULL;
  SSchTask *pTask = NULL;
D
dapan1121 已提交
914
  
D
dapan1121 已提交
915 916
  SSchJob **job = taosHashGet(schMgmt.jobs, &pParam->queryId, sizeof(pParam->queryId));
  if (NULL == job || NULL == (*job)) {
D
dapan1121 已提交
917
    qError("QID:%"PRIx64" taosHashGet queryId not exist, may be dropped", pParam->queryId);
D
dapan1121 已提交
918
    SCH_ERR_JRET(TSDB_CODE_QRY_JOB_FREED);
D
dapan1121 已提交
919 920
  }

D
dapan1121 已提交
921 922 923 924 925
  pJob = *job;

  atomic_add_fetch_32(&pJob->ref, 1);

  int32_t s = taosHashGetSize(pJob->execTasks);
D
dapan1121 已提交
926
  if (s <= 0) {
D
dapan1121 已提交
927
    qError("QID:%"PRIx64",TID:%"PRId64" no task in execTask list", pParam->queryId, pParam->taskId);
D
dapan1121 已提交
928 929
    SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }
H
Haojun Liao 已提交
930

D
dapan1121 已提交
931
  SSchTask **task = taosHashGet(pJob->execTasks, &pParam->taskId, sizeof(pParam->taskId));
D
dapan1121 已提交
932
  if (NULL == task || NULL == (*task)) {
D
dapan1121 已提交
933
    qError("QID:%"PRIx64",TID:%"PRId64" taosHashGet taskId not exist", pParam->queryId, pParam->taskId);
D
dapan1121 已提交
934 935
    SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }
D
dapan1121 已提交
936 937 938 939

  pTask = *task;

  SCH_TASK_DLOG("rsp msg received, type:%d, code:%x", msgType, rspCode);
D
dapan1121 已提交
940
  
D
dapan1121 已提交
941
  SCH_ERR_JRET(schHandleResponseMsg(pJob, pTask, msgType, pMsg->pData, pMsg->len, rspCode));
D
dapan1121 已提交
942

H
Haojun Liao 已提交
943
_return:
D
dapan1121 已提交
944 945 946 947 948

  if (pJob) {
    atomic_sub_fetch_32(&pJob->ref, 1);
  }

D
dapan1121 已提交
949 950 951 952
  tfree(param);
  SCH_RET(code);
}

D
dapan1121 已提交
953
int32_t schHandleSubmitCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
954 955
  return schHandleCallback(param, pMsg, TDMT_VND_SUBMIT_RSP, code);
}
H
Haojun Liao 已提交
956

D
dapan1121 已提交
957
int32_t schHandleCreateTableCallback(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
958 959 960
  return schHandleCallback(param, pMsg, TDMT_VND_CREATE_TABLE_RSP, code);
}

D
dapan1121 已提交
961
int32_t schHandleQueryCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
962 963
  return schHandleCallback(param, pMsg, TDMT_VND_QUERY_RSP, code);
}
H
Haojun Liao 已提交
964

D
dapan1121 已提交
965
int32_t schHandleFetchCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
966 967
  return schHandleCallback(param, pMsg, TDMT_VND_FETCH_RSP, code);
}
H
Haojun Liao 已提交
968

D
dapan1121 已提交
969
int32_t schHandleReadyCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
970 971
  return schHandleCallback(param, pMsg, TDMT_VND_RES_READY_RSP, code);
}
H
Haojun Liao 已提交
972

D
dapan1121 已提交
973
int32_t schHandleDropCallback(void* param, const SDataBuf* pMsg, int32_t code) {  
D
dapan1121 已提交
974
  SSchCallbackParam *pParam = (SSchCallbackParam *)param;
D
dapan1121 已提交
975
  qDebug("QID:%"PRIx64",TID:%"PRIx64" drop task rsp received, code:%x", pParam->queryId, pParam->taskId, code);
D
dapan1121 已提交
976 977
}

D
dapan1121 已提交
978
int32_t schGetCallbackFp(int32_t msgType, __async_send_cb_fn_t *fp) {
D
dapan1121 已提交
979
  switch (msgType) {
H
Haojun Liao 已提交
980 981 982
    case TDMT_VND_CREATE_TABLE:
      *fp = schHandleCreateTableCallback;
      break;
D
dapan1121 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
    case TDMT_VND_SUBMIT: 
      *fp = schHandleSubmitCallback;
      break;
    case TDMT_VND_QUERY: 
      *fp = schHandleQueryCallback;
      break;
    case TDMT_VND_RES_READY: 
      *fp = schHandleReadyCallback;
      break;
    case TDMT_VND_FETCH: 
      *fp = schHandleFetchCallback;
      break;
    case TDMT_VND_DROP_TASK:
      *fp = schHandleDropCallback;
      break;
    default:
D
dapan1121 已提交
999
      qError("unknown msg type for callback, msgType:%d", msgType);
D
dapan1121 已提交
1000 1001 1002 1003 1004 1005 1006
      SCH_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1007
int32_t schAsyncSendMsg(void *transport, SEpSet* epSet, uint64_t qId, uint64_t tId, int32_t msgType, void *msg, uint32_t msgSize) {
D
dapan1121 已提交
1008 1009 1010
  int32_t code = 0;
  SMsgSendInfo* pMsgSendInfo = calloc(1, sizeof(SMsgSendInfo));
  if (NULL == pMsgSendInfo) {
D
dapan 已提交
1011
    qError("QID:%"PRIx64 ",TID:%"PRIx64 " calloc %d failed", qId, tId, (int32_t)sizeof(SMsgSendInfo));
D
dapan1121 已提交
1012 1013 1014 1015 1016
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  SSchCallbackParam *param = calloc(1, sizeof(SSchCallbackParam));
  if (NULL == param) {
D
dapan 已提交
1017
    qError("QID:%"PRIx64 ",TID:%"PRIx64 " calloc %d failed", qId, tId, (int32_t)sizeof(SSchCallbackParam));
D
dapan1121 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  __async_send_cb_fn_t fp = NULL;
  SCH_ERR_JRET(schGetCallbackFp(msgType, &fp));

  param->queryId = qId;
  param->taskId = tId;

  pMsgSendInfo->param = param;
  pMsgSendInfo->msgInfo.pData = msg;
  pMsgSendInfo->msgInfo.len = msgSize;
  pMsgSendInfo->msgType = msgType;
  pMsgSendInfo->fp = fp;
  
  int64_t  transporterId = 0;
D
dapan 已提交
1034
  
D
dapan1121 已提交
1035 1036 1037 1038 1039
  code = asyncSendMsgToServer(transport, epSet, &transporterId, pMsgSendInfo);
  if (code) {
    qError("QID:%"PRIx64 ",TID:%"PRIx64 " asyncSendMsgToServer failed, code:%x", qId, tId, code);
    SCH_ERR_JRET(code);
  }
D
dapan1121 已提交
1040 1041 1042 1043
  
  return TSDB_CODE_SUCCESS;

_return:
D
dapan 已提交
1044
  
D
dapan1121 已提交
1045 1046 1047 1048 1049 1050
  tfree(param);
  tfree(pMsgSendInfo);

  SCH_RET(code);
}

D
dapan1121 已提交
1051
void schConvertAddrToEpSet(SQueryNodeAddr *addr, SEpSet *epSet) {
D
dapan1121 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060
  epSet->inUse = addr->inUse;
  epSet->numOfEps = addr->numOfEps;
  
  for (int8_t i = 0; i < epSet->numOfEps; ++i) {
    strncpy(epSet->fqdn[i], addr->epAddr[i].fqdn, sizeof(addr->epAddr[i].fqdn));
    epSet->port[i] = addr->epAddr[i].port;
  }
}

D
dapan1121 已提交
1061
int32_t schBuildAndSendMsg(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr, int32_t msgType) {
D
dapan1121 已提交
1062 1063 1064
  uint32_t msgSize = 0;
  void *msg = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1065
  bool isCandidateAddr = false;
D
dapan 已提交
1066
  SEpSet epSet;
D
dapan1121 已提交
1067 1068 1069 1070 1071 1072 1073

  if (NULL == addr) {
    addr = taosArrayGet(pTask->candidateAddrs, atomic_load_8(&pTask->candidateIdx));
    
    isCandidateAddr = true;
  }

D
dapan 已提交
1074
  schConvertAddrToEpSet(addr, &epSet);
D
dapan1121 已提交
1075 1076
  
  switch (msgType) {
H
Haojun Liao 已提交
1077
    case TDMT_VND_CREATE_TABLE:
D
dapan1121 已提交
1078
    case TDMT_VND_SUBMIT: {
D
dapan1121 已提交
1079
      msgSize = pTask->msgLen;
D
dapan1121 已提交
1080 1081 1082 1083 1084 1085 1086
      msg = calloc(1, msgSize);
      if (NULL == msg) {
        SCH_TASK_ELOG("calloc %d failed", msgSize);
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }

      memcpy(msg, pTask->msg, msgSize);
D
dapan1121 已提交
1087 1088
      break;
    }
1089

D
dapan1121 已提交
1090
    case TDMT_VND_QUERY: {
D
dapan1121 已提交
1091
      msgSize = sizeof(SSubQueryMsg) + pTask->msgLen;
D
dapan1121 已提交
1092 1093
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1094
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1095 1096 1097 1098
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }

      SSubQueryMsg *pMsg = msg;
D
dapan1121 已提交
1099

D
dapan 已提交
1100 1101
      pMsg->header.vgId = htonl(addr->nodeId);
      
D
dapan1121 已提交
1102
      pMsg->sId = htobe64(schMgmt.sId);
D
dapan1121 已提交
1103 1104
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);
D
dapan1121 已提交
1105
      pMsg->taskType = TASK_TYPE_TEMP;      
D
dapan1121 已提交
1106 1107
      pMsg->contentLen = htonl(pTask->msgLen);
      memcpy(pMsg->msg, pTask->msg, pTask->msgLen);
D
dapan1121 已提交
1108
      break;
1109 1110
    }

D
dapan1121 已提交
1111
    case TDMT_VND_RES_READY: {
S
Shengliang Guan 已提交
1112
      msgSize = sizeof(SResReadyReq);
D
dapan1121 已提交
1113 1114
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1115
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1116 1117 1118
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }

S
Shengliang Guan 已提交
1119
      SResReadyReq *pMsg = msg;
D
dapan1121 已提交
1120
      
D
dapan 已提交
1121 1122
      pMsg->header.vgId = htonl(addr->nodeId);  
      
D
dapan1121 已提交
1123
      pMsg->sId = htobe64(schMgmt.sId);      
D
dapan1121 已提交
1124 1125
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);      
D
dapan1121 已提交
1126 1127 1128
      break;
    }
    case TDMT_VND_FETCH: {
S
Shengliang Guan 已提交
1129
      msgSize = sizeof(SResFetchReq);
D
dapan1121 已提交
1130 1131
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1132
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1133 1134 1135
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
    
S
Shengliang Guan 已提交
1136
      SResFetchReq *pMsg = msg;
D
dapan1121 已提交
1137
      
D
dapan 已提交
1138 1139
      pMsg->header.vgId = htonl(addr->nodeId);  
      
D
dapan1121 已提交
1140
      pMsg->sId = htobe64(schMgmt.sId);      
D
dapan1121 已提交
1141 1142
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);      
D
dapan1121 已提交
1143 1144 1145
      break;
    }
    case TDMT_VND_DROP_TASK:{
S
Shengliang Guan 已提交
1146
      msgSize = sizeof(STaskDropReq);
D
dapan1121 已提交
1147 1148
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1149
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1150 1151 1152
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
    
S
Shengliang Guan 已提交
1153
      STaskDropReq *pMsg = msg;
D
dapan1121 已提交
1154
      
D
dapan 已提交
1155 1156
      pMsg->header.vgId = htonl(addr->nodeId);   
      
D
dapan1121 已提交
1157
      pMsg->sId = htobe64(schMgmt.sId);      
D
dapan1121 已提交
1158 1159
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);      
D
dapan1121 已提交
1160 1161 1162
      break;
    }
    default:
D
dapan1121 已提交
1163
      SCH_TASK_ELOG("unknown msg type to send, msgType:%d", msgType);
D
dapan1121 已提交
1164 1165 1166 1167
      SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
      break;
  }

D
dapan1121 已提交
1168
  atomic_store_32(&pTask->lastMsgType, msgType);
D
dapan1121 已提交
1169

D
dapan1121 已提交
1170
  SCH_ERR_JRET(schAsyncSendMsg(pJob->transport, &epSet, pJob->queryId, pTask->taskId, msgType, msg, msgSize));
D
dapan1121 已提交
1171

D
dapan1121 已提交
1172 1173 1174 1175
  if (isCandidateAddr) {
    SCH_ERR_RET(schRecordTaskExecNode(pJob, pTask, addr));
  }
  
D
dapan1121 已提交
1176 1177 1178 1179
  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
1180 1181
  atomic_store_32(&pTask->lastMsgType, -1);

D
dapan1121 已提交
1182 1183 1184
  tfree(msg);
  SCH_RET(code);
}
D
dapan1121 已提交
1185

D
dapan1121 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194
static FORCE_INLINE bool schJobNeedToStop(SSchJob *pJob, int8_t *pStatus) {
  int8_t status = SCH_GET_JOB_STATUS(pJob);
  if (pStatus) {
    *pStatus = status;
  }

  return (status == JOB_TASK_STATUS_FAILED || status == JOB_TASK_STATUS_CANCELLED 
       || status == JOB_TASK_STATUS_CANCELLING || status == JOB_TASK_STATUS_DROPPING);
}
D
dapan1121 已提交
1195

D
dapan1121 已提交
1196 1197

// Note: no more error processing, handled in function internal
D
dapan1121 已提交
1198
int32_t schLaunchTask(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
1199 1200 1201 1202 1203
  int8_t status = 0;
  int32_t code = 0;
  
  if (schJobNeedToStop(pJob, &status)) {
    SCH_TASK_ELOG("no need to launch task cause of job status, job status:%d", status);
D
dapan1121 已提交
1204 1205 1206 1207 1208
    
    code = atomic_load_32(&pJob->errCode);
    SCH_ERR_RET(code);
    
    SCH_RET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
1209
  }
D
dapan1121 已提交
1210
  
D
dapan1121 已提交
1211
  SSubplan *plan = pTask->plan;
D
dapan1121 已提交
1212

D
dapan1121 已提交
1213 1214 1215 1216 1217 1218
  if (NULL == pTask->msg) {
    code = qSubPlanToString(plan, &pTask->msg, &pTask->msgLen);
    if (TSDB_CODE_SUCCESS != code || NULL == pTask->msg || pTask->msgLen <= 0) {
      SCH_TASK_ELOG("subplanToString error, code:%x, msg:%p, len:%d", code, pTask->msg, pTask->msgLen);
      SCH_ERR_JRET(code);
    }
1219 1220

    printf("physical plan:%s\n", pTask->msg);
D
dapan1121 已提交
1221
  }
D
dapan1121 已提交
1222 1223
  
  SCH_ERR_JRET(schSetTaskCandidateAddrs(pJob, pTask));
D
dapan1121 已提交
1224

H
Haojun Liao 已提交
1225
  // NOTE: race condition: the task should be put into the hash table before send msg to server
D
dapan 已提交
1226 1227
  if (SCH_GET_TASK_STATUS(pTask) != JOB_TASK_STATUS_EXECUTING) {
    SCH_ERR_JRET(schPushTaskToExecList(pJob, pTask));
D
dapan1121 已提交
1228

D
dapan 已提交
1229 1230
    SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_EXECUTING);
  }
D
dapan1121 已提交
1231

D
dapan1121 已提交
1232
  SCH_ERR_JRET(schBuildAndSendMsg(pJob, pTask, NULL, plan->msgType));
D
dapan1121 已提交
1233
  
D
dapan1121 已提交
1234
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1235 1236 1237

_return:

D
dapan1121 已提交
1238
  SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, code));
D
dapan1121 已提交
1239 1240
  
  SCH_RET(code);
D
dapan1121 已提交
1241 1242
}

D
dapan1121 已提交
1243 1244
int32_t schLaunchJob(SSchJob *pJob) {
  SSchLevel *level = taosArrayGet(pJob->levels, pJob->levelIdx);
D
dapan1121 已提交
1245 1246

  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_EXECUTING));
D
dapan1121 已提交
1247
  
D
dapan 已提交
1248
  for (int32_t i = 0; i < level->taskNum; ++i) {
D
dapan1121 已提交
1249 1250
    SSchTask *pTask = taosArrayGet(level->subTasks, i);
    SCH_ERR_RET(schLaunchTask(pJob, pTask));
D
dapan1121 已提交
1251
  }
D
dapan 已提交
1252
  
D
dapan1121 已提交
1253
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1254 1255
}

D
dapan1121 已提交
1256 1257 1258 1259 1260
void schDropTaskOnExecutedNode(SSchJob *pJob, SSchTask *pTask) {
  if (NULL == pTask->execAddrs) {
    SCH_TASK_DLOG("no exec address, status:%d", SCH_GET_TASK_STATUS(pTask));
    return;
  }
H
Haojun Liao 已提交
1261

D
dapan1121 已提交
1262 1263 1264
  int32_t size = (int32_t)taosArrayGetSize(pTask->execAddrs);
  
  if (size <= 0) {
D
dapan1121 已提交
1265
    SCH_TASK_DLOG("task has no exec address, no need to drop it, status:%d", SCH_GET_TASK_STATUS(pTask));
D
dapan1121 已提交
1266 1267
    return;
  }
H
Haojun Liao 已提交
1268

D
dapan1121 已提交
1269 1270 1271
  SQueryNodeAddr *addr = NULL;
  for (int32_t i = 0; i < size; ++i) {
    addr = (SQueryNodeAddr *)taosArrayGet(pTask->execAddrs, i);
D
dapan1121 已提交
1272

D
dapan1121 已提交
1273 1274
    schBuildAndSendMsg(pJob, pTask, addr, TDMT_VND_DROP_TASK);
  }
D
dapan1121 已提交
1275 1276

  SCH_TASK_DLOG("task has %d exec address", size);
D
dapan1121 已提交
1277 1278 1279 1280
}

void schDropTaskInHashList(SSchJob *pJob, SHashObj *list) {
  void *pIter = taosHashIterate(list, NULL);
D
dapan1121 已提交
1281
  while (pIter) {
D
dapan1121 已提交
1282
    SSchTask *pTask = *(SSchTask **)pIter;
H
Haojun Liao 已提交
1283

D
dapan1121 已提交
1284 1285
    if (!SCH_TASK_NO_NEED_DROP(pTask)) {
      schDropTaskOnExecutedNode(pJob, pTask);
H
Haojun Liao 已提交
1286
    }
D
dapan1121 已提交
1287 1288 1289 1290
    
    pIter = taosHashIterate(list, pIter);
  } 
}
H
Haojun Liao 已提交
1291

D
dapan1121 已提交
1292 1293 1294 1295
void schDropJobAllTasks(SSchJob *pJob) {
  schDropTaskInHashList(pJob, pJob->execTasks);
  schDropTaskInHashList(pJob, pJob->succTasks);
  schDropTaskInHashList(pJob, pJob->failTasks);
D
dapan1121 已提交
1296
}
1297

1298
int32_t schExecJobImpl(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** job, bool syncSchedule) {
D
dapan1121 已提交
1299
  if (nodeList && taosArrayGetSize(nodeList) <= 0) {
D
dapan1121 已提交
1300
    qInfo("QID:%"PRIx64" input nodeList is empty", pDag->queryId);
D
dapan1121 已提交
1301 1302
  }

D
dapan1121 已提交
1303
  int32_t code = 0;
D
dapan1121 已提交
1304 1305
  SSchJob *pJob = calloc(1, sizeof(SSchJob));
  if (NULL == pJob) {
D
dapan1121 已提交
1306
    qError("QID:%"PRIx64" calloc %d failed", pDag->queryId, (int32_t)sizeof(SSchJob));
D
dapan1121 已提交
1307
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
1308 1309
  }

D
dapan1121 已提交
1310 1311 1312
  pJob->attr.syncSchedule = syncSchedule;
  pJob->transport = transport;
  pJob->nodeList = nodeList;
D
dapan 已提交
1313

D
dapan1121 已提交
1314
  SCH_ERR_JRET(schValidateAndBuildJob(pDag, pJob));
D
dapan1121 已提交
1315

D
dapan1121 已提交
1316 1317 1318
  pJob->execTasks = taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == pJob->execTasks) {
    SCH_JOB_ELOG("taosHashInit %d execTasks failed", pDag->numOfSubplans);
D
dapan 已提交
1319 1320 1321
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
1322 1323 1324
  pJob->succTasks = taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == pJob->succTasks) {
    SCH_JOB_ELOG("taosHashInit %d succTasks failed", pDag->numOfSubplans);
D
dapan 已提交
1325 1326
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan 已提交
1327

D
dapan1121 已提交
1328 1329 1330
  pJob->failTasks = taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == pJob->failTasks) {
    SCH_JOB_ELOG("taosHashInit %d failTasks failed", pDag->numOfSubplans);
D
dapan1121 已提交
1331 1332 1333
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
1334
  tsem_init(&pJob->rspSem, 0, 0);
D
dapan1121 已提交
1335

D
dapan1121 已提交
1336
  code = taosHashPut(schMgmt.jobs, &pJob->queryId, sizeof(pJob->queryId), &pJob, POINTER_BYTES);
D
dapan1121 已提交
1337 1338
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
1339
      SCH_JOB_ELOG("job already exist, isQueryJob:%d", pJob->attr.queryJob);
D
dapan1121 已提交
1340 1341
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
    } else {
D
dapan1121 已提交
1342 1343
      SCH_JOB_ELOG("taosHashPut job failed, errno:%d", errno);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1344
    }
D
dapan1121 已提交
1345 1346
  }

D
dapan1121 已提交
1347
  pJob->status = JOB_TASK_STATUS_NOT_START;
D
dapan1121 已提交
1348
  
D
dapan1121 已提交
1349
  SCH_ERR_JRET(schLaunchJob(pJob));
1350

D
dapan1121 已提交
1351
  *(SSchJob **)job = pJob;
D
dapan1121 已提交
1352
  
D
dapan 已提交
1353
  if (syncSchedule) {
D
dapan1121 已提交
1354
    SCH_JOB_DLOG("will wait for rsp now, job status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1355
    tsem_wait(&pJob->rspSem);
D
dapan1121 已提交
1356 1357
  }

D
dapan1121 已提交
1358
  SCH_JOB_DLOG("job exec done, job status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1359

D
dapan1121 已提交
1360
  return TSDB_CODE_SUCCESS;
1361

D
dapan1121 已提交
1362
_return:
D
dapan1121 已提交
1363

D
dapan1121 已提交
1364 1365 1366
  *(SSchJob **)job = NULL;
  
  scheduleFreeJob(pJob);
D
dapan1121 已提交
1367 1368
  
  SCH_RET(code);
1369
}
D
dapan1121 已提交
1370

D
dapan1121 已提交
1371 1372 1373 1374 1375 1376 1377
int32_t schCancelJob(SSchJob *pJob) {
  //TODO

  //TODO MOVE ALL TASKS FROM EXEC LIST TO FAIL LIST

}

D
dapan1121 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

int32_t schedulerInit(SSchedulerCfg *cfg) {
  if (schMgmt.jobs) {
    qError("scheduler already initialized");
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  if (cfg) {
    schMgmt.cfg = *cfg;
    
    if (schMgmt.cfg.maxJobNum == 0) {
      schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_JOB_NUMBER;
    }
  } else {
    schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_JOB_NUMBER;
  }

  schMgmt.jobs = taosHashInit(schMgmt.cfg.maxJobNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == schMgmt.jobs) {
    qError("init schduler jobs failed, num:%u", schMgmt.cfg.maxJobNum);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
1401
  if (taosGetSystemUUID((char *)&schMgmt.sId, sizeof(schMgmt.sId))) {
D
dapan1121 已提交
1402 1403 1404 1405
    qError("generate schdulerId failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR);
  }

D
dapan1121 已提交
1406
  qInfo("scheduler %"PRIx64" initizlized, maxJob:%u", schMgmt.sId, schMgmt.cfg.maxJobNum);
D
dapan1121 已提交
1407 1408 1409 1410
  
  return TSDB_CODE_SUCCESS;
}

1411
int32_t scheduleExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob, SQueryResult *pRes) {
H
Haojun Liao 已提交
1412
  if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == pRes) {
D
dapan1121 已提交
1413 1414 1415
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

D
dapan1121 已提交
1416 1417
  SSchJob *job = NULL;

D
dapan1121 已提交
1418
  SCH_ERR_RET(schExecJobImpl(transport, nodeList, pDag, pJob, true));
D
dapan1121 已提交
1419

D
dapan1121 已提交
1420
  job = *pJob;
D
dapan1121 已提交
1421

D
dapan 已提交
1422
  pRes->code = atomic_load_32(&job->errCode);
D
dapan1121 已提交
1423 1424
  pRes->numOfRows = job->resNumOfRows;
  
D
dapan1121 已提交
1425 1426 1427
  return TSDB_CODE_SUCCESS;
}

1428
int32_t scheduleAsyncExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob) {
1429
  if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob) {
D
dapan1121 已提交
1430 1431 1432
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

H
Haojun Liao 已提交
1433
  SCH_ERR_RET(schExecJobImpl(transport, nodeList, pDag, pJob, false));
D
dapan1121 已提交
1434
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1435 1436
}

1437
int32_t schedulerConvertDagToTaskList(SQueryDag* pDag, SArray **pTasks) {
D
dapan1121 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  if (NULL == pDag || pDag->numOfSubplans <= 0 || taosArrayGetSize(pDag->pSubplans) <= 0) {
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t levelNum = taosArrayGetSize(pDag->pSubplans);
  if (1 != levelNum) {
    qError("invalid level num: %d", levelNum);
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SArray *plans = taosArrayGet(pDag->pSubplans, 0);
  int32_t taskNum = taosArrayGetSize(plans);
  if (taskNum <= 0) {
    qError("invalid task num: %d", taskNum);
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SArray *info = taosArrayInit(taskNum, sizeof(STaskInfo));
  if (NULL == info) {
    qError("taosArrayInit %d taskInfo failed", taskNum);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  STaskInfo tInfo = {0};
  char *msg = NULL;
  int32_t msgLen = 0;
  int32_t code = 0;
  
  for (int32_t i = 0; i < taskNum; ++i) {
    SSubplan *plan = taosArrayGetP(plans, i);

    tInfo.addr = plan->execNode;

    code = qSubPlanToString(plan, &msg, &msgLen);
    if (TSDB_CODE_SUCCESS != code || NULL == msg || msgLen <= 0) {
      qError("subplanToString error, code:%x, msg:%p, len:%d", code, msg, msgLen);
      SCH_ERR_JRET(code);
    }

    int32_t msgSize = sizeof(SSubQueryMsg) + msgLen;
    msg = calloc(1, msgSize);
    if (NULL == msg) {
      qError("calloc %d failed", msgSize);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
    
H
Haojun Liao 已提交
1484
    SSubQueryMsg *pMsg = (SSubQueryMsg*) msg;
D
dapan1121 已提交
1485 1486 1487 1488 1489
    
    pMsg->header.vgId = htonl(tInfo.addr.nodeId);
    
    pMsg->sId = htobe64(schMgmt.sId);
    pMsg->queryId = htobe64(plan->id.queryId);
D
dapan1121 已提交
1490
    pMsg->taskId = htobe64(schGenUUID());
D
dapan1121 已提交
1491
    pMsg->taskType = TASK_TYPE_PERSISTENT;
D
dapan1121 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
    pMsg->contentLen = htonl(msgLen);
    memcpy(pMsg->msg, msg, msgLen);

    tInfo.msg = pMsg;

    if (NULL == taosArrayPush(info, &tInfo)) {
      qError("taosArrayPush failed, idx:%d", i);
      free(msg);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
  }

  *pTasks = info;
  info = NULL;
  
_return:

  schedulerFreeTaskList(info);

  SCH_RET(code);
}

D
dapan1121 已提交
1514 1515 1516 1517 1518 1519
int32_t schedulerCopyTask(STaskInfo *src, SArray **dst, int32_t copyNum) {
  if (NULL == src || NULL == dst || copyNum <= 0) {
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
H
Haojun Liao 已提交
1520

D
dapan1121 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
  *dst = taosArrayInit(copyNum, sizeof(STaskInfo));
  if (NULL == *dst) {
    qError("taosArrayInit %d taskInfo failed", copyNum);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  int32_t msgSize = src->msg->contentLen + sizeof(*src->msg);
  STaskInfo info = {0};

  info.addr = src->addr;
H
Haojun Liao 已提交
1531

D
dapan1121 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
  for (int32_t i = 0; i < copyNum; ++i) {
    info.msg = malloc(msgSize);
    if (NULL == info.msg) {
      qError("malloc %d failed", msgSize);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

    memcpy(info.msg, src->msg, msgSize);

    info.msg->taskId = schGenUUID();
H
Haojun Liao 已提交
1542

D
dapan1121 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
    if (NULL == taosArrayPush(*dst, &info)) {
      qError("taosArrayPush failed, idx:%d", i);
      free(info.msg);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
  }

  return TSDB_CODE_SUCCESS;

_return:

  schedulerFreeTaskList(*dst);
  *dst = NULL;
H
Haojun Liao 已提交
1556

D
dapan1121 已提交
1557 1558 1559
  SCH_RET(code);
}

D
dapan1121 已提交
1560

1561 1562
int32_t scheduleFetchRows(SSchJob *pJob, void** pData) {
  if (NULL == pJob || NULL == pData) {
D
dapan1121 已提交
1563
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan 已提交
1564
  }
D
dapan 已提交
1565
  int32_t code = 0;
D
dapan 已提交
1566

D
dapan1121 已提交
1567 1568
  atomic_add_fetch_32(&pJob->ref, 1);

D
dapan1121 已提交
1569 1570 1571
  int8_t status = SCH_GET_JOB_STATUS(pJob);
  if (status == JOB_TASK_STATUS_DROPPING) {
    SCH_JOB_ELOG("job is dropping, status:%d", status);
D
dapan1121 已提交
1572
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
1573 1574
  }

D
dapan1121 已提交
1575 1576
  if (!SCH_JOB_NEED_FETCH(&pJob->attr)) {
    SCH_JOB_ELOG("no need to fetch data, status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1577
    SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
1578 1579
  }

D
dapan1121 已提交
1580 1581
  if (atomic_val_compare_exchange_8(&pJob->userFetch, 0, 1) != 0) {
    SCH_JOB_ELOG("prior fetching not finished, userFetch:%d", atomic_load_8(&pJob->userFetch));
D
dapan1121 已提交
1582
    SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan 已提交
1583 1584
  }

D
dapan1121 已提交
1585
  if (JOB_TASK_STATUS_FAILED == status || JOB_TASK_STATUS_DROPPING == status) {
D
dapan1121 已提交
1586
    SCH_JOB_ELOG("job failed or dropping, status:%d", status);
D
dapan1121 已提交
1587 1588
    SCH_ERR_JRET(atomic_load_32(&pJob->errCode));
  } else if (status == JOB_TASK_STATUS_SUCCEED) {
D
dapan1121 已提交
1589
    SCH_JOB_ELOG("job already succeed, status:%d", status);
D
dapan1121 已提交
1590 1591 1592
    goto _return;
  } else if (status == JOB_TASK_STATUS_PARTIAL_SUCCEED) {
    SCH_ERR_JRET(schFetchFromRemote(pJob));
D
dapan 已提交
1593 1594
  }

D
dapan1121 已提交
1595
  tsem_wait(&pJob->rspSem);
D
dapan 已提交
1596

D
dapan1121 已提交
1597
  status = SCH_GET_JOB_STATUS(pJob);
D
dapan 已提交
1598

D
dapan1121 已提交
1599
  if (JOB_TASK_STATUS_FAILED == status || JOB_TASK_STATUS_DROPPING == status) {
D
dapan1121 已提交
1600 1601
    SCH_JOB_ELOG("job failed or dropping, status:%d", status);
    SCH_ERR_JRET(atomic_load_32(&pJob->errCode));
D
dapan 已提交
1602 1603
  }
  
D
dapan1121 已提交
1604 1605
  if (pJob->res && ((SRetrieveTableRsp *)pJob->res)->completed) {
    SCH_ERR_JRET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_SUCCEED));
D
dapan 已提交
1606 1607
  }

D
dapan1121 已提交
1608 1609
_return:

D
dapan1121 已提交
1610
  while (true) {
D
dapan1121 已提交
1611
    *pData = atomic_load_ptr(&pJob->res);
D
dapan1121 已提交
1612
    
D
dapan1121 已提交
1613
    if (*pData != atomic_val_compare_exchange_ptr(&pJob->res, *pData, NULL)) {
D
dapan1121 已提交
1614 1615 1616 1617 1618
      continue;
    }

    break;
  }
D
dapan 已提交
1619

D
dapan1121 已提交
1620 1621 1622 1623 1624 1625 1626 1627
  if (NULL == *pData) {
    SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)calloc(1, sizeof(SRetrieveTableRsp));
    if (rsp) {
      rsp->completed = 1;
    }

    *pData = rsp;
  }
D
dapan1121 已提交
1628 1629 1630

  atomic_val_compare_exchange_8(&pJob->userFetch, 1, 0);

D
dapan1121 已提交
1631 1632
  SCH_JOB_DLOG("fetch done, code:%x", code);

D
dapan1121 已提交
1633
  atomic_sub_fetch_32(&pJob->ref, 1);
D
dapan 已提交
1634

D
dapan1121 已提交
1635
  SCH_RET(code);
D
dapan 已提交
1636
}
D
dapan1121 已提交
1637

D
dapan1121 已提交
1638 1639
int32_t scheduleCancelJob(void *job) {
  SSchJob *pJob = (SSchJob *)job;
D
dapan1121 已提交
1640

D
dapan1121 已提交
1641
  atomic_add_fetch_32(&pJob->ref, 1);
D
dapan1121 已提交
1642

D
dapan1121 已提交
1643 1644 1645 1646 1647
  int32_t code = schCancelJob(pJob);

  atomic_sub_fetch_32(&pJob->ref, 1);

  SCH_RET(code);
D
dapan1121 已提交
1648 1649
}

D
dapan1121 已提交
1650 1651
void scheduleFreeJob(void *job) {
  if (NULL == job) {
D
dapan 已提交
1652 1653
    return;
  }
D
dapan1121 已提交
1654

D
dapan1121 已提交
1655
  SSchJob *pJob = job;
D
dapan1121 已提交
1656
  uint64_t queryId = pJob->queryId;
D
dapan1121 已提交
1657
  bool setJobFree = false;
D
dapan1121 已提交
1658

D
dapan1121 已提交
1659 1660 1661 1662 1663
  if (SCH_GET_JOB_STATUS(pJob) > 0) {
    if (0 != taosHashRemove(schMgmt.jobs, &pJob->queryId, sizeof(pJob->queryId))) {
      SCH_JOB_ELOG("taosHashRemove job from list failed, may already freed, pJob:%p", pJob);
      return;
    }
D
dapan1121 已提交
1664

D
dapan1121 已提交
1665
    SCH_JOB_DLOG("job removed from list, no further ref, ref:%d", atomic_load_32(&pJob->ref));
D
dapan1121 已提交
1666

D
dapan1121 已提交
1667 1668 1669 1670 1671
    while (true) {
      int32_t ref = atomic_load_32(&pJob->ref);
      if (0 == ref) {
        break;
      } else if (ref > 0) {
D
dapan1121 已提交
1672 1673 1674 1675
        if (1 == ref && atomic_load_8(&pJob->userFetch) > 0 && !setJobFree) {
          schProcessOnJobDropped(pJob, TSDB_CODE_QRY_JOB_FREED);
          setJobFree = true;
        }
H
Haojun Liao 已提交
1676

D
dapan1121 已提交
1677 1678 1679 1680
        usleep(1);
      } else {
        assert(0);
      }
D
dapan1121 已提交
1681
    }
D
dapan1121 已提交
1682

D
dapan1121 已提交
1683
    SCH_JOB_DLOG("job no ref now, status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1684

D
dapan1121 已提交
1685 1686 1687
    if (pJob->status == JOB_TASK_STATUS_EXECUTING) {
      schCancelJob(pJob);
    }
1688

D
dapan1121 已提交
1689 1690
    schDropJobAllTasks(pJob);
  }
D
dapan1121 已提交
1691 1692 1693 1694

  pJob->subPlans = NULL; // it is a reference to pDag->pSubplans
  
  int32_t numOfLevels = taosArrayGetSize(pJob->levels);
1695
  for(int32_t i = 0; i < numOfLevels; ++i) {
D
dapan1121 已提交
1696
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);
1697 1698 1699 1700

    int32_t numOfTasks = taosArrayGetSize(pLevel->subTasks);
    for(int32_t j = 0; j < numOfTasks; ++j) {
      SSchTask* pTask = taosArrayGet(pLevel->subTasks, j);
D
dapan1121 已提交
1701
      schFreeTask(pTask);
1702 1703 1704 1705
    }

    taosArrayDestroy(pLevel->subTasks);
  }
D
dapan1121 已提交
1706
  
D
dapan1121 已提交
1707 1708 1709
  taosHashCleanup(pJob->execTasks);
  taosHashCleanup(pJob->failTasks);
  taosHashCleanup(pJob->succTasks);
D
dapan1121 已提交
1710
  
D
dapan1121 已提交
1711
  taosArrayDestroy(pJob->levels);
D
dapan1121 已提交
1712
  taosArrayDestroy(pJob->nodeList);
D
dapan1121 已提交
1713 1714

  tfree(pJob->res);
D
dapan1121 已提交
1715
  
D
dapan1121 已提交
1716
  tfree(pJob);
D
dapan1121 已提交
1717 1718

  qDebug("QID:%"PRIx64" job freed", queryId);
D
dapan1121 已提交
1719
}
D
dapan1121 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733

void schedulerFreeTaskList(SArray *taskList) {
  if (NULL == taskList) {
    return;
  }

  int32_t taskNum = taosArrayGetSize(taskList);
  for (int32_t i = 0; i < taskNum; ++i) {
    STaskInfo *info = taosArrayGet(taskList, i);
    tfree(info->msg);
  }

  taosArrayDestroy(taskList);
}
D
dapan1121 已提交
1734 1735 1736 1737 1738 1739 1740 1741
  
void schedulerDestroy(void) {
  if (schMgmt.jobs) {
    taosHashCleanup(schMgmt.jobs); //TODO
    schMgmt.jobs = NULL;
  }
}