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

20
#define CONN_PERSIST_TIME(para) (para * 1000 * 10)
dengyihao's avatar
dengyihao 已提交
21

dengyihao's avatar
dengyihao 已提交
22 23 24
typedef struct SCliConn {
  uv_connect_t connReq;
  uv_stream_t* stream;
dengyihao's avatar
dengyihao 已提交
25
  uv_write_t*  writeReq;
dengyihao's avatar
dengyihao 已提交
26
  void*        hostThrd;
dengyihao's avatar
dengyihao 已提交
27
  SConnBuffer  readBuf;
dengyihao's avatar
dengyihao 已提交
28 29
  void*        data;
  queue        conn;
dengyihao's avatar
dengyihao 已提交
30 31
  char         spi;
  char         secured;
dengyihao's avatar
dengyihao 已提交
32
  uint64_t     expireTime;
dengyihao's avatar
dengyihao 已提交
33
  int8_t       notifyCount;  // timers already notify to client
dengyihao's avatar
dengyihao 已提交
34
} SCliConn;
dengyihao's avatar
dengyihao 已提交
35

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

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

dengyihao's avatar
dengyihao 已提交
54 55 56 57 58 59 60 61 62
} SCliThrdObj;

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

dengyihao's avatar
dengyihao 已提交
63 64 65 66
typedef struct SConnList {
  queue conn;
} SConnList;

dengyihao's avatar
dengyihao 已提交
67
// conn pool
dengyihao's avatar
dengyihao 已提交
68
// add expire timeout and capacity limit
69 70
static void*     creatConnPool(int size);
static void*     destroyConnPool(void* pool);
dengyihao's avatar
dengyihao 已提交
71 72
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 已提交
73

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

dengyihao's avatar
dengyihao 已提交
90 91 92 93
// process data read from server, auth/decompress etc later
static void clientHandleResp(SCliConn* conn);
// handle except about conn
static void clientHandleExcept(SCliConn* conn);
94 95
// handle req from app
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);
dengyihao's avatar
dengyihao 已提交
96

dengyihao's avatar
dengyihao 已提交
97 98
static void clientMsgDestroy(SCliMsg* pMsg);
static void destroyTransConnCtx(STransConnCtx* ctx);
dengyihao's avatar
dengyihao 已提交
99 100 101 102
// thread obj
static SCliThrdObj* createThrdObj();
static void         destroyThrdObj(SCliThrdObj* pThrd);
// thread
dengyihao's avatar
dengyihao 已提交
103 104
static void* clientThread(void* arg);

105
static void clientHandleResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
106
  STransConnCtx* pCtx = ((SCliMsg*)conn->data)->ctx;
dengyihao's avatar
dengyihao 已提交
107
  SRpcInfo*      pRpc = pCtx->pTransInst;
dengyihao's avatar
dengyihao 已提交
108

dengyihao's avatar
dengyihao 已提交
109 110 111 112
  STransMsgHead* pHead = (STransMsgHead*)(conn->readBuf.buf);
  pHead->code = htonl(pHead->code);
  pHead->msgLen = htonl(pHead->msgLen);

dengyihao's avatar
dengyihao 已提交
113
  SRpcMsg rpcMsg;
dengyihao's avatar
dengyihao 已提交
114 115 116 117
  rpcMsg.contLen = transContLenFromMsg(pHead->msgLen);
  rpcMsg.pCont = transContFromHead(pHead);
  rpcMsg.code = pHead->code;
  rpcMsg.msgType = pHead->msgType;
dengyihao's avatar
dengyihao 已提交
118
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
119

dengyihao's avatar
dengyihao 已提交
120
  (pRpc->cfp)(NULL, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
121
  conn->notifyCount += 1;
dengyihao's avatar
dengyihao 已提交
122 123

  SCliThrdObj* pThrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
124
  tfree(conn->data);
dengyihao's avatar
dengyihao 已提交
125
  addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn);
dengyihao's avatar
dengyihao 已提交
126 127

  // start thread's timer of conn pool if not active
