vnodeOpen.c 6.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 "vnd.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
  // TODO: check if directory exists

  // check config
  if (vnodeCheckCfg(pCfg) < 0) {
S
Shengliang Guan 已提交
26
    vError("vgId:%d, failed to create vnode since:%s", pCfg->vgId, tstrerror(terrno));
H
Hongze Cheng 已提交
27 28 29 30
    return -1;
  }

  // create vnode env
H
Hongze Cheng 已提交
31 32 33 34 35 36 37 38 39 40
  if (pTfs) {
    if (tfsMkdirAt(pTfs, path, (SDiskID){0}) < 0) {
      vError("vgId:%d, failed to create vnode since:%s", pCfg->vgId, tstrerror(terrno));
      return -1;
    }
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
  } else {
    if (taosMkDir(path)) {
      return TAOS_SYSTEM_ERROR(errno);
    }
H
Hongze Cheng 已提交
41
    snprintf(dir, TSDB_FILENAME_LEN, "%s", path);
H
Hongze Cheng 已提交
42 43
  }

H
Hongze Cheng 已提交
44 45 46 47 48
  if (pCfg) {
    info.config = *pCfg;
  } else {
    info.config = vnodeCfgDefault;
  }
M
Minghao Li 已提交
49 50
  info.state.committed = -1;
  info.state.applied = -1;
H
Hongze Cheng 已提交
51
  info.state.commitID = 0;
H
Hongze Cheng 已提交
52 53

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

H
Hongze Cheng 已提交
58
  vInfo("vgId:%d, vnode is created", info.config.vgId);
H
Hongze Cheng 已提交
59

H
Hongze Cheng 已提交
60 61 62
  return 0;
}

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

H
Hongze Cheng 已提交
65 66 67 68
SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) {
  SVnode    *pVnode = NULL;
  SVnodeInfo info = {0};
  char       dir[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
69
  char       tdir[TSDB_FILENAME_LEN * 2];
H
Hongze Cheng 已提交
70
  int        ret;
H
more  
Hongze Cheng 已提交
71

H
Hongze Cheng 已提交
72 73 74 75 76
  if (pTfs) {
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
  } else {
    snprintf(dir, TSDB_FILENAME_LEN, "%s", path);
  }
H
Hongze Cheng 已提交
77

H
Hongze Cheng 已提交
78 79
  info.config = vnodeCfgDefault;

H
Hongze Cheng 已提交
80 81 82 83
  // 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 已提交
84 85 86
    return NULL;
  }

H
Hongze Cheng 已提交
87
  // create handle
H
Hongze Cheng 已提交
88
  pVnode = (SVnode *)taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
H
more  
Hongze Cheng 已提交
89
  if (pVnode == NULL) {
H
Hongze Cheng 已提交
90
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
91
    vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
H
more  
Hongze Cheng 已提交
92 93 94
    return NULL;
  }

H
Hongze Cheng 已提交
95 96
  pVnode->path = (char *)&pVnode[1];
  strcpy(pVnode->path, path);
H
Hongze Cheng 已提交
97
  pVnode->config = info.config;
H
Hongze Cheng 已提交
98
  pVnode->state.committed = info.state.committed;
H
Hongze Cheng 已提交
99
  pVnode->state.commitTerm = info.state.commitTerm;
H
Hongze Cheng 已提交
100
  pVnode->state.applied = info.state.committed;
H
Hongze Cheng 已提交
101
  pVnode->state.commitID = info.state.commitID;
H
Hongze Cheng 已提交
102
  pVnode->state.commitTerm = info.state.commitTerm;
H
Hongze Cheng 已提交
103 104
  pVnode->pTfs = pTfs;
  pVnode->msgCb = msgCb;
105
  taosThreadMutexInit(&pVnode->lock, NULL);
106
  pVnode->blocked = false;
H
Hongze Cheng 已提交
107

108
  tsem_init(&pVnode->syncSem, 0, 0);
H
Hongze Cheng 已提交
109
  tsem_init(&(pVnode->canCommit), 0, 1);
H
Hongze Cheng 已提交
110 111
  taosThreadMutexInit(&pVnode->mutex, NULL);
  taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
H
Hongze Cheng 已提交
112

H
Hongze Cheng 已提交
113 114
  int8_t rollback = vnodeShouldRollback(pVnode);

H
Hongze Cheng 已提交
115
  // open buffer pool
H
Hongze Cheng 已提交
116
  if (vnodeOpenBufPool(pVnode) < 0) {
S
Shengliang Guan 已提交
117
    vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
118
    goto _err;
H
more  
Hongze Cheng 已提交
119 120
  }

H
Hongze Cheng 已提交
121
  // open meta
H
Hongze Cheng 已提交
122
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
S
Shengliang Guan 已提交
123
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
124
    goto _err;
H
refact  
Hongze Cheng 已提交
125 126
  }

H
Hongze Cheng 已提交
127
  // open tsdb
H
Hongze Cheng 已提交
128
  if (!VND_IS_RSMA(pVnode) && tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback) < 0) {
S
Shengliang Guan 已提交
129
    vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
130 131 132 133
    goto _err;
  }

  // open sma
