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

taosdata / TDengine / #5139

05 Nov 2026 03:32AM UTC coverage: 74.033% (+0.09%) from 73.942%
#5139

push

travis-ci

jbjia
fix: capture coverage once from per-thread gcda

Per-case lcov walked the shared build tree and kept each case busy for minutes. Pass GCOV_PREFIX into the test container so each thread writes its own counters, then merge and capture after the suite.

373718 of 504798 relevant lines covered (74.03%)

4155647.08 hits per line

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

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

18
#include "sync.h"
19
#include "tss.h"
20
#include "tq.h"
21
#include "tsdb.h"
22
#include "vnd.h"
23

24
void vnodeGetPrimaryDir(const char *relPath, int32_t diskPrimary, STfs *pTfs, char *buf, size_t bufLen) {
246,212 ✔
25
  if (pTfs) {
246,212 ✔
26
    SDiskID diskId = {0};
246,151 ✔
27
    diskId.id = diskPrimary;
246,151 ✔
28
    snprintf(buf, bufLen - 1, "%s%s%s", tfsGetDiskPath(pTfs, diskId), TD_DIRSEP, relPath);
246,151 ✔
29
  } else {
30
    snprintf(buf, bufLen - 1, "%s", relPath);
99 ✔
31
  }
32
  buf[bufLen - 1] = '\0';
246,357 ✔
33
}
246,357 ✔
34

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

47
  } else {
48
    vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, buf, bufLen);
219,455 ✔
49
  }
50
}
219,725 ✔
51

52
static int32_t vnodeMkDir(STfs *pTfs, const char *path) {
25,171 ✔
53
  if (pTfs) {
25,171 ✔
54
    return tfsMkdirRecur(pTfs, path);
25,183 ✔
55
  } else {
56
    return taosMkDir(path);
×
57
  }
58
}
59

60
int32_t vnodeCreate(const char *path, SVnodeCfg *pCfg, int32_t diskPrimary, STfs *pTfs) {
11,282 ✔
61
  int32_t    code = 0;
11,282 ✔
62
  SVnodeInfo info = {0};
11,282 ✔
63
  char       dir[TSDB_FILENAME_LEN] = {0};
11,288 ✔
64

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

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

78
  if (pCfg) {
11,289 ✔
79
    info.config = *pCfg;
11,289 ✔
80
  } else {
81
    info.config = vnodeCfgDefault;
×
82
  }
83
  info.state.committed = -1;
11,289 ✔
84
  info.state.applied = -1;
11,289 ✔
85
  info.state.commitID = 0;
11,289 ✔
86

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

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

109
  vInfo("vgId:%d, vnode is created", info.config.vgId);
11,284 ✔
110
  return 0;
11,189 ✔
111
}
112

113
bool vnodeShouldRemoveWal(SVnode *pVnode) { return pVnode->config.walCfg.clearFiles == 1; }
1,448 ✔
114

115
int32_t vnodeAlterReplica(const char *path, SAlterVnodeReplicaReq *pReq, int32_t diskPrimary, STfs *pTfs) {
1,450 ✔
116
  SVnodeInfo info = {0};
1,450 ✔
117
  char       dir[TSDB_FILENAME_LEN] = {0};
1,450 ✔
118
  int32_t    ret = 0;
1,450 ✔
119

120
  // [TD-SEC-2026-034] the loops below use replica/learnerReplica as bounds on the fixed-size
121
  // syncCfg.nodeInfo[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA]. Callers in mgmt_vnode already reject
122
  // out-of-range counters, this is the last line of defense against overflowing nodeInfo.
123
  if (pReq->replica < 1 || pReq->replica > TSDB_MAX_REPLICA || pReq->learnerReplica < 0 ||
1,450 ✔
124
      pReq->learnerReplica > TSDB_MAX_LEARNER_REPLICA) {
1,448 ✔
125
    vError("vgId:%d, failed to alter replica since invalid replica:%d learnerReplica:%d", pReq->vgId, pReq->replica,
2 ✔
126
           pReq->learnerReplica);
127
    terrno = TSDB_CODE_INVALID_MSG;
2 ✔
128
    return -1;
2 ✔
129
  }
130

131
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
1,448 ✔
132

133
  ret = vnodeLoadInfo(dir, &info);
1,448 ✔
134
  if (ret < 0) {
1,448 ✔
135
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->vgId, path, tstrerror(terrno));
×
136
    return ret;
×
137
  }
138

139
  SSyncCfg *pCfg = &info.config.syncCfg;
1,448 ✔
140

141
  pCfg->replicaNum = 0;
1,448 ✔
142
  pCfg->totalReplicaNum = 0;
1,448 ✔
143
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
1,448 ✔
144

