clientInt.h 10.9 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"
D
dapan1121 已提交
25
#include "catalog.h"
dengyihao's avatar
dengyihao 已提交
26 27
#include "query.h"
#include "taos.h"
L
Liu Jicong 已提交
28
#include "tcommon.h"
H
Haojun Liao 已提交
29
#include "tdatablock.h"
30
#include "tdef.h"
31 32
#include "thash.h"
#include "tlist.h"
dengyihao's avatar
dengyihao 已提交
33
#include "tmsg.h"
34
#include "tmsgtype.h"
35
#include "trpc.h"
36

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

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

47
#define ERROR_MSG_BUF_DEFAULT_SIZE 512
L
Liu Jicong 已提交
48
#define HEARTBEAT_INTERVAL         1500  // ms
49
#define SYNC_ON_TOP_OF_ASYNC       1
50

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

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

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

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

L
Liu Jicong 已提交
78
typedef struct {
L
Liu Jicong 已提交
79
  int8_t inited;
D
dapan1121 已提交
80
  int64_t       appId;
L
Liu Jicong 已提交
81
  // ctl
L
Liu Jicong 已提交
82 83
  int8_t        threadStop;
  TdThread      thread;
wafwerar's avatar
wafwerar 已提交
84
  TdThreadMutex lock;       // used when app init and cleanup
D
dapan1121 已提交
85
  SHashObj     *appSummary;
L
Liu Jicong 已提交
86
  SArray*       appHbMgrs;  // SArray<SAppHbMgr*> one for each cluster
D
dapan1121 已提交
87 88
  FHbReqHandle  reqHandle[CONN_TYPE__MAX];
  FHbRspHandle  rspHandle[CONN_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 98
} SQueryExecMetric;

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

L
Liu Jicong 已提交
102
struct SAppInstInfo {
D
dapan1121 已提交
103 104 105 106 107 108 109 110 111
  int64_t            numOfConns;
  SCorEpSet          mgmtEp;
  TdThreadMutex      qnodeMutex;
  SArray*            pQnodeList;
  SAppClusterSummary summary;
  SList*             pConnList;  // STscObj linked list
  uint64_t           clusterId;
  void*              pTransporter;
  SAppHbMgr*         pAppHbMgr;
L
Liu Jicong 已提交
112
};
113 114

typedef struct SAppInfo {
L
Liu Jicong 已提交
115 116 117 118 119 120
  int64_t       startTime;
  char          appName[TSDB_APP_NAME_LEN];
  char*         ep;
  int32_t       pid;
  int32_t       numOfThreads;
  SHashObj*     pInstMap;
wafwerar's avatar
wafwerar 已提交
121
  TdThreadMutex mutex;
122 123 124
} SAppInfo;

typedef struct STscObj {
L
Liu Jicong 已提交
125 126 127 128
  char          user[TSDB_USER_LEN];
  char          pass[TSDB_PASSWORD_LEN];
  char          db[TSDB_DB_FNAME_LEN];
  char          ver[128];
L
Liu Jicong 已提交
129
  int8_t        connType;
L
Liu Jicong 已提交
130 131
  int32_t       acctId;
  uint32_t      connId;
D
dapan1121 已提交
132
  TAOS         *id;         // ref ID returned by taosAddRef
L
Liu Jicong 已提交
133 134 135
  TdThreadMutex mutex;      // used to protect the operation on db
  int32_t       numOfReqs;  // number of sqlObj bound to this connection
  SAppInstInfo* pAppInfo;
136
  SHashObj*     pRequests;
137
  int8_t        schemalessType;  // todo remove it, this attribute should be move to request
138 139
} STscObj;

140 141
typedef struct SResultColumn {
  union {
L
Liu Jicong 已提交
142 143
    char*    nullbitmap;  // bitmap, one bit for each item in the list
    int32_t* offset;
144
  };
L
Liu Jicong 已提交
145
  char* pData;
146 147
} SResultColumn;

148
typedef struct SReqResultInfo {
D
dapan1121 已提交
149
  SQueryExecRes  execRes;
150 151
  const char*    pRspMsg;
  const char*    pData;
L
Liu Jicong 已提交
152 153
  TAOS_FIELD*    fields;      // todo, column names are not needed.
  TAOS_FIELD*    userFields;  // the fields info that return to user
154 155
  uint32_t       numOfCols;
  int32_t*       length;
156
  char**         convertBuf;
157 158 159 160 161 162
  TAOS_ROW       row;
  SResultColumn* pCol;
  uint32_t       numOfRows;
  uint64_t       totalRows;
  uint32_t       current;
  bool           completed;
H
Haojun Liao 已提交
163
  int32_t        precision;
164
  bool           convertUcs4;
165
  int32_t        payloadLen;
wmmhello's avatar
wmmhello 已提交
166
  char*          convertJson;
167 168 169
} SReqResultInfo;

typedef struct SRequestSendRecvBody {
170
  tsem_t             rspSem;  // not used now
171 172 173
  __taos_async_fn_t  queryFp;
  __taos_async_fn_t  fetchFp;
  void*              param;
174 175
  SDataBuf           requestMsg;
  int64_t            queryJob;  // query job, created according to sql query DAG.
L
Liu Jicong 已提交
176
  struct SQueryPlan* pDag;      // the query dag, generated according to the sql statement.
177
  SReqResultInfo     resInfo;
178
} SRequestSendRecvBody;
179

L
Liu Jicong 已提交
180
typedef struct {
L
Liu Jicong 已提交
181 182
  int8_t         resType;
  char           topic[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
183
  char           db[TSDB_DB_FNAME_LEN];
L
Liu Jicong 已提交
184 185 186 187 188
  int32_t        vgId;
  SSchemaWrapper schema;
  int32_t        resIter;
  SMqDataBlkRsp  rsp;
  SReqResultInfo resInfo;
L
Liu Jicong 已提交
189
} SMqRspObj;
L
Liu Jicong 已提交
190

191
typedef struct SRequestObj {
L
Liu Jicong 已提交
192
  int8_t               resType;  // query or tmq
dengyihao's avatar
dengyihao 已提交
193 194 195
  uint64_t             requestId;
  int32_t              type;  // request type
  STscObj*             pTscObj;
196
  char*                pDb;     // current database string
dengyihao's avatar
dengyihao 已提交
197 198 199 200
  char*                sqlstr;  // sql string
  int32_t              sqlLen;
  int64_t              self;
  char*                msgBuf;
D
stmt  
dapan1121 已提交
201
  int32_t              msgBufLen;
dengyihao's avatar
dengyihao 已提交
202
  int32_t              code;
D
dapan1121 已提交
203 204
  SArray*              dbList;
  SArray*              tableList;
dengyihao's avatar
dengyihao 已提交
205
  SQueryExecMetric     metric;
H
Haojun Liao 已提交
206
  SRequestSendRecvBody body;
207
  bool                 stableQuery;
208

D
dapan1121 已提交
209
  bool                 killed;
210 211
  uint32_t             prevCode; //previous error code: todo refactor, add update flag for catalog
  uint32_t             retry;
212 213
} SRequestObj;

214 215 216 217 218
typedef struct SSyncQueryParam {
  tsem_t       sem;
  SRequestObj* pRequest;
} SSyncQueryParam;

219
void*   doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
L
Liu Jicong 已提交
220
void*   doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
221

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

L
Liu Jicong 已提交
230
static FORCE_INLINE SReqResultInfo* tmqGetCurResInfo(TAOS_RES* res) {
L
Liu Jicong 已提交
231
  SMqRspObj* msg = (SMqRspObj*)res;
L
Liu Jicong 已提交
232
  return (SReqResultInfo*)&msg->resInfo;
L
Liu Jicong 已提交
233 234
}

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

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

260
extern SAppInfo appInfo;
H
Haojun Liao 已提交
261
extern int32_t  clientReqRefPool;
262
extern int32_t  clientConnRefPool;
263

H
Haojun Liao 已提交
264 265
__async_send_cb_fn_t getMsgRspHandle(int32_t msgType);

H
Haojun Liao 已提交
266
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pReqObj);
267

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

273 274
uint64_t generateRequestId();

275
void*        createRequest(STscObj* pObj, int32_t type);
L
Liu Jicong 已提交
276 277 278
void         destroyRequest(SRequestObj* pRequest);
SRequestObj* acquireRequest(int64_t rid);
int32_t      releaseRequest(int64_t rid);
279

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

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

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

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

291
STscObj* taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db,
L
Liu Jicong 已提交
292
                            uint16_t port, int connType);
