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

taosdata / TDengine / #4906

30 Dec 2025 10:52AM UTC coverage: 65.514% (+0.09%) from 65.423%
#4906

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

4080 existing lines in 123 files now uncovered.

193840 of 295877 relevant lines covered (65.51%)

120444601.14 hits per line

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

77.14
/source/dnode/vnode/src/vnd/vnodeCommit.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 "meta.h"
17
#include "sync.h"
18
#include "tencrypt.h"
19
#include "vnd.h"
20
#include "vnodeInt.h"
21

22
extern int32_t tsdbPreCommit(STsdb *pTsdb);
23
extern int32_t tsdbCommitBegin(STsdb *pTsdb, SCommitInfo *pInfo);
24
extern int32_t tsdbCommitCommit(STsdb *pTsdb);
25
extern int32_t tsdbCommitAbort(STsdb *pTsdb);
26

27
#define VND_INFO_FNAME_TMP "vnode_tmp.json"
28

29
static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData);
30
static int vnodeCommitImpl(SCommitInfo *pInfo);
31

32
#define WAIT_TIME_MILI_SEC 10  // miliseconds
33

34
static int32_t vnodeTryRecycleBufPool(SVnode *pVnode) {
×
35
  int32_t code = 0;
×
36

37
  if (pVnode->onRecycle == NULL) {
×
38
    if (pVnode->recycleHead == NULL) {
×
39
      vDebug("vgId:%d, no recyclable buffer pool", TD_VID(pVnode));
×
40
      goto _exit;
×
41
    } else {
42
      vDebug("vgId:%d, buffer pool %p of id %d on recycle queue, try to recycle", TD_VID(pVnode), pVnode->recycleHead,
×
43
             pVnode->recycleHead->id);
44

45
      pVnode->onRecycle = pVnode->recycleHead;
×
46
      if (pVnode->recycleHead == pVnode->recycleTail) {
×
47
        pVnode->recycleHead = pVnode->recycleTail = NULL;
×
48
      } else {
49
        pVnode->recycleHead = pVnode->recycleHead->recycleNext;
×
50
        pVnode->recycleHead->recyclePrev = NULL;
×
51
      }
52
      pVnode->onRecycle->recycleNext = pVnode->onRecycle->recyclePrev = NULL;
×
53
    }
54
  }
55

56
  code = vnodeBufPoolRecycle(pVnode->onRecycle);
×
57
  if (code) goto _exit;
×
58

59
_exit:
×
60
  if (code) {
×
61
    vError("vgId:%d, %s failed since %s", TD_VID(pVnode), __func__, tstrerror(code));
×
62
  }
63
  return code;
×
64
}
65
static int32_t vnodeGetBufPoolToUse(SVnode *pVnode) {
8,331,518✔
66
  int32_t code = 0;
8,331,518✔
67
  int32_t lino = 0;
8,331,518✔
68

69
  (void)taosThreadMutexLock(&pVnode->mutex);
8,331,518✔
70

71
  int32_t nTry = 0;
8,331,518✔
72
  for (;;) {
73
    ++nTry;
8,331,518✔
74

75
    if (pVnode->freeList) {
8,331,518✔
76
      vDebug("vgId:%d, allocate free buffer pool on %d try, pPool:%p id:%d", TD_VID(pVnode), nTry, pVnode->freeList,
8,331,518✔
77
             pVnode->freeList->id);
78

79
      pVnode->inUse = pVnode->freeList;
8,331,518✔
80
      pVnode->inUse->nRef = 1;
8,331,518✔
81
      pVnode->freeList = pVnode->inUse->freeNext;
8,331,518✔
82
      pVnode->inUse->freeNext = NULL;
8,331,518✔
83
      break;
8,331,518✔
84
    } else {
85
      vDebug("vgId:%d, no free buffer pool on %d try, try to recycle...", TD_VID(pVnode), nTry);
×
86

87
      code = vnodeTryRecycleBufPool(pVnode);
×
88
      TSDB_CHECK_CODE(code, lino, _exit);
×
89

90
      if (pVnode->freeList == NULL) {
×
91
        vDebug("vgId:%d, no free buffer pool on %d try, wait %d ms...", TD_VID(pVnode), nTry, WAIT_TIME_MILI_SEC);
×
92

93
        struct timeval  tv;
×
94
        struct timespec ts;
×
95
        if (taosGetTimeOfDay(&tv) != 0) {
×
96
          continue;
×
97
        }
98
        ts.tv_nsec = tv.tv_usec * 1000 + WAIT_TIME_MILI_SEC * 1000000;
×
99
        if (ts.tv_nsec > 999999999l) {
×
100
          ts.tv_sec = tv.tv_sec + 1;
×
101
          ts.tv_nsec -= 1000000000l;
×
102
        } else {
103
          ts.tv_sec = tv.tv_sec;
×
104
        }
105

106
        code = taosThreadCondTimedWait(&pVnode->poolNotEmpty, &pVnode->mutex, &ts);
×
107
        // ignore timeout error and retry
108
        if (code == TSDB_CODE_TIMEOUT_ERROR) {
×
109
          code = TSDB_CODE_SUCCESS;
×
110
        }
111
        TSDB_CHECK_CODE(code, lino, _exit);
×
112
      }
113
    }
114
  }
115

116
_exit:
8,331,518✔
117
  (void)taosThreadMutexUnlock(&pVnode->mutex);
8,331,518✔
118
  if (code) {
8,331,518✔
119
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
120
  }
121
  return code;
8,331,518✔
122
}
123
int vnodeBegin(SVnode *pVnode) {
8,331,004✔
124
  int32_t code = 0;
8,331,004✔
125
  int32_t lino = 0;
8,331,004✔
126

127
  // alloc buffer pool
128
  code = vnodeGetBufPoolToUse(pVnode);
8,331,004✔
129
  TSDB_CHECK_CODE(code, lino, _exit);
8,331,518✔
130

131
  // begin meta
132
  code = metaBegin(pVnode->pMeta, META_BEGIN_HEAP_BUFFERPOOL);
8,331,518✔
133
  TSDB_CHECK_CODE(code, lino, _exit);
8,329,854✔
134

135
  // begin tsdb
136
  code = tsdbBegin(pVnode->pTsdb);
8,329,854✔
137
  TSDB_CHECK_CODE(code, lino, _exit);
8,328,744✔
138

139
_exit:
8,328,744✔
140
  if (code) {
8,328,744✔
141
    terrno = code;
×
142
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
143
  }
144
  return code;
8,329,535✔
145
}
146