145
  for (int i = 0; i < pReq->replica; ++i) {
5,562 ✔
146
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
4,114 ✔
147
    pNode->nodeId = pReq->replicas[i].id;
4,114 ✔
148
    pNode->nodePort = pReq->replicas[i].port;
4,114 ✔
149
    tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
4,114 ✔
150
    pNode->nodeRole = TAOS_SYNC_ROLE_VOTER;
4,114 ✔
151
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
4,114 ✔
152
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
4,114 ✔
153
    pCfg->replicaNum++;
4,114 ✔
154
  }
155
  if (pReq->selfIndex != -1) {
1,448 ✔
156
    pCfg->myIndex = pReq->selfIndex;
1,448 ✔
157
  }
158
  for (int i = pCfg->replicaNum; i < pReq->replica + pReq->learnerReplica; ++i) {
1,800 ✔
159
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
352 ✔
160
    pNode->nodeId = pReq->learnerReplicas[pCfg->totalReplicaNum].id;
352 ✔
161
    pNode->nodePort = pReq->learnerReplicas[pCfg->totalReplicaNum].port;
352 ✔
162
    pNode->nodeRole = TAOS_SYNC_ROLE_LEARNER;
352 ✔
163
    tstrncpy(pNode->nodeFqdn, pReq->learnerReplicas[pCfg->totalReplicaNum].fqdn, sizeof(pNode->nodeFqdn));
352 ✔
164
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
352 ✔
165
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
352 ✔
166
    pCfg->totalReplicaNum++;
352 ✔
167
  }
168
  pCfg->totalReplicaNum += pReq->replica;
1,448 ✔
169
  if (pReq->learnerSelfIndex != -1) {
1,448 ✔
170
    pCfg->myIndex = pReq->replica + pReq->learnerSelfIndex;
×
171
  }
172
  pCfg->changeVersion = pReq->changeVersion;
1,448 ✔
173

174
  if (info.config.walCfg.clearFiles) {
1,448 ✔
175
    info.config.walCfg.clearFiles = 0;
×
176

177
    vInfo("vgId:%d, reset wal clearFiles", pReq->vgId);
×
178
  }
179

180
  vInfo("vgId:%d, save config while alter, replicas:%d totalReplicas:%d selfIndex:%d changeVersion:%d", pReq->vgId,
1,448 ✔
181
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
182

183
  info.config.syncCfg = *pCfg;
1,448 ✔
184
  ret = vnodeSaveInfo(dir, &info);
1,448 ✔
185
  if (ret < 0) {
1,448 ✔
186
    vError("vgId:%d, failed to save vnode config since %s", pReq->vgId, tstrerror(terrno));
×
187
    return ret;
×
188
  }
189

190
  ret = vnodeCommitInfo(dir);
1,448 ✔
191
  if (ret < 0) {
1,448 ✔
192
    vError("vgId:%d, failed to commit vnode config since %s", pReq->vgId, tstrerror(terrno));
×
193
    return ret;
×
194
  }
195

196
  vInfo("vgId:%d, vnode config is saved", info.config.vgId);
1,448 ✔
197
  return 0;
1,448 ✔
198
}
199

200
static int32_t vnodeVgroupIdLen(int32_t vgId) {
86 ✔
201
  char tmp[TSDB_FILENAME_LEN];
86 ✔
202
  (void)snprintf(tmp, TSDB_FILENAME_LEN, "%d", vgId);
86 ✔
203
  return strlen(tmp);
86 ✔
204
}
205

206
int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
34 ✔
207
                            int32_t diskPrimary, STfs *pTfs) {
208
  int32_t ret = 0;
34 ✔
209

210
  char oldRname[TSDB_FILENAME_LEN] = {0};
34 ✔
211
  char newRname[TSDB_FILENAME_LEN] = {0};
34 ✔
212
  char tsdbPath[TSDB_FILENAME_LEN] = {0};
34 ✔
213
  char tsdbFilePrefix[TSDB_FILENAME_LEN] = {0};
34 ✔
214
  snprintf(tsdbPath, TSDB_FILENAME_LEN, "%s%stsdb", srcPath, TD_DIRSEP);
34 ✔
215
  snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP);
34 ✔
216
  int32_t prefixLen = strlen(tsdbFilePrefix);
34 ✔
217

218
  STfsDir *tsdbDir = NULL;
34 ✔
219
  int32_t  tret = tfsOpendir(pTfs, tsdbPath, &tsdbDir);
34 ✔
220
  if (tsdbDir == NULL) {
34 ✔
221
    return 0;
×
222
  }
223

