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

taosdata / TDengine / #4541

19 Jul 2025 01:13AM UTC coverage: 56.753% (-1.6%) from 58.31%
#4541

push

travis-ci

web-flow
fix: subquery memleak (#32024)

124299 of 282344 branches covered (44.02%)

Branch coverage included in aggregate %.

181106 of 255787 relevant lines covered (70.8%)

24937406.43 hits per line

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

54.31
/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) {
358,387✔
23
  if (pTfs) {
358,387!
24
    SDiskID diskId = {0};
358,401✔
25
    diskId.id = diskPrimary;
358,401✔
26
    snprintf(buf, bufLen - 1, "%s%s%s", tfsGetDiskPath(pTfs, diskId), TD_DIRSEP, relPath);
358,401✔
27
  } else {
28
    snprintf(buf, bufLen - 1, "%s", relPath);
×
29
  }
30
  buf[bufLen - 1] = '\0';
358,444✔
31
}
358,444✔
32

33
void vnodeGetPrimaryPath(SVnode *pVnode, bool mount, char *buf, size_t bufLen) {
329,701✔
34
  if (pVnode->mounted) {
329,701!
35
    if (mount) {  // mount path
×
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);
×
42
    }
43
    buf[bufLen - 1] = '\0';
×
44

45
  } else {
46
    vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, buf, bufLen);
329,701✔
47
  }
48
}
329,835✔
49

50
static int32_t vnodeMkDir(STfs *pTfs, const char *path) {
26,549✔
51
  if (pTfs) {
26,549!
52
    return tfsMkdirRecur(pTfs, path);
26,549✔
53
  } else {
54
    return taosMkDir(path);
×
55
  }
56
}
57

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

63
  // check config
64
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
11,455!
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)) {
11,492!
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);
11,500✔
75

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

85
  SVnodeInfo oldInfo = {0};
11,500✔
86
  oldInfo.config = vnodeCfgDefault;
11,500✔
87
  if (vnodeLoadInfo(dir, &oldInfo) == 0) {
11,500!
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);
11,500✔
102
  if ((code = vnodeSaveInfo(dir, &info)) < 0 || (code = vnodeCommitInfo(dir)) < 0) {
11,500!
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);
11,500✔
108
  return 0;
11,500✔
109
}
110

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

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

118
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
2,165✔
119

120
  ret = vnodeLoadInfo(dir, &info);
2,165✔
121
  if (ret < 0) {
2,165!
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;
2,165✔
127

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

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

161
  if (info.config.walCfg.clearFiles) {
2,165!
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,
2,165!
168
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
169

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

177
  ret = vnodeCommitInfo(dir);
2,165✔
178
  if (ret < 0) {
2,165!
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);
2,165!
184
  return 0;
2,165✔
185
}
186

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

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

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

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

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

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

220
    int32_t tsdbFileVgId = 0;
334✔
221
    ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
334✔
222
    if (ret != 0) {
334!
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) {
334!
229
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
334✔
230

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

235
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
334✔
236
      if (ret != 0) {
334!
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);
56✔
245

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

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

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

262
  ret = vnodeLoadInfo(dir, &info);
56✔
263
  if (ret < 0) {
56!
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,
56!
269
        pReq->hashBegin, pReq->hashEnd);
270
  info.config.vgId = pReq->dstVgId;
56✔
271
  info.config.hashBegin = pReq->hashBegin;
56✔
272
  info.config.hashEnd = pReq->hashEnd;
56✔
273
  info.config.hashChange = true;
56✔
274
  info.config.walCfg.vgId = pReq->dstVgId;
56✔
275
  info.config.syncCfg.changeVersion = pReq->changeVersion;
56✔
276

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

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

290
  info.config.syncCfg = *pCfg;
56✔
291

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

298
  ret = vnodeCommitInfo(dir);
56✔
299
  if (ret < 0) {
56!
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);
56!
305
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
56✔
306
  if (ret < 0) {
56!
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);
56!
313
  return 0;
56✔
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) {
5,115✔
355
  vInfo("path:%s is removed while destroy vnode", path);
5,115✔
356
  if (tfsRmdir(pTfs, path) < 0) {
5,115!
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) {
5,115!
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
    tssDeleteFileByPrefixFromDefault(prefix);
×
366
  }
367
#endif
368
}
5,115✔
369

370
static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) {
15,059✔
371
  int32_t ndisk = 1;
15,059✔
372
  if (pTfs) {
15,059!
373
    ndisk = tfsGetDisksAtLevel(pTfs, 0);
15,060✔
374
  }
375
  if (diskPrimary < 0 || diskPrimary >= ndisk) {
15,059!
376
    vError("disk:%d is unavailable from the %d disks mounted at level 0", diskPrimary, ndisk);
×
377
    return terrno = TSDB_CODE_FS_INVLD_CFG;
×
378
  }
379
  return 0;
15,060✔
380
}
381

