transCli.c 22.2 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
/*
 * 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"

dengyihao's avatar
dengyihao 已提交
20
#define CONN_HOST_THREAD_INDEX(conn) (conn ? ((SCliConn*)conn)->hThrdIdx : -1)
21
#define CONN_PERSIST_TIME(para) (para * 1000 * 10)
dengyihao's avatar
dengyihao 已提交
22

dengyihao's avatar
dengyihao 已提交
23 24 25
typedef struct SCliConn {
  uv_connect_t connReq;
  uv_stream_t* stream;
dengyihao's avatar
dengyihao 已提交
26
  uv_write_t*  writeReq;
dengyihao's avatar
dengyihao 已提交
27
  void*        hostThrd;
dengyihao's avatar
dengyihao 已提交
28
  SConnBuffer  readBuf;
dengyihao's avatar
dengyihao 已提交
29 30
  void*        data;
  queue        conn;
dengyihao's avatar
dengyihao 已提交
31
  uint64_t     expireTime;
dengyihao's avatar
dengyihao 已提交
32 33
  int8_t       ctnRdCnt;  // continue read count
  int          hThrdIdx;
dengyihao's avatar
dengyihao 已提交
34

dengyihao's avatar
dengyihao 已提交
35 36 37 38 39 40 41
  SRpcPush* push;
  int       persist;  //
  // spi configure
  char    spi;
  char    secured;
  int32_t ref;
  // debug and log info
dengyihao's avatar
dengyihao 已提交
42
  struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
43
  struct sockaddr_in locaddr;
dengyihao's avatar
dengyihao 已提交
44
} SCliConn;
dengyihao's avatar
dengyihao 已提交
45

dengyihao's avatar
dengyihao 已提交
46
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
47 48 49 50
  STransConnCtx* ctx;
  SRpcMsg        msg;
  queue          q;
  uint64_t       st;
dengyihao's avatar
dengyihao 已提交
51 52 53
} SCliMsg;

typedef struct SCliThrdObj {
dengyihao's avatar
dengyihao 已提交
54 55 56 57
  pthread_t  thread;
  uv_loop_t* loop;
  // uv_async_t*     cliAsync;  //
  SAsyncPool*     asyncPool;
dengyihao's avatar
dengyihao 已提交
58
  uv_timer_t*     timer;
dengyihao's avatar
dengyihao 已提交
59
  void*           pool;  // conn pool
dengyihao's avatar
dengyihao 已提交
60 61
  queue           msg;
  pthread_mutex_t msgMtx;
dengyihao's avatar
dengyihao 已提交
62
  uint64_t        nextTimeout;  // next timeout
dengyihao's avatar
dengyihao 已提交
63
  void*           pTransInst;   //
dengyihao's avatar
dengyihao 已提交
64
  bool            quit;
dengyihao's avatar
dengyihao 已提交
65 66 67 68 69 70 71 72 73
} SCliThrdObj;

typedef struct SClientObj {
  char          label[TSDB_LABEL_LEN];
  int32_t       index;
  int           numOfThreads;
  SCliThrdObj** pThreadObj;
} SClientObj;

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

dengyihao's avatar
dengyihao 已提交
78
// conn pool
dengyihao's avatar
dengyihao 已提交
79
// add expire timeout and capacity limit
80 81
static void*     creatConnPool(int size);
static void*     destroyConnPool(void* pool);
dengyihao's avatar
dengyihao 已提交
82 83
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 已提交
84

dengyihao's avatar
dengyihao 已提交
85 86
// register timer in each thread to clear expire conn
static void clientTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
87 88 89
// check whether already read complete packet from server
static bool clientReadComplete(SConnBuffer* pBuf);
// alloc buf for read
dengyihao's avatar
dengyihao 已提交
90
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
91
// callback after read nbytes from socket
dengyihao's avatar
dengyihao 已提交
92
static void clientReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
93
// callback after write data to socket
dengyihao's avatar
dengyihao 已提交
94
static void clientWriteCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
95
// callback after conn  to server
dengyihao's avatar
dengyihao 已提交
96
static void clientConnCb(uv_connect_t* req, int status);
dengyihao's avatar
dengyihao 已提交
97
static void clientAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
98
static void clientDestroy(uv_handle_t* handle);
99
static void clientConnDestroy(SCliConn* pConn, bool clear /*clear tcp handle or not*/);
dengyihao's avatar
dengyihao 已提交
100

