dmMain.c 9.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
#include "dmMgmt.h"
S
Shengliang Guan 已提交
18
#include "mnode.h"
H
Hongze Cheng 已提交
19
#include "tconfig.h"
20
#include "tglobal.h"
H
refact  
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22
// clang-format off
23 24 25
#define DM_APOLLO_URL    "The apollo string to use when configuring the server, such as: -a 'jsonFile:./tests/cfg.json', cfg.json text can be '{\"fqdn\":\"td1\"}'."
#define DM_CFG_DIR       "Configuration directory."
#define DM_DMP_CFG       "Dump configuration."
S
Shengliang Guan 已提交
26
#define DM_SDB_INFO      "Dump sdb info."
27 28 29 30 31
#define DM_ENV_CMD       "The env cmd variable string to use when configuring the server, such as: -e 'TAOS_FQDN=td1'."
#define DM_ENV_FILE      "The env variable file path to use when configuring the server, default is './.env', .env text can be 'TAOS_FQDN=td1'."
#define DM_MACHINE_CODE  "Get machine code."
#define DM_VERSION       "Print program version."
#define DM_EMAIL         "<support@taosdata.com>"
H
Hongze Cheng 已提交
32
// clang-format on
S
Shengliang Guan 已提交
33
static struct {
wafwerar's avatar
wafwerar 已提交
34
#ifdef WINDOWS
H
Hongze Cheng 已提交
35
  bool winServiceMode;
wafwerar's avatar
wafwerar 已提交
36
#endif
S
Shengliang Guan 已提交
37
  bool         dumpConfig;
S
Shengliang Guan 已提交
38
  bool         dumpSdb;
S
Shengliang Guan 已提交
39 40 41
  bool         generateGrant;
  bool         printAuth;
  bool         printVersion;
42
  bool         printHelp;
S
Shengliang Guan 已提交
43 44
  char         envFile[PATH_MAX];
  char         apolloUrl[PATH_MAX];
wafwerar's avatar
wafwerar 已提交
45
  const char **envCmd;
S
Shengliang Guan 已提交
46
  SArray      *pArgs;  // SConfigPair
D
dapan1121 已提交
47
  int64_t      startTime;
S
shm  
Shengliang Guan 已提交
48
} global = {0};
S
Shengliang Guan 已提交
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static void dmSetDebugFlag(int32_t signum, void *sigInfo, void *context) { taosSetAllDebugFlag(143, true); }
static void dmSetAssert(int32_t signum, void *sigInfo, void *context) { tsAssert = 1; }

static void dmStopDnode(int signum, void *sigInfo, void *context) {
  // taosIgnSignal(SIGUSR1);
  // taosIgnSignal(SIGUSR2);
  taosIgnSignal(SIGTERM);
  taosIgnSignal(SIGHUP);
  taosIgnSignal(SIGINT);
  taosIgnSignal(SIGABRT);
  taosIgnSignal(SIGBREAK);

  dInfo("shut down signal is %d", signum);
#ifndef WINDOWS
  dInfo("sender PID:%d cmdline:%s", ((siginfo_t *)sigInfo)->si_pid,
        taosGetCmdlineByPID(((siginfo_t *)sigInfo)->si_pid));
#endif

  dmStop();
}
S
Shengliang Guan 已提交
70

