transSvr.c 40.5 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
 * 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/>.
 */

15
#include "transComm.h"
dengyihao's avatar
dengyihao 已提交
16

dengyihao's avatar
dengyihao 已提交
17 18
static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT;

dengyihao's avatar
dengyihao 已提交
19
static char* notify = "a";
dengyihao's avatar
dengyihao 已提交
20

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

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

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

dengyihao's avatar
dengyihao 已提交
41
  SSvrRegArg regArg;
dengyihao's avatar
dengyihao 已提交
42
  bool       broken;  // conn broken;
dengyihao's avatar
dengyihao 已提交
43

dengyihao's avatar
dengyihao 已提交
44 45 46 47 48 49 50
  ConnStatus status;

  uint32_t clientIp;
  uint16_t port;

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

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

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

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

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

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

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

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

  bool inited;
dengyihao's avatar
dengyihao 已提交
100 101 102
} SServerObj;

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
133 134 135 136
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 已提交
137

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

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

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
187 188 189
  STransMsgHead* pHead = NULL;

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

dengyihao's avatar
dengyihao 已提交
195
  if (transDecompressMsg((char**)&pHead, msgLen) < 0) {
dengyihao's avatar
dengyihao 已提交
196
    tError("%s conn %p recv invalid packet, failed to decompress", transLabel(pTransInst), pConn);
dengyihao's avatar
dengyihao 已提交
197 198
    return false;
  }
dengyihao's avatar
dengyihao 已提交
199
  tDebug("head version: %d 2", pHead->version);
dengyihao's avatar
dengyihao 已提交
200

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

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

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

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

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

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

  int64_t        cost = taosGetTimestampUs() - taosNtoh64(pHead->timestamp);
  static int64_t EXCEPTION_LIMIT_US = 100 * 1000;

dengyihao's avatar
dengyihao 已提交
237
  if (pConn->status == ConnNormal && pHead->noResp == 0) {
dengyihao's avatar
dengyihao 已提交
238
    transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
239
    if (cost >= EXCEPTION_LIMIT_US) {
dengyihao's avatar
dengyihao 已提交
240 241
      tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus, recv exception",
              transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, (int)cost);
dengyihao's avatar
dengyihao 已提交
242
    } else {
D
dapan1121 已提交
243
      tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus", transLabel(pTransInst), pConn,
dengyihao's avatar
dengyihao 已提交
244 245
              TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, (int)cost);
    }
dengyihao's avatar
dengyihao 已提交
246
  } else {
dengyihao's avatar
dengyihao 已提交
247
    if (cost >= EXCEPTION_LIMIT_US) {
D
dapan1121 已提交
248
      tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, noResp:%d, code:%d, cost:%dus, recv exception",
dengyihao's avatar
dengyihao 已提交
249 250
              transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, pHead->noResp,
              transMsg.code, (int)(cost));
dengyihao's avatar
dengyihao 已提交
251
    } else {
D
dapan1121 已提交
252
      tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, noResp:%d, code:%d, cost:%dus",
dengyihao's avatar
dengyihao 已提交
253 254
              transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, pHead->noResp,
              transMsg.code, (int)(cost));
dengyihao's avatar
dengyihao 已提交
255
    }
dengyihao's avatar
dengyihao 已提交
256 257
  }

dengyihao's avatar
dengyihao 已提交
258
  // pHead->noResp = 1,
dengyihao's avatar
dengyihao 已提交
259 260 261
  // 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 已提交
262
  transMsg.info.ahandle = (void*)pHead->ahandle;
dengyihao's avatar
dengyihao 已提交
263
  transMsg.info.handle = (void*)transAcquireExHandle(transGetRefMgt(), pConn->refId);
S
Shengliang Guan 已提交
264
  transMsg.info.refId = pConn->refId;
dengyihao's avatar
dengyihao 已提交
265
  transMsg.info.traceId = pHead->traceId;
dengyihao's avatar
dengyihao 已提交
266
  transMsg.info.cliVer = htonl(pHead->compatibilityVer);
dengyihao's avatar
dengyihao 已提交
267

dengyihao's avatar
dengyihao 已提交
268 269
  tGTrace("%s handle %p conn:%p translated to app, refId:%" PRIu64, transLabel(pTransInst), transMsg.info.handle, pConn,
          pConn->refId);
dengyihao's avatar
dengyihao 已提交
270 271 272 273
  ASSERTS(transMsg.info.handle != NULL, "trans-svr failed to alloc handle to msg");
  if (transMsg.info.handle == NULL) {
    return false;
  }
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
  pConnInfo->clientIp = pConn->clientIp;
  pConnInfo->clientPort = pConn->port;
dengyihao's avatar
dengyihao 已提交
283 284
  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
  return true;
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 293 294
  SSvrConn* conn = cli->data;
  STrans*   pTransInst = conn->pTransInst;

dengyihao's avatar
dengyihao 已提交
295
  SConnBuffer* pBuf = &conn->readBuf;
dengyihao's avatar
dengyihao 已提交
296 297
  if (nread > 0) {
    pBuf->len += nread;
dengyihao's avatar
dengyihao 已提交
298 299
    if (pBuf->len <= TRANS_PACKET_LIMIT) {
      while (transReadComplete(pBuf)) {
dengyihao's avatar
dengyihao 已提交
300
        if (true == pBuf->invalid || false == uvHandleReq(conn)) {
dengyihao's avatar
dengyihao 已提交
301 302
          tError("%s conn %p read invalid packet, received from %s, local info:%s", transLabel(pTransInst), conn,
                 conn->dst, conn->src);
dengyihao's avatar
dengyihao 已提交
303
          destroyConn(conn, true);
dengyihao's avatar
dengyihao 已提交
304
          return;
dengyihao's avatar
dengyihao 已提交
305
        }
dengyihao's avatar
dengyihao 已提交
306 307 308
      }
      return;
    } else {
dengyihao's avatar
dengyihao 已提交
309
      tError("%s conn %p read invalid packet, exceed limit, received from %s, local info:%s", transLabel(pTransInst),
dengyihao's avatar
dengyihao 已提交
310
             conn, conn->dst, conn->src);
dengyihao's avatar
dengyihao 已提交
311 312
      destroyConn(conn, true);
      return;
dengyihao's avatar
dengyihao 已提交
313 314
    }
  }
