dndMain.c 6.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 18
#include "dnd.h"
#include "tconfig.h"
H
refact  
Hongze Cheng 已提交
19

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

S
Shengliang Guan 已提交
32
static void dndStopDnode(int signum, void *info, void *ctx) {
S
shm  
Shengliang Guan 已提交
33
  dInfo("system signal:%d received", signum);
S
shm  
Shengliang Guan 已提交
34 35 36 37
  SDnode *pDnode = atomic_val_compare_exchange_ptr(&global.pDnode, 0, global.pDnode);
  if (pDnode != NULL) {
    dndHandleEvent(pDnode, DND_EVENT_STOP);
  }
38
}
S
Shengliang Guan 已提交
39

S
Shengliang Guan 已提交
40
static void dndHandleChild(int signum, void *info, void *ctx) {
S
shm  
Shengliang Guan 已提交
41 42 43 44
  dInfo("sigchild received");
  if (global.pDnode != NULL) {
    dndHandleEvent(global.pDnode, DND_EVENT_CHILD);
  }
S
Shengliang Guan 已提交
45 46
}

S
shm  
Shengliang Guan 已提交
47
static void dndSetSignalHandle() {
S
Shengliang Guan 已提交
48 49 50 51 52 53 54
  taosSetSignal(SIGTERM, dndStopDnode);
  taosSetSignal(SIGHUP, dndStopDnode);
  taosSetSignal(SIGINT, dndStopDnode);
  taosSetSignal(SIGABRT, dndStopDnode);
  taosSetSignal(SIGBREAK, dndStopDnode);

  if (!tsMultiProcess) {
S
shm  
Shengliang Guan 已提交
55
  } else if (global.ntype == DNODE || global.ntype == NODE_MAX) {
S
Shengliang Guan 已提交
56 57
    taosSetSignal(SIGCHLD, dndHandleChild);
  } else {
S
shm  
Shengliang Guan 已提交
58
    taosKillChildOnParentStopped();
S
Shengliang Guan 已提交
59
  }
S
Shengliang Guan 已提交
60 61
}

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

S
Shengliang Guan 已提交
95 96 97
  return 0;
}

S
Shengliang Guan 已提交
98 99
static void dndGenerateGrant() {
  // grantParseParameter();
S
Shengliang Guan 已提交
100
  printf("this feature is not implemented yet\n");
S
Shengliang Guan 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

static void dndPrintVersion() {
#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);
}

static void dndDumpCfg() {
  SConfig *pCfg = taosGetCfg();
  cfgDumpCfg(pCfg, 0, 1);
}

S
shm  
Shengliang Guan 已提交
119
static SDnodeOpt dndGetOpt() {
S
Shengliang Guan 已提交
120 121 122 123 124 125 126 127 128 129
  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 已提交
130
  option.disks = tsDiskCfg;
S
Shengliang Guan 已提交
131
  option.numOfDisks = tsDiskCfgNum;
S
Shengliang Guan 已提交
132
  option.ntype = global.ntype;
S
Shengliang Guan 已提交
133 134 135
  return option;
}

S
shm  
Shengliang Guan 已提交
136 137 138 139 140 141
static int32_t dndInitLog() {
  char logName[12] = {0};
  snprintf(logName, sizeof(logName), "%slog", dndNodeLogStr(global.ntype));
  return taosCreateLog(logName, 1, configDir, global.envFile, global.apolloUrl, global.pArgs, 0);
}

S
shm  
Shengliang Guan 已提交
142 143
static void dndSetProcInfo(int32_t argc, char **argv) {
  taosSetProcPath(argc, argv);
S
shm  
Shengliang Guan 已提交
144
  if (global.ntype != DNODE && global.ntype != NODE_MAX) {
S
shm  
Shengliang Guan 已提交
145
    const char *name = dndNodeProcStr(global.ntype);
S
shm  
Shengliang Guan 已提交
146
    taosSetProcName(argc, argv, name);
S
shm  
Shengliang Guan 已提交
147 148 149
  }
}

S
shm  
Shengliang Guan 已提交
150
static int32_t dndRunDnode() {
S
config  
Shengliang Guan 已提交
151
  if (dndInit() != 0) {
S
shm  
Shengliang Guan 已提交
152
    dError("failed to init environment since %s", terrstr());
S
Shengliang Guan 已提交
153 154
    return -1;
  }
S
Shengliang Guan 已提交
155

S
shm  
Shengliang Guan 已提交
156
  SDnodeOpt option = dndGetOpt();
S
Shengliang Guan 已提交
157
  SDnode   *pDnode = dndCreate(&option);
158
  if (pDnode == NULL) {
S
shm  
Shengliang Guan 已提交
159
    dError("failed to to create dnode since %s", terrstr());
S
Shengliang Guan 已提交
160
    return -1;
S
shm  
Shengliang Guan 已提交
161
  } else {
S
shm  
Shengliang Guan 已提交
162 163
    global.pDnode = pDnode;
    dndSetSignalHandle();
S
Shengliang Guan 已提交
164 165
  }

S
Shengliang Guan 已提交
166
  dInfo("start the service");
S
shm  
Shengliang Guan 已提交
167
  int32_t code = dndRun(pDnode);
S
Shengliang Guan 已提交
168
  dInfo("start shutting down the service");
S
Shengliang Guan 已提交
169

S
shm  
Shengliang Guan 已提交
170
  global.pDnode = NULL;
S
Shengliang Guan 已提交
171 172
  dndClose(pDnode);
  dndCleanup();
S
Shengliang Guan 已提交
173
  taosCloseLog();
S
config  
Shengliang Guan 已提交
174
  taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
175
  return code;
H
refact  
Hongze Cheng 已提交
176
}
S
Shengliang Guan 已提交
177 178

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

S
Shengliang Guan 已提交
184 185
  if (dndParseArgs(argc, argv) != 0) {
    printf("failed to start since parse args error\n");
S
Shengliang Guan 已提交
186 187 188
    return -1;
  }

S
shm  
Shengliang Guan 已提交
189 190
  if (global.generateGrant) {
    dndGenerateGrant();
S
Shengliang Guan 已提交
191 192 193
    return 0;
  }

S
shm  
Shengliang Guan 已提交
194 195
  if (global.printVersion) {
    dndPrintVersion();
S
Shengliang Guan 已提交
196
    return 0;
S
Shengliang Guan 已提交
197 198
  }

S
shm  
Shengliang Guan 已提交
199 200
  if (dndInitLog() != 0) {
    printf("failed to start since init log error\n");
S
Shengliang Guan 已提交
201
    return -1;
S
Shengliang Guan 已提交
202 203
  }

S
shm  
Shengliang Guan 已提交
204
  if (taosInitCfg(configDir, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
205
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
206 207 208
    return -1;
  }

S
shm  
Shengliang Guan 已提交
209 210
  if (global.dumpConfig) {
    dndDumpCfg();
S
Shengliang Guan 已提交
211
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
212
    taosCloseLog();
S
Shengliang Guan 已提交
213 214 215
    return 0;
  }

S
shm  
Shengliang Guan 已提交
216
  dndSetProcInfo(argc, (char **)argv);
S
shm  
Shengliang Guan 已提交
217
  return dndRunDnode();
S
Shengliang Guan 已提交
218
}