qworkerInt.h 8.2 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

D
dapan1121 已提交
23 24
#include "tlockfree.h"

D
dapan1121 已提交
25
#define QWORKER_DEFAULT_SCHEDULER_NUMBER 10000
D
dapan1121 已提交
26
#define QWORKER_DEFAULT_TASK_NUMBER 10000
D
dapan1121 已提交
27 28 29
#define QWORKER_DEFAULT_SCH_TASK_NUMBER 10000

enum {
D
dapan1121 已提交
30 31 32 33 34 35
  QW_PHASE_PRE_QUERY = 1,
  QW_PHASE_POST_QUERY,
  QW_PHASE_PRE_CQUERY,
  QW_PHASE_POST_CQUERY,
  QW_PHASE_PRE_FETCH,
  QW_PHASE_POST_FETCH,
D
dapan1121 已提交
36 37
};

D
dapan1121 已提交
38
enum {
D
dapan1121 已提交
39 40 41 42
  QW_EVENT_CANCEL = 1,
  QW_EVENT_READY,
  QW_EVENT_FETCH,
  QW_EVENT_DROP,
43
  QW_EVENT_CQUERY,
D
dapan1121 已提交
44 45 46 47 48 49 50 51

  QW_EVENT_MAX,
};

enum {
  QW_EVENT_NOT_RECEIVED = 0,
  QW_EVENT_RECEIVED,
  QW_EVENT_PROCESSED,
D
dapan1121 已提交
52 53 54 55 56 57 58
};

enum {
  QW_READ = 1,
  QW_WRITE,
};

D
dapan1121 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
enum {
  QW_EXIST_ACQUIRE = 1,
  QW_EXIST_RET_ERR,
};

enum {
  QW_NOT_EXIST_RET_ERR = 1,
  QW_NOT_EXIST_ADD,
};

enum {
  QW_ADD_RET_ERR = 1,
  QW_ADD_ACQUIRE,
};

74 75 76 77
typedef struct SQWDebug {
  int32_t lockDebug;
} SQWDebug;

D
dapan1121 已提交
78 79 80 81 82 83 84 85
typedef struct SQWMsg {
  void   *node;
  char   *msg;
  int32_t msgLen;
  void   *connection;
} SQWMsg;

typedef struct SQWPhaseInput {
D
dapan1121 已提交
86
  int8_t         status;
D
dapan1121 已提交
87
  int8_t         taskType;
D
dapan1121 已提交
88 89 90
  int32_t        code;
  qTaskInfo_t    taskHandle;
  DataSinkHandle sinkHandle;
D
dapan1121 已提交
91 92 93 94
} SQWPhaseInput;

typedef struct SQWPhaseOutput {
  int32_t rspCode;
D
dapan1121 已提交
95
  bool    needStop;  
D
dapan1121 已提交
96 97 98
} SQWPhaseOutput;


D
dapan1121 已提交
99
typedef struct SQWTaskStatus {  
D
dapan1121 已提交
100 101
  int32_t  code;
  int8_t   status;
D
dapan1121 已提交
102
} SQWTaskStatus;
D
dapan1121 已提交
103

D
dapan1121 已提交
104
typedef struct SQWTaskCtx {
105
  SRWLatch        lock;
D
dapan1121 已提交
106
  int8_t          phase;
D
dapan1121 已提交
107
  int8_t          taskType;
D
dapan1121 已提交
108

D
dapan1121 已提交
109 110 111 112
  void           *readyConnection;
  void           *dropConnection;
  void           *cancelConnection;
  
D
dapan1121 已提交
113
  bool            emptyRes;
D
dapan1121 已提交
114
  int8_t          queryContinue;
D
dapan1121 已提交
115
  int8_t          queryInQueue;
D
dapan1121 已提交
116
  int32_t         rspCode; 
D
dapan1121 已提交
117 118

  int8_t          events[QW_EVENT_MAX];
D
dapan1121 已提交
119
  
120 121
  qTaskInfo_t     taskHandle;
  DataSinkHandle  sinkHandle;
D
dapan1121 已提交
122
} SQWTaskCtx;
D
dapan1121 已提交
123

D
dapan1121 已提交
124
typedef struct SQWSchStatus {
D
dapan1121 已提交
125
  int32_t   lastAccessTs; // timestamp in second
D
dapan1121 已提交
126
  SRWLatch  tasksLock;
127
  SHashObj *tasksHash;   // key:queryId+taskId, value: SQWTaskStatus
D
dapan1121 已提交
128
} SQWSchStatus;
D
dapan1121 已提交
129 130 131

// Qnode/Vnode level task management
typedef struct SQWorkerMgmt {
D
dapan1121 已提交
132 133 134 135 136 137 138 139 140
  SQWorkerCfg      cfg;
  int8_t           nodeType;
  int32_t          nodeId;
  SRWLatch         schLock;
  SRWLatch         ctxLock;
  SHashObj        *schHash;       //key: schedulerId,    value: SQWSchStatus
  SHashObj        *ctxHash;       //key: queryId+taskId, value: SQWTaskCtx
  void            *nodeObj;
  putReqToQueryQFp putToQueueFp;
D
dapan1121 已提交
141 142
} SQWorkerMgmt;

