dmHandle.c 7.6 KB
Newer Older
S
shm  
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
shm  
Shengliang Guan 已提交
17
#include "dmInt.h"
S
shm  
Shengliang Guan 已提交
18 19 20 21 22 23 24 25

void dmSendStatusReq(SDnodeMgmt *pMgmt) {
  SDnode    *pDnode = pMgmt->pDnode;
  SStatusReq req = {0};

  taosRLockLatch(&pMgmt->latch);
  req.sver = tsVersion;
  req.dver = pMgmt->dver;
S
shm  
Shengliang Guan 已提交
26 27
  req.dnodeId = pDnode->dnodeId;
  req.clusterId = pDnode->clusterId;
S
shm  
Shengliang Guan 已提交
28 29 30
  req.rebootTime = pDnode->rebootTime;
  req.updateTime = pMgmt->updateTime;
  req.numOfCores = tsNumOfCores;
S
shm  
Shengliang Guan 已提交
31
  req.numOfSupportVnodes = pDnode->numOfSupportVnodes;
S
shm  
Shengliang Guan 已提交
32
  tstrncpy(req.dnodeEp, pDnode->localEp, TSDB_EP_LEN);
S
shm  
Shengliang Guan 已提交
33 34 35 36 37

  req.clusterCfg.statusInterval = tsStatusInterval;
  req.clusterCfg.checkTime = 0;
  char timestr[32] = "1970-01-01 00:00:00.00";
  (void)taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0);
wafwerar's avatar
wafwerar 已提交
38
  memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN);
S
shm  
Shengliang Guan 已提交
39 40 41 42
  memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN);
  memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN);
  taosRUnLockLatch(&pMgmt->latch);

S
Shengliang Guan 已提交
43 44
  SMgmtWrapper *pWrapper = dndAcquireWrapper(pDnode, VNODES);
  if (pWrapper != NULL) {
45 46 47
    SMonVloadInfo info = {0};
    dmGetVnodeLoads(pWrapper, &info);
    req.pVloads = info.pVloads;
S
Shengliang Guan 已提交
48 49
    dndReleaseWrapper(pWrapper);
  }
S
shm  
Shengliang Guan 已提交
50 51 52 53 54 55

  int32_t contLen = tSerializeSStatusReq(NULL, 0, &req);
  void   *pHead = rpcMallocCont(contLen);
  tSerializeSStatusReq(pHead, contLen, &req);
  taosArrayDestroy(req.pVloads);

S
shm  
Shengliang Guan 已提交
56
  SRpcMsg rpcMsg = {.pCont = pHead, .contLen = contLen, .msgType = TDMT_MND_STATUS, .ahandle = (void *)0x9527};
S
shm  
Shengliang Guan 已提交
57 58
  pMgmt->statusSent = 1;

S
shm  
Shengliang Guan 已提交
59
  dTrace("send req:%s to mnode, app:%p", TMSG_INFO(rpcMsg.msgType), rpcMsg.ahandle);
S
Shengliang Guan 已提交
60 61 62
  SEpSet epSet = {0};
  dmGetMnodeEpSet(pMgmt, &epSet);
  tmsgSendReq(&pMgmt->msgCb, &epSet, &rpcMsg);
S
shm  
Shengliang Guan 已提交
63 64
}

S
shm  
Shengliang Guan 已提交
65
static void dmUpdateDnodeCfg(SDnodeMgmt *pMgmt, SDnodeCfg *pCfg) {
S
shm  
Shengliang Guan 已提交
66 67 68
  SDnode *pDnode = pMgmt->pDnode;

  if (pDnode->dnodeId == 0) {
S
shm  
Shengliang Guan 已提交
69 70
    dInfo("set dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
    taosWLockLatch(&pMgmt->latch);
S
shm  
Shengliang Guan 已提交
71 72
    pDnode->dnodeId = pCfg->dnodeId;
    pDnode->clusterId = pCfg->clusterId;
S
shm  
Shengliang Guan 已提交
73 74 75 76 77
    dmWriteFile(pMgmt);
    taosWUnLockLatch(&pMgmt->latch);
  }
}

S
shm  
Shengliang Guan 已提交
78 79 80
int32_t dmProcessStatusRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  SDnode  *pDnode = pMgmt->pDnode;
  SRpcMsg *pRsp = &pMsg->rpcMsg;
S
shm  
Shengliang Guan 已提交
81 82

  if (pRsp->code != TSDB_CODE_SUCCESS) {
S
shm  
Shengliang Guan 已提交
83 84 85
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pDnode->dropped && pDnode->dnodeId > 0) {
      dInfo("dnode:%d, set to dropped since not exist in mnode", pDnode->dnodeId);
      pDnode->dropped = 1;
S
shm  
Shengliang Guan 已提交
86 87 88 89 90 91 92
      dmWriteFile(pMgmt);
    }
  } else {
    SStatusRsp statusRsp = {0};
    if (pRsp->pCont != NULL && pRsp->contLen != 0 &&
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
      pMgmt->dver = statusRsp.dver;
S
shm  
Shengliang Guan 已提交
93
      dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
S
shm  
Shengliang Guan 已提交
94 95
      dmUpdateDnodeEps(pMgmt, statusRsp.pDnodeEps);
    }
S
shm  
Shengliang Guan 已提交
96
    tFreeSStatusRsp(&statusRsp);
S
shm  
Shengliang Guan 已提交
97 98 99 100 101
  }

  pMgmt->statusSent = 0;
}

S
shm  
Shengliang Guan 已提交
102 103 104 105 106
int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  SRpcMsg *pRsp = &pMsg->rpcMsg;
  dError("auth rsp is received, but not supported yet");
  return 0;
}
S
shm  
Shengliang Guan 已提交
107

