transCli.c 33.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 20
/*
 * 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 {
dengyihao's avatar
dengyihao 已提交
21
  T_REF_DECLARE()
dengyihao's avatar
dengyihao 已提交
22 23
  uv_connect_t connReq;
  uv_stream_t* stream;
dengyihao's avatar
dengyihao 已提交
24
  uv_write_t   writeReq;
dengyihao's avatar
dengyihao 已提交
25
  void*        hostThrd;
dengyihao's avatar
dengyihao 已提交
26
  SConnBuffer  readBuf;
dengyihao's avatar
dengyihao 已提交
27
  void*        data;
dengyihao's avatar
dengyihao 已提交
28 29 30 31 32
  STransQueue  cliMsgs;
  queue        conn;
  uint64_t     expireTime;
  int          hThrdIdx;
  STransCtx    ctx;
dengyihao's avatar
dengyihao 已提交
33

dengyihao's avatar
dengyihao 已提交
34 35
  bool       broken;  // link broken or not
  ConnStatus status;  //
dengyihao's avatar
dengyihao 已提交
36 37 38 39

  char*    ip;
  uint32_t port;

dengyihao's avatar
dengyihao 已提交
40
  // debug and log info
dengyihao's avatar
dengyihao 已提交
41
  struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
42
  struct sockaddr_in locaddr;
dengyihao's avatar
dengyihao 已提交
43
} SCliConn;
dengyihao's avatar
dengyihao 已提交
44

dengyihao's avatar
dengyihao 已提交
45
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
46
  STransConnCtx* ctx;
dengyihao's avatar
formate  
dengyihao 已提交
47
  STransMsg      msg;
dengyihao's avatar
dengyihao 已提交
48 49
  queue          q;
  uint64_t       st;
dengyihao's avatar
dengyihao 已提交
50
  STransMsgType  type;
dengyihao's avatar
dengyihao 已提交
51
  int            sent;  //(0: no send, 1: alread sent)
dengyihao's avatar
dengyihao 已提交
52 53 54
} SCliMsg;

typedef struct SCliThrdObj {
dengyihao's avatar
dengyihao 已提交
55
  TdThread    thread;
dengyihao's avatar
dengyihao 已提交
56
  uv_loop_t*  loop;
dengyihao's avatar
dengyihao 已提交
57
  SAsyncPool* asyncPool;
dengyihao's avatar
dengyihao 已提交
58
  uv_timer_t  timer;
dengyihao's avatar
dengyihao 已提交
59 60 61
  void*       pool;  // conn pool

  // msg queue
dengyihao's avatar
dengyihao 已提交
62
  queue         msg;
wafwerar's avatar
wafwerar 已提交
63
  TdThreadMutex msgMtx;
dengyihao's avatar
dengyihao 已提交
64 65 66 67

  uint64_t nextTimeout;  // next timeout
  void*    pTransInst;   //
  bool     quit;
dengyihao's avatar
dengyihao 已提交
68 69
} SCliThrdObj;

U
ubuntu 已提交
70
typedef struct SCliObj {
dengyihao's avatar
dengyihao 已提交
71 72 73 74
  char          label[TSDB_LABEL_LEN];
  int32_t       index;
  int           numOfThreads;
  SCliThrdObj** pThreadObj;
U
ubuntu 已提交
75
} SCliObj;
dengyihao's avatar
dengyihao 已提交
76

dengyihao's avatar
dengyihao 已提交
77 78 79 80
typedef struct SConnList {
  queue conn;
} SConnList;

dengyihao's avatar
dengyihao 已提交
81
// conn pool
dengyihao's avatar
dengyihao 已提交
82
// add expire timeout and capacity limit
dengyihao's avatar
dengyihao 已提交
83
static void*     createConnPool(int size);
84
static void*     destroyConnPool(void* pool);
dengyihao's avatar
dengyihao 已提交
85
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port);
dengyihao's avatar
dengyihao 已提交
86
static void      addConnToPool(void* pool, SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
87

dengyihao's avatar
dengyihao 已提交
88
// register timer in each thread to clear expire conn
U
ubuntu 已提交
89 90
static void cliTimeoutCb(uv_timer_t* handle);
// alloc buf for recv
dengyihao's avatar
dengyihao 已提交
91
static void cliAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
U
ubuntu 已提交
92 93
// callback after  read nbytes from socket
static void cliRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
94
// callback after write data to socket
U
ubuntu 已提交
95
static void cliSendCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
96
// callback after conn  to server
U
ubuntu 已提交
97 98
static void cliConnCb(uv_connect_t* req, int status);
static void cliAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
99

dengyihao's avatar
dengyihao 已提交
100 101
static void cliAppCb(SCliConn* pConn, STransMsg* pMsg);

U
ubuntu 已提交
102 103 104
static SCliConn* cliCreateConn(SCliThrdObj* thrd);
static void      cliDestroyConn(SCliConn* pConn, bool clear /*clear tcp handle or not*/);
static void      cliDestroy(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
105
static void      cliSend(SCliConn* pConn);
dengyihao's avatar
dengyihao 已提交
106

dengyihao's avatar
dengyihao 已提交
107
// process data read from server, add decompress etc later
U
ubuntu 已提交
108
static void cliHandleResp(SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
109
// handle except about conn
U
ubuntu 已提交
110
static void cliHandleExcept(SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
111

112
// handle req from app
U
ubuntu 已提交
113 114
static void cliHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);
static void cliHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd);
dengyihao's avatar
dengyihao 已提交
115
static void cliHandleRelease(SCliMsg* pMsg, SCliThrdObj* pThrd);
dengyihao's avatar
dengyihao 已提交
116
static void (*cliAsyncHandle[])(SCliMsg* pMsg, SCliThrdObj* pThrd) = {cliHandleReq, cliHandleQuit, cliHandleRelease};
dengyihao's avatar
dengyihao 已提交
117

U
ubuntu 已提交
118
static void cliSendQuit(SCliThrdObj* thrd);
U
ubuntu 已提交
119
static void destroyUserdata(STransMsg* userdata);
dengyihao's avatar
dengyihao 已提交
120

U
ubuntu 已提交
121
static int cliRBChoseIdx(STrans* pTransInst);
dengyihao's avatar
dengyihao 已提交
122

dengyihao's avatar
dengyihao 已提交
123 124
static void destroyCmsg(SCliMsg* cmsg);
static void transDestroyConnCtx(STransConnCtx* ctx);
dengyihao's avatar
dengyihao 已提交
125 126 127
// thread obj
static SCliThrdObj* createThrdObj();
static void         destroyThrdObj(SCliThrdObj* pThrd);
dengyihao's avatar
dengyihao 已提交
128

dengyihao's avatar
dengyihao 已提交
129 130 131 132 133 134 135 136 137
static void cliWalkCb(uv_handle_t* handle, void* arg);

#define CLI_RELEASE_UV(loop)        \
  do {                              \
    uv_walk(loop, cliWalkCb, NULL); \
    uv_run(loop, UV_RUN_DEFAULT);   \
    uv_loop_close(loop);            \
  } while (0);

dengyihao's avatar
dengyihao 已提交
138 139 140 141 142 143
// snprintf may cause performance problem
#define CONN_CONSTRUCT_HASH_KEY(key, ip, port)          \
  do {                                                  \
    snprintf(key, sizeof(key), "%s:%d", ip, (int)port); \
  } while (0)

dengyihao's avatar
dengyihao 已提交
144
#define CONN_HOST_THREAD_INDEX(conn) (conn ? ((SCliConn*)conn)->hThrdIdx : -1)
dengyihao's avatar
dengyihao 已提交
145 146 147
#define CONN_PERSIST_TIME(para)      (para * 1000 * 10)
#define CONN_GET_HOST_THREAD(conn)   (conn ? ((SCliConn*)conn)->hostThrd : NULL)
#define CONN_GET_INST_LABEL(conn)    (((STrans*)(((SCliThrdObj*)(conn)->hostThrd)->pTransInst))->label)
dengyihao's avatar
dengyihao 已提交
148 149 150
#define CONN_SHOULD_RELEASE(conn, head)                                                  \
  do {                                                                                   \
    if ((head)->release == 1 && (head->msgLen) == sizeof(*head)) {                       \
dengyihao's avatar
dengyihao 已提交
151 152
      uint64_t ahandle = head->ahandle;                                                  \
      CONN_GET_MSGCTX_BY_AHANDLE(conn, ahandle);                                         \
dengyihao's avatar
dengyihao 已提交
153 154 155 156 157 158 159 160 161 162 163
      conn->status = ConnRelease;                                                        \
      transClearBuffer(&conn->readBuf);                                                  \
      transFreeMsg(transContFromHead((char*)head));                                      \
      tDebug("cli conn %p receive release request, ref: %d", conn, T_REF_VAL_GET(conn)); \
      while (T_REF_VAL_GET(conn) > 1) {                                                  \
        transUnrefCliHandle(conn);                                                       \
      }                                                                                  \
      if (T_REF_VAL_GET(conn) == 1) {                                                    \
        SCliThrdObj* thrd = conn->hostThrd;                                              \
        addConnToPool(thrd->pool, conn);                                                 \
      }                                                                                  \
dengyihao's avatar
dengyihao 已提交
164
      destroyCmsg(pMsg);                                                                 \
dengyihao's avatar
dengyihao 已提交
165 166
      return;                                                                            \
    }                                                                                    \
dengyihao's avatar
dengyihao 已提交
167 168
  } while (0)

dengyihao's avatar
dengyihao 已提交
169 170 171 172 173
#define CONN_GET_MSGCTX_BY_AHANDLE(conn, ahandle)                                         \
  do {                                                                                    \
    int i = 0, sz = transQueueSize(&conn->cliMsgs);                                       \
    for (; i < sz; i++) {                                                                 \
      pMsg = transQueueGet(&conn->cliMsgs, i);                                            \
S
Shengliang 已提交
174
      if (pMsg != NULL && pMsg->ctx != NULL && (uint64_t)pMsg->ctx->ahandle == ahandle) { \
dengyihao's avatar
dengyihao 已提交
175 176 177 178 179 180 181 182
        break;                                                                            \
      }                                                                                   \
    }                                                                                     \
    if (i == sz) {                                                                        \
      pMsg = NULL;                                                                        \
    } else {                                                                              \
      pMsg = transQueueRm(&conn->cliMsgs, i);                                             \
    }                                                                                     \
dengyihao's avatar
dengyihao 已提交
183
  } while (0)
U
ubuntu 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
#define CONN_GET_NEXT_SENDMSG(conn)                 \
  do {                                              \
    int i = 0;                                      \
    do {                                            \
      pCliMsg = transQueueGet(&conn->cliMsgs, i++); \
      if (pCliMsg && 0 == pCliMsg->sent) {          \
        break;                                      \
      }                                             \
    } while (pCliMsg != NULL);                      \
    if (pCliMsg == NULL) {                          \
      goto _RETURN;                                 \
    }                                               \
  } while (0)
dengyihao's avatar
dengyihao 已提交
197

dengyihao's avatar
dengyihao 已提交
198 199 200 201 202
#define CONN_HANDLE_THREAD_QUIT(thrd) \
  do {                                \
    if (thrd->quit) {                 \
      return;                         \
    }                                 \
dengyihao's avatar
dengyihao 已提交
203 204 205 206 207
  } while (0)

#define CONN_HANDLE_BROKEN(conn) \
  do {                           \
    if (conn->broken) {          \
dengyihao's avatar
formate  
dengyihao 已提交
208
      cliHandleExcept(conn);     \
dengyihao's avatar
dengyihao 已提交
209
      return;                    \
dengyihao's avatar
dengyihao 已提交
210
    }                            \
dengyihao's avatar
dengyihao 已提交
211
  } while (0)
dengyihao's avatar
dengyihao 已提交
212

dengyihao's avatar
formate  
dengyihao 已提交
213 214
#define CONN_SET_PERSIST_BY_APP(conn) \
  do {                                \
dengyihao's avatar
dengyihao 已提交
215 216
    if (conn->status == ConnNormal) { \
      conn->status = ConnAcquire;     \
dengyihao's avatar
formate  
dengyihao 已提交
217 218 219
      transRefCliHandle(conn);        \
    }                                 \
  } while (0)
220

dengyihao's avatar
dengyihao 已提交
221 222 223 224
#define CONN_NO_PERSIST_BY_APP(conn) \
  (((conn)->status == ConnNormal || (conn)->status == ConnInPool) && T_REF_VAL_GET(conn) == 1)
#define CONN_RELEASE_BY_SERVER(conn) \
  (((conn)->status == ConnRelease || (conn)->status == ConnInPool) && T_REF_VAL_GET(conn) == 1)
225

dengyihao's avatar
dengyihao 已提交
226 227
#define REQUEST_NO_RESP(msg)         ((msg)->noResp == 1)
#define REQUEST_PERSIS_HANDLE(msg)   ((msg)->persistHandle == 1)
dengyihao's avatar
dengyihao 已提交
228
#define REQUEST_RELEASE_HANDLE(cmsg) ((cmsg)->type == Release)
dengyihao's avatar
dengyihao 已提交
229

U
ubuntu 已提交
230
static void* cliWorkThread(void* arg);
dengyihao's avatar
dengyihao 已提交
231

dengyihao's avatar
dengyihao 已提交
232
bool cliMaySendCachedMsg(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
233
  if (!transQueueEmpty(&conn->cliMsgs)) {
dengyihao's avatar
dengyihao 已提交
234
    SCliMsg* pCliMsg = NULL;
U
ubuntu 已提交
235
    CONN_GET_NEXT_SENDMSG(conn);
dengyihao's avatar
dengyihao 已提交
236 237
    cliSend(conn);
  }
dengyihao's avatar
dengyihao 已提交
238
  return false;
U
ubuntu 已提交
239 240
_RETURN:
  return false;
dengyihao's avatar
dengyihao 已提交
241
}
dengyihao's avatar
formate  
dengyihao 已提交
242
void cliHandleResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
243
  SCliThrdObj* pThrd = conn->hostThrd;
dengyihao's avatar
formate  
dengyihao 已提交
244
  STrans*      pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
245

dengyihao's avatar
dengyihao 已提交
246 247 248
  STransMsgHead* pHead = (STransMsgHead*)(conn->readBuf.buf);
  pHead->code = htonl(pHead->code);
  pHead->msgLen = htonl(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
249

dengyihao's avatar
dengyihao 已提交
250 251 252 253 254 255
  STransMsg transMsg = {0};
  transMsg.contLen = transContLenFromMsg(pHead->msgLen);
  transMsg.pCont = transContFromHead((char*)pHead);
  transMsg.code = pHead->code;
  transMsg.msgType = pHead->msgType;
  transMsg.ahandle = NULL;
dengyihao's avatar
dengyihao 已提交
256

dengyihao's avatar
dengyihao 已提交
257 258
  SCliMsg*       pMsg = NULL;
  STransConnCtx* pCtx = NULL;
dengyihao's avatar
dengyihao 已提交
259
  CONN_SHOULD_RELEASE(conn, pHead);
dengyihao's avatar
dengyihao 已提交
260

dengyihao's avatar
dengyihao 已提交
261 262
  if (CONN_NO_PERSIST_BY_APP(conn)) {
    pMsg = transQueuePop(&conn->cliMsgs);
dengyihao's avatar
dengyihao 已提交
263 264 265 266 267 268 269 270 271 272 273
    pCtx = pMsg ? pMsg->ctx : NULL;
    if (pMsg == NULL && !CONN_NO_PERSIST_BY_APP(conn)) {
      transMsg.ahandle = transCtxDumpVal(&conn->ctx, transMsg.msgType);
      if (transMsg.ahandle == NULL) {
        transMsg.ahandle = transCtxDumpBrokenlinkVal(&conn->ctx, (int32_t*)&(transMsg.msgType));
      }
      tDebug("cli conn %p construct ahandle %p, persist: 0", conn, transMsg.ahandle);
    } else {
      transMsg.ahandle = pCtx ? pCtx->ahandle : NULL;
      tDebug("cli conn %p get ahandle %p, persist: 0", conn, transMsg.ahandle);
    }
U
ubuntu 已提交
274
  } else {
dengyihao's avatar
dengyihao 已提交
275 276 277 278
    uint64_t ahandle = (uint64_t)pHead->ahandle;
    CONN_GET_MSGCTX_BY_AHANDLE(conn, ahandle);
    if (pMsg == NULL) {
      transMsg.ahandle = transCtxDumpVal(&conn->ctx, transMsg.msgType);
dengyihao's avatar
dengyihao 已提交
279
      tDebug("cli conn %p construct ahandle %p by %s, persist: 1", conn, transMsg.ahandle, TMSG_INFO(transMsg.msgType));
dengyihao's avatar
dengyihao 已提交
280
      if (!CONN_RELEASE_BY_SERVER(conn) && transMsg.ahandle == NULL) {
dengyihao's avatar
dengyihao 已提交
281
        transMsg.code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
dengyihao's avatar
dengyihao 已提交
282
        transMsg.ahandle = transCtxDumpBrokenlinkVal(&conn->ctx, (int32_t*)&(transMsg.msgType));
dengyihao's avatar
dengyihao 已提交
283
        tDebug("cli conn %p construct ahandle %p due brokenlink, persist: 1", conn, transMsg.ahandle);
dengyihao's avatar
dengyihao 已提交
284 285 286 287 288 289
      }
    } else {
      pCtx = pMsg ? pMsg->ctx : NULL;
      transMsg.ahandle = pCtx ? pCtx->ahandle : NULL;
      tDebug("cli conn %p get ahandle %p, persist: 1", conn, transMsg.ahandle);
    }
U
ubuntu 已提交
290
  }
dengyihao's avatar
dengyihao 已提交
291 292 293
  // buf's mem alread translated to transMsg.pCont
  transClearBuffer(&conn->readBuf);

dengyihao's avatar
dengyihao 已提交
294
  if (!CONN_NO_PERSIST_BY_APP(conn)) {
dengyihao's avatar
dengyihao 已提交
295
    transMsg.handle = conn;
U
ubuntu 已提交
296
    tDebug("%s cli conn %p ref by app", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
297 298
  }

dengyihao's avatar
dengyihao 已提交
299 300 301
  tDebug("%s cli conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pTransInst->label, conn,
         TMSG_INFO(pHead->msgType), taosInetNtoa(conn->addr.sin_addr), ntohs(conn->addr.sin_port),
         taosInetNtoa(conn->locaddr.sin_addr), ntohs(conn->locaddr.sin_port), transMsg.contLen);
dengyihao's avatar
dengyihao 已提交
302

dengyihao's avatar
dengyihao 已提交
303 304 305 306 307
  if (pCtx == NULL && CONN_NO_PERSIST_BY_APP(conn)) {
    tTrace("except, server continue send while cli ignore it");
    // transUnrefCliHandle(conn);
    return;
  }
dengyihao's avatar
dengyihao 已提交
308 309 310 311 312
  if (CONN_RELEASE_BY_SERVER(conn) && transMsg.ahandle == NULL) {
    tTrace("except, server continue send while cli ignore it");
    // transUnrefCliHandle(conn);
    return;
  }
dengyihao's avatar
dengyihao 已提交
313

U
ubuntu 已提交
314
  if (pCtx == NULL || pCtx->pSem == NULL) {
U
ubuntu 已提交
315
    tTrace("%s cli conn %p handle resp", pTransInst->label, conn);
dengyihao's avatar
dengyihao 已提交
316 317
    cliAppCb(conn, &transMsg);
    //(pTransInst->cfp)(pTransInst->parent, &transMsg, NULL);
dengyihao's avatar
test  
dengyihao 已提交
318
  } else {
U
ubuntu 已提交
319
    tTrace("%s cli conn(sync) %p handle resp", pTransInst->label, conn);
dengyihao's avatar
dengyihao 已提交
320
    memcpy((char*)pCtx->pRsp, (char*)&transMsg, sizeof(transMsg));
dengyihao's avatar
test  
dengyihao 已提交
321
    tsem_post(pCtx->pSem);
dengyihao's avatar
dengyihao 已提交
322
  }
dengyihao's avatar
dengyihao 已提交
323 324
  destroyCmsg(pMsg);

dengyihao's avatar
dengyihao 已提交
325
  if (cliMaySendCachedMsg(conn) == true) {
dengyihao's avatar
dengyihao 已提交
326 327
    return;
  }
dengyihao's avatar
dengyihao 已提交
328

U
ubuntu 已提交
329
  if (CONN_NO_PERSIST_BY_APP(conn)) {
dengyihao's avatar
dengyihao 已提交
330
    addConnToPool(pThrd->pool, conn);
dengyihao's avatar
dengyihao 已提交
331
  }
dengyihao's avatar
test  
dengyihao 已提交
332

dengyihao's avatar
dengyihao 已提交
333
  uv_read_start((uv_stream_t*)conn->stream, cliAllocRecvBufferCb, cliRecvCb);
dengyihao's avatar
dengyihao 已提交
334
  // start thread's timer of conn pool if not active
dengyihao's avatar
dengyihao 已提交
335
  if (!uv_is_active((uv_handle_t*)&pThrd->timer) && pTransInst->idleTime > 0) {
U
ubuntu 已提交
336
    // uv_timer_start((uv_timer_t*)&pThrd->timer, cliTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
337
  }
dengyihao's avatar
dengyihao 已提交
338
}
U
ubuntu 已提交
339 340

void cliHandleExcept(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
341
  if (transQueueEmpty(&pConn->cliMsgs)) {
dengyihao's avatar
dengyihao 已提交
342
    if (pConn->broken == true && CONN_NO_PERSIST_BY_APP(pConn)) {
dengyihao's avatar
dengyihao 已提交
343
      tTrace("%s cli conn %p handle except, persist:0", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
formate  
dengyihao 已提交
344 345 346
      transUnrefCliHandle(pConn);
      return;
    }
dengyihao's avatar
dengyihao 已提交
347
  }
dengyihao's avatar
dengyihao 已提交
348
  SCliThrdObj* pThrd = pConn->hostThrd;
dengyihao's avatar
formate  
dengyihao 已提交
349
  STrans*      pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
350
  bool         once = false;
D
dapan1121 已提交
351
  do {
dengyihao's avatar
dengyihao 已提交
352
    SCliMsg* pMsg = transQueuePop(&pConn->cliMsgs);
D
dapan1121 已提交
353 354
    if (pMsg == NULL && once) {
      break;
dengyihao's avatar
dengyihao 已提交
355
    }
dengyihao's avatar
dengyihao 已提交
356
    STransConnCtx* pCtx = pMsg ? pMsg->ctx : NULL;
U
ubuntu 已提交
357

dengyihao's avatar
dengyihao 已提交
358 359 360 361
    STransMsg transMsg = {0};
    transMsg.code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
    transMsg.msgType = pMsg ? pMsg->msg.msgType + 1 : 0;
    transMsg.ahandle = NULL;
dengyihao's avatar
dengyihao 已提交
362
    transMsg.handle = pConn;
dengyihao's avatar
dengyihao 已提交
363

dengyihao's avatar
dengyihao 已提交
364
    if (pMsg == NULL && !CONN_NO_PERSIST_BY_APP(pConn)) {
dengyihao's avatar
dengyihao 已提交
365
      transMsg.ahandle = transCtxDumpVal(&pConn->ctx, transMsg.msgType);
dengyihao's avatar
dengyihao 已提交
366 367
      tDebug("%s cli conn %p construct ahandle %p by %s", CONN_GET_INST_LABEL(pConn), pConn, transMsg.ahandle,
             TMSG_INFO(transMsg.msgType));
dengyihao's avatar
dengyihao 已提交
368 369
      if (transMsg.ahandle == NULL) {
        transMsg.ahandle = transCtxDumpBrokenlinkVal(&pConn->ctx, (int32_t*)&(transMsg.msgType));
dengyihao's avatar
dengyihao 已提交
370 371
        tDebug("%s cli conn %p construct ahandle %p due to brokenlink", CONN_GET_INST_LABEL(pConn), pConn,
               transMsg.ahandle);
dengyihao's avatar
dengyihao 已提交
372
      }
dengyihao's avatar
dengyihao 已提交
373 374 375 376 377
    } else {
      transMsg.ahandle = pCtx ? pCtx->ahandle : NULL;
    }

    if (pCtx == NULL || pCtx->pSem == NULL) {
U
ubuntu 已提交
378 379
      tTrace("%s cli conn %p handle except", pTransInst->label, pConn);
      if (transMsg.ahandle == NULL) {
D
dapan1121 已提交
380
        once = true;
U
ubuntu 已提交
381 382
        continue;
      }
dengyihao's avatar
dengyihao 已提交
383 384
      cliAppCb(pConn, &transMsg);
      //(pTransInst->cfp)(pTransInst->parent, &transMsg, NULL);
dengyihao's avatar
dengyihao 已提交
385
    } else {
U
ubuntu 已提交
386
      tTrace("%s cli conn(sync) %p handle except", pTransInst->label, pConn);
dengyihao's avatar
dengyihao 已提交
387 388 389 390 391
      memcpy((char*)(pCtx->pRsp), (char*)(&transMsg), sizeof(transMsg));
      tsem_post(pCtx->pSem);
    }
    destroyCmsg(pMsg);
    tTrace("%s cli conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn);
D
dapan1121 已提交
392
  } while (!transQueueEmpty(&pConn->cliMsgs));
dengyihao's avatar
test  
dengyihao 已提交
393

dengyihao's avatar
dengyihao 已提交
394
  transUnrefCliHandle(pConn);
dengyihao's avatar
dengyihao 已提交
395
}
dengyihao's avatar
dengyihao 已提交
396

U
ubuntu 已提交
397
void cliTimeoutCb(uv_timer_t* handle) {
dengyihao's avatar
dengyihao 已提交
398
  SCliThrdObj* pThrd = handle->data;
dengyihao's avatar
dengyihao 已提交
399
  STrans*      pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
400
  int64_t      currentTime = pThrd->nextTimeout;
dengyihao's avatar
dengyihao 已提交
401
  tTrace("%s, cli conn timeout, try to remove expire conn from conn pool", pTransInst->label);
dengyihao's avatar
dengyihao 已提交
402

dengyihao's avatar
dengyihao 已提交
403
  SConnList* p = taosHashIterate((SHashObj*)pThrd->pool, NULL);
dengyihao's avatar
dengyihao 已提交
404 405 406 407 408 409
  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);
dengyihao's avatar
dengyihao 已提交
410
        transUnrefCliHandle(c);
dengyihao's avatar
dengyihao 已提交
411 412 413 414
      } else {
        break;
      }
    }
dengyihao's avatar
dengyihao 已提交
415
    p = taosHashIterate((SHashObj*)pThrd->pool, p);
dengyihao's avatar
dengyihao 已提交
416 417
  }

dengyihao's avatar
dengyihao 已提交
418 419
  pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pTransInst->idleTime);
  uv_timer_start(handle, cliTimeoutCb, CONN_PERSIST_TIME(pTransInst->idleTime) / 2, 0);
