rpcTcp.c 19.4 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

dengyihao's avatar
dengyihao 已提交
16 17
#include "rpcTcp.h"
#include <uv.h>
S
slguan 已提交
18
#include "os.h"
dengyihao's avatar
dengyihao 已提交
19 20
#include "rpcHead.h"
#include "rpcLog.h"
21
#include "taosdef.h"
F
freemine 已提交
22
#include "taoserror.h"
dengyihao's avatar
dengyihao 已提交
23
#include "tutil.h"
H
hzcheng 已提交
24

dengyihao's avatar
dengyihao 已提交
25 26 27
#ifdef USE_UV

#else
J
Jeff Tao 已提交
28
typedef struct SFdObj {
dengyihao's avatar
dengyihao 已提交
29 30 31
  void *             signature;
  SOCKET             fd;       // TCP socket FD
  void *             thandle;  // handle from upper layer, like TAOS
J
Jeff Tao 已提交
32 33
  uint32_t           ip;
  uint16_t           port;
dengyihao's avatar
dengyihao 已提交
34
  int16_t            closedByApp;  // 1: already closed by App
J
Jeff Tao 已提交
35
  struct SThreadObj *pThreadObj;
dengyihao's avatar
dengyihao 已提交
36 37
  struct SFdObj *    prev;
  struct SFdObj *    next;
H
hzcheng 已提交
38 39
} SFdObj;

J
Jeff Tao 已提交
40
typedef struct SThreadObj {
H
hzcheng 已提交
41 42
  pthread_t       thread;
  SFdObj *        pHead;
J
Jeff Tao 已提交
43
  pthread_mutex_t mutex;
J
jtao1735 已提交
44
  uint32_t        ip;
45
  bool            stop;
46
  EpollFd         pollFd;
H
hzcheng 已提交
47 48
  int             numOfFds;
  int             threadId;
49
  char            label[TSDB_LABEL_LEN];
dengyihao's avatar
dengyihao 已提交
50 51
  void *          shandle;  // handle passed by upper layer during server initialization
  void *(*processData)(SRecvInfo *pPacket);
H
hzcheng 已提交
52 53
} SThreadObj;

Y
yihaoDeng 已提交
54
typedef struct {
dengyihao's avatar
dengyihao 已提交
55 56 57
  char         label[TSDB_LABEL_LEN];
  int32_t      index;
  int          numOfThreads;
Y
yihaoDeng 已提交
58 59 60
  SThreadObj **pThreadObj;
} SClientObj;

H
hzcheng 已提交
61
typedef struct {
dengyihao's avatar
dengyihao 已提交
62 63 64 65 66 67 68 69
  SOCKET       fd;
  uint32_t     ip;
  uint16_t     port;
  int8_t       stop;
  int8_t       reserve;
  char         label[TSDB_LABEL_LEN];
  int          numOfThreads;
  void *       shandle;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
70
  SThreadObj **pThreadObj;
dengyihao's avatar
dengyihao 已提交
71
  pthread_t    thread;
H
hzcheng 已提交
72 73
} SServerObj;

dengyihao's avatar
dengyihao 已提交
74
static void *  taosProcessTcpData(void *param);
S
TD-1057  
Shengliang Guan 已提交
75
static SFdObj *taosMallocFdObj(SThreadObj *pThreadObj, SOCKET fd);
J
Jeff Tao 已提交
76 77
static void    taosFreeFdObj(SFdObj *pFdObj);
static void    taosReportBrokenLink(SFdObj *pFdObj);
dengyihao's avatar
dengyihao 已提交
78
static void *  taosAcceptTcpConnection(void *arg);
H
hzcheng 已提交
79

