• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4912

04 Jan 2026 09:05AM UTC coverage: 64.888% (-0.1%) from 65.028%
#4912

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

5351 existing lines in 123 files now uncovered.

194856 of 300296 relevant lines covered (64.89%)

118198896.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.02
/source/dnode/vnode/src/vnd/vnodeOpen.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "sync.h"
17
#include "tss.h"
18
#include "tq.h"
19
#include "tsdb.h"
20
#include "vnd.h"
21

22
void vnodeGetPrimaryDir(const char *relPath, int32_t diskPrimary, STfs *pTfs, char *buf, size_t bufLen) {
72,506,353✔
23
  if (pTfs) {
72,506,353✔
24
    SDiskID diskId = {0};
72,505,739✔
25
    diskId.id = diskPrimary;
72,505,739✔
26
    snprintf(buf, bufLen - 1, "%s%s%s", tfsGetDiskPath(pTfs, diskId), TD_DIRSEP, relPath);
72,505,739✔
27
  } else {
28
    snprintf(buf, bufLen - 1, "%s", relPath);
1,535✔
29
  }
30
  buf[bufLen - 1] = '\0';
72,503,440✔
31
}
72,511,799✔
32

33
void vnodeGetPrimaryPath(SVnode *pVnode, bool mount, char *buf, size_t bufLen) {
65,433,199✔
34
  if (pVnode->mounted) {
65,433,199✔
35
    if (mount) {  // mount path
19,800✔
36
      SDiskID diskId = {0};
×
37
      diskId.id = pVnode->diskPrimary;
×
38
      snprintf(buf, bufLen - 1, "%s%svnode%svnode%d", tfsGetDiskPath(pVnode->pMountTfs, diskId), TD_DIRSEP, TD_DIRSEP,
×
39
               pVnode->config.mountVgId);
40
    } else {  // host path
41
      vnodeGetPrimaryDir(pVnode->path, 0, pVnode->pTfs, buf, bufLen);
19,800✔
42
    }
43
    buf[bufLen - 1] = '\0';
19,800✔
44

45
  } else {
46
    vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, buf, bufLen);
65,413,150✔
47
  }
48
}
65,437,658✔
49

50
static int32_t vnodeMkDir(STfs *pTfs, const char *path) {
6,282,220✔
51
  if (pTfs) {
6,282,220✔
52
    return tfsMkdirRecur(pTfs, path);
6,282,863✔
53
  } else {
UNCOV
54
    return taosMkDir(path);
×
55
  }
56
}
57

58
int32_t vnodeCreate(const char *path, SVnodeCfg *pCfg, int32_t diskPrimary, STfs *pTfs) {
2,605,234✔
59
  int32_t    code = 0;
2,605,234✔
60
  SVnodeInfo info = {0};
2,605,234✔
61
  char       dir[TSDB_FILENAME_LEN] = {0};
2,605,406✔
62

63
  // check config
64
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
2,605,406✔
65
    vError("vgId:%d, failed to create vnode since:%s", pCfg->vgId, tstrerror(code));
×
66
    return code;
×
67
  }
68

69
  // create vnode env
70
  if (vnodeMkDir(pTfs, path)) {
2,605,406✔
71
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", pCfg->vgId, strerror(ERRNO), path);
×
72
    return TAOS_SYSTEM_ERROR(ERRNO);
×
73
  }
74
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
2,605,406✔
75

76
  if (pCfg) {
2,605,406✔
77
    info.config = *pCfg;
2,605,406✔
78
  } else {
79
    info.config = vnodeCfgDefault;
×
80
  }
81
  info.state.committed = -1;
2,605,406✔
82
  info.state.applied = -1;
2,605,406✔
83
  info.state.commitID = 0;
2,605,406✔
84

85
  SVnodeInfo oldInfo = {0};
2,605,406✔
86
  oldInfo.config = vnodeCfgDefault;
