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

L
Liu Jicong 已提交
23
#include "catalog.h"
dengyihao's avatar
dengyihao 已提交
24
#include "parser.h"
25
#include "planner.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 "tmisce.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
#define ERROR_MSG_BUF_DEFAULT_SIZE 512
L
Liu Jicong 已提交
40
#define HEARTBEAT_INTERVAL         1500  // ms
41

42 43 44
enum {
  RES_TYPE__QUERY = 1,
  RES_TYPE__TMQ,
L
Liu Jicong 已提交
45
  RES_TYPE__TMQ_META,
46
  RES_TYPE__TMQ_METADATA,
47 48
};

L
Liu Jicong 已提交
49
#define SHOW_VARIABLES_RESULT_COLS       2
D
dapan1121 已提交
50 51 52
#define SHOW_VARIABLES_RESULT_FIELD1_LEN (TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE)
#define SHOW_VARIABLES_RESULT_FIELD2_LEN (TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE)

X
Xiaoyu Wang 已提交
53 54 55
#define TD_RES_QUERY(res)        (*(int8_t*)res == RES_TYPE__QUERY)
#define TD_RES_TMQ(res)          (*(int8_t*)res == RES_TYPE__TMQ)
#define TD_RES_TMQ_META(res)     (*(int8_t*)res == RES_TYPE__TMQ_META)
56
#define TD_RES_TMQ_METADATA(res) (*(int8_t*)res == RES_TYPE__TMQ_METADATA)
57

L
Liu Jicong 已提交
58 59
typedef struct SAppInstInfo SAppInstInfo;

L
Liu Jicong 已提交
60
typedef struct {
D
dapan1121 已提交
61 62
  char*   key;
  int32_t idx;
L
Liu Jicong 已提交
63 64 65
  // statistics
  int32_t reportCnt;
  int32_t connKeyCnt;
K
kailixu 已提交
66
  int32_t passKeyCnt;   // with passVer call back
L
Liu Jicong 已提交
67 68 69
  int64_t reportBytes;  // not implemented
  int64_t startTime;
  // ctl
L
Liu Jicong 已提交
70
  SRWLatch      lock;  // lock is used in serialization
L
Liu Jicong 已提交
71
  SAppInstInfo* pAppInstInfo;
L
Liu Jicong 已提交
72
  SHashObj*     activeInfo;  // hash<SClientHbKey, SClientHbReq>
L
Liu Jicong 已提交
73 74
} SAppHbMgr;

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

K
kailixu 已提交
77
typedef int32_t (*FHbReqHandle)(SClientHbKey* connKey, void* param, SClientHbReq* req, int32_t cb);
D
dapan1121 已提交
78

L
Liu Jicong 已提交
79
typedef struct {
L
Liu Jicong 已提交
80 81
  int8_t  inited;
  int64_t appId;
L
Liu Jicong 已提交
82
  // ctl
L
Liu Jicong 已提交
83 84
  int8_t        threadStop;
  TdThread      thread;
L
Liu Jicong 已提交
85 86
  TdThreadMutex lock;  // used when app init and cleanup
  SHashObj*     appSummary;
L
Liu Jicong 已提交
87
  SArray*       appHbMgrs;  // SArray<SAppHbMgr*> one for each cluster
D
dapan1121 已提交
88 89
  FHbReqHandle  reqHandle[CONN_TYPE__MAX];
  FHbRspHandle  rspHandle[CONN_TYPE__MAX];
L
Liu Jicong 已提交
90 91
} SClientHbMgr;

92
typedef struct SQueryExecMetric {
93 94
  int64_t start;        // start timestamp, us
  int64_t ctgStart;     // start to parse, us
95 96 97 98 99 100 101
  int64_t execStart;     // start to parse, us

  int64_t parseCostUs;   
  int64_t ctgCostUs;
  int64_t analyseCostUs;
  int64_t planCostUs;
  int64_t execCostUs;
102 103
} SQueryExecMetric;