J
jtao1735 已提交
80
void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThreads, void *fp, void *shandle) {
J
Jeff Tao 已提交
81 82
  SServerObj *pServerObj;
  SThreadObj *pThreadObj;
H
hzcheng 已提交
83

J
Jeff Tao 已提交
84
  pServerObj = (SServerObj *)calloc(sizeof(SServerObj), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
85 86
  if (pServerObj == NULL) {
    tError("TCP:%s no enough memory", label);
F
freemine 已提交
87
    terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
88 89 90
    return NULL;
  }

91
  pServerObj->fd = -1;
92
  taosResetPthread(&pServerObj->thread);
J
jtao1735 已提交
93
  pServerObj->ip = ip;
94
  pServerObj->port = port;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
95
  tstrncpy(pServerObj->label, label, sizeof(pServerObj->label));
96 97
  pServerObj->numOfThreads = numOfThreads;

陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
98
  pServerObj->pThreadObj = (SThreadObj **)calloc(sizeof(SThreadObj *), numOfThreads);
99 100
  if (pServerObj->pThreadObj == NULL) {
    tError("TCP:%s no enough memory", label);
F
freemine 已提交
101
    terrno = TAOS_SYSTEM_ERROR(errno);
J
Jeff Tao 已提交
102
    free(pServerObj);
103
    return NULL;
H
hzcheng 已提交
104
  }
J
Jeff Tao 已提交
105

dengyihao's avatar
dengyihao 已提交
106
  int            code = 0;
107 108 109 110
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);

F
freemine 已提交
111
  // initialize parameters in case it may encounter error later
J
Jeff Tao 已提交
112
  for (int i = 0; i < numOfThreads; ++i) {
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
113 114 115
    pThreadObj = (SThreadObj *)calloc(sizeof(SThreadObj), 1);
    if (pThreadObj == NULL) {
      tError("TCP:%s no enough memory", label);
F
freemine 已提交
116
      terrno = TAOS_SYSTEM_ERROR(errno);
dengyihao's avatar
dengyihao 已提交
117
      for (int j = 0; j < i; ++j) free(pServerObj->pThreadObj[j]);
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
118 119 120 121
      free(pServerObj->pThreadObj);
      free(pServerObj);
      return NULL;
    }
F
freemine 已提交
122

陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
123
    pServerObj->pThreadObj[i] = pThreadObj;
124
    pThreadObj->pollFd = -1;
125
    taosResetPthread(&pThreadObj->thread);
126
    pThreadObj->processData = fp;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
127
    tstrncpy(pThreadObj->label, label, sizeof(pThreadObj->label));
128
    pThreadObj->shandle = shandle;
Y
yihaoDeng 已提交
129
    pThreadObj->stop = false;
130
  }
H
hzcheng 已提交
131

132 133
  // initialize mutex, thread, fd which may fail
  for (int i = 0; i < numOfThreads; ++i) {
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
134
    pThreadObj = pServerObj->pThreadObj[i];
J
Jeff Tao 已提交
135
    code = pthread_mutex_init(&(pThreadObj->mutex), NULL);
J
Jeff Tao 已提交
136 137
    if (code < 0) {
      tError("%s failed to init TCP process data mutex(%s)", label, strerror(errno));
138
      break;
139
    }
H
hzcheng 已提交
140

141
    pThreadObj->pollFd = (EpollFd)epoll_create(10);  // size does not matter
142 143
    if (pThreadObj->pollFd < 0) {
      tError("%s failed to create TCP epoll", label);
J
Jeff Tao 已提交
144 145
      code = -1;
      break;
146
    }
H
hzcheng 已提交
147

J
Jeff Tao 已提交
148
    code = pthread_create(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj));
J
Jeff Tao 已提交
149 150 151
    if (code != 0) {
      tError("%s failed to create TCP process data thread(%s)", label, strerror(errno));
      break;
152
    }
H
hzcheng 已提交
153

154
    pThreadObj->threadId = i;
H
hzcheng 已提交
155 156
  }

157
  pServerObj->fd = taosOpenTcpServerSocket(pServerObj->ip, pServerObj->port);
F
freemine 已提交
158
  if (pServerObj->fd < 0) code = -1;
159

F
freemine 已提交
160
  if (code == 0) {
161
    code = pthread_create(&pServerObj->thread, &thattr, taosAcceptTcpConnection, (void *)pServerObj);
J
Jeff Tao 已提交
162
    if (code != 0) {
S
TD-2805  
Shengliang Guan 已提交
163
      tError("%s failed to create TCP accept thread(%s)", label, strerror(code));
J
Jeff Tao 已提交
164
    }
H
hzcheng 已提交
165 166
  }

J
Jeff Tao 已提交
167
  if (code != 0) {
F
freemine 已提交
168
    terrno = TAOS_SYSTEM_ERROR(errno);
169
    taosCleanUpTcpServer(pServerObj);
J
Jeff Tao 已提交
170 171
    pServerObj = NULL;
  } else {
172
    tDebug("%s TCP server is initialized, ip:0x%x port:%hu numOfThreads:%d", label, ip, port, numOfThreads);
J
Jeff Tao 已提交
173
  }
H
hzcheng 已提交
174

175
  pthread_attr_destroy(&thattr);
