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

21 22 23 24 25 26 27 28 29
#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 已提交
30
static struct {
wafwerar's avatar
wafwerar 已提交
31 32 33
#ifdef WINDOWS
  bool         winServiceMode;
#endif
S
Shengliang Guan 已提交
34
  bool         dumpConfig;
S
Shengliang Guan 已提交
35
  bool         dumpSdb;
S
Shengliang Guan 已提交
36 37 38
  bool         generateGrant;
  bool         printAuth;
  bool         printVersion;
39
  bool         printHelp;
S
Shengliang Guan 已提交
40 41
  char         envFile[PATH_MAX];
  char         apolloUrl[PATH_MAX];
wafwerar's avatar
wafwerar 已提交
42
  const char **envCmd;
S
Shengliang Guan 已提交
43
  SArray      *pArgs;  // SConfigPair
S
Shengliang Guan 已提交
44
  EDndNodeType ntype;
S
shm  
Shengliang Guan 已提交
45
} global = {0};
S
Shengliang Guan 已提交
46

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

S
Shengliang Guan 已提交
49 50 51 52 53 54
static void dmSetSignalHandle() {
  taosSetSignal(SIGTERM, dmStopDnode);
  taosSetSignal(SIGHUP, dmStopDnode);
  taosSetSignal(SIGINT, dmStopDnode);
  taosSetSignal(SIGABRT, dmStopDnode);
  taosSetSignal(SIGBREAK, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
55 56
#ifndef WINDOWS
  taosSetSignal(SIGTSTP, dmStopDnode);
S
Shengliang Guan 已提交
57
  taosSetSignal(SIGQUIT, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
58
#endif
S
Shengliang Guan 已提交
59 60

  if (!tsMultiProcess) {
S
Shengliang Guan 已提交
61
  } else if (global.ntype == DNODE || global.ntype == NODE_END) {
S
Shengliang Guan 已提交
62
    taosIgnSignal(SIGCHLD);
S
Shengliang Guan 已提交
63
  } else {
S
shm  
Shengliang Guan 已提交
64
    taosKillChildOnParentStopped();
S
Shengliang Guan 已提交
65
  }
S
Shengliang Guan 已提交
66 67
}

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

S
Shengliang Guan 已提交
117 118 119
  return 0;
}

S
Shengliang Guan 已提交
120
static void dmGenerateGrant() { mndGenerateMachineCode(); }
S
Shengliang Guan 已提交
121

S
Shengliang Guan 已提交
122
static void dmPrintVersion() {
S
Shengliang Guan 已提交
123 124 125 126 127 128 129 130 131 132
#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);
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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 已提交
148
static void dmDumpCfg() {
S
Shengliang Guan 已提交
149
  SConfig *pCfg = taosGetCfg();
150
  cfgDumpCfg(pCfg, 0, true);
S
Shengliang Guan 已提交
151 152
}

S
Shengliang Guan 已提交
153
static int32_t dmInitLog() {
S
shm  
Shengliang Guan 已提交
154
  char logName[12] = {0};
S
Shengliang Guan 已提交
155
  snprintf(logName, sizeof(logName), "%slog", dmNodeLogName(global.ntype));
wafwerar's avatar
wafwerar 已提交
156
  return taosCreateLog(logName, 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0);
S
shm  
Shengliang Guan 已提交
157 158
}

S
Shengliang Guan 已提交
159
static void dmSetProcInfo(int32_t argc, char **argv) {
S
shm  
Shengliang Guan 已提交
160
  taosSetProcPath(argc, argv);
S
Shengliang Guan 已提交
161
  if (global.ntype != DNODE && global.ntype != NODE_END) {
S
Shengliang Guan 已提交
162
    const char *name = dmNodeProcName(global.ntype);
S
shm  
Shengliang Guan 已提交
163
    taosSetProcName(argc, argv, name);
S
shm  
Shengliang Guan 已提交
164 165 166
  }
}

wafwerar's avatar
wafwerar 已提交
167 168 169 170
static void taosCleanupArgs() {
  if (global.envCmd != NULL) taosMemoryFree(global.envCmd);
}

S
Shengliang Guan 已提交
171
int main(int argc, char const *argv[]) {
D
dapan1121 已提交
172 173
  if (!taosCheckSystemIsLittleEnd()) {
    printf("failed to start since on non-little-end machines\n");
174 175 176
    return -1;
  }

S
Shengliang Guan 已提交
177
  if (dmParseArgs(argc, argv) != 0) {
S
Shengliang Guan 已提交
178
    printf("failed to start since parse args error\n");
wafwerar's avatar
wafwerar 已提交
179
    taosCleanupArgs();
S
Shengliang Guan 已提交
180 181 182
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
183 184 185 186 187 188 189 190 191 192 193 194
#ifdef WINDOWS
  int mainWindows(int argc,char** argv);
  if (global.winServiceMode) {
    stratWindowsService(mainWindows);
  } else {
    return mainWindows(argc, argv);
  }
  return 0;
}
int mainWindows(int argc,char** argv) {
#endif

S
shm  
Shengliang Guan 已提交
195
  if (global.generateGrant) {
S
Shengliang Guan 已提交
196
    dmGenerateGrant();
wafwerar's avatar
wafwerar 已提交
197
    taosCleanupArgs();
S
Shengliang Guan 已提交
198 199 200
    return 0;
  }

201 202 203 204 205 206
  if (global.printHelp) {
    dmPrintHelp();
    taosCleanupArgs();
    return 0;
  }

S
shm  
Shengliang Guan 已提交
207
  if (global.printVersion) {
S
Shengliang Guan 已提交
208
    dmPrintVersion();
wafwerar's avatar
wafwerar 已提交
209
    taosCleanupArgs();
S
Shengliang Guan 已提交
210
    return 0;
S
Shengliang Guan 已提交
211 212
  }

S
Shengliang Guan 已提交
213
  if (dmInitLog() != 0) {
S
Shengliang Guan 已提交
214
    printf("failed to start since init log error\n");
wafwerar's avatar
wafwerar 已提交
215
    taosCleanupArgs();
S
Shengliang Guan 已提交
216
    return -1;
S
Shengliang Guan 已提交
217 218
  }

wafwerar's avatar
wafwerar 已提交
219
  if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
220
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
221
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
222
    taosCleanupArgs();
S
Shengliang Guan 已提交
223 224
    return -1;
  }
D
dapan1121 已提交
225 226
  
  taosConvInit();
S
Shengliang Guan 已提交
227

S
shm  
Shengliang Guan 已提交
228
  if (global.dumpConfig) {
S
Shengliang Guan 已提交
229
    dmDumpCfg();
S
Shengliang Guan 已提交
230
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
231
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
232
    taosCleanupArgs();
S
Shengliang Guan 已提交
233 234 235
    return 0;
  }

S
Shengliang Guan 已提交
236 237 238 239 240 241 242 243
  if (global.dumpSdb) {
    mndDumpSdb();
    taosCleanupCfg();
    taosCloseLog();
    taosCleanupArgs();
    return 0;
  }

S
Shengliang Guan 已提交
244
  dmSetProcInfo(argc, (char **)argv);
wafwerar's avatar
wafwerar 已提交
245
  taosCleanupArgs();
246 247 248 249 250 251

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

S
Shengliang Guan 已提交
252
  dInfo("start to init service");
253 254 255 256 257 258
  dmSetSignalHandle();
  int32_t code = dmRun();
  dInfo("shutting down the service");

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