224
  while (1) {
154 ✔
225
    const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
188 ✔
226
    if (tsdbFile == NULL) break;
188 ✔
227
    if (tsdbFile->rname[0] == '\0') continue;
154 ✔
228
    tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
154 ✔
229

230
    char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
154 ✔
231
    if (tsdbFilePrefixPos == NULL) continue;
154 ✔
232

233
    int32_t tsdbFileVgId = 0;
86 ✔
234
    ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
86 ✔
235
    if (ret != 0) {
86 ✔
236
      vError("vgId:%d, failed to get tsdb file vgid since %s", dstVgId, tstrerror(ret));
×
237
      tfsClosedir(tsdbDir);
×
238
      return ret;
×
239
    }
240

241
    if (tsdbFileVgId == srcVgId) {
86 ✔
242
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
86 ✔
243

244
      tsdbFilePrefixPos[prefixLen] = 0;
86 ✔
245
      snprintf(newRname, TSDB_FILENAME_LEN, "%s%d%s", oldRname, dstVgId, tsdbFileSurfixPos);
86 ✔
246
      vInfo("vgId:%d, rename file from %s to %s", dstVgId, tsdbFile->rname, newRname);
86 ✔
247

248
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
86 ✔
249
      if (ret != 0) {
86 ✔
250
        vError("vgId:%d, failed to rename file from %s to %s since %s", dstVgId, tsdbFile->rname, newRname, terrstr());
×
251
        tfsClosedir(tsdbDir);
×
252
        return ret;
×
253
      }
254
    }
255
  }
256

257
  tfsClosedir(tsdbDir);
34 ✔
258

259
  vInfo("vgId:%d, rename dir from %s to %s", dstVgId, srcPath, dstPath);
34 ✔
260
  ret = tfsRename(pTfs, diskPrimary, srcPath, dstPath);
34 ✔
261
  if (ret != 0) {
34 ✔
262
    vError("vgId:%d, failed to rename dir from %s to %s since %s", dstVgId, srcPath, dstPath, terrstr());
×
263
  }
264
  return ret;
34 ✔
265
}
266

267
int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnodeHashRangeReq *pReq,
34 ✔
268
                            int32_t diskPrimary, STfs *pTfs) {
269
  SVnodeInfo info = {0};
34 ✔
270
  char       dir[TSDB_FILENAME_LEN] = {0};
34 ✔
271
  int32_t    ret = 0;
34 ✔
272

273
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
34 ✔
274

275
  ret = vnodeLoadInfo(dir, &info);
34 ✔
276
  if (ret < 0) {
34 ✔
277
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->srcVgId, srcPath, tstrerror(terrno));
×
278
    return ret;
×
279
  }
280

281
  vInfo("vgId:%d, alter hashrange from [%u, %u] to [%u, %u]", pReq->srcVgId, info.config.hashBegin, info.config.hashEnd,
34 ✔
282
        pReq->hashBegin, pReq->hashEnd);
283
  info.config.vgId = pReq->dstVgId;
34 ✔
284
  info.config.hashBegin = pReq->hashBegin;
34 ✔
285
  info.config.hashEnd = pReq->hashEnd;
34 ✔
286
  info.config.hashChange = true;
34 ✔
287
  info.config.walCfg.vgId = pReq->dstVgId;
34 ✔
288
  info.config.syncCfg.changeVersion = pReq->changeVersion;
34 ✔
289

290
  SSyncCfg *pCfg = &info.config.syncCfg;
34 ✔
291
  pCfg->myIndex = 0;
34 ✔
292
  pCfg->replicaNum = 1;
34 ✔
293
  pCfg->totalReplicaNum = 1;
34 ✔
294
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
34 ✔
295

296
  vInfo("vgId:%d, alter vnode replicas to 1", pReq->srcVgId);
34 ✔
297
  SNodeInfo *pNode = &pCfg->nodeInfo[0];
34 ✔
298
  pNode->nodePort = tsServerPort;
