syncIO.c 12.6 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "syncIO.h"
H
Haojun Liao 已提交
17
#include <tdatablock.h>
M
Minghao Li 已提交
18
#include "syncMessage.h"
M
Minghao Li 已提交
19
#include "syncUtil.h"
M
Minghao Li 已提交
20 21 22 23
#include "tglobal.h"
#include "ttimer.h"
#include "tutil.h"

M
Minghao Li 已提交
24 25
SSyncIO *gSyncIO = NULL;

M
Minghao Li 已提交
26
// local function ------------
M
sync io  
Minghao Li 已提交
27 28
static SSyncIO *syncIOCreate(char *host, uint16_t port);
static int32_t  syncIODestroy(SSyncIO *io);
M
Minghao Li 已提交
29 30
static int32_t  syncIOStartInternal(SSyncIO *io);
static int32_t  syncIOStopInternal(SSyncIO *io);
M
sync io  
Minghao Li 已提交
31

M
Minghao Li 已提交
32
static void   *syncIOConsumerFunc(void *param);
M
Minghao Li 已提交
33 34 35 36 37 38 39 40 41 42
static void    syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);
static void    syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);
static int32_t syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey);

static int32_t syncIOStartQ(SSyncIO *io);
static int32_t syncIOStopQ(SSyncIO *io);
static int32_t syncIOStartPing(SSyncIO *io);
static int32_t syncIOStopPing(SSyncIO *io);
static void    syncIOTickQ(void *param, void *tmrId);
static void    syncIOTickPing(void *param, void *tmrId);
M
Minghao Li 已提交
43 44
// ----------------------------

M
sync io  
Minghao Li 已提交
45 46
// public function ------------
int32_t syncIOStart(char *host, uint16_t port) {
M
Minghao Li 已提交
47
  int32_t ret = 0;
M
sync io  
Minghao Li 已提交
48
  gSyncIO = syncIOCreate(host, port);
M
Minghao Li 已提交
49 50
  assert(gSyncIO != NULL);

wafwerar's avatar
wafwerar 已提交
51
  taosSeedRand(taosGetTimestampSec());
M
Minghao Li 已提交
52
  ret = syncIOStartInternal(gSyncIO);
M
Minghao Li 已提交
53 54
  assert(ret == 0);

M
Minghao Li 已提交
55 56
  sTrace("syncIOStart ok, gSyncIO:%p", gSyncIO);
  return ret;
M
Minghao Li 已提交
57
}
M
Minghao Li 已提交
58

M
sync io  
Minghao Li 已提交
59 60 61
int32_t syncIOStop() {
  int32_t ret = syncIOStopInternal(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
62

M
sync io  
Minghao Li 已提交
63 64
  ret = syncIODestroy(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
65 66 67
  return ret;
}

M
Minghao Li 已提交
68 69 70
int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) {
  assert(pEpSet->inUse == 0);
  assert(pEpSet->numOfEps == 1);
M
Minghao Li 已提交
71

M
Minghao Li 已提交
72 73 74 75
  int32_t ret = 0;
  char    logBuf[256];
  snprintf(logBuf, sizeof(logBuf), "==syncIOSendMsg== %s:%d", pEpSet->eps[0].fqdn, pEpSet->eps[0].port);
  syncRpcMsgPrint2(logBuf, pMsg);
M
Minghao Li 已提交
76

M
Minghao Li 已提交
77 78
  pMsg->handle = NULL;
  rpcSendRequest(clientRpc, pEpSet, pMsg, NULL);
M
Minghao Li 已提交
79
  return ret;
M
Minghao Li 已提交
80 81 82
}

int32_t syncIOEqMsg(void *queue, SRpcMsg *pMsg) {
M
Minghao Li 已提交
83 84 85 86
  int32_t ret = 0;
  char    logBuf[128];
  syncRpcMsgPrint2((char *)"==syncIOEqMsg==", pMsg);

M
Minghao Li 已提交
87 88 89 90 91 92 93
  SRpcMsg *pTemp;
  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, pMsg, sizeof(SRpcMsg));

  STaosQueue *pMsgQ = queue;
  taosWriteQitem(pMsgQ, pTemp);

M
Minghao Li 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  return ret;
}

