transCli.c 22.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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

#include "transComm.h"

typedef struct SCliConn {
dengyihao's avatar
dengyihao 已提交
21
  T_REF_DECLARE()
dengyihao's avatar
dengyihao 已提交
22 23
  uv_connect_t connReq;
  uv_stream_t* stream;
dengyihao's avatar
dengyihao 已提交
24
  uv_write_t   writeReq;
dengyihao's avatar
dengyihao 已提交
25
  void*        hostThrd;
dengyihao's avatar
dengyihao 已提交
26
  SConnBuffer  readBuf;
dengyihao's avatar
dengyihao 已提交
27 28
  void*        data;
  queue        conn;
dengyihao's avatar
dengyihao 已提交
29
  uint64_t     expireTime;
dengyihao's avatar
dengyihao 已提交
30
  int          hThrdIdx;
dengyihao's avatar
dengyihao 已提交
31
  bool         broken;  // link broken or not
dengyihao's avatar
dengyihao 已提交
32

dengyihao's avatar
dengyihao 已提交
33
  int persist;  //
dengyihao's avatar
dengyihao 已提交
34
  // spi configure
dengyihao's avatar
dengyihao 已提交
35 36
  char spi;
  char secured;
dengyihao's avatar
dengyihao 已提交
37
  // debug and log info
dengyihao's avatar
dengyihao 已提交
38
  struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
39
  struct sockaddr_in locaddr;
dengyihao's avatar
dengyihao 已提交
40

dengyihao's avatar
dengyihao 已提交
41
} SCliConn;
dengyihao's avatar
dengyihao 已提交
42

dengyihao's avatar
dengyihao 已提交
43
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
44
  STransConnCtx* ctx;
U
ubuntu 已提交
45
  STransMsg        msg;
dengyihao's avatar
dengyihao 已提交
46 47
  queue          q;
  uint64_t       st;
dengyihao's avatar
dengyihao 已提交
48 49 50
} SCliMsg;

typedef struct SCliThrdObj {
dengyihao's avatar
dengyihao 已提交
51 52
  pthread_t   thread;
  uv_loop_t*  loop;
dengyihao's avatar
dengyihao 已提交
53
  SAsyncPool* asyncPool;
dengyihao's avatar
dengyihao 已提交
54
  uv_timer_t  timer;
dengyihao's avatar
dengyihao 已提交
55 56 57
  void*       pool;  // conn pool

  // msg queue
dengyihao's avatar
dengyihao 已提交
58 59
  queue           msg;
  pthread_mutex_t msgMtx;
dengyihao's avatar
dengyihao 已提交
60 61 62 63

  uint64_t nextTimeout;  // next timeout
  void*    pTransInst;   //
  bool     quit;
dengyihao's avatar
dengyihao 已提交
64 65
} SCliThrdObj;

U
ubuntu 已提交
66
typedef struct SCliObj {
dengyihao's avatar
dengyihao 已提交
67 68 69 70
  char          label[TSDB_LABEL_LEN];
  int32_t       index;
  int           numOfThreads;
  SCliThrdObj** pThreadObj;
U
ubuntu 已提交
71
} SCliObj;
dengyihao's avatar
dengyihao 已提交
72

dengyihao's avatar
dengyihao 已提交
73 74 75 76
typedef struct SConnList {
  queue conn;
} SConnList;

dengyihao's avatar
dengyihao 已提交
77
// conn pool
dengyihao's avatar
dengyihao 已提交
78
// add expire timeout and capacity limit
dengyihao's avatar
dengyihao 已提交
79
static void*     createConnPool(int size);
80
static void*     destroyConnPool(void* pool);
dengyihao's avatar
dengyihao 已提交
81 82
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port);
static void      addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
83

dengyihao's avatar
dengyihao 已提交
84
// register timer in each thread to clear expire conn
U
ubuntu 已提交
85 86 87 88 89
static void cliTimeoutCb(uv_timer_t* handle);
// alloc buf for recv
static void cliAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
// callback after  read nbytes from socket
static void cliRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
90
// callback after write data to socket
U
ubuntu 已提交
91
static void cliSendCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
92
// callback after conn  to server
U
ubuntu 已提交
93 94
static void cliConnCb(uv_connect_t* req, int status);
static void cliAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
95

U
ubuntu 已提交
96 97 98
static SCliConn* cliCreateConn(SCliThrdObj* thrd);
static void      cliDestroyConn(SCliConn* pConn, bool clear /*clear tcp handle or not*/);
static void      cliDestroy(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
99

dengyihao's avatar
dengyihao 已提交
100
// process data read from server, add decompress etc later
U
ubuntu 已提交
101
static void cliHandleResp(SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
102
// handle except about conn
U
ubuntu 已提交
103
static void cliHandleExcept(SCliConn* conn);
104
// handle req from app
U
ubuntu 已提交
105 106 107
static void cliHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);
static void cliHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd);
static void cliSendQuit(SCliThrdObj* thrd);
U
ubuntu 已提交
108
static void destroyUserdata(STransMsg* userdata);
dengyihao's avatar
dengyihao 已提交
109

