vnodeMain.c 3.6 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
refact  
Hongze Cheng 已提交
16
#include "vnd.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 30
  SVnodeCfg cfg = defaultVnodeOptions;
  if (pVnodeCfg != NULL) {
    cfg.vgId = pVnodeCfg->vgId;
    cfg.pDnode = pVnodeCfg->pDnode;
S
Shengliang Guan 已提交
31
    cfg.pTfs = pVnodeCfg->pTfs;
S
Shengliang Guan 已提交
32
  }
H
more  
Hongze Cheng 已提交
33 34

  // Validate options
S
Shengliang Guan 已提交
35
  if (vnodeValidateOptions(&cfg) < 0) {
H
more  
Hongze Cheng 已提交
36 37 38 39
    // TODO
    return NULL;
  }

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

  taosMkDir(path);

H
refact  
Hongze Cheng 已提交
49 50 51 52 53 54
  // Open the vnode
  if (vnodeOpenImpl(pVnode) < 0) {
    // TODO: handle error
    return NULL;
  }

H
save  
Hongze Cheng 已提交
55 56 57
  return pVnode;
}

H
refact  
Hongze Cheng 已提交
58 59 60 61 62
void vnodeClose(SVnode *pVnode) {
  if (pVnode) {
    vnodeCloseImpl(pVnode);
    vnodeFree(pVnode);
  }
H
more  
Hongze Cheng 已提交
63 64 65 66 67
}

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

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

  pVnode = (SVnode *)calloc(1, sizeof(*pVnode));
  if (pVnode == NULL) {
    // TODO
    return NULL;
  }

S
Shengliang Guan 已提交
77 78
  pVnode->vgId = pVnodeCfg->vgId;
  pVnode->pDnode = pVnodeCfg->pDnode;
S
Shengliang Guan 已提交
79
  pVnode->pTfs = pVnodeCfg->pTfs;
H
refact  
Hongze Cheng 已提交
80
  pVnode->path = strdup(path);
H
refact  
Hongze Cheng 已提交
81
  vnodeOptionsCopy(&(pVnode->config), pVnodeCfg);
H
refact  
Hongze Cheng 已提交
82

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

H
more  
Hongze Cheng 已提交
85
  return pVnode;
H
save  
Hongze Cheng 已提交
86 87
}

H
more  
Hongze Cheng 已提交
88 89
static void vnodeFree(SVnode *pVnode) {
  if (pVnode) {
H
more  
Hongze Cheng 已提交
90
    tsem_destroy(&(pVnode->canCommit));
H
refact  
Hongze Cheng 已提交
91 92
    tfree(pVnode->path);
    free(pVnode);
H
more  
Hongze Cheng 已提交
93
  }
H
refact  
Hongze Cheng 已提交
94 95 96
}

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

H
more  
Hongze Cheng 已提交
99
  if (vnodeOpenBufPool(pVnode) < 0) {
H
more  
Hongze Cheng 已提交
100 101 102 103
    // TODO: handle error
    return -1;
  }

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

  // Open tsdb
  sprintf(dir, "%s/tsdb", pVnode->path);
S
Shengliang Guan 已提交
114
  pVnode->pTsdb = tsdbOpen(dir, pVnode->vgId, &(pVnode->config.tsdbCfg), vBufPoolGetMAF(pVnode), pVnode->pMeta, pVnode->pTfs);
H
more  
Hongze Cheng 已提交
115
  if (pVnode->pTsdb == NULL) {
H
refact  
Hongze Cheng 已提交
116 117 118 119
    // TODO: handle error
    return -1;
  }

L
Liu Jicong 已提交
120
  // Open TQ
H
more  
Hongze Cheng 已提交
121
  sprintf(dir, "%s/tq", pVnode->path);
L
Liu Jicong 已提交
122
  pVnode->pTq = tqOpen(dir, &(pVnode->config.tqCfg), vBufPoolGetMAF(pVnode));
H
refact  
Hongze Cheng 已提交
123 124 125 126
  if (pVnode->pTq == NULL) {
    // TODO: handle error
    return -1;
  }
H
merge  
Hongze Cheng 已提交
127 128 129

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

D
dapan1121 已提交
136 137 138 139 140
  // Open Query
  if (vnodeQueryOpen(pVnode)) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
141 142 143 144 145
  // TODO
  return 0;
}

static void vnodeCloseImpl(SVnode *pVnode) {
H
more  
Hongze Cheng 已提交
146
  vnodeSyncCommit(pVnode);
H
refact  
Hongze Cheng 已提交
147
  if (pVnode) {
H
merge  
Hongze Cheng 已提交
148
    vnodeCloseBufPool(pVnode);
H
refact  
Hongze Cheng 已提交
149
    metaClose(pVnode->pMeta);
H
more  
Hongze Cheng 已提交
150 151 152
    tsdbClose(pVnode->pTsdb);
    tqClose(pVnode->pTq);
    walClose(pVnode->pWal);
H
refact  
Hongze Cheng 已提交
153
  }
L
Liu Jicong 已提交
154
}