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 82
                         int64_t startTs, schedulerExecCallback fp, void* param) {
   if (NULL == pTrans || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == fp || NULL == param) {
D
dapan1121 已提交
83 84 85
     SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
   }
   
D
dapan1121 已提交
86 87
   SSchResInfo resInfo = {.execFp = fp, .userParam = param};                      
   SCH_RET(schAsyncExecJob(pTrans, pNodeList, pDag, pJob, sql, startTs, &resInfo));
D
dapan1121 已提交
88 89
}

L
Liu Jicong 已提交
90
int32_t schedulerFetchRows(int64_t job, void **pData) {
D
dapan1121 已提交
91
  if (NULL == pData) {
D
dapan1121 已提交
92
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan 已提交
93 94
  }

L
Liu Jicong 已提交
95
  int32_t  code = 0;
D
dapan1121 已提交
96
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
97 98 99 100
  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 已提交
101

D
dapan1121 已提交
102 103 104
  pJob->attr.syncSchedule = true;
  pJob->userRes.fetchRes = pData;
  code = schFetchRows(pJob);
D
dapan 已提交
105

D
dapan1121 已提交
106
  schReleaseJob(job);
D
dapan 已提交
107

D
dapan1121 已提交
108 109
  SCH_RET(code);
}
D
dapan1121 已提交
110

D
dapan1121 已提交
111 112 113
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 已提交
114
  }
D
dapan 已提交
115

D
dapan1121 已提交
116 117 118 119 120
  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 已提交
121
  }
D
dapan1121 已提交
122

D
dapan1121 已提交
123 124 125 126 127
  pJob->attr.syncSchedule = false;
  pJob->userRes.fetchFp = fp;
  pJob->userRes.userParam = param;
  
  code = schFetchRows(pJob);
L
Liu Jicong 已提交
128

D
dapan1121 已提交
129
  schReleaseJob(job);
D
dapan 已提交
130

D
dapan1121 已提交
131
  SCH_RET(code);
D
dapan 已提交
132
}
D
dapan1121 已提交
133

D
dapan1121 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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);
    SCH_ERR_RET(TSDB_CODE_SCH_STATUS_ERROR);
  }

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

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

D
dapan1121 已提交
154 155 156 157 158 159 160
      taosArrayPush(pSub, &subDesc);
    }
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
161
int32_t scheduleCancelJob(int64_t job) {
D
dapan1121 已提交
162
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
163 164 165 166
  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 已提交
167

D
dapan1121 已提交
168 169
  int32_t code = schCancelJob(pJob);

D
dapan1121 已提交
170
  schReleaseJob(job);
D
dapan1121 已提交
171 172

  SCH_RET(code);
D
dapan1121 已提交
173 174
}

D
dapan1121 已提交
175
void schedulerFreeJob(int64_t job) {
D
dapan1121 已提交
176
  SSchJob *pJob = schAcquireJob(job);
D
dapan1121 已提交
177
  if (NULL == pJob) {
D
dapan1121 已提交
178
    qDebug("acquire job from jobRef list failed, may be dropped, refId:%" PRIx64, job);
D
dapan 已提交
179 180
    return;
  }
D
dapan1121 已提交
181

D
dapan1121 已提交
182 183
  if (atomic_load_8(&pJob->userFetch) > 0) {
    schProcessOnJobDropped(pJob, TSDB_CODE_QRY_JOB_FREED);
D
dapan1121 已提交
184
  }
D
dapan1121 已提交
185

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

D
dapan1121 已提交
188 189
  if (taosRemoveRef(schMgmt.jobRef, job)) {
    SCH_JOB_ELOG("remove job from job list failed, refId:%" PRIx64, job);
190
  }
D
dapan1121 已提交
191 192

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

D
dapan1121 已提交
195
void schedulerDestroy(void) {
196 197
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

D
dapan1121 已提交
198
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
199
    SSchJob *pJob = taosIterateRef(schMgmt.jobRef, 0);
H
Hongze Cheng 已提交
200
    int64_t  refId = 0;
C
Cary Xu 已提交
201

D
dapan1121 已提交
202
    while (pJob) {
D
dapan1121 已提交
203
      refId = pJob->refId;
C
Cary Xu 已提交
204 205 206
      if (refId == 0) {
        break;
      }
D
dapan1121 已提交
207
      taosRemoveRef(schMgmt.jobRef, pJob->refId);
L
Liu Jicong 已提交
208

D
dapan1121 已提交
209
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
210
    }
D
dapan1121 已提交
211
  }
D
dapan1121 已提交
212 213

  if (schMgmt.hbConnections) {
H
Hongze Cheng 已提交
214
    void *pIter = taosHashIterate(schMgmt.hbConnections, NULL);
D
dapan1121 已提交
215 216 217 218
    while (pIter != NULL) {
      SSchHbTrans *hb = pIter;
      schFreeRpcCtx(&hb->rpcCtx);
      pIter = taosHashIterate(schMgmt.hbConnections, pIter);
H
Hongze Cheng 已提交
219
    }
D
dapan1121 已提交
220 221 222
    taosHashCleanup(schMgmt.hbConnections);
    schMgmt.hbConnections = NULL;
  }
D
dapan1121 已提交
223
}