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

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);
  }

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

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

D
dapan1121 已提交
70 71
int32_t schedulerExecJob(SSchedulerReq *pReq, int64_t *pJobId) {
  qDebug("scheduler %s exec job start", pReq->syncReq ? "SYNC" : "ASYNC");
D
dapan1121 已提交
72 73 74 75

  int32_t code = 0;  
  SSchJob *pJob = NULL;

D
dapan1121 已提交
76 77 78 79 80
  *pJobId = 0;

  SCH_ERR_RET(schSwitchJobStatus(&pJob, JOB_TASK_STATUS_INIT, pReq));

  SCH_ERR_RET(schHandleOpBeginEvent(pJob, SCH_OP_EXEC, pReq));
D
dapan1121 已提交
81
  
D
dapan1121 已提交
82 83 84
  SCH_ERR_RET(schSwitchJobStatus(&pJob, JOB_TASK_STATUS_EXEC, pReq));

  SCH_ERR_RET(schHandleOpEndEvent(pJob, SCH_OP_EXEC, pReq));
D
dapan1121 已提交
85 86

  *pJobId = pJob->refId;
D
dapan1121 已提交
87 88

_return:
D
dapan1121 已提交
89
  
D
dapan1121 已提交
90 91 92
  schDumpJobExecRes(pJob, pReq->pQueryRes);

  schReleaseJob(pJob->refId);
D
dapan1121 已提交
93 94

  return code;
D
dapan1121 已提交
95 96
}

D
dapan1121 已提交
97 98
int32_t schedulerFetchRows(int64_t job, SSchedulerReq *pReq) {
  qDebug("scheduler %s fetch rows start", pReq->syncReq ? "SYNC" : "ASYNC");
D
dapan 已提交
99

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

D
dapan1121 已提交
107 108 109 110
  SCH_ERR_RET(schHandleOpBeginEvent(pJob, SCH_OP_FETCH, pReq));



D
dapan1121 已提交
111 112
  SCH_ERR_RET(schBeginOperation(pJob, SCH_OP_FETCH, true));

D
dapan1121 已提交
113
  pJob->userRes.fetchRes = pData;
D
dapan1121 已提交
114
  code = schJobFetchRows(pJob);
D
dapan 已提交
115

D
dapan1121 已提交
116
  schReleaseJob(job);
D
dapan 已提交
117

D
dapan1121 已提交
118 119
  SCH_RET(code);
}
D
dapan1121 已提交
120

D
dapan1121 已提交
121
void schedulerFetchRowsA(int64_t job, schedulerFetchFp fp, void* param) {
D
dapan1121 已提交
122 123
  qDebug("scheduler async fetch rows start");

D
dapan1121 已提交
124
  int32_t code = 0;
D
dapan1121 已提交
125
  if (NULL == fp || NULL == param) {
D
dapan1121 已提交
126
    SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
127
  }
D
dapan 已提交
128

D
dapan1121 已提交
129 130
  SSchJob *pJob = schAcquireJob(job);
  if (NULL == pJob) {
D
dapan1121 已提交
131 132
    qError("acquire sch job from job list failed, may be dropped, jobId:0x%" PRIx64, job);
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
133
  }
D
dapan1121 已提交
134

D
dapan1121 已提交
135 136
  SCH_ERR_JRET(schBeginOperation(pJob, SCH_OP_FETCH, false));
  
D
dapan1121 已提交
137 138 139
  pJob->userRes.fetchFp = fp;
  pJob->userRes.userParam = param;
  
D
dapan1121 已提交
140
  SCH_ERR_JRET(schJobFetchRowsA(pJob));
D
dapan1121 已提交
141 142

_return:
L
Liu Jicong 已提交
143

D
dapan1121 已提交
144 145 146 147
  if (code) {
    fp(NULL, param, code);
  }
  
D
dapan1121 已提交
148
  schReleaseJob(job);
D
dapan 已提交
149
}
D
dapan1121 已提交
150

D
dapan1121 已提交
151 152 153 154
int32_t schedulerGetTasksStatus(int64_t job, SArray *pSub) {
  int32_t  code = 0;
  SSchJob *pJob = schAcquireJob(job);
  if (NULL == pJob) {
D
dapan1121 已提交
155
    qDebug("acquire job from jobRef list failed, may not started or dropped, refId:0x%" PRIx64, job);
D
dapan1121 已提交
156 157 158
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
  }

D
dapan1121 已提交
159
  if (pJob->status < JOB_TASK_STATUS_INIT || pJob->levelNum <= 0 || NULL == pJob->levels) {
D
dapan1121 已提交
160
    qDebug("job not initialized or not executable job, refId:0x%" PRIx64, job);
D
dapan1121 已提交
161
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
162 163 164 165
  }

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

D
dapan1121 已提交
167
    for (int32_t m = 0; m < pLevel->taskNum; ++m) {
X
Xiaoyu Wang 已提交
168
      SSchTask     *pTask = taosArrayGet(pLevel->subTasks, m);
169 170 171
      SQuerySubDesc subDesc = {0};
      subDesc.tid = pTask->taskId;
      strcpy(subDesc.status, jobTaskStatusStr(pTask->status));
H
Hongze Cheng 已提交
172

D
dapan1121 已提交
173 174 175 176
      taosArrayPush(pSub, &subDesc);
    }
  }

