transCli.c 11.7 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
  SConnBuffer  readBuf;
dengyihao's avatar
dengyihao 已提交
25 26
  void*        data;
  queue        conn;
dengyihao's avatar
dengyihao 已提交
27 28
  char         spi;
  char         secured;
dengyihao's avatar
dengyihao 已提交
29
} SCliConn;
dengyihao's avatar
dengyihao 已提交
30

dengyihao's avatar
dengyihao 已提交
31
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
32 33 34 35
  STransConnCtx* ctx;
  SRpcMsg        msg;
  queue          q;
  uint64_t       st;
dengyihao's avatar
dengyihao 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
} SCliMsg;

typedef struct SCliThrdObj {
  pthread_t       thread;
  uv_loop_t*      loop;
  uv_async_t*     cliAsync;  //
  void*           cache;     // conn pool
  queue           msg;
  pthread_mutex_t msgMtx;
  void*           shandle;
} SCliThrdObj;

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

dengyihao's avatar
dengyihao 已提交
55 56 57 58
typedef struct SConnList {
  queue conn;
} SConnList;

dengyihao's avatar
dengyihao 已提交
59
// conn pool
dengyihao's avatar
dengyihao 已提交
60 61 62
// add expire timeout and capacity limit
static void*     connCacheCreate(int size);
static void*     connCacheDestroy(void* cache);
dengyihao's avatar
dengyihao 已提交
63 64 65
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 已提交
66 67 68 69 70
// process data read from server, auth/decompress etc
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 已提交
71
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
72
// callback after read nbytes from socket
dengyihao's avatar
dengyihao 已提交
73
static void clientReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
74
// callback after write data to socket
dengyihao's avatar
dengyihao 已提交
75
static void clientWriteCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
76
// callback after conn  to server
dengyihao's avatar
dengyihao 已提交
77
static void clientConnCb(uv_connect_t* req, int status);
dengyihao's avatar
dengyihao 已提交
78
static void clientAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
79 80
static void clientDestroy(uv_handle_t* handle);
static void clientConnDestroy(SCliConn* pConn);
dengyihao's avatar
dengyihao 已提交
81

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

dengyihao's avatar
dengyihao 已提交
84 85
static void* clientThread(void* arg);

dengyihao's avatar
dengyihao 已提交
86 87 88
static void clientProcessData(SCliConn* conn) {
  // impl
}
dengyihao's avatar
dengyihao 已提交
89 90
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);

dengyihao's avatar
dengyihao 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static void* connCacheCreate(int size) {
  SHashObj* cache = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  return false;
}
static void* connCacheDestroy(void* cache) {
  SConnList* connList = taosHashIterate((SHashObj*)cache, 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);
  }
  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;
    QUEUE_INIT(&plist->conn);
    taosHashPut(pCache, key, strlen(key), plist, sizeof(*plist));
  }

  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));

  SHashObj*  pCache = cache;
  SConnList* plist = taosHashGet(pCache, key, strlen(key));
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
}
dengyihao's avatar
dengyihao 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
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 已提交
154
static void clientAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
155
  // impl later
dengyihao's avatar
dengyihao 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  static const int CAPACITY = 512;

  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
  if (pBuf->cap == 0) {
    pBuf->buf = (char*)calloc(CAPACITY, sizeof(char));
    pBuf->len = 0;
    pBuf->cap = CAPACITY;
    pBuf->left = -1;
    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;
        pBuf->buf = realloc(pBuf->buf, pBuf->len + pBuf->left);
      }
    }
    buf->base = pBuf->buf + pBuf->len;
    buf->len = pBuf->cap - pBuf->len;
  }
dengyihao's avatar
dengyihao 已提交
180 181
}
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
182
  // impl later
dengyihao's avatar
dengyihao 已提交
183 184
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
185
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
186 187 188 189 190 191 192
    pBuf->len += nread;
    if (clientReadComplete(pBuf)) {
      tDebug("alread read complete pack");
      clientProcessData(conn);
    } else {
      tDebug("read halp packet, continue to read");
    }
dengyihao's avatar
dengyihao 已提交
193 194
    return;
  }
dengyihao's avatar
dengyihao 已提交
195

dengyihao's avatar
dengyihao 已提交
196 197 198
  if (nread != UV_EOF) {
    tDebug("Read error %s\n", uv_err_name(nread));
  }
dengyihao's avatar
dengyihao 已提交
199 200
  //
  uv_close((uv_handle_t*)handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
201
}
dengyihao's avatar
dengyihao 已提交
202 203

static void clientConnDestroy(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
204
  // impl later
dengyihao's avatar
dengyihao 已提交
205
  //
dengyihao's avatar
dengyihao 已提交
206
}
dengyihao's avatar
dengyihao 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220
static void clientDestroy(uv_handle_t* handle) {
  SCliConn* conn = handle->data;
  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 已提交
221
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocReadBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
222 223
  // impl later
}
dengyihao's avatar
dengyihao 已提交
224 225

static void clientWrite(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
226 227 228 229 230 231
  SCliMsg*       pCliMsg = pConn->data;
  SRpcMsg*       pMsg = (SRpcMsg*)(&pCliMsg->msg);
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);

  int   msgLen = transMsgLenFromCont(pMsg->contLen);
  char* msg = (char*)(pHead);
