dnodeModule.c 4.6 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"
S
dnode  
slguan 已提交
20 21
#include "mnode.h"
#include "http.h"
麦壳饼's avatar
麦壳饼 已提交
22
#include "tmqtt.h"
S
dnode  
slguan 已提交
23
#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() {
S
Shengliang Guan 已提交
48 49 50 51 52 53
  tsModule[TSDB_MOD_MNODE].enable       = false;
  tsModule[TSDB_MOD_MNODE].name         = "mnode";
  tsModule[TSDB_MOD_MNODE].initFp       = mnodeInitSystem;
  tsModule[TSDB_MOD_MNODE].cleanUpFp    = mnodeCleanupSystem;
  tsModule[TSDB_MOD_MNODE].startFp      = mnodeStartSystem;
  tsModule[TSDB_MOD_MNODE].stopFp       = mnodeStopSystem;
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
TD-1310  
Shengliang Guan 已提交
65
#ifdef _MQTT
66 67 68 69 70 71 72 73 74
  tsModule[TSDB_MOD_MQTT].enable = (tsEnableMqttModule == 1);
  tsModule[TSDB_MOD_MQTT].name = "mqtt";
  tsModule[TSDB_MOD_MQTT].initFp = mqttInitSystem;
  tsModule[TSDB_MOD_MQTT].cleanUpFp = mqttCleanUpSystem;
  tsModule[TSDB_MOD_MQTT].startFp = mqttStartSystem;
  tsModule[TSDB_MOD_MQTT].stopFp = mqttStopSystem;
  if (tsEnableMqttModule) {
    dnodeSetModuleStatus(TSDB_MOD_MQTT);
  }
S
TD-1310  
Shengliang Guan 已提交
75
#endif  
76

S
slguan 已提交
77 78 79 80 81 82 83 84 85
  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 已提交
86 87
}

88
void dnodeCleanupModules() {
Y
yifan hao 已提交
89
  for (EModuleType module = 1; module < TSDB_MOD_MAX; ++module) {
S
slguan 已提交
90 91
    if (tsModule[module].enable && tsModule[module].stopFp) {
      (*tsModule[module].stopFp)();
S
#1177  
slguan 已提交
92
    }
S
slguan 已提交
93 94
    if (tsModule[module].cleanUpFp) {
      (*tsModule[module].cleanUpFp)();
S
#1177  
slguan 已提交
95 96 97
    }
  }

S
Shengliang Guan 已提交
98 99
  if (tsModule[TSDB_MOD_MNODE].enable && tsModule[TSDB_MOD_MNODE].cleanUpFp) {
    (*tsModule[TSDB_MOD_MNODE].cleanUpFp)();
S
#1177  
slguan 已提交
100 101 102 103
  }
}

int32_t dnodeInitModules() {
S
slguan 已提交
104 105
  dnodeAllocModules();

S
slguan 已提交
106
  for (EModuleType module = 0; module < TSDB_MOD_MAX; ++module) {
S
slguan 已提交
107 108 109
    if (tsModule[module].initFp) {
      if ((*tsModule[module].initFp)() != 0) {
        dError("failed to init module:%s", tsModule[module].name);
S
#1177  
slguan 已提交
110 111 112 113 114
        return -1;
      }
    }
  }

S
slguan 已提交
115
  return 0;
S
#1177  
slguan 已提交
116 117
}

S
slguan 已提交
118
void dnodeStartModules() {
S
slguan 已提交
119
  for (EModuleType module = 1; module < TSDB_MOD_MAX; ++module) {
S
slguan 已提交
120 121 122 123 124 125
    if (tsModule[module].enable && tsModule[module].startFp) {
      if ((*tsModule[module].startFp)() != 0) {
        dError("failed to start module:%s", tsModule[module].name);
      }
    }
  }
S
#1177  
slguan 已提交
126
}
S
slguan 已提交
127 128

void dnodeProcessModuleStatus(uint32_t moduleStatus) {
S
Shengliang Guan 已提交
129
  for (int32_t module = TSDB_MOD_MNODE; module < TSDB_MOD_HTTP; ++module) {
130 131
    bool enableModule = moduleStatus & (1 << module);
    if (!tsModule[module].enable && enableModule) {
132
      dInfo("module status:%u is received, start %s module", tsModuleStatus, tsModule[module].name);
133 134 135 136
      tsModule[module].enable = true;
      dnodeSetModuleStatus(module);
      (*tsModule[module].startFp)();
    }
S
slguan 已提交
137

138
    if (tsModule[module].enable && !enableModule) {
139
      dInfo("module status:%u is received, stop %s module", tsModuleStatus, tsModule[module].name);
140 141 142 143
      tsModule[module].enable = false;
      dnodeUnSetModuleStatus(module);
      (*tsModule[module].stopFp)();
    }
S
slguan 已提交
144
  }
S
slguan 已提交
145
}