2,605,406✔
87
  if (vnodeLoadInfo(dir, &oldInfo) == 0) {
2,605,406✔
88
    code = (oldInfo.config.dbId == info.config.dbId) ? 0 : TSDB_CODE_VND_ALREADY_EXIST_BUT_NOT_MATCH;
×
89
    if (code == 0) {
×
90
      vWarn("vgId:%d, vnode config info already exists at %s.", oldInfo.config.vgId, dir);
×
91
    } else {
92
      vError("vgId:%d, vnode config info already exists at %s. oldDbId:%" PRId64 "(%s) at cluster:%" PRId64
×
93
             ", newDbId:%" PRId64 "(%s) at cluser:%" PRId64 ", code:%s",
94
             oldInfo.config.vgId, dir, oldInfo.config.dbId, oldInfo.config.dbname,
95
             oldInfo.config.syncCfg.nodeInfo[oldInfo.config.syncCfg.myIndex].clusterId, info.config.dbId,
96
             info.config.dbname, info.config.syncCfg.nodeInfo[info.config.syncCfg.myIndex].clusterId, tstrerror(code));
97
    }
98
    return code;
×
99
  }
100

101
  vInfo("vgId:%d, save config while create", info.config.vgId);
2,605,406✔
102
  if ((code = vnodeSaveInfo(dir, &info)) < 0 || (code = vnodeCommitInfo(dir)) < 0) {
2,605,406✔
103
    vError("vgId:%d, failed to save vnode config since %s", pCfg ? pCfg->vgId : 0, tstrerror(code));
×
104
    return code;
×
105
  }
106

107
  vInfo("vgId:%d, vnode is created", info.config.vgId);
2,605,406✔
108
  return 0;
2,605,108✔
109
}
110

111
bool vnodeShouldRemoveWal(SVnode *pVnode) { return pVnode->config.walCfg.clearFiles == 1; }
751,858✔
112

113
int32_t vnodeAlterReplica(const char *path, SAlterVnodeReplicaReq *pReq, int32_t diskPrimary, STfs *pTfs) {
751,858✔
114
  SVnodeInfo info = {0};
751,858✔
115
  char       dir[TSDB_FILENAME_LEN] = {0};
751,858✔
116
  int32_t    ret = 0;
751,858✔
117

118
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
751,858✔
119

120
  ret = vnodeLoadInfo(dir, &info);
751,858✔
121
  if (ret < 0) {
751,858✔
122
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->vgId, path, tstrerror(terrno));
×
123
    return ret;
×
124
  }
125

126
  SSyncCfg *pCfg = &info.config.syncCfg;
751,858✔
127

128
  pCfg->replicaNum = 0;
751,858✔
129
  pCfg->totalReplicaNum = 0;
751,858✔
130
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
751,858✔
131

132
  for (int i = 0; i < pReq->replica; ++i) {
2,916,850✔
133
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
2,164,992✔
134
    pNode->nodeId = pReq->replicas[i].id;
2,164,992✔
135
    pNode->nodePort = pReq->replicas[i].port;
2,164,992✔
136
    tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
2,164,992✔
137
    pNode->nodeRole = TAOS_SYNC_ROLE_VOTER;
2,164,992✔
138
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
2,164,992✔
139
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
2,164,992✔
140
    pCfg->replicaNum++;
2,164,992✔
141
  }
142
  if (pReq->selfIndex != -1) {
751,858✔
143
    pCfg->myIndex = pReq->selfIndex;
751,858✔
144
  }
145
  for (int i = pCfg->replicaNum; i < pReq->replica + pReq->learnerReplica; ++i) {
936,732✔
146
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
184,874✔
147
    pNode->nodeId = pReq->learnerReplicas[pCfg->totalReplicaNum].id;
184,874✔
148
    pNode->nodePort = pReq->learnerReplicas[pCfg->totalReplicaNum].port;
184,874✔
149
    pNode->nodeRole = TAOS_SYNC_ROLE_LEARNER;
184,874✔
150
    tstrncpy(pNode->nodeFqdn, pReq->learnerReplicas[pCfg->totalReplicaNum].fqdn, sizeof(pNode->nodeFqdn));
184,874✔
151
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
184,874✔
152
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
184,874✔
153
    pCfg->totalReplicaNum++;
184,874✔
154
  }
