scheduler.c 51.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

D
dapan 已提交
278
int32_t schRecordTaskSucceedNode(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
279 280 281
  int32_t idx = atomic_load_8(&pTask->candidateIdx);
  SQueryNodeAddr *addr = taosArrayGet(pTask->candidateAddrs, idx);
  if (NULL == addr) {
D
dapan1121 已提交
282
    SCH_TASK_ELOG("taosArrayGet candidate addr failed, idx:%d, size:%d", idx, (int32_t)taosArrayGetSize(pTask->candidateAddrs));
D
dapan1121 已提交
283 284
    SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }
D
dapan1121 已提交
285 286

  pTask->succeedAddr = *addr;
287

D
dapan1121 已提交
288
  return TSDB_CODE_SUCCESS;
289 290
}

D
dapan1121 已提交
291 292 293 294 295 296 297 298

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;
299
}
D
dapan1121 已提交
300

D
dapan1121 已提交
301

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

D
dapan1121 已提交
317 318
  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 已提交
319
    SCH_JOB_ELOG("taosHashInit %d failed", SCHEDULE_DEFAULT_TASK_NUMBER);
D
dapan1121 已提交
320 321
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
322

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

D
dapan1121 已提交
329 330
  pJob->levelNum = levelNum;
  pJob->levelIdx = levelNum - 1;
331

D
dapan1121 已提交
332
  pJob->subPlans = pDag->pSubplans;
333

D
dapan 已提交
334
  SSchLevel level = {0};
D
dapan1121 已提交
335 336
  SArray *plans = NULL;
  int32_t taskNum = 0;
D
dapan 已提交
337
  SSchLevel *pLevel = NULL;
338

D
dapan1121 已提交
339
  level.status = JOB_TASK_STATUS_NOT_START;
D
dapan1121 已提交
340

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

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

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

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

D
dapan1121 已提交
374 375 376 377 378
      SCH_SET_JOB_TYPE(&pJob->attr, plan->type);

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

D
dapan1121 已提交
392 393
      SCH_TASK_DLOG("task initialized, level:%d", pLevel->level);
    }
D
dapan1121 已提交
394

D
dapan1121 已提交
395
    SCH_JOB_DLOG("level initialized, taskNum:%d", taskNum);
D
dapan1121 已提交
396
  }
D
dapan1121 已提交
397 398

  SCH_ERR_JRET(schBuildTaskRalation(pJob, planToTask));
D
dapan1121 已提交
399 400

_return:
D
dapan1121 已提交
401 402 403 404
  if (planToTask) {
    taosHashCleanup(planToTask);
  }

D
dapan1121 已提交
405
  SCH_RET(code);
406 407
}

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

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

D
dapan1121 已提交
420 421 422
  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 已提交
423 424 425
      SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

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

D
dapan1121 已提交
428 429 430 431
    return TSDB_CODE_SUCCESS;
  }

  int32_t addNum = 0;
D
dapan 已提交
432
  int32_t nodeNum = 0;
433
  if (pJob->nodeList) {
D
dapan 已提交
434
    nodeNum = taosArrayGetSize(pJob->nodeList);
D
dapan1121 已提交
435
    
436
    for (int32_t i = 0; i < nodeNum && addNum < SCH_MAX_CANDIDATE_EP_NUM; ++i) {
D
dapan 已提交
437 438
      SQueryNodeAddr *naddr = taosArrayGet(pJob->nodeList, i);
      
439
      if (NULL == taosArrayPush(pTask->candidateAddrs, naddr)) {
D
dapan 已提交
440 441 442
        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 已提交
443 444

      ++addNum;
D
dapan1121 已提交
445
    }
D
dapan1121 已提交
446 447
  }

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

D
dapan1121 已提交
453
/*
454
  for (int32_t i = 0; i < job->dataSrcEps.numOfEps && addNum < SCH_MAX_CANDIDATE_EP_NUM; ++i) {
D
dapan1121 已提交
455 456 457 458
    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 已提交
459
  }
D
dapan1121 已提交
460
*/
D
dapan 已提交
461 462

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
463
}
D
dapan1121 已提交
464

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

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

