dmMain.c 10.0 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"
21
#include "version.h"
H
refact  
Hongze Cheng 已提交
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#if defined(CUS_NAME) || defined(CUS_PROMPT) || defined(CUS_EMAIL)
#include "cus_name.h"
#else
#ifndef CUS_NAME
    #define CUS_NAME      "TDengine"
#endif

#ifndef CUS_PROMPT
    #define CUS_PROMPT    "taos"
#endif

#ifndef CUS_EMAIL
    #define CUS_EMAIL     "<support@taosdata.com>"
#endif
#endif
H
Hongze Cheng 已提交
38
// clang-format off
39 40 41
#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 已提交
42
#define DM_SDB_INFO      "Dump sdb info."
43 44 45 46 47
#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>"
D
dapan1121 已提交
48
#define DM_MEM_DBG       "Enable memory debug"
H
Hongze Cheng 已提交
49
// clang-format on
S
Shengliang Guan 已提交
50
static struct {
wafwerar's avatar
wafwerar 已提交
51
#ifdef WINDOWS
H
Hongze Cheng 已提交
52
  bool winServiceMode;
wafwerar's avatar
wafwerar 已提交
53
#endif
S
Shengliang Guan 已提交
54
  bool         dumpConfig;
S
Shengliang Guan 已提交
55
  bool         dumpSdb;
S
Shengliang Guan 已提交
56
  bool         generateGrant;
D
dapan1121 已提交
57
  bool         memDbg;
S
Shengliang Guan 已提交
58 59
  bool         printAuth;
  bool         printVersion;
60
  bool         printHelp;
S
Shengliang Guan 已提交
61 62
  char         envFile[PATH_MAX];
  char         apolloUrl[PATH_MAX];
wafwerar's avatar
wafwerar 已提交
63
  const char **envCmd;
S
Shengliang Guan 已提交
64
  SArray      *pArgs;  // SConfigPair
D
dapan1121 已提交
65
  int64_t      startTime;
S
shm  
Shengliang Guan 已提交
66
} global = {0};
S
Shengliang Guan 已提交
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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 已提交
88

D
dapan1121 已提交
89 90 91 92 93
void dmLogCrash(int signum, void *sigInfo, void *context) {
  taosIgnSignal(SIGTERM);
  taosIgnSignal(SIGHUP);
  taosIgnSignal(SIGINT);
  taosIgnSignal(SIGBREAK);
D
dapan1121 已提交
94

95
#ifndef WINDOWS
D
dapan1121 已提交
96
  taosIgnSignal(SIGBUS);
D
dapan1121 已提交
97
#endif
D
dapan1121 已提交
98 99 100 101
  taosIgnSignal(SIGABRT);
  taosIgnSignal(SIGFPE);
  taosIgnSignal(SIGSEGV);

102
  char       *pMsg = NULL;
D
dapan1121 已提交
103 104 105
  const char *flags = "UTL FATAL ";
  ELogLevel   level = DEBUG_FATAL;
  int32_t     dflag = 255;
106 107
  int64_t     msgLen = -1;

D
dapan1121 已提交
108 109 110 111 112
  if (tsEnableCrashReport) {
    if (taosGenCrashJsonMsg(signum, &pMsg, dmGetClusterId(), global.startTime)) {
      taosPrintLog(flags, level, dflag, "failed to generate crash json msg");
      goto _return;
    } else {
113
      msgLen = strlen(pMsg);
D
dapan1121 已提交
114 115
    }
  }
116

D
dapan1121 已提交
117 118 119
_return:

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

D
dapan1121 已提交
121
#ifdef _TD_DARWIN_64
D
dapan1121 已提交
122
  exit(signum);
D
dapan1121 已提交
123 124 125
#elif defined(WINDOWS)
  exit(signum);
#endif
D
dapan1121 已提交
126 127
}

