dnodeModule.c 5.0 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
TD-1382  
Shengliang Guan 已提交
19
#include "taosmsg.h"
S
slguan 已提交
20
#include "tglobal.h"
S
dnode  
slguan 已提交
21 22
#include "mnode.h"
#include "http.h"
麦壳饼's avatar
麦壳饼 已提交
23
#include "tmqtt.h"
S
dnode  
slguan 已提交
24
#include "monitor.h"
S
TD-1382  
Shengliang Guan 已提交
25
#include "dnode.h"
26
#include "dnodeInt.h"
S
#1177  
slguan 已提交
27 28
#include "dnodeModule.h"

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

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

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

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

S
slguan 已提交
49
static void dnodeAllocModules() {
S
Shengliang Guan 已提交
50 51 52 53 54 55
  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;
56

S
slguan 已提交
57 58 59 60 61 62 63 64 65
  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);
  }
66

S
TD-1310  
Shengliang Guan 已提交
67
#ifdef _MQTT
68 69 70 71 72 73 74 75 76
  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 已提交
77
#endif  
78

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

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

S
TD-1710  
Shengliang Guan 已提交
100
  if (tsModule[TSDB_MOD_MNODE].cleanUpFp) {
S
Shengliang Guan 已提交
101
    (*tsModule[TSDB_MOD_MNODE].cleanUpFp)();
S
#1177  
slguan 已提交
102 103 104 105
  }
}

int32_t dnodeInitModules() {
S
slguan 已提交
106 107
  dnodeAllocModules();

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

S
slguan 已提交
117
  return 0;
S
#1177  
slguan 已提交
118 119
}

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

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

140
    if (tsModule[module].enable && !enableModule) {
S
TD-1382  
Shengliang Guan 已提交
141
      dInfo("module status:%u is set, stop %s module", moduleStatus, tsModule[module].name);
142 143 144 145
      tsModule[module].enable = false;
      dnodeUnSetModuleStatus(module);
      (*tsModule[module].stopFp)();
    }
S
slguan 已提交
146
  }
S
slguan 已提交
147
}
S
TD-1382  
Shengliang Guan 已提交
148

S
TD-1671  
Shengliang Guan 已提交
149 150 151
bool dnodeStartMnode(void *pMnodes) {
  SDMMnodeInfos *mnodes = pMnodes;

S
TD-1652  
Shengliang Guan 已提交
152 153 154
  if (tsModuleStatus & (1 << TSDB_MOD_MNODE)) {
    dDebug("mnode module is already started, module status:%d", tsModuleStatus);
    return false;
S
TD-1382  
Shengliang Guan 已提交
155
  }
R
TD-1382  
root 已提交
156

S
TD-1652  
Shengliang Guan 已提交
157 158 159 160
  uint32_t moduleStatus = tsModuleStatus | (1 << TSDB_MOD_MNODE);
  dInfo("start mnode module, module status:%d, new status:%d", tsModuleStatus, moduleStatus);
  dnodeProcessModuleStatus(moduleStatus);

S
TD-1671  
Shengliang Guan 已提交
161 162
  sdbUpdateSync(mnodes);

S
TD-1652  
Shengliang Guan 已提交
163
  return true;
S
TD-1382  
Shengliang Guan 已提交
164
}