dengyihao's avatar
dengyihao 已提交
101 102 103 104
// process data read from server, auth/decompress etc later
static void clientHandleResp(SCliConn* conn);
// handle except about conn
static void clientHandleExcept(SCliConn* conn);
105 106
// handle req from app
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);
dengyihao's avatar
dengyihao 已提交
107 108
static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd);
static void clientSendQuit(SCliThrdObj* thrd);
dengyihao's avatar
dengyihao 已提交
109

dengyihao's avatar
dengyihao 已提交
110 111 112 113
static void destroyUserdata(SRpcMsg* userdata);

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

120
static void clientHandleResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
121 122
  SCliMsg*       pMsg = conn->data;
  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
123
  SRpcInfo*      pRpc = pCtx->pTransInst;
dengyihao's avatar
dengyihao 已提交
124

dengyihao's avatar
dengyihao 已提交
125 126 127 128
  STransMsgHead* pHead = (STransMsgHead*)(conn->readBuf.buf);
  pHead->code = htonl(pHead->code);
  pHead->msgLen = htonl(pHead->msgLen);

dengyihao's avatar
dengyihao 已提交
129
  SRpcMsg rpcMsg;
dengyihao's avatar
dengyihao 已提交
130
  rpcMsg.contLen = transContLenFromMsg(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
131
  rpcMsg.pCont = transContFromHead((char*)pHead);
dengyihao's avatar
dengyihao 已提交
132 133
  rpcMsg.code = pHead->code;
  rpcMsg.msgType = pHead->msgType;
dengyihao's avatar
dengyihao 已提交
134
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
135

dengyihao's avatar
dengyihao 已提交
136 137
  if (rpcMsg.msgType == TDMT_VND_QUERY_RSP || rpcMsg.msgType == TDMT_VND_FETCH_RSP ||
      rpcMsg.msgType == TDMT_VND_RES_READY) {
dengyihao's avatar
dengyihao 已提交
138 139
    rpcMsg.handle = conn;
    conn->persist = 1;
dengyihao's avatar
dengyihao 已提交
140
    tDebug("client conn %p persist by app", conn);
dengyihao's avatar
dengyihao 已提交
141 142
  }

dengyihao's avatar
dengyihao 已提交
143 144 145
  tDebug("client conn %p %s received from %s:%d, local info: %s:%d", conn, 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));
dengyihao's avatar
dengyihao 已提交
146

dengyihao's avatar
dengyihao 已提交
147
  conn->secured = pHead->secured;
dengyihao's avatar
dengyihao 已提交
148
  if (conn->push != NULL && conn->ctnRdCnt != 0) {
dengyihao's avatar
dengyihao 已提交
149
    (*conn->push->callback)(conn->push->arg, &rpcMsg);
dengyihao's avatar
dengyihao 已提交
150
    conn->push = NULL;
dengyihao's avatar
dengyihao 已提交
151
  } else {
dengyihao's avatar
dengyihao 已提交
152
    if (pCtx->pSem == NULL) {
dengyihao's avatar
fixBug  
dengyihao 已提交
153
      tTrace("client conn %p handle resp", conn);
dengyihao's avatar
dengyihao 已提交
154 155 156 157 158 159
      (pRpc->cfp)(pRpc->parent, &rpcMsg, NULL);
    } else {
      tTrace("client conn(sync) %p handle resp", conn);
      memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg));
      tsem_post(pCtx->pSem);
    }
dengyihao's avatar
dengyihao 已提交
160
  }
dengyihao's avatar
dengyihao 已提交
161
  conn->ctnRdCnt += 1;
dengyihao's avatar
dengyihao 已提交
162

dengyihao's avatar
dengyihao 已提交
163
  // buf's mem alread translated to rpcMsg.pCont
