mondeInt.c 6.8 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
#include "mnodeUser.h"
#include "mnodeVgroup.h"
#include "mnodeWorker.h"
37
#include "mnodeTelem.h"
S
Shengliang Guan 已提交
38 39

static struct {
S
Shengliang Guan 已提交
40 41 42 43 44 45 46
  int32_t    state;
  int32_t    dnodeId;
  int64_t    clusterId;
  tmr_h      timer;
  SSteps    *steps1;
  SSteps    *steps2;
  SMnodePara para;
S
Shengliang Guan 已提交
47 48 49 50
} tsMint;

tmr_h mnodeGetTimer() { return tsMint.timer; }

S
Shengliang Guan 已提交
51
int32_t mnodeGetDnodeId() { return tsMint.para.dnodeId; }
S
Shengliang Guan 已提交
52

S
Shengliang Guan 已提交
53
int64_t mnodeGetClusterId() { return tsMint.para.clusterId; }
S
Shengliang Guan 已提交
54

S
Shengliang Guan 已提交
55
EMnStatus mnodeGetStatus() { return tsMint.state; }
S
Shengliang Guan 已提交
56

S
Shengliang Guan 已提交
57
void mnodeSendMsgToDnode(struct SEpSet *epSet, struct SRpcMsg *rpcMsg) {
S
Shengliang Guan 已提交
58
  (*tsMint.para.SendMsgToDnode)(epSet, rpcMsg);
S
Shengliang Guan 已提交
59 60
}

S
Shengliang Guan 已提交
61
void mnodeSendMsgToMnode(struct SRpcMsg *rpcMsg) { return (*tsMint.para.SendMsgToMnode)(rpcMsg); }
S
Shengliang Guan 已提交
62

S
Shengliang Guan 已提交
63
void mnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell) { (*tsMint.para.SendRedirectMsg)(rpcMsg, forShell); }
S
Shengliang Guan 已提交
64

S
Shengliang Guan 已提交
65
int32_t mnodeGetLoad(SMnodeLoad *pLoad) { return 0; }
S
Shengliang Guan 已提交
66 67

static int32_t mnodeSetPara(SMnodePara para) {
S
Shengliang Guan 已提交
68
  tsMint.para = para;
S
Shengliang Guan 已提交
69

S
Shengliang Guan 已提交
70 71 72 73 74
  if (tsMint.para.SendMsgToDnode == NULL) return -1;
  if (tsMint.para.SendMsgToMnode == NULL) return -1;
  if (tsMint.para.SendRedirectMsg == NULL) return -1;
  if (tsMint.para.dnodeId < 0) return -1;
  if (tsMint.para.clusterId < 0) return -1;
S
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  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
  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);
126
  taosStepAdd(steps, "mnode-telem", mnodeInitTelem, mnodeCleanupTelem);
S
Shengliang Guan 已提交
127 128 129 130 131
  taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer);

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

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

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

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

S
Shengliang Guan 已提交
144 145 146 147 148 149 150 151
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;
  }

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

S
Shengliang Guan 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  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 已提交
180
  sdbUnDeploy();
S
Shengliang Guan 已提交
181 182
  mnodeCleanup();
}
S
Shengliang Guan 已提交
183 184

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

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

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

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

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

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

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

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

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

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

int32_t mnodeStart() { return 0; }
void    mnodeStop() {}