U
ubuntu 已提交
110
static int cliRBChoseIdx(STrans* pTransInst);
dengyihao's avatar
dengyihao 已提交
111

dengyihao's avatar
dengyihao 已提交
112 113
static void destroyCmsg(SCliMsg* cmsg);
static void transDestroyConnCtx(STransConnCtx* ctx);
dengyihao's avatar
dengyihao 已提交
114 115 116
// thread obj
static SCliThrdObj* createThrdObj();
static void         destroyThrdObj(SCliThrdObj* pThrd);
dengyihao's avatar
dengyihao 已提交
117 118 119 120

#define CONN_HOST_THREAD_INDEX(conn) (conn ? ((SCliConn*)conn)->hThrdIdx : -1)
#define CONN_PERSIST_TIME(para) (para * 1000 * 10)

U
ubuntu 已提交
121
#define CONN_GET_INST_LABEL(conn) (((STrans*)(((SCliThrdObj*)conn->hostThrd)->pTransInst))->label)
dengyihao's avatar
dengyihao 已提交
122 123 124
#define CONN_HANDLE_THREAD_QUIT(conn, thrd) \
  do {                                      \
    if (thrd->quit) {                       \
U
ubuntu 已提交
125
      cliHandleExcept(conn);             \
dengyihao's avatar
dengyihao 已提交
126
      goto _RETURE;                         \
dengyihao's avatar
dengyihao 已提交
127 128 129 130 131 132
    }                                       \
  } while (0)

#define CONN_HANDLE_BROKEN(conn) \
  do {                           \
    if (conn->broken) {          \
U
ubuntu 已提交
133
      cliHandleExcept(conn);  \
dengyihao's avatar
dengyihao 已提交
134
      goto _RETURE;              \
dengyihao's avatar
dengyihao 已提交
135 136 137
    }                            \
  } while (0);

U
ubuntu 已提交
138
#define CONN_SET_PERSIST_BY_APP(conn) do { if (conn->persist == false) { conn->persist = true; transRefCliHandle(conn);}} while(0)
U
ubuntu 已提交
139 140
#define CONN_NO_PERSIST_BY_APP(conn) ((conn)->persist == false)

U
ubuntu 已提交
141
static void* cliWorkThread(void* arg);
dengyihao's avatar
dengyihao 已提交
142

U
ubuntu 已提交
143
void  cliHandleResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
144
  SCliThrdObj* pThrd = conn->hostThrd;
U
ubuntu 已提交
145
  STrans*    pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
146

dengyihao's avatar
dengyihao 已提交
147 148 149 150
  STransMsgHead* pHead = (STransMsgHead*)(conn->readBuf.buf);
  pHead->code = htonl(pHead->code);
  pHead->msgLen = htonl(pHead->msgLen);

dengyihao's avatar
fix bug  
dengyihao 已提交
151 152 153
  // buf's mem alread translated to rpcMsg.pCont
  transClearBuffer(&conn->readBuf);

U
ubuntu 已提交
154
  STransMsg rpcMsg = {0};
dengyihao's avatar
dengyihao 已提交
155
  rpcMsg.contLen = transContLenFromMsg(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
156
  rpcMsg.pCont = transContFromHead((char*)pHead);
dengyihao's avatar
dengyihao 已提交
157 158
  rpcMsg.code = pHead->code;
  rpcMsg.msgType = pHead->msgType;
U
ubuntu 已提交
159
  rpcMsg.ahandle = NULL; 
dengyihao's avatar
dengyihao 已提交
160

U
ubuntu 已提交
161 162 163 164 165 166 167 168 169 170 171
  SCliMsg* pMsg = conn->data;
  STransConnCtx *pCtx = pMsg ? pMsg->ctx : NULL;
  if (pMsg == NULL && !CONN_NO_PERSIST_BY_APP(conn)) {
    rpcMsg.ahandle = pTransInst->mfp ? (*pTransInst->mfp)(pTransInst->parent, rpcMsg.msgType) : NULL;
  } else {
    rpcMsg.ahandle = pCtx ? pCtx->ahandle : NULL;  
  }
  //if (rpcMsg.ahandle == NULL) {
  //  tDebug("%s cli conn %p handle except", CONN_GET_INST_LABEL(conn), conn);
  //  return;
  //}
dengyihao's avatar
dengyihao 已提交
172

U
ubuntu 已提交
173 174
  if (pTransInst->pfp != NULL && (*pTransInst->pfp)(pTransInst->parent, rpcMsg.msgType)) {
    rpcMsg.handle = conn;
U
ubuntu 已提交
175
    CONN_SET_PERSIST_BY_APP(conn);
U
ubuntu 已提交
176
    tDebug("%s cli conn %p ref by app", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
177 178
  }

U
ubuntu 已提交
179
  tDebug("%s cli conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pTransInst->label, conn,
dengyihao's avatar
fix bug  
dengyihao 已提交
180 181
         TMSG_INFO(pHead->msgType), inet_ntoa(conn->addr.sin_addr), ntohs(conn->addr.sin_port),
         inet_ntoa(conn->locaddr.sin_addr), ntohs(conn->locaddr.sin_port), rpcMsg.contLen);
dengyihao's avatar
dengyihao 已提交
182

dengyihao's avatar
dengyihao 已提交
183
  conn->secured = pHead->secured;
dengyihao's avatar
dengyihao 已提交
184

U
ubuntu 已提交
185
  if (pCtx == NULL || pCtx->pSem == NULL) {
U
ubuntu 已提交
186
    tTrace("%s cli conn %p handle resp", pTransInst->label, conn);
dengyihao's avatar
test  
dengyihao 已提交
187 188
    (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL);
  } else {
U
ubuntu 已提交
189
    tTrace("%s cli conn(sync) %p handle resp", pTransInst->label, conn);
dengyihao's avatar
test  
dengyihao 已提交
190 191
    memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg));
    tsem_post(pCtx->pSem);
dengyihao's avatar
dengyihao 已提交
192
  }
