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

taosdata / TDengine / #3565

25 Dec 2024 05:34AM UTC coverage: 51.098% (-11.1%) from 62.21%
#3565

push

travis-ci

web-flow
Merge pull request #29316 from taosdata/enh/3.0/TD-33266

enh(ut):Add wal & config UT.

111558 of 284773 branches covered (39.17%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 2 files covered. (50.0%)

39015 existing lines in 102 files now uncovered.

177882 of 281666 relevant lines covered (63.15%)

15090998.35 hits per line

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

28.45
/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 "tcs.h"
18
#include "tsdb.h"
19
#include "vnd.h"
20

21
void vnodeGetPrimaryDir(const char *relPath, int32_t diskPrimary, STfs *pTfs, char *buf, size_t bufLen) {
166✔
22
  if (pTfs) {
166!
23
    SDiskID diskId = {0};
166✔
24
    diskId.id = diskPrimary;
166✔
25
    snprintf(buf, bufLen - 1, "%s%s%s", tfsGetDiskPath(pTfs, diskId), TD_DIRSEP, relPath);
166✔
26
  } else {
27
    snprintf(buf, bufLen - 1, "%s", relPath);
×
28
  }
29
  buf[bufLen - 1] = '\0';
166✔
30
}
166✔
31

32
static int32_t vnodeMkDir(STfs *pTfs, const char *path) {
36✔
33
  if (pTfs) {
36!
34
    return tfsMkdirRecur(pTfs, path);
36✔
35
  } else {
36
    return taosMkDir(path);
×
37
  }
38
}
39

40
int32_t vnodeCreate(const char *path, SVnodeCfg *pCfg, int32_t diskPrimary, STfs *pTfs) {
18✔
41
  int32_t    code = 0;
18✔
42
  SVnodeInfo info = {0};
18✔
43
  char       dir[TSDB_FILENAME_LEN] = {0};
18✔
44

45
  // check config
46
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
18!
47
    vError("vgId:%d, failed to create vnode since:%s", pCfg->vgId, tstrerror(code));
×
48
    return code;
×
49
  }
50

51
  // create vnode env
52
  if (vnodeMkDir(pTfs, path)) {
18!
53
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", pCfg->vgId, strerror(errno), path);
×
54
    return TAOS_SYSTEM_ERROR(errno);
×
55
  }
56
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
18✔
57

58
  if (pCfg) {
18!
59
    info.config = *pCfg;
18✔
60
  } else {
61
    info.config = vnodeCfgDefault;
×
62
  }
63
  info.state.committed = -1;
18✔
64
  info.state.applied = -1;
18✔
65
  info.state.commitID = 0;
18✔
66

67
  SVnodeInfo oldInfo = {0};
18✔
68
  oldInfo.config = vnodeCfgDefault;
18✔
69
  if (vnodeLoadInfo(dir, &oldInfo) == 0) {
18!
70
    code = (oldInfo.config.dbId == info.config.dbId) ? 0 : TSDB_CODE_VND_ALREADY_EXIST_BUT_NOT_MATCH;
×
71
    if (code == 0) {
×
72
      vWarn("vgId:%d, vnode config info already exists at %s.", oldInfo.config.vgId, dir);
×
73
    } else {
74
      vError("vgId:%d, vnode config info already exists at %s. oldDbId:%" PRId64 "(%s) at cluster:%" PRId64
×
75
             ", newDbId:%" PRId64 "(%s) at cluser:%" PRId64 ", code:%s",
76
             oldInfo.config.vgId, dir, oldInfo.config.dbId, oldInfo.config.dbname,
77
             oldInfo.config.syncCfg.nodeInfo[oldInfo.config.syncCfg.myIndex].clusterId, info.config.dbId,
78
             info.config.dbname, info.config.syncCfg.nodeInfo[info.config.syncCfg.myIndex].clusterId, tstrerror(code));
79
    }
80
    return code;
×
81
  }
82

83
  vInfo("vgId:%d, save config while create", info.config.vgId);
18!
84
  if ((code = vnodeSaveInfo(dir, &info)) < 0 || (code = vnodeCommitInfo(dir)) < 0) {
18!
85
    vError("vgId:%d, failed to save vnode config since %s", pCfg ? pCfg->vgId : 0, tstrerror(code));
×
86
    return code;
×
87
  }
88

89
  vInfo("vgId:%d, vnode is created", info.config.vgId);
18!
90
  return 0;
18✔
91
}
92

