syncIO.c 8.9 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
  sTrace("<--- syncIOSendMsg ---> clientRpc:%p, numOfEps:%d, inUse:%d, destAddr:%s-%u", clientRpc, pEpSet->numOfEps,
         pEpSet->inUse, pEpSet->eps[0].fqdn, pEpSet->eps[0].port);
M
Minghao Li 已提交
46
  pMsg->handle = NULL;
M
sync io  
Minghao Li 已提交
47
  rpcSendRequest(clientRpc, pEpSet, pMsg, NULL);
M
Minghao Li 已提交
48 49
  return 0;
}
M
Minghao Li 已提交
50

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

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

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

M
sync io  
Minghao Li 已提交
62 63 64
int32_t syncIOStop() {
  int32_t ret = syncIOStopInternal(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
65

M
sync io  
Minghao Li 已提交
66 67
  ret = syncIODestroy(gSyncIO);
  assert(ret == 0);
M
Minghao Li 已提交
68 69 70
  return ret;
}

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

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

M
sync io  
Minghao Li 已提交
83 84
// local function ------------
static int32_t syncIOStartInternal(SSyncIO *io) {
M
Minghao Li 已提交
85 86
  taosBlockSIGPIPE();

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

  return 0;
}

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

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

M
sync io  
Minghao Li 已提交
159 160 161
  io->pMsgQ = taosOpenQueue();
  io->pQset = taosOpenQset();
  taosAddIntoQset(io->pQset, io->pMsgQ, NULL);
M
Minghao Li 已提交
162

M
sync io  
Minghao Li 已提交
163 164
  io->myAddr.inUse = 0;
  addEpIntoEpSet(&io->myAddr, host, port);
M
Minghao Li 已提交
165

M
sync io  
Minghao Li 已提交
166
  return io;
M
Minghao Li 已提交
167 168
}

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

  return 0;
}

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

M
sync io  
Minghao Li 已提交
192
  STaosQall *qall;
M
sync io  
Minghao Li 已提交
193
  SRpcMsg   *pRpcMsg, rpcMsg;
M
sync io  
Minghao Li 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  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) {
          SyncPing *pSyncMsg = syncPingBuild(pRpcMsg->contLen);
          syncPingFromRpcMsg(pRpcMsg, pSyncMsg);
          io->FpOnSyncPing(io->pSyncNode, pSyncMsg);
        }
      } else if (pRpcMsg->msgType == SYNC_PING_REPLY) {
        SyncPingReply *pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen);
        syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg);
        io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg);
      } 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 已提交
243 244
  }

M
sync io  
Minghao Li 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  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) {
  sTrace("syncIOProcessRequest: type:%d, contLen:%d, cont:%s", pMsg->msgType, pMsg->contLen, (char *)pMsg->pCont);

  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 已提交
275
  return 0;
M
Minghao Li 已提交
276
}
M
sync io  
Minghao Li 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

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