dnode.c 7.1 KB
Newer Older
S
Shengliang Guan 已提交
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
S
Shengliang Guan 已提交
17
#include "dndBnode.h"
S
Shengliang Guan 已提交
18
#include "dndMgmt.h"
S
Shengliang Guan 已提交
19
#include "dndMnode.h"
S
Shengliang Guan 已提交
20 21
#include "dndQnode.h"
#include "dndSnode.h"
S
Shengliang Guan 已提交
22 23
#include "dndTransport.h"
#include "dndVnodes.h"
S
Shengliang Guan 已提交
24
#include "sync.h"
S
Shengliang Guan 已提交
25
#include "wal.h"
S
Shengliang Guan 已提交
26
#include "tfs.h"
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
EStat dndGetStat(SDnode *pDnode) { return pDnode->stat; }
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30
void dndSetStat(SDnode *pDnode, EStat stat) {
S
Shengliang Guan 已提交
31
  dDebug("dnode status set from %s to %s", dndStatStr(pDnode->stat), dndStatStr(stat));
S
Shengliang Guan 已提交
32 33 34
  pDnode->stat = stat;
}

S
Shengliang Guan 已提交
35
char *dndStatStr(EStat stat) {
S
Shengliang Guan 已提交
36
  switch (stat) {
S
Shengliang Guan 已提交
37
    case DND_STAT_INIT:
S
Shengliang Guan 已提交
38
      return "init";
S
Shengliang Guan 已提交
39
    case DND_STAT_RUNNING:
S
Shengliang Guan 已提交
40
      return "running";
S
Shengliang Guan 已提交
41
    case DND_STAT_STOPPED:
S
Shengliang Guan 已提交
42 43 44 45 46 47
      return "stopped";
    default:
      return "unknown";
  }
}

S
Shengliang Guan 已提交
48
void dndReportStartup(SDnode *pDnode, char *pName, char *pDesc) {
S
Shengliang Guan 已提交
49
  SStartupMsg *pStartup = &pDnode->startup;
S
Shengliang Guan 已提交
50 51
  tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN);
  tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN);
S
Shengliang Guan 已提交
52 53 54
  pStartup->finished = 0;
}

S
Shengliang Guan 已提交
55 56 57
void dndGetStartup(SDnode *pDnode, SStartupMsg *pStartup) {
  memcpy(pStartup, &pDnode->startup, sizeof(SStartupMsg));
  pStartup->finished = (dndGetStat(pDnode) == DND_STAT_RUNNING);
S
Shengliang Guan 已提交
58 59
}

S
Shengliang Guan 已提交
60
static FileFd dndCheckRunning(char *dataDir) {
S
Shengliang Guan 已提交
61 62 63 64 65 66
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s/.running", dataDir);

  FileFd fd = taosOpenFileCreateWriteTrunc(filepath);
  if (fd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
67
    dError("failed to lock file:%s since %s, quit", filepath, terrstr());
S
Shengliang Guan 已提交
68 69 70 71 72 73
    return -1;
  }

  int32_t ret = taosLockFile(fd);
  if (ret != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
74
    dError("failed to lock file:%s since %s, quit", filepath, terrstr());
S
Shengliang Guan 已提交
75 76 77 78
    taosCloseFile(fd);
    return -1;
  }

S
Shengliang Guan 已提交
79
  return fd;
S
Shengliang Guan 已提交
80 81
}

