transCli.c 22.4 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 45 46 47
  STransConnCtx* ctx;
  SRpcMsg        msg;
  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 66 67 68 69 70 71 72
} SCliThrdObj;

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

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 85
// register timer in each thread to clear expire conn
static void clientTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
86
// alloc buf for read
dengyihao's avatar
dengyihao 已提交
87
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
88
// callback after read nbytes from socket
dengyihao's avatar
dengyihao 已提交
89
static void clientRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
90
// callback after write data to socket
dengyihao's avatar
dengyihao 已提交
91
static void clientSendDataCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
92
// callback after conn  to server
dengyihao's avatar
dengyihao 已提交
93
static void clientConnCb(uv_connect_t* req, int status);
dengyihao's avatar
dengyihao 已提交
94
static void clientAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
95 96 97 98

static SCliConn* clientConnCreate(SCliThrdObj* thrd);
static void      clientConnDestroy(SCliConn* pConn, bool clear /*clear tcp handle or not*/);
static void      clientDestroy(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
99

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

dengyihao's avatar
dengyihao 已提交
110 111
static int clientRBChoseIdx(SRpcInfo* pTransInst);

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 121 122 123 124 125

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

#define CONN_GET_INST_LABEL(conn) (((SRpcInfo*)(((SCliThrdObj*)conn->hostThrd)->pTransInst))->label)
#define CONN_HANDLE_THREAD_QUIT(conn, thrd) \
  do {                                      \
    if (thrd->quit) {                       \
      clientHandleExcept(conn);             \
dengyihao's avatar
dengyihao 已提交
126
      goto _RETURE;                         \
dengyihao's avatar
dengyihao 已提交
127 128 129 130 131 132 133
    }                                       \
  } while (0)

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

dengyihao's avatar
dengyihao 已提交
138 139
static void* clientThread(void* arg);

dengyihao's avatar
dengyihao 已提交
140 141
static void* clientNotifyApp() {}
static void  clientHandleResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
142 143
  SCliMsg*       pMsg = conn->data;
  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
144 145 146

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

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

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

wafwerar's avatar
wafwerar 已提交
155
  SRpcMsg rpcMsg = {0};
dengyihao's avatar
dengyihao 已提交
156
  rpcMsg.contLen = transContLenFromMsg(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
157
  rpcMsg.pCont = transContFromHead((char*)pHead);
dengyihao's avatar
dengyihao 已提交
158 159
  rpcMsg.code = pHead->code;
  rpcMsg.msgType = pHead->msgType;
dengyihao's avatar
dengyihao 已提交
160
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
161

dengyihao's avatar
dengyihao 已提交
162
  if (pTransInst->pfp != NULL && (pTransInst->pfp)(pTransInst->parent, rpcMsg.msgType)) {
dengyihao's avatar
dengyihao 已提交
163
    rpcMsg.handle = conn;
dengyihao's avatar
dengyihao 已提交
164 165
    transRefCliHandle(conn);

dengyihao's avatar
dengyihao 已提交
166
    conn->persist = 1;
dengyihao's avatar
dengyihao 已提交
167
    tDebug("client conn %p persist by app", conn);
dengyihao's avatar
dengyihao 已提交
168 169
  }

dengyihao's avatar
dengyihao 已提交
170
  tDebug("%s client conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pTransInst->label, conn,
dengyihao's avatar
fix bug  
dengyihao 已提交
171 172
         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 已提交
173

dengyihao's avatar
dengyihao 已提交
174
  conn->secured = pHead->secured;
dengyihao's avatar
dengyihao 已提交
175 176

  if (pCtx->pSem == NULL) {
dengyihao's avatar
test  
dengyihao 已提交
177 178 179 180 181 182
    tTrace("%s client conn %p handle resp", pTransInst->label, conn);
    (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL);
  } else {
    tTrace("%s client conn(sync) %p handle resp", pTransInst->label, conn);
    memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg));
    tsem_post(pCtx->pSem);
dengyihao's avatar
dengyihao 已提交
183
  }
dengyihao's avatar
dengyihao 已提交
184

dengyihao's avatar
dengyihao 已提交
185
  uv_read_start((uv_stream_t*)conn->stream, clientAllocBufferCb, clientRecvCb);
dengyihao's avatar
dengyihao 已提交
186

dengyihao's avatar
dengyihao 已提交
187
  // user owns conn->persist = 1
dengyihao's avatar
dengyihao 已提交
188
  if (conn->persist == 0) {
dengyihao's avatar
dengyihao 已提交
189
    addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn);
dengyihao's avatar
dengyihao 已提交
190
  }
