syncIO.c 7.1 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 26 27 28 29 30
// local function ------------
static int32_t doSyncIOStart(SSyncIO *io);
static int32_t doSyncIOStop(SSyncIO *io);
static int32_t doSyncIOPing(SSyncIO *io);
static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);
static int32_t doSyncIODestroy(SSyncIO *io);
M
Minghao Li 已提交
31 32

static SSyncIO *syncIOCreate();
M
Minghao Li 已提交
33
static void    *syncIOConsumer(void *param);
M
Minghao Li 已提交
34 35 36 37
static int      syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey);
static void     syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);
static void     syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet);
static void     syncIOTick(void *param, void *tmrId);
M
Minghao Li 已提交
38 39
// ----------------------------

M
Minghao Li 已提交
40 41 42 43 44
int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) {
  pMsg->handle = NULL;
  rpcSendRequest(handle, pEpSet, pMsg, NULL);
  return 0;
}
M
Minghao Li 已提交
45

M
Minghao Li 已提交
46 47 48 49
int32_t syncIOStart() {
  gSyncIO = syncIOCreate();
  assert(gSyncIO != NULL);

M
Minghao Li 已提交
50 51 52
  int32_t ret = doSyncIOStart(gSyncIO);
  assert(ret == 0);

M
Minghao Li 已提交
53 54
  return 0;
}
M
Minghao Li 已提交
55 56 57

int32_t syncIOStop() { return 0; }

M
Minghao Li 已提交
58
// local function ------------
M
Minghao Li 已提交
59
static void syncIOTick(void *param, void *tmrId) {
M
Minghao Li 已提交
60
  SSyncIO *io = (SSyncIO *)param;
M
Minghao Li 已提交
61
  sDebug("syncIOTick ... ");
M
Minghao Li 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

  SRpcMsg rpcMsg;
  rpcMsg.pCont = rpcMallocCont(10);
  snprintf(rpcMsg.pCont, 10, "TICK");
  rpcMsg.contLen = 10;
  rpcMsg.handle = NULL;
  rpcMsg.msgType = 2;

  SRpcMsg *pTemp;

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

  taosWriteQitem(io->pMsgQ, pTemp);

M
Minghao Li 已提交
77
  taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, io->syncTimer);
M
Minghao Li 已提交
78 79
}

M
Minghao Li 已提交
80
static void *syncIOConsumer(void *param) {
M
Minghao Li 已提交
81 82 83
  SSyncIO *io = param;

  STaosQall *qall;
M
Minghao Li 已提交
84
  SRpcMsg   *pRpcMsg, rpcMsg;
M
Minghao Li 已提交
85 86 87 88 89 90 91 92 93 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 119 120 121
  int        type;

  qall = taosAllocateQall();

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

    for (int i = 0; i < numOfMsgs; ++i) {
      taosGetQitem(qall, (void **)&pRpcMsg);
      sDebug("sync-io recv type:%d msg:%s", pRpcMsg->msgType, (char *)(pRpcMsg->pCont));
    }

    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;
        rpcMsg.handle = pRpcMsg->handle;
        rpcMsg.code = 0;
        rpcSendResponse(&rpcMsg);
      }

      taosFreeQitem(pRpcMsg);
    }
  }

  taosFreeQall(qall);
  return NULL;
}

M
Minghao Li 已提交
122
static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey) {
M
Minghao Li 已提交
123 124 125 126 127 128
  // 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;
}

M
Minghao Li 已提交
129 130
static void syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) {
  sDebug("syncIODoReply ... ");
M
Minghao Li 已提交
131 132 133
  rpcFreeCont(pMsg->pCont);
}

M
Minghao Li 已提交
134
static void syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) {
M
Minghao Li 已提交
135 136 137 138 139 140 141 142 143 144
  SSyncIO *io = pParent;
  SRpcMsg *pTemp;

  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, pMsg, sizeof(SRpcMsg));

  sDebug("request is received, type:%d, contLen:%d, item:%p", pMsg->msgType, pMsg->contLen, pTemp);
  taosWriteQitem(io->pMsgQ, pTemp);
}

M
Minghao Li 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static SSyncIO *syncIOCreate() {
  SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO));
  memset(io, 0, sizeof(*io));

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

  io->start = doSyncIOStart;
  io->stop = doSyncIOStop;
  io->ping = doSyncIOPing;
  io->onMsg = doSyncIOOnMsg;
  io->destroy = doSyncIODestroy;

  return io;
}

M
Minghao Li 已提交
162 163 164 165 166 167 168 169 170 171 172 173
static int32_t doSyncIOStart(SSyncIO *io) {
  taosBlockSIGPIPE();

  tsRpcForceTcp = 1;

  // cient rpc init
  {
    SRpcInit rpcInit;
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localPort = 0;
    rpcInit.label = "SYNC-IO-CLIENT";
    rpcInit.numOfThreads = 1;
M
Minghao Li 已提交
174
    rpcInit.cfp = syncIODoReply;
M
Minghao Li 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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));
    rpcInit.localPort = 38000;
    rpcInit.label = "SYNC-IO-SERVER";
    rpcInit.numOfThreads = 1;
M
Minghao Li 已提交
197
    rpcInit.cfp = syncIODoRequest;
M
Minghao Li 已提交
198 199
    rpcInit.sessions = 1000;
    rpcInit.idleTime = 2 * 1500;
M
Minghao Li 已提交
200
    rpcInit.afp = syncIOAuth;
M
Minghao Li 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    rpcInit.parent = io;
    rpcInit.connType = TAOS_CONN_SERVER;

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

  io->epSet.inUse = 0;
  addEpIntoEpSet(&io->epSet, "127.0.0.1", 38000);

  // start consumer thread
  {
M
Minghao Li 已提交
216
    if (pthread_create(&io->tid, NULL, syncIOConsumer, io) != 0) {
M
Minghao Li 已提交
217 218 219 220 221 222 223
      sError("failed to create sync consumer thread since %s", strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
  }

  // start tmr thread
M
Minghao Li 已提交
224 225
  // io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC");
  // io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager);
M
Minghao Li 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

  return 0;
}

static int32_t doSyncIOStop(SSyncIO *io) {
  atomic_store_8(&io->isStart, 0);
  pthread_join(io->tid, NULL);
  return 0;
}

static int32_t doSyncIOPing(SSyncIO *io) {
  SRpcMsg rpcMsg, rspMsg;

  rpcMsg.pCont = rpcMallocCont(10);
  snprintf(rpcMsg.pCont, 10, "ping");
  rpcMsg.contLen = 10;
  rpcMsg.handle = NULL;
  rpcMsg.msgType = 1;

  rpcSendRequest(io->clientRpc, &io->epSet, &rpcMsg, NULL);

  return 0;
}

M
Minghao Li 已提交
250
static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { return 0; }
M
Minghao Li 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

static int32_t doSyncIODestroy(SSyncIO *io) {
  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;
  }

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

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

  return 0;
M
Minghao Li 已提交
277
}