rpcTcp.c 19.3 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/>.
 */

S
slguan 已提交
16
#include "os.h"
H
hzcheng 已提交
17
#include "tutil.h"
18
#include "taosdef.h"
F
freemine 已提交
19
#include "taoserror.h"
S
slguan 已提交
20
#include "rpcLog.h"
21
#include "rpcHead.h"
J
Jeff Tao 已提交
22
#include "rpcTcp.h"
H
hzcheng 已提交
23

J
Jeff Tao 已提交
24 25
typedef struct SFdObj {
  void              *signature;
S
TD-1057  
Shengliang Guan 已提交
26
  SOCKET             fd;          // TCP socket FD
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
27
  void              *thandle;     // handle from upper layer, like TAOS
J
Jeff Tao 已提交
28 29
  uint32_t           ip;
  uint16_t           port;
30
  int16_t            closedByApp; // 1: already closed by App
J
Jeff Tao 已提交
31 32 33
  struct SThreadObj *pThreadObj;
  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];
46 47
  void           *shandle;  // handle passed by upper layer during server initialization
  void           *(*processData)(SRecvInfo *pPacket);
H
hzcheng 已提交
48 49
} SThreadObj;

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

H
hzcheng 已提交
57
typedef struct {
S
TD-1057  
Shengliang Guan 已提交
58
  SOCKET      fd;
J
jtao1735 已提交
59
  uint32_t    ip;
L
lihui 已提交
60
  uint16_t    port;
S
TD-1207  
Shengliang Guan 已提交
61 62
  int8_t      stop;
  int8_t      reserve;
63
  char        label[TSDB_LABEL_LEN];
H
hzcheng 已提交
64 65
  int         numOfThreads;
  void *      shandle;
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
66
  SThreadObj **pThreadObj;
H
hzcheng 已提交
67 68 69
  pthread_t   thread;
} SServerObj;

J
Jeff Tao 已提交
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);
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

J
Jeff Tao 已提交
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);
陶建辉(Jeff)'s avatar
TD-1645  
陶建辉(Jeff) 已提交
113 114 115 116 117
      for (int j=0; j<i; ++j) free(pServerObj->pThreadObj[j]);
      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
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

J
Jeff Tao 已提交
265 266

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
396 397 398 399 400 401 402 403
  struct sockaddr_in sin;
  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)) {
    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;
F
freemine 已提交
410
    tDebug("%s %p TCP connection to 0x%x:%hu is created, localPort:%hu FD:%p numOfFds:%d",
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
411
            pThreadObj->label, thandle, ip, port, localPort, pFdObj, pThreadObj->numOfFds);
J
Jeff Tao 已提交
412 413
  } else {
    tError("%s failed to malloc client FdObj(%s)", pThreadObj->label, strerror(errno));
H
Haojun Liao 已提交
414
    taosCloseSocket(fd);
J
Jeff Tao 已提交
415 416 417 418 419 420
  }

  return pFdObj;
}

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

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

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

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
440
  return ret;
441 442
}

443 444 445 446 447
static void taosReportBrokenLink(SFdObj *pFdObj) {

  SThreadObj *pThreadObj = pFdObj->pThreadObj;

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

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

  taosFreeFdObj(pFdObj);
}

static int taosReadTcpData(SFdObj *pFdObj, SRecvInfo *pInfo) {
  SRpcHead    rpcHead;
  int32_t     msgLen, leftLen, retLen, headLen;
  char       *buffer, *msg;

  SThreadObj *pThreadObj = pFdObj->pThreadObj;

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

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

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

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

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

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

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

  return 0;
517 518
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

J
Jeff Tao 已提交
587
  return NULL;
H
hzcheng 已提交
588 589
}

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

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

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

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

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

J
Jeff Tao 已提交
619
  return pFdObj;
H
hzcheng 已提交
620 621
}

J
Jeff Tao 已提交
622
static void taosFreeFdObj(SFdObj *pFdObj) {
623 624 625 626

  if (pFdObj == NULL) return;
  if (pFdObj->signature != pFdObj) return;

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

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

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

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

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

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

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

F
freemine 已提交
656
  tDebug("%s %p TCP connection is closed, FD:%p fd:%d numOfFds:%d",
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
657
          pThreadObj->label, pFdObj->thandle, pFdObj, pFdObj->fd, pThreadObj->numOfFds);
658

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