dmHandle.c 8.7 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
Shengliang Guan 已提交
17
#include "dmImp.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20 21 22 23 24 25 26 27 28
static void dmUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) {
  if (pDnode->data.dnodeId == 0) {
    dInfo("set dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
    taosWLockLatch(&pDnode->data.latch);
    pDnode->data.dnodeId = pCfg->dnodeId;
    pDnode->data.clusterId = pCfg->clusterId;
    dmWriteEps(pDnode);
    taosWUnLockLatch(&pDnode->data.latch);
  }
}
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30
static int32_t dmProcessStatusRsp(SDnode *pDnode, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
31 32 33 34
  if (pRsp->code != TSDB_CODE_SUCCESS) {
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pDnode->data.dropped && pDnode->data.dnodeId > 0) {
      dInfo("dnode:%d, set to dropped since not exist in mnode", pDnode->data.dnodeId);
      pDnode->data.dropped = 1;
S
Shengliang Guan 已提交
35
      dmWriteEps(pDnode);
S
Shengliang Guan 已提交
36 37 38
    }
  } else {
    SStatusRsp statusRsp = {0};
S
Shengliang Guan 已提交
39
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
S
Shengliang Guan 已提交
40
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
S
Shengliang Guan 已提交
41 42 43
      pDnode->data.dnodeVer = statusRsp.dnodeVer;
      dmUpdateDnodeCfg(pDnode, &statusRsp.dnodeCfg);
      dmUpdateEps(pDnode, statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
44 45 46 47 48 49 50 51
    }
    tFreeSStatusRsp(&statusRsp);
  }

  return TSDB_CODE_SUCCESS;
}

void dmSendStatusReq(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
52 53
  SStatusReq req = {0};

S
Shengliang Guan 已提交
54
  taosRLockLatch(&pDnode->data.latch);
S
shm  
Shengliang Guan 已提交
55
  req.sver = tsVersion;
S
Shengliang Guan 已提交
56
  req.dnodeVer = pDnode->data.dnodeVer;
S
Shengliang Guan 已提交
57 58 59
  req.dnodeId = pDnode->data.dnodeId;
  req.clusterId = pDnode->data.clusterId;
  req.rebootTime = pDnode->data.rebootTime;
S
Shengliang Guan 已提交
60
  req.updateTime = pDnode->data.updateTime;
S
shm  
Shengliang Guan 已提交
61
  req.numOfCores = tsNumOfCores;
S
Shengliang Guan 已提交
62 63
  req.numOfSupportVnodes = pDnode->data.supportVnodes;
  tstrncpy(req.dnodeEp, pDnode->data.localEp, TSDB_EP_LEN);
S
shm  
Shengliang Guan 已提交
64 65 66 67 68

  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 已提交
69
  memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN);
S
shm  
Shengliang Guan 已提交
70 71
  memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN);
  memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN);
S
Shengliang Guan 已提交
72
  taosRUnLockLatch(&pDnode->data.latch);
S
shm  
Shengliang Guan 已提交
73

S
Shengliang Guan 已提交
74 75 76
  SMonVloadInfo info = {0};
  dmGetVnodeLoads(pDnode, &info);
  req.pVloads = info.pVloads;
S
shm  
Shengliang Guan 已提交
77 78 79 80

  int32_t contLen = tSerializeSStatusReq(NULL, 0, &req);
  void   *pHead = rpcMallocCont(contLen);
  tSerializeSStatusReq(pHead, contLen, &req);
S
Shengliang Guan 已提交
81
  tFreeSStatusReq(&req);
S
shm  
Shengliang Guan 已提交
82

S
shm  
Shengliang Guan 已提交
83
  SRpcMsg rpcMsg = {.pCont = pHead, .contLen = contLen, .msgType = TDMT_MND_STATUS, .ahandle = (void *)0x9527};
S
Shengliang Guan 已提交
84 85
  SRpcMsg rpcRsp = {0};

S
shm  
Shengliang Guan 已提交
86
  dTrace("send req:%s to mnode, app:%p", TMSG_INFO(rpcMsg.msgType), rpcMsg.ahandle);
S
Shengliang Guan 已提交
87
  dmSendToMnodeRecv(pDnode, &rpcMsg, &rpcRsp);
S
Shengliang Guan 已提交
88
  dmProcessStatusRsp(pDnode, &rpcRsp);
