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

H
Hongze Cheng 已提交
18
int vnodeCreate(const char *path, SVnodeCfg *pCfg, STfs *pTfs) {
H
Hongze Cheng 已提交
19 20
  SVnodeInfo info = {0};
  char       dir[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22 23 24 25 26 27 28 29 30
  // 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 已提交
31 32 33 34 35
  if (tfsMkdir(pTfs, path) < 0) {
    vError("vgId: %d failed to create vnode since: %s", pCfg->vgId, tstrerror(terrno));
    return -1;
  }

H
Hongze Cheng 已提交
36
  snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
H
Hongze Cheng 已提交
37 38 39
  info.config = *pCfg;

  if (vnodeSaveInfo(dir, &info) < 0 || vnodeCommitInfo(dir, &info) < 0) {
H
Hongze Cheng 已提交
40 41 42 43
    vError("vgId: %d failed to save vnode config since %s", pCfg->vgId, tstrerror(terrno));
    return -1;
  }

H
Hongze Cheng 已提交
44 45
  vInfo("vgId: %d vnode is created", pCfg->vgId);

H
Hongze Cheng 已提交
46 47 48
  return 0;
}

H
refact  
Hongze Cheng 已提交
49 50
void vnodeDestroy(const char *path, STfs *pTfs) { tfsRmdir(pTfs, path); }

H
Hongze Cheng 已提交
51 52 53 54
SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) {
  SVnode    *pVnode = NULL;
  SVnodeInfo info = {0};
  char       dir[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
55
  char       tdir[TSDB_FILENAME_LEN * 2];
H
Hongze Cheng 已提交
56
  int        ret;
H
more  
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58 59 60 61 62 63
  snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);

  // load vnode info
  ret = vnodeLoadInfo(dir, &info);
  if (ret < 0) {
    vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
H
more  
Hongze Cheng 已提交
64 65 66
    return NULL;
  }

H
Hongze Cheng 已提交
67
  // create handle
H
Hongze Cheng 已提交
68
  pVnode = (SVnode *)taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
H
more  
Hongze Cheng 已提交
69
  if (pVnode == NULL) {
H
Hongze Cheng 已提交
70 71
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    vError("vgId: %d failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
H
more  
Hongze Cheng 已提交
72 73 74
    return NULL;
  }

H
Hongze Cheng 已提交
75 76
  pVnode->path = (char *)&pVnode[1];
  strcpy(pVnode->path, path);
H
Hongze Cheng 已提交
77
  pVnode->config = info.config;
H
Hongze Cheng 已提交
78
  pVnode->state = info.state;
H
Hongze Cheng 已提交
79 80 81 82 83
  pVnode->pTfs = pTfs;
  pVnode->msgCb = msgCb;

  tsem_init(&(pVnode->canCommit), 0, 1);

H
Hongze Cheng 已提交
84
  // open buffer pool
H
more  
Hongze Cheng 已提交
85
  if (vnodeOpenBufPool(pVnode) < 0) {
H
Hongze Cheng 已提交
86 87
    vError("vgId: %d failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
H
more  
Hongze Cheng 已提交
88 89
  }

H
Hongze Cheng 已提交
90
  // open meta
H
Hongze Cheng 已提交
91
  if (metaOpen(pVnode, &pVnode->pMeta) < 0) {
H
Hongze Cheng 已提交
92 93
    vError("vgId: %d failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
H
refact  
Hongze Cheng 已提交
94 95
  }

H
Hongze Cheng 已提交
96 97
  // open tsdb
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_TSDB_DIR);
H
Hongze Cheng 已提交
98
  pVnode->pTsdb = tsdbOpen(tdir, pVnode, &(pVnode->config.tsdbCfg), vBufPoolGetMAF(pVnode));
H
more  
Hongze Cheng 已提交
99
  if (pVnode->pTsdb == NULL) {
H
Hongze Cheng 已提交
100 101
    vError("vgId: %d failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
H
refact  
Hongze Cheng 已提交
102 103
  }

H
Hongze Cheng 已提交
104 105 106
  // open wal
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
H
merge  
Hongze Cheng 已提交
107
  if (pVnode->pWal == NULL) {
H
Hongze Cheng 已提交
108 109
    vError("vgId: %d failed to open vnode wal since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
H
merge  
Hongze Cheng 已提交
110
  }
H
refact  
Hongze Cheng 已提交
111

H
Hongze Cheng 已提交
112 113 114
  // open tq
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
  pVnode->pTq = tqOpen(tdir, pVnode, pVnode->pWal, pVnode->pMeta, vBufPoolGetMAF(pVnode));
L
Liu Jicong 已提交
115
  if (pVnode->pTq == NULL) {
H
Hongze Cheng 已提交
116 117
    vError("vgId: %d failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
L
Liu Jicong 已提交
118 119
  }

H
Hongze Cheng 已提交
120
  // open query
D
dapan1121 已提交
121
  if (vnodeQueryOpen(pVnode)) {
H
Hongze Cheng 已提交
122 123
    vError("vgId: %d failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
D
dapan1121 已提交
124 125
  }

M
Minghao Li 已提交
126 127
  // sync integration
  // open sync
M
Minghao Li 已提交
128
  if (vnodeSyncOpen(pVnode, dir)) {
M
Minghao Li 已提交
129 130 131
    goto _err;
  }

H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#if 0
  if (vnodeBegin() < 0) {
    goto _err;
  }
#endif

  return pVnode;

_err:
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
  if (pVnode->pTq) tqClose(pVnode->pTq);
  if (pVnode->pWal) walClose(pVnode->pWal);
  if (pVnode->pTsdb) tsdbClose(pVnode->pTsdb);
  if (pVnode->pMeta) metaClose(pVnode->pMeta);
  tsem_destroy(&(pVnode->canCommit));
  taosMemoryFree(pVnode);
  return NULL;
H
refact  
Hongze Cheng 已提交
149 150
}

H
Hongze Cheng 已提交
151
void vnodeClose(SVnode *pVnode) {
H
refact  
Hongze Cheng 已提交
152
  if (pVnode) {
H
Hongze Cheng 已提交
153 154 155
    // commit (TODO: use option to control)
    vnodeSyncCommit(pVnode);
    // close vnode
D
dapan1121 已提交
156
    vnodeQueryClose(pVnode);
M
Minghao Li 已提交
157 158 159 160

    // sync integration
    vnodeSyncClose(pVnode);

H
Hongze Cheng 已提交
161 162 163 164 165 166 167 168
    walClose(pVnode->pWal);
    tqClose(pVnode->pTq);
    tsdbClose(pVnode->pTsdb);
    metaClose(pVnode->pMeta);
    vnodeCloseBufPool(pVnode);
    // destroy handle
    tsem_destroy(&(pVnode->canCommit));
    taosMemoryFree(pVnode);
H
refact  
Hongze Cheng 已提交
169
  }
L
Liu Jicong 已提交
170
}
H
Hongze Cheng 已提交
171

H
Hongze Cheng 已提交
172 173 174
int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; }

void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot) { pSnapshot->lastApplyIndex = pVnode->state.committed; }