dengyihao's avatar
test  
dengyihao 已提交
191 192 193
  destroyCmsg(conn->data);
  conn->data = NULL;

dengyihao's avatar
dengyihao 已提交
194
  // start thread's timer of conn pool if not active
dengyihao's avatar
dengyihao 已提交
195 196
  if (!uv_is_active((uv_handle_t*)&pThrd->timer) && pTransInst->idleTime > 0) {
    // uv_timer_start((uv_timer_t*)&pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
197
  }
dengyihao's avatar
dengyihao 已提交
198 199
}
static void clientHandleExcept(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
200
  if (pConn->data == NULL) {
dengyihao's avatar
dengyihao 已提交
201
    // handle conn except in conn pool
dengyihao's avatar
dengyihao 已提交
202
    transUnrefCliHandle(pConn);
dengyihao's avatar
dengyihao 已提交
203 204
    return;
  }
dengyihao's avatar
dengyihao 已提交
205 206 207
  SCliThrdObj* pThrd = pConn->hostThrd;
  SRpcInfo*    pTransInst = pThrd->pTransInst;

dengyihao's avatar
dengyihao 已提交
208
  SCliMsg*       pMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
209 210
  STransConnCtx* pCtx = pMsg->ctx;

dengyihao's avatar
dengyihao 已提交
211
  SRpcMsg rpcMsg = {0};
dengyihao's avatar
dengyihao 已提交
212
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
213
  rpcMsg.code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
dengyihao's avatar
dengyihao 已提交
214
  rpcMsg.msgType = pMsg->msg.msgType + 1;
dengyihao's avatar
dengyihao 已提交
215

dengyihao's avatar
dengyihao 已提交
216
  if (pCtx->pSem == NULL) {
dengyihao's avatar
test  
dengyihao 已提交
217
    tTrace("%s client conn %p handle resp", pTransInst->label, pConn);
dengyihao's avatar
dengyihao 已提交
218
    (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
219
  } else {
dengyihao's avatar
test  
dengyihao 已提交
220
    tTrace("%s client conn(sync) %p handle resp", pTransInst->label, pConn);
dengyihao's avatar
dengyihao 已提交
221 222
    memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg));
    tsem_post(pCtx->pSem);
dengyihao's avatar
dengyihao 已提交
223
  }
dengyihao's avatar
test  
dengyihao 已提交
224 225 226
  destroyCmsg(pConn->data);
  pConn->data = NULL;

dengyihao's avatar
dengyihao 已提交
227
  tTrace("%s client conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
228
  transUnrefCliHandle(pConn);
dengyihao's avatar
dengyihao 已提交
229
}
dengyihao's avatar
dengyihao 已提交
230

dengyihao's avatar
dengyihao 已提交
231 232
static void clientTimeoutCb(uv_timer_t* handle) {
  SCliThrdObj* pThrd = handle->data;
dengyihao's avatar
dengyihao 已提交
233
  SRpcInfo*    pRpc = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
234
  int64_t      currentTime = pThrd->nextTimeout;
dengyihao's avatar
fix bug  
dengyihao 已提交
235
  tTrace("%s, client conn timeout, try to remove expire conn from conn pool", pRpc->label);
dengyihao's avatar
dengyihao 已提交
236

dengyihao's avatar
dengyihao 已提交
237
  SConnList* p = taosHashIterate((SHashObj*)pThrd->pool, NULL);
dengyihao's avatar
dengyihao 已提交
238 239 240 241 242 243
  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 已提交
244
        transUnrefCliHandle(c);
dengyihao's avatar
dengyihao 已提交
245 246 247 248
      } else {
        break;
      }
    }
dengyihao's avatar
dengyihao 已提交
249
    p = taosHashIterate((SHashObj*)pThrd->pool, p);
