scheduler.c 46.9 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 49 50 51 52

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);
  pTask->taskId = atomic_add_fetch_64(&schMgmt.taskId, 1);
  pTask->execAddrs = taosArrayInit(SCH_MAX_CONDIDATE_EP_NUM, sizeof(SQueryNodeAddr));
  if (NULL == pTask->execAddrs) {
    SCH_TASK_ELOG("taosArrayInit %d exec addrs failed", SCH_MAX_CONDIDATE_EP_NUM);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  return TSDB_CODE_SUCCESS;
}

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

  // TODO NEED TO VERFY WITH ASYNC_SEND MEMORY FREE
  //tfree(pTask->msg); 

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

  if (pTask->parents) {
    taosArrayDestroy(pTask->parents);
  }
H
Haojun Liao 已提交
53 54 55 56

  if (pTask->execAddrs) {
    taosArrayDestroy(pTask->execAddrs);
  }
D
dapan1121 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}


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 已提交
75
      if (SCH_GET_TASK_STATUS(pTask) != JOB_TASK_STATUS_EXECUTING && SCH_GET_TASK_STATUS(pTask) != JOB_TASK_STATUS_PARTIAL_SUCCEED) {
D
dapan1121 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        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 已提交
92 93
  int32_t code = 0;

D
dapan1121 已提交
94
  int8_t oriStatus = 0;
D
dapan1121 已提交
95

D
dapan1121 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  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
dapan 已提交
144
        SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
145 146 147 148
        break;
        
      default:
        SCH_JOB_ELOG("invalid job status:%d", oriStatus);
D
dapan 已提交
149
        SCH_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
150 151 152 153 154
    }

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

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

D
dapan1121 已提交
158 159
    break;
  }
D
dapan1121 已提交
160

D
dapan 已提交
161 162 163 164 165 166 167 168 169
  return TSDB_CODE_SUCCESS;

_return:

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


