vnodeMain.c 3.5 KB
Newer Older
H
save  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "vnodeDef.h"

H
more  
Hongze Cheng 已提交
18
static SVnode *vnodeNew(const char *path, const SVnodeCfg *pVnodeCfg, int32_t vid);
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

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

  // Set default options
H
refact  
Hongze Cheng 已提交
27 28
  if (pVnodeCfg == NULL) {
    pVnodeCfg = &defaultVnodeOptions;
H
more  
Hongze Cheng 已提交
29 30 31
  }

  // Validate options
H
refact  
Hongze Cheng 已提交
32
  if (vnodeValidateOptions(pVnodeCfg) < 0) {
H
more  
Hongze Cheng 已提交
33 34 35 36
    // TODO
    return NULL;
  }

H
refact  
Hongze Cheng 已提交
37
  // Create the handle
H
more  
Hongze Cheng 已提交
38
  pVnode = vnodeNew(path, pVnodeCfg, vid);
H
more  
Hongze Cheng 已提交
39 40 41 42 43 44 45
  if (pVnode == NULL) {
    // TODO: handle error
    return NULL;
  }

  taosMkDir(path);

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

H
save  
Hongze Cheng 已提交
52 53 54
  return pVnode;
}

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

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

/* ------------------------ STATIC METHODS ------------------------ */
H
more  
Hongze Cheng 已提交
65
static SVnode *vnodeNew(const char *path, const SVnodeCfg *pVnodeCfg, int32_t vid) {
H
refact  
Hongze Cheng 已提交
66 67 68 69 70 71 72 73
  SVnode *pVnode = NULL;

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

H
more  
Hongze Cheng 已提交
74
  pVnode->vgId = vid;
H
refact  
Hongze Cheng 已提交
75
  pVnode->path = strdup(path);
H
refact  
Hongze Cheng 已提交
76
  vnodeOptionsCopy(&(pVnode->config), pVnodeCfg);
H
refact  
Hongze Cheng 已提交
77

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

H
more  
Hongze Cheng 已提交
80
  return pVnode;
H
save  
Hongze Cheng 已提交
81 82
}

H
more  
Hongze Cheng 已提交
83 84
static void vnodeFree(SVnode *pVnode) {
  if (pVnode) {
H
more  
Hongze Cheng 已提交
85
    tsem_destroy(&(pVnode->canCommit));
H
refact  
Hongze Cheng 已提交
86 87
    tfree(pVnode->path);
    free(pVnode);
H
more  
Hongze Cheng 已提交
88
  }
H
refact  
Hongze Cheng 已提交
89 90 91
}

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

H
more  
Hongze Cheng 已提交
94
  if (vnodeOpenBufPool(pVnode) < 0) {
H
more  
Hongze Cheng 已提交
95 96 97 98
    // TODO: handle error
    return -1;
  }

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

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

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

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

D
dapan1121 已提交
131 132 133 134 135
  // Open Query
  if (vnodeQueryOpen(pVnode)) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
136 137 138 139 140
  // TODO
  return 0;
}

static void vnodeCloseImpl(SVnode *pVnode) {
H
more  
Hongze Cheng 已提交
141
  vnodeSyncCommit(pVnode);
H
refact  
Hongze Cheng 已提交
142
  if (pVnode) {
H
merge  
Hongze Cheng 已提交
143
    vnodeCloseBufPool(pVnode);
H
refact  
Hongze Cheng 已提交
144
    metaClose(pVnode->pMeta);
H
more  
Hongze Cheng 已提交
145 146 147
    tsdbClose(pVnode->pTsdb);
    tqClose(pVnode->pTq);
    walClose(pVnode->pWal);
H
refact  
Hongze Cheng 已提交
148
  }
H
more  
Hongze Cheng 已提交
149
}