UNCOV
93
bool vnodeShouldRemoveWal(SVnode *pVnode) { return pVnode->config.walCfg.clearFiles == 1; }
×
94

UNCOV
95
int32_t vnodeAlterReplica(const char *path, SAlterVnodeReplicaReq *pReq, int32_t diskPrimary, STfs *pTfs) {
×
UNCOV
96
  SVnodeInfo info = {0};
×
UNCOV
97
  char       dir[TSDB_FILENAME_LEN] = {0};
×
UNCOV
98
  int32_t    ret = 0;
×
99

UNCOV
100
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
101

UNCOV
102
  ret = vnodeLoadInfo(dir, &info);
×
UNCOV
103
  if (ret < 0) {
×
104
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->vgId, path, tstrerror(terrno));
×
105
    return ret;
×
106
  }
107

UNCOV
108
  SSyncCfg *pCfg = &info.config.syncCfg;
×
109

UNCOV
110
  pCfg->replicaNum = 0;
×
UNCOV
111
  pCfg->totalReplicaNum = 0;
×
UNCOV
112
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
×
113

UNCOV
114
  for (int i = 0; i < pReq->replica; ++i) {
×
UNCOV
115
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
×
UNCOV
116
    pNode->nodeId = pReq->replicas[i].id;
×
UNCOV
117
    pNode->nodePort = pReq->replicas[i].port;
×
UNCOV
118
    tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
×
UNCOV
119
    pNode->nodeRole = TAOS_SYNC_ROLE_VOTER;
×
UNCOV
120
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
×
UNCOV
121
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
×
UNCOV
122
    pCfg->replicaNum++;
×
123
  }
UNCOV
124
  if (pReq->selfIndex != -1) {
×
UNCOV
125
    pCfg->myIndex = pReq->selfIndex;
×
126
  }
UNCOV
127
  for (int i = pCfg->replicaNum; i < pReq->replica + pReq->learnerReplica; ++i) {
×
UNCOV
128
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
×
UNCOV
129
    pNode->nodeId = pReq->learnerReplicas[pCfg->totalReplicaNum].id;
×
UNCOV
130
    pNode->nodePort = pReq->learnerReplicas[pCfg->totalReplicaNum].port;
×
UNCOV
131
    pNode->nodeRole = TAOS_SYNC_ROLE_LEARNER;
×
UNCOV
132
    tstrncpy(pNode->nodeFqdn, pReq->learnerReplicas[pCfg->totalReplicaNum].fqdn, sizeof(pNode->nodeFqdn));
×
UNCOV
133
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
×
UNCOV
134
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
×
UNCOV
135
    pCfg->totalReplicaNum++;
×
136
  }
UNCOV
137
  pCfg->totalReplicaNum += pReq->replica;
×
UNCOV
138
  if (pReq->learnerSelfIndex != -1) {
×
139
    pCfg->myIndex = pReq->replica + pReq->learnerSelfIndex;
×
140
  }
UNCOV
141
  pCfg->changeVersion = pReq->changeVersion;
×
142

UNCOV
143
  if (info.config.walCfg.clearFiles) {
×
144
    info.config.walCfg.clearFiles = 0;
×
145

146
    vInfo("vgId:%d, reset wal clearFiles", pReq->vgId);
×
147
  }
148

