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

taosdata / TDengine / #4120

17 May 2025 06:43AM UTC coverage: 63.022% (+0.2%) from 62.797%
#4120

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

157536 of 318088 branches covered (49.53%)

Branch coverage included in aggregate %.

176 of 225 new or added lines in 20 files covered. (78.22%)

1933 existing lines in 69 files now uncovered.

242799 of 317143 relevant lines covered (76.56%)

18674429.06 hits per line

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

60.42
/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 "vnd.h"
19
#include "vnodeInt.h"
20

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

26
#define VND_INFO_FNAME_TMP "vnode_tmp.json"
27

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

31
#define WAIT_TIME_MILI_SEC 10  // miliseconds
32

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

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

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

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

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

68
  (void)taosThreadMutexLock(&pVnode->mutex);
44,860✔
69

70
  int32_t nTry = 0;
44,860✔
71
  for (;;) {
72
    ++nTry;
44,860✔
73

74
    if (pVnode->freeList) {
44,860!
75
      vDebug("vgId:%d, allocate free buffer pool on %d try, pPool:%p id:%d", TD_VID(pVnode), nTry, pVnode->freeList,
44,860✔
76
             pVnode->freeList->id);
77

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

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

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

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

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

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

126
  // alloc buffer pool
127
  code = vnodeGetBufPoolToUse(pVnode);
44,860✔
128
  TSDB_CHECK_CODE(code, lino, _exit);
44,860!
129

130
  // begin meta
131
  code = metaBegin(pVnode->pMeta, META_BEGIN_HEAP_BUFFERPOOL);
44,860✔
132
  TSDB_CHECK_CODE(code, lino, _exit);
44,857!
133

134
  // begin tsdb
135
  code = tsdbBegin(pVnode->pTsdb);
44,857✔
136
  TSDB_CHECK_CODE(code, lino, _exit);
44,849!
137

138
  // begin sma
139
  if (VND_IS_RSMA(pVnode)) {
44,849✔
140
    code = smaBegin(pVnode->pSma);
39✔
141
    TSDB_CHECK_CODE(code, lino, _exit);
39!
142
  }
143

144
_exit:
44,849✔
145
  if (code) {
44,849!
146
    terrno = code;
×
147
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
148
  }
149
  return code;
44,853✔
150
}
151

152
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
10,948,782✔
153
  bool diskAvail = osDataSpaceAvailable();
10,948,782✔
154
  bool needCommit = false;
10,948,845✔
155

156
  (void)taosThreadMutexLock(&pVnode->mutex);
10,948,845✔
157
  if (pVnode->inUse && diskAvail) {
10,949,926!
158
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
10,962,261✔
159
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
12,305✔
160
                             pVnode->state.applied - pVnode->state.committed > 4096));
3,576✔
161
  }
162
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
10,949,926!
163
         ", meta changed:%d"
164
         ", state:[%" PRId64 ",%" PRId64 "]",
165
         TD_VID(pVnode), needCommit, diskAvail, pVnode->inUse ? pVnode->inUse->size : 0,
166
         pVnode->inUse ? pVnode->inUse->node.size : 0, pVnode->pMeta->changed, pVnode->state.applied,
167
         pVnode->state.committed);
168
  (void)taosThreadMutexUnlock(&pVnode->mutex);
10,949,926✔
169
  return needCommit;
10,950,049✔
170
}
171

172
int vnodeSaveInfo(const char *dir, const SVnodeInfo *pInfo) {
43,772✔
173
  int32_t   code = 0;
43,772✔
174
  int32_t   lino;
175
  char      fname[TSDB_FILENAME_LEN];
176
  TdFilePtr pFile = NULL;
43,772✔
177
  char     *data = NULL;
43,772✔
178

179
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
43,772✔
180

181
  code = vnodeEncodeInfo(pInfo, &data);
43,772✔
182
  TSDB_CHECK_CODE(code, lino, _exit);
43,757!
183

184
  // save info to a vnode_tmp.json
185
  pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
43,757✔
186
  if (pFile == NULL) {
43,642!
187
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
188
  }
189

190
  if (taosWriteFile(pFile, data, strlen(data)) < 0) {
43,642!
191
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
192
  }
193

194
  if (taosFsyncFile(pFile) < 0) {
43,740!
195
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
196
  }
197

198
_exit:
43,780✔
199
  if (code) {
43,780!
200
    vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
201
  } else {
202
    vInfo("vgId:%d, vnode info is saved, fname:%s replica:%d selfIndex:%d changeVersion:%d", pInfo->config.vgId, fname,
43,780✔
203
          pInfo->config.syncCfg.replicaNum, pInfo->config.syncCfg.myIndex, pInfo->config.syncCfg.changeVersion);
204
  }
205
  if (taosCloseFile(&pFile) != 0) {
43,784!
206
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
207
  }
208
  taosMemoryFree(data);
43,787!
209
  TAOS_RETURN(code);
43,787✔
210
}
211