dengyihao's avatar
dengyihao 已提交
193

U
ubuntu 已提交
194
  uv_read_start((uv_stream_t*)conn->stream, cliAllocBufferCb, cliRecvCb);
dengyihao's avatar
dengyihao 已提交
195

U
ubuntu 已提交
196
  if (CONN_NO_PERSIST_BY_APP(conn)) {
dengyihao's avatar
dengyihao 已提交
197
    addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn);
dengyihao's avatar
dengyihao 已提交
198
  }
dengyihao's avatar
test  
dengyihao 已提交
199 200 201
  destroyCmsg(conn->data);
  conn->data = NULL;

dengyihao's avatar
dengyihao 已提交
202
  // start thread's timer of conn pool if not active
dengyihao's avatar
dengyihao 已提交
203
  if (!uv_is_active((uv_handle_t*)&pThrd->timer) && pTransInst->idleTime > 0) {
U
ubuntu 已提交
204
    // uv_timer_start((uv_timer_t*)&pThrd->timer, cliTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
205
  }
dengyihao's avatar
dengyihao 已提交
206
}
U
ubuntu 已提交
207 208

void cliHandleExcept(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
209
  if (pConn->data == NULL) {
U
ubuntu 已提交
210 211 212 213
     if (pConn->broken == true || CONN_NO_PERSIST_BY_APP(pConn)) {
        transUnrefCliHandle(pConn);
        return;
     }
dengyihao's avatar
dengyihao 已提交
214
  }
dengyihao's avatar
dengyihao 已提交
215
  SCliThrdObj* pThrd = pConn->hostThrd;
U
ubuntu 已提交
216
  STrans*    pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
217

dengyihao's avatar
dengyihao 已提交
218
  SCliMsg*       pMsg = pConn->data;
U
ubuntu 已提交
219
  STransConnCtx *pCtx = pMsg ? pMsg->ctx : NULL;
dengyihao's avatar
dengyihao 已提交
220

U
ubuntu 已提交
221
  STransMsg rpcMsg = {0};
dengyihao's avatar
dengyihao 已提交
222
  rpcMsg.code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
U
ubuntu 已提交
223 224 225 226 227 228 229 230
  rpcMsg.msgType = pMsg ? pMsg->msg.msgType + 1 : 0;
  rpcMsg.ahandle = NULL; 

  if (pMsg == NULL && !CONN_NO_PERSIST_BY_APP(pConn)) {
    rpcMsg.ahandle = pTransInst->mfp ? (*pTransInst->mfp)(pTransInst->parent, rpcMsg.msgType) : NULL;
  } else {
    rpcMsg.ahandle = pCtx ? pCtx->ahandle : NULL;  
  }
dengyihao's avatar
dengyihao 已提交
231

U
ubuntu 已提交
232
  if (pCtx == NULL || pCtx->pSem == NULL) {
U
ubuntu 已提交
233
    tTrace("%s cli conn %p handle resp", pTransInst->label, pConn);
dengyihao's avatar
dengyihao 已提交
234
    (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
235
  } else {
U
ubuntu 已提交
236
    tTrace("%s cli conn(sync) %p handle resp", pTransInst->label, pConn);
dengyihao's avatar
dengyihao 已提交
237 238
    memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg));
    tsem_post(pCtx->pSem);
dengyihao's avatar
dengyihao 已提交
239
  }
dengyihao's avatar
test  
dengyihao 已提交
240 241 242
  destroyCmsg(pConn->data);
  pConn->data = NULL;

U
ubuntu 已提交
243
  tTrace("%s cli conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
244
  transUnrefCliHandle(pConn);
dengyihao's avatar
dengyihao 已提交
245
}
dengyihao's avatar
dengyihao 已提交
246

