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

taosdata / TDengine / #3530

16 Nov 2024 07:44AM UTC coverage: 60.219% (-0.7%) from 60.888%
#3530

push

travis-ci

web-flow
Update 03-ad.md

118417 of 252124 branches covered (46.97%)

Branch coverage included in aggregate %.

198982 of 274951 relevant lines covered (72.37%)

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

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

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

45
  // check config
46
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
9,863!
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)) {
9,853!
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);
9,864✔
57

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

67
  SVnodeInfo oldInfo = {0};
9,864✔
68
  oldInfo.config = vnodeCfgDefault;
9,864✔
69
  if (vnodeLoadInfo(dir, &oldInfo) == 0) {
9,864!
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);
9,863!
75
  if ((code = vnodeSaveInfo(dir, &info)) < 0 || (code = vnodeCommitInfo(dir)) < 0) {
9,864!
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);
9,864!
81
  return 0;
9,864✔
82
}
83

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

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

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

93
  ret = vnodeLoadInfo(dir, &info);
1,340✔
94
  if (ret < 0) {
1,340!
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,340✔
100

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

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

134
  if (info.config.walCfg.clearFiles) {
1,340!
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,340!
141
        pCfg->replicaNum, pCfg->totalReplicaNum, pCfg->myIndex, pCfg->changeVersion);
142

143
  info.config.syncCfg = *pCfg;
1,340✔
144
  ret = vnodeSaveInfo(dir, &info);
1,340✔
145
  if (ret < 0) {
1,340!
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,340✔
151
  if (ret < 0) {
1,340!
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,340!
157
  return 0;
1,340✔
158
}
159

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

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

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

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

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

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

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

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

201
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
186✔
202
      if (ret != 0) {
186!
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);
74✔
211

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

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

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

228
  ret = vnodeLoadInfo(dir, &info);
74✔
229
  if (ret < 0) {
74!
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,
74!
235
        pReq->hashBegin, pReq->hashEnd);
236
  info.config.vgId = pReq->dstVgId;
74✔
237
  info.config.hashBegin = pReq->hashBegin;
74✔
238
  info.config.hashEnd = pReq->hashEnd;
74✔
239
  info.config.hashChange = true;
74✔
240
  info.config.walCfg.vgId = pReq->dstVgId;
74✔
241
  info.config.syncCfg.changeVersion = pReq->changeVersion;
74✔
242

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

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

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

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

264
  ret = vnodeCommitInfo(dir);
74✔
265
  if (ret < 0) {
74!
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);
74!
271
  ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, diskPrimary, pTfs);
74✔
272
  if (ret < 0) {
74!
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);
74!
279
  return 0;
74✔
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) {
4,038✔
321
  vInfo("path:%s is removed while destroy vnode", path);
4,038!
322
  if (tfsRmdir(pTfs, path) < 0) {
4,038!
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) {
4,038!
328
    char vnode_prefix[TSDB_FILENAME_LEN];
329
    snprintf(vnode_prefix, TSDB_FILENAME_LEN, "%d/v%df", nodeId, vgId);
×
330
    tcsDeleteObjectsByPrefix(vnode_prefix);
×
331
  }
332
}
4,038✔
333

334
static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) {
12,270✔
335
  int32_t ndisk = 1;
12,270✔
336
  if (pTfs) {
12,270!
337
    ndisk = tfsGetDisksAtLevel(pTfs, 0);
12,270✔
338
  }
339
  if (diskPrimary < 0 || diskPrimary >= ndisk) {
12,269!
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;
12,270✔
344
}
345

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

354
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
12,270!
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);
12,269✔
359

360
  info.config = vnodeCfgDefault;
12,269✔
361

362
  // load vnode info
363
  ret = vnodeLoadInfo(dir, &info);
12,269✔
364
  if (ret < 0) {
12,267!
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)) {
12,267✔
371
    vError("vgId:%d, failed to prepare vnode dir since %s, path: %s", info.config.vgId, strerror(errno), path);
10!
372
    return NULL;
×
373
  }
374
  // save vnode info on dnode ep changed
375
  bool      updated = false;
12,240✔
376
  SSyncCfg *pCfg = &info.config.syncCfg;
12,240✔
377
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
30,839✔
378
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
18,579✔
379
    if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
18,579!
380
      updated = true;
×
381
    }
382
  }
383
  if (updated) {
12,260!
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);
12,260✔
396
  if (pVnode == NULL) {
12,238!
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];
12,238✔
403
  strcpy(pVnode->path, path);
12,238✔
404
  pVnode->config = info.config;
12,238✔
405
  pVnode->state.committed = info.state.committed;
12,238✔
406
  pVnode->state.commitTerm = info.state.commitTerm;
12,238✔
407
  pVnode->state.commitID = info.state.commitID;
12,238✔
408
  pVnode->state.applied = info.state.committed;
12,238✔
409
  pVnode->state.applyTerm = info.state.commitTerm;
12,238✔
410
  pVnode->pTfs = pTfs;
12,238✔
411
  pVnode->diskPrimary = diskPrimary;
12,238✔
412
  pVnode->msgCb = msgCb;
12,238✔
413
  (void)taosThreadMutexInit(&pVnode->lock, NULL);
12,238✔
414
  pVnode->blocked = false;
12,250✔
415
  pVnode->disableWrite = false;
12,250✔
416

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

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

429
  int8_t rollback = vnodeShouldRollback(pVnode);
12,271✔
430

431
  // open buffer pool
432
  if (vnodeOpenBufPool(pVnode) < 0) {
12,245!
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) {
12,269!
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) {
12,270!
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) {
12,266!
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);
12,268✔
455
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
12,268✔
456
  TAOS_UNUSED(ret);
457

458
  pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
12,264✔
459
  if (pVnode->pWal == NULL) {
12,271!
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);
12,271✔
466
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
12,271✔
467
  TAOS_UNUSED(ret);
468

469
  // open query
470
  if (vnodeQueryOpen(pVnode)) {
12,271!
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)) {
12,271!
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)) {
12,268!
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) {
12,267!
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);
12,270!
497
  if (vnodeSyncOpen(pVnode, dir, info.config.syncCfg.changeVersion)) {
12,271!
498
    vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
×
499
    goto _err;
×
500
  }
501

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

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

510
  return pVnode;
12,271✔
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) {
12,269✔
526
  vnodeSyncPreClose(pVnode);
12,269✔
527
  vnodeQueryPreClose(pVnode);
12,270✔
528
}
12,264✔
529

530
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
12,271✔
531

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

539
    vnodeSyncClose(pVnode);
12,271✔
540
    vnodeQueryClose(pVnode);
12,268✔
541
    tqClose(pVnode->pTq);
12,271✔
542
    walClose(pVnode->pWal);
12,271✔
543
    if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
12,270!
544
    smaClose(pVnode->pSma);
12,267✔
545
    if (pVnode->pMeta) metaClose(&pVnode->pMeta);
12,258!
546
    vnodeCloseBufPool(pVnode);
12,271✔
547

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

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

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

569
ESyncRole vnodeGetRole(SVnode *pVnode) { return syncGetRole(pVnode->sync); }
3,962✔
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); }
20✔
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