mnode.c 11.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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 "tglobal.h"
#include "tqueue.h"
#include "mnodeAcct.h"
#include "mnodeAuth.h"
#include "mnodeBalance.h"
#include "mnodeCluster.h"
#include "mnodeDb.h"
#include "mnodeDnode.h"
#include "mnodeFunc.h"
#include "mnodeMnode.h"
#include "mnodeOper.h"
#include "mnodeProfile.h"
#include "mnodeShow.h"
#include "mnodeStable.h"
#include "mnodeSync.h"
#include "mnodeTelem.h"
#include "mnodeUser.h"
#include "mnodeVgroup.h"
S
Shengliang Guan 已提交
36
#include "mnodeTrans.h"
S
Shengliang Guan 已提交
37

S
Shengliang Guan 已提交
38
SMnodeBak tsMint = {0};
S
Shengliang Guan 已提交
39 40 41 42 43

int32_t mnodeGetDnodeId() { return tsMint.para.dnodeId; }

int64_t mnodeGetClusterId() { return tsMint.para.clusterId; }

S
Shengliang Guan 已提交
44 45 46 47
void mnodeSendMsgToDnode(SMnode *pMnode, struct SEpSet *epSet, struct SRpcMsg *rpcMsg) {
  assert(pMnode);
  (*pMnode->sendMsgToDnodeFp)(pMnode->pServer, epSet, rpcMsg);
}
S
Shengliang Guan 已提交
48

S
Shengliang Guan 已提交
49 50 51 52
void mnodeSendMsgToMnode(SMnode *pMnode, struct SRpcMsg *rpcMsg) {
  assert(pMnode);
  (*pMnode->sendMsgToMnodeFp)(pMnode->pServer, rpcMsg);
}
S
Shengliang Guan 已提交
53

S
Shengliang Guan 已提交
54 55 56 57
void mnodeSendRedirectMsg(SMnode *pMnode, struct SRpcMsg *rpcMsg, bool forShell) {
  assert(pMnode);
  (*pMnode->sendRedirectMsgFp)(pMnode->pServer, rpcMsg, forShell);
}
S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

static int32_t mnodeInitTimer() {
  if (tsMint.timer == NULL) {
    tsMint.timer = taosTmrInit(tsMaxShellConns, 200, 3600000, "MND");
  }

  if (tsMint.timer == NULL) {
    return -1;
  }

  return 0;
}

static void mnodeCleanupTimer() {
  if (tsMint.timer != NULL) {
    taosTmrCleanUp(tsMint.timer);
    tsMint.timer = NULL;
  }
}

tmr_h mnodeGetTimer() { return tsMint.timer; }

S
Shengliang Guan 已提交
80 81 82 83 84 85 86
static int32_t mnodeSetPara(SMnode *pMnode, SMnodePara para) {
  pMnode->dnodeId = para.dnodeId;
  pMnode->clusterId = para.clusterId;
  pMnode->putMsgToApplyMsgFp = para.putMsgToApplyMsgFp;
  pMnode->sendMsgToDnodeFp = para.sendMsgToDnodeFp;
  pMnode->sendMsgToMnodeFp = para.sendMsgToMnodeFp;
  pMnode->sendRedirectMsgFp = para.sendRedirectMsgFp;
S
Shengliang Guan 已提交
87

S
Shengliang Guan 已提交
88
  if (pMnode->sendMsgToDnodeFp == NULL) {
S
Shengliang Guan 已提交
89 90 91 92
    terrno = TSDB_CODE_MND_APP_ERROR;
    return -1;
  }

S
Shengliang Guan 已提交
93
  if (pMnode->sendMsgToMnodeFp == NULL) {
S
Shengliang Guan 已提交
94 95 96 97
    terrno = TSDB_CODE_MND_APP_ERROR;
    return -1;
  }

S
Shengliang Guan 已提交
98
  if (pMnode->sendRedirectMsgFp == NULL) {
S
Shengliang Guan 已提交
99 100 101 102
    terrno = TSDB_CODE_MND_APP_ERROR;
    return -1;
  }

S
Shengliang Guan 已提交
103
  if (pMnode->putMsgToApplyMsgFp == NULL) {
S
Shengliang Guan 已提交
104 105 106 107
    terrno = TSDB_CODE_MND_APP_ERROR;
    return -1;
  }

S
Shengliang Guan 已提交
108
  if (pMnode->dnodeId < 0) {
S
Shengliang Guan 已提交
109 110 111 112
    terrno = TSDB_CODE_MND_APP_ERROR;
    return -1;
  }

S
Shengliang Guan 已提交
113
  if (pMnode->clusterId < 0) {
S
Shengliang Guan 已提交
114 115 116 117 118 119 120 121 122 123 124
    terrno = TSDB_CODE_MND_APP_ERROR;
    return -1;
  }

  return 0;
}