D
dapan1121 已提交
170
int32_t schBuildTaskRalation(SSchJob *pJob, SHashObj *planToTask) {
D
dapan1121 已提交
171 172
  for (int32_t i = 0; i < pJob->levelNum; ++i) {
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);
D
dapan 已提交
173
    
D
dapan1121 已提交
174 175 176 177 178
    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 已提交
179 180

      if (childNum > 0) {
D
dapan1121 已提交
181 182 183 184 185
        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 已提交
186 187 188
        pTask->children = taosArrayInit(childNum, POINTER_BYTES);
        if (NULL == pTask->children) {
          SCH_TASK_ELOG("taosArrayInit %d children failed", childNum);
D
dapan1121 已提交
189 190 191 192 193
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
      }

      for (int32_t n = 0; n < childNum; ++n) {
D
dapan1121 已提交
194
        SSubplan **child = taosArrayGet(pPlan->pChildren, n);
D
dapan 已提交
195
        SSchTask **childTask = taosHashGet(planToTask, child, POINTER_BYTES);
D
dapan 已提交
196
        if (NULL == childTask || NULL == *childTask) {
D
dapan1121 已提交
197
          SCH_TASK_ELOG("subplan children relationship error, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
198 199 200
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

D
dapan1121 已提交
201 202
        if (NULL == taosArrayPush(pTask->children, childTask)) {
          SCH_TASK_ELOG("taosArrayPush childTask failed, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
203 204 205 206 207
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
      }

      if (parentNum > 0) {
D
dapan1121 已提交
208 209 210 211 212
        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 已提交
213 214 215
        pTask->parents = taosArrayInit(parentNum, POINTER_BYTES);
        if (NULL == pTask->parents) {
          SCH_TASK_ELOG("taosArrayInit %d parents failed", parentNum);
D
dapan1121 已提交
216 217
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
D
dapan1121 已提交
218 219 220 221 222
      } 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 已提交
223 224 225
      }

      for (int32_t n = 0; n < parentNum; ++n) {
D
dapan1121 已提交
226
        SSubplan **parent = taosArrayGet(pPlan->pParents, n);
D
dapan 已提交
227
        SSchTask **parentTask = taosHashGet(planToTask, parent, POINTER_BYTES);
D
dapan 已提交
228
        if (NULL == parentTask || NULL == *parentTask) {
D
dapan1121 已提交
229
          SCH_TASK_ELOG("subplan parent relationship error, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
230 231 232
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

D
dapan1121 已提交
233 234
        if (NULL == taosArrayPush(pTask->parents, parentTask)) {
          SCH_TASK_ELOG("taosArrayPush parentTask failed, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
D
dapan1121 已提交
235 236
          SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
D
dapan1121 已提交
237 238 239
      }  

      SCH_TASK_DLOG("level:%d, parentNum:%d, childNum:%d", i, parentNum, childNum);
D
dapan1121 已提交
240 241 242
    }
  }

D
dapan1121 已提交
243 244 245
  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 已提交
246 247 248
    SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }

D
dapan1121 已提交
249 250 251
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
252 253 254 255 256 257 258

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

  assert(NULL != addr);

  pTask->succeedAddr = *addr;
259

D
dapan1121 已提交
260
  return TSDB_CODE_SUCCESS;
261 262
}

D
dapan1121 已提交
263 264 265 266 267 268 269 270

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;
271
}
D
dapan1121 已提交
272

D
dapan1121 已提交
273

D
dapan1121 已提交
274
int32_t schValidateAndBuildJob(SQueryDag *pDag, SSchJob *pJob) {
D
dapan1121 已提交
275
  int32_t code = 0;
D
dapan1121 已提交
276
  pJob->queryId = pDag->queryId;
D
dapan1121 已提交
277
  
D
dapan1121 已提交
278 279
  if (pDag->numOfSubplans <= 0) {
    SCH_JOB_ELOG("invalid subplan num:%d", pDag->numOfSubplans);
D
dapan 已提交
280 281 282
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
  
D
dapan1121 已提交
283
  int32_t levelNum = (int32_t)taosArrayGetSize(pDag->pSubplans);
284
  if (levelNum <= 0) {
D
dapan1121 已提交
285
    SCH_JOB_ELOG("invalid level num:%d", levelNum);
D
dapan1121 已提交
286
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
287 288
  }

D
dapan1121 已提交
289 290
  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 已提交
291
    SCH_JOB_ELOG("taosHashInit %d failed", SCHEDULE_DEFAULT_TASK_NUMBER);
D
dapan1121 已提交
292 293
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
294

D
dapan1121 已提交
295 296
  pJob->levels = taosArrayInit(levelNum, sizeof(SSchLevel));
  if (NULL == pJob->levels) {
D
dapan1121 已提交
297
    SCH_JOB_ELOG("taosArrayInit %d failed", levelNum);
D
dapan1121 已提交
298
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
299 300
  }

D
dapan1121 已提交
301 302
  pJob->levelNum = levelNum;
  pJob->levelIdx = levelNum - 1;
303

D
dapan1121 已提交
304
  pJob->subPlans = pDag->pSubplans;
305

D
dapan 已提交
306
  SSchLevel level = {0};
D
dapan1121 已提交
307 308
  SArray *plans = NULL;
  int32_t taskNum = 0;
D
dapan 已提交
309
  SSchLevel *pLevel = NULL;
310

D
dapan1121 已提交
311
  level.status = JOB_TASK_STATUS_NOT_START;
D
dapan1121 已提交
312

313
  for (int32_t i = 0; i < levelNum; ++i) {
D
dapan1121 已提交
314
    if (NULL == taosArrayPush(pJob->levels, &level)) {
D
dapan1121 已提交
315
      SCH_JOB_ELOG("taosArrayPush level failed, level:%d", i);
D
dapan1121 已提交
316 317 318
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

D
dapan1121 已提交
319
    pLevel = taosArrayGet(pJob->levels, i);
D
dapan1121 已提交
320 321
  
    pLevel->level = i;
D
dapan1121 已提交
322
    
D
dapan1121 已提交
323
    plans = taosArrayGetP(pDag->pSubplans, i);
D
dapan1121 已提交
324 325
    if (NULL == plans) {
      SCH_JOB_ELOG("empty level plan, level:%d", i);
D
dapan1121 已提交
326
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
327 328
    }

D
dapan1121 已提交
329 330 331
    taskNum = (int32_t)taosArrayGetSize(plans);
    if (taskNum <= 0) {
      SCH_JOB_ELOG("invalid level plan number:%d, level:%d", taskNum, i);
D
dapan1121 已提交
332
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
333 334
    }

D
dapan1121 已提交
335
    pLevel->taskNum = taskNum;
D
dapan1121 已提交
336
    
D
dapan1121 已提交
337
    pLevel->subTasks = taosArrayInit(taskNum, sizeof(SSchTask));
D
dapan1121 已提交
338
    if (NULL == pLevel->subTasks) {
D
dapan1121 已提交
339
      SCH_JOB_ELOG("taosArrayInit %d failed", taskNum);
D
dapan1121 已提交
340
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
341 342
    }
    
D
dapan1121 已提交
343 344
    for (int32_t n = 0; n < taskNum; ++n) {
      SSubplan *plan = taosArrayGetP(plans, n);
D
dapan 已提交
345

D
dapan1121 已提交
346 347 348 349 350
      SCH_SET_JOB_TYPE(&pJob->attr, plan->type);

      SSchTask  task = {0};
      SSchTask *pTask = &task;
      
D
dapan1121 已提交
351
      SCH_ERR_JRET(schInitTask(pJob, &task, plan, pLevel));
D
dapan1121 已提交
352
      
D
dapan1121 已提交
353
      void *p = taosArrayPush(pLevel->subTasks, &task);
D
dapan1121 已提交
354
      if (NULL == p) {
D
dapan1121 已提交
355
        SCH_TASK_ELOG("taosArrayPush task to level failed, level:%d, taskIdx:%d", pLevel->level, n);
D
dapan1121 已提交
356 357 358 359
        SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
      
      if (0 != taosHashPut(planToTask, &plan, POINTER_BYTES, &p, POINTER_BYTES)) {
D
dapan1121 已提交
360
        SCH_TASK_ELOG("taosHashPut to planToTaks failed, taskIdx:%d", n);
D
dapan1121 已提交
361 362
        SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
363

D
dapan1121 已提交
364 365
      SCH_TASK_DLOG("task initialized, level:%d", pLevel->level);
    }
D
dapan1121 已提交
366

D
dapan1121 已提交
367
    SCH_JOB_DLOG("level initialized, taskNum:%d", taskNum);
D
dapan1121 已提交
368
  }
D
dapan1121 已提交
369 370

  SCH_ERR_JRET(schBuildTaskRalation(pJob, planToTask));
D
dapan1121 已提交
371 372

_return:
D
dapan1121 已提交
373 374 375 376
  if (planToTask) {
    taosHashCleanup(planToTask);
  }

D
dapan1121 已提交
377
  SCH_RET(code);
378 379
}

D
dapan1121 已提交
380 381
int32_t schSetTaskCandidateAddrs(SSchJob *pJob, SSchTask *pTask) {
  if (NULL != pTask->candidateAddrs) {
D
dapan 已提交
382 383 384
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
385 386 387 388
  pTask->candidateIdx = 0;
  pTask->candidateAddrs = taosArrayInit(SCH_MAX_CONDIDATE_EP_NUM, sizeof(SQueryNodeAddr));
  if (NULL == pTask->candidateAddrs) {
    SCH_TASK_ELOG("taosArrayInit %d condidate addrs failed", SCH_MAX_CONDIDATE_EP_NUM);
D
dapan1121 已提交
389 390 391
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
392 393 394
  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 已提交
395 396 397
      SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

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

D
dapan1121 已提交
400 401 402 403
    return TSDB_CODE_SUCCESS;
  }

  int32_t addNum = 0;
D
dapan 已提交
404
  int32_t nodeNum = 0;
405
  if (pJob->nodeList) {
D
dapan 已提交
406
    nodeNum = taosArrayGetSize(pJob->nodeList);
D
dapan1121 已提交
407
    
D
dapan 已提交
408 409 410 411 412 413 414
    for (int32_t i = 0; i < nodeNum && addNum < SCH_MAX_CONDIDATE_EP_NUM; ++i) {
      SQueryNodeAddr *naddr = taosArrayGet(pJob->nodeList, i);
      
      if (NULL == taosArrayPush(pTask->candidateAddrs, &pTask->plan->execNode)) {
        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 已提交
415 416

      ++addNum;
D
dapan1121 已提交
417
    }
D
dapan1121 已提交
418 419
  }

D
dapan1121 已提交
420 421 422 423 424
  if (addNum <= 0) {
    SCH_TASK_ELOG("no available execNode as condidate addr, nodeNum:%d", nodeNum);
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
425 426
/*
  for (int32_t i = 0; i < job->dataSrcEps.numOfEps && addNum < SCH_MAX_CONDIDATE_EP_NUM; ++i) {
D
dapan1121 已提交
427 428 429 430
    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 已提交
431
  }
D
dapan1121 已提交
432
*/
D
dapan 已提交
433 434

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
435
}
D
dapan1121 已提交
436

D
dapan1121 已提交
437
int32_t schPushTaskToExecList(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
438 439 440
  int32_t code = taosHashPut(pJob->execTasks, &pTask->taskId, sizeof(pTask->taskId), &pTask, POINTER_BYTES);
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
441
      SCH_TASK_ELOG("task already in execTask list, code:%x", code);
D
dapan1121 已提交
442 443 444
      SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
    }
    
D
dapan1121 已提交
445
    SCH_TASK_ELOG("taosHashPut task to execTask list failed, errno:%d", errno);
D
dapan 已提交
446 447 448
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

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

D
dapan 已提交
451 452 453
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
454 455 456
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 已提交
457 458
  }

D
dapan1121 已提交
459 460 461 462 463 464 465 466 467 468
  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 已提交
469 470 471 472
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  *moved = true;
D
dapan1121 已提交
473 474

  SCH_TASK_DLOG("task moved to succTask list, numOfTasks:%d", taosHashGetSize(pJob->succTasks));
D
dapan1121 已提交
475 476 477 478
  
  return TSDB_CODE_SUCCESS;
}

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

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

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

  SCH_TASK_DLOG("task moved to failTask list, numOfTasks:%d", taosHashGetSize(pJob->failTasks));
D
dapan 已提交
502 503 504 505
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

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 已提交
533
int32_t schTaskCheckAndSetRetry(SSchJob *job, SSchTask *task, int32_t errCode, bool *needRetry) {
D
dapan1121 已提交
534 535
  // 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 已提交
536 537
  // TODO set condidateIdx
  // TODO record failed but tried task
D
dapan 已提交
538

D
dapan1121 已提交
539 540 541
  *needRetry = false;

  return TSDB_CODE_SUCCESS;
D
dapan 已提交
542 543
}

D
dapan 已提交
544

D
dapan1121 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

// Note: no more error processing, handled in function internal
int32_t schProcessOnJobFailure(SSchJob *pJob, int32_t errCode) {
  // if already FAILED, no more processing
  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_FAILED));
  
  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 已提交
566
// Note: no more error processing, handled in function internal
D
dapan1121 已提交
567
int32_t schFetchFromRemote(SSchJob *pJob) {
D
dapan 已提交
568 569
  int32_t code = 0;
  
D
dapan1121 已提交
570 571
  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 已提交
572 573 574
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
575 576 577 578 579 580 581
  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 已提交
582 583

  SCH_ERR_JRET(schBuildAndSendMsg(pJob, pJob->fetchTask, &pJob->resNode, TDMT_VND_FETCH));
D
dapan 已提交
584 585 586 587

  return TSDB_CODE_SUCCESS;
  
_return:
D
dapan1121 已提交
588 589

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

D
dapan1121 已提交
591 592
  schProcessOnJobFailure(pJob, code);

D
dapan 已提交
593
  return code;
D
dapan 已提交
594 595
}

D
dapan 已提交
596

D
dapan1121 已提交
597 598 599
// Note: no more error processing, handled in function internal
int32_t schProcessOnJobPartialSuccess(SSchJob *pJob) {
  int32_t code = 0;
D
dapan 已提交
600
  
D
dapan1121 已提交
601
  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_PARTIAL_SUCCEED));
D
dapan 已提交
602

D
dapan1121 已提交
603
  if ((!SCH_JOB_NEED_FETCH(&pJob->attr)) && pJob->attr.syncSchedule) {
D
dapan 已提交
604
    tsem_post(&pJob->rspSem);
D
dapan 已提交
605
  }
D
dapan1121 已提交
606 607 608 609
  
  if (atomic_load_8(&pJob->userFetch)) {
    SCH_ERR_JRET(schFetchFromRemote(pJob));
  }
D
dapan 已提交
610

D
dapan 已提交
611
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
612 613 614 615 616 617

_return:

  SCH_ERR_RET(schProcessOnJobFailure(pJob, code));

  SCH_RET(code);
D
dapan 已提交
618 619
}

D
dapan1121 已提交
620

D
dapan1121 已提交
621
int32_t schProcessOnDataFetched(SSchJob *job) {
D
dapan 已提交
622 623 624
  atomic_val_compare_exchange_32(&job->remoteFetch, 1, 0);

  tsem_post(&job->rspSem);
D
dapan 已提交
625 626 627
}


D
dapan1121 已提交
628 629 630 631

// Note: no more error processing, handled in function internal
int32_t schProcessOnTaskFailure(SSchJob *pJob, SSchTask *pTask, int32_t errCode) {
  bool needRetry = false;
D
dapan 已提交
632
  bool moved = false;
D
dapan1121 已提交
633
  int32_t taskDone = 0;
D
dapan1121 已提交
634
  int32_t code = 0;
D
dapan1121 已提交
635 636

  SCH_TASK_DLOG("taskOnFailure, code:%x", errCode);
D
dapan 已提交
637
  
D
dapan1121 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
  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 已提交
669 670
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
671

D
dapan1121 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
_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 已提交
693
  SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_PARTIAL_SUCCEED);
D
dapan1121 已提交
694 695

  SCH_ERR_JRET(schRecordTaskSucceedNode(pTask));
D
dapan 已提交
696
  
D
dapan1121 已提交
697
  int32_t parentNum = pTask->parents ? (int32_t)taosArrayGetSize(pTask->parents) : 0;
D
dapan 已提交
698
  if (parentNum == 0) {
D
dapan1121 已提交
699 700
    int32_t taskDone = 0;
    
D
dapan1121 已提交
701 702 703 704 705
    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 已提交
706
      
D
dapan1121 已提交
707 708
      if (taskDone < pTask->level->taskNum) {
        SCH_TASK_ELOG("wait all tasks, done:%d, all:%d", taskDone, pTask->level->taskNum);
D
dapan1121 已提交
709
        
D
dapan1121 已提交
710
        return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
711 712
      } else if (taskDone > pTask->level->taskNum) {
        assert(0);
D
dapan1121 已提交
713 714
      }

D
dapan1121 已提交
715
      if (pTask->level->taskFailed > 0) {
D
dapan1121 已提交
716 717 718
        SCH_RET(schProcessOnJobFailure(pJob, 0));
      } else {
        SCH_RET(schProcessOnJobPartialSuccess(pJob));
D
dapan1121 已提交
719 720
      }
    } else {
D
dapan1121 已提交
721
      pJob->resNode = pTask->succeedAddr;
D
dapan1121 已提交
722
    }
D
dapan 已提交
723

D
dapan1121 已提交
724
    pJob->fetchTask = pTask;
D
dapan1121 已提交
725 726 727 728 729

    code = schMoveTaskToExecList(pJob, pTask, &moved);
    if (code && moved) {
      SCH_ERR_RET(code);
    }
D
dapan1121 已提交
730
    
D
dapan1121 已提交
731
    SCH_ERR_RET(schProcessOnJobPartialSuccess(pJob));
D
dapan 已提交
732 733 734 735

    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
736
/*
D
dapan 已提交
737
  if (SCH_IS_DATA_SRC_TASK(task) && job->dataSrcEps.numOfEps < SCH_MAX_CONDIDATE_EP_NUM) {
D
dapan 已提交
738 739 740 741 742
    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 已提交
743
*/
D
dapan 已提交
744

D
dapan 已提交
745
  for (int32_t i = 0; i < parentNum; ++i) {
D
dapan1121 已提交
746
    SSchTask *par = *(SSchTask **)taosArrayGet(pTask->parents, i);
D
dapan1121 已提交
747 748
    pErrTask = par;
    
D
dapan1121 已提交
749
    atomic_add_fetch_32(&par->childReady, 1);
D
dapan 已提交
750

D
dapan1121 已提交
751
    SCH_LOCK(SCH_WRITE, &par->lock);
D
dapan1121 已提交
752
    qSetSubplanExecutionNode(par->plan, pTask->plan->id.templateId, &pTask->succeedAddr);
D
dapan1121 已提交
753
    SCH_UNLOCK(SCH_WRITE, &par->lock);
D
dapan 已提交
754 755
    
    if (SCH_TASK_READY_TO_LUNCH(par)) {
D
dapan1121 已提交
756
      SCH_ERR_RET(schLaunchTask(pJob, par));
D
dapan 已提交
757 758 759 760 761
    }
  }

  return TSDB_CODE_SUCCESS;

D
dapan1121 已提交
762
_return:
D
dapan 已提交
763

D
dapan1121 已提交
764
  SCH_ERR_RET(schProcessOnTaskFailure(pJob, pErrTask, code));
D
dapan 已提交
765

D
dapan1121 已提交
766
  SCH_ERR_RET(code);
D
dapan 已提交
767 768
}

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

D
dapan1121 已提交
772 773
  SCH_ERR_JRET(schValidateTaskReceivedMsgType(pJob, pTask, msgType));

D
dapan1121 已提交
774
  switch (msgType) {
H
Haojun Liao 已提交
775
    case TDMT_VND_CREATE_TABLE_RSP: {
D
dapan1121 已提交
776 777
        if (rspCode != TSDB_CODE_SUCCESS) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
H
Haojun Liao 已提交
778
        }
D
dapan1121 已提交
779 780
        
        SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
781

D
dapan1121 已提交
782 783
        break;
      }
D
dapan1121 已提交
784
    case TDMT_VND_SUBMIT_RSP: {
D
dapan1121 已提交
785 786 787 788 789 790 791 792 793 794 795
        #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 已提交
796
        }
D
dapan1121 已提交
797 798 799 800 801

        SShellSubmitRsp *rsp = (SShellSubmitRsp *)msg;
        if (rsp) {
          pJob->resNumOfRows += rsp->affectedRows;
        }
D
dapan1121 已提交
802 803 804 805
        #endif

        SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));

D
dapan1121 已提交
806
        break;
D
dapan1121 已提交
807
      }
D
dapan1121 已提交
808
    case TDMT_VND_QUERY_RSP: {
D
dapan1121 已提交
809 810
        SQueryTableRsp *rsp = (SQueryTableRsp *)msg;
        
D
dapan1121 已提交
811
        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg || rsp->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
812
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
D
dapan1121 已提交
813
        }
D
dapan1121 已提交
814 815 816
        
        SCH_ERR_JRET(schBuildAndSendMsg(pJob, pTask, NULL, TDMT_VND_RES_READY));
        
D
dapan1121 已提交
817 818
        break;
      }
D
dapan1121 已提交
819
    case TDMT_VND_RES_READY_RSP: {
D
dapan1121 已提交
820 821
        SResReadyRsp *rsp = (SResReadyRsp *)msg;
        
D
dapan1121 已提交
822 823
        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg || rsp->code != TSDB_CODE_SUCCESS) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rsp->code));
