dmMain.c 5.8 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 "dmImp.h"
S
Shengliang Guan 已提交
18
#include "tconfig.h"
H
refact  
Hongze Cheng 已提交
19

S
Shengliang Guan 已提交
20
static struct {
S
Shengliang Guan 已提交
21 22 23 24 25 26 27 28
  bool         dumpConfig;
  bool         generateGrant;
  bool         printAuth;
  bool         printVersion;
  char         envFile[PATH_MAX];
  char         apolloUrl[PATH_MAX];
  SArray      *pArgs;  // SConfigPair
  SDnode      *pDnode;
S
Shengliang Guan 已提交
29
  EDndNodeType ntype;
S
shm  
Shengliang Guan 已提交
30
} global = {0};
S
Shengliang Guan 已提交
31

S
Shengliang Guan 已提交
32
static void dmStopDnode(int signum, void *info, void *ctx) {
S
shm  
Shengliang Guan 已提交
33 34
  SDnode *pDnode = atomic_val_compare_exchange_ptr(&global.pDnode, 0, global.pDnode);
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
35
    dmSetEvent(pDnode, DND_EVENT_STOP);
S
shm  
Shengliang Guan 已提交
36
  }
37
}
S
Shengliang Guan 已提交
38

S
Shengliang Guan 已提交
39 40 41 42 43 44 45 46
static void dmSetSignalHandle() {
  taosSetSignal(SIGTERM, dmStopDnode);
  taosSetSignal(SIGHUP, dmStopDnode);
  taosSetSignal(SIGINT, dmStopDnode);
  taosSetSignal(SIGTSTP, dmStopDnode);
  taosSetSignal(SIGABRT, dmStopDnode);
  taosSetSignal(SIGBREAK, dmStopDnode);
  taosSetSignal(SIGQUIT, dmStopDnode);
S
Shengliang Guan 已提交
47 48

  if (!tsMultiProcess) {
S
Shengliang Guan 已提交
49
  } else if (global.ntype == DNODE || global.ntype == NODE_END) {
S
Shengliang Guan 已提交
50
    taosIgnSignal(SIGCHLD);
S
Shengliang Guan 已提交
51
  } else {
S
shm  
Shengliang Guan 已提交
52
    taosKillChildOnParentStopped();
S
Shengliang Guan 已提交
53
  }
S
Shengliang Guan 已提交
54 55
}

S
Shengliang Guan 已提交
56
static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
S
Shengliang Guan 已提交
57
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
58 59 60 61 62 63
    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 已提交
64
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
65
      } else {
66
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
67 68
        return -1;
      }
S
Shengliang Guan 已提交
69 70 71 72
    } else if (strcmp(argv[i], "-a") == 0) {
      tstrncpy(global.apolloUrl, argv[++i], PATH_MAX);
    } else if (strcmp(argv[i], "-e") == 0) {
      tstrncpy(global.envFile, argv[++i], PATH_MAX);
S
Shengliang Guan 已提交
73
    } else if (strcmp(argv[i], "-n") == 0) {
S
shm  
Shengliang Guan 已提交
74
      global.ntype = atoi(argv[++i]);
S
Shengliang Guan 已提交
75
      if (global.ntype <= DNODE || global.ntype > NODE_END) {
S
Shengliang Guan 已提交
76
        printf("'-n' range is [1 - %d], default is 0\n", NODE_END - 1);
S
shm  
Shengliang Guan 已提交
77 78
        return -1;
      }
S
shm  
Shengliang Guan 已提交
79 80
    } else if (strcmp(argv[i], "-k") == 0) {
      global.generateGrant = true;
S
Shengliang Guan 已提交
81 82
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
S
Shengliang Guan 已提交
83
    } else if (strcmp(argv[i], "-V") == 0) {
S
shm  
Shengliang Guan 已提交
84
      global.printVersion = true;
S
Shengliang Guan 已提交
85 86
    } else {
    }
S
Shengliang Guan 已提交
87 88
  }

S
Shengliang Guan 已提交
89 90 91
  return 0;
}

S
Shengliang Guan 已提交
92
static void dmGenerateGrant() { mndGenerateMachineCode(); }
S
Shengliang Guan 已提交
93

S
Shengliang Guan 已提交
94
static void dmPrintVersion() {
S
Shengliang Guan 已提交
95 96 97 98 99 100 101 102 103 104
#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);
}

S
Shengliang Guan 已提交
105
static void dmDumpCfg() {
S
Shengliang Guan 已提交
106 107 108 109
  SConfig *pCfg = taosGetCfg();
  cfgDumpCfg(pCfg, 0, 1);
}

