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

S
Shengliang Guan 已提交
19
static void dmUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) {
20
  if (pDnode->data.dnodeId == 0 || pDnode->data.clusterId == 0) {
S
Shengliang Guan 已提交
21 22 23 24 25 26 27 28
    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 40
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
        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
    }
dengyihao's avatar
dengyihao 已提交
45
    rpcFreeCont(pRsp->pCont);
S
Shengliang Guan 已提交
46 47 48 49 50 51 52
    tFreeSStatusRsp(&statusRsp);
  }

  return TSDB_CODE_SUCCESS;
}

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

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

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

S
Shengliang Guan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  SMonVloadInfo vinfo = {0};
  dmGetVnodeLoads(pDnode, &vinfo);
  req.pVloads = vinfo.pVloads;
  pDnode->data.unsyncedVgId = 0;
  pDnode->data.vndState = TAOS_SYNC_STATE_LEADER;
  for (int32_t i = 0; i < taosArrayGetSize(req.pVloads); ++i) {
    SVnodeLoad *pLoad = taosArrayGet(req.pVloads, i);
    if (pLoad->syncState != TAOS_SYNC_STATE_LEADER && pLoad->syncState != TAOS_SYNC_STATE_FOLLOWER) {
      pDnode->data.unsyncedVgId = pLoad->vgId;
      pDnode->data.vndState = pLoad->syncState;
    }
  }

  SMonMloadInfo minfo = {0};
  dmGetMnodeLoads(pDnode, &minfo);
  pDnode->data.isMnode = minfo.isMnode;
  pDnode->data.mndState = minfo.load.syncState;
S
shm  
Shengliang Guan 已提交
93 94

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

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

S
shm  
Shengliang Guan 已提交
102
  dTrace("send req:%s to mnode, app:%p", TMSG_INFO(rpcMsg.msgType), rpcMsg.ahandle);
S
Shengliang Guan 已提交
103
  dmSendToMnodeRecv(pDnode, &rpcMsg, &rpcRsp);
S
Shengliang Guan 已提交
104
  dmProcessStatusRsp(pDnode, &rpcRsp);
S
shm  
Shengliang Guan 已提交
105 106
}

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

S
Shengliang Guan 已提交
113
int32_t dmProcessGrantRsp(SDnode *pDnode, SNodeMsg *pMsg) {
S
shm  
Shengliang Guan 已提交
114 115 116 117
  SRpcMsg *pRsp = &pMsg->rpcMsg;
  dError("grant rsp is received, but not supported yet");
  return 0;
}
S
shm  
Shengliang Guan 已提交
118

S
Shengliang Guan 已提交
119
int32_t dmProcessConfigReq(SDnode *pDnode, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
120
  SRpcMsg       *pReq = &pMsg->rpcMsg;
S
shm  
Shengliang Guan 已提交
121
  SDCfgDnodeReq *pCfg = pReq->pCont;
S
shm  
Shengliang Guan 已提交
122
  dError("config req is received, but not supported yet");
S
shm  
Shengliang Guan 已提交
123 124 125
  return TSDB_CODE_OPS_NOT_SUPPORT;
}

S
Shengliang Guan 已提交
126 127
int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype);
S
Shengliang Guan 已提交
128
  if (pWrapper != NULL) {
S
Shengliang Guan 已提交
129
    dmReleaseWrapper(pWrapper);
S
Shengliang Guan 已提交
130 131 132 133 134
    terrno = TSDB_CODE_NODE_ALREADY_DEPLOYED;
    dError("failed to create node since %s", terrstr());
    return -1;
  }

135
  taosThreadMutexLock(&pDnode->mutex);
S
Shengliang Guan 已提交
136 137 138 139 140 141 142 143
  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 已提交
144
  int32_t code = (*pWrapper->fp.createFp)(pWrapper, pMsg);
S
Shengliang Guan 已提交
145
  if (code != 0) {
S
Shengliang Guan 已提交
146
    dError("node:%s, failed to create since %s", pWrapper->name, terrstr());
S
Shengliang Guan 已提交
147
  } else {
S
Shengliang Guan 已提交
148
    dDebug("node:%s, has been created", pWrapper->name);
149
    (void)dmOpenNode(pWrapper);
S
Shengliang Guan 已提交
150
    pWrapper->required = true;
S
Shengliang Guan 已提交
151
    pWrapper->deployed = true;
152
    pWrapper->procType = pDnode->ptype;
S
Shengliang Guan 已提交
153 154
  }