dengyihao's avatar
dengyihao 已提交
420
}
U
ubuntu 已提交
421 422

void* createConnPool(int size) {
423 424
  // thread local, no lock
  return taosHashInit(size, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
425
}
U
ubuntu 已提交
426
void* destroyConnPool(void* pool) {
dengyihao's avatar
dengyihao 已提交
427
  SConnList* connList = taosHashIterate((SHashObj*)pool, NULL);
dengyihao's avatar
dengyihao 已提交
428 429 430 431 432
  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);
U
ubuntu 已提交
433
      cliDestroyConn(c, true);
dengyihao's avatar
dengyihao 已提交
434
    }
dengyihao's avatar
dengyihao 已提交
435
    connList = taosHashIterate((SHashObj*)pool, connList);
dengyihao's avatar
dengyihao 已提交
436
  }
dengyihao's avatar
dengyihao 已提交
437
  taosHashCleanup(pool);
438
  return NULL;
dengyihao's avatar
dengyihao 已提交
439 440
}

dengyihao's avatar
dengyihao 已提交
441
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) {
dengyihao's avatar
dengyihao 已提交
442
  char key[128] = {0};
dengyihao's avatar
dengyihao 已提交
443
  CONN_CONSTRUCT_HASH_KEY(key, ip, port);
dengyihao's avatar
dengyihao 已提交
444

dengyihao's avatar
dengyihao 已提交
445 446
  SHashObj*  pPool = pool;
  SConnList* plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
447 448
  if (plist == NULL) {
    SConnList list;
dengyihao's avatar
dengyihao 已提交
449 450
    taosHashPut(pPool, key, strlen(key), (void*)&list, sizeof(list));
    plist = taosHashGet(pPool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
451
    QUEUE_INIT(&plist->conn);
dengyihao's avatar
dengyihao 已提交
452 453 454 455 456 457 458
  }

  if (QUEUE_IS_EMPTY(&plist->conn)) {
    return NULL;
  }
  queue* h = QUEUE_HEAD(&plist->conn);
  QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
459
  SCliConn* conn = QUEUE_DATA(h, SCliConn, conn);
U
ubuntu 已提交
460
  conn->status = ConnNormal;
dengyihao's avatar
dengyihao 已提交
461 462
  QUEUE_INIT(&conn->conn);
  return conn;
dengyihao's avatar
dengyihao 已提交
463
}
dengyihao's avatar
dengyihao 已提交
464
static void addConnToPool(void* pool, SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
465 466 467
  SCliThrdObj* thrd = conn->hostThrd;
  CONN_HANDLE_THREAD_QUIT(thrd);

dengyihao's avatar
dengyihao 已提交
468 469 470 471
  STrans* pTransInst = ((SCliThrdObj*)conn->hostThrd)->pTransInst;
  conn->expireTime = taosGetTimestampMs() + CONN_PERSIST_TIME(pTransInst->idleTime);
  transCtxCleanup(&conn->ctx);
  transQueueClear(&conn->cliMsgs);
dengyihao's avatar
dengyihao 已提交
472
  conn->status = ConnInPool;
dengyihao's avatar
dengyihao 已提交
473

dengyihao's avatar
dengyihao 已提交
474
  char key[128] = {0};
dengyihao's avatar
dengyihao 已提交
475
  CONN_CONSTRUCT_HASH_KEY(key, conn->ip, conn->port);
U
ubuntu 已提交
476
  tTrace("cli conn %p added to conn pool, read buf cap: %d", conn, conn->readBuf.cap);
dengyihao's avatar
dengyihao 已提交
477

dengyihao's avatar
dengyihao 已提交
478
  SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
479 480 481
  // list already create before
  assert(plist != NULL);
  QUEUE_PUSH(&plist->conn, &conn->conn);
dengyihao's avatar
dengyihao 已提交
482
  assert(!QUEUE_IS_EMPTY(&plist->conn));
dengyihao's avatar
dengyihao 已提交
483
}
dengyihao's avatar
dengyihao 已提交
484
static void cliAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
485
  SCliConn*    conn = handle->data;
dengyihao's avatar
dengyihao 已提交
486
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
487
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
488
}
U
ubuntu 已提交
489
static void cliRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
490
  // impl later