static int32_t mnodeAllocInitSteps() {
  struct SSteps *steps = taosStepInit(16, NULL);
  if (steps == NULL) return -1;

S
Shengliang Guan 已提交
125
  if (taosStepAdd(steps, "mnode-trans", mnodeInitTrans, mnodeCleanupTrans) != 0) return -1;
S
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  if (taosStepAdd(steps, "mnode-cluster", mnodeInitCluster, mnodeCleanupCluster) != 0) return -1;
  if (taosStepAdd(steps, "mnode-dnode", mnodeInitDnode, mnodeCleanupDnode) != 0) return -1;
  if (taosStepAdd(steps, "mnode-mnode", mnodeInitMnode, mnodeCleanupMnode) != 0) return -1;
  if (taosStepAdd(steps, "mnode-acct", mnodeInitAcct, mnodeCleanupAcct) != 0) return -1;
  if (taosStepAdd(steps, "mnode-auth", mnodeInitAuth, mnodeCleanupAuth) != 0) return -1;
  if (taosStepAdd(steps, "mnode-user", mnodeInitUser, mnodeCleanupUser) != 0) return -1;
  if (taosStepAdd(steps, "mnode-db", mnodeInitDb, mnodeCleanupDb) != 0) return -1;
  if (taosStepAdd(steps, "mnode-vgroup", mnodeInitVgroup, mnodeCleanupVgroup) != 0) return -1;
  if (taosStepAdd(steps, "mnode-stable", mnodeInitStable, mnodeCleanupStable) != 0) return -1;
  if (taosStepAdd(steps, "mnode-func", mnodeInitFunc, mnodeCleanupFunc) != 0) return -1;
  if (taosStepAdd(steps, "mnode-sdb", sdbInit, sdbCleanup) != 0) return -1;

  tsMint.pInitSteps = steps;
  return 0;
}

static int32_t mnodeAllocStartSteps() {
S
Shengliang Guan 已提交
143
  struct SSteps *steps = taosStepInit(8, NULL);
S
Shengliang Guan 已提交
144 145 146
  if (steps == NULL) return -1;

  taosStepAdd(steps, "mnode-timer", mnodeInitTimer, NULL);
147
  taosStepAdd(steps, "mnode-sdb-file", sdbOpen, sdbClose);
S
Shengliang Guan 已提交
148 149 150 151 152 153 154 155 156 157 158
  taosStepAdd(steps, "mnode-balance", mnodeInitBalance, mnodeCleanupBalance);
  taosStepAdd(steps, "mnode-profile", mnodeInitProfile, mnodeCleanupProfile);
  taosStepAdd(steps, "mnode-show", mnodeInitShow, mnodeCleanUpShow);
  taosStepAdd(steps, "mnode-sync", mnodeInitSync, mnodeCleanUpSync);
  taosStepAdd(steps, "mnode-telem", mnodeInitTelem, mnodeCleanupTelem);
  taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer);

  tsMint.pStartSteps = steps;
  return 0;
}

S
Shengliang Guan 已提交
159 160 161 162 163
SMnode *mnodeCreate(SMnodePara para) {
  SMnode *pMnode = calloc(1, sizeof(SMnode));

  if (mnodeSetPara(pMnode, para) != 0) {
    free(pMnode);
S
Shengliang Guan 已提交
164
    mError("failed to init mnode para since %s", terrstr());
S
Shengliang Guan 已提交
165
    return NULL;
S
Shengliang Guan 已提交
166 167 168 169
  }

  if (mnodeAllocInitSteps() != 0) {
    mError("failed to alloc init steps since %s", terrstr());
S
Shengliang Guan 已提交
170
    return NULL;
S
Shengliang Guan 已提交
171 172 173 174
  }

  if (mnodeAllocStartSteps() != 0) {
    mError("failed to alloc start steps since %s", terrstr());
S
Shengliang Guan 已提交
175
    return NULL;
S
Shengliang Guan 已提交
176 177
  }

S
Shengliang Guan 已提交
178 179
   taosStepExec(tsMint.pInitSteps);
   return NULL;
S
Shengliang Guan 已提交
180 181 182 183
}

void mnodeCleanup() { taosStepCleanup(tsMint.pInitSteps); }