dengyihao's avatar
dengyihao 已提交
250 251
  }

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

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

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

  if (QUEUE_IS_EMPTY(&plist->conn)) {
    return NULL;
  }
  queue* h = QUEUE_HEAD(&plist->conn);
  QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
292 293 294 295

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

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

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

dengyihao's avatar
dengyihao 已提交
306
  conn->expireTime = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
307
  SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
308 309 310 311
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
}
dengyihao's avatar
dengyihao 已提交
312
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
313 314
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
315
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
316
}
dengyihao's avatar
dengyihao 已提交
317
static void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
318
  // impl later
dengyihao's avatar
dengyihao 已提交
319 320 321
  if (handle->data == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
322 323
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
324
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
325
    pBuf->len += nread;
dengyihao's avatar
dengyihao 已提交
326
    if (transReadComplete(pBuf)) {
dengyihao's avatar
dengyihao 已提交
327
      tTrace("%s client conn %p read complete", CONN_GET_INST_LABEL(conn), conn);
328
      clientHandleResp(conn);
dengyihao's avatar
dengyihao 已提交
329
    } else {
dengyihao's avatar
dengyihao 已提交
330
      tTrace("%s client conn %p read partial packet, continue to read", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
331
    }
dengyihao's avatar
dengyihao 已提交
332 333
    return;
  }
dengyihao's avatar
dengyihao 已提交
334

335 336
  assert(nread <= 0);
  if (nread == 0) {
dengyihao's avatar
dengyihao 已提交
337 338 339
    // 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).
340 341
    return;
  }
dengyihao's avatar
fix bug  
dengyihao 已提交
342
  if (nread < 0) {
dengyihao's avatar
dengyihao 已提交
343
    tError("%s client conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
344
    conn->broken = true;
dengyihao's avatar
dengyihao 已提交
345
    clientHandleExcept(conn);
dengyihao's avatar
dengyihao 已提交
346
  }
dengyihao's avatar
dengyihao 已提交
347
}
dengyihao's avatar
dengyihao 已提交
348

dengyihao's avatar
dengyihao 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
static SCliConn* clientConnCreate(SCliThrdObj* pThrd) {
  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;
  conn->broken = false;
  transRefCliHandle(conn);
  return conn;
}
365
static void clientConnDestroy(SCliConn* conn, bool clear) {
dengyihao's avatar
dengyihao 已提交
366 367 368 369
  tTrace("%s client conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn);
  QUEUE_REMOVE(&conn->conn);
  if (clear) {
    uv_close((uv_handle_t*)conn->stream, clientDestroy);
370
  }
dengyihao's avatar
dengyihao 已提交
371
}
dengyihao's avatar
dengyihao 已提交
372 373
static void clientDestroy(uv_handle_t* handle) {
  SCliConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
374 375

  free(conn->stream);
dengyihao's avatar
dengyihao 已提交
376
  tTrace("%s client conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
377
  free(conn);
dengyihao's avatar
dengyihao 已提交
378 379
}

dengyihao's avatar
dengyihao 已提交
380
static void clientSendDataCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
381
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
382

dengyihao's avatar
dengyihao 已提交
383
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
384
    tTrace("%s client conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
385
    SCliMsg* pMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
386
    if (pMsg == NULL) {
dengyihao's avatar
dengyihao 已提交
387
      return;
dengyihao's avatar
dengyihao 已提交
388
    }
dengyihao's avatar
dengyihao 已提交
389
    destroyUserdata(&pMsg->msg);
dengyihao's avatar
dengyihao 已提交
390
  } else {
dengyihao's avatar
dengyihao 已提交
391
    tError("%s client conn %p failed to write: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
392
    clientHandleExcept(pConn);
dengyihao's avatar
dengyihao 已提交
393 394
    return;
  }
dengyihao's avatar
dengyihao 已提交
395
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocBufferCb, clientRecvCb);
dengyihao's avatar
dengyihao 已提交
396
}
dengyihao's avatar
dengyihao 已提交
397

dengyihao's avatar
dengyihao 已提交
398 399 400
static void clientSendData(SCliConn* pConn) {
  CONN_HANDLE_BROKEN(pConn);

dengyihao's avatar
dengyihao 已提交
401
  SCliMsg*       pCliMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
402
  STransConnCtx* pCtx = pCliMsg->ctx;
dengyihao's avatar
dengyihao 已提交
403 404 405

  SCliThrdObj* pThrd = pConn->hostThrd;
  SRpcInfo*    pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
406 407 408

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

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

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

dengyihao's avatar
dengyihao 已提交
431 432 433 434
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);

  uv_buf_t wb = uv_buf_init((char*)pHead, msgLen);
dengyihao's avatar
dengyihao 已提交
435 436 437 438
  tDebug("%s client conn %p %s is send to %s:%d, local info %s:%d", CONN_GET_INST_LABEL(pConn), 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 已提交
439 440 441 442 443
  uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientSendDataCb);

  return;
_RETURE:
  return;
dengyihao's avatar
dengyihao 已提交
444
}
dengyihao's avatar
dengyihao 已提交
445 446
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
447 448
  SCliConn* pConn = req->data;
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
449
    tError("%s client conn %p failed to connect server: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
450
    clientHandleExcept(pConn);
451
    return;
dengyihao's avatar
dengyihao 已提交
452
  }
dengyihao's avatar
dengyihao 已提交
453 454 455
  int addrlen = sizeof(pConn->addr);
  uv_tcp_getpeername((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->addr, &addrlen);

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

dengyihao's avatar
dengyihao 已提交
459
  tTrace("%s client conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
460

dengyihao's avatar
dengyihao 已提交
461
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
462
  clientSendData(pConn);
dengyihao's avatar
dengyihao 已提交
463 464
}

dengyihao's avatar
dengyihao 已提交
465
static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
466
  tDebug("client work thread %p start to quit", pThrd);
dengyihao's avatar
dengyihao 已提交
467
  destroyCmsg(pMsg);
dengyihao's avatar
fix bug  
dengyihao 已提交
468
  destroyConnPool(pThrd->pool);
dengyihao's avatar
dengyihao 已提交
469 470 471

  uv_timer_stop(&pThrd->timer);

dengyihao's avatar
dengyihao 已提交
472 473 474
  pThrd->quit = true;
  uv_stop(pThrd->loop);
}
dengyihao's avatar
dengyihao 已提交
475 476
static SCliConn* clientGetConn(SCliMsg* pMsg, SCliThrdObj* pThrd) {
  SCliConn* conn = NULL;
dengyihao's avatar
dengyihao 已提交
477 478
  if (pMsg->msg.handle != NULL) {
    conn = (SCliConn*)(pMsg->msg.handle);
dengyihao's avatar
dengyihao 已提交
479
    transUnrefCliHandle(conn);
dengyihao's avatar
dengyihao 已提交
480
    if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
481
      tTrace("%s client conn %p reused", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
482 483
    }
  } else {
dengyihao's avatar
dengyihao 已提交
484
    STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
485 486
    conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port);
    if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
487
  }
dengyihao's avatar
dengyihao 已提交
488 489 490 491 492 493 494 495 496
  return conn;
}
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
  tTrace("%s client msg tran time cost: %" PRIu64 "us", ((SRpcInfo*)pThrd->pTransInst)->label, el);

  STransConnCtx* pCtx = pMsg->ctx;
  SRpcInfo*      pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