D
dapan1121 已提交
824
        }
D
dapan1121 已提交
825 826 827
        
        SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));
        
D
dapan1121 已提交
828 829
        break;
      }
D
dapan1121 已提交
830
    case TDMT_VND_FETCH_RSP: {
D
dapan1121 已提交
831 832
        SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg;

D
dapan1121 已提交
833 834
        if (rspCode != TSDB_CODE_SUCCESS || NULL == msg) {
          SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode));
D
dapan1121 已提交
835
        }
D
dapan1121 已提交
836 837 838 839 840

        atomic_store_ptr(&pJob->res, rsp);
        atomic_store_32(&pJob->resNumOfRows, rsp->numOfRows);
        
        SCH_ERR_JRET(schProcessOnDataFetched(pJob));
D
dapan1121 已提交
841 842
        
        break;
D
dapan1121 已提交
843
      }
D
dapan1121 已提交
844
    case TDMT_VND_DROP_TASK: {
D
dapan1121 已提交
845 846 847
        // SHOULD NEVER REACH HERE
        assert(0);
        break;
D
dapan1121 已提交
848
      }
D
dapan1121 已提交
849
    default:
D
dapan1121 已提交
850 851 852
      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 已提交
853 854 855 856 857
  }

  return TSDB_CODE_SUCCESS;