D
dapan 已提交
479 480 481
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
482 483 484
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 已提交
485 486
  }

D
dapan1121 已提交
487 488 489 490 491 492 493 494 495 496
  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 已提交
497 498 499 500
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  *moved = true;
D
dapan1121 已提交
501 502

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

D
dapan1121 已提交
507 508 509 510 511
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 已提交
512 513
  }

D
dapan1121 已提交
514 515 516 517 518 519 520 521 522 523
  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 已提交
524 525 526 527
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  *moved = true;
D
dapan1121 已提交
528 529

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

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

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 已提交
561
int32_t schTaskCheckAndSetRetry(SSchJob *job, SSchTask *task, int32_t errCode, bool *needRetry) {
D
dapan1121 已提交
562 563
  // 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 已提交
564 565
  // TODO set condidateIdx
  // TODO record failed but tried task
D
dapan 已提交
566

D
dapan1121 已提交
567 568 569
  *needRetry = false;

  return TSDB_CODE_SUCCESS;
D
dapan 已提交
570 571
}

D
dapan1121 已提交
572
int32_t schProcessOnJobFailureImpl(SSchJob *pJob, int32_t status, int32_t errCode) {
D
dapan1121 已提交
573
  // if already FAILED, no more processing
D
dapan1121 已提交
574
  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, status));
D
dapan1121 已提交
575 576 577 578 579 580 581 582 583
  
  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);
  }

D
dapan1121 已提交
584 585
  int32_t code = atomic_load_32(&pJob->errCode);
  SCH_ERR_RET(code);
D
dapan1121 已提交
586

D
dapan1121 已提交
587
  SCH_JOB_ELOG("job errCode is invalid, errCode:%d", code);
D
dapan1121 已提交
588 589 590 591
}



D
dapan1121 已提交
592 593 594 595 596 597 598 599 600 601 602
// 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 已提交
603
// Note: no more error processing, handled in function internal
D
dapan1121 已提交
604
int32_t schFetchFromRemote(SSchJob *pJob) {
D
dapan 已提交
605 606
  int32_t code = 0;
  
D
dapan1121 已提交
607 608
  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 已提交
609 610 611
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
612 613 614 615 616 617 618
  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 已提交
619 620

  SCH_ERR_JRET(schBuildAndSendMsg(pJob, pJob->fetchTask, &pJob->resNode, TDMT_VND_FETCH));
D
dapan 已提交
621 622 623 624

  return TSDB_CODE_SUCCESS;
  
_return:
D
dapan1121 已提交
625 626

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

D
dapan1121 已提交
628 629
  schProcessOnJobFailure(pJob, code);

D
dapan 已提交
630
  return code;
D
dapan 已提交
631 632
}

D
dapan 已提交
633

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

D
dapan1121 已提交
640
  if ((!SCH_JOB_NEED_FETCH(&pJob->attr)) && pJob->attr.syncSchedule) {
D
dapan 已提交
641
    tsem_post(&pJob->rspSem);
D
dapan 已提交
642
  }
D
dapan1121 已提交
643 644 645 646
  
  if (atomic_load_8(&pJob->userFetch)) {
    SCH_ERR_JRET(schFetchFromRemote(pJob));
  }
D
dapan 已提交
647

D
dapan 已提交
648
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
649 650 651 652 653 654

_return:

  SCH_ERR_RET(schProcessOnJobFailure(pJob, code));

  SCH_RET(code);
D
dapan 已提交
655 656
}

D
dapan1121 已提交
657
int32_t schProcessOnDataFetched(SSchJob *job) {
D
dapan 已提交
658 659
  atomic_val_compare_exchange_32(&job->remoteFetch, 1, 0);
  tsem_post(&job->rspSem);
D
dapan 已提交
660 661
}