155
  pCfg->totalReplicaNum += pReq->replica;
751,858✔
156
  if (pReq->learnerSelfIndex != -1) {
751,858✔
157
    pCfg->myIndex = pReq->replica + pReq->learnerSelfIndex;
×
158
  }
159
  pCfg->changeVersion = pReq->changeVersion;
751,858✔
160

161
  if (info.config.walCfg.clearFiles) {
751,858✔
162
    info.config.walCfg.clearFiles = 0;
×
163

164
    vInfo("vgId:%d, reset wal clearFiles", pReq->vgId);
×
165
  }
166

167
  vInfo("vgId:%d, save config while alter, replicas:%d totalReplicas:%d selfIndex:%d changeVersion:%d", pReq->vgId,
751,858✔
168
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
169

170
  info.config.syncCfg = *pCfg;
751,858✔
171
  ret = vnodeSaveInfo(dir, &info);
751,858✔
172
  if (ret < 0) {
751,858✔
173
    vError("vgId:%d, failed to save vnode config since %s", pReq->vgId, tstrerror(terrno));
×
174
    return ret;
×
175
  }
176

177
  ret = vnodeCommitInfo(dir);
751,858✔
178
  if (ret < 0) {
751,858✔
179
    vError("vgId:%d, failed to commit vnode config since %s", pReq->vgId, tstrerror(terrno));
×
180
    return ret;
×
181
  }
182

183
  vInfo("vgId:%d, vnode config is saved", info.config.vgId);
751,858✔
184
  return 0;
751,858✔
185
}
186

187
static int32_t vnodeVgroupIdLen(int32_t vgId) {
86,854✔
188
  char tmp[TSDB_FILENAME_LEN];
86,854✔
189
  (void)tsnprintf(tmp, TSDB_FILENAME_LEN, "%d", vgId);
86,854✔
190
  return strlen(tmp);
86,854✔
191
}
192

193
int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
30,409✔
194
                            int32_t diskPrimary, STfs *pTfs) {
195
  int32_t ret = 0;
30,409✔
196

197
  char oldRname[TSDB_FILENAME_LEN] = {0};
30,409✔
198
  char newRname[TSDB_FILENAME_LEN] = {0};
30,409✔
199
  char tsdbPath[TSDB_FILENAME_LEN] = {0};
30,409✔
200
  char tsdbFilePrefix[TSDB_FILENAME_LEN] = {0};
30,409✔
201
  snprintf(tsdbPath, TSDB_FILENAME_LEN, "%s%stsdb", srcPath, TD_DIRSEP);
30,409✔
202
  snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP);
30,409✔
203
  int32_t prefixLen = strlen(tsdbFilePrefix);
30,409✔
204

205
  STfsDir *tsdbDir = NULL;
30,409✔
206
  int32_t  tret = tfsOpendir(pTfs, tsdbPath, &tsdbDir);
30,409✔
207
  if (tsdbDir == NULL) {
30,409✔
208
    return 0;
×
209
  }
210

211
  while (1) {
147,672✔
212
    const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
178,081✔
213
    if (tsdbFile == NULL) break;
178,081✔
214
    if (tsdbFile->rname[0] == '\0') continue;
147,672✔
215
    tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
147,672✔
216

217
    char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
147,672✔
218
    if (tsdbFilePrefixPos == NULL) continue;
147,672✔
219

220
    int32_t tsdbFileVgId = 0;
86,854✔
221
    ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
86,854✔
222
    if (ret != 0) {
86,854✔
223
      vError("vgId:%d, failed to get tsdb file vgid since %s", dstVgId, tstrerror(ret));
×
224
      tfsClosedir(tsdbDir);
×
225
      return ret;
×
226
    }
227

228
    if (tsdbFileVgId == srcVgId) {
86,854✔
229
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
86,854✔
230

231
      tsdbFilePrefixPos[prefixLen] = 0;
86,854✔
232
      snprintf(newRname, TSDB_FILENAME_LEN, "%s%d%s", oldRname, dstVgId, tsdbFileSurfixPos);
86,854✔
233
      vInfo("vgId:%d, rename file from %s to %s", dstVgId, tsdbFile->rname, newRname);
86,854✔
234

235
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
86,854✔
236
      if (ret != 0) {
86,854✔
237
        vError("vgId:%d, failed to rename file from %s to %s since %s", dstVgId, tsdbFile->rname, newRname, terrstr());
×
238
        tfsClosedir(tsdbDir);
×
239
        return ret;
×
240
      }
241
    }
242
  }
