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 40 41

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

tmr_h mnodeGetTimer() { return tsMint.timer; }

int32_t mnodeGetDnodeId() { return tsMint.dnodeId; }

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

S
Shengliang Guan 已提交
55
EMnStatus mnodeGetStatus() { return tsMint.state; }
S
Shengliang Guan 已提交
56 57 58 59 60 61 62 63 64

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); }

S
Shengliang Guan 已提交
65 66 67 68
void mnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port) {
  (*tsMint.fp.GetDnodeEp)(dnodeId, ep, fqdn, port);
}

S
Shengliang Guan 已提交
69 70 71 72 73
int32_t mnodeGetStatistics(SMnodeStat *stat) { return 0; }

static int32_t mnodeSetPara(SMnodePara para) {
  tsMint.fp = para.fp;
  tsMint.dnodeId = para.dnodeId;
S
Shengliang Guan 已提交
74
  tsMint.clusterId = para.clusterId;
S
Shengliang Guan 已提交
75 76 77 78

  if (tsMint.fp.SendMsgToDnode == NULL) return -1;
  if (tsMint.fp.SendMsgToMnode == NULL) return -1;
  if (tsMint.fp.SendRedirectMsg == NULL) return -1;
S
Shengliang Guan 已提交
79
  if (tsMint.fp.GetDnodeEp == NULL) return -1;
S
Shengliang Guan 已提交
80
  if (tsMint.dnodeId < 0) return -1;
S
Shengliang Guan 已提交
81
  if (tsMint.clusterId < 0) return -1;
S
Shengliang Guan 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

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

S
Shengliang Guan 已提交
105
  taosStepAdd(steps, "mnode-sdb", sdbInit, sdbCleanup);
S
Shengliang Guan 已提交
106
  taosStepAdd(steps, "mnode-cluster", mnodeInitCluster, mnodeCleanupCluster);
S
Shengliang Guan 已提交
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
  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);
133
  taosStepAdd(steps, "mnode-telem", mnodeInitTelem, mnodeCleanupTelem);
S
Shengliang Guan 已提交
134 135 136 137 138
  taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer);

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

S
Shengliang Guan 已提交
140 141 142 143 144 145
static void mnodeCleanupStep1() { taosStepCleanup(tsMint.steps1); }

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

static bool mnodeNeedDeploy() {
  if (tsMint.dnodeId > 0) return false;
S
Shengliang Guan 已提交
146
  if (tsMint.clusterId > 0) return false;
S
Shengliang Guan 已提交
147 148
  if (strcmp(tsFirst, tsLocalEp) != 0) return false;
  return true;
S
Shengliang Guan 已提交
149 150
}

S
Shengliang Guan 已提交
151 152 153 154 155 156 157 158
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 已提交
159
  if (tsMint.dnodeId <= 0 || tsMint.clusterId <= 0) {
S
Shengliang Guan 已提交
160 161 162
    mError("failed to deploy mnode since cluster not ready");
    return TSDB_CODE_MND_NOT_READY;
  }
S
Shengliang Guan 已提交
163

S
Shengliang Guan 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  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 已提交
187
  sdbUnDeploy();
S
Shengliang Guan 已提交
188 189
  mnodeCleanup();
}
S
Shengliang Guan 已提交
190 191

int32_t mnodeInit(SMnodePara para) {
S
Shengliang Guan 已提交
192
  mDebugFlag = 207;
S
Shengliang Guan 已提交
193
  if (tsMint.state != MN_STATUS_UNINIT) {
S
Shengliang Guan 已提交
194 195
    return 0;
  } else {
S
Shengliang Guan 已提交
196
    tsMint.state = MN_STATUS_INIT;
S
Shengliang Guan 已提交
197 198
  }

S
Shengliang Guan 已提交
199 200 201
  mInfo("starting to initialize mnode ...");

  int32_t code = mnodeSetPara(para);
S
Shengliang Guan 已提交
202
  if (code != 0) {
S
Shengliang Guan 已提交
203
    tsMint.state = MN_STATUS_UNINIT;
S
Shengliang Guan 已提交
204 205 206
    return code;
  }

S
Shengliang Guan 已提交
207 208 209 210 211
  code = mnodeInitStep1();
  if (code != 0) {
    tsMint.state = MN_STATUS_UNINIT;
    return -1;
  }
S
Shengliang Guan 已提交
212

S
Shengliang Guan 已提交
213
  code = sdbRead();
S
Shengliang Guan 已提交
214 215
  if (code != 0) {
    if (mnodeNeedDeploy()) {
S
Shengliang Guan 已提交
216
      code = sdbDeploy();
S
Shengliang Guan 已提交
217
      if (code != 0) {
S
Shengliang Guan 已提交
218 219 220
        mnodeCleanupStep1();
        tsMint.state = MN_STATUS_UNINIT;
        return -1;
S
Shengliang Guan 已提交
221 222
      }
    } else {
S
Shengliang Guan 已提交
223 224 225
      mnodeCleanupStep1();
      tsMint.state = MN_STATUS_UNINIT;
      return -1;
S
Shengliang Guan 已提交
226 227 228
    }
  }

S
Shengliang Guan 已提交
229
  code = mnodeInitStep2();
S
Shengliang Guan 已提交
230
  if (code != 0) {
S
Shengliang Guan 已提交
231 232
    mnodeCleanupStep1();
    tsMint.state = MN_STATUS_UNINIT;
S
Shengliang Guan 已提交
233 234 235
    return -1;
  }

S
Shengliang Guan 已提交
236
  tsMint.state = MN_STATUS_READY;
S
Shengliang Guan 已提交
237 238 239 240 241
  mInfo("mnode is initialized successfully");
  return 0;
}

void mnodeCleanup() {
S
Shengliang Guan 已提交
242
  if (tsMint.state != MN_STATUS_UNINIT && tsMint.state != MN_STATUS_CLOSING) {
S
Shengliang Guan 已提交
243
    mInfo("starting to clean up mnode");
S
Shengliang Guan 已提交
244 245 246 247
    tsMint.state = MN_STATUS_CLOSING;

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

S
Shengliang Guan 已提交
249
    tsMint.state = MN_STATUS_UNINIT;
S
Shengliang Guan 已提交
250 251 252
    mInfo("mnode is cleaned up");
  }
}