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

taosdata / TDengine / #5139

05 Nov 2026 03:32AM UTC coverage: 74.033% (+0.09%) from 73.942%
#5139

push

travis-ci

jbjia
fix: capture coverage once from per-thread gcda

Per-case lcov walked the shared build tree and kept each case busy for minutes. Pass GCOV_PREFIX into the test container so each thread writes its own counters, then merge and capture after the suite.

373718 of 504798 relevant lines covered (74.03%)

4155647.08 hits per line

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

78.78
/source/dnode/vnode/src/vnd/vnodeCommit.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * SPDX-License-Identifier: AGPL-3.0-or-later
5
 *
6
 * This program is free software: you can use, redistribute, and/or modify
7
 * it under the terms of the GNU Affero General Public License, version 3
8
 * or later ("AGPL"), as published by the Free Software Foundation.
9
 *
10
 * This program is distributed in the hope that it will be useful, but WITHOUT
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 * FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "meta.h"
19
#include "sync.h"
20
#include "tencrypt.h"
21
#include "vnd.h"
22
#include "vnodeInt.h"
23

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

29
#define VND_INFO_FNAME_TMP "vnode_tmp.json"
30

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

34
#define WAIT_TIME_MILI_SEC 10  // miliseconds
35

36
static int32_t vnodeTryRecycleBufPool(SVnode *pVnode) {
×
37
  int32_t code = 0;
×
38

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

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

58
  code = vnodeBufPoolRecycle(pVnode->onRecycle);
×
59
  if (code) goto _exit;
×
60

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

71
  (void)taosThreadMutexLock(&pVnode->mutex);
26,392 ✔
72

73
  int32_t nTry = 0;
26,393 ✔
74
  for (;;) {
169 ✔
75
    ++nTry;
26,393 ✔
76

77
    if (pVnode->freeList) {
26,393 ✔
78
      vDebug("vgId:%d, allocate free buffer pool on %d try, pPool:%p id:%d", TD_VID(pVnode), nTry, pVnode->freeList,
26,393 ✔
79
             pVnode->freeList->id);
80

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

89
      code = vnodeTryRecycleBufPool(pVnode);
×
90
      TSDB_CHECK_CODE(code, lino, _exit);
×
91

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

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

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

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

129
  // alloc buffer pool
130
  code = vnodeGetBufPoolToUse(pVnode);
26,393 ✔
131
  TSDB_CHECK_CODE(code, lino, _exit);
26,394 ✔
132

133
  // begin meta
134
  code = metaBegin(pVnode->pMeta, META_BEGIN_HEAP_BUFFERPOOL);
26,394 ✔
135
  TSDB_CHECK_CODE(code, lino, _exit);
26,285 ✔
136

137
  // begin tsdb
138
  code = tsdbBegin(pVnode->pTsdb);
26,285 ✔
139
  TSDB_CHECK_CODE(code, lino, _exit);
26,352 ✔
140

141
_exit:
26,352 ✔
142
  if (code) {
26,183 ✔
143
    terrno = code;
×
144
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
145
  }
146
  return code;
26,348 ✔
147
}
148

149
int32_t vnodeProcessSyncCommitReq(SVnode *pVnode, SRpcMsg *pMsg) {
119 ✔
150
  SVnodeSyncCommitReq req = {0};
119 ✔
151

152
  if (pMsg->contLen < sizeof(SMsgHead) ||
238 ✔
153
      tDeserializeSVnodeSyncCommitReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
119 ✔
154
                                       &req) != 0 ||
119 ✔
155
      req.vgId != TD_VID(pVnode)) {
119 ✔
156
    return TSDB_CODE_INVALID_MSG;
×
157
  }
158

159
  TAOS_CHECK_RETURN(vnodeSyncCommit(pVnode));
119 ✔
160
  return vnodeBegin(pVnode);
119 ✔
161
}
162

163
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
1,563,395 ✔
164
  bool diskAvail = osDataSpaceAvailable();
1,563,395 ✔
165
  bool needCommit = false;
1,563,397 ✔
166

167
  (void)taosThreadMutexLock(&pVnode->mutex);
1,563,397 ✔
168
  if (pVnode->inUse && diskAvail) {
1,563,415 ✔
169
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
1,575,002 ✔
170
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
11,587 ✔
171
                             pVnode->state.applied - pVnode->state.committed > 4096));