176
  return (void *)pServerObj;
H
hzcheng 已提交
177 178
}

dengyihao's avatar
dengyihao 已提交
179 180 181 182
static void taosStopTcpThread(SThreadObj *pThreadObj) {
  if (pThreadObj == NULL) {
    return;
  }
F
freemine 已提交
183 184
  // save thread into local variable and signal thread to stop
  pthread_t thread = pThreadObj->thread;
185 186 187
  if (!taosCheckPthreadValid(thread)) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
188
  pThreadObj->stop = true;
dengyihao's avatar
dengyihao 已提交
189
  if (taosComparePthread(thread, pthread_self())) {
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
190 191 192
    pthread_detach(pthread_self());
    return;
  }
dengyihao's avatar
dengyihao 已提交
193
  pthread_join(thread, NULL);
194 195
}

196
void taosStopTcpServer(void *handle) {
197
  SServerObj *pServerObj = handle;
H
hzcheng 已提交
198 199

  if (pServerObj == NULL) return;
S
TD-1207  
Shengliang Guan 已提交
200
  pServerObj->stop = 1;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
201

S
TD-1207  
Shengliang Guan 已提交
202
  if (pServerObj->fd >= 0) {
dengyihao's avatar
dengyihao 已提交
203
    taosShutDownSocketRD(pServerObj->fd);
S
TD-1207  
Shengliang Guan 已提交
204
  }
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
205
  if (taosCheckPthreadValid(pServerObj->thread)) {
206
    if (taosComparePthread(pServerObj->thread, pthread_self())) {
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
207 208 209 210 211
      pthread_detach(pthread_self());
    } else {
      pthread_join(pServerObj->thread, NULL);
    }
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
212

213
  tDebug("%s TCP server is stopped", pServerObj->label);
214 215 216 217 218 219
}

void taosCleanUpTcpServer(void *handle) {
  SServerObj *pServerObj = handle;
  SThreadObj *pThreadObj;
  if (pServerObj == NULL) return;
H
hzcheng 已提交
220

J
Jeff Tao 已提交
221
  for (int i = 0; i < pServerObj->numOfThreads; ++i) {
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
222
    pThreadObj = pServerObj->pThreadObj[i];
223
    taosStopTcpThread(pThreadObj);
H
hzcheng 已提交
224 225
  }

226
  tDebug("%s TCP server is cleaned up", pServerObj->label);
H
hzcheng 已提交
227

S
TD-1848  
Shengliang Guan 已提交
228 229
  tfree(pServerObj->pThreadObj);
  tfree(pServerObj);
H
hzcheng 已提交
230 231
}

232
static void *taosAcceptTcpConnection(void *arg) {
S
TD-1057  
Shengliang Guan 已提交
233
  SOCKET             connFd = -1;
J
Jeff Tao 已提交
234 235
  struct sockaddr_in caddr;
  int                threadId = 0;
dengyihao's avatar
dengyihao 已提交
236 237
  SThreadObj *       pThreadObj;
  SServerObj *       pServerObj;
J
Jeff Tao 已提交
238 239

  pServerObj = (SServerObj *)arg;
240
  tDebug("%s TCP server is ready, ip:0x%x:%hu", pServerObj->label, pServerObj->ip, pServerObj->port);
241
  setThreadName("acceptTcpConn");
J
Jeff Tao 已提交
242 243 244

  while (1) {
    socklen_t addrlen = sizeof(caddr);
245
    connFd = accept(pServerObj->fd, (struct sockaddr *)&caddr, &addrlen);
S
TD-1207  
Shengliang Guan 已提交
246 247 248 249 250
    if (pServerObj->stop) {
      tDebug("%s TCP server stop accepting new connections", pServerObj->label);
      break;
    }

251 252
    if (connFd == -1) {
      if (errno == EINVAL) {
253
        tDebug("%s TCP server stop accepting new connections, exiting", pServerObj->label);
254 255
        break;
      }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
256

257
      tError("%s TCP accept failure(%s)", pServerObj->label, strerror(errno));
J
Jeff Tao 已提交
258 259 260 261
      continue;
    }

    taosKeepTcpAlive(connFd);
dengyihao's avatar
dengyihao 已提交
262 263
    struct timeval to = {5, 0};
    int32_t        ret = taosSetSockOpt(connFd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
Y
yihaoDeng 已提交
264 265
    if (ret != 0) {
      taosCloseSocket(connFd);
266 267
      tError("%s failed to set recv timeout fd(%s)for connection from:%s:%hu", pServerObj->label, strerror(errno),
             taosInetNtoa(caddr.sin_addr), htons(caddr.sin_port));
Y
yihaoDeng 已提交
268 269
      continue;
    }
F
freemine 已提交
270

J
Jeff Tao 已提交
271
    // pick up the thread to handle this connection
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
272
    pThreadObj = pServerObj->pThreadObj[threadId];
J
Jeff Tao 已提交
273 274 275 276

    SFdObj *pFdObj = taosMallocFdObj(pThreadObj, connFd);
    if (pFdObj) {
      pFdObj->ip = caddr.sin_addr.s_addr;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
277
      pFdObj->port = htons(caddr.sin_port);
278
      tDebug("%s new TCP connection from %s:%hu, fd:%d FD:%p numOfFds:%d", pServerObj->label,
dengyihao's avatar
dengyihao 已提交
279
             taosInetNtoa(caddr.sin_addr), pFdObj->port, connFd, pFdObj, pThreadObj->numOfFds);
J
Jeff Tao 已提交
280
    } else {
S
TD-1057  
Shengliang Guan 已提交
281
      taosCloseSocket(connFd);
282 283
      tError("%s failed to malloc FdObj(%s) for connection from:%s:%hu", pServerObj->label, strerror(errno),
             taosInetNtoa(caddr.sin_addr), htons(caddr.sin_port));
F
freemine 已提交
284
    }
J
Jeff Tao 已提交
285 286 287 288 289

    // pick up next thread for next connection
    threadId++;
    threadId = threadId % pServerObj->numOfThreads;
  }
290

S
TD-1057  
Shengliang Guan 已提交
291
  taosCloseSocket(pServerObj->fd);
292
  return NULL;
J
Jeff Tao 已提交
293 294
}

Y
yihaoDeng 已提交
295 296 297 298
void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int numOfThreads, void *fp, void *shandle) {
  SClientObj *pClientObj = (SClientObj *)calloc(1, sizeof(SClientObj));
  if (pClientObj == NULL) {
    tError("TCP:%s no enough memory", label);
F
freemine 已提交
299
    terrno = TAOS_SYSTEM_ERROR(errno);
J
Jeff Tao 已提交
300
    return NULL;
F
freemine 已提交
301
  }
J
Jeff Tao 已提交
302

Y
yihaoDeng 已提交
303 304
  tstrncpy(pClientObj->label, label, sizeof(pClientObj->label));
  pClientObj->numOfThreads = numOfThreads;
dengyihao's avatar
dengyihao 已提交
305
  pClientObj->pThreadObj = (SThreadObj **)calloc(numOfThreads, sizeof(SThreadObj *));
Y
yihaoDeng 已提交
306 307 308
  if (pClientObj->pThreadObj == NULL) {
    tError("TCP:%s no enough memory", label);
    tfree(pClientObj);
F
freemine 已提交
309
    terrno = TAOS_SYSTEM_ERROR(errno);
J
Jeff Tao 已提交
310 311
  }

dengyihao's avatar
dengyihao 已提交
312
  int            code = 0;
Y
yihaoDeng 已提交
313
  pthread_attr_t thattr;
J
Jeff Tao 已提交
314 315
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
Y
yihaoDeng 已提交
316 317 318 319 320

  for (int i = 0; i < numOfThreads; ++i) {
    SThreadObj *pThreadObj = (SThreadObj *)calloc(1, sizeof(SThreadObj));
    if (pThreadObj == NULL) {
      tError("TCP:%s no enough memory", label);
F
freemine 已提交
321
      terrno = TAOS_SYSTEM_ERROR(errno);
dengyihao's avatar
dengyihao 已提交
322
      for (int j = 0; j < i; ++j) free(pClientObj->pThreadObj[j]);
Y
yihaoDeng 已提交
323 324 325 326 327 328
      free(pClientObj);
      pthread_attr_destroy(&thattr);
      return NULL;
    }
    pClientObj->pThreadObj[i] = pThreadObj;
    taosResetPthread(&pThreadObj->thread);
dengyihao's avatar
dengyihao 已提交
329 330
    pThreadObj->ip = ip;
    pThreadObj->stop = false;
Y
yihaoDeng 已提交
331 332 333
    tstrncpy(pThreadObj->label, label, sizeof(pThreadObj->label));
    pThreadObj->shandle = shandle;
    pThreadObj->processData = fp;
J
Jeff Tao 已提交
334 335
  }

Y
yihaoDeng 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  // initialize mutex, thread, fd which may fail
  for (int i = 0; i < numOfThreads; ++i) {
    SThreadObj *pThreadObj = pClientObj->pThreadObj[i];
    code = pthread_mutex_init(&(pThreadObj->mutex), NULL);
    if (code < 0) {
      tError("%s failed to init TCP process data mutex(%s)", label, strerror(errno));
      break;
    }

    pThreadObj->pollFd = (int64_t)epoll_create(10);  // size does not matter
    if (pThreadObj->pollFd < 0) {
      tError("%s failed to create TCP epoll", label);
      code = -1;
      break;
    }
J
Jeff Tao 已提交
351

Y
yihaoDeng 已提交
352 353 354 355 356 357 358 359
    code = pthread_create(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj));
    if (code != 0) {
      tError("%s failed to create TCP process data thread(%s)", label, strerror(errno));
      break;
    }
    pThreadObj->threadId = i;
  }
  if (code != 0) {
F
freemine 已提交
360
    terrno = TAOS_SYSTEM_ERROR(errno);
Y
yihaoDeng 已提交
361 362
    taosCleanUpTcpClient(pClientObj);
    pClientObj = NULL;
F
freemine 已提交
363 364
  }
  return pClientObj;
J
Jeff Tao 已提交
365 366
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
367
void taosStopTcpClient(void *chandle) {
Y
TD-2884  
yihaoDeng 已提交
368
  SClientObj *pClientObj = chandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
369

Y
TD-2884  
yihaoDeng 已提交
370 371
  if (pClientObj == NULL) return;

dengyihao's avatar
dengyihao 已提交
372
  tDebug("%s TCP client is stopped", pClientObj->label);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
373 374
}

J
Jeff Tao 已提交
375
void taosCleanUpTcpClient(void *chandle) {
Y
yihaoDeng 已提交
376 377
  SClientObj *pClientObj = chandle;
  if (pClientObj == NULL) return;
F
freemine 已提交
378
  for (int i = 0; i < pClientObj->numOfThreads; ++i) {
dengyihao's avatar
dengyihao 已提交
379
    SThreadObj *pThreadObj = pClientObj->pThreadObj[i];
F
freemine 已提交
380
    taosStopTcpThread(pThreadObj);
Y
yihaoDeng 已提交
381
  }
F
freemine 已提交
382

Y
yihaoDeng 已提交
383 384
  tDebug("%s TCP client is cleaned up", pClientObj->label);
  tfree(pClientObj->pThreadObj);
F
freemine 已提交
385
  tfree(pClientObj);
J
Jeff Tao 已提交
386 387
}

J
jtao1735 已提交
388
void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uint16_t port) {
dengyihao's avatar
dengyihao 已提交
389 390 391
  SClientObj *pClientObj = shandle;
  int32_t     index = atomic_load_32(&pClientObj->index) % pClientObj->numOfThreads;
  atomic_store_32(&pClientObj->index, index + 1);
Y
yihaoDeng 已提交
392
  SThreadObj *pThreadObj = pClientObj->pThreadObj[index];
J
Jeff Tao 已提交
393

S
TD-1057  
Shengliang Guan 已提交
394
  SOCKET fd = taosOpenTcpClientSocket(ip, port, pThreadObj->ip);
W
fix bug  
wpan 已提交
395 396 397
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  if (fd == (SOCKET)-1) return NULL;
#else
398
  if (fd <= 0) return NULL;
W
fix bug  
wpan 已提交
399
#endif
J
Jeff Tao 已提交
400

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
401
  struct sockaddr_in sin;
dengyihao's avatar
dengyihao 已提交
402 403 404
  uint16_t           localPort = 0;
  unsigned int       addrlen = sizeof(sin);
  if (getsockname(fd, (struct sockaddr *)&sin, &addrlen) == 0 && sin.sin_family == AF_INET && addrlen == sizeof(sin)) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
405 406 407
    localPort = (uint16_t)ntohs(sin.sin_port);
  }

