dnode.c 6.9 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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
99 100 101 102 103 104
  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;
  }

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

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

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

S
Shengliang Guan 已提交
123 124 125 126 127 128 129 130 131 132 133 134
  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 已提交
135
  memcpy(&pDnode->opt, pOption, sizeof(SDnodeOpt));
S
Shengliang Guan 已提交
136 137 138
  return 0;
}

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

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

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

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

160
  SDnode *pDnode = calloc(1, sizeof(SDnode));
S
Shengliang Guan 已提交
161 162 163 164 165 166 167
  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 已提交
168
  dndSetStat(pDnode, DND_STAT_INIT);
S
Shengliang Guan 已提交
169

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

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

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

S
Shengliang Guan 已提交
188
  if (vnodeInit(pDnode->opt.numOfCommitThreads) != 0) {
189 190 191 192 193
    dError("failed to init vnode env");
    dndCleanup(pDnode);
    return NULL;
  }

S
Shengliang Guan 已提交
194
  if (dndInitDnode(pDnode) != 0) {
S
Shengliang Guan 已提交
195
    dError("failed to init dnode");
S
Shengliang Guan 已提交
196
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
197 198 199
    return NULL;
  }

S
Shengliang Guan 已提交
200
  if (dndInitVnodes(pDnode) != 0) {
S
Shengliang Guan 已提交
201
    dError("failed to init vnodes");
S
Shengliang Guan 已提交
202
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
203 204 205
    return NULL;
  }

S
Shengliang Guan 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  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 已提交
224
  if (dndInitMnode(pDnode) != 0) {
S
Shengliang Guan 已提交
225
    dError("failed to init mnode");
S
Shengliang Guan 已提交
226
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
227 228 229
    return NULL;
  }

S
Shengliang Guan 已提交
230
  if (dndInitTrans(pDnode) != 0) {
S
Shengliang Guan 已提交
231
    dError("failed to init transport");
S
Shengliang Guan 已提交
232
    dndCleanup(pDnode);
S
Shengliang Guan 已提交
233 234 235
    return NULL;
  }

S
Shengliang Guan 已提交
236
  dndSetStat(pDnode, DND_STAT_RUNNING);
S
Shengliang Guan 已提交
237
  dndSendStatusReq(pDnode);
S
Shengliang Guan 已提交
238
  dndReportStartup(pDnode, "TDengine", "initialized successfully");
S
Shengliang Guan 已提交
239
  dInfo("TDengine is initialized successfully, pDnode:%p", pDnode);
S
Shengliang Guan 已提交
240

241
  return pDnode;
S
Shengliang Guan 已提交
242 243
}

S
Shengliang Guan 已提交
244
void dndCleanup(SDnode *pDnode) {
S
Shengliang Guan 已提交
245 246
  if (pDnode == NULL) return;

S
Shengliang Guan 已提交
247
  if (dndGetStat(pDnode) == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
248 249 250 251 252
    dError("dnode is shutting down");
    return;
  }

  dInfo("start to cleanup TDengine");
S
Shengliang Guan 已提交
253 254 255
  dndSetStat(pDnode, DND_STAT_STOPPED);
  dndCleanupTrans(pDnode);
  dndCleanupMnode(pDnode);
S
Shengliang Guan 已提交
256 257 258
  dndCleanupBnode(pDnode);
  dndCleanupSnode(pDnode);
  dndCleanupQnode(pDnode);
S
Shengliang Guan 已提交
259 260
  dndCleanupVnodes(pDnode);
  dndCleanupDnode(pDnode);
261
  vnodeClear();
S
Shengliang Guan 已提交
262 263
  walCleanUp();
  rpcCleanup();
264

S
Shengliang Guan 已提交
265
  dndCleanupEnv(pDnode);
S
Shengliang Guan 已提交
266
  free(pDnode);
S
Shengliang Guan 已提交
267
  dInfo("TDengine is cleaned up successfully");
S
Shengliang Guan 已提交
268
}