qworkerInt.h 6.5 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 30 31 32 33 34
#define QWORKER_DEFAULT_SCH_TASK_NUMBER 10000

enum {
  QW_READY_NOT_RECEIVED = 0,
  QW_READY_RECEIVED,
  QW_READY_RESPONSED,
};

D
dapan1121 已提交
35 36 37 38 39 40 41 42 43 44
enum {
  QW_TASK_INFO_STATUS = 1,
  QW_TASK_INFO_READY,
};

enum {
  QW_READ = 1,
  QW_WRITE,
};

D
dapan1121 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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,
};

typedef struct SQWTaskStatus {  
D
dapan1121 已提交
61 62 63 64 65 66
  SRWLatch lock;
  int32_t  code;
  int8_t   status;
  int8_t   ready; 
  bool     cancel;
  bool     drop;
D
dapan1121 已提交
67
} SQWTaskStatus;
D
dapan1121 已提交
68

D
dapan1121 已提交
69
typedef struct SQWTaskCtx {
70
  SRWLatch        lock;
D
dapan1121 已提交
71 72
  int8_t          sinkScheduled;
  int8_t          queryScheduled;
D
dapan1121 已提交
73
  
74
  bool            needRsp;
75 76
  qTaskInfo_t     taskHandle;
  DataSinkHandle  sinkHandle;
D
dapan1121 已提交
77
} SQWTaskCtx;
D
dapan1121 已提交
78

D
dapan1121 已提交
79
typedef struct SQWSchStatus {
D
dapan1121 已提交
80
  int32_t   lastAccessTs; // timestamp in second
D
dapan1121 已提交
81
  SRWLatch  tasksLock;
82
  SHashObj *tasksHash;   // key:queryId+taskId, value: SQWTaskStatus
D
dapan1121 已提交
83
} SQWSchStatus;
D
dapan1121 已提交
84 85 86

// Qnode/Vnode level task management
typedef struct SQWorkerMgmt {
D
dapan1121 已提交
87 88 89 90 91 92 93 94 95
  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 已提交
96 97
} SQWorkerMgmt;

D
dapan1121 已提交
98
#define QW_GOT_RES_DATA(data) (true)
D
dapan1121 已提交
99 100 101 102
#define QW_LOW_RES_DATA(data) (false)

#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 已提交
103
#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 已提交
104 105 106
#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_IDS() sId, qId, tId
D
dapan1121 已提交
107

D
dapan1121 已提交
108 109 110 111
#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 已提交
112 113 114
#define QW_ELOG(param, ...) qError("QW:%p " param, mgmt, __VA_ARGS__)
#define QW_DLOG(param, ...) qDebug("QW:%p " param, mgmt, __VA_ARGS__)

D
dapan1121 已提交
115 116
#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 已提交
117

D
dapan1121 已提交
118 119 120
#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 已提交
121

D
dapan1121 已提交
122 123 124
#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 已提交
125 126 127 128


#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000

D
dapan1121 已提交
129 130
#define QW_LOCK(type, _lock) do {   \
  if (QW_READ == (type)) {          \
D
dapan1121 已提交
131 132 133 134 135
    assert(atomic_load_32((_lock)) >= 0);  \
    qDebug("QW RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
    taosRLockLatch(_lock);           \
    qDebug("QW RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
136
  } else {                                                \
D
dapan1121 已提交
137 138
    assert(atomic_load_32((_lock)) >= 0);  \
    qDebug("QW WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
139
    taosWLockLatch(_lock);                                \
D
dapan1121 已提交
140 141
    qDebug("QW WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
142 143
  }                                                       \
} while (0)
D
dapan1121 已提交
144
    
D
dapan1121 已提交
145 146
#define QW_UNLOCK(type, _lock) do {                       \
  if (QW_READ == (type)) {                                \
D
dapan1121 已提交
147 148
    assert(atomic_load_32((_lock)) > 0);  \
    qDebug("QW RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
149
    taosRUnLockLatch(_lock);                              \
D
dapan1121 已提交
150 151
    qDebug("QW RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
152
  } else {                                                \
D
dapan1121 已提交
153 154
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
    qDebug("QW WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
155
    taosWUnLockLatch(_lock);                              \
D
dapan1121 已提交
156 157
    qDebug("QW WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
158 159
  }                                                       \
} while (0)
D
dapan1121 已提交
160

D
dapan1121 已提交
161 162


D
dapan1121 已提交
163 164 165
int32_t qwAcquireScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch);
int32_t qwAcquireAddScheduler(int32_t rwType, SQWorkerMgmt *mgmt, uint64_t sId, SQWSchStatus **sch);
int32_t qwAcquireTask(SQWorkerMgmt *mgmt, int32_t rwType, SQWSchStatus *sch, uint64_t qId, uint64_t tId, SQWTaskStatus **task);
D
dapan1121 已提交
166

D
dapan1121 已提交
167 168 169 170 171 172

#ifdef __cplusplus
}
#endif

#endif /*_TD_QWORKER_INT_H_*/