int32_t syncIOQTimerStart() {
  int32_t ret = syncIOStartQ(gSyncIO);
  assert(ret == 0);
  return ret;
}

int32_t syncIOQTimerStop() {
  int32_t ret = syncIOStopQ(gSyncIO);
  assert(ret == 0);
  return ret;
}

int32_t syncIOPingTimerStart() {
  int32_t ret = syncIOStartPing(gSyncIO);
  assert(ret == 0);
  return ret;
}

int32_t syncIOPingTimerStop() {
  int32_t ret = syncIOStopPing(gSyncIO);
  assert(ret == 0);
  return ret;
M
Minghao Li 已提交
119 120
}

M
sync io  
Minghao Li 已提交
121
// local function ------------
M
Minghao Li 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static SSyncIO *syncIOCreate(char *host, uint16_t port) {
  SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO));
  memset(io, 0, sizeof(*io));

  io->pMsgQ = taosOpenQueue();
  io->pQset = taosOpenQset();
  taosAddIntoQset(io->pQset, io->pMsgQ, NULL);

  io->myAddr.inUse = 0;
  io->myAddr.numOfEps = 0;
  addEpIntoEpSet(&io->myAddr, host, port);

  io->qTimerMS = TICK_Q_TIMER_MS;
  io->pingTimerMS = TICK_Ping_TIMER_MS;

  return io;
}

static int32_t syncIODestroy(SSyncIO *io) {
  int32_t ret = 0;
  int8_t  start = atomic_load_8(&io->isStart);
  assert(start == 0);

  if (io->serverRpc != NULL) {
    rpcClose(io->serverRpc);
    io->serverRpc = NULL;
  }

  if (io->clientRpc != NULL) {
    rpcClose(io->clientRpc);
    io->clientRpc = NULL;
  }

  taosCloseQueue(io->pMsgQ);
  taosCloseQset(io->pQset);

  return ret;
}