3,242 ✔
172
  }
173
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
1,563,415 ✔
174
         ", meta changed:%d"
175
         ", state:[%" PRId64 ",%" PRId64 "]",
176
         TD_VID(pVnode), needCommit, diskAvail, pVnode->inUse ? pVnode->inUse->size : 0,
177
         pVnode->inUse ? pVnode->inUse->node.size : 0, pVnode->pMeta->changed, pVnode->state.applied,
178
         pVnode->state.committed);
179
  (void)taosThreadMutexUnlock(&pVnode->mutex);
1,563,415 ✔
180
  return needCommit;
1,563,416 ✔
181
}
182

183
int vnodeSaveInfo(const char *dir, const SVnodeInfo *pInfo) {
25,349 ✔
184
  int32_t   code = 0;
25,349 ✔
185
  int32_t   lino;
150 ✔
186
  char      fname[TSDB_FILENAME_LEN];
25,349 ✔
187
  char     *data = NULL;
25,349 ✔
188

189
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
25,349 ✔
190

191
  code = vnodeEncodeInfo(pInfo, &data);
25,349 ✔
192
  TSDB_CHECK_CODE(code, lino, _exit);
25,330 ✔
193

194
  int32_t len = strlen(data);
25,330 ✔
195
  
196
  // Use encrypted write if tsCfgKey is enabled
197
  code = taosWriteCfgFile(fname, data, len);
25,330 ✔
198
  if (code != 0) {
25,168 ✔
199
    TSDB_CHECK_CODE(code, lino, _exit);
149 ✔
200
  }
201

202
_exit:
25,168 ✔
203
  if (code) {
25,005 ✔
204
    vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
205
  } else {
206
    vInfo("vgId:%d, vnode info is saved, fname:%s replica:%d selfIndex:%d changeVersion:%d", pInfo->config.vgId, fname,
25,154 ✔
207
          pInfo->config.syncCfg.replicaNum, pInfo->config.syncCfg.myIndex, pInfo->config.syncCfg.changeVersion);
208
  }
209
  taosMemoryFree(data);
25,342 ✔
210
  TAOS_RETURN(code);
25,348 ✔
211
}
212

213
int vnodeCommitInfo(const char *dir) {
25,348 ✔
214
  char fname[TSDB_FILENAME_LEN];
25,348 ✔
215
  char tfname[TSDB_FILENAME_LEN];
25,348 ✔
216

217
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
25,348 ✔
218
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
25,348 ✔
219

220
  int32_t code = taosRenameFile(tfname, fname);
25,348 ✔
221
  if (code < 0) {
25,337 ✔
222
    return code;
×
223
  }
224

225
  vInfo("vnode info is committed, dir:%s", dir);
25,337 ✔
226
  return 0;
25,204 ✔
227
}
228

229
int vnodeLoadInfo(const char *dir, SVnodeInfo *pInfo) {
26,739 ✔
230
  int32_t   code = 0;
26,739 ✔
231
  int32_t   lino;
209 ✔
232
  char      fname[TSDB_FILENAME_LEN];
26,739 ✔
233
  char     *pData = NULL;
26,746 ✔
234
  int32_t   dataLen = 0;
26,746 ✔
235

236
  if (taosWaitCfgKeyLoaded() != 0) {
26,746 ✔
237
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
238
  }
239

240
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
26,746 ✔
241

242
  // Use taosReadCfgFile for automatic decryption support (returns null-terminated string)
243
  code = taosReadCfgFile(fname, &pData, &dataLen);
26,746 ✔
244
  if (code != 0) {
26,736 ✔
245
    TSDB_CHECK_CODE(code, lino, _exit);
11,292 ✔
246
  }
247

248
  // decode info
249
  code = vnodeDecodeInfo(pData, pInfo);
15,444 ✔
250
  TSDB_CHECK_CODE(code, lino, _exit);
15,430 ✔
251

252
  pInfo->config.walCfg.committed = pInfo->state.committed;
15,430 ✔
253
_exit:
26,722 ✔
254
  if (code) {
26,734 ✔
255
    vError("vgId:%d %s failed at %s:%d since %s, file:%s", pInfo->config.vgId, __func__, __FILE__, lino,
11,292 ✔
256
           tstrerror(code), fname);
257
  }
258
  taosMemoryFree(pData);
26,734 ✔
259
  return code;
26,726 ✔
260
}
261

