transSvr.c 38.2 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 47 48 49 50 51 52
  ConnStatus status;

  uint32_t clientIp;
  uint16_t port;

  char src[32];
  char dst[32];
U
ubuntu 已提交
53

dengyihao's avatar
dengyihao 已提交
54 55 56 57 58 59
  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 已提交
60
} SSvrConn;
dengyihao's avatar
dengyihao 已提交
61

dengyihao's avatar
dengyihao 已提交
62 63
typedef struct SSvrMsg {
  SSvrConn*     pConn;
dengyihao's avatar
dengyihao 已提交
64 65 66
  STransMsg     msg;
  queue         q;
  STransMsgType type;
dengyihao's avatar
dengyihao 已提交
67
} SSvrMsg;
dengyihao's avatar
dengyihao 已提交
68

dengyihao's avatar
dengyihao 已提交
69
typedef struct SWorkThrd {
dengyihao's avatar
dengyihao 已提交
70
  TdThread      thread;
wafwerar's avatar
wafwerar 已提交
71
  uv_connect_t  connect_req;
dengyihao's avatar
dengyihao 已提交
72 73 74 75
  uv_pipe_t*    pipe;
  uv_os_fd_t    fd;
  uv_loop_t*    loop;
  SAsyncPool*   asyncPool;
dengyihao's avatar
dengyihao 已提交
76
  uv_prepare_t* prepare;
dengyihao's avatar
dengyihao 已提交
77
  queue         msg;
dengyihao's avatar
dengyihao 已提交
78 79 80 81

  queue conn;
  void* pTransInst;
  bool  quit;
dengyihao's avatar
dengyihao 已提交
82
} SWorkThrd;
dengyihao's avatar
dengyihao 已提交
83 84

typedef struct SServerObj {
dengyihao's avatar
dengyihao 已提交
85
  TdThread   thread;
dengyihao's avatar
dengyihao 已提交
86 87 88 89
  uv_tcp_t   server;
  uv_loop_t* loop;

  // work thread info
dengyihao's avatar
dengyihao 已提交
90 91 92 93
  int         workerIdx;
  int         numOfThreads;
  int         numOfWorkerReady;
  SWorkThrd** pThreadObj;
dengyihao's avatar
dengyihao 已提交
94

wafwerar's avatar
wafwerar 已提交
95
  uv_pipe_t   pipeListen;
dengyihao's avatar
dengyihao 已提交
96 97 98 99
  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 已提交
100 101

  bool inited;
dengyihao's avatar
dengyihao 已提交
102 103 104
} SServerObj;

static void uvAllocConnBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
105
static void uvAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
106
static void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
dengyihao's avatar
dengyihao 已提交
107
static void uvOnTimeoutCb(uv_timer_t* handle);
dengyihao's avatar
dengyihao 已提交
108
static void uvOnSendCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
109
static void uvOnPipeWriteCb(uv_write_t* req, int status);
dengyihao's avatar
dengyihao 已提交
110 111 112
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 已提交
113
static void uvAcceptAsyncCb(uv_async_t* handle);
dengyihao's avatar
dengyihao 已提交
114
static void uvShutDownCb(uv_shutdown_t* req, int status);
dengyihao's avatar
dengyihao 已提交
115
static void uvPrepareCb(uv_prepare_t* handle);
dengyihao's avatar
dengyihao 已提交
116

dengyihao's avatar
dengyihao 已提交
117 118
static bool uvRecvReleaseReq(SSvrConn* conn, STransMsgHead* pHead);

dengyihao's avatar
dengyihao 已提交
119 120 121 122 123 124
/*
 * 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 已提交
125 126
static void uvWalkCb(uv_handle_t* handle, void* arg);
static void uvFreeCb(uv_handle_t* handle);
dengyihao's avatar
dengyihao 已提交
127

dengyihao's avatar
dengyihao 已提交
128 129
static FORCE_INLINE void uvStartSendRespImpl(SSvrMsg* smsg);

dengyihao's avatar
dengyihao 已提交
130 131
static void uvPrepareSendData(SSvrMsg* msg, uv_buf_t* wb);
static void uvStartSendResp(SSvrMsg* msg);
dengyihao's avatar
dengyihao 已提交
132

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

dengyihao's avatar
dengyihao 已提交
135 136 137 138
static FORCE_INLINE void      destroySmsg(SSvrMsg* smsg);
static FORCE_INLINE SSvrConn* createConn(void* hThrd);
static FORCE_INLINE void      destroyConn(SSvrConn* conn, bool clear /*clear handle or not*/);
static FORCE_INLINE void      destroyConnRegArg(SSvrConn* conn);
dengyihao's avatar
dengyihao 已提交
139

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

dengyihao's avatar
dengyihao 已提交
142 143 144 145 146 147
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 已提交
148

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

dengyihao's avatar
dengyihao 已提交
151
// server and worker thread
152 153
static void* transWorkerThread(void* arg);
static void* transAcceptThread(void* arg);
dengyihao's avatar
dengyihao 已提交
154

dengyihao's avatar
dengyihao 已提交
155
// add handle loop
dengyihao's avatar
dengyihao 已提交
156
static bool addHandleToWorkloop(SWorkThrd* pThrd, char* pipeName);
dengyihao's avatar
dengyihao 已提交
157 158
static bool addHandleToAcceptloop(void* arg);

dengyihao's avatar
dengyihao 已提交
159 160 161 162 163 164 165
#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 已提交
166 167 168 169
#define ASYNC_ERR_JRET(thrd)                            \
  do {                                                  \
    if (thrd->quit) {                                   \
      tTrace("worker thread already quit, ignore msg"); \
dengyihao's avatar
dengyihao 已提交
170
      goto _return1;                                    \
dengyihao's avatar
dengyihao 已提交
171 172 173
    }                                                   \
  } while (0)

dengyihao's avatar
dengyihao 已提交
174
void uvAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
175
  SSvrConn*    conn = handle->data;
dengyihao's avatar
dengyihao 已提交
176 177
  SConnBuffer* pBuf = &conn->readBuf;
  transAllocBuffer(pBuf, buf);
dengyihao's avatar
dengyihao 已提交
178 179 180 181
}

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

