scheduler.c 5.3 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"
D
dapan1121 已提交
20
#include "qworker.h"
21

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

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

D
dapan1121 已提交
32 33 34 35
  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 已提交
36

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

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

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

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

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

  int32_t code = 0;  
  SSchJob *pJob = NULL;

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

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

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

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

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

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

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

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

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

_return:
L
Liu Jicong 已提交
91

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

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

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

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

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

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

D
dapan1121 已提交
114 115
_return:

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

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

  schCleanClusterHb(pTrans);
}

D
dapan1121 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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 已提交
147 148
void schedulerFreeJob(int64_t* jobId, int32_t errCode) {
  if (0 == *jobId) {
D
dapan1121 已提交
149 150
    return;
  }
D
dapan1121 已提交
151 152

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

D
dapan1121 已提交
158 159
  SCH_JOB_DLOG("start to free job 0x%" PRIx64 ", errCode:0x%x", *jobId, errCode);

D
dapan1121 已提交
160
  schHandleJobDrop(pJob, errCode);
D
dapan1121 已提交
161 162
  
  schReleaseJob(*jobId);
D
dapan1121 已提交
163
  *jobId = 0;
D
dapan1121 已提交
164
}
D
dapan1121 已提交
165

D
dapan1121 已提交
166
void schedulerDestroy(void) {
167 168
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

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

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

D
dapan1121 已提交
180
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
181
    }
D
dapan1121 已提交
182
  }
D
dapan1121 已提交
183

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

  qWorkerDestroy(&schMgmt.queryMgmt);
  schMgmt.queryMgmt = NULL;
D
dapan1121 已提交
199
}