147
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
572,458,189✔
148
  bool diskAvail = osDataSpaceAvailable();
572,458,189✔
149
  bool needCommit = false;
572,456,917✔
150

151
  (void)taosThreadMutexLock(&pVnode->mutex);
572,456,917✔
152
  if (pVnode->inUse && diskAvail) {
572,462,193✔
153
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
575,260,342✔
154
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
2,800,683✔
155
                             pVnode->state.applied - pVnode->state.committed > 4096));
1,223,837✔
156
  }
157
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
572,461,304✔
158
         ", meta changed:%d"
159
         ", state:[%" PRId64 ",%" PRId64 "]",
160
         TD_VID(pVnode), needCommit, diskAvail, pVnode->inUse ? pVnode->inUse->size : 0,
161
         pVnode->inUse ? pVnode->inUse->node.size : 0, pVnode->pMeta->changed, pVnode->state.applied,
162
         pVnode->state.committed);
163
  (void)taosThreadMutexUnlock(&pVnode->mutex);
572,461,304✔
164
  return needCommit;
572,462,053✔
165
}
166

167
int vnodeSaveInfo(const char *dir, const SVnodeInfo *pInfo) {
8,071,370✔
168
  int32_t   code = 0;
8,071,370✔
169
  int32_t   lino;
170
  char      fname[TSDB_FILENAME_LEN];
8,070,786✔
171
  char     *data = NULL;
8,071,370✔
172

173
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
8,071,370✔
174

175
  code = vnodeEncodeInfo(pInfo, &data);
8,071,370✔
176
  TSDB_CHECK_CODE(code, lino, _exit);
8,070,474✔
177

178
  int32_t len = strlen(data);
8,070,474✔
179
  
180
  // Use encrypted write if tsCfgKey is enabled
181
  code = taosWriteCfgFile(fname, data, len);
8,070,474✔
182
  if (code != 0) {
8,062,474✔
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
_exit:
8,062,474✔
187
  if (code) {
8,060,905✔
188
    vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
189
  } else {
190
    vInfo("vgId:%d, vnode info is saved, fname:%s replica:%d selfIndex:%d changeVersion:%d", pInfo->config.vgId, fname,
8,060,905✔
191
          pInfo->config.syncCfg.replicaNum, pInfo->config.syncCfg.myIndex, pInfo->config.syncCfg.changeVersion);
192
  }
193
  taosMemoryFree(data);
8,071,370✔
194
  TAOS_RETURN(code);
8,071,370✔
195
}
196

197
int vnodeCommitInfo(const char *dir) {
8,071,370✔
198
  char fname[TSDB_FILENAME_LEN];
8,070,786✔
199
  char tfname[TSDB_FILENAME_LEN];
8,070,786✔
200

201
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
8,071,370✔
202
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
8,071,370✔
203

204
  int32_t code = taosRenameFile(tfname, fname);
8,071,370✔
205
  if (code < 0) {
8,071,370✔
206
    return code;
×
207
  }
208

209
  vInfo("vnode info is committed, dir:%s", dir);
8,071,370✔
210
  return 0;
8,071,370✔
211
}
212

213
int vnodeLoadInfo(const char *dir, SVnodeInfo *pInfo) {
7,004,272✔
214
  int32_t   code = 0;
7,004,272✔
215
  int32_t   lino;
216
  char      fname[TSDB_FILENAME_LEN];
7,003,526✔
217
  char     *pData = NULL;
7,009,570✔
218
  int32_t   dataLen = 0;
7,009,570✔
219

220
  if (taosWaitCfgKeyLoaded() != 0) {
7,008,940✔
221
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
222
  }
223

224
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
7,008,940✔
225

226
  // Use taosReadCfgFile for automatic decryption support (returns null-terminated string)
227
  code = taosReadCfgFile(fname, &pData, &dataLen);
7,008,940✔
228
  if (code != 0) {
7,009,414✔
229
    TSDB_CHECK_CODE(code, lino, _exit);
2,606,044✔
230
  }
231

232
  // decode info
233
  code = vnodeDecodeInfo(pData, pInfo);
4,403,370✔
234
  TSDB_CHECK_CODE(code, lino, _exit);
4,401,808✔
235

236
  pInfo->config.walCfg.committed = pInfo->state.committed;
4,401,808✔
237
_exit:
7,007,746✔
238
  if (code) {
7,007,995✔
239
    vError("vgId:%d %s failed at %s:%d since %s, file:%s", pInfo->config.vgId, __func__, __FILE__, lino,
2,606,044✔
240
           tstrerror(code), fname);
241
  }
242
  taosMemoryFree(pData);
7,008,203✔
243
  return code;
7,008,117✔
244
}
245

246
static int32_t vnodePrepareCommit(SVnode *pVnode, SCommitInfo *pInfo) {
4,684,155✔
247
  int32_t code = 0;
4,684,155✔
248
  int32_t lino = 0;
4,684,155✔
249
  char    dir[TSDB_FILENAME_LEN] = {0};
4,684,155✔
250
  int64_t lastCommitted = pInfo->info.state.committed;
4,684,683✔
251

252
  // wait last commit task
253
  vnodeAWait(&pVnode->commitTask);
4,684,683✔
254

255
  code = syncNodeGetConfig(pVnode->sync, &pVnode->config.syncCfg);
4,685,211✔
256
  TSDB_CHECK_CODE(code, lino, _exit);
4,684,773✔
257

258
  pVnode->state.commitTerm = pVnode->state.applyTerm;
4,684,773✔
259

260
  pInfo->info.config = pVnode->config;
4,685,211✔
261
  pInfo->info.state.committed = pVnode->state.applied;
4,684,773✔
262
  pInfo->info.state.commitTerm = pVnode->state.applyTerm;
4,684,885✔
263
  pInfo->info.state.commitID = ++pVnode->state.commitID;
4,685,211✔
264
  pInfo->pVnode = pVnode;
4,683,701✔
265
  pInfo->txn = metaGetTxn(pVnode->pMeta);
4,683,701✔
266

267
  // save info
268
  vnodeGetPrimaryPath(pVnode, false, dir, TSDB_FILENAME_LEN);
4,685,211✔
269

270
  vDebug("vgId:%d, save config while prepare commit", TD_VID(pVnode));
4,683,759✔
271
  code = vnodeSaveInfo(dir, &pInfo->info);
4,687,325✔
272
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
273

274
  code = tsdbPreCommit(pVnode->pTsdb);
4,685,211✔
275
  TSDB_CHECK_CODE(code, lino, _exit);
4,684,832✔
276

277
  code = metaPrepareAsyncCommit(pVnode->pMeta);
4,684,832✔
278
  TSDB_CHECK_CODE(code, lino, _exit);
4,682,946✔
279

280
  (void)taosThreadMutexLock(&pVnode->mutex);
4,682,946✔
281
  pVnode->onCommit = pVnode->inUse;
4,683,987✔
282
  pVnode->inUse = NULL;
4,683,576✔
283
  (void)taosThreadMutexUnlock(&pVnode->mutex);
4,680,850✔
284

285
_exit:
4,683,580✔
286
  if (code) {
4,683,580✔
287
    vError("vgId:%d, %s failed at line %d since %s, commit id:%" PRId64, TD_VID(pVnode), __func__, lino,
×
288
           tstrerror(code), pVnode->state.commitID);
289
  } else {
290
    vDebug("vgId:%d, %s done, commit id:%" PRId64, TD_VID(pVnode), __func__, pInfo->info.state.commitID);
4,683,580✔
291
  }
292

293
  return code;
4,688,089✔
294
}
295
static void vnodeReturnBufPool(SVnode *pVnode) {
4,685,211✔
296
  (void)taosThreadMutexLock(&pVnode->mutex);
4,685,211✔
297

298
  SVBufPool *pPool = pVnode->onCommit;
4,685,211✔
299
  int32_t    nRef = atomic_sub_fetch_32(&pPool->nRef, 1);
4,685,211✔
300

301
  pVnode->onCommit = NULL;
4,685,211✔
302
  if (nRef == 0) {
4,685,211✔
303
    vnodeBufPoolAddToFreeList(pPool);
4,657,865✔
304
  } else if (nRef > 0) {
27,346✔
305
    vDebug("vgId:%d, buffer pool %p of id %d is added to recycle queue", TD_VID(pVnode), pPool, pPool->id);
27,346✔
306

307
    if (pVnode->recycleTail == NULL) {
27,346✔
308
      pPool->recyclePrev = pPool->recycleNext = NULL;
27,346✔
309
      pVnode->recycleHead = pVnode->recycleTail = pPool;
27,346✔
310
    } else {
311
      pPool->recyclePrev = pVnode->recycleTail;
×
312
      pPool->recycleNext = NULL;
×
313
      pVnode->recycleTail->recycleNext = pPool;
×
314
      pVnode->recycleTail = pPool;
×
315
    }
316
  } else {
317
    vError("vgId:%d, buffer pool %p of id %d nRef:%d", TD_VID(pVnode), pPool, pPool->id, nRef);
×
318
  }
319

320
  (void)taosThreadMutexUnlock(&pVnode->mutex);
4,685,211✔
321
}
4,685,211✔
322
static int32_t vnodeCommit(void *arg) {
4,685,211✔
323
  int32_t code = 0;
4,685,211✔
324

325
  SCommitInfo *pInfo = (SCommitInfo *)arg;
4,685,211✔
326
  SVnode      *pVnode = pInfo->pVnode;
4,685,211✔
327

328
  // commit
329
  METRICS_TIMING_BLOCK(pVnode->writeMetrics.commit_time, METRIC_LEVEL_HIGH, {
4,685,211✔
330
    if ((code = vnodeCommitImpl(pInfo))) {
331
      vFatal("vgId:%d, failed to commit vnode since %s", TD_VID(pVnode), terrstr());
332
      taosMsleep(100);
333
      exit(EXIT_FAILURE);
334
      goto _exit;
335
    }
336
  });
337

338
  METRICS_UPDATE(pVnode->writeMetrics.commit_count, METRIC_LEVEL_HIGH, 1);
4,685,211✔
339

340
  vnodeReturnBufPool(pVnode);
4,685,211✔
341

342
_exit:
4,685,211✔
343
  taosMemoryFree(arg);
4,685,211✔
344
  return code;
4,685,211✔
345
}
346

347
static void vnodeCommitCancel(void *arg) { taosMemoryFree(arg); }
×
348

349
int vnodeAsyncCommitEx(SVnode *pVnode, bool forceTrim) {
4,683,701✔
350
  int32_t code = 0;
4,683,701✔
351
  int32_t lino = 0;
4,683,701✔
352

353
  SCommitInfo *pInfo = (SCommitInfo *)taosMemoryCalloc(1, sizeof(*pInfo));
4,683,701✔
354
  if (NULL == pInfo) {
4,684,683✔
355
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
356
  }
357

358
  pInfo->forceTrim = forceTrim;
4,684,683✔
359

360
  // prepare to commit
361
  code = vnodePrepareCommit(pVnode, pInfo);
4,684,683✔
362
  TSDB_CHECK_CODE(code, lino, _exit);
4,684,811✔
363

364
  // schedule the task
365
  code = vnodeAsync(COMMIT_TASK_ASYNC, EVA_PRIORITY_HIGH, vnodeCommit, vnodeCommitCancel, pInfo, &pVnode->commitTask);
4,684,811✔
366
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
367

368
_exit:
4,685,211✔
369
  if (code) {
4,685,211✔
370
    taosMemoryFree(pInfo);
×
371
    vError("vgId:%d %s failed at line %d since %s" PRId64, TD_VID(pVnode), __func__, lino, tstrerror(code));
×
372
  } else {
373
    vInfo("vgId:%d, vnode async commit done, commitId:%" PRId64 " term:%" PRId64 " applied:%" PRId64 " forceTrim:%d",
4,685,211✔
374
          TD_VID(pVnode), pVnode->state.commitID, pVnode->state.applyTerm, pVnode->state.applied, forceTrim);
375
  }
376
  return code;
4,685,211✔
377
}
378

379
int vnodeAsyncCommit(SVnode *pVnode) { return vnodeAsyncCommitEx(pVnode, false); }
4,681,061✔
380

381
int32_t vnodeSyncCommit(SVnode *pVnode) {
53,146✔
382
  int32_t lino;
383
  int32_t code = vnodeAsyncCommit(pVnode);
53,146✔
384
  TSDB_CHECK_CODE(code, lino, _exit);
53,146✔
385
  vnodeAWait(&pVnode->commitTask);
53,146✔
386

387
_exit:
53,146✔
388
  if (code) {
53,146✔
389
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
390
  } else {
391
    vInfo("vgId:%d, sync commit end", TD_VID(pVnode));
53,146✔
392
  }
393

394
  return code;
53,146✔
395
}
396

397
static int vnodeCommitImpl(SCommitInfo *pInfo) {
4,685,211✔
398
  int32_t code = 0;
4,685,211✔
399
  int32_t lino = 0;
4,685,211✔
400

401
  char    dir[TSDB_FILENAME_LEN] = {0};
4,685,211✔
402
  SVnode *pVnode = pInfo->pVnode;
4,685,211✔
403

404
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
4,684,683✔
405
        pInfo->info.state.commitID, pInfo->info.state.committed, pInfo->info.state.commitTerm);
406

407
  // persist wal before starting
408
  if ((code = walPersist(pVnode->pWal)) < 0) {
4,685,579✔
409
    vError("vgId:%d, failed to persist wal since %s", TD_VID(pVnode), tstrerror(code));
×
410
    return code;
×
411
  }
412

413
  vnodeGetPrimaryPath(pVnode, false, dir, TSDB_FILENAME_LEN);
4,685,211✔
414

415
  code = syncBeginSnapshot(pVnode->sync, pInfo->info.state.committed);
4,685,211✔
416
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
417

418
  code = tsdbCommitBegin(pVnode->pTsdb, pInfo);
4,685,211✔
419
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
420

421
  if (!TSDB_CACHE_NO(pVnode->config)) {
4,685,211✔
422
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.last_cache_commit_time, METRIC_LEVEL_HIGH,
75,772✔
423
                         { code = tsdbCacheCommit(pVnode->pTsdb); });
424
    METRICS_UPDATE(pVnode->writeMetrics.last_cache_commit_count, METRIC_LEVEL_HIGH, 1);
75,772✔
425
    TSDB_CHECK_CODE(code, lino, _exit);
75,772✔
426
  }
