transCli.c 10.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
  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
// conn pool
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 已提交
59 60 61 62 63
// 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 已提交
64
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
65
// callback after read nbytes from socket
dengyihao's avatar
dengyihao 已提交
66
static void clientReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
67
// callback after write data to socket
dengyihao's avatar
dengyihao 已提交
68
static void clientWriteCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
69
// callback after conn  to server
dengyihao's avatar
dengyihao 已提交
70
static void clientConnCb(uv_connect_t* req, int status);
dengyihao's avatar
dengyihao 已提交
71
static void clientAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
72 73
static void clientDestroy(uv_handle_t* handle);
static void clientConnDestroy(SCliConn* pConn);
dengyihao's avatar
dengyihao 已提交
74

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

dengyihao's avatar
dengyihao 已提交
77 78
static void* clientThread(void* arg);

dengyihao's avatar
dengyihao 已提交
79 80 81
static void clientProcessData(SCliConn* conn) {
  // impl
}
dengyihao's avatar
dengyihao 已提交
82 83
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);

dengyihao's avatar
dengyihao 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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 已提交
100
static void clientAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
101
  // impl later
dengyihao's avatar
dengyihao 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  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 已提交
126 127
}
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
128
  // impl later
dengyihao's avatar
dengyihao 已提交
129 130
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
131
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
132 133 134 135 136 137 138
    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 已提交
139 140
    return;
  }
dengyihao's avatar
dengyihao 已提交
141

dengyihao's avatar
dengyihao 已提交
142 143 144
  if (nread != UV_EOF) {
    tDebug("Read error %s\n", uv_err_name(nread));
  }
dengyihao's avatar
dengyihao 已提交
145 146
  //
  uv_close((uv_handle_t*)handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
147
}
dengyihao's avatar
dengyihao 已提交
148 149

static void clientConnDestroy(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
150
  // impl later
dengyihao's avatar
dengyihao 已提交
151
  //
dengyihao's avatar
dengyihao 已提交
152
}
dengyihao's avatar
dengyihao 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166
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 已提交
167
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocReadBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
168 169
  // impl later
}
dengyihao's avatar
dengyihao 已提交
170 171

static void clientWrite(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
172 173 174 175 176 177
  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 已提交
178 179 180 181

  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 已提交
182 183
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
184
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
185 186 187 188 189 190
  if (status != 0) {
    tError("failed to connect %s", uv_err_name(status));
    clientConnDestroy(pConn);
    return;
  }

dengyihao's avatar
dengyihao 已提交
191 192 193 194 195
  SCliMsg*       pMsg = pConn->data;
  STransConnCtx* pCtx = ((SCliMsg*)(pConn->data))->ctx;

  SRpcMsg rpcMsg;
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
196 197 198

  if (status != 0) {
    // call user fp later
dengyihao's avatar
dengyihao 已提交
199 200 201
    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 已提交
202
    uv_close((uv_handle_t*)req->handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
203 204
    return;
  }
dengyihao's avatar
dengyihao 已提交
205
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
206
  clientWrite(pConn);
dengyihao's avatar
dengyihao 已提交
207 208 209 210
}

static SCliConn* getConnFromCache(void* cache, char* ip, uint32_t port) {
  // impl later
dengyihao's avatar
dengyihao 已提交
211

dengyihao's avatar
dengyihao 已提交
212 213
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
214 215 216
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn) {
  // impl later
}
dengyihao's avatar
dengyihao 已提交
217

dengyihao's avatar
dengyihao 已提交
218
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
219 220
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
dengyihao's avatar
dengyihao 已提交
221
  tDebug("msg tran time cost: %" PRIu64 "", el);
dengyihao's avatar
dengyihao 已提交
222
  et = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
223

dengyihao's avatar
dengyihao 已提交
224 225
  STransConnCtx* pCtx = pMsg->ctx;
  SCliConn*      conn = getConnFromCache(pThrd->cache, pCtx->ip, pCtx->port);
dengyihao's avatar
dengyihao 已提交
226
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
227
    // impl later
dengyihao's avatar
dengyihao 已提交
228 229
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
230
    clientWrite(conn);
dengyihao's avatar
dengyihao 已提交
231
  } else {
dengyihao's avatar
dengyihao 已提交
232
    SCliConn* conn = calloc(1, sizeof(SCliConn));
dengyihao's avatar
dengyihao 已提交
233 234 235

    conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
dengyihao's avatar
dengyihao 已提交
236
    conn->writeReq = malloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
237 238 239

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

dengyihao's avatar
dengyihao 已提交
241
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
242
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
243
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
244
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
245 246 247 248 249 250
  }
}
static void clientAsyncCb(uv_async_t* handle) {
  SCliThrdObj* pThrd = handle->data;
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
251

dengyihao's avatar
dengyihao 已提交
252 253 254 255
  // 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 已提交
256

dengyihao's avatar
dengyihao 已提交
257 258 259 260
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
261 262

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
263 264 265 266 267 268
    clientHandleReq(pMsg, pThrd);
    count++;
    if (count >= 2) {
      tError("send batch size: %d", count);
    }
  }
dengyihao's avatar
dengyihao 已提交
269 270 271 272 273 274 275 276 277
}

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

dengyihao's avatar
dengyihao 已提交
279 280 281 282 283
  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 已提交
284 285 286 287 288 289 290 291 292
    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 已提交
293

dengyihao's avatar
dengyihao 已提交
294 295
    pThrd->shandle = shandle;
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
296 297 298
    if (err == 0) {
      tDebug("sucess to create tranport-client thread %d", i);
    }
dengyihao's avatar
dengyihao 已提交
299
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
300 301 302
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
303 304 305 306
static void clientMsgDestroy(SCliMsg* pMsg) {
  // impl later
  free(pMsg);
}
dengyihao's avatar
dengyihao 已提交
307 308 309
void taosCloseClient(void* arg) {
  // impl later
  SClientObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
310 311 312 313
  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 已提交
314
    free(pThrd->cliAsync);
dengyihao's avatar
dengyihao 已提交
315 316 317 318 319
    free(pThrd->loop);
    free(pThrd);
  }
  free(cli->pThreadObj);
  free(cli);
dengyihao's avatar
dengyihao 已提交
320
}
dengyihao's avatar
dengyihao 已提交
321 322 323

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

dengyihao's avatar
dengyihao 已提交
327 328
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
329 330 331 332
  int32_t flen = 0;
  if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
    // imp later
  }
dengyihao's avatar
dengyihao 已提交
333

dengyihao's avatar
dengyihao 已提交
334 335 336 337 338 339 340
  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 已提交
341 342 343 344 345 346 347

  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 已提交
348 349 350 351
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
352 353 354 355

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

  pthread_mutex_lock(&thrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
356
  QUEUE_PUSH(&thrd->msg, &cliMsg->q);
dengyihao's avatar
dengyihao 已提交
357 358 359 360 361
  pthread_mutex_unlock(&thrd->msgMtx);

  uv_async_send(thrd->cliAsync);
}
#endif