S
shm  
Shengliang Guan 已提交
108 109 110 111 112
int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  SRpcMsg *pRsp = &pMsg->rpcMsg;
  dError("grant rsp is received, but not supported yet");
  return 0;
}
S
shm  
Shengliang Guan 已提交
113

S
shm  
Shengliang Guan 已提交
114 115
int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) {
  SRpcMsg       *pReq = &pMsg->rpcMsg;
S
shm  
Shengliang Guan 已提交
116
  SDCfgDnodeReq *pCfg = pReq->pCont;
S
shm  
Shengliang Guan 已提交
117
  dError("config req is received, but not supported yet");
S
shm  
Shengliang Guan 已提交
118 119 120
  return TSDB_CODE_OPS_NOT_SUPPORT;
}

S
Shengliang Guan 已提交
121
static int32_t dmProcessCreateNodeMsg(SDnode *pDnode, EDndType ntype, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  SMgmtWrapper *pWrapper = dndAcquireWrapper(pDnode, ntype);
  if (pWrapper != NULL) {
    dndReleaseWrapper(pWrapper);
    terrno = TSDB_CODE_NODE_ALREADY_DEPLOYED;
    dError("failed to create node since %s", terrstr());
    return -1;
  }

  pWrapper = &pDnode->wrappers[ntype];

  if (taosMkDir(pWrapper->path) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to create dir:%s since %s", pWrapper->path, terrstr());
    return -1;
  }

  int32_t code = (*pWrapper->fp.createMsgFp)(pWrapper, pMsg);
  if (code != 0) {
    dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
  } else {
    dDebug("node:%s, has been opened", pWrapper->name);
    pWrapper->deployed = true;
  }

  return code;
}

S
Shengliang Guan 已提交
149
static int32_t dmProcessDropNodeMsg(SDnode *pDnode, EDndType ntype, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  SMgmtWrapper *pWrapper = dndAcquireWrapper(pDnode, ntype);
  if (pWrapper == NULL) {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    dError("failed to drop node since %s", terrstr());
    return -1;
  }

  taosWLockLatch(&pWrapper->latch);
  pWrapper->deployed = false;

  int32_t code = (*pWrapper->fp.dropMsgFp)(pWrapper, pMsg);
  if (code != 0) {
    pWrapper->deployed = true;
    dError("node:%s, failed to drop since %s", pWrapper->name, terrstr());
  } else {
    pWrapper->deployed = false;
    dDebug("node:%s, has been dropped", pWrapper->name);
  }

  taosWUnLockLatch(&pWrapper->latch);
  dndReleaseWrapper(pWrapper);
  return code;
}

S
Shengliang Guan 已提交
174
int32_t dmProcessCDnodeReq(SDnode *pDnode, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
175 176
  switch (pMsg->rpcMsg.msgType) {
    case TDMT_DND_CREATE_MNODE:
S
Shengliang Guan 已提交
177
      return dmProcessCreateNodeMsg(pDnode, MNODE, pMsg);
S
Shengliang Guan 已提交
178
    case TDMT_DND_DROP_MNODE:
S
Shengliang Guan 已提交
179
      return dmProcessDropNodeMsg(pDnode, MNODE, pMsg);
S
Shengliang Guan 已提交
180
    case TDMT_DND_CREATE_QNODE:
S
Shengliang Guan 已提交
181
      return dmProcessCreateNodeMsg(pDnode, QNODE, pMsg);
S
Shengliang Guan 已提交
182
    case TDMT_DND_DROP_QNODE:
S
Shengliang Guan 已提交
183
      return dmProcessDropNodeMsg(pDnode, QNODE, pMsg);
S
Shengliang Guan 已提交
184
    case TDMT_DND_CREATE_SNODE:
S
Shengliang Guan 已提交
185
      return dmProcessCreateNodeMsg(pDnode, SNODE, pMsg);
S
Shengliang Guan 已提交
186
    case TDMT_DND_DROP_SNODE:
S
Shengliang Guan 已提交
187
      return dmProcessDropNodeMsg(pDnode, SNODE, pMsg);
S
Shengliang Guan 已提交
188
    case TDMT_DND_CREATE_BNODE:
S
Shengliang Guan 已提交
189
      return dmProcessCreateNodeMsg(pDnode, BNODE, pMsg);
S
Shengliang Guan 已提交
190
    case TDMT_DND_DROP_BNODE:
S
Shengliang Guan 已提交
191
      return dmProcessDropNodeMsg(pDnode, BNODE, pMsg);
S
Shengliang Guan 已提交
192 193 194 195 196 197
    default:
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
      return -1;
  }
}

S
Shengliang Guan 已提交
198
void dmInitMsgHandle(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
199
  // Requests handled by DNODE
S
Shengliang Guan 已提交
200 201 202 203 204 205 206 207 208 209
  dndSetMsgHandle(pWrapper, TDMT_DND_CREATE_MNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_DROP_MNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_CREATE_QNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_DROP_QNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_CREATE_SNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_DROP_SNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_CREATE_BNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_DROP_BNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_CONFIG_DNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_DND_NETWORK_TEST, dmProcessMgmtMsg, DEFAULT_HANDLE);
S
shm  
Shengliang Guan 已提交
210 211

  // Requests handled by MNODE
212
  dndSetMsgHandle(pWrapper, TDMT_MND_STATUS_RSP, dmProcessMonitorMsg, DEFAULT_HANDLE);
S
Shengliang Guan 已提交
213 214
  dndSetMsgHandle(pWrapper, TDMT_MND_GRANT_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dndSetMsgHandle(pWrapper, TDMT_MND_AUTH_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE);
S
shm  
Shengliang Guan 已提交
215
}