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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

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

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

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

45
  // check config
46
  if ((code = vnodeCheckCfg(pCfg)) < 0) {
10,961!
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,012✔
57

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

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

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

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

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

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

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

105
  for (int i = 0; i < pReq->replica; ++i) {
5,329✔
106
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
3,913✔
107
    pNode->nodeId = pReq->replicas[i].id;
3,913✔
108
    pNode->nodePort = pReq->replicas[i].port;
3,913✔
109
    tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
3,913✔
110
    pNode->nodeRole = TAOS_SYNC_ROLE_VOTER;
3,913✔
111
    bool ret = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
3,913✔
112
    vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
3,913!
113
    pCfg->replicaNum++;
3,913✔
114
  }
115
  if (pReq->selfIndex != -1) {
1,416!
116
    pCfg->myIndex = pReq->selfIndex;
1,416✔
117
  }
118
  for (int i = pCfg->replicaNum; i < pReq->replica + pReq->learnerReplica; ++i) {
1,753✔
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,416✔
129
  if (pReq->learnerSelfIndex != -1) {
1,416!
130
    pCfg->myIndex = pReq->replica + pReq->learnerSelfIndex;
×
131
  }
132
  pCfg->changeVersion = pReq->changeVersion;
1,416✔
133

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

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

160
static int32_t vnodeVgroupIdLen(int32_t vgId) {
351✔
161
  char tmp[TSDB_FILENAME_LEN];
162
  sprintf(tmp, "%d", vgId);
351✔
163
  return strlen(tmp);
351✔
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) {
523✔
185
    const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
609✔
186
    if (tsdbFile == NULL) break;
609✔
187
    if (tsdbFile->rname[0] == '\0') continue;
523!
188
    tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
523✔
189

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

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

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

201
      ret = tfsRename(pTfs, diskPrimary, tsdbFile->rname, newRname);
351✔
202
      if (ret != 0) {
351!
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,111✔
321
  vInfo("path:%s is removed while destroy vnode", path);
5,111!
322
  if (tfsRmdir(pTfs, path) < 0) {
5,111!
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,111!
328
    char vnode_prefix[TSDB_FILENAME_LEN];
329
    snprintf(vnode_prefix, TSDB_FILENAME_LEN, "%d/v%df", nodeId, vgId);
5✔
330
    tcsDeleteObjectsByPrefix(vnode_prefix);
5✔
331
  }
332
}
5,111✔
333

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

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

354
  if (vnodeCheckDisk(diskPrimary, pTfs)) {
13,558!
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,558✔
359

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

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

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

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

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

431
  // open buffer pool
432
  if (vnodeOpenBufPool(pVnode) < 0) {
13,534!
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,558!
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,555!
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,554!
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,557✔
455
  ret = taosRealPath(tdir, NULL, sizeof(tdir));
13,557✔
456
  TAOS_UNUSED(ret);
457

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

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

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

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

510
  return pVnode;
13,559✔
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,553✔
526
  vnodeSyncPreClose(pVnode);
13,553✔
527
  vnodeQueryPreClose(pVnode);
13,558✔
528
}
13,548✔
529

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

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

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

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

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

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

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

571
int32_t vnodeUpdateArbTerm(SVnode *pVnode, int64_t arbTerm) { return syncUpdateArbTerm(pVnode->sync, arbTerm); }
12✔
572
int32_t vnodeGetArbToken(SVnode *pVnode, char *outToken) { return syncGetArbToken(pVnode->sync, outToken); }
12✔
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