dengyihao's avatar
dengyihao 已提交
164 165
  transClearBuffer(&conn->readBuf);

dengyihao's avatar
dengyihao 已提交
166
  uv_read_start((uv_stream_t*)conn->stream, clientAllocBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
167 168

  SCliThrdObj* pThrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
169

dengyihao's avatar
dengyihao 已提交
170
  // user owns conn->persist = 1
dengyihao's avatar
dengyihao 已提交
171
  if (conn->push == NULL && conn->persist == 0) {
dengyihao's avatar
dengyihao 已提交
172
    addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn);
dengyihao's avatar
dengyihao 已提交
173
  }
dengyihao's avatar
dengyihao 已提交
174 175
  destroyCmsg(conn->data);
  conn->data = NULL;
dengyihao's avatar
dengyihao 已提交
176
  // start thread's timer of conn pool if not active
dengyihao's avatar
dengyihao 已提交
177
  if (!uv_is_active((uv_handle_t*)pThrd->timer) && pRpc->idleTime > 0) {
dengyihao's avatar
dengyihao 已提交
178
    // uv_timer_start((uv_timer_t*)pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
179
  }
dengyihao's avatar
dengyihao 已提交
180 181
}
static void clientHandleExcept(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
182
  if (pConn->data == NULL && pConn->push == NULL) {
dengyihao's avatar
dengyihao 已提交
183
    // handle conn except in conn pool
dengyihao's avatar
dengyihao 已提交
184 185 186
    clientConnDestroy(pConn, true);
    return;
  }
dengyihao's avatar
dengyihao 已提交
187
  tTrace("client conn %p start to destroy", pConn);
dengyihao's avatar
dengyihao 已提交
188
  SCliMsg* pMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
189

dengyihao's avatar
fixBug  
dengyihao 已提交
190 191 192 193
  tmsg_t msgType = TDMT_MND_CONNECT;
  if (pMsg != NULL) {
    msgType = pMsg->msg.msgType;
  }
dengyihao's avatar
dengyihao 已提交
194 195
  STransConnCtx* pCtx = pMsg->ctx;

dengyihao's avatar
dengyihao 已提交
196
  SRpcMsg rpcMsg = {0};
dengyihao's avatar
dengyihao 已提交
197
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
198
  rpcMsg.code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
dengyihao's avatar
fixBug  
dengyihao 已提交
199
  rpcMsg.msgType = msgType + 1;
dengyihao's avatar
dengyihao 已提交
200

dengyihao's avatar
dengyihao 已提交
201
  if (pConn->push != NULL && pConn->ctnRdCnt != 0) {
dengyihao's avatar
dengyihao 已提交
202
    (*pConn->push->callback)(pConn->push->arg, &rpcMsg);
dengyihao's avatar
dengyihao 已提交
203
    pConn->push = NULL;
dengyihao's avatar
dengyihao 已提交
204
  } else {
dengyihao's avatar
dengyihao 已提交
205 206 207 208 209 210
    if (pCtx->pSem == NULL) {
      (pCtx->pTransInst->cfp)(pCtx->pTransInst->parent, &rpcMsg, NULL);
    } else {
      memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg));
      tsem_post(pCtx->pSem);
    }
dengyihao's avatar
dengyihao 已提交
211 212 213
    if (pConn->push != NULL) {
      (*pConn->push->callback)(pConn->push->arg, &rpcMsg);
    }
dengyihao's avatar
dengyihao 已提交
214 215 216 217 218
    pConn->push = NULL;
  }
  if (pConn->push == NULL) {
    destroyCmsg(pConn->data);
    pConn->data = NULL;
dengyihao's avatar
dengyihao 已提交
219
  }
dengyihao's avatar
dengyihao 已提交
220
  // transDestroyConnCtx(pCtx);
dengyihao's avatar
dengyihao 已提交
221
  clientConnDestroy(pConn, true);
dengyihao's avatar
dengyihao 已提交
222
  pConn->ctnRdCnt += 1;
dengyihao's avatar
dengyihao 已提交
223
}
dengyihao's avatar
dengyihao 已提交
224

