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

taosdata / TDengine / #3720

26 Mar 2025 06:20AM UTC coverage: 30.242% (-31.7%) from 61.936%
#3720

push

travis-ci

web-flow
feat(taosBenchmark): supports decimal data type (#30456)

* feat: taosBenchmark supports decimal data type

* build: decimal script not use pytest.sh

* fix: fix typo for decimal script

* test: insertBasic.py debug

71234 of 313946 branches covered (22.69%)

Branch coverage included in aggregate %.

38 of 423 new or added lines in 8 files covered. (8.98%)

120240 existing lines in 447 files now uncovered.

118188 of 312400 relevant lines covered (37.83%)

1450220.33 hits per line

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

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

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

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

46
  // check config
47
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
109!
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)) {
108!
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);
109✔
58

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

68
  SVnodeInfo oldInfo = {0};
109✔
69
  oldInfo.config = vnodeCfgDefault;
109✔
70
  if (vnodeLoadInfo(dir, &oldInfo) == 0) {
109!
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);
109✔
85
  if ((code = vnodeSaveInfo(dir, &info)) < 0 || (code = vnodeCommitInfo(dir)) < 0) {
109!
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);
109✔
91
  return 0;
109✔
92
}
93

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

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

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

103
  ret = vnodeLoadInfo(dir, &info);
462✔
104
  if (ret < 0) {
462!
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;
462✔
110

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

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

144
  if (info.config.walCfg.clearFiles) {
462!
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,
462!
151
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
152

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

160
  ret = vnodeCommitInfo(dir);
462✔
161
  if (ret < 0) {
462!
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);
462!
167
  return 0;
462✔
168
}
169

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

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

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

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

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

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

203
    int32_t tsdbFileVgId = 0;
8✔
204
    ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
8✔
205
    if (ret != 0) {
8!
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) {
8!
212
      char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);
8✔
213

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

218
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
8✔
219
      if (ret != 0) {
8!
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);
1✔
228

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

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

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

245
  ret = vnodeLoadInfo(dir, &info);
1✔
246
  if (ret < 0) {
1!
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,
1!
252
        pReq->hashBegin, pReq->hashEnd);
253
  info.config.vgId = pReq->dstVgId;
1✔
254
  info.config.hashBegin = pReq->hashBegin;
1✔
255
  info.config.hashEnd = pReq->hashEnd;
1✔
256
  info.config.hashChange = true;
1✔
257
  info.config.walCfg.vgId = pReq->dstVgId;
1✔
258
  info.config.syncCfg.changeVersion = pReq->changeVersion;
1✔
259

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

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

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

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

281
  ret = vnodeCommitInfo(dir);
1✔
282
  if (ret < 0) {
1!
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);
1!
288
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
1✔
289
  if (ret < 0) {
1!
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);
1!
296
  return 0;
1✔
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) {
62✔
338
  vInfo("path:%s is removed while destroy vnode", path);
62✔
339
  if (tfsRmdir(pTfs, path) < 0) {
62!
340
    vError("failed to remove path:%s since %s", path, tstrerror(terrno));
×
341
  }
342

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

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

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

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

379
  info.config = vnodeCfgDefault;
592✔
380

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

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

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

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

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

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

444
  int8_t rollback = vnodeShouldRollback(pVnode);
592✔
445

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

453
  // open meta
454
  (void)taosThreadRwlockInit(&pVnode->metaRWLock, NULL);
592✔
455
  vInfo("vgId:%d, start to open vnode meta", TD_VID(pVnode));
592✔
456
  if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
592!
457
    vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
458
    goto _err;
×
459
  }
460

461
  vInfo("vgId:%d, start to upgrade meta", TD_VID(pVnode));
592✔
462
  if (metaUpgrade(pVnode, &pVnode->pMeta) < 0) {
592!
463
    vError("vgId:%d, failed to upgrade meta since %s", TD_VID(pVnode), tstrerror(terrno));
×
464
  }
465

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

473
  // open wal
474
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
592✔
475
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
592✔
476
  TAOS_UNUSED(ret);
477

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

485
  // open tq
486
  (void)tsnprintf(tdir, sizeof(tdir), "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
592✔
487
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
592✔
488
  TAOS_UNUSED(ret);
489

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

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

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

513
  // open sma
514
  vInfo("vgId:%d, start to open vnode sma", TD_VID(pVnode));
592✔
515
  if (smaOpen(pVnode, rollback, force)) {
592!
516
    vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
517
    goto _err;
×
518
  }
519

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

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

535
  if (rollback) {
592!
536
    vnodeRollback(pVnode);
×
537
  }
538

539
  snprintf(pVnode->monitor.strClusterId, TSDB_CLUSTER_ID_LEN, "%" PRId64, pVnode->config.syncCfg.nodeInfo[0].clusterId);
592✔
540
  snprintf(pVnode->monitor.strDnodeId, TSDB_NODE_ID_LEN, "%" PRId32, pVnode->config.syncCfg.nodeInfo[0].nodeId);
592✔
541
  snprintf(pVnode->monitor.strVgId, TSDB_VGROUP_ID_LEN, "%" PRId32, pVnode->config.vgId);
592✔
542

543
  return pVnode;
592✔
544

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

554
  (void)taosThreadRwlockDestroy(&pVnode->metaRWLock);
×
555
  taosMemoryFree(pVnode);
×
556
  return NULL;
×
557
}
558

559
void vnodePreClose(SVnode *pVnode) {
592✔
560
  vnodeSyncPreClose(pVnode);
592✔
561
  vnodeQueryPreClose(pVnode);
592✔
562
}
592✔
563

564
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
592✔
565

566
void vnodeClose(SVnode *pVnode) {
592✔
567
  if (pVnode) {
592!
568
    vnodeAWait(&pVnode->commitTask);
592✔
569
    vnodeSyncClose(pVnode);
592✔
570
    vnodeQueryClose(pVnode);
592✔
571
    tqDestroyNotifyHandleMap(&pVnode->pNotifyHandleMap);
592✔
572
    tqClose(pVnode->pTq);
592✔
573
    walClose(pVnode->pWal);
592✔
574
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
592!
575
    smaClose(pVnode->pSma);
592✔
576
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
591!
577
    vnodeCloseBufPool(pVnode);
591✔
578

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

590
// start the sync timer after the queue is ready
591
int32_t vnodeStart(SVnode *pVnode) {
592✔
592
  if (pVnode == NULL) {
592!
593
    return TSDB_CODE_INVALID_PARA;
×
594
  }
595
  return vnodeSyncStart(pVnode);
592✔
596
}
597

598
int32_t vnodeIsCatchUp(SVnode *pVnode) { return syncIsCatchUp(pVnode->sync); }
1,124✔
599

600
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
1,124✔
601

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

605
void vnodeStop(SVnode *pVnode) {}
×
606

607
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