scheduler.c 6.6 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
 */

L
Liu Jicong 已提交
16
#include "catalog.h"
H
Hongze Cheng 已提交
17
#include "command.h"
L
Liu Jicong 已提交
18
#include "query.h"
D
dapan1121 已提交
19
#include "schedulerInt.h"
H
Hongze Cheng 已提交
20
#include "tmsg.h"
D
dapan1121 已提交
21
#include "tref.h"
D
dapan1121 已提交
22
#include "trpc.h"
23

D
dapan1121 已提交
24
SSchedulerMgmt schMgmt = {
25
    .jobRef = -1,
D
dapan1121 已提交
26
};
D
dapan1121 已提交
27

D
dapan1121 已提交
28
int32_t schedulerInit(SSchedulerCfg *cfg) {
D
dapan1121 已提交
29
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
30 31 32 33 34 35
    qError("scheduler already initialized");
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  if (cfg) {
    schMgmt.cfg = *cfg;
L
Liu Jicong 已提交
36

D
dapan1121 已提交
37
    if (schMgmt.cfg.maxJobNum == 0) {
D
dapan1121 已提交
38
      schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_MAX_JOB_NUM;
D
dapan1121 已提交
39
    }
D
dapan1121 已提交
40 41 42
    if (schMgmt.cfg.maxNodeTableNum <= 0) {
      schMgmt.cfg.maxNodeTableNum = SCHEDULE_DEFAULT_MAX_NODE_TABLE_NUM;
    }
D
dapan1121 已提交
43
  } else {
D
dapan1121 已提交
44 45
    schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_MAX_JOB_NUM;
    schMgmt.cfg.maxNodeTableNum = SCHEDULE_DEFAULT_MAX_NODE_TABLE_NUM;
D
dapan1121 已提交
46
  }
L
Liu Jicong 已提交
47

D
dapan1121 已提交
48 49
  schMgmt.jobRef = taosOpenRef(schMgmt.cfg.maxJobNum, schFreeJobImpl);
  if (schMgmt.jobRef < 0) {
D
dapan1121 已提交
50 51 52 53 54 55 56
    qError("init schduler jobRef failed, num:%u", schMgmt.cfg.maxJobNum);
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  schMgmt.hbConnections = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
  if (NULL == schMgmt.hbConnections) {
    qError("taosHashInit hb connections failed");
D
dapan1121 已提交
57 58 59
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
60
  if (taosGetSystemUUID((char *)&schMgmt.sId, sizeof(schMgmt.sId))) {
D
dapan1121 已提交
61 62 63 64
    qError("generate schdulerId failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR);
  }

L
Liu Jicong 已提交
65 66
  qInfo("scheduler %" PRIx64 " initizlized, maxJob:%u", schMgmt.sId, schMgmt.cfg.maxJobNum);

D
dapan1121 已提交
67 68 69
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
70
int32_t schedulerExecJob(void *pTrans, SArray *pNodeList, SQueryPlan *pDag, int64_t *pJob, const char *sql,
D
dapan1121 已提交
71
                         int64_t startTs, SQueryResult *pRes) {
D
dapan1121 已提交
72
  if (NULL == pTrans || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == pRes) {
D
dapan1121 已提交
73 74 75
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

D
dapan1121 已提交
76 77
  SSchResInfo resInfo = {.queryRes = pRes};
  SCH_RET(schExecJob(pTrans, pNodeList, pDag, pJob, sql, startTs, &resInfo));
D
dapan1121 已提交
78 79
}

D
dapan1121 已提交
80
int32_t schedulerAsyncExecJob(void *pTrans, SArray *pNodeList, SQueryPlan *pDag, int64_t *pJob, const char *sql,
D
dapan1121 已提交
81
                         int64_t startTs, schedulerExecCallback fp, void* param) {
82 83 84 85 86 87 88 89 90 91 92 93 94
  int32_t code = 0;
  if (NULL == pTrans || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == fp) {
    code = TSDB_CODE_QRY_INVALID_INPUT;
  } else {
    SSchResInfo resInfo = {.execFp = fp, .userParam = param};
    code = schAsyncExecJob(pTrans, pNodeList, pDag, pJob, sql, startTs, &resInfo);
  }

  if (code != TSDB_CODE_SUCCESS) {
    fp(NULL, param, code);
  }

  return code;
D
dapan1121 已提交
95 96
}

L
Liu Jicong 已提交
97
int32_t schedulerFetchRows(int64_t job, void **pData) {
D
dapan1121 已提交
98
  if (NULL == pData) {
D
dapan1121 已提交
99
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan 已提交
100 101
  }

L
Liu Jicong 已提交
102
  int32_t  code = 0;
D
dapan1121 已提交
103
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
104 105 106 107
  if (NULL == pJob) {
    qError("acquire job from jobRef list failed, may be dropped, refId:%" PRIx64, job);
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
  }
D
dapan1121 已提交
108

D
dapan1121 已提交
109 110 111
  pJob->attr.syncSchedule = true;
  pJob->userRes.fetchRes = pData;
  code = schFetchRows(pJob);
D
dapan 已提交
112

D
dapan1121 已提交
113
  schReleaseJob(job);
D
dapan 已提交
114

D
dapan1121 已提交
115 116
  SCH_RET(code);
}
D
dapan1121 已提交
117

D
dapan1121 已提交
118 119 120
int32_t schedulerAsyncFetchRows(int64_t job, schedulerFetchCallback fp, void* param) {
  if (NULL == fp || NULL == param) {
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
121
  }
D
dapan 已提交
122

D
dapan1121 已提交
123 124 125 126 127
  int32_t  code = 0;
  SSchJob *pJob = schAcquireJob(job);
  if (NULL == pJob) {
    qError("acquire job from jobRef list failed, may be dropped, refId:%" PRIx64, job);
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
128
  }
D
dapan1121 已提交
129

D
dapan1121 已提交
130 131 132 133
  pJob->attr.syncSchedule = false;
  pJob->userRes.fetchFp = fp;
  pJob->userRes.userParam = param;
  
D
dapan1121 已提交
134
  code = schAsyncFetchRows(pJob);
L
Liu Jicong 已提交
135

D
dapan1121 已提交
136
  schReleaseJob(job);
D
dapan 已提交
137

D
dapan1121 已提交
138
  SCH_RET(code);
D
dapan 已提交
139
}
D
dapan1121 已提交
140

D
dapan1121 已提交
141 142 143 144 145 146 147 148 149 150
int32_t schedulerGetTasksStatus(int64_t job, SArray *pSub) {
  int32_t  code = 0;
  SSchJob *pJob = schAcquireJob(job);
  if (NULL == pJob) {
    qDebug("acquire job from jobRef list failed, may not started or dropped, refId:%" PRIx64, job);
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
  }

  if (pJob->status < JOB_TASK_STATUS_NOT_START || pJob->levelNum <= 0 || NULL == pJob->levels) {
    qDebug("job not initialized or not executable job, refId:%" PRIx64, job);
D
dapan1121 已提交
151
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
152 153 154 155
  }

  for (int32_t i = pJob->levelNum - 1; i >= 0; --i) {
    SSchLevel *pLevel = taosArrayGet(pJob->levels, i);
H
Hongze Cheng 已提交
156

D
dapan1121 已提交
157
    for (int32_t m = 0; m < pLevel->taskNum; ++m) {
X
Xiaoyu Wang 已提交
158
      SSchTask     *pTask = taosArrayGet(pLevel->subTasks, m);
D
dapan1121 已提交
159
      SQuerySubDesc subDesc = {.tid = pTask->taskId, .status = pTask->status};
H
Hongze Cheng 已提交
160

D
dapan1121 已提交
161 162 163 164
      taosArrayPush(pSub, &subDesc);
    }
  }

D
dapan1121 已提交
165 166 167 168 169
_return:

  schReleaseJob(job);

  SCH_RET(code);
D
dapan1121 已提交
170 171
}

D
dapan1121 已提交
172
int32_t scheduleCancelJob(int64_t job) {
D
dapan1121 已提交
173
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
174 175 176 177
  if (NULL == pJob) {
    qError("acquire job from jobRef list failed, may be dropped, refId:%" PRIx64, job);
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
  }
D
dapan1121 已提交
178

D
dapan1121 已提交
179 180
  int32_t code = schCancelJob(pJob);

D
dapan1121 已提交
181
  schReleaseJob(job);
D
dapan1121 已提交
182 183

  SCH_RET(code);
D
dapan1121 已提交
184 185
}

D
dapan1121 已提交
186
void schedulerFreeJob(int64_t job) {
D
dapan1121 已提交
187
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
188
  if (NULL == pJob) {
D
dapan1121 已提交
189
    qDebug("acquire job from jobRef list failed, may be dropped, refId:%" PRIx64, job);
D
dapan 已提交
190 191
    return;
  }
D
dapan1121 已提交
192

D
dapan1121 已提交
193 194
  if (atomic_load_8(&pJob->userFetch) > 0) {
    schProcessOnJobDropped(pJob, TSDB_CODE_QRY_JOB_FREED);
D
dapan1121 已提交
195
  }
D
dapan1121 已提交
196

D
dapan1121 已提交
197
  SCH_JOB_DLOG("start to remove job from jobRef list, refId:%" PRIx64, job);
198

D
dapan1121 已提交
199 200
  if (taosRemoveRef(schMgmt.jobRef, job)) {
    SCH_JOB_ELOG("remove job from job list failed, refId:%" PRIx64, job);
201
  }
D
dapan1121 已提交
202 203

  schReleaseJob(job);
D
dapan1121 已提交
204
}
D
dapan1121 已提交
205

D
dapan1121 已提交
206
void schedulerDestroy(void) {
207 208
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

D
dapan1121 已提交
209
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
210
    SSchJob *pJob = taosIterateRef(schMgmt.jobRef, 0);
H
Hongze Cheng 已提交
211
    int64_t  refId = 0;
C
Cary Xu 已提交
212

D
dapan1121 已提交
213
    while (pJob) {
D
dapan1121 已提交
214
      refId = pJob->refId;
C
Cary Xu 已提交
215 216 217
      if (refId == 0) {
        break;
      }
D
dapan1121 已提交
218
      taosRemoveRef(schMgmt.jobRef, pJob->refId);
L
Liu Jicong 已提交
219

D
dapan1121 已提交
220
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
221
    }
D
dapan1121 已提交
222
  }
D
dapan1121 已提交
223 224

  if (schMgmt.hbConnections) {
H
Hongze Cheng 已提交
225
    void *pIter = taosHashIterate(schMgmt.hbConnections, NULL);
D
dapan1121 已提交
226 227 228 229
    while (pIter != NULL) {
      SSchHbTrans *hb = pIter;
      schFreeRpcCtx(&hb->rpcCtx);
      pIter = taosHashIterate(schMgmt.hbConnections, pIter);
H
Hongze Cheng 已提交
230
    }
D
dapan1121 已提交
231 232 233
    taosHashCleanup(schMgmt.hbConnections);
    schMgmt.hbConnections = NULL;
  }
D
dapan1121 已提交
234
}