S
Shengliang Guan 已提交
128
static void dmSetSignalHandle() {
129 130
  taosSetSignal(SIGUSR1, dmSetDebugFlag);
  taosSetSignal(SIGUSR2, dmSetAssert);
S
Shengliang Guan 已提交
131 132 133 134
  taosSetSignal(SIGTERM, dmStopDnode);
  taosSetSignal(SIGHUP, dmStopDnode);
  taosSetSignal(SIGINT, dmStopDnode);
  taosSetSignal(SIGBREAK, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
135 136
#ifndef WINDOWS
  taosSetSignal(SIGTSTP, dmStopDnode);
S
Shengliang Guan 已提交
137
  taosSetSignal(SIGQUIT, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
138
#endif
D
dapan1121 已提交
139

D
dapan1121 已提交
140
#ifndef WINDOWS
D
dapan1121 已提交
141
  taosSetSignal(SIGBUS, dmLogCrash);
142
#endif
D
dapan1121 已提交
143 144 145
  taosSetSignal(SIGABRT, dmLogCrash);
  taosSetSignal(SIGFPE, dmLogCrash);
  taosSetSignal(SIGSEGV, dmLogCrash);
S
Shengliang Guan 已提交
146 147
}

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

wafwerar's avatar
wafwerar 已提交
151
  int32_t cmdEnvIndex = 0;
wafwerar's avatar
wafwerar 已提交
152
  if (argc < 2) return 0;
153

154 155
  global.envCmd = taosMemoryMalloc((argc - 1) * sizeof(char *));
  memset(global.envCmd, 0, (argc - 1) * sizeof(char *));
S
Shengliang Guan 已提交
156
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
157 158 159 160 161 162
    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 已提交
163
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
164
      } else {
165
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
166 167
        return -1;
      }
S
Shengliang Guan 已提交
168 169
    } else if (strcmp(argv[i], "-a") == 0) {
      tstrncpy(global.apolloUrl, argv[++i], PATH_MAX);
S
Shengliang Guan 已提交
170 171
    } else if (strcmp(argv[i], "-s") == 0) {
      global.dumpSdb = true;
wafwerar's avatar
wafwerar 已提交
172
    } else if (strcmp(argv[i], "-E") == 0) {
S
Shengliang Guan 已提交
173
      tstrncpy(global.envFile, argv[++i], PATH_MAX);
S
shm  
Shengliang Guan 已提交
174 175
    } else if (strcmp(argv[i], "-k") == 0) {
      global.generateGrant = true;
S
Shengliang Guan 已提交
176 177
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
S
Shengliang Guan 已提交
178
    } else if (strcmp(argv[i], "-V") == 0) {
S
shm  
Shengliang Guan 已提交
179
      global.printVersion = true;
H
Hongze Cheng 已提交
180
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
181 182
    } else if (strcmp(argv[i], "--win_service") == 0) {
      global.winServiceMode = true;
H
Hongze Cheng 已提交
183
#endif
wafwerar's avatar
wafwerar 已提交
184 185 186
    } else if (strcmp(argv[i], "-e") == 0) {
      global.envCmd[cmdEnvIndex] = argv[++i];
      cmdEnvIndex++;
D
dapan1121 已提交
187 188
    } else if (strcmp(argv[i], "-dm") == 0) {
      global.memDbg = true;
189
    } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ||
D
dapan1121 已提交
190
               strcmp(argv[i], "-?") == 0) {
191
      global.printHelp = true;
S
Shengliang Guan 已提交
192 193
    } else {
    }
S
Shengliang Guan 已提交
194 195
  }

S
Shengliang Guan 已提交
196 197 198
  return 0;
}

199 200 201 202 203 204 205 206 207 208 209 210 211
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 已提交
212
static void dmGenerateGrant() { mndGenerateMachineCode(); }
S
Shengliang Guan 已提交
213

S
Shengliang Guan 已提交
214
static void dmPrintVersion() {
S
Shengliang Guan 已提交
215 216 217 218 219 220 221
#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);
222 223 224
#ifdef TD_ENTERPRISE
  printf("gitinfoOfInternal: %s\n", gitinfoOfInternal);
#endif
S
Shengliang Guan 已提交
225 226 227
  printf("buildInfo: %s\n", buildinfo);
}

228 229 230 231 232
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 已提交
233
  printf("%s%s%s%s\n", indent, "-s,", indent, DM_SDB_INFO);
234 235 236 237
  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);
D
dapan1121 已提交
238
  printf("%s%s%s%s\n", indent, "-dm,", indent, DM_MEM_DBG);
239 240 241 242 243
  printf("%s%s%s%s\n", indent, "-V,", indent, DM_VERSION);

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

S
Shengliang Guan 已提交
244
static void dmDumpCfg() {
S
Shengliang Guan 已提交
245
  SConfig *pCfg = taosGetCfg();
246
  cfgDumpCfg(pCfg, 0, true);
S
Shengliang Guan 已提交
247 248
}

S
Shengliang Guan 已提交
249
static int32_t dmInitLog() {
250
  return taosCreateLog(CUS_PROMPT"dlog", 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0);
S
shm  
Shengliang Guan 已提交
251 252
}

wafwerar's avatar
wafwerar 已提交
253
static void taosCleanupArgs() {
dengyihao's avatar
dengyihao 已提交
254
  if (global.envCmd != NULL) taosMemoryFreeClear(global.envCmd);
wafwerar's avatar
wafwerar 已提交
255 256
}