D
dapan1121 已提交
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

H
Haojun Liao 已提交
669
  SCH_TASK_DLOG("taskOnFailure, code:%s", tstrerror(errCode));
D
dapan 已提交
670
  
D
dapan1121 已提交
671 672 673
  SCH_ERR_JRET(schTaskCheckAndSetRetry(pJob, pTask, errCode, &needRetry));
  
  if (!needRetry) {
H
Haojun Liao 已提交
674
    SCH_TASK_ELOG("task failed and no more retry, code:%s", tstrerror(errCode));
D
dapan1121 已提交
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

    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

D
dapan1121 已提交
728
  SCH_ERR_JRET(schRecordTaskSucceedNode(pJob, 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
      if (taskDone < pTask->level->taskNum) {
S
Shengliang Guan 已提交
741
        SCH_TASK_DLOG("wait all tasks, done:%d, all:%d", taskDone, pTask->level->taskNum);
D
dapan1121 已提交
742
        
D
dapan1121 已提交
743
        return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
744
      } else if (taskDone > pTask->level->taskNum) {
D
dapan1121 已提交
745
        SCH_TASK_ELOG("taskDone number invalid, done:%d, total:%d", taskDone, pTask->level->taskNum);
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
        atomic_store_ptr(&pJob->res, rsp);
D
dapan1121 已提交
878
        atomic_add_fetch_32(&pJob->resNumOfRows, htonl(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 885
        SCH_TASK_DLOG("got fetch rsp, rows:%d, complete:%d", htonl(rsp->numOfRows), rsp->completed);

D
dapan1121 已提交
886
        SCH_ERR_JRET(schProcessOnDataFetched(pJob));
D
dapan1121 已提交
887
        break;
D
dapan1121 已提交
888
      }
D
dapan1121 已提交
889
    case TDMT_VND_DROP_TASK_RSP: {
D
dapan1121 已提交
890
        // SHOULD NEVER REACH HERE
D
dapan1121 已提交
891 892
        SCH_TASK_ELOG("invalid status to handle drop task rsp, ref:%d", atomic_load_32(&pJob->ref));
        SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
D
dapan1121 已提交
893
        break;
D
dapan1121 已提交
894
      }
D
dapan1121 已提交
895
    default:
D
dapan1121 已提交
896 897
      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 已提交
898 899 900 901 902
  }

  return TSDB_CODE_SUCCESS;

_return:
D
dapan1121 已提交
903 904 905 906

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

D
dapan 已提交
909

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

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

  atomic_add_fetch_32(&pJob->ref, 1);

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

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

  pTask = *task;
939
  SCH_TASK_DLOG("rsp msg received, type:%s, code:%s", TMSG_INFO(msgType), tstrerror(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

  qDebug("QID:%"PRIx64 ",TID:%"PRIx64 " req msg sent, type:%d, %s", qId, tId, msgType, TMSG_INFO(msgType));
H
Haojun Liao 已提交
1042

D
dapan1121 已提交
1043 1044 1045
  return TSDB_CODE_SUCCESS;

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

  SCH_RET(code);
}

D
dapan1121 已提交
1053
void schConvertAddrToEpSet(SQueryNodeAddr *addr, SEpSet *epSet) {
D
dapan1121 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062
  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 已提交
1063
int32_t schBuildAndSendMsg(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr, int32_t msgType) {
D
dapan1121 已提交
1064 1065 1066
  uint32_t msgSize = 0;
  void *msg = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1067
  bool isCandidateAddr = false;
D
dapan 已提交
1068
  SEpSet epSet;
D
dapan1121 已提交
1069 1070 1071 1072 1073 1074 1075

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

D
dapan 已提交
1076
  schConvertAddrToEpSet(addr, &epSet);
D
dapan1121 已提交
1077 1078
  
  switch (msgType) {
H
Haojun Liao 已提交
1079
    case TDMT_VND_CREATE_TABLE:
D
dapan1121 已提交
1080
    case TDMT_VND_SUBMIT: {
D
dapan1121 已提交
1081
      msgSize = pTask->msgLen;
D
dapan1121 已提交
1082 1083 1084 1085 1086 1087 1088
      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 已提交
1089 1090
      break;
    }
1091

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

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

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

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

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

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

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

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

_return:

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

D
dapan1121 已提交
1184 1185 1186
  tfree(msg);
  SCH_RET(code);
}
D
dapan1121 已提交
1187

D
dapan1121 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196
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 已提交
1197

D
dapan1121 已提交
1198 1199

// Note: no more error processing, handled in function internal
D
dapan1121 已提交
1200
int32_t schLaunchTask(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
1201 1202 1203 1204 1205
  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 已提交
1206 1207 1208 1209 1210
    
    code = atomic_load_32(&pJob->errCode);
    SCH_ERR_RET(code);
    
    SCH_RET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
1211
  }
D
dapan1121 已提交
1212
  
D
dapan1121 已提交
1213
  SSubplan *plan = pTask->plan;
D
dapan1121 已提交
1214

D
dapan1121 已提交
1215 1216 1217 1218 1219 1220
  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);
    }
1221

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

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

D
dapan 已提交
1231 1232
    SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_EXECUTING);
  }
D
dapan1121 已提交
1233

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

_return:

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

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

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

D
dapan1121 已提交
1258 1259 1260 1261 1262
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 已提交
1263

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

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

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

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

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

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

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

1300
int32_t schExecJobImpl(void *transport, SArray *pNodeList, SQueryDag* pDag, struct SSchJob** job, bool syncSchedule) {
H
Haojun Liao 已提交
1301 1302
  qDebug("QID:0x%"PRIx64" job started", pDag->queryId);

1303
  if (pNodeList && taosArrayGetSize(pNodeList) <= 0) {
H
Haojun Liao 已提交
1304
    qDebug("QID:0x%"PRIx64" input exec nodeList is empty", pDag->queryId);
D
dapan1121 已提交
1305 1306
  }

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

D
dapan1121 已提交
1314 1315
  pJob->attr.syncSchedule = syncSchedule;
  pJob->transport = transport;
1316 1317 1318 1319

  if (pNodeList != NULL) {
    pJob->nodeList = taosArrayDup(pNodeList);
  }
D
dapan 已提交
1320

D
dapan1121 已提交
1321
  SCH_ERR_JRET(schValidateAndBuildJob(pDag, pJob));
D
dapan1121 已提交
1322

D
dapan1121 已提交
1323 1324 1325
  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 已提交
1326 1327 1328
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
1329 1330 1331
  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 已提交
1332 1333
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan 已提交
1334

D
dapan1121 已提交
1335 1336 1337
  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 已提交
1338 1339 1340
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

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

D
dapan1121 已提交
1343
  code = taosHashPut(schMgmt.jobs, &pJob->queryId, sizeof(pJob->queryId), &pJob, POINTER_BYTES);
D
dapan1121 已提交
1344 1345
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
1346
      SCH_JOB_ELOG("job already exist, isQueryJob:%d", pJob->attr.queryJob);
D
dapan1121 已提交
1347 1348
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
    } else {
D
dapan1121 已提交
1349 1350
      SCH_JOB_ELOG("taosHashPut job failed, errno:%d", errno);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1351
    }
D
dapan1121 已提交
1352 1353
  }

D
dapan1121 已提交
1354
  pJob->status = JOB_TASK_STATUS_NOT_START;
D
dapan1121 已提交
1355
  
D
dapan1121 已提交
1356
  SCH_ERR_JRET(schLaunchJob(pJob));
1357

D
dapan1121 已提交
1358
  *(SSchJob **)job = pJob;
D
dapan1121 已提交
1359
  
D
dapan 已提交
1360
  if (syncSchedule) {
D
dapan1121 已提交
1361
    SCH_JOB_DLOG("will wait for rsp now, job status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1362
    tsem_wait(&pJob->rspSem);
D
dapan1121 已提交
1363 1364
  }

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

D
dapan1121 已提交
1367
  return TSDB_CODE_SUCCESS;
1368

D
dapan1121 已提交
1369
_return:
D
dapan1121 已提交
1370

D
dapan1121 已提交
1371 1372
  *(SSchJob **)job = NULL;
  
D
dapan1121 已提交
1373
  schedulerFreeJob(pJob);
D
dapan1121 已提交
1374 1375
  
  SCH_RET(code);
1376
}
D
dapan1121 已提交
1377

D
dapan1121 已提交
1378 1379 1380 1381 1382 1383 1384
int32_t schCancelJob(SSchJob *pJob) {
  //TODO

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

}

D
dapan1121 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407

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 已提交
1408
  if (taosGetSystemUUID((char *)&schMgmt.sId, sizeof(schMgmt.sId))) {
D
dapan1121 已提交
1409 1410 1411 1412
    qError("generate schdulerId failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR);
  }

D
dapan1121 已提交
1413
  qInfo("scheduler %"PRIx64" initizlized, maxJob:%u", schMgmt.sId, schMgmt.cfg.maxJobNum);
D
dapan1121 已提交
1414 1415 1416 1417
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1418
int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob, SQueryResult *pRes) {
H
Haojun Liao 已提交
1419
  if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == pRes) {
D
dapan1121 已提交
1420 1421 1422
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

D
dapan1121 已提交
1423 1424
  SSchJob *job = NULL;

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

D
dapan1121 已提交
1427
  job = *pJob;
D
dapan1121 已提交
1428

D
dapan 已提交
1429
  pRes->code = atomic_load_32(&job->errCode);
D
dapan1121 已提交
1430 1431
  pRes->numOfRows = job->resNumOfRows;
  
D
dapan1121 已提交
1432 1433 1434
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1435
int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryDag* pDag, struct SSchJob** pJob) {
1436
  if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob) {
D
dapan1121 已提交
1437 1438 1439
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

1440
  SCH_ERR_RET(schExecJobImpl(transport, pNodeList, pDag, pJob, false));
D
dapan1121 已提交
1441
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1442 1443
}

1444
int32_t schedulerConvertDagToTaskList(SQueryDag* pDag, SArray **pTasks) {
D
dapan1121 已提交
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 1484 1485 1486 1487 1488 1489
  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;
    if (NULL == msg) {
      qError("calloc %d failed", msgSize);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
    
L
fix tq  
Liu Jicong 已提交
1490 1491 1492
    SSubQueryMsg* pMsg = calloc(1, msgSize);
    /*SSubQueryMsg *pMsg = (SSubQueryMsg*) msg;*/
    memcpy(pMsg->msg, msg, msgLen);
D
dapan1121 已提交
1493
    
L
Liu Jicong 已提交
1494
    pMsg->header.vgId = tInfo.addr.nodeId;
D
dapan1121 已提交
1495
    
L
Liu Jicong 已提交
1496 1497 1498
    pMsg->sId = schMgmt.sId;
    pMsg->queryId = plan->id.queryId;
    pMsg->taskId = schGenUUID();
D
dapan1121 已提交
1499
    pMsg->taskType = TASK_TYPE_PERSISTENT;
L
Liu Jicong 已提交
1500
    pMsg->contentLen = msgLen;
L
fix tq  
Liu Jicong 已提交
1501
    /*memcpy(pMsg->msg, ((SSubQueryMsg*)msg)->msg, msgLen);*/
D
dapan1121 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

    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 已提交
1520 1521 1522 1523 1524 1525
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 已提交
1526

D
dapan1121 已提交
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
  *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 已提交
1537

D
dapan1121 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
  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 已提交
1548

D
dapan1121 已提交
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
    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 已提交
1562

D
dapan1121 已提交
1563 1564 1565
  SCH_RET(code);
}

D
dapan1121 已提交
1566

D
dapan1121 已提交
1567
int32_t schedulerFetchRows(SSchJob *pJob, void** pData) {
1568
  if (NULL == pJob || NULL == pData) {
D
dapan1121 已提交
1569
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan 已提交
1570 1571
  }

1572
  int32_t code = 0;
D
dapan1121 已提交
1573 1574
  atomic_add_fetch_32(&pJob->ref, 1);

D
dapan1121 已提交
1575 1576 1577
  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 已提交
1578
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
1579 1580
  }

D
dapan1121 已提交
1581 1582
  if (!SCH_JOB_NEED_FETCH(&pJob->attr)) {
    SCH_JOB_ELOG("no need to fetch data, status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1583
    SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
1584 1585
  }

D
dapan1121 已提交
1586 1587
  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 已提交
1588
    SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan 已提交
1589 1590
  }

D
dapan1121 已提交
1591
  if (JOB_TASK_STATUS_FAILED == status || JOB_TASK_STATUS_DROPPING == status) {
D
dapan1121 已提交
1592
    SCH_JOB_ELOG("job failed or dropping, status:%d", status);
D
dapan1121 已提交
1593 1594
    SCH_ERR_JRET(atomic_load_32(&pJob->errCode));
  } else if (status == JOB_TASK_STATUS_SUCCEED) {
D
dapan1121 已提交
1595
    SCH_JOB_ELOG("job already succeed, status:%d", status);
D
dapan1121 已提交
1596 1597 1598
    goto _return;
  } else if (status == JOB_TASK_STATUS_PARTIAL_SUCCEED) {
    SCH_ERR_JRET(schFetchFromRemote(pJob));
D
dapan 已提交
1599 1600
  }

D
dapan1121 已提交
1601
  tsem_wait(&pJob->rspSem);
D
dapan 已提交
1602

D
dapan1121 已提交
1603
  status = SCH_GET_JOB_STATUS(pJob);
D
dapan 已提交
1604

D
dapan1121 已提交
1605
  if (JOB_TASK_STATUS_FAILED == status || JOB_TASK_STATUS_DROPPING == status) {
D
dapan1121 已提交
1606 1607
    SCH_JOB_ELOG("job failed or dropping, status:%d", status);
    SCH_ERR_JRET(atomic_load_32(&pJob->errCode));
D
dapan 已提交
1608 1609
  }
  
D
dapan1121 已提交
1610 1611
  if (pJob->res && ((SRetrieveTableRsp *)pJob->res)->completed) {
    SCH_ERR_JRET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_SUCCEED));
D
dapan 已提交
1612 1613
  }

D
dapan1121 已提交
1614 1615
_return:

D
dapan1121 已提交
1616
  while (true) {
D
dapan1121 已提交
1617 1618
    *pData = atomic_load_ptr(&pJob->res);
    if (*pData != atomic_val_compare_exchange_ptr(&pJob->res, *pData, NULL)) {
D
dapan1121 已提交
1619 1620 1621 1622 1623
      continue;
    }

    break;
  }
D
dapan 已提交
1624

D
dapan1121 已提交
1625 1626 1627 1628 1629 1630 1631
  if (NULL == *pData) {
    SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)calloc(1, sizeof(SRetrieveTableRsp));
    if (rsp) {
      rsp->completed = 1;
    }

    *pData = rsp;
D
dapan1121 已提交
1632
    SCH_JOB_DLOG("empty res and set query complete, code:%x", code);
D
dapan1121 已提交
1633
  }