M
sync io  
Minghao Li 已提交
161
static int32_t syncIOStartInternal(SSyncIO *io) {
M
Minghao Li 已提交
162
  int32_t ret = 0;
M
Minghao Li 已提交
163 164
  taosBlockSIGPIPE();

M
sync io  
Minghao Li 已提交
165
  rpcInit();
M
Minghao Li 已提交
166 167 168 169 170 171 172 173 174
  tsRpcForceTcp = 1;

  // cient rpc init
  {
    SRpcInit rpcInit;
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localPort = 0;
    rpcInit.label = "SYNC-IO-CLIENT";
    rpcInit.numOfThreads = 1;
M
sync io  
Minghao Li 已提交
175
    rpcInit.cfp = syncIOProcessReply;
M
Minghao Li 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    rpcInit.sessions = 100;
    rpcInit.idleTime = 100;
    rpcInit.user = "sync-io";
    rpcInit.secret = "sync-io";
    rpcInit.ckey = "key";
    rpcInit.spi = 0;
    rpcInit.connType = TAOS_CONN_CLIENT;

    io->clientRpc = rpcOpen(&rpcInit);
    if (io->clientRpc == NULL) {
      sError("failed to initialize RPC");
      return -1;
    }
  }

  // server rpc init
  {
    SRpcInit rpcInit;
    memset(&rpcInit, 0, sizeof(rpcInit));
M
sync io  
Minghao Li 已提交
195
    rpcInit.localPort = io->myAddr.eps[0].port;
M
Minghao Li 已提交
196 197
    rpcInit.label = "SYNC-IO-SERVER";
    rpcInit.numOfThreads = 1;
M
sync io  
Minghao Li 已提交
198
    rpcInit.cfp = syncIOProcessRequest;
M
Minghao Li 已提交
199 200
    rpcInit.sessions = 1000;
    rpcInit.idleTime = 2 * 1500;
M
Minghao Li 已提交
201
    rpcInit.afp = syncIOAuth;
M
Minghao Li 已提交
202 203 204 205 206 207 208 209 210 211 212 213
    rpcInit.parent = io;
    rpcInit.connType = TAOS_CONN_SERVER;

    void *pRpc = rpcOpen(&rpcInit);
    if (pRpc == NULL) {
      sError("failed to start RPC server");
      return -1;
    }
  }

  // start consumer thread
  {
wafwerar's avatar
wafwerar 已提交
214
    if (taosThreadCreate(&io->consumerTid, NULL, syncIOConsumerFunc, io) != 0) {
M
Minghao Li 已提交
215 216 217 218 219 220 221
      sError("failed to create sync consumer thread since %s", strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
  }

  // start tmr thread
M
Minghao Li 已提交
222
  io->timerMgr = taosTmrInit(1000, 50, 10000, "SYNC-IO");
M
Minghao Li 已提交
223

M
Minghao Li 已提交
224 225
  atomic_store_8(&io->isStart, 1);
  return ret;
M
Minghao Li 已提交
226 227
}

M
sync io  
Minghao Li 已提交
228
static int32_t syncIOStopInternal(SSyncIO *io) {
M
Minghao Li 已提交
229
  int32_t ret = 0;
M
Minghao Li 已提交
230
  atomic_store_8(&io->isStart, 0);
wafwerar's avatar
wafwerar 已提交
231
  taosThreadJoin(io->consumerTid, NULL);
M
Minghao Li 已提交
232 233
  taosTmrCleanUp(io->timerMgr);
  return ret;
M
sync io  
Minghao Li 已提交
234 235 236
}

static void *syncIOConsumerFunc(void *param) {
M
Minghao Li 已提交
237
  SSyncIO   *io = param;
M
sync io  
Minghao Li 已提交
238
  STaosQall *qall;
M
Minghao Li 已提交
239
  SRpcMsg   *pRpcMsg, rpcMsg;
M
sync io  
Minghao Li 已提交
240 241 242 243 244
  qall = taosAllocateQall();

  while (1) {
    int numOfMsgs = taosReadAllQitemsFromQset(io->pQset, qall, NULL, NULL);
    sTrace("syncIOConsumerFunc %d msgs are received", numOfMsgs);
M
Minghao Li 已提交
245 246 247
    if (numOfMsgs <= 0) {
      break;
    }
M
sync io  
Minghao Li 已提交
248 249 250

    for (int i = 0; i < numOfMsgs; ++i) {
      taosGetQitem(qall, (void **)&pRpcMsg);
M
Minghao Li 已提交
251
      syncRpcMsgLog2((char *)"==syncIOConsumerFunc==", pRpcMsg);
M
Minghao Li 已提交
252

M
Minghao Li 已提交
253
      // use switch case instead of if else
M
sync io  
Minghao Li 已提交
254 255
      if (pRpcMsg->msgType == SYNC_PING) {
        if (io->FpOnSyncPing != NULL) {
M
Minghao Li 已提交
256 257 258 259
          SyncPing *pSyncMsg = syncPingFromRpcMsg2(pRpcMsg);
          assert(pSyncMsg != NULL);
          io->FpOnSyncPing(io->pSyncNode, pSyncMsg);
          syncPingDestroy(pSyncMsg);
M
sync io  
Minghao Li 已提交
260
        }
M
Minghao Li 已提交
261

M
sync io  
Minghao Li 已提交
262
      } else if (pRpcMsg->msgType == SYNC_PING_REPLY) {
M
Minghao Li 已提交
263
        if (io->FpOnSyncPingReply != NULL) {
M
Minghao Li 已提交
264
          SyncPingReply *pSyncMsg = syncPingReplyFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
265
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
266
          io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg);
M
Minghao Li 已提交
267
          syncPingReplyDestroy(pSyncMsg);
M
Minghao Li 已提交
268
        }
M
Minghao Li 已提交
269

M
Minghao Li 已提交
270 271 272
      } else if (pRpcMsg->msgType == SYNC_CLIENT_REQUEST) {
        if (io->FpOnSyncClientRequest != NULL) {
          SyncClientRequest *pSyncMsg = syncClientRequestFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
273
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
274 275 276 277
          io->FpOnSyncClientRequest(io->pSyncNode, pSyncMsg);
          syncClientRequestDestroy(pSyncMsg);
        }

M
Minghao Li 已提交
278
      } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE) {
M
Minghao Li 已提交
279
        if (io->FpOnSyncRequestVote != NULL) {
M
Minghao Li 已提交
280
          SyncRequestVote *pSyncMsg = syncRequestVoteFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
281
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
282 283 284 285 286
          io->FpOnSyncRequestVote(io->pSyncNode, pSyncMsg);
          syncRequestVoteDestroy(pSyncMsg);
        }

      } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE_REPLY) {
M
Minghao Li 已提交
287
        if (io->FpOnSyncRequestVoteReply != NULL) {
M
Minghao Li 已提交
288
          SyncRequestVoteReply *pSyncMsg = syncRequestVoteReplyFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
289
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
290 291 292 293 294
          io->FpOnSyncRequestVoteReply(io->pSyncNode, pSyncMsg);
          syncRequestVoteReplyDestroy(pSyncMsg);
        }

      } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES) {
M
Minghao Li 已提交
295
        if (io->FpOnSyncAppendEntries != NULL) {
M
Minghao Li 已提交
296
          SyncAppendEntries *pSyncMsg = syncAppendEntriesFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
297
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
298 299 300 301 302
          io->FpOnSyncAppendEntries(io->pSyncNode, pSyncMsg);
          syncAppendEntriesDestroy(pSyncMsg);
        }

      } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES_REPLY) {
M
Minghao Li 已提交
303
        if (io->FpOnSyncAppendEntriesReply != NULL) {
M
Minghao Li 已提交
304
          SyncAppendEntriesReply *pSyncMsg = syncAppendEntriesReplyFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
305
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
306 307 308 309
          io->FpOnSyncAppendEntriesReply(io->pSyncNode, pSyncMsg);
          syncAppendEntriesReplyDestroy(pSyncMsg);
        }

M
Minghao Li 已提交
310
      } else if (pRpcMsg->msgType == SYNC_TIMEOUT) {
M
Minghao Li 已提交
311
        if (io->FpOnSyncTimeout != NULL) {
M
Minghao Li 已提交
312
          SyncTimeout *pSyncMsg = syncTimeoutFromRpcMsg2(pRpcMsg);
M
Minghao Li 已提交
313
          assert(pSyncMsg != NULL);
M
Minghao Li 已提交
314 315 316 317
          io->FpOnSyncTimeout(io->pSyncNode, pSyncMsg);
          syncTimeoutDestroy(pSyncMsg);
        }
      } else {
M
Minghao Li 已提交
318
        sTrace("unknown msgType:%d, no operator", pRpcMsg->msgType);
M
sync io  
Minghao Li 已提交
319 320 321 322 323 324 325 326 327
      }
    }

    taosResetQitems(qall);
    for (int i = 0; i < numOfMsgs; ++i) {
      taosGetQitem(qall, (void **)&pRpcMsg);
      rpcFreeCont(pRpcMsg->pCont);

      if (pRpcMsg->handle != NULL) {
M
Minghao Li 已提交
328
        int msgSize = 32;
M
sync io  
Minghao Li 已提交
329
        memset(&rpcMsg, 0, sizeof(rpcMsg));
M
Minghao Li 已提交
330
        rpcMsg.msgType = SYNC_RESPONSE;
M
sync io  
Minghao Li 已提交
331 332 333 334 335 336
        rpcMsg.pCont = rpcMallocCont(msgSize);
        rpcMsg.contLen = msgSize;
        snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "give a reply");
        rpcMsg.handle = pRpcMsg->handle;
        rpcMsg.code = 0;

M
Minghao Li 已提交
337
        syncRpcMsgPrint2((char *)"syncIOConsumerFunc rpcSendResponse --> ", &rpcMsg);
M
sync io  
Minghao Li 已提交
338 339 340 341 342
        rpcSendResponse(&rpcMsg);
      }

      taosFreeQitem(pRpcMsg);
    }