262
static int32_t vnodePrepareCommit(SVnode *pVnode, SCommitInfo *pInfo) {
12,444 ✔
263
  int32_t code = 0;
12,444 ✔
264
  int32_t lino = 0;
12,444 ✔
265
  char    dir[TSDB_FILENAME_LEN] = {0};
12,444 ✔
266
  int64_t lastCommitted = pInfo->info.state.committed;
12,444 ✔
267

268
  // wait last commit task
269
  vnodeAWait(&pVnode->commitTask);
12,444 ✔
270

271
  if (pVnode->sync != 0) {
12,446 ✔
272
    code = syncNodeGetConfig(pVnode->sync, &pVnode->config.syncCfg);
12,368 ✔
273
    TSDB_CHECK_CODE(code, lino, _exit);
12,368 ✔
274
  }
275

276
  pVnode->state.commitTerm = pVnode->state.applyTerm;
12,446 ✔
277

278
  pInfo->info.config = pVnode->config;
12,446 ✔
279
  pInfo->info.state.committed = pVnode->state.applied;
12,446 ✔
280
  pInfo->info.state.commitTerm = pVnode->state.applyTerm;
12,446 ✔
281
  pInfo->info.state.commitID = ++pVnode->state.commitID;
12,446 ✔
282
  pInfo->pVnode = pVnode;
12,446 ✔
283
  pInfo->txn = metaGetTxn(pVnode->pMeta);
12,446 ✔
284

285
  // save info
286
  vnodeGetPrimaryPath(pVnode, false, dir, TSDB_FILENAME_LEN);
12,447 ✔
287

288
  vDebug("vgId:%d, save config while prepare commit", TD_VID(pVnode));
12,444 ✔
289
  code = vnodeSaveInfo(dir, &pInfo->info);
12,454 ✔
290
  TSDB_CHECK_CODE(code, lino, _exit);
12,445 ✔
291

292
  code = tsdbPreCommit(pVnode->pTsdb);
12,445 ✔
293
  TSDB_CHECK_CODE(code, lino, _exit);
12,444 ✔
294

295
  code = metaPrepareAsyncCommit(pVnode->pMeta);
12,444 ✔
296
  TSDB_CHECK_CODE(code, lino, _exit);
12,378 ✔
297

298
  (void)taosThreadMutexLock(&pVnode->mutex);
12,378 ✔
299
  pVnode->onCommit = pVnode->inUse;
12,419 ✔
300
  pVnode->inUse = NULL;
12,419 ✔
301
  (void)taosThreadMutexUnlock(&pVnode->mutex);
12,419 ✔
302

303
_exit:
12,402 ✔
304
  if (code) {
12,355 ✔
305
    vError("vgId:%d, %s failed at line %d since %s, commit id:%" PRId64, TD_VID(pVnode), __func__, lino,
×
306
           tstrerror(code), pVnode->state.commitID);
307
  } else {
308
    vDebug("vgId:%d, %s done, commit id:%" PRId64, TD_VID(pVnode), __func__, pInfo->info.state.commitID);
12,410 ✔
309
  }
310

311
  return code;
12,459 ✔
312
}
313
static void vnodeReturnBufPool(SVnode *pVnode) {
12,448 ✔
314
  (void)taosThreadMutexLock(&pVnode->mutex);
12,448 ✔
315

316
  SVBufPool *pPool = pVnode->onCommit;
12,449 ✔
317
  int32_t    nRef = atomic_sub_fetch_32(&pPool->nRef, 1);
12,449 ✔
318

319
  pVnode->onCommit = NULL;
12,449 ✔
320
  if (nRef == 0) {
12,449 ✔
321
    vnodeBufPoolAddToFreeList(pPool);
12,374 ✔
322
  } else if (nRef > 0) {
75 ✔
323
    vDebug("vgId:%d, buffer pool %p of id %d is added to recycle queue", TD_VID(pVnode), pPool, pPool->id);
75 ✔
324

325
    if (pVnode->recycleTail == NULL) {
75 ✔
326
      pPool->recyclePrev = pPool->recycleNext = NULL;
75 ✔
327
      pVnode->recycleHead = pVnode->recycleTail = pPool;
75 ✔
328
    } else {
329
      pPool->recyclePrev = pVnode->recycleTail;
×
330
      pPool->recycleNext = NULL;
×
331
      pVnode->recycleTail->recycleNext = pPool;
×
332
      pVnode->recycleTail = pPool;
×
333
    }
334
  } else {
335
    vError("vgId:%d, buffer pool %p of id %d nRef:%d", TD_VID(pVnode), pPool, pPool->id, nRef);
×
336
  }
337

338
  (void)taosThreadMutexUnlock(&pVnode->mutex);
12,448 ✔
339
}
12,447 ✔
340
static int32_t vnodeCommit(void *arg) {
12,370 ✔
341
  int32_t code = 0;
12,370 ✔
342

343
  SCommitInfo *pInfo = (SCommitInfo *)arg;
12,370 ✔
344
  SVnode      *pVnode = pInfo->pVnode;
12,370 ✔
345

346
  // commit
347
  METRICS_TIMING_BLOCK(pVnode->writeMetrics.commit_time, METRIC_LEVEL_HIGH, {
12,370 ✔
348
    if ((code = vnodeCommitImpl(pInfo))) {
349
      vFatal("vgId:%d, failed to commit vnode since %s", TD_VID(pVnode), terrstr());
350
      taosMsleep(100);
351
      exit(EXIT_FAILURE);
352
      goto _exit;
353
    }
354
  });
355

356
  METRICS_UPDATE(pVnode->writeMetrics.commit_count, METRIC_LEVEL_HIGH, 1);
12,369 ✔
357

358
  vnodeReturnBufPool(pVnode);
12,369 ✔
359

360
_exit:
12,370 ✔
361
  taosMemoryFree(arg);
12,370 ✔
362
  return code;
12,370 ✔
363
}
364

