dmInt.c 6.7 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
  SStartupInfo *pStartup = &pDnode->startup;
S
Shengliang Guan 已提交
141 142
  tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN);
  tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN);
S
Shengliang Guan 已提交
143 144 145 146 147
  dInfo("step:%s, %s", pStartup->name, pStartup->desc);
}

void dmReportStartupByWrapper(SMgmtWrapper *pWrapper, const char *pName, const char *pDesc) {
  dmReportStartup(pWrapper->pDnode, pName, pDesc);
S
Shengliang Guan 已提交
148 149
}

S
Shengliang Guan 已提交
150
static void dmGetServerStatus(SDnode *pDnode, SServerStatusRsp *pStatus) {
S
Shengliang Guan 已提交
151 152
  pStatus->details[0] = 0;

S
Shengliang Guan 已提交
153 154
  if (pDnode->status == DND_STAT_INIT) {
    pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK;
S
Shengliang Guan 已提交
155
    snprintf(pStatus->details, sizeof(pStatus->details), "%s: %s", pDnode->startup.name, pDnode->startup.desc);
S
Shengliang Guan 已提交
156
  } else if (pDnode->status == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
157 158
    pStatus->statusCode = TSDB_SRV_STATUS_EXTING;
  } else {
S
Shengliang Guan 已提交
159
    SDnodeData *pData = &pDnode->data;
S
Shengliang Guan 已提交
160
    if (pData->isMnode && pData->mndState != TAOS_SYNC_STATE_LEADER && pData->mndState == TAOS_SYNC_STATE_FOLLOWER) {
S
Shengliang Guan 已提交
161 162
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
      snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(pData->mndState));
S
Shengliang Guan 已提交
163
    } else if (pData->unsyncedVgId != 0 && pData->vndState != TAOS_SYNC_STATE_LEADER &&
S
Shengliang Guan 已提交
164 165 166 167 168 169
               pData->vndState != TAOS_SYNC_STATE_FOLLOWER) {
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
      snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pData->unsyncedVgId,
               syncStr(pData->vndState));
    } else {
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
S
Shengliang Guan 已提交
170 171 172 173
    }
  }
}

S
Shengliang Guan 已提交
174
void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pRpc) {
175
  dDebug("net test req is received");
176
  SRpcMsg rsp = {.handle = pRpc->handle, .refId = pRpc->refId, .ahandle = pRpc->ahandle, .code = 0};
177
  rsp.pCont = rpcMallocCont(pRpc->contLen);
178 179 180 181 182
  if (rsp.pCont == NULL) {
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    rsp.contLen = pRpc->contLen;
  }
183 184 185
  rpcSendResponse(&rsp);
}

S
Shengliang Guan 已提交
186 187 188 189 190 191
void dmProcessServerStatusReq(SDnode *pDnode, SRpcMsg *pReq) {
  dDebug("server status req is received");

  SServerStatusRsp statusRsp = {0};
  dmGetServerStatus(pDnode, &statusRsp);

dengyihao's avatar
dengyihao 已提交
192
  SRpcMsg rspMsg = {.handle = pReq->handle, .ahandle = pReq->ahandle, .refId = pReq->refId};
S
Shengliang Guan 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
  if (rspLen < 0) {
    rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  void *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
  rspMsg.pCont = pRsp;
  rspMsg.contLen = rspLen;
S
Shengliang Guan 已提交
208

S
Shengliang Guan 已提交
209 210
_OVER:
  rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
211
}
S
Shengliang Guan 已提交
212

S
Shengliang Guan 已提交
213
void dmGetMonitorSysInfo(SMonSysInfo *pInfo) {
S
Shengliang Guan 已提交
214 215 216 217 218 219 220 221 222 223 224
  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);
}