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);
S
Shengliang Guan 已提交
51
  taosInitRWLatch(&pDnode->wrapperLock);
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);
wafwerar's avatar
wafwerar 已提交
70
  taosMemoryFree(pDnode);
S
shm  
Shengliang Guan 已提交
71
  dDebug("dnode memory is cleared, data:%p", pDnode);
S
shm  
Shengliang Guan 已提交
72 73
}

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

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

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

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

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

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

S
Shengliang Guan 已提交
114 115 116 117
    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 已提交
118 119
  }

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

S
Shengliang Guan 已提交
125 126
  pDnode->data.msgCb = dmGetMsgcb(&pDnode->wrappers[DNODE]);
  tmsgSetDefaultMsgCb(&pDnode->data.msgCb);
S
shm  
Shengliang Guan 已提交
127

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

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

  return pDnode;
}

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