qworkerInt.h 11.6 KB
Newer Older
D
dapan1121 已提交
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_QWORKER_INT_H_
#define _TD_QWORKER_INT_H_

#ifdef __cplusplus
extern "C" {
#endif

wafwerar's avatar
wafwerar 已提交
23
#include "osDef.h"
S
Shengliang Guan 已提交
24
#include "qworker.h"
D
dapan1121 已提交
25
#include "tlockfree.h"
D
dapan1121 已提交
26
#include "ttimer.h"
D
dapan1121 已提交
27
#include "tref.h"
D
dapan1121 已提交
28

D
dapan1121 已提交
29
#define QW_DEFAULT_SCHEDULER_NUMBER 10000
dengyihao's avatar
dengyihao 已提交
30 31 32 33
#define QW_DEFAULT_TASK_NUMBER      10000
#define QW_DEFAULT_SCH_TASK_NUMBER  10000
#define QW_DEFAULT_SHORT_RUN_TIMES  2
#define QW_DEFAULT_HEARTBEAT_MSEC   3000
D
dapan1121 已提交
34

D
dapan1121 已提交
35
enum {
D
dapan1121 已提交
36 37 38 39
  QW_PHASE_PRE_QUERY = 1,
  QW_PHASE_POST_QUERY,
  QW_PHASE_PRE_FETCH,
  QW_PHASE_POST_FETCH,
D
dapan1121 已提交
40 41
  QW_PHASE_PRE_CQUERY,
  QW_PHASE_POST_CQUERY,
D
dapan1121 已提交
42 43
};

D
dapan1121 已提交
44
enum {
D
dapan1121 已提交
45 46 47 48
  QW_EVENT_CANCEL = 1,
  QW_EVENT_READY,
  QW_EVENT_FETCH,
  QW_EVENT_DROP,
49
  QW_EVENT_CQUERY,
D
dapan1121 已提交
50 51 52 53 54 55 56 57

  QW_EVENT_MAX,
};

enum {
  QW_EVENT_NOT_RECEIVED = 0,
  QW_EVENT_RECEIVED,
  QW_EVENT_PROCESSED,
D
dapan1121 已提交
58 59 60 61 62 63 64
};

enum {
  QW_READ = 1,
  QW_WRITE,
};

D
dapan1121 已提交
65 66 67 68 69
enum {
  QW_NOT_EXIST_RET_ERR = 1,
  QW_NOT_EXIST_ADD,
};

70
typedef struct SQWDebug {
D
dapan1121 已提交
71 72
  bool lockEnable;
  bool statusEnable;
D
dapan1121 已提交
73
  bool dumpEnable;
74 75
} SQWDebug;

D
dapan1121 已提交
76
typedef struct SQWConnInfo {
dengyihao's avatar
dengyihao 已提交
77 78 79
  void *  handle;
  void *  ahandle;
  int64_t refId;
D
dapan1121 已提交
80 81
} SQWConnInfo;

D
dapan1121 已提交
82
typedef struct SQWMsg {
dengyihao's avatar
dengyihao 已提交
83 84 85 86 87
  void *      node;
  int32_t     code;
  char *      msg;
  int32_t     msgLen;
  SQWConnInfo connInfo;
D
dapan1121 已提交
88 89
} SQWMsg;

D
dapan1121 已提交
90
typedef struct SQWHbParam {
D
dapan1121 已提交
91
  bool    inUse;
D
dapan1121 已提交
92 93 94 95
  int32_t qwrId;
  int64_t refId;
} SQWHbParam;

D
dapan1121 已提交
96
typedef struct SQWHbInfo {
dengyihao's avatar
dengyihao 已提交
97 98
  SSchedulerHbRsp rsp;
  SQWConnInfo     connInfo;
D
dapan1121 已提交
99 100
} SQWHbInfo;

D
dapan1121 已提交
101
typedef struct SQWPhaseInput {
dengyihao's avatar
dengyihao 已提交
102
  int32_t code;
D
dapan1121 已提交
103 104 105
} SQWPhaseInput;

typedef struct SQWPhaseOutput {
wafwerar's avatar
wafwerar 已提交
106 107 108
#ifdef WINDOWS
  size_t avoidCompilationErrors;
#endif
D
dapan1121 已提交
109 110
} SQWPhaseOutput;

dengyihao's avatar
dengyihao 已提交
111 112 113 114
typedef struct SQWTaskStatus {
  int64_t refId;  // job's refId
  int32_t code;
  int8_t  status;
D
dapan1121 已提交
115
} SQWTaskStatus;
D
dapan1121 已提交
116

D
dapan1121 已提交
117
typedef struct SQWTaskCtx {
dengyihao's avatar
dengyihao 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  SRWLatch lock;
  int8_t   phase;
  int8_t   taskType;
  int8_t   explain;

  bool    queryFetched;
  bool    queryEnd;
  bool    queryContinue;
  bool    queryInQueue;
  int32_t rspCode;

  SQWConnInfo ctrlConnInfo;
  SQWConnInfo dataConnInfo;

  int8_t events[QW_EVENT_MAX];

dengyihao's avatar
dengyihao 已提交
134 135
  void *taskHandle;
  void *sinkHandle;
D
dapan1121 已提交
136
} SQWTaskCtx;
D
dapan1121 已提交
137

D
dapan1121 已提交
138
typedef struct SQWSchStatus {
dengyihao's avatar
dengyihao 已提交
139
  int32_t        lastAccessTs;  // timestamp in second
D
dapan1121 已提交
140
  SRWLatch       hbConnLock;
D
dapan1121 已提交
141
  SQWConnInfo    hbConnInfo;
dengyihao's avatar
dengyihao 已提交
142
  SQueryNodeEpId hbEpId;
D
dapan1121 已提交
143
  SRWLatch       tasksLock;
dengyihao's avatar
dengyihao 已提交
144
  SHashObj *     tasksHash;  // key:queryId+taskId, value: SQWTaskStatus
D
dapan1121 已提交
145
} SQWSchStatus;
D
dapan1121 已提交
146 147

// Qnode/Vnode level task management
D
dapan1121 已提交
148 149
typedef struct SQWorker {
  int64_t     refId;
S
Shengliang Guan 已提交
150 151 152
  SQWorkerCfg cfg;
  int8_t      nodeType;
  int32_t     nodeId;
dengyihao's avatar
dengyihao 已提交
153
  void *      timer;
S
Shengliang Guan 已提交
154 155 156
  tmr_h       hbTimer;
  SRWLatch    schLock;
  // SRWLatch ctxLock;
dengyihao's avatar
dengyihao 已提交
157 158 159
  SHashObj *schHash;  // key: schedulerId,    value: SQWSchStatus
  SHashObj *ctxHash;  // key: queryId+taskId, value: SQWTaskCtx
  SMsgCb    msgCb;
D
dapan1121 已提交
160 161 162
} SQWorker;

typedef struct SQWorkerMgmt {
D
dapan1121 已提交
163 164 165 166 167
  SRWLatch   lock;
  int32_t    qwRef;
  int32_t    qwNum;
  SQWHbParam param[1024];
  int32_t    paramIdx;
D
dapan1121 已提交
168 169
} SQWorkerMgmt;

D
dapan1121 已提交
170
#define QW_FPARAMS_DEF SQWorker *mgmt, uint64_t sId, uint64_t qId, uint64_t tId, int64_t rId
dengyihao's avatar
dengyihao 已提交
171 172
#define QW_IDS()       sId, qId, tId, rId
#define QW_FPARAMS()   mgmt, QW_IDS()
D
dapan1121 已提交
173

D
dapan1121 已提交
174 175
#define QW_GET_EVENT_VALUE(ctx, event) atomic_load_8(&(ctx)->events[event])

dengyihao's avatar
dengyihao 已提交
176 177 178
#define QW_IS_EVENT_RECEIVED(ctx, event)   (atomic_load_8(&(ctx)->events[event]) == QW_EVENT_RECEIVED)
#define QW_IS_EVENT_PROCESSED(ctx, event)  (atomic_load_8(&(ctx)->events[event]) == QW_EVENT_PROCESSED)
#define QW_SET_EVENT_RECEIVED(ctx, event)  atomic_store_8(&(ctx)->events[event], QW_EVENT_RECEIVED)
179
#define QW_SET_EVENT_PROCESSED(ctx, event) atomic_store_8(&(ctx)->events[event], QW_EVENT_PROCESSED)
D
dapan1121 已提交
180

D
dapan1121 已提交
181 182
#define QW_GET_PHASE(ctx) atomic_load_8(&(ctx)->phase)

dengyihao's avatar
dengyihao 已提交
183
#define QW_SET_RSP_CODE(ctx, code)    atomic_store_32(&(ctx)->rspCode, code)
D
dapan1121 已提交
184
#define QW_UPDATE_RSP_CODE(ctx, code) atomic_val_compare_exchange_32(&(ctx)->rspCode, 0, code)
D
dapan1121 已提交
185

D
dapan1121 已提交
186
#define QW_IS_QUERY_RUNNING(ctx) (QW_GET_PHASE(ctx) == QW_PHASE_PRE_QUERY || QW_GET_PHASE(ctx) == QW_PHASE_PRE_CQUERY)
D
dapan1121 已提交
187

dengyihao's avatar
dengyihao 已提交
188
#define QW_TASK_NOT_EXIST(code)     (TSDB_CODE_QRY_SCH_NOT_EXIST == (code) || TSDB_CODE_QRY_TASK_NOT_EXIST == (code))
D
dapan1121 已提交
189
#define QW_TASK_ALREADY_EXIST(code) (TSDB_CODE_QRY_TASK_ALREADY_EXIST == (code))
dengyihao's avatar
dengyihao 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
#define QW_TASK_READY(status)                                                                                      \
  (status == JOB_TASK_STATUS_SUCCEED || status == JOB_TASK_STATUS_FAILED || status == JOB_TASK_STATUS_CANCELLED || \
   status == JOB_TASK_STATUS_PARTIAL_SUCCEED)
#define QW_SET_QTID(id, qId, tId)                      \
  do {                                                 \
    *(uint64_t *)(id) = (qId);                         \
    *(uint64_t *)((char *)(id) + sizeof(qId)) = (tId); \
  } while (0)
#define QW_GET_QTID(id, qId, tId)                      \
  do {                                                 \
    (qId) = *(uint64_t *)(id);                         \
    (tId) = *(uint64_t *)((char *)(id) + sizeof(qId)); \
  } while (0)

#define QW_ERR_RET(c)                 \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
      return _code;                   \
    }                                 \
  } while (0)