_return:
D
dapan1121 已提交
858 859 860 861

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

D
dapan 已提交
864

D
dapan1121 已提交
865
int32_t schHandleCallback(void* param, const SDataBuf* pMsg, int32_t msgType, int32_t rspCode) {
D
dapan1121 已提交
866 867
  int32_t code = 0;
  SSchCallbackParam *pParam = (SSchCallbackParam *)param;
D
dapan1121 已提交
868 869
  SSchJob *pJob = NULL;
  SSchTask *pTask = NULL;
D
dapan1121 已提交
870
  
D
dapan1121 已提交
871 872
  SSchJob **job = taosHashGet(schMgmt.jobs, &pParam->queryId, sizeof(pParam->queryId));
  if (NULL == job || NULL == (*job)) {
D
dapan1121 已提交
873
    qError("QID:%"PRIx64" taosHashGet queryId not exist, may be dropped", pParam->queryId);
D
dapan1121 已提交
874 875 876
    SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }

D
dapan1121 已提交
877 878 879 880 881
  pJob = *job;

  atomic_add_fetch_32(&pJob->ref, 1);

  int32_t s = taosHashGetSize(pJob->execTasks);
D
dapan1121 已提交
882 883 884 885
  if (s <= 0) {
    qError("QID:%"PRIx64",TID:%"PRIx64" no task in execTask list", pParam->queryId, pParam->taskId);
    SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }
H
Haojun Liao 已提交
886

D
dapan1121 已提交
887
  SSchTask **task = taosHashGet(pJob->execTasks, &pParam->taskId, sizeof(pParam->taskId));
D
dapan1121 已提交
888
  if (NULL == task || NULL == (*task)) {
D
dapan1121 已提交
889
    qError("QID:%"PRIx64",TID:%"PRIx64" taosHashGet taskId not exist", pParam->queryId, pParam->taskId);
D
dapan1121 已提交
890 891
    SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }
D
dapan1121 已提交
892 893 894 895

  pTask = *task;

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

H
Haojun Liao 已提交
899
_return:
D
dapan1121 已提交
900 901 902 903 904

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

D
dapan1121 已提交
905 906 907 908
  tfree(param);
  SCH_RET(code);
}