497

dengyihao's avatar
dengyihao 已提交
498
  SCliConn* conn = clientGetConn(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
499
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
500
    conn->data = pMsg;
dengyihao's avatar
dengyihao 已提交
501
    transDestroyBuffer(&conn->readBuf);
dengyihao's avatar
dengyihao 已提交
502
    clientSendData(conn);
dengyihao's avatar
dengyihao 已提交
503
  } else {
dengyihao's avatar
dengyihao 已提交
504
    conn = clientConnCreate(pThrd);
dengyihao's avatar
dengyihao 已提交
505
    conn->data = pMsg;
dengyihao's avatar
dengyihao 已提交
506

dengyihao's avatar
dengyihao 已提交
507 508 509 510
    int ret = transSetConnOption((uv_tcp_t*)conn->stream);
    if (ret) {
      tError("%s client conn %p failed to set conn option, errmsg %s", pTransInst->label, conn, uv_err_name(ret));
    }
dengyihao's avatar
dengyihao 已提交
511
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
512
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
513
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
514
    tTrace("%s client conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port);
dengyihao's avatar
dengyihao 已提交
515
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
516
  }
dengyihao's avatar
dengyihao 已提交
517
  conn->hThrdIdx = pCtx->hThrdIdx;
dengyihao's avatar
dengyihao 已提交
518 519
}
static void clientAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
520 521
  SAsyncItem*  item = handle->data;
  SCliThrdObj* pThrd = item->pThrd;