155
  taosThreadMutexUnlock(&pDnode->mutex);
S
Shengliang Guan 已提交
156 157 158
  return code;
}

S
Shengliang Guan 已提交
159 160
int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype);
S
Shengliang Guan 已提交
161 162 163 164 165 166
  if (pWrapper == NULL) {
    terrno = TSDB_CODE_NODE_NOT_DEPLOYED;
    dError("failed to drop node since %s", terrstr());
    return -1;
  }

167
  taosThreadMutexLock(&pDnode->mutex);
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169
  int32_t code = (*pWrapper->fp.dropFp)(pWrapper, pMsg);
S
Shengliang Guan 已提交
170 171 172 173
  if (code != 0) {
    dError("node:%s, failed to drop since %s", pWrapper->name, terrstr());
  } else {
    dDebug("node:%s, has been dropped", pWrapper->name);
174 175
    pWrapper->required = false;
    pWrapper->deployed = false;
S
Shengliang Guan 已提交
176 177
  }

S
Shengliang Guan 已提交
178
  dmReleaseWrapper(pWrapper);
S
Shengliang Guan 已提交
179 180

  if (code == 0) {
181
    dmCloseNode(pWrapper);
182
    taosRemoveDir(pWrapper->path);
S
Shengliang Guan 已提交
183
  }
184
  taosThreadMutexUnlock(&pDnode->mutex);
S
Shengliang Guan 已提交
185
  return code;
S
Shengliang Guan 已提交
186 187
}

S
Shengliang Guan 已提交
188
static void dmSetMgmtMsgHandle(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
189
  // Requests handled by DNODE
S
Shengliang Guan 已提交
190 191 192 193 194 195 196 197 198
  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 已提交
199 200

  // Requests handled by MNODE
S
Shengliang Guan 已提交
201 202
  dmSetMsgHandle(pWrapper, TDMT_MND_GRANT_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE);
  dmSetMsgHandle(pWrapper, TDMT_MND_AUTH_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE);
S
shm  
Shengliang Guan 已提交
203
}
S
Shengliang Guan 已提交
204

S
Shengliang Guan 已提交
205 206 207 208 209 210 211 212 213
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 已提交
214

S
Shengliang Guan 已提交
215 216 217 218
static void dmStopMgmt(SMgmtWrapper *pWrapper) {
  dmStopMonitorThread(pWrapper->pDnode);
  dmStopStatusThread(pWrapper->pDnode);
}
S
Shengliang Guan 已提交
219

220
static int32_t dmSpawnUdfd(SDnode *pDnode);
S
slzhou 已提交
221 222 223

void dmUdfdExit(uv_process_t *process, int64_t exitStatus, int termSignal) {
  dInfo("udfd process exited with status %" PRId64 ", signal %d", exitStatus, termSignal);
224
  SDnode *pDnode = process->data;
S
slzhou 已提交
225 226
  if (exitStatus == 0 && termSignal == 0 || atomic_load_32(&pDnode->udfdData.stopCalled)) {
    dInfo("udfd process exit due to SIGINT or dnode-mgmt called stop");
S
slzhou 已提交
227
  } else {
S
slzhou 已提交
228
    dInfo("udfd process restart");
229
    dmSpawnUdfd(pDnode);
S
slzhou 已提交
230 231 232
  }
}

