vnodeMain.c 3.9 KB
Newer Older
H
save  
Hongze Cheng 已提交
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/>.
 */

H
Hongze Cheng 已提交
16
#include "vnodeInt.h"
H
save  
Hongze Cheng 已提交
17

S
Shengliang Guan 已提交
18
static SVnode *vnodeNew(const char *path, const SVnodeCfg *pVnodeCfg);
H
more  
Hongze Cheng 已提交
19
static void    vnodeFree(SVnode *pVnode);
H
refact  
Hongze Cheng 已提交
20 21
static int     vnodeOpenImpl(SVnode *pVnode);
static void    vnodeCloseImpl(SVnode *pVnode);
H
more  
Hongze Cheng 已提交
22

S
Shengliang Guan 已提交
23
SVnode *vnodeOpen(const char *path, const SVnodeCfg *pVnodeCfg) {
H
save  
Hongze Cheng 已提交
24
  SVnode *pVnode = NULL;
H
more  
Hongze Cheng 已提交
25 26

  // Set default options
S
Shengliang Guan 已提交
27 28 29
  SVnodeCfg cfg = defaultVnodeOptions;
  if (pVnodeCfg != NULL) {
    cfg.vgId = pVnodeCfg->vgId;
S
Shengliang Guan 已提交
30
    cfg.msgCb = pVnodeCfg->msgCb;
S
Shengliang Guan 已提交
31
    cfg.pTfs = pVnodeCfg->pTfs;
D
dapan1121 已提交
32
    cfg.dbId = pVnodeCfg->dbId;
D
dapan1121 已提交
33 34 35
    cfg.hashBegin = pVnodeCfg->hashBegin;
    cfg.hashEnd = pVnodeCfg->hashEnd;
    cfg.hashMethod = pVnodeCfg->hashMethod;
S
Shengliang Guan 已提交
36
  }
H
more  
Hongze Cheng 已提交
37 38

  // Validate options
S
Shengliang Guan 已提交
39
  if (vnodeValidateOptions(&cfg) < 0) {
H
more  
Hongze Cheng 已提交
40 41 42 43
    // TODO
    return NULL;
  }

H
refact  
Hongze Cheng 已提交
44
  // Create the handle
S
Shengliang Guan 已提交
45
  pVnode = vnodeNew(path, &cfg);
H
more  
Hongze Cheng 已提交
46 47 48 49 50 51 52
  if (pVnode == NULL) {
    // TODO: handle error
    return NULL;
  }

  taosMkDir(path);

H
refact  
Hongze Cheng 已提交
53 54 55 56 57 58
  // Open the vnode
  if (vnodeOpenImpl(pVnode) < 0) {
    // TODO: handle error
    return NULL;
  }

H
save  
Hongze Cheng 已提交
59 60 61
  return pVnode;
}

H
refact  
Hongze Cheng 已提交
62 63 64 65 66
void vnodeClose(SVnode *pVnode) {
  if (pVnode) {
    vnodeCloseImpl(pVnode);
    vnodeFree(pVnode);
  }
H
more  
Hongze Cheng 已提交
67 68 69 70 71
}

void vnodeDestroy(const char *path) { taosRemoveDir(path); }

/* ------------------------ STATIC METHODS ------------------------ */
S
Shengliang Guan 已提交
72
static SVnode *vnodeNew(const char *path, const SVnodeCfg *pVnodeCfg) {
H
refact  
Hongze Cheng 已提交
73 74
  SVnode *pVnode = NULL;

wafwerar's avatar
wafwerar 已提交
75
  pVnode = (SVnode *)taosMemoryCalloc(1, sizeof(*pVnode));
H
refact  
Hongze Cheng 已提交
76 77 78 79 80
  if (pVnode == NULL) {
    // TODO
    return NULL;
  }

S
Shengliang Guan 已提交
81
  pVnode->vgId = pVnodeCfg->vgId;
S
Shengliang Guan 已提交
82
  pVnode->msgCb = pVnodeCfg->msgCb;
S
Shengliang Guan 已提交
83
  pVnode->pTfs = pVnodeCfg->pTfs;
H
refact  
Hongze Cheng 已提交
84
  pVnode->path = strdup(path);
H
refact  
Hongze Cheng 已提交
85
  vnodeOptionsCopy(&(pVnode->config), pVnodeCfg);
H
refact  
Hongze Cheng 已提交
86

H
more  
Hongze Cheng 已提交
87 88
  tsem_init(&(pVnode->canCommit), 0, 1);

H
more  
Hongze Cheng 已提交
89
  return pVnode;
H
save  
Hongze Cheng 已提交
90 91
}

