syncIO.c 10.1 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 19 20 21 22
#include "syncOnMessage.h"
#include "tglobal.h"
#include "ttimer.h"
#include "tutil.h"

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

M
Minghao Li 已提交
25
// local function ------------
M
sync io  
Minghao Li 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39
static int32_t  syncIOStartInternal(SSyncIO *io);
static int32_t  syncIOStopInternal(SSyncIO *io);
static SSyncIO *syncIOCreate(char *host, uint16_t port);
static int32_t  syncIODestroy(SSyncIO *io);

static void *syncIOConsumerFunc(void *param);
static int   syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey);
static void  syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);
static void  syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);

static int32_t syncIOTickQInternal(SSyncIO *io);
static void    syncIOTickQFunc(void *param, void *tmrId);
static int32_t syncIOTickPingInternal(SSyncIO *io);
static void    syncIOTickPingFunc(void *param, void *tmrId);
M
Minghao Li 已提交
40 41
// ----------------------------

M
sync io  
Minghao Li 已提交
42 43 44
// public function ------------
int32_t syncIOStart(char *host, uint16_t port) {
  gSyncIO = syncIOCreate(host, port);
M
Minghao Li 已提交
45 46
  assert(gSyncIO != NULL);

M
sync io  
Minghao Li 已提交
47
  int32_t ret = syncIOStartInternal(gSyncIO);
M
Minghao Li 已提交
48 49
  assert(ret == 0);

M
Minghao Li 已提交
50
  sTrace("syncIOStart ok, gSyncIO:%p gSyncIO->clientRpc:%p", gSyncIO, gSyncIO->clientRpc);
M
Minghao Li 已提交
51 52
  return 0;
}
M
Minghao Li 已提交
53

