dnodeModule.c 4.3 KB
Newer Older
S
#1177  
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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"
#include "tlog.h"
#include "tglobalcfg.h"
S
dnode  
slguan 已提交
20 21 22
#include "mnode.h"
#include "http.h"
#include "monitor.h"
S
#1177  
slguan 已提交
23 24 25
#include "dnodeModule.h"
#include "dnodeSystem.h"

S
dnode  
slguan 已提交
26 27
SModule  tsModule[TSDB_MOD_MAX] = {0};
uint32_t tsModuleStatus         = 0;
S
#1177  
slguan 已提交
28 29

void dnodeAllocModules() {
S
dnode  
slguan 已提交
30 31 32 33 34 35 36
  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;
  tsModule[TSDB_MOD_MGMT].num           = tsNumOfMPeers;
  tsModule[TSDB_MOD_MGMT].curNum        = 0;
S
#1177  
slguan 已提交
37 38
  tsModule[TSDB_MOD_MGMT].equalVnodeNum = tsMgmtEqualVnodeNum;

S
dnode  
slguan 已提交
39 40 41 42 43 44 45
  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;
  tsModule[TSDB_MOD_HTTP].num           = (tsEnableHttpModule == 1) ? -1 : 0;
  tsModule[TSDB_MOD_HTTP].curNum        = 0;
S
#1177  
slguan 已提交
46 47
  tsModule[TSDB_MOD_HTTP].equalVnodeNum = 0;

S
dnode  
slguan 已提交
48 49 50 51 52 53 54
  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;
  tsModule[TSDB_MOD_MONITOR].num           = (tsEnableMonitorModule == 1) ? -1 : 0;
  tsModule[TSDB_MOD_MONITOR].curNum        = 0;
S
#1177  
slguan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  tsModule[TSDB_MOD_MONITOR].equalVnodeNum = 0;
}

void dnodeCleanUpModules() {
  for (int mod = 1; mod < TSDB_MOD_MAX; ++mod) {
    if (tsModule[mod].num != 0 && tsModule[mod].stopFp) {
      (*tsModule[mod].stopFp)();
    }
    if (tsModule[mod].num != 0 && tsModule[mod].cleanUpFp) {
      (*tsModule[mod].cleanUpFp)();
    }
  }

  if (tsModule[TSDB_MOD_MGMT].num != 0 && tsModule[TSDB_MOD_MGMT].cleanUpFp) {
    (*tsModule[TSDB_MOD_MGMT].cleanUpFp)();
  }
}

void dnodeProcessModuleStatus(uint32_t status) {
S
dnode  
slguan 已提交
74
  if (dnodeGetRunStatus() != TSDB_DNODE_RUN_STATUS_RUNING) {
S
#1177  
slguan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    return;
  }

  int news = status;
  int olds = tsModuleStatus;

  for (int moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) {
    int newStatus = news & (1 << moduleType);
    int oldStatus = olds & (1 << moduleType);

    if (oldStatus > 0) {
      if (newStatus == 0) {
        if (tsModule[moduleType].stopFp) {
          dPrint("module:%s is stopped on this node", tsModule[moduleType].name);
          (*tsModule[moduleType].stopFp)();
        }
      }
    } else if (oldStatus == 0) {
      if (newStatus > 0) {
        if (tsModule[moduleType].startFp) {
          dPrint("module:%s is started on this node", tsModule[moduleType].name);
          (*tsModule[moduleType].startFp)();
        }
      }
    } else {
    }
  }
  tsModuleStatus = status;
}

int32_t dnodeInitModules() {
  for (int mod = 0; mod < TSDB_MOD_MAX; ++mod) {
    if (tsModule[mod].num != 0 && tsModule[mod].initFp) {
      if ((*tsModule[mod].initFp)() != 0) {
        dError("TDengine initialization failed");
        return -1;
      }
    }
  }

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

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

  if (tsModule[TSDB_MOD_MGMT].num != 0 && tsModule[TSDB_MOD_MGMT].cleanUpFp) {
    (*tsModule[TSDB_MOD_MGMT].cleanUpFp)();
  }
}
S
dnode  
slguan 已提交
131

S
#1177  
slguan 已提交
132
void (*dnodeStartModules)() = dnodeStartModulesImp;