transCli.c 14.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 20 21 22
/*
 * 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 {
  uv_connect_t connReq;
  uv_stream_t* stream;
dengyihao's avatar
dengyihao 已提交
23
  uv_write_t*  writeReq;
dengyihao's avatar
dengyihao 已提交
24
  void*        hostThrd;
dengyihao's avatar
dengyihao 已提交
25
  SConnBuffer  readBuf;
dengyihao's avatar
dengyihao 已提交
26 27
  void*        data;
  queue        conn;
dengyihao's avatar
dengyihao 已提交
28 29
  char         spi;
  char         secured;
dengyihao's avatar
dengyihao 已提交
30
  uint64_t     expireTime;
dengyihao's avatar
dengyihao 已提交
31
} SCliConn;
dengyihao's avatar
dengyihao 已提交
32

dengyihao's avatar
dengyihao 已提交
33
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
34 35 36 37
  STransConnCtx* ctx;
  SRpcMsg        msg;
  queue          q;
  uint64_t       st;
dengyihao's avatar
dengyihao 已提交
38 39 40 41 42 43
} SCliMsg;

typedef struct SCliThrdObj {
  pthread_t       thread;
  uv_loop_t*      loop;
  uv_async_t*     cliAsync;  //
dengyihao's avatar
dengyihao 已提交
44 45
  uv_timer_t*     pTimer;
  void*           cache;  // conn pool
dengyihao's avatar
dengyihao 已提交
46 47
  queue           msg;
  pthread_mutex_t msgMtx;
dengyihao's avatar
dengyihao 已提交
48
  uint64_t        nextTimeout;  // next timeout
dengyihao's avatar
dengyihao 已提交
49
  void*           pTransInst;   //
dengyihao's avatar
dengyihao 已提交
50

dengyihao's avatar
dengyihao 已提交
51 52 53 54 55 56 57 58 59
} SCliThrdObj;

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

dengyihao's avatar
dengyihao 已提交
60 61 62 63
typedef struct SConnList {
  queue conn;
} SConnList;

dengyihao's avatar
dengyihao 已提交
64
// conn pool
dengyihao's avatar
dengyihao 已提交
65 66 67
// add expire timeout and capacity limit
static void*     connCacheCreate(int size);
static void*     connCacheDestroy(void* cache);
dengyihao's avatar
dengyihao 已提交
68 69 70
static SCliConn* getConnFromCache(void* cache, char* ip, uint32_t port);
static void      addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn);

dengyihao's avatar
dengyihao 已提交
71 72
// register timer in each thread to clear expire conn
static void clientTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
73
// process data read from server, auth/decompress etc later
dengyihao's avatar
dengyihao 已提交
74 75 76 77
static void clientProcessData(SCliConn* conn);
// check whether already read complete packet from server
static bool clientReadComplete(SConnBuffer* pBuf);
// alloc buf for read
dengyihao's avatar
dengyihao 已提交
78
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
79
// callback after read nbytes from socket
dengyihao's avatar
dengyihao 已提交
80
static void clientReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
81
// callback after write data to socket
dengyihao's avatar
dengyihao 已提交
82
static void clientWriteCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
83
// callback after conn  to server
dengyihao's avatar
dengyihao 已提交
84
static void clientConnCb(uv_connect_t* req, int status);
dengyihao's avatar
dengyihao 已提交
85
static void clientAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
86 87
static void clientDestroy(uv_handle_t* handle);
static void clientConnDestroy(SCliConn* pConn);
dengyihao's avatar
dengyihao 已提交
88

dengyihao's avatar
dengyihao 已提交
89 90
static void clientMsgDestroy(SCliMsg* pMsg);

dengyihao's avatar
dengyihao 已提交
91 92
static void* clientThread(void* arg);

dengyihao's avatar
dengyihao 已提交
93
static void clientProcessData(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
94
  STransConnCtx* pCtx = ((SCliMsg*)conn->data)->ctx;
dengyihao's avatar
dengyihao 已提交
95
  SRpcInfo*      pRpc = pCtx->pRpc;
dengyihao's avatar
dengyihao 已提交
96 97 98 99 100 101
  SRpcMsg        rpcMsg;

  rpcMsg.pCont = conn->readBuf.buf;
  rpcMsg.contLen = conn->readBuf.len;
  rpcMsg.ahandle = pCtx->ahandle;
  (pRpc->cfp)(NULL, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
102 103 104 105 106

  SCliThrdObj* pThrd = conn->hostThrd;
  addConnToCache(pThrd->cache, pCtx->ip, pCtx->port, conn);
  free(pCtx->ip);
  free(pCtx);
dengyihao's avatar
dengyihao 已提交
107 108
  // impl
}
dengyihao's avatar
dengyihao 已提交
109 110
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);

dengyihao's avatar
dengyihao 已提交
111 112
static void clientTimeoutCb(uv_timer_t* handle) {
  SCliThrdObj* pThrd = handle->data;
dengyihao's avatar
dengyihao 已提交
113
  SRpcInfo*    pRpc = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  int64_t      currentTime = pThrd->nextTimeout;

  SConnList* p = taosHashIterate((SHashObj*)pThrd->cache, NULL);
  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);
        clientConnDestroy(c);
      } else {
        break;
      }
    }
    p = taosHashIterate((SHashObj*)pThrd->cache, p);
  }

  pThrd->nextTimeout = taosGetTimestampMs() + pRpc->idleTime * 1000 * 10;
  uv_timer_start(handle, clientTimeoutCb, pRpc->idleTime * 10, 0);
}
dengyihao's avatar
dengyihao 已提交
134 135
static void* connCacheCreate(int size) {
  SHashObj* cache = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
136
  return cache;
dengyihao's avatar
dengyihao 已提交
137 138 139
}
static void* connCacheDestroy(void* cache) {
  SConnList* connList = taosHashIterate((SHashObj*)cache, NULL);
dengyihao's avatar
dengyihao 已提交
140 141 142 143 144 145 146 147
  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);
      clientConnDestroy(c);
    }
    connList = taosHashIterate((SHashObj*)cache, connList);
dengyihao's avatar
dengyihao 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  }
  taosHashClear(cache);
}

static SCliConn* getConnFromCache(void* cache, char* ip, uint32_t port) {
  char key[128] = {0};
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));

  SHashObj*  pCache = cache;
  SConnList* plist = taosHashGet(pCache, key, strlen(key));
  if (plist == NULL) {
    SConnList list;
    plist = &list;
    taosHashPut(pCache, key, strlen(key), plist, sizeof(*plist));
dengyihao's avatar
dengyihao 已提交
163 164
    plist = taosHashGet(pCache, key, strlen(key));
    QUEUE_INIT(&plist->conn);
dengyihao's avatar
dengyihao 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178
  }

  if (QUEUE_IS_EMPTY(&plist->conn)) {
    return NULL;
  }
  queue* h = QUEUE_HEAD(&plist->conn);
  QUEUE_REMOVE(h);
  return QUEUE_DATA(h, SCliConn, conn);
}
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn) {
  char key[128] = {0};
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));

dengyihao's avatar
dengyihao 已提交
179
  SRpcInfo* pRpc = ((SCliThrdObj*)conn->hostThrd)->pTransInst;
dengyihao's avatar
dengyihao 已提交
180 181
  conn->expireTime = taosGetTimestampMs() + pRpc->idleTime * 1000 * 10;
  SConnList* plist = taosHashGet((SHashObj*)cache, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
182 183 184 185
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
}
dengyihao's avatar
dengyihao 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
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;
    } else {
      return true;
    }
  } else {
    return false;
  }
}
dengyihao's avatar
dengyihao 已提交
202
static void clientAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
203
  // impl later
dengyihao's avatar
dengyihao 已提交
204 205 206 207 208
  static const int CAPACITY = 512;

  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
  if (pBuf->cap == 0) {
dengyihao's avatar
dengyihao 已提交
209
    pBuf->buf = (char*)calloc(1, CAPACITY * sizeof(char));
dengyihao's avatar
dengyihao 已提交
210 211 212
    pBuf->len = 0;
    pBuf->cap = CAPACITY;
    pBuf->left = -1;
dengyihao's avatar
dengyihao 已提交
213

dengyihao's avatar
dengyihao 已提交
214 215 216 217 218 219 220 221 222
    buf->base = pBuf->buf;
    buf->len = CAPACITY;
  } else {
    if (pBuf->len >= pBuf->cap) {
      if (pBuf->left == -1) {
        pBuf->cap *= 2;
        pBuf->buf = realloc(pBuf->buf, pBuf->cap);
      } else if (pBuf->len + pBuf->left > pBuf->cap) {
        pBuf->cap = pBuf->len + pBuf->left;
dengyihao's avatar
dengyihao 已提交
223
        pBuf->buf = realloc(pBuf->buf, pBuf->cap);
dengyihao's avatar
dengyihao 已提交
224 225 226 227 228
      }
    }
    buf->base = pBuf->buf + pBuf->len;
    buf->len = pBuf->cap - pBuf->len;
  }
dengyihao's avatar
dengyihao 已提交
229 230
}
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
231
  // impl later
dengyihao's avatar
dengyihao 已提交
232 233
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
234
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
235 236
    pBuf->len += nread;
    if (clientReadComplete(pBuf)) {
dengyihao's avatar
dengyihao 已提交
237
      tDebug("alread read complete");
dengyihao's avatar
dengyihao 已提交
238 239 240 241
      clientProcessData(conn);
    } else {
      tDebug("read halp packet, continue to read");
    }
dengyihao's avatar
dengyihao 已提交
242 243
    return;
  }
dengyihao's avatar
dengyihao 已提交
244

dengyihao's avatar
dengyihao 已提交
245 246 247
  if (nread != UV_EOF) {
    tDebug("Read error %s\n", uv_err_name(nread));
  }
dengyihao's avatar
dengyihao 已提交
248 249
  //
  uv_close((uv_handle_t*)handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
250
}
dengyihao's avatar
dengyihao 已提交
251 252

static void clientConnDestroy(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
253
  // impl later
dengyihao's avatar
dengyihao 已提交
254
  //
dengyihao's avatar
dengyihao 已提交
255
}
dengyihao's avatar
dengyihao 已提交
256 257
static void clientDestroy(uv_handle_t* handle) {
  SCliConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
258
  QUEUE_REMOVE(&conn->conn);
dengyihao's avatar
dengyihao 已提交
259 260 261 262 263 264 265 266 267 268 269
  clientConnDestroy(conn);
}

static void clientWriteCb(uv_write_t* req, int status) {
  SCliConn* pConn = req->data;
  if (status == 0) {
    tDebug("data already was written on stream");
  } else {
    uv_close((uv_handle_t*)pConn->stream, clientDestroy);
    return;
  }
dengyihao's avatar
dengyihao 已提交
270 271 272 273 274 275
  SCliThrdObj* pThrd = pConn->hostThrd;
  if (pConn->stream == NULL) {
    pConn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)pConn->stream);
    pConn->stream->data = pConn;
  }
dengyihao's avatar
dengyihao 已提交
276
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocReadBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
277 278
  // impl later
}
dengyihao's avatar
dengyihao 已提交
279 280

static void clientWrite(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
281 282 283 284
  SCliMsg*       pCliMsg = pConn->data;
  SRpcMsg*       pMsg = (SRpcMsg*)(&pCliMsg->msg);
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);

dengyihao's avatar
dengyihao 已提交
285
  int msgLen = transMsgLenFromCont(pMsg->contLen);
dengyihao's avatar
dengyihao 已提交
286

dengyihao's avatar
dengyihao 已提交
287 288 289 290 291
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);

  uv_buf_t wb = uv_buf_init((char*)pHead, msgLen);
  tDebug("data write out, msgType : %d, len: %d", pHead->msgType, msgLen);
dengyihao's avatar
dengyihao 已提交
292 293
  uv_write(pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb);
}
dengyihao's avatar
dengyihao 已提交
294 295
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
296
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
297
  SCliMsg*  pMsg = pConn->data;
dengyihao's avatar
dengyihao 已提交
298

dengyihao's avatar
dengyihao 已提交
299 300
  STransConnCtx* pCtx = pMsg->ctx;
  SRpcInfo*      pRpc = pCtx->pRpc;
dengyihao's avatar
dengyihao 已提交
301 302

  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
303 304
    // tError("failed to connect server(%s, %d), errmsg: %s", pCtx->ip, pCtx->port, uv_strerror(status));
    tError("failed to connect server,  errmsg: %s", uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
305
    // call user fp later
dengyihao's avatar
dengyihao 已提交
306 307 308
    SRpcMsg rpcMsg;
    rpcMsg.ahandle = pCtx->ahandle;
    // SRpcInfo* pRpc = pMsg->ctx->pRpc;
dengyihao's avatar
dengyihao 已提交
309
    (pRpc->cfp)(NULL, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
310
    uv_close((uv_handle_t*)req->handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
311 312
    return;
  }
dengyihao's avatar
dengyihao 已提交
313

dengyihao's avatar
dengyihao 已提交
314
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
315
  clientWrite(pConn);
dengyihao's avatar
dengyihao 已提交
316 317
}

dengyihao's avatar
dengyihao 已提交
318
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
319 320
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
dengyihao's avatar
dengyihao 已提交
321
  tDebug("msg tran time cost: %" PRIu64 "", el);
dengyihao's avatar
dengyihao 已提交
322
  et = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
323

dengyihao's avatar
dengyihao 已提交
324 325
  STransConnCtx* pCtx = pMsg->ctx;
  SCliConn*      conn = getConnFromCache(pThrd->cache, pCtx->ip, pCtx->port);
dengyihao's avatar
dengyihao 已提交
326
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
327
    // impl later
dengyihao's avatar
dengyihao 已提交
328 329
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
330 331 332 333

    conn->readBuf.len = 0;
    memset(conn->readBuf.buf, 0, conn->readBuf.cap);
    conn->readBuf.left = -1;
dengyihao's avatar
dengyihao 已提交
334
    clientWrite(conn);
dengyihao's avatar
dengyihao 已提交
335
  } else {
dengyihao's avatar
dengyihao 已提交
336
    SCliConn* conn = calloc(1, sizeof(SCliConn));
dengyihao's avatar
dengyihao 已提交
337

dengyihao's avatar
dengyihao 已提交
338
    // read/write stream handle
dengyihao's avatar
dengyihao 已提交
339 340
    conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
dengyihao's avatar
dengyihao 已提交
341 342 343
    conn->stream->data = conn;

    // write req handle
dengyihao's avatar
dengyihao 已提交
344
    conn->writeReq = malloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
345
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
346
    QUEUE_INIT(&conn->conn);
dengyihao's avatar
dengyihao 已提交
347 348 349

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

dengyihao's avatar
dengyihao 已提交
352
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
353
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
354
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
355
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
356 357 358 359 360 361
  }
}
static void clientAsyncCb(uv_async_t* handle) {
  SCliThrdObj* pThrd = handle->data;
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
362

dengyihao's avatar
dengyihao 已提交
363 364 365 366
  // batch process to avoid to lock/unlock frequently
  pthread_mutex_lock(&pThrd->msgMtx);
  QUEUE_MOVE(&pThrd->msg, &wq);
  pthread_mutex_unlock(&pThrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
367

dengyihao's avatar
dengyihao 已提交
368 369 370 371
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
372 373

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
374 375 376 377 378 379
    clientHandleReq(pMsg, pThrd);
    count++;
    if (count >= 2) {
      tError("send batch size: %d", count);
    }
  }
dengyihao's avatar
dengyihao 已提交
380 381 382 383
}

static void* clientThread(void* arg) {
  SCliThrdObj* pThrd = (SCliThrdObj*)arg;
dengyihao's avatar
dengyihao 已提交
384

dengyihao's avatar
dengyihao 已提交
385 386 387 388 389
  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 已提交
390

dengyihao's avatar
dengyihao 已提交
391
  SRpcInfo* pRpc = shandle;
dengyihao's avatar
dengyihao 已提交
392 393 394 395 396
  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 已提交
397
    SCliThrdObj* pThrd = (SCliThrdObj*)calloc(1, sizeof(SCliThrdObj));
dengyihao's avatar
dengyihao 已提交
398

dengyihao's avatar
dengyihao 已提交
399 400
    QUEUE_INIT(&pThrd->msg);
    pthread_mutex_init(&pThrd->msgMtx, NULL);
dengyihao's avatar
dengyihao 已提交
401

dengyihao's avatar
dengyihao 已提交
402 403 404 405 406 407
    pThrd->loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
    uv_loop_init(pThrd->loop);

    pThrd->cliAsync = malloc(sizeof(uv_async_t));
    uv_async_init(pThrd->loop, pThrd->cliAsync, clientAsyncCb);
    pThrd->cliAsync->data = pThrd;
dengyihao's avatar
dengyihao 已提交
408

dengyihao's avatar
dengyihao 已提交
409 410
    pThrd->pTimer = malloc(sizeof(uv_timer_t));
    uv_timer_init(pThrd->loop, pThrd->pTimer);
dengyihao's avatar
dengyihao 已提交
411 412
    pThrd->pTimer->data = pThrd;
    pThrd->nextTimeout = taosGetTimestampMs() + pRpc->idleTime * 1000 * 10;
dengyihao's avatar
dengyihao 已提交
413

dengyihao's avatar
dengyihao 已提交
414 415
    pThrd->cache = connCacheCreate(1);
    pThrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
416

dengyihao's avatar
dengyihao 已提交
417
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
418 419 420
    if (err == 0) {
      tDebug("sucess to create tranport-client thread %d", i);
    }
dengyihao's avatar
dengyihao 已提交
421
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
422 423 424
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
425 426 427 428
static void clientMsgDestroy(SCliMsg* pMsg) {
  // impl later
  free(pMsg);
}
dengyihao's avatar
dengyihao 已提交
429 430 431
void taosCloseClient(void* arg) {
  // impl later
  SClientObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
432 433 434 435
  for (int i = 0; i < cli->numOfThreads; i++) {
    SCliThrdObj* pThrd = cli->pThreadObj[i];
    pthread_join(pThrd->thread, NULL);
    pthread_mutex_destroy(&pThrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
436
    free(pThrd->cliAsync);
dengyihao's avatar
dengyihao 已提交
437 438 439 440 441
    free(pThrd->loop);
    free(pThrd);
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
442
}
dengyihao's avatar
dengyihao 已提交
443 444 445

void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) {
  // impl later
dengyihao's avatar
dengyihao 已提交
446 447 448
  char*    ip = (char*)(pEpSet->fqdn[pEpSet->inUse]);
  uint32_t port = pEpSet->port[pEpSet->inUse];

dengyihao's avatar
dengyihao 已提交
449 450
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
451 452 453 454
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
dengyihao's avatar
dengyihao 已提交
455

dengyihao's avatar
dengyihao 已提交
456 457 458 459 460 461 462
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));

  pCtx->pRpc = (SRpcInfo*)shandle;
  pCtx->ahandle = pMsg->ahandle;
  pCtx->msgType = pMsg->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
463 464 465 466 467 468 469

  assert(pRpc->connType == TAOS_CONN_CLIENT);
  // atomic or not
  int64_t index = pRpc->index;
  if (pRpc->index++ >= pRpc->numOfThreads) {
    pRpc->index = 0;
  }
dengyihao's avatar
dengyihao 已提交
470 471 472 473
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
474 475 476 477

  SCliThrdObj* thrd = ((SClientObj*)pRpc->tcphandle)->pThreadObj[index % pRpc->numOfThreads];

  pthread_mutex_lock(&thrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
478
  QUEUE_PUSH(&thrd->msg, &cliMsg->q);
dengyihao's avatar
dengyihao 已提交
479 480 481 482 483
  pthread_mutex_unlock(&thrd->msgMtx);

  uv_async_send(thrd->cliAsync);
}
#endif