scheduler.c 5.2 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 "query.h"
D
dapan1121 已提交
17
#include "schInt.h"
H
Hongze Cheng 已提交
18
#include "tmsg.h"
D
dapan1121 已提交
19
#include "tref.h"
20

D
dapan1121 已提交
21
SSchedulerMgmt schMgmt = {
22
    .jobRef = -1,
D
dapan1121 已提交
23
};
D
dapan1121 已提交
24

D
dapan1121 已提交
25
int32_t schedulerInit() {
D
dapan1121 已提交
26
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
27 28 29 30
    qError("scheduler already initialized");
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

D
dapan1121 已提交
31 32 33 34
  schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_MAX_JOB_NUM;
  schMgmt.cfg.maxNodeTableNum = SCHEDULE_DEFAULT_MAX_NODE_TABLE_NUM;
  schMgmt.cfg.schPolicy = SCHEDULE_DEFAULT_POLICY;
  schMgmt.cfg.enableReSchedule = true;
L
Liu Jicong 已提交
35

D
dapan1121 已提交
36 37
  qDebug("schedule policy init to %d", schMgmt.cfg.schPolicy);
  
D
dapan1121 已提交
38 39
  schMgmt.jobRef = taosOpenRef(schMgmt.cfg.maxJobNum, schFreeJobImpl);
  if (schMgmt.jobRef < 0) {
D
dapan1121 已提交
40 41 42 43 44 45 46
    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 已提交
47 48 49
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
50
  if (taosGetSystemUUID((char *)&schMgmt.sId, sizeof(schMgmt.sId))) {
D
dapan1121 已提交
51 52 53 54
    qError("generate schdulerId failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR);
  }

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

D
dapan1121 已提交
57 58 59
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
60 61
int32_t schedulerExecJob(SSchedulerReq *pReq, int64_t *pJobId) {
  qDebug("scheduler %s exec job start", pReq->syncReq ? "SYNC" : "ASYNC");
D
dapan1121 已提交
62 63 64 65

  int32_t code = 0;  
  SSchJob *pJob = NULL;

D
dapan1121 已提交
66
  SCH_ERR_JRET(schInitJob(pJobId, pReq));
D
dapan1121 已提交
67

D
dapan1121 已提交
68
  SCH_ERR_JRET(schHandleOpBeginEvent(*pJobId, &pJob, SCH_OP_EXEC, pReq));
D
dapan1121 已提交
69

D
dapan1121 已提交
70
  SCH_ERR_JRET(schSwitchJobStatus(pJob, JOB_TASK_STATUS_INIT, pReq));
D
dapan1121 已提交
71

D
dapan1121 已提交
72
  SCH_ERR_JRET(schSwitchJobStatus(pJob, JOB_TASK_STATUS_EXEC, pReq));
D
dapan1121 已提交
73 74

_return:
D
dapan1121 已提交
75
  
D
dapan1121 已提交
76
  SCH_RET(schHandleOpEndEvent(pJob, SCH_OP_EXEC, pReq, code));
D
dapan1121 已提交
77 78
}

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

L
Liu Jicong 已提交
82
  int32_t  code = 0;
D
dapan1121 已提交
83
  SSchJob *pJob = NULL;
D
dapan1121 已提交
84

D
dapan1121 已提交
85
  SCH_ERR_JRET(schHandleOpBeginEvent(jobId, &pJob, SCH_OP_FETCH, pReq));
D
dapan 已提交
86

D
dapan1121 已提交
87
  SCH_ERR_JRET(schJobFetchRows(pJob));
D
dapan1121 已提交
88 89

_return:
L
Liu Jicong 已提交
90

D
dapan1121 已提交
91
  SCH_RET(schHandleOpEndEvent(pJob, SCH_OP_FETCH, pReq, code));
D
dapan 已提交
92
}
D
dapan1121 已提交
93

D
dapan1121 已提交
94
int32_t schedulerGetTasksStatus(int64_t jobId, SArray *pSub) {
D
dapan1121 已提交
95
  int32_t  code = 0;
D
dapan1121 已提交
96
  SSchJob *pJob = NULL;
D
dapan1121 已提交
97

D
dapan1121 已提交
98
  SCH_ERR_JRET(schHandleOpBeginEvent(jobId, &pJob, SCH_OP_GET_STATUS, NULL));
D
dapan1121 已提交
99 100 101

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

D
dapan1121 已提交
103
    for (int32_t m = 0; m < pLevel->taskNum; ++m) {
X
Xiaoyu Wang 已提交
104
      SSchTask     *pTask = taosArrayGet(pLevel->subTasks, m);
105 106 107
      SQuerySubDesc subDesc = {0};
      subDesc.tid = pTask->taskId;
      strcpy(subDesc.status, jobTaskStatusStr(pTask->status));
H
Hongze Cheng 已提交
108

D
dapan1121 已提交
109 110 111 112
      taosArrayPush(pSub, &subDesc);
    }
  }

D
dapan1121 已提交
113 114
_return:

D
dapan1121 已提交
115
  SCH_RET(schHandleOpEndEvent(pJob, SCH_OP_GET_STATUS, NULL, code));
D
dapan1121 已提交
116 117
}

D
dapan1121 已提交
118 119 120 121 122 123 124 125
void schedulerStopQueryHb(void *pTrans) {
  if (NULL == pTrans) {
    return;
  }

  schCleanClusterHb(pTrans);
}

D
dapan1121 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
int32_t schedulerUpdatePolicy(int32_t policy) {
  switch (policy) {
    case SCH_LOAD_SEQ:
    case SCH_RANDOM:
    case SCH_ALL:
      schMgmt.cfg.schPolicy = policy;
      qDebug("schedule policy updated to %d", schMgmt.cfg.schPolicy);
      break;
    default:
      return TSDB_CODE_TSC_INVALID_INPUT;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t schedulerEnableReSchedule(bool enableResche) {
  schMgmt.cfg.enableReSchedule = enableResche;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
146 147
void schedulerFreeJob(int64_t* jobId, int32_t errCode) {
  if (0 == *jobId) {
D
dapan1121 已提交
148 149
    return;
  }
D
dapan1121 已提交
150 151

  SSchJob *pJob = schAcquireJob(*jobId);
D
dapan1121 已提交
152
  if (NULL == pJob) {
D
dapan1121 已提交
153
    qError("Acquire sch job failed, may be dropped, jobId:0x%" PRIx64, *jobId);
D
dapan 已提交
154 155
    return;
  }
D
dapan1121 已提交
156

D
dapan1121 已提交
157
  schHandleJobDrop(pJob, errCode);
D
dapan1121 已提交
158 159
  
  schReleaseJob(*jobId);
D
dapan1121 已提交
160
  *jobId = 0;
D
dapan1121 已提交
161
}
D
dapan1121 已提交
162

D
dapan1121 已提交
163
void schedulerDestroy(void) {
164 165
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

D
dapan1121 已提交
166
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
167
    SSchJob *pJob = taosIterateRef(schMgmt.jobRef, 0);
H
Hongze Cheng 已提交
168
    int64_t  refId = 0;
C
Cary Xu 已提交
169

D
dapan1121 已提交
170
    while (pJob) {
D
dapan1121 已提交
171
      refId = pJob->refId;
C
Cary Xu 已提交
172 173 174
      if (refId == 0) {
        break;
      }
D
dapan1121 已提交
175
      taosRemoveRef(schMgmt.jobRef, pJob->refId);
L
Liu Jicong 已提交
176

D
dapan1121 已提交
177
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
178
    }
D
dapan1121 已提交
179
  }
D
dapan1121 已提交
180

D
dapan1121 已提交
181
  SCH_LOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
182
  if (schMgmt.hbConnections) {
H
Hongze Cheng 已提交
183
    void *pIter = taosHashIterate(schMgmt.hbConnections, NULL);
D
dapan1121 已提交
184 185 186 187
    while (pIter != NULL) {
      SSchHbTrans *hb = pIter;
      schFreeRpcCtx(&hb->rpcCtx);
      pIter = taosHashIterate(schMgmt.hbConnections, pIter);
H
Hongze Cheng 已提交
188
    }
D
dapan1121 已提交
189 190 191
    taosHashCleanup(schMgmt.hbConnections);
    schMgmt.hbConnections = NULL;
  }
D
dapan1121 已提交
192
  SCH_UNLOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
193
}