#define QW_RET(c)                     \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
    }                                 \
    return _code;                     \
  } while (0)
#define QW_ERR_JRET(c)               \
  do {                               \
    code = c;                        \
    if (code != TSDB_CODE_SUCCESS) { \
      terrno = code;                 \
      goto _return;                  \
    }                                \
  } while (0)
D
dapan1121 已提交
228

D
dapan1121 已提交
229 230
#define QW_ELOG(_param, ...) qError("QW:%p " _param, mgmt, __VA_ARGS__)
#define QW_DLOG(_param, ...) qDebug("QW:%p " _param, mgmt, __VA_ARGS__)
D
dapan1121 已提交
231

D
dapan1121 已提交
232
#define QW_DUMP(_param, ...)                      \
dengyihao's avatar
dengyihao 已提交
233 234
  do {                                           \
    if (gQWDebug.dumpEnable) {                   \
D
dapan1121 已提交
235
      qDebug("QW:%p " _param, mgmt, __VA_ARGS__); \
dengyihao's avatar
dengyihao 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    }                                            \
  } while (0)

#define QW_SCH_ELOG(param, ...) qError("QW:%p SID:%" PRIx64 " " param, mgmt, sId, __VA_ARGS__)
#define QW_SCH_DLOG(param, ...) qDebug("QW:%p SID:%" PRIx64 " " param, mgmt, sId, __VA_ARGS__)