dengyihao's avatar
dengyihao 已提交
186
static bool uvHandleReq(SSvrConn* pConn) {
dengyihao's avatar
dengyihao 已提交
187
  STrans* pTransInst = pConn->pTransInst;
dengyihao's avatar
opt rpc  
dengyihao 已提交
188

dengyihao's avatar
dengyihao 已提交
189 190 191
  STransMsgHead* pHead = NULL;

  int msgLen = transDumpFromBuffer(&pConn->readBuf, (char**)&pHead);
dengyihao's avatar
dengyihao 已提交
192
  if (msgLen <= 0) {
dengyihao's avatar
dengyihao 已提交
193 194
    tError("%s conn %p read invalid packet", transLabel(pTransInst), pConn);
    return false;
dengyihao's avatar
dengyihao 已提交
195
  }
dengyihao's avatar
dengyihao 已提交
196

dengyihao's avatar
dengyihao 已提交
197 198 199 200 201
  if (transDecompressMsg((char**)&pHead, msgLen) < 0) {
    tDebug("%s conn %p recv invalid packet, failed to decompress", transLabel(pTransInst), pConn);
    return false;
  }

dengyihao's avatar
dengyihao 已提交
202
  pHead->code = htonl(pHead->code);
dengyihao's avatar
dengyihao 已提交
203
  pHead->msgLen = htonl(pHead->msgLen);
dengyihao's avatar
dengyihao 已提交
204

dengyihao's avatar
dengyihao 已提交
205
  memcpy(pConn->user, pHead->user, strlen(pHead->user));
dengyihao's avatar
dengyihao 已提交
206

dengyihao's avatar
dengyihao 已提交
207 208 209 210
  if (uvRecvReleaseReq(pConn, pHead)) {
    return true;
  }

dengyihao's avatar
dengyihao 已提交
211 212 213 214 215
  // 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 已提交
216
  //  uv_queue_work(((SWorkThrd*)pConn->hostThrd)->loop, wreq, uvWorkDoTask, uvWorkAfterTask);
dengyihao's avatar
dengyihao 已提交
217

dengyihao's avatar
dengyihao 已提交
218
  STransMsg transMsg;
dengyihao's avatar
dengyihao 已提交
219
  memset(&transMsg, 0, sizeof(transMsg));
dengyihao's avatar
dengyihao 已提交
220 221 222 223
  transMsg.contLen = transContLenFromMsg(pHead->msgLen);
  transMsg.pCont = pHead->content;
  transMsg.msgType = pHead->msgType;
  transMsg.code = pHead->code;
dengyihao's avatar
dengyihao 已提交
224

dengyihao's avatar
dengyihao 已提交
225
  pConn->inType = pHead->msgType;
dengyihao's avatar
dengyihao 已提交
226 227 228 229
  if (pConn->status == ConnNormal) {
    if (pHead->persist == 1) {
      pConn->status = ConnAcquire;
      transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
230
      tDebug("conn %p acquired by server app", pConn);
dengyihao's avatar
dengyihao 已提交
231 232
    }
  }
dengyihao's avatar
dengyihao 已提交
233
  STraceId* trace = &pHead->traceId;
dengyihao's avatar
dengyihao 已提交
234
  if (pConn->status == ConnNormal && pHead->noResp == 0) {
dengyihao's avatar
dengyihao 已提交
235
    transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
236

dengyihao's avatar
dengyihao 已提交
237
    tGDebug("%s conn %p %s received from %s, local info:%s, len:%d", transLabel(pTransInst), pConn,
dengyihao's avatar
dengyihao 已提交
238
            TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen);
dengyihao's avatar
dengyihao 已提交
239
  } else {
dengyihao's avatar
dengyihao 已提交
240
    tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, resp:%d, code:%d", transLabel(pTransInst), pConn,
dengyihao's avatar
dengyihao 已提交
241
            TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, pHead->noResp, transMsg.code);
dengyihao's avatar
dengyihao 已提交
242 243
  }

dengyihao's avatar
dengyihao 已提交
244
  // pHead->noResp = 1,
dengyihao's avatar
dengyihao 已提交
245 246 247
  // 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 已提交
248
  transMsg.info.ahandle = (void*)pHead->ahandle;
dengyihao's avatar
dengyihao 已提交
249
  transMsg.info.handle = (void*)transAcquireExHandle(transGetRefMgt(), pConn->refId);
S
Shengliang Guan 已提交
250
  transMsg.info.refId = pConn->refId;
dengyihao's avatar
dengyihao 已提交
251 252
  transMsg.info.traceId = pHead->traceId;

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

dengyihao's avatar
dengyihao 已提交
257
  if (pHead->noResp == 1) {
S
Shengliang Guan 已提交
258
    transMsg.info.refId = -1;
dengyihao's avatar
dengyihao 已提交
259
  }
dengyihao's avatar
dengyihao 已提交
260 261

  // set up conn info
262
  SRpcConnInfo* pConnInfo = &(transMsg.info.conn);
dengyihao's avatar
dengyihao 已提交
263 264
  pConnInfo->clientIp = pConn->clientIp;
  pConnInfo->clientPort = pConn->port;
dengyihao's avatar
dengyihao 已提交
265 266
  tstrncpy(pConnInfo->user, pConn->user, sizeof(pConnInfo->user));

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

dengyihao's avatar
dengyihao 已提交
269
  (*pTransInst->cfp)(pTransInst->parent, &transMsg, NULL);
dengyihao's avatar
dengyihao 已提交
270
  return true;
dengyihao's avatar
dengyihao 已提交
271 272
}

dengyihao's avatar
dengyihao 已提交
273
void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
274 275 276
  SSvrConn* conn = cli->data;
  STrans*   pTransInst = conn->pTransInst;