D
dapan1121 已提交
909
int32_t schHandleSubmitCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
910 911
  return schHandleCallback(param, pMsg, TDMT_VND_SUBMIT_RSP, code);
}
H
Haojun Liao 已提交
912

D
dapan1121 已提交
913
int32_t schHandleCreateTableCallback(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
914 915 916
  return schHandleCallback(param, pMsg, TDMT_VND_CREATE_TABLE_RSP, code);
}

D
dapan1121 已提交
917
int32_t schHandleQueryCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
918 919
  return schHandleCallback(param, pMsg, TDMT_VND_QUERY_RSP, code);
}
H
Haojun Liao 已提交
920

D
dapan1121 已提交
921
int32_t schHandleFetchCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
922 923
  return schHandleCallback(param, pMsg, TDMT_VND_FETCH_RSP, code);
}
H
Haojun Liao 已提交
924

D
dapan1121 已提交
925
int32_t schHandleReadyCallback(void* param, const SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
926 927
  return schHandleCallback(param, pMsg, TDMT_VND_RES_READY_RSP, code);
}
H
Haojun Liao 已提交
928

D
dapan1121 已提交
929
int32_t schHandleDropCallback(void* param, const SDataBuf* pMsg, int32_t code) {  
D
dapan1121 已提交
930
  SSchCallbackParam *pParam = (SSchCallbackParam *)param;
D
dapan1121 已提交
931
  qDebug("QID:%"PRIx64",TID:%"PRIx64" drop task rsp received, code:%x", pParam->queryId, pParam->taskId, code);
D
dapan1121 已提交
932 933
}

D
dapan1121 已提交
934
int32_t schGetCallbackFp(int32_t msgType, __async_send_cb_fn_t *fp) {
D
dapan1121 已提交
935
  switch (msgType) {
H
Haojun Liao 已提交
936 937 938
    case TDMT_VND_CREATE_TABLE:
      *fp = schHandleCreateTableCallback;
      break;
D
dapan1121 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
    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 已提交
955
      qError("unknown msg type for callback, msgType:%d", msgType);
D
dapan1121 已提交
956 957 958 959 960 961 962
      SCH_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
963
int32_t schAsyncSendMsg(void *transport, SEpSet* epSet, uint64_t qId, uint64_t tId, int32_t msgType, void *msg, uint32_t msgSize) {
D
dapan1121 已提交
964 965 966
  int32_t code = 0;
  SMsgSendInfo* pMsgSendInfo = calloc(1, sizeof(SMsgSendInfo));
  if (NULL == pMsgSendInfo) {
D
dapan 已提交
967
    qError("QID:%"PRIx64 ",TID:%"PRIx64 " calloc %d failed", qId, tId, (int32_t)sizeof(SMsgSendInfo));
D
dapan1121 已提交
968 969 970 971 972
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  SSchCallbackParam *param = calloc(1, sizeof(SSchCallbackParam));
  if (NULL == param) {
D
dapan 已提交
973
    qError("QID:%"PRIx64 ",TID:%"PRIx64 " calloc %d failed", qId, tId, (int32_t)sizeof(SSchCallbackParam));
D
dapan1121 已提交
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
    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 已提交
990
  
D
dapan1121 已提交
991 992 993 994 995
  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 已提交
996 997 998 999
  
  return TSDB_CODE_SUCCESS;

_return:
D
dapan 已提交
1000
  
D
dapan1121 已提交
1001 1002 1003 1004 1005 1006
  tfree(param);
  tfree(pMsgSendInfo);

  SCH_RET(code);
}

D
dapan1121 已提交
1007
void schConvertAddrToEpSet(SQueryNodeAddr *addr, SEpSet *epSet) {
D
dapan1121 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016
  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 已提交
1017
int32_t schBuildAndSendMsg(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr, int32_t msgType) {
D
dapan1121 已提交
1018 1019 1020
  uint32_t msgSize = 0;
  void *msg = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1021
  bool isCandidateAddr = false;
D
dapan 已提交
1022
  SEpSet epSet;
D
dapan1121 已提交
1023 1024 1025 1026 1027 1028 1029

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

D
dapan 已提交
1030
  schConvertAddrToEpSet(addr, &epSet);
D
dapan1121 已提交
1031 1032
  
  switch (msgType) {
H
Haojun Liao 已提交
1033
    case TDMT_VND_CREATE_TABLE:
D
dapan1121 已提交
1034
    case TDMT_VND_SUBMIT: {
D
dapan1121 已提交
1035 1036
      msgSize = pTask->msgLen;
      msg = pTask->msg;
D
dapan1121 已提交
1037 1038
      break;
    }
1039

D
dapan1121 已提交
1040
    case TDMT_VND_QUERY: {
D
dapan1121 已提交
1041
      msgSize = sizeof(SSubQueryMsg) + pTask->msgLen;
D
dapan1121 已提交
1042 1043
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1044
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1045 1046 1047 1048
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }

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

D
dapan 已提交
1050 1051
      pMsg->header.vgId = htonl(addr->nodeId);
      
D
dapan1121 已提交
1052
      pMsg->sId = htobe64(schMgmt.sId);
D
dapan1121 已提交
1053 1054 1055 1056
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);
      pMsg->contentLen = htonl(pTask->msgLen);
      memcpy(pMsg->msg, pTask->msg, pTask->msgLen);
D
dapan1121 已提交
1057
      break;
1058 1059
    }

D
dapan1121 已提交
1060
    case TDMT_VND_RES_READY: {
S
Shengliang Guan 已提交
1061
      msgSize = sizeof(SResReadyReq);
D
dapan1121 已提交
1062 1063
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1064
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1065 1066 1067
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }

S
Shengliang Guan 已提交
1068
      SResReadyReq *pMsg = msg;
D
dapan1121 已提交
1069
      
D
dapan 已提交
1070 1071
      pMsg->header.vgId = htonl(addr->nodeId);  
      
D
dapan1121 已提交
1072
      pMsg->sId = htobe64(schMgmt.sId);      
D
dapan1121 已提交
1073 1074
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);      
D
dapan1121 已提交
1075 1076 1077
      break;
    }
    case TDMT_VND_FETCH: {
S
Shengliang Guan 已提交
1078
      msgSize = sizeof(SResFetchReq);
D
dapan1121 已提交
1079 1080
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1081
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1082 1083 1084
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
    
S
Shengliang Guan 已提交
1085
      SResFetchReq *pMsg = msg;
D
dapan1121 已提交
1086
      
D
dapan 已提交
1087 1088
      pMsg->header.vgId = htonl(addr->nodeId);  
      
D
dapan1121 已提交
1089
      pMsg->sId = htobe64(schMgmt.sId);      
D
dapan1121 已提交
1090 1091
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);      
D
dapan1121 已提交
1092 1093 1094
      break;
    }
    case TDMT_VND_DROP_TASK:{
S
Shengliang Guan 已提交
1095
      msgSize = sizeof(STaskDropReq);
D
dapan1121 已提交
1096 1097
      msg = calloc(1, msgSize);
      if (NULL == msg) {
D
dapan 已提交
1098
        SCH_TASK_ELOG("calloc %d failed", msgSize);
D
dapan1121 已提交
1099 1100 1101
        SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
    
S
Shengliang Guan 已提交
1102
      STaskDropReq *pMsg = msg;
D
dapan1121 已提交
1103
      
D
dapan 已提交
1104 1105
      pMsg->header.vgId = htonl(addr->nodeId);   
      
D
dapan1121 已提交
1106
      pMsg->sId = htobe64(schMgmt.sId);      
D
dapan1121 已提交
1107 1108
      pMsg->queryId = htobe64(pJob->queryId);
      pMsg->taskId = htobe64(pTask->taskId);      
D
dapan1121 已提交
1109 1110 1111
      break;
    }
    default:
D
dapan1121 已提交
1112
      SCH_TASK_ELOG("unknown msg type to send, msgType:%d", msgType);
D
dapan1121 已提交
1113 1114 1115 1116
      SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
      break;
  }

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

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

D
dapan1121 已提交
1121 1122 1123 1124
  if (isCandidateAddr) {
    SCH_ERR_RET(schRecordTaskExecNode(pJob, pTask, addr));
  }
  
D
dapan1121 已提交
1125 1126 1127 1128
  return TSDB_CODE_SUCCESS;

_return:

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

D
dapan1121 已提交
1131 1132 1133
  tfree(msg);
  SCH_RET(code);
}
D
dapan1121 已提交
1134

D
dapan1121 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143
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 已提交
1144

D
dapan1121 已提交
1145 1146

// Note: no more error processing, handled in function internal
D
dapan1121 已提交
1147
int32_t schLaunchTask(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
1148 1149 1150 1151 1152
  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 已提交
1153 1154 1155 1156 1157
    
    code = atomic_load_32(&pJob->errCode);
    SCH_ERR_RET(code);
    
    SCH_RET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
1158
  }
D
dapan1121 已提交
1159
  
D
dapan1121 已提交
1160
  SSubplan *plan = pTask->plan;
D
dapan1121 已提交
1161

D
dapan1121 已提交
1162 1163 1164 1165 1166 1167
  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);
    }
