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

taosdata / TDengine / #4404

30 Jun 2025 02:45AM UTC coverage: 62.241% (-0.4%) from 62.635%
#4404

push

travis-ci

web-flow
Merge pull request #31480 from taosdata/docs/3.0/TD-34215

add stmt2 docs

153837 of 315978 branches covered (48.69%)

Branch coverage included in aggregate %.

238272 of 314005 relevant lines covered (75.88%)

6134648.6 hits per line

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

60.55
/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) {
36,861✔
65
  int32_t code = 0;
36,861✔
66
  int32_t lino = 0;
36,861✔
67

68
  (void)taosThreadMutexLock(&pVnode->mutex);
36,861✔
69

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

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

78
      pVnode->inUse = pVnode->freeList;
36,861✔
79
      pVnode->inUse->nRef = 1;
36,861✔
80
      pVnode->freeList = pVnode->inUse->freeNext;
36,861✔
81
      pVnode->inUse->freeNext = NULL;
36,861✔
82
      break;
36,861✔
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:
36,861✔
116
  (void)taosThreadMutexUnlock(&pVnode->mutex);
36,861✔
117
  if (code) {
36,861!
118
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
119
  }
120
  return code;
36,861✔
121
}
122
int vnodeBegin(SVnode *pVnode) {
36,861✔
123
  int32_t code = 0;
36,861✔
124
  int32_t lino = 0;
36,861✔
125

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

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

134
  // begin tsdb
135
  code = tsdbBegin(pVnode->pTsdb);
36,861✔
136
  TSDB_CHECK_CODE(code, lino, _exit);
36,852!
137

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

144
_exit:
36,852✔
145
  if (code) {
36,852!
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;
36,854✔
150
}
151

152
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
2,272,546✔
153
  bool diskAvail = osDataSpaceAvailable();
2,272,546✔
154
  bool needCommit = false;
2,272,584✔
155

156
  (void)taosThreadMutexLock(&pVnode->mutex);
2,272,584✔
157
  if (pVnode->inUse && diskAvail) {
2,272,794!
158
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
2,283,549✔
159
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
10,751✔
160
                             pVnode->state.applied - pVnode->state.committed > 4096));
3,176✔
161
  }
162
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
2,272,794!
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);
2,272,794✔
169
  return needCommit;
2,272,818✔
170
}
171

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

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

181
  code = vnodeEncodeInfo(pInfo, &data);
35,736✔
182
  TSDB_CHECK_CODE(code, lino, _exit);
35,718!
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);
35,718✔
186
  if (pFile == NULL) {
35,626!
187
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
188
  }
189

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

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

198
_exit:
35,739✔
199
  if (code) {
35,739!
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,
35,739✔
203
          pInfo->config.syncCfg.replicaNum, pInfo->config.syncCfg.myIndex, pInfo->config.syncCfg.changeVersion);
204
  }
205
  if (taosCloseFile(&pFile) != 0) {
35,744!
206
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
207
  }
208
  taosMemoryFree(data);
35,747!
209
  TAOS_RETURN(code);
35,747✔
210
}
211

212
int vnodeCommitInfo(const char *dir) {
35,747✔
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);
35,747✔
217
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
35,747✔
218

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

224
  vInfo("vnode info is committed, dir:%s", dir);
35,746✔
225
  return 0;
35,747✔
226
}
227

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

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

238
  // read info
239
  pFile = taosOpenFile(fname, TD_FILE_READ);
24,991✔
240
  if (pFile == NULL) {
25,008✔
241
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
10,340!
242
  }
243

244
  code = taosFStatFile(pFile, &size, NULL);
14,668✔
245
  TSDB_CHECK_CODE(code, lino, _exit);
14,660!
246

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

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

256
  pData[size] = '\0';
14,663✔
257

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

262
  pInfo->config.walCfg.committed = pInfo->state.committed;
14,654✔
263
_exit:
24,995✔
264
  if (code) {
24,995✔
265
    if (pFile) {
10,340!
266
      vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
267
    }
268
  }
269
  taosMemoryFree(pData);
24,997!
270
  if (taosCloseFile(&pFile) != 0) {
25,005!
271
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
272
  }
273
  return code;
25,013✔
274
}
275

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

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

285
  code = syncNodeGetConfig(pVnode->sync, &pVnode->config.syncCfg);
23,766✔
286
  TSDB_CHECK_CODE(code, lino, _exit);
23,768!
287

288
  pVnode->state.commitTerm = pVnode->state.applyTerm;
23,768✔
289

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

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

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

304
  code = tsdbPreCommit(pVnode->pTsdb);
