mnodeMain.c 4.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"
24
#include "mnode.h"
S
slguan 已提交
25
#include "dnode.h"
S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35
#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"
S
Shengliang Guan 已提交
36
#include "mnodeCluster.h"
37
#include "mnodeShow.h"
S
Shengliang Guan 已提交
38 39 40 41 42 43 44
#include "mnodeProfile.h"

typedef struct {
  const char *const name;
  int               (*init)();
  void              (*cleanup)();
} SMnodeComponent;
S
slguan 已提交
45

46
void *tsMnodeTmr = NULL;
S
slguan 已提交
47
static bool tsMgmtIsRunning = false;
S
slguan 已提交
48

S
Shengliang Guan 已提交
49 50
static const SMnodeComponent tsMnodeComponents[] = {
  {"profile", mnodeInitProfile, mnodeCleanupProfile},
S
Shengliang Guan 已提交
51
  {"cluster", mnodeInitCluster, mnodeCleanupCluster},
S
Shengliang Guan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
  {"accts",   mnodeInitAccts,   mnodeCleanupAccts},
  {"users",   mnodeInitUsers,   mnodeCleanupUsers},
  {"dnodes",  mnodeInitDnodes,  mnodeCleanupDnodes},
  {"dbs",     mnodeInitDbs,     mnodeCleanupDbs},
  {"vgroups", mnodeInitVgroups, mnodeCleanupVgroups},
  {"tables",  mnodeInitTables,  mnodeCleanupTables},  
  {"mnodes",  mnodeInitMnodes,  mnodeCleanupMnodes},
  {"sdb",     sdbInit,          sdbCleanUp},
  {"balance", balanceInit,      balanceCleanUp},
  {"grant",   grantInit,        grantCleanUp},
  {"show",    mnodeInitShow,    mnodeCleanUpShow}
};

S
Shengliang Guan 已提交
65 66 67 68
static void mnodeInitTimer();
static void mnodeCleanupTimer();
static bool mnodeNeedStart() ;

S
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void mnodeCleanupComponents(int32_t stepId) {
  for (int32_t i = stepId; i >= 0; i--) {
    tsMnodeComponents[i].cleanup();
  }
}

static int32_t mnodeInitComponents() {
  int32_t code = 0;
  for (int32_t i = 0; i < sizeof(tsMnodeComponents) / sizeof(tsMnodeComponents[0]); i++) {
    if (tsMnodeComponents[i].init() != 0) {
      mnodeCleanupComponents(i);
      code = -1;
      break;
    }
  }
  return code;
}

87
int32_t mnodeStartSystem() {
S
slguan 已提交
88
  if (tsMgmtIsRunning) {
89
    mInfo("mnode module already started...");
S
slguan 已提交
90
    return 0;
91
  }
S
slguan 已提交
92

93
  mInfo("starting to initialize mnode ...");
94 95 96
  if (mkdir(tsMnodeDir, 0755) != 0 && errno != EEXIST) {
    mError("failed to init mnode dir:%s, reason:%s", tsMnodeDir, strerror(errno));
    return -1;
S
slguan 已提交
97 98
  }

S
Shengliang Guan 已提交
99 100 101 102
  dnodeAllocateMnodeWqueue();
  dnodeAllocateMnodeRqueue();
  dnodeAllocateMnodePqueue();

S
Shengliang Guan 已提交
103
  if (mnodeInitComponents() != 0) {
S
slguan 已提交
104 105 106
    return -1;
  }

S
slguan 已提交
107
  grantReset(TSDB_GRANT_ALL, 0);
S
slguan 已提交
108
  tsMgmtIsRunning = true;
S
slguan 已提交
109

110
  mInfo("mnode is initialized successfully");
111 112 113

  sdbUpdateSync();

S
slguan 已提交
114 115
  return 0;
}
S
#1177  
slguan 已提交
116

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

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

S
Shengliang Guan 已提交
129 130 131
  dnodeFreeMnodeWqueue();
  dnodeFreeMnodeRqueue();
  dnodeFreeMnodePqueue();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
132
  mnodeCleanupTimer();
133 134
  mnodeCleanupComponents(sizeof(tsMnodeComponents) / sizeof(tsMnodeComponents[0]) - 1);
  
135
  mInfo("mnode is cleaned up");
136 137
}

S
Shengliang Guan 已提交
138
void mnodeStopSystem() {
S
slguan 已提交
139
  if (sdbIsMaster()) {
140
    mDebug("it is a master mnode, it could not be stopped");
S
slguan 已提交
141
    return;
142
  }
S
Shengliang Guan 已提交
143
  
144
  mnodeCleanupSystem();
145 146

  if (remove(tsMnodeDir) != 0) {
147
    mInfo("failed to remove mnode file, reason:%s", strerror(errno));
148
  } else {
149
    mInfo("mnode file is removed");
150
  }
S
slguan 已提交
151
}
S
Shengliang Guan 已提交
152 153

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

static void mnodeCleanupTimer() {
160 161 162
  if (tsMnodeTmr != NULL) {
    taosTmrCleanUp(tsMnodeTmr);
    tsMnodeTmr = NULL;
S
Shengliang Guan 已提交
163 164 165 166 167
  }
}

static bool mnodeNeedStart() {
  struct stat dirstat;
168 169 170 171
  char mnodeFileName[TSDB_FILENAME_LEN * 2] = {0};
  sprintf(mnodeFileName, "%s/wal0", tsMnodeDir);

  bool fileExist = (stat(mnodeFileName, &dirstat) == 0);
S
Shengliang Guan 已提交
172 173 174
  bool asMaster = (strcmp(tsFirst, tsLocalEp) == 0);

  if (asMaster || fileExist) {
175
    mDebug("mnode module start, asMaster:%d fileExist:%d", asMaster, fileExist);
S
Shengliang Guan 已提交
176
    return true;
177 178 179
  } else {
    mDebug("mnode module won't start, asMaster:%d fileExist:%d", asMaster, fileExist);
    return false;
S
Shengliang Guan 已提交
180 181
  }
}
182 183 184

bool mnodeIsRunning() {
  return tsMgmtIsRunning;
185
}