293

294 295
SRequestObj* launchQuery(STscObj* pTscObj, const char* sql, int sqlLen);

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

D
dapan1121 已提交
298
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList);
299

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

302 303
void taos_close_internal(void *taos);

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

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

// conn level
L
Liu Jicong 已提交
315
int  hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType);
L
Liu Jicong 已提交
316 317 318 319 320
void hbDeregisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey);

// --- mq
void hbMgrInitMqHbRspHandle();

321
SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res);
H
Haojun Liao 已提交
322
int32_t      scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList);
D
dapan1121 已提交
323
void         launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData *pResultMeta);
L
Liu Jicong 已提交
324
int32_t      refreshMeta(STscObj* pTscObj, SRequestObj* pRequest);
L
Liu Jicong 已提交
325
int32_t      updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList);
326 327 328
void         doAsyncQuery(SRequestObj* pRequest, bool forceUpdateMeta);
int32_t      removeMeta(STscObj* pTscObj, SArray* tbList);// todo move to clientImpl.c and become a static function
int32_t      handleAlterTbExecRes(void* res, struct SCatalog* pCatalog);// todo move to xxx
D
dapan1121 已提交
329
bool         qnodeRequired(SRequestObj* pRequest);
D
stmt  
dapan1121 已提交
330

331 332 333 334 335
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTINT_H