schedulerInt.h 5.8 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

D
dapan 已提交
32
#define SCH_MAX_CONDIDATE_EP_NUM TSDB_MAX_REPLICA
D
dapan 已提交
33

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

39
typedef struct SSchedulerMgmt {
D
dapan1121 已提交
40 41
  uint64_t      taskId; // sequential taksId
  uint64_t      sId;    // schedulerId
D
dapan 已提交
42
  SSchedulerCfg cfg;
D
dapan1121 已提交
43
  SHashObj     *jobs;   // key: queryId, value: SQueryJob*
44
} SSchedulerMgmt;
45

D
dapan1121 已提交
46 47 48 49 50
typedef struct SSchCallbackParam {
  uint64_t queryId;
  uint64_t taskId;
} SSchCallbackParam;

D
dapan 已提交
51
typedef struct SSchLevel {
D
dapan1121 已提交
52 53 54 55 56 57 58
  int32_t  level;
  int8_t   status;
  SRWLatch lock;
  int32_t  taskFailed;
  int32_t  taskSucceed;
  int32_t  taskNum;
  SArray  *subTasks;  // Element is SQueryTask
D
dapan 已提交
59
} SSchLevel;
D
dapan1121 已提交
60 61


D
dapan 已提交
62
typedef struct SSchTask {
D
dapan1121 已提交
63
  uint64_t             taskId;         // task id
D
dapan1121 已提交
64
  SRWLatch             lock;           // task lock
D
dapan1121 已提交
65 66 67 68 69
  SSchLevel           *level;          // level
  SSubplan            *plan;           // subplan
  char                *msg;            // operator tree
  int32_t              msgLen;         // msg length
  int8_t               status;         // task status
D
dapan1121 已提交
70 71
  int32_t              lastMsgType;    // last sent msg type
  SQueryNodeAddr       succeedAddr;    // task executed success node address
72 73
  int8_t               candidateIdx;   // current try condidation index
  SArray              *candidateAddrs; // condidate node addresses, element is SQueryNodeAddr
D
dapan1121 已提交
74
  SArray              *execAddrs;      // all tried node for current task, element is SQueryNodeAddr
D
dapan1121 已提交
75 76 77 78
  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 已提交
79
} SSchTask;
D
dapan1121 已提交
80

D
dapan 已提交
81
typedef struct SSchJobAttr {
D
dapan1121 已提交
82
  bool needFetch;
D
dapan 已提交
83 84 85
  bool syncSchedule;
  bool queryJob;
} SSchJobAttr;
D
dapan1121 已提交
86

D
dapan 已提交
87
typedef struct SSchJob {
88
  uint64_t         queryId;
D
dapan1121 已提交
89
  SSchJobAttr      attr;
90
  int32_t          levelNum;
D
dapan1121 已提交
91 92 93 94 95
  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

96
  int32_t          levelIdx;
D
dapan1121 已提交
97
  SEpSet           dataSrcEps;
D
dapan1121 已提交
98 99 100 101
  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 已提交
102
  int32_t          ref;
D
dapan1121 已提交
103
  int8_t           status;  
D
dapan1121 已提交
104
  SQueryNodeAddr   resNode;
D
dapan 已提交
105
  tsem_t           rspSem;
D
dapan1121 已提交
106
  int8_t           userFetch;
D
dapan 已提交
107
  int32_t          remoteFetch;
D
dapan1121 已提交
108
  SSchTask        *fetchTask;
D
dapan 已提交
109
  int32_t          errCode;
D
dapan1121 已提交
110
  void            *res;         //TODO free it or not
D
dapan1121 已提交
111
  int32_t          resNumOfRows;
112
  SQueryProfileSummary summary;
D
dapan 已提交
113
} SSchJob;
D
dapan1121 已提交
114

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

D
dapan1121 已提交
117 118
#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 已提交
119
#define SCH_TASK_NO_NEED_DROP(task) ((task)->plan->type == QUERY_TYPE_MODIFY)
120

H
Haojun Liao 已提交
121
#define SCH_SET_TASK_STATUS(task, st) atomic_store_8(&(task)->status, st)
D
dapan1121 已提交
122 123
#define SCH_GET_TASK_STATUS(task) atomic_load_8(&(task)->status)

H
Haojun Liao 已提交
124
#define SCH_SET_JOB_STATUS(job, st) atomic_store_8(&(job)->status, st)
D
dapan1121 已提交
125 126
#define SCH_GET_JOB_STATUS(job) atomic_load_8(&(job)->status)

D
dapan1121 已提交
127 128 129 130 131 132
#define SCH_SET_JOB_TYPE(pAttr, type) (pAttr)->queryJob = ((type) != QUERY_TYPE_MODIFY)
#define SCH_JOB_NEED_FETCH(pAttr) ((pAttr)->queryJob)

#define SCH_JOB_ELOG(param, ...) qError("QID:%"PRIx64" " param, pJob->queryId, __VA_ARGS__)
#define SCH_JOB_DLOG(param, ...) qDebug("QID:%"PRIx64" " param, pJob->queryId, __VA_ARGS__)

D
dapan1121 已提交
133 134 135
#define SCH_TASK_ELOG(param, ...) qError("QID:%"PRIx64",TID:%"PRId64" " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
#define SCH_TASK_DLOG(param, ...) qDebug("QID:%"PRIx64",TID:%"PRId64" " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
#define SCH_TASK_WLOG(param, ...) qWarn("QID:%"PRIx64",TID:%"PRId64" " param, pJob->queryId, pTask->taskId, __VA_ARGS__)
D
dapan1121 已提交
136 137 138 139

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

D
dapan1121 已提交
141 142 143
#define SCH_LOCK(type, _lock) (SCH_READ == (type) ? taosRLockLatch(_lock) : taosWLockLatch(_lock))
#define SCH_UNLOCK(type, _lock) (SCH_READ == (type) ? taosRUnLockLatch(_lock) : taosWUnLockLatch(_lock))

144

D
dapan1121 已提交
145
static int32_t schLaunchTask(SSchJob *job, SSchTask *task);
D
dapan1121 已提交
146
static int32_t schBuildAndSendMsg(SSchJob *job, SSchTask *task, SQueryNodeAddr *addr, int32_t msgType);
D
dapan 已提交
147

H
refact  
Hongze Cheng 已提交
148 149 150 151
#ifdef __cplusplus
}
#endif

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