clientInt.h 10.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

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

49 50 51 52 53 54 55 56
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 已提交
57 58
typedef struct SAppInstInfo SAppInstInfo;

L
Liu Jicong 已提交
59
typedef struct {
dengyihao's avatar
dengyihao 已提交
60 61
  void*         param;
  SClientHbReq* req;
D
dapan1121 已提交
62 63
} SHbConnInfo;

L
Liu Jicong 已提交
64
typedef struct {
dengyihao's avatar
dengyihao 已提交
65
  char* key;
L
Liu Jicong 已提交
66 67 68 69 70 71 72 73 74 75
  // 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 已提交
76 77
  SHashObj* activeInfo;  // hash<SClientHbKey, SClientHbReq>
  SHashObj* connInfo;    // hash<SClientHbKey, SHbConnInfo>
L
Liu Jicong 已提交
78 79
} SAppHbMgr;

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

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

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

95
typedef struct SQueryExecMetric {
D
dapan1121 已提交
96 97 98 99
  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
100 101
} SQueryExecMetric;

102
typedef struct SInstanceSummary {
dengyihao's avatar
dengyihao 已提交
103 104 105 106 107 108 109 110 111 112
  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
113
} SInstanceSummary;
114 115

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

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

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

typedef struct STscObj {
L
Liu Jicong 已提交
140 141 142 143
  char          user[TSDB_USER_LEN];
  char          pass[TSDB_PASSWORD_LEN];
  char          db[TSDB_DB_FNAME_LEN];
  char          ver[128];
L
Liu Jicong 已提交
144
  int8_t        connType;
L
Liu Jicong 已提交
145 146 147 148 149 150
  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;
151
  SHashObj*     pRequests;
152 153
} STscObj;

154 155
typedef struct SResultColumn {
  union {
L
Liu Jicong 已提交
156 157
    char*    nullbitmap;  // bitmap, one bit for each item in the list
    int32_t* offset;
158
  };
L
Liu Jicong 已提交
159
  char* pData;
160 161
} SResultColumn;

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

typedef struct SRequestSendRecvBody {
181
  tsem_t             rspSem;  // not used now
182 183 184
  __taos_async_fn_t  queryFp;
  __taos_async_fn_t  fetchFp;
  void*              param;
185 186
  SDataBuf           requestMsg;
  int64_t            queryJob;  // query job, created according to sql query DAG.
L
Liu Jicong 已提交
187
  struct SQueryPlan* pDag;      // the query dag, generated according to the sql statement.
188
  SReqResultInfo     resInfo;
189
} SRequestSendRecvBody;
190

L
Liu Jicong 已提交
191
typedef struct {
L
Liu Jicong 已提交
192 193 194 195 196 197 198
  int8_t         resType;
  char           topic[TSDB_TOPIC_FNAME_LEN];
  int32_t        vgId;
  SSchemaWrapper schema;
  int32_t        resIter;
  SMqDataBlkRsp  rsp;
  SReqResultInfo resInfo;
L
Liu Jicong 已提交
199
} SMqRspObj;
L
Liu Jicong 已提交
200

201
typedef struct SRequestObj {
L
Liu Jicong 已提交
202
  int8_t               resType;  // query or tmq
dengyihao's avatar
dengyihao 已提交
203 204 205
  uint64_t             requestId;
  int32_t              type;  // request type
  STscObj*             pTscObj;
206
  char*                pDb;     // current database string
dengyihao's avatar
dengyihao 已提交
207 208 209 210
  char*                sqlstr;  // sql string
  int32_t              sqlLen;
  int64_t              self;
  char*                msgBuf;
D
stmt  
dapan1121 已提交
211
  int32_t              msgBufLen;
dengyihao's avatar
dengyihao 已提交
212
  int32_t              code;
D
dapan1121 已提交
213 214
  SArray*              dbList;
  SArray*              tableList;
dengyihao's avatar
dengyihao 已提交
215
  SQueryExecMetric     metric;
H
Haojun Liao 已提交
216
  SRequestSendRecvBody body;
217 218
} SRequestObj;

219 220 221 222 223 224
typedef struct SSyncQueryParam {
  tsem_t       sem;
  SRequestObj* pRequest;
} SSyncQueryParam;

void*   doAsyncFetchRow(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
L
Liu Jicong 已提交
225
void*   doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
226

L
Liu Jicong 已提交
227 228 229 230
void    doSetOneRowPtr(SReqResultInfo* pResultInfo);
void    setResPrecision(SReqResultInfo* pResInfo, int32_t precision);
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4);
void    setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols);
L
Liu Jicong 已提交
231
void    doFreeReqResultInfo(SReqResultInfo* pResInfo);
232
SRequestObj* execQuery(STscObj* pTscObj, const char* sql, int sqlLen);
L
Liu Jicong 已提交
233