D
dapan1121 已提交
1634 1635 1636

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

1637
  SCH_JOB_DLOG("fetch done, totalRows:%d, code:%s", pJob->resNumOfRows, tstrerror(code));
D
dapan1121 已提交
1638
  atomic_sub_fetch_32(&pJob->ref, 1);
D
dapan 已提交
1639

D
dapan1121 已提交
1640
  SCH_RET(code);
D
dapan 已提交
1641
}
D
dapan1121 已提交
1642

D
dapan1121 已提交
1643 1644
int32_t scheduleCancelJob(void *job) {
  SSchJob *pJob = (SSchJob *)job;
D
dapan1121 已提交
1645

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

D
dapan1121 已提交
1648 1649 1650 1651 1652
  int32_t code = schCancelJob(pJob);

  atomic_sub_fetch_32(&pJob->ref, 1);

  SCH_RET(code);
D
dapan1121 已提交
1653 1654
}

D
dapan1121 已提交
1655
void schedulerFreeJob(void *job) {
D
dapan1121 已提交
1656
  if (NULL == job) {
D
dapan 已提交
1657 1658
    return;
  }
D
dapan1121 已提交
1659

D
dapan1121 已提交
1660
  SSchJob *pJob = job;
D
dapan1121 已提交
1661
  uint64_t queryId = pJob->queryId;
D
dapan1121 已提交
1662
  bool setJobFree = false;
D
dapan1121 已提交
1663

D
dapan1121 已提交
1664 1665 1666 1667 1668
  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 已提交
1669

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

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

D
dapan1121 已提交
1682 1683
        usleep(1);
      } else {
D
dapan1121 已提交
1684 1685
        SCH_JOB_ELOG("invalid job ref number, ref:%d", ref);
        break;
D
dapan1121 已提交
1686
      }
