clientInt.h 7.1 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 23
/*
 * 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

#include "taos.h"
H
Haojun Liao 已提交
24
#include "common.h"
H
Hongze Cheng 已提交
25
#include "tmsg.h"
26 27
#include "tdef.h"
#include "tep.h"
28 29
#include "thash.h"
#include "tlist.h"
30
#include "tmsgtype.h"
31
#include "trpc.h"
H
Haojun Liao 已提交
32
#include "query.h"
33

L
Liu Jicong 已提交
34 35 36 37 38 39
#define HEARTBEAT_INTERVAL 1500  // ms

typedef struct SAppInstInfo SAppInstInfo;

typedef int32_t (*FHbRspHandle)(SClientHbRsp* pReq);

D
dapan1121 已提交
40 41 42 43 44 45 46
typedef int32_t (*FHbReqHandle)(SClientHbKey *connKey, void* param, SClientHbReq *req);

typedef struct SHbConnInfo {
  void         *param;
  SClientHbReq *req;
} SHbConnInfo;

L
Liu Jicong 已提交
47
typedef struct SAppHbMgr {
D
dapan1121 已提交
48
  char   *key;
L
Liu Jicong 已提交
49 50 51 52 53 54 55 56 57 58 59
  // 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
  SHashObj* activeInfo;    // hash<SClientHbKey, SClientHbReq>
D
dapan1121 已提交
60
  SHashObj* connInfo;      // hash<SClientHbKey, SHbConnInfo>
L
Liu Jicong 已提交
61 62 63 64 65 66 67 68 69
} SAppHbMgr;

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 已提交
70 71
  FHbReqHandle    reqHandle[HEARTBEAT_TYPE_MAX];
  FHbRspHandle    rspHandle[HEARTBEAT_TYPE_MAX];
L
Liu Jicong 已提交
72 73 74
} SClientHbMgr;


75
typedef struct SQueryExecMetric {
76 77 78 79
  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
80 81
} SQueryExecMetric;

82
typedef struct SInstanceSummary {
83 84 85 86 87 88 89 90 91 92
  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
93
} SInstanceSummary;
94 95 96 97 98

typedef struct SHeartBeatInfo {
  void  *pTimer;   // timer, used to send request msg to mnode
} SHeartBeatInfo;

L
Liu Jicong 已提交
99 100 101 102
struct SAppInstInfo {
  int64_t          numOfConns;
  SCorEpSet        mgmtEp;
  SInstanceSummary summary;
103
  SList            *pConnList;  // STscObj linked list
L
Liu Jicong 已提交
104
  int64_t          clusterId;
105
  void             *pTransporter;
L
Liu Jicong 已提交
106 107
  struct SAppHbMgr *pAppHbMgr;
};
108 109

typedef struct SAppInfo {
H
Haojun Liao 已提交
110 111 112 113 114 115 116
  int64_t         startTime;
  char            appName[TSDB_APP_NAME_LEN];
  char           *ep;
  int32_t         pid;
  int32_t         numOfThreads;
  SHashObj       *pInstMap;
  pthread_mutex_t mutex;
117 118 119
} SAppInfo;

typedef struct STscObj {
120 121 122 123 124
  char             user[TSDB_USER_LEN];
  char             pass[TSDB_PASSWORD_LEN];
  char             db[TSDB_DB_FNAME_LEN];
  int32_t          acctId;
  uint32_t         connId;
L
Liu Jicong 已提交
125
  int32_t          connType;
126 127
  uint64_t         id;       // ref ID returned by taosAddRef
  pthread_mutex_t  mutex;     // used to protect the operation on db
H
Haojun Liao 已提交
128
  int32_t          numOfReqs; // number of sqlObj bound to this connection
129
  SAppInstInfo    *pAppInfo;
130 131
} STscObj;

L
Liu Jicong 已提交
132 133 134 135
typedef struct SMqConsumer {
  STscObj* pTscObj;
} SMqConsumer;

136 137
typedef struct SReqResultInfo {
  const char  *pRspMsg;
H
Haojun Liao 已提交
138 139
  const char  *pData;
  TAOS_FIELD  *fields;
140
  uint32_t     numOfCols;
H
Haojun Liao 已提交
141 142 143
  int32_t     *length;
  TAOS_ROW     row;
  char       **pCol;
144
  uint32_t     numOfRows;
145
  uint64_t     totalRows;
146
  uint32_t     current;
147
  bool         completed;
148 149
} SReqResultInfo;

150 151 152 153 154 155 156
typedef struct SShowReqInfo {
  int64_t         execId;        // showId/queryId
  int32_t         vgId;
  SArray         *pArray;        // SArray<SVgroupInfo>
  int32_t         currentIndex;  // current accessed vgroup index.
} SShowReqInfo;

157
typedef struct SRequestSendRecvBody {
H
Haojun Liao 已提交
158 159 160 161 162 163 164
  tsem_t            rspSem;        // not used now
  void*             fp;
  SShowReqInfo      showInfo;      // todo this attribute will be removed after the query framework being completed.
  SDataBuf          requestMsg;
  struct SSchJob   *pQueryJob;     // query job, created according to sql query DAG.
  struct SQueryDag *pDag;          // the query dag, generated according to the sql statement.
  SReqResultInfo    resInfo;
165
} SRequestSendRecvBody;
166

167 168
#define ERROR_MSG_BUF_DEFAULT_SIZE  512

169 170 171 172 173
typedef struct SRequestObj {
  uint64_t         requestId;
  int32_t          type;   // request type
  STscObj         *pTscObj;
  char            *sqlstr;  // sql string
X
Xiaoyu Wang 已提交
174
  int32_t          sqlLen;
175 176 177
  int64_t          self;
  char            *msgBuf;
  void            *pInfo;   // sql parse info, generated by parser module
178
  int32_t          code;
H
Haojun Liao 已提交
179 180
  SQueryExecMetric metric;
  SRequestSendRecvBody body;
181 182
} SRequestObj;

183
extern SAppInfo appInfo;
H
Haojun Liao 已提交
184
extern int32_t  clientReqRefPool;
185
extern int32_t  clientConnRefPool;
186

H
Haojun Liao 已提交
187
extern int (*handleRequestRspFp[TDMT_MAX])(void*, const SDataBuf* pMsg, int32_t code);
H
Haojun Liao 已提交
188
int genericRspCallback(void* param, const SDataBuf* pMsg, int32_t code);
H
Haojun Liao 已提交
189
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pReqObj);
190 191 192

int   taos_init();

193
void* createTscObj(const char* user, const char* auth, const char *db, SAppInstInfo* pAppInfo);
194
void  destroyTscObj(void*pObj);
195

196 197
uint64_t generateRequestId();

198 199 200
void *createRequest(STscObj* pObj, __taos_async_fn_t fp, void* param, int32_t type);
void  destroyRequest(SRequestObj* pRequest);

H
Haojun Liao 已提交
201
char *getDbOfConnection(STscObj* pObj);
202
void  setConnectionDB(STscObj* pTscObj, const char* db);
203 204

void taos_init_imp(void);
H
Haojun Liao 已提交
205
int  taos_options_imp(TSDB_OPTION option, const char *str);
206

H
Haojun Liao 已提交
207
void* openTransporter(const char *user, const char *auth, int32_t numOfThreads);
208 209

void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet);
210

211 212
void initMsgHandleFp();

213 214
TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port);

H
Haojun Liao 已提交
215
void *doFetchRow(SRequestObj* pRequest);
216

H
Haojun Liao 已提交
217
void  setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t numOfCols, int32_t numOfRows);
218

L
Liu Jicong 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
// --- heartbeat 
// global, called by mgmt
int  hbMgrInit();
void hbMgrCleanUp();
int  hbHandleRsp(SClientHbBatchRsp* hbRsp);

// cluster level
SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo);
void appHbMgrCleanup(SAppHbMgr* pAppHbMgr);

// conn level
int  hbRegisterConn(SAppHbMgr* pAppHbMgr, SClientHbKey connKey, FGetConnInfo func);
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();


239 240 241 242 243
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_CLIENTINT_H