L
Liu Jicong 已提交
234
static FORCE_INLINE SReqResultInfo* tmqGetCurResInfo(TAOS_RES* res) {
L
Liu Jicong 已提交
235
  SMqRspObj* msg = (SMqRspObj*)res;
L
Liu Jicong 已提交
236
  return (SReqResultInfo*)&msg->resInfo;
L
Liu Jicong 已提交
237 238
}

L
Liu Jicong 已提交
239
static FORCE_INLINE SReqResultInfo* tmqGetNextResInfo(TAOS_RES* res, bool convertUcs4) {
L
Liu Jicong 已提交
240
  SMqRspObj* msg = (SMqRspObj*)res;
L
Liu Jicong 已提交
241 242 243
  msg->resIter++;
  if (msg->resIter < msg->rsp.blockNum) {
    SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)taosArrayGetP(msg->rsp.blockData, msg->resIter);
L
Liu Jicong 已提交
244 245 246
    if (msg->rsp.withSchema) {
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(msg->rsp.blockSchema, msg->resIter);
      setResSchemaInfo(&msg->resInfo, pSW->pSchema, pSW->nCols);
L
Liu Jicong 已提交
247 248 249 250
      taosMemoryFreeClear(msg->resInfo.row);
      taosMemoryFreeClear(msg->resInfo.pCol);
      taosMemoryFreeClear(msg->resInfo.length);
      taosMemoryFreeClear(msg->resInfo.convertBuf);
L
Liu Jicong 已提交
251
    }
L
Liu Jicong 已提交
252 253
    setQueryResultFromRsp(&msg->resInfo, pRetrieve, convertUcs4);
    return &msg->resInfo;
L
Liu Jicong 已提交
254 255 256 257 258 259 260 261 262
  }
  return NULL;
}

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

263
extern SAppInfo appInfo;
H
Haojun Liao 已提交
264
extern int32_t  clientReqRefPool;
265
extern int32_t  clientConnRefPool;
266

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

271
void*    createTscObj(const char* user, const char* auth, const char* db, int32_t connType, SAppInstInfo* pAppInfo);
L
Liu Jicong 已提交
272 273 274
void     destroyTscObj(void* pObj);
STscObj* acquireTscObj(int64_t rid);
int32_t  releaseTscObj(int64_t rid);
275

276 277
uint64_t generateRequestId();

278
void*        createRequest(STscObj* pObj, void* param, int32_t type);
L
Liu Jicong 已提交
279 280 281
void         destroyRequest(SRequestObj* pRequest);
SRequestObj* acquireRequest(int64_t rid);
int32_t      releaseRequest(int64_t rid);
282

dengyihao's avatar
dengyihao 已提交
283
char* getDbOfConnection(STscObj* pObj);
284
void  setConnectionDB(STscObj* pTscObj, const char* db);
H
Haojun Liao 已提交
285
void  resetConnectDB(STscObj* pTscObj);
286

L
Liu Jicong 已提交
287
int taos_options_imp(TSDB_OPTION option, const char* str);
288

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

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

294 295
void initMsgHandleFp();

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

299 300
SRequestObj* launchQuery(STscObj* pTscObj, const char* sql, int sqlLen);

D
stmt  
dapan1121 已提交
301 302
int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtCallback* pStmtCb);

L
Liu Jicong 已提交
303
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList);
304

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

dengyihao's avatar
dengyihao 已提交
307
// --- heartbeat
L
Liu Jicong 已提交
308 309 310 311 312 313
// global, called by mgmt
int  hbMgrInit();
void hbMgrCleanUp();
int  hbHandleRsp(SClientHbBatchRsp* hbRsp);

// cluster level
dengyihao's avatar
dengyihao 已提交
314 315
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key);
void       appHbMgrCleanup(void);
L
Liu Jicong 已提交
316 317

// conn level
L
Liu Jicong 已提交
318
int  hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType);
L
Liu Jicong 已提交
319 320 321 322 323 324 325
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();

326 327
SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res);
void         launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery);
D
dapan 已提交
328
int32_t      scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList, void** res);
L
Liu Jicong 已提交
329
int32_t      refreshMeta(STscObj* pTscObj, SRequestObj* pRequest);
D
stmt  
dapan1121 已提交
330

331 332 333 334 335
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTINT_H