212
int vnodeCommitInfo(const char *dir) {
43,781✔
213
  char fname[TSDB_FILENAME_LEN];
214
  char tfname[TSDB_FILENAME_LEN];
215

216
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
43,781✔
217
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
43,781✔
218

219
  int32_t code = taosRenameFile(tfname, fname);
43,781✔
220
  if (code < 0) {
43,784!
221
    return code;
×
222
  }
223

224
  vInfo("vnode info is committed, dir:%s", dir);
43,784✔
225
  return 0;
43,787✔
226
}
227

228
int vnodeLoadInfo(const char *dir, SVnodeInfo *pInfo) {
28,096✔
229
  int32_t   code = 0;
28,096✔
230
  int32_t   lino;
231
  char      fname[TSDB_FILENAME_LEN];
232
  TdFilePtr pFile = NULL;
28,096✔
233
  char     *pData = NULL;
28,096✔
234
  int64_t   size;
235

236
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
28,096✔
237

238
  // read info
239
  pFile = taosOpenFile(fname, TD_FILE_READ);
28,096✔
240
  if (pFile == NULL) {
28,090✔
241
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
11,861!
242
  }
243

244
  code = taosFStatFile(pFile, &size, NULL);
16,229✔
245
  TSDB_CHECK_CODE(code, lino, _exit);
16,212!
246

247
  pData = taosMemoryMalloc(size + 1);
16,212!
248
  if (pData == NULL) {
16,244!
249
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
250
  }
251

252
  if (taosReadFile(pFile, pData, size) < 0) {
16,244!
253
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
254
  }
255

256
  pData[size] = '\0';
16,227✔
257

258
  // decode info
259
  code = vnodeDecodeInfo(pData, pInfo);
16,227✔
260
  TSDB_CHECK_CODE(code, lino, _exit);
16,227!
261

262
  pInfo->config.walCfg.committed = pInfo->state.committed;
16,227✔
263
_exit:
28,088✔
264
  if (code) {
28,088✔
265
    if (pFile) {
11,861!
266
      vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
267
    }
268
  }
269
  taosMemoryFree(pData);
28,096!
270
  if (taosCloseFile(&pFile) != 0) {
28,099!
271
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
272
  }
273
  return code;
28,109✔
274
}
275

276
static int32_t vnodePrepareCommit(SVnode *pVnode, SCommitInfo *pInfo) {
30,229✔
277
  int32_t code = 0;
30,229✔
278
  int32_t lino = 0;
30,229✔
279
  char    dir[TSDB_FILENAME_LEN] = {0};
30,229✔
280
  int64_t lastCommitted = pInfo->info.state.committed;
30,229✔
281

282
  // wait last commit task
283
  vnodeAWait(&pVnode->commitTask);
30,229✔
284

285
  code = syncNodeGetConfig(pVnode->sync, &pVnode->config.syncCfg);
30,236✔
286
  TSDB_CHECK_CODE(code, lino, _exit);
30,235!
287

288
  pVnode->state.commitTerm = pVnode->state.applyTerm;
30,235✔
289

290
  pInfo->info.config = pVnode->config;
30,235✔
291
  pInfo->info.state.committed = pVnode->state.applied;
30,235✔
292
  pInfo->info.state.commitTerm = pVnode->state.applyTerm;
30,235✔
293
  pInfo->info.state.commitID = ++pVnode->state.commitID;
30,235✔
294
  pInfo->pVnode = pVnode;
30,235✔
295
  pInfo->txn = metaGetTxn(pVnode->pMeta);
30,235✔
296

297
  // save info
298
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, dir, TSDB_FILENAME_LEN);
30,239✔
299