dengyihao's avatar
dengyihao 已提交
232 233 234 235

  uv_buf_t wb = uv_buf_init(msg, msgLen);
  uv_write(pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb);
}
dengyihao's avatar
dengyihao 已提交
236 237
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
238
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
239 240 241 242 243 244
  if (status != 0) {
    tError("failed to connect %s", uv_err_name(status));
    clientConnDestroy(pConn);
    return;
  }

dengyihao's avatar
dengyihao 已提交
245 246 247 248 249
  SCliMsg*       pMsg = pConn->data;
  STransConnCtx* pCtx = ((SCliMsg*)(pConn->data))->ctx;

  SRpcMsg rpcMsg;
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
250 251 252

  if (status != 0) {
    // call user fp later
dengyihao's avatar
dengyihao 已提交
253 254 255
    tError("failed to connect server(%s, %d), errmsg: %s", pCtx->ip, pCtx->port, uv_strerror(status));
    SRpcInfo* pRpc = pMsg->ctx->pRpc;
    (pRpc->cfp)(NULL, &rpcMsg, NULL);
dengyihao's avatar
dengyihao 已提交
256
    uv_close((uv_handle_t*)req->handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
257 258
    return;
  }
dengyihao's avatar
dengyihao 已提交
259
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
260
  clientWrite(pConn);
dengyihao's avatar
dengyihao 已提交
261 262
}

dengyihao's avatar
dengyihao 已提交
263
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
264 265
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
dengyihao's avatar
dengyihao 已提交
266
  tDebug("msg tran time cost: %" PRIu64 "", el);
dengyihao's avatar
dengyihao 已提交
267
  et = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
268

dengyihao's avatar
dengyihao 已提交
269 270
  STransConnCtx* pCtx = pMsg->ctx;
  SCliConn*      conn = getConnFromCache(pThrd->cache, pCtx->ip, pCtx->port);
dengyihao's avatar
dengyihao 已提交
271
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
272
    // impl later
dengyihao's avatar
dengyihao 已提交
273 274
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
275
    clientWrite(conn);
dengyihao's avatar
dengyihao 已提交
276
  } else {
dengyihao's avatar
dengyihao 已提交
277
    SCliConn* conn = calloc(1, sizeof(SCliConn));
dengyihao's avatar
dengyihao 已提交
278 279 280

    conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
dengyihao's avatar
dengyihao 已提交
281
    conn->writeReq = malloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
282 283 284

    conn->connReq.data = conn;
    conn->data = pMsg;
dengyihao's avatar
dengyihao 已提交
285

dengyihao's avatar
dengyihao 已提交
286
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
287
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
288
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
289
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
290 291 292 293 294 295
  }
}
static void clientAsyncCb(uv_async_t* handle) {
  SCliThrdObj* pThrd = handle->data;
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
296

dengyihao's avatar
dengyihao 已提交
297 298 299 300
  // 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 已提交
301

dengyihao's avatar
dengyihao 已提交
302 303 304 305
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
306 307

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
308 309 310 311 312 313
    clientHandleReq(pMsg, pThrd);
    count++;
    if (count >= 2) {
      tError("send batch size: %d", count);
    }
  }
dengyihao's avatar
dengyihao 已提交
314 315 316 317 318 319 320 321 322
}

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 已提交
323

dengyihao's avatar
dengyihao 已提交
324 325 326 327 328
  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 已提交
329 330 331 332 333 334 335 336 337
    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;
dengyihao's avatar
dengyihao 已提交
338

dengyihao's avatar
dengyihao 已提交
339 340
    pThrd->shandle = shandle;
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
341 342 343
    if (err == 0) {
      tDebug("sucess to create tranport-client thread %d", i);
    }
dengyihao's avatar
dengyihao 已提交
344
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
345 346 347
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
348 349 350 351
static void clientMsgDestroy(SCliMsg* pMsg) {
  // impl later
  free(pMsg);
}
dengyihao's avatar
dengyihao 已提交
352 353 354
void taosCloseClient(void* arg) {
  // impl later
  SClientObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
355 356 357 358
  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 已提交
359
    free(pThrd->cliAsync);
dengyihao's avatar
dengyihao 已提交
360 361 362 363 364
    free(pThrd->loop);
    free(pThrd);
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
365
}
dengyihao's avatar
dengyihao 已提交
366 367 368

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

dengyihao's avatar
dengyihao 已提交
372 373
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
374 375 376 377
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
dengyihao's avatar
dengyihao 已提交
378

dengyihao's avatar
dengyihao 已提交
379 380 381 382 383 384 385
  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 已提交
386 387 388 389 390 391 392

  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 已提交
393 394 395 396
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
397 398 399 400

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

  pthread_mutex_lock(&thrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
401
  QUEUE_PUSH(&thrd->msg, &cliMsg->q);
dengyihao's avatar
dengyihao 已提交
402 403 404 405 406
  pthread_mutex_unlock(&thrd->msgMtx);

  uv_async_send(thrd->cliAsync);
}
#endif