dndMain.c 4.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 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
  bool    dumpConfig;
  bool    generateGrant;
  bool    printAuth;
  bool    printVersion;
  char    envFile[PATH_MAX];
  char    apolloUrl[PATH_MAX];
  SDnode *pDnode;
S
shm  
Shengliang Guan 已提交
28
} global = {0};
S
Shengliang Guan 已提交
29

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

S
shm  
Shengliang Guan 已提交
38 39 40 41 42 43
static void dndSetSignalHandle() {
  taosSetSignal(SIGTERM, dndSigintHandle);
  taosSetSignal(SIGHUP, dndSigintHandle);
  taosSetSignal(SIGINT, dndSigintHandle);
  taosSetSignal(SIGABRT, dndSigintHandle);
  taosSetSignal(SIGBREAK, dndSigintHandle);
S
Shengliang Guan 已提交
44 45
}

S
Shengliang Guan 已提交
46
static int32_t dndParseArgs(int32_t argc, char const *argv[]) {
S
Shengliang Guan 已提交
47
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
48 49 50 51 52 53
    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 已提交
54
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
55
      } else {
56
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
57 58
        return -1;
      }
S
Shengliang Guan 已提交
59 60 61 62
    } 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 已提交
63
    } else if (strcmp(argv[i], "-k") == 0) {
S
shm  
Shengliang Guan 已提交
64
      global.generateGrant = true;
S
Shengliang Guan 已提交
65 66
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
S
Shengliang Guan 已提交
67
    } else if (strcmp(argv[i], "-V") == 0) {
S
shm  
Shengliang Guan 已提交
68
      global.printVersion = true;
S
Shengliang Guan 已提交
69 70
    } else {
    }
S
Shengliang Guan 已提交
71 72
  }

S
Shengliang Guan 已提交
73 74 75
  return 0;
}

S
Shengliang Guan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static void dndGenerateGrant() {
  // grantParseParameter();
}

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);
}

SDnodeOpt dndGetOpt() {
  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);
  option.pDisks = tsDiskCfg;
  option.numOfDisks = tsDiskCfgNum;
  return option;
}

S
shm  
Shengliang Guan 已提交
112
static int32_t dndRunDnode() {
S
config  
Shengliang Guan 已提交
113
  if (dndInit() != 0) {
S
Shengliang Guan 已提交
114
    dError("failed to initialize environment since %s", terrstr());
S
Shengliang Guan 已提交
115 116
    return -1;
  }
S
Shengliang Guan 已提交
117

S
shm  
Shengliang Guan 已提交
118 119 120
  SDnodeOpt option = dndGetOpt();

  SDnode *pDnode = dndCreate(&option);
121
  if (pDnode == NULL) {
S
shm  
Shengliang Guan 已提交
122
    dError("failed to to create dnode object since %s", terrstr());
S
Shengliang Guan 已提交
123
    return -1;
S
shm  
Shengliang Guan 已提交
124
  } else {
S
shm  
Shengliang Guan 已提交
125 126
    global.pDnode = pDnode;
    dndSetSignalHandle();
S
Shengliang Guan 已提交
127 128
  }

S
shm  
Shengliang Guan 已提交
129
  dInfo("start the TDengine service");
S
shm  
Shengliang Guan 已提交
130
  int32_t code = dndRun(pDnode);
S
shm  
Shengliang Guan 已提交
131
  dInfo("start shutting down the TDengine service");
S
Shengliang Guan 已提交
132

S
shm  
Shengliang Guan 已提交
133
  global.pDnode = NULL;
S
Shengliang Guan 已提交
134 135
  dndClose(pDnode);
  dndCleanup();
S
Shengliang Guan 已提交
136
  taosCloseLog();
S
config  
Shengliang Guan 已提交
137
  taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
138
  return code;
H
refact  
Hongze Cheng 已提交
139
}
S
Shengliang Guan 已提交
140 141

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

S
Shengliang Guan 已提交
147 148
  if (dndParseArgs(argc, argv) != 0) {
    printf("failed to start since parse args error\n");
S
Shengliang Guan 已提交
149 150 151
    return -1;
  }

S
shm  
Shengliang Guan 已提交
152 153
  if (global.generateGrant) {
    dndGenerateGrant();
S
Shengliang Guan 已提交
154 155 156
    return 0;
  }

S
shm  
Shengliang Guan 已提交
157 158
  if (global.printVersion) {
    dndPrintVersion();
S
Shengliang Guan 已提交
159
    return 0;
S
Shengliang Guan 已提交
160 161
  }

S
shm  
Shengliang Guan 已提交
162
  if (taosCreateLog("taosdlog", 1, configDir, global.envFile, global.apolloUrl, NULL, 0) != 0) {
S
Shengliang Guan 已提交
163
    printf("failed to start since read log config error\n");
S
Shengliang Guan 已提交
164
    return -1;
S
Shengliang Guan 已提交
165 166
  }

S
shm  
Shengliang Guan 已提交
167
  if (taosInitCfg(configDir, global.envFile, global.apolloUrl, NULL, 0) != 0) {
S
Shengliang Guan 已提交
168
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
169 170 171
    return -1;
  }

S
shm  
Shengliang Guan 已提交
172 173
  if (global.dumpConfig) {
    dndDumpCfg();
S
Shengliang Guan 已提交
174
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
175
    taosCloseLog();
S
Shengliang Guan 已提交
176 177 178
    return 0;
  }

S
shm  
Shengliang Guan 已提交
179
  return dndRunDnode();
S
Shengliang Guan 已提交
180
}