vnodeMgmt.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
#include "vnodeMain.h"
#include "vnodeMgmt.h"

static struct {
22 23 24 25
  SWorkerPool createPool;
  taos_queue  createQueue;
  SWorkerPool workerPool;
  taos_queue  workerQueue;
26 27 28
  int32_t (*msgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *);
} tsVmgmt = {0};

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
static int32_t vnodeParseCreateVnodeReq(SRpcMsg *rpcMsg, int32_t *vgId, SVnodeCfg *pCfg) {
  SCreateVnodeMsg *pCreate = rpcMsg->pCont;
  *vgId = htonl(pCreate->vgId);

  pCfg->dropped = 0;
  tstrncpy(pCfg->db, pCreate->db, sizeof(pCfg->db));

  pCfg->tsdb.cacheBlockSize = htonl(pCreate->cacheBlockSize);
  pCfg->tsdb.totalBlocks = htonl(pCreate->totalBlocks);
  pCfg->tsdb.daysPerFile = htonl(pCreate->daysPerFile);
  pCfg->tsdb.daysToKeep1 = htonl(pCreate->daysToKeep1);
  pCfg->tsdb.daysToKeep2 = htonl(pCreate->daysToKeep2);
  pCfg->tsdb.daysToKeep0 = htonl(pCreate->daysToKeep0);
  pCfg->tsdb.minRowsPerFileBlock = htonl(pCreate->minRowsPerFileBlock);
  pCfg->tsdb.maxRowsPerFileBlock = htonl(pCreate->maxRowsPerFileBlock);
  pCfg->tsdb.precision = pCreate->precision;
  pCfg->tsdb.compression = pCreate->compression;
  pCfg->tsdb.cacheLastRow = pCreate->cacheLastRow;
  pCfg->tsdb.update = pCreate->update;

  pCfg->wal.fsyncPeriod = htonl(pCreate->fsyncPeriod);
  pCfg->wal.walLevel = pCreate->walLevel;

  pCfg->sync.replica = pCreate->replica;
  pCfg->sync.quorum = pCreate->quorum;

  for (int32_t j = 0; j < pCreate->replica; ++j) {
    pCfg->sync.nodes[j].nodePort = htons(pCreate->nodes[j].port);
    tstrncpy(pCfg->sync.nodes[j].nodeFqdn, pCreate->nodes[j].fqdn, TSDB_FQDN_LEN);
  }

  return 0;
}

static int32_t vnodeProcessCreateVnodeReq(SRpcMsg *rpcMsg) {
  SVnodeCfg vnodeCfg = {0};
  int32_t   vgId = 0;

  int32_t code = vnodeParseCreateVnodeReq(rpcMsg, &vgId, &vnodeCfg);
  if (code != 0) {
    vError("failed to parse create vnode msg since %s", tstrerror(code));
  }

  vDebug("vgId:%d, create vnode req is received", vgId);

  SVnode *pVnode = vnodeAcquireInAllState(vgId);
  if (pVnode != NULL) {
    vDebug("vgId:%d, already exist, return success", vgId);
    vnodeRelease(pVnode);
    return code;
  }

  code = vnodeCreateVnode(vgId, &vnodeCfg);
  if (code != 0) {
    vError("vgId:%d, failed to create vnode since %s", vgId, tstrerror(code));
  }

  return code;
}

static int32_t vnodeProcessAlterVnodeReq(SRpcMsg *rpcMsg) {
  SVnodeCfg vnodeCfg = {0};
  int32_t   vgId = 0;

  int32_t code = vnodeParseCreateVnodeReq(rpcMsg, &vgId, &vnodeCfg);
  if (code != 0) {
    vError("failed to parse create vnode msg since %s", tstrerror(code));
  }

  vDebug("vgId:%d, alter vnode req is received", vgId);

  SVnode *pVnode = vnodeAcquire(vgId);
  if (pVnode == NULL) {
    code = terrno;
    vDebug("vgId:%d, failed to alter vnode since %s", vgId, tstrerror(code));
    return code;
  }

  code = vnodeAlterVnode(pVnode, &vnodeCfg);
  if (code != 0) {
    vError("vgId:%d, failed to alter vnode since %s", vgId, tstrerror(code));
  }

  vnodeRelease(pVnode);
  return code;
}