#define QW_TASK_ELOG(param, ...) qError("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId, __VA_ARGS__)
#define QW_TASK_WLOG(param, ...) qWarn("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId, __VA_ARGS__)
#define QW_TASK_DLOG(param, ...) qDebug("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId, __VA_ARGS__)
#define QW_TASK_DLOGL(param, ...) \
  qDebugL("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId, __VA_ARGS__)

#define QW_TASK_ELOG_E(param) qError("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId)
#define QW_TASK_WLOG_E(param) qWarn("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId)
#define QW_TASK_DLOG_E(param) qDebug("QW:%p QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, qId, tId)

#define QW_SCH_TASK_ELOG(param, ...) \
  qError("QW:%p SID:0x%" PRIx64 ",QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, sId, qId, tId, __VA_ARGS__)
#define QW_SCH_TASK_WLOG(param, ...) \
  qWarn("QW:%p SID:0x%" PRIx64 ",QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, sId, qId, tId, __VA_ARGS__)
#define QW_SCH_TASK_DLOG(param, ...) \
  qDebug("QW:%p SID:0x%" PRIx64 ",QID:0x%" PRIx64 ",TID:0x%" PRIx64 " " param, mgmt, sId, qId, tId, __VA_ARGS__)

#define QW_LOCK_DEBUG(...)     \
  do {                         \
    if (gQWDebug.lockEnable) { \
      qDebug(__VA_ARGS__);     \
    }                          \
  } while (0)
D
dapan1121 已提交
265 266 267

#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000

dengyihao's avatar
dengyihao 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
#define QW_LOCK(type, _lock)                                                                       \
  do {                                                                                             \
    if (QW_READ == (type)) {                                                                       \
      assert(atomic_load_32((_lock)) >= 0);                                                        \
      QW_LOCK_DEBUG("QW RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      taosRLockLatch(_lock);                                                                       \
      QW_LOCK_DEBUG("QW RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      assert(atomic_load_32((_lock)) > 0);                                                         \
    } else {                                                                                       \
      assert(atomic_load_32((_lock)) >= 0);                                                        \
      QW_LOCK_DEBUG("QW WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      taosWLockLatch(_lock);                                                                       \
      QW_LOCK_DEBUG("QW WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);                               \
    }                                                                                              \
  } while (0)

#define QW_UNLOCK(type, _lock)                                                                      \
  do {                                                                                              \
    if (QW_READ == (type)) {                                                                        \
      assert(atomic_load_32((_lock)) > 0);                                                          \
      QW_LOCK_DEBUG("QW RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      taosRUnLockLatch(_lock);                                                                      \
      QW_LOCK_DEBUG("QW RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      assert(atomic_load_32((_lock)) >= 0);                                                         \
    } else {                                                                                        \
      assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);                                \
      QW_LOCK_DEBUG("QW WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      taosWUnLockLatch(_lock);                                                                      \
      QW_LOCK_DEBUG("QW WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
      assert(atomic_load_32((_lock)) >= 0);                                                         \
    }                                                                                               \
  } while (0)
D
dapan1121 已提交
301

D
dapan1121 已提交
302 303 304

extern SQWorkerMgmt gQwMgmt;

wafwerar's avatar
wafwerar 已提交
305
static FORCE_INLINE SQWorker *qwAcquire(int64_t refId) { return (SQWorker *)taosAcquireRef(atomic_load_32(&gQwMgmt.qwRef), refId); }
D
dapan1121 已提交
306

wafwerar's avatar
wafwerar 已提交
307
static FORCE_INLINE int32_t qwRelease(int64_t refId) { return taosReleaseRef(gQwMgmt.qwRef, refId); }
D
dapan1121 已提交
308 309


D
dapan1121 已提交
310 311 312 313 314
#ifdef __cplusplus
}
#endif

#endif /*_TD_QWORKER_INT_H_*/