D
dapan1121 已提交
143 144 145 146
#define QW_FPARAMS_DEF SQWorkerMgmt *mgmt, uint64_t sId, uint64_t qId, uint64_t tId
#define QW_IDS() sId, qId, tId
#define QW_FPARAMS() mgmt, QW_IDS()

147 148 149 150
#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)
#define QW_SET_EVENT_PROCESSED(ctx, event) atomic_store_8(&(ctx)->events[event], QW_EVENT_PROCESSED)
D
dapan1121 已提交
151

D
dapan1121 已提交
152 153 154 155 156
#define QW_GET_PHASE(ctx) atomic_load_8(&(ctx)->phase)

#define QW_SET_RSP_CODE(ctx, code) atomic_val_compare_exchange_32(&(ctx)->rspCode, 0, code)

#define QW_IN_EXECUTOR(ctx) (QW_GET_PHASE(ctx) == QW_PHASE_PRE_QUERY || QW_GET_PHASE(ctx) == QW_PHASE_PRE_CQUERY || QW_GET_PHASE(ctx) == QW_PHASE_PRE_FETCH)
D
dapan1121 已提交
157

D
dapan1121 已提交
158 159
#define QW_TASK_NOT_EXIST(code) (TSDB_CODE_QRY_SCH_NOT_EXIST == (code) || TSDB_CODE_QRY_TASK_NOT_EXIST == (code))
#define QW_TASK_ALREADY_EXIST(code) (TSDB_CODE_QRY_TASK_ALREADY_EXIST == (code))
D
dapan1121 已提交
160
#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)
D
dapan1121 已提交
161 162
#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)
D
dapan1121 已提交
163

D
dapan1121 已提交
164 165 166 167
#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 已提交
168 169 170
#define QW_ELOG(param, ...) qError("QW:%p " param, mgmt, __VA_ARGS__)
#define QW_DLOG(param, ...) qDebug("QW:%p " param, mgmt, __VA_ARGS__)

D
dapan1121 已提交
171 172
#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__)
D
dapan1121 已提交
173

D
dapan1121 已提交
174 175 176
#define QW_TASK_ELOG(param, ...) qError("QW:%p QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, qId, tId, __VA_ARGS__)
#define QW_TASK_WLOG(param, ...) qWarn("QW:%p QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, qId, tId, __VA_ARGS__)
#define QW_TASK_DLOG(param, ...) qDebug("QW:%p QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, qId, tId, __VA_ARGS__)
D
dapan1121 已提交
177

D
dapan1121 已提交
178 179 180 181
#define QW_TASK_ELOG_E(param) qError("QW:%p QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, qId, tId)
#define QW_TASK_WLOG_E(param) qWarn("QW:%p QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, qId, tId)
#define QW_TASK_DLOG_E(param) qDebug("QW:%p QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, qId, tId)

D
dapan1121 已提交
182 183 184
#define QW_SCH_TASK_ELOG(param, ...) qError("QW:%p SID:%"PRIx64",QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, sId, qId, tId, __VA_ARGS__)
#define QW_SCH_TASK_WLOG(param, ...) qWarn("QW:%p SID:%"PRIx64",QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, sId, qId, tId, __VA_ARGS__)
#define QW_SCH_TASK_DLOG(param, ...) qDebug("QW:%p SID:%"PRIx64",QID:%"PRIx64",TID:%"PRIx64" " param, mgmt, sId, qId, tId, __VA_ARGS__)
D
dapan1121 已提交
185

186
#define QW_LOCK_DEBUG(...) do { if (gQWDebug.lockDebug) { qDebug(__VA_ARGS__); } } while (0)
D
dapan1121 已提交
187 188 189

#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000

D
dapan1121 已提交
190 191
#define QW_LOCK(type, _lock) do {   \
  if (QW_READ == (type)) {          \
D
dapan1121 已提交
192
    assert(atomic_load_32((_lock)) >= 0);  \
193
    QW_LOCK_DEBUG("QW RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
194
    taosRLockLatch(_lock);           \
195
    QW_LOCK_DEBUG("QW RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
196
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
197
  } else {                                                \
D
dapan1121 已提交
198
    assert(atomic_load_32((_lock)) >= 0);  \
199
    QW_LOCK_DEBUG("QW WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
200
    taosWLockLatch(_lock);                                \
201
    QW_LOCK_DEBUG("QW WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
202
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
203 204
  }                                                       \
} while (0)
D
dapan1121 已提交
205
    
D
dapan1121 已提交
206 207
#define QW_UNLOCK(type, _lock) do {                       \
  if (QW_READ == (type)) {                                \
D
dapan1121 已提交
208
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
209
    QW_LOCK_DEBUG("QW RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
210
    taosRUnLockLatch(_lock);                              \
D
dapan1121 已提交
211
    QW_LOCK_DEBUG("QW RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
212
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
213
  } else {                                                \
D
dapan1121 已提交
214
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
215
    QW_LOCK_DEBUG("QW WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
216
    taosWUnLockLatch(_lock);                              \
D
dapan1121 已提交
217
    QW_LOCK_DEBUG("QW WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
218
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
219 220
  }                                                       \
} while (0)
D
dapan1121 已提交
221

D
dapan1121 已提交
222 223


D
dapan1121 已提交
224 225 226 227 228 229

#ifdef __cplusplus
}
#endif

#endif /*_TD_QWORKER_INT_H_*/