transComm.h 10.8 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */
#ifdef USE_UV

dengyihao's avatar
dengyihao 已提交
17 18 19 20
#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
21 22 23
#include <uv.h>
#include "lz4.h"
#include "os.h"
dengyihao's avatar
dengyihao 已提交
24
#include "osSocket.h"
dengyihao's avatar
dengyihao 已提交
25 26 27 28 29 30
#include "rpcCache.h"
#include "rpcHead.h"
#include "rpcLog.h"
#include "taoserror.h"
#include "tglobal.h"
#include "thash.h"
dengyihao's avatar
dengyihao 已提交
31
#include "theap.h"
dengyihao's avatar
dengyihao 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include "tidpool.h"
#include "tmd5.h"
#include "tmempool.h"
#include "tmsg.h"
#include "transportInt.h"
#include "tref.h"
#include "trpc.h"
#include "ttimer.h"
#include "tutil.h"

typedef void* queue[2];
/* Private macros. */
#define QUEUE_NEXT(q) (*(queue**)&((*(q))[0]))
#define QUEUE_PREV(q) (*(queue**)&((*(q))[1]))

#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))
/* Initialize an empty queue. */
#define QUEUE_INIT(q)    \
  {                      \
    QUEUE_NEXT(q) = (q); \
    QUEUE_PREV(q) = (q); \
  }

/* Return true if the queue has no element. */
#define QUEUE_IS_EMPTY(q) ((const queue*)(q) == (const queue*)QUEUE_NEXT(q))

/* Insert an element at the back of a queue. */
#define QUEUE_PUSH(q, e)           \
  {                                \
    QUEUE_NEXT(e) = (q);           \
    QUEUE_PREV(e) = QUEUE_PREV(q); \
    QUEUE_PREV_NEXT(e) = (e);      \
    QUEUE_PREV(q) = (e);           \
  }

/* Remove the given element from the queue. Any element can be removed at any *
 * time. */
#define QUEUE_REMOVE(e)                 \
  {                                     \
    QUEUE_PREV_NEXT(e) = QUEUE_NEXT(e); \
    QUEUE_NEXT_PREV(e) = QUEUE_PREV(e); \
  }
dengyihao's avatar
dengyihao 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#define QUEUE_SPLIT(h, q, n)       \
  do {                             \
    QUEUE_PREV(n) = QUEUE_PREV(h); \
    QUEUE_PREV_NEXT(n) = (n);      \
    QUEUE_NEXT(n) = (q);           \
    QUEUE_PREV(h) = QUEUE_PREV(q); \
    QUEUE_PREV_NEXT(h) = (h);      \
    QUEUE_PREV(q) = (n);           \
  } while (0)

#define QUEUE_MOVE(h, n)        \
  do {                          \
    if (QUEUE_IS_EMPTY(h)) {    \
      QUEUE_INIT(n);            \
    } else {                    \
      queue* q = QUEUE_HEAD(h); \
      QUEUE_SPLIT(h, q, n);     \
    }                           \
  } while (0)
dengyihao's avatar
dengyihao 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107

/* Return the element at the front of the queue. */
#define QUEUE_HEAD(q) (QUEUE_NEXT(q))

/* Return the element at the back of the queue. */
#define QUEUE_TAIL(q) (QUEUE_PREV(q))

/* Iterate over the element of a queue. * Mutating the queue while iterating
 * results in undefined behavior. */
#define QUEUE_FOREACH(q, e) for ((q) = QUEUE_NEXT(e); (q) != (e); (q) = QUEUE_NEXT(q))

/* Return the structure holding the given element. */
#define QUEUE_DATA(e, type, field) ((type*)((void*)((char*)(e)-offsetof(type, field))))

dengyihao's avatar
dengyihao 已提交
108 109
#define TRANS_RETRY_COUNT_LIMIT 10  // retry count limit
#define TRANS_RETRY_INTERVAL    5   // ms retry interval
dengyihao's avatar
dengyihao 已提交
110
#define TRANS_CONN_TIMEOUT      3   // connect timeout
dengyihao's avatar
dengyihao 已提交
111

dengyihao's avatar
dengyihao 已提交
112
typedef struct {
dengyihao's avatar
dengyihao 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  SRpcInfo* pRpc;     // associated SRpcInfo
  SEpSet    epSet;    // ip list provided by app
  void*     ahandle;  // handle provided by app
  // struct SRpcConn* pConn;     // pConn allocated
  tmsg_t   msgType;  // message type
  uint8_t* pCont;    // content provided by app
  int32_t  contLen;  // content length
  // int32_t  code;     // error code
  // int16_t  numOfTry;  // number of try for different servers
  // int8_t   oldInUse;  // server EP inUse passed by app
  // int8_t   redirect;  // flag to indicate redirect
  int8_t   connType;  // connection type
  int64_t  rid;       // refId returned by taosAddRef
  SRpcMsg* pRsp;      // for synchronous API
  tsem_t*  pSem;      // for synchronous API
  char*    ip;
  uint32_t port;
  // SEpSet*          pSet;      // for synchronous API
dengyihao's avatar
dengyihao 已提交
131 132
} SRpcReqContext;

