transCli.c 53.2 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1
/** Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
dengyihao's avatar
dengyihao 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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"

dengyihao's avatar
dengyihao 已提交
18
typedef struct SConnList {
dengyihao's avatar
dengyihao 已提交
19
  queue   conns;
dengyihao's avatar
dengyihao 已提交
20
  int32_t size;
dengyihao's avatar
dengyihao 已提交
21 22
} SConnList;

dengyihao's avatar
dengyihao 已提交
23
typedef struct SCliConn {
dengyihao's avatar
dengyihao 已提交
24
  T_REF_DECLARE()
dengyihao's avatar
dengyihao 已提交
25 26
  uv_connect_t connReq;
  uv_stream_t* stream;
dengyihao's avatar
dengyihao 已提交
27
  queue        wreqQueue;
dengyihao's avatar
dengyihao 已提交
28
  uv_timer_t*  timer;
dengyihao's avatar
dengyihao 已提交
29

dengyihao's avatar
dengyihao 已提交
30 31 32 33
  void* hostThrd;

  SConnBuffer readBuf;
  STransQueue cliMsgs;
dengyihao's avatar
dengyihao 已提交
34 35 36

  queue      q;
  SConnList* list;
dengyihao's avatar
dengyihao 已提交
37 38

  STransCtx  ctx;
dengyihao's avatar
dengyihao 已提交
39 40
  bool       broken;  // link broken or not
  ConnStatus status;  //
dengyihao's avatar
dengyihao 已提交
41

dengyihao's avatar
dengyihao 已提交
42
  int64_t  refId;
dengyihao's avatar
dengyihao 已提交
43 44 45
  char*    ip;
  uint32_t port;

dengyihao's avatar
dengyihao 已提交
46
  SDelayTask* task;
dengyihao's avatar
dengyihao 已提交
47

dengyihao's avatar
dengyihao 已提交
48
  // debug and log info
dengyihao's avatar
dengyihao 已提交
49 50 51
  char src[32];
  char dst[32];

dengyihao's avatar
dengyihao 已提交
52
} SCliConn;
dengyihao's avatar
dengyihao 已提交
53

dengyihao's avatar
dengyihao 已提交
54
typedef struct SCliMsg {
dengyihao's avatar
dengyihao 已提交
55
  STransConnCtx* ctx;
dengyihao's avatar
formate  
dengyihao 已提交
56
  STransMsg      msg;
dengyihao's avatar
dengyihao 已提交
57
  queue          q;
dengyihao's avatar
dengyihao 已提交
58
  STransMsgType  type;
dengyihao's avatar
dengyihao 已提交
59

dengyihao's avatar
dengyihao 已提交
60
  int64_t  refId;
dengyihao's avatar
dengyihao 已提交
61 62
  uint64_t st;
  int      sent;  //(0: no send, 1: alread sent)
dengyihao's avatar
dengyihao 已提交
63 64
} SCliMsg;

dengyihao's avatar
dengyihao 已提交
65
typedef struct SCliThrd {
dengyihao's avatar
dengyihao 已提交
66 67 68 69 70 71
  TdThread      thread;  // tid
  int64_t       pid;     // pid
  uv_loop_t*    loop;
  SAsyncPool*   asyncPool;
  uv_prepare_t* prepare;
  void*         pool;  // conn pool
dengyihao's avatar
dengyihao 已提交
72
  // timer handles
dengyihao's avatar
dengyihao 已提交
73
  SArray* timerList;
dengyihao's avatar
dengyihao 已提交
74
  // msg queue
dengyihao's avatar
dengyihao 已提交
75
  queue         msg;
wafwerar's avatar
wafwerar 已提交
76
  TdThreadMutex msgMtx;
dengyihao's avatar
dengyihao 已提交
77
  SDelayQueue*  delayQueue;
dengyihao's avatar
dengyihao 已提交
78
  SDelayQueue*  timeoutQueue;
dengyihao's avatar
dengyihao 已提交
79 80
  uint64_t      nextTimeout;  // next timeout
  void*         pTransInst;   //
dengyihao's avatar
dengyihao 已提交
81

dengyihao's avatar
dengyihao 已提交
82 83
  SHashObj* fqdn2ipCache;
  SCvtAddr  cvtAddr;
dengyihao's avatar
dengyihao 已提交
84

dengyihao's avatar
dengyihao 已提交
85 86
  SCliMsg* stopMsg;

dengyihao's avatar
dengyihao 已提交
87
  bool quit;
dengyihao's avatar
dengyihao 已提交
88
} SCliThrd;
dengyihao's avatar
dengyihao 已提交
89

U
ubuntu 已提交
90
typedef struct SCliObj {
dengyihao's avatar
dengyihao 已提交
91 92 93 94
  char       label[TSDB_LABEL_LEN];
  int32_t    index;
  int        numOfThreads;
  SCliThrd** pThreadObj;
U
ubuntu 已提交
95
} SCliObj;
dengyihao's avatar
dengyihao 已提交
96

dengyihao's avatar
dengyihao 已提交
97
// conn pool
dengyihao's avatar
dengyihao 已提交
98
// add expire timeout and capacity limit
dengyihao's avatar
dengyihao 已提交
99
static void*     createConnPool(int size);
100
static void*     destroyConnPool(void* pool);
dengyihao's avatar
dengyihao 已提交
101
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port);
dengyihao's avatar
dengyihao 已提交
102
static void      addConnToPool(void* pool, SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
103
static void      doCloseIdleConn(void* param);
dengyihao's avatar
dengyihao 已提交
104

dengyihao's avatar
dengyihao 已提交
105 106
// register timer for read
static void cliReadTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
107
// register timer in each thread to clear expire conn
dengyihao's avatar
dengyihao 已提交
108
// static void cliTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
109
// alloc buffer for recv
dengyihao's avatar
dengyihao 已提交
110
static FORCE_INLINE void cliAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
111
// callback after recv nbytes from socket
U
ubuntu 已提交
112
static void cliRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
113
// callback after send data to socket
U
ubuntu 已提交
114
static void cliSendCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
115
// callback after conn to server
U
ubuntu 已提交
116 117
static void cliConnCb(uv_connect_t* req, int status);
static void cliAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
118
static void cliIdleCb(uv_idle_t* handle);
dengyihao's avatar
dengyihao 已提交
119
static void cliPrepareCb(uv_prepare_t* handle);
dengyihao's avatar
dengyihao 已提交
120

dengyihao's avatar
dengyihao 已提交
121 122
static bool cliRecvReleaseReq(SCliConn* conn, STransMsgHead* pHead);

dengyihao's avatar
dengyihao 已提交
123
static int32_t allocConnRef(SCliConn* conn, bool update);
dengyihao's avatar
dengyihao 已提交
124

dengyihao's avatar
dengyihao 已提交
125
static int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg);
dengyihao's avatar
dengyihao 已提交
126

dengyihao's avatar
dengyihao 已提交
127
static SCliConn* cliCreateConn(SCliThrd* thrd);
U
ubuntu 已提交
128 129
static void      cliDestroyConn(SCliConn* pConn, bool clear /*clear tcp handle or not*/);
static void      cliDestroy(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
130
static void      cliSend(SCliConn* pConn);
dengyihao's avatar
dengyihao 已提交
131
static void      cliDestroyConnMsgs(SCliConn* conn, bool destroy);
dengyihao's avatar
dengyihao 已提交
132

dengyihao's avatar
dengyihao 已提交
133
// cli util func
dengyihao's avatar
dengyihao 已提交
134 135
static FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx);
static FORCE_INLINE void cliMayCvtFqdnToIp(SEpSet* pEpSet, SCvtAddr* pCvtAddr);
dengyihao's avatar
dengyihao 已提交
136

dengyihao's avatar
dengyihao 已提交
137
static FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* resp);
dengyihao's avatar
dengyihao 已提交
138

dengyihao's avatar
dengyihao 已提交
139 140 141
static FORCE_INLINE uint32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn);
static FORCE_INLINE void     cliUpdateFqdnCache(SHashObj* cache, char* fqdn);

dengyihao's avatar
dengyihao 已提交
142
// process data read from server, add decompress etc later
U
ubuntu 已提交
143
static void cliHandleResp(SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
144
// handle except about conn
U
ubuntu 已提交
145
static void cliHandleExcept(SCliConn* conn);
dengyihao's avatar
dengyihao 已提交
146

147
// handle req from app
dengyihao's avatar
dengyihao 已提交
148 149 150 151 152 153 154
static void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd);
static void cliHandleQuit(SCliMsg* pMsg, SCliThrd* pThrd);
static void cliHandleRelease(SCliMsg* pMsg, SCliThrd* pThrd);
static void cliHandleUpdate(SCliMsg* pMsg, SCliThrd* pThrd);
static void (*cliAsyncHandle[])(SCliMsg* pMsg, SCliThrd* pThrd) = {cliHandleReq, cliHandleQuit, cliHandleRelease, NULL,
                                                                   cliHandleUpdate};

dengyihao's avatar
dengyihao 已提交
155 156 157
static FORCE_INLINE void destroyUserdata(STransMsg* userdata);
static FORCE_INLINE void destroyCmsg(void* cmsg);
static FORCE_INLINE int  cliRBChoseIdx(STrans* pTransInst);
dengyihao's avatar
dengyihao 已提交
158
static FORCE_INLINE void transDestroyConnCtx(STransConnCtx* ctx);
dengyihao's avatar
dengyihao 已提交
159

dengyihao's avatar
dengyihao 已提交
160
// thread obj
dengyihao's avatar
dengyihao 已提交
161
static SCliThrd* createThrdObj(void* trans);
dengyihao's avatar
dengyihao 已提交
162
static void      destroyThrdObj(SCliThrd* pThrd);
dengyihao's avatar
dengyihao 已提交
163

dengyihao's avatar
dengyihao 已提交
164 165
static void cliWalkCb(uv_handle_t* handle, void* arg);

dengyihao's avatar
dengyihao 已提交
166 167
static void cliReleaseUnfinishedMsg(SCliConn* conn) {
  for (int i = 0; i < transQueueSize(&conn->cliMsgs); i++) {
dengyihao's avatar
dengyihao 已提交
168
    SCliMsg* msg = transQueueGet(&conn->cliMsgs, i);
dengyihao's avatar
dengyihao 已提交
169
    if (msg != NULL && msg->ctx != NULL && msg->ctx->ahandle != (void*)0x9527) {
dengyihao's avatar
dengyihao 已提交
170 171
      if (conn->ctx.freeFunc != NULL && msg->ctx->ahandle != NULL) {
        conn->ctx.freeFunc(msg->ctx->ahandle);
dengyihao's avatar
dengyihao 已提交
172 173
      }
    }
dengyihao's avatar
dengyihao 已提交
174
    destroyCmsg(msg);
dengyihao's avatar
dengyihao 已提交
175 176
  }
}
dengyihao's avatar
dengyihao 已提交
177 178 179 180 181 182 183
#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 已提交
184 185 186 187 188 189
// 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 已提交
190 191 192 193 194
#define CONN_HOST_THREAD_IDX1(idx, exh, refId, pThrd) \
  do {                                                \
    if (exh == NULL) {                                \
      idx = -1;                                       \
    } else {                                          \
dengyihao's avatar
dengyihao 已提交
195
      ASYNC_CHECK_HANDLE((exh), refId);               \
dengyihao's avatar
dengyihao 已提交
196
      pThrd = (SCliThrd*)(exh)->pThrd;                \
dengyihao's avatar
dengyihao 已提交
197 198
    }                                                 \
  } while (0)
dengyihao's avatar
dengyihao 已提交
199
#define CONN_PERSIST_TIME(para)    ((para) <= 90000 ? 90000 : (para))
dengyihao's avatar
dengyihao 已提交
200
#define CONN_GET_HOST_THREAD(conn) (conn ? ((SCliConn*)conn)->hostThrd : NULL)
dengyihao's avatar
dengyihao 已提交
201
#define CONN_GET_INST_LABEL(conn)  (((STrans*)(((SCliThrd*)(conn)->hostThrd)->pTransInst))->label)
dengyihao's avatar
dengyihao 已提交
202

dengyihao's avatar
dengyihao 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
#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);                            \
      if (pMsg->ctx != NULL && (uint64_t)pMsg->ctx->ahandle == ahandle) { \
        break;                                                            \
      }                                                                   \
    }                                                                     \
    if (i == sz) {                                                        \
      pMsg = NULL;                                                        \
      tDebug("msg not found, %" PRIu64 "", ahandle);                      \
    } else {                                                              \
      pMsg = transQueueRm(&conn->cliMsgs, i);                             \
      tDebug("msg found, %" PRIu64 "", ahandle);                          \
    }                                                                     \
dengyihao's avatar
dengyihao 已提交
219
  } while (0)
U
ubuntu 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
#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 已提交
233

dengyihao's avatar
dengyihao 已提交
234 235 236 237 238
#define CONN_HANDLE_THREAD_QUIT(thrd) \
  do {                                \
    if (thrd->quit) {                 \
      return;                         \
    }                                 \
dengyihao's avatar
dengyihao 已提交
239 240 241 242 243
  } while (0)

#define CONN_HANDLE_BROKEN(conn) \
  do {                           \
    if (conn->broken) {          \
dengyihao's avatar
formate  
dengyihao 已提交
244
      cliHandleExcept(conn);     \
dengyihao's avatar
dengyihao 已提交
245
      return;                    \
dengyihao's avatar
dengyihao 已提交
246
    }                            \
dengyihao's avatar
dengyihao 已提交
247
  } while (0)
dengyihao's avatar
dengyihao 已提交
248