dengyihao's avatar
dengyihao 已提交
128 129 130
  if (!uv_is_active((uv_handle_t*)pThrd->pTimer) && pRpc->idleTime > 0) {
    uv_timer_start((uv_timer_t*)pThrd->pTimer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
  }
dengyihao's avatar
dengyihao 已提交
131 132 133 134 135 136 137 138
  destroyTransConnCtx(pCtx);
}
static void clientHandleExcept(SCliConn* pConn) {
  SCliMsg* pMsg = pConn->data;

  STransConnCtx* pCtx = pMsg->ctx;
  SRpcInfo*      pRpc = pCtx->pTransInst;

dengyihao's avatar
dengyihao 已提交
139 140 141 142
  transFreeMsg((pMsg->msg.pCont));
  pMsg->msg.pCont = NULL;

  SRpcMsg rpcMsg = {0};
dengyihao's avatar
dengyihao 已提交
143
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
144
  rpcMsg.code = -1;
dengyihao's avatar
dengyihao 已提交
145 146
  // SRpcInfo* pRpc = pMsg->ctx->pRpc;
  (pRpc->cfp)(NULL, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
147
  tfree(pConn->data);
dengyihao's avatar
dengyihao 已提交
148 149 150
  pConn->notifyCount += 1;
  destroyTransConnCtx(pCtx);
  clientConnDestroy(pConn, true);
dengyihao's avatar
dengyihao 已提交
151
}
dengyihao's avatar
dengyihao 已提交
152

dengyihao's avatar
dengyihao 已提交
153 154
static void clientTimeoutCb(uv_timer_t* handle) {
  SCliThrdObj* pThrd = handle->data;
dengyihao's avatar
dengyihao 已提交
155
  SRpcInfo*    pRpc = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
156
  int64_t      currentTime = pThrd->nextTimeout;
dengyihao's avatar
dengyihao 已提交
157
  tDebug("timeout, try to remove expire conn from conn pool");
dengyihao's avatar
dengyihao 已提交
158

dengyihao's avatar
dengyihao 已提交
159
  SConnList* p = taosHashIterate((SHashObj*)pThrd->pool, NULL);
dengyihao's avatar
dengyihao 已提交
160 161 162 163 164 165
  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);
166 167 168
        // uv_stream_t stm = *(c->stream);
        // uv_close((uv_handle_t*)&stm, clientDestroy);
        clientConnDestroy(c, true);
dengyihao's avatar
dengyihao 已提交
169 170 171 172
      } else {
        break;
      }
    }
dengyihao's avatar
dengyihao 已提交
173
    p = taosHashIterate((SHashObj*)pThrd->pool, p);
dengyihao's avatar
dengyihao 已提交
174 175
  }

dengyihao's avatar
dengyihao 已提交
176 177
  pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
  uv_timer_start(handle, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
178
}
179 180 181
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 已提交
182
}
183
static void* destroyConnPool(void* pool) {
dengyihao's avatar
dengyihao 已提交
184
  SConnList* connList = taosHashIterate((SHashObj*)pool, NULL);
dengyihao's avatar
dengyihao 已提交
185 186 187 188 189
  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);
190
      clientConnDestroy(c, true);
dengyihao's avatar
dengyihao 已提交
191
    }
dengyihao's avatar
dengyihao 已提交
192
    connList = taosHashIterate((SHashObj*)pool, connList);
dengyihao's avatar
dengyihao 已提交
193
  }
dengyihao's avatar
dengyihao 已提交
194
  taosHashClear(pool);
dengyihao's avatar
dengyihao 已提交
195 196
}

dengyihao's avatar
dengyihao 已提交
197
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) {
dengyihao's avatar
dengyihao 已提交
198 199 200 201
  char key[128] = {0};
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));

dengyihao's avatar
dengyihao 已提交
202 203
  SHashObj*  pPool = pool;
  SConnList* plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