365
static void vnodeCommitCancel(void *arg) { taosMemoryFree(arg); }
×
366

367
int vnodeAsyncCommitEx(SVnode *pVnode, bool forceTrim) {
12,367 ✔
368
  int32_t code = 0;
12,367 ✔
369
  int32_t lino = 0;
12,367 ✔
370

371
  SCommitInfo *pInfo = (SCommitInfo *)taosMemoryCalloc(1, sizeof(*pInfo));
12,367 ✔
372
  if (NULL == pInfo) {
12,361 ✔
373
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
374
  }
375

376
  pInfo->forceTrim = forceTrim;
12,361 ✔
377

378
  // prepare to commit
379
  code = vnodePrepareCommit(pVnode, pInfo);
12,361 ✔
380
  TSDB_CHECK_CODE(code, lino, _exit);
12,348 ✔
381

382
  // schedule the task
383
  code = vnodeAsync(COMMIT_TASK_ASYNC, EVA_PRIORITY_HIGH, vnodeCommit, vnodeCommitCancel, pInfo, &pVnode->commitTask);
12,348 ✔
384
  TSDB_CHECK_CODE(code, lino, _exit);
12,370 ✔
385

386
_exit:
12,370 ✔
387
  if (code) {
12,315 ✔
388
    taosMemoryFree(pInfo);
×
389
    vError("vgId:%d %s failed at line %d since %s" PRId64, TD_VID(pVnode), __func__, lino, tstrerror(code));
×
390
  } else {
391
    vInfo("vgId:%d, vnode async commit done, commitId:%" PRId64 " term:%" PRId64 " applied:%" PRId64 " forceTrim:%d",
12,370 ✔
392
          TD_VID(pVnode), pVnode->state.commitID, pVnode->state.applyTerm, pVnode->state.applied, forceTrim);
393
  }
394
  return code;
12,370 ✔
395
}
396

397
int vnodeAsyncCommit(SVnode *pVnode, bool forceTrimWal) { return vnodeAsyncCommitEx(pVnode, forceTrimWal); }
12,367 ✔
398