dengyihao's avatar
formate  
dengyihao 已提交
249 250
#define CONN_SET_PERSIST_BY_APP(conn) \
  do {                                \
dengyihao's avatar
dengyihao 已提交
251 252
    if (conn->status == ConnNormal) { \
      conn->status = ConnAcquire;     \
dengyihao's avatar
formate  
dengyihao 已提交
253 254 255
      transRefCliHandle(conn);        \
    }                                 \
  } while (0)
256

dengyihao's avatar
dengyihao 已提交
257 258 259 260
#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)
261

S
Shengliang Guan 已提交
262 263
#define REQUEST_NO_RESP(msg)         ((msg)->info.noResp == 1)
#define REQUEST_PERSIS_HANDLE(msg)   ((msg)->info.persistHandle == 1)
dengyihao's avatar
dengyihao 已提交
264
#define REQUEST_RELEASE_HANDLE(cmsg) ((cmsg)->type == Release)
dengyihao's avatar
dengyihao 已提交
265

dengyihao's avatar
dengyihao 已提交
266
#define EPSET_IS_VALID(epSet)       ((epSet) != NULL && (epSet)->numOfEps != 0)
dengyihao's avatar
dengyihao 已提交
267
#define EPSET_GET_SIZE(epSet)       (epSet)->numOfEps
dengyihao's avatar
dengyihao 已提交
268
#define EPSET_GET_INUSE_IP(epSet)   ((epSet)->eps[(epSet)->inUse].fqdn)
dengyihao's avatar
dengyihao 已提交
269
#define EPSET_GET_INUSE_PORT(epSet) ((epSet)->eps[(epSet)->inUse].port)
dengyihao's avatar
dengyihao 已提交
270 271 272 273 274 275
#define EPSET_FORWARD_INUSE(epSet)                             \
  do {                                                         \
    if ((epSet)->numOfEps != 0) {                              \
      ++((epSet)->inUse);                                      \
      (epSet)->inUse = ((epSet)->inUse) % ((epSet)->numOfEps); \
    }                                                          \
dengyihao's avatar
dengyihao 已提交
276
  } while (0)
dengyihao's avatar
dengyihao 已提交
277

dengyihao's avatar
dengyihao 已提交
278 279 280 281 282 283 284 285 286 287 288
#define EPSET_DEBUG_STR(epSet, tbuf)                                                                                   \
  do {                                                                                                                 \
    int len = snprintf(tbuf, sizeof(tbuf), "epset:{");                                                                 \
    for (int i = 0; i < (epSet)->numOfEps; i++) {                                                                      \
      if (i == (epSet)->numOfEps - 1) {                                                                                \
        len += snprintf(tbuf + len, sizeof(tbuf) - len, "%d. %s:%d", i, (epSet)->eps[i].fqdn, (epSet)->eps[i].port);   \
      } else {                                                                                                         \
        len += snprintf(tbuf + len, sizeof(tbuf) - len, "%d. %s:%d, ", i, (epSet)->eps[i].fqdn, (epSet)->eps[i].port); \
      }                                                                                                                \
    }                                                                                                                  \
    len += snprintf(tbuf + len, sizeof(tbuf) - len, "}, inUse:%d", (epSet)->inUse);                                    \
dengyihao's avatar
dengyihao 已提交
289
  } while (0);
dengyihao's avatar
dengyihao 已提交
290

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

dengyihao's avatar
dengyihao 已提交
293
bool cliMaySendCachedMsg(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
294
  if (!transQueueEmpty(&conn->cliMsgs)) {
dengyihao's avatar
dengyihao 已提交
295
    SCliMsg* pCliMsg = NULL;
U
ubuntu 已提交
296
    CONN_GET_NEXT_SENDMSG(conn);
dengyihao's avatar
dengyihao 已提交
297 298 299 300 301 302
    if (pCliMsg == NULL)
      return false;
    else {
      cliSend(conn);
      return true;
    }
dengyihao's avatar
dengyihao 已提交
303
  }
dengyihao's avatar
dengyihao 已提交
304
  return false;
U
ubuntu 已提交
305 306
_RETURN:
  return false;
dengyihao's avatar
dengyihao 已提交
307
}
dengyihao's avatar
formate  
dengyihao 已提交
308
void cliHandleResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
309 310
  SCliThrd* pThrd = conn->hostThrd;
  STrans*   pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
311

dengyihao's avatar
dengyihao 已提交
312 313 314 315 316 317
  if (conn->timer) {
    if (uv_is_active((uv_handle_t*)conn->timer)) {
      tDebug("%s conn %p stop timer", CONN_GET_INST_LABEL(conn), conn);
      uv_timer_stop(conn->timer);
    }
    taosArrayPush(pThrd->timerList, &conn->timer);
dengyihao's avatar
dengyihao 已提交
318
    conn->timer->data = NULL;
dengyihao's avatar
dengyihao 已提交
319
    conn->timer = NULL;
dengyihao's avatar
dengyihao 已提交
320 321
  }

dengyihao's avatar
opt rpc  
dengyihao 已提交
322
  STransMsgHead* pHead = NULL;
dengyihao's avatar
dengyihao 已提交
323 324 325

  int32_t msgLen = transDumpFromBuffer(&conn->readBuf, (char**)&pHead);
  if (msgLen <= 0) {
dengyihao's avatar
dengyihao 已提交
326 327 328
    tDebug("%s conn %p recv invalid packet ", CONN_GET_INST_LABEL(conn), conn);
    return;
  }
dengyihao's avatar
dengyihao 已提交
329 330 331 332

  if (transDecompressMsg((char**)&pHead, msgLen) < 0) {
    tDebug("%s conn %p recv invalid packet, failed to decompress", CONN_GET_INST_LABEL(conn), conn);
  }