D
dapan1121 已提交
71 72 73 74 75
void dmLogCrash(int signum, void *sigInfo, void *context) {
  taosIgnSignal(SIGTERM);
  taosIgnSignal(SIGHUP);
  taosIgnSignal(SIGINT);
  taosIgnSignal(SIGBREAK);
D
dapan1121 已提交
76 77

#ifndef WINDOWS  
D
dapan1121 已提交
78
  taosIgnSignal(SIGBUS);
D
dapan1121 已提交
79
#endif
D
dapan1121 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  taosIgnSignal(SIGABRT);
  taosIgnSignal(SIGFPE);
  taosIgnSignal(SIGSEGV);

  char *pMsg = NULL;
  const char *flags = "UTL FATAL ";
  ELogLevel   level = DEBUG_FATAL;
  int32_t     dflag = 255;
  int64_t     msgLen= -1;
  
  if (tsEnableCrashReport) {
    if (taosGenCrashJsonMsg(signum, &pMsg, dmGetClusterId(), global.startTime)) {
      taosPrintLog(flags, level, dflag, "failed to generate crash json msg");
      goto _return;
    } else {
      msgLen = strlen(pMsg);  
    }
  }
  
_return:

  taosLogCrashInfo("taosd", pMsg, msgLen, signum, sigInfo);
D
dapan1121 已提交
102

D
dapan1121 已提交
103
#ifdef _TD_DARWIN_64
D
dapan1121 已提交
104
  exit(signum);
D
dapan1121 已提交
105 106 107
#elif defined(WINDOWS)
  exit(signum);
#endif
D
dapan1121 已提交
108 109
}

S
Shengliang Guan 已提交
110
static void dmSetSignalHandle() {
111 112
  taosSetSignal(SIGUSR1, dmSetDebugFlag);
  taosSetSignal(SIGUSR2, dmSetAssert);
S
Shengliang Guan 已提交
113 114 115 116
  taosSetSignal(SIGTERM, dmStopDnode);
  taosSetSignal(SIGHUP, dmStopDnode);
  taosSetSignal(SIGINT, dmStopDnode);
  taosSetSignal(SIGBREAK, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
117 118
#ifndef WINDOWS
  taosSetSignal(SIGTSTP, dmStopDnode);
S
Shengliang Guan 已提交
119
  taosSetSignal(SIGQUIT, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
120
#endif
D
dapan1121 已提交
121

D
dapan1121 已提交
122
#ifndef WINDOWS
D
dapan1121 已提交
123
  taosSetSignal(SIGBUS, dmLogCrash);
D
dapan1121 已提交
124
#endif  
D
dapan1121 已提交
125 126 127
  taosSetSignal(SIGABRT, dmLogCrash);
  taosSetSignal(SIGFPE, dmLogCrash);
  taosSetSignal(SIGSEGV, dmLogCrash);
S
Shengliang Guan 已提交
128 129
}

S
Shengliang Guan 已提交
130
static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
D
dapan1121 已提交
131 132
  global.startTime = taosGetTimestampMs();

wafwerar's avatar
wafwerar 已提交
133
  int32_t cmdEnvIndex = 0;
wafwerar's avatar
wafwerar 已提交
134
  if (argc < 2) return 0;
D
dapan1121 已提交
135
  
136 137
  global.envCmd = taosMemoryMalloc((argc - 1) * sizeof(char *));
  memset(global.envCmd, 0, (argc - 1) * sizeof(char *));
S
Shengliang Guan 已提交
138
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
139 140 141 142 143 144
    if (strcmp(argv[i], "-c") == 0) {
      if (i < argc - 1) {
        if (strlen(argv[++i]) >= PATH_MAX) {
          printf("config file path overflow");
          return -1;
        }
S
Shengliang Guan 已提交
145
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
146
      } else {
147
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
148 149
        return -1;
      }
S
Shengliang Guan 已提交
150 151
    } else if (strcmp(argv[i], "-a") == 0) {
      tstrncpy(global.apolloUrl, argv[++i], PATH_MAX);
S
Shengliang Guan 已提交
152 153
    } else if (strcmp(argv[i], "-s") == 0) {
      global.dumpSdb = true;
wafwerar's avatar
wafwerar 已提交
154
    } else if (strcmp(argv[i], "-E") == 0) {
S
Shengliang Guan 已提交
155
      tstrncpy(global.envFile, argv[++i], PATH_MAX);
S
shm  
Shengliang Guan 已提交
156 157
    } else if (strcmp(argv[i], "-k") == 0) {
      global.generateGrant = true;
S
Shengliang Guan 已提交
158 159
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
S
Shengliang Guan 已提交
160
    } else if (strcmp(argv[i], "-V") == 0) {
S
shm  
Shengliang Guan 已提交
161
      global.printVersion = true;
H
Hongze Cheng 已提交
162
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
163 164
    } else if (strcmp(argv[i], "--win_service") == 0) {
      global.winServiceMode = true;
H
Hongze Cheng 已提交
165
#endif
wafwerar's avatar
wafwerar 已提交
166 167 168
    } else if (strcmp(argv[i], "-e") == 0) {
      global.envCmd[cmdEnvIndex] = argv[++i];
      cmdEnvIndex++;
169 170
    } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ||
               strcmp(argv[i], "-?")) {
171
      global.printHelp = true;
S
Shengliang Guan 已提交
172 173
    } else {
    }
S
Shengliang Guan 已提交
174 175
  }

