dnodeInt.c 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
#include "dnodeCheck.h"
S
Shengliang Guan 已提交
18 19
#include "dnodeEps.h"
#include "dnodeMsg.h"
20 21
#include "dnodeTrans.h"
#include "mnode.h"
S
Shengliang Guan 已提交
22 23 24 25 26
#include "sync.h"
#include "tcache.h"
#include "tconfig.h"
#include "tnote.h"
#include "tstep.h"
27
#include "vnode.h"
S
Shengliang Guan 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "wal.h"

static struct {
  EDnStat      runStatus;
  SStartupStep startup;
  SSteps      *steps;
} tsDnode;

EDnStat dnodeGetRunStat() { return tsDnode.runStatus; }

void dnodeSetRunStat(EDnStat stat) { tsDnode.runStatus = stat; }

static void dnodeReportStartup(char *name, char *desc) {
  SStartupStep *startup = &tsDnode.startup;
  tstrncpy(startup->name, name, strlen(startup->name));
  tstrncpy(startup->desc, desc, strlen(startup->desc));
  startup->finished = 0;
}

static void dnodeReportStartupFinished(char *name, char *desc) {
  SStartupStep *startup = &tsDnode.startup;
  tstrncpy(startup->name, name, strlen(startup->name));
  tstrncpy(startup->desc, desc, strlen(startup->desc));
  startup->finished = 1;
}
53

S
Shengliang Guan 已提交
54
void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsDnode.startup, sizeof(SStartupStep)); }
55

S
Shengliang Guan 已提交
56
static int32_t dnodeInitVnode() {
57
  SVnodePara para;
S
Shengliang Guan 已提交
58
  para.fp.GetDnodeEp = dnodeGetEp;
59 60 61
  para.fp.SendMsgToDnode = dnodeSendMsgToDnode;
  para.fp.SendMsgToMnode = dnodeSendMsgToMnode;

62
  return vnodeInit(para);
63 64
}

S
Shengliang Guan 已提交
65
static int32_t dnodeInitMnode() {
66
  SMnodePara para;
S
Shengliang Guan 已提交
67
  para.fp.GetDnodeEp = dnodeGetEp;
68 69 70
  para.fp.SendMsgToDnode = dnodeSendMsgToDnode;
  para.fp.SendMsgToMnode = dnodeSendMsgToMnode;
  para.fp.SendRedirectMsg = dnodeSendRedirectMsg;
S
Shengliang Guan 已提交
71 72
  para.dnodeId = dnodeGetDnodeId();
  para.clusterId = dnodeGetClusterId();
73

74
  return mnodeInit(para);
75 76
}

S
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static int32_t dnodeInitTfs() {}

static int32_t dnodeInitMain() {
  tsDnode.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();

  return taosCheckGlobalCfg();
}

static void dnodeCleanupMain() {
  taos_cleanup();
  taosCloseLog();
  taosStopCacheRefreshWorker();
}

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;
}

static void dnodeCleanupDir() {}

168
int32_t dnodeInit() {
S
Shengliang Guan 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  SSteps *steps = taosStepInit(24, dnodeReportStartup);
  if (steps == NULL) return -1;

  taosStepAdd(steps, "dnode-main", dnodeInitMain, dnodeCleanupMain);
  taosStepAdd(steps, "dnode-dir", dnodeInitDir, dnodeCleanupDir);
  taosStepAdd(steps, "dnode-check", dnodeInitCheck, dnodeCleanupCheck);
  taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup);
  taosStepAdd(steps, "dnode-tfs", dnodeInitTfs, NULL);
  taosStepAdd(steps, "dnode-wal", walInit, walCleanUp);
  taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp);
  taosStepAdd(steps, "dnode-eps", dnodeInitEps, dnodeCleanupEps);
  taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup);
  taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup);
  taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans);
  taosStepAdd(steps, "dnode-msg", dnodeInitMsg, dnodeCleanupMsg);

  tsDnode.steps = steps;
  taosStepExec(tsDnode.steps);
187

S
Shengliang Guan 已提交
188
  dnodeSetRunStat(DN_RUN_STAT_RUNNING);
189 190
  dnodeReportStartupFinished("TDengine", "initialized successfully");
  dInfo("TDengine is initialized successfully");
191

192
  return 0;
193 194
}

195
void dnodeCleanup() {
S
Shengliang Guan 已提交
196 197
  if (dnodeGetRunStat() != DN_RUN_STAT_STOPPED) {
    dnodeSetRunStat(DN_RUN_STAT_STOPPED);
S
Shengliang Guan 已提交
198 199
    taosStepCleanup(tsDnode.steps);
    tsDnode.steps = NULL;
200 201
  }
}