UNCOV
149
  vInfo("vgId:%d, save config while alter, replicas:%d totalReplicas:%d selfIndex:%d changeVersion:%d", pReq->vgId,
×
150
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
151

UNCOV
152
  info.config.syncCfg = *pCfg;
×
UNCOV
153
  ret = vnodeSaveInfo(dir, &info);
×
UNCOV
154
  if (ret < 0) {
×
155
    vError("vgId:%d, failed to save vnode config since %s", pReq->vgId, tstrerror(terrno));
×
156
    return ret;
×
157
  }
158

UNCOV
159
  ret = vnodeCommitInfo(dir);
×
UNCOV
160
  if (ret < 0) {
×
161
    vError("vgId:%d, failed to commit vnode config since %s", pReq->vgId, tstrerror(terrno));
×
162
    return ret;
×
163
  }
164

UNCOV
165
  vInfo("vgId:%d, vnode config is saved", info.config.vgId);
×
UNCOV
166
  return 0;
×
167
}
168

UNCOV
169
static int32_t vnodeVgroupIdLen(int32_t vgId) {
×
170
  char tmp[TSDB_FILENAME_LEN];
UNCOV
171
  (void)tsnprintf(tmp, TSDB_FILENAME_LEN, "%d", vgId);
×
UNCOV
172
  return strlen(tmp);
×
173
}
174

UNCOV
175
int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
×
176
                            int32_t diskPrimary, STfs *pTfs) {
UNCOV
177
  int32_t ret = 0;
×
178

UNCOV
179
  char oldRname[TSDB_FILENAME_LEN] = {0};
×
UNCOV
180
  char newRname[TSDB_FILENAME_LEN] = {0};
×
UNCOV
181
  char tsdbPath[TSDB_FILENAME_LEN] = {0};
×
UNCOV
182
  char tsdbFilePrefix[TSDB_FILENAME_LEN] = {0};
×
UNCOV
183
  snprintf(tsdbPath, TSDB_FILENAME_LEN, "%s%stsdb", srcPath, TD_DIRSEP);
×
UNCOV
184
  snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP);
×
UNCOV
185
  int32_t prefixLen = strlen(tsdbFilePrefix);
×
186

UNCOV
187
  STfsDir *tsdbDir = NULL;
×
UNCOV
188
  int32_t  tret = tfsOpendir(pTfs, tsdbPath, &tsdbDir);
×
UNCOV
189
  if (tsdbDir == NULL) {
×
190
    return 0;
×
191
  }
192

UNCOV
193
  while (1) {
×
UNCOV
194
    const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
×
UNCOV
195
    if (tsdbFile == NULL) break;
×
UNCOV
196
    if (tsdbFile->rname[0] == '\0') continue;
×
UNCOV
197
    tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
×
198

UNCOV
199
    char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
×
UNCOV
200
    if (tsdbFilePrefixPos == NULL) continue;
×
201

UNCOV
202
    int32_t tsdbFileVgId = 0;
×
UNCOV
203
    ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
×
UNCOV
204
    if (ret != 0) {
×
205
      vError("vgId:%d, failed to get tsdb file vgid since %s", dstVgId, tstrerror(ret));
×
206
      tfsClosedir(tsdbDir);
×
207
      return ret;
×
208
    }
209

UNCOV
210
    if (tsdbFileVgId == srcVgId) {
×
UNCOV
211
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
×
212

UNCOV
213
      tsdbFilePrefixPos[prefixLen] = 0;
×
UNCOV
214
      snprintf(newRname, TSDB_FILENAME_LEN, "%s%d%s", oldRname, dstVgId, tsdbFileSurfixPos);
×
UNCOV
215
      vInfo("vgId:%d, rename file from %s to %s", dstVgId, tsdbFile->rname, newRname);
×
216

UNCOV
217
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
×
UNCOV
218
      if (ret != 0) {
×
219
        vError("vgId:%d, failed to rename file from %s to %s since %s", dstVgId, tsdbFile->rname, newRname, terrstr());
×
220
        tfsClosedir(tsdbDir);
×
221
        return ret;
×
222
      }
223
    }
224
  }
225

UNCOV
226
  tfsClosedir(tsdbDir);
×
227

