mnodeMain.c 4.3 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
Shengliang Guan 已提交
36 37 38 39 40 41 42
#include "mnodeProfile.h"

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

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

S
Shengliang Guan 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static const SMnodeComponent tsMnodeComponents[] = {
  {"profile", mnodeInitProfile, mnodeCleanupProfile},
  {"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 已提交
62 63 64 65
static void mnodeInitTimer();
static void mnodeCleanupTimer();
static bool mnodeNeedStart() ;

S
Shengliang Guan 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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;
}

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

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

S
Shengliang Guan 已提交
96 97 98 99
  dnodeAllocateMnodeWqueue();
  dnodeAllocateMnodeRqueue();
  dnodeAllocateMnodePqueue();

S
Shengliang Guan 已提交
100
  if (mnodeInitComponents() != 0) {
S
slguan 已提交
101 102 103
    return -1;
  }

S
slguan 已提交
104
  grantReset(TSDB_GRANT_ALL, 0);
S
slguan 已提交
105
  tsMgmtIsRunning = true;
S
slguan 已提交
106

107
  mInfo("mnode is initialized successfully");
S
slguan 已提交
108 109
  return 0;
}
S
#1177  
slguan 已提交
110

111
int32_t mnodeInitSystem() {
S
Shengliang Guan 已提交
112
  mnodeInitTimer();
113 114
  if (!mnodeNeedStart()) return 0;
  return mnodeStartSystem();
S
slguan 已提交
115
}
116

117
void mnodeCleanupSystem() {
118
  mInfo("starting to clean up mnode");
guanshengliang's avatar
guanshengliang 已提交
119
  tsMgmtIsRunning = false;
S
Shengliang Guan 已提交
120

S
Shengliang Guan 已提交
121 122 123
  dnodeFreeMnodeWqueue();
  dnodeFreeMnodeRqueue();
  dnodeFreeMnodePqueue();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
124
  mnodeCleanupTimer();
125 126
  mnodeCleanupComponents(sizeof(tsMnodeComponents) / sizeof(tsMnodeComponents[0]) - 1);
  
127
  mInfo("mnode is cleaned up");
128 129
}

S
Shengliang Guan 已提交
130
void mnodeStopSystem() {
S
slguan 已提交
131
  if (sdbIsMaster()) {
132
    mDebug("it is a master mnode, it could not be stopped");
S
slguan 已提交
133
    return;
134
  }
S
Shengliang Guan 已提交
135
  
136
  mnodeCleanupSystem();
137 138

  if (remove(tsMnodeDir) != 0) {
139
    mInfo("failed to remove mnode file, reason:%s", strerror(errno));
140
  } else {
141
    mInfo("mnode file is removed");
142
  }
S
slguan 已提交
143
}
S
Shengliang Guan 已提交
144 145

static void mnodeInitTimer() {
S
Shengliang Guan 已提交
146
  if (tsMnodeTmr == NULL) {
147
    tsMnodeTmr = taosTmrInit(tsMaxShellConns, 200, 3600000, "MND");
S
Shengliang Guan 已提交
148 149 150 151
  }
}

static void mnodeCleanupTimer() {
152 153 154
  if (tsMnodeTmr != NULL) {
    taosTmrCleanUp(tsMnodeTmr);
    tsMnodeTmr = NULL;
S
Shengliang Guan 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168
  }
}

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;
}
169 170 171

bool mnodeIsRunning() {
  return tsMgmtIsRunning;
172
}