dndMain.c 3.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
shm  
Shengliang Guan 已提交
17
#include "dndMain.h"
H
refact  
Hongze Cheng 已提交
18

S
Shengliang Guan 已提交
19
static struct {
S
shm  
Shengliang Guan 已提交
20 21 22 23 24 25 26
  bool    dumpConfig;
  bool    generateGrant;
  bool    printAuth;
  bool    printVersion;
  char    envFile[PATH_MAX];
  char    apolloUrl[PATH_MAX];
  SDnode *pDnode;
S
shm  
Shengliang Guan 已提交
27
} global = {0};
S
Shengliang Guan 已提交
28

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

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

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

S
Shengliang Guan 已提交
68 69 70
  return 0;
}

S
shm  
Shengliang Guan 已提交
71
static int32_t dndRunDnode() {
S
config  
Shengliang Guan 已提交
72
  if (dndInit() != 0) {
S
shm  
Shengliang Guan 已提交
73
    dInfo("failed to initialize dnode environment since %s", terrstr());
S
Shengliang Guan 已提交
74 75
    return -1;
  }
S
Shengliang Guan 已提交
76

S
shm  
Shengliang Guan 已提交
77 78 79
  SDnodeOpt option = dndGetOpt();

  SDnode *pDnode = dndCreate(&option);
80
  if (pDnode == NULL) {
S
shm  
Shengliang Guan 已提交
81
    dError("failed to to create dnode object since %s", terrstr());
S
Shengliang Guan 已提交
82
    return -1;
S
shm  
Shengliang Guan 已提交
83
  } else {
S
shm  
Shengliang Guan 已提交
84 85
    global.pDnode = pDnode;
    dndSetSignalHandle();
S
Shengliang Guan 已提交
86 87
  }

S
shm  
Shengliang Guan 已提交
88
  dInfo("start the TDengine service");
S
shm  
Shengliang Guan 已提交
89
  int32_t code = dndRun(pDnode);
S
shm  
Shengliang Guan 已提交
90
  dInfo("start shutting down the TDengine service");
S
Shengliang Guan 已提交
91

S
shm  
Shengliang Guan 已提交
92
  global.pDnode = NULL;
S
Shengliang Guan 已提交
93 94
  dndClose(pDnode);
  dndCleanup();
S
Shengliang Guan 已提交
95
  taosCloseLog();
S
config  
Shengliang Guan 已提交
96
  taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
97
  return code;
H
refact  
Hongze Cheng 已提交
98
}
S
Shengliang Guan 已提交
99 100

int main(int argc, char const *argv[]) {
101
  if (!taosCheckSystemIsSmallEnd()) {
S
Shengliang Guan 已提交
102
    dError("failed to start since its on non-small-end machines");
103 104 105
    return -1;
  }

S
shm  
Shengliang Guan 已提交
106
  if (dndParseOption(argc, argv) != 0) {
S
Shengliang Guan 已提交
107 108 109
    return -1;
  }

S
shm  
Shengliang Guan 已提交
110 111
  if (global.generateGrant) {
    dndGenerateGrant();
S
Shengliang Guan 已提交
112 113 114
    return 0;
  }

S
shm  
Shengliang Guan 已提交
115 116
  if (global.printVersion) {
    dndPrintVersion();
S
Shengliang Guan 已提交
117
    return 0;
S
Shengliang Guan 已提交
118 119
  }

S
shm  
Shengliang Guan 已提交
120
  if (taosCreateLog("taosdlog", 1, configDir, global.envFile, global.apolloUrl, NULL, 0) != 0) {
S
shm  
Shengliang Guan 已提交
121
    dError("failed to start TDengine since read log config error");
S
Shengliang Guan 已提交
122
    return -1;
S
Shengliang Guan 已提交
123 124
  }

S
shm  
Shengliang Guan 已提交
125
  if (taosInitCfg(configDir, global.envFile, global.apolloUrl, NULL, 0) != 0) {
S
shm  
Shengliang Guan 已提交
126
    dError("failed to start TDengine since read config error");
S
Shengliang Guan 已提交
127 128 129
    return -1;
  }

S
shm  
Shengliang Guan 已提交
130 131
  if (global.dumpConfig) {
    dndDumpCfg();
S
Shengliang Guan 已提交
132
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
133
    taosCloseLog();
S
Shengliang Guan 已提交
134 135 136
    return 0;
  }

S
shm  
Shengliang Guan 已提交
137
  return dndRunDnode();
S
Shengliang Guan 已提交
138
}