M
Minghao Li 已提交
343 344
  }

M
sync io  
Minghao Li 已提交
345 346 347 348 349
  taosFreeQall(qall);
  return NULL;
}

static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) {
M
Minghao Li 已提交
350
  syncRpcMsgPrint2((char *)"==syncIOProcessRequest==", pMsg);
M
sync io  
Minghao Li 已提交
351 352 353 354 355 356 357 358
  SSyncIO *io = pParent;
  SRpcMsg *pTemp;
  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, pMsg, sizeof(SRpcMsg));
  taosWriteQitem(io->pMsgQ, pTemp);
}

static void syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) {
M
Minghao Li 已提交
359 360 361 362 363
  if (pMsg->msgType == SYNC_RESPONSE) {
    sTrace("==syncIOProcessReply==");
  } else {
    syncRpcMsgPrint2((char *)"==syncIOProcessReply==", pMsg);
  }
M
sync io  
Minghao Li 已提交
364 365 366
  rpcFreeCont(pMsg->pCont);
}

M
Minghao Li 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static int32_t syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey) {
  // app shall retrieve the auth info based on meterID from DB or a data file
  // demo code here only for simple demo
  int32_t ret = 0;
  return ret;
}

static int32_t syncIOStartQ(SSyncIO *io) {
  int32_t ret = 0;
  taosTmrReset(syncIOTickQ, io->qTimerMS, io, io->timerMgr, &io->qTimer);
  return ret;
}

