scheduler.c 6.8 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
  if (NULL == pJob) {
105
    qError("acquire job from jobRef list failed, may be dropped, jobId:0x%" PRIx64, job);
D
dapan1121 已提交
106 107
    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

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

D
dapan1121 已提交
124 125
  SSchJob *pJob = schAcquireJob(job);
  if (NULL == pJob) {
126
    qError("acquire job from jobRef list failed, may be dropped, jobId:0x%" PRIx64, job);
127
    fp(NULL, param, TSDB_CODE_SCH_STATUS_ERROR);
128
    return;
D
dapan1121 已提交
129
  }
D
dapan1121 已提交
130

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

D
dapan1121 已提交
137
  schReleaseJob(job);
D
dapan 已提交
138
}
D
dapan1121 已提交
139

D
dapan1121 已提交
140 141 142 143 144 145 146 147 148 149
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 已提交
150
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
D
dapan1121 已提交
151 152 153 154
  }

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

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

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

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

  schReleaseJob(job);

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

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

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

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

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

D
dapan1121 已提交
185 186 187 188 189 190 191 192
void schedulerStopQueryHb(void *pTrans) {
  if (NULL == pTrans) {
    return;
  }

  schCleanClusterHb(pTrans);
}

D
dapan1121 已提交
193
void schedulerFreeJob(int64_t job) {
D
dapan1121 已提交
194
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
195
  if (NULL == pJob) {
196
    qError("acquire job from jobRef list failed, may be dropped, jobId:0x%" PRIx64, job);
D
dapan 已提交
197 198
    return;
  }
D
dapan1121 已提交
199

D
dapan1121 已提交
200 201
  if (atomic_load_8(&pJob->userFetch) > 0) {
    schProcessOnJobDropped(pJob, TSDB_CODE_QRY_JOB_FREED);
D
dapan1121 已提交
202
  }
D
dapan1121 已提交
203

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

D
dapan1121 已提交
206 207
  if (taosRemoveRef(schMgmt.jobRef, job)) {
    SCH_JOB_ELOG("remove job from job list failed, refId:%" PRIx64, job);
208
  }
D
dapan1121 已提交
209 210

  schReleaseJob(job);
D
dapan1121 已提交
211
}
D
dapan1121 已提交
212

D
dapan1121 已提交
213
void schedulerDestroy(void) {
214 215
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

D
dapan1121 已提交
216
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
217
    SSchJob *pJob = taosIterateRef(schMgmt.jobRef, 0);
H
Hongze Cheng 已提交
218
    int64_t  refId = 0;
C
Cary Xu 已提交
219

D
dapan1121 已提交
220
    while (pJob) {
D
dapan1121 已提交
221
      refId = pJob->refId;
C
Cary Xu 已提交
222 223 224
      if (refId == 0) {
        break;
      }
D
dapan1121 已提交
225
      taosRemoveRef(schMgmt.jobRef, pJob->refId);
L
Liu Jicong 已提交
226

D
dapan1121 已提交
227
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
228
    }
D
dapan1121 已提交
229
  }
D
dapan1121 已提交
230

D
dapan1121 已提交
231
  SCH_LOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
232
  if (schMgmt.hbConnections) {
H
Hongze Cheng 已提交
233
    void *pIter = taosHashIterate(schMgmt.hbConnections, NULL);
D
dapan1121 已提交
234 235 236 237
    while (pIter != NULL) {
      SSchHbTrans *hb = pIter;
      schFreeRpcCtx(&hb->rpcCtx);
      pIter = taosHashIterate(schMgmt.hbConnections, pIter);
H
Hongze Cheng 已提交
238
    }
D
dapan1121 已提交
239 240 241
    taosHashCleanup(schMgmt.hbConnections);
    schMgmt.hbConnections = NULL;
  }
D
dapan1121 已提交
242
  SCH_UNLOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
243
}