dengyihao's avatar
dengyihao 已提交
491 492 493
  if (handle->data == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
494 495
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
496
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
497
    pBuf->len += nread;
dengyihao's avatar
dengyihao 已提交
498
    if (transReadComplete(pBuf)) {
U
ubuntu 已提交
499 500
      tTrace("%s cli conn %p read complete", CONN_GET_INST_LABEL(conn), conn);
      cliHandleResp(conn);
dengyihao's avatar
dengyihao 已提交
501
    } else {
U
ubuntu 已提交
502
      tTrace("%s cli conn %p read partial packet, continue to read", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
503
    }
dengyihao's avatar
dengyihao 已提交
504 505
    return;
  }
dengyihao's avatar
dengyihao 已提交
506

507 508
  assert(nread <= 0);
  if (nread == 0) {
dengyihao's avatar
dengyihao 已提交
509 510 511
    // ref http://docs.libuv.org/en/v1.x/stream.html?highlight=uv_read_start#c.uv_read_cb
    // nread might be 0, which does not indicate an error or EOF. This is equivalent to EAGAIN or EWOULDBLOCK under
    // read(2).
dengyihao's avatar
dengyihao 已提交
512
    tTrace("%s cli conn %p read empty", CONN_GET_INST_LABEL(conn), conn);
513 514
    return;
  }
dengyihao's avatar
fix bug  
dengyihao 已提交
515
  if (nread < 0) {
U
ubuntu 已提交
516
    tError("%s cli conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
517
    conn->broken = true;
U
ubuntu 已提交
518
    cliHandleExcept(conn);
dengyihao's avatar
dengyihao 已提交
519
  }
dengyihao's avatar
dengyihao 已提交
520
}
dengyihao's avatar
dengyihao 已提交
521

U
ubuntu 已提交
522
static SCliConn* cliCreateConn(SCliThrdObj* pThrd) {
wafwerar's avatar
wafwerar 已提交
523
  SCliConn* conn = taosMemoryCalloc(1, sizeof(SCliConn));
dengyihao's avatar
dengyihao 已提交
524
  // read/write stream handle
wafwerar's avatar
wafwerar 已提交
525
  conn->stream = (uv_stream_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
526 527 528 529 530
  uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
  conn->stream->data = conn;

  conn->writeReq.data = conn;
  conn->connReq.data = conn;
dengyihao's avatar
dengyihao 已提交
531 532

  transQueueInit(&conn->cliMsgs, NULL);
dengyihao's avatar
dengyihao 已提交
533 534
  QUEUE_INIT(&conn->conn);
  conn->hostThrd = pThrd;
dengyihao's avatar
dengyihao 已提交
535 536
  conn->status = ConnNormal;
  conn->broken = 0;
dengyihao's avatar
dengyihao 已提交
537 538 539
  transRefCliHandle(conn);
  return conn;
}
U
ubuntu 已提交
540 541
static void cliDestroyConn(SCliConn* conn, bool clear) {
  tTrace("%s cli conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
542

dengyihao's avatar
dengyihao 已提交
543 544
  QUEUE_REMOVE(&conn->conn);
  if (clear) {
U
ubuntu 已提交
545
    uv_close((uv_handle_t*)conn->stream, cliDestroy);
546
  }
dengyihao's avatar
dengyihao 已提交
547
}
U
ubuntu 已提交
548
static void cliDestroy(uv_handle_t* handle) {
dengyihao's avatar
dengyihao 已提交
549
  SCliConn* conn = handle->data;
wafwerar's avatar
wafwerar 已提交
550 551
  taosMemoryFree(conn->ip);
  taosMemoryFree(conn->stream);
dengyihao's avatar
dengyihao 已提交
552 553
  transCtxCleanup(&conn->ctx);
  transQueueDestroy(&conn->cliMsgs);
U
ubuntu 已提交
554
  tTrace("%s cli conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
555
  transDestroyBuffer(&conn->readBuf);
wafwerar's avatar
wafwerar 已提交
556
  taosMemoryFree(conn);
dengyihao's avatar
dengyihao 已提交
557
}
dengyihao's avatar
dengyihao 已提交
558
static bool cliHandleNoResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
559 560
  bool res = false;
  if (!transQueueEmpty(&conn->cliMsgs)) {
dengyihao's avatar
dengyihao 已提交
561
    SCliMsg* pMsg = transQueueGet(&conn->cliMsgs, 0);
dengyihao's avatar
dengyihao 已提交
562
    if (REQUEST_NO_RESP(&pMsg->msg)) {
dengyihao's avatar
dengyihao 已提交
563 564
      transQueuePop(&conn->cliMsgs);
      // taosArrayRemove(msgs, 0);
dengyihao's avatar
dengyihao 已提交
565 566 567 568 569
      destroyCmsg(pMsg);
      res = true;
    }
    if (res == true) {
      if (cliMaySendCachedMsg(conn) == false) {
dengyihao's avatar
dengyihao 已提交
570 571
        SCliThrdObj* thrd = conn->hostThrd;
        addConnToPool(thrd->pool, conn);
dengyihao's avatar
dengyihao 已提交
572 573 574 575 576
      }
    }
  }
  return res;
}
U
ubuntu 已提交
577
static void cliSendCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
578
  SCliConn* pConn = req->data;
dengyihao's avatar
dengyihao 已提交
579

dengyihao's avatar
dengyihao 已提交
580
  if (status == 0) {
U
ubuntu 已提交
581
    tTrace("%s cli conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
582
  } else {
U
ubuntu 已提交
583 584
    tError("%s cli conn %p failed to write: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status));
    cliHandleExcept(pConn);
dengyihao's avatar
dengyihao 已提交
585 586
    return;
  }
dengyihao's avatar
dengyihao 已提交
587 588 589 590
  if (cliHandleNoResp(pConn) == true) {
    tTrace("%s cli conn %p no resp required", CONN_GET_INST_LABEL(pConn), pConn);
    return;
  }
dengyihao's avatar
dengyihao 已提交
591
  uv_read_start((uv_stream_t*)pConn->stream, cliAllocRecvBufferCb, cliRecvCb);
dengyihao's avatar
dengyihao 已提交
592
}
dengyihao's avatar
dengyihao 已提交
593

U
ubuntu 已提交
594
void cliSend(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
595 596
  CONN_HANDLE_BROKEN(pConn);

dengyihao's avatar
dengyihao 已提交
597 598
  // assert(taosArrayGetSize(pConn->cliMsgs) > 0);
  assert(!transQueueEmpty(&pConn->cliMsgs));
dengyihao's avatar
dengyihao 已提交
599 600

  SCliMsg* pCliMsg = NULL;
U
ubuntu 已提交
601
  CONN_GET_NEXT_SENDMSG(pConn);
dengyihao's avatar
dengyihao 已提交
602 603
  pCliMsg->sent = 1;

dengyihao's avatar
dengyihao 已提交
604
  STransConnCtx* pCtx = pCliMsg->ctx;
dengyihao's avatar
dengyihao 已提交
605 606

  SCliThrdObj* pThrd = pConn->hostThrd;
dengyihao's avatar
formate  
dengyihao 已提交
607
  STrans*      pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
608

U
ubuntu 已提交
609
  STransMsg* pMsg = (STransMsg*)(&pCliMsg->msg);
dengyihao's avatar
dengyihao 已提交
610 611 612 613
  if (pMsg->pCont == 0) {
    pMsg->pCont = (void*)rpcMallocCont(0);
    pMsg->contLen = 0;
  }
dengyihao's avatar
dengyihao 已提交
614
  int msgLen = transMsgLenFromCont(pMsg->contLen);
dengyihao's avatar
dengyihao 已提交
615

dengyihao's avatar
dengyihao 已提交
616 617
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
  pHead->ahandle = pCtx != NULL ? (uint64_t)pCtx->ahandle : 0;
dengyihao's avatar
dengyihao 已提交
618 619
  pHead->noResp = REQUEST_NO_RESP(pMsg) ? 1 : 0;
  pHead->persist = REQUEST_PERSIS_HANDLE(pMsg) ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
620 621
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);
dengyihao's avatar
dengyihao 已提交
622
  pHead->release = REQUEST_RELEASE_HANDLE(pCliMsg) ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
623
  memcpy(pHead->user, pTransInst->user, strlen(pTransInst->user));
dengyihao's avatar
dengyihao 已提交
624 625

  uv_buf_t wb = uv_buf_init((char*)pHead, msgLen);
dengyihao's avatar
dengyihao 已提交
626 627 628
  tDebug("%s cli conn %p %s is send to %s:%d, local info %s:%d", CONN_GET_INST_LABEL(pConn), pConn,
         TMSG_INFO(pHead->msgType), taosInetNtoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port),
         taosInetNtoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port));
dengyihao's avatar
dengyihao 已提交
629

dengyihao's avatar
dengyihao 已提交
630 631 632
  if (pHead->persist == 1) {
    CONN_SET_PERSIST_BY_APP(pConn);
  }
dengyihao's avatar
dengyihao 已提交
633

dengyihao's avatar
dengyihao 已提交
634
  pConn->writeReq.data = pConn;
U
ubuntu 已提交
635
  uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, cliSendCb);
dengyihao's avatar
dengyihao 已提交
636

U
ubuntu 已提交
637 638
  return;
_RETURN:
dengyihao's avatar
dengyihao 已提交
639
  return;
dengyihao's avatar
dengyihao 已提交
640
}
U
ubuntu 已提交
641 642

