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

H
Haojun Liao 已提交
23
#include "common.h"
dengyihao's avatar
dengyihao 已提交
24 25 26
#include "parser.h"
#include "query.h"
#include "taos.h"
27 28
#include "tdef.h"
#include "tep.h"
29 30
#include "thash.h"
#include "tlist.h"
dengyihao's avatar
dengyihao 已提交
31
#include "tmsg.h"
32
#include "tmsgtype.h"
33
#include "trpc.h"
34 35 36 37 38 39 40 41 42

#define CHECK_CODE_GOTO(expr, label) \
  do {                               \
    int32_t code = expr;             \
    if (TSDB_CODE_SUCCESS != code) { \
      terrno = code;                 \
      goto label;                    \
    }                                \
  } while (0)
43

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

typedef struct SAppInstInfo SAppInstInfo;

D
dapan1121 已提交
48
typedef struct SHbConnInfo {
dengyihao's avatar
dengyihao 已提交
49 50
  void*         param;
  SClientHbReq* req;
D
dapan1121 已提交
51 52
} SHbConnInfo;

L
Liu Jicong 已提交
53
typedef struct SAppHbMgr {
dengyihao's avatar
dengyihao 已提交
54
  char* key;
L
Liu Jicong 已提交
55 56 57 58 59 60 61 62 63 64
  // 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 已提交
65 66
  SHashObj* activeInfo;  // hash<SClientHbKey, SClientHbReq>
  SHashObj* connInfo;    // hash<SClientHbKey, SHbConnInfo>
L
Liu Jicong 已提交
67 68
} SAppHbMgr;

dengyihao's avatar
dengyihao 已提交
69
typedef int32_t (*FHbRspHandle)(struct SAppHbMgr* pAppHbMgr, SClientHbRsp* pRsp);
D
dapan1121 已提交
70

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

L
Liu Jicong 已提交
73 74 75 76 77 78 79
typedef struct SClientHbMgr {
  int8_t inited;
  // ctl
  int8_t          threadStop;
  pthread_t       thread;
  pthread_mutex_t lock;       // used when app init and cleanup
  SArray*         appHbMgrs;  // SArray<SAppHbMgr*> one for each cluster
D
dapan1121 已提交
80 81
  FHbReqHandle    reqHandle[HEARTBEAT_TYPE_MAX];
  FHbRspHandle    rspHandle[HEARTBEAT_TYPE_MAX];
L
Liu Jicong 已提交
82 83
} SClientHbMgr;

84
typedef struct SQueryExecMetric {
dengyihao's avatar
dengyihao 已提交
85 86 87 88
  int64_t start;   // start timestamp
  int64_t parsed;  // start to parse
  int64_t send;    // start to send to server
  int64_t rsp;     // receive response from server
89 90
} SQueryExecMetric;

91
typedef struct SInstanceSummary {
dengyihao's avatar
dengyihao 已提交
92 93 94 95 96 97 98 99 100 101
  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
102
} SInstanceSummary;
103 104

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

L
Liu Jicong 已提交
108
struct SAppInstInfo {
dengyihao's avatar
dengyihao 已提交
109 110 111 112 113 114 115
  int64_t           numOfConns;
  SCorEpSet         mgmtEp;
  SInstanceSummary  summary;
  SList*            pConnList;  // STscObj linked list
  int64_t           clusterId;
  void*             pTransporter;
  struct SAppHbMgr* pAppHbMgr;
L
Liu Jicong 已提交
116
};
117 118

typedef struct SAppInfo {
H
Haojun Liao 已提交
119 120
  int64_t         startTime;
  char            appName[TSDB_APP_NAME_LEN];
dengyihao's avatar
dengyihao 已提交
121
  char*           ep;
H
Haojun Liao 已提交
122 123
  int32_t         pid;
  int32_t         numOfThreads;
dengyihao's avatar
dengyihao 已提交
124
  SHashObj*       pInstMap;
H
Haojun Liao 已提交
125
  pthread_mutex_t mutex;
126 127 128
} SAppInfo;

typedef struct STscObj {
dengyihao's avatar
dengyihao 已提交
129 130 131 132 133 134 135 136 137 138 139
  char            user[TSDB_USER_LEN];
  char            pass[TSDB_PASSWORD_LEN];
  char            db[TSDB_DB_FNAME_LEN];
  char            ver[128];
  int32_t         acctId;
  uint32_t        connId;
  int32_t         connType;
  uint64_t        id;         // ref ID returned by taosAddRef
  pthread_mutex_t mutex;      // used to protect the operation on db
  int32_t         numOfReqs;  // number of sqlObj bound to this connection
  SAppInstInfo*   pAppInfo;
140 141
} STscObj;