D
dapan1121 已提交
1168
  }
D
dapan1121 已提交
1169 1170
  
  SCH_ERR_JRET(schSetTaskCandidateAddrs(pJob, pTask));
D
dapan1121 已提交
1171

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

D
dapan 已提交
1176 1177
    SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_EXECUTING);
  }
D
dapan1121 已提交
1178

D
dapan1121 已提交
1179
  SCH_ERR_JRET(schBuildAndSendMsg(pJob, pTask, NULL, plan->msgType));
D
dapan1121 已提交
1180
  
D
dapan1121 已提交
1181
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1182 1183 1184

_return:

D
dapan1121 已提交
1185
  SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, code));
D
dapan1121 已提交
1186 1187
  
  SCH_RET(code);
D
dapan1121 已提交
1188 1189
}

D
dapan1121 已提交
1190 1191
int32_t schLaunchJob(SSchJob *pJob) {
  SSchLevel *level = taosArrayGet(pJob->levels, pJob->levelIdx);
D
dapan1121 已提交
1192 1193

  SCH_ERR_RET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_EXECUTING));
D
dapan1121 已提交
1194
  
D
dapan 已提交
1195
  for (int32_t i = 0; i < level->taskNum; ++i) {
D
dapan1121 已提交
1196 1197
    SSchTask *pTask = taosArrayGet(level->subTasks, i);
    SCH_ERR_RET(schLaunchTask(pJob, pTask));
D
dapan1121 已提交
1198
  }
D
dapan 已提交
1199
  
D
dapan1121 已提交
1200
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1201 1202
}

D
dapan1121 已提交
1203 1204 1205 1206 1207
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 已提交
1208

D
dapan1121 已提交
1209 1210 1211
  int32_t size = (int32_t)taosArrayGetSize(pTask->execAddrs);
  
  if (size <= 0) {
D
dapan1121 已提交
1212
    SCH_TASK_DLOG("task has no exec address, no need to drop it, status:%d", SCH_GET_TASK_STATUS(pTask));
D
dapan1121 已提交
1213 1214
    return;
  }
H
Haojun Liao 已提交
1215

D
dapan1121 已提交
1216 1217 1218
  SQueryNodeAddr *addr = NULL;
  for (int32_t i = 0; i < size; ++i) {
    addr = (SQueryNodeAddr *)taosArrayGet(pTask->execAddrs, i);
D
dapan1121 已提交
1219

D
dapan1121 已提交
1220 1221
    schBuildAndSendMsg(pJob, pTask, addr, TDMT_VND_DROP_TASK);
  }
D
dapan1121 已提交
1222 1223

  SCH_TASK_DLOG("task has %d exec address", size);
D
dapan1121 已提交
1224 1225 1226 1227
}

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