233
static int32_t dmSpawnUdfd(SDnode *pDnode) {
S
slzhou 已提交
234 235 236
  dInfo("dnode start spawning udfd");
  uv_process_options_t options = {0};

S
slzhou 已提交
237
  char path[PATH_MAX] = {0};
238 239 240 241 242 243
  if (tsProcPath == NULL) {
    path[0] = '.';
  } else {
    strncpy(path, tsProcPath, strlen(tsProcPath));
    taosDirName(path);
  }
wafwerar's avatar
wafwerar 已提交
244 245 246
#ifdef WINDOWS
  strcat(path, "udfd.exe");
#else
S
slzhou 已提交
247
  strcat(path, "/udfd");
wafwerar's avatar
wafwerar 已提交
248
#endif
S
slzhou 已提交
249 250 251 252 253
  char* argsUdfd[] = {path, "-c", configDir, NULL};
  options.args = argsUdfd;
  options.file = path;

  options.exit_cb = dmUdfdExit;
S
slzhou 已提交
254

255 256
  SUdfdData *pData = &pDnode->udfdData;
  uv_pipe_init(&pData->loop, &pData->ctrlPipe, 1);
S
slzhou 已提交
257 258

  uv_stdio_container_t child_stdio[3];
259 260 261
  child_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  child_stdio[0].data.stream = (uv_stream_t*) &pData->ctrlPipe;
  child_stdio[1].flags = UV_IGNORE;
S
slzhou 已提交
262 263
  child_stdio[2].flags = UV_INHERIT_FD;
  child_stdio[2].data.fd = 2;
264
  options.stdio_count = 3;
S
slzhou 已提交
265 266
  options.stdio = child_stdio;

S
slzhou 已提交
267 268
  options.flags = UV_PROCESS_DETACHED;

S
slzhou 已提交
269 270
  char dnodeIdEnvItem[32] = {0};
  char thrdPoolSizeEnvItem[32] = {0};
271
  snprintf(dnodeIdEnvItem, 32, "%s=%d", "DNODE_ID", pDnode->data.dnodeId);
S
slzhou 已提交
272 273 274 275 276 277
  float numCpuCores = 4;
  taosGetCpuCores(&numCpuCores);
  snprintf(thrdPoolSizeEnvItem,32,  "%s=%d", "UV_THREADPOOL_SIZE", (int)numCpuCores*2);
  char* envUdfd[] = {dnodeIdEnvItem, thrdPoolSizeEnvItem, NULL};
  options.env = envUdfd;

278 279
  int err = uv_spawn(&pData->loop, &pData->process, &options);
  pData->process.data = (void*)pDnode;
S
slzhou 已提交
280 281 282 283 284 285 286

  if (err != 0) {
    dError("can not spawn udfd. path: %s, error: %s", path, uv_strerror(err));
  }
  return err;
}

S
slzhou 已提交
287 288 289 290 291 292
static void dmUdfdCloseWalkCb(uv_handle_t* handle, void* arg) {
  if (!uv_is_closing(handle)) {
    uv_close(handle, NULL);
  }
}

S
slzhou 已提交
293 294 295 296 297 298 299
static void dmUdfdStopAsyncCb(uv_async_t *async) {
  SDnode *pDnode = async->data;
  SUdfdData *pData = &pDnode->udfdData;
  uv_stop(&pData->loop);
}

static void dmWatchUdfd(void *args) {
300 301 302
  SDnode *pDnode = args;
  SUdfdData *pData = &pDnode->udfdData;
  uv_loop_init(&pData->loop);
S
slzhou 已提交
303 304
  uv_async_init(&pData->loop, &pData->stopAsync, dmUdfdStopAsyncCb);
  pData->stopAsync.data = pDnode;
305 306 307
  int32_t err = dmSpawnUdfd(pDnode);
  atomic_store_32(&pData->spawnErr, err);
  uv_barrier_wait(&pData->barrier);
S
slzhou 已提交
308
  uv_run(&pData->loop, UV_RUN_DEFAULT);
S
slzhou 已提交
309 310 311 312 313
  uv_loop_close(&pData->loop);

  uv_walk(&pData->loop, dmUdfdCloseWalkCb, NULL);
  uv_run(&pData->loop, UV_RUN_DEFAULT);
  uv_loop_close(&pData->loop);
S
slzhou 已提交
314 315 316
  return;
}

S
slzhou 已提交
317
static int32_t dmStartUdfd(SDnode *pDnode) {
318 319 320
  char dnodeId[8] = {0};
  snprintf(dnodeId, sizeof(dnodeId), "%d", pDnode->data.dnodeId);
  uv_os_setenv("DNODE_ID", dnodeId);
321 322 323 324 325
  SUdfdData *pData = &pDnode->udfdData;
  if (pData->startCalled) {
    dInfo("dnode-mgmt start udfd already called");
    return 0;
  }
S
slzhou 已提交
326
  pData->startCalled = true;
327 328 329
  uv_barrier_init(&pData->barrier, 2);
  uv_thread_create(&pData->thread, dmWatchUdfd, pDnode);
  uv_barrier_wait(&pData->barrier);
S
slzhou 已提交
330 331 332 333 334 335 336 337 338 339 340
  int32_t err = atomic_load_32(&pData->spawnErr);
  if (err != 0) {
    uv_barrier_destroy(&pData->barrier);
    uv_async_send(&pData->stopAsync);
    uv_thread_join(&pData->thread);
    pData->needCleanUp = false;
    dInfo("dnode-mgmt udfd cleaned up after spawn err");
  } else {
    pData->needCleanUp = true;
  }
  return err;
S
slzhou 已提交
341 342
}

