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

#include "catalog.h"
#include "command.h"
#include "query.h"
D
dapan1121 已提交
19
#include "schInt.h"
D
dapan1121 已提交
20 21 22 23
#include "tmsg.h"
#include "tref.h"
#include "trpc.h"

D
dapan1121 已提交
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
void schUpdateJobErrCode(SSchJob *pJob, int32_t errCode) {
  if (TSDB_CODE_SUCCESS == errCode) {
    return;
  }

  int32_t origCode = atomic_load_32(&pJob->errCode);
  if (TSDB_CODE_SUCCESS == origCode) {
    if (origCode == atomic_val_compare_exchange_32(&pJob->errCode, origCode, errCode)) {
      goto _return;
    }

    origCode = atomic_load_32(&pJob->errCode);
  }

  if (NEED_CLIENT_HANDLE_ERROR(origCode)) {
    return;
  }

  if (NEED_CLIENT_HANDLE_ERROR(errCode)) {
    atomic_store_32(&pJob->errCode, errCode);
    goto _return;
  }

  return;

_return:
50
  SCH_JOB_DLOG("job errCode updated to %s", tstrerror(errCode));
D
dapan1121 已提交
51 52
}

D
dapan1121 已提交
53 54
bool schJobDone(SSchJob *pJob) {
  int8_t status = SCH_GET_JOB_STATUS(pJob);
H
Hongze Cheng 已提交
55 56

  return (status == JOB_TASK_STATUS_FAIL || status == JOB_TASK_STATUS_DROP || status == JOB_TASK_STATUS_SUCC);
D
dapan1121 已提交
57
}
D
dapan1121 已提交
58

D
dapan1121 已提交
59 60 61 62 63 64
FORCE_INLINE bool schJobNeedToStop(SSchJob *pJob, int8_t *pStatus) {
  int8_t status = SCH_GET_JOB_STATUS(pJob);
  if (pStatus) {
    *pStatus = status;
  }

D
dapan1121 已提交
65 66 67 68
  if (schJobDone(pJob)) {
    return true;
  }

D
dapan1121 已提交
69
  if ((*pJob->chkKillFp)(pJob->chkKillParam)) {
D
dapan1121 已提交
70 71
    schUpdateJobErrCode(pJob, TSDB_CODE_TSC_QUERY_KILLED);
    return true;
D
dapan1121 已提交
72
  }
D
dapan1121 已提交
73 74

  return false;
D
dapan1121 已提交
75 76
}

D
dapan1121 已提交
77
int32_t schUpdateJobStatus(SSchJob *pJob, int8_t newStatus) {
D
dapan1121 已提交
78 79 80 81 82 83 84 85
  int32_t code = 0;

  int8_t oriStatus = 0;

  while (true) {
    oriStatus = SCH_GET_JOB_STATUS(pJob);

    if (oriStatus == newStatus) {
D
dapan1121 已提交
86 87 88 89
      if (JOB_TASK_STATUS_FETCH == newStatus) {
        return code;
      }
      
D
dapan1121 已提交
90
      SCH_ERR_JRET(TSDB_CODE_SCH_IGNORE_ERROR);
D
dapan1121 已提交
91 92 93 94
    }

    switch (oriStatus) {
      case JOB_TASK_STATUS_NULL:
D
dapan1121 已提交
95
        if (newStatus != JOB_TASK_STATUS_INIT) {
S
Shengliang Guan 已提交
96
          SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
97 98 99
        }

        break;
D
dapan1121 已提交
100 101
      case JOB_TASK_STATUS_INIT:
        if (newStatus != JOB_TASK_STATUS_EXEC && newStatus != JOB_TASK_STATUS_DROP) {
S
Shengliang Guan 已提交
102
          SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
103 104 105
        }

        break;
D
dapan1121 已提交
106 107 108
      case JOB_TASK_STATUS_EXEC:
        if (newStatus != JOB_TASK_STATUS_PART_SUCC && newStatus != JOB_TASK_STATUS_FAIL &&
            newStatus != JOB_TASK_STATUS_DROP) {
S
Shengliang Guan 已提交
109
          SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
110 111 112
        }

        break;
D
dapan1121 已提交
113 114
      case JOB_TASK_STATUS_PART_SUCC:
        if (newStatus != JOB_TASK_STATUS_FAIL && newStatus != JOB_TASK_STATUS_SUCC &&
D
dapan1121 已提交
115 116
            newStatus != JOB_TASK_STATUS_DROP && newStatus != JOB_TASK_STATUS_EXEC &&
            newStatus != JOB_TASK_STATUS_FETCH) {
S
Shengliang Guan 已提交
117
          SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
118 119
        }

D
dapan1121 已提交
120 121 122
        break;
      case JOB_TASK_STATUS_FETCH:
        if (newStatus != JOB_TASK_STATUS_FAIL && newStatus != JOB_TASK_STATUS_SUCC &&
D
dapan1121 已提交
123 124
            newStatus != JOB_TASK_STATUS_DROP && newStatus != JOB_TASK_STATUS_EXEC &&
            newStatus != JOB_TASK_STATUS_FETCH) {
D
dapan1121 已提交
125 126 127
          SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
        }
      
D
dapan1121 已提交
128
        break;
D
dapan1121 已提交
129 130 131
      case JOB_TASK_STATUS_SUCC:
      case JOB_TASK_STATUS_FAIL:
        if (newStatus != JOB_TASK_STATUS_DROP) {
S
Shengliang Guan 已提交
132
          SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
133 134 135
        }

        break;
D
dapan1121 已提交
136
      case JOB_TASK_STATUS_DROP:
D
dapan1121 已提交
137 138 139 140
        SCH_ERR_JRET(TSDB_CODE_QRY_JOB_FREED);

      default:
        SCH_JOB_ELOG("invalid job status:%s", jobTaskStatusStr(oriStatus));
S
Shengliang Guan 已提交
141
        SCH_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    }

    if (oriStatus != atomic_val_compare_exchange_8(&pJob->status, oriStatus, newStatus)) {
      continue;
    }

    SCH_JOB_DLOG("job status updated from %s to %s", jobTaskStatusStr(oriStatus), jobTaskStatusStr(newStatus));

    break;
  }

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
157 158 159 160 161
  if (TSDB_CODE_SCH_IGNORE_ERROR == code) {
    SCH_JOB_DLOG("ignore job status update, from %s to %s", jobTaskStatusStr(oriStatus), jobTaskStatusStr(newStatus));
  } else {
    SCH_JOB_ELOG("invalid job status update, from %s to %s", jobTaskStatusStr(oriStatus), jobTaskStatusStr(newStatus));
  }
D
dapan1121 已提交
162 163 164
  SCH_RET(code);
}