34 ✔
299
  tstrncpy(pNode->nodeFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
34 ✔
300
  bool ret1 = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
34 ✔
301
  vInfo("vgId:%d, ep:%s:%u dnode:%d", pReq->srcVgId, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
34 ✔
302

303
  info.config.syncCfg = *pCfg;
34 ✔
304

305
  ret = vnodeSaveInfo(dir, &info);
34 ✔
306
  if (ret < 0) {
34 ✔
307
    vError("vgId:%d, failed to save vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
308
    return ret;
×
309
  }
310

311
  ret = vnodeCommitInfo(dir);
34 ✔
312
  if (ret < 0) {
34 ✔
313
    vError("vgId:%d, failed to commit vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
314
    return ret;
×
315
  }
316

317
  vInfo("vgId:%d, rename %s to %s", pReq->dstVgId, srcPath, dstPath);
34 ✔
318
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
34 ✔
319
  if (ret < 0) {
34 ✔
320
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", pReq->dstVgId, srcPath, dstPath,
×
321
           tstrerror(terrno));
322
    return ret;
×
323
  }
324

325
  vInfo("vgId:%d, vnode hashrange is altered", info.config.vgId);
34 ✔
326
  return 0;
34 ✔
327
}
328

329
int32_t vnodeRestoreVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
×
330
                             int32_t diskPrimary, STfs *pTfs) {
331
  SVnodeInfo info = {0};
×
332
  char       dir[TSDB_FILENAME_LEN] = {0};
×
333
  int32_t    code = 0;
×
334

335
  vnodeGetPrimaryDir(dstPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
336
  if (vnodeLoadInfo(dir, &info) == 0) {
×
337
    if (info.config.vgId != dstVgId) {
×
338
      vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
339
      return TSDB_CODE_FAILED;
×
340
    }
341
    return dstVgId;
×
342
  }
343

344
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
345
  if ((code = vnodeLoadInfo(dir, &info)) < 0) {
×
346
    vError("vgId:%d, failed to read vnode config from %s since %s", srcVgId, srcPath, tstrerror(terrno));
×
347
    return code;
×
348
  }
349

350
  if (info.config.vgId == srcVgId) {
×
351
    vInfo("vgId:%d, rollback alter hashrange", srcVgId);
×
352
    return srcVgId;
×
353
  } else if (info.config.vgId != dstVgId) {
×
354
    vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
355
    return TSDB_CODE_FAILED;
×
356
  }
357

358
  vInfo("vgId:%d, rename %s to %s", dstVgId, srcPath, dstPath);
×
359
  if (vnodeRenameVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs) < 0) {
×
360
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", dstVgId, srcPath, dstPath, tstrerror(terrno));
×
361
    return TSDB_CODE_FAILED;
×
362
  }
363

364
  return dstVgId;
×
365
}
366

367
void vnodeDestroy(int32_t vgId, const char *path, STfs *pTfs, int32_t nodeId) {
6,167 ✔
368
  vInfo("path:%s is removed while destroy vnode", path);
6,167 ✔
369
  if (tfsRmdir(pTfs, path) < 0) {
6,167 ✔
370
    vError("failed to remove path:%s since %s", path, tstrerror(terrno));
×
371
  }
372

373
#ifdef USE_SHARED_STORAGE
374
  if (nodeId > 0 && vgId > 0 && tsSsEnabled) {
6,167 ✔
375
    // we should only do this on the leader node, but it is ok to do this on all nodes
376
    char prefix[TSDB_FILENAME_LEN];
58 ✔
377
    snprintf(prefix, TSDB_FILENAME_LEN, "vnode%d/", vgId);
58 ✔
378
    int32_t code = tssDeleteFileByPrefixFromDefault(prefix);
58 ✔
379
    if (code < 0) {
58 ✔
380
      vError("vgId:%d, failed to remove vnode files from shared storage since %s", vgId, tstrerror(code));
57 ✔
381
    } else {
382
      vInfo("vgId:%d, removed vnode files from shared storage", vgId);
1 ✔
383
    }
384
  }
385
#endif
386
}
6,167 ✔
387

388
static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) {
13,939 ✔
389
  int32_t ndisk = 1;
13,939 ✔
390
  if (pTfs) {
13,939 ✔
391
    ndisk = tfsGetDisksAtLevel(pTfs, 0);
13,940 ✔
392
  }
393
  if (diskPrimary < 0 || diskPrimary >= ndisk) {
13,921 ✔
394
    vError("disk:%d is unavailable from the %d disks mounted at level 0", diskPrimary, ndisk);
×
395
    return terrno = TSDB_CODE_FS_INVLD_CFG;
×
396
  }
397
  return 0;
13,810 ✔
398
}
399

400
SVnode *vnodeOpen(const char *path, int32_t diskPrimary, STfs *pTfs, STfs *pMountTfs, SMsgCb msgCb, bool force) {
13,868 ✔
401
  SVnode    *pVnode = NULL;
13,868 ✔
402
  SVnodeInfo info = {0};
13,868 ✔
403
  char       dir[TSDB_FILENAME_LEN] = {0};
13,947 ✔
404
  char       tdir[TSDB_FILENAME_LEN * 2] = {0};
13,948 ✔
405
  int32_t    ret = 0;
13,948 ✔
406
  bool       mounted = pMountTfs != NULL;
13,948 ✔
407
  terrno = TSDB_CODE_SUCCESS;
13,948 ✔
408

409
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
13,947 ✔
410
    vError("failed to open vnode from %s since %s. diskPrimary:%d", path, terrstr(), diskPrimary);
×
411
    return NULL;
×
412
  }
413
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
13,916 ✔
414

415
  info.config = vnodeCfgDefault;
13,934 ✔
416

417
  // load vnode info
418
  vInfo("vgId:%d, start to vnode load info %s", info.config.vgId, dir);