382
SVnode *vnodeOpen(const char *path, int32_t diskPrimary, STfs *pTfs, STfs *pMountTfs, SMsgCb msgCb, bool force) {
15,051✔
383
  SVnode    *pVnode = NULL;
15,051✔
384
  SVnodeInfo info = {0};
15,051✔
385
  char       dir[TSDB_FILENAME_LEN] = {0};
15,051✔
386
  char       tdir[TSDB_FILENAME_LEN * 2] = {0};
15,051✔
387
  int32_t    ret = 0;
15,051✔
388
  bool       mounted = pMountTfs != NULL;
15,051✔
389
  terrno = TSDB_CODE_SUCCESS;
15,051✔
390

391
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
15,061!
392
    vError("failed to open vnode from %s since %s. diskPrimary:%d", path, terrstr(), diskPrimary);
×
393
    return NULL;
×
394
  }
395
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
15,060✔
396

397
  info.config = vnodeCfgDefault;
15,060✔
398

399
  // load vnode info
400
  vInfo("vgId:%d, start to vnode load info %s", info.config.vgId, dir);
15,060✔
401
  ret = vnodeLoadInfo(dir, &info);
15,068✔
402
  if (ret < 0) {
15,061!
403
    vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
×
404
    terrno = TSDB_CODE_NEED_RETRY;
×
405
    return NULL;
×
406
  }
407

408
  if (!mounted && vnodeMkDir(pTfs, path)) {
15,061✔
409
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", info.config.vgId, strerror(ERRNO), path);
25!
410
    return NULL;
×
411
  }
412
  // save vnode info on dnode ep changed
413
  bool      updated = false;
15,020✔
414
  SSyncCfg *pCfg = &info.config.syncCfg;
15,020✔
415
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
39,170✔
416
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
24,116✔
417
    if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
24,116!
418
      updated = true;
×
419
    }
420
  }
421
  if (updated) {
15,054!
422
    vInfo("vgId:%d, save vnode info since dnode info changed", info.config.vgId);
×
423
    if (vnodeSaveInfo(dir, &info) < 0) {
×
424
      vError("vgId:%d, failed to save vnode info since %s", info.config.vgId, tstrerror(terrno));
×
425
    }
426

427
    if (vnodeCommitInfo(dir) < 0) {
×
428
      vError("vgId:%d, failed to commit vnode info since %s", info.config.vgId, tstrerror(terrno));
×
429
    }
430
  }
431

432
  // create handle
433
  pVnode = taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
15,036!
434
  if (pVnode == NULL) {
15,040!
435
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
436
    vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
×
437
    return NULL;
×
438
  }
439

440
  pVnode->path = (char *)&pVnode[1];
15,040✔
441
  memcpy(pVnode->path, path, strlen(path) + 1);
15,040✔
442
  pVnode->config = info.config;
15,040✔
443
  pVnode->state.committed = info.state.committed;
15,040✔
444
  pVnode->state.commitTerm = info.state.commitTerm;
15,040✔
445
  pVnode->state.commitID = info.state.commitID;
15,040✔
446
  pVnode->state.applied = info.state.committed;
15,040✔
447
  pVnode->state.applyTerm = info.state.commitTerm;
