daemon.c 5.1 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
S
Shengliang Guan 已提交
15 16

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17 18
#include "dnode.h"
#include "os.h"
S
Shengliang Guan 已提交
19
#include "tconfig.h"
S
Shengliang Guan 已提交
20
#include "tglobal.h"
S
Shengliang Guan 已提交
21
#include "tnote.h"
S
Shengliang Guan 已提交
22
#include "ulog.h"
H
refact  
Hongze Cheng 已提交
23

S
Shengliang Guan 已提交
24 25 26 27 28 29 30 31
static struct {
  bool stop;
  bool dumpConfig;
  bool generateGrant;
  bool printAuth;
  bool printVersion;
  char configDir[PATH_MAX];
} global = {0};
S
Shengliang Guan 已提交
32

33 34 35 36
void dmnSigintHandle(int signum, void *info, void *ctx) {
  uError("singal:%d is received", signum);
  global.stop = true;
}
S
Shengliang Guan 已提交
37

S
Shengliang Guan 已提交
38 39 40 41 42 43
void dmnSetSignalHandle() {
  taosSetSignal(SIGTERM, dmnSigintHandle);
  taosSetSignal(SIGHUP, dmnSigintHandle);
  taosSetSignal(SIGINT, dmnSigintHandle);
  taosSetSignal(SIGABRT, dmnSigintHandle);
  taosSetSignal(SIGBREAK, dmnSigintHandle);
S
Shengliang Guan 已提交
44 45
}

46
int dmnParseOption(int argc, char const *argv[]) {
S
Shengliang Guan 已提交
47
  tstrncpy(global.configDir, "/etc/taos", PATH_MAX);
S
Shengliang Guan 已提交
48

S
Shengliang Guan 已提交
49 50 51 52 53 54 55 56 57
  for (int i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "-c") == 0) {
      if (i < argc - 1) {
        if (strlen(argv[++i]) >= PATH_MAX) {
          printf("config file path overflow");
          return -1;
        }
        tstrncpy(global.configDir, argv[i], PATH_MAX);
      } else {
58
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
59 60 61 62 63 64 65 66 67 68 69 70
        return -1;
      }
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
    } else if (strcmp(argv[i], "-k") == 0) {
      global.generateGrant = true;
    } else if (strcmp(argv[i], "-A") == 0) {
      global.printAuth = true;
    } else if (strcmp(argv[i], "-V") == 0) {
      global.printVersion = true;
    } else {
    }
S
Shengliang Guan 已提交
71 72
  }

S
Shengliang Guan 已提交
73 74 75
  return 0;
}

S
Shengliang Guan 已提交
76 77 78 79 80
void dmnGenerateGrant() {
#if 0
  grantParseParameter();
#endif
}
S
Shengliang Guan 已提交
81 82 83

void dmnPrintVersion() {
#ifdef TD_ENTERPRISE
84
  char *releaseName = "enterprise";
S
Shengliang Guan 已提交
85
#else
86
  char *releaseName = "community";
S
Shengliang Guan 已提交
87
#endif
88
  printf("%s version: %s compatible_version: %s\n", releaseName, version, compatible_version);
S
Shengliang Guan 已提交
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
  printf("gitinfo: %s\n", gitinfo);
  printf("gitinfoI: %s\n", gitinfoOfInternal);
  printf("builuInfo: %s\n", buildinfo);
}

int dmnReadConfig(const char *path) {
  taosInitGlobalCfg();
  taosReadGlobalLogCfg();

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

  char temp[PATH_MAX];
  snprintf(temp, PATH_MAX, "%s/taosdlog", tsLogDir);
  if (taosInitLog(temp, tsNumOfLogLines, 1) != 0) {
    printf("failed to init log file\n");
    return -1;
  }

  if (taosInitNotes() != 0) {
    printf("failed to init log file\n");
    return -1;
  }

115
  if (taosReadCfgFromFile() != 0) {
S
Shengliang Guan 已提交
116 117 118 119
    uError("failed to read global config");
    return -1;
  }

120
  if (taosCheckAndPrintCfg() != 0) {
S
Shengliang Guan 已提交
121 122 123 124 125 126 127
    uError("failed to check global config");
    return -1;
  }

  taosSetCoreDump(tsEnableCoreFile);
  return 0;
}
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130 131 132 133
void dmnDumpConfig() { taosDumpGlobalCfg(); }

void dmnWaitSignal() {
  dmnSetSignalHandle();
  while (!global.stop) {
S
Shengliang Guan 已提交
134 135
    taosMsleep(100);
  }
S
Shengliang Guan 已提交
136 137
}

138
void dmnInitOption(SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
139
  pOption->sver = 30000000; //3.0.0.0
140
  pOption->numOfCores = tsNumOfCores;
S
Shengliang Guan 已提交
141 142 143 144
  pOption->numOfSupportMnodes = 1;
  pOption->numOfSupportVnodes = 1;
  pOption->numOfSupportQnodes = 1;
  pOption->statusInterval = tsStatusInterval;
145 146 147 148 149
  pOption->numOfThreadsPerCore = tsNumOfThreadsPerCore;
  pOption->ratioOfQueryCores = tsRatioOfQueryCores;
  pOption->maxShellConns = tsMaxShellConns;
  pOption->shellActivityTimer = tsShellActivityTimer;
  pOption->serverPort = tsServerPort;
S
Shengliang Guan 已提交
150
  tstrncpy(pOption->dataDir, tsDataDir, TSDB_FILENAME_LEN);
151
  tstrncpy(pOption->localEp, tsLocalEp, TSDB_EP_LEN);
152 153 154 155 156
  tstrncpy(pOption->localFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
  tstrncpy(pOption->firstEp, tsFirst, TSDB_EP_LEN);
  tstrncpy(pOption->timezone, tsTimezone, TSDB_TIMEZONE_LEN);
  tstrncpy(pOption->locale, tsLocale, TSDB_LOCALE_LEN);
  tstrncpy(pOption->charset, tsCharset, TSDB_LOCALE_LEN);
S
Shengliang Guan 已提交
157 158
  tstrncpy(pOption->buildinfo, buildinfo, 64);
  tstrncpy(pOption->gitinfo, gitinfo, 48);
S
Shengliang Guan 已提交
159 160 161
}

int dmnRunDnode() {
162 163
  SDnodeOpt option = {0};
  dmnInitOption(&option);
S
Shengliang Guan 已提交
164

165 166
  SDnode *pDnode = dndInit(&option);
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
167 168 169 170 171 172
    uInfo("Failed to start TDengine, please check the log at %s", tsLogDir);
    return -1;
  }

  uInfo("Started TDengine service successfully.");
  dmnWaitSignal();
S
Shengliang Guan 已提交
173
  uInfo("TDengine is shut down!");
S
Shengliang Guan 已提交
174

175
  dndCleanup(pDnode);
S
Shengliang Guan 已提交
176
  taosCloseLog();
H
refact  
Hongze Cheng 已提交
177 178
  return 0;
}
S
Shengliang Guan 已提交
179 180

int main(int argc, char const *argv[]) {
181
  if (dmnParseOption(argc, argv) != 0) {
S
Shengliang Guan 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    return -1;
  }

  if (global.generateGrant) {
    dmnGenerateGrant();
    return 0;
  }

  if (global.printVersion) {
    dmnPrintVersion();
    return 0;
  }

  if (dmnReadConfig(global.configDir) != 0) {
    return -1;
  }

  if (global.dumpConfig) {
    dmnDumpConfig();
    return 0;
  }

  return dmnRunDnode();
}