L
Liu Jicong 已提交
104
struct SAppInstInfo {
D
dapan1121 已提交
105 106
  int64_t            numOfConns;
  SCorEpSet          mgmtEp;
D
dapan1121 已提交
107 108
  int32_t            totalDnodes;
  int32_t            onlineDnodes;
D
dapan1121 已提交
109 110 111 112 113 114 115
  TdThreadMutex      qnodeMutex;
  SArray*            pQnodeList;
  SAppClusterSummary summary;
  SList*             pConnList;  // STscObj linked list
  uint64_t           clusterId;
  void*              pTransporter;
  SAppHbMgr*         pAppHbMgr;
D
dapan1121 已提交
116
  char*              instKey;
L
Liu Jicong 已提交
117
};
118 119

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

K
kailixu 已提交
129
typedef struct {
K
kailixu 已提交
130 131 132
  int32_t            ver;
  void*              param;
  __taos_notify_fn_t fp;
K
kailixu 已提交
133 134
} SPassInfo;

135
typedef struct STscObj {
L
Liu Jicong 已提交
136 137 138
  char          user[TSDB_USER_LEN];
  char          pass[TSDB_PASSWORD_LEN];
  char          db[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
139 140
  char          sVer[TSDB_VERSION_LEN];
  char          sDetailVer[128];
141
  int8_t        sysInfo;
L
Liu Jicong 已提交
142
  int8_t        connType;
L
Liu Jicong 已提交
143 144
  int32_t       acctId;
  uint32_t      connId;
D
dapan1121 已提交
145
  int64_t       id;         // ref ID returned by taosAddRef
L
Liu Jicong 已提交
146 147 148
  TdThreadMutex mutex;      // used to protect the operation on db
  int32_t       numOfReqs;  // number of sqlObj bound to this connection
  SAppInstInfo* pAppInfo;
149
  SHashObj*     pRequests;
K
kailixu 已提交
150
  SPassInfo     passInfo;
151 152
} STscObj;

D
dapan1121 已提交
153 154 155 156
typedef struct STscDbg {
  bool memEnable;
} STscDbg;

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

165
typedef struct SReqResultInfo {
D
dapan1121 已提交
166
  SExecResult    execRes;
167 168
  const char*    pRspMsg;
  const char*    pData;
L
Liu Jicong 已提交
169 170
  TAOS_FIELD*    fields;      // todo, column names are not needed.
  TAOS_FIELD*    userFields;  // the fields info that return to user
171 172
  uint32_t       numOfCols;
  int32_t*       length;
173
  char**         convertBuf;
174 175
  TAOS_ROW       row;
  SResultColumn* pCol;
176
  uint64_t       numOfRows; // from int32_t change to int64_t
177
  uint64_t       totalRows;
178
  uint64_t       current;
179
  bool           localResultFetched;
180
  bool           completed;
H
Haojun Liao 已提交
181
  int32_t        precision;
182
  bool           convertUcs4;
183
  int32_t        payloadLen;
wmmhello's avatar
wmmhello 已提交
184
  char*          convertJson;
185 186 187
} SReqResultInfo;

typedef struct SRequestSendRecvBody {
dengyihao's avatar
dengyihao 已提交
188 189 190
  tsem_t            rspSem;  // not used now
  __taos_async_fn_t queryFp;
  __taos_async_fn_t fetchFp;
D
dapan1121 已提交
191
  EQueryExecMode    execMode;
dengyihao's avatar
dengyihao 已提交
192 193 194 195 196
  void*             param;
  SDataBuf          requestMsg;
  int64_t           queryJob;  // query job, created according to sql query DAG.
  int32_t           subplanNum;
  SReqResultInfo    resInfo;
197
} SRequestSendRecvBody;
198

L
Liu Jicong 已提交
199
typedef struct {
L
Liu Jicong 已提交
200 201
  int8_t         resType;
  char           topic[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
202
  char           db[TSDB_DB_FNAME_LEN];
L
Liu Jicong 已提交
203 204 205 206
  int32_t        vgId;
  SSchemaWrapper schema;
  int32_t        resIter;
  SReqResultInfo resInfo;
L
Liu Jicong 已提交
207
  SMqDataRsp     rsp;
L
Liu Jicong 已提交
208
} SMqRspObj;
L
Liu Jicong 已提交
209

L
Liu Jicong 已提交
210 211 212 213 214 215 216 217
typedef struct {
  int8_t     resType;
  char       topic[TSDB_TOPIC_FNAME_LEN];
  char       db[TSDB_DB_FNAME_LEN];
  int32_t    vgId;
  SMqMetaRsp metaRsp;
} SMqMetaRspObj;

L
Liu Jicong 已提交
218 219 220 221 222 223 224 225 226 227 228
typedef struct {
  int8_t         resType;
  char           topic[TSDB_TOPIC_FNAME_LEN];
  char           db[TSDB_DB_FNAME_LEN];
  int32_t        vgId;
  SSchemaWrapper schema;
  int32_t        resIter;
  SReqResultInfo resInfo;
  STaosxRsp      rsp;
} SMqTaosxRspObj;

229
typedef struct SRequestObj {
L
Liu Jicong 已提交
230
  int8_t               resType;  // query or tmq
dengyihao's avatar
dengyihao 已提交
231 232 233
  uint64_t             requestId;
  int32_t              type;  // request type
  STscObj*             pTscObj;
234
  char*                pDb;     // current database string
dengyihao's avatar
dengyihao 已提交
235 236 237 238
  char*                sqlstr;  // sql string
  int32_t              sqlLen;
  int64_t              self;
  char*                msgBuf;
D
stmt  
dapan1121 已提交
239
  int32_t              msgBufLen;
dengyihao's avatar
dengyihao 已提交
240
  int32_t              code;
D
dapan1121 已提交
241 242
  SArray*              dbList;
  SArray*              tableList;
D
dapan1121 已提交
243
  SArray*              targetTableList;
dengyihao's avatar
dengyihao 已提交
244
  SQueryExecMetric     metric;
H
Haojun Liao 已提交
245
  SRequestSendRecvBody body;
D
dapan1121 已提交
246
  int32_t              stmtType;
H
Hongze Cheng 已提交
247 248 249
  bool                 syncQuery;     // todo refactor: async query object
  bool                 stableQuery;   // todo refactor
  bool                 validateOnly;  // todo refactor
H
Haojun Liao 已提交
250
  bool                 killed;
D
dapan1121 已提交
251
  bool                 inRetry;
H
Haojun Liao 已提交
252 253
  uint32_t             prevCode;  // previous error code: todo refactor, add update flag for catalog
  uint32_t             retry;
254
  int64_t              allocatorRefId;
X
Xiaoyu Wang 已提交
255
  SQuery*              pQuery;
256 257
} SRequestObj;

258 259 260 261 262
typedef struct SSyncQueryParam {
  tsem_t       sem;
  SRequestObj* pRequest;
} SSyncQueryParam;

L
Liu Jicong 已提交
263 264
void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4);
265

L
Liu Jicong 已提交
266 267 268 269 270 271 272 273
void    doSetOneRowPtr(SReqResultInfo* pResultInfo);
void    setResPrecision(SReqResultInfo* pResInfo, int32_t precision);
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4,
                              bool freeAfterUse);