dengyihao's avatar
dengyihao 已提交
225 226
static void clientTimeoutCb(uv_timer_t* handle) {
  SCliThrdObj* pThrd = handle->data;
dengyihao's avatar
dengyihao 已提交
227
  SRpcInfo*    pRpc = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
228
  int64_t      currentTime = pThrd->nextTimeout;
dengyihao's avatar
dengyihao 已提交
229
  tTrace("client conn timeout, try to remove expire conn from conn pool");
dengyihao's avatar
dengyihao 已提交
230

dengyihao's avatar
dengyihao 已提交
231
  SConnList* p = taosHashIterate((SHashObj*)pThrd->pool, NULL);
dengyihao's avatar
dengyihao 已提交
232 233 234 235 236 237
  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);
238 239 240
        // uv_stream_t stm = *(c->stream);
        // uv_close((uv_handle_t*)&stm, clientDestroy);
        clientConnDestroy(c, true);
dengyihao's avatar
dengyihao 已提交
241 242 243 244
      } else {
        break;
      }
    }
dengyihao's avatar
dengyihao 已提交
245
    p = taosHashIterate((SHashObj*)pThrd->pool, p);
dengyihao's avatar
dengyihao 已提交
246 247
  }

dengyihao's avatar
dengyihao 已提交
248 249
  pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
  uv_timer_start(handle, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
250
}
251 252 253
static void* creatConnPool(int size) {
  // thread local, no lock
  return taosHashInit(size, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
254
}
255
static void* destroyConnPool(void* pool) {
dengyihao's avatar
dengyihao 已提交
256
  SConnList* connList = taosHashIterate((SHashObj*)pool, NULL);
dengyihao's avatar
dengyihao 已提交
257 258 259 260 261
  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);
262
      clientConnDestroy(c, true);
dengyihao's avatar
dengyihao 已提交
263
    }
dengyihao's avatar
dengyihao 已提交
264
    connList = taosHashIterate((SHashObj*)pool, connList);
dengyihao's avatar
dengyihao 已提交
265
  }
dengyihao's avatar
dengyihao 已提交
266
  taosHashClear(pool);
dengyihao's avatar
dengyihao 已提交
267 268
}

dengyihao's avatar
dengyihao 已提交
269
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) {
dengyihao's avatar
dengyihao 已提交
270 271 272 273
  char key[128] = {0};
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));

dengyihao's avatar
dengyihao 已提交
274 275
  SHashObj*  pPool = pool;
  SConnList* plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
276 277
  if (plist == NULL) {
    SConnList list;
dengyihao's avatar
dengyihao 已提交
278 279
    taosHashPut(pPool, key, strlen(key), (void*)&list, sizeof(list));
    plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
280
    QUEUE_INIT(&plist->conn);
dengyihao's avatar
dengyihao 已提交
281 282 283 284 285 286 287
  }

  if (QUEUE_IS_EMPTY(&plist->conn)) {
    return NULL;
  }
  queue* h = QUEUE_HEAD(&plist->conn);
  QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
288 289 290 291

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

dengyihao's avatar
dengyihao 已提交
296 297
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));
dengyihao's avatar
dengyihao 已提交
298
  tTrace("client conn %p added to conn pool, read buf cap: %d", conn, conn->readBuf.cap);
dengyihao's avatar
dengyihao 已提交
299

dengyihao's avatar
dengyihao 已提交
300
  SRpcInfo* pRpc = ((SCliThrdObj*)conn->hostThrd)->pTransInst;
dengyihao's avatar
dengyihao 已提交
301

dengyihao's avatar
dengyihao 已提交
302
  conn->expireTime = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
303
  SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
304
  conn->ctnRdCnt = 0;
dengyihao's avatar
dengyihao 已提交
305 306 307 308
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
}
dengyihao's avatar
dengyihao 已提交
309 310 311 312 313 314 315 316 317
static bool clientReadComplete(SConnBuffer* data) {
  STransMsgHead head;
  int32_t       headLen = sizeof(head);
  if (data->len >= headLen) {
    memcpy((char*)&head, data->buf, headLen);
    int32_t msgLen = (int32_t)htonl((uint32_t)head.msgLen);
    if (msgLen > data->len) {
      data->left = msgLen - data->len;
      return false;
dengyihao's avatar
dengyihao 已提交
318 319
    } else if (msgLen == data->len) {
      data->left = 0;
dengyihao's avatar
dengyihao 已提交
320 321 322 323 324 325
      return true;
    }
  } else {
    return false;
  }
}
dengyihao's avatar
dengyihao 已提交
326
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
327 328
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
329
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
330 331
}
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
332
  // impl later
