dnodeModule.c 4.1 KB
Newer Older
S
#1177  
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
S
slguan 已提交
18
#include "taosdef.h"
S
slguan 已提交
19
#include "tglobal.h"
J
jtao1735 已提交
20
#include "trpc.h"
S
dnode  
slguan 已提交
21 22 23
#include "mnode.h"
#include "http.h"
#include "monitor.h"
24
#include "dnodeInt.h"
S
#1177  
slguan 已提交
25 26
#include "dnodeModule.h"

S
slguan 已提交
27 28 29 30 31 32 33 34 35
typedef struct {
  bool      enable;
  char *    name;
  int32_t (*initFp)();
  int32_t (*startFp)();
  void    (*cleanUpFp)();
  void    (*stopFp)();
} SModule;

S
Shuduo Sang 已提交
36
static SModule  tsModule[TSDB_MOD_MAX] = {{0}};
S
slguan 已提交
37
static uint32_t tsModuleStatus = 0;
S
#1177  
slguan 已提交
38

S
slguan 已提交
39 40 41 42 43 44 45
static void dnodeSetModuleStatus(int32_t module) {
  tsModuleStatus |= (1 << module);
}

static void dnodeUnSetModuleStatus(int32_t module) {
  tsModuleStatus &= ~(1 << module);
}
S
#1177  
slguan 已提交
46

S
slguan 已提交
47
static void dnodeAllocModules() {
48
  tsModule[TSDB_MOD_MGMT].enable       = false;
S
slguan 已提交
49 50 51 52 53
  tsModule[TSDB_MOD_MGMT].name         = "mgmt";
  tsModule[TSDB_MOD_MGMT].initFp       = mgmtInitSystem;
  tsModule[TSDB_MOD_MGMT].cleanUpFp    = mgmtCleanUpSystem;
  tsModule[TSDB_MOD_MGMT].startFp      = mgmtStartSystem;
  tsModule[TSDB_MOD_MGMT].stopFp       = mgmtStopSystem;
54

S
slguan 已提交
55 56 57 58 59 60 61 62 63
  tsModule[TSDB_MOD_HTTP].enable       = (tsEnableHttpModule == 1);
  tsModule[TSDB_MOD_HTTP].name         = "http";
  tsModule[TSDB_MOD_HTTP].initFp       = httpInitSystem;
  tsModule[TSDB_MOD_HTTP].cleanUpFp    = httpCleanUpSystem;
  tsModule[TSDB_MOD_HTTP].startFp      = httpStartSystem;
  tsModule[TSDB_MOD_HTTP].stopFp       = httpStopSystem;
  if (tsEnableHttpModule) {
    dnodeSetModuleStatus(TSDB_MOD_HTTP);
  }
64

S
slguan 已提交
65 66 67 68 69 70 71 72 73
  tsModule[TSDB_MOD_MONITOR].enable    = (tsEnableMonitorModule == 1);
  tsModule[TSDB_MOD_MONITOR].name      = "monitor";
  tsModule[TSDB_MOD_MONITOR].initFp    = monitorInitSystem;
  tsModule[TSDB_MOD_MONITOR].cleanUpFp = monitorCleanUpSystem;
  tsModule[TSDB_MOD_MONITOR].startFp   = monitorStartSystem;
  tsModule[TSDB_MOD_MONITOR].stopFp    = monitorStopSystem;
  if (tsEnableMonitorModule) {
    dnodeSetModuleStatus(TSDB_MOD_MONITOR);
  }
S
#1177  
slguan 已提交
74 75 76
}

void dnodeCleanUpModules() {
S
slguan 已提交
77 78 79
  for (int32_t module = 1; module < TSDB_MOD_MAX; ++module) {
    if (tsModule[module].enable && tsModule[module].stopFp) {
      (*tsModule[module].stopFp)();
S
#1177  
slguan 已提交
80
    }
S
slguan 已提交
81 82
    if (tsModule[module].cleanUpFp) {
      (*tsModule[module].cleanUpFp)();
S
#1177  
slguan 已提交
83 84 85
    }
  }

S
slguan 已提交
86
  if (tsModule[TSDB_MOD_MGMT].enable && tsModule[TSDB_MOD_MGMT].cleanUpFp) {
S
#1177  
slguan 已提交
87 88 89 90 91
    (*tsModule[TSDB_MOD_MGMT].cleanUpFp)();
  }
}

int32_t dnodeInitModules() {
S
slguan 已提交
92 93
  dnodeAllocModules();

S
slguan 已提交
94
  for (EModuleType module = 0; module < TSDB_MOD_MAX; ++module) {
S
slguan 已提交
95 96 97
    if (tsModule[module].initFp) {
      if ((*tsModule[module].initFp)() != 0) {
        dError("failed to init module:%s", tsModule[module].name);
S
#1177  
slguan 已提交
98 99 100 101 102
        return -1;
      }
    }
  }

S
slguan 已提交
103
  return 0;
S
#1177  
slguan 已提交
104 105
}

S
slguan 已提交
106
void dnodeStartModules() {
S
slguan 已提交
107
  for (EModuleType module = 1; module < TSDB_MOD_MAX; ++module) {
S
slguan 已提交
108 109 110 111 112 113
    if (tsModule[module].enable && tsModule[module].startFp) {
      if ((*tsModule[module].startFp)() != 0) {
        dError("failed to start module:%s", tsModule[module].name);
      }
    }
  }
S
#1177  
slguan 已提交
114
}
S
slguan 已提交
115 116

void dnodeProcessModuleStatus(uint32_t moduleStatus) {
S
slguan 已提交
117 118 119 120 121
  bool enableMgmtModule = moduleStatus & (1 << TSDB_MOD_MGMT);
  if (!tsModule[TSDB_MOD_MGMT].enable && enableMgmtModule) {
    dPrint("module status is received, start mgmt module", tsModuleStatus, moduleStatus);
    tsModule[TSDB_MOD_MGMT].enable = true;
    dnodeSetModuleStatus(TSDB_MOD_MGMT);
S
slguan 已提交
122
    (*tsModule[TSDB_MOD_MGMT].startFp)();
S
slguan 已提交
123 124
  }

S
slguan 已提交
125 126 127 128 129 130
  if (tsModule[TSDB_MOD_MGMT].enable && !enableMgmtModule) {
    dPrint("module status is received, stop mgmt module", tsModuleStatus, moduleStatus);
    tsModule[TSDB_MOD_MGMT].enable = false;
    dnodeUnSetModuleStatus(TSDB_MOD_MGMT);
    (*tsModule[TSDB_MOD_MGMT].stopFp)();
  }
S
slguan 已提交
131
}