300
  vDebug("vgId:%d, save config while prepare commit", TD_VID(pVnode));
30,234✔
301
  code = vnodeSaveInfo(dir, &pInfo->info);
30,235✔
302
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
303

304
  code = tsdbPreCommit(pVnode->pTsdb);
30,239✔
305
  TSDB_CHECK_CODE(code, lino, _exit);
30,238!
306

307
  code = metaPrepareAsyncCommit(pVnode->pMeta);
30,238✔
308
  TSDB_CHECK_CODE(code, lino, _exit);
30,235!
309

310
  code = smaPrepareAsyncCommit(pVnode->pSma);
30,235✔
311
  TSDB_CHECK_CODE(code, lino, _exit);
30,233!
312

313
  (void)taosThreadMutexLock(&pVnode->mutex);
30,233✔
314
  pVnode->onCommit = pVnode->inUse;
30,237✔
315
  pVnode->inUse = NULL;
30,237✔
316
  (void)taosThreadMutexUnlock(&pVnode->mutex);
30,237✔
317

318
_exit:
30,235✔
319
  if (code) {
30,235!
320
    vError("vgId:%d, %s failed at line %d since %s, commit id:%" PRId64, TD_VID(pVnode), __func__, lino,
×
321
           tstrerror(code), pVnode->state.commitID);
322
  } else {
323
    vDebug("vgId:%d, %s done, commit id:%" PRId64, TD_VID(pVnode), __func__, pInfo->info.state.commitID);
30,235✔
324
  }
325

326
  return code;
30,236✔
327
}
328
static void vnodeReturnBufPool(SVnode *pVnode) {
30,239✔
329
  (void)taosThreadMutexLock(&pVnode->mutex);
30,239✔
330

331
  SVBufPool *pPool = pVnode->onCommit;
30,239✔
332
  int32_t    nRef = atomic_sub_fetch_32(&pPool->nRef, 1);
30,239✔
333

334
  pVnode->onCommit = NULL;
30,239✔
335
  if (nRef == 0) {
30,239✔
336
    vnodeBufPoolAddToFreeList(pPool);
29,602✔
337
  } else if (nRef > 0) {
637!
338
    vDebug("vgId:%d, buffer pool %p of id %d is added to recycle queue", TD_VID(pVnode), pPool, pPool->id);
637✔
339

340
    if (pVnode->recycleTail == NULL) {
637!
341
      pPool->recyclePrev = pPool->recycleNext = NULL;
637✔
342
      pVnode->recycleHead = pVnode->recycleTail = pPool;
637✔
343
    } else {
344
      pPool->recyclePrev = pVnode->recycleTail;
×
345
      pPool->recycleNext = NULL;
×
346
      pVnode->recycleTail->recycleNext = pPool;
×
347
      pVnode->recycleTail = pPool;
×
348
    }
349
  } else {
350
    vError("vgId:%d, buffer pool %p of id %d nRef:%d", TD_VID(pVnode), pPool, pPool->id, nRef);
×
351
  }
352

353
  (void)taosThreadMutexUnlock(&pVnode->mutex);
30,239✔
354
}
30,239✔
355
static int32_t vnodeCommit(void *arg) {
30,239✔
356
  int32_t code = 0;
30,239✔
357

358
  SCommitInfo *pInfo = (SCommitInfo *)arg;
30,239✔
359
  SVnode      *pVnode = pInfo->pVnode;
30,239✔
360

361
  // commit
362
  if ((code = vnodeCommitImpl(pInfo))) {
30,239!
363
    vFatal("vgId:%d, failed to commit vnode since %s", TD_VID(pVnode), terrstr());
×
364
    taosMsleep(100);
×
365
    exit(EXIT_FAILURE);
×
366
    goto _exit;
367
  }
368

369
  vnodeReturnBufPool(pVnode);
30,239✔
370

371
_exit:
30,239✔
372
  taosMemoryFree(arg);
30,239!
373
  return code;
30,238✔
374
}
375

376
static void vnodeCommitCancel(void *arg) { taosMemoryFree(arg); }
×
377

