rpcTcp.c 19.5 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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 已提交
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
}
663
#endif