H
Hongze Cheng 已提交
134
  if (smaOpen(pVnode, rollback)) {
S
Shengliang Guan 已提交
135
    vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
136
    goto _err;
H
refact  
Hongze Cheng 已提交
137 138
  }

H
Hongze Cheng 已提交
139 140
  // open wal
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
wafwerar's avatar
wafwerar 已提交
141
  taosRealPath(tdir, NULL, sizeof(tdir));
142

H
Hongze Cheng 已提交
143
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
H
merge  
Hongze Cheng 已提交
144
  if (pVnode->pWal == NULL) {
145
    vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
H
Hongze Cheng 已提交
146
    goto _err;
H
merge  
Hongze Cheng 已提交
147
  }
H
refact  
Hongze Cheng 已提交
148

H
Hongze Cheng 已提交
149 150
  // open tq
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
wafwerar's avatar
wafwerar 已提交
151
  taosRealPath(tdir, NULL, sizeof(tdir));
L
Liu Jicong 已提交
152
  pVnode->pTq = tqOpen(tdir, pVnode);
L
Liu Jicong 已提交
153
  if (pVnode->pTq == NULL) {
S
Shengliang Guan 已提交
154
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
155
    goto _err;
L
Liu Jicong 已提交
156 157
  }

H
Hongze Cheng 已提交
158
  // open query
D
dapan1121 已提交
159
  if (vnodeQueryOpen(pVnode)) {
S
Shengliang Guan 已提交
160
    vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
161
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
162
    goto _err;
D
dapan1121 已提交
163 164
  }

H
Hongze Cheng 已提交
165 166
  // vnode begin
  if (vnodeBegin(pVnode) < 0) {
S
Shengliang Guan 已提交
167
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
168
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
169 170 171
    goto _err;
  }

172 173
  // open sync
  if (vnodeSyncOpen(pVnode, dir)) {
S
Shengliang Guan 已提交
174
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
175 176
    goto _err;
  }
H
Hongze Cheng 已提交
177 178 179 180

  if (rollback) {
    vnodeRollback(pVnode);
  }
H
Hongze Cheng 已提交
181 182 183 184 185 186 187

  return pVnode;

_err:
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
  if (pVnode->pTq) tqClose(pVnode->pTq);
  if (pVnode->pWal) walClose(pVnode->pWal);
188
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
C
Cary Xu 已提交
189
  if (pVnode->pSma) smaClose(pVnode->pSma);
H
Hongze Cheng 已提交
190
  if (pVnode->pMeta) metaClose(pVnode->pMeta);
191
  if (pVnode->pPool) vnodeCloseBufPool(pVnode);
C
Cary Xu 已提交
192

H
Hongze Cheng 已提交
193 194 195
  tsem_destroy(&(pVnode->canCommit));
  taosMemoryFree(pVnode);
  return NULL;
H
refact  
Hongze Cheng 已提交
196 197
}

198 199 200 201 202 203
void vnodePreClose(SVnode *pVnode) {
  if (pVnode) {
    syncLeaderTransfer(pVnode->sync);
  }
}

H
Hongze Cheng 已提交
204
void vnodeClose(SVnode *pVnode) {
H
refact  
Hongze Cheng 已提交
205
  if (pVnode) {
H
Hongze Cheng 已提交
206
    vnodeCommit(pVnode);
M
Minghao Li 已提交
207
    vnodeSyncClose(pVnode);
D
dapan1121 已提交
208
    vnodeQueryClose(pVnode);
H
Hongze Cheng 已提交
209 210
    walClose(pVnode->pWal);
    tqClose(pVnode->pTq);
211
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
C
Cary Xu 已提交
212
    smaClose(pVnode->pSma);
H
Hongze Cheng 已提交
213 214 215 216
    metaClose(pVnode->pMeta);
    vnodeCloseBufPool(pVnode);
    // destroy handle
    tsem_destroy(&(pVnode->canCommit));
217
    tsem_destroy(&pVnode->syncSem);
H
Hongze Cheng 已提交
218 219
    taosThreadCondDestroy(&pVnode->poolNotEmpty);
    taosThreadMutexDestroy(&pVnode->mutex);
220
    taosThreadMutexDestroy(&pVnode->lock);
H
Hongze Cheng 已提交
221
    taosMemoryFree(pVnode);
H
refact  
Hongze Cheng 已提交
222
  }
L
Liu Jicong 已提交
223
}
H
Hongze Cheng 已提交
224

225 226 227 228 229 230 231 232
// start the sync timer after the queue is ready
int32_t vnodeStart(SVnode *pVnode) {
  vnodeSyncStart(pVnode);
  return 0;
}

void vnodeStop(SVnode *pVnode) {}

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

H
Hongze Cheng 已提交
235 236 237 238 239 240
void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot) {
  pSnapshot->data = NULL;
  pSnapshot->lastApplyIndex = pVnode->state.committed;
  pSnapshot->lastApplyTerm = pVnode->state.commitTerm;
  pSnapshot->lastConfigIndex = -1;
}