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"
S
dnode  
slguan 已提交
20 21 22
#include "mnode.h"
#include "http.h"
#include "monitor.h"
S
slguan 已提交
23
#include "dnodeLog.h"
S
#1177  
slguan 已提交
24 25
#include "dnodeModule.h"

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

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

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

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

S
slguan 已提交
46
static void dnodeAllocModules() {
47
  tsModule[TSDB_MOD_MGMT].enable       = false;
S
slguan 已提交
48 49 50 51 52
  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;
53

S
slguan 已提交
54 55 56 57 58 59 60 61 62
  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);
  }
63

S
slguan 已提交
64 65 66 67 68 69 70 71 72
  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 已提交
73 74 75
}

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

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

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

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

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

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

void dnodeProcessModuleStatus(uint32_t moduleStatus) {
S
slguan 已提交
116 117 118 119 120
  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 已提交
121
    (*tsModule[TSDB_MOD_MGMT].startFp)();
S
slguan 已提交
122 123
  }

S
slguan 已提交
124 125 126 127 128 129
  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 已提交
130
}