D
dapan1121 已提交
1687
    }
D
dapan1121 已提交
1688

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

D
dapan1121 已提交
1691 1692 1693
    if (pJob->status == JOB_TASK_STATUS_EXECUTING) {
      schCancelJob(pJob);
    }
1694

D
dapan1121 已提交
1695 1696
    schDropJobAllTasks(pJob);
  }
D
dapan1121 已提交
1697 1698 1699 1700

  pJob->subPlans = NULL; // it is a reference to pDag->pSubplans
  
  int32_t numOfLevels = taosArrayGetSize(pJob->levels);
1701
  for(int32_t i = 0; i < numOfLevels; ++i) {
D
dapan1121 已提交
1702
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);
1703 1704 1705 1706

    int32_t numOfTasks = taosArrayGetSize(pLevel->subTasks);
    for(int32_t j = 0; j < numOfTasks; ++j) {
      SSchTask* pTask = taosArrayGet(pLevel->subTasks, j);
D
dapan1121 已提交
1707
      schFreeTask(pTask);
1708 1709 1710 1711
    }

    taosArrayDestroy(pLevel->subTasks);
  }
D
dapan1121 已提交
1712
  
D
dapan1121 已提交
1713 1714 1715
  taosHashCleanup(pJob->execTasks);
  taosHashCleanup(pJob->failTasks);
  taosHashCleanup(pJob->succTasks);
D
dapan1121 已提交
1716
  
D
dapan1121 已提交
1717
  taosArrayDestroy(pJob->levels);
D
dapan1121 已提交
1718
  taosArrayDestroy(pJob->nodeList);
D
dapan1121 已提交
1719 1720

  tfree(pJob->res);
D
dapan1121 已提交
1721
  
D
dapan1121 已提交
1722
  tfree(pJob);
D
dapan1121 已提交
1723

1724
  qDebug("QID:0x%"PRIx64" job freed", queryId);
D
dapan1121 已提交
1725
}
D
dapan1121 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739

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 已提交
1740 1741 1742 1743 1744 1745 1746 1747
  
void schedulerDestroy(void) {
  if (schMgmt.jobs) {
    taosHashCleanup(schMgmt.jobs); //TODO
    schMgmt.jobs = NULL;
  }
}