378
int vnodeAsyncCommit(SVnode *pVnode) {
30,238✔
379
  int32_t code = 0;
30,238✔
380
  int32_t lino = 0;
30,238✔
381

382
  SCommitInfo *pInfo = (SCommitInfo *)taosMemoryCalloc(1, sizeof(*pInfo));
30,238!
383
  if (NULL == pInfo) {
30,226!
384
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
385
  }
386

387
  // prepare to commit
388
  code = vnodePrepareCommit(pVnode, pInfo);
30,226✔
389
  TSDB_CHECK_CODE(code, lino, _exit);
30,232!
390

391
  // schedule the task
392
  code = vnodeAsync(COMMIT_TASK_ASYNC, EVA_PRIORITY_HIGH, vnodeCommit, vnodeCommitCancel, pInfo, &pVnode->commitTask);
30,232✔
393
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
394

395
_exit:
30,239✔
396
  if (code) {
30,239!
397
    taosMemoryFree(pInfo);
×
398
    vError("vgId:%d %s failed at line %d since %s" PRId64, TD_VID(pVnode), __func__, lino, tstrerror(code));
×
399
  } else {
400
    vInfo("vgId:%d, vnode async commit done, commitId:%" PRId64 " term:%" PRId64 " applied:%" PRId64, TD_VID(pVnode),
30,239✔
401
          pVnode->state.commitID, pVnode->state.applyTerm, pVnode->state.applied);
402
  }
403
  return code;
30,239✔
404
}
405

406
int32_t vnodeSyncCommit(SVnode *pVnode) {
92✔
407
  int32_t lino;
408
  int32_t code = vnodeAsyncCommit(pVnode);
92✔
409
  TSDB_CHECK_CODE(code, lino, _exit);
92!
410
  vnodeAWait(&pVnode->commitTask);
92✔
411

412
_exit:
92✔
413
  if (code) {
92!
414
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
415
  } else {
416
    vInfo("vgId:%d, sync commit end", TD_VID(pVnode));
92!
417
  }
418

419
  return code;
92✔
420
}
421

422
static int vnodeCommitImpl(SCommitInfo *pInfo) {
30,239✔
423
  int32_t code = 0;
30,239✔
424
  int32_t lino = 0;
30,239✔
425

426
  char    dir[TSDB_FILENAME_LEN] = {0};
30,239✔
427
  SVnode *pVnode = pInfo->pVnode;
30,239✔
428

429
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
30,239✔
430
        pInfo->info.state.commitID, pInfo->info.state.committed, pInfo->info.state.commitTerm);
431

432
  // persist wal before starting
433
  if ((code = walPersist(pVnode->pWal)) < 0) {
30,244!
434
    vError("vgId:%d, failed to persist wal since %s", TD_VID(pVnode), tstrerror(code));
×
435
    return code;
×
436
  }
437

438
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, dir, TSDB_FILENAME_LEN);
30,238✔
439

440
  code = syncBeginSnapshot(pVnode->sync, pInfo->info.state.committed);
30,239✔
441
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
442

443
  code = tsdbCommitBegin(pVnode->pTsdb, pInfo);
30,239✔
444
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
445

446
  if (!TSDB_CACHE_NO(pVnode->config)) {
30,239✔
447
    code = tsdbCacheCommit(pVnode->pTsdb);
5,158✔
448
    TSDB_CHECK_CODE(code, lino, _exit);
5,154!
449
  }
450

451
  if (VND_IS_RSMA(pVnode)) {
30,235✔
452
    code = smaCommit(pVnode->pSma, pInfo);
18✔
453
    TSDB_CHECK_CODE(code, lino, _exit);
18!
454
  }
455

456
  // commit info
457
  code = vnodeCommitInfo(dir);
30,235✔
458
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
459

460
  code = tsdbCommitCommit(pVnode->pTsdb);
30,239✔
461
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
462

463
  if (VND_IS_RSMA(pVnode)) {
30,239✔
464
    code = smaFinishCommit(pVnode->pSma);
18✔
465
    TSDB_CHECK_CODE(code, lino, _exit);
18!
466
  }
467

468
  code = metaFinishCommit(pVnode->pMeta, pInfo->txn);
30,239✔
469
  TSDB_CHECK_CODE(code, lino, _exit);
30,238!
470

471
  pVnode->state.committed = pInfo->info.state.committed;
30,238✔
472