204 205
  if (plist == NULL) {
    SConnList list;
dengyihao's avatar
dengyihao 已提交
206 207
    taosHashPut(pPool, key, strlen(key), (void*)&list, sizeof(list));
    plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
208
    QUEUE_INIT(&plist->conn);
dengyihao's avatar
dengyihao 已提交
209 210 211 212 213 214 215 216 217
  }

  if (QUEUE_IS_EMPTY(&plist->conn)) {
    return NULL;
  }
  queue* h = QUEUE_HEAD(&plist->conn);
  QUEUE_REMOVE(h);
  return QUEUE_DATA(h, SCliConn, conn);
}
dengyihao's avatar
dengyihao 已提交
218
static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
219 220 221 222
  char key[128] = {0};
  tstrncpy(key, ip, strlen(ip));
  tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));

dengyihao's avatar
dengyihao 已提交
223
  SRpcInfo* pRpc = ((SCliThrdObj*)conn->hostThrd)->pTransInst;
dengyihao's avatar
dengyihao 已提交
224
  conn->expireTime = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
225
  SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
226
  conn->notifyCount = 0;
dengyihao's avatar
dengyihao 已提交
227 228 229 230
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
}
dengyihao's avatar
dengyihao 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
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 已提交
247
static void clientAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
248
  // impl later
dengyihao's avatar
dengyihao 已提交
249 250 251 252 253
  static const int CAPACITY = 512;

  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
  if (pBuf->cap == 0) {
dengyihao's avatar
dengyihao 已提交
254
    pBuf->buf = (char*)calloc(1, CAPACITY * sizeof(char));
dengyihao's avatar
dengyihao 已提交
255 256 257
    pBuf->len = 0;
    pBuf->cap = CAPACITY;
    pBuf->left = -1;
dengyihao's avatar
dengyihao 已提交
258

dengyihao's avatar
dengyihao 已提交
259 260 261 262 263 264 265 266 267
    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 已提交
268
        pBuf->buf = realloc(pBuf->buf, pBuf->cap);
dengyihao's avatar
dengyihao 已提交
269 270 271 272 273
      }
    }
    buf->base = pBuf->buf + pBuf->len;
    buf->len = pBuf->cap - pBuf->len;
  }
dengyihao's avatar
dengyihao 已提交
274 275
}
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
276
  // impl later
dengyihao's avatar
dengyihao 已提交
277 278
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
279
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
280 281
    pBuf->len += nread;
    if (clientReadComplete(pBuf)) {
282
      tDebug("conn %p read complete", conn);
283
      clientHandleResp(conn);
dengyihao's avatar
dengyihao 已提交
284
    } else {
285
      tDebug("conn %p read partial packet, continue to read", conn);
dengyihao's avatar
dengyihao 已提交
286
    }
dengyihao's avatar
dengyihao 已提交
287 288
    return;
  }
289 290
  assert(nread <= 0);
  if (nread == 0) {
291
    tError("conn %p closed", conn);
292 293
    return;
  }
dengyihao's avatar
dengyihao 已提交
294
  if (nread < 0) {
295
    tError("conn %p read error: %s", conn, uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
296
    clientHandleExcept(conn);
dengyihao's avatar
dengyihao 已提交
297
  }
298 299
  // tDebug("Read error %s\n", uv_err_name(nread));
  // uv_close((uv_handle_t*)handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
300
}
dengyihao's avatar
dengyihao 已提交
301

302 303 304 305 306 307 308 309 310
static void clientConnDestroy(SCliConn* conn, bool clear) {
  tDebug("conn %p destroy", conn);
  if (clear) {
    uv_close((uv_handle_t*)conn->stream, NULL);
  }
  free(conn->stream);
  free(conn->readBuf.buf);
  free(conn->writeReq);
  free(conn);
dengyihao's avatar
dengyihao 已提交
311
}
dengyihao's avatar
dengyihao 已提交
312 313
static void clientDestroy(uv_handle_t* handle) {
  SCliConn* conn = handle->data;
314 315
  // QUEUE_REMOVE(&conn->conn);
  clientConnDestroy(conn, false);
dengyihao's avatar
dengyihao 已提交
316 317 318 319
}

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