UNCOV
228
  vInfo("vgId:%d, rename dir from %s to %s", dstVgId, srcPath, dstPath);
×
UNCOV
229
  ret = tfsRename(pTfs, diskPrimary, srcPath, dstPath);
×
UNCOV
230
  if (ret != 0) {
×
231
    vError("vgId:%d, failed to rename dir from %s to %s since %s", dstVgId, srcPath, dstPath, terrstr());
×
232
  }
UNCOV
233
  return ret;
×
234
}
235

UNCOV
236
int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnodeHashRangeReq *pReq,
×
237
                            int32_t diskPrimary, STfs *pTfs) {
UNCOV
238
  SVnodeInfo info = {0};
×
UNCOV
239
  char       dir[TSDB_FILENAME_LEN] = {0};
×
UNCOV
240
  int32_t    ret = 0;
×
241

UNCOV
242
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
243

UNCOV
244
  ret = vnodeLoadInfo(dir, &info);
×
UNCOV
245
  if (ret < 0) {
×
246
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->srcVgId, srcPath, tstrerror(terrno));
×
247
    return ret;
×
248
  }
249

UNCOV
250
  vInfo("vgId:%d, alter hashrange from [%u, %u] to [%u, %u]", pReq->srcVgId, info.config.hashBegin, info.config.hashEnd,
×
251
        pReq->hashBegin, pReq->hashEnd);
UNCOV
252
  info.config.vgId = pReq->dstVgId;
×
UNCOV
253
  info.config.hashBegin = pReq->hashBegin;
×
UNCOV
254
  info.config.hashEnd = pReq->hashEnd;
×
UNCOV
255
  info.config.hashChange = true;
×
UNCOV
256
  info.config.walCfg.vgId = pReq->dstVgId;
×
UNCOV
257
  info.config.syncCfg.changeVersion = pReq->changeVersion;
×
258

UNCOV
259
  SSyncCfg *pCfg = &info.config.syncCfg;
×
UNCOV
260
  pCfg->myIndex = 0;
×
UNCOV
261
  pCfg->replicaNum = 1;
×
UNCOV
262
  pCfg->totalReplicaNum = 1;
×
UNCOV
263
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
×
264

UNCOV
265
  vInfo("vgId:%d, alter vnode replicas to 1", pReq->srcVgId);
×
UNCOV
266
  SNodeInfo *pNode = &pCfg->nodeInfo[0];
×
UNCOV
267
  pNode->nodePort = tsServerPort;
