transSvr.c 36.1 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
dengyihao's avatar
dengyihao 已提交
3
 * * This program is free software: you can use, redistribute, and/or modify
dengyihao's avatar
dengyihao 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 * 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

17
#include "transComm.h"
dengyihao's avatar
dengyihao 已提交
18

dengyihao's avatar
dengyihao 已提交
19 20
static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT;

dengyihao's avatar
dengyihao 已提交
21
static char* notify = "a";
dengyihao's avatar
dengyihao 已提交
22

dengyihao's avatar
dengyihao 已提交
23 24 25 26
typedef struct {
  int       notifyCount;  //
  int       init;         // init or not
  STransMsg msg;
dengyihao's avatar
dengyihao 已提交
27
} SSvrRegArg;
dengyihao's avatar
dengyihao 已提交
28

dengyihao's avatar
dengyihao 已提交
29
typedef struct SSvrConn {
dengyihao's avatar
dengyihao 已提交
30
  T_REF_DECLARE()
dengyihao's avatar
dengyihao 已提交
31
  uv_tcp_t*  pTcp;
dengyihao's avatar
dengyihao 已提交
32
  queue      wreqQueue;
dengyihao's avatar
dengyihao 已提交
33
  uv_timer_t pTimer;
dengyihao's avatar
dengyihao 已提交
34 35

  queue       queue;
dengyihao's avatar
dengyihao 已提交
36
  SConnBuffer readBuf;  // read buf,
dengyihao's avatar
dengyihao 已提交
37 38 39
  int         inType;
  void*       pTransInst;  // rpc init
  void*       ahandle;     //
dengyihao's avatar
dengyihao 已提交
40
  void*       hostThrd;
dengyihao's avatar
dengyihao 已提交
41
  STransQueue srvMsgs;
dengyihao's avatar
dengyihao 已提交
42

dengyihao's avatar
dengyihao 已提交
43
  SSvrRegArg regArg;
dengyihao's avatar
dengyihao 已提交
44
  bool       broken;  // conn broken;
dengyihao's avatar
dengyihao 已提交
45

dengyihao's avatar
dengyihao 已提交
46
  ConnStatus         status;
dengyihao's avatar
dengyihao 已提交
47
  struct sockaddr_in addr;
dengyihao's avatar
dengyihao 已提交
48
  struct sockaddr_in localAddr;
U
ubuntu 已提交
49

dengyihao's avatar
dengyihao 已提交
50 51 52 53 54 55
  int64_t refId;
  int     spi;
  char    info[64];
  char    user[TSDB_UNI_LEN];  // user ID for the link
  char    secret[TSDB_PASSWORD_LEN];
  char    ckey[TSDB_PASSWORD_LEN];  // ciphering key
dengyihao's avatar
dengyihao 已提交
56
} SSvrConn;
dengyihao's avatar
dengyihao 已提交
57

dengyihao's avatar
dengyihao 已提交
58 59
typedef struct SSvrMsg {
  SSvrConn*     pConn;
dengyihao's avatar
dengyihao 已提交
60 61 62
  STransMsg     msg;
  queue         q;
  STransMsgType type;
dengyihao's avatar
dengyihao 已提交
63
} SSvrMsg;
dengyihao's avatar
dengyihao 已提交
64

dengyihao's avatar
dengyihao 已提交
65
typedef struct SWorkThrd {
dengyihao's avatar
dengyihao 已提交
66
  TdThread      thread;
wafwerar's avatar
wafwerar 已提交
67
  uv_connect_t  connect_req;
dengyihao's avatar
dengyihao 已提交
68 69 70 71 72
  uv_pipe_t*    pipe;
  uv_os_fd_t    fd;
  uv_loop_t*    loop;
  SAsyncPool*   asyncPool;
  queue         msg;
wafwerar's avatar
wafwerar 已提交
73
  TdThreadMutex msgMtx;
dengyihao's avatar
dengyihao 已提交
74 75 76 77

  queue conn;
  void* pTransInst;
  bool  quit;
dengyihao's avatar
dengyihao 已提交
78
} SWorkThrd;
dengyihao's avatar
dengyihao 已提交
79 80

typedef struct SServerObj {
dengyihao's avatar
dengyihao 已提交
81
  TdThread   thread;
dengyihao's avatar
dengyihao 已提交
82 83 84 85
  uv_tcp_t   server;
  uv_loop_t* loop;

  // work thread info
dengyihao's avatar
dengyihao 已提交
86 87 88 89
  int         workerIdx;
  int         numOfThreads;
  int         numOfWorkerReady;
  SWorkThrd** pThreadObj;
dengyihao's avatar
dengyihao 已提交
90

wafwerar's avatar
wafwerar 已提交
91
  uv_pipe_t   pipeListen;
dengyihao's avatar
dengyihao 已提交
92 93 94 95
  uv_pipe_t** pipe;
  uint32_t    ip;
  uint32_t    port;
  uv_async_t* pAcceptAsync;  // just to quit from from accept thread
dengyihao's avatar
dengyihao 已提交
96 97

  bool inited;
dengyihao's avatar
dengyihao 已提交
98 99 100
} SServerObj;

static void uvAllocConnBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
101
static void uvAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
102
static void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
103
static void uvOnTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
104
static void uvOnSendCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
105
static void uvOnPipeWriteCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
106 107 108
static void uvOnAcceptCb(uv_stream_t* stream, int status);
static void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf);
static void uvWorkerAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
109
static void uvAcceptAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
110
static void uvShutDownCb(uv_shutdown_t* req, int status);
dengyihao's avatar
dengyihao 已提交
111 112 113 114 115 116 117

/*
 * time-consuming task throwed into BG work thread
 */
static void uvWorkDoTask(uv_work_t* req);
static void uvWorkAfterTask(uv_work_t* req, int status);