dengyihao's avatar
dengyihao 已提交
333 334 335
  if (handle->data == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
336 337
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
338
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
339 340
    pBuf->len += nread;
    if (clientReadComplete(pBuf)) {
dengyihao's avatar
dengyihao 已提交
341
      uv_read_stop((uv_stream_t*)conn->stream);
dengyihao's avatar
dengyihao 已提交
342
      tTrace("client conn %p read complete", conn);
343
      clientHandleResp(conn);
dengyihao's avatar
dengyihao 已提交
344
    } else {
dengyihao's avatar
dengyihao 已提交
345
      tTrace("client conn %p read partial packet, continue to read", conn);
dengyihao's avatar
dengyihao 已提交
346
    }
dengyihao's avatar
dengyihao 已提交
347 348
    return;
  }
349 350
  assert(nread <= 0);
  if (nread == 0) {
dengyihao's avatar
dengyihao 已提交
351 352 353
    // 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).
354 355
    return;
  }
dengyihao's avatar
dengyihao 已提交
356
  if (nread < 0 || nread == UV_EOF) {
dengyihao's avatar
dengyihao 已提交
357
    tError("client conn %p read error: %s", conn, uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
358
    clientHandleExcept(conn);
dengyihao's avatar
dengyihao 已提交
359
  }
360 361
  // tDebug("Read error %s\n", uv_err_name(nread));
  // uv_close((uv_handle_t*)handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
362
}
dengyihao's avatar
dengyihao 已提交
363

364
static void clientConnDestroy(SCliConn* conn, bool clear) {
dengyihao's avatar
dengyihao 已提交
365
  //
dengyihao's avatar
dengyihao 已提交
366 367
  conn->ref--;
  if (conn->ref == 0) {
dengyihao's avatar
dengyihao 已提交
368
    tTrace("client conn %p remove from conn pool", conn);
dengyihao's avatar
dengyihao 已提交
369 370 371 372
    QUEUE_REMOVE(&conn->conn);
    if (clear) {
      uv_close((uv_handle_t*)conn->stream, clientDestroy);
    }
373
  }
dengyihao's avatar
dengyihao 已提交
374
}
dengyihao's avatar
dengyihao 已提交
375 376
static void clientDestroy(uv_handle_t* handle) {
  SCliConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
377
  // transDestroyBuffer(&conn->readBuf);
dengyihao's avatar
dengyihao 已提交
378 379 380

  free(conn->stream);
  free(conn->writeReq);
dengyihao's avatar
dengyihao 已提交
381
  tTrace("client conn %p destroy successfully", conn);
dengyihao's avatar
dengyihao 已提交
382 383 384
  free(conn);

  // clientConnDestroy(conn, false);
dengyihao's avatar
dengyihao 已提交
385 386 387 388 389
}

static void clientWriteCb(uv_write_t* req, int status) {
  SCliConn* pConn = req->data;
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
390
    tTrace("client conn %p data already was written out", pConn);
dengyihao's avatar
dengyihao 已提交
391
    SCliMsg* pMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
392
    if (pMsg == NULL) {
dengyihao's avatar
dengyihao 已提交
393 394
      // handle
      return;
dengyihao's avatar
dengyihao 已提交
395
    }
dengyihao's avatar
dengyihao 已提交
396
    destroyUserdata(&pMsg->msg);
dengyihao's avatar
dengyihao 已提交
397
  } else {
dengyihao's avatar
dengyihao 已提交
398
    tError("client conn %p failed to write: %s", pConn, uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
399
    clientHandleExcept(pConn);
dengyihao's avatar
dengyihao 已提交
400 401
    return;
  }
dengyihao's avatar
dengyihao 已提交
402
  SCliThrdObj* pThrd = pConn->hostThrd;
dengyihao's avatar
dengyihao 已提交
403
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
404
}
dengyihao's avatar
dengyihao 已提交
405 406