D
dapan1121 已提交
1231 1232
    if (!SCH_TASK_NO_NEED_DROP(pTask)) {
      schDropTaskOnExecutedNode(pJob, pTask);
H
Haojun Liao 已提交
1233
    }
D
dapan1121 已提交
1234 1235 1236 1237
    
    pIter = taosHashIterate(list, pIter);
  } 
}
H
Haojun Liao 已提交
1238

D
dapan1121 已提交
1239 1240 1241 1242
void schDropJobAllTasks(SSchJob *pJob) {
  schDropTaskInHashList(pJob, pJob->execTasks);
  schDropTaskInHashList(pJob, pJob->succTasks);
  schDropTaskInHashList(pJob, pJob->failTasks);
D
dapan1121 已提交
1243
}
1244

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

D
dapan1121 已提交
1250
  int32_t code = 0;
D
dapan1121 已提交
1251 1252
  SSchJob *pJob = calloc(1, sizeof(SSchJob));
  if (NULL == pJob) {
D
dapan1121 已提交
1253
    qError("QID:%"PRIx64" calloc %d failed", pDag->queryId, (int32_t)sizeof(SSchJob));
D
dapan1121 已提交
1254
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
1255 1256
  }

D
dapan1121 已提交
1257 1258 1259
  pJob->attr.syncSchedule = syncSchedule;
  pJob->transport = transport;
  pJob->nodeList = nodeList;
D
dapan 已提交
1260

D
dapan1121 已提交
1261
  SCH_ERR_JRET(schValidateAndBuildJob(pDag, pJob));
D
dapan1121 已提交
1262

D
dapan1121 已提交
1263 1264 1265
  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 已提交
1266 1267 1268
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
1269 1270 1271
  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 已提交
1272 1273
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan 已提交
1274

D
dapan1121 已提交
1275 1276 1277
  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 已提交
1278 1279 1280
    SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

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

D
dapan1121 已提交
1283
  code = taosHashPut(schMgmt.jobs, &pJob->queryId, sizeof(pJob->queryId), &pJob, POINTER_BYTES);
D
dapan1121 已提交
1284 1285
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
1286
      SCH_JOB_ELOG("job already exist, isQueryJob:%d", pJob->attr.queryJob);
D
dapan1121 已提交
1287 1288
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
    } else {
D
dapan1121 已提交
1289 1290
      SCH_JOB_ELOG("taosHashPut job failed, errno:%d", errno);
      SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1291
    }
D
dapan1121 已提交
1292 1293
  }

D
dapan1121 已提交
1294
  pJob->status = JOB_TASK_STATUS_NOT_START;
D
dapan1121 已提交
1295
  
D
dapan1121 已提交
1296
  SCH_ERR_JRET(schLaunchJob(pJob));
1297

D
dapan1121 已提交
1298
  *(SSchJob **)job = pJob;
D
dapan1121 已提交
1299
  
D
dapan 已提交
1300
  if (syncSchedule) {
D
dapan1121 已提交
1301
    SCH_JOB_DLOG("will wait for rsp now, job status:%d", SCH_GET_JOB_STATUS(pJob));
D
dapan1121 已提交
1302
    tsem_wait(&pJob->rspSem);
D
dapan1121 已提交
1303 1304
  }

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

D
dapan1121 已提交
1307
  return TSDB_CODE_SUCCESS;
1308

D
dapan1121 已提交
1309
_return:
D
dapan1121 已提交
1310

D
dapan1121 已提交
1311 1312 1313
  *(SSchJob **)job = NULL;
  
  scheduleFreeJob(pJob);
D
dapan1121 已提交
1314 1315
  
  SCH_RET(code);
1316
}
D
dapan1121 已提交
1317

D
dapan1121 已提交
1318 1319 1320 1321 1322 1323 1324
int32_t schCancelJob(SSchJob *pJob) {
  //TODO

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

}

D
dapan1121 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

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 已提交
1348
  if (taosGetSystemUUID((char *)&schMgmt.sId, sizeof(schMgmt.sId))) {
D
dapan1121 已提交
1349 1350 1351 1352
    qError("generate schdulerId failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR);
  }

D
dapan1121 已提交
1353
  qInfo("scheduler %"PRIx64" initizlized, maxJob:%u", schMgmt.sId, schMgmt.cfg.maxJobNum);
D
dapan1121 已提交
1354 1355 1356 1357
  
  return TSDB_CODE_SUCCESS;
}

1358
int32_t scheduleExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob, SQueryResult *pRes) {
H
Haojun Liao 已提交
1359
  if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == pRes) {
D
dapan1121 已提交
1360 1361 1362
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

D
dapan1121 已提交
1363 1364
  SSchJob *job = NULL;

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

D
dapan1121 已提交
1367
  job = *pJob;
D
dapan1121 已提交
1368

D
dapan 已提交
1369
  pRes->code = atomic_load_32(&job->errCode);
D
dapan1121 已提交
1370 1371
  pRes->numOfRows = job->resNumOfRows;
  
D
dapan1121 已提交
1372 1373 1374
  return TSDB_CODE_SUCCESS;
}

1375
int32_t scheduleAsyncExecJob(void *transport, SArray *nodeList, SQueryDag* pDag, struct SSchJob** pJob) {
1376
  if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob) {
D
dapan1121 已提交
1377 1378 1379
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

H
Haojun Liao 已提交
1380
  SCH_ERR_RET(schExecJobImpl(transport, nodeList, pDag, pJob, false));
D
dapan1121 已提交
1381
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1382 1383
}

1384
int32_t schedulerConvertDagToTaskList(SQueryDag* pDag, SArray **pTasks) {
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 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
  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);
    }
    
    SSubQueryMsg *pMsg = msg;
    
    pMsg->header.vgId = htonl(tInfo.addr.nodeId);
    
    pMsg->sId = htobe64(schMgmt.sId);
    pMsg->queryId = htobe64(plan->id.queryId);
    pMsg->taskId = htobe64(atomic_add_fetch_64(&schMgmt.taskId, 1));
    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);
}