399
int32_t vnodeSyncCommit(SVnode *pVnode) {
289 ✔
400
  int32_t lino;
×
401
  int32_t code = 0;
289 ✔
402

403
  if (pVnode->sync == 0) {
289 ✔
404
    SCommitInfo info = {0};
79 ✔
405
    code = vnodePrepareCommit(pVnode, &info);
79 ✔
406
    TSDB_CHECK_CODE(code, lino, _exit);
78 ✔
407
    code = vnodeCommitImpl(&info);
78 ✔
408
    vnodeReturnBufPool(pVnode);
79 ✔
409
  } else {
410
    code = vnodeAsyncCommit(pVnode, false);
210 ✔
411
    TSDB_CHECK_CODE(code, lino, _exit);
211 ✔
412
    vnodeAWait(&pVnode->commitTask);
211 ✔
413
  }
414

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

422
  return code;
289 ✔
423
}
424

425
static int vnodeCommitImpl(SCommitInfo *pInfo) {
12,441 ✔
426
  int32_t code = 0;
12,441 ✔
427
  int32_t lino = 0;
12,441 ✔
428

429
  char    dir[TSDB_FILENAME_LEN] = {0};
12,441 ✔
430
  SVnode *pVnode = pInfo->pVnode;
12,441 ✔
431

432
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
12,441 ✔
433
        pInfo->info.state.commitID, pInfo->info.state.committed, pInfo->info.state.commitTerm);
434

435
  // persist wal before starting
436
  if ((code = walPersist(pVnode->pWal)) < 0) {
12,451 ✔
437
    vError("vgId:%d, failed to persist wal since %s", TD_VID(pVnode), tstrerror(code));
×
438
    return code;
×
439
  }
440

441
  vnodeGetPrimaryPath(pVnode, false, dir, TSDB_FILENAME_LEN);
12,446 ✔
442

443
  if (pVnode->sync != 0) {
12,443 ✔
444
    code = syncBeginSnapshot(pVnode->sync, pInfo->info.state.committed);
12,367 ✔
445
    TSDB_CHECK_CODE(code, lino, _exit);
12,367 ✔
446
  }
447

448
  code = tsdbCommitBegin(pVnode->pTsdb, pInfo);
12,443 ✔
449
  TSDB_CHECK_CODE(code, lino, _exit);
12,447 ✔
450

451
  if (!TSDB_CACHE_NO(pVnode->config)) {
12,447 ✔
452
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.last_cache_commit_time, METRIC_LEVEL_HIGH,
98 ✔
453
                         { code = tsdbCacheCommit(pVnode->pTsdb); });
454
    METRICS_UPDATE(pVnode->writeMetrics.last_cache_commit_count, METRIC_LEVEL_HIGH, 1);
98 ✔
455
    TSDB_CHECK_CODE(code, lino, _exit);
98 ✔
456
  }
457

458
  // blob storage engine commit
459
  code = bseCommit(pVnode->pBse);
12,447 ✔
460
  // commit info
461
  code = vnodeCommitInfo(dir);
12,449 ✔
462
  TSDB_CHECK_CODE(code, lino, _exit);
12,448 ✔
463

464
  code = tsdbCommitCommit(pVnode->pTsdb);
12,448 ✔
465
  TSDB_CHECK_CODE(code, lino, _exit);
12,449 ✔
466

467
  code = metaFinishCommit(pVnode->pMeta, pInfo->txn);
12,449 ✔
468
  TSDB_CHECK_CODE(code, lino, _exit);
12,441 ✔
469

470
  pVnode->state.committed = pInfo->info.state.committed;
12,441 ✔
471

472
  if (pVnode->sync != 0) {
12,441 ✔
473
    code = syncEndSnapshot(pVnode->sync, pInfo->forceTrim);
12,365 ✔
474
    TSDB_CHECK_CODE(code, lino, _exit);
12,365 ✔
475
  }
476

477
  code = tqOffsetCommit(pVnode->pTq);
12,441 ✔
478
  TSDB_CHECK_CODE(code, lino, _exit);
12,448 ✔
479
  
480
_exit:
12,448 ✔
481
  if (code) {
12,389 ✔
482
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
483
  } else {
484
    vInfo("vgId:%d, commit end", TD_VID(pVnode));
12,444 ✔
485
  }
486
  return code;
12,393 ✔
487
}
488

489
bool vnodeShouldRollback(SVnode *pVnode) {
13,960 ✔
490
  char    tFName[TSDB_FILENAME_LEN] = {0};
13,960 ✔
491
  int32_t offset = 0;
13,960 ✔
492

493
  vnodeGetPrimaryPath(pVnode, false, tFName, TSDB_FILENAME_LEN);
13,960 ✔
494
  offset = strlen(tFName);
13,960 ✔
495
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
13,960 ✔
496

497
  return taosCheckExistFile(tFName);
13,960 ✔
498
}
499

