dnodeInt.c 5.8 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 20
#include "dnodeConfig.h"
#include "dnodeDnode.h"
#include "dnodeTransport.h"
21
#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
#include "wal.h"

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

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

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

S
Shengliang Guan 已提交
40
void dnodeReportStartup(char *name, char *desc) {
S
Shengliang Guan 已提交
41
  SStartupStep *startup = &tsInt.startup;
S
Shengliang Guan 已提交
42 43 44 45 46 47
  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 已提交
48
  SStartupStep *startup = &tsInt.startup;
S
Shengliang Guan 已提交
49 50 51 52
  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, &tsInt.startup, sizeof(SStartupStep)); }
55

S
Shengliang Guan 已提交
56
static int32_t dnodeInitVnode() {
S
Shengliang Guan 已提交
57
  return vnodeInit();
58 59
}

S
Shengliang Guan 已提交
60
static int32_t dnodeInitMnode() {
61
  SMnodePara para;
S
Shengliang Guan 已提交
62
  para.fp.GetDnodeEp = dnodeGetEp;
63 64 65
  para.fp.SendMsgToDnode = dnodeSendMsgToDnode;
  para.fp.SendMsgToMnode = dnodeSendMsgToMnode;
  para.fp.SendRedirectMsg = dnodeSendRedirectMsg;
S
Shengliang Guan 已提交
66 67
  para.dnodeId = dnodeGetDnodeId();
  para.clusterId = dnodeGetClusterId();
68

69
  return mnodeInit(para);
70 71
}

S
Shengliang Guan 已提交
72 73 74
static int32_t dnodeInitTfs() {}

static int32_t dnodeInitMain() {
S
Shengliang Guan 已提交
75
  tsInt.runStatus = DN_RUN_STAT_STOPPED;
S
Shengliang Guan 已提交
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 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
  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() {}

163
int32_t dnodeInit() {
S
Shengliang Guan 已提交
164 165 166 167 168 169 170 171 172 173
  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);
S
Shengliang Guan 已提交
174
  taosStepAdd(steps, "dnode-eps", dnodeInitConfig, dnodeCleanupConfig);
S
Shengliang Guan 已提交
175 176 177
  taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup);
  taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup);
  taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans);
S
Shengliang Guan 已提交
178
  taosStepAdd(steps, "dnode-msg", dnodeInitDnode, dnodeCleanupDnode);
S
Shengliang Guan 已提交
179

S
Shengliang Guan 已提交
180 181
  tsInt.steps = steps;
  taosStepExec(tsInt.steps);
182

S
Shengliang Guan 已提交
183
  dnodeSetRunStat(DN_RUN_STAT_RUNNING);
184 185
  dnodeReportStartupFinished("TDengine", "initialized successfully");
  dInfo("TDengine is initialized successfully");
186

187
  return 0;
188 189
}

190
void dnodeCleanup() {
S
Shengliang Guan 已提交
191 192
  if (dnodeGetRunStat() != DN_RUN_STAT_STOPPED) {
    dnodeSetRunStat(DN_RUN_STAT_STOPPED);
S
Shengliang Guan 已提交
193 194
    taosStepCleanup(tsInt.steps);
    tsInt.steps = NULL;
195 196
  }
}
S
Shengliang Guan 已提交
197 198 199 200 201 202 203 204

// tsVnode.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMgmtMsg;
//   tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMgmtMsg;
//   tsVnode.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMgmtMsg;
//   tsVnode.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg;
//   tsVnode.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg;
//   tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg;