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
#include "rpcTcp.h"
S
slguan 已提交
17
#include "os.h"
dengyihao's avatar
dengyihao 已提交
18 19
#include "rpcHead.h"
#include "rpcLog.h"
20
#include "taosdef.h"
F
freemine 已提交
21
#include "taoserror.h"
dengyihao's avatar
dengyihao 已提交
22
#include "tutil.h"
H
hzcheng 已提交
23

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

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

Y
yihaoDeng 已提交
50
typedef struct {
dengyihao's avatar
dengyihao 已提交
51 52 53
  char         label[TSDB_LABEL_LEN];
  int32_t      index;
  int          numOfThreads;
Y
yihaoDeng 已提交
54 55 56
  SThreadObj **pThreadObj;
} SClientObj;

H
hzcheng 已提交
57
typedef struct {
dengyihao's avatar
dengyihao 已提交
58 59 60 61 62 63 64 65
  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) 已提交
66
  SThreadObj **pThreadObj;
dengyihao's avatar
dengyihao 已提交
67
  pthread_t    thread;
H
hzcheng 已提交
68 69
} SServerObj;

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

J
jtao1735 已提交
76
void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThreads, void *fp, void *shandle) {
J
Jeff Tao 已提交
77 78
  SServerObj *pServerObj;
  SThreadObj *pThreadObj;
H
hzcheng 已提交
79

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

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

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

dengyihao's avatar
dengyihao 已提交
102
  int            code = 0;
103 104 105 106
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);

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

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

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

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

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

150
    pThreadObj->threadId = i;
H
hzcheng 已提交
151 152
  }

153
  pServerObj->fd = taosOpenTcpServerSocket(pServerObj->ip, pServerObj->port);
F
freemine 已提交
154
  if (pServerObj->fd < 0) code = -1;
155

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

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

171
  pthread_attr_destroy(&thattr);
172
  return (void *)pServerObj;
H
hzcheng 已提交
173 174
}

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

192
void taosStopTcpServer(void *handle) {
193
  SServerObj *pServerObj = handle;
H
hzcheng 已提交
194 195

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

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

209
  tDebug("%s TCP server is stopped", pServerObj->label);
210 211 212 213 214 215
}

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

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

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

S
TD-1848  
Shengliang Guan 已提交
224 225
  tfree(pServerObj->pThreadObj);
  tfree(pServerObj);
H
hzcheng 已提交
226 227
}

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

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

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

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

253
      tError("%s TCP accept failure(%s)", pServerObj->label, strerror(errno));
J
Jeff Tao 已提交
254 255 256 257
      continue;
    }

    taosKeepTcpAlive(connFd);
dengyihao's avatar
dengyihao 已提交
258 259
    struct timeval to = {5, 0};
    int32_t        ret = taosSetSockOpt(connFd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
Y
yihaoDeng 已提交
260 261
    if (ret != 0) {
      taosCloseSocket(connFd);
262 263
      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 已提交
264 265
      continue;
    }
F
freemine 已提交
266

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

    SFdObj *pFdObj = taosMallocFdObj(pThreadObj, connFd);
    if (pFdObj) {
      pFdObj->ip = caddr.sin_addr.s_addr;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
273
      pFdObj->port = htons(caddr.sin_port);
274
      tDebug("%s new TCP connection from %s:%hu, fd:%d FD:%p numOfFds:%d", pServerObj->label,
dengyihao's avatar
dengyihao 已提交
275
             taosInetNtoa(caddr.sin_addr), pFdObj->port, connFd, pFdObj, pThreadObj->numOfFds);
J
Jeff Tao 已提交
276
    } else {
S
TD-1057  
Shengliang Guan 已提交
277
      taosCloseSocket(connFd);
278 279
      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 已提交
280
    }
J
Jeff Tao 已提交
281 282 283 284 285

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

S
TD-1057  
Shengliang Guan 已提交
287
  taosCloseSocket(pServerObj->fd);
288
  return NULL;
J
Jeff Tao 已提交
289 290
}