static SDropVnodeMsg *vnodeParseDropVnodeReq(SRpcMsg *rpcMsg) {
  SDropVnodeMsg *pDrop = rpcMsg->pCont;
  pDrop->vgId = htonl(pDrop->vgId);
  return pDrop;
}

static int32_t vnodeProcessSyncVnodeReq(SRpcMsg *rpcMsg) {
  SSyncVnodeMsg *pSync = (SSyncVnodeMsg *)vnodeParseDropVnodeReq(rpcMsg);

  int32_t code = 0;
  int32_t vgId = pSync->vgId;
  vDebug("vgId:%d, sync vnode req is received", vgId);

  SVnode *pVnode = vnodeAcquire(vgId);
  if (pVnode == NULL) {
    code = terrno;
    vDebug("vgId:%d, failed to sync since %s", vgId, tstrerror(code));
    return code;
  }

  code = vnodeSyncVnode(pVnode);
  if (code != 0) {
    vError("vgId:%d, failed to compact vnode since %s", vgId, tstrerror(code));
  }

  vnodeRelease(pVnode);
  return code;
}

static int32_t vnodeProcessCompactVnodeReq(SRpcMsg *rpcMsg) {
  SCompactVnodeMsg *pCompact = (SCompactVnodeMsg *)vnodeParseDropVnodeReq(rpcMsg);

  int32_t code = 0;
  int32_t vgId = pCompact->vgId;
  vDebug("vgId:%d, compact vnode req is received", vgId);

  SVnode *pVnode = vnodeAcquire(vgId);
  if (pVnode == NULL) {
    code = terrno;
    vDebug("vgId:%d, failed to compact since %s", vgId, tstrerror(code));
    return code;
  }

  code = vnodeCompactVnode(pVnode);
  if (code != 0) {
    vError("vgId:%d, failed to compact vnode since %s", vgId, tstrerror(code));
  }

  vnodeRelease(pVnode);
  return code;
}

static int32_t vnodeProcessDropVnodeReq(SRpcMsg *rpcMsg) {
  SDropVnodeMsg *pDrop = vnodeParseDropVnodeReq(rpcMsg);

  int32_t code = 0;
  int32_t vgId = pDrop->vgId;
  vDebug("vgId:%d, drop vnode req is received", vgId);

  SVnode *pVnode = vnodeAcquire(vgId);
  if (pVnode == NULL) {
    code = terrno;
    vDebug("vgId:%d, failed to drop since %s", vgId, tstrerror(code));
    return code;
  }

  code = vnodeDropVnode(pVnode);
  if (code != 0) {
    vError("vgId:%d, failed to drop vnode since %s", vgId, tstrerror(code));
  }

  vnodeRelease(pVnode);
  return code;
}

static int32_t vnodeProcessAlterStreamReq(SRpcMsg *pMsg) {
  vError("alter stream msg not processed");
  return TSDB_CODE_VND_MSG_NOT_PROCESSED;
}

196 197 198 199 200 201 202 203 204 205 206 207 208
static int32_t vnodeProcessMgmtStart(void *unused, SVnMgmtMsg *pMgmt, int32_t qtype) {
  SRpcMsg *pMsg = &pMgmt->rpcMsg;
  int32_t  msgType = pMsg->msgType;

  if (tsVmgmt.msgFp[msgType]) {
    vTrace("msg:%p, ahandle:%p type:%s will be processed", pMgmt, pMsg->ahandle, taosMsg[msgType]);
    return (*tsVmgmt.msgFp[msgType])(pMsg);
  } else {
    vError("msg:%p, ahandle:%p type:%s not processed since no handle", pMgmt, pMsg->ahandle, taosMsg[msgType]);
    return TSDB_CODE_DND_MSG_NOT_PROCESSED;
  }
}

209
static void vnodeProcessMgmtEnd(void *unused, SVnMgmtMsg *pMgmt, int32_t qtype, int32_t code) {
210
  SRpcMsg *pMsg = &pMgmt->rpcMsg;
211
  vTrace("msg:%p, is processed, result:%s", pMgmt, tstrerror(code));
212

213 214 215
  SRpcMsg rsp = {.code = code, .handle = pMsg->handle};
  rpcSendResponse(&rsp);
  taosFreeQitem(pMgmt);
216 217 218
}

