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

static void* clientThread(void* arg);

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

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

static void clientConnDestroy(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
82
  // impl later
dengyihao's avatar
dengyihao 已提交
83
  //
dengyihao's avatar
dengyihao 已提交
84
}
dengyihao's avatar
dengyihao 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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;
  }

  uv_read_start((uv_stream_t*)pConn->stream, clientAllocrReadBufferCb, clientReadCb);
dengyihao's avatar
dengyihao 已提交
100 101
  // impl later
}
dengyihao's avatar
dengyihao 已提交
102 103 104 105 106 107 108 109 110 111

static void clientWrite(SCliConn* pConn) {
  SCliMsg*  pMsg = pConn->data;
  SRpcHead* pHead = rpcHeadFromCont(pMsg->context->pCont);
  int       msgLen = rpcMsgLenFromCont(pMsg->context->contLen);
  char*     msg = (char*)(pHead);

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

  SCliMsg* pMsg = pConn->data;
  SEpSet*  pEpSet = &pMsg->context->epSet;
  SRpcMsg  rpcMsg;
dengyihao's avatar
dengyihao 已提交
124 125
  // rpcMsg.ahandle = pMsg->context->ahandle;
  // rpcMsg.pCont = NULL;
dengyihao's avatar
dengyihao 已提交
126 127 128 129 130 131

  char*    fqdn = pEpSet->fqdn[pEpSet->inUse];
  uint32_t port = pEpSet->port[pEpSet->inUse];
  if (status != 0) {
    // call user fp later
    tError("failed to connect server(%s, %d), errmsg: %s", fqdn, port, uv_strerror(status));
dengyihao's avatar
dengyihao 已提交
132 133 134
    SRpcInfo* pRpc = pMsg->context->pRpc;
    (pRpc->cfp)(NULL, &rpcMsg, pEpSet);
    uv_close((uv_handle_t*)req->handle, clientDestroy);
dengyihao's avatar
dengyihao 已提交
135 136
    return;
  }
dengyihao's avatar
dengyihao 已提交
137
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
138 139 140 141
}

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

dengyihao's avatar
dengyihao 已提交
143 144
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
145 146 147
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn) {
  // impl later
}
dengyihao's avatar
dengyihao 已提交
148

dengyihao's avatar
dengyihao 已提交
149 150 151
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
  SEpSet* pEpSet = &pMsg->context->epSet;

dengyihao's avatar
dengyihao 已提交
152 153 154
  char*    fqdn = pEpSet->fqdn[pEpSet->inUse];
  uint32_t port = pEpSet->port[pEpSet->inUse];

dengyihao's avatar
dengyihao 已提交
155 156 157
  uint64_t el = taosGetTimestampUs() - pMsg->st;
  tDebug("msg tran time cost: %" PRIu64 "", el);

dengyihao's avatar
dengyihao 已提交
158 159
  SCliConn* conn = getConnFromCache(pThrd->cache, fqdn, port);
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
160
    // impl later
dengyihao's avatar
dengyihao 已提交
161 162
    conn->data = pMsg;
    conn->writeReq->data = conn;
dengyihao's avatar
dengyihao 已提交
163 164 165
    clientWrite(conn);
    // uv_buf_t wb;
    // uv_write(conn->writeReq, (uv_stream_t*)conn->stream, &wb, 1, clientWriteCb);
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 175 176

    conn->connReq.data = conn;
    conn->data = pMsg;
    struct sockaddr_in addr;
    uv_ip4_addr(fqdn, port, &addr);
dengyihao's avatar
dengyihao 已提交
177
    // handle error in callback if fail to connect
dengyihao's avatar
dengyihao 已提交
178 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 186 187 188 189 190 191 192 193 194 195 196 197
    // SRpcMsg   rpcMsg;
    // SEpSet*   pEpSet = &pMsg->context->epSet;
    // SRpcInfo* pRpc = pMsg->context->pRpc;
    //// rpcMsg.ahandle = pMsg->context->ahandle;
    // rpcMsg.pCont = NULL;
    // rpcMsg.ahandle = pMsg->context->ahandle;
    // uint64_t el1 = taosGetTimestampUs() - et;
    // tError("msg tran back first: time cost: %" PRIu64 "", el1);
    // et = taosGetTimestampUs();
    //(pRpc->cfp)(NULL, &rpcMsg, pEpSet);
    // uint64_t el2 = taosGetTimestampUs() - et;
    // tError("msg tran back second: time cost: %" PRIu64 "", el2);
  }
}
static void clientAsyncCb(uv_async_t* handle) {
  SCliThrdObj* pThrd = handle->data;
  SCliMsg*     pMsg = NULL;
  queue        wq;
dengyihao's avatar
dengyihao 已提交
198

dengyihao's avatar
dengyihao 已提交
199 200 201 202
  // 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 已提交
203

dengyihao's avatar
dengyihao 已提交
204 205 206 207 208 209 210 211 212 213 214
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
    pMsg = QUEUE_DATA(h, SCliMsg, q);
    clientHandleReq(pMsg, pThrd);
    count++;
    if (count >= 2) {
      tError("send batch size: %d", count);
    }
  }
dengyihao's avatar
dengyihao 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228
}

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));
  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 已提交
229 230 231 232 233 234 235 236 237
    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 已提交
238

dengyihao's avatar
dengyihao 已提交
239 240
    pThrd->shandle = shandle;
    int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
241 242 243
    if (err == 0) {
      tDebug("sucess to create tranport-client thread %d", i);
    }
dengyihao's avatar
dengyihao 已提交
244
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
245 246 247 248 249 250 251 252
  }
  return cli;
}

void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) {
  // impl later
  SRpcInfo* pRpc = (SRpcInfo*)shandle;

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

dengyihao's avatar
dengyihao 已提交
255 256 257 258 259
  SRpcReqContext* pContext;
  pContext = (SRpcReqContext*)((char*)pMsg->pCont - sizeof(SRpcHead) - sizeof(SRpcReqContext));
  pContext->ahandle = pMsg->ahandle;
  pContext->pRpc = (SRpcInfo*)shandle;
  pContext->epSet = *pEpSet;
dengyihao's avatar
dengyihao 已提交
260
  pContext->contLen = len;
dengyihao's avatar
dengyihao 已提交
261 262 263 264 265 266 267 268 269 270 271 272
  pContext->pCont = pMsg->pCont;
  pContext->msgType = pMsg->msgType;
  pContext->oldInUse = pEpSet->inUse;

  assert(pRpc->connType == TAOS_CONN_CLIENT);
  // atomic or not
  int64_t index = pRpc->index;
  if (pRpc->index++ >= pRpc->numOfThreads) {
    pRpc->index = 0;
  }
  SCliMsg* msg = malloc(sizeof(SCliMsg));
  msg->context = pContext;
dengyihao's avatar
dengyihao 已提交
273
  msg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
274 275 276 277 278 279 280 281 282 283

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

  pthread_mutex_lock(&thrd->msgMtx);
  QUEUE_PUSH(&thrd->msg, &msg->q);
  pthread_mutex_unlock(&thrd->msgMtx);

  uv_async_send(thrd->cliAsync);
}
#endif