427

428
  // blob storage engine commit
429
  code = bseCommit(pVnode->pBse);
4,685,211✔
430
  // commit info
431
  code = vnodeCommitInfo(dir);
4,685,211✔
432
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
433

434
  code = tsdbCommitCommit(pVnode->pTsdb);
4,685,211✔
435
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
436

437
  code = metaFinishCommit(pVnode->pMeta, pInfo->txn);
4,685,211✔
438
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
439

440
  pVnode->state.committed = pInfo->info.state.committed;
4,685,211✔
441

442
  code = syncEndSnapshot(pVnode->sync, pInfo->forceTrim);
4,685,211✔
443
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
444

445
  code = tqCommitOffset(pVnode->pTq);
4,685,211✔
446
  TSDB_CHECK_CODE(code, lino, _exit);
4,685,211✔
447
  
448
_exit:
4,685,211✔
449
  if (code) {
4,685,211✔
450
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
451
  } else {
452
    vInfo("vgId:%d, commit end", TD_VID(pVnode));
4,685,211✔
453
  }
454
  return code;
4,685,211✔
455
}
456

457
bool vnodeShouldRollback(SVnode *pVnode) {
3,649,967✔
458
  char    tFName[TSDB_FILENAME_LEN] = {0};
3,649,967✔
459
  int32_t offset = 0;
3,649,967✔
460

461
  vnodeGetPrimaryPath(pVnode, false, tFName, TSDB_FILENAME_LEN);
3,649,967✔
462
  offset = strlen(tFName);
3,649,967✔
463
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
3,649,967✔
464

465
  return taosCheckExistFile(tFName);
3,649,967✔
466
}
467