13,934 ✔
419
  ret = vnodeLoadInfo(dir, &info);
13,980 ✔
420
  if (ret < 0) {
13,919 ✔
421
    vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
×
422
    terrno = TSDB_CODE_NEED_RETRY;
×
423
    return NULL;
×
424
  }
425

426
  if (!mounted && vnodeMkDir(pTfs, path)) {
13,919 ✔
427
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", info.config.vgId, strerror(ERRNO), path);
69 ✔
428
    return NULL;
×
429
  }
430
  // save vnode info on dnode ep changed
431
  bool      updated = false;
13,752 ✔
432
  SSyncCfg *pCfg = &info.config.syncCfg;
13,752 ✔
433
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
34,830 ✔
434
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
20,952 ✔
435
    if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
20,952 ✔
436
      updated = true;
×
437
    }
438
  }
439
  if (updated) {
13,878 ✔
440
    vInfo("vgId:%d, save vnode info since dnode info changed", info.config.vgId);
×
441
    if (vnodeSaveInfo(dir, &info) < 0) {
×
442
      vError("vgId:%d, failed to save vnode info since %s", info.config.vgId, tstrerror(terrno));
×
443
    }
444

445
    if (vnodeCommitInfo(dir) < 0) {
×
446
      vError("vgId:%d, failed to commit vnode info since %s", info.config.vgId, tstrerror(terrno));
×
447
    }
448
  }
449

450
  // create handle
451
  pVnode = taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
13,878 ✔
452
  if (pVnode == NULL) {
13,941 ✔
453
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
454
    vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
×
455
    return NULL;
×
456
  }
457

458
  pVnode->path = (char *)&pVnode[1];
13,941 ✔
459
  memcpy(pVnode->path, path, strlen(path) + 1);
13,941 ✔
460
  pVnode->config = info.config;
13,941 ✔
461
  pVnode->state.committed = info.state.committed;
13,941 ✔
462
  pVnode->state.commitTerm = info.state.commitTerm;
13,941 ✔
463
  pVnode->state.commitID = info.state.commitID;
13,941 ✔
464
  pVnode->state.applied = info.state.committed;
13,941 ✔
465
  pVnode->state.applyTerm = info.state.commitTerm;
13,941 ✔
466
  pVnode->pTfs = pTfs;
13,941 ✔
467
  pVnode->pMountTfs = pMountTfs;
13,941 ✔
468
  pVnode->mounted = mounted;
13,941 ✔
469
  pVnode->diskPrimary = diskPrimary;
13,941 ✔
470
  pVnode->msgCb = msgCb;
13,941 ✔
471
  (void)taosThreadMutexInit(&pVnode->lock, NULL);
13,941 ✔
472
  pVnode->blocked = false;
13,936 ✔
473
  pVnode->disableWrite = false;
13,936 ✔
474
  pVnode->closing = 0;
13,936 ✔
475
  pVnode->vacuumRunning = 0;
13,936 ✔
476
  (void)taosThreadMutexInit(&pVnode->backupSnapMutex, NULL);
13,936 ✔
477

478
  if (tsem_init(&pVnode->syncSem, 0, 0) != 0) {
13,953 ✔
479
    vError("vgId:%d, failed to init semaphore", TD_VID(pVnode));
×
480
    goto _err;
×
481
  }
482
  (void)taosThreadMutexInit(&pVnode->mutex, NULL);
13,932 ✔
483
  (void)taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
13,931 ✔
484

485
  vInfo("vgId:%d, finished vnode load info %s, vnode committed:%" PRId64, info.config.vgId, dir,
13,937 ✔
486
        pVnode->state.committed);
487

488
  int8_t rollback = vnodeShouldRollback(pVnode);
13,953 ✔
489

490
  // open buffer pool
491
  vInfo("vgId:%d, start to open vnode buffer pool", TD_VID(pVnode));
13,941 ✔
492
  if (vnodeOpenBufPool(pVnode) < 0) {
13,950 ✔
493
    vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
494
    goto _err;
×
495
  }
496

497
  // open meta
498
  (void)taosThreadRwlockInit(&pVnode->metaRWLock, NULL);
13,956 ✔
499
  vInfo("vgId:%d, start to open vnode meta", TD_VID(pVnode));
13,956 ✔
500
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
13,955 ✔
501
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
1 ✔
502
    goto _err;
1 ✔
503
  }
504

505
  vInfo("vgId:%d, start to upgrade meta", TD_VID(pVnode));