S
Shengliang Guan 已提交
184
int32_t mnodeDeploy(SMnodeCfg *pCfg) {
S
Shengliang Guan 已提交
185 186 187 188 189 190 191 192 193 194 195
  if (tsMint.para.dnodeId <= 0 && tsMint.para.clusterId <= 0) {
    if (sdbDeploy() != 0) {
      mError("failed to deploy sdb since %s", terrstr());
      return -1;
    }
  }

  mDebug("mnode is deployed");
  return 0;
}

S
Shengliang Guan 已提交
196
void mnodeUnDeploy() { sdbUnDeploy(); }
S
Shengliang Guan 已提交
197

S
Shengliang Guan 已提交
198
int32_t mnodeStart(SMnodeCfg *pCfg) { return taosStepExec(tsMint.pStartSteps); }
S
Shengliang Guan 已提交
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

int32_t mnodeAlter(SMnodeCfg *pCfg) { return 0; }

void mnodeStop() { taosStepCleanup(tsMint.pStartSteps); }

int32_t mnodeGetLoad(SMnodeLoad *pLoad) { return 0; }

SMnodeMsg *mnodeInitMsg(SRpcMsg *pRpcMsg) {
  SMnodeMsg *pMsg = taosAllocateQitem(sizeof(SMnodeMsg));
  if (pMsg == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  if (rpcGetConnInfo(pRpcMsg->handle, &pMsg->conn) != 0) {
    mnodeCleanupMsg(pMsg);
    mError("can not get user from conn:%p", pMsg->rpcMsg.handle);
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    return NULL;
  }

  pMsg->rpcMsg = *pRpcMsg;
  pMsg->createdTime = taosGetTimestampSec();

  return pMsg;
}

void mnodeCleanupMsg(SMnodeMsg *pMsg) {
  if (pMsg->pUser != NULL) {
    sdbRelease(pMsg->pUser);
  }

  taosFreeQitem(pMsg);
}

static void mnodeProcessRpcMsg(SMnodeMsg *pMsg) {
  int32_t msgType = pMsg->rpcMsg.msgType;

S
Shengliang Guan 已提交
237 238
  MnodeRpcFp fp = tsMint.msgFp[msgType];
  if (fp == NULL) {
S
Shengliang Guan 已提交
239 240
  }

S
Shengliang Guan 已提交
241 242 243 244
  int32_t code = (fp)(pMsg);
  if (code != 0) {
    assert(code);
  }
S
Shengliang Guan 已提交
245 246 247 248 249 250 251 252 253 254
}

void mnodeSetMsgFp(int32_t msgType, MnodeRpcFp fp) {
  if (msgType > 0 || msgType < TSDB_MSG_TYPE_MAX) {
    tsMint.msgFp[msgType] = fp;
  }
}

void mnodeProcessMsg(SMnodeMsg *pMsg, EMnMsgType msgType) {
  if (!mnodeIsMaster()) {
S
Shengliang Guan 已提交
255
    mnodeSendRedirectMsg(NULL, &pMsg->rpcMsg, true);
S
Shengliang Guan 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 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 315 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    mnodeCleanupMsg(pMsg);
    return;
  }

  switch (msgType) {
    case MN_MSG_TYPE_READ:
    case MN_MSG_TYPE_WRITE:
    case MN_MSG_TYPE_SYNC:
      mnodeProcessRpcMsg(pMsg);
      break;
    case MN_MSG_TYPE_APPLY:
      break;
    default:
      break;
  }
}

#if 0

static void mnodeProcessWriteReq(SMnodeMsg *pMsg, void *unused) {
  int32_t msgType = pMsg->rpcMsg.msgType;
  void   *ahandle = pMsg->rpcMsg.ahandle;
  int32_t code = 0;

  if (pMsg->rpcMsg.pCont == NULL) {
    mError("msg:%p, app:%p type:%s content is null", pMsg, ahandle, taosMsg[msgType]);
    code = TSDB_CODE_MND_INVALID_MSG_LEN;
    goto PROCESS_WRITE_REQ_END;
  }

  if (!mnodeIsMaster()) {
    SMnRsp *rpcRsp = &pMsg->rpcRsp;
    SEpSet *epSet = rpcMallocCont(sizeof(SEpSet));
    mnodeGetMnodeEpSetForShell(epSet, true);
    rpcRsp->rsp = epSet;
    rpcRsp->len = sizeof(SEpSet);

    mDebug("msg:%p, app:%p type:%s in write queue, is redirected, numOfEps:%d inUse:%d", pMsg, ahandle,
           taosMsg[msgType], epSet->numOfEps, epSet->inUse);

    code = TSDB_CODE_RPC_REDIRECT;
    goto PROCESS_WRITE_REQ_END;
  }

  if (tsMworker.writeMsgFp[msgType] == NULL) {
    mError("msg:%p, app:%p type:%s not processed", pMsg, ahandle, taosMsg[msgType]);
    code = TSDB_CODE_MND_MSG_NOT_PROCESSED;
    goto PROCESS_WRITE_REQ_END;
  }

  code = (*tsMworker.writeMsgFp[msgType])(pMsg);

PROCESS_WRITE_REQ_END:
  mnodeSendRsp(pMsg, code);
}

static void mnodeProcessReadReq(SMnodeMsg *pMsg, void *unused) {
  int32_t msgType = pMsg->rpcMsg.msgType;
  void   *ahandle = pMsg->rpcMsg.ahandle;
  int32_t code = 0;

  if (pMsg->rpcMsg.pCont == NULL) {
    mError("msg:%p, app:%p type:%s in mread queue, content is null", pMsg, ahandle, taosMsg[msgType]);
    code = TSDB_CODE_MND_INVALID_MSG_LEN;
    goto PROCESS_READ_REQ_END;
  }

  if (!mnodeIsMaster()) {
    SMnRsp *rpcRsp = &pMsg->rpcRsp;
    SEpSet *epSet = rpcMallocCont(sizeof(SEpSet));
    if (!epSet) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto PROCESS_READ_REQ_END;
    }
    mnodeGetMnodeEpSetForShell(epSet, true);
    rpcRsp->rsp = epSet;
    rpcRsp->len = sizeof(SEpSet);

    mDebug("msg:%p, app:%p type:%s in mread queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType],
           epSet->numOfEps, epSet->inUse);
    code = TSDB_CODE_RPC_REDIRECT;
    goto PROCESS_READ_REQ_END;
  }

  if (tsMworker.readMsgFp[msgType] == NULL) {
    mError("msg:%p, app:%p type:%s in mread queue, not processed", pMsg, ahandle, taosMsg[msgType]);
    code = TSDB_CODE_MND_MSG_NOT_PROCESSED;
    goto PROCESS_READ_REQ_END;
  }

  mTrace("msg:%p, app:%p type:%s will be processed in mread queue", pMsg, ahandle, taosMsg[msgType]);
  code = (*tsMworker.readMsgFp[msgType])(pMsg);

PROCESS_READ_REQ_END:
  mnodeSendRsp(pMsg, code);
}