S
Shengliang Guan 已提交
176 177 178
  return 0;
}

179 180 181 182 183 184 185 186 187 188 189 190 191
static void dmPrintArgs(int32_t argc, char const *argv[]) {
  char path[1024] = {0};
  taosGetCwd(path, sizeof(path));

  char    args[1024] = {0};
  int32_t arglen = snprintf(args, sizeof(args), "%s", argv[0]);
  for (int32_t i = 1; i < argc; ++i) {
    arglen = arglen + snprintf(args + arglen, sizeof(args) - arglen, " %s", argv[i]);
  }

  dInfo("startup path:%s args:%s", path, args);
}

S
Shengliang Guan 已提交
192
static void dmGenerateGrant() { mndGenerateMachineCode(); }
S
Shengliang Guan 已提交
193

S
Shengliang Guan 已提交
194
static void dmPrintVersion() {
S
Shengliang Guan 已提交
195 196 197 198 199 200 201 202 203 204
#ifdef TD_ENTERPRISE
  char *releaseName = "enterprise";
#else
  char *releaseName = "community";
#endif
  printf("%s version: %s compatible_version: %s\n", releaseName, version, compatible_version);
  printf("gitinfo: %s\n", gitinfo);
  printf("buildInfo: %s\n", buildinfo);
}

205 206 207 208 209
static void dmPrintHelp() {
  char indent[] = "  ";
  printf("Usage: taosd [OPTION...] \n\n");
  printf("%s%s%s%s\n", indent, "-a,", indent, DM_APOLLO_URL);
  printf("%s%s%s%s\n", indent, "-c,", indent, DM_CFG_DIR);
S
Shengliang Guan 已提交
210
  printf("%s%s%s%s\n", indent, "-s,", indent, DM_SDB_INFO);
211 212 213 214 215 216 217 218 219
  printf("%s%s%s%s\n", indent, "-C,", indent, DM_DMP_CFG);
  printf("%s%s%s%s\n", indent, "-e,", indent, DM_ENV_CMD);
  printf("%s%s%s%s\n", indent, "-E,", indent, DM_ENV_FILE);
  printf("%s%s%s%s\n", indent, "-k,", indent, DM_MACHINE_CODE);
  printf("%s%s%s%s\n", indent, "-V,", indent, DM_VERSION);

  printf("\n\nReport bugs to %s.\n", DM_EMAIL);
}

S
Shengliang Guan 已提交
220
static void dmDumpCfg() {
S
Shengliang Guan 已提交
221
  SConfig *pCfg = taosGetCfg();
222
  cfgDumpCfg(pCfg, 0, true);
S
Shengliang Guan 已提交
223 224
}

S
Shengliang Guan 已提交
225
static int32_t dmInitLog() {
226
  return taosCreateLog("taosdlog", 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0);
S
shm  
Shengliang Guan 已提交
227 228
}

wafwerar's avatar
wafwerar 已提交
229
static void taosCleanupArgs() {
dengyihao's avatar
dengyihao 已提交
230
  if (global.envCmd != NULL) taosMemoryFreeClear(global.envCmd);
wafwerar's avatar
wafwerar 已提交
231 232
}