13,935 ✔
506
  if (!mounted && metaUpgrade(pVnode, &pVnode->pMeta) < 0) {
13,955 ✔
507
    vError("vgId:%d, failed to upgrade meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
508
  }
509

510
  if (pVnode->config.rebuildVTableStats) {
13,939 ✔
511
    vInfo("vgId:%d, rebuild virtual table stats since vnode config predates persisted vtable counters",
×
512
          TD_VID(pVnode));
513
    if (metaRebuildVTableStats(pVnode->pMeta) == 0) {
×
514
      pVnode->config.rebuildVTableStats = false;
×
515
      if (!mounted) {
×
516
        info.config.rebuildVTableStats = false;
×
517
        info.config.vndStats.numOfVTables = pVnode->config.vndStats.numOfVTables;
×
518
        info.config.vndStats.numOfVCTables = pVnode->config.vndStats.numOfVCTables;
×
519
        if (vnodeSaveInfo(dir, &info) < 0 || vnodeCommitInfo(dir) < 0) {
×
520
          vError("vgId:%d, failed to save vnode info after virtual table stats rebuild since %s", TD_VID(pVnode),
×
521
                 tstrerror(terrno));
522
        }
523
      }
524
    } else {
525
      vError("vgId:%d, failed to rebuild virtual table stats since %s", TD_VID(pVnode), tstrerror(terrno));
×
526
    }
527
  }
528

529
  // init txn manager and rebuild in-memory txn state from B+ tree
530
  vInfo("vgId:%d, start to init vnode txn manager", TD_VID(pVnode));
13,935 ✔
531
  if (vnodeTxnInit(pVnode) < 0) {
13,955 ✔
532
    vError("vgId:%d, failed to init txn manager since %s", TD_VID(pVnode), tstrerror(terrno));
×
533
    goto _err;
×
534
  }
535
  if (vnodeTxnRebuildFromMeta(pVnode) < 0) {
13,955 ✔
536
    vError("vgId:%d, failed to rebuild txn state from meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
537
    goto _err;
×
538
  }
539

540
  // open tsdb
541
  vInfo("vgId:%d, start to open vnode tsdb", TD_VID(pVnode));
13,955 ✔
542
  if ((terrno = tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback, force)) < 0) {
13,956 ✔
543
    vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
10 ✔
544
    goto _err;
10 ✔
545
  }
546

547
  if (TSDB_CACHE_RESET(pVnode->config)) {
13,915 ✔
548
    // flag vnode tsdb cache
549
    vInfo("vgId:%d, start to flag vnode tsdb cache", TD_VID(pVnode));
×
550

551
    if (metaFlagCache(pVnode) < 0) {
×
552
      vError("vgId:%d, failed to flag tsdb cache since %s", TD_VID(pVnode), tstrerror(terrno));
×
553
      goto _err;
×
554
    }
555
  }
556

557
  // open wal
558
  (void)snprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
13,915 ✔
559
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
13,915 ✔
560
  TAOS_UNUSED(ret);
113 ✔
561

562
  vInfo("vgId:%d, start to open vnode wal", TD_VID(pVnode));
13,904 ✔
563
  pVnode->config.walCfg.enableTxnFile = 1;  // vnode WAL: enable .txn files for CDC lazy load
13,919 ✔
564
  // Bind the dnode-shared tfs handle *before* walOpen() so its one-time repair pass
565
  // (walCheckAndRepairMeta/walCheckAndRepairIdx) already knows about every mount point a
566
  // historical segment might live on -- binding after walOpen() returns is too late: that
567
  // pass would only look under tdir (the primary mount) and drop every segment it can't
568
  // find there, permanently losing WAL data on the next walSaveMeta(). No-op when
569
  // pVnode->pTfs is NULL (single mount point).
570
  // relDir must be the wal directory's path *relative to a mount point root*, i.e.
571
  // "<path>/wal" (e.g. "vnode/vnode2/wal") -- NOT just VNODE_WAL_DIR ("wal") on its own,
572
  // otherwise every vnode on this dnode would collide on the same "<mount>/wal/" directory
573
  // and reuse the same version-numbered segment file names.
574
  pVnode->config.walCfg.pTfs = pVnode->pTfs;
13,919 ✔
575
  if (pVnode->pTfs != NULL) {
13,919 ✔
576
    (void)snprintf(pVnode->config.walCfg.relDir, sizeof(pVnode->config.walCfg.relDir), "%s%s%s", path, TD_DIRSEP,
13,944 ✔
577
                   VNODE_WAL_DIR);
578
  }
579
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
13,919 ✔
580
  if (pVnode->pWal == NULL) {
13,937 ✔
581
    vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
×
582
    goto _err;
×
583
  }
584

585
  // open tq
586
  (void)snprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
13,937 ✔
587
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
13,937 ✔
588
  TAOS_UNUSED(ret);
114 ✔
589

590
  // open query
591
  if (pVnode->msgCb.mgmt != NULL) {
13,923 ✔
592
    vInfo("vgId:%d, start to open vnode query", TD_VID(pVnode));
13,864 ✔
593
    if (vnodeQueryOpen(pVnode)) {
13,866 ✔
594
      vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
×
595
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
596
      goto _err;
×
597
    }
598
  }
599

600
  // sma required the tq is initialized before the vnode open
601
  vInfo("vgId:%d, start to open vnode tq", TD_VID(pVnode));
13,925 ✔
602
  if (tqOpen(tdir, pVnode)) {
13,927 ✔
603
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
×
604
    goto _err;
×
605
  }
606

607
  // open txn WAL manager (CDC txn-atomic cache)
608
  vInfo("vgId:%d, start to open txn WAL manager", TD_VID(pVnode));
13,901 ✔
609
  pVnode->pTxnWalMgr = txnMgrOpen(pVnode->pWal, pVnode, info.lastTxnConsumeTs);
13,950 ✔
610
  if (pVnode->pTxnWalMgr == NULL) {
13,945 ✔
611
    vError("vgId:%d, failed to open txn WAL manager since %s", TD_VID(pVnode), tstrerror(terrno));
×
612
    goto _err;
×
613
  }
614
  // Rebuild cache from WAL for any txns that were in-flight before restart.
615
  {
616
    int64_t minTxnVer = walGetFirstVer(pVnode->pWal);
13,945 ✔
617
    int64_t appliedVer = pVnode->state.applied;
13,945 ✔
618
    if (minTxnVer > 0 && minTxnVer <= appliedVer) {
13,945 ✔
619
      (void)txnMgrReloadFromWal(pVnode->pTxnWalMgr, pVnode->pWal, minTxnVer, appliedVer);
73 ✔
620
    }
621
  }
622
  // Set initial WAL keep-version based on slots loaded from WAL restart scan.
623
  txnMgrRefreshWalKeepVersion(pVnode->pTxnWalMgr, pVnode->pWal, pVnode);
13,945 ✔
624

625
  // open blob store engine
626
  vInfo("vgId:%d, start to open blob store engine", TD_VID(pVnode));
13,944 ✔
627
  (void)snprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_BSE_DIR);