243

244
  tfsClosedir(tsdbDir);
30,409✔
245

246
  vInfo("vgId:%d, rename dir from %s to %s", dstVgId, srcPath, dstPath);
30,409✔
247
  ret = tfsRename(pTfs, diskPrimary, srcPath, dstPath);
30,409✔
248
  if (ret != 0) {
30,409✔
249
    vError("vgId:%d, failed to rename dir from %s to %s since %s", dstVgId, srcPath, dstPath, terrstr());
×
250
  }
251
  return ret;
30,409✔
252
}
253

254
int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnodeHashRangeReq *pReq,
30,409✔
255
                            int32_t diskPrimary, STfs *pTfs) {
256
  SVnodeInfo info = {0};
30,409✔
257
  char       dir[TSDB_FILENAME_LEN] = {0};
30,409✔
258
  int32_t    ret = 0;
30,409✔
259

260
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
30,409✔
261

262
  ret = vnodeLoadInfo(dir, &info);
30,409✔
263
  if (ret < 0) {
30,409✔
264
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->srcVgId, srcPath, tstrerror(terrno));
×
265
    return ret;
×
266
  }
267

268
  vInfo("vgId:%d, alter hashrange from [%u, %u] to [%u, %u]", pReq->srcVgId, info.config.hashBegin, info.config.hashEnd,
30,409✔
269
        pReq->hashBegin, pReq->hashEnd);
270
  info.config.vgId = pReq->dstVgId;
30,409✔
271
  info.config.hashBegin = pReq->hashBegin;
30,409✔
272
  info.config.hashEnd = pReq->hashEnd;
30,409✔
273
  info.config.hashChange = true;
30,409✔
274
  info.config.walCfg.vgId = pReq->dstVgId;
30,409✔
275
  info.config.syncCfg.changeVersion = pReq->changeVersion;
30,409✔
276

277
  SSyncCfg *pCfg = &info.config.syncCfg;
30,409✔
278
  pCfg->myIndex = 0;
30,409✔
279
  pCfg->replicaNum = 1;
30,409✔
280
  pCfg->totalReplicaNum = 1;
30,409✔
281
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
30,409✔
282

283
  vInfo("vgId:%d, alter vnode replicas to 1", pReq->srcVgId);
30,409✔
284
  SNodeInfo *pNode = &pCfg->nodeInfo[0];
30,409✔
285
  pNode->nodePort = tsServerPort;
