dmInt.c 4.9 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
S
Shengliang Guan 已提交
17
#include "dmInt.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
const char *dmStatName(EDndRunStatus status) {
S
Shengliang Guan 已提交
20 21 22 23 24 25 26 27 28 29 30 31
  switch (status) {
    case DND_STAT_INIT:
      return "init";
    case DND_STAT_RUNNING:
      return "running";
    case DND_STAT_STOPPED:
      return "stopped";
    default:
      return "UNKNOWN";
  }
}

S
Shengliang Guan 已提交
32
const char *dmLogName(EDndNodeType ntype) {
S
Shengliang Guan 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  switch (ntype) {
    case VNODE:
      return "vnode";
    case QNODE:
      return "qnode";
    case SNODE:
      return "snode";
    case MNODE:
      return "mnode";
    case BNODE:
      return "bnode";
    default:
      return "taosd";
  }
}

S
Shengliang Guan 已提交
49
const char *dmProcName(EDndNodeType ntype) {
S
Shengliang Guan 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  switch (ntype) {
    case VNODE:
      return "taosv";
    case QNODE:
      return "taosq";
    case SNODE:
      return "taoss";
    case MNODE:
      return "taosm";
    case BNODE:
      return "taosb";
    default:
      return "taosd";
  }
}

S
Shengliang Guan 已提交
66
const char *dmEventName(EDndEvent ev) {
S
Shengliang Guan 已提交
67 68 69 70 71 72 73 74 75 76
  switch (ev) {
    case DND_EVENT_START:
      return "start";
    case DND_EVENT_STOP:
      return "stop";
    case DND_EVENT_CHILD:
      return "child";
    default:
      return "UNKNOWN";
  }
S
Shengliang Guan 已提交
77 78
}

S
Shengliang Guan 已提交
79
void dmSetStatus(SDnode *pDnode, EDndRunStatus status) {
S
Shengliang Guan 已提交
80
  if (pDnode->status != status) {
S
Shengliang Guan 已提交
81
    dDebug("dnode status set from %s to %s", dmStatName(pDnode->status), dmStatName(status));
S
Shengliang Guan 已提交
82 83 84 85
    pDnode->status = status;
  }
}

S
Shengliang Guan 已提交
86
void dmSetEvent(SDnode *pDnode, EDndEvent event) {
S
Shengliang Guan 已提交
87 88 89 90 91
  if (event == DND_EVENT_STOP) {
    pDnode->event = event;
  }
}

S
Shengliang Guan 已提交
92
void dmSetMsgHandle(SMgmtWrapper *pWrapper, tmsg_t msgType, NodeMsgFp nodeMsgFp, int8_t vgId) {
S
Shengliang Guan 已提交
93 94 95 96
  pWrapper->msgFps[TMSG_INDEX(msgType)] = nodeMsgFp;
  pWrapper->msgVgIds[TMSG_INDEX(msgType)] = vgId;
}

S
Shengliang Guan 已提交
97
SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) {
S
Shengliang Guan 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
  SMgmtWrapper *pRetWrapper = pWrapper;

  taosRLockLatch(&pWrapper->latch);
  if (pWrapper->deployed) {
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
    dTrace("node:%s, is acquired, refCount:%d", pWrapper->name, refCount);
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    pRetWrapper = NULL;
  }
  taosRUnLockLatch(&pWrapper->latch);

  return pRetWrapper;
}

S
Shengliang Guan 已提交
114
int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  int32_t code = 0;

  taosRLockLatch(&pWrapper->latch);
  if (pWrapper->deployed || (pWrapper->procType == DND_PROC_PARENT && pWrapper->required)) {
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
    dTrace("node:%s, is marked, refCount:%d", pWrapper->name, refCount);
  } else {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    code = -1;
  }
  taosRUnLockLatch(&pWrapper->latch);

  return code;
}

S
Shengliang Guan 已提交
130
void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
131 132 133 134 135 136 137 138
  if (pWrapper == NULL) return;

  taosRLockLatch(&pWrapper->latch);
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
  taosRUnLockLatch(&pWrapper->latch);
  dTrace("node:%s, is released, refCount:%d", pWrapper->name, refCount);
}

S
Shengliang Guan 已提交
139
void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) {
S
Shengliang Guan 已提交
140 141 142 143 144 145
  SStartupReq *pStartup = &pDnode->startup;
  tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN);
  tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN);
  pStartup->finished = 0;
}

S
Shengliang Guan 已提交
146
static void dmGetStartup(SDnode *pDnode, SStartupReq *pStartup) {
S
Shengliang Guan 已提交
147
  memcpy(pStartup, &pDnode->startup, sizeof(SStartupReq));
S
Shengliang Guan 已提交
148
  pStartup->finished = (pDnode->status == DND_STAT_RUNNING);
S
Shengliang Guan 已提交
149 150
}

S
Shengliang Guan 已提交
151
void dmProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
152 153
  dDebug("startup req is received");
  SStartupReq *pStartup = rpcMallocCont(sizeof(SStartupReq));
S
Shengliang Guan 已提交
154
  dmGetStartup(pDnode, pStartup);
S
Shengliang Guan 已提交
155 156 157 158 159 160

  dDebug("startup req is sent, step:%s desc:%s finished:%d", pStartup->name, pStartup->desc, pStartup->finished);
  SRpcMsg rpcRsp = {
      .handle = pReq->handle, .pCont = pStartup, .contLen = sizeof(SStartupReq), .ahandle = pReq->ahandle};
  rpcSendResponse(&rpcRsp);
}
S
Shengliang Guan 已提交
161

S
Shengliang Guan 已提交
162
void dmGetMonitorSysInfo(SMonSysInfo *pInfo) {
S
Shengliang Guan 已提交
163 164 165 166 167 168 169 170 171 172 173
  taosGetCpuUsage(&pInfo->cpu_engine, &pInfo->cpu_system);
  taosGetCpuCores(&pInfo->cpu_cores);
  taosGetProcMemory(&pInfo->mem_engine);
  taosGetSysMemory(&pInfo->mem_system);
  pInfo->mem_total = tsTotalMemoryKB;
  pInfo->disk_engine = 0;
  pInfo->disk_used = tsDataSpace.size.used;
  pInfo->disk_total = tsDataSpace.size.total;
  taosGetCardInfoDelta(&pInfo->net_in, &pInfo->net_out);
  taosGetProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk);
}