×
UNCOV
268
  tstrncpy(pNode->nodeFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
×
UNCOV
269
  bool ret1 = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
×
UNCOV
270
  vInfo("vgId:%d, ep:%s:%u dnode:%d", pReq->srcVgId, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
×
271

UNCOV
272
  info.config.syncCfg = *pCfg;
×
273

UNCOV
274
  ret = vnodeSaveInfo(dir, &info);
×
UNCOV
275
  if (ret < 0) {
×
276
    vError("vgId:%d, failed to save vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
277
    return ret;
×
278
  }
279

UNCOV
280
  ret = vnodeCommitInfo(dir);
×
UNCOV
281
  if (ret < 0) {
×
282
    vError("vgId:%d, failed to commit vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
283
    return ret;
×
284
  }
285

UNCOV
286
  vInfo("vgId:%d, rename %s to %s", pReq->dstVgId, srcPath, dstPath);
×
UNCOV
287
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
×
UNCOV
288
  if (ret < 0) {
×
289
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", pReq->dstVgId, srcPath, dstPath,
×
290
           tstrerror(terrno));
291
    return ret;
×
292
  }
293

UNCOV
294
  vInfo("vgId:%d, vnode hashrange is altered", info.config.vgId);
×
UNCOV
295
  return 0;
×
296
}
297

298
int32_t vnodeRestoreVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
×
299
                             int32_t diskPrimary, STfs *pTfs) {
300
  SVnodeInfo info = {0};
×
301
  char       dir[TSDB_FILENAME_LEN] = {0};
×
302
  int32_t    code = 0;
×
303

304
  vnodeGetPrimaryDir(dstPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
305
  if (vnodeLoadInfo(dir, &info) == 0) {
×
306
    if (info.config.vgId != dstVgId) {
×
307
      vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
308
      return TSDB_CODE_FAILED;
×
309
    }
310
    return dstVgId;
×
311
  }
312

313
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
314
  if ((code = vnodeLoadInfo(dir, &info)) < 0) {
×
315
    vError("vgId:%d, failed to read vnode config from %s since %s", srcVgId, srcPath, tstrerror(terrno));
×
316
    return code;
×
317
  }
318

319
  if (info.config.vgId == srcVgId) {
×
320
    vInfo("vgId:%d, rollback alter hashrange", srcVgId);
×
321
    return srcVgId;
×
322
  } else if (info.config.vgId != dstVgId) {
×
323
    vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
324
    return TSDB_CODE_FAILED;
×
325
  }
326

327
  vInfo("vgId:%d, rename %s to %s", dstVgId, srcPath, dstPath);
×
328
  if (vnodeRenameVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs) < 0) {
×
329
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", dstVgId, srcPath, dstPath, tstrerror(terrno));
×
330
    return TSDB_CODE_FAILED;
×
331
  }
332

333
  return dstVgId;
×
334
}
335

336
void vnodeDestroy(int32_t vgId, const char *path, STfs *pTfs, int32_t nodeId) {
16✔
337
  vInfo("path:%s is removed while destroy vnode", path);
16!
338
  if (tfsRmdir(pTfs, path) < 0) {
16!
339
    vError("failed to remove path:%s since %s", path, tstrerror(terrno));
×
340
  }
341

342
  // int32_t nlevel = tfsGetLevel(pTfs);
343
  if (nodeId > 0 && vgId > 0 /*&& nlevel > 1*/ && tsS3Enabled) {
16!
344
    char vnode_prefix[TSDB_FILENAME_LEN];
345
    snprintf(vnode_prefix, TSDB_FILENAME_LEN, "%d/v%df", nodeId, vgId);
×
346
    tcsDeleteObjectsByPrefix(vnode_prefix);
×
347
  }
348
}
16✔
349

350
static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) {
18✔
351
  int32_t ndisk = 1;
18✔
352
  if (pTfs) {
18!
353
    ndisk = tfsGetDisksAtLevel(pTfs, 0);
18✔
354
  }
355
  if (diskPrimary < 0 || diskPrimary >= ndisk) {
18!
356
    vError("disk:%d is unavailable from the %d disks mounted at level 0", diskPrimary, ndisk);
×
357
    return terrno = TSDB_CODE_FS_INVLD_CFG;
×
358
  }
359
  return 0;
18✔
360
}
361

362
SVnode *vnodeOpen(const char *path, int32_t diskPrimary, STfs *pTfs, SMsgCb msgCb, bool force) {
18✔
363
  SVnode    *pVnode = NULL;
18✔
364
  SVnodeInfo info = {0};
18✔
365
  char       dir[TSDB_FILENAME_LEN] = {0};
18✔
366
  char       tdir[TSDB_FILENAME_LEN * 2] = {0};
18✔
367
  int32_t    ret = 0;
18✔
368
  terrno = TSDB_CODE_SUCCESS;
18✔
369

370
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
18!
371
    vError("failed to open vnode from %s since %s. diskPrimary:%d", path, terrstr(), diskPrimary);
×
372
    return NULL;
×
373
  }
374
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
18✔
375

376
  info.config = vnodeCfgDefault;
18✔
377

378
  // load vnode info
379
  vInfo("vgId:%d, start to vnode load info %s", info.config.vgId, dir);
18!
380
  ret = vnodeLoadInfo(dir, &info);
18✔
381
  if (ret < 0) {
18!
382
    vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
×
383
    terrno = TSDB_CODE_NEED_RETRY;
×
384
    return NULL;
×
385
  }
386