static void clientWrite(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
407
  SCliMsg*       pCliMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
408
  STransConnCtx* pCtx = pCliMsg->ctx;
dengyihao's avatar
dengyihao 已提交
409
  SRpcInfo*      pTransInst = pCtx->pTransInst;
dengyihao's avatar
dengyihao 已提交
410 411 412

  SRpcMsg* pMsg = (SRpcMsg*)(&pCliMsg->msg);

dengyihao's avatar
dengyihao 已提交
413
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
dengyihao's avatar
dengyihao 已提交
414 415 416 417 418 419 420
  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 已提交
421 422
    memcpy(uMsg->user, pTransInst->user, tListLen(uMsg->user));
    memcpy(uMsg->secret, pTransInst->secret, tListLen(uMsg->secret));
dengyihao's avatar
dengyihao 已提交
423

dengyihao's avatar
dengyihao 已提交
424 425 426 427 428 429 430
    // 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 已提交
431
    pHead->secured = 1;
dengyihao's avatar
dengyihao 已提交
432 433
    msgLen += sizeof(STransUserMsg);
  }
dengyihao's avatar
dengyihao 已提交
434

dengyihao's avatar
dengyihao 已提交
435 436 437
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);

dengyihao's avatar
dengyihao 已提交
438 439
  // if (pHead->msgType == TDMT_VND_QUERY || pHead->msgType == TDMT_VND_)

dengyihao's avatar
dengyihao 已提交
440
  uv_buf_t wb = uv_buf_init((char*)pHead, msgLen);
dengyihao's avatar
dengyihao 已提交
441 442 443
  tDebug("client conn %p %s is send to %s:%d, local info %s:%d", pConn, 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));
dengyihao's avatar
dengyihao 已提交
444 445
  uv_write(pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb);
}
dengyihao's avatar
dengyihao 已提交
446 447
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
448 449
  SCliConn* pConn = req->data;
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
450
    // tError("failed to connect server(%s, %d), errmsg: %s", pCtx->ip, pCtx->port, uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
451
    tError("client conn %p failed to connect server: %s", pConn, uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
452
    clientHandleExcept(pConn);
453
    return;
dengyihao's avatar
dengyihao 已提交
454
  }
dengyihao's avatar
dengyihao 已提交
455 456 457
  int addrlen = sizeof(pConn->addr);
  uv_tcp_getpeername((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->addr, &addrlen);

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

dengyihao's avatar
dengyihao 已提交
461
  tTrace("client conn %p create", pConn);
dengyihao's avatar
dengyihao 已提交
462

dengyihao's avatar
dengyihao 已提交
463
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
464
  clientWrite(pConn);
dengyihao's avatar
dengyihao 已提交
465 466
}

dengyihao's avatar
dengyihao 已提交
467
static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
468
  tDebug("client work thread %p start to quit", pThrd);
dengyihao's avatar
dengyihao 已提交
469
  destroyCmsg(pMsg);
dengyihao's avatar
dengyihao 已提交
470
  // transDestroyAsyncPool(pThr) uv_close((uv_handle_t*)pThrd->cliAsync, NULL);
dengyihao's avatar
dengyihao 已提交
471 472 473 474 475
  uv_timer_stop(pThrd->timer);
  pThrd->quit = true;
  // uv__async_stop(pThrd->cliAsync);
  uv_stop(pThrd->loop);
}
dengyihao's avatar
dengyihao 已提交
476
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
477 478
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
dengyihao's avatar
dengyihao 已提交
479
  tTrace("client msg tran time cost: %" PRIu64 "us", el);
dengyihao's avatar
dengyihao 已提交
480
  et = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
481

dengyihao's avatar
dengyihao 已提交
482
  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
483 484 485 486 487

  SCliConn* conn = NULL;
  if (pMsg->msg.handle == NULL) {
    conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port);
    if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
