scheduler.c 4.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 "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(SSchedulerCfg *cfg) {
D
dapan1121 已提交
26
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
27 28 29 30 31 32
    qError("scheduler already initialized");
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  if (cfg) {
    schMgmt.cfg = *cfg;
L
Liu Jicong 已提交
33

D
dapan1121 已提交
34
    if (schMgmt.cfg.maxJobNum == 0) {
D
dapan1121 已提交
35
      schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_MAX_JOB_NUM;
D
dapan1121 已提交
36
    }
D
dapan1121 已提交
37 38 39
    if (schMgmt.cfg.maxNodeTableNum <= 0) {
      schMgmt.cfg.maxNodeTableNum = SCHEDULE_DEFAULT_MAX_NODE_TABLE_NUM;
    }
D
dapan1121 已提交
40
  } else {
D
dapan1121 已提交
41 42
    schMgmt.cfg.maxJobNum = SCHEDULE_DEFAULT_MAX_JOB_NUM;
    schMgmt.cfg.maxNodeTableNum = SCHEDULE_DEFAULT_MAX_NODE_TABLE_NUM;
D
dapan1121 已提交
43
  }
L
Liu Jicong 已提交
44

D
dapan1121 已提交
45 46
  schMgmt.jobRef = taosOpenRef(schMgmt.cfg.maxJobNum, schFreeJobImpl);
  if (schMgmt.jobRef < 0) {
D
dapan1121 已提交
47 48 49 50 51 52 53
    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 已提交
54 55 56
    SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
57
  if (taosGetSystemUUID((char *)&schMgmt.sId, sizeof(schMgmt.sId))) {
D
dapan1121 已提交
58 59 60 61
    qError("generate schdulerId failed, errno:%d", errno);
    SCH_ERR_RET(TSDB_CODE_QRY_SYS_ERROR);
  }

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

D
dapan1121 已提交
64 65 66
  return TSDB_CODE_SUCCESS;
}

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

  int32_t code = 0;  
  SSchJob *pJob = NULL;

D
dapan1121 已提交
73
  SCH_ERR_JRET(schInitJob(pJobId, pReq));
D
dapan1121 已提交
74

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

D
dapan1121 已提交
77
  SCH_ERR_JRET(schSwitchJobStatus(pJob, JOB_TASK_STATUS_INIT, pReq));
D
dapan1121 已提交
78

D
dapan1121 已提交
79
  SCH_ERR_JRET(schSwitchJobStatus(pJob, JOB_TASK_STATUS_EXEC, pReq));
D
dapan1121 已提交
80 81

_return:
D
dapan1121 已提交
82
  
D
dapan1121 已提交
83
  SCH_RET(schHandleOpEndEvent(pJob, SCH_OP_EXEC, pReq, code));
D
dapan1121 已提交
84 85
}

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

L
Liu Jicong 已提交
89
  int32_t  code = 0;
D
dapan1121 已提交
90
  SSchJob *pJob = NULL;
D
dapan1121 已提交
91

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

D
dapan1121 已提交
94
  SCH_ERR_JRET(schJobFetchRows(pJob));
D
dapan1121 已提交
95 96

_return:
L
Liu Jicong 已提交
97

D
dapan1121 已提交
98
  SCH_RET(schHandleOpEndEvent(pJob, SCH_OP_FETCH, pReq, code));
D
dapan 已提交
99
}
D
dapan1121 已提交
100

