dmObj.c 4.5 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
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "dmImp.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
static int32_t dmInitVars(SDnode *pDnode, const SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
20 21
  pDnode->data.dnodeId = 0;
  pDnode->data.clusterId = 0;
S
Shengliang Guan 已提交
22 23 24 25
  pDnode->data.dnodeVer = 0;
  pDnode->data.updateTime = 0;
  pDnode->data.rebootTime = taosGetTimestampMs();
  pDnode->data.dropped = 0;
S
Shengliang Guan 已提交
26 27 28 29
  pDnode->data.localEp = strdup(pOption->localEp);
  pDnode->data.localFqdn = strdup(pOption->localFqdn);
  pDnode->data.firstEp = strdup(pOption->firstEp);
  pDnode->data.secondEp = strdup(pOption->secondEp);
S
Shengliang Guan 已提交
30
  pDnode->data.dataDir = strdup(pOption->dataDir);
S
Shengliang Guan 已提交
31 32
  pDnode->data.disks = pOption->disks;
  pDnode->data.numOfDisks = pOption->numOfDisks;
S
Shengliang Guan 已提交
33 34
  pDnode->data.supportVnodes = pOption->numOfSupportVnodes;
  pDnode->data.serverPort = pOption->serverPort;
S
Shengliang Guan 已提交
35
  pDnode->ntype = pOption->ntype;
S
shm  
Shengliang Guan 已提交
36

S
Shengliang Guan 已提交
37 38
  if (pDnode->data.dataDir == NULL || pDnode->data.localEp == NULL || pDnode->data.localFqdn == NULL ||
      pDnode->data.firstEp == NULL || pDnode->data.secondEp == NULL) {
S
shm  
Shengliang Guan 已提交
39 40 41
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
shm  
Shengliang Guan 已提交
42

S
Shengliang Guan 已提交
43
  if (!tsMultiProcess || pDnode->ntype == DNODE || pDnode->ntype == NODE_END) {
S
Shengliang Guan 已提交
44
    pDnode->data.lockfile = dmCheckRunning(pDnode->data.dataDir);
S
Shengliang Guan 已提交
45
    if (pDnode->data.lockfile == NULL) {
S
shm  
Shengliang Guan 已提交
46 47
      return -1;
    }
S
shm  
Shengliang Guan 已提交
48 49
  }

S
Shengliang Guan 已提交
50
  taosInitRWLatch(&pDnode->data.latch);
51
  taosThreadMutexInit(&pDnode->mutex, NULL);
S
shm  
Shengliang Guan 已提交
52 53 54
  return 0;
}

S
Shengliang Guan 已提交
55
static void dmClearVars(SDnode *pDnode) {
S
Shengliang Guan 已提交
56
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
57
    SMgmtWrapper *pMgmt = &pDnode->wrappers[n];
wafwerar's avatar
wafwerar 已提交
58
    taosMemoryFreeClear(pMgmt->path);
S
shm  
Shengliang Guan 已提交
59
  }
S
Shengliang Guan 已提交
60 61 62 63
  if (pDnode->data.lockfile != NULL) {
    taosUnLockFile(pDnode->data.lockfile);
    taosCloseFile(&pDnode->data.lockfile);
    pDnode->data.lockfile = NULL;
S
shm  
Shengliang Guan 已提交
64
  }
S
Shengliang Guan 已提交
65 66 67 68 69
  taosMemoryFreeClear(pDnode->data.localEp);
  taosMemoryFreeClear(pDnode->data.localFqdn);
  taosMemoryFreeClear(pDnode->data.firstEp);
  taosMemoryFreeClear(pDnode->data.secondEp);
  taosMemoryFreeClear(pDnode->data.dataDir);
70 71
  taosThreadMutexDestroy(&pDnode->mutex);
  memset(&pDnode->mutex, 0, sizeof(pDnode->mutex));
wafwerar's avatar
wafwerar 已提交
72
  taosMemoryFree(pDnode);
S
shm  
Shengliang Guan 已提交
73
  dDebug("dnode memory is cleared, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
74 75
}

S
Shengliang Guan 已提交
76
SDnode *dmCreate(const SDnodeOpt *pOption) {
S
Shengliang Guan 已提交
77
  dDebug("start to create dnode");
S
shm  
Shengliang Guan 已提交
78
  int32_t code = -1;
S
xshm  
Shengliang Guan 已提交
79
  char    path[PATH_MAX] = {0};
S
shm  
Shengliang Guan 已提交
80 81
  SDnode *pDnode = NULL;

wafwerar's avatar
wafwerar 已提交
82
  pDnode = taosMemoryCalloc(1, sizeof(SDnode));
S
shm  
Shengliang Guan 已提交
83 84 85 86 87
  if (pDnode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

S
Shengliang Guan 已提交
88
  if (dmInitVars(pDnode, pOption) != 0) {
S
shm  
Shengliang Guan 已提交
89
    dError("failed to init variables since %s", terrstr());
S
shm  
Shengliang Guan 已提交
90 91 92
    goto _OVER;
  }

S
Shengliang Guan 已提交
93
  dmSetStatus(pDnode, DND_STAT_INIT);
S
Shengliang Guan 已提交
94
  dmSetMgmtFp(&pDnode->wrappers[DNODE]);
S
Shengliang Guan 已提交
95
  mmSetMgmtFp(&pDnode->wrappers[MNODE]);
S
Shengliang Guan 已提交
96
  vmSetMgmtFp(&pDnode->wrappers[VNODE]);
S
Shengliang Guan 已提交
97 98 99
  qmSetMgmtFp(&pDnode->wrappers[QNODE]);
  smSetMgmtFp(&pDnode->wrappers[SNODE]);
  bmSetMgmtFp(&pDnode->wrappers[BNODE]);
S
shm  
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
  for (EDndNodeType n = DNODE; n < NODE_END; ++n) {
S
shm  
Shengliang Guan 已提交
102
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
Shengliang Guan 已提交
103
    snprintf(path, sizeof(path), "%s%s%s", pDnode->data.dataDir, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
104
    pWrapper->path = strdup(path);
S
Shengliang Guan 已提交
105
    pWrapper->procShm.id = -1;
S
shm  
Shengliang Guan 已提交
106
    pWrapper->pDnode = pDnode;
S
Shengliang Guan 已提交
107
    pWrapper->ntype = n;
S
Shengliang Guan 已提交
108 109 110
    pWrapper->procType = DND_PROC_SINGLE;
    taosInitRWLatch(&pWrapper->latch);

S
shm  
Shengliang Guan 已提交
111 112 113 114 115
    if (pWrapper->path == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

S
Shengliang Guan 已提交
116 117 118 119
    if (n != DNODE && dmReadShmFile(pWrapper) != 0) {
      dError("node:%s, failed to read shm file since %s", pWrapper->name, terrstr());
      goto _OVER;
    }
S
shm  
Shengliang Guan 已提交
120 121
  }

S
Shengliang Guan 已提交
122
  if (dmInitMsgHandle(pDnode) != 0) {
S
Shengliang Guan 已提交
123
    dError("failed to init msg handles since %s", terrstr());
S
shm  
Shengliang Guan 已提交
124 125 126
    goto _OVER;
  }

S
shm  
Shengliang Guan 已提交
127
  dInfo("dnode is created, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
128 129 130 131
  code = 0;

_OVER:
  if (code != 0 && pDnode) {
S
Shengliang Guan 已提交
132
    dmClearVars(pDnode);
133
    pDnode = NULL;
S
shm  
Shengliang Guan 已提交
134
    dError("failed to create dnode since %s", terrstr());
S
shm  
Shengliang Guan 已提交
135 136 137 138 139
  }

  return pDnode;
}

S
Shengliang Guan 已提交
140
void dmClose(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
141
  if (pDnode == NULL) return;
S
Shengliang Guan 已提交
142
  dmClearVars(pDnode);
S
shm  
Shengliang Guan 已提交
143
  dInfo("dnode is closed, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
144
}