1461 1462
int32_t scheduleFetchRows(SSchJob *pJob, void** pData) {
  if (NULL == pJob || NULL == pData) {
D
dapan1121 已提交
1463
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan 已提交
1464
  }
D
dapan 已提交
1465
  int32_t code = 0;
D
dapan 已提交
1466

D
dapan1121 已提交
1467 1468 1469 1470 1471 1472
  int8_t status = SCH_GET_JOB_STATUS(pJob);
  if (status == JOB_TASK_STATUS_DROPPING) {
    SCH_JOB_ELOG("job is dropping, status:%d", status);
    return TSDB_CODE_SCH_STATUS_ERROR;
  }

D
dapan1121 已提交
1473 1474 1475 1476 1477
  atomic_add_fetch_32(&pJob->ref, 1);
  
  if (!SCH_JOB_NEED_FETCH(&pJob->attr)) {
    SCH_JOB_ELOG("no need to fetch data, status:%d", SCH_GET_JOB_STATUS(pJob));
    atomic_sub_fetch_32(&pJob->ref, 1);
D
dapan1121 已提交
1478 1479 1480
    SCH_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

D
dapan1121 已提交
1481 1482 1483 1484
  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));
    atomic_sub_fetch_32(&pJob->ref, 1);
    SCH_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan 已提交
1485 1486
  }

D
dapan1121 已提交
1487
  if (status == JOB_TASK_STATUS_FAILED) {
D
dapan1121 已提交
1488
    *pData = atomic_load_ptr(&pJob->res);
D
dapan1121 已提交
1489 1490 1491
    atomic_store_ptr(&pJob->res, NULL);
    SCH_ERR_JRET(atomic_load_32(&pJob->errCode));
  } else if (status == JOB_TASK_STATUS_SUCCEED) {
D
dapan1121 已提交
1492
    *pData = atomic_load_ptr(&pJob->res);
D
dapan1121 已提交
1493 1494 1495 1496
    atomic_store_ptr(&pJob->res, NULL);
    goto _return;
  } else if (status == JOB_TASK_STATUS_PARTIAL_SUCCEED) {
    SCH_ERR_JRET(schFetchFromRemote(pJob));
D
dapan 已提交
1497 1498
  }

D
dapan1121 已提交
1499
  tsem_wait(&pJob->rspSem);
D
dapan 已提交
1500

D
dapan1121 已提交
1501
  status = SCH_GET_JOB_STATUS(pJob);
D
dapan 已提交
1502

D
dapan1121 已提交
1503 1504
  if (status == JOB_TASK_STATUS_FAILED) {
    code = atomic_load_32(&pJob->errCode);
D
dapan1121 已提交
1505
    SCH_ERR_JRET(code);
D
dapan 已提交
1506 1507
  }
  
D
dapan1121 已提交
1508 1509
  if (pJob->res && ((SRetrieveTableRsp *)pJob->res)->completed) {
    SCH_ERR_JRET(schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_SUCCEED));
D
dapan 已提交
1510 1511
  }

D
dapan1121 已提交
1512
  while (true) {
D
dapan1121 已提交
1513
    *pData = atomic_load_ptr(&pJob->res);
D
dapan1121 已提交
1514
    
D
dapan1121 已提交
1515
    if (*pData != atomic_val_compare_exchange_ptr(&pJob->res, *pData, NULL)) {
D
dapan1121 已提交
1516 1517 1518 1519 1520
      continue;
    }

    break;
  }
D
dapan 已提交
1521

D
dapan 已提交
1522
_return:
D
dapan1121 已提交
1523 1524 1525 1526

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

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

D
dapan1121 已提交
1528
  SCH_RET(code);
D
dapan 已提交
1529
}
D
dapan1121 已提交
1530

D
dapan1121 已提交
1531 1532
int32_t scheduleCancelJob(void *job) {
  SSchJob *pJob = (SSchJob *)job;
D
dapan1121 已提交
1533

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

D
dapan1121 已提交
1536 1537 1538 1539 1540
  int32_t code = schCancelJob(pJob);

  atomic_sub_fetch_32(&pJob->ref, 1);

  SCH_RET(code);
D
dapan1121 已提交
1541 1542
}

D
dapan1121 已提交
1543 1544
void scheduleFreeJob(void *job) {
  if (NULL == job) {
D
dapan 已提交
1545 1546
    return;
  }
D
dapan1121 已提交
1547

D
dapan1121 已提交
1548
  SSchJob *pJob = job;
D
dapan1121 已提交
1549
  uint64_t queryId = pJob->queryId;
D
dapan1121 已提交
1550

D
dapan1121 已提交
1551 1552 1553 1554 1555
  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 已提交
1556

D
dapan1121 已提交
1557
    schCheckAndUpdateJobStatus(pJob, JOB_TASK_STATUS_DROPPING);
D
dapan1121 已提交
1558

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

D
dapan1121 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569
    while (true) {
      int32_t ref = atomic_load_32(&pJob->ref);
      if (0 == ref) {
        break;
      } else if (ref > 0) {
        usleep(1);
      } else {
        assert(0);
      }
D
dapan1121 已提交
1570
    }
D
dapan1121 已提交
1571

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

D
dapan1121 已提交
1574 1575 1576
    if (pJob->status == JOB_TASK_STATUS_EXECUTING) {
      schCancelJob(pJob);
    }
1577

D
dapan1121 已提交
1578 1579
    schDropJobAllTasks(pJob);
  }
D
dapan1121 已提交
1580 1581 1582 1583

  pJob->subPlans = NULL; // it is a reference to pDag->pSubplans
  
  int32_t numOfLevels = taosArrayGetSize(pJob->levels);
1584
  for(int32_t i = 0; i < numOfLevels; ++i) {
D
dapan1121 已提交
1585
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);
1586 1587 1588 1589

    int32_t numOfTasks = taosArrayGetSize(pLevel->subTasks);
    for(int32_t j = 0; j < numOfTasks; ++j) {
      SSchTask* pTask = taosArrayGet(pLevel->subTasks, j);
D
dapan1121 已提交
1590
      schFreeTask(pTask);
1591 1592 1593 1594
    }

    taosArrayDestroy(pLevel->subTasks);
  }
D
dapan1121 已提交
1595
  
D
dapan1121 已提交
1596 1597 1598
  taosHashCleanup(pJob->execTasks);
  taosHashCleanup(pJob->failTasks);
  taosHashCleanup(pJob->succTasks);
D
dapan1121 已提交
1599
  
D
dapan1121 已提交
1600 1601 1602
  taosArrayDestroy(pJob->levels);

  tfree(pJob->res);
D
dapan1121 已提交
1603
  
D
dapan1121 已提交
1604
  tfree(pJob);
D
dapan1121 已提交
1605 1606

  qDebug("QID:%"PRIx64" job freed", queryId);
D
dapan1121 已提交
1607
}
D
dapan1121 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621

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 已提交
1622 1623 1624 1625 1626 1627 1628 1629
  
void schedulerDestroy(void) {
  if (schMgmt.jobs) {
    taosHashCleanup(schMgmt.jobs); //TODO
    schMgmt.jobs = NULL;
  }
}