void    setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols);
void    doFreeReqResultInfo(SReqResultInfo* pResInfo);
int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq);
void    syncCatalogFn(SMetaData* pResult, void* param, int32_t code);
D
dapan1121 已提交
274

X
Xiaoyu Wang 已提交
275
TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly);
dengyihao's avatar
dengyihao 已提交
276 277 278 279 280
TAOS_RES* taosQueryImplWithReqid(TAOS* taos, const char* sql, bool validateOnly, int64_t reqid);

void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly);
void taosAsyncQueryImplWithReqid(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
                                 int64_t reqid);
L
Liu Jicong 已提交
281

282
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols);
283

L
Liu Jicong 已提交
284
static FORCE_INLINE SReqResultInfo* tmqGetCurResInfo(TAOS_RES* res) {
L
Liu Jicong 已提交
285
  SMqRspObj* msg = (SMqRspObj*)res;
L
Liu Jicong 已提交
286
  return (SReqResultInfo*)&msg->resInfo;
L
Liu Jicong 已提交
287 288
}

289
SReqResultInfo* tmqGetNextResInfo(TAOS_RES* res, bool convertUcs4);
L
Liu Jicong 已提交
290 291 292 293 294 295

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

296
extern SAppInfo appInfo;
H
Haojun Liao 已提交
297
extern int32_t  clientReqRefPool;
298
extern int32_t  clientConnRefPool;
dengyihao's avatar
dengyihao 已提交
299
extern int32_t  timestampDeltaLimit;
D
dapan1121 已提交
300 301
extern int64_t  lastClusterId;