UNCOV
468
void vnodeRollback(SVnode *pVnode) {
×
UNCOV
469
  char    tFName[TSDB_FILENAME_LEN] = {0};
×
UNCOV
470
  int32_t offset = 0;
×
471

UNCOV
472
  vnodeGetPrimaryPath(pVnode, false, tFName, TSDB_FILENAME_LEN);
×
UNCOV
473
  offset = strlen(tFName);
×
UNCOV
474
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
×
475

UNCOV
476
  if (taosRemoveFile(tFName) != 0) {
×
477
    vError("vgId:%d, failed to remove file %s since %s", TD_VID(pVnode), tFName, tstrerror(terrno));
×
478
  }
UNCOV
479
}
×
480

481
static int vnodeEncodeState(const void *pObj, SJson *pJson) {
8,070,888✔
482
  const SVState *pState = (SVState *)pObj;
8,070,888✔
483

484
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit version", pState->committed));
8,070,888✔
485
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID));
8,071,370✔
486
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm));
8,071,370✔
487

488
  return 0;
8,068,961✔
489
}
490

491
static int vnodeDecodeState(const SJson *pJson, void *pObj) {
4,431,074✔
492
  SVState *pState = (SVState *)pObj;
4,431,074✔
493

494
  int32_t code;
495
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
4,431,074✔
496
  if (code) return code;
4,431,074✔
497
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
4,431,074✔
498
  if (code) return code;
4,431,074✔
499
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
4,431,074✔
500
  if (code) return code;
4,431,074✔
501

502
  return 0;
4,431,074✔
503
}
504