30,409✔
286
  tstrncpy(pNode->nodeFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
30,409✔
287
  bool ret1 = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
30,409✔
288
  vInfo("vgId:%d, ep:%s:%u dnode:%d", pReq->srcVgId, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
30,409✔
289

290
  info.config.syncCfg = *pCfg;
30,409✔
291

292
  ret = vnodeSaveInfo(dir, &info);
30,409✔
293
  if (ret < 0) {
30,409✔
294
    vError("vgId:%d, failed to save vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
295
    return ret;
×
296
  }
297

298
  ret = vnodeCommitInfo(dir);
30,409✔
299
  if (ret < 0) {
30,409✔
300
    vError("vgId:%d, failed to commit vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
301
    return ret;
×
302
  }
303

304
  vInfo("vgId:%d, rename %s to %s", pReq->dstVgId, srcPath, dstPath);
30,409✔
305
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
30,409✔
306
  if (ret < 0) {
30,409✔
307
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", pReq->dstVgId, srcPath, dstPath,
×
308
           tstrerror(terrno));
309
    return ret;
×
310
  }
311

312
  vInfo("vgId:%d, vnode hashrange is altered", info.config.vgId);
30,409✔
313
  return 0;
30,409✔
314
}
315

316
int32_t vnodeRestoreVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
×
317
                             int32_t diskPrimary, STfs *pTfs) {
318
  SVnodeInfo info = {0};
×
319
  char       dir[TSDB_FILENAME_LEN] = {0};
×
320
  int32_t    code = 0;
×
321

322
  vnodeGetPrimaryDir(dstPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
323
  if (vnodeLoadInfo(dir, &info) == 0) {
×
324
    if (info.config.vgId != dstVgId) {
×
325
      vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
326
      return TSDB_CODE_FAILED;
×
327
    }
328
    return dstVgId;
×
329
  }
330

331
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
332
  if ((code = vnodeLoadInfo(dir, &info)) < 0) {
×
333
    vError("vgId:%d, failed to read vnode config from %s since %s", srcVgId, srcPath, tstrerror(terrno));
×
334
    return code;
×
335
  }
336

337
  if (info.config.vgId == srcVgId) {
×
338
    vInfo("vgId:%d, rollback alter hashrange", srcVgId);
×
339
    return srcVgId;
×
340
  } else if (info.config.vgId != dstVgId) {
×
341
    vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
342
    return TSDB_CODE_FAILED;
×
343
  }
344

345
  vInfo("vgId:%d, rename %s to %s", dstVgId, srcPath, dstPath);
×
346
  if (vnodeRenameVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs) < 0) {
×
347
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", dstVgId, srcPath, dstPath, tstrerror(terrno));
×
348
    return TSDB_CODE_FAILED;
×
349
  }
350

351
  return dstVgId;
×
352
}
353

354
void vnodeDestroy(int32_t vgId, const char *path, STfs *pTfs, int32_t nodeId) {
1,400,704✔
355
  vInfo("path:%s is removed while destroy vnode", path);
1,400,704✔
356
  if (tfsRmdir(pTfs, path) < 0) {
1,400,704✔
357
    vError("failed to remove path:%s since %s", path, tstrerror(terrno));
×
358
  }
359

360
#ifdef USE_SHARED_STORAGE
361
  if (nodeId > 0 && vgId > 0 && tsSsEnabled) {
1,400,704✔
362
    // we should only do this on the leader node, but it is ok to do this on all nodes
363
    char prefix[TSDB_FILENAME_LEN];
×
364
    snprintf(prefix, TSDB_FILENAME_LEN, "vnode%d/", vgId);
×
365
    int32_t code = tssDeleteFileByPrefixFromDefault(prefix);
×
366
    if (code < 0) {
×
367
      vError("vgId:%d, failed to remove vnode files from shared storage since %s", vgId, tstrerror(code));
×
368
    } else {
369
      vInfo("vgId:%d, removed vnode files from shared storage", vgId);
×
370
    }
371
  }
372
#endif
373
}
1,400,704✔
374

375
static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) {
3,668,414✔
376
  int32_t ndisk = 1;
3,668,414✔
377
  if (pTfs) {
3,668,414✔
378
    ndisk = tfsGetDisksAtLevel(pTfs, 0);
3,681,035✔
379
  }
380
  if (diskPrimary < 0 || diskPrimary >= ndisk) {
3,668,843✔
381
    vError("disk:%d is unavailable from the %d disks mounted at level 0", diskPrimary, ndisk);
×
382
    return terrno = TSDB_CODE_FS_INVLD_CFG;
×
383
  }
384
  return 0;
3,681,464✔
385
}
386

387
SVnode *vnodeOpen(const char *path, int32_t diskPrimary, STfs *pTfs, STfs *pMountTfs, SMsgCb msgCb, bool force) {
3,674,467✔
388
  SVnode    *pVnode = NULL;
3,674,467✔
389
  SVnodeInfo info = {0};
3,674,467✔
390
  char       dir[TSDB_FILENAME_LEN] = {0};
3,681,464✔
391
  char       tdir[TSDB_FILENAME_LEN * 2] = {0};
3,680,986✔
392
  int32_t    ret = 0;
3,680,986✔
393
  bool       mounted = pMountTfs != NULL;
3,680,986✔
394
  terrno = TSDB_CODE_SUCCESS;
3,680,986✔
395

396
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
3,680,826✔
397
    vError("failed to open vnode from %s since %s. diskPrimary:%d", path, terrstr(), diskPrimary);
×
398
    return NULL;
×
399
  }