D
dapan1121 已提交
101
int32_t schedulerGetTasksStatus(int64_t jobId, SArray *pSub) {
D
dapan1121 已提交
102
  int32_t  code = 0;
D
dapan1121 已提交
103
  SSchJob *pJob = NULL;
D
dapan1121 已提交
104

D
dapan1121 已提交
105
  SCH_ERR_JRET(schHandleOpBeginEvent(jobId, &pJob, SCH_OP_GET_STATUS, NULL));
D
dapan1121 已提交
106 107 108

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

D
dapan1121 已提交
110
    for (int32_t m = 0; m < pLevel->taskNum; ++m) {
X
Xiaoyu Wang 已提交
111
      SSchTask     *pTask = taosArrayGet(pLevel->subTasks, m);
112 113 114
      SQuerySubDesc subDesc = {0};
      subDesc.tid = pTask->taskId;
      strcpy(subDesc.status, jobTaskStatusStr(pTask->status));
H
Hongze Cheng 已提交
115

D
dapan1121 已提交
116 117 118 119
      taosArrayPush(pSub, &subDesc);
    }
  }

D
dapan1121 已提交
120 121
_return:

D
dapan1121 已提交
122
  SCH_RET(schHandleOpEndEvent(pJob, SCH_OP_GET_STATUS, NULL, code));
D
dapan1121 已提交
123 124
}

D
dapan1121 已提交
125 126 127 128 129 130 131 132
void schedulerStopQueryHb(void *pTrans) {
  if (NULL == pTrans) {
    return;
  }

  schCleanClusterHb(pTrans);
}

D
dapan1121 已提交
133 134
void schedulerFreeJob(int64_t* jobId, int32_t errCode) {
  if (0 == *jobId) {
D
dapan1121 已提交
135 136
    return;
  }
D
dapan1121 已提交
137 138

  SSchJob *pJob = schAcquireJob(*jobId);
D
dapan1121 已提交
139
  if (NULL == pJob) {
D
dapan1121 已提交
140
    qError("Acquire sch job failed, may be dropped, jobId:0x%" PRIx64, *jobId);
D
dapan 已提交
141 142
    return;
  }
D
dapan1121 已提交
143

D
dapan1121 已提交
144
  schSwitchJobStatus(pJob, JOB_TASK_STATUS_DROP, (void*)&errCode);
D
dapan1121 已提交
145 146
  
  schReleaseJob(*jobId);
D
dapan1121 已提交
147
  *jobId = 0;
D
dapan1121 已提交
148
}
D
dapan1121 已提交
149

D
dapan1121 已提交
150
void schedulerDestroy(void) {
151 152
  atomic_store_8((int8_t *)&schMgmt.exit, 1);

D
dapan1121 已提交
153
  if (schMgmt.jobRef >= 0) {
D
dapan1121 已提交
154
    SSchJob *pJob = taosIterateRef(schMgmt.jobRef, 0);
H
Hongze Cheng 已提交
155
    int64_t  refId = 0;
C
Cary Xu 已提交
156

D
dapan1121 已提交
157
    while (pJob) {
D
dapan1121 已提交
158
      refId = pJob->refId;
C
Cary Xu 已提交
159 160 161
      if (refId == 0) {
        break;
      }
D
dapan1121 已提交
162
      taosRemoveRef(schMgmt.jobRef, pJob->refId);
L
Liu Jicong 已提交
163

D
dapan1121 已提交
164
      pJob = taosIterateRef(schMgmt.jobRef, refId);
D
dapan1121 已提交
165
    }
D
dapan1121 已提交
166
  }
D
dapan1121 已提交
167

D
dapan1121 已提交
168
  SCH_LOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
169
  if (schMgmt.hbConnections) {
H
Hongze Cheng 已提交
170
    void *pIter = taosHashIterate(schMgmt.hbConnections, NULL);
D
dapan1121 已提交
171 172 173 174
    while (pIter != NULL) {
      SSchHbTrans *hb = pIter;
      schFreeRpcCtx(&hb->rpcCtx);
      pIter = taosHashIterate(schMgmt.hbConnections, pIter);
H
Hongze Cheng 已提交
175
    }
D
dapan1121 已提交
176 177 178
    taosHashCleanup(schMgmt.hbConnections);
    schMgmt.hbConnections = NULL;
  }
D
dapan1121 已提交
179
  SCH_UNLOCK(SCH_WRITE, &schMgmt.hbLock);
D
dapan1121 已提交
180
}