S
Shengliang Guan 已提交
110
static SDnodeOpt dmGetOpt() {
S
Shengliang Guan 已提交
111 112 113 114 115 116 117 118 119 120
  SConfig  *pCfg = taosGetCfg();
  SDnodeOpt option = {0};

  option.numOfSupportVnodes = cfgGetItem(pCfg, "supportVnodes")->i32;
  tstrncpy(option.dataDir, tsDataDir, sizeof(option.dataDir));
  tstrncpy(option.firstEp, tsFirst, sizeof(option.firstEp));
  tstrncpy(option.secondEp, tsSecond, sizeof(option.firstEp));
  option.serverPort = tsServerPort;
  tstrncpy(option.localFqdn, tsLocalFqdn, sizeof(option.localFqdn));
  snprintf(option.localEp, sizeof(option.localEp), "%s:%u", option.localFqdn, option.serverPort);
S
Shengliang Guan 已提交
121
  option.disks = tsDiskCfg;
S
Shengliang Guan 已提交
122
  option.numOfDisks = tsDiskCfgNum;
S
Shengliang Guan 已提交
123
  option.ntype = global.ntype;
S
Shengliang Guan 已提交
124 125 126
  return option;
}

S
Shengliang Guan 已提交
127
static int32_t dmInitLog() {
S
shm  
Shengliang Guan 已提交
128
  char logName[12] = {0};
S
Shengliang Guan 已提交
129
  snprintf(logName, sizeof(logName), "%slog", dmLogName(global.ntype));
S
shm  
Shengliang Guan 已提交
130 131 132
  return taosCreateLog(logName, 1, configDir, global.envFile, global.apolloUrl, global.pArgs, 0);
}

S
Shengliang Guan 已提交
133
static void dmSetProcInfo(int32_t argc, char **argv) {
S
shm  
Shengliang Guan 已提交
134
  taosSetProcPath(argc, argv);
S
Shengliang Guan 已提交
135
  if (global.ntype != DNODE && global.ntype != NODE_END) {
S
Shengliang Guan 已提交
136
    const char *name = dmProcName(global.ntype);
S
shm  
Shengliang Guan 已提交
137
    taosSetProcName(argc, argv, name);
S
shm  
Shengliang Guan 已提交
138 139 140
  }
}

S
Shengliang Guan 已提交
141 142
static int32_t dmRunDnode() {
  if (dmInit() != 0) {
S
shm  
Shengliang Guan 已提交
143
    dError("failed to init environment since %s", terrstr());
S
Shengliang Guan 已提交
144 145
    return -1;
  }
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148
  SDnodeOpt option = dmGetOpt();
  SDnode   *pDnode = dmCreate(&option);
149
  if (pDnode == NULL) {
S
shm  
Shengliang Guan 已提交
150
    dError("failed to to create dnode since %s", terrstr());
S
Shengliang Guan 已提交
151
    return -1;
S
shm  
Shengliang Guan 已提交
152
  } else {
S
shm  
Shengliang Guan 已提交
153
    global.pDnode = pDnode;
S
Shengliang Guan 已提交
154
    dmSetSignalHandle();
S
Shengliang Guan 已提交
155 156
  }

S
Shengliang Guan 已提交
157
  dInfo("start the service");
S
Shengliang Guan 已提交
158
  int32_t code = dmRun(pDnode);
S
Shengliang Guan 已提交
159
  dInfo("start shutting down the service");
S
Shengliang Guan 已提交
160

S
shm  
Shengliang Guan 已提交
161
  global.pDnode = NULL;
S
Shengliang Guan 已提交
162 163
  dmClose(pDnode);
  dmCleanup();
S
Shengliang Guan 已提交
164
  taosCloseLog();
S
config  
Shengliang Guan 已提交
165
  taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
166
  return code;
H
refact  
Hongze Cheng 已提交
167
}
S
Shengliang Guan 已提交
168 169

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

S
Shengliang Guan 已提交
175
  if (dmParseArgs(argc, argv) != 0) {
S
Shengliang Guan 已提交
176
    printf("failed to start since parse args error\n");
S
Shengliang Guan 已提交
177 178 179
    return -1;
  }

S
shm  
Shengliang Guan 已提交
180
  if (global.generateGrant) {
S
Shengliang Guan 已提交
181
    dmGenerateGrant();
S
Shengliang Guan 已提交
182 183 184
    return 0;
  }

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

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

S
shm  
Shengliang Guan 已提交
195
  if (taosInitCfg(configDir, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
196
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
197 198 199
    return -1;
  }

S
shm  
Shengliang Guan 已提交
200
  if (global.dumpConfig) {
S
Shengliang Guan 已提交
201
    dmDumpCfg();
S
Shengliang Guan 已提交
202
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
203
    taosCloseLog();
S
Shengliang Guan 已提交
204 205 206
    return 0;
  }

S
Shengliang Guan 已提交
207 208
  dmSetProcInfo(argc, (char **)argv);
  return dmRunDnode();
S
Shengliang Guan 已提交
209
}