S
Shengliang Guan 已提交
82
static int32_t dndInitEnv(SDnode *pDnode, SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
83 84
  pDnode->lockFd = dndCheckRunning(pOption->dataDir);
  if (pDnode->lockFd < 0) {
S
Shengliang Guan 已提交
85 86 87 88
    return -1;
  }

  char path[PATH_MAX + 100];
S
Shengliang Guan 已提交
89
  snprintf(path, sizeof(path), "%s%smnode", pOption->dataDir, TD_DIRSEP);
90
  pDnode->dir.mnode = tstrdup(path);
S
Shengliang Guan 已提交
91
  snprintf(path, sizeof(path), "%s%svnode", pOption->dataDir, TD_DIRSEP);
92
  pDnode->dir.vnodes = tstrdup(path);
S
Shengliang Guan 已提交
93
  snprintf(path, sizeof(path), "%s%sdnode", pOption->dataDir, TD_DIRSEP);
94
  pDnode->dir.dnode = tstrdup(path);
S
Shengliang Guan 已提交
95
  snprintf(path, sizeof(path), "%s%ssnode", pOption->dataDir, TD_DIRSEP);
S
Shengliang Guan 已提交
96
  pDnode->dir.snode = tstrdup(path);
S
Shengliang Guan 已提交
97
  snprintf(path, sizeof(path), "%s%sbnode", pOption->dataDir, TD_DIRSEP);
S
Shengliang Guan 已提交
98
  pDnode->dir.bnode = tstrdup(path);
S
Shengliang Guan 已提交
99

S
Shengliang Guan 已提交
100 101 102 103 104 105
  if (pDnode->dir.mnode == NULL || pDnode->dir.vnodes == NULL || pDnode->dir.dnode == NULL) {
    dError("failed to malloc dir object");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

106
  if (taosMkDir(pDnode->dir.dnode) != 0) {
S
Shengliang Guan 已提交
107 108 109 110 111
    dError("failed to create dir:%s since %s", pDnode->dir.dnode, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

112
  if (taosMkDir(pDnode->dir.mnode) != 0) {
S
Shengliang Guan 已提交
113 114 115 116 117
    dError("failed to create dir:%s since %s", pDnode->dir.mnode, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

118
  if (taosMkDir(pDnode->dir.vnodes) != 0) {
S
Shengliang Guan 已提交
119 120 121 122 123
    dError("failed to create dir:%s since %s", pDnode->dir.vnodes, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

S
Shengliang Guan 已提交
124 125 126 127 128 129 130 131 132 133 134 135
  if (taosMkDir(pDnode->dir.snode) != 0) {
    dError("failed to create dir:%s since %s", pDnode->dir.snode, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (taosMkDir(pDnode->dir.bnode) != 0) {
    dError("failed to create dir:%s since %s", pDnode->dir.bnode, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

S
Shengliang Guan 已提交
136
  memcpy(&pDnode->opt, pOption, sizeof(SDnodeOpt));
S
Shengliang Guan 已提交
137 138 139
  return 0;
}

S
Shengliang Guan 已提交
140
static void dndCleanupEnv(SDnode *pDnode) {
S
Shengliang Guan 已提交
141 142 143 144 145
  tfree(pDnode->dir.mnode);
  tfree(pDnode->dir.vnodes);
  tfree(pDnode->dir.dnode);
  tfree(pDnode->dir.snode);
  tfree(pDnode->dir.bnode);
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148 149 150 151 152
  if (pDnode->lockFd >= 0) {
    taosUnLockFile(pDnode->lockFd);
    taosCloseFile(pDnode->lockFd);
    pDnode->lockFd = 0;
  }

S
Shengliang Guan 已提交
153 154 155
  taosStopCacheRefreshWorker();
}

S
Shengliang Guan 已提交
156
SDnode *dndInit(SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
157 158 159 160
  taosIgnSIGPIPE();
  taosBlockSIGPIPE();
  taosResolveCRC();

161
  SDnode *pDnode = calloc(1, sizeof(SDnode));
S
Shengliang Guan 已提交
162 163 164 165 166 167 168
  if (pDnode == NULL) {
    dError("failed to create dnode object");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  dInfo("start to initialize TDengine");
S
Shengliang Guan 已提交
169
  dndSetStat(pDnode, DND_STAT_INIT);
S
Shengliang Guan 已提交
170

S
Shengliang Guan 已提交
171
  if (dndInitEnv(pDnode, pOption) != 0) {
S
Shengliang Guan 已提交
172
    dError("failed to init env");
S
Shengliang Guan 已提交
173
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
174 175 176 177 178
    return NULL;
  }

  if (rpcInit() != 0) {
    dError("failed to init rpc env");
S
Shengliang Guan 已提交
179
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
180 181 182 183 184
    return NULL;
  }

  if (walInit() != 0) {
    dError("failed to init wal env");
S
Shengliang Guan 已提交
185
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
186 187 188
    return NULL;
  }

S
Shengliang Guan 已提交
189 190 191 192 193 194 195 196 197 198
  SDiskCfg dCfg;
  strcpy(dCfg.dir, pDnode->opt.dataDir);
  dCfg.level = 0;
  dCfg.primary = 1;
  if (tfsInit(&dCfg, 1) != 0) {
    dError("failed to init tfs env");
    dndCleanup(pDnode);
    return NULL;
  }

S
Shengliang Guan 已提交
199
  if (vnodeInit(pDnode->opt.numOfCommitThreads) != 0) {
200 201 202 203 204
    dError("failed to init vnode env");
    dndCleanup(pDnode);
    return NULL;
  }

S
Shengliang Guan 已提交
205
  if (dndInitDnode(pDnode) != 0) {
S
Shengliang Guan 已提交
206
    dError("failed to init dnode");
S
Shengliang Guan 已提交
207
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
208 209 210
    return NULL;
  }

S
Shengliang Guan 已提交
211
  if (dndInitVnodes(pDnode) != 0) {
S
Shengliang Guan 已提交
212
    dError("failed to init vnodes");
S
Shengliang Guan 已提交
213
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
214 215 216
    return NULL;
  }

S
Shengliang Guan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  if (dndInitQnode(pDnode) != 0) {
    dError("failed to init qnode");
    dndCleanup(pDnode);
    return NULL;
  }

  if (dndInitSnode(pDnode) != 0) {
    dError("failed to init snode");
    dndCleanup(pDnode);
    return NULL;
  }

  if (dndInitBnode(pDnode) != 0) {
    dError("failed to init bnode");
    dndCleanup(pDnode);
    return NULL;
  }

S
Shengliang Guan 已提交
235
  if (dndInitMnode(pDnode) != 0) {
S
Shengliang Guan 已提交
236
    dError("failed to init mnode");
S
Shengliang Guan 已提交
237
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
238 239 240
    return NULL;
  }

S
Shengliang Guan 已提交
241
  if (dndInitTrans(pDnode) != 0) {
S
Shengliang Guan 已提交
242
    dError("failed to init transport");
S
Shengliang Guan 已提交
243
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
244 245 246
    return NULL;
  }

S
Shengliang Guan 已提交
247
  dndSetStat(pDnode, DND_STAT_RUNNING);
S
Shengliang Guan 已提交
248
  dndSendStatusReq(pDnode);
S
Shengliang Guan 已提交
249
  dndReportStartup(pDnode, "TDengine", "initialized successfully");
S
Shengliang Guan 已提交
250
  dInfo("TDengine is initialized successfully, pDnode:%p", pDnode);
S
Shengliang Guan 已提交
251

252
  return pDnode;
S
Shengliang Guan 已提交
253 254
}

S
Shengliang Guan 已提交
255
void dndCleanup(SDnode *pDnode) {
S
Shengliang Guan 已提交
256 257
  if (pDnode == NULL) return;

S
Shengliang Guan 已提交
258
  if (dndGetStat(pDnode) == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
259 260 261 262 263
    dError("dnode is shutting down");
    return;
  }

  dInfo("start to cleanup TDengine");
S
Shengliang Guan 已提交
264 265 266
  dndSetStat(pDnode, DND_STAT_STOPPED);
  dndCleanupTrans(pDnode);
  dndCleanupMnode(pDnode);
S
Shengliang Guan 已提交
267 268 269
  dndCleanupBnode(pDnode);
  dndCleanupSnode(pDnode);
  dndCleanupQnode(pDnode);
S
Shengliang Guan 已提交
270 271
  dndCleanupVnodes(pDnode);
  dndCleanupDnode(pDnode);
272
  vnodeClear();
S
Shengliang Guan 已提交
273
  tfsDestroy();
S
Shengliang Guan 已提交
274 275
  walCleanUp();
  rpcCleanup();
276

S
Shengliang Guan 已提交
277
  dndCleanupEnv(pDnode);
S
Shengliang Guan 已提交
278
  free(pDnode);
S
Shengliang Guan 已提交
279
  dInfo("TDengine is cleaned up successfully");
S
Shengliang Guan 已提交
280
}