H
Haojun Liao 已提交
302 303
__async_send_cb_fn_t getMsgRspHandle(int32_t msgType);

H
Haojun Liao 已提交
304
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pReqObj);
305

306
void*    createTscObj(const char* user, const char* auth, const char* db, int32_t connType, SAppInstInfo* pAppInfo);
L
Liu Jicong 已提交
307 308 309
void     destroyTscObj(void* pObj);
STscObj* acquireTscObj(int64_t rid);
int32_t  releaseTscObj(int64_t rid);
dengyihao's avatar
dengyihao 已提交
310
void     destroyAppInst(SAppInstInfo* pAppInfo);
311

312 313
uint64_t generateRequestId();

dengyihao's avatar
dengyihao 已提交
314
void*        createRequest(uint64_t connId, int32_t type, int64_t reqid);
L
Liu Jicong 已提交
315 316 317
void         destroyRequest(SRequestObj* pRequest);
SRequestObj* acquireRequest(int64_t rid);
int32_t      releaseRequest(int64_t rid);
D
dapan1121 已提交
318
int32_t      removeRequest(int64_t rid);
dengyihao's avatar
dengyihao 已提交
319
void         doDestroyRequest(void* p);
320

dengyihao's avatar
dengyihao 已提交
321
char* getDbOfConnection(STscObj* pObj);
322
void  setConnectionDB(STscObj* pTscObj, const char* db);
H
Haojun Liao 已提交
323
void  resetConnectDB(STscObj* pTscObj);
324

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

dengyihao's avatar
dengyihao 已提交
327
void* openTransporter(const char* user, const char* auth, int32_t numOfThreads);
D
dapan1121 已提交
328
void tscStopCrashReport();
329

dengyihao's avatar
dengyihao 已提交
330 331 332 333 334
typedef struct AsyncArg {
  SRpcMsg msg;
  SEpSet* pEpset;
} AsyncArg;

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

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

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

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

dengyihao's avatar
dengyihao 已提交
345
int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql,
dengyihao's avatar
dengyihao 已提交
346
                     SRequestObj** pRequest, int64_t reqid);
347

L
Liu Jicong 已提交
348
void taos_close_internal(void* taos);
349

dengyihao's avatar
dengyihao 已提交
350
// --- heartbeat
L
Liu Jicong 已提交
351 352 353 354 355
// global, called by mgmt
int  hbMgrInit();
void hbMgrCleanUp();

// cluster level
dengyihao's avatar
dengyihao 已提交
356 357
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key);
void       appHbMgrCleanup(void);
dengyihao's avatar
dengyihao 已提交
358
void       hbRemoveAppHbMrg(SAppHbMgr** pAppHbMgr);
dengyihao's avatar
dengyihao 已提交
359 360
void       destroyAllRequests(SHashObj* pRequests);
void       stopAllRequests(SHashObj* pRequests);
L
Liu Jicong 已提交
361 362

// conn level
L
Liu Jicong 已提交
363
int  hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType);
K
kailixu 已提交
364
void hbDeregisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey, void* connFp);
L
Liu Jicong 已提交
365

366 367 368 369 370 371
typedef struct SSqlCallbackWrapper {
  SParseContext* pParseCtx;
  SCatalogReq*   pCatalogReq;
  SRequestObj*   pRequest;
} SSqlCallbackWrapper;

372
SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res);
H
Haojun Liao 已提交
373
int32_t      scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList);
374 375 376 377 378 379 380 381
void    launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta, SSqlCallbackWrapper* pWrapper);
int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest);
int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList);
void    doAsyncQuery(SRequestObj* pRequest, bool forceUpdateMeta);
int32_t removeMeta(STscObj* pTscObj, SArray* tbList);
int32_t handleAlterTbExecRes(void* res, struct SCatalog* pCatalog);
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog);
bool    qnodeRequired(SRequestObj* pRequest);
X
Xiaoyu Wang 已提交
382
void    continueInsertFromCsv(SSqlCallbackWrapper* pWrapper, SRequestObj* pRequest);
383
void    destorySqlCallbackWrapper(SSqlCallbackWrapper* pWrapper);
D
stmt  
dapan1121 已提交
384

385 386 387 388 389
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTINT_H