400
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
3,681,464✔
401

402
  info.config = vnodeCfgDefault;
3,681,230✔
403

404
  // load vnode info
405
  vInfo("vgId:%d, start to vnode load info %s", info.config.vgId, dir);
3,681,230✔
406
  ret = vnodeLoadInfo(dir, &info);
3,683,446✔
407
  if (ret < 0) {
3,679,559✔
408
    vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
×
409
    terrno = TSDB_CODE_NEED_RETRY;
×
410
    return NULL;
×
411
  }
412

413
  if (!mounted && vnodeMkDir(pTfs, path)) {
3,679,559✔
414
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", info.config.vgId, strerror(ERRNO), path);
498✔
415
    return NULL;
×
416
  }
417
  // save vnode info on dnode ep changed
418
  bool      updated = false;
3,677,883✔
419
  SSyncCfg *pCfg = &info.config.syncCfg;
3,677,883✔
420
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
10,146,246✔
421
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
6,468,568✔
422
    if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
6,463,534✔
423
      updated = true;
×
424
    }
425
  }
426
  if (updated) {
3,676,727✔
427
    vInfo("vgId:%d, save vnode info since dnode info changed", info.config.vgId);
×
428
    if (vnodeSaveInfo(dir, &info) < 0) {
×
429
      vError("vgId:%d, failed to save vnode info since %s", info.config.vgId, tstrerror(terrno));
×
430
    }
431

432
    if (vnodeCommitInfo(dir) < 0) {
×
433
      vError("vgId:%d, failed to commit vnode info since %s", info.config.vgId, tstrerror(terrno));
×
434
    }
435
  }
436

437
  // create handle
438
  pVnode = taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
3,677,325✔
439
  if (pVnode == NULL) {
3,679,817✔
440
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
441
    vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
×
442
    return NULL;
×
443
  }
444

445
  pVnode->path = (char *)&pVnode[1];
3,679,817✔
446
  memcpy(pVnode->path, path, strlen(path) + 1);
3,681,214✔
447
  pVnode->config = info.config;
3,680,514✔
448
  pVnode->state.committed = info.state.committed;
3,680,067✔
449
  pVnode->state.commitTerm = info.state.commitTerm;
3,680,517✔
450
  pVnode->state.commitID = info.state.commitID;
3,679,817✔
451
  pVnode->state.applied = info.state.committed;
3,679,817✔
452
  pVnode->state.applyTerm = info.state.commitTerm;
3,679,817✔
453
  pVnode->pTfs = pTfs;
3,679,112✔
454
  pVnode->pMountTfs = pMountTfs;
3,679,817✔
455
  pVnode->mounted = mounted;
3,680,767✔
456
  pVnode->diskPrimary = diskPrimary;
3,679,817✔
457
  pVnode->msgCb = msgCb;
3,678,407✔
458
  (void)taosThreadMutexInit(&pVnode->lock, NULL);
3,679,112✔
459
  pVnode->blocked = false;
3,679,112✔
460
  pVnode->disableWrite = false;
3,679,112✔
461

462
  if (tsem_init(&pVnode->syncSem, 0, 0) != 0) {
3,679,112✔
463
    vError("vgId:%d, failed to init semaphore", TD_VID(pVnode));
×
464
    goto _err;
×
465
  }
466
  (void)taosThreadMutexInit(&pVnode->mutex, NULL);
3,678,407✔
467
  (void)taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
3,680,767✔
468

469
  vInfo("vgId:%d, finished vnode load info %s, vnode committed:%" PRId64, info.config.vgId, dir,
3,679,817✔
470
        pVnode->state.committed);
471

472
  int8_t rollback = vnodeShouldRollback(pVnode);
3,681,219✔
473