static void vnodeInitMgmtReqFp() {
219 220 221 222 223
  tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessCreateVnodeReq;
  tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessAlterVnodeReq;
  tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessSyncVnodeReq;
  tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessCompactVnodeReq;
  tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessDropVnodeReq;
224 225 226 227 228 229 230 231 232 233 234 235
  tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessAlterStreamReq;
}

static int32_t vnodeWriteToMgmtQueue(SRpcMsg *pMsg) {
  int32_t     size = sizeof(SVnMgmtMsg) + pMsg->contLen;
  SVnMgmtMsg *pMgmt = taosAllocateQitem(size);
  if (pMgmt == NULL) return TSDB_CODE_DND_OUT_OF_MEMORY;

  pMgmt->rpcMsg = *pMsg;
  pMgmt->rpcMsg.pCont = pMgmt->pCont;
  memcpy(pMgmt->pCont, pMsg->pCont, pMsg->contLen);

236 237 238 239 240
  if (pMsg->msgType == TSDB_MSG_TYPE_MD_CREATE_VNODE) {
    return taosWriteQitem(tsVmgmt.createQueue, TAOS_QTYPE_RPC, pMgmt);
  } else {
    return taosWriteQitem(tsVmgmt.workerQueue, TAOS_QTYPE_RPC, pMgmt);
  }
241 242 243 244 245
}

void vnodeProcessMgmtMsg(SRpcMsg *pMsg) {
  int32_t code = vnodeWriteToMgmtQueue(pMsg);
  if (code != TSDB_CODE_SUCCESS) {
246
    vError("msg, ahandle:%p type:%s not processed since %s", pMsg->ahandle, taosMsg[pMsg->msgType], tstrerror(code));
247 248 249 250 251 252 253 254 255 256
    SRpcMsg rsp = {.handle = pMsg->handle, .code = code};
    rpcSendResponse(&rsp);
  }

  rpcFreeCont(pMsg->pCont);
}

int32_t vnodeInitMgmt() {
  vnodeInitMgmtReqFp();

257 258
  SWorkerPool *pPool = &tsVmgmt.createPool;
  pPool->name = "vnode-mgmt-create";
259
  pPool->startFp = (ProcessStartFp)vnodeProcessMgmtStart;
260
  pPool->endFp = (ProcessEndFp)vnodeProcessMgmtEnd;
261 262 263 264 265 266
  pPool->min = 1;
  pPool->max = 1;
  if (tWorkerInit(pPool) != 0) {
    return TSDB_CODE_VND_OUT_OF_MEMORY;
  }

267
  tsVmgmt.createQueue = tWorkerAllocQueue(pPool, NULL);
268

269 270 271 272 273 274 275 276 277 278 279 280 281
  pPool = &tsVmgmt.workerPool;
  pPool->name = "vnode-mgmt-worker";
  pPool->startFp = (ProcessStartFp)vnodeProcessMgmtStart;
  pPool->endFp = (ProcessEndFp)vnodeProcessMgmtEnd;
  pPool->min = 1;
  pPool->max = 1;
  if (tWorkerInit(pPool) != 0) {
    return TSDB_CODE_VND_OUT_OF_MEMORY;
  }

  tsVmgmt.workerQueue = tWorkerAllocQueue(pPool, NULL);

  vInfo("vmgmt is initialized");
282 283 284 285
  return TSDB_CODE_SUCCESS;
}

void vnodeCleanupMgmt() {
286 287 288 289 290 291 292
  tWorkerFreeQueue(&tsVmgmt.createPool, tsVmgmt.createQueue);
  tWorkerCleanup(&tsVmgmt.createPool);
  tsVmgmt.createQueue = NULL;

  tWorkerFreeQueue(&tsVmgmt.workerPool, tsVmgmt.workerQueue);
  tWorkerCleanup(&tsVmgmt.workerPool);
  tsVmgmt.createQueue = NULL;
293 294
  vInfo("vmgmt is closed");
}