dengyihao's avatar
formate  
dengyihao 已提交
133
typedef SRpcMsg      STransMsg;
dengyihao's avatar
dengyihao 已提交
134 135
typedef SRpcCtx      STransCtx;
typedef SRpcCtxVal   STransCtxVal;
dengyihao's avatar
formate  
dengyihao 已提交
136
typedef SRpcInfo     STrans;
U
ubuntu 已提交
137 138
typedef SRpcConnInfo STransHandleInfo;

dengyihao's avatar
dengyihao 已提交
139
typedef struct {
dengyihao's avatar
dengyihao 已提交
140 141 142
  SEpSet  epSet;     // ip list provided by app
  void*   ahandle;   // handle provided by app
  tmsg_t  msgType;   // message type
dengyihao's avatar
dengyihao 已提交
143
  int8_t  connType;  // connection type cli/srv
dengyihao's avatar
dengyihao 已提交
144 145
  int64_t rid;       // refId returned by taosAddRef

dengyihao's avatar
dengyihao 已提交
146
  int8_t     retryCount;
dengyihao's avatar
dengyihao 已提交
147 148 149
  STransCtx  appCtx;  //
  STransMsg* pRsp;    // for synchronous API
  tsem_t*    pSem;    // for synchronous API
dengyihao's avatar
dengyihao 已提交
150

dengyihao's avatar
dengyihao 已提交
151
  int hThrdIdx;
dengyihao's avatar
dengyihao 已提交
152 153 154 155 156 157
} STransConnCtx;

#pragma pack(push, 1)

typedef struct {
  char version : 4;  // RPC version
dengyihao's avatar
dengyihao 已提交
158 159 160 161
  char comp : 2;     // compression algorithm, 0:no compression 1:lz4
  char noResp : 2;   // noResp bits, 0: resp, 1: resp
  char persist : 2;  // persist handle,0: no persit, 1: persist handle
  char release : 2;
dengyihao's avatar
dengyihao 已提交
162
  char secured : 2;
dengyihao's avatar
dengyihao 已提交
163
  char spi : 2;
dengyihao's avatar
dengyihao 已提交
164

dengyihao's avatar
dengyihao 已提交
165
  char     user[TSDB_UNI_LEN];
dengyihao's avatar
dengyihao 已提交
166 167
  uint64_t ahandle;  // ahandle assigned by client
  uint32_t code;     // del later
dengyihao's avatar
dengyihao 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  uint32_t msgType;
  int32_t  msgLen;
  uint8_t  content[0];  // message body starts from here
} STransMsgHead;

typedef struct {
  int32_t reserved;
  int32_t contLen;
} STransCompMsg;

typedef struct {
  uint32_t timeStamp;
  uint8_t  auth[TSDB_AUTH_LEN];
} STransDigestMsg;

dengyihao's avatar
dengyihao 已提交
183 184
typedef struct {
  uint8_t user[TSDB_UNI_LEN];
dengyihao's avatar
dengyihao 已提交
185
  uint8_t secret[TSDB_PASSWORD_LEN];
dengyihao's avatar
dengyihao 已提交
186 187
} STransUserMsg;

dengyihao's avatar
dengyihao 已提交
188 189
#pragma pack(pop)

dengyihao's avatar
dengyihao 已提交
190
typedef enum { Normal, Quit, Release, Register } STransMsgType;
dengyihao's avatar
dengyihao 已提交
191
typedef enum { ConnNormal, ConnAcquire, ConnRelease, ConnBroken, ConnInPool } ConnStatus;
dengyihao's avatar
dengyihao 已提交
192

dengyihao's avatar
dengyihao 已提交
193
#define container_of(ptr, type, member) ((type*)((char*)(ptr)-offsetof(type, member)))
dengyihao's avatar
dengyihao 已提交
194
#define RPC_RESERVE_SIZE                (sizeof(STranConnCtx))
dengyihao's avatar
dengyihao 已提交
195

