dmMain.c 6.9 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 "tconfig.h"
H
refact  
Hongze Cheng 已提交
19

20 21 22 23 24 25 26 27 28
#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."
#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_NODE_TYPE     "Startup type of the node, default is 0."
#define DM_MACHINE_CODE  "Get machine code."
#define DM_VERSION       "Print program version."
#define DM_EMAIL         "<support@taosdata.com>"
S
Shengliang Guan 已提交
29
static struct {
S
Shengliang Guan 已提交
30 31 32 33
  bool         dumpConfig;
  bool         generateGrant;
  bool         printAuth;
  bool         printVersion;
34
  bool         printHelp;
S
Shengliang Guan 已提交
35 36
  char         envFile[PATH_MAX];
  char         apolloUrl[PATH_MAX];
wafwerar's avatar
wafwerar 已提交
37
  const char **envCmd;
S
Shengliang Guan 已提交
38
  SArray      *pArgs;  // SConfigPair
S
Shengliang Guan 已提交
39
  EDndNodeType ntype;
S
shm  
Shengliang Guan 已提交
40
} global = {0};
S
Shengliang Guan 已提交
41

42
static void dmStopDnode(int signum, void *info, void *ctx) { dmStop(); }
S
Shengliang Guan 已提交
43

S
Shengliang Guan 已提交
44 45 46 47 48 49
static void dmSetSignalHandle() {
  taosSetSignal(SIGTERM, dmStopDnode);
  taosSetSignal(SIGHUP, dmStopDnode);
  taosSetSignal(SIGINT, dmStopDnode);
  taosSetSignal(SIGABRT, dmStopDnode);
  taosSetSignal(SIGBREAK, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
50 51
#ifndef WINDOWS
  taosSetSignal(SIGTSTP, dmStopDnode);
S
Shengliang Guan 已提交
52
  taosSetSignal(SIGQUIT, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
53
#endif
S
Shengliang Guan 已提交
54 55

  if (!tsMultiProcess) {
S
Shengliang Guan 已提交
56
  } else if (global.ntype == DNODE || global.ntype == NODE_END) {
S
Shengliang Guan 已提交
57
    taosIgnSignal(SIGCHLD);
S
Shengliang Guan 已提交
58
  } else {
S
shm  
Shengliang Guan 已提交
59
    taosKillChildOnParentStopped();
S
Shengliang Guan 已提交
60
  }
S
Shengliang Guan 已提交
61 62
}

S
Shengliang Guan 已提交
63
static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
wafwerar's avatar
wafwerar 已提交
64
  int32_t cmdEnvIndex = 0;
wafwerar's avatar
wafwerar 已提交
65
  if (argc < 2) return 0;
66 67
  global.envCmd = taosMemoryMalloc((argc - 1) * sizeof(char *));
  memset(global.envCmd, 0, (argc - 1) * sizeof(char *));
S
Shengliang Guan 已提交
68
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
69 70 71 72 73 74
    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 已提交
75
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
76
      } else {
77
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
78 79
        return -1;
      }
S
Shengliang Guan 已提交
80 81
    } else if (strcmp(argv[i], "-a") == 0) {
      tstrncpy(global.apolloUrl, argv[++i], PATH_MAX);
wafwerar's avatar
wafwerar 已提交
82
    } else if (strcmp(argv[i], "-E") == 0) {
S
Shengliang Guan 已提交
83
      tstrncpy(global.envFile, argv[++i], PATH_MAX);
S
Shengliang Guan 已提交
84
    } else if (strcmp(argv[i], "-n") == 0) {
S
shm  
Shengliang Guan 已提交
85
      global.ntype = atoi(argv[++i]);
S
Shengliang Guan 已提交
86
      if (global.ntype <= DNODE || global.ntype > NODE_END) {
S
Shengliang Guan 已提交
87
        printf("'-n' range is [1 - %d], default is 0\n", NODE_END - 1);
S
shm  
Shengliang Guan 已提交
88 89
        return -1;
      }
S
shm  
Shengliang Guan 已提交
90 91
    } else if (strcmp(argv[i], "-k") == 0) {
      global.generateGrant = true;
S
Shengliang Guan 已提交
92 93
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
S
Shengliang Guan 已提交
94
    } else if (strcmp(argv[i], "-V") == 0) {
S
shm  
Shengliang Guan 已提交
95
      global.printVersion = true;
wafwerar's avatar
wafwerar 已提交
96 97 98
    } else if (strcmp(argv[i], "-e") == 0) {
      global.envCmd[cmdEnvIndex] = argv[++i];
      cmdEnvIndex++;
99 100
    } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ||
               strcmp(argv[i], "-?")) {
101
      global.printHelp = true;
S
Shengliang Guan 已提交
102 103
    } else {
    }
S
Shengliang Guan 已提交
104 105
  }

