dnodeInt.c 4.7 KB
Newer Older
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 "dnodeDnode.h"
S
Shengliang Guan 已提交
18
#include "dnodeMnode.h"
S
Shengliang Guan 已提交
19
#include "dnodeTransport.h"
S
Shengliang Guan 已提交
20
#include "dnodeVnodes.h"
S
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29 30 31
#include "sync.h"
#include "tcache.h"
#include "tconfig.h"
#include "tnote.h"
#include "tstep.h"
#include "wal.h"

static struct {
  EDnStat      runStatus;
  SStartupStep startup;
  SSteps      *steps;
S
Shengliang Guan 已提交
32
} tsInt;
S
Shengliang Guan 已提交
33

S
Shengliang Guan 已提交
34
EDnStat dnodeGetRunStat() { return tsInt.runStatus; }
S
Shengliang Guan 已提交
35

S
Shengliang Guan 已提交
36
void dnodeSetRunStat(EDnStat stat) { tsInt.runStatus = stat; }
S
Shengliang Guan 已提交
37

S
Shengliang Guan 已提交
38
static void dnodeReportStartup(char *name, char *desc) {
S
Shengliang Guan 已提交
39
  SStartupStep *startup = &tsInt.startup;
S
Shengliang Guan 已提交
40 41 42 43 44 45
  tstrncpy(startup->name, name, strlen(startup->name));
  tstrncpy(startup->desc, desc, strlen(startup->desc));
  startup->finished = 0;
}

static void dnodeReportStartupFinished(char *name, char *desc) {
S
Shengliang Guan 已提交
46
  SStartupStep *startup = &tsInt.startup;
S
Shengliang Guan 已提交
47 48 49 50
  tstrncpy(startup->name, name, strlen(startup->name));
  tstrncpy(startup->desc, desc, strlen(startup->desc));
  startup->finished = 1;
}
51

S
Shengliang Guan 已提交
52
void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsInt.startup, sizeof(SStartupStep)); }
53

S
Shengliang Guan 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static int32_t dnodeCheckRunning(char *dir) {
  char filepath[256] = {0};
  snprintf(filepath, sizeof(filepath), "%s/.running", dir);

  FileFd fd = taosOpenFileCreateWriteTrunc(filepath);
  if (fd < 0) {
    dError("failed to open lock file:%s since %s, quit", filepath, strerror(errno));
    return -1;
  }

  int32_t ret = taosLockFile(fd);
  if (ret != 0) {
    dError("failed to lock file:%s since %s, quit", filepath, strerror(errno));
    taosCloseFile(fd);
    return -1;
  }

  return 0;
}

static int32_t dnodeInitDir() {
  sprintf(tsMnodeDir, "%s/mnode", tsDataDir);
  sprintf(tsVnodeDir, "%s/vnode", tsDataDir);
  sprintf(tsDnodeDir, "%s/dnode", tsDataDir);

  if (!taosMkDir(tsDnodeDir)) {
    dError("failed to create dir:%s since %s", tsDnodeDir, strerror(errno));
    return -1;
  }

  if (!taosMkDir(tsMnodeDir)) {
    dError("failed to create dir:%s since %s", tsMnodeDir, strerror(errno));
    return -1;
  }

  if (!taosMkDir(tsVnodeDir)) {
    dError("failed to create dir:%s since %s", tsVnodeDir, strerror(errno));
    return -1;
  }

  if (dnodeCheckRunning(tsDnodeDir) != 0) {
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static int32_t dnodeInitMain() {
  tsInt.runStatus = DN_RUN_STAT_STOPPED;
  tscEmbedded = 1;
  taosIgnSIGPIPE();
  taosBlockSIGPIPE();
  taosResolveCRC();
  taosInitGlobalCfg();
  taosReadGlobalLogCfg();
  taosSetCoreDump(tsEnableCoreFile);

  if (!taosMkDir(tsLogDir)) {
    printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno));
    return -1;
  }

  char temp[TSDB_FILENAME_LEN];
  sprintf(temp, "%s/taosdlog", tsLogDir);
  if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) {
    printf("failed to init log file\n");
  }

  if (!taosReadGlobalCfg()) {
    taosPrintGlobalCfg();
    dError("TDengine read global config failed");
    return -1;
  }

  dInfo("start to initialize TDengine");

  taosInitNotes();

  if (taosCheckGlobalCfg() != 0) {
    return -1;
  }

  dnodeInitDir();

  return -1;
}

static void dnodeCleanupMain() {
  taos_cleanup();
  taosCloseLog();
  taosStopCacheRefreshWorker();
}
S
Shengliang Guan 已提交
146

147
int32_t dnodeInit() {
S
Shengliang Guan 已提交
148 149 150 151 152
  SSteps *steps = taosStepInit(24, dnodeReportStartup);
  if (steps == NULL) return -1;

  taosStepAdd(steps, "dnode-main", dnodeInitMain, dnodeCleanupMain);
  taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup);
S
Shengliang Guan 已提交
153
  taosStepAdd(steps, "dnode-tfs", NULL, NULL);
S
Shengliang Guan 已提交
154 155
  taosStepAdd(steps, "dnode-wal", walInit, walCleanUp);
  taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp);
S
Shengliang Guan 已提交
156
  taosStepAdd(steps, "dnode-dnode", dnodeInitDnode, dnodeCleanupDnode);
S
Shengliang Guan 已提交
157 158
  taosStepAdd(steps, "dnode-vnodes", dnodeInitVnodes, dnodeCleanupVnodes);
  taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, dnodeCleanupMnode);
S
Shengliang Guan 已提交
159 160
  taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans);

S
Shengliang Guan 已提交
161 162
  tsInt.steps = steps;
  taosStepExec(tsInt.steps);
163

S
Shengliang Guan 已提交
164
  dnodeSetRunStat(DN_RUN_STAT_RUNNING);
165 166
  dnodeReportStartupFinished("TDengine", "initialized successfully");
  dInfo("TDengine is initialized successfully");
167

168
  return 0;
169 170
}

171
void dnodeCleanup() {
S
Shengliang Guan 已提交
172 173
  if (dnodeGetRunStat() != DN_RUN_STAT_STOPPED) {
    dnodeSetRunStat(DN_RUN_STAT_STOPPED);
S
Shengliang Guan 已提交
174 175
    taosStepCleanup(tsInt.steps);
    tsInt.steps = NULL;
176 177
  }
}