S
Shengliang Guan 已提交
233
int main(int argc, char const *argv[]) {
D
dapan1121 已提交
234 235
  if (!taosCheckSystemIsLittleEnd()) {
    printf("failed to start since on non-little-end machines\n");
236 237 238
    return -1;
  }

S
Shengliang Guan 已提交
239
  if (dmParseArgs(argc, argv) != 0) {
S
Shengliang Guan 已提交
240
    printf("failed to start since parse args error\n");
wafwerar's avatar
wafwerar 已提交
241
    taosCleanupArgs();
S
Shengliang Guan 已提交
242 243 244
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
245
#ifdef WINDOWS
H
Hongze Cheng 已提交
246
  int mainWindows(int argc, char **argv);
wafwerar's avatar
wafwerar 已提交
247 248 249 250 251 252 253
  if (global.winServiceMode) {
    stratWindowsService(mainWindows);
  } else {
    return mainWindows(argc, argv);
  }
  return 0;
}
H
Hongze Cheng 已提交
254
int mainWindows(int argc, char **argv) {
wafwerar's avatar
wafwerar 已提交
255 256
#endif

S
shm  
Shengliang Guan 已提交
257
  if (global.generateGrant) {
S
Shengliang Guan 已提交
258
    dmGenerateGrant();
wafwerar's avatar
wafwerar 已提交
259
    taosCleanupArgs();
S
Shengliang Guan 已提交
260 261 262
    return 0;
  }

263 264 265 266 267 268
  if (global.printHelp) {
    dmPrintHelp();
    taosCleanupArgs();
    return 0;
  }

S
shm  
Shengliang Guan 已提交
269
  if (global.printVersion) {
S
Shengliang Guan 已提交
270
    dmPrintVersion();
wafwerar's avatar
wafwerar 已提交
271
    taosCleanupArgs();
S
Shengliang Guan 已提交
272
    return 0;
S
Shengliang Guan 已提交
273 274
  }

S
Shengliang Guan 已提交
275
  if (dmInitLog() != 0) {
S
Shengliang Guan 已提交
276
    printf("failed to start since init log error\n");
wafwerar's avatar
wafwerar 已提交
277
    taosCleanupArgs();
S
Shengliang Guan 已提交
278
    return -1;
S
Shengliang Guan 已提交
279 280
  }

281 282
  dmPrintArgs(argc, argv);

wafwerar's avatar
wafwerar 已提交
283
  if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
284
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
285
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
286
    taosCleanupArgs();
S
Shengliang Guan 已提交
287 288
    return -1;
  }
H
Hongze Cheng 已提交
289

S
Shengliang Guan 已提交
290 291 292 293 294 295
  if (taosConvInit() != 0) {
    dError("failed to init conv");
    taosCloseLog();
    taosCleanupArgs();
    return -1;
  }
S
Shengliang Guan 已提交
296

S
shm  
Shengliang Guan 已提交
297
  if (global.dumpConfig) {
S
Shengliang Guan 已提交
298
    dmDumpCfg();
S
Shengliang Guan 已提交
299
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
300
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
301
    taosCleanupArgs();
302
    taosConvDestroy();
S
Shengliang Guan 已提交
303 304 305
    return 0;
  }

S
Shengliang Guan 已提交
306 307 308 309 310
  if (global.dumpSdb) {
    mndDumpSdb();
    taosCleanupCfg();
    taosCloseLog();
    taosCleanupArgs();
311
    taosConvDestroy();
S
Shengliang Guan 已提交
312 313 314
    return 0;
  }

315
  osSetProcPath(argc, (char **)argv);
wafwerar's avatar
wafwerar 已提交
316
  taosCleanupArgs();
317

318
  if (dmInit() != 0) {
319
    dError("failed to init dnode since %s", terrstr());
320 321 322 323

    taosCleanupCfg();
    taosCloseLog();
    taosConvDestroy();
324 325 326
    return -1;
  }

S
Shengliang Guan 已提交
327
  dInfo("start to init service");
328 329 330 331 332 333
  dmSetSignalHandle();
  int32_t code = dmRun();
  dInfo("shutting down the service");

  dmCleanup();
  return code;
S
Shengliang Guan 已提交
334
}