23,769✔
305
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
306

307
  code = metaPrepareAsyncCommit(pVnode->pMeta);
23,769✔
308
  TSDB_CHECK_CODE(code, lino, _exit);
23,766!
309

310
  code = smaPrepareAsyncCommit(pVnode->pSma);
23,766✔
311
  TSDB_CHECK_CODE(code, lino, _exit);
23,767!
312

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

318
_exit:
23,768✔
319
  if (code) {
23,768!
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);
23,768✔
324
  }
325

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

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

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

340
    if (pVnode->recycleTail == NULL) {
547!
341
      pPool->recyclePrev = pPool->recycleNext = NULL;
547✔
342
      pVnode->recycleHead = pVnode->recycleTail = pPool;
547✔
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);
23,769✔
354
}
23,769✔
355
static int32_t vnodeCommit(void *arg) {
23,767✔
356
  int32_t code = 0;
23,767✔
357

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

361
  // commit
362
  METRICS_TIMING_BLOCK(pVnode->writeMetrics.commit_time, METRIC_LEVEL_HIGH, {
23,767!
363
    if ((code = vnodeCommitImpl(pInfo))) {
364
      vFatal("vgId:%d, failed to commit vnode since %s", TD_VID(pVnode), terrstr());
365
      taosMsleep(100);
366
      exit(EXIT_FAILURE);
367
      goto _exit;
368
    }
369
  });
370

371
  METRICS_UPDATE(pVnode->writeMetrics.commit_count, METRIC_LEVEL_HIGH, 1);
23,769!
372

373
  vnodeReturnBufPool(pVnode);
23,769✔
374

375
_exit:
23,769✔
376
  taosMemoryFree(arg);
23,769!
377
  return code;
23,769✔
378
}
379

380
static void vnodeCommitCancel(void *arg) { taosMemoryFree(arg); }
×
381

382
int vnodeAsyncCommit(SVnode *pVnode) {
23,767✔
383
  int32_t code = 0;
23,767✔
384
  int32_t lino = 0;
23,767✔
385

386
  SCommitInfo *pInfo = (SCommitInfo *)taosMemoryCalloc(1, sizeof(*pInfo));
23,767!
387
  if (NULL == pInfo) {
23,765!
388
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
389
  }
390

391
  // prepare to commit
392
  code = vnodePrepareCommit(pVnode, pInfo);
23,765✔
393
  TSDB_CHECK_CODE(code, lino, _exit);
23,767!
394

395
  // schedule the task
396
  code = vnodeAsync(COMMIT_TASK_ASYNC, EVA_PRIORITY_HIGH, vnodeCommit, vnodeCommitCancel, pInfo, &pVnode->commitTask);
23,767✔
397
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
398

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

410
int32_t vnodeSyncCommit(SVnode *pVnode) {
85✔
411
  int32_t lino;
412
  int32_t code = vnodeAsyncCommit(pVnode);
85✔
413
  TSDB_CHECK_CODE(code, lino, _exit);
85!
414
  vnodeAWait(&pVnode->commitTask);
85✔
415

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

423
  return code;
85✔
424
}
425

426
static int vnodeCommitImpl(SCommitInfo *pInfo) {
23,765✔
427
  int32_t code = 0;
23,765✔
428
  int32_t lino = 0;
23,765✔
429

430
  char    dir[TSDB_FILENAME_LEN] = {0};
23,765✔
431
  SVnode *pVnode = pInfo->pVnode;
23,765✔
432

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

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

442
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, dir, TSDB_FILENAME_LEN);
23,769✔
443

444
  code = syncBeginSnapshot(pVnode->sync, pInfo->info.state.committed);
23,769✔
445
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
446

447
  code = tsdbCommitBegin(pVnode->pTsdb, pInfo);
23,769✔
448
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
449

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

457
  if (VND_IS_RSMA(pVnode)) {
23,769✔
458
    code = smaCommit(pVnode->pSma, pInfo);
18✔
459
    TSDB_CHECK_CODE(code, lino, _exit);
18!
460
  }
461

462
  // commit info
463
  code = vnodeCommitInfo(dir);
23,769✔
464
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
465

466
  code = tsdbCommitCommit(pVnode->pTsdb);
23,769✔
467
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
468

469
  if (VND_IS_RSMA(pVnode)) {
23,769✔
470
    code = smaFinishCommit(pVnode->pSma);
18✔
471
    TSDB_CHECK_CODE(code, lino, _exit);
18!
472
  }
473

474
  code = metaFinishCommit(pVnode->pMeta, pInfo->txn);
23,769✔
475
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
476

477
  pVnode->state.committed = pInfo->info.state.committed;
23,769✔
478

479
  if (smaPostCommit(pVnode->pSma) < 0) {
23,769!
480
    vError("vgId:%d, failed to post-commit sma since %s", TD_VID(pVnode), tstrerror(terrno));
×
481
    return -1;
×
482
  }
483

484
  code = syncEndSnapshot(pVnode->sync);
23,769✔
485
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
486

487
  code = tqCommitOffset(pVnode->pTq);
23,769✔
488
  TSDB_CHECK_CODE(code, lino, _exit);
23,769!
489
  
490
_exit:
23,769✔
491
  if (code) {
23,769!
492
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
493
  } else {
494
    vInfo("vgId:%d, commit end", TD_VID(pVnode));
23,769✔
495
  }
496
  return code;
23,769✔
497
}
498