dengyihao's avatar
dengyihao 已提交
522 523
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
524

dengyihao's avatar
dengyihao 已提交
525
  // batch process to avoid to lock/unlock frequently
dengyihao's avatar
dengyihao 已提交
526 527 528
  pthread_mutex_lock(&item->mtx);
  QUEUE_MOVE(&item->qmsg, &wq);
  pthread_mutex_unlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
529

dengyihao's avatar
dengyihao 已提交
530 531 532 533
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
534 535

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
536 537 538 539 540
    if (pMsg->ctx == NULL) {
      clientHandleQuit(pMsg, pThrd);
    } else {
      clientHandleReq(pMsg, pThrd);
    }
dengyihao's avatar
dengyihao 已提交
541
    count++;
dengyihao's avatar
dengyihao 已提交
542 543
  }
  if (count >= 2) {
dengyihao's avatar
dengyihao 已提交
544
    tTrace("client process batch size: %d", count);
dengyihao's avatar
dengyihao 已提交
545
  }
dengyihao's avatar
dengyihao 已提交
546 547 548 549
}

static void* clientThread(void* arg) {
  SCliThrdObj* pThrd = (SCliThrdObj*)arg;
dengyihao's avatar
dengyihao 已提交
550
  setThreadName("trans-client-work");
dengyihao's avatar
dengyihao 已提交
551 552 553 554 555
  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 已提交
556

dengyihao's avatar
dengyihao 已提交
557
  SRpcInfo* pRpc = shandle;
dengyihao's avatar
dengyihao 已提交
558 559 560 561 562
  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 已提交
563
    SCliThrdObj* pThrd = createThrdObj();
dengyihao's avatar
dengyihao 已提交
564
    pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
565
    pThrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
566

dengyihao's avatar
dengyihao 已提交
567
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
568
    if (err == 0) {
dengyihao's avatar
dengyihao 已提交
569
      tDebug("success to create tranport-client thread %d", i);
dengyihao's avatar
dengyihao 已提交
570
    }
dengyihao's avatar
dengyihao 已提交
571
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
572 573 574
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588

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 已提交
589 590
  free(pMsg);
}
dengyihao's avatar
dengyihao 已提交
591

dengyihao's avatar
dengyihao 已提交
592 593 594 595 596 597 598 599
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 已提交
600
  pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, clientAsyncCb);
dengyihao's avatar
dengyihao 已提交
601

dengyihao's avatar
dengyihao 已提交
602 603
  uv_timer_init(pThrd->loop, &pThrd->timer);
  pThrd->timer.data = pThrd;
dengyihao's avatar
dengyihao 已提交
604

dengyihao's avatar
dengyihao 已提交
605
  pThrd->pool = createConnPool(4);
dengyihao's avatar
dengyihao 已提交
606 607

  pThrd->quit = false;
