clientInt.h 9.5 KB
Newer Older
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 TDENGINE_CLIENTINT_H
#define TDENGINE_CLIENTINT_H

#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
23
#include "parser.h"
24
#include "planner.h"
dengyihao's avatar
dengyihao 已提交
25 26
#include "query.h"
#include "taos.h"
L
Liu Jicong 已提交
27
#include "tcommon.h"
H
Haojun Liao 已提交
28
#include "tdatablock.h"
29
#include "tdef.h"
30 31
#include "thash.h"
#include "tlist.h"
dengyihao's avatar
dengyihao 已提交
32
#include "tmsg.h"
33
#include "tmsgtype.h"
34
#include "trpc.h"
35

S
Shengliang Guan 已提交
36
#include "tconfig.h"
S
Shengliang Guan 已提交
37

38 39
#define CHECK_CODE_GOTO(expr, label) \
  do {                               \
D
dapan1121 已提交
40
    code = expr;                     \
41 42 43 44
    if (TSDB_CODE_SUCCESS != code) { \
      goto label;                    \
    }                                \
  } while (0)
45

L
Liu Jicong 已提交
46 47
#define HEARTBEAT_INTERVAL 1500  // ms

L
Liu Jicong 已提交
48 49 50 51 52
enum {
  CONN_TYPE__QUERY = 1,
  CONN_TYPE__TMQ,
};

L
Liu Jicong 已提交
53 54
typedef struct SAppInstInfo SAppInstInfo;

L
Liu Jicong 已提交
55
typedef struct {
dengyihao's avatar
dengyihao 已提交
56 57
  void*         param;
  SClientHbReq* req;
D
dapan1121 已提交
58 59
} SHbConnInfo;

L
Liu Jicong 已提交
60
typedef struct {
dengyihao's avatar
dengyihao 已提交
61
  char* key;
L
Liu Jicong 已提交
62 63 64 65 66 67 68 69 70 71
  // statistics
  int32_t reportCnt;
  int32_t connKeyCnt;
  int64_t reportBytes;  // not implemented
  int64_t startTime;
  // ctl
  SRWLatch lock;  // lock is used in serialization
  // connection
  SAppInstInfo* pAppInstInfo;
  // info
dengyihao's avatar
dengyihao 已提交
72 73
  SHashObj* activeInfo;  // hash<SClientHbKey, SClientHbReq>
  SHashObj* connInfo;    // hash<SClientHbKey, SHbConnInfo>
L
Liu Jicong 已提交
74 75
} SAppHbMgr;

L
Liu Jicong 已提交
76
typedef int32_t (*FHbRspHandle)(SAppHbMgr* pAppHbMgr, SClientHbRsp* pRsp);
D
dapan1121 已提交
77

dengyihao's avatar
dengyihao 已提交
78
typedef int32_t (*FHbReqHandle)(SClientHbKey* connKey, void* param, SClientHbReq* req);
D
dapan1121 已提交
79

L
Liu Jicong 已提交
80
typedef struct {
L
Liu Jicong 已提交
81 82
  int8_t inited;
  // ctl
L
Liu Jicong 已提交
83 84
  int8_t        threadStop;
  TdThread      thread;
wafwerar's avatar
wafwerar 已提交
85
  TdThreadMutex lock;       // used when app init and cleanup
L
Liu Jicong 已提交
86 87 88
  SArray*       appHbMgrs;  // SArray<SAppHbMgr*> one for each cluster
  FHbReqHandle  reqHandle[HEARTBEAT_TYPE_MAX];
  FHbRspHandle  rspHandle[HEARTBEAT_TYPE_MAX];
L
Liu Jicong 已提交
89 90
} SClientHbMgr;

91
typedef struct SQueryExecMetric {
D
dapan1121 已提交
92 93 94 95
  int64_t start;   // start timestamp, us
  int64_t parsed;  // start to parse, us
  int64_t send;    // start to send to server, us
  int64_t rsp;     // receive response from server, us
96 97
} SQueryExecMetric;

98
typedef struct SInstanceSummary {
dengyihao's avatar
dengyihao 已提交
99 100 101 102 103 104 105 106 107 108
  uint64_t numOfInsertsReq;
  uint64_t numOfInsertRows;
  uint64_t insertElapsedTime;
  uint64_t insertBytes;  // submit to tsdb since launched.

  uint64_t fetchBytes;
  uint64_t queryElapsedTime;
  uint64_t numOfSlowQueries;
  uint64_t totalRequests;
  uint64_t currentRequests;  // the number of SRequestObj
109
} SInstanceSummary;
110 111

typedef struct SHeartBeatInfo {
dengyihao's avatar
dengyihao 已提交
112
  void* pTimer;  // timer, used to send request msg to mnode
113 114
} SHeartBeatInfo;

L
Liu Jicong 已提交
115
struct SAppInstInfo {
L
Liu Jicong 已提交
116 117 118 119 120 121 122
  int64_t          numOfConns;
  SCorEpSet        mgmtEp;
  SInstanceSummary summary;
  SList*           pConnList;  // STscObj linked list
  int64_t          clusterId;
  void*            pTransporter;
  SAppHbMgr*       pAppHbMgr;
L
Liu Jicong 已提交
123
};
124 125