U
ubuntu 已提交
247
void cliTimeoutCb(uv_timer_t* handle) {
dengyihao's avatar
dengyihao 已提交
248
  SCliThrdObj* pThrd = handle->data;
U
ubuntu 已提交
249
  STrans*    pRpc = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
250
  int64_t      currentTime = pThrd->nextTimeout;
U
ubuntu 已提交
251
  tTrace("%s, cli conn timeout, try to remove expire conn from conn pool", pRpc->label);
dengyihao's avatar
dengyihao 已提交
252

dengyihao's avatar
dengyihao 已提交
253
  SConnList* p = taosHashIterate((SHashObj*)pThrd->pool, NULL);
dengyihao's avatar
dengyihao 已提交
254 255 256 257 258 259
  while (p != NULL) {
    while (!QUEUE_IS_EMPTY(&p->conn)) {
      queue*    h = QUEUE_HEAD(&p->conn);
      SCliConn* c = QUEUE_DATA(h, SCliConn, conn);
      if (c->expireTime < currentTime) {
        QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
260
        transUnrefCliHandle(c);
dengyihao's avatar
dengyihao 已提交
261 262 263 264
      } else {
        break;
      }
    }
dengyihao's avatar
dengyihao 已提交
265
    p = taosHashIterate((SHashObj*)pThrd->pool, p);
dengyihao's avatar
dengyihao 已提交
266 267
  }

dengyihao's avatar
dengyihao 已提交
268
  pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
U
ubuntu 已提交
269
  uv_timer_start(handle, cliTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
270
}
U
ubuntu 已提交
271 272

void* createConnPool(int size) {
273 274
  // thread local, no lock
  return taosHashInit(size, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
275
}
U
ubuntu 已提交
276
void* destroyConnPool(void* pool) {
dengyihao's avatar
dengyihao 已提交
277
  SConnList* connList = taosHashIterate((SHashObj*)pool, NULL);
dengyihao's avatar
dengyihao 已提交
278 279 280 281 282
  while (connList != NULL) {
    while (!QUEUE_IS_EMPTY(&connList->conn)) {
      queue* h = QUEUE_HEAD(&connList->conn);
      QUEUE_REMOVE(h);
      SCliConn* c = QUEUE_DATA(h, SCliConn, conn);
U
ubuntu 已提交
283
      cliDestroyConn(c, true);
dengyihao's avatar
dengyihao 已提交
284
    }
dengyihao's avatar
dengyihao 已提交
285
    connList = taosHashIterate((SHashObj*)pool, connList);
dengyihao's avatar
dengyihao 已提交
286
  }
dengyihao's avatar
dengyihao 已提交
287
  taosHashCleanup(pool);
dengyihao's avatar
dengyihao 已提交
288 289
}

dengyihao's avatar
dengyihao 已提交
290
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) {
dengyihao's avatar
dengyihao 已提交
291 292 293 294
  char key[128] = {0};
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));

dengyihao's avatar
dengyihao 已提交
295 296
  SHashObj*  pPool = pool;
  SConnList* plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
297 298
  if (plist == NULL) {
    SConnList list;
dengyihao's avatar
dengyihao 已提交
299 300
    taosHashPut(pPool, key, strlen(key), (void*)&list, sizeof(list));
    plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
301
    QUEUE_INIT(&plist->conn);
dengyihao's avatar
dengyihao 已提交
302 303 304 305 306 307 308
  }

  if (QUEUE_IS_EMPTY(&plist->conn)) {
    return NULL;
  }
  queue* h = QUEUE_HEAD(&plist->conn);
  QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
309 310 311 312

  SCliConn* conn = QUEUE_DATA(h, SCliConn, conn);
  QUEUE_INIT(&conn->conn);
  return conn;
dengyihao's avatar
dengyihao 已提交
313
}
dengyihao's avatar
dengyihao 已提交
314
static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
315
  char key[128] = {0};
dengyihao's avatar
dengyihao 已提交
316

dengyihao's avatar
dengyihao 已提交
317 318
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));
U
ubuntu 已提交
319
  tTrace("cli conn %p added to conn pool, read buf cap: %d", conn, conn->readBuf.cap);
dengyihao's avatar
dengyihao 已提交
320

U
ubuntu 已提交
321
  STrans* pRpc = ((SCliThrdObj*)conn->hostThrd)->pTransInst;
dengyihao's avatar
dengyihao 已提交
322

dengyihao's avatar
dengyihao 已提交
323
  conn->expireTime = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
324
  SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
325 326 327 328
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
}
U
ubuntu 已提交
329
static void cliAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
330 331
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
332
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
333
}
U
ubuntu 已提交
334
static void cliRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
335
  // impl later