387
  if (vnodeMkDir(pTfs, path)) {
18!
388
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", info.config.vgId, strerror(errno), path);
×
389
    return NULL;
×
390
  }
391
  // save vnode info on dnode ep changed
392
  bool      updated = false;
18✔
393
  SSyncCfg *pCfg = &info.config.syncCfg;
18✔
394
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
36✔
395
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
18✔
396
    if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
18!
397
      updated = true;
×
398
    }
399
  }
400
  if (updated) {
18!
401
    vInfo("vgId:%d, save vnode info since dnode info changed", info.config.vgId);
×
402
    if (vnodeSaveInfo(dir, &info) < 0) {
×
403
      vError("vgId:%d, failed to save vnode info since %s", info.config.vgId, tstrerror(terrno));
×
404
    }
405

406
    if (vnodeCommitInfo(dir) < 0) {
×
407
      vError("vgId:%d, failed to commit vnode info since %s", info.config.vgId, tstrerror(terrno));
×
408
    }
409
  }
410

411
  // create handle
412
  pVnode = taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
18!
413
  if (pVnode == NULL) {
18!
414
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
415
    vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
×
416
    return NULL;
×
417
  }
418

419
  pVnode->path = (char *)&pVnode[1];
18✔
420
  memcpy(pVnode->path, path, strlen(path) + 1);
18✔
421
  pVnode->config = info.config;
18✔
422
  pVnode->state.committed = info.state.committed;
18✔
423
  pVnode->state.commitTerm = info.state.commitTerm;
18✔
424
  pVnode->state.commitID = info.state.commitID;
18✔
425
  pVnode->state.applied = info.state.committed;
18✔
426
  pVnode->state.applyTerm = info.state.commitTerm;
18✔
427
  pVnode->pTfs = pTfs;
18✔
428
  pVnode->diskPrimary = diskPrimary;
18✔
429
  pVnode->msgCb = msgCb;
18✔
430
  (void)taosThreadMutexInit(&pVnode->lock, NULL);
18✔
431
  pVnode->blocked = false;
18✔
432
  pVnode->disableWrite = false;
18✔
433

434
  if (tsem_init(&pVnode->syncSem, 0, 0) != 0) {
18!
435
    vError("vgId:%d, failed to init semaphore", TD_VID(pVnode));
×
436
    goto _err;
×
437
  }
438
  (void)taosThreadMutexInit(&pVnode->mutex, NULL);
18✔
439
  (void)taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
18✔
440

441
  int8_t rollback = vnodeShouldRollback(pVnode);
18✔
442

443
  // open buffer pool
444
  vInfo("vgId:%d, start to open vnode buffer pool", TD_VID(pVnode));
18!
445
  if (vnodeOpenBufPool(pVnode) < 0) {
18!
446
    vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
447
    goto _err;
×
448
  }
449

450
  // open meta
451
  vInfo("vgId:%d, start to open vnode meta", TD_VID(pVnode));
18!
452
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
18!
453
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
454
    goto _err;
×
455
  }
456

457
  vInfo("vgId:%d, start to upgrade meta", TD_VID(pVnode));
18!
458
  if (metaUpgrade(pVnode, &pVnode->pMeta) < 0) {
18!
459
    vError("vgId:%d, failed to upgrade meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
460
  }
461

462
  // open tsdb
463
  vInfo("vgId:%d, start to open vnode tsdb", TD_VID(pVnode));
18!
464
  if (!VND_IS_RSMA(pVnode) && tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback, force) < 0) {
18!
465
    vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
×
466
    goto _err;
×
467
  }
468

469
  // open wal
470
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
18✔
471
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
18✔
472
  TAOS_UNUSED(ret);
473

474
  vInfo("vgId:%d, start to open vnode wal", TD_VID(pVnode));
18!
475
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
18✔
476
  if (pVnode->pWal == NULL) {
18!
477
    vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
×
478
    goto _err;
×
479
  }
480

481
  // open tq
482
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
18✔
483
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
18✔
484
  TAOS_UNUSED(ret);