499
bool vnodeShouldRollback(SVnode *pVnode) {
13,051✔
500
  char    tFName[TSDB_FILENAME_LEN] = {0};
13,051✔
501
  int32_t offset = 0;
13,051✔
502

503
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, tFName, TSDB_FILENAME_LEN);
13,051✔
504
  offset = strlen(tFName);
13,080✔
505
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
13,080✔
506

507
  return taosCheckExistFile(tFName);
13,080✔
508
}
509

510
void vnodeRollback(SVnode *pVnode) {
×
511
  char    tFName[TSDB_FILENAME_LEN] = {0};
×
512
  int32_t offset = 0;
×
513

514
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, tFName, TSDB_FILENAME_LEN);
×
515
  offset = strlen(tFName);
×
516
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
×
517

518
  if (taosRemoveFile(tFName) != 0) {
×
519
    vError("vgId:%d, failed to remove file %s since %s", TD_VID(pVnode), tFName, tstrerror(terrno));
×
520
  }
521
}
×
522

523
static int vnodeEncodeState(const void *pObj, SJson *pJson) {
35,715✔
524
  const SVState *pState = (SVState *)pObj;
35,715✔
525

526
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit version", pState->committed));
35,715!
527
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID));
35,746!
528
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm));
35,741✔
529

530
  return 0;
35,743✔
531
}
532

533
static int vnodeDecodeState(const SJson *pJson, void *pObj) {
14,702✔
534
  SVState *pState = (SVState *)pObj;
14,702✔
535

536
  int32_t code;
537
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
14,702✔
538
  if (code) return code;
14,727!
539
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
14,727✔
540
  if (code) return code;
14,726!
541
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
14,726✔
542
  if (code) return code;
14,722!
543

544
  return 0;
14,722✔
545
}
546

547
static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData) {
35,734✔
548
  int32_t code = 0;
35,734✔
549
  int32_t lino;
550
  SJson  *pJson = NULL;
35,734✔
551
  char   *pData = NULL;
35,734✔
552

553
  pJson = tjsonCreateObject();
35,734✔
554
  if (pJson == NULL) {
35,741!
555
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
556
  }
557

558
  code = tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config);
35,741✔
559
  TSDB_CHECK_CODE(code, lino, _exit);
35,726!
560

561
  code = tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state);
35,726✔
562
  TSDB_CHECK_CODE(code, lino, _exit);
35,744!
563

564
  pData = tjsonToString(pJson);
35,744✔
565
  if (pData == NULL) {
35,722!
566
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
567
  }
568

569
  tjsonDelete(pJson);
35,722✔
570

571
_exit:
35,738✔
572
  if (code) {
35,738!
573
    tjsonDelete(pJson);
×
574
    *ppData = NULL;
×
575
  } else {
576
    *ppData = pData;
35,738✔
577
  }
578
  return code;
35,738✔
579
}
580

581
int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
14,654✔
582
  int32_t code = 0;
14,654✔
583
  int32_t lino;
584
  SJson  *pJson = NULL;
14,654✔
585

586
  pJson = tjsonParse(pData);
14,654✔
587
  if (pJson == NULL) {
14,697!
588
    TSDB_CHECK_CODE(code = TSDB_CODE_INVALID_DATA_FMT, lino, _exit);
×
589
  }
590

591
  code = tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config);
14,697✔
592
  TSDB_CHECK_CODE(code, lino, _exit);
14,711!
593

594
  code = tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state);
14,711✔
595
  TSDB_CHECK_CODE(code, lino, _exit);
14,722!
596

597
_exit:
14,722✔
598
  tjsonDelete(pJson);
14,722✔
599
  return code;
14,724✔
600
}
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