dengyihao's avatar
dengyihao 已提交
118 119
static void uvWalkCb(uv_handle_t* handle, void* arg);
static void uvFreeCb(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
120

dengyihao's avatar
dengyihao 已提交
121 122 123
static void uvStartSendRespInternal(SSvrMsg* smsg);
static void uvPrepareSendData(SSvrMsg* msg, uv_buf_t* wb);
static void uvStartSendResp(SSvrMsg* msg);
dengyihao's avatar
dengyihao 已提交
124

dengyihao's avatar
dengyihao 已提交
125
static void uvNotifyLinkBrokenToApp(SSvrConn* conn);
dengyihao's avatar
dengyihao 已提交
126

dengyihao's avatar
dengyihao 已提交
127
static void destroySmsg(SSvrMsg* smsg);
128
// check whether already read complete packet
dengyihao's avatar
dengyihao 已提交
129 130 131
static SSvrConn* createConn(void* hThrd);
static void      destroyConn(SSvrConn* conn, bool clear /*clear handle or not*/);
static void      destroyConnRegArg(SSvrConn* conn);
dengyihao's avatar
dengyihao 已提交
132

dengyihao's avatar
dengyihao 已提交
133
static int reallocConnRef(SSvrConn* conn);
dengyihao's avatar
dengyihao 已提交
134

dengyihao's avatar
dengyihao 已提交
135 136 137 138 139 140
static void uvHandleQuit(SSvrMsg* msg, SWorkThrd* thrd);
static void uvHandleRelease(SSvrMsg* msg, SWorkThrd* thrd);
static void uvHandleResp(SSvrMsg* msg, SWorkThrd* thrd);
static void uvHandleRegister(SSvrMsg* msg, SWorkThrd* thrd);
static void (*transAsyncHandle[])(SSvrMsg* msg, SWorkThrd* thrd) = {uvHandleResp, uvHandleQuit, uvHandleRelease,
                                                                    uvHandleRegister, NULL};
dengyihao's avatar
dengyihao 已提交
141

142
static void uvDestroyConn(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
143

dengyihao's avatar
dengyihao 已提交
144
// server and worker thread
145 146
static void* transWorkerThread(void* arg);
static void* transAcceptThread(void* arg);
dengyihao's avatar
dengyihao 已提交
147

dengyihao's avatar
dengyihao 已提交
148
// add handle loop
dengyihao's avatar
dengyihao 已提交
149
static bool addHandleToWorkloop(SWorkThrd* pThrd, char* pipeName);
dengyihao's avatar
dengyihao 已提交
150 151
static bool addHandleToAcceptloop(void* arg);

dengyihao's avatar
dengyihao 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#define CONN_SHOULD_RELEASE(conn, head)                                                                   \
  do {                                                                                                    \
    if ((head)->release == 1 && (head->msgLen) == sizeof(*head)) {                                        \
      reallocConnRef(conn);                                                                               \
      tTrace("conn %p received release request", conn);                                                   \
                                                                                                          \
      STraceId traceId = head->traceId;                                                                   \
      conn->status = ConnRelease;                                                                         \
      transClearBuffer(&conn->readBuf);                                                                   \
      transFreeMsg(transContFromHead((char*)head));                                                       \
                                                                                                          \
      STransMsg tmsg = {                                                                                  \
          .code = 0, .info.handle = (void*)conn, .info.traceId = traceId, .info.ahandle = (void*)0x9527}; \
      SSvrMsg* srvMsg = taosMemoryCalloc(1, sizeof(SSvrMsg));                                             \
      srvMsg->msg = tmsg;                                                                                 \
      srvMsg->type = Release;                                                                             \
      srvMsg->pConn = conn;                                                                               \
      if (!transQueuePush(&conn->srvMsgs, srvMsg)) {                                                      \
        return;                                                                                           \
      }                                                                                                   \
      if (conn->regArg.init) {                                                                            \
        tTrace("conn %p release, notify server app", conn);                                               \
        STrans* pTransInst = conn->pTransInst;                                                            \
        (*pTransInst->cfp)(pTransInst->parent, &(conn->regArg.msg), NULL);                                \
        memset(&conn->regArg, 0, sizeof(conn->regArg));                                                   \
      }                                                                                                   \
      uvStartSendRespInternal(srvMsg);                                                                    \
      return;                                                                                             \
    }                                                                                                     \
dengyihao's avatar
dengyihao 已提交
181 182 183 184 185 186 187 188 189
  } while (0)

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

dengyihao's avatar
dengyihao 已提交
190 191 192 193
#define ASYNC_ERR_JRET(thrd)                            \
  do {                                                  \
    if (thrd->quit) {                                   \
      tTrace("worker thread already quit, ignore msg"); \
dengyihao's avatar
dengyihao 已提交
194
      goto _return1;                                    \
dengyihao's avatar
dengyihao 已提交
195 196 197
    }                                                   \
  } while (0)

dengyihao's avatar
dengyihao 已提交
198
void uvAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
199
  SSvrConn*    conn = handle->data;
dengyihao's avatar
dengyihao 已提交
200 201
  SConnBuffer* pBuf = &conn->readBuf;
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
202 203 204 205
}

// refers specifically to query or insert timeout
static void uvHandleActivityTimeout(uv_timer_t* handle) {
dengyihao's avatar
dengyihao 已提交
206
  SSvrConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
207
  tDebug("%p timeout since no activity", conn);
dengyihao's avatar
dengyihao 已提交
208 209
}

dengyihao's avatar
dengyihao 已提交
210
static void uvHandleReq(SSvrConn* pConn) {
dengyihao's avatar
dengyihao 已提交
211
  SConnBuffer* pBuf = &pConn->readBuf;
dengyihao's avatar
dengyihao 已提交
212 213 214 215
  char*        msg = pBuf->buf;
  uint32_t     msgLen = pBuf->len;

  STransMsgHead* pHead = (STransMsgHead*)msg;
dengyihao's avatar
dengyihao 已提交
216
  pHead->code = htonl(pHead->code);
dengyihao's avatar
dengyihao 已提交
217
  pHead->msgLen = htonl(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
218
  memcpy(pConn->user, pHead->user, strlen(pHead->user));
dengyihao's avatar
dengyihao 已提交
219

dengyihao's avatar
dengyihao 已提交
220 221 222 223 224
  // TODO(dengyihao): time-consuming task throwed into BG Thread
  //  uv_work_t* wreq = taosMemoryMalloc(sizeof(uv_work_t));
  //  wreq->data = pConn;
  //  uv_read_stop((uv_stream_t*)pConn->pTcp);
  //  transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
225
  //  uv_queue_work(((SWorkThrd*)pConn->hostThrd)->loop, wreq, uvWorkDoTask, uvWorkAfterTask);
dengyihao's avatar
dengyihao 已提交
226

dengyihao's avatar
dengyihao 已提交
227
  CONN_SHOULD_RELEASE(pConn, pHead);
dengyihao's avatar
dengyihao 已提交
228

dengyihao's avatar
dengyihao 已提交
229
  STransMsg transMsg;
dengyihao's avatar
dengyihao 已提交
230
  memset(&transMsg, 0, sizeof(transMsg));
dengyihao's avatar
dengyihao 已提交
231 232 233 234
  transMsg.contLen = transContLenFromMsg(pHead->msgLen);
  transMsg.pCont = pHead->content;
  transMsg.msgType = pHead->msgType;
  transMsg.code = pHead->code;
dengyihao's avatar
dengyihao 已提交
235

dengyihao's avatar
dengyihao 已提交
236
  transClearBuffer(&pConn->readBuf);
dengyihao's avatar
dengyihao 已提交
237

dengyihao's avatar
dengyihao 已提交
238
  pConn->inType = pHead->msgType;
dengyihao's avatar
dengyihao 已提交
239 240 241 242
  if (pConn->status == ConnNormal) {
    if (pHead->persist == 1) {
      pConn->status = ConnAcquire;
      transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
243
      tDebug("conn %p acquired by server app", pConn);
dengyihao's avatar
dengyihao 已提交
244 245
    }
  }
dengyihao's avatar
dengyihao 已提交
246
  STrans*   pTransInst = pConn->pTransInst;
dengyihao's avatar
dengyihao 已提交
247
  STraceId* trace = &pHead->traceId;
dengyihao's avatar
dengyihao 已提交
248
  if (pConn->status == ConnNormal && pHead->noResp == 0) {
dengyihao's avatar
dengyihao 已提交
249
    transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
250

S
Shengliang Guan 已提交
251
    tGTrace("%s conn %p %s received from %s:%d, local info:%s:%d, msg size:%d", transLabel(pTransInst), pConn,
dengyihao's avatar
dengyihao 已提交
252
            TMSG_INFO(transMsg.msgType), taosInetNtoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port),
dengyihao's avatar
dengyihao 已提交
253 254
            taosInetNtoa(pConn->localAddr.sin_addr), ntohs(pConn->localAddr.sin_port), transMsg.contLen);
  } else {
S
Shengliang Guan 已提交
255
    tGTrace("%s conn %p %s received from %s:%d, local info:%s:%d, msg size:%d, resp:%d, code:%d",
dengyihao's avatar
dengyihao 已提交
256 257 258
            transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), taosInetNtoa(pConn->addr.sin_addr),
            ntohs(pConn->addr.sin_port), taosInetNtoa(pConn->localAddr.sin_addr), ntohs(pConn->localAddr.sin_port),
            transMsg.contLen, pHead->noResp, transMsg.code);
dengyihao's avatar
dengyihao 已提交
259 260 261
    // no ref here
  }

dengyihao's avatar
dengyihao 已提交
262
  // pHead->noResp = 1,
dengyihao's avatar
dengyihao 已提交
263 264 265
  // 1. server application should not send resp on handle
  // 2. once send out data, cli conn released to conn pool immediately
  // 3. not mixed with persist
dengyihao's avatar
dengyihao 已提交
266
  transMsg.info.ahandle = (void*)pHead->ahandle;
dengyihao's avatar
dengyihao 已提交
267
  transMsg.info.handle = (void*)transAcquireExHandle(transGetRefMgt(), pConn->refId);
S
Shengliang Guan 已提交
268
  transMsg.info.refId = pConn->refId;
dengyihao's avatar
dengyihao 已提交
269 270
  transMsg.info.traceId = pHead->traceId;

dengyihao's avatar
dengyihao 已提交
271 272
  tGTrace("%s handle %p conn:%p translated to app, refId:%" PRIu64, transLabel(pTransInst), transMsg.info.handle, pConn,
          pConn->refId);
S
Shengliang Guan 已提交
273
  assert(transMsg.info.handle != NULL);
dengyihao's avatar
dengyihao 已提交
274

dengyihao's avatar
dengyihao 已提交
275
  if (pHead->noResp == 1) {
S
Shengliang Guan 已提交
276
    transMsg.info.refId = -1;
dengyihao's avatar
dengyihao 已提交
277
  }
dengyihao's avatar
dengyihao 已提交
278 279

  // set up conn info
280
  SRpcConnInfo* pConnInfo = &(transMsg.info.conn);
dengyihao's avatar
dengyihao 已提交
281 282 283 284
  pConnInfo->clientIp = (uint32_t)(pConn->addr.sin_addr.s_addr);
  pConnInfo->clientPort = ntohs(pConn->addr.sin_port);
  tstrncpy(pConnInfo->user, pConn->user, sizeof(pConnInfo->user));

dengyihao's avatar
dengyihao 已提交
285
  transReleaseExHandle(transGetRefMgt(), pConn->refId);
dengyihao's avatar
dengyihao 已提交
286

dengyihao's avatar
dengyihao 已提交
287
  (*pTransInst->cfp)(pTransInst->parent, &transMsg, NULL);
dengyihao's avatar
dengyihao 已提交
288
  // uv_timer_start(&pConn->pTimer, uvHandleActivityTimeout, pRpc->idleTime * 10000, 0);
dengyihao's avatar
dengyihao 已提交
289 290
}

dengyihao's avatar
dengyihao 已提交
291
void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
292
  // opt
dengyihao's avatar
dengyihao 已提交
293
  SSvrConn*    conn = cli->data;