488
      tTrace("client conn %p get from conn pool", conn);
dengyihao's avatar
dengyihao 已提交
489 490 491 492
    }
  } else {
    conn = (SCliConn*)(pMsg->msg.handle);
    if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
493
      tTrace("client conn %p reused", conn);
dengyihao's avatar
dengyihao 已提交
494 495 496
    }
  }

dengyihao's avatar
dengyihao 已提交
497
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
498 499
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
500
    transDestroyBuffer(&conn->readBuf);
dengyihao's avatar
dengyihao 已提交
501 502 503 504 505

    if (pThrd->quit) {
      clientHandleExcept(conn);
      return;
    }
dengyihao's avatar
dengyihao 已提交
506
    clientWrite(conn);
dengyihao's avatar
dengyihao 已提交
507

dengyihao's avatar
dengyihao 已提交
508
  } else {
dengyihao's avatar
dengyihao 已提交
509
    conn = calloc(1, sizeof(SCliConn));
dengyihao's avatar
dengyihao 已提交
510
    conn->ref++;
dengyihao's avatar
dengyihao 已提交
511
    // read/write stream handle
dengyihao's avatar
dengyihao 已提交
512 513
    conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
dengyihao's avatar
dengyihao 已提交
514 515 516
    conn->stream->data = conn;

    // write req handle
dengyihao's avatar
dengyihao 已提交
517
    conn->writeReq = malloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
518
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
519

dengyihao's avatar
dengyihao 已提交
520
    QUEUE_INIT(&conn->conn);
dengyihao's avatar
dengyihao 已提交
521 522 523

    conn->connReq.data = conn;
    conn->data = pMsg;
dengyihao's avatar
dengyihao 已提交
524
    conn->hostThrd = pThrd;
dengyihao's avatar
dengyihao 已提交
525

dengyihao's avatar
dengyihao 已提交
526 527
    // conn->push = pMsg->msg.push;
    // conn->ctnRdCnt = 0;
dengyihao's avatar
dengyihao 已提交
528

dengyihao's avatar
dengyihao 已提交
529
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
530
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
531
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
532
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
533
  }
dengyihao's avatar
dengyihao 已提交
534 535 536 537

  conn->push = pMsg->msg.push;
  conn->ctnRdCnt = 0;
  conn->hThrdIdx = pCtx->hThrdIdx;
dengyihao's avatar
dengyihao 已提交
538 539
}
static void clientAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
540 541
  SAsyncItem*  item = handle->data;
  SCliThrdObj* pThrd = item->pThrd;
dengyihao's avatar
dengyihao 已提交
542 543
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
544

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

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

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

static void* clientThread(void* arg) {
  SCliThrdObj* pThrd = (SCliThrdObj*)arg;
dengyihao's avatar
dengyihao 已提交
571
  setThreadName("trans-client-work");
dengyihao's avatar
dengyihao 已提交
572 573 574 575 576
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
}

void* taosInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
  SClientObj* cli = calloc(1, sizeof(SClientObj));
dengyihao's avatar
dengyihao 已提交
577

dengyihao's avatar
dengyihao 已提交
578
  SRpcInfo* 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