473
  if (smaPostCommit(pVnode->pSma) < 0) {
30,238!
474
    vError("vgId:%d, failed to post-commit sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
475
    return -1;
×
476
  }
477

478
  code = syncEndSnapshot(pVnode->sync);
30,234✔
479
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
480

481
  code = tqCommitOffset(pVnode->pTq);
30,239✔
482
  TSDB_CHECK_CODE(code, lino, _exit);
30,239!
483
  
484
_exit:
30,239✔
485
  if (code) {
30,239!
UNCOV
486
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
487
  } else {
488
    vInfo("vgId:%d, commit end", TD_VID(pVnode));
30,239✔
489
  }
490
  return code;
30,239✔
491
}
492

493
bool vnodeShouldRollback(SVnode *pVnode) {
14,565✔
494
  char    tFName[TSDB_FILENAME_LEN] = {0};
14,565✔
495
  int32_t offset = 0;
14,565✔
496

497
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, tFName, TSDB_FILENAME_LEN);
14,565✔
498
  offset = strlen(tFName);
14,609✔
499
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
14,609✔
500

501
  return taosCheckExistFile(tFName);
14,609✔
502
}
503

UNCOV
504
void vnodeRollback(SVnode *pVnode) {
×
UNCOV
505
  char    tFName[TSDB_FILENAME_LEN] = {0};
×
UNCOV
506
  int32_t offset = 0;
×
507

508
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, tFName, TSDB_FILENAME_LEN);
×
509
  offset = strlen(tFName);
×
UNCOV
510
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
×
511

512
  if (taosRemoveFile(tFName) != 0) {
×
513
    vError("vgId:%d, failed to remove file %s since %s", TD_VID(pVnode), tFName, tstrerror(terrno));
×
514
  }
515
}
×
516

517
static int vnodeEncodeState(const void *pObj, SJson *pJson) {
43,761✔
518
  const SVState *pState = (SVState *)pObj;
43,761✔
519

520
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit version", pState->committed));
43,761!
521
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID));
43,781!
522
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm));
43,775!
523

524
  return 0;
43,784✔
525
}
526

527
static int vnodeDecodeState(const SJson *pJson, void *pObj) {
16,270✔
528
  SVState *pState = (SVState *)pObj;
16,270✔
529

530
  int32_t code;
531
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
16,270✔
532
  if (code) return code;
16,305!
533
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
16,305✔
534
  if (code) return code;
16,305!
535
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
16,305✔
536
  if (code) return code;
16,302!
537

538
  return 0;
16,302✔
539
}
540

541
static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData) {
43,765✔
542
  int32_t code = 0;
43,765✔
543
  int32_t lino;
544
  SJson  *pJson = NULL;
43,765✔
545
  char   *pData = NULL;
43,765✔
546

547
  pJson = tjsonCreateObject();
43,765✔
548
  if (pJson == NULL) {
43,772!
UNCOV
549
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
550
  }
551

552
  code = tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config);
43,772✔
553
  TSDB_CHECK_CODE(code, lino, _exit);
43,761!
554

555
  code = tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state);
43,761✔
556
  TSDB_CHECK_CODE(code, lino, _exit);
43,776!
557

558
  pData = tjsonToString(pJson);
43,776✔
559
  if (pData == NULL) {
43,760!
UNCOV
560
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
561
  }
562

563
  tjsonDelete(pJson);
43,760✔
564

565
_exit:
43,763✔
566
  if (code) {
43,763!
UNCOV
567
    tjsonDelete(pJson);
×
UNCOV
568
    *ppData = NULL;
×
569
  } else {
570
    *ppData = pData;
43,763✔
571
  }
572
  return code;
43,763✔
573
}
574

575
int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
16,215✔
576
  int32_t code = 0;
16,215✔
577
  int32_t lino;
578
  SJson  *pJson = NULL;
16,215✔
579

580
  pJson = tjsonParse(pData);
16,215✔
581
  if (pJson == NULL) {
16,269!
UNCOV
582
    TSDB_CHECK_CODE(code = TSDB_CODE_INVALID_DATA_FMT, lino, _exit);
×
583
  }
584

585
  code = tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config);
16,269✔
586
  TSDB_CHECK_CODE(code, lino, _exit);
16,293!
587

588
  code = tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state);
16,293✔
589
  TSDB_CHECK_CODE(code, lino, _exit);
16,303!
590

591
_exit:
16,303✔
592
  tjsonDelete(pJson);
16,303✔
593
  return code;
16,305✔
594
}
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