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

taosdata / TDengine / #3525

10 Nov 2024 03:50AM UTC coverage: 60.818% (-0.08%) from 60.898%
#3525

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

118634 of 249004 branches covered (47.64%)

Branch coverage included in aggregate %.

136 of 169 new or added lines in 23 files covered. (80.47%)

542 existing lines in 129 files now uncovered.

199071 of 273386 relevant lines covered (72.82%)

15691647.46 hits per line

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

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

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

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

45
  // check config
46
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
10,947!
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)) {
11,008!
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);
11,013✔
57

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

67
  SVnodeInfo oldInfo = {0};
11,013✔
68
  oldInfo.config = vnodeCfgDefault;
11,013✔
69
  if (vnodeLoadInfo(dir, &oldInfo) == 0) {
11,013!
70
    vWarn("vgId:%d, vnode config info already exists at %s.", oldInfo.config.vgId, dir);
×
71
    return (oldInfo.config.dbId == info.config.dbId) ? 0 : -1;
×
72
  }
73

74
  vInfo("vgId:%d, save config while create", info.config.vgId);
11,013!
75
  if ((code = vnodeSaveInfo(dir, &info)) < 0 || (code = vnodeCommitInfo(dir)) < 0) {
11,013!
76
    vError("vgId:%d, failed to save vnode config since %s", pCfg ? pCfg->vgId : 0, tstrerror(code));
×
77
    return code;
×
78
  }
79

80
  vInfo("vgId:%d, vnode is created", info.config.vgId);
11,013!
81
  return 0;
11,013✔
82
}
83

84
bool vnodeShouldRemoveWal(SVnode *pVnode) { return pVnode->config.walCfg.clearFiles == 1; }
1,415✔
85

86
int32_t vnodeAlterReplica(const char *path, SAlterVnodeReplicaReq *pReq, int32_t diskPrimary, STfs *pTfs) {
1,415✔
87
  SVnodeInfo info = {0};
1,415✔
88
  char       dir[TSDB_FILENAME_LEN] = {0};
1,415✔
89
  int32_t    ret = 0;
1,415✔
90

91
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
1,415✔
92

93
  ret = vnodeLoadInfo(dir, &info);
1,415✔
94
  if (ret < 0) {
1,415!
95
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->vgId, path, tstrerror(terrno));
×
96
    return ret;
×
97
  }
98

99
  SSyncCfg *pCfg = &info.config.syncCfg;
1,415✔
100

101
  pCfg->replicaNum = 0;
1,415✔
102
  pCfg->totalReplicaNum = 0;
1,415✔
103
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
1,415✔
104

105
  for (int i = 0; i < pReq->replica; ++i) {
5,326✔
106
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
3,911✔
107
    pNode->nodeId = pReq->replicas[i].id;
3,911✔
108
    pNode->nodePort = pReq->replicas[i].port;
3,911✔
109
    tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
3,911✔
110
    pNode->nodeRole = TAOS_SYNC_ROLE_VOTER;
3,911✔
111
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
3,911✔
112
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
3,911!
113
    pCfg->replicaNum++;
3,911✔
114
  }
115
  if (pReq->selfIndex != -1) {
1,415!
116
    pCfg->myIndex = pReq->selfIndex;
1,415✔
117
  }
118
  for (int i = pCfg->replicaNum; i < pReq->replica + pReq->learnerReplica; ++i) {
1,752✔
119
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
337✔
120
    pNode->nodeId = pReq->learnerReplicas[pCfg->totalReplicaNum].id;
337✔
121
    pNode->nodePort = pReq->learnerReplicas[pCfg->totalReplicaNum].port;
337✔
122
    pNode->nodeRole = TAOS_SYNC_ROLE_LEARNER;
337✔
123
    tstrncpy(pNode->nodeFqdn, pReq->learnerReplicas[pCfg->totalReplicaNum].fqdn, sizeof(pNode->nodeFqdn));
337✔
124
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
337✔
125
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
337!
126
    pCfg->totalReplicaNum++;
337✔
127
  }