dengyihao's avatar
dengyihao 已提交
336 337 338
  if (handle->data == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
339 340
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
341
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
342
    pBuf->len += nread;
dengyihao's avatar
dengyihao 已提交
343
    if (transReadComplete(pBuf)) {
U
ubuntu 已提交
344 345
      tTrace("%s cli conn %p read complete", CONN_GET_INST_LABEL(conn), conn);
      cliHandleResp(conn);
dengyihao's avatar
dengyihao 已提交
346
    } else {
U
ubuntu 已提交
347
      tTrace("%s cli conn %p read partial packet, continue to read", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
348
    }
dengyihao's avatar
dengyihao 已提交
349 350
    return;
  }
dengyihao's avatar
dengyihao 已提交
351

352 353
  assert(nread <= 0);
  if (nread == 0) {
dengyihao's avatar
dengyihao 已提交
354 355 356
    // ref http://docs.libuv.org/en/v1.x/stream.html?highlight=uv_read_start#c.uv_read_cb
    // nread might be 0, which does not indicate an error or EOF. This is equivalent to EAGAIN or EWOULDBLOCK under
    // read(2).
357 358
    return;
  }
dengyihao's avatar
fix bug  
dengyihao 已提交
359
  if (nread < 0) {
U
ubuntu 已提交
360
    tError("%s cli conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
361
    conn->broken = true;
U
ubuntu 已提交
362
    cliHandleExcept(conn);
dengyihao's avatar
dengyihao 已提交
363
  }
dengyihao's avatar
dengyihao 已提交
364
}
dengyihao's avatar
dengyihao 已提交
365

U
ubuntu 已提交
366
static SCliConn* cliCreateConn(SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
367 368 369 370 371 372 373 374 375 376 377
  SCliConn* conn = calloc(1, sizeof(SCliConn));
  // read/write stream handle
  conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
  uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
  conn->stream->data = conn;

  conn->writeReq.data = conn;
  conn->connReq.data = conn;

  QUEUE_INIT(&conn->conn);
  conn->hostThrd = pThrd;
U
ubuntu 已提交
378
  conn->persist = false;
dengyihao's avatar
dengyihao 已提交
379 380 381 382
  conn->broken = false;
  transRefCliHandle(conn);
  return conn;
}
U
ubuntu 已提交
383 384
static void cliDestroyConn(SCliConn* conn, bool clear) {
  tTrace("%s cli conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
385 386
  QUEUE_REMOVE(&conn->conn);
  if (clear) {
U
ubuntu 已提交
387
    uv_close((uv_handle_t*)conn->stream, cliDestroy);
388
  }
dengyihao's avatar
dengyihao 已提交
389
}
U
ubuntu 已提交
390
static void cliDestroy(uv_handle_t* handle) {
dengyihao's avatar
dengyihao 已提交
391
  SCliConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
392 393

  free(conn->stream);
U
ubuntu 已提交
394
  tTrace("%s cli conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
395
  free(conn);
dengyihao's avatar
dengyihao 已提交
396 397
}

U
ubuntu 已提交
398
static void cliSendCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
399
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
400

dengyihao's avatar
dengyihao 已提交
401
  if (status == 0) {
U
ubuntu 已提交
402
    tTrace("%s cli conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
403
    SCliMsg* pMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
404
    if (pMsg == NULL) {
dengyihao's avatar
dengyihao 已提交
405
      return;
dengyihao's avatar
dengyihao 已提交
406
    }
dengyihao's avatar
dengyihao 已提交
407
    destroyUserdata(&pMsg->msg);
dengyihao's avatar
dengyihao 已提交
408
  } else {
U
ubuntu 已提交
409 410
    tError("%s cli conn %p failed to write: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status));
    cliHandleExcept(pConn);
dengyihao's avatar
dengyihao 已提交
411 412
    return;
  }
U
ubuntu 已提交
413
  uv_read_start((uv_stream_t*)pConn->stream, cliAllocBufferCb, cliRecvCb);
dengyihao's avatar
dengyihao 已提交
414
}
dengyihao's avatar
dengyihao 已提交
415

U
ubuntu 已提交
416
void cliSend(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
417 418
  CONN_HANDLE_BROKEN(pConn);

dengyihao's avatar
dengyihao 已提交
419
  SCliMsg*       pCliMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
420
  STransConnCtx* pCtx = pCliMsg->ctx;
dengyihao's avatar
dengyihao 已提交
421 422

  SCliThrdObj* pThrd = pConn->hostThrd;
U
ubuntu 已提交
423
  STrans*    pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
424

U
ubuntu 已提交
425
  STransMsg* pMsg = (STransMsg*)(&pCliMsg->msg);
dengyihao's avatar
dengyihao 已提交
426

dengyihao's avatar
dengyihao 已提交
427
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
dengyihao's avatar
dengyihao 已提交
428 429 430 431 432 433 434
  int            msgLen = transMsgLenFromCont(pMsg->contLen);

  if (!pConn->secured) {
    char* buf = calloc(1, msgLen + sizeof(STransUserMsg));
    memcpy(buf, (char*)pHead, msgLen);

    STransUserMsg* uMsg = (STransUserMsg*)(buf + msgLen);
dengyihao's avatar
dengyihao 已提交
435 436
    memcpy(uMsg->user, pTransInst->user, tListLen(uMsg->user));
    memcpy(uMsg->secret, pTransInst->secret, tListLen(uMsg->secret));
dengyihao's avatar
dengyihao 已提交
437

dengyihao's avatar
dengyihao 已提交
438 439 440 441 442 443 444
    // to avoid mem leak
    destroyUserdata(pMsg);

    pMsg->pCont = (char*)buf + sizeof(STransMsgHead);
    pMsg->contLen = msgLen + sizeof(STransUserMsg) - sizeof(STransMsgHead);

    pHead = (STransMsgHead*)buf;
dengyihao's avatar
dengyihao 已提交
445
    pHead->secured = 1;
dengyihao's avatar
dengyihao 已提交
446 447
    msgLen += sizeof(STransUserMsg);
  }
dengyihao's avatar
dengyihao 已提交
448

dengyihao's avatar
dengyihao 已提交
449 450 451 452
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);

  uv_buf_t wb = uv_buf_init((char*)pHead, msgLen);
U
ubuntu 已提交
453
  tDebug("%s cli conn %p %s is send to %s:%d, local info %s:%d", CONN_GET_INST_LABEL(pConn), pConn,
dengyihao's avatar
dengyihao 已提交
454 455 456
         TMSG_INFO(pHead->msgType), inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port),
         inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port));

U
ubuntu 已提交
457
  uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, cliSendCb);
dengyihao's avatar
dengyihao 已提交
458 459 460 461

  return;
_RETURE:
  return;
dengyihao's avatar
dengyihao 已提交
462
}
U
ubuntu 已提交
463 464