L
Liu Jicong 已提交
142 143 144 145
typedef struct SMqConsumer {
  STscObj* pTscObj;
} SMqConsumer;

146
typedef struct SReqResultInfo {
dengyihao's avatar
dengyihao 已提交
147 148 149 150 151 152 153 154 155 156 157
  const char* pRspMsg;
  const char* pData;
  TAOS_FIELD* fields;
  uint32_t    numOfCols;
  int32_t*    length;
  TAOS_ROW    row;
  char**      pCol;
  uint32_t    numOfRows;
  uint64_t    totalRows;
  uint32_t    current;
  bool        completed;
158 159
} SReqResultInfo;

160
typedef struct SShowReqInfo {
dengyihao's avatar
dengyihao 已提交
161 162 163 164
  int64_t execId;  // showId/queryId
  int32_t vgId;
  SArray* pArray;        // SArray<SVgroupInfo>
  int32_t currentIndex;  // current accessed vgroup index.
165 166
} SShowReqInfo;

167
typedef struct SRequestSendRecvBody {
dengyihao's avatar
dengyihao 已提交
168
  tsem_t            rspSem;  // not used now
H
Haojun Liao 已提交
169
  void*             fp;
dengyihao's avatar
dengyihao 已提交
170
  SShowReqInfo      showInfo;  // todo this attribute will be removed after the query framework being completed.
H
Haojun Liao 已提交
171
  SDataBuf          requestMsg;
dengyihao's avatar
dengyihao 已提交
172 173
  struct SSchJob*   pQueryJob;  // query job, created according to sql query DAG.
  struct SQueryDag* pDag;       // the query dag, generated according to the sql statement.
H
Haojun Liao 已提交
174
  SReqResultInfo    resInfo;
175
} SRequestSendRecvBody;
176

dengyihao's avatar
dengyihao 已提交
177
#define ERROR_MSG_BUF_DEFAULT_SIZE 512
178

179
typedef struct SRequestObj {
dengyihao's avatar
dengyihao 已提交
180 181 182 183 184 185 186 187 188 189
  uint64_t             requestId;
  int32_t              type;  // request type
  STscObj*             pTscObj;
  char*                sqlstr;  // sql string
  int32_t              sqlLen;
  int64_t              self;
  char*                msgBuf;
  void*                pInfo;  // sql parse info, generated by parser module
  int32_t              code;
  SQueryExecMetric     metric;
H
Haojun Liao 已提交
190
  SRequestSendRecvBody body;
191 192
} SRequestObj;

193
extern SAppInfo appInfo;
H
Haojun Liao 已提交
194
extern int32_t  clientReqRefPool;
195
extern int32_t  clientConnRefPool;
196

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

dengyihao's avatar
dengyihao 已提交
201
int taos_init();
202

dengyihao's avatar
dengyihao 已提交
203 204
void* createTscObj(const char* user, const char* auth, const char* db, SAppInstInfo* pAppInfo);
void  destroyTscObj(void* pObj);
205

206 207
uint64_t generateRequestId();

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

dengyihao's avatar
dengyihao 已提交
211
char* getDbOfConnection(STscObj* pObj);
212
void  setConnectionDB(STscObj* pTscObj, const char* db);
213 214

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

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

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

222 223
void initMsgHandleFp();

dengyihao's avatar
dengyihao 已提交
224 225
TAOS* taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db,
                            uint16_t port);
226

dengyihao's avatar
dengyihao 已提交
227
void* doFetchRow(SRequestObj* pRequest);
228

dengyihao's avatar
dengyihao 已提交
229
void setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t numOfCols, int32_t numOfRows);
230

dengyihao's avatar
dengyihao 已提交
231
int32_t buildRequest(STscObj* pTscObj, const char* sql, int sqlLen, SRequestObj** pRequest);
232 233 234

int32_t parseSql(SRequestObj* pRequest, SQueryNode** pQuery);

dengyihao's avatar
dengyihao 已提交
235
// --- heartbeat
L
Liu Jicong 已提交
236 237 238 239 240 241
// global, called by mgmt
int  hbMgrInit();
void hbMgrCleanUp();
int  hbHandleRsp(SClientHbBatchRsp* hbRsp);

// cluster level
dengyihao's avatar
dengyihao 已提交
242 243
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key);
void       appHbMgrCleanup(void);
L
Liu Jicong 已提交
244 245

// conn level
D
dapan1121 已提交
246
int  hbRegisterConn(SAppHbMgr* pAppHbMgr, int32_t connId, int64_t clusterId, int32_t hbType);
L
Liu Jicong 已提交
247 248 249 250 251 252 253
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();

254 255 256 257 258
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTINT_H