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

dengyihao's avatar
dengyihao 已提交
30
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
31 32 33 34
  STransConnCtx* ctx;
  SRpcMsg        msg;
  queue          q;
  uint64_t       st;
dengyihao's avatar
dengyihao 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
} 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 已提交
54 55 56 57
// 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 已提交
58
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
59
static void clientReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
60 61
static void clientWriteCb(uv_write_t* req, int status);
static void clientConnCb(uv_connect_t* req, int status);
dengyihao's avatar
dengyihao 已提交
62
static void clientAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
63 64
static void clientDestroy(uv_handle_t* handle);
static void clientConnDestroy(SCliConn* pConn);
dengyihao's avatar
dengyihao 已提交
65 66 67

static void* clientThread(void* arg);

dengyihao's avatar
dengyihao 已提交
68 69
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);

dengyihao's avatar
dengyihao 已提交
70
static void clientAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
71 72 73
  // impl later
}
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
74
  // impl later
dengyihao's avatar
dengyihao 已提交
75 76 77 78
  SCliConn* conn = handle->data;
  if (nread > 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
79

dengyihao's avatar
dengyihao 已提交
80 81
  //
  uv_close((uv_handle_t*)handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
82
}
dengyihao's avatar
dengyihao 已提交
83 84

static void clientConnDestroy(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
85
  // impl later
dengyihao's avatar
dengyihao 已提交
86
  //
dengyihao's avatar
dengyihao 已提交
87
}
dengyihao's avatar
dengyihao 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101
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 已提交
102
  uv_read_start((uv_stream_t*)pConn->stream, clientAllocReadBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
103 104
  // impl later
}
dengyihao's avatar
dengyihao 已提交
105 106

static void clientWrite(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
107 108 109 110 111 112
  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 已提交
113 114 115 116

  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 已提交
117 118
static void clientConnCb(uv_connect_t* req, int status) {
  // impl later
dengyihao's avatar
dengyihao 已提交
119
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
120 121 122 123 124 125
  if (status != 0) {
    tError("failed to connect %s", uv_err_name(status));
    clientConnDestroy(pConn);
    return;
  }

dengyihao's avatar
dengyihao 已提交
126 127 128 129 130
  SCliMsg*       pMsg = pConn->data;
  STransConnCtx* pCtx = ((SCliMsg*)(pConn->data))->ctx;

  SRpcMsg rpcMsg;
  rpcMsg.ahandle = pCtx->ahandle;
dengyihao's avatar
dengyihao 已提交
131 132 133

  if (status != 0) {
    // call user fp later
dengyihao's avatar
dengyihao 已提交
134 135 136
    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 已提交
137
    uv_close((uv_handle_t*)req->handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
138 139
    return;
  }
dengyihao's avatar
dengyihao 已提交
140
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
141
  clientWrite(pConn);
dengyihao's avatar
dengyihao 已提交
142 143 144 145
}

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

dengyihao's avatar
dengyihao 已提交
147 148
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
149 150 151
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn) {
  // impl later
}
dengyihao's avatar
dengyihao 已提交
152

dengyihao's avatar
dengyihao 已提交
153
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
154 155
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
dengyihao's avatar
dengyihao 已提交
156
  tDebug("msg tran time cost: %" PRIu64 "", el);
dengyihao's avatar
dengyihao 已提交
157
  et = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
158

dengyihao's avatar
dengyihao 已提交
159 160
  STransConnCtx* pCtx = pMsg->ctx;
  SCliConn*      conn = getConnFromCache(pThrd->cache, pCtx->ip, pCtx->port);
dengyihao's avatar
dengyihao 已提交
161
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
162
    // impl later
dengyihao's avatar
dengyihao 已提交
163 164
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
165
    clientWrite(conn);
dengyihao's avatar
dengyihao 已提交
166 167 168 169 170
  } else {
    SCliConn* conn = malloc(sizeof(SCliConn));

    conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
dengyihao's avatar
dengyihao 已提交
171
    conn->writeReq = malloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
172 173 174

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

dengyihao's avatar
dengyihao 已提交
176
    struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
177
    uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
178
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
179
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
dengyihao's avatar
dengyihao 已提交
180 181 182 183 184 185
  }
}
static void clientAsyncCb(uv_async_t* handle) {
  SCliThrdObj* pThrd = handle->data;
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
186

dengyihao's avatar
dengyihao 已提交
187 188 189 190
  // 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 已提交
191

dengyihao's avatar
dengyihao 已提交
192 193 194 195
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
196 197

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
198 199 200 201 202 203
    clientHandleReq(pMsg, pThrd);
    count++;
    if (count >= 2) {
      tError("send batch size: %d", count);
    }
  }
dengyihao's avatar
dengyihao 已提交
204 205 206 207 208 209 210 211 212
}

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

dengyihao's avatar
dengyihao 已提交
214 215 216 217 218
  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 已提交
219 220 221 222 223 224 225 226 227
    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 已提交
228

dengyihao's avatar
dengyihao 已提交
229 230
    pThrd->shandle = shandle;
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
231 232 233
    if (err == 0) {
      tDebug("sucess to create tranport-client thread %d", i);
    }
dengyihao's avatar
dengyihao 已提交
234
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
235 236 237
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
238 239 240 241
void taosCloseClient(void* arg) {
  // impl later
  SClientObj* cli = arg;
}
dengyihao's avatar
dengyihao 已提交
242 243 244

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

dengyihao's avatar
dengyihao 已提交
248 249
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

dengyihao's avatar
dengyihao 已提交
250 251
  int len = rpcCompressRpcMsg(pMsg->pCont, pMsg->contLen);

dengyihao's avatar
dengyihao 已提交
252 253 254 255 256 257 258 259 260 261 262
  STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));

  pCtx->pRpc = (SRpcInfo*)shandle;
  pCtx->ahandle = pMsg->ahandle;
  // pContext->contLen = len;
  // pContext->pCont = pMsg->pCont;
  pCtx->msgType = pMsg->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
  // pContext->epSet = *pEpSet;
  // pContext->oldInUse = pEpSet->inUse;
dengyihao's avatar
dengyihao 已提交
263 264 265 266 267 268 269

  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 已提交
270 271 272 273
  SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
274 275 276 277

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

  pthread_mutex_lock(&thrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
278
  QUEUE_PUSH(&thrd->msg, &cliMsg->q);
dengyihao's avatar
dengyihao 已提交
279 280 281 282 283
  pthread_mutex_unlock(&thrd->msgMtx);

  uv_async_send(thrd->cliAsync);
}
#endif