128
  pCfg->totalReplicaNum += pReq->replica;
1,415✔
129
  if (pReq->learnerSelfIndex != -1) {
1,415!
130
    pCfg->myIndex = pReq->replica + pReq->learnerSelfIndex;
×
131
  }
132
  pCfg->changeVersion = pReq->changeVersion;
1,415✔
133

134
  if (info.config.walCfg.clearFiles) {
1,415!
135
    info.config.walCfg.clearFiles = 0;
×
136

137
    vInfo("vgId:%d, reset wal clearFiles", pReq->vgId);
×
138
  }
139

140
  vInfo("vgId:%d, save config while alter, replicas:%d totalReplicas:%d selfIndex:%d changeVersion:%d", pReq->vgId,
1,415!
141
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
142

143
  info.config.syncCfg = *pCfg;
1,415✔
144
  ret = vnodeSaveInfo(dir, &info);
1,415✔
145
  if (ret < 0) {
1,415!
146
    vError("vgId:%d, failed to save vnode config since %s", pReq->vgId, tstrerror(terrno));
×
147
    return ret;
×
148
  }
149

150
  ret = vnodeCommitInfo(dir);
1,415✔
151
  if (ret < 0) {
1,415!
152
    vError("vgId:%d, failed to commit vnode config since %s", pReq->vgId, tstrerror(terrno));
×
153
    return ret;
×
154
  }
155

156
  vInfo("vgId:%d, vnode config is saved", info.config.vgId);
1,415!
157
  return 0;
1,415✔
158
}
159

160
static int32_t vnodeVgroupIdLen(int32_t vgId) {
361✔
161
  char tmp[TSDB_FILENAME_LEN];
162
  sprintf(tmp, "%d", vgId);
361✔
163
  return strlen(tmp);
361✔
164
}
165

166
int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
86✔
167
                            int32_t diskPrimary, STfs *pTfs) {
168
  int32_t ret = 0;
86✔
169

170
  char oldRname[TSDB_FILENAME_LEN] = {0};
86✔
171
  char newRname[TSDB_FILENAME_LEN] = {0};
86✔
172
  char tsdbPath[TSDB_FILENAME_LEN] = {0};
86✔
173
  char tsdbFilePrefix[TSDB_FILENAME_LEN] = {0};
86✔
174
  snprintf(tsdbPath, TSDB_FILENAME_LEN, "%s%stsdb", srcPath, TD_DIRSEP);
86✔
175
  snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP);
86✔
176
  int32_t prefixLen = strlen(tsdbFilePrefix);
86✔
177

178
  STfsDir *tsdbDir = NULL;
86✔
179
  int32_t  tret = tfsOpendir(pTfs, tsdbPath, &tsdbDir);
86✔
180
  if (tsdbDir == NULL) {
86!
181
    return 0;
×
182
  }
183

184
  while (1) {
533✔
185
    const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
619✔
186
    if (tsdbFile == NULL) break;
619✔
187
    if (tsdbFile->rname[0] == '\0') continue;
533!
188
    tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
533✔
189

190
    char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
533✔
191
    if (tsdbFilePrefixPos == NULL) continue;
533✔
192

193
    int32_t tsdbFileVgId = atoi(tsdbFilePrefixPos + prefixLen);
361✔
194
    if (tsdbFileVgId == srcVgId) {
361!
195
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
361✔
196

197
      tsdbFilePrefixPos[prefixLen] = 0;
361✔
198
      snprintf(newRname, TSDB_FILENAME_LEN, "%s%d%s", oldRname, dstVgId, tsdbFileSurfixPos);
361✔
199
      vInfo("vgId:%d, rename file from %s to %s", dstVgId, tsdbFile->rname, newRname);
361!
200

201
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
361✔
202
      if (ret != 0) {
361!
203
        vError("vgId:%d, failed to rename file from %s to %s since %s", dstVgId, tsdbFile->rname, newRname, terrstr());
×
204
        tfsClosedir(tsdbDir);
×
205
        return ret;
×
206
      }
207
    }
208
  }
