vnodeOpen.c 4.7 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

H
Hongze Cheng 已提交
23
int vnodeCreate(const char *path, SVnodeCfg *pCfg, STfs *pTfs) {
H
Hongze Cheng 已提交
24 25
  SVnodeInfo info = {0};
  char       dir[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
26

H
Hongze Cheng 已提交
27 28 29 30 31 32 33 34 35
  // TODO: check if directory exists

  // check config
  if (vnodeCheckCfg(pCfg) < 0) {
    vError("vgId: %d failed to create vnode since: %s", pCfg->vgId, tstrerror(terrno));
    return -1;
  }

  // create vnode env
H
Hongze Cheng 已提交
36 37 38 39 40
  if (tfsMkdir(pTfs, path) < 0) {
    vError("vgId: %d failed to create vnode since: %s", pCfg->vgId, tstrerror(terrno));
    return -1;
  }

H
Hongze Cheng 已提交
41
  snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
H
Hongze Cheng 已提交
42 43 44
  info.config = *pCfg;

  if (vnodeSaveInfo(dir, &info) < 0 || vnodeCommitInfo(dir, &info) < 0) {
H
Hongze Cheng 已提交
45 46 47 48 49 50 51
    vError("vgId: %d failed to save vnode config since %s", pCfg->vgId, tstrerror(terrno));
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
52
SVnode *vnodeOpen(const char *path, const SVnodeCfg *pVnodeCfg) {
H
save  
Hongze Cheng 已提交
53
  SVnode *pVnode = NULL;
H
more  
Hongze Cheng 已提交
54 55

  // Set default options
H
Hongze Cheng 已提交
56
  SVnodeCfg cfg = vnodeCfgDefault;
S
Shengliang Guan 已提交
57 58
  if (pVnodeCfg != NULL) {
    cfg.vgId = pVnodeCfg->vgId;
S
Shengliang Guan 已提交
59
    cfg.msgCb = pVnodeCfg->msgCb;
S
Shengliang Guan 已提交
60
    cfg.pTfs = pVnodeCfg->pTfs;
D
dapan1121 已提交
61
    cfg.dbId = pVnodeCfg->dbId;
D
dapan1121 已提交
62 63 64
    cfg.hashBegin = pVnodeCfg->hashBegin;
    cfg.hashEnd = pVnodeCfg->hashEnd;
    cfg.hashMethod = pVnodeCfg->hashMethod;
S
Shengliang Guan 已提交
65
  }
H
more  
Hongze Cheng 已提交
66 67

  // Validate options
H
Hongze Cheng 已提交
68
  if (vnodeCheckCfg(&cfg) < 0) {
H
more  
Hongze Cheng 已提交
69 70 71 72
    // TODO
    return NULL;
  }

H
refact  
Hongze Cheng 已提交
73
  // Create the handle
S
Shengliang Guan 已提交
74
  pVnode = vnodeNew(path, &cfg);
H
more  
Hongze Cheng 已提交
75 76 77 78 79 80 81
  if (pVnode == NULL) {
    // TODO: handle error
    return NULL;
  }

  taosMkDir(path);

H
refact  
Hongze Cheng 已提交
82 83 84 85 86 87
  // Open the vnode
  if (vnodeOpenImpl(pVnode) < 0) {
    // TODO: handle error
    return NULL;
  }

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

H
refact  
Hongze Cheng 已提交
91 92 93 94 95
void vnodeClose(SVnode *pVnode) {
  if (pVnode) {
    vnodeCloseImpl(pVnode);
    vnodeFree(pVnode);
  }
H
more  
Hongze Cheng 已提交
96 97 98 99 100
}

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

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

wafwerar's avatar
wafwerar 已提交
104
  pVnode = (SVnode *)taosMemoryCalloc(1, sizeof(*pVnode));
H
refact  
Hongze Cheng 已提交
105 106 107 108 109
  if (pVnode == NULL) {
    // TODO
    return NULL;
  }

S
Shengliang Guan 已提交
110
  pVnode->vgId = pVnodeCfg->vgId;
S
Shengliang Guan 已提交
111
  pVnode->msgCb = pVnodeCfg->msgCb;
S
Shengliang Guan 已提交
112
  pVnode->pTfs = pVnodeCfg->pTfs;
H
refact  
Hongze Cheng 已提交
113
  pVnode->path = strdup(path);
H
refact  
Hongze Cheng 已提交
114
  vnodeOptionsCopy(&(pVnode->config), pVnodeCfg);
H
refact  
Hongze Cheng 已提交
115

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

H
more  
Hongze Cheng 已提交
118
  return pVnode;
H
save  
Hongze Cheng 已提交
119 120
}

H
more  
Hongze Cheng 已提交
121 122
static void vnodeFree(SVnode *pVnode) {
  if (pVnode) {
H
more  
Hongze Cheng 已提交
123
    tsem_destroy(&(pVnode->canCommit));
wafwerar's avatar
wafwerar 已提交
124 125
    taosMemoryFreeClear(pVnode->path);
    taosMemoryFree(pVnode);
H
more  
Hongze Cheng 已提交
126
  }
H
refact  
Hongze Cheng 已提交
127 128 129
}

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

H
more  
Hongze Cheng 已提交
132
  if (vnodeOpenBufPool(pVnode) < 0) {
H
more  
Hongze Cheng 已提交
133 134 135 136
    // TODO: handle error
    return -1;
  }

H
refact  
Hongze Cheng 已提交
137 138
  // Open meta
  sprintf(dir, "%s/meta", pVnode->path);
H
more  
Hongze Cheng 已提交
139
  pVnode->pMeta = metaOpen(dir, &(pVnode->config.metaCfg), vBufPoolGetMAF(pVnode));
H
more  
Hongze Cheng 已提交
140
  if (pVnode->pMeta == NULL) {
H
refact  
Hongze Cheng 已提交
141 142 143 144 145 146
    // TODO: handle error
    return -1;
  }

  // Open tsdb
  sprintf(dir, "%s/tsdb", pVnode->path);
L
Liu Jicong 已提交
147 148
  pVnode->pTsdb =
      tsdbOpen(dir, pVnode->vgId, &(pVnode->config.tsdbCfg), vBufPoolGetMAF(pVnode), pVnode->pMeta, pVnode->pTfs);
H
more  
Hongze Cheng 已提交
149
  if (pVnode->pTsdb == NULL) {
H
refact  
Hongze Cheng 已提交
150 151 152 153
    // TODO: handle error
    return -1;
  }

H
merge  
Hongze Cheng 已提交
154 155
  // Open WAL
  sprintf(dir, "%s/wal", pVnode->path);
H
refact  
Hongze Cheng 已提交
156
  pVnode->pWal = walOpen(dir, &(pVnode->config.walCfg));
H
merge  
Hongze Cheng 已提交
157 158 159 160
  if (pVnode->pWal == NULL) {
    // TODO: handle error
    return -1;
  }
H
refact  
Hongze Cheng 已提交
161

L
Liu Jicong 已提交
162 163
  // Open TQ
  sprintf(dir, "%s/tq", pVnode->path);
L
Liu Jicong 已提交
164
  pVnode->pTq = tqOpen(dir, pVnode, pVnode->pWal, pVnode->pMeta, &(pVnode->config.tqCfg), vBufPoolGetMAF(pVnode));
L
Liu Jicong 已提交
165 166 167 168 169
  if (pVnode->pTq == NULL) {
    // TODO: handle error
    return -1;
  }

D
dapan1121 已提交
170 171 172 173 174
  // Open Query
  if (vnodeQueryOpen(pVnode)) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
175 176 177 178 179
  // TODO
  return 0;
}

static void vnodeCloseImpl(SVnode *pVnode) {
H
more  
Hongze Cheng 已提交
180
  vnodeSyncCommit(pVnode);
H
refact  
Hongze Cheng 已提交
181
  if (pVnode) {
H
merge  
Hongze Cheng 已提交
182
    vnodeCloseBufPool(pVnode);
H
refact  
Hongze Cheng 已提交
183
    metaClose(pVnode->pMeta);
H
more  
Hongze Cheng 已提交
184 185 186
    tsdbClose(pVnode->pTsdb);
    tqClose(pVnode->pTq);
    walClose(pVnode->pWal);
D
dapan1121 已提交
187
    vnodeQueryClose(pVnode);
H
refact  
Hongze Cheng 已提交
188
  }
L
Liu Jicong 已提交
189
}