static void mnodeProcessPeerReq(SMnodeMsg *pMsg, void *unused) {
  int32_t msgType = pMsg->rpcMsg.msgType;
  void   *ahandle = pMsg->rpcMsg.ahandle;
  int32_t code = 0;

  if (pMsg->rpcMsg.pCont == NULL) {
    mError("msg:%p, ahandle:%p type:%s in mpeer queue, content is null", pMsg, ahandle, taosMsg[msgType]);
    code = TSDB_CODE_MND_INVALID_MSG_LEN;
    goto PROCESS_PEER_REQ_END;
  }

  if (!mnodeIsMaster()) {
    SMnRsp *rpcRsp = &pMsg->rpcRsp;
    SEpSet *epSet = rpcMallocCont(sizeof(SEpSet));
    mnodeGetMnodeEpSetForPeer(epSet, true);
    rpcRsp->rsp = epSet;
    rpcRsp->len = sizeof(SEpSet);

    mDebug("msg:%p, ahandle:%p type:%s in mpeer queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle,
           taosMsg[msgType], epSet->numOfEps, epSet->inUse);

    code = TSDB_CODE_RPC_REDIRECT;
    goto PROCESS_PEER_REQ_END;
  }

  if (tsMworker.peerReqFp[msgType] == NULL) {
    mError("msg:%p, ahandle:%p type:%s in mpeer queue, not processed", pMsg, ahandle, taosMsg[msgType]);
    code = TSDB_CODE_MND_MSG_NOT_PROCESSED;
    goto PROCESS_PEER_REQ_END;
  }

  code = (*tsMworker.peerReqFp[msgType])(pMsg);

PROCESS_PEER_REQ_END:
  mnodeSendRsp(pMsg, code);
}

static void mnodeProcessPeerRsp(SMnodeMsg *pMsg, void *unused) {
  int32_t  msgType = pMsg->rpcMsg.msgType;
  SRpcMsg *pRpcMsg = &pMsg->rpcMsg;

  if (!mnodeIsMaster()) {
    mError("msg:%p, ahandle:%p type:%s not processed for not master", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]);
    mnodeCleanupMsg2(pMsg);
  }

  if (tsMworker.peerRspFp[msgType]) {
    (*tsMworker.peerRspFp[msgType])(pRpcMsg);
  } else {
    mError("msg:%p, ahandle:%p type:%s is not processed", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]);
  }

  mnodeCleanupMsg2(pMsg);
}
#endif