209

210
  tfsClosedir(tsdbDir);
86✔
211

212
  vInfo("vgId:%d, rename dir from %s to %s", dstVgId, srcPath, dstPath);
86!
213
  ret = tfsRename(pTfs, diskPrimary, srcPath, dstPath);
86✔
214
  if (ret != 0) {
86!
215
    vError("vgId:%d, failed to rename dir from %s to %s since %s", dstVgId, srcPath, dstPath, terrstr());
×
216
  }
217
  return ret;
86✔
218
}
219

220
int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnodeHashRangeReq *pReq,
86✔
221
                            int32_t diskPrimary, STfs *pTfs) {
222
  SVnodeInfo info = {0};
86✔
223
  char       dir[TSDB_FILENAME_LEN] = {0};
86✔
224
  int32_t    ret = 0;
86✔
225

226
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
86✔
227

228
  ret = vnodeLoadInfo(dir, &info);
86✔
229
  if (ret < 0) {
86!
230
    vError("vgId:%d, failed to read vnode config from %s since %s", pReq->srcVgId, srcPath, tstrerror(terrno));
×
231
    return ret;
×
232
  }
233

234
  vInfo("vgId:%d, alter hashrange from [%u, %u] to [%u, %u]", pReq->srcVgId, info.config.hashBegin, info.config.hashEnd,
86!
235
        pReq->hashBegin, pReq->hashEnd);
236
  info.config.vgId = pReq->dstVgId;
86✔
237
  info.config.hashBegin = pReq->hashBegin;
86✔
238
  info.config.hashEnd = pReq->hashEnd;
86✔
239
  info.config.hashChange = true;
86✔
240
  info.config.walCfg.vgId = pReq->dstVgId;
86✔
241
  info.config.syncCfg.changeVersion = pReq->changeVersion;
86✔
242

243
  SSyncCfg *pCfg = &info.config.syncCfg;
86✔
244
  pCfg->myIndex = 0;
86✔
245
  pCfg->replicaNum = 1;
86✔
246
  pCfg->totalReplicaNum = 1;
86✔
247
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
86✔
248

249
  vInfo("vgId:%d, alter vnode replicas to 1", pReq->srcVgId);
86!
250
  SNodeInfo *pNode = &pCfg->nodeInfo[0];
86✔
251
  pNode->nodePort = tsServerPort;
86✔
252
  tstrncpy(pNode->nodeFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
86✔
253
  bool ret1 = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
86✔
254
  vInfo("vgId:%d, ep:%s:%u dnode:%d", pReq->srcVgId, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
86!
255

256
  info.config.syncCfg = *pCfg;
86✔
257

258
  ret = vnodeSaveInfo(dir, &info);
86✔
259
  if (ret < 0) {
86!
260
    vError("vgId:%d, failed to save vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
261
    return ret;
×
262
  }
263

264
  ret = vnodeCommitInfo(dir);
86✔
265
  if (ret < 0) {
86!
266
    vError("vgId:%d, failed to commit vnode config since %s", pReq->dstVgId, tstrerror(terrno));
×
267
    return ret;
×
268
  }
269

270
  vInfo("vgId:%d, rename %s to %s", pReq->dstVgId, srcPath, dstPath);
86!
271
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
86✔
272
  if (ret < 0) {
86!
273
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", pReq->dstVgId, srcPath, dstPath,
×
274
           tstrerror(terrno));
275
    return ret;
×
276
  }
277

278
  vInfo("vgId:%d, vnode hashrange is altered", info.config.vgId);
86!
279
  return 0;
86✔
280
}
281

282
int32_t vnodeRestoreVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
×
283
                             int32_t diskPrimary, STfs *pTfs) {
284
  SVnodeInfo info = {0};
×
285
  char       dir[TSDB_FILENAME_LEN] = {0};
×
286
  int32_t    code = 0;
×
287

288
  vnodeGetPrimaryDir(dstPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
289
  if (vnodeLoadInfo(dir, &info) == 0) {
×
290
    if (info.config.vgId != dstVgId) {
×
291
      vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId);
×
292
      return TSDB_CODE_FAILED;
×
293
    }
294
    return dstVgId;
×
295
  }
296

297
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
×
298
  if ((code = vnodeLoadInfo(dir, &info)) < 0) {
×
299
    vError("vgId:%d, failed to read vnode config from %s since %s", srcVgId, srcPath, tstrerror(terrno));
×
300
    return code;
×
301
  }
302

303
  if (info.config.vgId == srcVgId) {
×
304
    vInfo("vgId:%d, rollback alter hashrange", srcVgId);
×
305
    return srcVgId;
×
306
  } else 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

311
  vInfo("vgId:%d, rename %s to %s", dstVgId, srcPath, dstPath);
×
312
  if (vnodeRenameVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs) < 0) {
×
313
    vError("vgId:%d, failed to rename vnode from %s to %s since %s", dstVgId, srcPath, dstPath, tstrerror(terrno));
×
314
    return TSDB_CODE_FAILED;
×
315
  }
316

317
  return dstVgId;
×
318
}
319