void cliConnCb(uv_connect_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
465
  // impl later
dengyihao's avatar
dengyihao 已提交
466 467
  SCliConn* pConn = req->data;
  if (status != 0) {
U
ubuntu 已提交
468 469
    tError("%s cli conn %p failed to connect server: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status));
    cliHandleExcept(pConn);
470
    return;
dengyihao's avatar
dengyihao 已提交
471
  }
dengyihao's avatar
dengyihao 已提交
472 473 474
  int addrlen = sizeof(pConn->addr);
  uv_tcp_getpeername((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->addr, &addrlen);

dengyihao's avatar
dengyihao 已提交
475 476 477
  addrlen = sizeof(pConn->locaddr);
  uv_tcp_getsockname((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->locaddr, &addrlen);

U
ubuntu 已提交
478
  tTrace("%s cli conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
479

dengyihao's avatar
dengyihao 已提交
480
  assert(pConn->stream == req->handle);
U
ubuntu 已提交
481
  cliSend(pConn);
dengyihao's avatar
dengyihao 已提交
482 483
}

U
ubuntu 已提交
484 485
static void cliHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) {
  tDebug("cli work thread %p start to quit", pThrd);
dengyihao's avatar
dengyihao 已提交
486
  destroyCmsg(pMsg);
dengyihao's avatar
fix bug  
dengyihao 已提交
487
  destroyConnPool(pThrd->pool);
dengyihao's avatar
dengyihao 已提交
488 489 490

  uv_timer_stop(&pThrd->timer);

dengyihao's avatar
dengyihao 已提交
491 492 493
  pThrd->quit = true;
  uv_stop(pThrd->loop);
}
U
ubuntu 已提交
494 495

SCliConn* cliGetConn(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
496
  SCliConn* conn = NULL;
dengyihao's avatar
dengyihao 已提交
497 498
  if (pMsg->msg.handle != NULL) {
    conn = (SCliConn*)(pMsg->msg.handle);
dengyihao's avatar
dengyihao 已提交
499
    if (conn != NULL) {
U
ubuntu 已提交
500
      tTrace("%s cli conn %p reused", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
501 502
    }
  } else {
dengyihao's avatar
dengyihao 已提交
503
    STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
504
    conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port);
U
ubuntu 已提交
505
    if (conn != NULL) tTrace("%s cli conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
506
  }
dengyihao's avatar
dengyihao 已提交
507 508
  return conn;
}
U
ubuntu 已提交
509 510

void cliHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
511 512
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
U
ubuntu 已提交
513
  tTrace("%s cli msg tran time cost: %" PRIu64 "us", ((STrans*)pThrd->pTransInst)->label, el);
dengyihao's avatar
dengyihao 已提交
514 515

  STransConnCtx* pCtx = pMsg->ctx;
U
ubuntu 已提交
516
  STrans*      pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
517

U
ubuntu 已提交
518
  SCliConn* conn = cliGetConn(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
519
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
520
    conn->data = pMsg;
dengyihao's avatar
dengyihao 已提交
521
    transDestroyBuffer(&conn->readBuf);
U
ubuntu 已提交
522
    cliSend(conn);
dengyihao's avatar
dengyihao 已提交
523
  } else {
U
ubuntu 已提交
524
    conn = cliCreateConn(pThrd);
dengyihao's avatar
dengyihao 已提交
525
    conn->data = pMsg;
dengyihao's avatar
dengyihao 已提交
526

dengyihao's avatar
dengyihao 已提交
527 528
    int ret = transSetConnOption((uv_tcp_t*)conn->stream);
    if (ret) {
U
ubuntu 已提交
529
      tError("%s cli conn %p failed to set conn option, errmsg %s", pTransInst->label, conn, uv_err_name(ret));
dengyihao's avatar
dengyihao 已提交
530
    }
dengyihao's avatar
dengyihao 已提交
531
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
532
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
533
    // handle error in callback if fail to connect
U
ubuntu 已提交
534 535
    tTrace("%s cli conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port);
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb);
dengyihao's avatar
dengyihao 已提交
536
  }
U
ubuntu 已提交
537
  
dengyihao's avatar
dengyihao 已提交
538
  conn->hThrdIdx = pCtx->hThrdIdx;
dengyihao's avatar
dengyihao 已提交
539
}
U
ubuntu 已提交
540
static void cliAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
541 542
  SAsyncItem*  item = handle->data;
  SCliThrdObj* pThrd = item->pThrd;
dengyihao's avatar
dengyihao 已提交
543
  SCliMsg*     pMsg = NULL;
dengyihao's avatar
dengyihao 已提交
544

dengyihao's avatar
dengyihao 已提交
545
  // batch process to avoid to lock/unlock frequently
U
ubuntu 已提交
546
  queue        wq;
dengyihao's avatar
dengyihao 已提交
547 548 549
  pthread_mutex_lock(&item->mtx);
  QUEUE_MOVE(&item->qmsg, &wq);
  pthread_mutex_unlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
550

dengyihao's avatar
dengyihao 已提交
551 552 553 554
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
555 556

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
557
    if (pMsg->ctx == NULL) {
U
ubuntu 已提交
558
      cliHandleQuit(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
559
    } else {
U
ubuntu 已提交
560
      cliHandleReq(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
561
    }
dengyihao's avatar
dengyihao 已提交
562
    count++;
dengyihao's avatar
dengyihao 已提交
563 564
  }
  if (count >= 2) {
U
ubuntu 已提交
565
    tTrace("cli process batch size: %d", count);
dengyihao's avatar
dengyihao 已提交
566
  }
dengyihao's avatar
dengyihao 已提交
567 568
}

U
ubuntu 已提交
569
static void* cliWorkThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
570
  SCliThrdObj* pThrd = (SCliThrdObj*)arg;
U
ubuntu 已提交
571
  setThreadName("trans-cli-work");
dengyihao's avatar
dengyihao 已提交
572 573 574
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
}

U
ubuntu 已提交
575
void* transInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
U
ubuntu 已提交
576
  SCliObj* cli = calloc(1, sizeof(SCliObj));
dengyihao's avatar
dengyihao 已提交
577

U
ubuntu 已提交
578
  STrans* pRpc = shandle;
dengyihao's avatar
dengyihao 已提交
579 580 581 582 583
  memcpy(cli->label, label, strlen(label));
  cli->numOfThreads = numOfThreads;
  cli->pThreadObj = (SCliThrdObj**)calloc(cli->numOfThreads, sizeof(SCliThrdObj*));

  for (int i = 0; i < cli->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
584
    SCliThrdObj* pThrd = createThrdObj();
dengyihao's avatar
dengyihao 已提交
585
    pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
586
    pThrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
587

U
ubuntu 已提交
588
    int err = pthread_create(&pThrd->thread, NULL, cliWorkThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
589
    if (err == 0) {
U
ubuntu 已提交
590
      tDebug("success to create tranport-cli thread %d", i);
dengyihao's avatar
dengyihao 已提交
591
    }
dengyihao's avatar
dengyihao 已提交
592
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
593 594 595
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
596

U
ubuntu 已提交
597
static void destroyUserdata(STransMsg* userdata) {
dengyihao's avatar
dengyihao 已提交
598 599 600 601 602 603 604 605 606 607 608 609
  if (userdata->pCont == NULL) {
    return;
  }
  transFreeMsg(userdata->pCont);
  userdata->pCont = NULL;
}
static void destroyCmsg(SCliMsg* pMsg) {
  if (pMsg == NULL) {
    return;
  }
  transDestroyConnCtx(pMsg->ctx);
  destroyUserdata(&pMsg->msg);
dengyihao's avatar
dengyihao 已提交
610 611
  free(pMsg);
}
dengyihao's avatar
dengyihao 已提交
612

dengyihao's avatar
dengyihao 已提交
613 614
static SCliThrdObj* createThrdObj() {
  SCliThrdObj* pThrd = (SCliThrdObj*)calloc(1, sizeof(SCliThrdObj));
U
ubuntu 已提交
615

dengyihao's avatar
dengyihao 已提交
616 617 618 619 620 621
  QUEUE_INIT(&pThrd->msg);
  pthread_mutex_init(&pThrd->msgMtx, NULL);

  pThrd->loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
  uv_loop_init(pThrd->loop);

U
ubuntu 已提交
622
  pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, cliAsyncCb);
dengyihao's avatar
dengyihao 已提交
623

dengyihao's avatar
dengyihao 已提交
624 625
  uv_timer_init(pThrd->loop, &pThrd->timer);
  pThrd->timer.data = pThrd;
dengyihao's avatar
dengyihao 已提交
626

dengyihao's avatar
dengyihao 已提交
627
  pThrd->pool = createConnPool(4);
dengyihao's avatar
dengyihao 已提交
628 629

  pThrd->quit = false;
dengyihao's avatar
dengyihao 已提交
630 631
  return pThrd;
}
dengyihao's avatar
dengyihao 已提交
632
static void destroyThrdObj(SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
633 634 635
  if (pThrd == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
636
  uv_stop(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
637 638
  pthread_join(pThrd->thread, NULL);
  pthread_mutex_destroy(&pThrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
639
  transDestroyAsyncPool(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
640 641

  uv_timer_stop(&pThrd->timer);
dengyihao's avatar
dengyihao 已提交
642 643 644
  free(pThrd->loop);
  free(pThrd);
}
dengyihao's avatar
dengyihao 已提交
645

dengyihao's avatar
dengyihao 已提交
646
static void transDestroyConnCtx(STransConnCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
647 648 649 650 651
  if (ctx != NULL) {
    free(ctx->ip);
  }
  free(ctx);
}
dengyihao's avatar
dengyihao 已提交
652
//
U
ubuntu 已提交
653
void cliSendQuit(SCliThrdObj* thrd) {
dengyihao's avatar
dengyihao 已提交
654 655
  // cli can stop gracefully
  SCliMsg* msg = calloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
656
  transSendAsync(thrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
657
}
U
ubuntu 已提交
658 659 660 661 662 663 664 665 666
int cliRBChoseIdx(STrans* pTransInst) {
  int64_t index = pTransInst->index;
  if (pTransInst->index++ >= pTransInst->numOfThreads) {
    pTransInst->index = 0;
  }
  return index % pTransInst->numOfThreads;
}

void transCloseClient(void* arg) {
U
ubuntu 已提交
667
  SCliObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
668
  for (int i = 0; i < cli->numOfThreads; i++) {
U
ubuntu 已提交
669
    cliSendQuit(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
670
    destroyThrdObj(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
671 672 673
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
674
}
dengyihao's avatar
dengyihao 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687
void transRefCliHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  int ref = T_REF_INC((SCliConn*)handle);
  UNUSED(ref);
}
void transUnrefCliHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  int ref = T_REF_DEC((SCliConn*)handle);
  if (ref == 0) {
U
ubuntu 已提交
688
    cliDestroyConn((SCliConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
689 690
  }
}
dengyihao's avatar
dengyihao 已提交
691

U
ubuntu 已提交
692 693 694
void transSendRequest(void *shandle, const char *ip, uint32_t port, STransMsg *pMsg) {
  STrans* pTransInst = (STrans*)shandle;
  int index = CONN_HOST_THREAD_INDEX((SCliConn *)pMsg->handle);
dengyihao's avatar
dengyihao 已提交
695
  if (index == -1) {
U
ubuntu 已提交
696
    index = cliRBChoseIdx(pTransInst);
dengyihao's avatar
dengyihao 已提交
697
  }
dengyihao's avatar
dengyihao 已提交
698 699 700 701
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
U
ubuntu 已提交
702
  tDebug("send request at thread:%d %p", index, pMsg);
dengyihao's avatar
dengyihao 已提交
703 704 705 706 707
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));
  pCtx->ahandle = pMsg->ahandle;
  pCtx->msgType = pMsg->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
708
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
709

dengyihao's avatar
dengyihao 已提交
710
  assert(pTransInst->connType == TAOS_CONN_CLIENT);
dengyihao's avatar
dengyihao 已提交
711
  // atomic or not
dengyihao's avatar
dengyihao 已提交
712

dengyihao's avatar
dengyihao 已提交
713 714 715 716
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
717

U
ubuntu 已提交
718
  SCliThrdObj* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
719
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
dengyihao's avatar
dengyihao 已提交
720
}
U
ubuntu 已提交
721 722
void transSendRecv(void* shandle, const char *ip, uint32_t port, STransMsg *pReq, STransMsg *pRsp) {
  STrans* pTransInst = (STrans*)shandle;
dengyihao's avatar
dengyihao 已提交
723
  int index = CONN_HOST_THREAD_INDEX(pReq->handle);
dengyihao's avatar
dengyihao 已提交
724
  if (index == -1) {
U
ubuntu 已提交
725
    index = cliRBChoseIdx(pTransInst);
dengyihao's avatar
dengyihao 已提交
726 727
  }

dengyihao's avatar
dengyihao 已提交
728 729 730 731 732
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));
  pCtx->ahandle = pReq->ahandle;
  pCtx->msgType = pReq->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
733
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
734 735 736 737 738 739 740 741 742
  pCtx->pSem = calloc(1, sizeof(tsem_t));
  pCtx->pRsp = pRsp;
  tsem_init(pCtx->pSem, 0, 0);

  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pReq;
  cliMsg->st = taosGetTimestampUs();

U
ubuntu 已提交
743
  SCliThrdObj* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
744 745 746 747 748 749
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
  tsem_t* pSem = pCtx->pSem;
  tsem_wait(pSem);
  tsem_destroy(pSem);
  free(pSem);
}
U
ubuntu 已提交
750

dengyihao's avatar
dengyihao 已提交
751
#endif