500
void vnodeRollback(SVnode *pVnode) {
×
501
  char    tFName[TSDB_FILENAME_LEN] = {0};
×
502
  int32_t offset = 0;
×
503

504
  vnodeGetPrimaryPath(pVnode, false, tFName, TSDB_FILENAME_LEN);
×
505
  offset = strlen(tFName);
×
506
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
×
507

508
  if (taosRemoveFile(tFName) != 0) {
×
509
    vError("vgId:%d, failed to remove file %s since %s", TD_VID(pVnode), tFName, tstrerror(terrno));
×
510
  }
511
}
×
512

513
static int vnodeEncodeState(const void *pObj, SJson *pJson) {
25,339 ✔
514
  const SVState *pState = (SVState *)pObj;
25,339 ✔
515

516
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit version", pState->committed));
25,339 ✔
517
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID));
25,345 ✔
518
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm));
25,340 ✔
519

520
  return 0;
25,197 ✔
521
}
522

523
static int vnodeDecodeState(const SJson *pJson, void *pObj) {
15,661 ✔
524
  SVState *pState = (SVState *)pObj;
15,661 ✔
525

526
  int32_t code;
114 ✔
527
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
15,661 ✔
528
  if (code) return code;
15,664 ✔
529
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
15,664 ✔
530
  if (code) return code;
15,663 ✔
531
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
15,663 ✔
532
  if (code) return code;
15,663 ✔
533

534
  return 0;
15,549 ✔
535
}
536

537
static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData) {
25,347 ✔
538
  int32_t code = 0;
25,347 ✔
539
  int32_t lino;
150 ✔
540
  SJson  *pJson = NULL;
25,347 ✔
541
  char   *pData = NULL;
25,347 ✔
542

543
  pJson = tjsonCreateObject();
25,347 ✔
544
  if (pJson == NULL) {
25,350 ✔
545
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
546
  }
547

548
  code = tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config);
25,350 ✔
549
  TSDB_CHECK_CODE(code, lino, _exit);
25,345 ✔
550

551
  code = tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state);
25,345 ✔
552
  TSDB_CHECK_CODE(code, lino, _exit);
25,350 ✔
553

554
  code = tjsonAddIntegerToObject(pJson, "lastTxnConsumeTs", (uint64_t)pInfo->lastTxnConsumeTs);
25,350 ✔
555
  TSDB_CHECK_CODE(code, lino, _exit);
25,343 ✔
556

557
  pData = tjsonToString(pJson);
25,343 ✔
558
  if (pData == NULL) {
25,347 ✔
559
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
560
  }
561

562
  tjsonDelete(pJson); 
25,347 ✔
563

564
_exit:
25,314 ✔
565
  if (code) {
25,164 ✔
566
    tjsonDelete(pJson);
×
567
    *ppData = NULL;
×
568
  } else {
569
    *ppData = pData;
25,314 ✔
570
  }
571
  return code;
25,314 ✔
572
}
573

574
int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
15,630 ✔
575
  int32_t code = 0;
15,630 ✔
576
  int32_t lino;
114 ✔
577
  SJson  *pJson = NULL;
15,630 ✔
578

579
  pJson = tjsonParse(pData);
15,630 ✔
580
  if (pJson == NULL) {
15,653 ✔
581
    TSDB_CHECK_CODE(code = TSDB_CODE_INVALID_DATA_FMT, lino, _exit);
×
582
  }
583

584
  code = tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config);
15,653 ✔
585
  TSDB_CHECK_CODE(code, lino, _exit);
15,662 ✔
586

587
  code = tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state);
15,662 ✔
588
  TSDB_CHECK_CODE(code, lino, _exit);
15,663 ✔
589

590
  // lastTxnConsumeTs is optional (absent in older vnode-info files  ->  stays 0)
591
  (void)tjsonGetBigIntValue(pJson, "lastTxnConsumeTs", &pInfo->lastTxnConsumeTs);
15,663 ✔
592

593
_exit:
15,662 ✔
594
  tjsonDelete(pJson);
15,662 ✔
595
  return code;
15,636 ✔
596
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc