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
#include "sync.h"
#include "tcache.h"
#include "tconfig.h"
#include "tnote.h"
#include "tstep.h"
#include "wal.h"

static struct {
S
Shengliang Guan 已提交
29 30 31
  SStartupMsg startup;
  EDnStat     runStat;
  SSteps     *steps;
S
Shengliang Guan 已提交
32
} tsInt;
S
Shengliang Guan 已提交
33

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

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

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

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

S
Shengliang Guan 已提交
52
void dnodeGetStartup(SStartupMsg *pStartup) { memcpy(pStartup, &tsInt.startup, sizeof(SStartupMsg)); }
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
static int32_t dnodeInitMain() {
S
Shengliang Guan 已提交
102
  tsInt.runStat = DN_RUN_STAT_STOPPED;
S
Shengliang Guan 已提交
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
  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();

S
Shengliang Guan 已提交
138
  return 0;
S
Shengliang Guan 已提交
139 140 141 142 143 144 145
}

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

147
int32_t dnodeInit() {
S
Shengliang Guan 已提交
148
  SSteps *steps = taosStepInit(10, dnodeReportStartup);
S
Shengliang Guan 已提交
149 150 151 152
  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
  taosStepAdd(steps, "dnode-wal", walInit, walCleanUp);
S
Shengliang Guan 已提交
155
  //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
  }
}