dmInt.c 3.8 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * 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
#include "dmInt.h"

static int32_t dmStartMgmt(SDnodeMgmt *pMgmt) {
  if (dmStartStatusThread(pMgmt) != 0) {
    return -1;
  }
  if (dmStartMonitorThread(pMgmt) != 0) {
    return -1;
  }
  return 0;
}

static void dmStopMgmt(SDnodeMgmt *pMgmt) {
  dmStopMonitorThread(pMgmt);
  dmStopStatusThread(pMgmt);
}

static int32_t dmOpenMgmt(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
  dInfo("dnode-mgmt start to init");
  SDnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SDnodeMgmt));
  if (pMgmt == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  pMgmt->data.dnodeId = 0;
  pMgmt->data.clusterId = 0;
  pMgmt->data.dnodeVer = 0;
  pMgmt->data.updateTime = 0;
  pMgmt->data.rebootTime = taosGetTimestampMs();
  pMgmt->data.dropped = 0;
  pMgmt->data.localEp = strdup(pInput->localEp);
  pMgmt->data.localFqdn = strdup(pInput->localFqdn);
  pMgmt->data.firstEp = strdup(pInput->firstEp);
  pMgmt->data.secondEp = strdup(pInput->secondEp);
  pMgmt->data.dataDir = strdup(pInput->dataDir);
  pMgmt->data.disks = pInput->disks;
  pMgmt->data.numOfDisks = pInput->numOfDisks;
  pMgmt->data.supportVnodes = pInput->supportVnodes;
  pMgmt->data.serverPort = pInput->serverPort;
  taosInitRWLatch(&pMgmt->data.latch);

  if (pMgmt->data.dataDir == NULL || pMgmt->data.localEp == NULL || pMgmt->data.localFqdn == NULL ||
      pMgmt->data.firstEp == NULL || pMgmt->data.secondEp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  pMgmt->data.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
  if (pMgmt->data.dnodeHash == NULL) {
    dError("failed to init dnode hash");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  if (dmReadEps(pMgmt) != 0) {
    dError("failed to read file since %s", terrstr());
    return -1;
  }

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

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

  if (udfStartUdfd(pMgmt->data.dnodeId) != 0) {
    dError("failed to start udfd");
  }

  dInfo("dnode-mgmt is initialized");
  return 0;
}

static void dmCloseMgmt(SDnodeMgmt *pMgmt) {
  dInfo("dnode-mgmt start to clean up");
  dmStopWorker(pMgmt);

  taosWLockLatch(&pMgmt->data.latch);
  if (pMgmt->data.dnodeEps != NULL) {
    taosArrayDestroy(pMgmt->data.dnodeEps);
    pMgmt->data.dnodeEps = NULL;
  }
  if (pMgmt->data.dnodeHash != NULL) {
    taosHashCleanup(pMgmt->data.dnodeHash);
    pMgmt->data.dnodeHash = NULL;
  }
  taosWUnLockLatch(&pMgmt->data.latch);

S
Shengliang Guan 已提交
109 110 111 112 113 114
  taosMemoryFreeClear(pMgmt->data.localEp);
  taosMemoryFreeClear(pMgmt->data.localFqdn);
  taosMemoryFreeClear(pMgmt->data.firstEp);
  taosMemoryFreeClear(pMgmt->data.secondEp);
  taosMemoryFreeClear(pMgmt->data.dataDir);

S
Shengliang Guan 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  dInfo("dnode-mgmt is cleaned up");
}

static int32_t dmRequireMgmt(const SMgmtInputOpt *pInput, bool *required) {
  *required = true;
  return 0;
}

SMgmtFunc dmGetMgmtFunc() {
  SMgmtFunc mgmtFunc = {0};
  mgmtFunc.openFp = dmOpenMgmt;
  mgmtFunc.closeFp = (NodeCloseFp)dmCloseMgmt;
  mgmtFunc.startFp = (NodeStartFp)dmStartMgmt;
  mgmtFunc.stopFp = (NodeStopFp)dmStopMgmt;
  mgmtFunc.requiredFp = dmRequireMgmt;
  mgmtFunc.getHandlesFp = dmGetMsgHandles;

  return mgmtFunc;
}