static int32_t syncIOStopQ(SSyncIO *io) {
  int32_t ret = 0;
  taosTmrStop(io->qTimer);
  io->qTimer = NULL;
  return ret;
}

static int32_t syncIOStartPing(SSyncIO *io) {
  int32_t ret = 0;
  taosTmrReset(syncIOTickPing, io->pingTimerMS, io, io->timerMgr, &io->pingTimer);
  return ret;
}

static int32_t syncIOStopPing(SSyncIO *io) {
  int32_t ret = 0;
  taosTmrStop(io->pingTimer);
  io->pingTimer = NULL;
  return ret;
M
Minghao Li 已提交
398
}
M
sync io  
Minghao Li 已提交
399

M
Minghao Li 已提交
400
static void syncIOTickQ(void *param, void *tmrId) {
M
sync io  
Minghao Li 已提交
401 402
  SSyncIO *io = (SSyncIO *)param;

M
Minghao Li 已提交
403 404 405 406 407 408
  SRaftId srcId, destId;
  srcId.addr = syncUtilAddr2U64(io->myAddr.eps[0].fqdn, io->myAddr.eps[0].port);
  srcId.vgId = -1;
  destId.addr = syncUtilAddr2U64(io->myAddr.eps[0].fqdn, io->myAddr.eps[0].port);
  destId.vgId = -1;
  SyncPingReply *pMsg = syncPingReplyBuild2(&srcId, &destId, "syncIOTickQ");
M
sync io  
Minghao Li 已提交
409

M
Minghao Li 已提交
410 411
  SRpcMsg rpcMsg;
  syncPingReply2RpcMsg(pMsg, &rpcMsg);
M
sync io  
Minghao Li 已提交
412 413 414
  SRpcMsg *pTemp;
  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, &rpcMsg, sizeof(SRpcMsg));
M
Minghao Li 已提交
415
  syncRpcMsgPrint2((char *)"==syncIOTickQ==", &rpcMsg);
M
sync io  
Minghao Li 已提交
416
  taosWriteQitem(io->pMsgQ, pTemp);
M
Minghao Li 已提交
417 418 419
  syncPingReplyDestroy(pMsg);

  taosTmrReset(syncIOTickQ, io->qTimerMS, io, io->timerMgr, &io->qTimer);
M
sync io  
Minghao Li 已提交
420 421
}

M
Minghao Li 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
static void syncIOTickPing(void *param, void *tmrId) {
  SSyncIO *io = (SSyncIO *)param;

  SRaftId srcId, destId;
  srcId.addr = syncUtilAddr2U64(io->myAddr.eps[0].fqdn, io->myAddr.eps[0].port);
  srcId.vgId = -1;
  destId.addr = syncUtilAddr2U64(io->myAddr.eps[0].fqdn, io->myAddr.eps[0].port);
  destId.vgId = -1;
  SyncPing *pMsg = syncPingBuild2(&srcId, &destId, "syncIOTickPing");
  // SyncPing *pMsg = syncPingBuild3(&srcId, &destId);

  SRpcMsg rpcMsg;
  syncPing2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"==syncIOTickPing==", &rpcMsg);
  rpcSendRequest(io->clientRpc, &io->myAddr, &rpcMsg, NULL);
  syncPingDestroy(pMsg);

  taosTmrReset(syncIOTickPing, io->pingTimerMS, io, io->timerMgr, &io->pingTimer);
M
sync io  
Minghao Li 已提交
440
}