S
shm  
Shengliang Guan 已提交
89 90
}

S
Shengliang Guan 已提交
91
int32_t dmProcessAuthRsp(SDnode *pDnode, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
92 93 94 95
  SRpcMsg *pRsp = &pMsg->rpcMsg;
  dError("auth rsp is received, but not supported yet");
  return 0;
}
S
shm  
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97
int32_t dmProcessGrantRsp(SDnode *pDnode, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
98 99 100 101
  SRpcMsg *pRsp = &pMsg->rpcMsg;
  dError("grant rsp is received, but not supported yet");
  return 0;
}
S
shm  
Shengliang Guan 已提交
102

S
Shengliang Guan 已提交
103
int32_t dmProcessConfigReq(SDnode *pDnode, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
104
  SRpcMsg       *pReq = &pMsg->rpcMsg;
S
shm  
Shengliang Guan 已提交
105
  SDCfgDnodeReq *pCfg = pReq->pCont;
S
shm  
Shengliang Guan 已提交
106
  dError("config req is received, but not supported yet");
S
shm  
Shengliang Guan 已提交
107 108 109
  return TSDB_CODE_OPS_NOT_SUPPORT;
}

S
Shengliang Guan 已提交
110 111
int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype);
S
Shengliang Guan 已提交
112
  if (pWrapper != NULL) {
S
Shengliang Guan 已提交
113
    dmReleaseWrapper(pWrapper);
S
Shengliang Guan 已提交
114 115 116 117 118
    terrno = TSDB_CODE_NODE_ALREADY_DEPLOYED;
    dError("failed to create node since %s", terrstr());
    return -1;
  }

119
  taosThreadMutexLock(&pDnode->mutex);
S
Shengliang Guan 已提交
120 121 122 123 124 125 126 127
  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;
  }

S
Shengliang Guan 已提交
128
  int32_t code = (*pWrapper->fp.createFp)(pWrapper, pMsg);
S
Shengliang Guan 已提交
129
  if (code != 0) {
S
Shengliang Guan 已提交
130
    dError("node:%s, failed to create since %s", pWrapper->name, terrstr());
S
Shengliang Guan 已提交
131
  } else {
S
Shengliang Guan 已提交
132 133
    dDebug("node:%s, has been created", pWrapper->name);
    pWrapper->required = true;
S
Shengliang Guan 已提交
134
    pWrapper->deployed = true;
135
    pWrapper->procType = pDnode->ptype;
S
Shengliang Guan 已提交
136
    (void)dmOpenNode(pWrapper);
S
Shengliang Guan 已提交
137 138
  }

139
  taosThreadMutexUnlock(&pDnode->mutex);
S
Shengliang Guan 已提交
140 141 142
  return code;
}

S
Shengliang Guan 已提交
143 144
int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype);
S
Shengliang Guan 已提交
145 146 147 148 149 150
  if (pWrapper == NULL) {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    dError("failed to drop node since %s", terrstr());
    return -1;
  }

151
  taosThreadMutexLock(&pDnode->mutex);
S
Shengliang Guan 已提交
152

S
Shengliang Guan 已提交
153
  int32_t code = (*pWrapper->fp.dropFp)(pWrapper, pMsg);
S
Shengliang Guan 已提交
154 155 156 157 158 159
  if (code != 0) {
    dError("node:%s, failed to drop since %s", pWrapper->name, terrstr());
  } else {
    dDebug("node:%s, has been dropped", pWrapper->name);
  }

S
Shengliang Guan 已提交
160
  dmReleaseWrapper(pWrapper);
S
Shengliang Guan 已提交
161 162 163

  if (code == 0) {
    dmCloseNode(pWrapper);
164 165 166
    pWrapper->required = false;
    pWrapper->deployed = false;
    taosRemoveDir(pWrapper->path);
S
Shengliang Guan 已提交
167
  }
168
  taosThreadMutexUnlock(&pDnode->mutex);
S
Shengliang Guan 已提交
169
  return code;
S
Shengliang Guan 已提交
170 171
}

S
Shengliang Guan 已提交
172
static void dmSetMgmtMsgHandle(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
173
  // Requests handled by DNODE
S
Shengliang Guan 已提交
174 175 176 177 178 179 180 181 182
  dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_MNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_DROP_MNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_QNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_DROP_QNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_SNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_DROP_SNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_BNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_DROP_BNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_DND_CONFIG_DNODE, dmProcessMgmtMsg, DEFAULT_HANDLE);