dengyihao's avatar
dengyihao 已提交
277
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
278 279
  if (nread > 0) {
    pBuf->len += nread;
S
Shengliang Guan 已提交
280
    tTrace("%s conn %p total read:%d, current read:%d", transLabel(pTransInst), conn, pBuf->len, (int)nread);
dengyihao's avatar
dengyihao 已提交
281 282 283
    if (pBuf->len <= TRANS_PACKET_LIMIT) {
      while (transReadComplete(pBuf)) {
        tTrace("%s conn %p alread read complete packet", transLabel(pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
284
        if (true == pBuf->invalid || false == uvHandleReq(conn)) {
dengyihao's avatar
dengyihao 已提交
285 286
          tError("%s conn %p read invalid packet, received from %s, local info:%s", transLabel(pTransInst), conn,
                 conn->dst, conn->src);
dengyihao's avatar
dengyihao 已提交
287
          destroyConn(conn, true);
dengyihao's avatar
dengyihao 已提交
288
          return;
dengyihao's avatar
dengyihao 已提交
289
        }
dengyihao's avatar
dengyihao 已提交
290 291 292
      }
      return;
    } else {
dengyihao's avatar
dengyihao 已提交
293
      tError("%s conn %p read invalid packet, exceed limit, received from %s, local info:%s", transLabel(pTransInst),
dengyihao's avatar
dengyihao 已提交
294
             conn, conn->dst, conn->src);
dengyihao's avatar
dengyihao 已提交
295 296
      destroyConn(conn, true);
      return;
dengyihao's avatar
dengyihao 已提交
297 298
    }
  }
299 300 301
  if (nread == 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
302

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

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

dengyihao's avatar
dengyihao 已提交
328
void uvOnSendCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
329
  SSvrConn* conn = transReqQueueRemove(req);
dengyihao's avatar
dengyihao 已提交
330 331
  if (conn == NULL) return;

dengyihao's avatar
dengyihao 已提交
332
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
333
    tTrace("conn %p data already was written on stream", conn);
dengyihao's avatar
dengyihao 已提交
334
    if (!transQueueEmpty(&conn->srvMsgs)) {
dengyihao's avatar
dengyihao 已提交
335 336 337 338
      SSvrMsg*  msg = transQueuePop(&conn->srvMsgs);
      STraceId* trace = &msg->msg.info.traceId;
      tGDebug("conn %p write data out", conn);

dengyihao's avatar
add UT  
dengyihao 已提交
339
      destroySmsg(msg);
dengyihao's avatar
dengyihao 已提交
340
      // send cached data
dengyihao's avatar
dengyihao 已提交
341
      if (!transQueueEmpty(&conn->srvMsgs)) {
dengyihao's avatar
dengyihao 已提交
342
        msg = (SSvrMsg*)transQueueGet(&conn->srvMsgs, 0);
dengyihao's avatar
dengyihao 已提交
343 344 345 346 347 348 349 350 351
        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 已提交
352
          transQueuePop(&conn->srvMsgs);
wafwerar's avatar
wafwerar 已提交
353
          taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
354

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

dengyihao's avatar
dengyihao 已提交
385 386
static void uvPrepareSendData(SSvrMsg* smsg, uv_buf_t* wb) {
  SSvrConn*  pConn = smsg->pConn;
dengyihao's avatar
formate  
dengyihao 已提交
387
  STransMsg* pMsg = &smsg->msg;
dengyihao's avatar
dengyihao 已提交
388 389 390 391 392
  if (pMsg->pCont == 0) {
    pMsg->pCont = (void*)rpcMallocCont(0);
    pMsg->contLen = 0;
  }
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
S
Shengliang Guan 已提交
393
  pHead->ahandle = (uint64_t)pMsg->info.ahandle;
dengyihao's avatar
dengyihao 已提交
394
  pHead->traceId = pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
395
  pHead->hasEpSet = pMsg->info.hasEpSet;
dengyihao's avatar
dengyihao 已提交
396
  pHead->magicNum = htonl(TRANS_MAGIC_NUM);
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
    if (smsg->type == Release) pHead->msgType = 0;
dengyihao's avatar
dengyihao 已提交
401
  } else {
dengyihao's avatar
dengyihao 已提交
402 403 404
    if (smsg->type == Release) {
      pHead->msgType = 0;
      pConn->status = ConnNormal;
dengyihao's avatar
dengyihao 已提交
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
  pHead->msgLen = htonl(pMsg->contLen + sizeof(STransMsgHead));
dengyihao's avatar
dengyihao 已提交
416

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

dengyihao's avatar
dengyihao 已提交
420
  STrans* pTransInst = pConn->pTransInst;
dengyihao's avatar
dengyihao 已提交
421
  if (pTransInst->compressSize != -1 && pTransInst->compressSize < pMsg->contLen) {
dengyihao's avatar
dengyihao 已提交
422 423 424 425
    len = transCompressMsg(pMsg->pCont, pMsg->contLen) + sizeof(STransMsgHead);
    pHead->msgLen = (int32_t)htonl((uint32_t)len);
  }

dengyihao's avatar
dengyihao 已提交
426
  STraceId* trace = &pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
427
  tGDebug("%s conn %p %s is sent to %s, local info:%s, len:%d", transLabel(pTransInst), pConn,
dengyihao's avatar
dengyihao 已提交
428
          TMSG_INFO(pHead->msgType), pConn->dst, pConn->src, len);
dengyihao's avatar
dengyihao 已提交
429

dengyihao's avatar
dengyihao 已提交
430
  wb->base = (char*)pHead;
dengyihao's avatar
dengyihao 已提交
431 432
  wb->len = len;
}
dengyihao's avatar
dengyihao 已提交
433

dengyihao's avatar
dengyihao 已提交
434
static FORCE_INLINE void uvStartSendRespImpl(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
435 436 437 438 439
  SSvrConn* pConn = smsg->pConn;
  if (pConn->broken) {
    return;
  }

dengyihao's avatar
dengyihao 已提交
440 441 442
  uv_buf_t wb;
  uvPrepareSendData(smsg, &wb);

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

dengyihao's avatar
dengyihao 已提交
461
  if (!transQueuePush(&pConn->srvMsgs, smsg)) {
dengyihao's avatar
dengyihao 已提交
462 463
    return;
  }
dengyihao's avatar
dengyihao 已提交
464
  uvStartSendRespImpl(smsg);
dengyihao's avatar
dengyihao 已提交
465 466
  return;
}
dengyihao's avatar
dengyihao 已提交
467

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

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

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

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

dengyihao's avatar
dengyihao 已提交
504
    SSvrMsg* msg = QUEUE_DATA(head, SSvrMsg, q);
dengyihao's avatar
dengyihao 已提交
505
    if (msg == NULL) {
dengyihao's avatar
dengyihao 已提交
506
      tError("unexcept occurred, continue");
dengyihao's avatar
dengyihao 已提交
507
      continue;
dengyihao's avatar
dengyihao 已提交
508
    }
dengyihao's avatar
dengyihao 已提交
509

dengyihao's avatar
dengyihao 已提交
510
    // release handle to rpc init
dengyihao's avatar
dengyihao 已提交
511 512 513 514 515
    if (msg->type == Quit) {
      (*transAsyncHandle[msg->type])(msg, pThrd);
    } else {
      STransMsg transMsg = msg->msg;

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

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

dengyihao's avatar
dengyihao 已提交
547
static void uvShutDownCb(uv_shutdown_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
548
  if (status != 0) {
S
Shengliang Guan 已提交
549
    tDebug("conn failed to shut down:%s", uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
550
  }
dengyihao's avatar
dengyihao 已提交
551
  uv_close((uv_handle_t*)req->handle, uvDestroyConn);
wafwerar's avatar
wafwerar 已提交
552
  taosMemoryFree(req);
dengyihao's avatar
dengyihao 已提交
553
}
dengyihao's avatar
dengyihao 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
static bool uvRecvReleaseReq(SSvrConn* pConn, STransMsgHead* pHead) {
  if ((pHead)->release == 1 && (pHead->msgLen) == sizeof(*pHead)) {
    reallocConnRef(pConn);
    tTrace("conn %p received release request", pConn);

    STraceId traceId = pHead->traceId;
    pConn->status = ConnRelease;
    transClearBuffer(&pConn->readBuf);
    transFreeMsg(transContFromHead((char*)pHead));

    STransMsg tmsg = {.code = 0, .info.handle = (void*)pConn, .info.traceId = traceId, .info.ahandle = (void*)0x9527};
    SSvrMsg*  srvMsg = taosMemoryCalloc(1, sizeof(SSvrMsg));
    srvMsg->msg = tmsg;
    srvMsg->type = Release;
    srvMsg->pConn = pConn;
    if (!transQueuePush(&pConn->srvMsgs, srvMsg)) {
      return true;
    }
    if (pConn->regArg.init) {
      tTrace("conn %p release, notify server app", pConn);
      STrans* pTransInst = pConn->pTransInst;
      (*pTransInst->cfp)(pTransInst->parent, &(pConn->regArg.msg), NULL);
      memset(&pConn->regArg, 0, sizeof(pConn->regArg));
    }
    uvStartSendRespImpl(srvMsg);
    return true;
  }
  return false;
}
dengyihao's avatar
dengyihao 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
static void uvPrepareCb(uv_prepare_t* handle) {
  // prepare callback
  SWorkThrd*  pThrd = handle->data;
  SAsyncPool* pool = pThrd->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);

    while (!QUEUE_IS_EMPTY(&wq)) {
      queue* head = QUEUE_HEAD(&wq);
      QUEUE_REMOVE(head);

      SSvrMsg* msg = QUEUE_DATA(head, SSvrMsg, q);
      if (msg == NULL) {
        tError("unexcept occurred, continue");
        continue;
      }
      // release handle to rpc init
      if (msg->type == Quit) {
        (*transAsyncHandle[msg->type])(msg, pThrd);
        continue;
      } else {
        STransMsg transMsg = msg->msg;

        SExHandle* exh1 = transMsg.info.handle;
        int64_t    refId = transMsg.info.refId;
        SExHandle* exh2 = transAcquireExHandle(transGetRefMgt(), refId);
        if (exh2 == NULL || exh1 != exh2) {
          tTrace("handle except msg %p, ignore it", exh1);
          transReleaseExHandle(transGetRefMgt(), refId);
          destroySmsg(msg);
          continue;
        }
        msg->pConn = exh1->handle;
        transReleaseExHandle(transGetRefMgt(), refId);
        (*transAsyncHandle[msg->type])(msg, pThrd);
      }
    }
  }
}
dengyihao's avatar
dengyihao 已提交
629

dengyihao's avatar
dengyihao 已提交
630 631 632
static void uvWorkDoTask(uv_work_t* req) {
  // doing time-consumeing task
  // only auth conn currently, add more func later
dengyihao's avatar
dengyihao 已提交
633
  tTrace("conn %p start to be processed in BG Thread", req->data);
dengyihao's avatar
dengyihao 已提交
634 635 636 637 638
  return;
}

static void uvWorkAfterTask(uv_work_t* req, int status) {
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
639
    tTrace("conn %p failed to processed ", req->data);
dengyihao's avatar
dengyihao 已提交
640 641 642 643
  }
  // Done time-consumeing task
  // add more func later
  // this func called in main loop
dengyihao's avatar
dengyihao 已提交
644
  tTrace("conn %p already processed ", req->data);
dengyihao's avatar
dengyihao 已提交
645 646 647
  taosMemoryFree(req);
}

dengyihao's avatar
dengyihao 已提交
648 649 650 651 652 653
void uvOnAcceptCb(uv_stream_t* stream, int status) {
  if (status == -1) {
    return;
  }
  SServerObj* pObj = container_of(stream, SServerObj, server);

wafwerar's avatar
wafwerar 已提交
654
  uv_tcp_t* cli = (uv_tcp_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
655 656 657
  uv_tcp_init(pObj->loop, cli);

  if (uv_accept(stream, (uv_stream_t*)cli) == 0) {
dengyihao's avatar
dengyihao 已提交
658
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
659
    if (pObj->numOfWorkerReady < pObj->numOfThreads) {
dengyihao's avatar
dengyihao 已提交
660 661
      tError("worker-threads are not ready for all, need %d instead of %d.", pObj->numOfThreads,
             pObj->numOfWorkerReady);
wafwerar's avatar
wafwerar 已提交
662 663 664
      uv_close((uv_handle_t*)cli, NULL);
      return;
    }
dengyihao's avatar
dengyihao 已提交
665
#endif
dengyihao's avatar
dengyihao 已提交
666

wafwerar's avatar
wafwerar 已提交
667
    uv_write_t* wr = (uv_write_t*)taosMemoryMalloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
668
    wr->data = cli;
dengyihao's avatar
dengyihao 已提交
669 670 671
    uv_buf_t buf = uv_buf_init((char*)notify, strlen(notify));

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

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

dengyihao's avatar
dengyihao 已提交
675
    uv_write2(wr, (uv_stream_t*)&(pObj->pipe[pObj->workerIdx][0]), &buf, 1, (uv_stream_t*)cli, uvOnPipeWriteCb);
dengyihao's avatar
dengyihao 已提交
676
  } else {
dengyihao's avatar
dengyihao 已提交
677 678 679 680 681
    if (!uv_is_closing((uv_handle_t*)cli)) {
      uv_close((uv_handle_t*)cli, NULL);
    } else {
      taosMemoryFree(cli);
    }
dengyihao's avatar
dengyihao 已提交
682 683 684
  }
}
void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
685
  tTrace("connection coming");
dengyihao's avatar
dengyihao 已提交
686 687 688 689 690
  if (nread < 0) {
    if (nread != UV_EOF) {
      tError("read error %s", uv_err_name(nread));
    }
    // TODO(log other failure reason)
S
Shengliang Guan 已提交
691
    tWarn("failed to create connect:%p", q);
dengyihao's avatar
dengyihao 已提交
692 693
    taosMemoryFree(buf->base);
    uv_close((uv_handle_t*)q, NULL);
dengyihao's avatar
dengyihao 已提交
694 695 696 697 698
    return;
  }
  // free memory allocated by
  assert(nread == strlen(notify));
  assert(buf->base[0] == notify[0]);
wafwerar's avatar
wafwerar 已提交
699
  taosMemoryFree(buf->base);
dengyihao's avatar
dengyihao 已提交
700

dengyihao's avatar
dengyihao 已提交
701
  SWorkThrd* pThrd = q->data;
dengyihao's avatar
dengyihao 已提交
702 703 704 705 706 707 708 709 710 711

  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 已提交
712
  SSvrConn* pConn = createConn(pThrd);
713

dengyihao's avatar
dengyihao 已提交
714
  pConn->pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
715
  /* init conn timer*/
dengyihao's avatar
dengyihao 已提交
716 717
  // uv_timer_init(pThrd->loop, &pConn->pTimer);
  // pConn->pTimer.data = pConn;
dengyihao's avatar
dengyihao 已提交
718 719 720 721

  pConn->hostThrd = pThrd;

  // init client handle
wafwerar's avatar
wafwerar 已提交
722
  pConn->pTcp = (uv_tcp_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
723 724 725
  uv_tcp_init(pThrd->loop, pConn->pTcp);
  pConn->pTcp->data = pConn;

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

dengyihao's avatar
dengyihao 已提交
728 729 730
  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 已提交
731
    tTrace("conn %p created, fd:%d", pConn, fd);
dengyihao's avatar
dengyihao 已提交
732

dengyihao's avatar
dengyihao 已提交
733 734 735
    struct sockaddr peername, sockname;
    int             addrlen = sizeof(peername);
    if (0 != uv_tcp_getpeername(pConn->pTcp, (struct sockaddr*)&peername, &addrlen)) {
dengyihao's avatar
dengyihao 已提交
736
      tError("conn %p failed to get peer info", pConn);
dengyihao's avatar
dengyihao 已提交
737
      transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
738
      return;
dengyihao's avatar
dengyihao 已提交
739
    }
dengyihao's avatar
dengyihao 已提交
740
    transSockInfo2Str(&peername, pConn->dst);
dengyihao's avatar
dengyihao 已提交
741

dengyihao's avatar
dengyihao 已提交
742 743
    addrlen = sizeof(sockname);
    if (0 != uv_tcp_getsockname(pConn->pTcp, (struct sockaddr*)&sockname, &addrlen)) {
dengyihao's avatar
dengyihao 已提交
744
      tError("conn %p failed to get local info", pConn);
dengyihao's avatar
dengyihao 已提交
745
      transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
746 747
      return;
    }
dengyihao's avatar
dengyihao 已提交
748
    transSockInfo2Str(&sockname, pConn->src);
dengyihao's avatar
dengyihao 已提交
749
    struct sockaddr_in addr = *(struct sockaddr_in*)&sockname;
dengyihao's avatar
dengyihao 已提交
750

dengyihao's avatar
dengyihao 已提交
751 752
    pConn->clientIp = addr.sin_addr.s_addr;
    pConn->port = ntohs(addr.sin_port);
dengyihao's avatar
dengyihao 已提交
753
    uv_read_start((uv_stream_t*)(pConn->pTcp), uvAllocRecvBufferCb, uvOnRecvCb);
dengyihao's avatar
dengyihao 已提交
754

dengyihao's avatar
dengyihao 已提交
755
  } else {
dengyihao's avatar
dengyihao 已提交
756
    tDebug("failed to create new connection");
dengyihao's avatar
dengyihao 已提交
757
    transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
758 759 760
  }
}

761
void* transAcceptThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
762
  // opt
dengyihao's avatar
dengyihao 已提交
763
  setThreadName("trans-accept");
dengyihao's avatar
dengyihao 已提交
764 765
  SServerObj* srv = (SServerObj*)arg;
  uv_run(srv->loop, UV_RUN_DEFAULT);
766 767

  return NULL;
dengyihao's avatar
dengyihao 已提交
768
}
dengyihao's avatar
dengyihao 已提交
769
void uvOnPipeConnectionCb(uv_connect_t* connect, int status) {
wafwerar's avatar
wafwerar 已提交
770 771 772
  if (status != 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
773
  SWorkThrd* pThrd = container_of(connect, SWorkThrd, connect_req);
wafwerar's avatar
wafwerar 已提交
774 775
  uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb);
}
dengyihao's avatar
dengyihao 已提交
776
static bool addHandleToWorkloop(SWorkThrd* pThrd, char* pipeName) {
wafwerar's avatar
wafwerar 已提交
777
  pThrd->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
778 779 780
  if (0 != uv_loop_init(pThrd->loop)) {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
781

dengyihao's avatar
dengyihao 已提交
782
#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
783
  uv_pipe_init(pThrd->loop, pThrd->pipe, 1);
dengyihao's avatar
dengyihao 已提交
784 785 786 787
#else
  uv_pipe_init(pThrd->loop, pThrd->pipe, 1);
  uv_pipe_open(pThrd->pipe, pThrd->fd);
#endif
dengyihao's avatar
dengyihao 已提交
788 789 790

  pThrd->pipe->data = pThrd;

dengyihao's avatar
dengyihao 已提交
791
  QUEUE_INIT(&pThrd->msg);
dengyihao's avatar
dengyihao 已提交
792

dengyihao's avatar
dengyihao 已提交
793 794 795 796 797
  pThrd->prepare = taosMemoryCalloc(1, sizeof(uv_prepare_t));
  uv_prepare_init(pThrd->loop, pThrd->prepare);
  uv_prepare_start(pThrd->prepare, uvPrepareCb);
  pThrd->prepare->data = pThrd;

dengyihao's avatar
fix bug  
dengyihao 已提交
798 799 800
  // conn set
  QUEUE_INIT(&pThrd->conn);

dengyihao's avatar
dengyihao 已提交
801
  pThrd->asyncPool = transAsyncPoolCreate(pThrd->loop, 5, pThrd, uvWorkerAsyncCb);
dengyihao's avatar
dengyihao 已提交
802
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
803
  uv_pipe_connect(&pThrd->connect_req, pThrd->pipe, pipeName, uvOnPipeConnectionCb);
dengyihao's avatar
dengyihao 已提交
804 805 806
#else
  uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb);
#endif
dengyihao's avatar
dengyihao 已提交
807 808 809 810 811 812 813 814 815
  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 已提交
816
    tError("failed to init accept server:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
817 818 819
    return false;
  }

dengyihao's avatar
dengyihao 已提交
820
  // register an async here to quit server gracefully
wafwerar's avatar
wafwerar 已提交
821
  srv->pAcceptAsync = taosMemoryCalloc(1, sizeof(uv_async_t));
dengyihao's avatar
dengyihao 已提交
822 823
  uv_async_init(srv->loop, srv->pAcceptAsync, uvAcceptAsyncCb);
  srv->pAcceptAsync->data = srv;
dengyihao's avatar
dengyihao 已提交
824

dengyihao's avatar
dengyihao 已提交
825
  struct sockaddr_in bind_addr;
dengyihao's avatar
dengyihao 已提交
826 827
  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 已提交
828
    tError("failed to bind:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
829 830
    return false;
  }
dengyihao's avatar
dengyihao 已提交
831
  if ((err = uv_listen((uv_stream_t*)&srv->server, 4096 * 2, uvOnAcceptCb)) != 0) {
S
Shengliang Guan 已提交
832
    tError("failed to listen:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
833
    terrno = TSDB_CODE_RPC_PORT_EADDRINUSE;
dengyihao's avatar
dengyihao 已提交
834 835 836
    return false;
  }
  return true;
dengyihao's avatar
dengyihao 已提交
837
}
838
void* transWorkerThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
839
  setThreadName("trans-worker");
dengyihao's avatar
dengyihao 已提交
840
  SWorkThrd* pThrd = (SWorkThrd*)arg;
dengyihao's avatar
dengyihao 已提交
841
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
842 843

  return NULL;
dengyihao's avatar
dengyihao 已提交
844 845
}

dengyihao's avatar
dengyihao 已提交
846
static FORCE_INLINE SSvrConn* createConn(void* hThrd) {
dengyihao's avatar
dengyihao 已提交
847
  SWorkThrd* pThrd = hThrd;
dengyihao's avatar
fix bug  
dengyihao 已提交
848

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

dengyihao's avatar
dengyihao 已提交
851
  transReqQueueInit(&pConn->wreqQueue);
dengyihao's avatar
fix bug  
dengyihao 已提交
852 853 854
  QUEUE_INIT(&pConn->queue);

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

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

dengyihao's avatar
dengyihao 已提交
858
  memset(&pConn->regArg, 0, sizeof(pConn->regArg));
dengyihao's avatar
dengyihao 已提交
859
  pConn->broken = false;
dengyihao's avatar
dengyihao 已提交
860
  pConn->status = ConnNormal;
dengyihao's avatar
opt rpc  
dengyihao 已提交
861
  transInitBuffer(&pConn->readBuf);
dengyihao's avatar
dengyihao 已提交
862

dengyihao's avatar
dengyihao 已提交
863 864 865
  SExHandle* exh = taosMemoryMalloc(sizeof(SExHandle));
  exh->handle = pConn;
  exh->pThrd = pThrd;
dengyihao's avatar
dengyihao 已提交
866 867
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  transAcquireExHandle(transGetRefMgt(), exh->refId);
dengyihao's avatar
dengyihao 已提交
868

dengyihao's avatar
dengyihao 已提交
869
  STrans* pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
870
  pConn->refId = exh->refId;
dengyihao's avatar
dengyihao 已提交
871
  transRefSrvHandle(pConn);
S
Shengliang Guan 已提交
872
  tTrace("%s handle %p, conn %p created, refId:%" PRId64, transLabel(pTransInst), exh, pConn, pConn->refId);
dengyihao's avatar
dengyihao 已提交
873 874
  return pConn;
}
dengyihao's avatar
dengyihao 已提交
875

dengyihao's avatar
dengyihao 已提交
876
static FORCE_INLINE void destroyConn(SSvrConn* conn, bool clear) {
dengyihao's avatar
dengyihao 已提交
877 878 879
  if (conn == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
880

881
  if (clear) {
dengyihao's avatar
dengyihao 已提交
882
    if (!uv_is_closing((uv_handle_t*)conn->pTcp)) {
dengyihao's avatar
dengyihao 已提交
883 884 885
      tTrace("conn %p to be destroyed", conn);
      uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn);
    }
886
  }
dengyihao's avatar
dengyihao 已提交
887
}
dengyihao's avatar
dengyihao 已提交
888
static FORCE_INLINE void destroyConnRegArg(SSvrConn* conn) {
dengyihao's avatar
dengyihao 已提交
889 890 891 892 893
  if (conn->regArg.init == 1) {
    transFreeMsg(conn->regArg.msg.pCont);
    conn->regArg.init = 0;
  }
}
dengyihao's avatar
dengyihao 已提交
894
static int reallocConnRef(SSvrConn* conn) {
dengyihao's avatar
dengyihao 已提交
895 896
  transReleaseExHandle(transGetRefMgt(), conn->refId);
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
897 898 899 900
  // 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 已提交
901 902
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  transAcquireExHandle(transGetRefMgt(), exh->refId);
dengyihao's avatar
dengyihao 已提交
903 904 905 906
  conn->refId = exh->refId;

  return 0;
}
dengyihao's avatar
dengyihao 已提交
907
static void uvDestroyConn(uv_handle_t* handle) {
dengyihao's avatar
dengyihao 已提交
908
  SSvrConn* conn = handle->data;
dengyihao's avatar
dengyihao 已提交
909

dengyihao's avatar
dengyihao 已提交
910 911 912
  if (conn == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
913
  SWorkThrd* thrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
914

dengyihao's avatar
dengyihao 已提交
915 916
  transReleaseExHandle(transGetRefMgt(), conn->refId);
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
917

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

dengyihao's avatar
dengyihao 已提交
921 922 923 924
  for (int i = 0; i < transQueueSize(&conn->srvMsgs); i++) {
    SSvrMsg* msg = transQueueGet(&conn->srvMsgs, i);
    destroySmsg(msg);
  }
dengyihao's avatar
dengyihao 已提交
925
  transQueueDestroy(&conn->srvMsgs);
dengyihao's avatar
dengyihao 已提交
926
  transReqQueueClear(&conn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
927

dengyihao's avatar
dengyihao 已提交
928
  QUEUE_REMOVE(&conn->queue);
wafwerar's avatar
wafwerar 已提交
929
  taosMemoryFree(conn->pTcp);
dengyihao's avatar
dengyihao 已提交
930
  destroyConnRegArg(conn);
dengyihao's avatar
dengyihao 已提交
931
  transDestroyBuffer(&conn->readBuf);
dengyihao's avatar
dengyihao 已提交
932
  taosMemoryFree(conn);
dengyihao's avatar
dengyihao 已提交
933

dengyihao's avatar
dengyihao 已提交
934
  if (thrd->quit && QUEUE_IS_EMPTY(&thrd->conn)) {
dengyihao's avatar
dengyihao 已提交
935
    tTrace("work thread quit");
dengyihao's avatar
dengyihao 已提交
936
    uv_walk(thrd->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
937
  }
dengyihao's avatar
dengyihao 已提交
938
}
wafwerar's avatar
wafwerar 已提交
939
static void uvPipeListenCb(uv_stream_t* handle, int status) {
dengyihao's avatar
dengyihao 已提交
940
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
941
    tError("server failed to init pipe, errmsg: %s", uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
942 943
    return;
  }
wafwerar's avatar
wafwerar 已提交
944 945

  SServerObj* srv = container_of(handle, SServerObj, pipeListen);
dengyihao's avatar
dengyihao 已提交
946
  uv_pipe_t*  pipe = &(srv->pipe[srv->numOfWorkerReady][0]);
wafwerar's avatar
wafwerar 已提交
947

dengyihao's avatar
dengyihao 已提交
948 949
  int ret = uv_pipe_init(srv->loop, pipe, 1);
  assert(ret == 0);
wafwerar's avatar
wafwerar 已提交
950

dengyihao's avatar
dengyihao 已提交
951 952 953 954 955
  ret = uv_accept((uv_stream_t*)&srv->pipeListen, (uv_stream_t*)pipe);
  assert(ret == 0);

  ret = uv_is_readable((uv_stream_t*)pipe);
  assert(ret == 1);
wafwerar's avatar
wafwerar 已提交
956

dengyihao's avatar
dengyihao 已提交
957 958
  ret = uv_is_writable((uv_stream_t*)pipe);
  assert(ret == 1);
wafwerar's avatar
wafwerar 已提交
959

dengyihao's avatar
dengyihao 已提交
960 961 962 963
  ret = uv_is_closing((uv_handle_t*)pipe);
  assert(ret == 0);

  srv->numOfWorkerReady++;
wafwerar's avatar
wafwerar 已提交
964
}
dengyihao's avatar
dengyihao 已提交
965

U
ubuntu 已提交
966
void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
wafwerar's avatar
wafwerar 已提交
967 968
  SServerObj* srv = taosMemoryCalloc(1, sizeof(SServerObj));
  srv->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
969 970
  srv->numOfThreads = numOfThreads;
  srv->workerIdx = 0;
wafwerar's avatar
wafwerar 已提交
971
  srv->numOfWorkerReady = 0;
dengyihao's avatar
dengyihao 已提交
972
  srv->pThreadObj = (SWorkThrd**)taosMemoryCalloc(srv->numOfThreads, sizeof(SWorkThrd*));
wafwerar's avatar
wafwerar 已提交
973
  srv->pipe = (uv_pipe_t**)taosMemoryCalloc(srv->numOfThreads, sizeof(uv_pipe_t*));
dengyihao's avatar
dengyihao 已提交
974 975 976 977
  srv->ip = ip;
  srv->port = port;
  uv_loop_init(srv->loop);

dengyihao's avatar
dengyihao 已提交
978
  char pipeName[PATH_MAX];
dengyihao's avatar
dengyihao 已提交
979
#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
980
  int ret = uv_pipe_init(srv->loop, &srv->pipeListen, 0);
dengyihao's avatar
dengyihao 已提交
981 982 983 984
  if (ret != 0) {
    tError("failed to init pipe, errmsg: %s", uv_err_name(ret));
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
985

dengyihao's avatar
dengyihao 已提交
986
  snprintf(pipeName, sizeof(pipeName), "\\\\?\\pipe\\trans.rpc.%d-%" PRIu64, taosSafeRand(), GetCurrentProcessId());
dengyihao's avatar
dengyihao 已提交
987 988 989
  // char pipeName[PATH_MAX] = {0};
  // snprintf(pipeName, sizeof(pipeName), "%s%spipe.trans.rpc.%08d-%" PRIu64, tsTempDir, TD_DIRSEP, taosSafeRand(),
  //          taosGetSelfPthreadId());
dengyihao's avatar
dengyihao 已提交
990

dengyihao's avatar
dengyihao 已提交
991
  ret = uv_pipe_bind(&srv->pipeListen, pipeName);
dengyihao's avatar
dengyihao 已提交
992 993 994 995
  if (ret != 0) {
    tError("failed to bind pipe, errmsg: %s", uv_err_name(ret));
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
996 997

  ret = uv_listen((uv_stream_t*)&srv->pipeListen, SOMAXCONN, uvPipeListenCb);
dengyihao's avatar
dengyihao 已提交
998 999 1000 1001
  if (ret != 0) {
    tError("failed to listen pipe, errmsg: %s", uv_err_name(ret));
    goto End;
  }
wafwerar's avatar
wafwerar 已提交
1002

dengyihao's avatar
dengyihao 已提交
1003
  for (int i = 0; i < srv->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
1004
    SWorkThrd* thrd = (SWorkThrd*)taosMemoryCalloc(1, sizeof(SWorkThrd));
dengyihao's avatar
dengyihao 已提交
1005
    thrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
1006
    thrd->quit = false;
dengyihao's avatar
dengyihao 已提交
1007
    srv->pThreadObj[i] = thrd;
dengyihao's avatar
dengyihao 已提交
1008
    thrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
1009

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

dengyihao's avatar
dengyihao 已提交
1013
    if (false == addHandleToWorkloop(thrd, pipeName)) {
dengyihao's avatar
dengyihao 已提交
1014 1015
      goto End;
    }
dengyihao's avatar
dengyihao 已提交
1016

dengyihao's avatar
dengyihao 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    int err = taosThreadCreate(&(thrd->thread), NULL, transWorkerThread, (void*)(thrd));
    if (err == 0) {
      tDebug("success to create worker-thread:%d", i);
    } else {
      // TODO: clear all other resource later
      tError("failed to create worker-thread:%d", i);
      goto End;
    }
  }
#else

  for (int i = 0; i < srv->numOfThreads; i++) {
    SWorkThrd* thrd = (SWorkThrd*)taosMemoryCalloc(1, sizeof(SWorkThrd));

    thrd->pTransInst = shandle;
    thrd->quit = false;
    thrd->pTransInst = shandle;

    srv->pipe[i] = (uv_pipe_t*)taosMemoryCalloc(2, sizeof(uv_pipe_t));
    srv->pThreadObj[i] = thrd;

    uv_os_sock_t fds[2];
    if (uv_socketpair(AF_UNIX, SOCK_STREAM, fds, UV_NONBLOCK_PIPE, UV_NONBLOCK_PIPE) != 0) {
      goto End;
    }

    uv_pipe_init(srv->loop, &(srv->pipe[i][0]), 1);
    uv_pipe_open(&(srv->pipe[i][0]), fds[1]);

    thrd->pipe = &(srv->pipe[i][1]);  // init read
    thrd->fd = fds[0];

    if (false == addHandleToWorkloop(thrd, pipeName)) {
      goto End;
    }

1053
    int err = taosThreadCreate(&(thrd->thread), NULL, transWorkerThread, (void*)(thrd));
dengyihao's avatar
dengyihao 已提交
1054
    if (err == 0) {
S
Shengliang Guan 已提交
1055
      tDebug("success to create worker-thread:%d", i);
dengyihao's avatar
dengyihao 已提交
1056 1057
    } else {
      // TODO: clear all other resource later
S
Shengliang Guan 已提交
1058
      tError("failed to create worker-thread:%d", i);
dengyihao's avatar
dengyihao 已提交
1059
      goto End;
dengyihao's avatar
dengyihao 已提交
1060 1061
    }
  }
dengyihao's avatar
dengyihao 已提交
1062

dengyihao's avatar
dengyihao 已提交
1063
#endif
1064
  if (false == taosValidIpAndPort(srv->ip, srv->port)) {
dengyihao's avatar
dengyihao 已提交
1065
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
1066
    tError("invalid ip/port, %d:%d, reason:%s", srv->ip, srv->port, terrstr());
1067 1068
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
1069

dengyihao's avatar
dengyihao 已提交
1070 1071 1072
  if (false == addHandleToAcceptloop(srv)) {
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
1073

1074
  int err = taosThreadCreate(&srv->thread, NULL, transAcceptThread, (void*)srv);
dengyihao's avatar
dengyihao 已提交
1075 1076 1077
  if (err == 0) {
    tDebug("success to create accept-thread");
  } else {
dengyihao's avatar
dengyihao 已提交
1078 1079
    tError("failed  to create accept-thread");
    goto End;
dengyihao's avatar
dengyihao 已提交
1080 1081
    // clear all resource later
  }
dengyihao's avatar
dengyihao 已提交
1082

dengyihao's avatar
dengyihao 已提交
1083
  srv->inited = true;
dengyihao's avatar
dengyihao 已提交
1084
  return srv;
dengyihao's avatar
dengyihao 已提交
1085
End:
U
ubuntu 已提交
1086
  transCloseServer(srv);
dengyihao's avatar
dengyihao 已提交
1087 1088
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
1089

dengyihao's avatar
dengyihao 已提交
1090
void uvHandleQuit(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1091
  thrd->quit = true;
dengyihao's avatar
dengyihao 已提交
1092
  if (QUEUE_IS_EMPTY(&thrd->conn)) {
dengyihao's avatar
dengyihao 已提交
1093
    uv_walk(thrd->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
1094 1095 1096
  } else {
    destroyAllConn(thrd);
  }
wafwerar's avatar
wafwerar 已提交
1097
  taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
1098
}
dengyihao's avatar
dengyihao 已提交
1099
void uvHandleRelease(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1100
  SSvrConn* conn = msg->pConn;
dengyihao's avatar
dengyihao 已提交
1101
  if (conn->status == ConnAcquire) {
dengyihao's avatar
dengyihao 已提交
1102
    reallocConnRef(conn);
dengyihao's avatar
dengyihao 已提交
1103
    if (!transQueuePush(&conn->srvMsgs, msg)) {
dengyihao's avatar
dengyihao 已提交
1104
      return;
dengyihao's avatar
dengyihao 已提交
1105
    }
dengyihao's avatar
dengyihao 已提交
1106
    uvStartSendRespImpl(msg);
dengyihao's avatar
dengyihao 已提交
1107
    return;
dengyihao's avatar
dengyihao 已提交
1108
  } else if (conn->status == ConnRelease || conn->status == ConnNormal) {
dengyihao's avatar
dengyihao 已提交
1109
    tDebug("%s conn %p already released, ignore release-msg", transLabel(thrd->pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
1110
  }
dengyihao's avatar
dengyihao 已提交
1111
  destroySmsg(msg);
dengyihao's avatar
dengyihao 已提交
1112
}
dengyihao's avatar
dengyihao 已提交
1113
void uvHandleResp(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1114
  // send msg to client
dengyihao's avatar
dengyihao 已提交
1115
  tDebug("%s conn %p start to send resp (2/2)", transLabel(thrd->pTransInst), msg->pConn);
dengyihao's avatar
dengyihao 已提交
1116 1117
  uvStartSendResp(msg);
}
dengyihao's avatar
dengyihao 已提交
1118
void uvHandleRegister(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1119
  SSvrConn* conn = msg->pConn;
dengyihao's avatar
dengyihao 已提交
1120
  tDebug("%s conn %p register brokenlink callback", transLabel(thrd->pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
1121
  if (conn->status == ConnAcquire) {
dengyihao's avatar
dengyihao 已提交
1122
    if (!transQueuePush(&conn->srvMsgs, msg)) {
dengyihao's avatar
dengyihao 已提交
1123 1124
      return;
    }
dengyihao's avatar
dengyihao 已提交
1125
    transQueuePop(&conn->srvMsgs);
dengyihao's avatar
dengyihao 已提交
1126 1127 1128
    conn->regArg.notifyCount = 0;
    conn->regArg.init = 1;
    conn->regArg.msg = msg->msg;
dengyihao's avatar
dengyihao 已提交
1129
    tDebug("conn %p register brokenlink callback succ", conn);
dengyihao's avatar
dengyihao 已提交
1130 1131 1132 1133 1134 1135

    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 已提交
1136
    taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
1137 1138
  }
}
dengyihao's avatar
dengyihao 已提交
1139
void destroyWorkThrd(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
1140 1141 1142
  if (pThrd == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
1143
  taosThreadJoin(pThrd->thread, NULL);
dengyihao's avatar
dengyihao 已提交
1144
  SRV_RELEASE_UV(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
1145
  TRANS_DESTROY_ASYNC_POOL_MSG(pThrd->asyncPool, SSvrMsg, destroySmsg);
dengyihao's avatar
dengyihao 已提交
1146
  transAsyncPoolDestroy(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
1147
  taosMemoryFree(pThrd->prepare);
dengyihao's avatar
dengyihao 已提交
1148
  taosMemoryFree(pThrd->loop);
wafwerar's avatar
wafwerar 已提交
1149
  taosMemoryFree(pThrd);
dengyihao's avatar
dengyihao 已提交
1150
}
dengyihao's avatar
dengyihao 已提交
1151
void sendQuitToWorkThrd(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
1152
  SSvrMsg* msg = taosMemoryCalloc(1, sizeof(SSvrMsg));
dengyihao's avatar
dengyihao 已提交
1153
  msg->type = Quit;
dengyihao's avatar
dengyihao 已提交
1154
  tDebug("server send quit msg to work thread");
dengyihao's avatar
dengyihao 已提交
1155
  transAsyncSend(pThrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
1156 1157
}

U
ubuntu 已提交
1158
void transCloseServer(void* arg) {
dengyihao's avatar
dengyihao 已提交
1159 1160
  // impl later
  SServerObj* srv = arg;
dengyihao's avatar
dengyihao 已提交
1161 1162

  tDebug("send quit msg to accept thread");
dengyihao's avatar
dengyihao 已提交
1163 1164 1165
  if (srv->inited) {
    uv_async_send(srv->pAcceptAsync);
    taosThreadJoin(srv->thread, NULL);
dengyihao's avatar
dengyihao 已提交
1166
    SRV_RELEASE_UV(srv->loop);
dengyihao's avatar
dengyihao 已提交
1167

dengyihao's avatar
dengyihao 已提交
1168 1169 1170 1171
    for (int i = 0; i < srv->numOfThreads; i++) {
      sendQuitToWorkThrd(srv->pThreadObj[i]);
      destroyWorkThrd(srv->pThreadObj[i]);
    }
dengyihao's avatar
dengyihao 已提交
1172 1173
  }

wafwerar's avatar
wafwerar 已提交
1174 1175 1176
  taosMemoryFree(srv->pThreadObj);
  taosMemoryFree(srv->pAcceptAsync);
  taosMemoryFree(srv->loop);
dengyihao's avatar
dengyihao 已提交
1177 1178

  for (int i = 0; i < srv->numOfThreads; i++) {
wafwerar's avatar
wafwerar 已提交
1179
    taosMemoryFree(srv->pipe[i]);
dengyihao's avatar
dengyihao 已提交
1180
  }
wafwerar's avatar
wafwerar 已提交
1181
  taosMemoryFree(srv->pipe);
dengyihao's avatar
dengyihao 已提交
1182

wafwerar's avatar
wafwerar 已提交
1183
  taosMemoryFree(srv);
dengyihao's avatar
dengyihao 已提交
1184
}
dengyihao's avatar
dengyihao 已提交
1185

dengyihao's avatar
dengyihao 已提交
1186 1187 1188 1189
void transRefSrvHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1190
  int ref = T_REF_INC((SSvrConn*)handle);
dengyihao's avatar
dengyihao 已提交
1191
  tTrace("conn %p ref count:%d", handle, ref);
dengyihao's avatar
dengyihao 已提交
1192 1193 1194 1195 1196 1197
}

void transUnrefSrvHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1198
  int ref = T_REF_DEC((SSvrConn*)handle);
dengyihao's avatar
dengyihao 已提交
1199
  tTrace("conn %p ref count:%d", handle, ref);
dengyihao's avatar
dengyihao 已提交
1200
  if (ref == 0) {
dengyihao's avatar
dengyihao 已提交
1201
    destroyConn((SSvrConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
1202 1203
  }
}
dengyihao's avatar
dengyihao 已提交
1204

dengyihao's avatar
dengyihao 已提交
1205
int transReleaseSrvHandle(void* handle) {
dengyihao's avatar
dengyihao 已提交
1206 1207 1208
  SRpcHandleInfo* info = handle;
  SExHandle*      exh = info->handle;
  int64_t         refId = info->refId;
dengyihao's avatar
dengyihao 已提交
1209

dengyihao's avatar
dengyihao 已提交
1210
  ASYNC_CHECK_HANDLE(exh, refId);
dengyihao's avatar
dengyihao 已提交
1211

dengyihao's avatar
dengyihao 已提交
1212
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1213 1214
  ASYNC_ERR_JRET(pThrd);

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

dengyihao's avatar
dengyihao 已提交
1217 1218 1219
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Release;
dengyihao's avatar
dengyihao 已提交
1220

dengyihao's avatar
dengyihao 已提交
1221
  tTrace("%s conn %p start to release", transLabel(pThrd->pTransInst), exh->handle);
dengyihao's avatar
dengyihao 已提交
1222
  transAsyncSend(pThrd->asyncPool, &m->q);
H
Haojun Liao 已提交
1223

dengyihao's avatar
dengyihao 已提交
1224
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1225
  return 0;
dengyihao's avatar
dengyihao 已提交
1226
_return1:
dengyihao's avatar
dengyihao 已提交
1227
  tTrace("handle %p failed to send to release handle", exh);
dengyihao's avatar
dengyihao 已提交
1228
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1229
  return -1;
dengyihao's avatar
dengyihao 已提交
1230
_return2:
dengyihao's avatar
dengyihao 已提交
1231
  tTrace("handle %p failed to send to release handle", exh);
dengyihao's avatar
dengyihao 已提交
1232
  return -1;
dengyihao's avatar
dengyihao 已提交
1233
}
dengyihao's avatar
dengyihao 已提交
1234
int transSendResponse(const STransMsg* msg) {
dengyihao's avatar
dengyihao 已提交
1235 1236 1237 1238 1239
  if (msg->info.noResp) {
    rpcFreeCont(msg->pCont);
    tTrace("no need send resp");
    return 0;
  }
S
Shengliang Guan 已提交
1240 1241
  SExHandle* exh = msg->info.handle;
  int64_t    refId = msg->info.refId;
dengyihao's avatar
dengyihao 已提交
1242
  ASYNC_CHECK_HANDLE(exh, refId);
dengyihao's avatar
dengyihao 已提交
1243
  assert(refId != 0);
dengyihao's avatar
dengyihao 已提交
1244

dengyihao's avatar
dengyihao 已提交
1245
  STransMsg tmsg = *msg;
S
Shengliang Guan 已提交
1246
  tmsg.info.refId = refId;
dengyihao's avatar
dengyihao 已提交
1247

dengyihao's avatar
dengyihao 已提交
1248
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1249
  ASYNC_ERR_JRET(pThrd);
dengyihao's avatar
dengyihao 已提交
1250

dengyihao's avatar
dengyihao 已提交
1251 1252 1253
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Normal;
dengyihao's avatar
dengyihao 已提交
1254 1255 1256

  STraceId* trace = (STraceId*)&msg->info.traceId;
  tGTrace("conn %p start to send resp (1/2)", exh->handle);
dengyihao's avatar
dengyihao 已提交
1257
  transAsyncSend(pThrd->asyncPool, &m->q);
H
Haojun Liao 已提交
1258

dengyihao's avatar
dengyihao 已提交
1259
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1260
  return 0;
H
Haojun Liao 已提交
1261

dengyihao's avatar
dengyihao 已提交
1262
_return1:
dengyihao's avatar
dengyihao 已提交
1263
  tTrace("handle %p failed to send resp", exh);
dengyihao's avatar
dengyihao 已提交
1264
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1265
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1266
  return -1;
dengyihao's avatar
dengyihao 已提交
1267
_return2:
dengyihao's avatar
dengyihao 已提交
1268
  tTrace("handle %p failed to send resp", exh);
dengyihao's avatar
dengyihao 已提交
1269
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1270
  return -1;
dengyihao's avatar
dengyihao 已提交
1271
}
dengyihao's avatar
dengyihao 已提交
1272
int transRegisterMsg(const STransMsg* msg) {
S
Shengliang Guan 已提交
1273 1274
  SExHandle* exh = msg->info.handle;
  int64_t    refId = msg->info.refId;
dengyihao's avatar
dengyihao 已提交
1275 1276
  ASYNC_CHECK_HANDLE(exh, refId);

dengyihao's avatar
dengyihao 已提交
1277
  STransMsg tmsg = *msg;
dengyihao's avatar
dengyihao 已提交
1278 1279
  tmsg.info.noResp = 1;

S
Shengliang Guan 已提交
1280
  tmsg.info.refId = refId;
dengyihao's avatar
dengyihao 已提交
1281

dengyihao's avatar
dengyihao 已提交
1282
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1283
  ASYNC_ERR_JRET(pThrd);
dengyihao's avatar
dengyihao 已提交
1284

dengyihao's avatar
dengyihao 已提交
1285 1286 1287
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Register;
dengyihao's avatar
dengyihao 已提交
1288

dengyihao's avatar
dengyihao 已提交
1289 1290
  STrans* pTransInst = pThrd->pTransInst;
  tTrace("%s conn %p start to register brokenlink callback", transLabel(pTransInst), exh->handle);
dengyihao's avatar
dengyihao 已提交
1291
  transAsyncSend(pThrd->asyncPool, &m->q);
H
Haojun Liao 已提交
1292

dengyihao's avatar
dengyihao 已提交
1293
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1294
  return 0;
dengyihao's avatar
dengyihao 已提交
1295

dengyihao's avatar
dengyihao 已提交
1296
_return1:
dengyihao's avatar
dengyihao 已提交
1297
  tTrace("handle %p failed to register brokenlink", exh);
dengyihao's avatar
dengyihao 已提交
1298
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1299
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1300
  return -1;
dengyihao's avatar
dengyihao 已提交
1301
_return2:
dengyihao's avatar
dengyihao 已提交
1302
  tTrace("handle %p failed to register brokenlink", exh);
dengyihao's avatar
dengyihao 已提交
1303
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1304
  return -1;
dengyihao's avatar
dengyihao 已提交
1305
}
dengyihao's avatar
dengyihao 已提交
1306

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

dengyihao's avatar
dengyihao 已提交
1309
#endif