dengyihao's avatar
dengyihao 已提交
588
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
589
    if (err == 0) {
dengyihao's avatar
dengyihao 已提交
590
      tDebug("success to create tranport-client 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 597 598 599 600 601 602 603 604 605 606 607 608 609

static void destroyUserdata(SRpcMsg* userdata) {
  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 615 616 617 618 619 620
static SCliThrdObj* createThrdObj() {
  SCliThrdObj* pThrd = (SCliThrdObj*)calloc(1, sizeof(SCliThrdObj));
  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);

dengyihao's avatar
dengyihao 已提交
621
  pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, clientAsyncCb);
dengyihao's avatar
dengyihao 已提交
622

dengyihao's avatar
dengyihao 已提交
623 624 625
  pThrd->timer = malloc(sizeof(uv_timer_t));
  uv_timer_init(pThrd->loop, pThrd->timer);
  pThrd->timer->data = pThrd;
dengyihao's avatar
dengyihao 已提交
626

dengyihao's avatar
dengyihao 已提交
627
  pThrd->pool = creatConnPool(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 640
  transDestroyAsyncPool(pThrd->asyncPool);
  // free(pThrd->cliAsync);
dengyihao's avatar
dengyihao 已提交
641
  free(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
//
dengyihao's avatar
dengyihao 已提交
653 654 655 656 657
static void clientSendQuit(SCliThrdObj* thrd) {
  // cli can stop gracefully
  SCliMsg* msg = calloc(1, sizeof(SCliMsg));
  msg->ctx = NULL;  //

dengyihao's avatar
dengyihao 已提交
658
  transSendAsync(thrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
659
}
dengyihao's avatar
dengyihao 已提交
660 661
void taosCloseClient(void* arg) {
  SClientObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
662
  for (int i = 0; i < cli->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
663
    clientSendQuit(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
664
    destroyThrdObj(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
665 666 667
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
668
}
dengyihao's avatar
dengyihao 已提交
669 670 671 672 673 674 675
static int clientRBChoseIdx(SRpcInfo* pRpc) {
  int64_t index = pRpc->index;
  if (pRpc->index++ >= pRpc->numOfThreads) {
    pRpc->index = 0;
  }
  return index % pRpc->numOfThreads;
}
dengyihao's avatar
dengyihao 已提交
676 677
void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) {
  // impl later
dengyihao's avatar
dengyihao 已提交
678 679
  char*    ip = (char*)(pEpSet->eps[pEpSet->inUse].fqdn);
  uint32_t port = pEpSet->eps[pEpSet->inUse].port;
dengyihao's avatar
dengyihao 已提交
680

dengyihao's avatar
dengyihao 已提交
681 682
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
683
  int index = CONN_HOST_THREAD_INDEX(pMsg->handle);
dengyihao's avatar
dengyihao 已提交
684 685 686
  if (index == -1) {
    index = clientRBChoseIdx(pRpc);
  }
dengyihao's avatar
dengyihao 已提交
687 688 689 690
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
dengyihao's avatar
dengyihao 已提交
691
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));
dengyihao's avatar
dengyihao 已提交
692
  pCtx->pTransInst = (SRpcInfo*)shandle;
dengyihao's avatar
dengyihao 已提交
693 694 695 696
  pCtx->ahandle = pMsg->ahandle;
  pCtx->msgType = pMsg->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
697
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
698 699 700

  assert(pRpc->connType == TAOS_CONN_CLIENT);
  // atomic or not
dengyihao's avatar
dengyihao 已提交
701

dengyihao's avatar
dengyihao 已提交
702 703 704 705
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
706

dengyihao's avatar
dengyihao 已提交
707
  SCliThrdObj* thrd = ((SClientObj*)pRpc->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
708
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
dengyihao's avatar
dengyihao 已提交
709
}
dengyihao's avatar
dengyihao 已提交
710

dengyihao's avatar
dengyihao 已提交
711 712 713 714 715 716
void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pReq, SRpcMsg* pRsp) {
  char*    ip = (char*)(pEpSet->eps[pEpSet->inUse].fqdn);
  uint32_t port = pEpSet->eps[pEpSet->inUse].port;

  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
717
  int index = CONN_HOST_THREAD_INDEX(pReq->handle);
dengyihao's avatar
dengyihao 已提交
718 719 720 721
  if (index == -1) {
    index = clientRBChoseIdx(pRpc);
  }

dengyihao's avatar
dengyihao 已提交
722 723 724 725 726 727
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));
  pCtx->pTransInst = (SRpcInfo*)shandle;
  pCtx->ahandle = pReq->ahandle;
  pCtx->msgType = pReq->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
728
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
729 730 731 732 733 734 735 736 737
  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();

dengyihao's avatar
dengyihao 已提交
738
  SCliThrdObj* thrd = ((SClientObj*)pRpc->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
739 740 741 742 743 744 745 746
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
  tsem_t* pSem = pCtx->pSem;
  tsem_wait(pSem);
  tsem_destroy(pSem);
  free(pSem);

  return;
}
dengyihao's avatar
dengyihao 已提交
747
#endif