315 316 317
  if (nread == 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
318

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

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

dengyihao's avatar
dengyihao 已提交
344
void uvOnSendCb(uv_write_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
345
  SSvrConn* conn = transReqQueueRemove(req);
dengyihao's avatar
dengyihao 已提交
346 347
  if (conn == NULL) return;

dengyihao's avatar
dengyihao 已提交
348
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
349
    tTrace("conn %p data already was written on stream", conn);
dengyihao's avatar
dengyihao 已提交
350
    if (!transQueueEmpty(&conn->srvMsgs)) {
dengyihao's avatar
dengyihao 已提交
351 352 353 354
      SSvrMsg*  msg = transQueuePop(&conn->srvMsgs);
      STraceId* trace = &msg->msg.info.traceId;
      tGDebug("conn %p write data out", conn);

dengyihao's avatar
add UT  
dengyihao 已提交
355
      destroySmsg(msg);
dengyihao's avatar
dengyihao 已提交
356
      // send cached data
dengyihao's avatar
dengyihao 已提交
357
      if (!transQueueEmpty(&conn->srvMsgs)) {
dengyihao's avatar
dengyihao 已提交
358
        msg = (SSvrMsg*)transQueueGet(&conn->srvMsgs, 0);
dengyihao's avatar
dengyihao 已提交
359 360 361 362 363 364 365 366 367
        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 已提交
368
          transQueuePop(&conn->srvMsgs);
wafwerar's avatar
wafwerar 已提交
369
          taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
370

dengyihao's avatar
dengyihao 已提交
371
          msg = (SSvrMsg*)transQueueGet(&conn->srvMsgs, 0);
dengyihao's avatar
dengyihao 已提交
372
          if (msg != NULL) {
dengyihao's avatar
dengyihao 已提交
373
            uvStartSendRespImpl(msg);
dengyihao's avatar
dengyihao 已提交
374
          }
dengyihao's avatar
dengyihao 已提交
375
        } else {
dengyihao's avatar
dengyihao 已提交
376
          uvStartSendRespImpl(msg);
dengyihao's avatar
dengyihao 已提交
377
        }
dengyihao's avatar
add UT  
dengyihao 已提交
378
      }
dengyihao's avatar
dengyihao 已提交
379
    }
dengyihao's avatar
dengyihao 已提交
380
    transUnrefSrvHandle(conn);
dengyihao's avatar
dengyihao 已提交
381
  } else {
dengyihao's avatar
dengyihao 已提交
382 383 384 385 386
    if (!uv_is_closing((uv_handle_t*)(conn->pTcp))) {
      tError("conn %p failed to write data, %s", conn, uv_err_name(status));
      conn->broken = true;
      transUnrefSrvHandle(conn);
    }
dengyihao's avatar
dengyihao 已提交
387 388
  }
}
dengyihao's avatar
dengyihao 已提交
389 390
static void uvOnPipeWriteCb(uv_write_t* req, int status) {
  if (status == 0) {
dengyihao's avatar
dengyihao 已提交
391
    tTrace("success to dispatch conn to work thread");
dengyihao's avatar
dengyihao 已提交
392 393 394
  } else {
    tError("fail to dispatch conn to work thread");
  }
dengyihao's avatar
dengyihao 已提交
395 396 397 398 399
  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 已提交
400
  taosMemoryFree(req);
dengyihao's avatar
dengyihao 已提交
401
}
dengyihao's avatar
dengyihao 已提交
402

dengyihao's avatar
dengyihao 已提交
403
static int uvPrepareSendData(SSvrMsg* smsg, uv_buf_t* wb) {
dengyihao's avatar
dengyihao 已提交
404
  SSvrConn*  pConn = smsg->pConn;
dengyihao's avatar
formate  
dengyihao 已提交
405
  STransMsg* pMsg = &smsg->msg;
dengyihao's avatar
dengyihao 已提交
406 407 408 409 410
  if (pMsg->pCont == 0) {
    pMsg->pCont = (void*)rpcMallocCont(0);
    pMsg->contLen = 0;
  }
  STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
S
Shengliang Guan 已提交
411
  pHead->ahandle = (uint64_t)pMsg->info.ahandle;
dengyihao's avatar
dengyihao 已提交
412
  pHead->traceId = pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
413
  pHead->hasEpSet = pMsg->info.hasEpSet;
dengyihao's avatar
dengyihao 已提交
414
  pHead->magicNum = htonl(TRANS_MAGIC_NUM);
dengyihao's avatar
dengyihao 已提交
415
  pHead->compatibilityVer = htonl(((STrans*)pConn->pTransInst)->compatibilityVer);
dengyihao's avatar
dengyihao 已提交
416
  pHead->version = TRANS_VER;
dengyihao's avatar
dengyihao 已提交
417

dengyihao's avatar
dengyihao 已提交
418
  // handle invalid drop_task resp, TD-20098
dengyihao's avatar
dengyihao 已提交
419
  if (pConn->inType == TDMT_SCH_DROP_TASK && pMsg->code == TSDB_CODE_VND_INVALID_VGROUP_ID) {
dengyihao's avatar
dengyihao 已提交
420 421 422 423 424
    transQueuePop(&pConn->srvMsgs);
    destroySmsg(smsg);
    return -1;
  }

dengyihao's avatar
dengyihao 已提交
425
  if (pConn->status == ConnNormal) {
dengyihao's avatar
dengyihao 已提交
426
    pHead->msgType = (0 == pMsg->msgType ? pConn->inType + 1 : pMsg->msgType);
dengyihao's avatar
dengyihao 已提交
427
    if (smsg->type == Release) pHead->msgType = 0;
dengyihao's avatar
dengyihao 已提交
428
  } else {
dengyihao's avatar
dengyihao 已提交
429 430 431
    if (smsg->type == Release) {
      pHead->msgType = 0;
      pConn->status = ConnNormal;
dengyihao's avatar
dengyihao 已提交
432
      destroyConnRegArg(pConn);
dengyihao's avatar
dengyihao 已提交
433 434
      transUnrefSrvHandle(pConn);
    } else {
dengyihao's avatar
dengyihao 已提交
435
      // set up resp msg type
dengyihao's avatar
dengyihao 已提交
436
      pHead->msgType = (0 == pMsg->msgType ? pConn->inType + 1 : pMsg->msgType);
dengyihao's avatar
dengyihao 已提交
437
    }
dengyihao's avatar
dengyihao 已提交
438
  }
dengyihao's avatar
dengyihao 已提交
439

dengyihao's avatar
dengyihao 已提交
440
  pHead->release = smsg->type == Release ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
441
  pHead->code = htonl(pMsg->code);
dengyihao's avatar
dengyihao 已提交
442
  pHead->msgLen = htonl(pMsg->contLen + sizeof(STransMsgHead));
dengyihao's avatar
dengyihao 已提交
443

dengyihao's avatar
dengyihao 已提交
444 445
  char*   msg = (char*)pHead;
  int32_t len = transMsgLenFromCont(pMsg->contLen);
dengyihao's avatar
dengyihao 已提交
446

dengyihao's avatar
dengyihao 已提交
447
  STrans* pTransInst = pConn->pTransInst;
dengyihao's avatar
dengyihao 已提交
448
  if (pTransInst->compressSize != -1 && pTransInst->compressSize < pMsg->contLen) {
dengyihao's avatar
dengyihao 已提交
449 450 451 452
    len = transCompressMsg(pMsg->pCont, pMsg->contLen) + sizeof(STransMsgHead);
    pHead->msgLen = (int32_t)htonl((uint32_t)len);
  }

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

dengyihao's avatar
dengyihao 已提交
457
  wb->base = (char*)pHead;
dengyihao's avatar
dengyihao 已提交
458
  wb->len = len;
dengyihao's avatar
dengyihao 已提交
459
  return 0;
dengyihao's avatar
dengyihao 已提交
460
}
dengyihao's avatar
dengyihao 已提交
461