D
dapan1121 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
int32_t schBuildTaskRalation(SSchJob *pJob, SHashObj *planToTask) {
  for (int32_t i = 0; i < pJob->levelNum; ++i) {
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);

    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)LIST_LENGTH(pPlan->pChildren) : 0;
      int32_t   parentNum = pPlan->pParents ? (int32_t)LIST_LENGTH(pPlan->pParents) : 0;

      if (childNum > 0) {
        if (pJob->levelIdx == pLevel->level) {
          SCH_JOB_ELOG("invalid query plan, lowest level, childNum:%d", childNum);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

        pTask->children = taosArrayInit(childNum, POINTER_BYTES);
        if (NULL == pTask->children) {
          SCH_TASK_ELOG("taosArrayInit %d children failed", childNum);
S
Shengliang Guan 已提交
184
          SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
        }
      }

      for (int32_t n = 0; n < childNum; ++n) {
        SSubplan  *child = (SSubplan *)nodesListGetNode(pPlan->pChildren, n);
        SSchTask **childTask = taosHashGet(planToTask, &child, POINTER_BYTES);
        if (NULL == childTask || NULL == *childTask) {
          SCH_TASK_ELOG("subplan children relationship error, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

        if (NULL == taosArrayPush(pTask->children, childTask)) {
          SCH_TASK_ELOG("taosArrayPush childTask failed, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
S
Shengliang Guan 已提交
198
          SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
199 200
        }

D
dapan1121 已提交
201
        SCH_TASK_DLOG("children info, the %d child TID 0x%" PRIx64, n, (*childTask)->taskId);
D
dapan1121 已提交
202 203 204 205 206 207 208 209 210 211 212
      }

      if (parentNum > 0) {
        if (0 == pLevel->level) {
          SCH_TASK_ELOG("invalid task info, level:0, parentNum:%d", parentNum);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

        pTask->parents = taosArrayInit(parentNum, POINTER_BYTES);
        if (NULL == pTask->parents) {
          SCH_TASK_ELOG("taosArrayInit %d parents failed", parentNum);
S
Shengliang Guan 已提交
213
          SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        }
      } 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);
        }
      }

      for (int32_t n = 0; n < parentNum; ++n) {
        SSubplan  *parent = (SSubplan *)nodesListGetNode(pPlan->pParents, n);
        SSchTask **parentTask = taosHashGet(planToTask, &parent, POINTER_BYTES);
        if (NULL == parentTask || NULL == *parentTask) {
          SCH_TASK_ELOG("subplan parent relationship error, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
          SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
        }

        if (NULL == taosArrayPush(pTask->parents, parentTask)) {
          SCH_TASK_ELOG("taosArrayPush parentTask failed, level:%d, taskIdx:%d, childIdx:%d", i, m, n);
S
Shengliang Guan 已提交
232
          SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
233 234
        }

H
Hongze Cheng 已提交
235
        SCH_TASK_DLOG("parents info, the %d parent TID 0x%" PRIx64, n, (*parentTask)->taskId);
D
dapan1121 已提交
236 237 238 239 240 241 242
      }

      SCH_TASK_DLOG("level:%d, parentNum:%d, childNum:%d", i, parentNum, childNum);
    }
  }

  SSchLevel *pLevel = taosArrayGet(pJob->levels, 0);
D
dapan1121 已提交
243 244 245 246 247 248
  if (SCH_IS_QUERY_JOB(pJob)) {
    if (pLevel->taskNum > 1) {
      SCH_JOB_ELOG("invalid query plan, level:0, taskNum:%d", pLevel->taskNum);
      SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
    }

H
Hongze Cheng 已提交
249
    SSchTask *pTask = taosArrayGet(pLevel->subTasks, 0);
X
Xiaoyu Wang 已提交
250
    if (SUBPLAN_TYPE_MODIFY != pTask->plan->subplanType || EXPLAIN_MODE_DISABLE != pJob->attr.explainMode) {
D
dapan1121 已提交
251 252
      pJob->attr.needFetch = true;
    }
D
dapan1121 已提交
253 254 255 256 257
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
258
int32_t schAppendJobDataSrc(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
259
  if (!SCH_IS_DATA_BIND_QRY_TASK(pTask)) {
D
dapan1121 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    return TSDB_CODE_SUCCESS;
  }

  taosArrayPush(pJob->dataSrcTasks, &pTask);

  return TSDB_CODE_SUCCESS;
}

int32_t schValidateAndBuildJob(SQueryPlan *pDag, SSchJob *pJob) {
  int32_t code = 0;
  pJob->queryId = pDag->queryId;

  if (pDag->numOfSubplans <= 0) {
    SCH_JOB_ELOG("invalid subplan num:%d", pDag->numOfSubplans);
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  pJob->dataSrcTasks = taosArrayInit(pDag->numOfSubplans, POINTER_BYTES);
  if (NULL == pJob->dataSrcTasks) {
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
  if (levelNum <= 0) {
    SCH_JOB_ELOG("invalid level num:%d", levelNum);
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SHashObj *planToTask = taosHashInit(
D
dapan1121 已提交
289
      pDag->numOfSubplans,
D
dapan1121 已提交
290 291 292 293
      taosGetDefaultHashFunction(POINTER_BYTES == sizeof(int64_t) ? TSDB_DATA_TYPE_BIGINT : TSDB_DATA_TYPE_INT), false,
      HASH_NO_LOCK);
  if (NULL == planToTask) {
    SCH_JOB_ELOG("taosHashInit %d failed", SCHEDULE_DEFAULT_MAX_TASK_NUM);
S
Shengliang Guan 已提交
294
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
295 296 297 298 299
  }

  pJob->levels = taosArrayInit(levelNum, sizeof(SSchLevel));
  if (NULL == pJob->levels) {
    SCH_JOB_ELOG("taosArrayInit %d failed", levelNum);
S
Shengliang Guan 已提交
300
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
301 302 303
  }

  pJob->levelNum = levelNum;
D
dapan1121 已提交
304
  SCH_RESET_JOB_LEVEL_IDX(pJob);
D
dapan1121 已提交
305 306 307 308 309 310

  SSchLevel      level = {0};
  SNodeListNode *plans = NULL;
  int32_t        taskNum = 0;
  SSchLevel     *pLevel = NULL;

D
dapan1121 已提交
311
  level.status = JOB_TASK_STATUS_INIT;
D
dapan1121 已提交
312 313 314 315

  for (int32_t i = 0; i < levelNum; ++i) {
    if (NULL == taosArrayPush(pJob->levels, &level)) {
      SCH_JOB_ELOG("taosArrayPush level failed, level:%d", i);
S
Shengliang Guan 已提交
316
      SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    }

    pLevel = taosArrayGet(pJob->levels, i);
    pLevel->level = i;

    plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
    if (NULL == plans) {
      SCH_JOB_ELOG("empty level plan, level:%d", i);
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
    }

    taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
    if (taskNum <= 0) {
      SCH_JOB_ELOG("invalid level plan number:%d, level:%d", taskNum, i);
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
    }

    pLevel->taskNum = taskNum;

    pLevel->subTasks = taosArrayInit(taskNum, sizeof(SSchTask));
    if (NULL == pLevel->subTasks) {
      SCH_JOB_ELOG("taosArrayInit %d failed", taskNum);
S
Shengliang Guan 已提交
339
      SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
340 341 342 343 344 345 346 347
    }

    for (int32_t n = 0; n < taskNum; ++n) {
      SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);

      SCH_SET_JOB_TYPE(pJob, plan->subplanType);

      SSchTask  task = {0};
D
dapan1121 已提交
348 349
      SSchTask *pTask = taosArrayPush(pLevel->subTasks, &task);
      if (NULL == pTask) {
D
dapan1121 已提交
350
        SCH_TASK_ELOG("taosArrayPush task to level failed, level:%d, taskIdx:%d", pLevel->level, n);
S
Shengliang Guan 已提交
351
        SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
352 353
      }

D
dapan1121 已提交
354
      SCH_ERR_JRET(schInitTask(pJob, pTask, plan, pLevel));
355

D
dapan1121 已提交
356
      SCH_ERR_JRET(schAppendJobDataSrc(pJob, pTask));
D
dapan1121 已提交
357

D
dapan1121 已提交
358
      if (0 != taosHashPut(planToTask, &plan, POINTER_BYTES, &pTask, POINTER_BYTES)) {
D
dapan1121 已提交
359
        SCH_TASK_ELOG("taosHashPut to planToTaks failed, taskIdx:%d", n);
S
Shengliang Guan 已提交
360
        SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
361 362
      }

D
dapan1121 已提交
363 364
      if (0 != taosHashPut(pJob->taskList, &pTask->taskId, sizeof(pTask->taskId), &pTask, POINTER_BYTES)) {
        SCH_TASK_ELOG("taosHashPut to taskList failed, taskIdx:%d", n);
S
Shengliang Guan 已提交
365
        SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
366 367
      }

D
dapan1121 已提交
368 369 370
      ++pJob->taskNum;
    }

D
dapan1121 已提交
371
    SCH_JOB_DLOG("level %d initialized, taskNum:%d", i, taskNum);
D
dapan1121 已提交
372 373 374 375 376
  }

  SCH_ERR_JRET(schBuildTaskRalation(pJob, planToTask));

_return:
H
Hongze Cheng 已提交
377

D
dapan1121 已提交
378 379 380 381 382 383 384
  if (planToTask) {
    taosHashCleanup(planToTask);
  }

  SCH_RET(code);
}

H
Hongze Cheng 已提交
385
int32_t schDumpJobExecRes(SSchJob *pJob, SExecResult *pRes) {
D
dapan1121 已提交
386 387
  pRes->code = atomic_load_32(&pJob->errCode);
  pRes->numOfRows = pJob->resNumOfRows;
D
dapan1121 已提交
388 389
  pRes->res = pJob->execRes.res;
  pRes->msgType = pJob->execRes.msgType;
D
dapan1121 已提交
390
  pRes->numOfBytes = pJob->execRes.numOfBytes;
D
dapan1121 已提交
391
  pJob->execRes.res = NULL;
D
dapan1121 已提交
392

D
dapan1121 已提交
393 394
  SCH_JOB_DLOG("execRes dumped, code: %s", tstrerror(pRes->code));

D
dapan1121 已提交
395 396 397
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
398
int32_t schDumpJobFetchRes(SSchJob *pJob, void **pData) {
D
dapan1121 已提交
399
  int32_t code = 0;
H
Hongze Cheng 已提交
400

D
dapan1121 已提交
401 402 403
  SCH_LOCK(SCH_WRITE, &pJob->resLock);

  pJob->fetched = true;
H
Hongze Cheng 已提交
404

D
dapan1121 已提交
405 406
  if (pJob->fetchRes && ((SRetrieveTableRsp *)pJob->fetchRes)->completed) {
    SCH_ERR_JRET(schSwitchJobStatus(pJob, JOB_TASK_STATUS_SUCC, NULL));
D
dapan1121 已提交
407 408 409
  }

  while (true) {
D
dapan1121 已提交
410 411
    *pData = atomic_load_ptr(&pJob->fetchRes);
    if (*pData != atomic_val_compare_exchange_ptr(&pJob->fetchRes, *pData, NULL)) {
D
dapan1121 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
      continue;
    }

    break;
  }

  if (NULL == *pData) {
    SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)taosMemoryCalloc(1, sizeof(SRetrieveTableRsp));
    if (rsp) {
      rsp->completed = 1;
    }

    *pData = rsp;
    SCH_JOB_DLOG("empty res and set query complete, code:%x", code);
  }

428
  SCH_JOB_DLOG("fetch done, totalRows:%" PRId64, pJob->resNumOfRows);
D
dapan1121 已提交
429

D
dapan1121 已提交
430 431 432
_return:

  SCH_UNLOCK(SCH_WRITE, &pJob->resLock);
H
Hongze Cheng 已提交
433

D
dapan1121 已提交
434
  return code;
D
dapan1121 已提交
435 436
}

H
Hongze Cheng 已提交
437 438
int32_t schNotifyUserExecRes(SSchJob *pJob) {
  SExecResult *pRes = taosMemoryCalloc(1, sizeof(SExecResult));
D
dapan1121 已提交
439
  if (pRes) {
D
dapan1121 已提交
440
    schDumpJobExecRes(pJob, pRes);
D
dapan1121 已提交
441 442
  }

D
dapan1121 已提交
443
  SCH_JOB_DLOG("sch start to invoke exec cb, code: %s", tstrerror(pJob->errCode));
D
dapan1121 已提交
444
  (*pJob->userRes.execFp)(pRes, pJob->userRes.cbParam, atomic_load_32(&pJob->errCode));
D
dapan1121 已提交
445
  SCH_JOB_DLOG("sch end from exec cb, code: %s", tstrerror(pJob->errCode));
D
dapan1121 已提交
446 447 448 449

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
450 451 452
int32_t schNotifyUserFetchRes(SSchJob *pJob) {
  void *pRes = NULL;

D
dapan1121 已提交
453
  schDumpJobFetchRes(pJob, &pRes);
D
dapan1121 已提交
454 455

  SCH_JOB_DLOG("sch start to invoke fetch cb, code: %s", tstrerror(pJob->errCode));
D
dapan1121 已提交
456
  (*pJob->userRes.fetchFp)(pRes, pJob->userRes.cbParam, atomic_load_32(&pJob->errCode));
D
dapan1121 已提交
457
  SCH_JOB_DLOG("sch end from fetch cb, code: %s", tstrerror(pJob->errCode));
D
dapan1121 已提交
458 459 460 461

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
462
void schPostJobRes(SSchJob *pJob, SCH_OP_TYPE op) {
D
dapan1121 已提交
463
  SCH_LOCK(SCH_WRITE, &pJob->opStatus.lock);
H
Hongze Cheng 已提交
464

D
dapan1121 已提交
465
  if (SCH_OP_NULL == pJob->opStatus.op) {
D
dapan1121 已提交
466
    SCH_JOB_DLOG("job not in any operation, no need to post job res, status:%s", jobTaskStatusStr(pJob->status));
D
dapan1121 已提交
467
    goto _return;
D
dapan1121 已提交
468
  }
H
Hongze Cheng 已提交
469

D
dapan1121 已提交
470
  if (op && pJob->opStatus.op != op) {
D
dapan1121 已提交
471
    SCH_JOB_ELOG("job in operation %s mis-match with expected %s", schGetOpStr(pJob->opStatus.op), schGetOpStr(op));
D
dapan1121 已提交
472
    goto _return;
D
dapan1121 已提交
473
  }
H
Hongze Cheng 已提交
474

D
dapan1121 已提交
475
  if (SCH_JOB_IN_SYNC_OP(pJob)) {
D
dapan1121 已提交
476
    SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
477 478
    tsem_post(&pJob->rspSem);
  } else if (SCH_JOB_IN_ASYNC_EXEC_OP(pJob)) {
D
dapan1121 已提交
479
    SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
480 481
    schNotifyUserExecRes(pJob);
  } else if (SCH_JOB_IN_ASYNC_FETCH_OP(pJob)) {
D
dapan1121 已提交
482
    SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
483 484
    schNotifyUserFetchRes(pJob);
  } else {
D
dapan1121 已提交
485
    SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
486 487
    SCH_JOB_ELOG("job not in any operation, status:%s", jobTaskStatusStr(pJob->status));
  }
D
dapan1121 已提交
488 489 490 491 492 493

  return;

_return:

  SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
494 495
}

D
dapan1121 已提交
496
int32_t schProcessOnJobFailure(SSchJob *pJob, int32_t errCode) {
497 498 499
  if (TSDB_CODE_SCH_IGNORE_ERROR == errCode) {
    return TSDB_CODE_SCH_IGNORE_ERROR;
  }
X
Xiaoyu Wang 已提交
500

D
dapan1121 已提交
501
  schUpdateJobErrCode(pJob, errCode);
H
Hongze Cheng 已提交
502

D
dapan1121 已提交
503
  int32_t code = atomic_load_32(&pJob->errCode);
D
dapan1121 已提交
504
  if (code) {
505
    SCH_JOB_DLOG("job failed with error %s", tstrerror(code));
D
dapan1121 已提交
506
  }
D
dapan1121 已提交
507

D
dapan1121 已提交
508
  schPostJobRes(pJob, 0);
D
dapan1121 已提交
509

D
dapan1121 已提交
510
  SCH_RET(TSDB_CODE_SCH_IGNORE_ERROR);
D
dapan1121 已提交
511 512
}

D
dapan1121 已提交
513
int32_t schHandleJobFailure(SSchJob *pJob, int32_t errCode) {
D
dapan1121 已提交
514 515 516 517
  if (TSDB_CODE_SCH_IGNORE_ERROR == errCode) {
    return TSDB_CODE_SCH_IGNORE_ERROR;
  }

D
dapan1121 已提交
518
  schSwitchJobStatus(pJob, JOB_TASK_STATUS_FAIL, &errCode);
D
dapan1121 已提交
519
  return TSDB_CODE_SCH_IGNORE_ERROR;
D
dapan1121 已提交
520 521
}

H
Hongze Cheng 已提交
522
int32_t schProcessOnJobDropped(SSchJob *pJob, int32_t errCode) { SCH_RET(schProcessOnJobFailure(pJob, errCode)); }
D
dapan1121 已提交
523 524 525 526 527 528 529 530

int32_t schHandleJobDrop(SSchJob *pJob, int32_t errCode) {
  if (TSDB_CODE_SCH_IGNORE_ERROR == errCode) {
    return TSDB_CODE_SCH_IGNORE_ERROR;
  }

  schSwitchJobStatus(pJob, JOB_TASK_STATUS_DROP, &errCode);
  return TSDB_CODE_SCH_IGNORE_ERROR;
D
dapan1121 已提交
531 532
}

H
Hongze Cheng 已提交
533
int32_t schProcessOnJobPartialSuccess(SSchJob *pJob) {
D
dapan1121 已提交
534 535 536 537 538
  if (schChkCurrentOp(pJob, SCH_OP_FETCH, -1)) {
    SCH_ERR_RET(schLaunchFetchTask(pJob));
  } else {
    schPostJobRes(pJob, 0);
  }
D
dapan1121 已提交
539 540 541 542

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
543
void schProcessOnDataFetched(SSchJob *pJob) { schPostJobRes(pJob, SCH_OP_FETCH); }
D
dapan1121 已提交
544

D
dapan1121 已提交
545
int32_t schProcessOnExplainDone(SSchJob *pJob, SSchTask *pTask, SRetrieveTableRsp *pRsp) {
546
  SCH_TASK_DLOG("got explain rsp, rows:%" PRId64 ", complete:%d", htobe64(pRsp->numOfRows), pRsp->completed);
D
dapan1121 已提交
547

548
  atomic_store_64(&pJob->resNumOfRows, htobe64(pRsp->numOfRows));
D
dapan1121 已提交
549
  atomic_store_ptr(&pJob->fetchRes, pRsp);
D
dapan1121 已提交
550

D
dapan1121 已提交
551
  SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_SUCC);
D
dapan1121 已提交
552

X
Xiaoyu Wang 已提交
553 554 555
  if (!SCH_IS_INSERT_JOB(pJob)) {
    schProcessOnDataFetched(pJob);
  }
D
dapan1121 已提交
556

D
dapan1121 已提交
557
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
558 559
}

D
dapan1121 已提交
560
int32_t schLaunchJobLowerLevel(SSchJob *pJob, SSchTask *pTask) {
D
dapan1121 已提交
561 562 563 564 565
  if (!SCH_IS_QUERY_JOB(pJob)) {
    return TSDB_CODE_SUCCESS;
  }

  SSchLevel *pLevel = pTask->level;
D
dapan1121 已提交
566
  int32_t    doneNum = atomic_add_fetch_32(&pLevel->taskExecDoneNum, 1);
D
dapan1121 已提交
567
  if (doneNum == pLevel->taskNum) {
D
dapan1121 已提交
568
    atomic_sub_fetch_32(&pJob->levelIdx, 1);
D
dapan1121 已提交
569 570 571 572 573 574 575 576

    pLevel = taosArrayGet(pJob->levels, pJob->levelIdx);
    for (int32_t i = 0; i < pLevel->taskNum; ++i) {
      SSchTask *pTask = taosArrayGet(pLevel->subTasks, i);

      if (pTask->children && taosArrayGetSize(pTask->children) > 0) {
        continue;
      }
H
Hongze Cheng 已提交
577

D
dapan1121 已提交
578 579 580 581
      if (SCH_TASK_ALREADY_LAUNCHED(pTask)) {
        continue;
      }

D
dapan1121 已提交
582 583 584 585 586 587 588
      SCH_ERR_RET(schLaunchTask(pJob, pTask));
    }
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
589
int32_t schSaveJobExecRes(SSchJob *pJob, SQueryTableRsp *rsp) {
D
dapan1121 已提交
590
  if (rsp->tbFName[0]) {
D
dapan1121 已提交
591
    SCH_LOCK(SCH_WRITE, &pJob->resLock);
H
Hongze Cheng 已提交
592

D
dapan1121 已提交
593 594 595
    if (NULL == pJob->execRes.res) {
      pJob->execRes.res = taosArrayInit(pJob->taskNum, sizeof(STbVerInfo));
      if (NULL == pJob->execRes.res) {
H
Hongze Cheng 已提交
596
        SCH_UNLOCK(SCH_WRITE, &pJob->resLock);
D
dapan1121 已提交
597 598 599 600 601 602 603 604 605
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
    }

    STbVerInfo tbInfo;
    strcpy(tbInfo.tbFName, rsp->tbFName);
    tbInfo.sversion = rsp->sversion;
    tbInfo.tversion = rsp->tversion;

D
dapan1121 已提交
606
    taosArrayPush((SArray *)pJob->execRes.res, &tbInfo);
D
dapan1121 已提交
607
    pJob->execRes.msgType = TDMT_SCH_QUERY;
D
dapan1121 已提交
608 609

    SCH_UNLOCK(SCH_WRITE, &pJob->resLock);
D
dapan1121 已提交
610 611 612 613 614
  }

  return TSDB_CODE_SUCCESS;
}

615
int32_t schGetTaskInJob(SSchJob *pJob, uint64_t taskId, SSchTask **pTask) {
D
dapan1121 已提交
616
  schGetTaskFromList(pJob->taskList, taskId, pTask);
617
  if (NULL == *pTask) {
D
dapan1121 已提交
618
    SCH_JOB_ELOG("task not found in job task list, taskId:0x%" PRIx64, taskId);
D
dapan1121 已提交
619
    SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
620 621 622 623 624
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
625 626
int32_t schLaunchJob(SSchJob *pJob) {
  if (EXPLAIN_MODE_STATIC == pJob->attr.explainMode) {
D
dapan1121 已提交
627
    SCH_ERR_RET(qExecStaticExplain(pJob->pDag, (SRetrieveTableRsp **)&pJob->fetchRes));
D
dapan1121 已提交
628
    SCH_ERR_RET(schSwitchJobStatus(pJob, JOB_TASK_STATUS_PART_SUCC, NULL));
D
dapan1121 已提交
629
  } else {
D
dapan1121 已提交
630 631
    SSchLevel *level = taosArrayGet(pJob->levels, pJob->levelIdx);
    SCH_ERR_RET(schLaunchLevelTasks(pJob, level));
D
dapan1121 已提交
632 633 634 635 636 637 638
  }

  return TSDB_CODE_SUCCESS;
}

void schDropJobAllTasks(SSchJob *pJob) {
  schDropTaskInHashList(pJob, pJob->execTasks);
H
Hongze Cheng 已提交
639 640
  //  schDropTaskInHashList(pJob, pJob->succTasks);
  //  schDropTaskInHashList(pJob, pJob->failTasks);
D
dapan1121 已提交
641 642 643 644 645 646 647 648 649 650 651
}

void schFreeJobImpl(void *job) {
  if (NULL == job) {
    return;
  }

  SSchJob *pJob = job;
  uint64_t queryId = pJob->queryId;
  int64_t  refId = pJob->refId;

D
dapan1121 已提交
652 653
  qDebug("QID:0x%" PRIx64 " begin to free sch job, refId:0x%" PRIx64 ", pointer:%p", queryId, refId, pJob);

D
dapan1121 已提交
654 655 656 657 658 659 660 661 662
  schDropJobAllTasks(pJob);

  int32_t numOfLevels = taosArrayGetSize(pJob->levels);
  for (int32_t i = 0; i < numOfLevels; ++i) {
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);

    int32_t numOfTasks = taosArrayGetSize(pLevel->subTasks);
    for (int32_t j = 0; j < numOfTasks; ++j) {
      SSchTask *pTask = taosArrayGet(pLevel->subTasks, j);
D
dapan1121 已提交
663
      schFreeTask(pJob, pTask);
D
dapan1121 已提交
664 665 666 667 668 669 670 671
    }

    taosArrayDestroy(pLevel->subTasks);
  }

  schFreeFlowCtrl(pJob);

  taosHashCleanup(pJob->execTasks);
H
Hongze Cheng 已提交
672 673
  //  taosHashCleanup(pJob->failTasks);
  //  taosHashCleanup(pJob->succTasks);
D
dapan1121 已提交
674
  taosHashCleanup(pJob->taskList);
H
Hongze Cheng 已提交
675

D
dapan1121 已提交
676 677 678 679 680 681
  taosArrayDestroy(pJob->levels);
  taosArrayDestroy(pJob->nodeList);
  taosArrayDestroy(pJob->dataSrcTasks);

  qExplainFreeCtx(pJob->explainCtx);

D
dapan1121 已提交
682
  destroyQueryExecRes(&pJob->execRes);
D
dapan1121 已提交
683

D
dapan1121 已提交
684
  qDestroyQueryPlan(pJob->pDag);
685
  nodesReleaseAllocatorWeakRef(pJob->allocatorRefId);
D
dapan1121 已提交
686

D
dapan1121 已提交
687
  taosMemoryFreeClear(pJob->userRes.execRes);
D
dapan1121 已提交
688
  taosMemoryFreeClear(pJob->fetchRes);
D
dapan1121 已提交
689
  taosMemoryFreeClear(pJob->sql);
D
dapan1121 已提交
690
  tsem_destroy(&pJob->rspSem);
D
dapan1121 已提交
691
  taosMemoryFree(pJob);
D
dapan1121 已提交
692

D
dapan1121 已提交
693 694 695 696
  int32_t jobNum = atomic_sub_fetch_32(&schMgmt.jobNum, 1);
  if (jobNum == 0) {
    schCloseJobRef();
  }
D
dapan1121 已提交
697 698

  qDebug("QID:0x%" PRIx64 " sch job freed, refId:0x%" PRIx64 ", pointer:%p", queryId, refId, pJob);
D
dapan1121 已提交
699 700
}

D
dapan1121 已提交
701
int32_t schJobFetchRows(SSchJob *pJob) {
H
Hongze Cheng 已提交
702
  int32_t code = 0;
D
dapan1121 已提交
703

X
Xiaoyu Wang 已提交
704
  if (!(pJob->attr.explainMode == EXPLAIN_MODE_STATIC) && !(SCH_IS_EXPLAIN_JOB(pJob) && SCH_IS_INSERT_JOB(pJob))) {
D
dapan1121 已提交
705
    SCH_ERR_RET(schLaunchFetchTask(pJob));
H
Hongze Cheng 已提交
706

D
dapan1121 已提交
707
    if (schChkCurrentOp(pJob, SCH_OP_FETCH, true)) {
D
dapan1121 已提交
708 709
      SCH_JOB_DLOG("sync wait for rsp now, job status:%s", SCH_GET_JOB_STATUS_STR(pJob));
      tsem_wait(&pJob->rspSem);
H
Hongze Cheng 已提交
710
      SCH_RET(schDumpJobFetchRes(pJob, pJob->userRes.fetchRes));
D
dapan1121 已提交
711 712
    }
  } else {
D
dapan1121 已提交
713
    if (schChkCurrentOp(pJob, SCH_OP_FETCH, true)) {
H
Hongze Cheng 已提交
714
      SCH_RET(schDumpJobFetchRes(pJob, pJob->userRes.fetchRes));
D
dapan1121 已提交
715 716 717
    } else {
      schPostJobRes(pJob, SCH_OP_FETCH);
    }
D
dapan1121 已提交
718 719
  }

D
dapan1121 已提交
720
  SCH_RET(code);
D
dapan1121 已提交
721 722
}

D
dapan1121 已提交
723
int32_t schInitJob(int64_t *pJobId, SSchedulerReq *pReq) {
D
dapan1121 已提交
724 725 726 727 728
  int32_t  code = 0;
  int64_t  refId = -1;
  SSchJob *pJob = taosMemoryCalloc(1, sizeof(SSchJob));
  if (NULL == pJob) {
    qError("QID:0x%" PRIx64 " calloc %d failed", pReq->pDag->queryId, (int32_t)sizeof(SSchJob));
S
Shengliang Guan 已提交
729
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
730 731 732
  }

  pJob->attr.explainMode = pReq->pDag->explainInfo.mode;
H
Haojun Liao 已提交
733
  pJob->attr.localExec = pReq->localReq;
D
dapan1121 已提交
734
  pJob->conn = *pReq->pConn;
D
dapan1121 已提交
735
  if (pReq->sql) {
736
    pJob->sql = taosStrdup(pReq->sql);
D
dapan1121 已提交
737
  }
D
dapan1121 已提交
738
  pJob->pDag = pReq->pDag;
739
  pJob->allocatorRefId = nodesMakeAllocatorWeakRef(pReq->allocatorRefId);
D
dapan1121 已提交
740 741 742
  pJob->chkKillFp = pReq->chkKillFp;
  pJob->chkKillParam = pReq->chkKillParam;
  pJob->userRes.execFp = pReq->execFp;
D
dapan1121 已提交
743
  pJob->userRes.cbParam = pReq->cbParam;
D
dapan1121 已提交
744 745 746 747

  if (pReq->pNodeList == NULL || taosArrayGetSize(pReq->pNodeList) <= 0) {
    qDebug("QID:0x%" PRIx64 " input exec nodeList is empty", pReq->pDag->queryId);
  } else {
H
Haojun Liao 已提交
748
    pJob->nodeList = taosArrayDup(pReq->pNodeList, NULL);
D
dapan1121 已提交
749
  }
H
Hongze Cheng 已提交
750 751 752

  pJob->taskList = taosHashInit(pReq->pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                                HASH_ENTRY_LOCK);
D
dapan1121 已提交
753 754
  if (NULL == pJob->taskList) {
    SCH_JOB_ELOG("taosHashInit %d taskList failed", pReq->pDag->numOfSubplans);
S
Shengliang Guan 已提交
755
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
756 757 758 759 760 761 762 763
  }

  SCH_ERR_JRET(schValidateAndBuildJob(pReq->pDag, pJob));

  if (SCH_IS_EXPLAIN_JOB(pJob)) {
    SCH_ERR_JRET(qExecExplainBegin(pReq->pDag, &pJob->explainCtx, pReq->startTs));
  }

H
Hongze Cheng 已提交
764 765
  pJob->execTasks = taosHashInit(pReq->pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                                 HASH_ENTRY_LOCK);
D
dapan1121 已提交
766 767
  if (NULL == pJob->execTasks) {
    SCH_JOB_ELOG("taosHashInit %d execTasks failed", pReq->pDag->numOfSubplans);
S
Shengliang Guan 已提交
768
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
769 770
  }

D
dapan1121 已提交
771 772 773 774
  if (tsem_init(&pJob->rspSem, 0, 0)) {
    SCH_JOB_ELOG("tsem_init failed, errno:%d", errno);
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
775

D
dapan1121 已提交
776 777
  pJob->refId = taosAddRef(schMgmt.jobRef, pJob);
  if (pJob->refId < 0) {
D
dapan1121 已提交
778 779 780 781 782 783
    SCH_JOB_ELOG("taosAddRef job failed, error:%s", tstrerror(terrno));
    SCH_ERR_JRET(terrno);
  }

  atomic_add_fetch_32(&schMgmt.jobNum, 1);

D
dapan1121 已提交
784
  *pJobId = pJob->refId;
D
dapan1121 已提交
785

H
Hongze Cheng 已提交
786
  SCH_JOB_DLOG("job refId:0x%" PRIx64 " created", pJob->refId);
D
dapan1121 已提交
787 788 789 790 791 792 793

  return TSDB_CODE_SUCCESS;

_return:

  if (NULL == pJob) {
    qDestroyQueryPlan(pReq->pDag);
D
dapan1121 已提交
794
  } else if (pJob->refId < 0) {
D
dapan1121 已提交
795 796
    schFreeJobImpl(pJob);
  } else {
D
dapan1121 已提交
797
    taosRemoveRef(schMgmt.jobRef, pJob->refId);
D
dapan1121 已提交
798
  }
H
Hongze Cheng 已提交
799

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

D
dapan1121 已提交
803 804
int32_t schExecJob(SSchJob *pJob, SSchedulerReq *pReq) {
  int32_t code = 0;
H
Hongze Cheng 已提交
805
  qDebug("QID:0x%" PRIx64 " sch job refId 0x%" PRIx64 " started", pReq->pDag->queryId, pJob->refId);
D
dapan1121 已提交
806

D
dapan1121 已提交
807
  SCH_ERR_RET(schLaunchJob(pJob));
H
Hongze Cheng 已提交
808

D
dapan1121 已提交
809 810 811
  if (pReq->syncReq) {
    SCH_JOB_DLOG("sync wait for rsp now, job status:%s", SCH_GET_JOB_STATUS_STR(pJob));
    tsem_wait(&pJob->rspSem);
D
dapan1121 已提交
812 813 814
  }

  SCH_JOB_DLOG("job exec done, job status:%s, jobId:0x%" PRIx64, SCH_GET_JOB_STATUS_STR(pJob), pJob->refId);
H
Hongze Cheng 已提交
815

D
dapan1121 已提交
816
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
817 818
}

H
Hongze Cheng 已提交
819
void schDirectPostJobRes(SSchedulerReq *pReq, int32_t errCode) {
D
dapan1121 已提交
820
  if (NULL == pReq || pReq->syncReq) {
D
dapan1121 已提交
821 822
    return;
  }
H
Hongze Cheng 已提交
823

D
dapan1121 已提交
824 825 826 827 828 829
  if (pReq->execFp) {
    (*pReq->execFp)(NULL, pReq->cbParam, errCode);
  } else if (pReq->fetchFp) {
    (*pReq->fetchFp)(NULL, pReq->cbParam, errCode);
  }
}
D
dapan1121 已提交
830

D
dapan1121 已提交
831 832 833 834 835
int32_t schChkResetJobRetry(SSchJob *pJob, int32_t rspCode) {
  if (pJob->status >= JOB_TASK_STATUS_PART_SUCC) {
    SCH_LOCK(SCH_WRITE, &pJob->resLock);
    if (pJob->fetched) {
      SCH_UNLOCK(SCH_WRITE, &pJob->resLock);
D
dapan1121 已提交
836 837 838
      pJob->noMoreRetry = true;
      SCH_JOB_ELOG("already fetched while got error %s", tstrerror(rspCode));
      SCH_ERR_RET(rspCode);
D
dapan1121 已提交
839 840 841 842 843 844 845 846 847
    }
    SCH_UNLOCK(SCH_WRITE, &pJob->resLock);

    schUpdateJobStatus(pJob, JOB_TASK_STATUS_EXEC);
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
int32_t schResetJobForRetry(SSchJob *pJob, int32_t rspCode) {
  SCH_ERR_RET(schChkResetJobRetry(pJob, rspCode));

  int32_t numOfLevels = taosArrayGetSize(pJob->levels);
  for (int32_t i = 0; i < numOfLevels; ++i) {
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);

    pLevel->taskExecDoneNum = 0;
    pLevel->taskLaunchedNum = 0;

    int32_t numOfTasks = taosArrayGetSize(pLevel->subTasks);
    for (int32_t j = 0; j < numOfTasks; ++j) {
      SSchTask *pTask = taosArrayGet(pLevel->subTasks, j);
      SCH_LOCK_TASK(pTask);
      SCH_ERR_RET(schChkUpdateRedirectCtx(pJob, pTask, NULL, rspCode));
      qClearSubplanExecutionNode(pTask->plan);
      schResetTaskForRetry(pJob, pTask);
      SCH_UNLOCK_TASK(pTask);
    }
  }

  SCH_RESET_JOB_LEVEL_IDX(pJob);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
874 875 876 877

int32_t schHandleJobRetry(SSchJob *pJob, SSchTask *pTask, SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;

D
dapan1121 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
  taosMemoryFreeClear(pMsg->pData);
  taosMemoryFreeClear(pMsg->pEpSet);

  SCH_UNLOCK_TASK(pTask);

  SCH_TASK_DLOG("start to redirect all job tasks cause of error: %s", tstrerror(rspCode));

  SCH_ERR_JRET(schResetJobForRetry(pJob, rspCode));

  SCH_ERR_JRET(schLaunchJob(pJob));

  SCH_LOCK_TASK(pTask);

  SCH_RET(code);

_return:

  SCH_LOCK_TASK(pTask);
D
dapan1121 已提交
896

D
dapan1121 已提交
897
  SCH_RET(schProcessOnTaskFailure(pJob, pTask, code));
D
dapan1121 已提交
898 899
}

D
dapan1121 已提交
900 901
bool schChkCurrentOp(SSchJob *pJob, int32_t op, int8_t sync) {
  bool r = false;
D
dapan1121 已提交
902
  SCH_LOCK(SCH_READ, &pJob->opStatus.lock);
D
dapan1121 已提交
903 904 905 906 907
  if (sync >= 0) {
    r = (pJob->opStatus.op == op) && (pJob->opStatus.syncReq == sync);
  } else {
    r = (pJob->opStatus.op == op);
  }
D
dapan1121 已提交
908 909 910 911 912
  SCH_UNLOCK(SCH_READ, &pJob->opStatus.lock);

  return r;
}

H
Hongze Cheng 已提交
913
void schProcessOnOpEnd(SSchJob *pJob, SCH_OP_TYPE type, SSchedulerReq *pReq, int32_t errCode) {
D
dapan1121 已提交
914
  int32_t op = 0;
H
Hongze Cheng 已提交
915

D
dapan1121 已提交
916 917
  switch (type) {
    case SCH_OP_EXEC:
D
dapan1121 已提交
918
      if (pReq && pReq->syncReq) {
D
dapan1121 已提交
919
        SCH_LOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
920 921
        op = atomic_val_compare_exchange_32(&pJob->opStatus.op, type, SCH_OP_NULL);
        if (SCH_OP_NULL == op || op != type) {
H
Hongze Cheng 已提交
922 923
          SCH_JOB_ELOG("job not in %s operation, op:%s, status:%s", schGetOpStr(type), schGetOpStr(op),
                       jobTaskStatusStr(pJob->status));
D
dapan1121 已提交
924
        }
D
dapan1121 已提交
925
        SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
926 927 928 929
        schDumpJobExecRes(pJob, pReq->pExecRes);
      }
      break;
    case SCH_OP_FETCH:
D
dapan1121 已提交
930
      if (pReq && pReq->syncReq) {
D
dapan1121 已提交
931
        SCH_LOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
932 933
        op = atomic_val_compare_exchange_32(&pJob->opStatus.op, type, SCH_OP_NULL);
        if (SCH_OP_NULL == op || op != type) {
H
Hongze Cheng 已提交
934 935
          SCH_JOB_ELOG("job not in %s operation, op:%s, status:%s", schGetOpStr(type), schGetOpStr(op),
                       jobTaskStatusStr(pJob->status));
D
dapan1121 已提交
936
        }
D
dapan1121 已提交
937
        SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
938 939 940 941 942 943 944
      }
      break;
    case SCH_OP_GET_STATUS:
      errCode = TSDB_CODE_SUCCESS;
      break;
    default:
      break;
D
dapan1121 已提交
945 946
  }

D
dapan1121 已提交
947
  if (errCode) {
D
dapan1121 已提交
948
    schHandleJobFailure(pJob, errCode);
D
dapan1121 已提交
949 950 951
  }

  SCH_JOB_DLOG("job end %s operation with code %s", schGetOpStr(type), tstrerror(errCode));
D
dapan1121 已提交
952 953
}

H
Hongze Cheng 已提交
954
int32_t schProcessOnOpBegin(SSchJob *pJob, SCH_OP_TYPE type, SSchedulerReq *pReq) {
D
dapan1121 已提交
955
  int32_t code = 0;
H
Hongze Cheng 已提交
956 957
  int8_t  status = SCH_GET_JOB_STATUS(pJob);

D
dapan1121 已提交
958 959
  switch (type) {
    case SCH_OP_EXEC:
D
dapan1121 已提交
960
      SCH_LOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
961 962
      if (SCH_OP_NULL != atomic_val_compare_exchange_32(&pJob->opStatus.op, SCH_OP_NULL, type)) {
        SCH_JOB_ELOG("job already in %s operation", schGetOpStr(pJob->opStatus.op));
D
dapan1121 已提交
963
        SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
S
Shengliang Guan 已提交
964 965
        schDirectPostJobRes(pReq, TSDB_CODE_APP_ERROR);
        SCH_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
966
      }
H
Hongze Cheng 已提交
967

D
dapan1121 已提交
968
      SCH_JOB_DLOG("job start %s operation", schGetOpStr(pJob->opStatus.op));
H
Hongze Cheng 已提交
969

D
dapan1121 已提交
970
      pJob->opStatus.syncReq = pReq->syncReq;
D
dapan1121 已提交
971
      SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
972 973
      break;
    case SCH_OP_FETCH:
D
dapan1121 已提交
974
      SCH_LOCK(SCH_WRITE, &pJob->opStatus.lock);
D
dapan1121 已提交
975 976
      if (SCH_OP_NULL != atomic_val_compare_exchange_32(&pJob->opStatus.op, SCH_OP_NULL, type)) {
        SCH_JOB_ELOG("job already in %s operation", schGetOpStr(pJob->opStatus.op));
D
dapan1121 已提交
977
        SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
S
Shengliang Guan 已提交
978 979
        schDirectPostJobRes(pReq, TSDB_CODE_APP_ERROR);
        SCH_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
980
      }
H
Hongze Cheng 已提交
981

D
dapan1121 已提交
982
      SCH_JOB_DLOG("job start %s operation", schGetOpStr(pJob->opStatus.op));
H
Hongze Cheng 已提交
983

D
dapan1121 已提交
984 985 986
      pJob->userRes.fetchRes = pReq->pFetchRes;
      pJob->userRes.fetchFp = pReq->fetchFp;
      pJob->userRes.cbParam = pReq->cbParam;
H
Hongze Cheng 已提交
987

D
dapan1121 已提交
988
      pJob->opStatus.syncReq = pReq->syncReq;
D
dapan1121 已提交
989
      SCH_UNLOCK(SCH_WRITE, &pJob->opStatus.lock);
H
Hongze Cheng 已提交
990

D
dapan1121 已提交
991 992
      if (!SCH_JOB_NEED_FETCH(pJob)) {
        SCH_JOB_ELOG("no need to fetch data, status:%s", SCH_GET_JOB_STATUS_STR(pJob));
S
Shengliang Guan 已提交
993
        SCH_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
994 995
      }

D
dapan1121 已提交
996
      if (status != JOB_TASK_STATUS_PART_SUCC && status != JOB_TASK_STATUS_FETCH) {
D
dapan1121 已提交
997
        SCH_JOB_ELOG("job status error for fetch, status:%s", jobTaskStatusStr(status));
D
dapan1121 已提交
998 999
        SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
      }
H
Hongze Cheng 已提交
1000

D
dapan1121 已提交
1001 1002 1003 1004 1005
      break;
    case SCH_OP_GET_STATUS:
      if (pJob->status < JOB_TASK_STATUS_INIT || pJob->levelNum <= 0 || NULL == pJob->levels) {
        qDebug("job not initialized or not executable job, refId:0x%" PRIx64, pJob->refId);
        SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
1006
      }
1007
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1008 1009
    default:
      SCH_JOB_ELOG("unknown operation type %d", type);
S
Shengliang Guan 已提交
1010
      SCH_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1011 1012
  }

D
dapan1121 已提交
1013 1014 1015 1016 1017
  if (schJobNeedToStop(pJob, &status)) {
    SCH_JOB_ELOG("abort op %s cause of job need to stop, status:%s", schGetOpStr(type), jobTaskStatusStr(status));
    SCH_ERR_RET(TSDB_CODE_SCH_IGNORE_ERROR);
  }

D
dapan1121 已提交
1018
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1019
}
D
dapan1121 已提交
1020

D
dapan1121 已提交
1021 1022 1023 1024
void schProcessOnCbEnd(SSchJob *pJob, SSchTask *pTask, int32_t errCode) {
  if (pTask) {
    SCH_UNLOCK_TASK(pTask);
  }
D
dapan1121 已提交
1025

D
dapan1121 已提交
1026
  if (errCode) {
D
dapan1121 已提交
1027
    schHandleJobFailure(pJob, errCode);
D
dapan1121 已提交
1028
  }
H
Hongze Cheng 已提交
1029

D
dapan1121 已提交
1030 1031 1032
  if (pJob) {
    schReleaseJob(pJob->refId);
  }
D
dapan1121 已提交
1033 1034
}

H
Hongze Cheng 已提交
1035
int32_t schProcessOnCbBegin(SSchJob **job, SSchTask **task, uint64_t qId, int64_t rId, uint64_t tId) {
D
dapan1121 已提交
1036
  int32_t code = 0;
H
Hongze Cheng 已提交
1037
  int8_t  status = 0;
D
dapan1121 已提交
1038 1039

  SSchTask *pTask = NULL;
H
Hongze Cheng 已提交
1040
  SSchJob  *pJob = schAcquireJob(rId);
D
dapan1121 已提交
1041 1042 1043
  if (NULL == pJob) {
    qWarn("QID:0x%" PRIx64 ",TID:0x%" PRIx64 "job no exist, may be dropped, refId:0x%" PRIx64, qId, tId, rId);
    SCH_ERR_RET(TSDB_CODE_QRY_JOB_NOT_EXIST);
D
dapan1121 已提交
1044
  }
H
Hongze Cheng 已提交
1045

D
dapan1121 已提交
1046
  if (schJobNeedToStop(pJob, &status)) {
D
dapan1121 已提交
1047
    SCH_TASK_DLOG("will not do further processing cause of job status %s", jobTaskStatusStr(status));
D
dapan1121 已提交
1048 1049 1050 1051 1052 1053 1054
    SCH_ERR_JRET(TSDB_CODE_SCH_IGNORE_ERROR);
  }

  SCH_ERR_JRET(schGetTaskInJob(pJob, tId, &pTask));

  SCH_LOCK_TASK(pTask);

D
dapan1121 已提交
1055 1056 1057
  *job = pJob;
  *task = pTask;

D
dapan1121 已提交
1058
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1059

D
dapan1121 已提交
1060 1061 1062 1063 1064 1065 1066 1067
_return:

  if (pTask) {
    SCH_UNLOCK_TASK(pTask);
  }
  if (pJob) {
    schReleaseJob(rId);
  }
H
Hongze Cheng 已提交
1068

D
dapan1121 已提交
1069 1070
  SCH_RET(code);
}