D
dapan1121 已提交
177 178 179 180 181
_return:

  schReleaseJob(job);

  SCH_RET(code);
D
dapan1121 已提交
182 183
}

D
dapan1121 已提交
184
int32_t scheduleCancelJob(int64_t job) {
D
dapan1121 已提交
185
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
186
  if (NULL == pJob) {
187
    qError("acquire job from jobRef list failed, may be dropped, jobId:0x%" PRIx64, job);
D
dapan1121 已提交
188 189
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
  }
D
dapan1121 已提交
190

D
dapan1121 已提交
191 192
  int32_t code = schCancelJob(pJob);

D
dapan1121 已提交
193
  schReleaseJob(job);
D
dapan1121 已提交
194 195

  SCH_RET(code);
D
dapan1121 已提交
196 197
}

D
dapan1121 已提交
198 199 200 201 202 203 204 205
void schedulerStopQueryHb(void *pTrans) {
  if (NULL == pTrans) {
    return;
  }

  schCleanClusterHb(pTrans);
}

D
dapan1121 已提交
206 207 208 209 210 211
void schedulerFreeJob(int64_t* job, int32_t errCode) {
  if (0 == *job) {
    return;
  }
  
  SSchJob *pJob = schAcquireJob(*job);
D
dapan1121 已提交
212
  if (NULL == pJob) {
D
dapan1121 已提交
213 214
    qError("acquire sch job failed, may be dropped, jobId:0x%" PRIx64, *job);
    *job = 0;
D
dapan 已提交
215 216
    return;
  }
D
dapan1121 已提交
217

D
dapan1121 已提交
218 219
  int32_t code = schProcessOnJobDropped(pJob, errCode);
  if (TSDB_CODE_SCH_JOB_IS_DROPPING == code) {
D
dapan1121 已提交
220 221
    SCH_JOB_DLOG("sch job is already dropping, refId:0x%" PRIx64, *job);
    *job = 0;
D
dapan1121 已提交
222
    return;
D
dapan1121 已提交
223
  }
D
dapan1121 已提交
224

D
dapan1121 已提交
225
  SCH_JOB_DLOG("start to remove job from jobRef list, refId:0x%" PRIx64, *job);
226

D
dapan1121 已提交
227 228
  if (taosRemoveRef(schMgmt.jobRef, *job)) {
    SCH_JOB_ELOG("remove job from job list failed, refId:0x%" PRIx64, *job);
229
  }
D
dapan1121 已提交
230

D
dapan1121 已提交
231 232
  schReleaseJob(*job);
  *job = 0;
D
dapan1121 已提交
233
}
D
dapan1121 已提交
234

D
dapan1121 已提交
235
void schedulerDestroy(void) {
236 237
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

D
dapan1121 已提交
238
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
239
    SSchJob *pJob = taosIterateRef(schMgmt.jobRef, 0);
H
Hongze Cheng 已提交
240
    int64_t  refId = 0;
C
Cary Xu 已提交
241

D
dapan1121 已提交
242
    while (pJob) {
D
dapan1121 已提交
243
      refId = pJob->refId;
C
Cary Xu 已提交
244 245 246
      if (refId == 0) {
        break;
      }
D
dapan1121 已提交
247
      taosRemoveRef(schMgmt.jobRef, pJob->refId);
L
Liu Jicong 已提交
248

D
dapan1121 已提交
249
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
250
    }
D
dapan1121 已提交
251
  }
D
dapan1121 已提交
252

D
dapan1121 已提交
253
  SCH_LOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
254
  if (schMgmt.hbConnections) {
H
Hongze Cheng 已提交
255
    void *pIter = taosHashIterate(schMgmt.hbConnections, NULL);
D
dapan1121 已提交
256 257 258 259
    while (pIter != NULL) {
      SSchHbTrans *hb = pIter;
      schFreeRpcCtx(&hb->rpcCtx);
      pIter = taosHashIterate(schMgmt.hbConnections, pIter);
H
Hongze Cheng 已提交
260
    }
D
dapan1121 已提交
261 262 263
    taosHashCleanup(schMgmt.hbConnections);
    schMgmt.hbConnections = NULL;
  }
D
dapan1121 已提交
264
  SCH_UNLOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
265
}