M
sync io  
Minghao Li 已提交
54 55 56
int32_t syncIOStop() {
  int32_t ret = syncIOStopInternal(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
57

M
sync io  
Minghao Li 已提交
58 59
  ret = syncIODestroy(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
60 61 62
  return ret;
}

M
sync io  
Minghao Li 已提交
63 64 65 66
int32_t syncIOTickQ() {
  int32_t ret = syncIOTickQInternal(gSyncIO);
  assert(ret == 0);
  return ret;
M
Minghao Li 已提交
67 68
}

M
sync io  
Minghao Li 已提交
69 70 71 72
int32_t syncIOTickPing() {
  int32_t ret = syncIOTickPingInternal(gSyncIO);
  assert(ret == 0);
  return ret;
M
Minghao Li 已提交
73 74
}

M
Minghao Li 已提交
75 76 77 78 79 80 81 82
int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) {
  sTrace(
      "<--- syncIOSendMsg ---> clientRpc:%p, numOfEps:%d, inUse:%d, destAddr:%s-%u, pMsg->ahandle:%p, pMsg->handle:%p, "
      "pMsg->msgType:%d, pMsg->contLen:%d",
      clientRpc, pEpSet->numOfEps, pEpSet->inUse, pEpSet->eps[0].fqdn, pEpSet->eps[0].port, pMsg->ahandle, pMsg->handle,
      pMsg->msgType, pMsg->contLen);
  {
    cJSON *pJson = syncRpcMsg2Json(pMsg);
M
Minghao Li 已提交
83
    char  *serialized = cJSON_Print(pJson);
M
Minghao Li 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    sTrace("process syncMessage send: pMsg:%s ", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }
  pMsg->handle = NULL;
  rpcSendRequest(clientRpc, pEpSet, pMsg, NULL);
  return 0;
}

int32_t syncIOEqMsg(void *queue, SRpcMsg *pMsg) {
  SRpcMsg *pTemp;
  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, pMsg, sizeof(SRpcMsg));

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

  return 0;
}

M
sync io  
Minghao Li 已提交
104 105
// local function ------------
static int32_t syncIOStartInternal(SSyncIO *io) {
M
Minghao Li 已提交
106 107
  taosBlockSIGPIPE();

M
sync io  
Minghao Li 已提交
108
  rpcInit();
M
Minghao Li 已提交
109 110 111 112 113 114 115 116 117
  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 已提交
118
    rpcInit.cfp = syncIOProcessReply;
M
Minghao Li 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    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 已提交
138
    rpcInit.localPort = io->myAddr.eps[0].port;
M
Minghao Li 已提交
139 140
    rpcInit.label = "SYNC-IO-SERVER";
    rpcInit.numOfThreads = 1;
M
sync io  
Minghao Li 已提交
141
    rpcInit.cfp = syncIOProcessRequest;
M
Minghao Li 已提交
142 143
    rpcInit.sessions = 1000;
    rpcInit.idleTime = 2 * 1500;
M
Minghao Li 已提交
144
    rpcInit.afp = syncIOAuth;
M
Minghao Li 已提交
145 146 147 148 149 150 151 152 153 154 155 156
    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
  {
M
sync io  
Minghao Li 已提交
157
    if (pthread_create(&io->consumerTid, NULL, syncIOConsumerFunc, io) != 0) {
M
Minghao Li 已提交
158 159 160 161 162 163 164
      sError("failed to create sync consumer thread since %s", strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
  }

  // start tmr thread
M
sync io  
Minghao Li 已提交
165
  io->ioTimerManager = taosTmrInit(1000, 50, 10000, "SYNC");
M
Minghao Li 已提交
166 167 168 169

  return 0;
}

M
sync io  
Minghao Li 已提交
170
static int32_t syncIOStopInternal(SSyncIO *io) {
M
Minghao Li 已提交
171
  atomic_store_8(&io->isStart, 0);
M
sync io  
Minghao Li 已提交
172
  pthread_join(io->consumerTid, NULL);
M
Minghao Li 已提交
173 174 175
  return 0;
}

M
sync io  
Minghao Li 已提交
176 177 178
static SSyncIO *syncIOCreate(char *host, uint16_t port) {
  SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO));
  memset(io, 0, sizeof(*io));
M
Minghao Li 已提交
179

M
sync io  
Minghao Li 已提交
180 181 182
  io->pMsgQ = taosOpenQueue();
  io->pQset = taosOpenQset();
  taosAddIntoQset(io->pQset, io->pMsgQ, NULL);
M
Minghao Li 已提交
183

M
sync io  
Minghao Li 已提交
184 185
  io->myAddr.inUse = 0;
  addEpIntoEpSet(&io->myAddr, host, port);
M
Minghao Li 已提交
186

M
sync io  
Minghao Li 已提交
187
  return io;
M
Minghao Li 已提交
188 189
}

M
sync io  
Minghao Li 已提交
190
static int32_t syncIODestroy(SSyncIO *io) {
M
Minghao Li 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203
  int8_t start = atomic_load_8(&io->isStart);
  assert(start == 0);

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

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

M
sync io  
Minghao Li 已提交
204 205 206 207 208 209 210 211
  taosCloseQueue(io->pMsgQ);
  taosCloseQset(io->pQset);

  return 0;
}

static void *syncIOConsumerFunc(void *param) {
  SSyncIO *io = param;
M
Minghao Li 已提交
212

M
sync io  
Minghao Li 已提交
213
  STaosQall *qall;
M
Minghao Li 已提交
214
  SRpcMsg   *pRpcMsg, rpcMsg;
M
sync io  
Minghao Li 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  int        type;

  qall = taosAllocateQall();

  while (1) {
    int numOfMsgs = taosReadAllQitemsFromQset(io->pQset, qall, NULL, NULL);
    sTrace("syncIOConsumerFunc %d msgs are received", numOfMsgs);
    if (numOfMsgs <= 0) break;

    for (int i = 0; i < numOfMsgs; ++i) {
      taosGetQitem(qall, (void **)&pRpcMsg);
      sTrace("syncIOConsumerFunc get item from queue: msgType:%d contLen:%d msg:%s", pRpcMsg->msgType, pRpcMsg->contLen,
             (char *)(pRpcMsg->pCont));

      if (pRpcMsg->msgType == SYNC_PING) {
        if (io->FpOnSyncPing != NULL) {
M
Minghao Li 已提交
231
          SyncPing *pSyncMsg;
M
Minghao Li 已提交
232
          pSyncMsg = syncPingBuild(pRpcMsg->contLen);
M
Minghao Li 已提交
233 234
          syncPingFromRpcMsg(pRpcMsg, pSyncMsg);
          // memcpy(pSyncMsg, tmpRpcMsg.pCont, tmpRpcMsg.contLen);
M
sync io  
Minghao Li 已提交
235
          io->FpOnSyncPing(io->pSyncNode, pSyncMsg);
M
Minghao Li 已提交
236
          syncPingDestroy(pSyncMsg);
M
sync io  
Minghao Li 已提交
237
        }
M
Minghao Li 已提交
238

M
sync io  
Minghao Li 已提交
239
      } else if (pRpcMsg->msgType == SYNC_PING_REPLY) {
M
Minghao Li 已提交
240
        if (io->FpOnSyncPingReply != NULL) {
M
Minghao Li 已提交
241 242 243
          SyncPingReply *pSyncMsg;
          pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen);
          syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg);
M
Minghao Li 已提交
244
          io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg);
M
Minghao Li 已提交
245
          syncPingReplyDestroy(pSyncMsg);
M
Minghao Li 已提交
246
        }
M
Minghao Li 已提交
247 248

      } else if (pRpcMsg->msgType == SYNC_TIMEOUT) {
M
Minghao Li 已提交
249 250 251 252 253 254 255 256
        if (io->FpOnSyncTimeout != NULL) {
          SyncTimeout *pSyncMsg;
          pSyncMsg = syncTimeoutBuild();
          syncTimeoutFromRpcMsg(pRpcMsg, pSyncMsg);
          io->FpOnSyncTimeout(io->pSyncNode, pSyncMsg);
          syncTimeoutDestroy(pSyncMsg);
        }
      } else {
M
sync io  
Minghao Li 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        ;
      }
    }

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

      if (pRpcMsg->handle != NULL) {
        int msgSize = 128;
        memset(&rpcMsg, 0, sizeof(rpcMsg));
        rpcMsg.pCont = rpcMallocCont(msgSize);
        rpcMsg.contLen = msgSize;
        snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "give a reply");
        rpcMsg.handle = pRpcMsg->handle;
        rpcMsg.code = 0;

        sTrace("syncIOConsumerFunc rpcSendResponse ... msgType:%d contLen:%d", pRpcMsg->msgType, rpcMsg.contLen);
        rpcSendResponse(&rpcMsg);
      }

      taosFreeQitem(pRpcMsg);
    }