15,040✔
448
  pVnode->pTfs = pTfs;
15,040✔
449
  pVnode->pMountTfs = pMountTfs;
15,040✔
450
  pVnode->mounted = mounted;
15,040✔
451
  pVnode->diskPrimary = diskPrimary;
15,040✔
452
  pVnode->msgCb = msgCb;
15,040✔
453
  (void)taosThreadMutexInit(&pVnode->lock, NULL);
15,040✔
454
  pVnode->blocked = false;
15,031✔
455
  pVnode->disableWrite = false;
15,031✔
456

457
  if (tsem_init(&pVnode->syncSem, 0, 0) != 0) {
15,031!
458
    vError("vgId:%d, failed to init semaphore", TD_VID(pVnode));
×
459
    goto _err;
×
460
  }
461
  (void)taosThreadMutexInit(&pVnode->mutex, NULL);
15,030✔
462
  (void)taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
15,047✔
463

464
  vInfo("vgId:%d, finished vnode load info %s, vnode committed:%" PRId64, info.config.vgId, dir, pVnode->state.committed);
15,020!
465

466
  int8_t rollback = vnodeShouldRollback(pVnode);
15,035✔
467

468
  // open buffer pool
469
  vInfo("vgId:%d, start to open vnode buffer pool", TD_VID(pVnode));
15,061✔
470
  if (vnodeOpenBufPool(pVnode) < 0) {
15,061!
471
    vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
472
    goto _err;
×
473
  }
474

475
  // open meta
476
  (void)taosThreadRwlockInit(&pVnode->metaRWLock, NULL);
15,061✔
477
  vInfo("vgId:%d, start to open vnode meta", TD_VID(pVnode));
15,061✔
478
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
15,061!
479
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
480
    goto _err;
×
481
  }
482

483
  vInfo("vgId:%d, start to upgrade meta", TD_VID(pVnode));
15,060✔
484
  if (!mounted && metaUpgrade(pVnode, &pVnode->pMeta) < 0) {
15,061!
485
    vError("vgId:%d, failed to upgrade meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
486
  }
487

488
  // open tsdb
489
  vInfo("vgId:%d, start to open vnode tsdb", TD_VID(pVnode));
15,061✔
490
  if (!VND_IS_RSMA(pVnode) && (terrno = tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback, force)) < 0) {
15,061!
491
    vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
×
492
    goto _err;
×
493
  }
494

495
  // open wal
496
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
15,051✔
497
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
15,051✔
498
  TAOS_UNUSED(ret);
499

500
  vInfo("vgId:%d, start to open vnode wal", TD_VID(pVnode));
15,054✔
501
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
15,059✔
502
  if (pVnode->pWal == NULL) {
15,059!
503
    vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
×
504
    goto _err;
×
505
  }
506

507
  // open tq
508
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
15,059✔
509
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
15,058✔
510
  TAOS_UNUSED(ret);
511

512
  // open query
513
  vInfo("vgId:%d, start to open vnode query", TD_VID(pVnode));
15,058✔
514
  if (vnodeQueryOpen(pVnode)) {
15,061!
515
    vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
×
516
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
517
    goto _err;
×
518
  }
519

520
  // sma required the tq is initialized before the vnode open
521
  vInfo("vgId:%d, start to open vnode tq", TD_VID(pVnode));
15,061✔
522
  if (tqOpen(tdir, pVnode)) {
15,062!
523
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
×
524
    goto _err;
×
525
  }
526

527
  // open sma
528
  vInfo("vgId:%d, start to open vnode sma", TD_VID(pVnode));
15,059✔
529
  if (smaOpen(pVnode, rollback, force)) {
15,061!
530
    vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
531
    goto _err;
×
532
  }
533
  // open blob store engine
534
  vInfo("vgId:%d, start to open blob store engine", TD_VID(pVnode));
15,061✔
535
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_BSE_DIR);
15,061✔
536

537
  SBseCfg cfg = {.vgId = pVnode->config.vgId, .keepDays = 365 * 24 * 3600};
15,061✔
538
  ret = bseOpen(tdir, &cfg, &pVnode->pBse);