H
more  
Hongze Cheng 已提交
92 93
static void vnodeFree(SVnode *pVnode) {
  if (pVnode) {
H
more  
Hongze Cheng 已提交
94
    tsem_destroy(&(pVnode->canCommit));
wafwerar's avatar
wafwerar 已提交
95 96
    taosMemoryFreeClear(pVnode->path);
    taosMemoryFree(pVnode);
H
more  
Hongze Cheng 已提交
97
  }
H
refact  
Hongze Cheng 已提交
98 99 100
}

static int vnodeOpenImpl(SVnode *pVnode) {
H
refact  
Hongze Cheng 已提交
101 102
  char dir[TSDB_FILENAME_LEN];

H
more  
Hongze Cheng 已提交
103
  if (vnodeOpenBufPool(pVnode) < 0) {
H
more  
Hongze Cheng 已提交
104 105 106 107
    // TODO: handle error
    return -1;
  }

H
refact  
Hongze Cheng 已提交
108 109
  // Open meta
  sprintf(dir, "%s/meta", pVnode->path);
H
more  
Hongze Cheng 已提交
110
  pVnode->pMeta = metaOpen(dir, &(pVnode->config.metaCfg), vBufPoolGetMAF(pVnode));
H
more  
Hongze Cheng 已提交
111
  if (pVnode->pMeta == NULL) {
H
refact  
Hongze Cheng 已提交
112 113 114 115 116 117
    // TODO: handle error
    return -1;
  }

  // Open tsdb
  sprintf(dir, "%s/tsdb", pVnode->path);
L
Liu Jicong 已提交
118 119
  pVnode->pTsdb =
      tsdbOpen(dir, pVnode->vgId, &(pVnode->config.tsdbCfg), vBufPoolGetMAF(pVnode), pVnode->pMeta, pVnode->pTfs);
H
more  
Hongze Cheng 已提交
120
  if (pVnode->pTsdb == NULL) {
H
refact  
Hongze Cheng 已提交
121 122 123 124
    // TODO: handle error
    return -1;
  }

H
merge  
Hongze Cheng 已提交
125 126
  // Open WAL
  sprintf(dir, "%s/wal", pVnode->path);
H
refact  
Hongze Cheng 已提交
127
  pVnode->pWal = walOpen(dir, &(pVnode->config.walCfg));
H
merge  
Hongze Cheng 已提交
128 129 130 131
  if (pVnode->pWal == NULL) {
    // TODO: handle error
    return -1;
  }
H
refact  
Hongze Cheng 已提交
132

L
Liu Jicong 已提交
133 134
  // Open TQ
  sprintf(dir, "%s/tq", pVnode->path);
L
Liu Jicong 已提交
135
  pVnode->pTq = tqOpen(dir, pVnode, pVnode->pWal, pVnode->pMeta, &(pVnode->config.tqCfg), vBufPoolGetMAF(pVnode));
L
Liu Jicong 已提交
136 137 138 139 140
  if (pVnode->pTq == NULL) {
    // TODO: handle error
    return -1;
  }

D
dapan1121 已提交
141 142 143 144 145
  // Open Query
  if (vnodeQueryOpen(pVnode)) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
146 147 148 149 150
  // TODO
  return 0;
}

static void vnodeCloseImpl(SVnode *pVnode) {
H
more  
Hongze Cheng 已提交
151
  vnodeSyncCommit(pVnode);
H
refact  
Hongze Cheng 已提交
152
  if (pVnode) {
H
merge  
Hongze Cheng 已提交
153
    vnodeCloseBufPool(pVnode);
H
refact  
Hongze Cheng 已提交
154
    metaClose(pVnode->pMeta);
H
more  
Hongze Cheng 已提交
155 156 157
    tsdbClose(pVnode->pTsdb);
    tqClose(pVnode->pTq);
    walClose(pVnode->pWal);
D
dapan1121 已提交
158
    vnodeQueryClose(pVnode);
H
refact  
Hongze Cheng 已提交
159
  }
L
Liu Jicong 已提交
160
}