dengyihao's avatar
dengyihao 已提交
321 322 323 324
  SCliMsg* pMsg = pConn->data;
  transFreeMsg((pMsg->msg.pCont));
  pMsg->msg.pCont = NULL;

dengyihao's avatar
dengyihao 已提交
325
  if (status == 0) {
326
    tDebug("conn %p data already was written out", pConn);
dengyihao's avatar
dengyihao 已提交
327
  } else {
328
    tError("conn %p failed to write: %s", pConn, uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
329
    clientHandleExcept(pConn);
dengyihao's avatar
dengyihao 已提交
330 331
    return;
  }
dengyihao's avatar
dengyihao 已提交
332
  SCliThrdObj* pThrd = pConn->hostThrd;
dengyihao's avatar
dengyihao 已提交
333 334 335 336 337
  // 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 已提交
338
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocReadBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
339 340
  // impl later
}
dengyihao's avatar
dengyihao 已提交
341 342

static void clientWrite(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
343 344 345 346
  SCliMsg*       pCliMsg = pConn->data;
  SRpcMsg*       pMsg = (SRpcMsg*)(&pCliMsg->msg);
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);

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

dengyihao's avatar
dengyihao 已提交
349 350 351 352
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);

  uv_buf_t wb = uv_buf_init((char*)pHead, msgLen);
353
  tDebug("conn %p data write out, msgType : %d, len: %d", pConn, pHead->msgType, msgLen);
dengyihao's avatar
dengyihao 已提交
354 355
  uv_write(pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb);
}
dengyihao's avatar
dengyihao 已提交
356 357
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
358 359
  SCliConn* pConn = req->data;
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
360
    // tError("failed to connect server(%s, %d), errmsg: %s", pCtx->ip, pCtx->port, uv_strerror(status));
361
    tError("conn %p failed to connect server: %s", pConn, uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
362
    clientHandleExcept(pConn);
363
    return;
dengyihao's avatar
dengyihao 已提交
364
  }
365
  tDebug("conn %p create", pConn);
dengyihao's avatar
dengyihao 已提交
366

dengyihao's avatar
dengyihao 已提交
367
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
368
  clientWrite(pConn);
dengyihao's avatar
dengyihao 已提交
369 370
}

dengyihao's avatar
dengyihao 已提交
371
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
372 373
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
dengyihao's avatar
dengyihao 已提交
374
  tDebug("msg tran time cost: %" PRIu64 "", el);
dengyihao's avatar
dengyihao 已提交
375
  et = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
376

dengyihao's avatar
dengyihao 已提交
377
  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
378
  SCliConn*      conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port);
dengyihao's avatar
dengyihao 已提交
379
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
380
    // impl later
381
    tDebug("conn %p get from conn pool", conn);
dengyihao's avatar
dengyihao 已提交
382 383
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
384 385 386 387

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

dengyihao's avatar
dengyihao 已提交
392
    // read/write stream handle
dengyihao's avatar
dengyihao 已提交
393 394
    conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
dengyihao's avatar
dengyihao 已提交
395 396 397
    conn->stream->data = conn;

    // write req handle
dengyihao's avatar
dengyihao 已提交
398
    conn->writeReq = malloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
399
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
400
    QUEUE_INIT(&conn->conn);
dengyihao's avatar
dengyihao 已提交
401 402 403

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

dengyihao's avatar
dengyihao 已提交
406
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
407
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
408
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
409
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
410 411 412 413 414 415
  }
}
static void clientAsyncCb(uv_async_t* handle) {
  SCliThrdObj* pThrd = handle->data;
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
416

dengyihao's avatar
dengyihao 已提交
417 418 419 420
  // 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 已提交
421

dengyihao's avatar
dengyihao 已提交
422 423 424 425
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
426 427

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
428 429
    clientHandleReq(pMsg, pThrd);
    count++;
dengyihao's avatar
dengyihao 已提交
430 431 432
  }
  if (count >= 2) {
    tDebug("already process batch size: %d", count);
dengyihao's avatar
dengyihao 已提交
433
  }
