mondeInt.c 6.9 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
/*
 * 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 "tstep.h"
S
Shengliang Guan 已提交
20 21 22
#include "mnodeAcct.h"
#include "mnodeAuth.h"
#include "mnodeBalance.h"
S
Shengliang Guan 已提交
23 24 25 26 27
#include "mnodeCluster.h"
#include "mnodeDb.h"
#include "mnodeDnode.h"
#include "mnodeFunc.h"
#include "mnodeMnode.h"
S
Shengliang Guan 已提交
28
#include "mnodeOper.h"
S
Shengliang Guan 已提交
29 30 31
#include "mnodeProfile.h"
#include "mnodeSdb.h"
#include "mnodeShow.h"
S
Shengliang Guan 已提交
32 33
#include "mnodeStable.h"
#include "mnodeSync.h"
S
Shengliang Guan 已提交
34 35 36 37 38 39 40 41 42 43
#include "mnodeUser.h"
#include "mnodeVgroup.h"
#include "mnodeWorker.h"

static struct {
  int32_t  state;
  int32_t  dnodeId;
  char     clusterId[TSDB_CLUSTER_ID_LEN];
  tmr_h    timer;
  SMnodeFp fp;
S
Shengliang Guan 已提交
44 45
  SSteps * steps1;
  SSteps * steps2;
S
Shengliang Guan 已提交
46 47 48 49 50 51 52 53
} tsMint;

tmr_h mnodeGetTimer() { return tsMint.timer; }

int32_t mnodeGetDnodeId() { return tsMint.dnodeId; }

char *mnodeGetClusterId() { return tsMint.clusterId; }

S
Shengliang Guan 已提交
54
EMnStatus mnodeIsServing() { return tsMint.state; }
S
Shengliang Guan 已提交
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

void mnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg) {
  (*tsMint.fp.SendMsgToDnode)(epSet, rpcMsg);
}

void mnodeSendMsgToMnode(struct SRpcMsg *rpcMsg) { return (*tsMint.fp.SendMsgToMnode)(rpcMsg); }

void mnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell) { (*tsMint.fp.SendRedirectMsg)(rpcMsg, forShell); }

int32_t mnodeGetStatistics(SMnodeStat *stat) { return 0; }

static int32_t mnodeSetPara(SMnodePara para) {
  tsMint.fp = para.fp;
  tsMint.dnodeId = para.dnodeId;
  strncpy(tsMint.clusterId, para.clusterId, TSDB_CLUSTER_ID_LEN);

  if (tsMint.fp.SendMsgToDnode == NULL) return -1;
  if (tsMint.fp.SendMsgToMnode == NULL) return -1;
  if (tsMint.fp.SendRedirectMsg == NULL) return -1;
  if (tsMint.dnodeId < 0) return -1;

  return 0;
}

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

  return 0;
}

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

S
Shengliang Guan 已提交
94 95
static int32_t mnodeInitStep1() {
  struct SSteps *steps = taosStepInit(16, NULL);
S
Shengliang Guan 已提交
96 97
  if (steps == NULL) return -1;

S
Shengliang Guan 已提交
98
  taosStepAdd(steps, "mnode-sdb", sdbInit, sdbCleanup);
S
Shengliang Guan 已提交
99
  taosStepAdd(steps, "mnode-cluster", mnodeInitCluster, mnodeCleanupCluster);
S
Shengliang Guan 已提交
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
  taosStepAdd(steps, "mnode-dnode", mnodeInitDnode, mnodeCleanupDnode);
  taosStepAdd(steps, "mnode-mnode", mnodeInitMnode, mnodeCleanupMnode);
  taosStepAdd(steps, "mnode-acct", mnodeInitAcct, mnodeCleanupAcct);
  taosStepAdd(steps, "mnode-auth", mnodeInitAuth, mnodeCleanupAuth);
  taosStepAdd(steps, "mnode-user", mnodeInitUser, mnodeCleanupUser);
  taosStepAdd(steps, "mnode-db", mnodeInitDb, mnodeCleanupDb);
  taosStepAdd(steps, "mnode-vgroup", mnodeInitVgroup, mnodeCleanupVgroup);
  taosStepAdd(steps, "mnode-stable", mnodeInitStable, mnodeCleanupStable);
  taosStepAdd(steps, "mnode-func", mnodeInitFunc, mnodeCleanupFunc);
  taosStepAdd(steps, "mnode-oper", mnodeInitOper, mnodeCleanupOper);

  tsMint.steps1 = steps;
  return taosStepExec(tsMint.steps1);
}

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

  taosStepAdd(steps, "mnode-timer", mnodeInitTimer, NULL);
  taosStepAdd(steps, "mnode-worker", mnodeInitWorker, NULL);
  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-worker", NULL, mnodeCleanupWorker);
  taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer);

  tsMint.steps2 = steps;
  return taosStepExec(tsMint.steps2);
}
S
Shengliang Guan 已提交
131

S
Shengliang Guan 已提交
132 133 134 135 136 137 138 139 140
static void mnodeCleanupStep1() { taosStepCleanup(tsMint.steps1); }

static void mnodeCleanupStep2() { taosStepCleanup(tsMint.steps2); }

static bool mnodeNeedDeploy() {
  if (tsMint.dnodeId > 0) return false;
  if (tsMint.clusterId[0] != 0) return false;
  if (strcmp(tsFirst, tsLocalEp) != 0) return false;
  return true;
S
Shengliang Guan 已提交
141 142
}

S
Shengliang Guan 已提交
143 144 145 146 147 148 149 150 151 152 153 154
int32_t mnodeDeploy() {
  if (tsMint.state != MN_STATUS_UNINIT) {
    mError("failed to deploy mnode since its deployed");
    return 0;
  } else {
    tsMint.state = MN_STATUS_INIT;
  }

  if (tsMint.dnodeId <= 0 || tsMint.clusterId[0] == 0) {
    mError("failed to deploy mnode since cluster not ready");
    return TSDB_CODE_MND_NOT_READY;
  }
S
Shengliang Guan 已提交
155

S
Shengliang Guan 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  mInfo("starting to deploy mnode");

  int32_t code = mnodeInitStep1();
  if (code != 0) {
    mError("failed to deploy mnode since init step1 error");
    tsMint.state = MN_STATUS_UNINIT;
    return TSDB_CODE_MND_SDB_ERROR;
  }

  code = mnodeInitStep2();
  if (code != 0) {
    mnodeCleanupStep1();
    mError("failed to deploy mnode since init step2 error");
    tsMint.state = MN_STATUS_UNINIT;
    return TSDB_CODE_MND_SDB_ERROR;
  }

  mDebug("mnode is deployed and waiting for raft to confirm");
  tsMint.state = MN_STATUS_READY;
  return 0;
}

void mnodeUnDeploy() {
S
Shengliang Guan 已提交
179
  sdbUnDeploy();
S
Shengliang Guan 已提交
180 181
  mnodeCleanup();
}
S
Shengliang Guan 已提交
182 183

int32_t mnodeInit(SMnodePara para) {
S
Shengliang Guan 已提交
184
  mDebugFlag = 207;
S
Shengliang Guan 已提交
185
  if (tsMint.state != MN_STATUS_UNINIT) {
S
Shengliang Guan 已提交
186 187
    return 0;
  } else {
S
Shengliang Guan 已提交
188
    tsMint.state = MN_STATUS_INIT;
S
Shengliang Guan 已提交
189 190
  }

S
Shengliang Guan 已提交
191 192 193
  mInfo("starting to initialize mnode ...");

  int32_t code = mnodeSetPara(para);
S
Shengliang Guan 已提交
194
  if (code != 0) {
S
Shengliang Guan 已提交
195
    tsMint.state = MN_STATUS_UNINIT;
S
Shengliang Guan 已提交
196 197 198
    return code;
  }

S
Shengliang Guan 已提交
199 200 201 202 203
  code = mnodeInitStep1();
  if (code != 0) {
    tsMint.state = MN_STATUS_UNINIT;
    return -1;
  }
S
Shengliang Guan 已提交
204

S
Shengliang Guan 已提交
205
  code = sdbRead();
S
Shengliang Guan 已提交
206 207
  if (code != 0) {
    if (mnodeNeedDeploy()) {
S
Shengliang Guan 已提交
208
      code = sdbDeploy();
S
Shengliang Guan 已提交
209
      if (code != 0) {
S
Shengliang Guan 已提交
210 211 212
        mnodeCleanupStep1();
        tsMint.state = MN_STATUS_UNINIT;
        return -1;
S
Shengliang Guan 已提交
213 214
      }
    } else {
S
Shengliang Guan 已提交
215 216 217
      mnodeCleanupStep1();
      tsMint.state = MN_STATUS_UNINIT;
      return -1;
S
Shengliang Guan 已提交
218 219 220
    }
  }

S
Shengliang Guan 已提交
221
  code = mnodeInitStep2();
S
Shengliang Guan 已提交
222
  if (code != 0) {
S
Shengliang Guan 已提交
223 224
    mnodeCleanupStep1();
    tsMint.state = MN_STATUS_UNINIT;
S
Shengliang Guan 已提交
225 226 227
    return -1;
  }

S
Shengliang Guan 已提交
228
  tsMint.state = MN_STATUS_READY;
S
Shengliang Guan 已提交
229 230 231 232 233
  mInfo("mnode is initialized successfully");
  return 0;
}

void mnodeCleanup() {
S
Shengliang Guan 已提交
234
  if (tsMint.state != MN_STATUS_UNINIT && tsMint.state != MN_STATUS_CLOSING) {
S
Shengliang Guan 已提交
235
    mInfo("starting to clean up mnode");
S
Shengliang Guan 已提交
236 237 238 239
    tsMint.state = MN_STATUS_CLOSING;

    mnodeCleanupStep2();
    mnodeCleanupStep1();
S
Shengliang Guan 已提交
240

S
Shengliang Guan 已提交
241
    tsMint.state = MN_STATUS_UNINIT;
S
Shengliang Guan 已提交
242 243 244 245 246 247 248
    mInfo("mnode is cleaned up");
  }
}

int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey) { return 0; }

void mnodeProcessMsg(SRpcMsg *rpcMsg) {}