dengyihao's avatar
dengyihao 已提交
333 334
  pHead->code = htonl(pHead->code);
  pHead->msgLen = htonl(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
335 336 337 338
  if (cliRecvReleaseReq(conn, pHead)) {
    return;
  }

dengyihao's avatar
dengyihao 已提交
339 340 341 342 343
  STransMsg transMsg = {0};
  transMsg.contLen = transContLenFromMsg(pHead->msgLen);
  transMsg.pCont = transContFromHead((char*)pHead);
  transMsg.code = pHead->code;
  transMsg.msgType = pHead->msgType;
S
Shengliang Guan 已提交
344
  transMsg.info.ahandle = NULL;
dengyihao's avatar
dengyihao 已提交
345
  transMsg.info.traceId = pHead->traceId;
dengyihao's avatar
dengyihao 已提交
346
  transMsg.info.hasEpSet = pHead->hasEpSet;
dengyihao's avatar
dengyihao 已提交
347

dengyihao's avatar
dengyihao 已提交
348 349 350 351
  SCliMsg*       pMsg = NULL;
  STransConnCtx* pCtx = NULL;
  if (CONN_NO_PERSIST_BY_APP(conn)) {
    pMsg = transQueuePop(&conn->cliMsgs);
dengyihao's avatar
dengyihao 已提交
352 353 354

    pCtx = pMsg ? pMsg->ctx : NULL;
    transMsg.info.ahandle = pCtx ? pCtx->ahandle : NULL;
dengyihao's avatar
dengyihao 已提交
355
    tDebug("%s conn %p get ahandle %p, persist: 0", CONN_GET_INST_LABEL(conn), conn, transMsg.info.ahandle);
U
ubuntu 已提交
356
  } else {
dengyihao's avatar
dengyihao 已提交
357 358 359
    uint64_t ahandle = (uint64_t)pHead->ahandle;
    CONN_GET_MSGCTX_BY_AHANDLE(conn, ahandle);
    if (pMsg == NULL) {
S
Shengliang Guan 已提交
360
      transMsg.info.ahandle = transCtxDumpVal(&conn->ctx, transMsg.msgType);
dengyihao's avatar
dengyihao 已提交
361 362
      tDebug("%s conn %p construct ahandle %p by %s, persist: 1", CONN_GET_INST_LABEL(conn), conn,
             transMsg.info.ahandle, TMSG_INFO(transMsg.msgType));
S
Shengliang Guan 已提交
363
      if (!CONN_RELEASE_BY_SERVER(conn) && transMsg.info.ahandle == NULL) {
D
dapan1121 已提交
364
        transMsg.code = TSDB_CODE_RPC_BROKEN_LINK;
S
Shengliang Guan 已提交
365
        transMsg.info.ahandle = transCtxDumpBrokenlinkVal(&conn->ctx, (int32_t*)&(transMsg.msgType));
dengyihao's avatar
dengyihao 已提交
366 367
        tDebug("%s conn %p construct ahandle %p due brokenlink, persist: 1", CONN_GET_INST_LABEL(conn), conn,
               transMsg.info.ahandle);
dengyihao's avatar
dengyihao 已提交
368 369
      }
    } else {
dengyihao's avatar
dengyihao 已提交
370
      pCtx = pMsg->ctx;
S
Shengliang Guan 已提交
371
      transMsg.info.ahandle = pCtx ? pCtx->ahandle : NULL;
dengyihao's avatar
dengyihao 已提交
372
      tDebug("%s conn %p get ahandle %p, persist: 1", CONN_GET_INST_LABEL(conn), conn, transMsg.info.ahandle);
dengyihao's avatar
dengyihao 已提交
373
    }
U
ubuntu 已提交
374
  }
dengyihao's avatar
dengyihao 已提交
375
  // buf's mem alread translated to transMsg.pCont
dengyihao's avatar
dengyihao 已提交
376
  if (!CONN_NO_PERSIST_BY_APP(conn)) {
dengyihao's avatar
dengyihao 已提交
377
    transMsg.info.handle = (void*)conn->refId;
dengyihao's avatar
dengyihao 已提交
378
    tDebug("%s conn %p ref by app", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
379
  }
dengyihao's avatar
dengyihao 已提交
380

dengyihao's avatar
dengyihao 已提交
381
  STraceId* trace = &transMsg.info.traceId;
dengyihao's avatar
dengyihao 已提交
382
  tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, code str:%s", CONN_GET_INST_LABEL(conn), conn,
dengyihao's avatar
dengyihao 已提交
383
          TMSG_INFO(pHead->msgType), conn->dst, conn->src, msgLen, tstrerror(transMsg.code));
dengyihao's avatar
dengyihao 已提交
384

dengyihao's avatar
dengyihao 已提交
385
  if (pCtx == NULL && CONN_NO_PERSIST_BY_APP(conn)) {
dengyihao's avatar
dengyihao 已提交
386
    tDebug("%s except, conn %p read while cli ignore it", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
387
    transFreeMsg(transMsg.pCont);
dengyihao's avatar
dengyihao 已提交
388 389
    return;
  }
S
Shengliang Guan 已提交
390
  if (CONN_RELEASE_BY_SERVER(conn) && transMsg.info.ahandle == NULL) {
dengyihao's avatar
dengyihao 已提交
391
    tDebug("%s except, conn %p read while cli ignore it", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
392
    transFreeMsg(transMsg.pCont);
dengyihao's avatar
dengyihao 已提交
393 394
    return;
  }
dengyihao's avatar
dengyihao 已提交
395

dengyihao's avatar
dengyihao 已提交
396
  if (pMsg == NULL || (pMsg && pMsg->type != Release)) {
dengyihao's avatar
dengyihao 已提交
397 398 399
    if (cliAppCb(conn, &transMsg, pMsg) != 0) {
      return;
    }
dengyihao's avatar
dengyihao 已提交
400
  }
dengyihao's avatar
dengyihao 已提交
401 402
  destroyCmsg(pMsg);

dengyihao's avatar
dengyihao 已提交
403
  if (cliMaySendCachedMsg(conn) == true) {
dengyihao's avatar
dengyihao 已提交
404 405
    return;
  }
dengyihao's avatar
dengyihao 已提交
406

U
ubuntu 已提交
407
  if (CONN_NO_PERSIST_BY_APP(conn)) {
dengyihao's avatar
dengyihao 已提交
408
    return addConnToPool(pThrd->pool, conn);
dengyihao's avatar
dengyihao 已提交
409
  }
dengyihao's avatar
test  
dengyihao 已提交
410

dengyihao's avatar
dengyihao 已提交
411
  uv_read_start((uv_stream_t*)conn->stream, cliAllocRecvBufferCb, cliRecvCb);
dengyihao's avatar
dengyihao 已提交
412
}
U
ubuntu 已提交
413

dengyihao's avatar
dengyihao 已提交
414
void cliHandleExceptImpl(SCliConn* pConn, int32_t code) {
dengyihao's avatar
dengyihao 已提交
415
  if (transQueueEmpty(&pConn->cliMsgs)) {
dengyihao's avatar
dengyihao 已提交
416
    if (pConn->broken == true && CONN_NO_PERSIST_BY_APP(pConn)) {
dengyihao's avatar
dengyihao 已提交
417
      tTrace("%s conn %p handle except, persist:0", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
formate  
dengyihao 已提交
418 419 420
      transUnrefCliHandle(pConn);
      return;
    }
dengyihao's avatar
dengyihao 已提交
421
  }
dengyihao's avatar
dengyihao 已提交
422 423 424
  SCliThrd* pThrd = pConn->hostThrd;
  STrans*   pTransInst = pThrd->pTransInst;
  bool      once = false;
D
dapan1121 已提交
425
  do {
dengyihao's avatar
dengyihao 已提交
426
    SCliMsg* pMsg = transQueuePop(&pConn->cliMsgs);
D
dapan1121 已提交
427 428
    if (pMsg == NULL && once) {
      break;
dengyihao's avatar
dengyihao 已提交
429
    }
dengyihao's avatar
dengyihao 已提交
430
    STransConnCtx* pCtx = pMsg ? pMsg->ctx : NULL;
U
ubuntu 已提交
431

dengyihao's avatar
dengyihao 已提交
432
    STransMsg transMsg = {0};
dengyihao's avatar
dengyihao 已提交
433
    transMsg.code = code == -1 ? (pConn->broken ? TSDB_CODE_RPC_BROKEN_LINK : TSDB_CODE_RPC_NETWORK_UNAVAIL) : code;
dengyihao's avatar
dengyihao 已提交
434
    transMsg.msgType = pMsg ? pMsg->msg.msgType + 1 : 0;
S
Shengliang Guan 已提交
435
    transMsg.info.ahandle = NULL;
dengyihao's avatar
dengyihao 已提交
436

dengyihao's avatar
dengyihao 已提交
437
    if (pMsg == NULL && !CONN_NO_PERSIST_BY_APP(pConn)) {
S
Shengliang Guan 已提交
438
      transMsg.info.ahandle = transCtxDumpVal(&pConn->ctx, transMsg.msgType);
dengyihao's avatar
dengyihao 已提交
439
      tDebug("%s conn %p construct ahandle %p by %s", CONN_GET_INST_LABEL(pConn), pConn, transMsg.info.ahandle,
dengyihao's avatar
dengyihao 已提交
440
             TMSG_INFO(transMsg.msgType));
S
Shengliang Guan 已提交
441 442
      if (transMsg.info.ahandle == NULL) {
        transMsg.info.ahandle = transCtxDumpBrokenlinkVal(&pConn->ctx, (int32_t*)&(transMsg.msgType));
dengyihao's avatar
dengyihao 已提交
443
        tDebug("%s conn %p construct ahandle %p due to brokenlink", CONN_GET_INST_LABEL(pConn), pConn,
S
Shengliang Guan 已提交
444
               transMsg.info.ahandle);
dengyihao's avatar
dengyihao 已提交
445
      }
dengyihao's avatar
dengyihao 已提交
446
    } else {
dengyihao's avatar
dengyihao 已提交
447
      transMsg.info.ahandle = (pMsg->type != Release && pCtx) ? pCtx->ahandle : NULL;
dengyihao's avatar
dengyihao 已提交
448 449 450
    }

    if (pCtx == NULL || pCtx->pSem == NULL) {
S
Shengliang Guan 已提交
451
      if (transMsg.info.ahandle == NULL) {
dengyihao's avatar
dengyihao 已提交
452
        if (REQUEST_NO_RESP(&pMsg->msg) || pMsg->type == Release) destroyCmsg(pMsg);
D
dapan1121 已提交
453
        once = true;
U
ubuntu 已提交
454 455
        continue;
      }
dengyihao's avatar
dengyihao 已提交
456
    }
dengyihao's avatar
dengyihao 已提交
457
    if (pMsg == NULL || (pMsg && pMsg->type != Release)) {
dengyihao's avatar
dengyihao 已提交
458 459 460
      if (cliAppCb(pConn, &transMsg, pMsg) != 0) {
        return;
      }
dengyihao's avatar
dengyihao 已提交
461 462
    }
    destroyCmsg(pMsg);
dengyihao's avatar
dengyihao 已提交
463
    tTrace("%s conn %p start to destroy, ref:%d", CONN_GET_INST_LABEL(pConn), pConn, T_REF_VAL_GET(pConn));
D
dapan1121 已提交
464
  } while (!transQueueEmpty(&pConn->cliMsgs));
dengyihao's avatar
dengyihao 已提交
465
  transUnrefCliHandle(pConn);
dengyihao's avatar
dengyihao 已提交
466
}
dengyihao's avatar
dengyihao 已提交
467
void cliHandleExcept(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
468
  tTrace("%s conn %p except ref:%d", CONN_GET_INST_LABEL(conn), conn, T_REF_VAL_GET(conn));
dengyihao's avatar
dengyihao 已提交
469 470
  cliHandleExceptImpl(conn, -1);
}
dengyihao's avatar
dengyihao 已提交
471

dengyihao's avatar
dengyihao 已提交
472 473 474 475
void cliReadTimeoutCb(uv_timer_t* handle) {
  // set up timeout cb
  SCliConn* conn = handle->data;
  tTrace("%s conn %p timeout, ref:%d", CONN_GET_INST_LABEL(conn), conn, T_REF_VAL_GET(conn));
dengyihao's avatar
dengyihao 已提交
476
  uv_read_stop(conn->stream);
dengyihao's avatar
dengyihao 已提交
477 478
  cliHandleExceptImpl(conn, TSDB_CODE_RPC_TIMEOUT);
}
U
ubuntu 已提交
479 480

void* createConnPool(int size) {
481 482
  // thread local, no lock
  return taosHashInit(size, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
483
}
U
ubuntu 已提交
484
void* destroyConnPool(void* pool) {
dengyihao's avatar
dengyihao 已提交
485
  SConnList* connList = taosHashIterate((SHashObj*)pool, NULL);
dengyihao's avatar
dengyihao 已提交
486
  while (connList != NULL) {
dengyihao's avatar
dengyihao 已提交
487 488
    while (!QUEUE_IS_EMPTY(&connList->conns)) {
      queue*    h = QUEUE_HEAD(&connList->conns);
dengyihao's avatar
dengyihao 已提交
489
      SCliConn* c = QUEUE_DATA(h, SCliConn, q);
U
ubuntu 已提交
490
      cliDestroyConn(c, true);
dengyihao's avatar
dengyihao 已提交
491
    }
dengyihao's avatar
dengyihao 已提交
492
    connList = taosHashIterate((SHashObj*)pool, connList);
dengyihao's avatar
dengyihao 已提交
493
  }
dengyihao's avatar
dengyihao 已提交
494
  taosHashCleanup(pool);
495
  return NULL;
dengyihao's avatar
dengyihao 已提交
496 497
}

dengyihao's avatar
dengyihao 已提交
498
static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) {
dengyihao's avatar
dengyihao 已提交
499
  char key[32] = {0};
dengyihao's avatar
dengyihao 已提交
500
  CONN_CONSTRUCT_HASH_KEY(key, ip, port);
dengyihao's avatar
dengyihao 已提交
501 502

  SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
503
  if (plist == NULL) {
dengyihao's avatar
dengyihao 已提交
504
    SConnList list = {0};
dengyihao's avatar
dengyihao 已提交
505 506
    taosHashPut((SHashObj*)pool, key, strlen(key), (void*)&list, sizeof(list));
    plist = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
507
    if (plist == NULL) return NULL;
dengyihao's avatar
dengyihao 已提交
508
    QUEUE_INIT(&plist->conns);
dengyihao's avatar
dengyihao 已提交
509 510
  }

dengyihao's avatar
dengyihao 已提交
511
  if (QUEUE_IS_EMPTY(&plist->conns)) {
dengyihao's avatar
dengyihao 已提交
512 513
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
514 515

  plist->size -= 1;
dengyihao's avatar
dengyihao 已提交
516
  queue*    h = QUEUE_HEAD(&plist->conns);
dengyihao's avatar
dengyihao 已提交
517
  SCliConn* conn = QUEUE_DATA(h, SCliConn, q);
dengyihao's avatar
dengyihao 已提交
518
  conn->status = ConnNormal;
dengyihao's avatar
dengyihao 已提交
519 520
  QUEUE_REMOVE(&conn->q);
  QUEUE_INIT(&conn->q);
dengyihao's avatar
dengyihao 已提交
521

dengyihao's avatar
dengyihao 已提交
522 523 524 525
  if (conn->task != NULL) {
    transDQCancel(((SCliThrd*)conn->hostThrd)->timeoutQueue, conn->task);
    conn->task = NULL;
  }
dengyihao's avatar
dengyihao 已提交
526
  return conn;
dengyihao's avatar
dengyihao 已提交
527
}
dengyihao's avatar
dengyihao 已提交
528 529 530 531 532 533
static void addConnToPool(void* pool, SCliConn* conn) {
  if (conn->status == ConnInPool) {
    return;
  }
  allocConnRef(conn, true);

dengyihao's avatar
dengyihao 已提交
534
  SCliThrd* thrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
535 536 537 538 539 540
  if (conn->timer != NULL) {
    uv_timer_stop(conn->timer);
    taosArrayPush(thrd->timerList, &conn->timer);
    conn->timer->data = NULL;
    conn->timer = NULL;
  }
dengyihao's avatar
dengyihao 已提交
541 542 543 544 545
  if (T_REF_VAL_GET(conn) > 1) {
    transUnrefCliHandle(conn);
  }

  cliDestroyConnMsgs(conn, false);
dengyihao's avatar
dengyihao 已提交
546

dengyihao's avatar
dengyihao 已提交
547 548 549 550 551 552 553
  conn->status = ConnInPool;

  if (conn->list == NULL) {
    char key[32] = {0};
    CONN_CONSTRUCT_HASH_KEY(key, conn->ip, conn->port);
    tTrace("%s conn %p added to conn pool, read buf cap:%d", CONN_GET_INST_LABEL(conn), conn, conn->readBuf.cap);
    conn->list = taosHashGet((SHashObj*)pool, key, strlen(key));
dengyihao's avatar
dengyihao 已提交
554 555
  } else {
    tTrace("%s conn %p added to conn pool, read buf cap:%d", CONN_GET_INST_LABEL(conn), conn, conn->readBuf.cap);
dengyihao's avatar
dengyihao 已提交
556
  }
dengyihao's avatar
dengyihao 已提交
557
  QUEUE_PUSH(&conn->list->conns, &conn->q);
dengyihao's avatar
dengyihao 已提交
558
  conn->list->size += 1;
dengyihao's avatar
dengyihao 已提交
559

dengyihao's avatar
dengyihao 已提交
560
  if (conn->list->size >= 50) {
dengyihao's avatar
dengyihao 已提交
561 562
    STaskArg* arg = taosMemoryCalloc(1, sizeof(STaskArg));
    arg->param1 = conn;
dengyihao's avatar
dengyihao 已提交
563
    arg->param2 = NULL;
dengyihao's avatar
dengyihao 已提交
564 565

    STrans* pTransInst = thrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
566 567
    conn->task = transDQSched(thrd->timeoutQueue, doCloseIdleConn, arg, CONN_PERSIST_TIME(pTransInst->idleTime));
  }
dengyihao's avatar
dengyihao 已提交
568
}
dengyihao's avatar
dengyihao 已提交
569
static int32_t allocConnRef(SCliConn* conn, bool update) {
dengyihao's avatar
dengyihao 已提交
570
  if (update) {
dengyihao's avatar
dengyihao 已提交
571
    transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
572
    conn->refId = -1;
dengyihao's avatar
dengyihao 已提交
573 574 575 576
  }
  SExHandle* exh = taosMemoryCalloc(1, sizeof(SExHandle));
  exh->handle = conn;
  exh->pThrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
577
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
dengyihao's avatar
dengyihao 已提交
578
  conn->refId = exh->refId;
dengyihao's avatar
dengyihao 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  return 0;
}

static int32_t specifyConnRef(SCliConn* conn, bool update, int64_t handle) {
  if (update) {
    transRemoveExHandle(transGetRefMgt(), conn->refId);
    conn->refId = -1;
  }
  SExHandle* exh = transAcquireExHandle(transGetRefMgt(), handle);
  if (exh == NULL) {
    return -1;
  }
  exh->handle = conn;
  exh->pThrd = conn->hostThrd;
  conn->refId = exh->refId;

  transReleaseExHandle(transGetRefMgt(), handle);
  return 0;
dengyihao's avatar
dengyihao 已提交
597
}
dengyihao's avatar
dengyihao 已提交
598

dengyihao's avatar
dengyihao 已提交
599
static void cliAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
600
  SCliConn*    conn = handle->data;
dengyihao's avatar
dengyihao 已提交
601
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
602
  tDebug("%s conn %p alloc read buf", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
603
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
604
}
U
ubuntu 已提交
605
static void cliRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
606
  // impl later
dengyihao's avatar
dengyihao 已提交
607 608 609
  if (handle->data == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
610 611
  SCliConn*    conn = handle->data;
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
612
  if (nread > 0) {
dengyihao's avatar
dengyihao 已提交
613
    pBuf->len += nread;
dengyihao's avatar
dengyihao 已提交
614
    while (transReadComplete(pBuf)) {
dengyihao's avatar
dengyihao 已提交
615
      tDebug("%s conn %p read complete", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
616 617 618 619 620 621
      if (pBuf->invalid) {
        cliHandleExcept(conn);
        break;
      } else {
        cliHandleResp(conn);
      }
dengyihao's avatar
dengyihao 已提交
622
    }
dengyihao's avatar
dengyihao 已提交
623 624
    return;
  }
dengyihao's avatar
dengyihao 已提交
625

626 627
  assert(nread <= 0);
  if (nread == 0) {
dengyihao's avatar
dengyihao 已提交
628 629 630
    // 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 已提交
631
    tTrace("%s conn %p read empty", CONN_GET_INST_LABEL(conn), conn);
632 633
    return;
  }
dengyihao's avatar
fix bug  
dengyihao 已提交
634
  if (nread < 0) {
dengyihao's avatar
dengyihao 已提交
635
    tWarn("%s conn %p read error:%s, ref:%d", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread), T_REF_VAL_GET(conn));
dengyihao's avatar
dengyihao 已提交
636
    conn->broken = true;
U
ubuntu 已提交
637
    cliHandleExcept(conn);
dengyihao's avatar
dengyihao 已提交
638
  }
dengyihao's avatar
dengyihao 已提交
639
}
dengyihao's avatar
dengyihao 已提交
640

dengyihao's avatar
dengyihao 已提交
641
static SCliConn* cliCreateConn(SCliThrd* pThrd) {
wafwerar's avatar
wafwerar 已提交
642
  SCliConn* conn = taosMemoryCalloc(1, sizeof(SCliConn));
dengyihao's avatar
dengyihao 已提交
643
  // read/write stream handle
wafwerar's avatar
wafwerar 已提交
644
  conn->stream = (uv_stream_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
645 646 647 648
  uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
  conn->stream->data = conn;

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

dengyihao's avatar
dengyihao 已提交
650 651
  transReqQueueInit(&conn->wreqQueue);

dengyihao's avatar
dengyihao 已提交
652
  transQueueInit(&conn->cliMsgs, NULL);
dengyihao's avatar
opt rpc  
dengyihao 已提交
653 654

  transInitBuffer(&conn->readBuf);
dengyihao's avatar
dengyihao 已提交
655
  QUEUE_INIT(&conn->q);
dengyihao's avatar
dengyihao 已提交
656
  conn->hostThrd = pThrd;
dengyihao's avatar
dengyihao 已提交
657 658
  conn->status = ConnNormal;
  conn->broken = 0;
dengyihao's avatar
dengyihao 已提交
659
  transRefCliHandle(conn);
dengyihao's avatar
dengyihao 已提交
660

dengyihao's avatar
dengyihao 已提交
661
  allocConnRef(conn, false);
dengyihao's avatar
dengyihao 已提交
662

dengyihao's avatar
dengyihao 已提交
663 664
  return conn;
}
U
ubuntu 已提交
665
static void cliDestroyConn(SCliConn* conn, bool clear) {
dengyihao's avatar
dengyihao 已提交
666
  SCliThrd* pThrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
667
  tTrace("%s conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
668 669
  QUEUE_REMOVE(&conn->q);
  QUEUE_INIT(&conn->q);
dengyihao's avatar
dengyihao 已提交
670
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
opt rpc  
dengyihao 已提交
671
  conn->refId = -1;
dengyihao's avatar
dengyihao 已提交
672

dengyihao's avatar
dengyihao 已提交
673 674 675 676 677 678 679 680 681 682
  if (conn->task != NULL) {
    transDQCancel(pThrd->timeoutQueue, conn->task);
    conn->task = NULL;
  }
  if (conn->timer != NULL) {
    uv_timer_stop(conn->timer);
    taosArrayPush(pThrd->timerList, &conn->timer);
    conn->timer->data = NULL;
    conn->timer = NULL;
  }
dengyihao's avatar
dengyihao 已提交
683

dengyihao's avatar
dengyihao 已提交
684
  if (clear) {
dengyihao's avatar
dengyihao 已提交
685
    if (!uv_is_closing((uv_handle_t*)conn->stream)) {
dengyihao's avatar
dengyihao 已提交
686
      uv_read_stop(conn->stream);
dengyihao's avatar
dengyihao 已提交
687 688
      uv_close((uv_handle_t*)conn->stream, cliDestroy);
    }
689
  }
dengyihao's avatar
dengyihao 已提交
690
}
U
ubuntu 已提交
691
static void cliDestroy(uv_handle_t* handle) {
dengyihao's avatar
dengyihao 已提交
692 693 694
  if (uv_handle_get_type(handle) != UV_TCP || handle->data == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
695
  SCliConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
696
  SCliThrd* pThrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
697 698
  if (conn->timer != NULL) {
    uv_timer_stop(conn->timer);
dengyihao's avatar
dengyihao 已提交
699 700
    taosArrayPush(pThrd->timerList, &conn->timer);
    conn->timer->data = NULL;
dengyihao's avatar
dengyihao 已提交
701 702
    conn->timer = NULL;
  }
dengyihao's avatar
dengyihao 已提交
703

dengyihao's avatar
dengyihao 已提交
704
  transRemoveExHandle(transGetRefMgt(), conn->refId);
wafwerar's avatar
wafwerar 已提交
705 706
  taosMemoryFree(conn->ip);
  taosMemoryFree(conn->stream);
dengyihao's avatar
dengyihao 已提交
707 708 709

  cliDestroyConnMsgs(conn, true);

dengyihao's avatar
dengyihao 已提交
710
  tTrace("%s conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
711
  transReqQueueClear(&conn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
712
  transDestroyBuffer(&conn->readBuf);
wafwerar's avatar
wafwerar 已提交
713
  taosMemoryFree(conn);
dengyihao's avatar
dengyihao 已提交
714
}
dengyihao's avatar
dengyihao 已提交
715
static bool cliHandleNoResp(SCliConn* conn) {
dengyihao's avatar
dengyihao 已提交
716 717
  bool res = false;
  if (!transQueueEmpty(&conn->cliMsgs)) {
dengyihao's avatar
dengyihao 已提交
718
    SCliMsg* pMsg = transQueueGet(&conn->cliMsgs, 0);
dengyihao's avatar
dengyihao 已提交
719
    if (REQUEST_NO_RESP(&pMsg->msg)) {
dengyihao's avatar
dengyihao 已提交
720
      transQueuePop(&conn->cliMsgs);
dengyihao's avatar
dengyihao 已提交
721 722 723 724 725
      destroyCmsg(pMsg);
      res = true;
    }
    if (res == true) {
      if (cliMaySendCachedMsg(conn) == false) {
dengyihao's avatar
dengyihao 已提交
726
        SCliThrd* thrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
727
        addConnToPool(thrd->pool, conn);
dengyihao's avatar
dengyihao 已提交
728 729 730
        res = false;
      } else {
        res = true;
dengyihao's avatar
dengyihao 已提交
731 732 733 734 735
      }
    }
  }
  return res;
}
U
ubuntu 已提交
736
static void cliSendCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
737 738
  SCliConn* pConn = transReqQueueRemove(req);
  if (pConn == NULL) return;
dengyihao's avatar
dengyihao 已提交
739

dengyihao's avatar
dengyihao 已提交
740
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
741
    tTrace("%s conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
742
  } else {
S
Shengliang Guan 已提交
743
    tError("%s conn %p failed to write:%s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status));
U
ubuntu 已提交
744
    cliHandleExcept(pConn);
dengyihao's avatar
dengyihao 已提交
745 746
    return;
  }
dengyihao's avatar
dengyihao 已提交
747
  if (cliHandleNoResp(pConn) == true) {
dengyihao's avatar
dengyihao 已提交
748
    tTrace("%s conn %p no resp required", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
749 750
    return;
  }
dengyihao's avatar
dengyihao 已提交
751
  uv_read_start((uv_stream_t*)pConn->stream, cliAllocRecvBufferCb, cliRecvCb);
dengyihao's avatar
dengyihao 已提交
752
}
dengyihao's avatar
dengyihao 已提交
753

U
ubuntu 已提交
754
void cliSend(SCliConn* pConn) {
dengyihao's avatar
dengyihao 已提交
755
  assert(!transQueueEmpty(&pConn->cliMsgs));
dengyihao's avatar
dengyihao 已提交
756 757

  SCliMsg* pCliMsg = NULL;
U
ubuntu 已提交
758
  CONN_GET_NEXT_SENDMSG(pConn);
dengyihao's avatar
dengyihao 已提交
759 760
  pCliMsg->sent = 1;

dengyihao's avatar
dengyihao 已提交
761
  STransConnCtx* pCtx = pCliMsg->ctx;
dengyihao's avatar
dengyihao 已提交
762

dengyihao's avatar
dengyihao 已提交
763 764
  SCliThrd* pThrd = pConn->hostThrd;
  STrans*   pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
765

U
ubuntu 已提交
766
  STransMsg* pMsg = (STransMsg*)(&pCliMsg->msg);
dengyihao's avatar
dengyihao 已提交
767 768 769 770
  if (pMsg->pCont == 0) {
    pMsg->pCont = (void*)rpcMallocCont(0);
    pMsg->contLen = 0;
  }
dengyihao's avatar
dengyihao 已提交
771

dengyihao's avatar
dengyihao 已提交
772
  int            msgLen = transMsgLenFromCont(pMsg->contLen);
dengyihao's avatar
dengyihao 已提交
773 774
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
  pHead->ahandle = pCtx != NULL ? (uint64_t)pCtx->ahandle : 0;
dengyihao's avatar
dengyihao 已提交
775 776
  pHead->noResp = REQUEST_NO_RESP(pMsg) ? 1 : 0;
  pHead->persist = REQUEST_PERSIS_HANDLE(pMsg) ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
777 778
  pHead->msgType = pMsg->msgType;
  pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);
dengyihao's avatar
dengyihao 已提交
779
  pHead->release = REQUEST_RELEASE_HANDLE(pCliMsg) ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
780
  memcpy(pHead->user, pTransInst->user, strlen(pTransInst->user));
dengyihao's avatar
dengyihao 已提交
781
  pHead->traceId = pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
782
  pHead->magicNum = htonl(TRANS_MAGIC_NUM);
dengyihao's avatar
dengyihao 已提交
783 784 785
  if (pHead->persist == 1) {
    CONN_SET_PERSIST_BY_APP(pConn);
  }
dengyihao's avatar
dengyihao 已提交
786

dengyihao's avatar
dengyihao 已提交
787
  STraceId* trace = &pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
788

dengyihao's avatar
dengyihao 已提交
789
  if (pTransInst->startTimer != NULL && pTransInst->startTimer(0, pMsg->msgType)) {
dengyihao's avatar
dengyihao 已提交
790
    uv_timer_t* timer = taosArrayGetSize(pThrd->timerList) > 0 ? *(uv_timer_t**)taosArrayPop(pThrd->timerList) : NULL;
dengyihao's avatar
dengyihao 已提交
791 792
    if (timer == NULL) {
      timer = taosMemoryCalloc(1, sizeof(uv_timer_t));
dengyihao's avatar
dengyihao 已提交
793
      tDebug("no available timer, create a timer %p", timer);
dengyihao's avatar
dengyihao 已提交
794 795 796 797 798
      uv_timer_init(pThrd->loop, timer);
    }
    timer->data = pConn;
    pConn->timer = timer;

dengyihao's avatar
dengyihao 已提交
799 800 801
    tGTrace("%s conn %p start timer for msg:%s", CONN_GET_INST_LABEL(pConn), pConn, TMSG_INFO(pMsg->msgType));
    uv_timer_start((uv_timer_t*)pConn->timer, cliReadTimeoutCb, TRANS_READ_TIMEOUT, 0);
  }
dengyihao's avatar
dengyihao 已提交
802

dengyihao's avatar
dengyihao 已提交
803
  if (pTransInst->compressSize != -1 && pTransInst->compressSize < pMsg->contLen) {
dengyihao's avatar
dengyihao 已提交
804 805 806
    msgLen = transCompressMsg(pMsg->pCont, pMsg->contLen) + sizeof(STransMsgHead);
    pHead->msgLen = (int32_t)htonl((uint32_t)msgLen);
  }
dengyihao's avatar
dengyihao 已提交
807 808
  tGDebug("%s conn %p %s is sent to %s, local info %s, len:%d", CONN_GET_INST_LABEL(pConn), pConn,
          TMSG_INFO(pHead->msgType), pConn->dst, pConn->src, msgLen);
dengyihao's avatar
dengyihao 已提交
809

dengyihao's avatar
dengyihao 已提交
810
  uv_buf_t    wb = uv_buf_init((char*)pHead, msgLen);
dengyihao's avatar
dengyihao 已提交
811
  uv_write_t* req = transReqQueuePush(&pConn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
812 813 814 815 816 817 818

  int status = uv_write(req, (uv_stream_t*)pConn->stream, &wb, 1, cliSendCb);
  if (status != 0) {
    tGError("%s conn %p failed to sent msg:%s, errmsg:%s", CONN_GET_INST_LABEL(pConn), pConn, TMSG_INFO(pMsg->msgType),
            uv_err_name(status));
    cliHandleExcept(pConn);
  }
U
ubuntu 已提交
819 820
  return;
_RETURN:
dengyihao's avatar
dengyihao 已提交
821
  return;
dengyihao's avatar
dengyihao 已提交
822
}
U
ubuntu 已提交
823 824

void cliConnCb(uv_connect_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
825
  // impl later
dengyihao's avatar
dengyihao 已提交
826 827
  SCliConn* pConn = req->data;
  if (status != 0) {
S
Shengliang Guan 已提交
828
    tError("%s conn %p failed to connect server:%s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status));
U
ubuntu 已提交
829
    cliHandleExcept(pConn);
830
    return;
dengyihao's avatar
dengyihao 已提交
831
  }
dengyihao's avatar
dengyihao 已提交
832 833
  struct sockaddr peername, sockname;

dengyihao's avatar
dengyihao 已提交
834
  int addrlen = sizeof(peername);
dengyihao's avatar
dengyihao 已提交
835
  uv_tcp_getpeername((uv_tcp_t*)pConn->stream, &peername, &addrlen);
dengyihao's avatar
dengyihao 已提交
836
  transSockInfo2Str(&peername, pConn->dst);
dengyihao's avatar
dengyihao 已提交
837

dengyihao's avatar
dengyihao 已提交
838 839
  addrlen = sizeof(sockname);
  uv_tcp_getsockname((uv_tcp_t*)pConn->stream, &sockname, &addrlen);
dengyihao's avatar
dengyihao 已提交
840
  transSockInfo2Str(&sockname, pConn->src);
dengyihao's avatar
dengyihao 已提交
841

dengyihao's avatar
dengyihao 已提交
842
  tTrace("%s conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
843
  assert(pConn->stream == req->handle);
dengyihao's avatar
dengyihao 已提交
844

U
ubuntu 已提交
845
  cliSend(pConn);
dengyihao's avatar
dengyihao 已提交
846 847
}

dengyihao's avatar
dengyihao 已提交
848
static void cliHandleQuit(SCliMsg* pMsg, SCliThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
849 850 851 852 853
  if (!transAsyncPoolIsEmpty(pThrd->asyncPool)) {
    pThrd->stopMsg = pMsg;
    return;
  }
  pThrd->stopMsg = NULL;
dengyihao's avatar
dengyihao 已提交
854
  pThrd->quit = true;
U
ubuntu 已提交
855
  tDebug("cli work thread %p start to quit", pThrd);
dengyihao's avatar
dengyihao 已提交
856
  destroyCmsg(pMsg);
dengyihao's avatar
fix bug  
dengyihao 已提交
857
  destroyConnPool(pThrd->pool);
dengyihao's avatar
dengyihao 已提交
858
  uv_walk(pThrd->loop, cliWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
859
}
dengyihao's avatar
dengyihao 已提交
860
static void cliHandleRelease(SCliMsg* pMsg, SCliThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
861
  int64_t    refId = (int64_t)(pMsg->msg.info.handle);
dengyihao's avatar
dengyihao 已提交
862
  SExHandle* exh = transAcquireExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
863
  if (exh == NULL) {
dengyihao's avatar
dengyihao 已提交
864
    tDebug("%" PRId64 " already released", refId);
dengyihao's avatar
dengyihao 已提交
865 866
    destroyCmsg(pMsg);
    return;
dengyihao's avatar
dengyihao 已提交
867 868 869
  }

  SCliConn* conn = exh->handle;
dengyihao's avatar
dengyihao 已提交
870 871
  transReleaseExHandle(transGetRefMgt(), refId);

dengyihao's avatar
dengyihao 已提交
872
  tDebug("%s conn %p start to release to inst", CONN_GET_INST_LABEL(conn), conn);
dengyihao's avatar
dengyihao 已提交
873

dengyihao's avatar
dengyihao 已提交
874 875
  if (T_REF_VAL_GET(conn) == 2) {
    transUnrefCliHandle(conn);
dengyihao's avatar
dengyihao 已提交
876 877
    if (!transQueuePush(&conn->cliMsgs, pMsg)) {
      return;
dengyihao's avatar
dengyihao 已提交
878 879
    }
    cliSend(conn);
dengyihao's avatar
dengyihao 已提交
880 881 882
  } else {
    tError("%s conn %p already released", CONN_GET_INST_LABEL(conn), conn);
    destroyCmsg(pMsg);
dengyihao's avatar
dengyihao 已提交
883 884
  }
}
dengyihao's avatar
dengyihao 已提交
885
static void cliHandleUpdate(SCliMsg* pMsg, SCliThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
886
  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
887
  pThrd->cvtAddr = pCtx->cvtAddr;
dengyihao's avatar
dengyihao 已提交
888 889
  destroyCmsg(pMsg);
}
U
ubuntu 已提交
890

dengyihao's avatar
dengyihao 已提交
891
SCliConn* cliGetConn(SCliMsg* pMsg, SCliThrd* pThrd, bool* ignore) {
dengyihao's avatar
dengyihao 已提交
892 893 894 895
  STransConnCtx* pCtx = pMsg->ctx;
  SCliConn*      conn = NULL;

  int64_t refId = (int64_t)(pMsg->msg.info.handle);
dengyihao's avatar
dengyihao 已提交
896
  if (refId != 0) {
dengyihao's avatar
dengyihao 已提交
897
    SExHandle* exh = transAcquireExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
898
    if (exh == NULL) {
dengyihao's avatar
dengyihao 已提交
899
      tError("failed to get conn, refId: %" PRId64 "", refId);
dengyihao's avatar
dengyihao 已提交
900 901
      *ignore = true;
      return NULL;
dengyihao's avatar
dengyihao 已提交
902 903
    } else {
      conn = exh->handle;
dengyihao's avatar
dengyihao 已提交
904 905
      if (conn == NULL) {
        conn = getConnFromPool(pThrd->pool, EPSET_GET_INUSE_IP(&pCtx->epSet), EPSET_GET_INUSE_PORT(&pCtx->epSet));
dengyihao's avatar
dengyihao 已提交
906
        if (conn != NULL) specifyConnRef(conn, true, refId);
dengyihao's avatar
dengyihao 已提交
907
      }
dengyihao's avatar
dengyihao 已提交
908
      transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
909 910 911
    }
    return conn;
  };
dengyihao's avatar
dengyihao 已提交
912 913 914

  conn = getConnFromPool(pThrd->pool, EPSET_GET_INUSE_IP(&pCtx->epSet), EPSET_GET_INUSE_PORT(&pCtx->epSet));
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
915
    tTrace("%s conn %p get from conn pool:%p", CONN_GET_INST_LABEL(conn), conn, pThrd->pool);
dengyihao's avatar
dengyihao 已提交
916
  } else {
dengyihao's avatar
dengyihao 已提交
917
    tTrace("%s not found conn in conn pool:%p", ((STrans*)pThrd->pTransInst)->label, pThrd->pool);
dengyihao's avatar
dengyihao 已提交
918
  }
dengyihao's avatar
dengyihao 已提交
919 920
  return conn;
}
dengyihao's avatar
dengyihao 已提交
921
FORCE_INLINE void cliMayCvtFqdnToIp(SEpSet* pEpSet, SCvtAddr* pCvtAddr) {
dengyihao's avatar
dengyihao 已提交
922 923 924
  if (pCvtAddr->cvt == false) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
925 926 927
  if (pEpSet->numOfEps == 1 && strncmp(pEpSet->eps[0].fqdn, pCvtAddr->fqdn, TSDB_FQDN_LEN) == 0) {
    memset(pEpSet->eps[0].fqdn, 0, TSDB_FQDN_LEN);
    memcpy(pEpSet->eps[0].fqdn, pCvtAddr->ip, TSDB_FQDN_LEN);
dengyihao's avatar
dengyihao 已提交
928 929
  }
}
dengyihao's avatar
dengyihao 已提交
930

dengyihao's avatar
dengyihao 已提交
931
FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx) {
dengyihao's avatar
dengyihao 已提交
932 933 934 935 936
  if (code != 0) return false;
  if (pCtx->retryCnt == 0) return false;
  if (transEpSetIsEqual(&pCtx->epSet, &pCtx->origEpSet)) return false;
  return true;
}
dengyihao's avatar
dengyihao 已提交
937
FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* pResp) {
dengyihao's avatar
dengyihao 已提交
938 939 940 941 942 943 944 945 946 947 948
  if (pMsg == NULL) return -1;

  memset(pResp, 0, sizeof(STransMsg));

  pResp->code = TSDB_CODE_RPC_BROKEN_LINK;
  pResp->msgType = pMsg->msg.msgType + 1;
  pResp->info.ahandle = pMsg->ctx ? pMsg->ctx->ahandle : NULL;
  pResp->info.traceId = pMsg->msg.info.traceId;

  return 0;
}
dengyihao's avatar
dengyihao 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
static FORCE_INLINE uint32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn) {
  uint32_t  addr = 0;
  uint32_t* v = taosHashGet(cache, fqdn, strlen(fqdn));
  if (v == NULL) {
    addr = taosGetIpv4FromFqdn(fqdn);
    taosHashPut(cache, fqdn, strlen(fqdn), &addr, sizeof(addr));
  } else {
    addr = *v;
  }
  return addr;
}
static FORCE_INLINE void cliUpdateFqdnCache(SHashObj* cache, char* fqdn) {
  // impl later
  return;
}
dengyihao's avatar
dengyihao 已提交
964

dengyihao's avatar
dengyihao 已提交
965
void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) {
dengyihao's avatar
formate  
dengyihao 已提交
966
  STrans*        pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
967
  STransConnCtx* pCtx = pMsg->ctx;
dengyihao's avatar
dengyihao 已提交
968

dengyihao's avatar
dengyihao 已提交
969
  cliMayCvtFqdnToIp(&pCtx->epSet, &pThrd->cvtAddr);
dengyihao's avatar
dengyihao 已提交
970 971 972 973 974
  if (!EPSET_IS_VALID(&pCtx->epSet)) {
    destroyCmsg(pMsg);
    tError("invalid epset");
    return;
  }
dengyihao's avatar
dengyihao 已提交
975

dengyihao's avatar
dengyihao 已提交
976 977 978
  bool      ignore = false;
  SCliConn* conn = cliGetConn(pMsg, pThrd, &ignore);
  if (ignore == true) {
dengyihao's avatar
dengyihao 已提交
979
    // persist conn already release by server
dengyihao's avatar
dengyihao 已提交
980 981
    STransMsg resp;
    cliBuildExceptResp(pMsg, &resp);
dengyihao's avatar
dengyihao 已提交
982 983 984
    if (pMsg->type != Release) {
      pTransInst->cfp(pTransInst->parent, &resp, NULL);
    }
dengyihao's avatar
dengyihao 已提交
985
    destroyCmsg(pMsg);
dengyihao's avatar
dengyihao 已提交
986 987
    return;
  }
dengyihao's avatar
dengyihao 已提交
988

dengyihao's avatar
dengyihao 已提交
989
  if (conn != NULL) {
dengyihao's avatar
dengyihao 已提交
990
    transCtxMerge(&conn->ctx, &pCtx->appCtx);
dengyihao's avatar
dengyihao 已提交
991
    transQueuePush(&conn->cliMsgs, pMsg);
U
ubuntu 已提交
992
    cliSend(conn);
dengyihao's avatar
dengyihao 已提交
993
  } else {
U
ubuntu 已提交
994
    conn = cliCreateConn(pThrd);
dengyihao's avatar
dengyihao 已提交
995 996 997 998

    int64_t refId = (int64_t)pMsg->msg.info.handle;
    if (refId != 0) specifyConnRef(conn, true, refId);

dengyihao's avatar
dengyihao 已提交
999
    transCtxMerge(&conn->ctx, &pCtx->appCtx);
dengyihao's avatar
dengyihao 已提交
1000
    transQueuePush(&conn->cliMsgs, pMsg);
dengyihao's avatar
dengyihao 已提交
1001

dengyihao's avatar
dengyihao 已提交
1002 1003
    conn->ip = strdup(EPSET_GET_INUSE_IP(&pCtx->epSet));
    conn->port = EPSET_GET_INUSE_PORT(&pCtx->epSet);
dengyihao's avatar
dengyihao 已提交
1004

dengyihao's avatar
dengyihao 已提交
1005 1006
    int ret = transSetConnOption((uv_tcp_t*)conn->stream);
    if (ret) {
dengyihao's avatar
dengyihao 已提交
1007
      tError("%s conn %p failed to set conn option, errmsg %s", transLabel(pTransInst), conn, uv_err_name(ret));
dengyihao's avatar
dengyihao 已提交
1008
    }
dengyihao's avatar
dengyihao 已提交
1009
    int32_t fd = taosCreateSocketWithTimeout(TRANS_CONN_TIMEOUT);
dengyihao's avatar
dengyihao 已提交
1010
    if (fd == -1) {
dengyihao's avatar
dengyihao 已提交
1011
      tTrace("%s conn %p failed to create socket", transLabel(pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
1012 1013 1014 1015
      cliHandleExcept(conn);
      return;
    }
    uv_tcp_open((uv_tcp_t*)conn->stream, fd);
1016

dengyihao's avatar
dengyihao 已提交
1017
    struct sockaddr_in addr;
1018
    addr.sin_family = AF_INET;
dengyihao's avatar
dengyihao 已提交
1019 1020

    addr.sin_addr.s_addr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, conn->ip);
1021
    addr.sin_port = (uint16_t)htons((uint16_t)conn->port);
dengyihao's avatar
dengyihao 已提交
1022
    tTrace("%s conn %p try to connect to %s:%d", pTransInst->label, conn, conn->ip, conn->port);
dengyihao's avatar
dengyihao 已提交
1023 1024
    ret = uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb);
    if (ret != 0) {
S
Shengliang Guan 已提交
1025
      tTrace("%s conn %p failed to connect to %s:%d, reason:%s", pTransInst->label, conn, conn->ip, conn->port,
dengyihao's avatar
dengyihao 已提交
1026 1027 1028 1029
             uv_err_name(ret));
      cliHandleExcept(conn);
      return;
    }
dengyihao's avatar
dengyihao 已提交
1030
  }
dengyihao's avatar
dengyihao 已提交
1031 1032
  STraceId* trace = &pMsg->msg.info.traceId;
  tGTrace("%s conn %p ready", pTransInst->label, conn);
dengyihao's avatar
dengyihao 已提交
1033
}
U
ubuntu 已提交
1034
static void cliAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
1035 1036 1037
  SAsyncItem* item = handle->data;
  SCliThrd*   pThrd = item->pThrd;
  SCliMsg*    pMsg = NULL;
dengyihao's avatar
dengyihao 已提交
1038

dengyihao's avatar
dengyihao 已提交
1039
  // batch process to avoid to lock/unlock frequently
dengyihao's avatar
formate  
dengyihao 已提交
1040
  queue wq;
wafwerar's avatar
wafwerar 已提交
1041
  taosThreadMutexLock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
1042
  QUEUE_MOVE(&item->qmsg, &wq);
wafwerar's avatar
wafwerar 已提交
1043
  taosThreadMutexUnlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
1044

dengyihao's avatar
dengyihao 已提交
1045 1046 1047 1048
  int count = 0;
  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* h = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
1049 1050

    SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
dengyihao's avatar
dengyihao 已提交
1051
    (*cliAsyncHandle[pMsg->type])(pMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
1052
    count++;
dengyihao's avatar
dengyihao 已提交
1053 1054
  }
  if (count >= 2) {
S
Shengliang Guan 已提交
1055
    tTrace("cli process batch size:%d", count);
dengyihao's avatar
dengyihao 已提交
1056
  }
dengyihao's avatar
dengyihao 已提交
1057
  // if (!uv_is_active((uv_handle_t*)pThrd->prepare)) uv_prepare_start(pThrd->prepare, cliPrepareCb);
dengyihao's avatar
dengyihao 已提交
1058

dengyihao's avatar
dengyihao 已提交
1059
  if (pThrd->stopMsg != NULL) cliHandleQuit(pThrd->stopMsg, pThrd);
dengyihao's avatar
dengyihao 已提交
1060
}
dengyihao's avatar
dengyihao 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
static void cliPrepareCb(uv_prepare_t* handle) {
  SCliThrd* thrd = handle->data;
  tTrace("prepare work start");

  SAsyncPool* pool = thrd->asyncPool;
  for (int i = 0; i < pool->nAsync; i++) {
    uv_async_t* async = &(pool->asyncs[i]);
    SAsyncItem* item = async->data;

    queue wq;
    taosThreadMutexLock(&item->mtx);
    QUEUE_MOVE(&item->qmsg, &wq);
    taosThreadMutexUnlock(&item->mtx);

    int count = 0;
    while (!QUEUE_IS_EMPTY(&wq)) {
      queue* h = QUEUE_HEAD(&wq);
      QUEUE_REMOVE(h);

      SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
      if (pMsg == NULL) {
        continue;
      }
      (*cliAsyncHandle[pMsg->type])(pMsg, thrd);
      count++;
    }
  }
  tTrace("prepare work end");
  if (thrd->stopMsg != NULL) cliHandleQuit(thrd->stopMsg, thrd);
dengyihao's avatar
dengyihao 已提交
1090
}
dengyihao's avatar
dengyihao 已提交
1091

dengyihao's avatar
dengyihao 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
void cliDestroyConnMsgs(SCliConn* conn, bool destroy) {
  transCtxCleanup(&conn->ctx);
  cliReleaseUnfinishedMsg(conn);
  if (destroy == 1) {
    transQueueDestroy(&conn->cliMsgs);
  } else {
    transQueueClear(&conn->cliMsgs);
  }
}

void cliIteraConnMsgs(SCliConn* conn) {
  SCliThrd* pThrd = conn->hostThrd;
  STrans*   pTransInst = pThrd->pTransInst;

  for (int i = 0; i < transQueueSize(&conn->cliMsgs); i++) {
    SCliMsg* cmsg = transQueueGet(&conn->cliMsgs, i);
    if (cmsg->type == Release || REQUEST_NO_RESP(&cmsg->msg) || cmsg->msg.msgType == TDMT_SCH_DROP_TASK) {
      continue;
    }

    STransMsg resp = {0};
    if (-1 == cliBuildExceptResp(cmsg, &resp)) {
      continue;
    }
    pTransInst->cfp(pTransInst->parent, &resp, NULL);

    cmsg->ctx->ahandle = NULL;
  }
}
dengyihao's avatar
dengyihao 已提交
1121 1122 1123
bool cliRecvReleaseReq(SCliConn* conn, STransMsgHead* pHead) {
  if (pHead->release == 1 && (pHead->msgLen) == sizeof(*pHead)) {
    uint64_t ahandle = pHead->ahandle;
dengyihao's avatar
dengyihao 已提交
1124
    tDebug("ahandle = %" PRIu64 "", ahandle);
dengyihao's avatar
dengyihao 已提交
1125 1126
    SCliMsg* pMsg = NULL;
    CONN_GET_MSGCTX_BY_AHANDLE(conn, ahandle);
dengyihao's avatar
dengyihao 已提交
1127

dengyihao's avatar
dengyihao 已提交
1128 1129
    transClearBuffer(&conn->readBuf);
    transFreeMsg(transContFromHead((char*)pHead));
dengyihao's avatar
dengyihao 已提交
1130 1131 1132 1133 1134 1135 1136

    for (int i = 0; ahandle == 0 && i < transQueueSize(&conn->cliMsgs); i++) {
      SCliMsg* cliMsg = transQueueGet(&conn->cliMsgs, i);
      if (cliMsg->type == Release) {
        assert(pMsg == NULL);
        return true;
      }
dengyihao's avatar
dengyihao 已提交
1137
    }
dengyihao's avatar
dengyihao 已提交
1138 1139 1140

    cliIteraConnMsgs(conn);

dengyihao's avatar
dengyihao 已提交
1141 1142
    tDebug("%s conn %p receive release request, refId:%" PRId64 "", CONN_GET_INST_LABEL(conn), conn, conn->refId);
    destroyCmsg(pMsg);
dengyihao's avatar
dengyihao 已提交
1143

dengyihao's avatar
dengyihao 已提交
1144 1145 1146 1147 1148 1149
    addConnToPool(((SCliThrd*)conn->hostThrd)->pool, conn);
    return true;
  }
  return false;
}

U
ubuntu 已提交
1150
static void* cliWorkThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
1151
  SCliThrd* pThrd = (SCliThrd*)arg;
dengyihao's avatar
dengyihao 已提交
1152
  pThrd->pid = taosGetSelfPthreadId();
U
ubuntu 已提交
1153
  setThreadName("trans-cli-work");
dengyihao's avatar
dengyihao 已提交
1154
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
1155
  return NULL;
dengyihao's avatar
dengyihao 已提交
1156 1157
}

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

dengyihao's avatar
dengyihao 已提交
1161
  STrans* pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
1162
  memcpy(cli->label, label, TSDB_LABEL_LEN);
dengyihao's avatar
dengyihao 已提交
1163
  cli->numOfThreads = numOfThreads;
dengyihao's avatar
dengyihao 已提交
1164
  cli->pThreadObj = (SCliThrd**)taosMemoryCalloc(cli->numOfThreads, sizeof(SCliThrd*));
dengyihao's avatar
dengyihao 已提交
1165 1166

  for (int i = 0; i < cli->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
1167 1168
    SCliThrd* pThrd = createThrdObj(shandle);
    int       err = taosThreadCreate(&pThrd->thread, NULL, cliWorkThread, (void*)(pThrd));
dengyihao's avatar
dengyihao 已提交
1169
    if (err == 0) {
S
Shengliang Guan 已提交
1170
      tDebug("success to create tranport-cli thread:%d", i);
dengyihao's avatar
dengyihao 已提交
1171
    }
dengyihao's avatar
dengyihao 已提交
1172
    cli->pThreadObj[i] = pThrd;
dengyihao's avatar
dengyihao 已提交
1173
  }
dengyihao's avatar
dengyihao 已提交
1174

dengyihao's avatar
dengyihao 已提交
1175 1176
  return cli;
}
dengyihao's avatar
dengyihao 已提交
1177

dengyihao's avatar
dengyihao 已提交
1178
static FORCE_INLINE void destroyUserdata(STransMsg* userdata) {
dengyihao's avatar
dengyihao 已提交
1179 1180 1181 1182 1183 1184
  if (userdata->pCont == NULL) {
    return;
  }
  transFreeMsg(userdata->pCont);
  userdata->pCont = NULL;
}
dengyihao's avatar
dengyihao 已提交
1185

dengyihao's avatar
dengyihao 已提交
1186
static FORCE_INLINE void destroyCmsg(void* arg) {
dengyihao's avatar
dengyihao 已提交
1187
  SCliMsg* pMsg = arg;
dengyihao's avatar
dengyihao 已提交
1188 1189 1190 1191 1192
  if (pMsg == NULL) {
    return;
  }
  transDestroyConnCtx(pMsg->ctx);
  destroyUserdata(&pMsg->msg);
wafwerar's avatar
wafwerar 已提交
1193
  taosMemoryFree(pMsg);
dengyihao's avatar
dengyihao 已提交
1194
}
dengyihao's avatar
dengyihao 已提交
1195

dengyihao's avatar
dengyihao 已提交
1196 1197 1198
static SCliThrd* createThrdObj(void* trans) {
  STrans* pTransInst = trans;

dengyihao's avatar
dengyihao 已提交
1199
  SCliThrd* pThrd = (SCliThrd*)taosMemoryCalloc(1, sizeof(SCliThrd));
U
ubuntu 已提交
1200

dengyihao's avatar
dengyihao 已提交
1201
  QUEUE_INIT(&pThrd->msg);
wafwerar's avatar
wafwerar 已提交
1202
  taosThreadMutexInit(&pThrd->msgMtx, NULL);
dengyihao's avatar
dengyihao 已提交
1203

wafwerar's avatar
wafwerar 已提交
1204
  pThrd->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
1205 1206
  uv_loop_init(pThrd->loop);

dengyihao's avatar
dengyihao 已提交
1207
  pThrd->asyncPool = transAsyncPoolCreate(pThrd->loop, 5, pThrd, cliAsyncCb);
dengyihao's avatar
dengyihao 已提交
1208 1209 1210 1211

  pThrd->prepare = taosMemoryCalloc(1, sizeof(uv_prepare_t));
  uv_prepare_init(pThrd->loop, pThrd->prepare);
  pThrd->prepare->data = pThrd;
dengyihao's avatar
dengyihao 已提交
1212
  // uv_prepare_start(pThrd->prepare, cliPrepareCb);
dengyihao's avatar
dengyihao 已提交
1213

dengyihao's avatar
dengyihao 已提交
1214 1215 1216 1217 1218 1219 1220 1221
  int32_t timerSize = 512;
  pThrd->timerList = taosArrayInit(timerSize, sizeof(void*));
  for (int i = 0; i < timerSize; i++) {
    uv_timer_t* timer = taosMemoryCalloc(1, sizeof(uv_timer_t));
    uv_timer_init(pThrd->loop, timer);
    taosArrayPush(pThrd->timerList, &timer);
  }

dengyihao's avatar
dengyihao 已提交
1222
  pThrd->pool = createConnPool(4);
dengyihao's avatar
dengyihao 已提交
1223
  transDQCreate(pThrd->loop, &pThrd->delayQueue);
dengyihao's avatar
dengyihao 已提交
1224

dengyihao's avatar
dengyihao 已提交
1225 1226
  transDQCreate(pThrd->loop, &pThrd->timeoutQueue);

dengyihao's avatar
dengyihao 已提交
1227 1228 1229 1230
  pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pTransInst->idleTime);
  pThrd->pTransInst = trans;

  pThrd->fqdn2ipCache = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
1231
  pThrd->quit = false;
dengyihao's avatar
dengyihao 已提交
1232 1233
  return pThrd;
}
dengyihao's avatar
dengyihao 已提交
1234
static void destroyThrdObj(SCliThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
1235 1236 1237
  if (pThrd == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1238

wafwerar's avatar
wafwerar 已提交
1239
  taosThreadJoin(pThrd->thread, NULL);
dengyihao's avatar
dengyihao 已提交
1240
  CLI_RELEASE_UV(pThrd->loop);
wafwerar's avatar
wafwerar 已提交
1241
  taosThreadMutexDestroy(&pThrd->msgMtx);
dengyihao's avatar
dengyihao 已提交
1242
  TRANS_DESTROY_ASYNC_POOL_MSG(pThrd->asyncPool, SCliMsg, destroyCmsg);
dengyihao's avatar
dengyihao 已提交
1243
  transAsyncPoolDestroy(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
1244

dengyihao's avatar
dengyihao 已提交
1245
  transDQDestroy(pThrd->delayQueue, destroyCmsg);
dengyihao's avatar
dengyihao 已提交
1246
  transDQDestroy(pThrd->timeoutQueue, NULL);
dengyihao's avatar
dengyihao 已提交
1247

dengyihao's avatar
dengyihao 已提交
1248 1249 1250 1251 1252
  for (int i = 0; i < taosArrayGetSize(pThrd->timerList); i++) {
    uv_timer_t* timer = taosArrayGetP(pThrd->timerList, i);
    taosMemoryFree(timer);
  }
  taosArrayDestroy(pThrd->timerList);
dengyihao's avatar
dengyihao 已提交
1253
  taosMemoryFree(pThrd->prepare);
wafwerar's avatar
wafwerar 已提交
1254
  taosMemoryFree(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
1255
  taosHashCleanup(pThrd->fqdn2ipCache);
wafwerar's avatar
wafwerar 已提交
1256
  taosMemoryFree(pThrd);
dengyihao's avatar
dengyihao 已提交
1257
}
dengyihao's avatar
dengyihao 已提交
1258

dengyihao's avatar
dengyihao 已提交
1259
static FORCE_INLINE void transDestroyConnCtx(STransConnCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
1260
  //
wafwerar's avatar
wafwerar 已提交
1261
  taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
1262
}
dengyihao's avatar
dengyihao 已提交
1263

dengyihao's avatar
dengyihao 已提交
1264
void cliSendQuit(SCliThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1265
  // cli can stop gracefully
wafwerar's avatar
wafwerar 已提交
1266
  SCliMsg* msg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
1267
  msg->type = Quit;
dengyihao's avatar
dengyihao 已提交
1268
  transAsyncSend(thrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
1269
  atomic_store_8(&thrd->asyncPool->stop, 1);
dengyihao's avatar
dengyihao 已提交
1270
}
dengyihao's avatar
dengyihao 已提交
1271 1272
void cliWalkCb(uv_handle_t* handle, void* arg) {
  if (!uv_is_closing(handle)) {
dengyihao's avatar
dengyihao 已提交
1273
    uv_read_stop((uv_stream_t*)handle);
dengyihao's avatar
dengyihao 已提交
1274
    uv_close(handle, cliDestroy);
dengyihao's avatar
dengyihao 已提交
1275 1276
  }
}
dengyihao's avatar
dengyihao 已提交
1277

dengyihao's avatar
dengyihao 已提交
1278
FORCE_INLINE int cliRBChoseIdx(STrans* pTransInst) {
dengyihao's avatar
dengyihao 已提交
1279 1280 1281 1282
  int8_t index = pTransInst->index;
  if (pTransInst->numOfThreads == 0) {
    return -1;
  }
U
ubuntu 已提交
1283 1284 1285 1286 1287
  if (pTransInst->index++ >= pTransInst->numOfThreads) {
    pTransInst->index = 0;
  }
  return index % pTransInst->numOfThreads;
}
dengyihao's avatar
dengyihao 已提交
1288
static FORCE_INLINE void doDelayTask(void* param) {
dengyihao's avatar
dengyihao 已提交
1289
  STaskArg* arg = param;
dengyihao's avatar
dengyihao 已提交
1290
  cliHandleReq((SCliMsg*)arg->param1, (SCliThrd*)arg->param2);
dengyihao's avatar
dengyihao 已提交
1291 1292
  taosMemoryFree(arg);
}
dengyihao's avatar
dengyihao 已提交
1293

dengyihao's avatar
dengyihao 已提交
1294 1295 1296
static void doCloseIdleConn(void* param) {
  STaskArg* arg = param;
  SCliConn* conn = arg->param1;
dengyihao's avatar
dengyihao 已提交
1297 1298
  tTrace("%s conn %p idle, close it", CONN_GET_INST_LABEL(conn), conn);
  conn->task = NULL;
dengyihao's avatar
dengyihao 已提交
1299 1300
  cliDestroyConn(conn, true);
  taosMemoryFree(arg);
dengyihao's avatar
dengyihao 已提交
1301 1302
}

dengyihao's avatar
dengyihao 已提交
1303 1304 1305
static void cliSchedMsgToNextNode(SCliMsg* pMsg, SCliThrd* pThrd) {
  STransConnCtx* pCtx = pMsg->ctx;

dengyihao's avatar
dengyihao 已提交
1306 1307
  STraceId* trace = &pMsg->msg.info.traceId;
  char      tbuf[256] = {0};
dengyihao's avatar
dengyihao 已提交
1308
  EPSET_DEBUG_STR(&pCtx->epSet, tbuf);
dengyihao's avatar
dengyihao 已提交
1309
  tGDebug("%s retry on next node, use %s, retryCnt:%d, limit:%d", transLabel(pThrd->pTransInst), tbuf,
dengyihao's avatar
dengyihao 已提交
1310
          pCtx->retryCnt + 1, pCtx->retryLimit);
dengyihao's avatar
dengyihao 已提交
1311 1312 1313 1314 1315

  STaskArg* arg = taosMemoryMalloc(sizeof(STaskArg));
  arg->param1 = pMsg;
  arg->param2 = pThrd;
  transDQSched(pThrd->delayQueue, doDelayTask, arg, TRANS_RETRY_INTERVAL);
dengyihao's avatar
dengyihao 已提交
1316
}
dengyihao's avatar
dengyihao 已提交
1317

dengyihao's avatar
dengyihao 已提交
1318
FORCE_INLINE void cliCompareAndSwap(int8_t* val, int8_t exp, int8_t newVal) {
dengyihao's avatar
dengyihao 已提交
1319 1320 1321 1322
  if (*val != exp) {
    *val = newVal;
  }
}
dengyihao's avatar
dengyihao 已提交
1323

dengyihao's avatar
dengyihao 已提交
1324
FORCE_INLINE bool cliTryExtractEpSet(STransMsg* pResp, SEpSet* dst) {
dengyihao's avatar
dengyihao 已提交
1325 1326 1327 1328 1329 1330
  if ((pResp == NULL || pResp->info.hasEpSet == 0)) {
    return false;
  }
  // rebuild resp msg
  SEpSet epset;
  if (tDeserializeSEpSet(pResp->pCont, pResp->contLen, &epset) < 0) {
dengyihao's avatar
dengyihao 已提交
1331 1332 1333 1334
    return false;
  }
  int32_t tlen = tSerializeSEpSet(NULL, 0, dst);

dengyihao's avatar
dengyihao 已提交
1335 1336 1337 1338 1339 1340 1341
  char*   buf = NULL;
  int32_t len = pResp->contLen - tlen;
  if (len != 0) {
    buf = rpcMallocCont(len);
    memcpy(buf, (char*)pResp->pCont + tlen, len);
  }
  rpcFreeCont(pResp->pCont);
dengyihao's avatar
dengyihao 已提交
1342 1343

  pResp->pCont = buf;
dengyihao's avatar
dengyihao 已提交
1344 1345 1346
  pResp->contLen = len;

  *dst = epset;
dengyihao's avatar
dengyihao 已提交
1347 1348
  return true;
}
dengyihao's avatar
dengyihao 已提交
1349
int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) {
dengyihao's avatar
dengyihao 已提交
1350 1351
  SCliThrd* pThrd = pConn->hostThrd;
  STrans*   pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
1352

dengyihao's avatar
dengyihao 已提交
1353
  if (pMsg == NULL || pMsg->ctx == NULL) {
dengyihao's avatar
dengyihao 已提交
1354
    tDebug("%s conn %p handle resp", pTransInst->label, pConn);
dengyihao's avatar
dengyihao 已提交
1355 1356 1357
    pTransInst->cfp(pTransInst->parent, pResp, NULL);
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
1358

dengyihao's avatar
dengyihao 已提交
1359 1360
  STransConnCtx* pCtx = pMsg->ctx;
  int32_t        code = pResp->code;
dengyihao's avatar
dengyihao 已提交
1361 1362

  bool retry = (pTransInst->retry != NULL && pTransInst->retry(code, pResp->msgType - 1)) ? true : false;
dengyihao's avatar
dengyihao 已提交
1363
  if (retry) {
dengyihao's avatar
dengyihao 已提交
1364 1365
    pMsg->sent = 0;
    pCtx->retryCnt += 1;
dengyihao's avatar
dengyihao 已提交
1366
    if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_BROKEN_LINK) {
dengyihao's avatar
dengyihao 已提交
1367
      cliCompareAndSwap(&pCtx->retryLimit, TRANS_RETRY_COUNT_LIMIT, EPSET_GET_SIZE(&pCtx->epSet) * 3);
dengyihao's avatar
dengyihao 已提交
1368 1369 1370
      if (pCtx->retryCnt < pCtx->retryLimit) {
        transUnrefCliHandle(pConn);
        EPSET_FORWARD_INUSE(&pCtx->epSet);
dengyihao's avatar
dengyihao 已提交
1371
        transFreeMsg(pResp->pCont);
dengyihao's avatar
dengyihao 已提交
1372 1373 1374 1375
        cliSchedMsgToNextNode(pMsg, pThrd);
        return -1;
      }
    } else {
dengyihao's avatar
dengyihao 已提交
1376
      cliCompareAndSwap(&pCtx->retryLimit, TRANS_RETRY_COUNT_LIMIT, TRANS_RETRY_COUNT_LIMIT);
dengyihao's avatar
dengyihao 已提交
1377 1378
      if (pCtx->retryCnt < pCtx->retryLimit) {
        if (pResp->contLen == 0) {
dengyihao's avatar
dengyihao 已提交
1379
          EPSET_FORWARD_INUSE(&pCtx->epSet);
dengyihao's avatar
dengyihao 已提交
1380
        } else {
dengyihao's avatar
dengyihao 已提交
1381
          if (tDeserializeSEpSet(pResp->pCont, pResp->contLen, &pCtx->epSet) < 0) {
dengyihao's avatar
dengyihao 已提交
1382
            tError("%s conn %p failed to deserialize epset", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
1383
          }
dengyihao's avatar
dengyihao 已提交
1384
        }
dengyihao's avatar
dengyihao 已提交
1385
        addConnToPool(pThrd->pool, pConn);
dengyihao's avatar
dengyihao 已提交
1386 1387 1388
        transFreeMsg(pResp->pCont);
        cliSchedMsgToNextNode(pMsg, pThrd);
        return -1;
dengyihao's avatar
dengyihao 已提交
1389
      } else {
dengyihao's avatar
dengyihao 已提交
1390 1391
        // change error code for taos client driver if retryCnt exceeds limit
        if (0 == strncmp(pTransInst->label, "TSC", strlen("TSC"))) pResp->code = TSDB_CODE_APP_NOT_READY;
dengyihao's avatar
dengyihao 已提交
1392
      }
dengyihao's avatar
dengyihao 已提交
1393 1394
    }
  }
dengyihao's avatar
dengyihao 已提交
1395

dengyihao's avatar
dengyihao 已提交
1396
  STraceId* trace = &pResp->info.traceId;
dengyihao's avatar
dengyihao 已提交
1397

dengyihao's avatar
dengyihao 已提交
1398
  bool hasEpSet = cliTryExtractEpSet(pResp, &pCtx->epSet);
dengyihao's avatar
dengyihao 已提交
1399
  if (hasEpSet) {
dengyihao's avatar
dengyihao 已提交
1400 1401
    char tbuf[256] = {0};
    EPSET_DEBUG_STR(&pCtx->epSet, tbuf);
dengyihao's avatar
dengyihao 已提交
1402
    tGDebug("%s conn %p extract epset from msg", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
1403
  }
dengyihao's avatar
dengyihao 已提交
1404

dengyihao's avatar
dengyihao 已提交
1405
  if (pCtx->pSem != NULL) {
dengyihao's avatar
dengyihao 已提交
1406
    tGDebug("%s conn %p(sync) handle resp", CONN_GET_INST_LABEL(pConn), pConn);
1407
    if (pCtx->pRsp == NULL) {
dengyihao's avatar
dengyihao 已提交
1408
      tGTrace("%s conn %p(sync) failed to resp, ignore", CONN_GET_INST_LABEL(pConn), pConn);
1409 1410 1411
    } else {
      memcpy((char*)pCtx->pRsp, (char*)pResp, sizeof(*pResp));
    }
dengyihao's avatar
dengyihao 已提交
1412
    tsem_post(pCtx->pSem);
1413
    pCtx->pRsp = NULL;
dengyihao's avatar
dengyihao 已提交
1414
  } else {
dengyihao's avatar
dengyihao 已提交
1415
    tGDebug("%s conn %p handle resp", CONN_GET_INST_LABEL(pConn), pConn);
dengyihao's avatar
dengyihao 已提交
1416
    if (retry == false && hasEpSet == true) {
dengyihao's avatar
dengyihao 已提交
1417
      pTransInst->cfp(pTransInst->parent, pResp, &pCtx->epSet);
dengyihao's avatar
dengyihao 已提交
1418 1419 1420 1421 1422 1423
    } else {
      if (!cliIsEpsetUpdated(code, pCtx)) {
        pTransInst->cfp(pTransInst->parent, pResp, NULL);
      } else {
        pTransInst->cfp(pTransInst->parent, pResp, &pCtx->epSet);
      }
dengyihao's avatar
dengyihao 已提交
1424
    }
dengyihao's avatar
dengyihao 已提交
1425
  }
dengyihao's avatar
dengyihao 已提交
1426
  return 0;
dengyihao's avatar
dengyihao 已提交
1427
}
U
ubuntu 已提交
1428 1429

void transCloseClient(void* arg) {
U
ubuntu 已提交
1430
  SCliObj* cli = arg;
dengyihao's avatar
dengyihao 已提交
1431
  for (int i = 0; i < cli->numOfThreads; i++) {
U
ubuntu 已提交
1432
    cliSendQuit(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
1433
    destroyThrdObj(cli->pThreadObj[i]);
dengyihao's avatar
dengyihao 已提交
1434
  }
wafwerar's avatar
wafwerar 已提交
1435 1436
  taosMemoryFree(cli->pThreadObj);
  taosMemoryFree(cli);
dengyihao's avatar
dengyihao 已提交
1437
}
dengyihao's avatar
dengyihao 已提交
1438 1439 1440 1441 1442
void transRefCliHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  int ref = T_REF_INC((SCliConn*)handle);
dengyihao's avatar
dengyihao 已提交
1443
  tTrace("%s conn %p ref %d", CONN_GET_INST_LABEL((SCliConn*)handle), handle, ref);
dengyihao's avatar
dengyihao 已提交
1444 1445 1446 1447 1448 1449 1450
  UNUSED(ref);
}
void transUnrefCliHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
  int ref = T_REF_DEC((SCliConn*)handle);
dengyihao's avatar
dengyihao 已提交
1451
  tTrace("%s conn %p ref:%d", CONN_GET_INST_LABEL((SCliConn*)handle), handle, ref);
dengyihao's avatar
dengyihao 已提交
1452
  if (ref == 0) {
U
ubuntu 已提交
1453
    cliDestroyConn((SCliConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
1454 1455
  }
}
dengyihao's avatar
dengyihao 已提交
1456
static FORCE_INLINE SCliThrd* transGetWorkThrdFromHandle(STrans* trans, int64_t handle) {
dengyihao's avatar
dengyihao 已提交
1457
  SCliThrd*  pThrd = NULL;
dengyihao's avatar
dengyihao 已提交
1458
  SExHandle* exh = transAcquireExHandle(transGetRefMgt(), handle);
dengyihao's avatar
dengyihao 已提交
1459 1460 1461
  if (exh == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1462

dengyihao's avatar
dengyihao 已提交
1463 1464 1465 1466 1467 1468
  if (exh->pThrd == NULL && trans != NULL) {
    int idx = cliRBChoseIdx(trans);
    if (idx < 0) return NULL;
    exh->pThrd = ((SCliObj*)trans->tcphandle)->pThreadObj[idx];
  }

dengyihao's avatar
dengyihao 已提交
1469
  pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1470
  transReleaseExHandle(transGetRefMgt(), handle);
dengyihao's avatar
dengyihao 已提交
1471 1472
  return pThrd;
}
dengyihao's avatar
dengyihao 已提交
1473
SCliThrd* transGetWorkThrd(STrans* trans, int64_t handle) {
dengyihao's avatar
dengyihao 已提交
1474
  if (handle == 0) {
dengyihao's avatar
dengyihao 已提交
1475
    int idx = cliRBChoseIdx(trans);
dengyihao's avatar
dengyihao 已提交
1476
    if (idx < 0) return NULL;
dengyihao's avatar
dengyihao 已提交
1477 1478
    return ((SCliObj*)trans->tcphandle)->pThreadObj[idx];
  }
dengyihao's avatar
dengyihao 已提交
1479
  SCliThrd* pThrd = transGetWorkThrdFromHandle(trans, handle);
D
dapan1121 已提交
1480
  return pThrd;
dengyihao's avatar
dengyihao 已提交
1481
}
dengyihao's avatar
dengyihao 已提交
1482
int transReleaseCliHandle(void* handle) {
dengyihao's avatar
dengyihao 已提交
1483 1484 1485
  int  idx = -1;
  bool valid = false;

dengyihao's avatar
dengyihao 已提交
1486
  SCliThrd* pThrd = transGetWorkThrdFromHandle(NULL, (int64_t)handle);
dengyihao's avatar
dengyihao 已提交
1487
  if (pThrd == NULL) {
dengyihao's avatar
dengyihao 已提交
1488
    return -1;
dengyihao's avatar
dengyihao 已提交
1489
  }
dengyihao's avatar
dengyihao 已提交
1490

dengyihao's avatar
dengyihao 已提交
1491
  STransMsg tmsg = {.info.handle = handle, .info.ahandle = (void*)0x9527};
dengyihao's avatar
rm code  
dengyihao 已提交
1492
  TRACE_SET_MSGID(&tmsg.info.traceId, tGenIdPI64());
dengyihao's avatar
dengyihao 已提交
1493

dengyihao's avatar
dengyihao 已提交
1494 1495 1496
  STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx));
  pCtx->ahandle = tmsg.info.ahandle;

dengyihao's avatar
dengyihao 已提交
1497
  SCliMsg* cmsg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
1498
  cmsg->msg = tmsg;
dengyihao's avatar
dengyihao 已提交
1499
  cmsg->type = Release;
dengyihao's avatar
dengyihao 已提交
1500
  cmsg->ctx = pCtx;
dengyihao's avatar
dengyihao 已提交
1501

dengyihao's avatar
dengyihao 已提交
1502 1503 1504
  STraceId* trace = &tmsg.info.traceId;
  tGDebug("send release request at thread:%08" PRId64 "", pThrd->pid);

dengyihao's avatar
dengyihao 已提交
1505
  if (0 != transAsyncSend(pThrd->asyncPool, &cmsg->q)) {
dengyihao's avatar
dengyihao 已提交
1506
    destroyCmsg(cmsg);
dengyihao's avatar
dengyihao 已提交
1507 1508
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1509
  return 0;
dengyihao's avatar
dengyihao 已提交
1510
}
dengyihao's avatar
dengyihao 已提交
1511

dengyihao's avatar
dengyihao 已提交
1512
int transSendRequest(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, STransCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
1513 1514 1515 1516 1517
  STrans* pTransInst = (STrans*)transAcquireExHandle(transGetInstMgt(), (int64_t)shandle);
  if (pTransInst == NULL) {
    transFreeMsg(pReq->pCont);
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1518

dengyihao's avatar
dengyihao 已提交
1519
  SCliThrd* pThrd = transGetWorkThrd(pTransInst, (int64_t)pReq->info.handle);
dengyihao's avatar
dengyihao 已提交
1520
  if (pThrd == NULL) {
dengyihao's avatar
dengyihao 已提交
1521
    transFreeMsg(pReq->pCont);
dengyihao's avatar
dengyihao 已提交
1522
    transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1523
    return TSDB_CODE_RPC_BROKEN_LINK;
dengyihao's avatar
dengyihao 已提交
1524 1525
  }

dengyihao's avatar
dengyihao 已提交
1526
  TRACE_SET_MSGID(&pReq->info.traceId, tGenIdPI64());
dengyihao's avatar
dengyihao 已提交
1527

wafwerar's avatar
wafwerar 已提交
1528
  STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx));
dengyihao's avatar
dengyihao 已提交
1529
  pCtx->epSet = *pEpSet;
S
Shengliang Guan 已提交
1530
  pCtx->ahandle = pReq->info.ahandle;
dengyihao's avatar
dengyihao 已提交
1531
  pCtx->msgType = pReq->msgType;
dengyihao's avatar
dengyihao 已提交
1532

H
Haojun Liao 已提交
1533
  if (ctx != NULL) pCtx->appCtx = *ctx;
dengyihao's avatar
dengyihao 已提交
1534

wafwerar's avatar
wafwerar 已提交
1535
  SCliMsg* cliMsg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
1536
  cliMsg->ctx = pCtx;
dengyihao's avatar
dengyihao 已提交
1537
  cliMsg->msg = *pReq;
dengyihao's avatar
dengyihao 已提交
1538
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
1539
  cliMsg->type = Normal;
dengyihao's avatar
dengyihao 已提交
1540
  cliMsg->refId = (int64_t)shandle;
dengyihao's avatar
dengyihao 已提交
1541

dengyihao's avatar
dengyihao 已提交
1542
  STraceId* trace = &pReq->info.traceId;
dengyihao's avatar
dengyihao 已提交
1543
  tGDebug("%s send request at thread:%08" PRId64 ", dst:%s:%d, app:%p", transLabel(pTransInst), pThrd->pid,
dengyihao's avatar
dengyihao 已提交
1544
          EPSET_GET_INUSE_IP(&pCtx->epSet), EPSET_GET_INUSE_PORT(&pCtx->epSet), pReq->info.ahandle);
dengyihao's avatar
dengyihao 已提交
1545 1546
  if (0 != transAsyncSend(pThrd->asyncPool, &(cliMsg->q))) {
    destroyCmsg(cliMsg);
dengyihao's avatar
dengyihao 已提交
1547
    transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1548 1549
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1550
  transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1551
  return 0;
dengyihao's avatar
dengyihao 已提交
1552
}
dengyihao's avatar
dengyihao 已提交
1553

dengyihao's avatar
dengyihao 已提交
1554
int transSendRecv(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, STransMsg* pRsp) {
dengyihao's avatar
dengyihao 已提交
1555 1556 1557 1558 1559
  STrans* pTransInst = (STrans*)transAcquireExHandle(transGetInstMgt(), (int64_t)shandle);
  if (pTransInst == NULL) {
    transFreeMsg(pReq->pCont);
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1560

dengyihao's avatar
dengyihao 已提交
1561 1562
  SCliThrd* pThrd = transGetWorkThrd(pTransInst, (int64_t)pReq->info.handle);
  if (pThrd == NULL) {
dengyihao's avatar
dengyihao 已提交
1563
    transFreeMsg(pReq->pCont);
dengyihao's avatar
dengyihao 已提交
1564
    transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1565
    return TSDB_CODE_RPC_BROKEN_LINK;
dengyihao's avatar
dengyihao 已提交
1566
  }
dengyihao's avatar
dengyihao 已提交
1567

dengyihao's avatar
dengyihao 已提交
1568 1569
  tsem_t* sem = taosMemoryCalloc(1, sizeof(tsem_t));
  tsem_init(sem, 0, 0);
dengyihao's avatar
dengyihao 已提交
1570

dengyihao's avatar
dengyihao 已提交
1571 1572
  TRACE_SET_MSGID(&pReq->info.traceId, tGenIdPI64());

wafwerar's avatar
wafwerar 已提交
1573
  STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx));
dengyihao's avatar
dengyihao 已提交
1574
  pCtx->epSet = *pEpSet;
dengyihao's avatar
dengyihao 已提交
1575
  pCtx->origEpSet = *pEpSet;
S
Shengliang Guan 已提交
1576
  pCtx->ahandle = pReq->info.ahandle;
dengyihao's avatar
dengyihao 已提交
1577
  pCtx->msgType = pReq->msgType;
dengyihao's avatar
dengyihao 已提交
1578
  pCtx->pSem = sem;
dengyihao's avatar
dengyihao 已提交
1579 1580
  pCtx->pRsp = pRsp;

wafwerar's avatar
wafwerar 已提交
1581
  SCliMsg* cliMsg = taosMemoryCalloc(1, sizeof(SCliMsg));
dengyihao's avatar
dengyihao 已提交
1582 1583 1584
  cliMsg->ctx = pCtx;
  cliMsg->msg = *pReq;
  cliMsg->st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
1585
  cliMsg->type = Normal;
dengyihao's avatar
dengyihao 已提交
1586
  cliMsg->refId = (int64_t)shandle;
dengyihao's avatar
dengyihao 已提交
1587

dengyihao's avatar
dengyihao 已提交
1588
  STraceId* trace = &pReq->info.traceId;
dengyihao's avatar
dengyihao 已提交
1589
  tGDebug("%s send request at thread:%08" PRId64 ", dst:%s:%d, app:%p", transLabel(pTransInst), pThrd->pid,
dengyihao's avatar
dengyihao 已提交
1590
          EPSET_GET_INUSE_IP(&pCtx->epSet), EPSET_GET_INUSE_PORT(&pCtx->epSet), pReq->info.ahandle);
dengyihao's avatar
dengyihao 已提交
1591

dengyihao's avatar
dengyihao 已提交
1592 1593
  int ret = transAsyncSend(pThrd->asyncPool, &cliMsg->q);
  if (ret != 0) {
dengyihao's avatar
dengyihao 已提交
1594
    destroyCmsg(cliMsg);
dengyihao's avatar
dengyihao 已提交
1595
    goto _RETURN;
dengyihao's avatar
dengyihao 已提交
1596
  }
dengyihao's avatar
dengyihao 已提交
1597
  tsem_wait(sem);
dengyihao's avatar
dengyihao 已提交
1598 1599

_RETURN:
dengyihao's avatar
dengyihao 已提交
1600 1601
  tsem_destroy(sem);
  taosMemoryFree(sem);
dengyihao's avatar
dengyihao 已提交
1602
  transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1603
  return ret;
dengyihao's avatar
dengyihao 已提交
1604
}
dengyihao's avatar
dengyihao 已提交
1605 1606 1607
/*
 *
 **/
dengyihao's avatar
dengyihao 已提交
1608
int transSetDefaultAddr(void* shandle, const char* ip, const char* fqdn) {
dengyihao's avatar
dengyihao 已提交
1609
  STrans* pTransInst = (STrans*)transAcquireExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1610 1611 1612
  if (pTransInst == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1613 1614 1615

  SCvtAddr cvtAddr = {0};
  if (ip != NULL && fqdn != NULL) {
dengyihao's avatar
dengyihao 已提交
1616 1617
    if (strlen(ip) <= sizeof(cvtAddr.ip)) memcpy(cvtAddr.ip, ip, strlen(ip));
    if (strlen(fqdn) <= sizeof(cvtAddr.fqdn)) memcpy(cvtAddr.fqdn, fqdn, strlen(fqdn));
dengyihao's avatar
dengyihao 已提交
1618 1619
    cvtAddr.cvt = true;
  }
dengyihao's avatar
dengyihao 已提交
1620 1621
  for (int i = 0; i < pTransInst->numOfThreads; i++) {
    STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx));
dengyihao's avatar
dengyihao 已提交
1622
    pCtx->cvtAddr = cvtAddr;
dengyihao's avatar
dengyihao 已提交
1623 1624 1625 1626

    SCliMsg* cliMsg = taosMemoryCalloc(1, sizeof(SCliMsg));
    cliMsg->ctx = pCtx;
    cliMsg->type = Update;
dengyihao's avatar
dengyihao 已提交
1627
    cliMsg->refId = (int64_t)shandle;
dengyihao's avatar
dengyihao 已提交
1628

dengyihao's avatar
dengyihao 已提交
1629
    SCliThrd* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[i];
S
Shengliang Guan 已提交
1630
    tDebug("%s update epset at thread:%08" PRId64, pTransInst->label, thrd->pid);
dengyihao's avatar
dengyihao 已提交
1631

dengyihao's avatar
dengyihao 已提交
1632 1633
    if (transAsyncSend(thrd->asyncPool, &(cliMsg->q)) != 0) {
      destroyCmsg(cliMsg);
dengyihao's avatar
dengyihao 已提交
1634
      transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1635 1636
      return -1;
    }
dengyihao's avatar
dengyihao 已提交
1637
  }
dengyihao's avatar
dengyihao 已提交
1638
  transReleaseExHandle(transGetInstMgt(), (int64_t)shandle);
dengyihao's avatar
dengyihao 已提交
1639
  return 0;
dengyihao's avatar
dengyihao 已提交
1640
}
dengyihao's avatar
dengyihao 已提交
1641 1642 1643 1644 1645

int64_t transAllocHandle() {
  SExHandle* exh = taosMemoryCalloc(1, sizeof(SExHandle));
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  tDebug("pre alloc refId %" PRId64 "", exh->refId);
dengyihao's avatar
dengyihao 已提交
1646

dengyihao's avatar
dengyihao 已提交
1647 1648
  return exh->refId;
}
dengyihao's avatar
dengyihao 已提交
1649
#endif