mnodeMain.c 3.7 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
  }

56
  if (mnodeInitAccts() < 0) {
S
slguan 已提交
57 58 59 60
    mError("failed to init accts");
    return -1;
  }

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

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

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

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

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

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

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

S
slguan 已提交
96 97
  if (balanceInit() < 0) {
    mError("failed to init balance")
S
slguan 已提交
98 99
  }

guanshengliang's avatar
guanshengliang 已提交
100 101 102 103 104
  if (grantInit() < 0) {
    mError("failed to init grant");
    return -1;
  }

105 106
  if (mnodeInitShow() < 0) {
    mError("failed to init show");
S
slguan 已提交
107 108 109
    return -1;
  }

S
slguan 已提交
110
  grantReset(TSDB_GRANT_ALL, 0);
S
slguan 已提交
111
  tsMgmtIsRunning = true;
S
slguan 已提交
112

S
slguan 已提交
113 114 115 116
  mPrint("TDengine mgmt is initialized successfully");

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

118
int32_t mnodeInitSystem() {
S
Shengliang Guan 已提交
119
  mnodeInitTimer();
120 121
  if (!mnodeNeedStart()) return 0;
  return mnodeStartSystem();
S
slguan 已提交
122
}
123

124
void mnodeCleanupSystem() {
125
  mPrint("starting to clean up mgmt");
guanshengliang's avatar
guanshengliang 已提交
126
  tsMgmtIsRunning = false;
S
Shengliang Guan 已提交
127 128

  mnodeCleanupTimer();
129
  mnodeCleanUpShow();
guanshengliang's avatar
guanshengliang 已提交
130 131 132 133
  grantCleanUp();
  balanceCleanUp();
  sdbCleanUp();
  mgmtCleanupMnodes();
134 135 136
  mnodeCleanupTables();
  mnodeCleanupVgroups();
  mnodeCleanupDbs();
S
slguan 已提交
137
  mgmtCleanupDnodes();
138 139
  mnodeCleanupUsers();
  mnodeCleanupAccts();
140 141 142
  mPrint("mgmt is cleaned up");
}

S
slguan 已提交
143
void mgmtStopSystem() {
S
slguan 已提交
144
  if (sdbIsMaster()) {
S
slguan 已提交
145 146
    mTrace("it is a master mgmt node, it could not be stopped");
    return;
147
  }
S
Shengliang Guan 已提交
148
  
149
  mnodeCleanupSystem();
S
slguan 已提交
150 151 152
  mPrint("mgmt file is removed");
  remove(tsMnodeDir);
}
S
Shengliang Guan 已提交
153 154

static void mnodeInitTimer() {
155 156
  if (tsMnodeTmr != NULL) {
    tsMnodeTmr = taosTmrInit((tsMaxShellConns)*3, 200, 3600000, "MND");
S
Shengliang Guan 已提交
157 158 159 160
  }
}

static void mnodeCleanupTimer() {
161 162 163
  if (tsMnodeTmr != NULL) {
    taosTmrCleanUp(tsMnodeTmr);
    tsMnodeTmr = NULL;
S
Shengliang Guan 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
  }
}

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;
}
178 179 180 181

bool mnodeIsRunning() {
  return tsMgmtIsRunning;
}