13,944 ✔
628

629
  SBseCfg cfg = {
13,944 ✔
630
      .vgId = pVnode->config.vgId,
13,944 ✔
631
      .keepDays = pVnode->config.tsdbCfg.days,
13,944 ✔
632
      .keeps = pVnode->config.tsdbCfg.keep0,
13,944 ✔
633
      .retention = pVnode->config.tsdbCfg.retentions[0],
634
      .precision = pVnode->config.tsdbCfg.precision,
13,944 ✔
635
  };
636
  tstrncpy(cfg.encryptAlgrName, pVnode->config.tdbEncryptData.encryptAlgrName, TSDB_ENCRYPT_ALGR_NAME_LEN);
13,944 ✔
637
  tstrncpy(cfg.encryptKey, tsDataKey, ENCRYPT_KEY_LEN + 1);
13,944 ✔
638

639
  ret = bseOpen(tdir, &cfg, &pVnode->pBse);
13,944 ✔
640
  if (ret != 0) {
13,944 ✔
641
    vError("vgId:%d, failed to open blob store engine since %s", TD_VID(pVnode), tstrerror(ret));
×
642
    terrno = ret;
×
643
    goto _err;
×
644
  }
645

646
  // vnode begin
647
  vInfo("vgId:%d, start to begin vnode", TD_VID(pVnode));
13,944 ✔
648
  if (vnodeBegin(pVnode) < 0) {
13,944 ✔
649
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
×
650
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
651
    goto _err;
×
652
  }
653

654
  // open sync
655
  if (pVnode->msgCb.mgmt != NULL) {
13,930 ✔
656
    vInfo("vgId:%d, start to open sync, changeVersion:%d", TD_VID(pVnode), info.config.syncCfg.changeVersion);
13,855 ✔
657
    if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
13,859 ✔
658
      vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
659
      goto _err;
×
660
    }
661
  }
662

663
  if (rollback) {
13,940 ✔
664
    vnodeRollback(pVnode);
×
665
  }
666

667
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
13,945 ✔
668
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
13,945 ✔
669
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
13,945 ✔
670

671
  return pVnode;
13,945 ✔
672

673
_err:
11 ✔
674
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
11 ✔
675
  if (pVnode->pTq) tqClose(pVnode->pTq);
11 ✔
676
  if (pVnode->pTxnWalMgr) {
11 ✔
677
    txnMgrClose(pVnode->pTxnWalMgr);
×
678
    pVnode->pTxnWalMgr = NULL;
×
679
  }
680
  if (pVnode->pWal) walClose(pVnode->pWal);
11 ✔
681
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
11 ✔
682
  vnodeTxnCleanup(pVnode);