void cliConnCb(uv_connect_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
643
  // impl later
dengyihao's avatar
dengyihao 已提交
644 645
  SCliConn* pConn = req->data;
  if (status != 0) {
U
ubuntu 已提交
646 647
    tError("%s cli conn %p failed to connect server: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status));
    cliHandleExcept(pConn);
648
    return;
dengyihao's avatar
dengyihao 已提交
649
  }
dengyihao's avatar
dengyihao 已提交
650 651 652
  int addrlen = sizeof(pConn->addr);
  uv_tcp_getpeername((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->addr, &addrlen);

dengyihao's avatar
dengyihao 已提交
653 654 655
  addrlen = sizeof(pConn->locaddr);
  uv_tcp_getsockname((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->locaddr, &addrlen);

U
ubuntu 已提交
656
  tTrace("%s cli conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
657
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
658

U
ubuntu 已提交
659
  cliSend(pConn);
dengyihao's avatar
dengyihao 已提交
660 661
}

U
ubuntu 已提交
662 663
static void cliHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) {
  tDebug("cli work thread %p start to quit", pThrd);
dengyihao's avatar
dengyihao 已提交
664
  destroyCmsg(pMsg);
dengyihao's avatar
fix bug  
dengyihao 已提交
665
  destroyConnPool(pThrd->pool);
dengyihao's avatar
dengyihao 已提交
666
  uv_timer_stop(&pThrd->timer);
dengyihao's avatar
dengyihao 已提交
667 668
  uv_walk(pThrd->loop, cliWalkCb, NULL);

dengyihao's avatar
dengyihao 已提交
669
  pThrd->quit = true;
dengyihao's avatar
dengyihao 已提交
670

dengyihao's avatar
dengyihao 已提交
671
  // uv_stop(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
672
}
dengyihao's avatar
dengyihao 已提交
673 674
static void cliHandleRelease(SCliMsg* pMsg, SCliThrdObj* pThrd) {
  SCliConn* conn = pMsg->msg.handle;
dengyihao's avatar
dengyihao 已提交
675
  tDebug("%s cli conn %p start to release to inst", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
676

dengyihao's avatar
dengyihao 已提交
677 678
  if (T_REF_VAL_GET(conn) == 2) {
    transUnrefCliHandle(conn);
dengyihao's avatar
dengyihao 已提交
679 680
    if (!transQueuePush(&conn->cliMsgs, pMsg)) {
      return;
dengyihao's avatar
dengyihao 已提交
681 682 683 684 685
    }
    cliSend(conn);
  } else {
    // conn already broken down
    transUnrefCliHandle(conn);
dengyihao's avatar
dengyihao 已提交
686 687
  }
}
U
ubuntu 已提交
688 689

SCliConn* cliGetConn(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
690
  SCliConn* conn = NULL;
dengyihao's avatar
dengyihao 已提交
691 692
  if (pMsg->msg.handle != NULL) {
    conn = (SCliConn*)(pMsg->msg.handle);
dengyihao's avatar
dengyihao 已提交
693
    if (conn != NULL) {
U
ubuntu 已提交
694
      tTrace("%s cli conn %p reused", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
695 696
    }
  } else {
dengyihao's avatar
dengyihao 已提交
697
    STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
698
    conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port);
dengyihao's avatar
dengyihao 已提交
699 700
    if (conn != NULL) {
      tTrace("%s cli conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
701 702
    } else {
      tTrace("not found conn in conn pool %p", pThrd->pool);
dengyihao's avatar
dengyihao 已提交
703
    }
dengyihao's avatar
dengyihao 已提交
704
  }
dengyihao's avatar
dengyihao 已提交
705 706
  return conn;
}
U
ubuntu 已提交
707 708

void cliHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
709 710
  uint64_t et = taosGetTimestampUs();
  uint64_t el = et - pMsg->st;
U
ubuntu 已提交
711
  tTrace("%s cli msg tran time cost: %" PRIu64 "us", ((STrans*)pThrd->pTransInst)->label, el);
dengyihao's avatar
dengyihao 已提交
712 713

  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
formate  
dengyihao 已提交
714
  STrans*        pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
715

U
ubuntu 已提交
716
  SCliConn* conn = cliGetConn(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
717
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
718
    conn->hThrdIdx = pCtx->hThrdIdx;
dengyihao's avatar
dengyihao 已提交
719

dengyihao's avatar
dengyihao 已提交
720
    transCtxMerge(&conn->ctx, &pCtx->appCtx);
dengyihao's avatar
dengyihao 已提交
721 722 723 724 725
    transQueuePush(&conn->cliMsgs, pMsg);
    //  tTrace("%s cli conn %p queue msg size %d", ((STrans*)pThrd->pTransInst)->label, conn, 2);
    //  return;
    //}
    // transDestroyBuffer(&conn->readBuf);
U
ubuntu 已提交
726
    cliSend(conn);
dengyihao's avatar
dengyihao 已提交
727
  } else {
U
ubuntu 已提交
728
    conn = cliCreateConn(pThrd);
dengyihao's avatar
dengyihao 已提交
729
    transCtxMerge(&conn->ctx, &pCtx->appCtx);
dengyihao's avatar
dengyihao 已提交
730
    transQueuePush(&conn->cliMsgs, pMsg);
dengyihao's avatar
dengyihao 已提交
731

dengyihao's avatar
dengyihao 已提交
732 733 734
    conn->hThrdIdx = pCtx->hThrdIdx;
    conn->ip = strdup(pMsg->ctx->ip);
    conn->port = pMsg->ctx->port;
dengyihao's avatar
dengyihao 已提交
735

dengyihao's avatar
dengyihao 已提交
736 737
    int ret = transSetConnOption((uv_tcp_t*)conn->stream);
    if (ret) {
U
ubuntu 已提交
738
      tError("%s cli conn %p failed to set conn option, errmsg %s", pTransInst->label, conn, uv_err_name(ret));
dengyihao's avatar
dengyihao 已提交
739
    }
740

dengyihao's avatar
dengyihao 已提交
741
    struct sockaddr_in addr;
742 743 744 745 746

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = taosGetIpv4FromFqdn(conn->ip);
    addr.sin_port = (uint16_t)htons((uint16_t)conn->port);
    // uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
dengyihao's avatar
dengyihao 已提交
747
    // handle error in callback if fail to connect
U
ubuntu 已提交
748 749
    tTrace("%s cli conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port);
    uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb);
dengyihao's avatar
dengyihao 已提交
750 751
  }
}
U
ubuntu 已提交
752
static void cliAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
753 754
  SAsyncItem*  item = handle->data;
  SCliThrdObj* pThrd = item->pThrd;
dengyihao's avatar
dengyihao 已提交
755
  SCliMsg*     pMsg = NULL;
dengyihao's avatar
dengyihao 已提交
756

dengyihao's avatar
dengyihao 已提交
757
  // batch process to avoid to lock/unlock frequently
dengyihao's avatar
formate  
dengyihao 已提交
758
  queue wq;
wafwerar's avatar
wafwerar 已提交
759
  taosThreadMutexLock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
760
  QUEUE_MOVE(&item->qmsg, &wq);
wafwerar's avatar
wafwerar 已提交
761
  taosThreadMutexUnlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
762

dengyihao's avatar
dengyihao 已提交
763 764 765 766
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
767 768

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
769 770
    if (pMsg == NULL) {
      continue;
dengyihao's avatar
dengyihao 已提交
771
    }
dengyihao's avatar
dengyihao 已提交
772
    (*cliAsyncHandle[pMsg->type])(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
773
    count++;
dengyihao's avatar
dengyihao 已提交
774 775
  }
  if (count >= 2) {
U
ubuntu 已提交
776
    tTrace("cli process batch size: %d", count);
dengyihao's avatar
dengyihao 已提交
777
  }
dengyihao's avatar
dengyihao 已提交
778 779
}

U
ubuntu 已提交
780
static void* cliWorkThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
781
  SCliThrdObj* pThrd = (SCliThrdObj*)arg;
U
ubuntu 已提交
782
  setThreadName("trans-cli-work");
dengyihao's avatar
dengyihao 已提交
783
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
784
  return NULL;
dengyihao's avatar
dengyihao 已提交
785 786
}

U
ubuntu 已提交
787
void* transInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
wafwerar's avatar
wafwerar 已提交
788
  SCliObj* cli = taosMemoryCalloc(1, sizeof(SCliObj));
dengyihao's avatar
dengyihao 已提交
789

dengyihao's avatar
dengyihao 已提交
790
  STrans* pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
791 792
  memcpy(cli->label, label, strlen(label));
  cli->numOfThreads = numOfThreads;
wafwerar's avatar
wafwerar 已提交
793
  cli->pThreadObj = (SCliThrdObj**)taosMemoryCalloc(cli->numOfThreads, sizeof(SCliThrdObj*));
dengyihao's avatar
dengyihao 已提交
794 795

  for (int i = 0; i < cli->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
796
    SCliThrdObj* pThrd = createThrdObj();
dengyihao's avatar
dengyihao 已提交
797
    pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pTransInst->idleTime);
dengyihao's avatar
dengyihao 已提交
798
    pThrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
799

wafwerar's avatar
wafwerar 已提交
800
    int err = taosThreadCreate(&pThrd->thread, NULL, cliWorkThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
801
    if (err == 0) {
U
ubuntu 已提交
802
      tDebug("success to create tranport-cli thread %d", i);
dengyihao's avatar
dengyihao 已提交
803
    }
dengyihao's avatar
dengyihao 已提交
804
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
805 806 807
  }
  return cli;
}
dengyihao's avatar
dengyihao 已提交
808

U
ubuntu 已提交
809
static void destroyUserdata(STransMsg* userdata) {
dengyihao's avatar
dengyihao 已提交
810 811 812 813 814 815 816 817 818 819 820 821
  if (userdata->pCont == NULL) {
    return;
  }
  transFreeMsg(userdata->pCont);
  userdata->pCont = NULL;
}
static void destroyCmsg(SCliMsg* pMsg) {
  if (pMsg == NULL) {
    return;
  }
  transDestroyConnCtx(pMsg->ctx);
  destroyUserdata(&pMsg->msg);
wafwerar's avatar
wafwerar 已提交
822
  taosMemoryFree(pMsg);
dengyihao's avatar
dengyihao 已提交
823
}
dengyihao's avatar
dengyihao 已提交
824

dengyihao's avatar
dengyihao 已提交
825
static SCliThrdObj* createThrdObj() {
wafwerar's avatar
wafwerar 已提交
826
  SCliThrdObj* pThrd = (SCliThrdObj*)taosMemoryCalloc(1, sizeof(SCliThrdObj));
U
ubuntu 已提交
827

dengyihao's avatar
dengyihao 已提交
828
  QUEUE_INIT(&pThrd->msg);
wafwerar's avatar
wafwerar 已提交
829
  taosThreadMutexInit(&pThrd->msgMtx, NULL);
dengyihao's avatar
dengyihao 已提交
830

wafwerar's avatar
wafwerar 已提交
831
  pThrd->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
832 833
  uv_loop_init(pThrd->loop);

U
ubuntu 已提交
834
  pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, cliAsyncCb);
dengyihao's avatar
dengyihao 已提交
835

dengyihao's avatar
dengyihao 已提交
836 837
  uv_timer_init(pThrd->loop, &pThrd->timer);
  pThrd->timer.data = pThrd;
dengyihao's avatar
dengyihao 已提交
838

dengyihao's avatar
dengyihao 已提交
839
  pThrd->pool = createConnPool(4);
dengyihao's avatar
dengyihao 已提交
840 841

  pThrd->quit = false;
dengyihao's avatar
dengyihao 已提交
842 843
  return pThrd;
}
dengyihao's avatar
dengyihao 已提交
844
static void destroyThrdObj(SCliThrdObj* pThrd) {
dengyihao's avatar
dengyihao 已提交
845 846 847
  if (pThrd == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
848
  taosThreadJoin(pThrd->thread, NULL);
dengyihao's avatar
dengyihao 已提交
849
  CLI_RELEASE_UV(pThrd->loop);
wafwerar's avatar
wafwerar 已提交
850
  taosThreadMutexDestroy(&pThrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
851
  transDestroyAsyncPool(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
852 853

  uv_timer_stop(&pThrd->timer);
wafwerar's avatar
wafwerar 已提交
854 855
  taosMemoryFree(pThrd->loop);
  taosMemoryFree(pThrd);
dengyihao's avatar
dengyihao 已提交
856
}
dengyihao's avatar
dengyihao 已提交
857

dengyihao's avatar
dengyihao 已提交
858
static void transDestroyConnCtx(STransConnCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
859
  if (ctx != NULL) {
wafwerar's avatar
wafwerar 已提交
860
    taosMemoryFree(ctx->ip);
dengyihao's avatar
dengyihao 已提交
861
  }
wafwerar's avatar
wafwerar 已提交
862
  taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
863
}
dengyihao's avatar
dengyihao 已提交
864
//
U
ubuntu 已提交
865
void cliSendQuit(SCliThrdObj* thrd) {
dengyihao's avatar
dengyihao 已提交
866
  // cli can stop gracefully
wafwerar's avatar
wafwerar 已提交
867
  SCliMsg* msg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
868
  msg->type = Quit;
dengyihao's avatar
dengyihao 已提交
869
  transSendAsync(thrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
870
}
dengyihao's avatar
dengyihao 已提交
871 872 873 874 875
void cliWalkCb(uv_handle_t* handle, void* arg) {
  if (!uv_is_closing(handle)) {
    uv_close(handle, NULL);
  }
}
dengyihao's avatar
dengyihao 已提交
876

U
ubuntu 已提交
877 878 879 880 881 882 883
int cliRBChoseIdx(STrans* pTransInst) {
  int64_t index = pTransInst->index;
  if (pTransInst->index++ >= pTransInst->numOfThreads) {
    pTransInst->index = 0;
  }
  return index % pTransInst->numOfThreads;
}
dengyihao's avatar
dengyihao 已提交
884 885 886 887 888
void cliAppCb(SCliConn* pConn, STransMsg* transMsg) {
  SCliThrdObj* pThrd = pConn->hostThrd;
  STrans*      pTransInst = pThrd->pTransInst;

  if (transMsg->code == TSDB_CODE_RPC_REDIRECT && pTransInst->retry != NULL) {
dengyihao's avatar
dengyihao 已提交
889 890 891
    SMEpSet emsg = {0};
    tDeserializeSMEpSet(transMsg->pCont, transMsg->contLen, &emsg);
    pTransInst->retry(pTransInst, transMsg, &(emsg.epSet));
dengyihao's avatar
dengyihao 已提交
892
  } else {
dengyihao's avatar
dengyihao 已提交
893
    pTransInst->cfp(pTransInst->parent, transMsg, NULL);
dengyihao's avatar
dengyihao 已提交
894 895
  }
}
U
ubuntu 已提交
896 897

void transCloseClient(void* arg) {
U
ubuntu 已提交
898
  SCliObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
899
  for (int i = 0; i < cli->numOfThreads; i++) {
U
ubuntu 已提交
900
    cliSendQuit(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
901
    destroyThrdObj(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
902
  }
wafwerar's avatar
wafwerar 已提交
903 904
  taosMemoryFree(cli->pThreadObj);
  taosMemoryFree(cli);
dengyihao's avatar
dengyihao 已提交
905
}
dengyihao's avatar
dengyihao 已提交
906 907 908 909 910 911 912 913 914 915 916 917
void transRefCliHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  int ref = T_REF_INC((SCliConn*)handle);
  UNUSED(ref);
}
void transUnrefCliHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  int ref = T_REF_DEC((SCliConn*)handle);
dengyihao's avatar
dengyihao 已提交
918
  tDebug("%s cli conn %p ref %d", CONN_GET_INST_LABEL((SCliConn*)handle), handle, ref);
dengyihao's avatar
dengyihao 已提交
919
  if (ref == 0) {
U
ubuntu 已提交
920
    cliDestroyConn((SCliConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
921 922
  }
}
dengyihao's avatar
dengyihao 已提交
923 924 925 926 927 928 929
void transReleaseCliHandle(void* handle) {
  SCliThrdObj* thrd = CONN_GET_HOST_THREAD(handle);
  if (thrd == NULL) {
    return;
  }

  STransMsg tmsg = {.handle = handle};
wafwerar's avatar
wafwerar 已提交
930
  SCliMsg*  cmsg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
931
  cmsg->msg = tmsg;
dengyihao's avatar
dengyihao 已提交
932
  cmsg->type = Release;
dengyihao's avatar
dengyihao 已提交
933 934 935

  transSendAsync(thrd->asyncPool, &cmsg->q);
}
dengyihao's avatar
dengyihao 已提交
936

dengyihao's avatar
dengyihao 已提交
937
void transSendRequest(void* shandle, const char* ip, uint32_t port, STransMsg* pMsg, STransCtx* ctx) {
U
ubuntu 已提交
938
  STrans* pTransInst = (STrans*)shandle;
dengyihao's avatar
formate  
dengyihao 已提交
939
  int     index = CONN_HOST_THREAD_INDEX((SCliConn*)pMsg->handle);
dengyihao's avatar
dengyihao 已提交
940
  if (index == -1) {
U
ubuntu 已提交
941
    index = cliRBChoseIdx(pTransInst);
dengyihao's avatar
dengyihao 已提交
942
  }
dengyihao's avatar
dengyihao 已提交
943

wafwerar's avatar
wafwerar 已提交
944
  STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx));
dengyihao's avatar
dengyihao 已提交
945 946 947 948
  pCtx->ahandle = pMsg->ahandle;
  pCtx->msgType = pMsg->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
949
  pCtx->hThrdIdx = index;
dengyihao's avatar
dengyihao 已提交
950 951 952 953

  if (ctx != NULL) {
    pCtx->appCtx = *ctx;
  }
dengyihao's avatar
dengyihao 已提交
954
  assert(pTransInst->connType == TAOS_CONN_CLIENT);
dengyihao's avatar
dengyihao 已提交
955

wafwerar's avatar
wafwerar 已提交
956
  SCliMsg* cliMsg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
957 958 959
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pMsg;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
960
  cliMsg->type = Normal;
dengyihao's avatar
dengyihao 已提交
961

U
ubuntu 已提交
962
  SCliThrdObj* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
963

L
Liu Jicong 已提交
964 965
  tDebug("send request at thread:%d %p, dst: %s:%d, app:%p", index, pMsg, ip, port, pMsg->ahandle);
  ASSERT(transSendAsync(thrd->asyncPool, &(cliMsg->q)) == 0);
dengyihao's avatar
dengyihao 已提交
966
}
dengyihao's avatar
dengyihao 已提交
967

dengyihao's avatar
formate  
dengyihao 已提交
968
void transSendRecv(void* shandle, const char* ip, uint32_t port, STransMsg* pReq, STransMsg* pRsp) {
U
ubuntu 已提交
969
  STrans* pTransInst = (STrans*)shandle;
dengyihao's avatar
formate  
dengyihao 已提交
970
  int     index = CONN_HOST_THREAD_INDEX(pReq->handle);
dengyihao's avatar
dengyihao 已提交
971
  if (index == -1) {
U
ubuntu 已提交
972
    index = cliRBChoseIdx(pTransInst);
dengyihao's avatar
dengyihao 已提交
973 974
  }

wafwerar's avatar
wafwerar 已提交
975
  STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx));
dengyihao's avatar
dengyihao 已提交
976 977 978 979
  pCtx->ahandle = pReq->ahandle;
  pCtx->msgType = pReq->msgType;
  pCtx->ip = strdup(ip);
  pCtx->port = port;
dengyihao's avatar
dengyihao 已提交
980
  pCtx->hThrdIdx = index;
wafwerar's avatar
wafwerar 已提交
981
  pCtx->pSem = taosMemoryCalloc(1, sizeof(tsem_t));
dengyihao's avatar
dengyihao 已提交
982 983 984
  pCtx->pRsp = pRsp;
  tsem_init(pCtx->pSem, 0, 0);

wafwerar's avatar
wafwerar 已提交
985
  SCliMsg* cliMsg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
986 987 988
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pReq;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
989
  cliMsg->type = Normal;
dengyihao's avatar
dengyihao 已提交
990

U
ubuntu 已提交
991
  SCliThrdObj* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[index];
dengyihao's avatar
dengyihao 已提交
992 993 994 995
  transSendAsync(thrd->asyncPool, &(cliMsg->q));
  tsem_t* pSem = pCtx->pSem;
  tsem_wait(pSem);
  tsem_destroy(pSem);
wafwerar's avatar
wafwerar 已提交
996
  taosMemoryFree(pSem);
dengyihao's avatar
dengyihao 已提交
997
}
U
ubuntu 已提交
998

dengyihao's avatar
dengyihao 已提交
999
#endif