dengyihao's avatar
dengyihao 已提交
608 609
  return pThrd;
}
dengyihao's avatar
dengyihao 已提交
610
static void destroyThrdObj(SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
611 612 613
  if (pThrd == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
614
  uv_stop(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
615 616
  pthread_join(pThrd->thread, NULL);
  pthread_mutex_destroy(&pThrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
617
  transDestroyAsyncPool(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
618 619

  uv_timer_stop(&pThrd->timer);
dengyihao's avatar
dengyihao 已提交
620 621 622
  free(pThrd->loop);
  free(pThrd);
}
dengyihao's avatar
dengyihao 已提交
623

dengyihao's avatar
dengyihao 已提交
624
static void transDestroyConnCtx(STransConnCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
625 626 627 628 629
  if (ctx != NULL) {
    free(ctx->ip);
  }
  free(ctx);
}
dengyihao's avatar
dengyihao 已提交
630
//
dengyihao's avatar
dengyihao 已提交
631 632 633
static void clientSendQuit(SCliThrdObj* thrd) {
  // cli can stop gracefully
  SCliMsg* msg = calloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
634
  transSendAsync(thrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
635
}
dengyihao's avatar
dengyihao 已提交
636 637
void taosCloseClient(void* arg) {
  SClientObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
638
  for (int i = 0; i < cli->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
639
    clientSendQuit(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
640
    destroyThrdObj(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
641 642 643
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
644
}
dengyihao's avatar
dengyihao 已提交
645 646 647 648
static int clientRBChoseIdx(SRpcInfo* pTransInst) {
  int64_t index = pTransInst->index;
  if (pTransInst->index++ >= pTransInst->numOfThreads) {
    pTransInst->index = 0;
dengyihao's avatar
dengyihao 已提交
649
  }
dengyihao's avatar
dengyihao 已提交
650
  return index % pTransInst->numOfThreads;
dengyihao's avatar
dengyihao 已提交
651
}
dengyihao's avatar
dengyihao 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664
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) {
dengyihao's avatar
dengyihao 已提交
665
    clientConnDestroy((SCliConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
666 667 668 669
  }

  // unref cli handle
}
dengyihao's avatar
dengyihao 已提交
670 671
void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) {
  // impl later
dengyihao's avatar
dengyihao 已提交
672 673
  char*    ip = (char*)(pEpSet->eps[pEpSet->inUse].fqdn);
  uint32_t port = pEpSet->eps[pEpSet->inUse].port;
dengyihao's avatar
dengyihao 已提交
674

dengyihao's avatar
dengyihao 已提交
675
  SRpcInfo* pTransInst = (SRpcInfo*)shandle;
dengyihao's avatar
dengyihao 已提交
676

dengyihao's avatar
dengyihao 已提交
677
  int index = CONN_HOST_THREAD_INDEX(pMsg->handle);
dengyihao's avatar
dengyihao 已提交
678
  if (index == -1) {
dengyihao's avatar
dengyihao 已提交
679
    index = clientRBChoseIdx(pTransInst);
dengyihao's avatar
dengyihao 已提交
680
  }
dengyihao's avatar
dengyihao 已提交
681 682 683 684
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
dengyihao's avatar
dengyihao 已提交
685 686 687 688 689
  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 已提交
690
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
691

dengyihao's avatar
dengyihao 已提交
692
  assert(pTransInst->connType == TAOS_CONN_CLIENT);
dengyihao's avatar
dengyihao 已提交
693
  // atomic or not
dengyihao's avatar
dengyihao 已提交
694

dengyihao's avatar
dengyihao 已提交
695 696 697 698
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
699

dengyihao's avatar
dengyihao 已提交
700
  SCliThrdObj* thrd = ((SClientObj*)pTransInst->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
701
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
dengyihao's avatar
dengyihao 已提交
702
}
dengyihao's avatar
dengyihao 已提交
703

dengyihao's avatar
dengyihao 已提交
704 705 706 707
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;

dengyihao's avatar
dengyihao 已提交
708
  SRpcInfo* pTransInst = (SRpcInfo*)shandle;
dengyihao's avatar
dengyihao 已提交
709

dengyihao's avatar
dengyihao 已提交
710
  int index = CONN_HOST_THREAD_INDEX(pReq->handle);
dengyihao's avatar
dengyihao 已提交
711
  if (index == -1) {
dengyihao's avatar
dengyihao 已提交
712
    index = clientRBChoseIdx(pTransInst);
dengyihao's avatar
dengyihao 已提交
713 714
  }

dengyihao's avatar
dengyihao 已提交
715 716 717 718 719
  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 已提交
720
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
721 722 723 724 725 726 727 728 729
  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 已提交
730
  SCliThrdObj* thrd = ((SClientObj*)pTransInst->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
731 732 733 734 735 736 737 738
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
  tsem_t* pSem = pCtx->pSem;
  tsem_wait(pSem);
  tsem_destroy(pSem);
  free(pSem);

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