S
slzhou 已提交
343
static int32_t dmStopUdfd(SDnode *pDnode) {
344 345 346
  dInfo("dnode-mgmt to stop udfd. need cleanup: %d, spawn err: %d",
        pDnode->udfdData.needCleanUp, pDnode->udfdData.spawnErr);
  SUdfdData *pData = &pDnode->udfdData;
S
slzhou 已提交
347
  if (!pData->needCleanUp || atomic_load_32(&pData->stopCalled)) {
S
slzhou 已提交
348 349
    return 0;
  }
S
slzhou 已提交
350 351
  atomic_store_32(&pData->stopCalled, 1);
  pData->needCleanUp = false;
352
  uv_barrier_destroy(&pData->barrier);
S
slzhou 已提交
353
  uv_async_send(&pData->stopAsync);
354
  uv_thread_join(&pData->thread);
S
slzhou 已提交
355
  dInfo("dnode-mgmt udfd cleaned up");
S
slzhou 已提交
356 357 358
  return 0;
}

S
Shengliang Guan 已提交
359
static int32_t dmInitMgmt(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
360
  dInfo("dnode-mgmt start to init");
S
Shengliang Guan 已提交
361 362 363 364 365 366 367 368 369
  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 已提交
370
  if (dmReadEps(pDnode) != 0) {
S
Shengliang Guan 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    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 已提交
388
  dmReportStartup(pDnode, "dnode-transport", "initialized");
S
Shengliang Guan 已提交
389

S
slzhou 已提交
390 391 392
  if (dmStartUdfd(pDnode) != 0) {
    dError("failed to start udfd");
  }
S
slzhou 已提交
393

S
Shengliang Guan 已提交
394
  dInfo("dnode-mgmt is initialized");
S
Shengliang Guan 已提交
395 396 397
  return 0;
}

S
Shengliang Guan 已提交
398
static void dmCleanupMgmt(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
399
  dInfo("dnode-mgmt start to clean up");
S
Shengliang Guan 已提交
400
  SDnode *pDnode = pWrapper->pDnode;
S
slzhou 已提交
401
  dmStopUdfd(pDnode);
S
Shengliang Guan 已提交
402 403 404
  dmStopWorker(pDnode);

  taosWLockLatch(&pDnode->data.latch);
S
Shengliang Guan 已提交
405 406 407
  if (pDnode->data.dnodeEps != NULL) {
    taosArrayDestroy(pDnode->data.dnodeEps);
    pDnode->data.dnodeEps = NULL;
S
Shengliang Guan 已提交
408
  }
S
Shengliang Guan 已提交
409 410 411
  if (pDnode->data.dnodeHash != NULL) {
    taosHashCleanup(pDnode->data.dnodeHash);
    pDnode->data.dnodeHash = NULL;
S
Shengliang Guan 已提交
412 413 414
  }
  taosWUnLockLatch(&pDnode->data.latch);

S
Shengliang Guan 已提交
415
  dmCleanupTrans(pDnode);
S
Shengliang Guan 已提交
416
  dInfo("dnode-mgmt is cleaned up");
S
Shengliang Guan 已提交
417 418
}

S
Shengliang Guan 已提交
419
static int32_t dmRequireMgmt(SMgmtWrapper *pWrapper, bool *required) {
S
Shengliang Guan 已提交
420 421 422 423 424 425
  *required = true;
  return 0;
}

void dmSetMgmtFp(SMgmtWrapper *pWrapper) {
  SMgmtFp mgmtFp = {0};
S
Shengliang Guan 已提交
426 427 428 429 430
  mgmtFp.openFp = dmInitMgmt;
  mgmtFp.closeFp = dmCleanupMgmt;
  mgmtFp.startFp = dmStartMgmt;
  mgmtFp.stopFp = dmStopMgmt;
  mgmtFp.requiredFp = dmRequireMgmt;
S
Shengliang Guan 已提交
431

S
Shengliang Guan 已提交
432
  dmSetMgmtMsgHandle(pWrapper);
S
Shengliang Guan 已提交
433 434 435
  pWrapper->name = "dnode";
  pWrapper->fp = mgmtFp;
}