320
void vnodeDestroy(int32_t vgId, const char *path, STfs *pTfs, int32_t nodeId) {
5,126✔
321
  vInfo("path:%s is removed while destroy vnode", path);
5,126!
322
  if (tfsRmdir(pTfs, path) < 0) {
5,126!
323
    vError("failed to remove path:%s since %s", path, tstrerror(terrno));
×
324
  }
325

326
  // int32_t nlevel = tfsGetLevel(pTfs);
327
  if (nodeId > 0 && vgId > 0 /*&& nlevel > 1*/ && tsS3Enabled) {
5,126!
328
    char vnode_prefix[TSDB_FILENAME_LEN];
UNCOV
329
    snprintf(vnode_prefix, TSDB_FILENAME_LEN, "%d/v%df", nodeId, vgId);
×
UNCOV
330
    tcsDeleteObjectsByPrefix(vnode_prefix);
×
331
  }
332
}
5,126✔
333

334
static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) {
13,539✔
335
  int32_t ndisk = 1;
13,539✔
336
  if (pTfs) {
13,539!
337
    ndisk = tfsGetDisksAtLevel(pTfs, 0);
13,539✔
338
  }
339
  if (diskPrimary < 0 || diskPrimary >= ndisk) {
13,537!
340
    vError("disk:%d is unavailable from the %d disks mounted at level 0", diskPrimary, ndisk);
×
341
    return terrno = TSDB_CODE_FS_INVLD_CFG;
×
342
  }
343
  return 0;
13,537✔
344
}
345

346
SVnode *vnodeOpen(const char *path, int32_t diskPrimary, STfs *pTfs, SMsgCb msgCb, bool force) {
13,531✔
347
  SVnode    *pVnode = NULL;
13,531✔
348
  SVnodeInfo info = {0};
13,531✔
349
  char       dir[TSDB_FILENAME_LEN] = {0};
13,531✔
350
  char       tdir[TSDB_FILENAME_LEN * 2] = {0};
13,531✔
351
  int32_t    ret = 0;
13,531✔
352
  terrno = TSDB_CODE_SUCCESS;
13,531✔
353

354
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
13,539!
355
    vError("failed to open vnode from %s since %s. diskPrimary:%d", path, terrstr(), diskPrimary);
×
356
    return NULL;
×
357
  }
358
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
13,537✔
359

360
  info.config = vnodeCfgDefault;
13,538✔
361

362
  // load vnode info
363
  ret = vnodeLoadInfo(dir, &info);
13,538✔
364
  if (ret < 0) {
13,538!
365
    vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
×
366
    terrno = TSDB_CODE_NEED_RETRY;
×
367
    return NULL;
×
368
  }