505
static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData) {
8,066,114✔
506
  int32_t code = 0;
8,066,114✔
507
  int32_t lino;
508
  SJson  *pJson = NULL;
8,066,114✔
509
  char   *pData = NULL;
8,066,114✔
510

511
  pJson = tjsonCreateObject();
8,066,114✔
512
  if (pJson == NULL) {
8,070,776✔
513
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
514
  }
515

516
  code = tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config);
8,070,776✔
517
  TSDB_CHECK_CODE(code, lino, _exit);
8,071,370✔
518

519
  code = tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state);
8,071,370✔
520
  TSDB_CHECK_CODE(code, lino, _exit);
8,070,737✔
521

522
  pData = tjsonToString(pJson);
8,070,737✔
523
  if (pData == NULL) {
8,070,458✔
524
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
525
  }
526

527
  tjsonDelete(pJson);
8,070,458✔
528

529
_exit:
8,069,960✔
530
  if (code) {
8,069,960✔
531
    tjsonDelete(pJson);
×
532
    *ppData = NULL;
×
533
  } else {
534
    *ppData = pData;
8,069,960✔
535
  }
536
  return code;
8,070,474✔
537
}
538

539
int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
4,427,407✔
540
  int32_t code = 0;
4,427,407✔
541
  int32_t lino;
542
  SJson  *pJson = NULL;
4,427,407✔
543

544
  pJson = tjsonParse(pData);
4,427,407✔
545
  if (pJson == NULL) {
4,430,402✔
546
    TSDB_CHECK_CODE(code = TSDB_CODE_INVALID_DATA_FMT, lino, _exit);
×
547
  }
548

549
  code = tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config);
4,430,402✔
550
  TSDB_CHECK_CODE(code, lino, _exit);
4,431,074✔
551

552
  code = tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state);
4,431,074✔
553
  TSDB_CHECK_CODE(code, lino, _exit);
4,431,074✔
554

555
_exit:
4,431,074✔
556
  tjsonDelete(pJson);
4,431,074✔
557
  return code;
4,431,074✔
558
}
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