dengyihao's avatar
dengyihao 已提交
462
static FORCE_INLINE void uvStartSendRespImpl(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
463 464 465 466 467
  SSvrConn* pConn = smsg->pConn;
  if (pConn->broken) {
    return;
  }

dengyihao's avatar
dengyihao 已提交
468
  uv_buf_t wb;
dengyihao's avatar
dengyihao 已提交
469 470 471
  if (uvPrepareSendData(smsg, &wb) < 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
472

dengyihao's avatar
dengyihao 已提交
473
  transRefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
474
  uv_write_t* req = transReqQueuePush(&pConn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
475
  uv_write(req, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnSendCb);
dengyihao's avatar
dengyihao 已提交
476
}
dengyihao's avatar
dengyihao 已提交
477
static void uvStartSendResp(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
478
  // impl
dengyihao's avatar
dengyihao 已提交
479
  SSvrConn* pConn = smsg->pConn;
dengyihao's avatar
dengyihao 已提交
480
  if (pConn->broken == true) {
dengyihao's avatar
dengyihao 已提交
481
    // persist by
dengyihao's avatar
dengyihao 已提交
482
    destroySmsg(smsg);
dengyihao's avatar
dengyihao 已提交
483 484 485
    transUnrefSrvHandle(pConn);
    return;
  }
dengyihao's avatar
dengyihao 已提交
486 487 488
  if (pConn->status == ConnNormal) {
    transUnrefSrvHandle(pConn);
  }
dengyihao's avatar
dengyihao 已提交
489

dengyihao's avatar
dengyihao 已提交
490
  if (!transQueuePush(&pConn->srvMsgs, smsg)) {
dengyihao's avatar
dengyihao 已提交
491 492
    return;
  }
dengyihao's avatar
dengyihao 已提交
493
  uvStartSendRespImpl(smsg);
dengyihao's avatar
dengyihao 已提交
494 495
  return;
}
dengyihao's avatar
dengyihao 已提交
496

dengyihao's avatar
dengyihao 已提交
497
static FORCE_INLINE void destroySmsg(SSvrMsg* smsg) {
dengyihao's avatar
dengyihao 已提交
498 499 500 501
  if (smsg == NULL) {
    return;
  }
  transFreeMsg(smsg->msg.pCont);
wafwerar's avatar
wafwerar 已提交
502
  taosMemoryFree(smsg);
dengyihao's avatar
dengyihao 已提交
503
}
dengyihao's avatar
dengyihao 已提交
504
static void destroyAllConn(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
505
  tTrace("thread %p destroy all conn ", pThrd);
dengyihao's avatar
fix bug  
dengyihao 已提交
506 507 508 509 510
  while (!QUEUE_IS_EMPTY(&pThrd->conn)) {
    queue* h = QUEUE_HEAD(&pThrd->conn);
    QUEUE_REMOVE(h);
    QUEUE_INIT(h);

dengyihao's avatar
dengyihao 已提交
511
    SSvrConn* c = QUEUE_DATA(h, SSvrConn, queue);
dengyihao's avatar
dengyihao 已提交
512 513 514
    while (T_REF_VAL_GET(c) >= 2) {
      transUnrefSrvHandle(c);
    }
dengyihao's avatar
dengyihao 已提交
515
    transUnrefSrvHandle(c);
dengyihao's avatar
fix bug  
dengyihao 已提交
516 517
  }
}
dengyihao's avatar
dengyihao 已提交
518
void uvWorkerAsyncCb(uv_async_t* handle) {
dengyihao's avatar
dengyihao 已提交
519 520 521 522
  SAsyncItem* item = handle->data;
  SWorkThrd*  pThrd = item->pThrd;
  SSvrConn*   conn = NULL;
  queue       wq;
dengyihao's avatar
dengyihao 已提交
523

dengyihao's avatar
dengyihao 已提交
524
  // batch process to avoid to lock/unlock frequently
wafwerar's avatar
wafwerar 已提交
525
  taosThreadMutexLock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
526
  QUEUE_MOVE(&item->qmsg, &wq);
wafwerar's avatar
wafwerar 已提交
527
  taosThreadMutexUnlock(&item->mtx);
dengyihao's avatar
dengyihao 已提交
528 529 530 531

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

dengyihao's avatar
dengyihao 已提交
533
    SSvrMsg* msg = QUEUE_DATA(head, SSvrMsg, q);
dengyihao's avatar
dengyihao 已提交
534
    if (msg == NULL) {
dengyihao's avatar
dengyihao 已提交
535
      tError("unexcept occurred, continue");
dengyihao's avatar
dengyihao 已提交
536
      continue;
dengyihao's avatar
dengyihao 已提交
537
    }
dengyihao's avatar
dengyihao 已提交
538

dengyihao's avatar
dengyihao 已提交
539
    // release handle to rpc init
dengyihao's avatar
dengyihao 已提交
540 541 542 543 544
    if (msg->type == Quit) {
      (*transAsyncHandle[msg->type])(msg, pThrd);
    } else {
      STransMsg transMsg = msg->msg;

S
Shengliang Guan 已提交
545 546
      SExHandle* exh1 = transMsg.info.handle;
      int64_t    refId = transMsg.info.refId;
dengyihao's avatar
dengyihao 已提交
547
      SExHandle* exh2 = transAcquireExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
548
      if (exh2 == NULL || exh1 != exh2) {
dengyihao's avatar
dengyihao 已提交
549
        tTrace("handle except msg %p, ignore it", exh1);
dengyihao's avatar
dengyihao 已提交
550
        transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
551 552 553 554
        destroySmsg(msg);
        continue;
      }
      msg->pConn = exh1->handle;
dengyihao's avatar
dengyihao 已提交
555
      transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
556
      (*transAsyncHandle[msg->type])(msg, pThrd);
dengyihao's avatar
dengyihao 已提交
557
    }
dengyihao's avatar
dengyihao 已提交
558 559
  }
}
dengyihao's avatar
dengyihao 已提交
560 561 562 563 564
static void uvWalkCb(uv_handle_t* handle, void* arg) {
  if (!uv_is_closing(handle)) {
    uv_close(handle, NULL);
  }
}
dengyihao's avatar
dengyihao 已提交
565 566 567 568
static void uvFreeCb(uv_handle_t* handle) {
  //
  taosMemoryFree(handle);
}
dengyihao's avatar
dengyihao 已提交
569

dengyihao's avatar
dengyihao 已提交
570 571
static void uvAcceptAsyncCb(uv_async_t* async) {
  SServerObj* srv = async->data;
dengyihao's avatar
dengyihao 已提交
572
  tDebug("close server port %d", srv->port);
dengyihao's avatar
dengyihao 已提交
573
  uv_walk(srv->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
574
}
dengyihao's avatar
dengyihao 已提交
575

dengyihao's avatar
dengyihao 已提交
576
static void uvShutDownCb(uv_shutdown_t* req, int status) {
dengyihao's avatar
dengyihao 已提交
577
  if (status != 0) {
S
Shengliang Guan 已提交
578
    tDebug("conn failed to shut down:%s", uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
579
  }
dengyihao's avatar
dengyihao 已提交
580
  uv_close((uv_handle_t*)req->handle, uvDestroyConn);
wafwerar's avatar
wafwerar 已提交
581
  taosMemoryFree(req);
dengyihao's avatar
dengyihao 已提交
582
}
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
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 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
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 已提交
658

dengyihao's avatar
dengyihao 已提交
659 660 661
static void uvWorkDoTask(uv_work_t* req) {
  // doing time-consumeing task
  // only auth conn currently, add more func later
dengyihao's avatar
dengyihao 已提交
662
  tTrace("conn %p start to be processed in BG Thread", req->data);
dengyihao's avatar
dengyihao 已提交
663 664 665 666 667
  return;
}

static void uvWorkAfterTask(uv_work_t* req, int status) {
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
668
    tTrace("conn %p failed to processed ", req->data);
dengyihao's avatar
dengyihao 已提交
669 670 671 672
  }
  // Done time-consumeing task
  // add more func later
  // this func called in main loop
dengyihao's avatar
dengyihao 已提交
673
  tTrace("conn %p already processed ", req->data);
dengyihao's avatar
dengyihao 已提交
674 675 676
  taosMemoryFree(req);
}

dengyihao's avatar
dengyihao 已提交
677 678 679 680 681 682
void uvOnAcceptCb(uv_stream_t* stream, int status) {
  if (status == -1) {
    return;
  }
  SServerObj* pObj = container_of(stream, SServerObj, server);

wafwerar's avatar
wafwerar 已提交
683
  uv_tcp_t* cli = (uv_tcp_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
684
  if (cli == NULL) return;
dengyihao's avatar
dengyihao 已提交
685

dengyihao's avatar
dengyihao 已提交
686 687 688 689 690 691 692 693
  int err = uv_tcp_init(pObj->loop, cli);
  if (err != 0) {
    tError("failed to create tcp: %s", uv_err_name(err));
    taosMemoryFree(cli);
    return;
  }
  err = uv_accept(stream, (uv_stream_t*)cli);
  if (err == 0) {
dengyihao's avatar
dengyihao 已提交
694
#if defined(WINDOWS) || defined(DARWIN)
wafwerar's avatar
wafwerar 已提交
695
    if (pObj->numOfWorkerReady < pObj->numOfThreads) {
dengyihao's avatar
dengyihao 已提交
696 697
      tError("worker-threads are not ready for all, need %d instead of %d.", pObj->numOfThreads,
             pObj->numOfWorkerReady);
dengyihao's avatar
dengyihao 已提交
698
      uv_close((uv_handle_t*)cli, uvFreeCb);
wafwerar's avatar
wafwerar 已提交
699 700
      return;
    }
dengyihao's avatar
dengyihao 已提交
701
#endif
dengyihao's avatar
dengyihao 已提交
702

wafwerar's avatar
wafwerar 已提交
703
    uv_write_t* wr = (uv_write_t*)taosMemoryMalloc(sizeof(uv_write_t));
dengyihao's avatar
dengyihao 已提交
704
    wr->data = cli;
dengyihao's avatar
dengyihao 已提交
705 706 707
    uv_buf_t buf = uv_buf_init((char*)notify, strlen(notify));

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

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

dengyihao's avatar
dengyihao 已提交
711
    uv_write2(wr, (uv_stream_t*)&(pObj->pipe[pObj->workerIdx][0]), &buf, 1, (uv_stream_t*)cli, uvOnPipeWriteCb);
dengyihao's avatar
dengyihao 已提交
712
  } else {
dengyihao's avatar
dengyihao 已提交
713
    if (!uv_is_closing((uv_handle_t*)cli)) {
dengyihao's avatar
dengyihao 已提交
714 715
      tError("failed to accept tcp: %s", uv_err_name(err));
      uv_close((uv_handle_t*)cli, uvFreeCb);
dengyihao's avatar
dengyihao 已提交
716
    } else {
dengyihao's avatar
dengyihao 已提交
717
      tError("failed to accept tcp: %s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
718 719
      taosMemoryFree(cli);
    }
dengyihao's avatar
dengyihao 已提交
720 721 722
  }
}
void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) {
dengyihao's avatar
dengyihao 已提交
723
  tTrace("connection coming");
dengyihao's avatar
dengyihao 已提交
724 725 726 727 728
  if (nread < 0) {
    if (nread != UV_EOF) {
      tError("read error %s", uv_err_name(nread));
    }
    // TODO(log other failure reason)
dengyihao's avatar
dengyihao 已提交
729
    tWarn("failed to create connect:%p, reason: %s", q, uv_err_name(nread));
dengyihao's avatar
dengyihao 已提交
730 731
    taosMemoryFree(buf->base);
    uv_close((uv_handle_t*)q, NULL);
dengyihao's avatar
dengyihao 已提交
732 733 734
    return;
  }
  // free memory allocated by
dengyihao's avatar
dengyihao 已提交
735 736
  ASSERTS(nread == strlen(notify), "trans-svr mem corrupted");
  ASSERTS(buf->base[0] == notify[0], "trans-svr mem corrupted");
wafwerar's avatar
wafwerar 已提交
737
  taosMemoryFree(buf->base);
dengyihao's avatar
dengyihao 已提交
738

dengyihao's avatar
dengyihao 已提交
739
  SWorkThrd* pThrd = q->data;
dengyihao's avatar
dengyihao 已提交
740 741 742 743

  uv_pipe_t* pipe = (uv_pipe_t*)q;
  if (!uv_pipe_pending_count(pipe)) {
    tError("No pending count");
dengyihao's avatar
dengyihao 已提交
744 745 746 747 748 749 750
    uv_close((uv_handle_t*)q, NULL);
    return;
  }
  if (pThrd->quit) {
    tWarn("thread already received quit msg, ignore incoming conn");

    uv_close((uv_handle_t*)q, NULL);
dengyihao's avatar
dengyihao 已提交
751 752 753
    return;
  }

dengyihao's avatar
dengyihao 已提交
754
  // uv_handle_type pending = uv_pipe_pending_type(pipe);
dengyihao's avatar
dengyihao 已提交
755

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

dengyihao's avatar
dengyihao 已提交
758
  pConn->pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
759
  /* init conn timer*/
dengyihao's avatar
dengyihao 已提交
760 761
  // uv_timer_init(pThrd->loop, &pConn->pTimer);
  // pConn->pTimer.data = pConn;
dengyihao's avatar
dengyihao 已提交
762 763 764 765

  pConn->hostThrd = pThrd;

  // init client handle
wafwerar's avatar
wafwerar 已提交
766
  pConn->pTcp = (uv_tcp_t*)taosMemoryMalloc(sizeof(uv_tcp_t));
dengyihao's avatar
dengyihao 已提交
767 768 769
  uv_tcp_init(pThrd->loop, pConn->pTcp);
  pConn->pTcp->data = pConn;

dengyihao's avatar
dengyihao 已提交
770
  // transSetConnOption((uv_tcp_t*)pConn->pTcp);
dengyihao's avatar
dengyihao 已提交
771

dengyihao's avatar
dengyihao 已提交
772 773 774
  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 已提交
775
    tTrace("conn %p created, fd:%d", pConn, fd);
dengyihao's avatar
dengyihao 已提交
776

dengyihao's avatar
dengyihao 已提交
777 778 779
    struct sockaddr peername, sockname;
    int             addrlen = sizeof(peername);
    if (0 != uv_tcp_getpeername(pConn->pTcp, (struct sockaddr*)&peername, &addrlen)) {
dengyihao's avatar
dengyihao 已提交
780
      tError("conn %p failed to get peer info", pConn);
dengyihao's avatar
dengyihao 已提交
781
      transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
782
      return;
dengyihao's avatar
dengyihao 已提交
783
    }
dengyihao's avatar
dengyihao 已提交
784
    transSockInfo2Str(&peername, pConn->dst);
dengyihao's avatar
dengyihao 已提交
785

dengyihao's avatar
dengyihao 已提交
786 787
    addrlen = sizeof(sockname);
    if (0 != uv_tcp_getsockname(pConn->pTcp, (struct sockaddr*)&sockname, &addrlen)) {
dengyihao's avatar
dengyihao 已提交
788
      tError("conn %p failed to get local info", pConn);
dengyihao's avatar
dengyihao 已提交
789
      transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
790 791
      return;
    }
dengyihao's avatar
dengyihao 已提交
792
    transSockInfo2Str(&sockname, pConn->src);
dengyihao's avatar
dengyihao 已提交
793

dengyihao's avatar
dengyihao 已提交
794
    struct sockaddr_in addr = *(struct sockaddr_in*)&peername;
dengyihao's avatar
dengyihao 已提交
795 796
    pConn->clientIp = addr.sin_addr.s_addr;
    pConn->port = ntohs(addr.sin_port);
dengyihao's avatar
dengyihao 已提交
797

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

dengyihao's avatar
dengyihao 已提交
800
  } else {
dengyihao's avatar
dengyihao 已提交
801
    tDebug("failed to create new connection");
dengyihao's avatar
dengyihao 已提交
802
    transUnrefSrvHandle(pConn);
dengyihao's avatar
dengyihao 已提交
803 804 805
  }
}

806
void* transAcceptThread(void* arg) {
dengyihao's avatar
dengyihao 已提交
807
  // opt
dengyihao's avatar
dengyihao 已提交
808
  setThreadName("trans-accept");
dengyihao's avatar
dengyihao 已提交
809 810
  SServerObj* srv = (SServerObj*)arg;
  uv_run(srv->loop, UV_RUN_DEFAULT);
811 812

  return NULL;
dengyihao's avatar
dengyihao 已提交
813
}
dengyihao's avatar
dengyihao 已提交
814
void uvOnPipeConnectionCb(uv_connect_t* connect, int status) {
wafwerar's avatar
wafwerar 已提交
815 816 817
  if (status != 0) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
818
  SWorkThrd* pThrd = container_of(connect, SWorkThrd, connect_req);
wafwerar's avatar
wafwerar 已提交
819 820
  uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb);
}
dengyihao's avatar
dengyihao 已提交
821
static bool addHandleToWorkloop(SWorkThrd* pThrd, char* pipeName) {
wafwerar's avatar
wafwerar 已提交
822
  pThrd->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
823 824 825
  if (0 != uv_loop_init(pThrd->loop)) {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
826

dengyihao's avatar
dengyihao 已提交
827
#if defined(WINDOWS) || defined(DARWIN)
dengyihao's avatar
dengyihao 已提交
828
  uv_pipe_init(pThrd->loop, pThrd->pipe, 1);
dengyihao's avatar
dengyihao 已提交
829 830 831 832
#else
  uv_pipe_init(pThrd->loop, pThrd->pipe, 1);
  uv_pipe_open(pThrd->pipe, pThrd->fd);
#endif
dengyihao's avatar
dengyihao 已提交
833 834 835

  pThrd->pipe->data = pThrd;

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

dengyihao's avatar
dengyihao 已提交
838 839 840 841 842
  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 已提交
843 844 845
  // conn set
  QUEUE_INIT(&pThrd->conn);

dengyihao's avatar
dengyihao 已提交
846
  pThrd->asyncPool = transAsyncPoolCreate(pThrd->loop, 8, pThrd, uvWorkerAsyncCb);
dengyihao's avatar
dengyihao 已提交
847
#if defined(WINDOWS) || defined(DARWIN)
wafwerar's avatar
wafwerar 已提交
848
  uv_pipe_connect(&pThrd->connect_req, pThrd->pipe, pipeName, uvOnPipeConnectionCb);
dengyihao's avatar
dengyihao 已提交
849 850 851
#else
  uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb);
#endif
dengyihao's avatar
dengyihao 已提交
852 853 854 855 856 857 858 859 860
  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 已提交
861
    tError("failed to init accept server:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
862 863 864
    return false;
  }

dengyihao's avatar
dengyihao 已提交
865
  // register an async here to quit server gracefully
wafwerar's avatar
wafwerar 已提交
866
  srv->pAcceptAsync = taosMemoryCalloc(1, sizeof(uv_async_t));
dengyihao's avatar
dengyihao 已提交
867 868
  uv_async_init(srv->loop, srv->pAcceptAsync, uvAcceptAsyncCb);
  srv->pAcceptAsync->data = srv;
dengyihao's avatar
dengyihao 已提交
869

dengyihao's avatar
dengyihao 已提交
870
  struct sockaddr_in bind_addr;
dengyihao's avatar
dengyihao 已提交
871 872
  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 已提交
873
    tError("failed to bind:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
874 875
    return false;
  }
dengyihao's avatar
dengyihao 已提交
876
  if ((err = uv_listen((uv_stream_t*)&srv->server, 4096 * 2, uvOnAcceptCb)) != 0) {
S
Shengliang Guan 已提交
877
    tError("failed to listen:%s", uv_err_name(err));
dengyihao's avatar
dengyihao 已提交
878
    terrno = TSDB_CODE_RPC_PORT_EADDRINUSE;
dengyihao's avatar
dengyihao 已提交
879 880 881
    return false;
  }
  return true;
dengyihao's avatar
dengyihao 已提交
882
}
883
void* transWorkerThread(void* arg) {
G
gccgdb1234 已提交
884
  setThreadName("trans-svr-work");
dengyihao's avatar
dengyihao 已提交
885
  SWorkThrd* pThrd = (SWorkThrd*)arg;
dengyihao's avatar
dengyihao 已提交
886
  uv_run(pThrd->loop, UV_RUN_DEFAULT);
887 888

  return NULL;
dengyihao's avatar
dengyihao 已提交
889 890
}

dengyihao's avatar
dengyihao 已提交
891
static FORCE_INLINE SSvrConn* createConn(void* hThrd) {
dengyihao's avatar
dengyihao 已提交
892
  SWorkThrd* pThrd = hThrd;
dengyihao's avatar
fix bug  
dengyihao 已提交
893

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

dengyihao's avatar
dengyihao 已提交
896
  transReqQueueInit(&pConn->wreqQueue);
dengyihao's avatar
fix bug  
dengyihao 已提交
897 898 899
  QUEUE_INIT(&pConn->queue);

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

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

dengyihao's avatar
dengyihao 已提交
903
  memset(&pConn->regArg, 0, sizeof(pConn->regArg));
dengyihao's avatar
dengyihao 已提交
904
  pConn->broken = false;
dengyihao's avatar
dengyihao 已提交
905
  pConn->status = ConnNormal;
dengyihao's avatar
opt rpc  
dengyihao 已提交
906
  transInitBuffer(&pConn->readBuf);
dengyihao's avatar
dengyihao 已提交
907

dengyihao's avatar
dengyihao 已提交
908 909 910
  SExHandle* exh = taosMemoryMalloc(sizeof(SExHandle));
  exh->handle = pConn;
  exh->pThrd = pThrd;
dengyihao's avatar
dengyihao 已提交
911 912
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  transAcquireExHandle(transGetRefMgt(), exh->refId);
dengyihao's avatar
dengyihao 已提交
913

dengyihao's avatar
dengyihao 已提交
914
  STrans* pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
915
  pConn->refId = exh->refId;
dengyihao's avatar
dengyihao 已提交
916
  transRefSrvHandle(pConn);
S
Shengliang Guan 已提交
917
  tTrace("%s handle %p, conn %p created, refId:%" PRId64, transLabel(pTransInst), exh, pConn, pConn->refId);
dengyihao's avatar
dengyihao 已提交
918 919
  return pConn;
}
dengyihao's avatar
dengyihao 已提交
920

dengyihao's avatar
dengyihao 已提交
921
static FORCE_INLINE void destroyConn(SSvrConn* conn, bool clear) {
dengyihao's avatar
dengyihao 已提交
922 923 924
  if (conn == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
925

926
  if (clear) {
dengyihao's avatar
dengyihao 已提交
927
    if (!uv_is_closing((uv_handle_t*)conn->pTcp)) {
dengyihao's avatar
dengyihao 已提交
928 929 930
      tTrace("conn %p to be destroyed", conn);
      uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn);
    }
931
  }
dengyihao's avatar
dengyihao 已提交
932
}
dengyihao's avatar
dengyihao 已提交
933
static FORCE_INLINE void destroyConnRegArg(SSvrConn* conn) {
dengyihao's avatar
dengyihao 已提交
934 935 936 937 938
  if (conn->regArg.init == 1) {
    transFreeMsg(conn->regArg.msg.pCont);
    conn->regArg.init = 0;
  }
}
dengyihao's avatar
dengyihao 已提交
939
static int reallocConnRef(SSvrConn* conn) {
dengyihao's avatar
dengyihao 已提交
940 941
  transReleaseExHandle(transGetRefMgt(), conn->refId);
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
942 943 944 945
  // 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 已提交
946 947
  exh->refId = transAddExHandle(transGetRefMgt(), exh);
  transAcquireExHandle(transGetRefMgt(), exh->refId);
dengyihao's avatar
dengyihao 已提交
948 949 950 951
  conn->refId = exh->refId;

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

dengyihao's avatar
dengyihao 已提交
955 956 957
  if (conn == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
958
  SWorkThrd* thrd = conn->hostThrd;
dengyihao's avatar
dengyihao 已提交
959

dengyihao's avatar
dengyihao 已提交
960 961
  transReleaseExHandle(transGetRefMgt(), conn->refId);
  transRemoveExHandle(transGetRefMgt(), conn->refId);
dengyihao's avatar
dengyihao 已提交
962

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

dengyihao's avatar
dengyihao 已提交
966 967 968 969
  for (int i = 0; i < transQueueSize(&conn->srvMsgs); i++) {
    SSvrMsg* msg = transQueueGet(&conn->srvMsgs, i);
    destroySmsg(msg);
  }
dengyihao's avatar
dengyihao 已提交
970
  transQueueDestroy(&conn->srvMsgs);
dengyihao's avatar
dengyihao 已提交
971
  transReqQueueClear(&conn->wreqQueue);
dengyihao's avatar
dengyihao 已提交
972

dengyihao's avatar
dengyihao 已提交
973
  QUEUE_REMOVE(&conn->queue);
wafwerar's avatar
wafwerar 已提交
974
  taosMemoryFree(conn->pTcp);
dengyihao's avatar
dengyihao 已提交
975
  destroyConnRegArg(conn);
dengyihao's avatar
dengyihao 已提交
976
  transDestroyBuffer(&conn->readBuf);
dengyihao's avatar
dengyihao 已提交
977
  taosMemoryFree(conn);
dengyihao's avatar
dengyihao 已提交
978

dengyihao's avatar
dengyihao 已提交
979
  if (thrd->quit && QUEUE_IS_EMPTY(&thrd->conn)) {
dengyihao's avatar
dengyihao 已提交
980
    tTrace("work thread quit");
dengyihao's avatar
dengyihao 已提交
981
    uv_walk(thrd->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
982
  }
dengyihao's avatar
dengyihao 已提交
983
}
wafwerar's avatar
wafwerar 已提交
984
static void uvPipeListenCb(uv_stream_t* handle, int status) {
dengyihao's avatar
dengyihao 已提交
985
  if (status != 0) {
dengyihao's avatar
dengyihao 已提交
986
    tError("server failed to init pipe, errmsg: %s", uv_err_name(status));
dengyihao's avatar
dengyihao 已提交
987 988
    return;
  }
wafwerar's avatar
wafwerar 已提交
989 990

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

dengyihao's avatar
dengyihao 已提交
993
  int ret = uv_pipe_init(srv->loop, pipe, 1);
dengyihao's avatar
dengyihao 已提交
994 995
  ASSERTS(ret == 0, "trans-svr failed to init pipe");
  if (ret != 0) return;
wafwerar's avatar
wafwerar 已提交
996

dengyihao's avatar
dengyihao 已提交
997
  ret = uv_accept((uv_stream_t*)&srv->pipeListen, (uv_stream_t*)pipe);
dengyihao's avatar
dengyihao 已提交
998 999
  ASSERTS(ret == 0, "trans-svr failed to accept pipe msg");
  if (ret != 0) return;
dengyihao's avatar
dengyihao 已提交
1000 1001

  ret = uv_is_readable((uv_stream_t*)pipe);
dengyihao's avatar
dengyihao 已提交
1002 1003
  ASSERTS(ret == 1, "trans-svr pipe status corrupted");
  if (ret != 1) return;
wafwerar's avatar
wafwerar 已提交
1004

dengyihao's avatar
dengyihao 已提交
1005
  ret = uv_is_writable((uv_stream_t*)pipe);
dengyihao's avatar
dengyihao 已提交
1006
  ASSERTS(ret == 1, "trans-svr pipe status corrupted");
dengyihao's avatar
dengyihao 已提交
1007
  if (ret != 1) return;
wafwerar's avatar
wafwerar 已提交
1008

dengyihao's avatar
dengyihao 已提交
1009
  ret = uv_is_closing((uv_handle_t*)pipe);
dengyihao's avatar
dengyihao 已提交
1010 1011
  ASSERTS(ret == 0, "trans-svr pipe status corrupted");
  if (ret != 0) return;
dengyihao's avatar
dengyihao 已提交
1012 1013

  srv->numOfWorkerReady++;
wafwerar's avatar
wafwerar 已提交
1014
}
dengyihao's avatar
dengyihao 已提交
1015

U
ubuntu 已提交
1016
void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
wafwerar's avatar
wafwerar 已提交
1017 1018
  SServerObj* srv = taosMemoryCalloc(1, sizeof(SServerObj));
  srv->loop = (uv_loop_t*)taosMemoryMalloc(sizeof(uv_loop_t));
dengyihao's avatar
dengyihao 已提交
1019 1020
  srv->numOfThreads = numOfThreads;
  srv->workerIdx = 0;
wafwerar's avatar
wafwerar 已提交
1021
  srv->numOfWorkerReady = 0;
dengyihao's avatar
dengyihao 已提交
1022
  srv->pThreadObj = (SWorkThrd**)taosMemoryCalloc(srv->numOfThreads, sizeof(SWorkThrd*));
wafwerar's avatar
wafwerar 已提交
1023
  srv->pipe = (uv_pipe_t**)taosMemoryCalloc(srv->numOfThreads, sizeof(uv_pipe_t*));
dengyihao's avatar
dengyihao 已提交
1024 1025 1026 1027
  srv->ip = ip;
  srv->port = port;
  uv_loop_init(srv->loop);

dengyihao's avatar
dengyihao 已提交
1028
  char pipeName[PATH_MAX];
1029 1030 1031 1032 1033 1034 1035

  if (false == taosValidIpAndPort(srv->ip, srv->port)) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    tError("invalid ip/port, %d:%d, reason:%s", srv->ip, srv->port, terrstr());
    goto End;
  }

dengyihao's avatar
dengyihao 已提交
1036
#if defined(WINDOWS) || defined(DARWIN)
dengyihao's avatar
dengyihao 已提交
1037
  int ret = uv_pipe_init(srv->loop, &srv->pipeListen, 0);
dengyihao's avatar
dengyihao 已提交
1038 1039 1040 1041
  if (ret != 0) {
    tError("failed to init pipe, errmsg: %s", uv_err_name(ret));
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
1042
#if defined(WINDOWS)
dengyihao's avatar
dengyihao 已提交
1043
  snprintf(pipeName, sizeof(pipeName), "\\\\?\\pipe\\trans.rpc.%d-%" PRIu64, taosSafeRand(), GetCurrentProcessId());
dengyihao's avatar
dengyihao 已提交
1044 1045 1046 1047
#elif defined(DARWIN)
  snprintf(pipeName, sizeof(pipeName), "%s%spipe.trans.rpc.%08d-%" PRIu64, tsTempDir, TD_DIRSEP, taosSafeRand(),
           taosGetSelfPthreadId());
#endif
dengyihao's avatar
dengyihao 已提交
1048

dengyihao's avatar
dengyihao 已提交
1049
  ret = uv_pipe_bind(&srv->pipeListen, pipeName);
dengyihao's avatar
dengyihao 已提交
1050 1051 1052 1053
  if (ret != 0) {
    tError("failed to bind pipe, errmsg: %s", uv_err_name(ret));
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
1054 1055

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

dengyihao's avatar
dengyihao 已提交
1061
  for (int i = 0; i < srv->numOfThreads; i++) {
dengyihao's avatar
dengyihao 已提交
1062
    SWorkThrd* thrd = (SWorkThrd*)taosMemoryCalloc(1, sizeof(SWorkThrd));
dengyihao's avatar
dengyihao 已提交
1063
    thrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
1064
    thrd->quit = false;
dengyihao's avatar
dengyihao 已提交
1065
    srv->pThreadObj[i] = thrd;
dengyihao's avatar
dengyihao 已提交
1066
    thrd->pTransInst = shandle;
dengyihao's avatar
dengyihao 已提交
1067

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

dengyihao's avatar
dengyihao 已提交
1071
    if (false == addHandleToWorkloop(thrd, pipeName)) {
dengyihao's avatar
dengyihao 已提交
1072 1073
      goto End;
    }
dengyihao's avatar
dengyihao 已提交
1074

dengyihao's avatar
dengyihao 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    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];
dengyihao's avatar
dengyihao 已提交
1097
    if (uv_socketpair(SOCK_STREAM, 0, fds, UV_NONBLOCK_PIPE, UV_NONBLOCK_PIPE) != 0) {
dengyihao's avatar
dengyihao 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
      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;
    }

1111
    int err = taosThreadCreate(&(thrd->thread), NULL, transWorkerThread, (void*)(thrd));
dengyihao's avatar
dengyihao 已提交
1112
    if (err == 0) {
S
Shengliang Guan 已提交
1113
      tDebug("success to create worker-thread:%d", i);
dengyihao's avatar
dengyihao 已提交
1114 1115
    } else {
      // TODO: clear all other resource later
S
Shengliang Guan 已提交
1116
      tError("failed to create worker-thread:%d", i);
dengyihao's avatar
dengyihao 已提交
1117
      goto End;
dengyihao's avatar
dengyihao 已提交
1118 1119
    }
  }
dengyihao's avatar
dengyihao 已提交
1120
#endif
dengyihao's avatar
dengyihao 已提交
1121

dengyihao's avatar
dengyihao 已提交
1122 1123 1124
  if (false == addHandleToAcceptloop(srv)) {
    goto End;
  }
dengyihao's avatar
dengyihao 已提交
1125

1126
  int err = taosThreadCreate(&srv->thread, NULL, transAcceptThread, (void*)srv);
dengyihao's avatar
dengyihao 已提交
1127 1128 1129
  if (err == 0) {
    tDebug("success to create accept-thread");
  } else {
dengyihao's avatar
dengyihao 已提交
1130 1131
    tError("failed  to create accept-thread");
    goto End;
dengyihao's avatar
dengyihao 已提交
1132 1133
    // clear all resource later
  }
dengyihao's avatar
dengyihao 已提交
1134

dengyihao's avatar
dengyihao 已提交
1135
  srv->inited = true;
dengyihao's avatar
dengyihao 已提交
1136
  return srv;
dengyihao's avatar
dengyihao 已提交
1137
End:
U
ubuntu 已提交
1138
  transCloseServer(srv);
dengyihao's avatar
dengyihao 已提交
1139 1140
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
1141

dengyihao's avatar
dengyihao 已提交
1142
void uvHandleQuit(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1143
  thrd->quit = true;
dengyihao's avatar
dengyihao 已提交
1144
  if (QUEUE_IS_EMPTY(&thrd->conn)) {
dengyihao's avatar
dengyihao 已提交
1145
    uv_walk(thrd->loop, uvWalkCb, NULL);
dengyihao's avatar
dengyihao 已提交
1146 1147 1148
  } else {
    destroyAllConn(thrd);
  }
wafwerar's avatar
wafwerar 已提交
1149
  taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
1150
}
dengyihao's avatar
dengyihao 已提交
1151
void uvHandleRelease(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1152
  SSvrConn* conn = msg->pConn;
dengyihao's avatar
dengyihao 已提交
1153
  if (conn->status == ConnAcquire) {
dengyihao's avatar
dengyihao 已提交
1154
    reallocConnRef(conn);
dengyihao's avatar
dengyihao 已提交
1155
    if (!transQueuePush(&conn->srvMsgs, msg)) {
dengyihao's avatar
dengyihao 已提交
1156
      return;
dengyihao's avatar
dengyihao 已提交
1157
    }
dengyihao's avatar
dengyihao 已提交
1158
    uvStartSendRespImpl(msg);
dengyihao's avatar
dengyihao 已提交
1159
    return;
dengyihao's avatar
dengyihao 已提交
1160
  } else if (conn->status == ConnRelease || conn->status == ConnNormal) {
dengyihao's avatar
dengyihao 已提交
1161
    tDebug("%s conn %p already released, ignore release-msg", transLabel(thrd->pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
1162
  }
dengyihao's avatar
dengyihao 已提交
1163
  destroySmsg(msg);
dengyihao's avatar
dengyihao 已提交
1164
}
dengyihao's avatar
dengyihao 已提交
1165
void uvHandleResp(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1166
  // send msg to client
dengyihao's avatar
dengyihao 已提交
1167
  tDebug("%s conn %p start to send resp (2/2)", transLabel(thrd->pTransInst), msg->pConn);
dengyihao's avatar
dengyihao 已提交
1168 1169
  uvStartSendResp(msg);
}
dengyihao's avatar
dengyihao 已提交
1170
void uvHandleRegister(SSvrMsg* msg, SWorkThrd* thrd) {
dengyihao's avatar
dengyihao 已提交
1171
  SSvrConn* conn = msg->pConn;
dengyihao's avatar
dengyihao 已提交
1172
  tDebug("%s conn %p register brokenlink callback", transLabel(thrd->pTransInst), conn);
dengyihao's avatar
dengyihao 已提交
1173
  if (conn->status == ConnAcquire) {
dengyihao's avatar
dengyihao 已提交
1174
    if (!transQueuePush(&conn->srvMsgs, msg)) {
dengyihao's avatar
dengyihao 已提交
1175 1176
      return;
    }
dengyihao's avatar
dengyihao 已提交
1177
    transQueuePop(&conn->srvMsgs);
dengyihao's avatar
dengyihao 已提交
1178 1179 1180
    conn->regArg.notifyCount = 0;
    conn->regArg.init = 1;
    conn->regArg.msg = msg->msg;
dengyihao's avatar
dengyihao 已提交
1181
    tDebug("conn %p register brokenlink callback succ", conn);
dengyihao's avatar
dengyihao 已提交
1182 1183 1184 1185 1186 1187

    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 已提交
1188
    taosMemoryFree(msg);
dengyihao's avatar
dengyihao 已提交
1189 1190
  }
}
dengyihao's avatar
dengyihao 已提交
1191
void destroyWorkThrd(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
1192 1193 1194
  if (pThrd == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
1195
  taosThreadJoin(pThrd->thread, NULL);
dengyihao's avatar
dengyihao 已提交
1196
  SRV_RELEASE_UV(pThrd->loop);
dengyihao's avatar
dengyihao 已提交
1197
  TRANS_DESTROY_ASYNC_POOL_MSG(pThrd->asyncPool, SSvrMsg, destroySmsg);
dengyihao's avatar
dengyihao 已提交
1198
  transAsyncPoolDestroy(pThrd->asyncPool);
dengyihao's avatar
dengyihao 已提交
1199
  taosMemoryFree(pThrd->prepare);
dengyihao's avatar
dengyihao 已提交
1200
  taosMemoryFree(pThrd->loop);
wafwerar's avatar
wafwerar 已提交
1201
  taosMemoryFree(pThrd);
dengyihao's avatar
dengyihao 已提交
1202
}
dengyihao's avatar
dengyihao 已提交
1203
void sendQuitToWorkThrd(SWorkThrd* pThrd) {
dengyihao's avatar
dengyihao 已提交
1204
  SSvrMsg* msg = taosMemoryCalloc(1, sizeof(SSvrMsg));
dengyihao's avatar
dengyihao 已提交
1205
  msg->type = Quit;
dengyihao's avatar
dengyihao 已提交
1206
  tDebug("server send quit msg to work thread");
dengyihao's avatar
dengyihao 已提交
1207
  transAsyncSend(pThrd->asyncPool, &msg->q);
dengyihao's avatar
dengyihao 已提交
1208 1209
}

U
ubuntu 已提交
1210
void transCloseServer(void* arg) {
dengyihao's avatar
dengyihao 已提交
1211 1212
  // impl later
  SServerObj* srv = arg;
dengyihao's avatar
dengyihao 已提交
1213

dengyihao's avatar
dengyihao 已提交
1214
  if (srv->inited) {
1215
    tDebug("send quit msg to accept thread");
dengyihao's avatar
dengyihao 已提交
1216 1217
    uv_async_send(srv->pAcceptAsync);
    taosThreadJoin(srv->thread, NULL);
dengyihao's avatar
dengyihao 已提交
1218
    SRV_RELEASE_UV(srv->loop);
dengyihao's avatar
dengyihao 已提交
1219

dengyihao's avatar
dengyihao 已提交
1220 1221 1222 1223
    for (int i = 0; i < srv->numOfThreads; i++) {
      sendQuitToWorkThrd(srv->pThreadObj[i]);
      destroyWorkThrd(srv->pThreadObj[i]);
    }
dengyihao's avatar
dengyihao 已提交
1224 1225
  } else {
    uv_loop_close(srv->loop);
dengyihao's avatar
dengyihao 已提交
1226 1227
  }

wafwerar's avatar
wafwerar 已提交
1228 1229 1230
  taosMemoryFree(srv->pThreadObj);
  taosMemoryFree(srv->pAcceptAsync);
  taosMemoryFree(srv->loop);
dengyihao's avatar
dengyihao 已提交
1231 1232

  for (int i = 0; i < srv->numOfThreads; i++) {
wafwerar's avatar
wafwerar 已提交
1233
    taosMemoryFree(srv->pipe[i]);
dengyihao's avatar
dengyihao 已提交
1234
  }
wafwerar's avatar
wafwerar 已提交
1235
  taosMemoryFree(srv->pipe);
dengyihao's avatar
dengyihao 已提交
1236

wafwerar's avatar
wafwerar 已提交
1237
  taosMemoryFree(srv);
dengyihao's avatar
dengyihao 已提交
1238
}
dengyihao's avatar
dengyihao 已提交
1239

dengyihao's avatar
dengyihao 已提交
1240 1241 1242 1243
void transRefSrvHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1244
  int ref = T_REF_INC((SSvrConn*)handle);
dengyihao's avatar
dengyihao 已提交
1245
  tTrace("conn %p ref count:%d", handle, ref);
dengyihao's avatar
dengyihao 已提交
1246 1247 1248 1249 1250 1251
}

void transUnrefSrvHandle(void* handle) {
  if (handle == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1252
  int ref = T_REF_DEC((SSvrConn*)handle);
dengyihao's avatar
dengyihao 已提交
1253
  tTrace("conn %p ref count:%d", handle, ref);
dengyihao's avatar
dengyihao 已提交
1254
  if (ref == 0) {
dengyihao's avatar
dengyihao 已提交
1255
    destroyConn((SSvrConn*)handle, true);
dengyihao's avatar
dengyihao 已提交
1256 1257
  }
}
dengyihao's avatar
dengyihao 已提交
1258

dengyihao's avatar
dengyihao 已提交
1259
int transReleaseSrvHandle(void* handle) {
dengyihao's avatar
dengyihao 已提交
1260 1261 1262
  SRpcHandleInfo* info = handle;
  SExHandle*      exh = info->handle;
  int64_t         refId = info->refId;
dengyihao's avatar
dengyihao 已提交
1263

dengyihao's avatar
dengyihao 已提交
1264
  ASYNC_CHECK_HANDLE(exh, refId);
dengyihao's avatar
dengyihao 已提交
1265

dengyihao's avatar
dengyihao 已提交
1266
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1267 1268
  ASYNC_ERR_JRET(pThrd);

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

dengyihao's avatar
dengyihao 已提交
1271 1272 1273
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Release;
dengyihao's avatar
dengyihao 已提交
1274

dengyihao's avatar
dengyihao 已提交
1275
  tDebug("%s conn %p start to release", transLabel(pThrd->pTransInst), exh->handle);
1276 1277 1278
  if (0 != transAsyncSend(pThrd->asyncPool, &m->q)) {
    destroySmsg(m);
  }
H
Haojun Liao 已提交
1279

dengyihao's avatar
dengyihao 已提交
1280
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1281
  return 0;
dengyihao's avatar
dengyihao 已提交
1282
_return1:
dengyihao's avatar
dengyihao 已提交
1283
  tDebug("handle %p failed to send to release handle", exh);
dengyihao's avatar
dengyihao 已提交
1284
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1285
  return -1;
dengyihao's avatar
dengyihao 已提交
1286
_return2:
dengyihao's avatar
dengyihao 已提交
1287
  tDebug("handle %p failed to send to release handle", exh);
dengyihao's avatar
dengyihao 已提交
1288
  return -1;
dengyihao's avatar
dengyihao 已提交
1289
}
1290

dengyihao's avatar
dengyihao 已提交
1291
int transSendResponse(const STransMsg* msg) {
dengyihao's avatar
dengyihao 已提交
1292 1293 1294 1295 1296
  if (msg->info.noResp) {
    rpcFreeCont(msg->pCont);
    tTrace("no need send resp");
    return 0;
  }
S
Shengliang Guan 已提交
1297 1298
  SExHandle* exh = msg->info.handle;
  int64_t    refId = msg->info.refId;
dengyihao's avatar
dengyihao 已提交
1299 1300
  ASYNC_CHECK_HANDLE(exh, refId);

dengyihao's avatar
dengyihao 已提交
1301
  STransMsg tmsg = *msg;
S
Shengliang Guan 已提交
1302
  tmsg.info.refId = refId;
dengyihao's avatar
dengyihao 已提交
1303

dengyihao's avatar
dengyihao 已提交
1304
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1305
  ASYNC_ERR_JRET(pThrd);
dengyihao's avatar
dengyihao 已提交
1306

dengyihao's avatar
dengyihao 已提交
1307 1308 1309
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Normal;
dengyihao's avatar
dengyihao 已提交
1310 1311

  STraceId* trace = (STraceId*)&msg->info.traceId;
dengyihao's avatar
dengyihao 已提交
1312
  tGDebug("conn %p start to send resp (1/2)", exh->handle);
1313 1314 1315
  if (0 != transAsyncSend(pThrd->asyncPool, &m->q)) {
    destroySmsg(m);
  }
H
Haojun Liao 已提交
1316

dengyihao's avatar
dengyihao 已提交
1317
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1318
  return 0;
H
Haojun Liao 已提交
1319

dengyihao's avatar
dengyihao 已提交
1320
_return1:
dengyihao's avatar
dengyihao 已提交
1321
  tDebug("handle %p failed to send resp", exh);
dengyihao's avatar
dengyihao 已提交
1322
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1323
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1324
  return -1;
dengyihao's avatar
dengyihao 已提交
1325
_return2:
dengyihao's avatar
dengyihao 已提交
1326
  tDebug("handle %p failed to send resp", exh);
dengyihao's avatar
dengyihao 已提交
1327
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1328
  return -1;
dengyihao's avatar
dengyihao 已提交
1329
}
dengyihao's avatar
dengyihao 已提交
1330
int transRegisterMsg(const STransMsg* msg) {
S
Shengliang Guan 已提交
1331 1332
  SExHandle* exh = msg->info.handle;
  int64_t    refId = msg->info.refId;
dengyihao's avatar
dengyihao 已提交
1333 1334
  ASYNC_CHECK_HANDLE(exh, refId);

dengyihao's avatar
dengyihao 已提交
1335
  STransMsg tmsg = *msg;
dengyihao's avatar
dengyihao 已提交
1336 1337
  tmsg.info.noResp = 1;

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

dengyihao's avatar
dengyihao 已提交
1340
  SWorkThrd* pThrd = exh->pThrd;
dengyihao's avatar
dengyihao 已提交
1341
  ASYNC_ERR_JRET(pThrd);
dengyihao's avatar
dengyihao 已提交
1342

dengyihao's avatar
dengyihao 已提交
1343 1344 1345
  SSvrMsg* m = taosMemoryCalloc(1, sizeof(SSvrMsg));
  m->msg = tmsg;
  m->type = Register;
dengyihao's avatar
dengyihao 已提交
1346

dengyihao's avatar
dengyihao 已提交
1347
  STrans* pTransInst = pThrd->pTransInst;
dengyihao's avatar
dengyihao 已提交
1348
  tDebug("%s conn %p start to register brokenlink callback", transLabel(pTransInst), exh->handle);
1349 1350 1351
  if (0 != transAsyncSend(pThrd->asyncPool, &m->q)) {
    destroySmsg(m);
  }
H
Haojun Liao 已提交
1352

dengyihao's avatar
dengyihao 已提交
1353
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1354
  return 0;
dengyihao's avatar
dengyihao 已提交
1355

dengyihao's avatar
dengyihao 已提交
1356
_return1:
dengyihao's avatar
dengyihao 已提交
1357
  tDebug("handle %p failed to register brokenlink", exh);
dengyihao's avatar
dengyihao 已提交
1358
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1359
  transReleaseExHandle(transGetRefMgt(), refId);
dengyihao's avatar
dengyihao 已提交
1360
  return -1;
dengyihao's avatar
dengyihao 已提交
1361
_return2:
dengyihao's avatar
dengyihao 已提交
1362
  tDebug("handle %p failed to register brokenlink", exh);
dengyihao's avatar
dengyihao 已提交
1363
  rpcFreeCont(msg->pCont);
dengyihao's avatar
dengyihao 已提交
1364
  return -1;
dengyihao's avatar
dengyihao 已提交
1365
}
dengyihao's avatar
dengyihao 已提交
1366

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