369

370
  if (vnodeMkDir(pTfs, path)) {
13,538✔
371
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", info.config.vgId, strerror(errno), path);
11!
372
    return NULL;
×
373
  }
374
  // save vnode info on dnode ep changed
375
  bool      updated = false;
13,508✔
376
  SSyncCfg *pCfg = &info.config.syncCfg;
13,508✔
377
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
33,490✔
378
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
19,949✔
379
    if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
19,949!
380
      updated = true;
×
381
    }
382
  }
383
  if (updated) {
13,541!
384
    vInfo("vgId:%d, save vnode info since dnode info changed", info.config.vgId);
×
385
    if (vnodeSaveInfo(dir, &info) < 0) {
×
386
      vError("vgId:%d, failed to save vnode info since %s", info.config.vgId, tstrerror(terrno));
×
387
    }
388

389
    if (vnodeCommitInfo(dir) < 0) {
×
390
      vError("vgId:%d, failed to commit vnode info since %s", info.config.vgId, tstrerror(terrno));
×
391
    }
392
  }
393

394
  // create handle
395
  pVnode = taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
13,541✔
396
  if (pVnode == NULL) {
13,507!
397
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
398
    vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
×
399
    return NULL;
×
400
  }
401

402
  pVnode->path = (char *)&pVnode[1];
13,507✔
403
  strcpy(pVnode->path, path);
13,507✔
404
  pVnode->config = info.config;
13,507✔
405
  pVnode->state.committed = info.state.committed;
13,507✔
406
  pVnode->state.commitTerm = info.state.commitTerm;
13,507✔
407
  pVnode->state.commitID = info.state.commitID;
13,507✔
408
  pVnode->state.applied = info.state.committed;
13,507✔
409
  pVnode->state.applyTerm = info.state.commitTerm;
13,507✔
410
  pVnode->pTfs = pTfs;
13,507✔
411
  pVnode->diskPrimary = diskPrimary;
13,507✔
412
  pVnode->msgCb = msgCb;
13,507✔
413
  (void)taosThreadMutexInit(&pVnode->lock, NULL);
13,507✔
414
  pVnode->blocked = false;
13,489✔
415
  pVnode->disableWrite = false;
13,489✔
416

417
  if (tsem_init(&pVnode->syncSem, 0, 0) != 0) {
13,489!
418
    vError("vgId:%d, failed to init semaphore", TD_VID(pVnode));
×
419
    goto _err;
×
420
  }
421
  (void)taosThreadMutexInit(&pVnode->mutex, NULL);
13,512✔
422
  (void)taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
13,499✔
423

424
  if (vnodeAChannelInit(1, &pVnode->commitChannel) != 0) {
13,493!
425
    vError("vgId:%d, failed to init commit channel", TD_VID(pVnode));
×
426
    goto _err;
×
427
  }
428

429
  int8_t rollback = vnodeShouldRollback(pVnode);
13,539✔
430

431
  // open buffer pool
432
  if (vnodeOpenBufPool(pVnode) < 0) {
13,515!
433
    vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
434
    goto _err;
×
435
  }
436

437
  // open meta
438
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
13,539!
439
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
440
    goto _err;
×
441
  }
442

443
  if (metaUpgrade(pVnode, &pVnode->pMeta) < 0) {
13,537!
444
    vError("vgId:%d, failed to upgrade meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
445
  }
446

447
  // open tsdb
448
  if (!VND_IS_RSMA(pVnode) && tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback, force) < 0) {
13,529!
449
    vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
×
450
    goto _err;
×
451
  }
452

453
  // open wal
454
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
13,536✔
455
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
13,536✔
456
  TAOS_UNUSED(ret);
457

458
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
13,534✔
459
  if (pVnode->pWal == NULL) {
13,539!
460
    vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
×
461
    goto _err;
×
462
  }
463

464
  // open tq
465
  sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
13,539✔
466
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
13,539✔
467
  TAOS_UNUSED(ret);
468

469
  // open query
470
  if (vnodeQueryOpen(pVnode)) {
13,537!
471
    vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
×
472
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
473
    goto _err;
×
474
  }
475

476
  // sma required the tq is initialized before the vnode open
477
  if (tqOpen(tdir, pVnode)) {
13,539!
478
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
×
479
    goto _err;
×
480
  }
481

482
  // open sma
483
  if (smaOpen(pVnode, rollback, force)) {
13,535!
484
    vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
485
    goto _err;
×
486
  }
487

488
  // vnode begin
489
  if (vnodeBegin(pVnode) < 0) {
13,535!
490
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
×
491
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
492
    goto _err;
×
493
  }
494

495
  // open sync
496
  vInfo("vgId:%d, start to open sync, changeVersion:%d", TD_VID(pVnode), info.config.syncCfg.changeVersion);
13,538!
497
  if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
13,539!
498
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
499
    goto _err;
×
500
  }
501

502
  if (rollback) {
13,539!
503
    vnodeRollback(pVnode);
×
504
  }
505

506
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
13,539✔
507
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
13,539✔
508
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
13,539✔
509

510
  return pVnode;
13,539✔
511

512
_err:
×
513
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
×
514
  if (pVnode->pTq) tqClose(pVnode->pTq);
×
515
  if (pVnode->pWal) walClose(pVnode->pWal);
×
516
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
×
517
  if (pVnode->pSma) smaClose(pVnode->pSma);
×
518
  if (pVnode->pMeta) metaClose(&pVnode->pMeta);
×
519
  if (pVnode->freeList) vnodeCloseBufPool(pVnode);
×
520

521
  taosMemoryFree(pVnode);
×
522
  return NULL;
×
523
}
524

525
void vnodePreClose(SVnode *pVnode) {
13,535✔
526
  vnodeSyncPreClose(pVnode);
13,535✔
527
  vnodeQueryPreClose(pVnode);
13,536✔
528
}
13,535✔
529

530
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
13,539✔
531

532
void vnodeClose(SVnode *pVnode) {
13,538✔
533
  if (pVnode) {
13,538!
534
    vnodeAWait(&pVnode->commitTask);
13,538✔
535
    if (vnodeAChannelDestroy(&pVnode->commitChannel, true) != 0) {
13,539!
536
      vError("vgId:%d, failed to destroy commit channel", TD_VID(pVnode));
×
537
    }
538

539
    vnodeSyncClose(pVnode);
13,539✔
540
    vnodeQueryClose(pVnode);
13,538✔
541
    tqClose(pVnode->pTq);
13,539✔
542
    walClose(pVnode->pWal);
13,539✔
543
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
13,539!
544
    smaClose(pVnode->pSma);
13,536✔
545
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
13,531!
546
    vnodeCloseBufPool(pVnode);
13,538✔
547

548
    // destroy handle
549
    if (tsem_destroy(&pVnode->syncSem) != 0) {
13,539!
550
      vError("vgId:%d, failed to destroy semaphore", TD_VID(pVnode));
×
551
    }
552
    (void)taosThreadCondDestroy(&pVnode->poolNotEmpty);
13,539✔
553
    (void)taosThreadMutexDestroy(&pVnode->mutex);
13,539✔
554
    (void)taosThreadMutexDestroy(&pVnode->lock);
13,539✔
555
    taosMemoryFree(pVnode);
13,539✔
556
  }
557
}
13,539✔
558

559
// start the sync timer after the queue is ready
560
int32_t vnodeStart(SVnode *pVnode) {
13,539✔
561
  if (pVnode == NULL) {
13,539!
562
    return TSDB_CODE_INVALID_PARA;
×
563
  }
564
  return vnodeSyncStart(pVnode);
13,539✔
565
}
566

567
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
4,694✔
568

569
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
4,694✔
570

571
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
18✔
572
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
18✔
573

574
void vnodeStop(SVnode *pVnode) {}
×
575

576
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