TDengine/source/dnode/mgmt/exe/dmMain.c

384 lines
10 KiB
C
Raw Normal View History

2021-09-22 08:15:20 +00:00
/*
* 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/>.
*/
2021-11-01 12:38:50 +00:00
#define _DEFAULT_SOURCE
2022-05-12 09:57:38 +00:00
#include "dmMgmt.h"
2022-09-26 08:18:48 +00:00
#include "mnode.h"
2022-10-13 03:56:16 +00:00
#include "tconfig.h"
#include "tglobal.h"
#include "version.h"
#ifdef TD_JEMALLOC_ENABLED
#include "jemalloc/jemalloc.h"
#endif
2021-09-22 08:15:20 +00:00
#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
2022-10-13 03:56:16 +00:00
// clang-format off
#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."
2022-09-26 08:32:14 +00:00
#define DM_SDB_INFO "Dump sdb info."
#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>"
2023-02-17 09:40:14 +00:00
#define DM_MEM_DBG "Enable memory debug"
2022-10-13 03:56:16 +00:00
// clang-format on
2021-11-22 02:03:41 +00:00
static struct {
2022-07-29 08:36:42 +00:00
#ifdef WINDOWS
2022-10-13 03:56:16 +00:00
bool winServiceMode;
2022-07-29 08:36:42 +00:00
#endif
2022-04-13 06:00:56 +00:00
bool dumpConfig;
2022-09-26 08:18:48 +00:00
bool dumpSdb;
2022-04-13 06:00:56 +00:00
bool generateGrant;
2023-02-17 09:40:14 +00:00
bool memDbg;
2022-04-13 06:00:56 +00:00
bool printAuth;
bool printVersion;
bool printHelp;
2022-04-13 06:00:56 +00:00
char envFile[PATH_MAX];
char apolloUrl[PATH_MAX];
2022-04-25 05:57:55 +00:00
const char **envCmd;
2022-04-13 06:00:56 +00:00
SArray *pArgs; // SConfigPair
2022-12-30 05:30:54 +00:00
int64_t startTime;
2022-03-12 15:08:48 +00:00
} global = {0};
2021-11-20 16:13:35 +00:00
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();
}
2021-11-20 16:13:35 +00:00
2022-12-30 05:30:54 +00:00
void dmLogCrash(int signum, void *sigInfo, void *context) {
// taosIgnSignal(SIGTERM);
// taosIgnSignal(SIGHUP);
// taosIgnSignal(SIGINT);
// taosIgnSignal(SIGBREAK);
#ifndef WINDOWS
taosIgnSignal(SIGBUS);
#endif
taosIgnSignal(SIGABRT);
taosIgnSignal(SIGFPE);
taosIgnSignal(SIGSEGV);
char *pMsg = NULL;
2022-12-30 05:30:54 +00:00
const char *flags = "UTL FATAL ";
ELogLevel level = DEBUG_FATAL;
int32_t dflag = 255;
int64_t msgLen = -1;
2022-12-30 05:30:54 +00:00
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);
2022-12-30 05:30:54 +00:00
}
}
2022-12-30 05:30:54 +00:00
_return:
taosLogCrashInfo("taosd", pMsg, msgLen, signum, sigInfo);
2023-01-04 11:12:16 +00:00
2023-01-10 01:28:04 +00:00
#ifdef _TD_DARWIN_64
2023-01-04 11:12:16 +00:00
exit(signum);
2023-01-10 01:28:04 +00:00
#elif defined(WINDOWS)
exit(signum);
#endif
2022-12-30 05:30:54 +00:00
}
2022-04-13 06:00:56 +00:00
static void dmSetSignalHandle() {
taosSetSignal(SIGUSR1, dmSetDebugFlag);
taosSetSignal(SIGUSR2, dmSetAssert);
2022-04-13 06:00:56 +00:00
taosSetSignal(SIGTERM, dmStopDnode);
taosSetSignal(SIGHUP, dmStopDnode);
taosSetSignal(SIGINT, dmStopDnode);
taosSetSignal(SIGBREAK, dmStopDnode);
2022-04-22 01:54:27 +00:00
#ifndef WINDOWS
taosSetSignal(SIGTSTP, dmStopDnode);
2022-04-13 06:00:56 +00:00
taosSetSignal(SIGQUIT, dmStopDnode);
2022-04-22 01:54:27 +00:00
#endif
2022-12-23 01:22:14 +00:00
2022-12-30 07:56:37 +00:00
#ifndef WINDOWS
2022-12-30 05:30:54 +00:00
taosSetSignal(SIGBUS, dmLogCrash);
#endif
2022-12-30 05:30:54 +00:00
taosSetSignal(SIGABRT, dmLogCrash);
taosSetSignal(SIGFPE, dmLogCrash);
taosSetSignal(SIGSEGV, dmLogCrash);
2021-10-04 12:47:39 +00:00
}
2022-04-13 06:00:56 +00:00
static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
2022-12-30 05:30:54 +00:00
global.startTime = taosGetTimestampMs();
2022-04-25 05:57:55 +00:00
int32_t cmdEnvIndex = 0;
2022-04-26 08:23:51 +00:00
if (argc < 2) return 0;
2022-05-16 13:36:29 +00:00
global.envCmd = taosMemoryMalloc((argc - 1) * sizeof(char *));
memset(global.envCmd, 0, (argc - 1) * sizeof(char *));
2022-02-22 11:49:11 +00:00
for (int32_t i = 1; i < argc; ++i) {
2021-11-22 02:03:41 +00:00
if (strcmp(argv[i], "-c") == 0) {
if (i < argc - 1) {
if (strlen(argv[++i]) >= PATH_MAX) {
printf("config file path overflow");
return -1;
}
2022-02-23 02:35:53 +00:00
tstrncpy(configDir, argv[i], PATH_MAX);
2021-11-22 02:03:41 +00:00
} else {
printf("'-c' requires a parameter, default is %s\n", configDir);
2021-11-22 02:03:41 +00:00
return -1;
}
2022-03-29 11:53:10 +00:00
} else if (strcmp(argv[i], "-a") == 0) {
tstrncpy(global.apolloUrl, argv[++i], PATH_MAX);
2022-09-26 08:18:48 +00:00
} else if (strcmp(argv[i], "-s") == 0) {
global.dumpSdb = true;
2022-04-25 05:57:55 +00:00
} else if (strcmp(argv[i], "-E") == 0) {
2022-03-29 11:53:10 +00:00
tstrncpy(global.envFile, argv[++i], PATH_MAX);
2022-03-31 07:11:09 +00:00
} else if (strcmp(argv[i], "-k") == 0) {
global.generateGrant = true;
2022-03-29 11:53:10 +00:00
} else if (strcmp(argv[i], "-C") == 0) {
global.dumpConfig = true;
2021-11-22 02:03:41 +00:00
} else if (strcmp(argv[i], "-V") == 0) {
2022-03-12 15:08:48 +00:00
global.printVersion = true;
2022-10-13 03:56:16 +00:00
#ifdef WINDOWS
2022-07-29 08:36:42 +00:00
} else if (strcmp(argv[i], "--win_service") == 0) {
global.winServiceMode = true;
2022-10-13 03:56:16 +00:00
#endif
2022-04-25 05:57:55 +00:00
} else if (strcmp(argv[i], "-e") == 0) {
global.envCmd[cmdEnvIndex] = argv[++i];
cmdEnvIndex++;
2023-02-17 09:40:14 +00:00
} else if (strcmp(argv[i], "-dm") == 0) {
global.memDbg = true;
2022-05-16 13:36:29 +00:00
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ||
2023-02-17 09:40:14 +00:00
strcmp(argv[i], "-?") == 0) {
global.printHelp = true;
2021-11-22 02:03:41 +00:00
} else {
}
2021-10-04 09:44:39 +00:00
}
2021-11-22 02:03:41 +00:00
return 0;
}
2022-12-08 02:50:24 +00:00
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);
}
2022-04-14 05:44:40 +00:00
static void dmGenerateGrant() { mndGenerateMachineCode(); }
2022-03-29 11:53:10 +00:00
2022-04-13 06:00:56 +00:00
static void dmPrintVersion() {
2022-03-29 11:53:10 +00:00
#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);
#ifdef TD_ENTERPRISE
printf("gitinfoOfInternal: %s\n", gitinfoOfInternal);
#endif
2022-03-29 11:53:10 +00:00
printf("buildInfo: %s\n", buildinfo);
}
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);
2022-09-26 08:32:14 +00:00
printf("%s%s%s%s\n", indent, "-s,", indent, DM_SDB_INFO);
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);
2023-02-17 09:40:14 +00:00
printf("%s%s%s%s\n", indent, "-dm,", indent, DM_MEM_DBG);
printf("%s%s%s%s\n", indent, "-V,", indent, DM_VERSION);
printf("\n\nReport bugs to %s.\n", DM_EMAIL);
}
2022-04-13 06:00:56 +00:00
static void dmDumpCfg() {
2022-03-29 11:53:10 +00:00
SConfig *pCfg = taosGetCfg();
cfgDumpCfg(pCfg, 0, true);
2022-03-29 11:53:10 +00:00
}
2022-04-13 06:00:56 +00:00
static int32_t dmInitLog() {
return taosCreateLog(CUS_PROMPT"dlog", 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0);
2022-03-29 13:13:09 +00:00
}
2022-04-25 05:57:55 +00:00
static void taosCleanupArgs() {
2022-12-29 06:12:09 +00:00
if (global.envCmd != NULL) taosMemoryFreeClear(global.envCmd);
2022-04-25 05:57:55 +00:00
}
2021-11-22 02:03:41 +00:00
int main(int argc, char const *argv[]) {
#ifdef TD_JEMALLOC_ENABLED
bool jeBackgroundThread = true;
mallctl("background_thread", NULL, NULL, &jeBackgroundThread, sizeof(bool));
#endif
2022-07-20 00:39:52 +00:00
if (!taosCheckSystemIsLittleEnd()) {
printf("failed to start since on non-little-end machines\n");
return -1;
}
2022-04-13 06:00:56 +00:00
if (dmParseArgs(argc, argv) != 0) {
2022-03-29 11:53:10 +00:00
printf("failed to start since parse args error\n");
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2021-11-22 02:03:41 +00:00
return -1;
}
2022-07-29 08:36:42 +00:00
#ifdef WINDOWS
2022-10-13 03:56:16 +00:00
int mainWindows(int argc, char **argv);
2022-07-29 08:36:42 +00:00
if (global.winServiceMode) {
stratWindowsService(mainWindows);
} else {
return mainWindows(argc, argv);
}
return 0;
}
2022-10-13 03:56:16 +00:00
int mainWindows(int argc, char **argv) {
2022-07-29 08:36:42 +00:00
#endif
2022-03-12 15:08:48 +00:00
if (global.generateGrant) {
2022-04-13 06:00:56 +00:00
dmGenerateGrant();
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2022-02-23 02:35:53 +00:00
return 0;
}
if (global.printHelp) {
dmPrintHelp();
taosCleanupArgs();
return 0;
}
2022-03-12 15:08:48 +00:00
if (global.printVersion) {
2022-04-13 06:00:56 +00:00
dmPrintVersion();
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2022-02-23 02:35:53 +00:00
return 0;
2021-11-22 02:03:41 +00:00
}
2023-02-17 09:40:14 +00:00
#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;
}
tsAsyncLog = false;
2023-02-17 09:40:14 +00:00
printf("memory dbg enabled\n");
}
#endif
2022-04-13 06:00:56 +00:00
if (dmInitLog() != 0) {
2022-05-20 09:36:42 +00:00
printf("failed to start since init log error\n");
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2022-02-22 11:49:11 +00:00
return -1;
2021-11-22 02:03:41 +00:00
}
2022-12-08 02:50:24 +00:00
dmPrintArgs(argc, argv);
2022-04-25 06:05:09 +00:00
if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
2022-03-29 11:53:10 +00:00
dError("failed to start since read config error");
2022-05-20 09:36:42 +00:00
taosCloseLog();
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2021-11-22 02:03:41 +00:00
return -1;
}
2022-10-13 03:56:16 +00:00
2022-12-07 14:24:47 +00:00
if (taosConvInit() != 0) {
dError("failed to init conv");
taosCloseLog();
taosCleanupArgs();
return -1;
}
2021-11-22 02:03:41 +00:00
2022-03-12 15:08:48 +00:00
if (global.dumpConfig) {
2022-04-13 06:00:56 +00:00
dmDumpCfg();
2022-02-24 11:06:39 +00:00
taosCleanupCfg();
2022-03-12 14:56:56 +00:00
taosCloseLog();
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2022-10-19 09:53:01 +00:00
taosConvDestroy();
2021-11-22 02:03:41 +00:00
return 0;
}
2022-09-26 08:18:48 +00:00
if (global.dumpSdb) {
mndDumpSdb();
taosCleanupCfg();
taosCloseLog();
taosCleanupArgs();
2022-10-19 09:53:01 +00:00
taosConvDestroy();
2022-09-26 08:18:48 +00:00
return 0;
}
2022-10-19 09:53:01 +00:00
osSetProcPath(argc, (char **)argv);
2022-04-25 05:57:55 +00:00
taosCleanupArgs();
2022-05-16 13:36:29 +00:00
2022-10-18 10:28:13 +00:00
if (dmInit() != 0) {
2023-07-14 11:19:46 +00:00
if (terrno == TSDB_CODE_NOT_FOUND) {
dError("failed to init dnode since unsupported platform, please visit https://www.taosdata.com for support");
} else {
dError("failed to init dnode since %s", terrstr());
}
2022-12-28 02:20:18 +00:00
taosCleanupCfg();
taosCloseLog();
taosConvDestroy();
2022-05-16 13:36:29 +00:00
return -1;
}
2022-07-08 07:20:15 +00:00
dInfo("start to init service");
2022-05-16 13:36:29 +00:00
dmSetSignalHandle();
2023-07-19 07:30:14 +00:00
tsDndStart = taosGetTimestampMs();
2023-07-23 15:25:58 +00:00
tsDndStartOsUptime = taosGetOsUptime();
2022-05-16 13:36:29 +00:00
int32_t code = dmRun();
dInfo("shutting down the service");
dmCleanup();
return code;
2021-11-22 02:03:41 +00:00
}