S
shm  
Shengliang Guan 已提交
183 184

  // Requests handled by MNODE
S
Shengliang Guan 已提交
185 186
  dmSetMsgHandle(pWrapper, TDMT_MND_GRANT_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_MND_AUTH_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE);
S
shm  
Shengliang Guan 已提交
187
}
S
Shengliang Guan 已提交
188

S
Shengliang Guan 已提交
189 190 191 192 193 194 195 196 197
static int32_t dmStartMgmt(SMgmtWrapper *pWrapper) {
  if (dmStartStatusThread(pWrapper->pDnode) != 0) {
    return -1;
  }
  if (dmStartMonitorThread(pWrapper->pDnode) != 0) {
    return -1;
  }
  return 0;
}
S
Shengliang Guan 已提交
198

S
Shengliang Guan 已提交
199 200 201 202
static void dmStopMgmt(SMgmtWrapper *pWrapper) {
  dmStopMonitorThread(pWrapper->pDnode);
  dmStopStatusThread(pWrapper->pDnode);
}
S
Shengliang Guan 已提交
203

S
Shengliang Guan 已提交
204
static int32_t dmInitMgmt(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
205
  dInfo("dnode-mgmt start to init");
S
Shengliang Guan 已提交
206 207 208 209 210 211 212 213 214
  SDnode *pDnode = pWrapper->pDnode;

  pDnode->data.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
  if (pDnode->data.dnodeHash == NULL) {
    dError("failed to init dnode hash");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
215
  if (dmReadEps(pDnode) != 0) {
S
Shengliang Guan 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    dError("failed to read file since %s", terrstr());
    return -1;
  }

  if (pDnode->data.dropped) {
    dError("dnode will not start since its already dropped");
    return -1;
  }

  if (dmStartWorker(pDnode) != 0) {
    return -1;
  }

  if (dmInitTrans(pDnode) != 0) {
    dError("failed to init transport since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
234
  dInfo("dnode-mgmt is initialized");
S
Shengliang Guan 已提交
235 236 237
  return 0;
}

S
Shengliang Guan 已提交
238
static void dmCleanupMgmt(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
239
  dInfo("dnode-mgmt start to clean up");
S
Shengliang Guan 已提交
240 241 242 243
  SDnode *pDnode = pWrapper->pDnode;
  dmStopWorker(pDnode);

  taosWLockLatch(&pDnode->data.latch);
S
Shengliang Guan 已提交
244 245 246
  if (pDnode->data.dnodeEps != NULL) {
    taosArrayDestroy(pDnode->data.dnodeEps);
    pDnode->data.dnodeEps = NULL;
S
Shengliang Guan 已提交
247
  }
S
Shengliang Guan 已提交
248 249 250
  if (pDnode->data.dnodeHash != NULL) {
    taosHashCleanup(pDnode->data.dnodeHash);
    pDnode->data.dnodeHash = NULL;
S
Shengliang Guan 已提交
251 252 253
  }
  taosWUnLockLatch(&pDnode->data.latch);

S
Shengliang Guan 已提交
254
  dmCleanupTrans(pDnode);
S
Shengliang Guan 已提交
255
  dInfo("dnode-mgmt is cleaned up");
S
Shengliang Guan 已提交
256 257
}

S
Shengliang Guan 已提交
258
static int32_t dmRequireMgmt(SMgmtWrapper *pWrapper, bool *required) {
S
Shengliang Guan 已提交
259 260 261 262 263 264
  *required = true;
  return 0;
}

void dmSetMgmtFp(SMgmtWrapper *pWrapper) {
  SMgmtFp mgmtFp = {0};
S
Shengliang Guan 已提交
265 266 267 268 269
  mgmtFp.openFp = dmInitMgmt;
  mgmtFp.closeFp = dmCleanupMgmt;
  mgmtFp.startFp = dmStartMgmt;
  mgmtFp.stopFp = dmStopMgmt;
  mgmtFp.requiredFp = dmRequireMgmt;
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271
  dmSetMgmtMsgHandle(pWrapper);
S
Shengliang Guan 已提交
272 273 274
  pWrapper->name = "dnode";
  pWrapper->fp = mgmtFp;
}