mnodeMain.c 3.9 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
17
#include "os.h"
S
slguan 已提交
18 19
#include "taosdef.h"
#include "tsched.h"
S
slguan 已提交
20
#include "tbalance.h"
S
slguan 已提交
21
#include "tgrant.h"
S
slguan 已提交
22
#include "ttimer.h"
S
slguan 已提交
23
#include "tglobal.h"
S
slguan 已提交
24
#include "dnode.h"
S
Shengliang Guan 已提交
25 26 27 28 29 30 31 32 33 34
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeAcct.h"
#include "mnodeDnode.h"
#include "mnodeMnode.h"
#include "mnodeDb.h"
#include "mnodeSdb.h"
#include "mnodeVgroup.h"
#include "mnodeUser.h"
#include "mnodeTable.h"
35
#include "mnodeShow.h"
S
slguan 已提交
36

37
void *tsMnodeTmr;
S
slguan 已提交
38
static bool tsMgmtIsRunning = false;
S
slguan 已提交
39

S
Shengliang Guan 已提交
40 41 42 43
static void mnodeInitTimer();
static void mnodeCleanupTimer();
static bool mnodeNeedStart() ;

44
int32_t mnodeStartSystem() {
S
slguan 已提交
45 46 47
  if (tsMgmtIsRunning) {
    mPrint("TDengine mgmt module already started...");
    return 0;
48
  }
S
slguan 已提交
49

S
slguan 已提交
50 51
  mPrint("starting to initialize TDengine mgmt ...");
  struct stat dirstat;
S
slguan 已提交
52 53
  if (stat(tsMnodeDir, &dirstat) < 0) {
    mkdir(tsMnodeDir, 0755);
S
slguan 已提交
54 55
  }

S
Shengliang Guan 已提交
56 57 58 59
  dnodeAllocateMnodeWqueue();
  dnodeAllocateMnodeRqueue();
  dnodeAllocateMnodePqueue();

60
  if (mnodeInitAccts() < 0) {
S
slguan 已提交
61 62 63 64
    mError("failed to init accts");
    return -1;
  }

65
  if (mnodeInitUsers() < 0) {
S
slguan 已提交
66 67 68 69
    mError("failed to init users");
    return -1;
  }

70
  if (mnodeInitDnodes() < 0) {
S
slguan 已提交
71 72 73 74
    mError("failed to init dnodes");
    return -1;
  }

75
  if (mnodeInitDbs() < 0) {
S
slguan 已提交
76 77 78 79
    mError("failed to init dbs");
    return -1;
  }

80
  if (mnodeInitVgroups() < 0) {
S
slguan 已提交
81 82 83 84
    mError("failed to init vgroups");
    return -1;
  }

85
  if (mnodeInitTables() < 0) {
S
slguan 已提交
86 87 88 89
    mError("failed to init tables");
    return -1;
  }

90
  if (mnodeInitMnodes() < 0) {
S
slguan 已提交
91
    mError("failed to init mnodes");
S
slguan 已提交
92 93 94
    return -1;
  }

S
slguan 已提交
95 96
  if (sdbInit() < 0) {
    mError("failed to init sdb");
S
slguan 已提交
97 98 99
    return -1;
  }

S
slguan 已提交
100 101
  if (balanceInit() < 0) {
    mError("failed to init balance")
S
slguan 已提交
102 103
  }

guanshengliang's avatar
guanshengliang 已提交
104 105 106 107 108
  if (grantInit() < 0) {
    mError("failed to init grant");
    return -1;
  }

109 110
  if (mnodeInitShow() < 0) {
    mError("failed to init show");
S
slguan 已提交
111 112 113
    return -1;
  }

S
slguan 已提交
114
  grantReset(TSDB_GRANT_ALL, 0);
S
slguan 已提交
115
  tsMgmtIsRunning = true;
S
slguan 已提交
116

S
slguan 已提交
117 118 119 120
  mPrint("TDengine mgmt is initialized successfully");

  return 0;
}
S
#1177  
slguan 已提交
121

122
int32_t mnodeInitSystem() {
S
Shengliang Guan 已提交
123
  mnodeInitTimer();
124 125
  if (!mnodeNeedStart()) return 0;
  return mnodeStartSystem();
S
slguan 已提交
126
}
127

128
void mnodeCleanupSystem() {
129
  mPrint("starting to clean up mgmt");
guanshengliang's avatar
guanshengliang 已提交
130
  tsMgmtIsRunning = false;
S
Shengliang Guan 已提交
131

S
Shengliang Guan 已提交
132 133 134
  dnodeFreeMnodeWqueue();
  dnodeFreeMnodeRqueue();
  dnodeFreeMnodePqueue();
S
Shengliang Guan 已提交
135
  mnodeCleanupTimer();
136
  mnodeCleanUpShow();
guanshengliang's avatar
guanshengliang 已提交
137 138 139 140
  grantCleanUp();
  balanceCleanUp();
  sdbCleanUp();
  mgmtCleanupMnodes();
141 142 143
  mnodeCleanupTables();
  mnodeCleanupVgroups();
  mnodeCleanupDbs();
S
slguan 已提交
144
  mgmtCleanupDnodes();
145 146
  mnodeCleanupUsers();
  mnodeCleanupAccts();
147 148 149
  mPrint("mgmt is cleaned up");
}

S
slguan 已提交
150
void mgmtStopSystem() {
S
slguan 已提交
151
  if (sdbIsMaster()) {
S
slguan 已提交
152 153
    mTrace("it is a master mgmt node, it could not be stopped");
    return;
154
  }
S
Shengliang Guan 已提交
155
  
156
  mnodeCleanupSystem();
S
slguan 已提交
157 158 159
  mPrint("mgmt file is removed");
  remove(tsMnodeDir);
}
S
Shengliang Guan 已提交
160 161

static void mnodeInitTimer() {
S
Shengliang Guan 已提交
162
  if (tsMnodeTmr == NULL) {
163
    tsMnodeTmr = taosTmrInit((tsMaxShellConns)*3, 200, 3600000, "MND");
S
Shengliang Guan 已提交
164 165 166 167
  }
}

static void mnodeCleanupTimer() {
168 169 170
  if (tsMnodeTmr != NULL) {
    taosTmrCleanUp(tsMnodeTmr);
    tsMnodeTmr = NULL;
S
Shengliang Guan 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
  }
}

static bool mnodeNeedStart() {
  struct stat dirstat;
  bool fileExist = (stat(tsMnodeDir, &dirstat) == 0);
  bool asMaster = (strcmp(tsFirst, tsLocalEp) == 0);

  if (asMaster || fileExist) {
    return true;
  }

  return false;
}
185 186 187 188

bool mnodeIsRunning() {
  return tsMgmtIsRunning;
}