dengyihao's avatar
dengyihao 已提交
196 197 198
#define RPC_MSG_OVERHEAD           (sizeof(SRpcHead) + sizeof(SRpcDigest))
#define rpcHeadFromCont(cont)      ((SRpcHead*)((char*)cont - sizeof(SRpcHead)))
#define rpcContFromHead(msg)       (msg + sizeof(SRpcHead))
dengyihao's avatar
dengyihao 已提交
199
#define rpcMsgLenFromCont(contLen) (contLen + sizeof(SRpcHead))
dengyihao's avatar
dengyihao 已提交
200 201
#define rpcContLenFromMsg(msgLen)  (msgLen - sizeof(SRpcHead))
#define rpcIsReq(type)             (type & 1U)
dengyihao's avatar
dengyihao 已提交
202

dengyihao's avatar
dengyihao 已提交
203 204
#define TRANS_RESERVE_SIZE (sizeof(STranConnCtx))

dengyihao's avatar
dengyihao 已提交
205 206 207
#define TRANS_MSG_OVERHEAD           (sizeof(STransMsgHead))
#define transHeadFromCont(cont)      ((STransMsgHead*)((char*)cont - sizeof(STransMsgHead)))
#define transContFromHead(msg)       (msg + sizeof(STransMsgHead))
dengyihao's avatar
dengyihao 已提交
208
#define transMsgLenFromCont(contLen) (contLen + sizeof(STransMsgHead))
dengyihao's avatar
dengyihao 已提交
209 210
#define transContLenFromMsg(msgLen)  (msgLen - sizeof(STransMsgHead));
#define transIsReq(type)             (type & 1U)
dengyihao's avatar
dengyihao 已提交
211

dengyihao's avatar
dengyihao 已提交
212 213 214 215 216
int       rpcAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey);
void      rpcBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey);
int32_t   rpcCompressRpcMsg(char* pCont, int32_t contLen);
SRpcHead* rpcDecompressRpcMsg(SRpcHead* pHead);

dengyihao's avatar
dengyihao 已提交
217 218 219 220 221
int  transAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey);
void transBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey);
bool transCompressMsg(char* msg, int32_t len, int32_t* flen);
bool transDecompressMsg(char* msg, int32_t len, int32_t* flen);

dengyihao's avatar
dengyihao 已提交
222
void transFreeMsg(void* msg);
dengyihao's avatar
dengyihao 已提交
223 224

//
dengyihao's avatar
dengyihao 已提交
225 226 227 228
typedef struct SConnBuffer {
  char* buf;
  int   len;
  int   cap;
dengyihao's avatar
fix bug  
dengyihao 已提交
229
  int   total;
dengyihao's avatar
dengyihao 已提交
230 231
} SConnBuffer;

dengyihao's avatar
dengyihao 已提交
232 233
typedef void (*AsyncCB)(uv_async_t* handle);

dengyihao's avatar
dengyihao 已提交
234
typedef struct {
dengyihao's avatar
dengyihao 已提交
235 236
  void*         pThrd;
  queue         qmsg;
wafwerar's avatar
wafwerar 已提交
237
  TdThreadMutex mtx;  // protect qmsg;
dengyihao's avatar
dengyihao 已提交
238 239
} SAsyncItem;

dengyihao's avatar
dengyihao 已提交
240 241 242 243 244 245
typedef struct {
  int         index;
  int         nAsync;
  uv_async_t* asyncs;
} SAsyncPool;

dengyihao's avatar
dengyihao 已提交
246
SAsyncPool* transCreateAsyncPool(uv_loop_t* loop, int sz, void* arg, AsyncCB cb);
dengyihao's avatar
dengyihao 已提交
247
void        transDestroyAsyncPool(SAsyncPool* pool);
dengyihao's avatar
dengyihao 已提交
248
int         transSendAsync(SAsyncPool* pool, queue* mq);
dengyihao's avatar
dengyihao 已提交
249

dengyihao's avatar
dengyihao 已提交
250 251 252 253 254
int  transInitBuffer(SConnBuffer* buf);
int  transClearBuffer(SConnBuffer* buf);
int  transDestroyBuffer(SConnBuffer* buf);
int  transAllocBuffer(SConnBuffer* connBuf, uv_buf_t* uvBuf);
bool transReadComplete(SConnBuffer* connBuf);
dengyihao's avatar
dengyihao 已提交
255

dengyihao's avatar
dengyihao 已提交
256
int transSetConnOption(uv_tcp_t* stream);
dengyihao's avatar
dengyihao 已提交
257

dengyihao's avatar
dengyihao 已提交
258 259 260 261 262
void transRefSrvHandle(void* handle);
void transUnrefSrvHandle(void* handle);

void transRefCliHandle(void* handle);
void transUnrefCliHandle(void* handle);
dengyihao's avatar
dengyihao 已提交
263

