dndObj.c 5.6 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
shm  
Shengliang Guan 已提交
16 17 18
#define _DEFAULT_SOURCE
#include "dndInt.h"

S
shm  
Shengliang Guan 已提交
19
static int32_t dndInitVars(SDnode *pDnode, const SDnodeOpt *pOption) {
S
shm  
Shengliang Guan 已提交
20 21 22 23 24 25 26
  pDnode->numOfSupportVnodes = pOption->numOfSupportVnodes;
  pDnode->serverPort = pOption->serverPort;
  pDnode->dataDir = strdup(pOption->dataDir);
  pDnode->localEp = strdup(pOption->localEp);
  pDnode->localFqdn = strdup(pOption->localFqdn);
  pDnode->firstEp = strdup(pOption->firstEp);
  pDnode->secondEp = strdup(pOption->secondEp);
S
Shengliang Guan 已提交
27
  pDnode->disks = pOption->disks;
S
shm  
Shengliang Guan 已提交
28
  pDnode->numOfDisks = pOption->numOfDisks;
S
Shengliang Guan 已提交
29
  pDnode->ntype = pOption->ntype;
S
shm  
Shengliang Guan 已提交
30 31 32 33 34 35 36 37 38 39
  pDnode->rebootTime = taosGetTimestampMs();

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

S
shm  
Shengliang Guan 已提交
40
static void dndClearVars(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
41 42
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pMgmt = &pDnode->wrappers[n];
wafwerar's avatar
wafwerar 已提交
43
    taosMemoryFreeClear(pMgmt->path);
S
shm  
Shengliang Guan 已提交
44
  }
S
shm  
Shengliang Guan 已提交
45
  dndCloseRuntimeFile(pDnode);
wafwerar's avatar
wafwerar 已提交
46 47 48 49 50 51
  taosMemoryFreeClear(pDnode->localEp);
  taosMemoryFreeClear(pDnode->localFqdn);
  taosMemoryFreeClear(pDnode->firstEp);
  taosMemoryFreeClear(pDnode->secondEp);
  taosMemoryFreeClear(pDnode->dataDir);
  taosMemoryFree(pDnode);
S
shm  
Shengliang Guan 已提交
52 53 54 55 56 57
  dDebug("dnode object memory is cleared, data:%p", pDnode);
}

SDnode *dndCreate(const SDnodeOpt *pOption) {
  dInfo("start to create dnode object");
  int32_t code = -1;
S
xshm  
Shengliang Guan 已提交
58
  char    path[PATH_MAX] = {0};
S
shm  
Shengliang Guan 已提交
59 60
  SDnode *pDnode = NULL;

wafwerar's avatar
wafwerar 已提交
61
  pDnode = taosMemoryCalloc(1, sizeof(SDnode));
S
shm  
Shengliang Guan 已提交
62 63 64 65 66
  if (pDnode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

S
shm  
Shengliang Guan 已提交
67 68
  if (dndInitVars(pDnode, pOption) != 0) {
    dError("failed to init variables since %s", terrstr());
S
shm  
Shengliang Guan 已提交
69 70 71 72
    goto _OVER;
  }

  dndSetStatus(pDnode, DND_STAT_INIT);
S
shm  
Shengliang Guan 已提交
73 74 75 76 77 78 79 80 81
  dmGetMgmtFp(&pDnode->wrappers[DNODE]);
  mmGetMgmtFp(&pDnode->wrappers[MNODE]);
  vmGetMgmtFp(&pDnode->wrappers[VNODES]);
  qmGetMgmtFp(&pDnode->wrappers[QNODE]);
  smGetMgmtFp(&pDnode->wrappers[SNODE]);
  bmGetMgmtFp(&pDnode->wrappers[BNODE]);

  if (dndOpenRuntimeFile(pDnode) != 0) {
    dError("failed to open runtime file since %s", terrstr());
S
shm  
Shengliang Guan 已提交
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 109
    goto _OVER;
  }

  if (dndInitServer(pDnode) != 0) {
    dError("failed to init trans server since %s", terrstr());
    goto _OVER;
  }

  if (dndInitClient(pDnode) != 0) {
    dError("failed to init trans client since %s", terrstr());
    goto _OVER;
  }

  if (dndInitMsgHandle(pDnode) != 0) {
    goto _OVER;
  }

  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    snprintf(path, sizeof(path), "%s%s%s", pDnode->dataDir, TD_DIRSEP, pWrapper->name);
    pWrapper->path = strdup(path);
    pWrapper->pDnode = pDnode;
    if (pWrapper->path == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

    pWrapper->procType = PROC_SINGLE;
S
shm  
Shengliang Guan 已提交
110
    taosInitRWLatch(&pWrapper->latch);
S
shm  
Shengliang Guan 已提交
111 112 113 114 115 116
  }

  code = 0;

_OVER:
  if (code != 0 && pDnode) {
S
shm  
Shengliang Guan 已提交
117
    dndClearVars(pDnode);
118
    pDnode = NULL;
S
shm  
Shengliang Guan 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    dError("failed to create dnode object since %s", terrstr());
  } else {
    dInfo("dnode object is created, data:%p", pDnode);
  }

  return pDnode;
}

void dndClose(SDnode *pDnode) {
  if (pDnode == NULL) return;

  if (dndGetStatus(pDnode) == DND_STAT_STOPPED) {
    dError("dnode is shutting down, data:%p", pDnode);
    return;
  }

  dInfo("start to close dnode, data:%p", pDnode);
  dndSetStatus(pDnode, DND_STAT_STOPPED);

  dndCleanupServer(pDnode);
  dndCleanupClient(pDnode);

S
shm  
Shengliang Guan 已提交
141 142 143
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
    dndCloseNode(pWrapper);
S
shm  
Shengliang Guan 已提交
144 145
  }

S
shm  
Shengliang Guan 已提交
146
  dndClearVars(pDnode);
S
shm  
Shengliang Guan 已提交
147 148 149 150 151 152 153
  dInfo("dnode object is closed, data:%p", pDnode);
}

void dndHandleEvent(SDnode *pDnode, EDndEvent event) {
  dInfo("dnode object receive event %d, data:%p", event, pDnode);
  pDnode->event = event;
}
S
shm  
Shengliang Guan 已提交
154

S
shm  
Shengliang Guan 已提交
155 156
SMgmtWrapper *dndAcquireWrapper(SDnode *pDnode, ENodeType ntype) {
  SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
S
shm  
Shengliang Guan 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  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
shm  
Shengliang Guan 已提交
172 173 174 175
int32_t dndMarkWrapper(SMgmtWrapper *pWrapper) {
  int32_t code = 0;

  taosRLockLatch(&pWrapper->latch);
S
shm  
Shengliang Guan 已提交
176
  if (pWrapper->deployed || (pWrapper->procType == PROC_PARENT && pWrapper->required)) {
S
shm  
Shengliang Guan 已提交
177 178 179 180 181 182 183 184 185 186 187
    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
shm  
Shengliang Guan 已提交
188 189 190 191 192 193 194 195
void dndReleaseWrapper(SMgmtWrapper *pWrapper) {
  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);
}