dengyihao's avatar
dengyihao 已提交
294
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
295
  STrans*      pTransInst = conn->pTransInst;
dengyihao's avatar
dengyihao 已提交
296 297
  if (nread > 0) {
    pBuf->len += nread;
S
Shengliang Guan 已提交
298
    tTrace("%s conn %p total read:%d, current read:%d", transLabel(pTransInst), conn, pBuf->len, (int)nread);
dengyihao's avatar
dengyihao 已提交
299
    if (transReadComplete(pBuf)) {
dengyihao's avatar
dengyihao 已提交
300
      tTrace("%s conn %p alread read complete packet", transLabel(pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
301
      uvHandleReq(conn);
dengyihao's avatar
dengyihao 已提交
302
    } else {
dengyihao's avatar
dengyihao 已提交
303
      tTrace("%s conn %p read partial packet, continue to read", transLabel(pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
304 305 306
    }
    return;
  }
307 308 309
  if (nread == 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
310

311
  tWarn("%s conn %p read error:%s", transLabel(pTransInst), conn, uv_err_name(nread));
U
ubuntu 已提交
312
  if (nread < 0) {
dengyihao's avatar
dengyihao 已提交
313
    conn->broken = true;
dengyihao's avatar
dengyihao 已提交
314 315
    if (conn->status == ConnAcquire) {
      if (conn->regArg.init) {
dengyihao's avatar
dengyihao 已提交
316
        tTrace("%s conn %p broken, notify server app", transLabel(pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
317 318 319 320 321
        STrans* pTransInst = conn->pTransInst;
        (*pTransInst->cfp)(pTransInst->parent, &(conn->regArg.msg), NULL);
        memset(&conn->regArg, 0, sizeof(conn->regArg));
      }
    }
dengyihao's avatar
dengyihao 已提交
322
    destroyConn(conn, true);
dengyihao's avatar
dengyihao 已提交
323 324 325 326
  }
}
void uvAllocConnBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
  buf->len = 2;
wafwerar's avatar
wafwerar 已提交
327
  buf->base = taosMemoryCalloc(1, sizeof(char) * buf->len);
dengyihao's avatar
dengyihao 已提交
328 329 330 331
}

void uvOnTimeoutCb(uv_timer_t* handle) {
  // opt
dengyihao's avatar
dengyihao 已提交
332
  SSvrConn* pConn = handle->data;
dengyihao's avatar
dengyihao 已提交
333
  tError("conn %p time out", pConn);
dengyihao's avatar
dengyihao 已提交
334 335
}

dengyihao's avatar
dengyihao 已提交
336
void uvOnSendCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
337
  SSvrConn* conn = transReqQueueRemove(req);
dengyihao's avatar
dengyihao 已提交
338 339
  if (conn == NULL) return;

dengyihao's avatar
dengyihao 已提交
340
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
341
    tTrace("conn %p data already was written on stream", conn);
dengyihao's avatar
dengyihao 已提交
342
    if (!transQueueEmpty(&conn->srvMsgs)) {
dengyihao's avatar
dengyihao 已提交
343
      SSvrMsg* msg = transQueuePop(&conn->srvMsgs);
dengyihao's avatar
add UT  
dengyihao 已提交
344
      destroySmsg(msg);
dengyihao's avatar
dengyihao 已提交
345
      // send cached data
dengyihao's avatar
dengyihao 已提交
346
      if (!transQueueEmpty(&conn->srvMsgs)) {
dengyihao's avatar
dengyihao 已提交
347
        msg = (SSvrMsg*)transQueueGet(&conn->srvMsgs, 0);
dengyihao's avatar
dengyihao 已提交
348 349 350 351 352 353 354 355 356
        if (msg->type == Register && conn->status == ConnAcquire) {
          conn->regArg.notifyCount = 0;
          conn->regArg.init = 1;
          conn->regArg.msg = msg->msg;
          if (conn->broken) {
            STrans* pTransInst = conn->pTransInst;
            (pTransInst->cfp)(pTransInst->parent, &(conn->regArg.msg), NULL);
            memset(&conn->regArg, 0, sizeof(conn->regArg));
          }
dengyihao's avatar
dengyihao 已提交
357
          transQueuePop(&conn->srvMsgs);
wafwerar's avatar
wafwerar 已提交
358
          taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
359

dengyihao's avatar
dengyihao 已提交
360
          msg = (SSvrMsg*)transQueueGet(&conn->srvMsgs, 0);
dengyihao's avatar
dengyihao 已提交
361 362 363
          if (msg != NULL) {
            uvStartSendRespInternal(msg);
          }
dengyihao's avatar
dengyihao 已提交
364 365 366
        } else {
          uvStartSendRespInternal(msg);
        }
dengyihao's avatar
add UT  
dengyihao 已提交
367
      }
dengyihao's avatar
dengyihao 已提交
368
    }
dengyihao's avatar
dengyihao 已提交
369
    transUnrefSrvHandle(conn);
dengyihao's avatar
dengyihao 已提交
370
  } else {
dengyihao's avatar
dengyihao 已提交
371
    tError("conn %p failed to write data, %s", conn, uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
372
    conn->broken = true;
dengyihao's avatar
dengyihao 已提交
373
    transUnrefSrvHandle(conn);
dengyihao's avatar
dengyihao 已提交
374 375
  }
}
dengyihao's avatar
dengyihao 已提交
376 377
static void uvOnPipeWriteCb(uv_write_t* req, int status) {
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
378
    tTrace("success to dispatch conn to work thread");
dengyihao's avatar
dengyihao 已提交
379 380 381
  } else {
    tError("fail to dispatch conn to work thread");
  }
dengyihao's avatar
dengyihao 已提交
382
  uv_close((uv_handle_t*)req->data, uvFreeCb);
wafwerar's avatar
wafwerar 已提交
383
  taosMemoryFree(req);
dengyihao's avatar
dengyihao 已提交
384
}
dengyihao's avatar
dengyihao 已提交
385

dengyihao's avatar
dengyihao 已提交
386 387
static void uvPrepareSendData(SSvrMsg* smsg, uv_buf_t* wb) {
  SSvrConn*  pConn = smsg->pConn;
dengyihao's avatar
formate  
dengyihao 已提交
388
  STransMsg* pMsg = &smsg->msg;
dengyihao's avatar
dengyihao 已提交
389 390 391 392 393
  if (pMsg->pCont == 0) {
    pMsg->pCont = (void*)rpcMallocCont(0);
    pMsg->contLen = 0;
  }
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
S
Shengliang Guan 已提交
394
  pHead->ahandle = (uint64_t)pMsg->info.ahandle;
dengyihao's avatar
dengyihao 已提交
395
  pHead->traceId = pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
396
  pHead->hasEpSet = pMsg->info.hasEpSet;
dengyihao's avatar
dengyihao 已提交
397

dengyihao's avatar
dengyihao 已提交
398
  if (pConn->status == ConnNormal) {
dengyihao's avatar
dengyihao 已提交
399
    pHead->msgType = (0 == pMsg->msgType ? pConn->inType + 1 : pMsg->msgType);
dengyihao's avatar
dengyihao 已提交
400
  } else {
dengyihao's avatar
dengyihao 已提交
401 402 403
    if (smsg->type == Release) {
      pHead->msgType = 0;
      pConn->status = ConnNormal;
dengyihao's avatar
dengyihao 已提交
404 405

      destroyConnRegArg(pConn);
dengyihao's avatar
dengyihao 已提交
406 407
      transUnrefSrvHandle(pConn);
    } else {
dengyihao's avatar
dengyihao 已提交
408
      // set up resp msg type
dengyihao's avatar
dengyihao 已提交
409
      pHead->msgType = (0 == pMsg->msgType ? pConn->inType + 1 : pMsg->msgType);
dengyihao's avatar
dengyihao 已提交
410
    }
dengyihao's avatar
dengyihao 已提交
411
  }
dengyihao's avatar
dengyihao 已提交
412

dengyihao's avatar
dengyihao 已提交
413
  pHead->release = smsg->type == Release ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
414
  pHead->code = htonl(pMsg->code);
dengyihao's avatar
dengyihao 已提交
415

dengyihao's avatar
dengyihao 已提交
416 417
  char*   msg = (char*)pHead;
  int32_t len = transMsgLenFromCont(pMsg->contLen);
dengyihao's avatar
dengyihao 已提交
418

dengyihao's avatar
dengyihao 已提交
419
  STrans*   pTransInst = pConn->pTransInst;
dengyihao's avatar
dengyihao 已提交
420
  STraceId* trace = &pMsg->info.traceId;
S
Shengliang Guan 已提交
421
  tGTrace("%s conn %p %s is sent to %s:%d, local info:%s:%d, msglen:%d", transLabel(pTransInst), pConn,
dengyihao's avatar
dengyihao 已提交
422 423
          TMSG_INFO(pHead->msgType), taosInetNtoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port),
          taosInetNtoa(pConn->localAddr.sin_addr), ntohs(pConn->localAddr.sin_port), len);
dengyihao's avatar
dengyihao 已提交
424
  pHead->msgLen = htonl(len);
dengyihao's avatar
dengyihao 已提交
425

dengyihao's avatar
dengyihao 已提交
426 427 428
  wb->base = msg;
  wb->len = len;
}
dengyihao's avatar
dengyihao 已提交
429

dengyihao's avatar
dengyihao 已提交
430
static void uvStartSendRespInternal(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
431 432 433 434 435
  SSvrConn* pConn = smsg->pConn;
  if (pConn->broken) {
    return;
  }

dengyihao's avatar
dengyihao 已提交
436 437 438
  uv_buf_t wb;
  uvPrepareSendData(smsg, &wb);

dengyihao's avatar
dengyihao 已提交
439
  transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
440
  uv_write_t* req = transReqQueuePush(&pConn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
441
  uv_write(req, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnSendCb);
dengyihao's avatar
dengyihao 已提交
442
}
dengyihao's avatar
dengyihao 已提交
443
static void uvStartSendResp(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
444
  // impl
dengyihao's avatar
dengyihao 已提交
445
  SSvrConn* pConn = smsg->pConn;
dengyihao's avatar
dengyihao 已提交
446
  if (pConn->broken == true) {
dengyihao's avatar
dengyihao 已提交
447
    // persist by
dengyihao's avatar
dengyihao 已提交
448 449
    transFreeMsg(smsg->msg.pCont);
    taosMemoryFree(smsg);
dengyihao's avatar
dengyihao 已提交
450 451 452
    transUnrefSrvHandle(pConn);
    return;
  }
dengyihao's avatar
dengyihao 已提交
453 454 455
  if (pConn->status == ConnNormal) {
    transUnrefSrvHandle(pConn);
  }
dengyihao's avatar
dengyihao 已提交
456

dengyihao's avatar
dengyihao 已提交
457
  if (!transQueuePush(&pConn->srvMsgs, smsg)) {
dengyihao's avatar
dengyihao 已提交
458 459 460
    return;
  }
  uvStartSendRespInternal(smsg);
dengyihao's avatar
dengyihao 已提交
461 462
  return;
}
dengyihao's avatar
dengyihao 已提交
463

dengyihao's avatar
dengyihao 已提交
464
static void destroySmsg(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
465 466 467 468
  if (smsg == NULL) {
    return;
  }
  transFreeMsg(smsg->msg.pCont);
wafwerar's avatar
wafwerar 已提交
469
  taosMemoryFree(smsg);
dengyihao's avatar
dengyihao 已提交
470
}
dengyihao's avatar
dengyihao 已提交
471
static void destroyAllConn(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
472
  tTrace("thread %p destroy all conn ", pThrd);
dengyihao's avatar
fix bug  
dengyihao 已提交
473 474 475 476 477
  while (!QUEUE_IS_EMPTY(&pThrd->conn)) {
    queue* h = QUEUE_HEAD(&pThrd->conn);
    QUEUE_REMOVE(h);
    QUEUE_INIT(h);

dengyihao's avatar
dengyihao 已提交
478
    SSvrConn* c = QUEUE_DATA(h, SSvrConn, queue);
dengyihao's avatar
dengyihao 已提交
479 480 481
    while (T_REF_VAL_GET(c) >= 2) {
      transUnrefSrvHandle(c);
    }
dengyihao's avatar
dengyihao 已提交
482
    transUnrefSrvHandle(c);
dengyihao's avatar
fix bug  
dengyihao 已提交
483 484
  }
}
dengyihao's avatar
dengyihao 已提交
485
void uvWorkerAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
486 487 488 489
  SAsyncItem* item = handle->data;
  SWorkThrd*  pThrd = item->pThrd;
  SSvrConn*   conn = NULL;
  queue       wq;
dengyihao's avatar
dengyihao 已提交
490

dengyihao's avatar
dengyihao 已提交
491
  // batch process to avoid to lock/unlock frequently
wafwerar's avatar
wafwerar 已提交
492
  taosThreadMutexLock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
493
  QUEUE_MOVE(&item->qmsg, &wq);
wafwerar's avatar
wafwerar 已提交
494
  taosThreadMutexUnlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
495 496 497 498

  while (!QUEUE_IS_EMPTY(&wq)) {
    queue* head = QUEUE_HEAD(&wq);
    QUEUE_REMOVE(head);
dengyihao's avatar
dengyihao 已提交
499

dengyihao's avatar
dengyihao 已提交
500
    SSvrMsg* msg = QUEUE_DATA(head, SSvrMsg, q);
dengyihao's avatar
dengyihao 已提交
501
    if (msg == NULL) {
dengyihao's avatar
dengyihao 已提交
502
      tError("unexcept occurred, continue");
dengyihao's avatar
dengyihao 已提交
503
      continue;
dengyihao's avatar
dengyihao 已提交
504
    }
dengyihao's avatar
dengyihao 已提交
505
    // release handle to rpc init
dengyihao's avatar
dengyihao 已提交
506 507
    if (msg->type == Quit) {
      (*transAsyncHandle[msg->type])(msg, pThrd);
dengyihao's avatar
dengyihao 已提交
508
      continue;
dengyihao's avatar
dengyihao 已提交
509 510 511
    } else {
      STransMsg transMsg = msg->msg;

S
Shengliang Guan 已提交
512 513
      SExHandle* exh1 = transMsg.info.handle;
      int64_t    refId = transMsg.info.refId;
dengyihao's avatar
dengyihao 已提交
514
      SExHandle* exh2 = transAcquireExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
515
      if (exh2 == NULL || exh1 != exh2) {
dengyihao's avatar
dengyihao 已提交
516
        tTrace("handle except msg %p, ignore it", exh1);
dengyihao's avatar
dengyihao 已提交
517
        transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
518 519 520 521
        destroySmsg(msg);
        continue;
      }
      msg->pConn = exh1->handle;
dengyihao's avatar
dengyihao 已提交
522
      transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
523
      (*transAsyncHandle[msg->type])(msg, pThrd);
dengyihao's avatar
dengyihao 已提交
524
    }
dengyihao's avatar
dengyihao 已提交
525 526
  }
}
dengyihao's avatar
dengyihao 已提交
527 528 529 530 531
static void uvWalkCb(uv_handle_t* handle, void* arg) {
  if (!uv_is_closing(handle)) {
    uv_close(handle, NULL);
  }
}
dengyihao's avatar
dengyihao 已提交
532 533 534 535
static void uvFreeCb(uv_handle_t* handle) {
  //
  taosMemoryFree(handle);
}
dengyihao's avatar
dengyihao 已提交
536

dengyihao's avatar
dengyihao 已提交
537 538
static void uvAcceptAsyncCb(uv_async_t* async) {
  SServerObj* srv = async->data;
dengyihao's avatar
dengyihao 已提交
539
  tDebug("close server port %d", srv->port);
dengyihao's avatar
dengyihao 已提交
540
  uv_walk(srv->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
541
}
dengyihao's avatar
dengyihao 已提交
542

dengyihao's avatar
dengyihao 已提交
543
static void uvShutDownCb(uv_shutdown_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
544
  if (status != 0) {
S
Shengliang Guan 已提交
545
    tDebug("conn failed to shut down:%s", uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
546
  }
dengyihao's avatar
dengyihao 已提交
547
  uv_close((uv_handle_t*)req->handle, uvDestroyConn);
wafwerar's avatar
wafwerar 已提交
548
  taosMemoryFree(req);
dengyihao's avatar
dengyihao 已提交
549 550
}

dengyihao's avatar
dengyihao 已提交
551 552 553
static void uvWorkDoTask(uv_work_t* req) {
  // doing time-consumeing task
  // only auth conn currently, add more func later
dengyihao's avatar
dengyihao 已提交
554
  tTrace("conn %p start to be processed in BG Thread", req->data);
dengyihao's avatar
dengyihao 已提交
555 556 557 558 559
  return;
}

static void uvWorkAfterTask(uv_work_t* req, int status) {
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
560
    tTrace("conn %p failed to processed ", req->data);
dengyihao's avatar
dengyihao 已提交
561 562 563 564
  }
  // Done time-consumeing task
  // add more func later
  // this func called in main loop
dengyihao's avatar
dengyihao 已提交
565
  tTrace("conn %p already processed ", req->data);
dengyihao's avatar
dengyihao 已提交
566 567 568
  taosMemoryFree(req);
}

dengyihao's avatar
dengyihao 已提交
569 570 571 572 573 574
void uvOnAcceptCb(uv_stream_t* stream, int status) {
  if (status == -1) {
    return;
  }
  SServerObj* pObj = container_of(stream, SServerObj, server);

wafwerar's avatar
wafwerar 已提交
575
  uv_tcp_t* cli = (uv_tcp_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
576 577 578
  uv_tcp_init(pObj->loop, cli);

  if (uv_accept(stream, (uv_stream_t*)cli) == 0) {
wafwerar's avatar
wafwerar 已提交
579
    if (pObj->numOfWorkerReady < pObj->numOfThreads) {
dengyihao's avatar
dengyihao 已提交
580 581
      tError("worker-threads are not ready for all, need %d instead of %d.", pObj->numOfThreads,
             pObj->numOfWorkerReady);
wafwerar's avatar
wafwerar 已提交
582 583 584
      uv_close((uv_handle_t*)cli, NULL);
      return;
    }
dengyihao's avatar
dengyihao 已提交
585

wafwerar's avatar
wafwerar 已提交
586
    uv_write_t* wr = (uv_write_t*)taosMemoryMalloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
587
    wr->data = cli;
dengyihao's avatar
dengyihao 已提交
588 589 590
    uv_buf_t buf = uv_buf_init((char*)notify, strlen(notify));

    pObj->workerIdx = (pObj->workerIdx + 1) % pObj->numOfThreads;
dengyihao's avatar
dengyihao 已提交
591

dengyihao's avatar
dengyihao 已提交
592
    tTrace("new conntion accepted by main server, dispatch to %dth worker-thread", pObj->workerIdx);
dengyihao's avatar
dengyihao 已提交
593

dengyihao's avatar
dengyihao 已提交
594
    uv_write2(wr, (uv_stream_t*)&(pObj->pipe[pObj->workerIdx][0]), &buf, 1, (uv_stream_t*)cli, uvOnPipeWriteCb);
dengyihao's avatar
dengyihao 已提交
595 596 597 598 599
  } else {
    uv_close((uv_handle_t*)cli, NULL);
  }
}
void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
600
  tTrace("connection coming");
dengyihao's avatar
dengyihao 已提交
601 602 603 604 605
  if (nread < 0) {
    if (nread != UV_EOF) {
      tError("read error %s", uv_err_name(nread));
    }
    // TODO(log other failure reason)
S
Shengliang Guan 已提交
606
    tWarn("failed to create connect:%p", q);
dengyihao's avatar
dengyihao 已提交
607 608 609
    taosMemoryFree(buf->base);
    uv_close((uv_handle_t*)q, NULL);
    // taosMemoryFree(q);
dengyihao's avatar
dengyihao 已提交
610 611 612 613 614
    return;
  }
  // free memory allocated by
  assert(nread == strlen(notify));
  assert(buf->base[0] == notify[0]);
wafwerar's avatar
wafwerar 已提交
615
  taosMemoryFree(buf->base);
dengyihao's avatar
dengyihao 已提交
616

dengyihao's avatar
dengyihao 已提交
617
  SWorkThrd* pThrd = q->data;
dengyihao's avatar
dengyihao 已提交
618 619 620 621 622 623 624 625 626 627

  uv_pipe_t* pipe = (uv_pipe_t*)q;
  if (!uv_pipe_pending_count(pipe)) {
    tError("No pending count");
    return;
  }

  uv_handle_type pending = uv_pipe_pending_type(pipe);
  assert(pending == UV_TCP);

dengyihao's avatar
dengyihao 已提交
628
  SSvrConn* pConn = createConn(pThrd);
629

dengyihao's avatar
dengyihao 已提交
630
  pConn->pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
631
  /* init conn timer*/
dengyihao's avatar
dengyihao 已提交
632 633
  // uv_timer_init(pThrd->loop, &pConn->pTimer);
  // pConn->pTimer.data = pConn;
dengyihao's avatar
dengyihao 已提交
634 635 636 637

  pConn->hostThrd = pThrd;

  // init client handle
wafwerar's avatar
wafwerar 已提交
638
  pConn->pTcp = (uv_tcp_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
639 640 641
  uv_tcp_init(pThrd->loop, pConn->pTcp);
  pConn->pTcp->data = pConn;

dengyihao's avatar
dengyihao 已提交
642 643
  transSetConnOption((uv_tcp_t*)pConn->pTcp);

dengyihao's avatar
dengyihao 已提交
644 645 646
  if (uv_accept(q, (uv_stream_t*)(pConn->pTcp)) == 0) {
    uv_os_fd_t fd;
    uv_fileno((const uv_handle_t*)pConn->pTcp, &fd);
S
Shengliang Guan 已提交
647
    tTrace("conn %p created, fd:%d", pConn, fd);
dengyihao's avatar
dengyihao 已提交
648

dengyihao's avatar
dengyihao 已提交
649 650
    int addrlen = sizeof(pConn->addr);
    if (0 != uv_tcp_getpeername(pConn->pTcp, (struct sockaddr*)&pConn->addr, &addrlen)) {
dengyihao's avatar
dengyihao 已提交
651
      tError("conn %p failed to get peer info", pConn);
dengyihao's avatar
dengyihao 已提交
652
      transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
653
      return;
dengyihao's avatar
dengyihao 已提交
654
    }
dengyihao's avatar
dengyihao 已提交
655

dengyihao's avatar
dengyihao 已提交
656 657
    addrlen = sizeof(pConn->localAddr);
    if (0 != uv_tcp_getsockname(pConn->pTcp, (struct sockaddr*)&pConn->localAddr, &addrlen)) {
dengyihao's avatar
dengyihao 已提交
658
      tError("conn %p failed to get local info", pConn);
dengyihao's avatar
dengyihao 已提交
659
      transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
660 661 662
      return;
    }

dengyihao's avatar
dengyihao 已提交
663
    uv_read_start((uv_stream_t*)(pConn->pTcp), uvAllocRecvBufferCb, uvOnRecvCb);
dengyihao's avatar
dengyihao 已提交
664

dengyihao's avatar
dengyihao 已提交
665
  } else {
dengyihao's avatar
dengyihao 已提交
666
    tDebug("failed to create new connection");
dengyihao's avatar
dengyihao 已提交
667
    transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
668 669 670
  }
}

671
void* transAcceptThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
672
  // opt
dengyihao's avatar
dengyihao 已提交
673
  setThreadName("trans-accept");
dengyihao's avatar
dengyihao 已提交
674 675
  SServerObj* srv = (SServerObj*)arg;
  uv_run(srv->loop, UV_RUN_DEFAULT);
676 677

  return NULL;
dengyihao's avatar
dengyihao 已提交
678
}
dengyihao's avatar
dengyihao 已提交
679
void uvOnPipeConnectionCb(uv_connect_t* connect, int status) {
wafwerar's avatar
wafwerar 已提交
680 681 682
  if (status != 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
683
  SWorkThrd* pThrd = container_of(connect, SWorkThrd, connect_req);
wafwerar's avatar
wafwerar 已提交
684 685
  uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb);
}
dengyihao's avatar
dengyihao 已提交
686
static bool addHandleToWorkloop(SWorkThrd* pThrd, char* pipeName) {
wafwerar's avatar
wafwerar 已提交
687
  pThrd->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
688 689 690
  if (0 != uv_loop_init(pThrd->loop)) {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
691

dengyihao's avatar
dengyihao 已提交
692
  uv_pipe_init(pThrd->loop, pThrd->pipe, 1);
wafwerar's avatar
wafwerar 已提交
693
  // int r = uv_pipe_open(pThrd->pipe, pThrd->fd);
dengyihao's avatar
dengyihao 已提交
694 695 696

  pThrd->pipe->data = pThrd;

dengyihao's avatar
dengyihao 已提交
697
  QUEUE_INIT(&pThrd->msg);
wafwerar's avatar
wafwerar 已提交
698
  taosThreadMutexInit(&pThrd->msgMtx, NULL);
dengyihao's avatar
dengyihao 已提交
699

dengyihao's avatar
fix bug  
dengyihao 已提交
700 701 702
  // conn set
  QUEUE_INIT(&pThrd->conn);

dengyihao's avatar
dengyihao 已提交
703
  pThrd->asyncPool = transAsyncPoolCreate(pThrd->loop, 1, pThrd, uvWorkerAsyncCb);
wafwerar's avatar
wafwerar 已提交
704 705
  uv_pipe_connect(&pThrd->connect_req, pThrd->pipe, pipeName, uvOnPipeConnectionCb);
  // uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb);
dengyihao's avatar
dengyihao 已提交
706 707 708 709 710 711 712 713 714
  return true;
}

static bool addHandleToAcceptloop(void* arg) {
  // impl later
  SServerObj* srv = arg;

  int err = 0;
  if ((err = uv_tcp_init(srv->loop, &srv->server)) != 0) {
S
Shengliang Guan 已提交
715
    tError("failed to init accept server:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
716 717 718
    return false;
  }

dengyihao's avatar
dengyihao 已提交
719
  // register an async here to quit server gracefully
wafwerar's avatar
wafwerar 已提交
720
  srv->pAcceptAsync = taosMemoryCalloc(1, sizeof(uv_async_t));
dengyihao's avatar
dengyihao 已提交
721 722
  uv_async_init(srv->loop, srv->pAcceptAsync, uvAcceptAsyncCb);
  srv->pAcceptAsync->data = srv;
dengyihao's avatar
dengyihao 已提交
723

dengyihao's avatar
dengyihao 已提交
724
  struct sockaddr_in bind_addr;
dengyihao's avatar
dengyihao 已提交
725 726
  uv_ip4_addr("0.0.0.0", srv->port, &bind_addr);
  if ((err = uv_tcp_bind(&srv->server, (const struct sockaddr*)&bind_addr, 0)) != 0) {
S
Shengliang Guan 已提交
727
    tError("failed to bind:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
728 729
    return false;
  }
dengyihao's avatar
dengyihao 已提交
730
  if ((err = uv_listen((uv_stream_t*)&srv->server, 512, uvOnAcceptCb)) != 0) {
S
Shengliang Guan 已提交
731
    tError("failed to listen:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
732
    terrno = TSDB_CODE_RPC_PORT_EADDRINUSE;
dengyihao's avatar
dengyihao 已提交
733 734 735
    return false;
  }
  return true;
dengyihao's avatar
dengyihao 已提交
736
}
737
void* transWorkerThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
738
  setThreadName("trans-worker");
dengyihao's avatar
dengyihao 已提交
739
  SWorkThrd* pThrd = (SWorkThrd*)arg;
dengyihao's avatar
dengyihao 已提交
740
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
741 742

  return NULL;
dengyihao's avatar
dengyihao 已提交
743 744
}

dengyihao's avatar
dengyihao 已提交
745
static SSvrConn* createConn(void* hThrd) {
dengyihao's avatar
dengyihao 已提交
746
  SWorkThrd* pThrd = hThrd;
dengyihao's avatar
fix bug  
dengyihao 已提交
747

dengyihao's avatar
dengyihao 已提交
748
  SSvrConn* pConn = (SSvrConn*)taosMemoryCalloc(1, sizeof(SSvrConn));
dengyihao's avatar
dengyihao 已提交
749

dengyihao's avatar
dengyihao 已提交
750
  transReqQueueInit(&pConn->wreqQueue);
dengyihao's avatar
fix bug  
dengyihao 已提交
751 752 753
  QUEUE_INIT(&pConn->queue);

  QUEUE_PUSH(&pThrd->conn, &pConn->queue);
dengyihao's avatar
dengyihao 已提交
754 755

  transQueueInit(&pConn->srvMsgs, NULL);
dengyihao's avatar
dengyihao 已提交
756

dengyihao's avatar
dengyihao 已提交
757
  memset(&pConn->regArg, 0, sizeof(pConn->regArg));
dengyihao's avatar
dengyihao 已提交
758
  pConn->broken = false;
dengyihao's avatar
dengyihao 已提交
759
  pConn->status = ConnNormal;
dengyihao's avatar
dengyihao 已提交
760

dengyihao's avatar
dengyihao 已提交
761 762 763
  SExHandle* exh = taosMemoryMalloc(sizeof(SExHandle));
  exh->handle = pConn;
  exh->pThrd = pThrd;
dengyihao's avatar
dengyihao 已提交
764 765
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  transAcquireExHandle(transGetRefMgt(), exh->refId);
dengyihao's avatar
dengyihao 已提交
766

dengyihao's avatar
dengyihao 已提交
767
  STrans* pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
768
  pConn->refId = exh->refId;
dengyihao's avatar
dengyihao 已提交
769
  transRefSrvHandle(pConn);
S
Shengliang Guan 已提交
770
  tTrace("%s handle %p, conn %p created, refId:%" PRId64, transLabel(pTransInst), exh, pConn, pConn->refId);
dengyihao's avatar
dengyihao 已提交
771 772
  return pConn;
}
dengyihao's avatar
dengyihao 已提交
773

dengyihao's avatar
dengyihao 已提交
774
static void destroyConn(SSvrConn* conn, bool clear) {
dengyihao's avatar
dengyihao 已提交
775 776 777
  if (conn == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
778

dengyihao's avatar
dengyihao 已提交
779
  transDestroyBuffer(&conn->readBuf);
780
  if (clear) {
dengyihao's avatar
dengyihao 已提交
781
    if (!uv_is_closing((uv_handle_t*)conn->pTcp)) {
dengyihao's avatar
dengyihao 已提交
782 783 784
      tTrace("conn %p to be destroyed", conn);
      uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn);
    }
785
  }
dengyihao's avatar
dengyihao 已提交
786
}
dengyihao's avatar
dengyihao 已提交
787
static void destroyConnRegArg(SSvrConn* conn) {
dengyihao's avatar
dengyihao 已提交
788 789 790 791 792
  if (conn->regArg.init == 1) {
    transFreeMsg(conn->regArg.msg.pCont);
    conn->regArg.init = 0;
  }
}
dengyihao's avatar
dengyihao 已提交
793
static int reallocConnRef(SSvrConn* conn) {
dengyihao's avatar
dengyihao 已提交
794 795
  transReleaseExHandle(transGetRefMgt(), conn->refId);
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
796 797 798 799
  // avoid app continue to send msg on invalid handle
  SExHandle* exh = taosMemoryMalloc(sizeof(SExHandle));
  exh->handle = conn;
  exh->pThrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
800 801
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  transAcquireExHandle(transGetRefMgt(), exh->refId);
dengyihao's avatar
dengyihao 已提交
802 803 804 805
  conn->refId = exh->refId;

  return 0;
}
dengyihao's avatar
dengyihao 已提交
806
static void uvDestroyConn(uv_handle_t* handle) {
dengyihao's avatar
dengyihao 已提交
807
  SSvrConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
808 809 810
  if (conn == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
811
  SWorkThrd* thrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
812

dengyihao's avatar
dengyihao 已提交
813 814
  transReleaseExHandle(transGetRefMgt(), conn->refId);
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
815

dengyihao's avatar
dengyihao 已提交
816 817
  STrans* pTransInst = thrd->pTransInst;
  tDebug("%s conn %p destroy", transLabel(pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
818

dengyihao's avatar
dengyihao 已提交
819 820 821 822
  for (int i = 0; i < transQueueSize(&conn->srvMsgs); i++) {
    SSvrMsg* msg = transQueueGet(&conn->srvMsgs, i);
    destroySmsg(msg);
  }
dengyihao's avatar
dengyihao 已提交
823

dengyihao's avatar
dengyihao 已提交
824
  transReqQueueClear(&conn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
825
  transQueueDestroy(&conn->srvMsgs);
dengyihao's avatar
dengyihao 已提交
826

dengyihao's avatar
dengyihao 已提交
827
  QUEUE_REMOVE(&conn->queue);
wafwerar's avatar
wafwerar 已提交
828
  taosMemoryFree(conn->pTcp);
dengyihao's avatar
dengyihao 已提交
829
  destroyConnRegArg(conn);
dengyihao's avatar
dengyihao 已提交
830
  taosMemoryFree(conn);
dengyihao's avatar
dengyihao 已提交
831

dengyihao's avatar
dengyihao 已提交
832
  if (thrd->quit && QUEUE_IS_EMPTY(&thrd->conn)) {
dengyihao's avatar
dengyihao 已提交
833
    tTrace("work thread quit");
dengyihao's avatar
dengyihao 已提交
834
    uv_walk(thrd->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
835
  }
dengyihao's avatar
dengyihao 已提交
836
}
wafwerar's avatar
wafwerar 已提交
837 838 839 840
static void uvPipeListenCb(uv_stream_t* handle, int status) {
  ASSERT(status == 0);

  SServerObj* srv = container_of(handle, SServerObj, pipeListen);
dengyihao's avatar
dengyihao 已提交
841
  uv_pipe_t*  pipe = &(srv->pipe[srv->numOfWorkerReady][0]);
wafwerar's avatar
wafwerar 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855
  ASSERT(0 == uv_pipe_init(srv->loop, pipe, 1));
  ASSERT(0 == uv_accept((uv_stream_t*)&srv->pipeListen, (uv_stream_t*)pipe));

  ASSERT(1 == uv_is_readable((uv_stream_t*)pipe));
  ASSERT(1 == uv_is_writable((uv_stream_t*)pipe));
  ASSERT(0 == uv_is_closing((uv_handle_t*)pipe));

  srv->numOfWorkerReady++;

  // ASSERT(0 == uv_listen((uv_stream_t*)&ctx.send.tcp, 512, uvOnAcceptCb));

  // r = uv_read_start((uv_stream_t*)&ctx.channel, alloc_cb, read_cb);
  // ASSERT(r == 0);
}
dengyihao's avatar
dengyihao 已提交
856

U
ubuntu 已提交
857
void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
wafwerar's avatar
wafwerar 已提交
858 859
  SServerObj* srv = taosMemoryCalloc(1, sizeof(SServerObj));
  srv->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
860 861
  srv->numOfThreads = numOfThreads;
  srv->workerIdx = 0;
wafwerar's avatar
wafwerar 已提交
862
  srv->numOfWorkerReady = 0;
dengyihao's avatar
dengyihao 已提交
863
  srv->pThreadObj = (SWorkThrd**)taosMemoryCalloc(srv->numOfThreads, sizeof(SWorkThrd*));
wafwerar's avatar
wafwerar 已提交
864
  srv->pipe = (uv_pipe_t**)taosMemoryCalloc(srv->numOfThreads, sizeof(uv_pipe_t*));
dengyihao's avatar
dengyihao 已提交
865 866 867 868
  srv->ip = ip;
  srv->port = port;
  uv_loop_init(srv->loop);

wafwerar's avatar
wafwerar 已提交
869 870
  assert(0 == uv_pipe_init(srv->loop, &srv->pipeListen, 0));
#ifdef WINDOWS
871
  char pipeName[64];
S
Shengliang Guan 已提交
872
  snprintf(pipeName, sizeof(pipeName), "\\\\?\\pipe\\trans.rpc.%p-" PRIu64, taosSafeRand(), GetCurrentProcessId());
wafwerar's avatar
wafwerar 已提交
873
#else
874
  char pipeName[PATH_MAX] = {0};
S
Shengliang Guan 已提交
875
  snprintf(pipeName, sizeof(pipeName), "%s%spipe.trans.rpc.%08X-" PRIu64, tsTempDir, TD_DIRSEP, taosSafeRand(),
dengyihao's avatar
dengyihao 已提交
876
           taosGetSelfPthreadId());
wafwerar's avatar
wafwerar 已提交
877 878 879 880
#endif
  assert(0 == uv_pipe_bind(&srv->pipeListen, pipeName));
  assert(0 == uv_listen((uv_stream_t*)&srv->pipeListen, SOMAXCONN, uvPipeListenCb));

dengyihao's avatar
dengyihao 已提交
881
  for (int i = 0; i < srv->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
882
    SWorkThrd* thrd = (SWorkThrd*)taosMemoryCalloc(1, sizeof(SWorkThrd));
dengyihao's avatar
dengyihao 已提交
883
    thrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
884
    thrd->quit = false;
dengyihao's avatar
dengyihao 已提交
885
    srv->pThreadObj[i] = thrd;
dengyihao's avatar
dengyihao 已提交
886
    thrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
887

wafwerar's avatar
wafwerar 已提交
888
    srv->pipe[i] = (uv_pipe_t*)taosMemoryCalloc(2, sizeof(uv_pipe_t));
dengyihao's avatar
dengyihao 已提交
889
    thrd->pipe = &(srv->pipe[i][1]);  // init read
dengyihao's avatar
dengyihao 已提交
890

dengyihao's avatar
dengyihao 已提交
891
    if (false == addHandleToWorkloop(thrd, pipeName)) {
dengyihao's avatar
dengyihao 已提交
892 893
      goto End;
    }
894
    int err = taosThreadCreate(&(thrd->thread), NULL, transWorkerThread, (void*)(thrd));
dengyihao's avatar
dengyihao 已提交
895
    if (err == 0) {
S
Shengliang Guan 已提交
896
      tDebug("success to create worker-thread:%d", i);
dengyihao's avatar
dengyihao 已提交
897 898
    } else {
      // TODO: clear all other resource later
S
Shengliang Guan 已提交
899
      tError("failed to create worker-thread:%d", i);
dengyihao's avatar
dengyihao 已提交
900
      goto End;
dengyihao's avatar
dengyihao 已提交
901 902
    }
  }
903
  if (false == taosValidIpAndPort(srv->ip, srv->port)) {
dengyihao's avatar
dengyihao 已提交
904
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
905
    tError("invalid ip/port, %d:%d, reason:%s", srv->ip, srv->port, terrstr());
906 907
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
908 909 910
  if (false == addHandleToAcceptloop(srv)) {
    goto End;
  }
911
  int err = taosThreadCreate(&srv->thread, NULL, transAcceptThread, (void*)srv);
dengyihao's avatar
dengyihao 已提交
912 913 914
  if (err == 0) {
    tDebug("success to create accept-thread");
  } else {
dengyihao's avatar
dengyihao 已提交
915 916
    tError("failed  to create accept-thread");
    goto End;
dengyihao's avatar
dengyihao 已提交
917 918
    // clear all resource later
  }
dengyihao's avatar
dengyihao 已提交
919
  srv->inited = true;
dengyihao's avatar
dengyihao 已提交
920
  return srv;
dengyihao's avatar
dengyihao 已提交
921
End:
U
ubuntu 已提交
922
  transCloseServer(srv);
dengyihao's avatar
dengyihao 已提交
923 924
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
925

dengyihao's avatar
dengyihao 已提交
926
void uvHandleQuit(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
927
  thrd->quit = true;
dengyihao's avatar
dengyihao 已提交
928
  if (QUEUE_IS_EMPTY(&thrd->conn)) {
dengyihao's avatar
dengyihao 已提交
929
    uv_walk(thrd->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
930 931 932
  } else {
    destroyAllConn(thrd);
  }
wafwerar's avatar
wafwerar 已提交
933
  taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
934
}
dengyihao's avatar
dengyihao 已提交
935
void uvHandleRelease(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
936
  SSvrConn* conn = msg->pConn;
dengyihao's avatar
dengyihao 已提交
937
  if (conn->status == ConnAcquire) {
dengyihao's avatar
dengyihao 已提交
938
    reallocConnRef(conn);
dengyihao's avatar
dengyihao 已提交
939
    if (!transQueuePush(&conn->srvMsgs, msg)) {
dengyihao's avatar
dengyihao 已提交
940
      return;
dengyihao's avatar
dengyihao 已提交
941 942 943
    }
    uvStartSendRespInternal(msg);
    return;
dengyihao's avatar
dengyihao 已提交
944
  } else if (conn->status == ConnRelease || conn->status == ConnNormal) {
dengyihao's avatar
dengyihao 已提交
945
    tDebug("%s conn %p already released, ignore release-msg", transLabel(thrd->pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
946
  }
dengyihao's avatar
dengyihao 已提交
947
  destroySmsg(msg);
dengyihao's avatar
dengyihao 已提交
948
}
dengyihao's avatar
dengyihao 已提交
949
void uvHandleResp(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
950
  // send msg to client
dengyihao's avatar
dengyihao 已提交
951
  tDebug("%s conn %p start to send resp (2/2)", transLabel(thrd->pTransInst), msg->pConn);
dengyihao's avatar
dengyihao 已提交
952 953
  uvStartSendResp(msg);
}
dengyihao's avatar
dengyihao 已提交
954
void uvHandleRegister(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
955
  SSvrConn* conn = msg->pConn;
dengyihao's avatar
dengyihao 已提交
956
  tDebug("%s conn %p register brokenlink callback", transLabel(thrd->pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
957
  if (conn->status == ConnAcquire) {
dengyihao's avatar
dengyihao 已提交
958
    if (!transQueuePush(&conn->srvMsgs, msg)) {
dengyihao's avatar
dengyihao 已提交
959 960
      return;
    }
dengyihao's avatar
dengyihao 已提交
961
    transQueuePop(&conn->srvMsgs);
dengyihao's avatar
dengyihao 已提交
962 963 964
    conn->regArg.notifyCount = 0;
    conn->regArg.init = 1;
    conn->regArg.msg = msg->msg;
dengyihao's avatar
dengyihao 已提交
965
    tDebug("conn %p register brokenlink callback succ", conn);
dengyihao's avatar
dengyihao 已提交
966 967 968 969 970 971

    if (conn->broken) {
      STrans* pTransInst = conn->pTransInst;
      (*pTransInst->cfp)(pTransInst->parent, &(conn->regArg.msg), NULL);
      memset(&conn->regArg, 0, sizeof(conn->regArg));
    }
wafwerar's avatar
wafwerar 已提交
972
    taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
973 974
  }
}
dengyihao's avatar
dengyihao 已提交
975
void destroyWorkThrd(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
976 977 978
  if (pThrd == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
979
  taosThreadJoin(pThrd->thread, NULL);
dengyihao's avatar
dengyihao 已提交
980
  SRV_RELEASE_UV(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
981
  TRANS_DESTROY_ASYNC_POOL_MSG(pThrd->asyncPool, SSvrMsg, destroySmsg);
dengyihao's avatar
dengyihao 已提交
982
  transAsyncPoolDestroy(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
983
  taosMemoryFree(pThrd->loop);
wafwerar's avatar
wafwerar 已提交
984
  taosMemoryFree(pThrd);
dengyihao's avatar
dengyihao 已提交
985
}
dengyihao's avatar
dengyihao 已提交
986
void sendQuitToWorkThrd(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
987
  SSvrMsg* msg = taosMemoryCalloc(1, sizeof(SSvrMsg));
dengyihao's avatar
dengyihao 已提交
988
  msg->type = Quit;
dengyihao's avatar
dengyihao 已提交
989
  tDebug("server send quit msg to work thread");
dengyihao's avatar
dengyihao 已提交
990
  transAsyncSend(pThrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
991 992
}

U
ubuntu 已提交
993
void transCloseServer(void* arg) {
dengyihao's avatar
dengyihao 已提交
994 995
  // impl later
  SServerObj* srv = arg;
dengyihao's avatar
dengyihao 已提交
996 997

  tDebug("send quit msg to accept thread");
dengyihao's avatar
dengyihao 已提交
998 999 1000 1001
  if (srv->inited) {
    uv_async_send(srv->pAcceptAsync);
    taosThreadJoin(srv->thread, NULL);
  }
dengyihao's avatar
dengyihao 已提交
1002
  SRV_RELEASE_UV(srv->loop);
dengyihao's avatar
dengyihao 已提交
1003

dengyihao's avatar
dengyihao 已提交
1004 1005 1006 1007 1008
  for (int i = 0; i < srv->numOfThreads; i++) {
    sendQuitToWorkThrd(srv->pThreadObj[i]);
    destroyWorkThrd(srv->pThreadObj[i]);
  }

wafwerar's avatar
wafwerar 已提交
1009 1010 1011
  taosMemoryFree(srv->pThreadObj);
  taosMemoryFree(srv->pAcceptAsync);
  taosMemoryFree(srv->loop);
dengyihao's avatar
dengyihao 已提交
1012 1013

  for (int i = 0; i < srv->numOfThreads; i++) {
wafwerar's avatar
wafwerar 已提交
1014
    taosMemoryFree(srv->pipe[i]);
dengyihao's avatar
dengyihao 已提交
1015
  }
wafwerar's avatar
wafwerar 已提交
1016
  taosMemoryFree(srv->pipe);
dengyihao's avatar
dengyihao 已提交
1017

wafwerar's avatar
wafwerar 已提交
1018
  taosMemoryFree(srv);
dengyihao's avatar
dengyihao 已提交
1019
}
dengyihao's avatar
dengyihao 已提交
1020

dengyihao's avatar
dengyihao 已提交
1021 1022 1023 1024
void transRefSrvHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1025
  int ref = T_REF_INC((SSvrConn*)handle);
dengyihao's avatar
dengyihao 已提交
1026
  tTrace("conn %p ref count:%d", handle, ref);
dengyihao's avatar
dengyihao 已提交
1027 1028 1029 1030 1031 1032
}

void transUnrefSrvHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1033
  int ref = T_REF_DEC((SSvrConn*)handle);
dengyihao's avatar
dengyihao 已提交
1034
  tTrace("conn %p ref count:%d", handle, ref);
dengyihao's avatar
dengyihao 已提交
1035
  if (ref == 0) {
dengyihao's avatar
dengyihao 已提交
1036
    destroyConn((SSvrConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
1037 1038
  }
}
dengyihao's avatar
dengyihao 已提交
1039

dengyihao's avatar
dengyihao 已提交
1040
int transReleaseSrvHandle(void* handle) {
dengyihao's avatar
dengyihao 已提交
1041 1042 1043
  SRpcHandleInfo* info = handle;
  SExHandle*      exh = info->handle;
  int64_t         refId = info->refId;
dengyihao's avatar
dengyihao 已提交
1044

dengyihao's avatar
dengyihao 已提交
1045
  ASYNC_CHECK_HANDLE(exh, refId);
dengyihao's avatar
dengyihao 已提交
1046

dengyihao's avatar
dengyihao 已提交
1047
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1048 1049
  ASYNC_ERR_JRET(pThrd);

S
Shengliang Guan 已提交
1050
  STransMsg tmsg = {.code = 0, .info.handle = exh, .info.ahandle = NULL, .info.refId = refId};
dengyihao's avatar
dengyihao 已提交
1051

dengyihao's avatar
dengyihao 已提交
1052 1053 1054
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Release;
dengyihao's avatar
dengyihao 已提交
1055

dengyihao's avatar
dengyihao 已提交
1056
  tTrace("%s conn %p start to release", transLabel(pThrd->pTransInst), exh->handle);
dengyihao's avatar
dengyihao 已提交
1057
  transAsyncSend(pThrd->asyncPool, &m->q);
dengyihao's avatar
dengyihao 已提交
1058
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1059
  return 0;
dengyihao's avatar
dengyihao 已提交
1060
_return1:
dengyihao's avatar
dengyihao 已提交
1061
  tTrace("handle %p failed to send to release handle", exh);
dengyihao's avatar
dengyihao 已提交
1062
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1063
  return -1;
dengyihao's avatar
dengyihao 已提交
1064
_return2:
dengyihao's avatar
dengyihao 已提交
1065
  tTrace("handle %p failed to send to release handle", exh);
dengyihao's avatar
dengyihao 已提交
1066
  return -1;
dengyihao's avatar
dengyihao 已提交
1067
}
dengyihao's avatar
dengyihao 已提交
1068
int transSendResponse(const STransMsg* msg) {
S
Shengliang Guan 已提交
1069 1070
  SExHandle* exh = msg->info.handle;
  int64_t    refId = msg->info.refId;
dengyihao's avatar
dengyihao 已提交
1071
  ASYNC_CHECK_HANDLE(exh, refId);
dengyihao's avatar
dengyihao 已提交
1072
  assert(refId != 0);
dengyihao's avatar
dengyihao 已提交
1073

dengyihao's avatar
dengyihao 已提交
1074
  STransMsg tmsg = *msg;
S
Shengliang Guan 已提交
1075
  tmsg.info.refId = refId;
dengyihao's avatar
dengyihao 已提交
1076

dengyihao's avatar
dengyihao 已提交
1077
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1078
  ASYNC_ERR_JRET(pThrd);
dengyihao's avatar
dengyihao 已提交
1079

dengyihao's avatar
dengyihao 已提交
1080 1081 1082
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Normal;
dengyihao's avatar
dengyihao 已提交
1083 1084 1085

  STraceId* trace = (STraceId*)&msg->info.traceId;
  tGTrace("conn %p start to send resp (1/2)", exh->handle);
dengyihao's avatar
dengyihao 已提交
1086
  transAsyncSend(pThrd->asyncPool, &m->q);
dengyihao's avatar
dengyihao 已提交
1087
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1088
  return 0;
dengyihao's avatar
dengyihao 已提交
1089
_return1:
dengyihao's avatar
dengyihao 已提交
1090
  tTrace("handle %p failed to send resp", exh);
dengyihao's avatar
dengyihao 已提交
1091
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1092
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1093
  return -1;
dengyihao's avatar
dengyihao 已提交
1094
_return2:
dengyihao's avatar
dengyihao 已提交
1095
  tTrace("handle %p failed to send resp", exh);
dengyihao's avatar
dengyihao 已提交
1096
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1097
  return -1;
dengyihao's avatar
dengyihao 已提交
1098
}
dengyihao's avatar
dengyihao 已提交
1099
int transRegisterMsg(const STransMsg* msg) {
S
Shengliang Guan 已提交
1100 1101
  SExHandle* exh = msg->info.handle;
  int64_t    refId = msg->info.refId;
dengyihao's avatar
dengyihao 已提交
1102 1103
  ASYNC_CHECK_HANDLE(exh, refId);

dengyihao's avatar
dengyihao 已提交
1104
  STransMsg tmsg = *msg;
S
Shengliang Guan 已提交
1105
  tmsg.info.refId = refId;
dengyihao's avatar
dengyihao 已提交
1106

dengyihao's avatar
dengyihao 已提交
1107
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1108
  ASYNC_ERR_JRET(pThrd);
dengyihao's avatar
dengyihao 已提交
1109

dengyihao's avatar
dengyihao 已提交
1110 1111 1112
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Register;
dengyihao's avatar
dengyihao 已提交
1113

dengyihao's avatar
dengyihao 已提交
1114 1115
  STrans* pTransInst = pThrd->pTransInst;
  tTrace("%s conn %p start to register brokenlink callback", transLabel(pTransInst), exh->handle);
dengyihao's avatar
dengyihao 已提交
1116
  transAsyncSend(pThrd->asyncPool, &m->q);
dengyihao's avatar
dengyihao 已提交
1117
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1118
  return 0;
dengyihao's avatar
dengyihao 已提交
1119

dengyihao's avatar
dengyihao 已提交
1120
_return1:
dengyihao's avatar
dengyihao 已提交
1121
  tTrace("handle %p failed to register brokenlink", exh);
dengyihao's avatar
dengyihao 已提交
1122
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1123
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1124
  return -1;
dengyihao's avatar
dengyihao 已提交
1125
_return2:
dengyihao's avatar
dengyihao 已提交
1126
  tTrace("handle %p failed to register brokenlink", exh);
dengyihao's avatar
dengyihao 已提交
1127
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1128
  return -1;
dengyihao's avatar
dengyihao 已提交
1129
}
dengyihao's avatar
dengyihao 已提交
1130

D
dapan1121 已提交
1131 1132
int transGetConnInfo(void* thandle, STransHandleInfo* pConnInfo) { return -1; }

dengyihao's avatar
dengyihao 已提交
1133
#endif