474
  // open buffer pool
475
  vInfo("vgId:%d, start to open vnode buffer pool", TD_VID(pVnode));
3,681,464✔
476
  if (vnodeOpenBufPool(pVnode) < 0) {
3,681,929✔
477
    vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
478
    goto _err;
×
479
  }
480

481
  // open meta
482
  (void)taosThreadRwlockInit(&pVnode->metaRWLock, NULL);
3,681,464✔
483
  vInfo("vgId:%d, start to open vnode meta", TD_VID(pVnode));
3,681,464✔
484
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
3,681,464✔
485
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
486
    goto _err;
×
487
  }
488

489
  vInfo("vgId:%d, start to upgrade meta", TD_VID(pVnode));
3,681,464✔
490
  if (!mounted && metaUpgrade(pVnode, &pVnode->pMeta) < 0) {
3,681,464✔
491
    vError("vgId:%d, failed to upgrade meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
492
  }
493

494
  // open tsdb
495
  vInfo("vgId:%d, start to open vnode tsdb", TD_VID(pVnode));
3,681,464✔
496
  if ((terrno = tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback, force)) < 0) {
3,681,464✔
497
    vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
×
498
    goto _err;
×
499
  }
500

501
  if (TSDB_CACHE_RESET(pVnode->config)) {
3,679,698✔
502
    // flag vnode tsdb cache
503
    vInfo("vgId:%d, start to flag vnode tsdb cache", TD_VID(pVnode));
×
504

505
    if (metaFlagCache(pVnode) < 0) {
×
506
      vError("vgId:%d, failed to flag tsdb cache since %s", TD_VID(pVnode), tstrerror(terrno));
×
507
      goto _err;
×
508
    }
509
  }
510

511
  // open wal
512
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
3,679,875✔
513
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
3,679,903✔
514
  TAOS_UNUSED(ret);
515

516
  vInfo("vgId:%d, start to open vnode wal", TD_VID(pVnode));
3,680,832✔
517
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
3,680,835✔
518
  if (pVnode->pWal == NULL) {
3,681,464✔
519
    vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
×
520
    goto _err;
×
521
  }
522

523
  // open tq
524
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
3,680,854✔
525
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
3,680,854✔
526
  TAOS_UNUSED(ret);
527

528
  // open query
529
  vInfo("vgId:%d, start to open vnode query", TD_VID(pVnode));
3,681,464✔
530
  if (vnodeQueryOpen(pVnode)) {
3,682,074✔
531
    vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
×
532
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
533
    goto _err;
×
534
  }
535

536
  // sma required the tq is initialized before the vnode open
537
  vInfo("vgId:%d, start to open vnode tq", TD_VID(pVnode));
3,681,464✔
538
  if (tqOpen(tdir, pVnode)) {
3,681,464✔
539
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
×
540
    goto _err;
×
541
  }
542

543
  // open blob store engine
544
  vInfo("vgId:%d, start to open blob store engine", TD_VID(pVnode));
3,680,970✔
545
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_BSE_DIR);
3,680,970✔
546

547
  SBseCfg cfg = {
7,362,539✔
548
      .vgId = pVnode->config.vgId,
3,681,464✔
549
      .keepDays = pVnode->config.tsdbCfg.days,
3,681,464✔
550
      .keeps = pVnode->config.tsdbCfg.keep0,
3,681,464✔
551
      .retention = pVnode->config.tsdbCfg.retentions[0],
552
      .precision = pVnode->config.tsdbCfg.precision,
3,681,464✔
553
  };
554
  ret = bseOpen(tdir, &cfg, &pVnode->pBse);
3,681,464✔
555
  if (ret != 0) {
3,681,464✔
556
    vError("vgId:%d, failed to open blob store engine since %s", TD_VID(pVnode), tstrerror(ret));
×
557
    terrno = ret;
×
558
    goto _err;
×
559
  }
560

561
  // vnode begin
562
  vInfo("vgId:%d, start to begin vnode", TD_VID(pVnode));