S
Shengliang Guan 已提交
106 107 108
  return 0;
}

S
Shengliang Guan 已提交
109
static void dmGenerateGrant() { mndGenerateMachineCode(); }
S
Shengliang Guan 已提交
110

S
Shengliang Guan 已提交
111
static void dmPrintVersion() {
S
Shengliang Guan 已提交
112 113 114 115 116 117 118 119 120 121
#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);
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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);
  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, "-n,", indent, DM_NODE_TYPE);
  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 已提交
137
static void dmDumpCfg() {
S
Shengliang Guan 已提交
138
  SConfig *pCfg = taosGetCfg();
139
  cfgDumpCfg(pCfg, 0, true);
S
Shengliang Guan 已提交
140 141
}

S
Shengliang Guan 已提交
142
static int32_t dmInitLog() {
S
shm  
Shengliang Guan 已提交
143
  char logName[12] = {0};
S
Shengliang Guan 已提交
144
  snprintf(logName, sizeof(logName), "%slog", dmNodeLogName(global.ntype));
wafwerar's avatar
wafwerar 已提交
145
  return taosCreateLog(logName, 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0);
S
shm  
Shengliang Guan 已提交
146 147
}

S
Shengliang Guan 已提交
148
static void dmSetProcInfo(int32_t argc, char **argv) {
S
shm  
Shengliang Guan 已提交
149
  taosSetProcPath(argc, argv);
S
Shengliang Guan 已提交
150
  if (global.ntype != DNODE && global.ntype != NODE_END) {
S
Shengliang Guan 已提交
151
    const char *name = dmNodeProcName(global.ntype);
S
shm  
Shengliang Guan 已提交
152
    taosSetProcName(argc, argv, name);
S
shm  
Shengliang Guan 已提交
153 154 155
  }
}

wafwerar's avatar
wafwerar 已提交
156 157 158 159
static void taosCleanupArgs() {
  if (global.envCmd != NULL) taosMemoryFree(global.envCmd);
}

S
Shengliang Guan 已提交
160
int main(int argc, char const *argv[]) {
161
  if (!taosCheckSystemIsSmallEnd()) {
S
Shengliang Guan 已提交
162
    printf("failed to start since on non-small-end machines\n");
163 164 165
    return -1;
  }

S
Shengliang Guan 已提交
166
  if (dmParseArgs(argc, argv) != 0) {
S
Shengliang Guan 已提交
167
    printf("failed to start since parse args error\n");
wafwerar's avatar
wafwerar 已提交
168
    taosCleanupArgs();
S
Shengliang Guan 已提交
169 170 171
    return -1;
  }

S
shm  
Shengliang Guan 已提交
172
  if (global.generateGrant) {
S
Shengliang Guan 已提交
173
    dmGenerateGrant();
wafwerar's avatar
wafwerar 已提交
174
    taosCleanupArgs();
S
Shengliang Guan 已提交
175 176 177
    return 0;
  }

178 179 180 181 182 183
  if (global.printHelp) {
    dmPrintHelp();
    taosCleanupArgs();
    return 0;
  }

S
shm  
Shengliang Guan 已提交
184
  if (global.printVersion) {
S
Shengliang Guan 已提交
185
    dmPrintVersion();
wafwerar's avatar
wafwerar 已提交
186
    taosCleanupArgs();
S
Shengliang Guan 已提交
187
    return 0;
S
Shengliang Guan 已提交
188 189
  }

S
Shengliang Guan 已提交
190
  if (dmInitLog() != 0) {
S
Shengliang Guan 已提交
191
    printf("failed to start since init log error\n");
wafwerar's avatar
wafwerar 已提交
192
    taosCleanupArgs();
S
Shengliang Guan 已提交
193
    return -1;
S
Shengliang Guan 已提交
194 195
  }

wafwerar's avatar
wafwerar 已提交
196
  if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
197
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
198
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
199
    taosCleanupArgs();
S
Shengliang Guan 已提交
200 201 202
    return -1;
  }

S
shm  
Shengliang Guan 已提交
203
  if (global.dumpConfig) {
S
Shengliang Guan 已提交
204
    dmDumpCfg();
S
Shengliang Guan 已提交
205
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
206
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
207
    taosCleanupArgs();
S
Shengliang Guan 已提交
208 209 210
    return 0;
  }

S
Shengliang Guan 已提交
211
  dmSetProcInfo(argc, (char **)argv);
wafwerar's avatar
wafwerar 已提交
212
  taosCleanupArgs();
213 214 215 216 217 218

  if (dmInit(global.ntype) != 0) {
    dError("failed to init dnode since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
219
  dInfo("start to open dnode");
220 221 222 223 224 225
  dmSetSignalHandle();
  int32_t code = dmRun();
  dInfo("shutting down the service");

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