syncIO.c 9.3 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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"
#include <tep.h>
#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
// public function ------------
int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) {
M
Minghao Li 已提交
44 45 46 47 48
  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);
M
Minghao Li 已提交
49
  pMsg->handle = NULL;
M
sync io  
Minghao Li 已提交
50
  rpcSendRequest(clientRpc, pEpSet, pMsg, NULL);
M
Minghao Li 已提交
51 52
  return 0;
}
M
Minghao Li 已提交
53

M
sync io  
Minghao Li 已提交
54 55
int32_t syncIOStart(char *host, uint16_t port) {
  gSyncIO = syncIOCreate(host, port);
M
Minghao Li 已提交
56 57
  assert(gSyncIO != NULL);

M
sync io  
Minghao Li 已提交
58
  int32_t ret = syncIOStartInternal(gSyncIO);
M
Minghao Li 已提交
59 60
  assert(ret == 0);

M
Minghao Li 已提交
61
  sTrace("syncIOStart ok, gSyncIO:%p gSyncIO->clientRpc:%p", gSyncIO, gSyncIO->clientRpc);
M
Minghao Li 已提交
62 63
  return 0;
}
M
Minghao Li 已提交
64

M
sync io  
Minghao Li 已提交
65 66 67
int32_t syncIOStop() {
  int32_t ret = syncIOStopInternal(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
68

M
sync io  
Minghao Li 已提交
69 70
  ret = syncIODestroy(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
71 72 73
  return ret;
}

M
sync io  
Minghao Li 已提交
74 75 76 77
int32_t syncIOTickQ() {
  int32_t ret = syncIOTickQInternal(gSyncIO);
  assert(ret == 0);
  return ret;
M
Minghao Li 已提交
78 79
}

M
sync io  
Minghao Li 已提交
80 81 82 83
int32_t syncIOTickPing() {
  int32_t ret = syncIOTickPingInternal(gSyncIO);
  assert(ret == 0);
  return ret;
M
Minghao Li 已提交
84 85
}

M
sync io  
Minghao Li 已提交
86 87
// local function ------------
static int32_t syncIOStartInternal(SSyncIO *io) {
M
Minghao Li 已提交
88 89
  taosBlockSIGPIPE();

M
sync io  
Minghao Li 已提交
90
  rpcInit();
M
Minghao Li 已提交
91 92 93 94 95 96 97 98 99
  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 已提交
100
    rpcInit.cfp = syncIOProcessReply;
M
Minghao Li 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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 已提交
120
    rpcInit.localPort = io->myAddr.eps[0].port;
M
Minghao Li 已提交
121 122
    rpcInit.label = "SYNC-IO-SERVER";
    rpcInit.numOfThreads = 1;
M
sync io  
Minghao Li 已提交
123
    rpcInit.cfp = syncIOProcessRequest;
M
Minghao Li 已提交
124 125
    rpcInit.sessions = 1000;
    rpcInit.idleTime = 2 * 1500;
M
Minghao Li 已提交
126
    rpcInit.afp = syncIOAuth;
M
Minghao Li 已提交
127 128 129 130 131 132 133 134 135 136 137 138
    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 已提交
139
    if (pthread_create(&io->consumerTid, NULL, syncIOConsumerFunc, io) != 0) {
M
Minghao Li 已提交
140 141 142 143 144 145 146
      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 已提交
147
  io->ioTimerManager = taosTmrInit(1000, 50, 10000, "SYNC");
M
Minghao Li 已提交
148 149 150 151

  return 0;
}

M
sync io  
Minghao Li 已提交
152
static int32_t syncIOStopInternal(SSyncIO *io) {
M
Minghao Li 已提交
153
  atomic_store_8(&io->isStart, 0);
M
sync io  
Minghao Li 已提交
154
  pthread_join(io->consumerTid, NULL);
M
Minghao Li 已提交
155 156 157
  return 0;
}

M
sync io  
Minghao Li 已提交
158 159 160
static SSyncIO *syncIOCreate(char *host, uint16_t port) {
  SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO));
  memset(io, 0, sizeof(*io));
M
Minghao Li 已提交
161

M
sync io  
Minghao Li 已提交
162 163 164
  io->pMsgQ = taosOpenQueue();
  io->pQset = taosOpenQset();
  taosAddIntoQset(io->pQset, io->pMsgQ, NULL);
M
Minghao Li 已提交
165

M
sync io  
Minghao Li 已提交
166 167
  io->myAddr.inUse = 0;
  addEpIntoEpSet(&io->myAddr, host, port);
M
Minghao Li 已提交
168

M
sync io  
Minghao Li 已提交
169
  return io;
M
Minghao Li 已提交
170 171
}

M
sync io  
Minghao Li 已提交
172
static int32_t syncIODestroy(SSyncIO *io) {
M
Minghao Li 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185
  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 已提交
186 187 188 189 190 191 192 193
  taosCloseQueue(io->pMsgQ);
  taosCloseQset(io->pQset);

  return 0;
}

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

M
sync io  
Minghao Li 已提交
195
  STaosQall *qall;
M
Minghao Li 已提交
196
  SRpcMsg *  pRpcMsg, rpcMsg;
M
sync io  
Minghao Li 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  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 已提交
213 214 215 216 217 218
          SyncPing *pSyncMsg;

          SRpcMsg tmpRpcMsg;
          memcpy(&tmpRpcMsg, pRpcMsg, sizeof(SRpcMsg));
          pSyncMsg = syncPingBuild(tmpRpcMsg.contLen);

M
Minghao Li 已提交
219 220 221 222
          syncPingFromRpcMsg(pRpcMsg, pSyncMsg);

          // memcpy(pSyncMsg, tmpRpcMsg.pCont, tmpRpcMsg.contLen);

M
sync io  
Minghao Li 已提交
223 224
          io->FpOnSyncPing(io->pSyncNode, pSyncMsg);
        }
M
Minghao Li 已提交
225

M
sync io  
Minghao Li 已提交
226 227 228
      } else if (pRpcMsg->msgType == SYNC_PING_REPLY) {
        SyncPingReply *pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen);
        syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg);
M
Minghao Li 已提交
229 230 231 232

        if (io->FpOnSyncPingReply != NULL) {
          io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg);
        }
M
sync io  
Minghao Li 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
      } else {
        ;
      }
    }

    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 已提交
258 259
  }

M
sync io  
Minghao Li 已提交
260 261 262 263 264 265 266 267 268 269 270 271
  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 已提交
272 273
  sTrace("<-- syncIOProcessRequest --> type:%d, contLen:%d, cont:%s", pMsg->msgType, pMsg->contLen,
         (char *)pMsg->pCont);
M
sync io  
Minghao Li 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

  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 已提交
291
  return 0;
M
Minghao Li 已提交
292
}
M
sync io  
Minghao Li 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

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 已提交
331
}