15,061✔
539
  if (ret != 0) {
15,061!
540
    vError("vgId:%d, failed to open blob store engine since %s", TD_VID(pVnode), tstrerror(ret));
×
541
    terrno = ret;
×
542
    goto _err;
×
543
  }
544

545
  // vnode begin
546
  vInfo("vgId:%d, start to begin vnode", TD_VID(pVnode));
15,061✔
547
  if (vnodeBegin(pVnode) < 0) {
15,061!
548
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
×
549
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
550
    goto _err;
×
551
  }
552

553
  // open sync
554
  vInfo("vgId:%d, start to open sync, changeVersion:%d", TD_VID(pVnode), info.config.syncCfg.changeVersion);
15,061✔
555
  if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
15,062!
556
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
557
    goto _err;
×
558
  }
559

560
  if (rollback) {
15,061!
561
    vnodeRollback(pVnode);
×
562
  }
563

564
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
15,061✔
565
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
15,061✔
566
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
15,061✔
567

568
  return pVnode;
15,061✔
569

570
_err:
×
571
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
×
572
  if (pVnode->pTq) tqClose(pVnode->pTq);
×
573
  if (pVnode->pWal) walClose(pVnode->pWal);
×
574
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
×
575
  if (pVnode->pSma) smaClose(pVnode->pSma);
×
576
  if (pVnode->pMeta) metaClose(&pVnode->pMeta);
×
577
  if (pVnode->freeList) vnodeCloseBufPool(pVnode);
×
578

579
  (void)taosThreadRwlockDestroy(&pVnode->metaRWLock);
×
580
  taosMemoryFree(pVnode);
×
581
  return NULL;
×
582
}
583

584
void vnodePreClose(SVnode *pVnode) {
15,061✔
585
  vnodeSyncPreClose(pVnode);
15,061✔
586
  vnodeQueryPreClose(pVnode);
15,061✔
587
}
15,051✔
588

589
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
15,061✔
590

591
void vnodeClose(SVnode *pVnode) {
15,061✔
592
  if (pVnode) {
15,061!
593
    vInfo("start to close vnode");
15,061✔
594
    vnodeAWait(&pVnode->commitTask2);
15,061✔
595
    vnodeAWait(&pVnode->commitTask);
15,061✔
596
    vnodeSyncClose(pVnode);
15,061✔
597
    vnodeQueryClose(pVnode);
15,060✔
598
    streamRemoveVnodeLeader(pVnode->config.vgId);
15,061✔
599
    tqClose(pVnode->pTq);
15,061✔
600
    walClose(pVnode->pWal);
15,059✔
601
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
15,061!
602
    smaClose(pVnode->pSma);
15,054✔
603
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
15,059!
604
    vnodeCloseBufPool(pVnode);
15,060✔
605

606
    if (pVnode->pBse) {
15,061!
607
      bseClose(pVnode->pBse);
15,061✔
608
    }
609

610
    // destroy handle
611
    if (tsem_destroy(&pVnode->syncSem) != 0) {
15,057!
612
      vError("vgId:%d, failed to destroy semaphore", TD_VID(pVnode));
×
613
    }
614
    (void)taosThreadCondDestroy(&pVnode->poolNotEmpty);
15,061✔
615
    (void)taosThreadMutexDestroy(&pVnode->mutex);
15,056✔
616
    (void)taosThreadMutexDestroy(&pVnode->lock);
15,058✔
617
    taosMemoryFree(pVnode);
15,057!
618
  }
619
}
15,055✔
620

621
// start the sync timer after the queue is ready
622
int32_t vnodeStart(SVnode *pVnode) {
15,061✔
623
  if (pVnode == NULL) {
15,061!
624
    return TSDB_CODE_INVALID_PARA;
×
625
  }
626
  return vnodeSyncStart(pVnode);
15,061✔
627
}
628

629
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
7,063✔
630

631
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
7,063✔
632

633
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
134✔
634
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
164✔
635

636
void vnodeStop(SVnode *pVnode) {}
×
637

638
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