typedef struct SAppInfo {
L
Liu Jicong 已提交
126 127 128 129 130 131
  int64_t       startTime;
  char          appName[TSDB_APP_NAME_LEN];
  char*         ep;
  int32_t       pid;
  int32_t       numOfThreads;
  SHashObj*     pInstMap;
wafwerar's avatar
wafwerar 已提交
132
  TdThreadMutex mutex;
133 134 135
} SAppInfo;

typedef struct STscObj {
L
Liu Jicong 已提交
136 137 138 139
  char          user[TSDB_USER_LEN];
  char          pass[TSDB_PASSWORD_LEN];
  char          db[TSDB_DB_FNAME_LEN];
  char          ver[128];
L
Liu Jicong 已提交
140
  int8_t        connType;
L
Liu Jicong 已提交
141 142 143 144 145 146
  int32_t       acctId;
  uint32_t      connId;
  uint64_t      id;         // ref ID returned by taosAddRef
  TdThreadMutex mutex;      // used to protect the operation on db
  int32_t       numOfReqs;  // number of sqlObj bound to this connection
  SAppInstInfo* pAppInfo;
147 148
} STscObj;

149 150
typedef struct SResultColumn {
  union {
L
Liu Jicong 已提交
151 152
    char*    nullbitmap;  // bitmap, one bit for each item in the list
    int32_t* offset;
153
  };
L
Liu Jicong 已提交
154
  char* pData;
155 156
} SResultColumn;

157
typedef struct SReqResultInfo {
158 159
  const char*    pRspMsg;
  const char*    pData;
L
Liu Jicong 已提交
160 161
  TAOS_FIELD*    fields;      // todo, column names are not needed.
  TAOS_FIELD*    userFields;  // the fields info that return to user
162 163
  uint32_t       numOfCols;
  int32_t*       length;
164
  char**         convertBuf;
165 166 167 168 169 170
  TAOS_ROW       row;
  SResultColumn* pCol;
  uint32_t       numOfRows;
  uint64_t       totalRows;
  uint32_t       current;
  bool           completed;
H
Haojun Liao 已提交
171
  int32_t        precision;
172
  int32_t        payloadLen;
173 174
} SReqResultInfo;

175
typedef struct SShowReqInfo {
dengyihao's avatar
dengyihao 已提交
176 177 178 179
  int64_t execId;  // showId/queryId
  int32_t vgId;
  SArray* pArray;        // SArray<SVgroupInfo>
  int32_t currentIndex;  // current accessed vgroup index.
180 181
} SShowReqInfo;

182
typedef struct SRequestSendRecvBody {
183 184 185 186 187
  tsem_t             rspSem;  // not used now
  void*              fp;
  SShowReqInfo       showInfo;  // todo this attribute will be removed after the query framework being completed.
  SDataBuf           requestMsg;
  int64_t            queryJob;  // query job, created according to sql query DAG.
L
Liu Jicong 已提交
188
  struct SQueryPlan* pDag;      // the query dag, generated according to the sql statement.
189
  SReqResultInfo     resInfo;
190
} SRequestSendRecvBody;
191

dengyihao's avatar
dengyihao 已提交
192
#define ERROR_MSG_BUF_DEFAULT_SIZE 512
193

L
Liu Jicong 已提交
194 195 196 197 198 199 200 201
enum {
  RES_TYPE__QUERY = 1,
  RES_TYPE__TMQ,
};

#define TD_RES_QUERY(res) (*(int8_t*)res == RES_TYPE__QUERY)
#define TD_RES_TMQ(res)   (*(int8_t*)res == RES_TYPE__TMQ)

L
Liu Jicong 已提交
202
typedef struct {
L
Liu Jicong 已提交
203 204 205 206
  int8_t  resType;
  char*   topic;
  SArray* res;  // SArray<SReqResultInfo>
  int32_t resIter;
L
Liu Jicong 已提交
207
  int32_t vgId;
L
Liu Jicong 已提交
208
} SMqRspObj;
L
Liu Jicong 已提交
209

210
typedef struct SRequestObj {
L
Liu Jicong 已提交
211
  int8_t               resType;  // query or tmq
dengyihao's avatar
dengyihao 已提交
212 213 214
  uint64_t             requestId;
  int32_t              type;  // request type
  STscObj*             pTscObj;
X
Xiaoyu Wang 已提交
215
  char*                pDb;
dengyihao's avatar
dengyihao 已提交
216 217 218 219 220 221
  char*                sqlstr;  // sql string
  int32_t              sqlLen;
  int64_t              self;
  char*                msgBuf;
  void*                pInfo;  // sql parse info, generated by parser module
  int32_t              code;
D
dapan1121 已提交
222 223
  SArray*              dbList;
  SArray*              tableList;
dengyihao's avatar
dengyihao 已提交
224
  SQueryExecMetric     metric;
H
Haojun Liao 已提交
225
  SRequestSendRecvBody body;
226 227
} SRequestObj;