3,681,464✔
563
  if (vnodeBegin(pVnode) < 0) {
3,681,464✔
564
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
×
565
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
566
    goto _err;
×
567
  }
568

569
  // open sync
570
  vInfo("vgId:%d, start to open sync, changeVersion:%d", TD_VID(pVnode), info.config.syncCfg.changeVersion);
3,681,464✔
571
  if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
3,681,464✔
572
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
573
    goto _err;
×
574
  }
575

576
  if (rollback) {
3,681,464✔
577
    vnodeRollback(pVnode);
×
578
  }
579

580
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
3,681,464✔
581
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
3,681,464✔
582
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
3,681,464✔
583

584
  return pVnode;
3,681,464✔
585

586
_err:
×
587
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
×
588
  if (pVnode->pTq) tqClose(pVnode->pTq);
×
589
  if (pVnode->pWal) walClose(pVnode->pWal);
×
590
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
×
591
  if (pVnode->pMeta) metaClose(&pVnode->pMeta);
×
592
  if (pVnode->freeList) vnodeCloseBufPool(pVnode);
×
593

594
  (void)taosThreadRwlockDestroy(&pVnode->metaRWLock);
×
595
  taosMemoryFree(pVnode);
×
596
  return NULL;
×
597
}
598

599
void vnodePreClose(SVnode *pVnode) {
3,681,054✔
600
  streamRemoveVnodeLeader(pVnode->config.vgId);
3,681,054✔
601
  vnodeSyncPreClose(pVnode);
3,681,464✔
602
  vnodeQueryPreClose(pVnode);
3,680,783✔
603
}
3,681,464✔
604

605
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
3,681,464✔
606

607
void vnodeClose(SVnode *pVnode) {
3,681,464✔
608
  if (pVnode) {
3,681,464✔
609
    vInfo("start to close vnode");
3,681,464✔
610
    vnodeAWait(&pVnode->commitTask2);
3,681,464✔
611
    vnodeAWait(&pVnode->commitTask);
3,681,464✔
612
    vnodeSyncClose(pVnode);
3,681,464✔
613
    vnodeQueryClose(pVnode);
3,681,464✔
614
    tqClose(pVnode->pTq);
3,681,464✔
615
    walClose(pVnode->pWal);
3,681,464✔
616
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
3,681,464✔
617
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
3,681,080✔
618
    vnodeCloseBufPool(pVnode);
3,681,202✔
619

620
    if (pVnode->pBse) {
3,681,464✔
621
      bseClose(pVnode->pBse);
3,681,464✔
622
    }
623

624
    // destroy handle
625
    if (tsem_destroy(&pVnode->syncSem) != 0) {
3,680,789✔
626
      vError("vgId:%d, failed to destroy semaphore", TD_VID(pVnode));
×
627
    }
628
    (void)taosThreadCondDestroy(&pVnode->poolNotEmpty);
3,680,411✔
629
    (void)taosThreadMutexDestroy(&pVnode->mutex);
3,680,599✔
630
    (void)taosThreadMutexDestroy(&pVnode->lock);
3,681,464✔
631
    taosMemoryFree(pVnode);
3,681,127✔
632
  }
633
}
3,680,599✔
634

635
// start the sync timer after the queue is ready
636
int32_t vnodeStart(SVnode *pVnode) {
3,681,464✔
637
  if (pVnode == NULL) {
3,681,464✔
638
    return TSDB_CODE_INVALID_PARA;
×
639
  }
640
  return vnodeSyncStart(pVnode);
3,681,464✔
641
}
642

643
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
1,936,585✔
644

645
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
1,936,585✔
646

647
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
91,641✔
648
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
123,625✔
649

650
int32_t vnodeSetWalKeepVersion(SVnode *pVnode, int64_t keepVersion) {
3,126✔
651
  if (pVnode == NULL || pVnode->pWal == NULL) {
3,126✔
652
    return TSDB_CODE_INVALID_PARA;
×
653
  }
654
  return walSetKeepVersion(pVnode->pWal, keepVersion);
3,126✔
655
}
656

657
void vnodeStop(SVnode *pVnode) {}
×
658

659
int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; }
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc