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

taosdata / TDengine / #3589

24 Jan 2025 08:57AM UTC coverage: 63.191% (-0.4%) from 63.555%
#3589

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

140478 of 285630 branches covered (49.18%)

Branch coverage included in aggregate %.

218744 of 282844 relevant lines covered (77.34%)

19421325.38 hits per line

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

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

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

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

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

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

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

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

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

90
  vInfo("vgId:%d, vnode is created", info.config.vgId);
9,329✔
91
  return 0;
9,329✔
92
}
93

94
bool vnodeShouldRemoveWal(SVnode *pVnode) { return pVnode->config.walCfg.clearFiles == 1; }
1,310✔
95

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

101
  vnodeGetPrimaryDir(path, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
1,310✔
102

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

109
  SSyncCfg *pCfg = &info.config.syncCfg;
1,310✔
110

111
  pCfg->replicaNum = 0;
1,310✔
112
  pCfg->totalReplicaNum = 0;
1,310✔
113
  memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
1,310✔
114

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

144
  if (info.config.walCfg.clearFiles) {
1,310!
145
    info.config.walCfg.clearFiles = 0;
×
146

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

150
  vInfo("vgId:%d, save config while alter, replicas:%d totalReplicas:%d selfIndex:%d changeVersion:%d", pReq->vgId,
1,310!
151
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
152

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

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

166
  vInfo("vgId:%d, vnode config is saved", info.config.vgId);
1,310!
167
  return 0;
1,310✔
168
}
169

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

176
int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId,
42✔
177
                            int32_t diskPrimary, STfs *pTfs) {
178
  int32_t ret = 0;
42✔
179

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

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

194
  while (1) {
272✔
195
    const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
314✔
196
    if (tsdbFile == NULL) break;
314✔
197
    if (tsdbFile->rname[0] == '\0') continue;
356!
198
    tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
272✔
199

200
    char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
272✔
201
    if (tsdbFilePrefixPos == NULL) continue;
272✔
202

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

211
    if (tsdbFileVgId == srcVgId) {
188!
212
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
188✔
213

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

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

227
  tfsClosedir(tsdbDir);
42✔
228

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

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

243
  vnodeGetPrimaryDir(srcPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN);
42✔
244

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

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

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

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

273
  info.config.syncCfg = *pCfg;
42✔
274

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

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

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

295
  vInfo("vgId:%d, vnode hashrange is altered", info.config.vgId);
42!
296
  return 0;
42✔
297
}
298

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

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

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

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

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

334
  return dstVgId;
×
335
}
336

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

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

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

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

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

377
  info.config = vnodeCfgDefault;
11,703✔
378

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

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

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

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

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

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

442
  int8_t rollback = vnodeShouldRollback(pVnode);
11,659✔
443

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

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

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

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

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

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

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

487
  // init handle map for stream event notification
488
  ret = tqInitNotifyHandleMap(&pVnode->pNotifyHandleMap);
11,704✔
489
  if (ret != TSDB_CODE_SUCCESS) {
11,701!
490
    vError("vgId:%d, failed to init StreamNotifyHandleMap", TD_VID(pVnode));
×
491
    terrno = ret;
×
492
    goto _err;
×
493
  }
494

495
  // open query
496
  vInfo("vgId:%d, start to open vnode query", TD_VID(pVnode));
11,701✔
497
  if (vnodeQueryOpen(pVnode)) {
11,705!
498
    vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
×
499
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
500
    goto _err;
×
501
  }
502

503
  // sma required the tq is initialized before the vnode open
504
  vInfo("vgId:%d, start to open vnode tq", TD_VID(pVnode));
11,705✔
505
  if (tqOpen(tdir, pVnode)) {
11,706!
506
    vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
×
507
    goto _err;
×
508
  }
509

510
  // open sma
511
  vInfo("vgId:%d, start to open vnode sma", TD_VID(pVnode));
11,700✔
512
  if (smaOpen(pVnode, rollback, force)) {
11,705!
513
    vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
514
    goto _err;
×
515
  }
516

517
  // vnode begin
518
  vInfo("vgId:%d, start to begin vnode", TD_VID(pVnode));
11,705✔
519
  if (vnodeBegin(pVnode) < 0) {
11,705!
520
    vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
×
521
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
522
    goto _err;
×
523
  }
524

525
  // open sync
526
  vInfo("vgId:%d, start to open sync, changeVersion:%d", TD_VID(pVnode), info.config.syncCfg.changeVersion);
11,704✔
527
  if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
11,705!
528
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
529
    goto _err;
×
530
  }
531

532
  if (rollback) {
11,705!
533
    vnodeRollback(pVnode);
×
534
  }
535

536
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
11,705✔
537
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
11,705✔
538
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
11,705✔
539

540
  return pVnode;
11,705✔
541

542
_err:
×
543
  if (pVnode->pQuery) vnodeQueryClose(pVnode);
×
544
  if (pVnode->pTq) tqClose(pVnode->pTq);
×
545
  if (pVnode->pWal) walClose(pVnode->pWal);
×
546
  if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
×
547
  if (pVnode->pSma) smaClose(pVnode->pSma);
×
548
  if (pVnode->pMeta) metaClose(&pVnode->pMeta);
×
549
  if (pVnode->freeList) vnodeCloseBufPool(pVnode);
×
550

551
  taosMemoryFree(pVnode);
×
552
  return NULL;
×
553
}
554

555
void vnodePreClose(SVnode *pVnode) {
11,705✔
556
  vnodeSyncPreClose(pVnode);
11,705✔
557
  vnodeQueryPreClose(pVnode);
11,705✔
558
}
11,700✔
559

560
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
11,705✔
561

562
void vnodeClose(SVnode *pVnode) {
11,703✔
563
  if (pVnode) {
11,703!
564
    vnodeAWait(&pVnode->commitTask);
11,703✔
565
    vnodeSyncClose(pVnode);
11,703✔
566
    vnodeQueryClose(pVnode);
11,702✔
567
    tqDestroyNotifyHandleMap(&pVnode->pNotifyHandleMap);
11,705✔
568
    tqClose(pVnode->pTq);
11,705✔
569
    walClose(pVnode->pWal);
11,704✔
570
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
11,705!
571
    smaClose(pVnode->pSma);
11,701✔
572
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
11,704!
573
    vnodeCloseBufPool(pVnode);
11,703✔
574

575
    // destroy handle
576
    if (tsem_destroy(&pVnode->syncSem) != 0) {
11,705!
577
      vError("vgId:%d, failed to destroy semaphore", TD_VID(pVnode));
×
578
    }
579
    (void)taosThreadCondDestroy(&pVnode->poolNotEmpty);
11,705✔
580
    (void)taosThreadMutexDestroy(&pVnode->mutex);
11,705✔
581
    (void)taosThreadMutexDestroy(&pVnode->lock);
11,705✔
582
    taosMemoryFree(pVnode);
11,705!
583
  }
584
}
11,705✔
585

586
// start the sync timer after the queue is ready
587
int32_t vnodeStart(SVnode *pVnode) {
11,705✔
588
  if (pVnode == NULL) {
11,705!
589
    return TSDB_CODE_INVALID_PARA;
×
590
  }
591
  return vnodeSyncStart(pVnode);
11,705✔
592
}
593

594
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
4,200✔
595

596
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
4,200✔
597

598
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
34✔
599
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
35✔
600

601
void vnodeStop(SVnode *pVnode) {}
×
602

603
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