schedulerInt.h 6.2 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57
typedef struct SSchApiStat {

} SSchApiStat;

typedef struct SSchRuntimeStat {

} SSchRuntimeStat;

typedef struct SSchJobStat {

} SSchJobStat;

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


58
typedef struct SSchedulerMgmt {
D
dapan1121 已提交
59 60 61 62 63
  uint64_t       taskId; // sequential taksId
  uint64_t       sId;    // schedulerId
  SSchedulerCfg  cfg;
  SHashObj      *jobs;   // key: queryId, value: SQueryJob*
  SSchedulerStat stat;
64
} SSchedulerMgmt;
65

D
dapan1121 已提交
66 67 68 69 70
typedef struct SSchCallbackParam {
  uint64_t queryId;
  uint64_t taskId;
} SSchCallbackParam;

D
dapan 已提交
71
typedef struct SSchLevel {
D
dapan1121 已提交
72 73 74 75 76 77 78
  int32_t  level;
  int8_t   status;
  SRWLatch lock;
  int32_t  taskFailed;
  int32_t  taskSucceed;
  int32_t  taskNum;
  SArray  *subTasks;  // Element is SQueryTask
D
dapan 已提交
79
} SSchLevel;
D
dapan1121 已提交
80

D
dapan 已提交
81
typedef struct SSchTask {
D
dapan1121 已提交
82
  uint64_t             taskId;         // task id
D
dapan1121 已提交
83
  SRWLatch             lock;           // task lock
D
dapan1121 已提交
84 85 86 87 88
  SSchLevel           *level;          // level
  SSubplan            *plan;           // subplan
  char                *msg;            // operator tree
  int32_t              msgLen;         // msg length
  int8_t               status;         // task status
D
dapan1121 已提交
89 90
  int32_t              lastMsgType;    // last sent msg type
  SQueryNodeAddr       succeedAddr;    // task executed success node address
91 92
  int8_t               candidateIdx;   // current try condidation index
  SArray              *candidateAddrs; // condidate node addresses, element is SQueryNodeAddr
D
dapan1121 已提交
93
  SArray              *execAddrs;      // all tried node for current task, element is SQueryNodeAddr
D
dapan1121 已提交
94 95 96 97
  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*
D
dapan 已提交
98
} SSchTask;
D
dapan1121 已提交
99

D
dapan 已提交
100
typedef struct SSchJobAttr {
D
dapan1121 已提交
101
  bool needFetch;
D
dapan 已提交
102 103 104
  bool syncSchedule;
  bool queryJob;
} SSchJobAttr;
D
dapan1121 已提交
105

D
dapan 已提交
106
typedef struct SSchJob {
107
  uint64_t         queryId;
D
dapan1121 已提交
108
  SSchJobAttr      attr;
109
  int32_t          levelNum;
D
dapan1121 已提交
110 111 112 113 114
  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

115
  int32_t          levelIdx;
D
dapan1121 已提交
116
  SEpSet           dataSrcEps;
D
dapan1121 已提交
117 118 119 120
  SHashObj        *execTasks; // executing tasks, key:taskid, value:SQueryTask*
  SHashObj        *succTasks; // succeed tasks, key:taskid, value:SQueryTask*
  SHashObj        *failTasks; // failed tasks, key:taskid, value:SQueryTask*

D
dapan1121 已提交
121
  int32_t          ref;
D
dapan1121 已提交
122
  int8_t           status;  
D
dapan1121 已提交
123
  SQueryNodeAddr   resNode;
D
dapan 已提交
124
  tsem_t           rspSem;
D
dapan1121 已提交
125
  int8_t           userFetch;
D
dapan 已提交
126
  int32_t          remoteFetch;
D
dapan1121 已提交
127
  SSchTask        *fetchTask;
D
dapan 已提交
128
  int32_t          errCode;
D
dapan1121 已提交
129
  void            *res;         //TODO free it or not
D
dapan1121 已提交
130
  int32_t          resNumOfRows;
131
  const char      *sql;
132
  SQueryProfileSummary summary;
D
dapan 已提交
133
} SSchJob;
D
dapan1121 已提交
134

D
dapan1121 已提交
135
#define SCH_TASK_READY_TO_LUNCH(task) (atomic_load_32(&(task)->childReady) >= taosArrayGetSize((task)->children))
D
dapan1121 已提交
136

D
dapan1121 已提交
137 138
#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 已提交
139
#define SCH_TASK_NO_NEED_DROP(task) ((task)->plan->type == QUERY_TYPE_MODIFY)
140

H
Haojun Liao 已提交
141
#define SCH_SET_TASK_STATUS(task, st) atomic_store_8(&(task)->status, st)
D
dapan1121 已提交
142 143
#define SCH_GET_TASK_STATUS(task) atomic_load_8(&(task)->status)

H
Haojun Liao 已提交
144
#define SCH_SET_JOB_STATUS(job, st) atomic_store_8(&(job)->status, st)
D
dapan1121 已提交
145 146
#define SCH_GET_JOB_STATUS(job) atomic_load_8(&(job)->status)

D
dapan1121 已提交
147 148 149
#define SCH_SET_JOB_TYPE(pAttr, type) (pAttr)->queryJob = ((type) != QUERY_TYPE_MODIFY)
#define SCH_JOB_NEED_FETCH(pAttr) ((pAttr)->queryJob)

H
Haojun Liao 已提交
150 151
#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 已提交
152 153

#define SCH_TASK_ELOG(param, ...) \
154
  qError("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
S
Shengliang Guan 已提交
155
#define SCH_TASK_DLOG(param, ...) \
156
  qDebug("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
S
Shengliang Guan 已提交
157
#define SCH_TASK_WLOG(param, ...) \
158
  qWarn("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
D
dapan1121 已提交
159 160 161 162

#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)
163

D
dapan1121 已提交
164 165 166
#define SCH_LOCK(type, _lock) (SCH_READ == (type) ? taosRLockLatch(_lock) : taosWLockLatch(_lock))
#define SCH_UNLOCK(type, _lock) (SCH_READ == (type) ? taosRUnLockLatch(_lock) : taosWUnLockLatch(_lock))

167

D
dapan1121 已提交
168
static int32_t schLaunchTask(SSchJob *job, SSchTask *task);
D
dapan1121 已提交
169
static int32_t schBuildAndSendMsg(SSchJob *job, SSchTask *task, SQueryNodeAddr *addr, int32_t msgType);
D
dapan 已提交
170

H
refact  
Hongze Cheng 已提交
171 172 173 174
#ifdef __cplusplus
}
#endif

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