11 ✔
683
  if (pVnode->pMeta) metaClose(&pVnode->pMeta);
11 ✔
684
  if (pVnode->pBse) {
11 ✔
685
    bseClose(pVnode->pBse);
×
686
    pVnode->pBse = NULL;
×
687
  }
688
  if (pVnode->freeList) vnodeCloseBufPool(pVnode);
11 ✔
689

690
  (void)taosThreadRwlockDestroy(&pVnode->metaRWLock);
11 ✔
691
  taosMemoryFree(pVnode);
11 ✔
692
  return NULL;
11 ✔
693
}
694

695
void vnodePreClose(SVnode *pVnode) {
13,866 ✔
696
  streamRemoveVnodeLeader(pVnode->config.vgId);
13,866 ✔
697
  vnodeCloseBackupSnap(pVnode);
13,866 ✔
698
  vnodeSyncPreClose(pVnode);
13,864 ✔
699
  vnodeQueryPreClose(pVnode);
13,866 ✔
700
}
13,865 ✔
701

702
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
13,866 ✔
703

704
void vnodeClose(SVnode *pVnode) {
13,955 ✔
705
  if (pVnode) {
13,955 ✔
706
    vInfo("start to close vnode");
13,945 ✔
707
    atomic_store_8(&pVnode->closing, 1);
13,945 ✔
708
    vnodeAWait(&pVnode->commitTask2);
13,945 ✔
709
    vnodeAWait(&pVnode->commitTask);
13,945 ✔
710

711
    // Vacuum may submit follow-up vacuum tasks (chained submission inside
712
    // vnodeTxnVacuumExecute when more entries remain). Loop until vacuumRunning
713
    // settles to 0 with no in-flight task. Bounded by the fact that
714
    // vnodeTxnSubmitVacuumAsync rejects new submissions once closing==1, so
715
    // at most one extra task can be queued past the closing barrier.
716
    vnodeAWait(&pVnode->vacuumTask);
13,945 ✔
717
    if (atomic_load_8(&pVnode->vacuumRunning)) {
13,945 ✔
718
      vnodeAWait(&pVnode->vacuumTask);
×
719
    }
720
    if (pVnode->sync != 0) {
13,945 ✔
721
      vnodeSyncClose(pVnode);
13,866 ✔
722
    }
723
    vnodeQueryClose(pVnode);
13,945 ✔
724
    tqClose(pVnode->pTq);
13,945 ✔
725
    if (pVnode->pTxnWalMgr) {
13,944 ✔
726
      txnMgrClose(pVnode->pTxnWalMgr);
13,945 ✔
727
      pVnode->pTxnWalMgr = NULL;
13,944 ✔
728
    }
729
    walClose(pVnode->pWal);
13,943 ✔
730
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
13,943 ✔
731
    vnodeTxnCleanup(pVnode);
13,935 ✔
732
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
13,945 ✔
733
    vnodeCloseBufPool(pVnode);
13,943 ✔
734

735
    if (pVnode->pBse) {
13,945 ✔
736
      bseClose(pVnode->pBse);
13,945 ✔
737
    }
738

739
    // destroy handle
740
    if (tsem_destroy(&pVnode->syncSem) != 0) {
13,943 ✔
741
      vError("vgId:%d, failed to destroy semaphore", TD_VID(pVnode));
×
742
    }
743
    (void)taosThreadCondDestroy(&pVnode->poolNotEmpty);
13,943 ✔
744
    (void)taosThreadMutexDestroy(&pVnode->backupSnapMutex);
13,945 ✔
745
    (void)taosThreadMutexDestroy(&pVnode->mutex);
13,945 ✔
746
    (void)taosThreadMutexDestroy(&pVnode->lock);
13,945 ✔
747
    taosMemoryFree(pVnode);
13,945 ✔
748
  }
749
}
13,953 ✔
750

751
// start the sync timer after the queue is ready
752
int32_t vnodeStart(SVnode *pVnode) {
13,786 ✔
753
  if (pVnode == NULL) {
13,786 ✔
754
    return TSDB_CODE_INVALID_PARA;
×
755
  }
756
  return vnodeSyncStart(pVnode);
13,786 ✔
757
}
758

759
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
3,657 ✔
760

761
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
3,657 ✔
762

763
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
257 ✔
764
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
304 ✔
765

766
int32_t vnodeSetWalKeepVersion(SVnode *pVnode, int64_t keepVersion) {
3 ✔
767
  if (pVnode == NULL || pVnode->pWal == NULL) {
3 ✔
768
    return TSDB_CODE_INVALID_PARA;
×
769
  }
770
  return walSetKeepVersion(pVnode->pWal, keepVersion);
3 ✔
771
}
772

773
void vnodeStop(SVnode *pVnode) {}
×
774

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

© 2026 Coveralls, Inc