J
Jeff Tao 已提交
408
  SFdObj *pFdObj = taosMallocFdObj(pThreadObj, fd);
F
freemine 已提交
409

J
Jeff Tao 已提交
410 411 412
  if (pFdObj) {
    pFdObj->thandle = thandle;
    pFdObj->port = port;
J
jtao1735 已提交
413
    pFdObj->ip = ip;
dengyihao's avatar
dengyihao 已提交
414 415
    tDebug("%s %p TCP connection to 0x%x:%hu is created, localPort:%hu FD:%p numOfFds:%d", pThreadObj->label, thandle,
           ip, port, localPort, pFdObj, pThreadObj->numOfFds);
J
Jeff Tao 已提交
416 417
  } else {
    tError("%s failed to malloc client FdObj(%s)", pThreadObj->label, strerror(errno));
H
Haojun Liao 已提交
418
    taosCloseSocket(fd);
J
Jeff Tao 已提交
419 420 421 422 423 424
  }

  return pFdObj;
}

void taosCloseTcpConnection(void *chandle) {
425
  SFdObj *pFdObj = chandle;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
426
  if (pFdObj == NULL || pFdObj->signature != pFdObj) return;
427

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
428
  SThreadObj *pThreadObj = pFdObj->pThreadObj;
F
freemine 已提交
429
  tDebug("%s %p TCP connection will be closed, FD:%p", pThreadObj->label, pFdObj->thandle, pFdObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
430 431

  // pFdObj->thandle = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
432
  pFdObj->closedByApp = 1;
S
Shengliang Guan 已提交
433
  taosShutDownSocketWR(pFdObj->fd);
434 435
}

J
Jeff Tao 已提交
436
int taosSendTcpData(uint32_t ip, uint16_t port, void *data, int len, void *chandle) {
437
  SFdObj *pFdObj = chandle;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
438
  if (pFdObj == NULL || pFdObj->signature != pFdObj) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
439 440 441
  SThreadObj *pThreadObj = pFdObj->pThreadObj;

  int ret = taosWriteMsg(pFdObj->fd, data, len);
F
freemine 已提交
442
  tTrace("%s %p TCP data is sent, FD:%p fd:%d bytes:%d", pThreadObj->label, pFdObj->thandle, pFdObj, pFdObj->fd, ret);
443

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
444
  return ret;
445 446
}

447 448 449 450
static void taosReportBrokenLink(SFdObj *pFdObj) {
  SThreadObj *pThreadObj = pFdObj->pThreadObj;

  // notify the upper layer, so it will clean the associated context
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
451
  if (pFdObj->closedByApp == 0) {
S
Shengliang Guan 已提交
452
    taosShutDownSocketWR(pFdObj->fd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
453

454 455 456 457 458 459
    SRecvInfo recvInfo;
    recvInfo.msg = NULL;
    recvInfo.msgLen = 0;
    recvInfo.ip = 0;
    recvInfo.port = 0;
    recvInfo.shandle = pThreadObj->shandle;
460
    recvInfo.thandle = pFdObj->thandle;
461 462 463
    recvInfo.chandle = NULL;
    recvInfo.connType = RPC_CONN_TCP;
    (*(pThreadObj->processData))(&recvInfo);
F
freemine 已提交
464
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
465 466 467 468 469

  taosFreeFdObj(pFdObj);
}

static int taosReadTcpData(SFdObj *pFdObj, SRecvInfo *pInfo) {
dengyihao's avatar
dengyihao 已提交
470 471 472
  SRpcHead rpcHead;
  int32_t  msgLen, leftLen, retLen, headLen;
  char *   buffer, *msg;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
473 474 475 476 477

  SThreadObj *pThreadObj = pFdObj->pThreadObj;

  headLen = taosReadMsg(pFdObj->fd, &rpcHead, sizeof(SRpcHead));
  if (headLen != sizeof(SRpcHead)) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
478
    tDebug("%s %p read error, FD:%p headLen:%d", pThreadObj->label, pFdObj->thandle, pFdObj, headLen);
F
freemine 已提交
479
    return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
480 481 482
  }

  msgLen = (int32_t)htonl((uint32_t)rpcHead.msgLen);
S
TD-1762  
Shengliang Guan 已提交
483 484
  int32_t size = msgLen + tsRpcOverhead;
  buffer = malloc(size);
S
Shengliang Guan 已提交
485
  if (NULL == buffer) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
486
    tError("%s %p TCP malloc(size:%d) fail", pThreadObj->label, pFdObj->thandle, msgLen);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
487
    return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
488
  } else {
dengyihao's avatar
dengyihao 已提交
489 490
    tTrace("%s %p read data, FD:%p fd:%d TCP malloc mem:%p", pThreadObj->label, pFdObj->thandle, pFdObj, pFdObj->fd,
           buffer);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
491 492 493 494 495 496 497
  }

  msg = buffer + tsRpcOverhead;
  leftLen = msgLen - headLen;
  retLen = taosReadMsg(pFdObj->fd, msg + headLen, leftLen);

  if (leftLen != retLen) {
dengyihao's avatar
dengyihao 已提交
498
    tError("%s %p read error, leftLen:%d retLen:%d FD:%p", pThreadObj->label, pFdObj->thandle, leftLen, retLen, pFdObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
499 500
    free(buffer);
    return -1;
501
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
502 503

  memcpy(msg, &rpcHead, sizeof(SRpcHead));
F
freemine 已提交
504

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
505 506 507 508 509
  pInfo->msg = msg;
  pInfo->msgLen = msgLen;
  pInfo->ip = pFdObj->ip;
  pInfo->port = pFdObj->port;
  pInfo->shandle = pThreadObj->shandle;
510
  pInfo->thandle = pFdObj->thandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
511 512 513 514
  pInfo->chandle = pFdObj;
  pInfo->connType = RPC_CONN_TCP;

  if (pFdObj->closedByApp) {
F
freemine 已提交
515
    free(buffer);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
516 517 518 519
    return -1;
  }

  return 0;
520 521
}

J
Jeff Tao 已提交
522 523 524
#define maxEvents 10

static void *taosProcessTcpData(void *param) {
dengyihao's avatar
dengyihao 已提交
525 526
  SThreadObj *       pThreadObj = param;
  SFdObj *           pFdObj;
H
hzcheng 已提交
527
  struct epoll_event events[maxEvents];
528
  SRecvInfo          recvInfo;
529

H
Haojun Liao 已提交
530 531
  char name[16] = {0};
  snprintf(name, tListLen(name), "%s-tcp", pThreadObj->label);
532
  setThreadName(name);
F
freemine 已提交
533

H
hzcheng 已提交
534
  while (1) {
S
TD-1057  
Shengliang Guan 已提交
535
    int fdNum = epoll_wait(pThreadObj->pollFd, events, maxEvents, TAOS_EPOLL_WAIT_TIME);
536
    if (pThreadObj->stop) {
537
      tDebug("%s TCP thread get stop event, exiting...", pThreadObj->label);
538 539
      break;
    }
H
hzcheng 已提交
540 541
    if (fdNum < 0) continue;

J
Jeff Tao 已提交
542
    for (int i = 0; i < fdNum; ++i) {
H
hzcheng 已提交
543 544 545
      pFdObj = events[i].data.ptr;

      if (events[i].events & EPOLLERR) {
546
        tDebug("%s %p FD:%p epoll errors", pThreadObj->label, pFdObj->thandle, pFdObj);
547
        taosReportBrokenLink(pFdObj);
H
hzcheng 已提交
548 549 550
        continue;
      }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
551
      if (events[i].events & EPOLLRDHUP) {
552
        tDebug("%s %p FD:%p RD hang up", pThreadObj->label, pFdObj->thandle, pFdObj);
553
        taosReportBrokenLink(pFdObj);
H
hzcheng 已提交
554 555 556
        continue;
      }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
557
      if (events[i].events & EPOLLHUP) {
558
        tDebug("%s %p FD:%p hang up", pThreadObj->label, pFdObj->thandle, pFdObj);
559
        taosReportBrokenLink(pFdObj);
560 561
        continue;
      }
H
hzcheng 已提交
562

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
563
      if (taosReadTcpData(pFdObj, &recvInfo) < 0) {
F
freemine 已提交
564
        shutdown(pFdObj->fd, SHUT_WR);
H
hzcheng 已提交
565 566 567
        continue;
      }

568
      pFdObj->thandle = (*(pThreadObj->processData))(&recvInfo);
569
      if (pFdObj->thandle == NULL) taosFreeFdObj(pFdObj);
H
hzcheng 已提交
570
    }
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
571

F
freemine 已提交
572
    if (pThreadObj->stop) break;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
573 574
  }

dengyihao's avatar
dengyihao 已提交
575
  if (pThreadObj->pollFd >= 0) {
S
TD-2837  
Shengliang Guan 已提交
576
    EpollClose(pThreadObj->pollFd);
577 578
    pThreadObj->pollFd = -1;
  }
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
579 580

  while (pThreadObj->pHead) {
581
    pFdObj = pThreadObj->pHead;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
582
    pThreadObj->pHead = pFdObj->next;
陶建辉(Jeff)'s avatar
TD-1669  
陶建辉(Jeff) 已提交
583
    taosReportBrokenLink(pFdObj);
H
hzcheng 已提交
584
  }
J
Jeff Tao 已提交
585

陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
586
  pthread_mutex_destroy(&(pThreadObj->mutex));
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
587
  tDebug("%s TCP thread exits ...", pThreadObj->label);
S
TD-1848  
Shengliang Guan 已提交
588
  tfree(pThreadObj);
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
589

J
Jeff Tao 已提交
590
  return NULL;
H
hzcheng 已提交
591 592
}

S
TD-1057  
Shengliang Guan 已提交
593
static SFdObj *taosMallocFdObj(SThreadObj *pThreadObj, SOCKET fd) {
H
hzcheng 已提交
594 595
  struct epoll_event event;

J
Jeff Tao 已提交
596
  SFdObj *pFdObj = (SFdObj *)calloc(sizeof(SFdObj), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
597 598 599
  if (pFdObj == NULL) {
    return NULL;
  }
H
hzcheng 已提交
600

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
601
  pFdObj->closedByApp = 0;
J
Jeff Tao 已提交
602 603 604
  pFdObj->fd = fd;
  pFdObj->pThreadObj = pThreadObj;
  pFdObj->signature = pFdObj;
H
hzcheng 已提交
605

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
606
  event.events = EPOLLIN | EPOLLRDHUP;
J
Jeff Tao 已提交
607 608
  event.data.ptr = pFdObj;
  if (epoll_ctl(pThreadObj->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) {
S
TD-1848  
Shengliang Guan 已提交
609
    tfree(pFdObj);
F
freemine 已提交
610
    terrno = TAOS_SYSTEM_ERROR(errno);
J
Jeff Tao 已提交
611
    return NULL;
H
hzcheng 已提交
612 613
  }

J
Jeff Tao 已提交
614 615 616 617 618 619 620
  // notify the data process, add into the FdObj list
  pthread_mutex_lock(&(pThreadObj->mutex));
  pFdObj->next = pThreadObj->pHead;
  if (pThreadObj->pHead) (pThreadObj->pHead)->prev = pFdObj;
  pThreadObj->pHead = pFdObj;
  pThreadObj->numOfFds++;
  pthread_mutex_unlock(&(pThreadObj->mutex));
H
hzcheng 已提交
621

J
Jeff Tao 已提交
622
  return pFdObj;
H
hzcheng 已提交
623 624
}

J
Jeff Tao 已提交
625
static void taosFreeFdObj(SFdObj *pFdObj) {
626 627 628
  if (pFdObj == NULL) return;
  if (pFdObj->signature != pFdObj) return;

629
  SThreadObj *pThreadObj = pFdObj->pThreadObj;
J
Jeff Tao 已提交
630
  pthread_mutex_lock(&pThreadObj->mutex);
631

J
Jeff Tao 已提交
632 633 634 635 636 637
  if (pFdObj->signature == NULL) {
    pthread_mutex_unlock(&pThreadObj->mutex);
    return;
  }

  pFdObj->signature = NULL;
638
  epoll_ctl(pThreadObj->pollFd, EPOLL_CTL_DEL, pFdObj->fd, NULL);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
639
  taosCloseSocket(pFdObj->fd);
640 641 642

  pThreadObj->numOfFds--;
  if (pThreadObj->numOfFds < 0)
dengyihao's avatar
dengyihao 已提交
643 644
    tError("%s %p TCP thread:%d, number of FDs is negative!!!", pThreadObj->label, pFdObj->thandle,
           pThreadObj->threadId);
645 646 647 648 649 650 651 652 653 654 655

  if (pFdObj->prev) {
    (pFdObj->prev)->next = pFdObj->next;
  } else {
    pThreadObj->pHead = pFdObj->next;
  }

  if (pFdObj->next) {
    (pFdObj->next)->prev = pFdObj->prev;
  }

J
Jeff Tao 已提交
656
  pthread_mutex_unlock(&pThreadObj->mutex);
657

dengyihao's avatar
dengyihao 已提交
658 659
  tDebug("%s %p TCP connection is closed, FD:%p fd:%d numOfFds:%d", pThreadObj->label, pFdObj->thandle, pFdObj,
         pFdObj->fd, pThreadObj->numOfFds);
660

S
TD-1848  
Shengliang Guan 已提交
661
  tfree(pFdObj);
662
}
dengyihao's avatar
dengyihao 已提交
663 664

#endif