dengyihao's avatar
dengyihao 已提交
264 265 266
void transReleaseCliHandle(void* handle);
void transReleaseSrvHandle(void* handle);

dengyihao's avatar
dengyihao 已提交
267 268
void transSendRequest(void* shandle, const SEpSet* pEpSet, STransMsg* pMsg, STransCtx* pCtx);
void transSendRecv(void* shandle, const SEpSet* pEpSet, STransMsg* pMsg, STransMsg* pRsp);
dengyihao's avatar
dengyihao 已提交
269 270
void transSendResponse(const STransMsg* msg);
void transRegisterMsg(const STransMsg* msg);
dengyihao's avatar
formate  
dengyihao 已提交
271
int  transGetConnInfo(void* thandle, STransHandleInfo* pInfo);
U
ubuntu 已提交
272 273 274 275

void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle);
void* transInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle);

dengyihao's avatar
formate  
dengyihao 已提交
276 277
void transCloseClient(void* arg);
void transCloseServer(void* arg);
U
ubuntu 已提交
278

dengyihao's avatar
dengyihao 已提交
279
void  transCtxInit(STransCtx* ctx);
dengyihao's avatar
dengyihao 已提交
280
void  transCtxCleanup(STransCtx* ctx);
dengyihao's avatar
dengyihao 已提交
281 282 283
void  transCtxClear(STransCtx* ctx);
void  transCtxMerge(STransCtx* dst, STransCtx* src);
void* transCtxDumpVal(STransCtx* ctx, int32_t key);
dengyihao's avatar
dengyihao 已提交
284
void* transCtxDumpBrokenlinkVal(STransCtx* ctx, int32_t* msgType);
dengyihao's avatar
dengyihao 已提交
285

dengyihao's avatar
dengyihao 已提交
286 287 288
// queue sending msgs
typedef struct {
  SArray* q;
wafwerar's avatar
wafwerar 已提交
289
  void (*freeFunc)(const void* arg);
dengyihao's avatar
dengyihao 已提交
290 291 292 293 294 295
} STransQueue;

/*
 * init queue
 * note: queue'size is small, default 1
 */
wafwerar's avatar
wafwerar 已提交
296
void transQueueInit(STransQueue* queue, void (*freeFunc)(const void* arg));
dengyihao's avatar
dengyihao 已提交
297 298 299 300 301 302

/*
 * put arg into queue
 * if queue'size > 1, return false; else return true
 */
bool transQueuePush(STransQueue* queue, void* arg);
dengyihao's avatar
dengyihao 已提交
303 304 305 306
/*
 * the size of queue
 */
int32_t transQueueSize(STransQueue* queue);
dengyihao's avatar
dengyihao 已提交
307 308 309 310 311
/*
 * pop head from queue
 */
void* transQueuePop(STransQueue* queue);
/*
dengyihao's avatar
dengyihao 已提交
312
 * get ith from queue
dengyihao's avatar
dengyihao 已提交
313
 */
dengyihao's avatar
dengyihao 已提交
314 315 316 317 318
void* transQueueGet(STransQueue* queue, int i);
/*
 * rm ith from queue
 */
void* transQueueRm(STransQueue* queue, int i);
dengyihao's avatar
dengyihao 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331
/*
 * queue empty or not
 */
bool transQueueEmpty(STransQueue* queue);
/*
 * clear queue
 */
void transQueueClear(STransQueue* queue);
/*
 * destroy queue
 */
void transQueueDestroy(STransQueue* queue);

dengyihao's avatar
dengyihao 已提交
332 333 334 335 336 337 338 339
/*
 * delay queue based on uv loop and uv timer, and only used in retry
 */
typedef struct STaskArg {
  void* param1;
  void* param2;
} STaskArg;

dengyihao's avatar
dengyihao 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
typedef struct SDelayTask {
  void (*func)(void* arg);
  void*    arg;
  uint64_t execTime;
  HeapNode node;
} SDelayTask;

typedef struct SDelayQueue {
  uv_timer_t* timer;
  Heap*       heap;
  uv_loop_t*  loop;
  void (*free)(void* arg);
} SDelayQueue;

int transCreateDelayQueue(uv_loop_t* loop, SDelayQueue** queue);

void transDestroyDelayQueue(SDelayQueue* queue);

int transPutTaskToDelayQueue(SDelayQueue* queue, void (*func)(void* arg), void* arg, uint64_t timeoutMs);

dengyihao's avatar
dengyihao 已提交
360 361 362 363
/*
 * init global func
 */
void transThreadOnce();
dengyihao's avatar
dengyihao 已提交
364

dengyihao's avatar
dengyihao 已提交
365 366 367 368
#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
369
#endif