M
Minghao Li 已提交
281 282
  }

M
sync io  
Minghao Li 已提交
283 284 285 286 287 288 289 290 291 292 293 294
  taosFreeQall(qall);
  return NULL;
}

static int 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
  int ret = 0;
  return ret;
}

static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) {
M
Minghao Li 已提交
295 296
  sTrace("<-- syncIOProcessRequest --> type:%d, contLen:%d, cont:%s", pMsg->msgType, pMsg->contLen,
         (char *)pMsg->pCont);
M
sync io  
Minghao Li 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

  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) {
  sTrace("syncIOProcessReply: type:%d, contLen:%d msg:%s", pMsg->msgType, pMsg->contLen, (char *)pMsg->pCont);
  rpcFreeCont(pMsg->pCont);
}

static int32_t syncIOTickQInternal(SSyncIO *io) {
  io->ioTimerTickQ = taosTmrStart(syncIOTickQFunc, 1000, io, io->ioTimerManager);
M
Minghao Li 已提交
314
  return 0;
M
Minghao Li 已提交
315
}
M
sync io  
Minghao Li 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

static void syncIOTickQFunc(void *param, void *tmrId) {
  SSyncIO *io = (SSyncIO *)param;
  sTrace("<-- syncIOTickQFunc -->");

  SRpcMsg rpcMsg;
  rpcMsg.contLen = 64;
  rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen);
  snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOTickQ");
  rpcMsg.handle = NULL;
  rpcMsg.msgType = 55;

  SRpcMsg *pTemp;
  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, &rpcMsg, sizeof(SRpcMsg));

  taosWriteQitem(io->pMsgQ, pTemp);
  taosTmrReset(syncIOTickQFunc, 1000, io, io->ioTimerManager, &io->ioTimerTickQ);
}

static int32_t syncIOTickPingInternal(SSyncIO *io) {
  io->ioTimerTickPing = taosTmrStart(syncIOTickPingFunc, 1000, io, io->ioTimerManager);
  return 0;
}

static void syncIOTickPingFunc(void *param, void *tmrId) {
  SSyncIO *io = (SSyncIO *)param;
  sTrace("<-- syncIOTickPingFunc -->");

  SRpcMsg rpcMsg;
  rpcMsg.contLen = 64;
  rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen);
  snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOTickPing");
  rpcMsg.handle = NULL;
  rpcMsg.msgType = 77;

  rpcSendRequest(io->clientRpc, &io->myAddr, &rpcMsg, NULL);
  taosTmrReset(syncIOTickPingFunc, 1000, io, io->ioTimerManager, &io->ioTimerTickPing);
M
sync io  
Minghao Li 已提交
354
}