Y
yihaoDeng 已提交
291 292 293 294
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 已提交
295
    terrno = TAOS_SYSTEM_ERROR(errno);
J
Jeff Tao 已提交
296
    return NULL;
F
freemine 已提交
297
  }
J
Jeff Tao 已提交
298

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

dengyihao's avatar
dengyihao 已提交
308
  int            code = 0;
Y
yihaoDeng 已提交
309
  pthread_attr_t thattr;
J
Jeff Tao 已提交
310 311
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
Y
yihaoDeng 已提交
312 313 314 315 316

  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 已提交
317
      terrno = TAOS_SYSTEM_ERROR(errno);
dengyihao's avatar
dengyihao 已提交
318
      for (int j = 0; j < i; ++j) free(pClientObj->pThreadObj[j]);
Y
yihaoDeng 已提交
319 320 321 322 323 324
      free(pClientObj);
      pthread_attr_destroy(&thattr);
      return NULL;
    }
    pClientObj->pThreadObj[i] = pThreadObj;
    taosResetPthread(&pThreadObj->thread);
dengyihao's avatar
dengyihao 已提交
325 326
    pThreadObj->ip = ip;
    pThreadObj->stop = false;
Y
yihaoDeng 已提交
327 328 329
    tstrncpy(pThreadObj->label, label, sizeof(pThreadObj->label));
    pThreadObj->shandle = shandle;
    pThreadObj->processData = fp;
J
Jeff Tao 已提交
330 331
  }

Y
yihaoDeng 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  // 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 已提交
347

Y
yihaoDeng 已提交
348 349 350 351 352 353 354 355
    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 已提交
356
    terrno = TAOS_SYSTEM_ERROR(errno);
Y
yihaoDeng 已提交
357 358
    taosCleanUpTcpClient(pClientObj);
    pClientObj = NULL;
F
freemine 已提交
359 360
  }
  return pClientObj;
J
Jeff Tao 已提交
361 362
}

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

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

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

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

Y
yihaoDeng 已提交
379 380
  tDebug("%s TCP client is cleaned up", pClientObj->label);
  tfree(pClientObj->pThreadObj);
F
freemine 已提交
381
  tfree(pClientObj);
J
Jeff Tao 已提交
382 383
}

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
397
  struct sockaddr_in sin;
dengyihao's avatar
dengyihao 已提交
398 399 400
  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) 已提交
401 402 403
    localPort = (uint16_t)ntohs(sin.sin_port);
  }

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

J
Jeff Tao 已提交
406 407 408
  if (pFdObj) {
    pFdObj->thandle = thandle;
    pFdObj->port = port;
J
jtao1735 已提交
409
    pFdObj->ip = ip;
H
Haojun Liao 已提交
410 411 412 413 414

    char ipport[40] = {0};
    taosIpPort2String(ip, port, ipport);
    tDebug("%s %p TCP connection to %s is created, localPort:%hu FD:%p numOfFds:%d", pThreadObj->label, thandle,
           ipport, localPort, pFdObj, pThreadObj->numOfFds);
J
Jeff Tao 已提交
415 416
  } else {
    tError("%s failed to malloc client FdObj(%s)", pThreadObj->label, strerror(errno));
H
Haojun Liao 已提交
417
    taosCloseSocket(fd);
J
Jeff Tao 已提交
418 419 420 421 422 423
  }

  return pFdObj;
}

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

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

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

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

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

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

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

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

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

  taosFreeFdObj(pFdObj);
}

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

  SThreadObj *pThreadObj = pFdObj->pThreadObj;

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

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

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

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

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

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

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

  return 0;
519 520
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

J
Jeff Tao 已提交
613 614 615 616 617 618 619
  // 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 已提交
620

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

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

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

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

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

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

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

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

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

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

S
TD-1848  
Shengliang Guan 已提交
660
  tfree(pFdObj);
661
}