485

486
  // open query
487
  vInfo("vgId:%d, start to open vnode query", TD_VID(pVnode));
18!
488
  if (vnodeQueryOpen(pVnode)) {
18!
489
    vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
×
490
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
491
    goto _err;
×
492
  }
493

494
  // sma required the tq is initialized before the vnode open
495
  vInfo("vgId:%d, start to open vnode tq", TD_VID(pVnode));
17!
496
  if (tqOpen(tdir, pVnode)) {
17!
497
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
×
498
    goto _err;
×
499
  }
500

501
  // open sma
502
  vInfo("vgId:%d, start to open vnode sma", TD_VID(pVnode));
18!
503
  if (smaOpen(pVnode, rollback, force)) {
18!
504
    vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
505
    goto _err;
×
506
  }
507

508
  // vnode begin
509
  vInfo("vgId:%d, start to begin vnode", TD_VID(pVnode));
18!
510
  if (vnodeBegin(pVnode) < 0) {
18!
511
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
×
512
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
513
    goto _err;
×
514
  }
515

516
  // open sync
517
  vInfo("vgId:%d, start to open sync, changeVersion:%d", TD_VID(pVnode), info.config.syncCfg.changeVersion);
18!
518
  if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
18!
519
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
520
    goto _err;
×
521
  }
522

523
  if (rollback) {
18!
524
    vnodeRollback(pVnode);
×
525
  }
526

527
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
18✔
528
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
18✔
529
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
18✔
530

531
  return pVnode;
18✔
532

533
_err:
×
534
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
×
535
  if (pVnode->pTq) tqClose(pVnode->pTq);
×
536
  if (pVnode->pWal) walClose(pVnode->pWal);
×
537
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
×
538
  if (pVnode->pSma) smaClose(pVnode->pSma);
×
539
  if (pVnode->pMeta) metaClose(&pVnode->pMeta);
×
540
  if (pVnode->freeList) vnodeCloseBufPool(pVnode);
×
541

542
  taosMemoryFree(pVnode);
×
543
  return NULL;
×
544
}
545

546
void vnodePreClose(SVnode *pVnode) {
18✔
547
  vnodeSyncPreClose(pVnode);
18✔
548
  vnodeQueryPreClose(pVnode);
18✔
549
}
18✔
550

551
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
18✔
552

553
void vnodeClose(SVnode *pVnode) {
18✔
554
  if (pVnode) {
18!
555
    vnodeAWait(&pVnode->commitTask);
18✔
556
    vnodeSyncClose(pVnode);
18✔
557
    vnodeQueryClose(pVnode);
18✔
558
    tqClose(pVnode->pTq);
18✔
559
    walClose(pVnode->pWal);
18✔
560
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
18!
561
    smaClose(pVnode->pSma);
18✔
562
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
18!
563
    vnodeCloseBufPool(pVnode);
18✔
564

565
    // destroy handle
566
    if (tsem_destroy(&pVnode->syncSem) != 0) {
18!
567
      vError("vgId:%d, failed to destroy semaphore", TD_VID(pVnode));
×
568
    }
569
    (void)taosThreadCondDestroy(&pVnode->poolNotEmpty);
18✔
570
    (void)taosThreadMutexDestroy(&pVnode->mutex);
18✔
571
    (void)taosThreadMutexDestroy(&pVnode->lock);
18✔
572
    taosMemoryFree(pVnode);
18!
573
  }
574
}
18✔
575

576
// start the sync timer after the queue is ready
577
int32_t vnodeStart(SVnode *pVnode) {
18✔
578
  if (pVnode == NULL) {
18!
579
    return TSDB_CODE_INVALID_PARA;
×
580
  }
581
  return vnodeSyncStart(pVnode);
18✔
582
}
583

UNCOV
584
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
×
585

UNCOV
586
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
×
587

UNCOV
588
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
×
UNCOV
589
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
×
590

591
void vnodeStop(SVnode *pVnode) {}
×
592

593
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