S
Shengliang Guan 已提交
257
int main(int argc, char const *argv[]) {
D
dapan1121 已提交
258 259
  if (!taosCheckSystemIsLittleEnd()) {
    printf("failed to start since on non-little-end machines\n");
260 261 262
    return -1;
  }

S
Shengliang Guan 已提交
263
  if (dmParseArgs(argc, argv) != 0) {
S
Shengliang Guan 已提交
264
    printf("failed to start since parse args error\n");
wafwerar's avatar
wafwerar 已提交
265
    taosCleanupArgs();
S
Shengliang Guan 已提交
266 267 268
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
269
#ifdef WINDOWS
H
Hongze Cheng 已提交
270
  int mainWindows(int argc, char **argv);
wafwerar's avatar
wafwerar 已提交
271 272 273 274 275 276 277
  if (global.winServiceMode) {
    stratWindowsService(mainWindows);
  } else {
    return mainWindows(argc, argv);
  }
  return 0;
}
H
Hongze Cheng 已提交
278
int mainWindows(int argc, char **argv) {
wafwerar's avatar
wafwerar 已提交
279 280
#endif

S
shm  
Shengliang Guan 已提交
281
  if (global.generateGrant) {
S
Shengliang Guan 已提交
282
    dmGenerateGrant();
wafwerar's avatar
wafwerar 已提交
283
    taosCleanupArgs();
S
Shengliang Guan 已提交
284 285 286
    return 0;
  }

287 288 289 290 291 292
  if (global.printHelp) {
    dmPrintHelp();
    taosCleanupArgs();
    return 0;
  }

S
shm  
Shengliang Guan 已提交
293
  if (global.printVersion) {
S
Shengliang Guan 已提交
294
    dmPrintVersion();
wafwerar's avatar
wafwerar 已提交
295
    taosCleanupArgs();
S
Shengliang Guan 已提交
296
    return 0;
S
Shengliang Guan 已提交
297 298
  }

D
dapan1121 已提交
299 300 301 302 303 304 305
#if defined(LINUX)
  if (global.memDbg) {
    int32_t code = taosMemoryDbgInit();
    if (code) {
      printf("failed to init memory dbg, error:%s\n", tstrerror(code));
      return code;
    }
306
    tsAsyncLog = false;
D
dapan1121 已提交
307 308 309 310
    printf("memory dbg enabled\n");
  }
#endif

S
Shengliang Guan 已提交
311
  if (dmInitLog() != 0) {
S
Shengliang Guan 已提交
312
    printf("failed to start since init log error\n");
wafwerar's avatar
wafwerar 已提交
313
    taosCleanupArgs();
S
Shengliang Guan 已提交
314
    return -1;
S
Shengliang Guan 已提交
315 316
  }

317 318
  dmPrintArgs(argc, argv);

wafwerar's avatar
wafwerar 已提交
319
  if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
320
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
321
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
322
    taosCleanupArgs();
S
Shengliang Guan 已提交
323 324
    return -1;
  }
H
Hongze Cheng 已提交
325

S
Shengliang Guan 已提交
326 327 328 329 330 331
  if (taosConvInit() != 0) {
    dError("failed to init conv");
    taosCloseLog();
    taosCleanupArgs();
    return -1;
  }
S
Shengliang Guan 已提交
332

S
shm  
Shengliang Guan 已提交
333
  if (global.dumpConfig) {
S
Shengliang Guan 已提交
334
    dmDumpCfg();
S
Shengliang Guan 已提交
335
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
336
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
337
    taosCleanupArgs();
338
    taosConvDestroy();
S
Shengliang Guan 已提交
339 340 341
    return 0;
  }

S
Shengliang Guan 已提交
342 343 344 345 346
  if (global.dumpSdb) {
    mndDumpSdb();
    taosCleanupCfg();
    taosCloseLog();
    taosCleanupArgs();
347
    taosConvDestroy();
S
Shengliang Guan 已提交
348 349 350
    return 0;
  }

351
  osSetProcPath(argc, (char **)argv);
wafwerar's avatar
wafwerar 已提交
352
  taosCleanupArgs();
353

354
  if (dmInit() != 0) {
355
    dError("failed to init dnode since %s", terrstr());
356 357 358 359

    taosCleanupCfg();
    taosCloseLog();
    taosConvDestroy();
360 361 362
    return -1;
  }

S
Shengliang Guan 已提交
363
  dInfo("start to init service");
364 365 366 367 368 369
  dmSetSignalHandle();
  int32_t code = dmRun();
  dInfo("shutting down the service");

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