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

S
TD-1671  
Shengliang Guan 已提交
112
  sdbUpdateSync(NULL);
113

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() {
S
TD-1710  
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134 135 136 137
  if (tsMgmtIsRunning) {
    mInfo("starting to clean up mnode");
    tsMgmtIsRunning = false;

    dnodeFreeMnodeWqueue();
    dnodeFreeMnodeRqueue();
    dnodeFreeMnodePqueue();
    mnodeCleanupTimer();
    mnodeCleanupComponents(sizeof(tsMnodeComponents) / sizeof(tsMnodeComponents[0]) - 1);

    mInfo("mnode is cleaned up");
  }
138 139
}

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

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

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

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

static bool mnodeNeedStart() {
  struct stat dirstat;
170
  char mnodeFileName[TSDB_FILENAME_LEN * 2] = {0};
S
TD-1257  
Shengliang Guan 已提交
171
  sprintf(mnodeFileName, "%s/wal/wal0", tsMnodeDir);
172 173

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

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

bool mnodeIsRunning() {
  return tsMgmtIsRunning;
187
}