dengyihao's avatar
dengyihao 已提交
434 435 436 437 438 439 440 441 442
}

static void* clientThread(void* arg) {
  SCliThrdObj* pThrd = (SCliThrdObj*)arg;
  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 已提交
443

dengyihao's avatar
dengyihao 已提交
444
  SRpcInfo* pRpc = shandle;
dengyihao's avatar
dengyihao 已提交
445 446 447 448 449
  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 已提交
450
    SCliThrdObj* pThrd = createThrdObj();
dengyihao's avatar
dengyihao 已提交
451
    pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime);
dengyihao's avatar
dengyihao 已提交
452
    pThrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
453

dengyihao's avatar
dengyihao 已提交
454
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
455 456 457
    if (err == 0) {
      tDebug("sucess to create tranport-client thread %d", i);
    }
dengyihao's avatar
dengyihao 已提交
458
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
459 460 461
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
462 463 464 465
static void clientMsgDestroy(SCliMsg* pMsg) {
  // impl later
  free(pMsg);
}
dengyihao's avatar
dengyihao 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
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);

  pThrd->cliAsync = malloc(sizeof(uv_async_t));
  uv_async_init(pThrd->loop, pThrd->cliAsync, clientAsyncCb);
  pThrd->cliAsync->data = pThrd;

  pThrd->pTimer = malloc(sizeof(uv_timer_t));
  uv_timer_init(pThrd->loop, pThrd->pTimer);
  pThrd->pTimer->data = pThrd;

482
  pThrd->pool = creatConnPool(1);
dengyihao's avatar
dengyihao 已提交
483 484
  return pThrd;
}
dengyihao's avatar
dengyihao 已提交
485
static void destroyThrdObj(SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
486 487 488
  if (pThrd == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
489 490 491 492 493 494
  pthread_join(pThrd->thread, NULL);
  pthread_mutex_destroy(&pThrd->msgMtx);
  free(pThrd->cliAsync);
  free(pThrd->loop);
  free(pThrd);
}
dengyihao's avatar
dengyihao 已提交
495 496 497 498 499 500 501

static void destroyTransConnCtx(STransConnCtx* ctx) {
  if (ctx != NULL) {
    free(ctx->ip);
  }
  free(ctx);
}
dengyihao's avatar
dengyihao 已提交
502
//
dengyihao's avatar
dengyihao 已提交
503 504 505
void taosCloseClient(void* arg) {
  // impl later
  SClientObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
506
  for (int i = 0; i < cli->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
507
    destroyThrdObj(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
508 509 510
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
511
}
dengyihao's avatar
dengyihao 已提交
512 513
void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) {
  // impl later
dengyihao's avatar
dengyihao 已提交
514 515 516
  char*    ip = (char*)(pEpSet->fqdn[pEpSet->inUse]);
  uint32_t port = pEpSet->port[pEpSet->inUse];

dengyihao's avatar
dengyihao 已提交
517 518
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
519 520 521 522
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
dengyihao's avatar
dengyihao 已提交
523

dengyihao's avatar
dengyihao 已提交
524 525
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));

dengyihao's avatar
dengyihao 已提交
526
  pCtx->pTransInst = (SRpcInfo*)shandle;
dengyihao's avatar
dengyihao 已提交
527 528 529 530
  pCtx->ahandle = pMsg->ahandle;
  pCtx->msgType = pMsg->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
531 532 533 534 535 536 537

  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 已提交
538 539 540 541
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
542 543 544 545

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

  pthread_mutex_lock(&thrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
546
  QUEUE_PUSH(&thrd->msg, &cliMsg->q);
dengyihao's avatar
dengyihao 已提交
547 548 549 550 551
  pthread_mutex_unlock(&thrd->msgMtx);

  uv_async_send(thrd->cliAsync);
}
#endif