L
Liu Jicong 已提交
228
static FORCE_INLINE SReqResultInfo* tmqGetCurResInfo(TAOS_RES* res) {
L
Liu Jicong 已提交
229 230
  SMqRspObj* msg = (SMqRspObj*)res;
  int32_t    resIter = msg->resIter == -1 ? 0 : msg->resIter;
L
Liu Jicong 已提交
231 232 233 234
  return (SReqResultInfo*)taosArrayGet(msg->res, resIter);
}

static FORCE_INLINE SReqResultInfo* tmqGetNextResInfo(TAOS_RES* res) {
L
Liu Jicong 已提交
235
  SMqRspObj* msg = (SMqRspObj*)res;
L
Liu Jicong 已提交
236 237 238 239 240 241 242 243 244 245 246
  if (++msg->resIter < taosArrayGetSize(msg->res)) {
    return (SReqResultInfo*)taosArrayGet(msg->res, msg->resIter);
  }
  return NULL;
}

static FORCE_INLINE SReqResultInfo* tscGetCurResInfo(TAOS_RES* res) {
  if (TD_RES_QUERY(res)) return &(((SRequestObj*)res)->body.resInfo);
  return tmqGetCurResInfo(res);
}

247
extern SAppInfo appInfo;
H
Haojun Liao 已提交
248
extern int32_t  clientReqRefPool;
249
extern int32_t  clientConnRefPool;
250

H
Haojun Liao 已提交
251
extern int (*handleRequestRspFp[TDMT_MAX])(void*, const SDataBuf* pMsg, int32_t code);
dengyihao's avatar
dengyihao 已提交
252
int           genericRspCallback(void* param, const SDataBuf* pMsg, int32_t code);
H
Haojun Liao 已提交
253
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pReqObj);
254

dengyihao's avatar
dengyihao 已提交
255
int taos_init();
256

dengyihao's avatar
dengyihao 已提交
257 258
void* createTscObj(const char* user, const char* auth, const char* db, SAppInstInfo* pAppInfo);
void  destroyTscObj(void* pObj);
259

260 261
uint64_t generateRequestId();

dengyihao's avatar
dengyihao 已提交
262
void* createRequest(STscObj* pObj, __taos_async_fn_t fp, void* param, int32_t type);
263 264
void  destroyRequest(SRequestObj* pRequest);

dengyihao's avatar
dengyihao 已提交
265
char* getDbOfConnection(STscObj* pObj);
266
void  setConnectionDB(STscObj* pTscObj, const char* db);
H
Haojun Liao 已提交
267
void  resetConnectDB(STscObj* pTscObj);
268 269

void taos_init_imp(void);
dengyihao's avatar
dengyihao 已提交
270
int  taos_options_imp(TSDB_OPTION option, const char* str);
271

dengyihao's avatar
dengyihao 已提交
272
void* openTransporter(const char* user, const char* auth, int32_t numOfThreads);
273

dengyihao's avatar
dengyihao 已提交
274
bool persistConnForSpecificMsg(void* parenct, tmsg_t msgType);
275
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet);
276

277 278
void initMsgHandleFp();

dengyihao's avatar
dengyihao 已提交
279
TAOS* taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db,
L
Liu Jicong 已提交
280
                            uint16_t port, int connType);
281

L
Liu Jicong 已提交
282 283
int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery);
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList);
284

dengyihao's avatar
dengyihao 已提交
285
int32_t buildRequest(STscObj* pTscObj, const char* sql, int sqlLen, SRequestObj** pRequest);
286

287
void*   doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
L
Liu Jicong 已提交
288
void    doSetOneRowPtr(SReqResultInfo* pResultInfo);
L
Liu Jicong 已提交
289 290
int32_t setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t numOfCols, int32_t numOfRows,
                         bool convertUcs4);
L
Liu Jicong 已提交
291
void    setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols);
L
Liu Jicong 已提交
292
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4);
293

dengyihao's avatar
dengyihao 已提交
294
// --- heartbeat
L
Liu Jicong 已提交
295 296 297 298 299 300
// global, called by mgmt
int  hbMgrInit();
void hbMgrCleanUp();
int  hbHandleRsp(SClientHbBatchRsp* hbRsp);

// cluster level
dengyihao's avatar
dengyihao 已提交
301 302
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key);
void       appHbMgrCleanup(void);
L
Liu Jicong 已提交
303 304

// conn level
D
dapan1121 已提交
305
int  hbRegisterConn(SAppHbMgr* pAppHbMgr, int32_t connId, int64_t clusterId, int32_t hbType);
L
Liu Jicong 已提交
306 307 308 309 310 311 312
void hbDeregisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey);

int hbAddConnInfo(SAppHbMgr* pAppHbMgr, SClientHbKey connKey, void* key, void* value, int32_t keyLen, int32_t valueLen);

// --- mq
void hbMgrInitMqHbRspHandle();

313 314 315 316 317
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTINT_H