schedulerInt.h 6.4 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#ifndef _TD_SCHEDULER_INT_H_
#define _TD_SCHEDULER_INT_H_

#ifdef __cplusplus
extern "C" {
#endif

23 24 25 26
#include "os.h"
#include "tarray.h"
#include "planner.h"
#include "scheduler.h"
27
#include "thash.h"
28

29
#define SCHEDULE_DEFAULT_JOB_NUMBER 1000
D
dapan1121 已提交
30
#define SCHEDULE_DEFAULT_TASK_NUMBER 1000
31

32
#define SCH_MAX_CANDIDATE_EP_NUM TSDB_MAX_REPLICA
D
dapan 已提交
33

D
dapan1121 已提交
34 35 36 37 38
enum {
  SCH_READ = 1,
  SCH_WRITE,
};

D
dapan1121 已提交
39 40 41 42 43
typedef struct SSchTrans {
  void *transInst;
  void *transHandle;
} SSchTrans;

D
dapan1121 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
typedef struct SSchApiStat {

} SSchApiStat;

typedef struct SSchRuntimeStat {

} SSchRuntimeStat;

typedef struct SSchJobStat {

} SSchJobStat;

typedef struct SSchedulerStat {
  SSchApiStat      api;
  SSchRuntimeStat  runtime;
  SSchJobStat      job;
} SSchedulerStat;


63
typedef struct SSchedulerMgmt {
D
dapan1121 已提交
64 65 66
  uint64_t       taskId; // sequential taksId
  uint64_t       sId;    // schedulerId
  SSchedulerCfg  cfg;
D
dapan1121 已提交
67
  int32_t        jobRef;
D
dapan1121 已提交
68
  SSchedulerStat stat;
69
} SSchedulerMgmt;
70

D
dapan1121 已提交
71 72
typedef struct SSchCallbackParam {
  uint64_t queryId;
D
dapan1121 已提交
73
  int64_t  refId;
D
dapan1121 已提交
74 75 76
  uint64_t taskId;
} SSchCallbackParam;

D
dapan 已提交
77
typedef struct SSchLevel {
D
dapan1121 已提交
78 79 80 81 82 83
  int32_t  level;
  int8_t   status;
  SRWLatch lock;
  int32_t  taskFailed;
  int32_t  taskSucceed;
  int32_t  taskNum;
D
dapan1121 已提交
84 85
  int32_t  taskLaunchIdx; // launch startup index
  SArray  *subTasks;      // Element is SQueryTask
D
dapan 已提交
86
} SSchLevel;
D
dapan1121 已提交
87

D
dapan 已提交
88
typedef struct SSchTask {
D
dapan1121 已提交
89
  uint64_t             taskId;         // task id
D
dapan1121 已提交
90
  SRWLatch             lock;           // task lock
D
dapan1121 已提交
91 92 93 94 95
  SSchLevel           *level;          // level
  SSubplan            *plan;           // subplan
  char                *msg;            // operator tree
  int32_t              msgLen;         // msg length
  int8_t               status;         // task status
D
dapan1121 已提交
96 97
  int32_t              lastMsgType;    // last sent msg type
  SQueryNodeAddr       succeedAddr;    // task executed success node address
98 99
  int8_t               candidateIdx;   // current try condidation index
  SArray              *candidateAddrs; // condidate node addresses, element is SQueryNodeAddr
D
dapan1121 已提交
100
  SArray              *execAddrs;      // all tried node for current task, element is SQueryNodeAddr
D
dapan1121 已提交
101 102 103 104
  SQueryProfileSummary summary;        // task execution summary
  int32_t              childReady;     // child task ready number
  SArray              *children;       // the datasource tasks,from which to fetch the result, element is SQueryTask*
  SArray              *parents;        // the data destination tasks, get data from current task, element is SQueryTask*
dengyihao's avatar
dengyihao 已提交
105
  void*               handle;          // task send handle 
D
dapan 已提交
106
} SSchTask;
D
dapan1121 已提交
107

D
dapan 已提交
108
typedef struct SSchJobAttr {
D
dapan1121 已提交
109
  bool needFetch;
D
dapan 已提交
110 111 112
  bool syncSchedule;
  bool queryJob;
} SSchJobAttr;
D
dapan1121 已提交
113

D
dapan 已提交
114
typedef struct SSchJob {
D
dapan1121 已提交
115
  int64_t          refId;
116
  uint64_t         queryId;
D
dapan1121 已提交
117
  SSchJobAttr      attr;
118
  int32_t          levelNum;
D
dapan1121 已提交
119 120 121 122 123
  void            *transport;
  SArray          *nodeList;   // qnode/vnode list, element is SQueryNodeAddr
  SArray          *levels;    // Element is SQueryLevel, starting from 0. SArray<SSchLevel>
  SArray          *subPlans;  // subplan pointer copied from DAG, no need to free it in scheduler

124
  int32_t          levelIdx;
D
dapan1121 已提交
125
  SEpSet           dataSrcEps;
D
dapan1121 已提交
126 127 128 129 130
  SHashObj        *execTasks; // executing tasks, key:taskid, value:SQueryTask*
  SHashObj        *succTasks; // succeed tasks, key:taskid, value:SQueryTask*
  SHashObj        *failTasks; // failed tasks, key:taskid, value:SQueryTask*

  int8_t           status;  
D
dapan1121 已提交
131
  SQueryNodeAddr   resNode;
D
dapan 已提交
132
  tsem_t           rspSem;
D
dapan1121 已提交
133
  int8_t           userFetch;
D
dapan 已提交
134
  int32_t          remoteFetch;
D
dapan1121 已提交
135
  SSchTask        *fetchTask;
D
dapan 已提交
136
  int32_t          errCode;
D
dapan1121 已提交
137
  void            *res;         //TODO free it or not
D
dapan1121 已提交
138
  int32_t          resNumOfRows;
139
  const char      *sql;
140
  SQueryProfileSummary summary;
D
dapan 已提交
141
} SSchJob;
D
dapan1121 已提交
142

D
dapan 已提交
143
#define SCH_TASK_READY_TO_LUNCH(readyNum, task) ((readyNum) >= taosArrayGetSize((task)->children))
D
dapan1121 已提交
144

D
dapan1121 已提交
145 146
#define SCH_IS_DATA_SRC_TASK(task) ((task)->plan->type == QUERY_TYPE_SCAN)
#define SCH_TASK_NEED_WAIT_ALL(task) ((task)->plan->type == QUERY_TYPE_MODIFY)
D
dapan1121 已提交
147
#define SCH_TASK_NO_NEED_DROP(task) ((task)->plan->type == QUERY_TYPE_MODIFY)
148

H
Haojun Liao 已提交
149
#define SCH_SET_TASK_STATUS(task, st) atomic_store_8(&(task)->status, st)
D
dapan1121 已提交
150 151
#define SCH_GET_TASK_STATUS(task) atomic_load_8(&(task)->status)

H
Haojun Liao 已提交
152
#define SCH_SET_JOB_STATUS(job, st) atomic_store_8(&(job)->status, st)
D
dapan1121 已提交
153 154
#define SCH_GET_JOB_STATUS(job) atomic_load_8(&(job)->status)

D
dapan1121 已提交
155 156 157
#define SCH_SET_JOB_TYPE(pAttr, type) (pAttr)->queryJob = ((type) != QUERY_TYPE_MODIFY)
#define SCH_JOB_NEED_FETCH(pAttr) ((pAttr)->queryJob)

H
Haojun Liao 已提交
158 159
#define SCH_JOB_ELOG(param, ...) qError("QID:0x%" PRIx64 " " param, pJob->queryId, __VA_ARGS__)
#define SCH_JOB_DLOG(param, ...) qDebug("QID:0x%" PRIx64 " " param, pJob->queryId, __VA_ARGS__)
S
Shengliang Guan 已提交
160 161

#define SCH_TASK_ELOG(param, ...) \
162
  qError("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
S
Shengliang Guan 已提交
163
#define SCH_TASK_DLOG(param, ...) \
164
  qDebug("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
S
Shengliang Guan 已提交
165
#define SCH_TASK_WLOG(param, ...) \
166
  qWarn("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
D
dapan1121 已提交
167 168 169 170

#define SCH_ERR_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { terrno = _code; return _code; } } while (0)
#define SCH_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { terrno = _code; } return _code; } while (0)
#define SCH_ERR_JRET(c) do { code = c; if (code != TSDB_CODE_SUCCESS) { terrno = code; goto _return; } } while (0)
171

D
dapan1121 已提交
172 173 174
#define SCH_LOCK(type, _lock) (SCH_READ == (type) ? taosRLockLatch(_lock) : taosWLockLatch(_lock))
#define SCH_UNLOCK(type, _lock) (SCH_READ == (type) ? taosRUnLockLatch(_lock) : taosWUnLockLatch(_lock))

175

D
dapan1121 已提交
176
static int32_t schLaunchTask(SSchJob *job, SSchTask *task);
D
dapan1121 已提交
177
static int32_t schBuildAndSendMsg(SSchJob *job, SSchTask *task, SQueryNodeAddr *addr, int32_t msgType);
D
dapan1121 已提交
178 179
SSchJob *schAcquireJob(int64_t refId);
int32_t schReleaseJob(int64_t refId);
D
dapan 已提交
180

H
refact  
Hongze Cheng 已提交
181 182 183 184
#ifdef __cplusplus
}
#endif

D
dapan1121 已提交
185
#endif /*_TD_SCHEDULER_INT_H_*/