mondeInt.c 7.0 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
  if (tsMint.para.SendMsgToDnode == NULL) return -1;
  if (tsMint.para.SendMsgToMnode == NULL) return -1;
  if (tsMint.para.SendRedirectMsg == NULL) return -1;
S
Shengliang Guan 已提交
73
  if (tsMint.para.PutMsgIntoApplyQueue == NULL) return -1;
S
Shengliang Guan 已提交
74 75
  if (tsMint.para.dnodeId < 0) return -1;
  if (tsMint.para.clusterId < 0) return -1;
S
Shengliang Guan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  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 已提交
95 96
static int32_t mnodeInitStep1() {
  struct SSteps *steps = taosStepInit(16, NULL);
S
Shengliang Guan 已提交
97 98
  if (steps == NULL) return -1;

S
Shengliang Guan 已提交
99
  taosStepAdd(steps, "mnode-sdb", sdbInit, sdbCleanup);
S
Shengliang Guan 已提交
100
  taosStepAdd(steps, "mnode-cluster", mnodeInitCluster, mnodeCleanupCluster);
S
Shengliang Guan 已提交
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
  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);
127
  taosStepAdd(steps, "mnode-telem", mnodeInitTelem, mnodeCleanupTelem);
S
Shengliang Guan 已提交
128 129 130 131 132
  taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer);

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

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

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

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

S
Shengliang Guan 已提交
145
int32_t mnodeDeploy(char *path, SMnodeCfg *pCfg) {
S
Shengliang Guan 已提交
146 147 148 149 150 151 152
  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 已提交
153
  if (tsMint.para.dnodeId <= 0 || tsMint.para.clusterId <= 0) {
S
Shengliang Guan 已提交
154 155 156
    mError("failed to deploy mnode since cluster not ready");
    return TSDB_CODE_MND_NOT_READY;
  }
S
Shengliang Guan 已提交
157

S
Shengliang Guan 已提交
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;
}

S
Shengliang Guan 已提交
180
void mnodeUnDeploy(char *path) {
S
Shengliang Guan 已提交
181
  sdbUnDeploy();
S
Shengliang Guan 已提交
182 183
  mnodeCleanup();
}
S
Shengliang Guan 已提交
184 185

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
248 249
int32_t mnodeStart(char *path, SMnodeCfg *pCfg) { return 0; }
int32_t mnodeAlter(SMnodeCfg *pCfg) { return 0; }
S
Shengliang Guan 已提交
250
void    mnodeStop() {}