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

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

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

60.54
/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

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

UNCOV
36
  if (pVnode->onRecycle == NULL) {
×
UNCOV
37
    if (pVnode->recycleHead == NULL) {
×
38
      vDebug("vgId:%d, no recyclable buffer pool", TD_VID(pVnode));
×
39
      goto _exit;
×
40
    } else {
UNCOV
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

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

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

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

68
  (void)taosThreadMutexLock(&pVnode->mutex);
38,663✔
69

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

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

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

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

UNCOV
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
        if (code && code != TSDB_CODE_TIMEOUT_ERROR) {
×
107
          TSDB_CHECK_CODE(code, lino, _exit);
×
108
        }
109
      }
110
    }
111
  }
112

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

124
  // alloc buffer pool
125
  code = vnodeGetBufPoolToUse(pVnode);
38,663✔
126
  TSDB_CHECK_CODE(code, lino, _exit);
38,668!
127

128
  // begin meta
129
  code = metaBegin(pVnode->pMeta, META_BEGIN_HEAP_BUFFERPOOL);
38,668✔
130
  TSDB_CHECK_CODE(code, lino, _exit);
38,667!
131

132
  // begin tsdb
133
  code = tsdbBegin(pVnode->pTsdb);
38,667✔
134
  TSDB_CHECK_CODE(code, lino, _exit);
38,660!
135

136
  // begin sma
137
  if (VND_IS_RSMA(pVnode)) {
38,660✔
138
    code = smaBegin(pVnode->pSma);
31✔
139
    TSDB_CHECK_CODE(code, lino, _exit);
31!
140
  }
141

142
_exit:
38,660✔
143
  if (code) {
38,660!
144
    terrno = code;
×
145
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
146
  }
147
  return code;
38,662✔
148
}
149

150
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
1,921,242✔
151
  bool diskAvail = osDataSpaceAvailable();
1,921,242✔
152
  bool needCommit = false;
1,921,249✔
153

154
  (void)taosThreadMutexLock(&pVnode->mutex);
1,921,249✔
155
  if (pVnode->inUse && diskAvail) {
1,921,452!
156
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
1,931,612✔
157
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
10,159✔
158
                             pVnode->state.applied - pVnode->state.committed > 4096));
3,132!
159
  }
160
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
1,921,452!
161
         ", meta changed:%d"
162
         ", state:[%" PRId64 ",%" PRId64 "]",
163
         TD_VID(pVnode), needCommit, diskAvail, pVnode->inUse ? pVnode->inUse->size : 0,
164
         pVnode->inUse ? pVnode->inUse->node.size : 0, pVnode->pMeta->changed, pVnode->state.applied,
165
         pVnode->state.committed);
166
  (void)taosThreadMutexUnlock(&pVnode->mutex);
1,921,452✔
167
  return needCommit;
1,921,459✔
168
}
169

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

177
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
37,759✔
178

179
  code = vnodeEncodeInfo(pInfo, &data);
37,759✔
180
  TSDB_CHECK_CODE(code, lino, _exit);
37,762!
181

182
  // save info to a vnode_tmp.json
183
  pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
37,762✔
184
  if (pFile == NULL) {
37,732!
185
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
186
  }
187

188
  if (taosWriteFile(pFile, data, strlen(data)) < 0) {
37,732!
189
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
190
  }
191

192
  if (taosFsyncFile(pFile) < 0) {
37,747!
193
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
194
  }
195

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

210
int vnodeCommitInfo(const char *dir) {
37,765✔
211
  char fname[TSDB_FILENAME_LEN];
212
  char tfname[TSDB_FILENAME_LEN];
213

214
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
37,765✔
215
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
37,765✔
216

217
  int32_t code = taosRenameFile(tfname, fname);
37,765✔
218
  if (code < 0) {
37,763!
219
    return code;
×
220
  }
221

222
  vInfo("vnode info is committed, dir:%s", dir);
37,763!
223
  return 0;
37,765✔
224
}
225

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

234
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
23,518✔
235

236
  // read info
237
  pFile = taosOpenFile(fname, TD_FILE_READ);
23,518✔
238
  if (pFile == NULL) {
23,543✔
239
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
9,875!
240
  }
241

242
  code = taosFStatFile(pFile, &size, NULL);
13,668✔
243
  TSDB_CHECK_CODE(code, lino, _exit);
13,663!
244

245
  pData = taosMemoryMalloc(size + 1);
13,663✔
246
  if (pData == NULL) {
13,674!
247
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
248
  }
249

250
  if (taosReadFile(pFile, pData, size) < 0) {
13,674!
251
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
252
  }
253

254
  pData[size] = '\0';
13,666✔
255

256
  // decode info
257
  code = vnodeDecodeInfo(pData, pInfo);
13,666✔
258
  TSDB_CHECK_CODE(code, lino, _exit);
13,664!
259

260
  pInfo->config.walCfg.committed = pInfo->state.committed;
13,664✔
261
_exit:
23,539✔
262
  if (code) {
23,539✔
263
    if (pFile) {
9,875!
264
      vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
265
    }
266
  }
267
  taosMemoryFree(pData);
23,539✔
268
  if (taosCloseFile(&pFile) != 0) {
23,544!
269
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
270
  }
271
  return code;
23,551✔
272
}
273

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

280
  // wait last commit task
281
  vnodeAWait(&pVnode->commitTask);
26,396✔
282

283
  code = syncNodeGetConfig(pVnode->sync, &pVnode->config.syncCfg);
26,394✔
284
  TSDB_CHECK_CODE(code, lino, _exit);
26,398!
285

286
  pVnode->state.commitTerm = pVnode->state.applyTerm;
26,398✔
287

288
  pInfo->info.config = pVnode->config;
26,398✔
289
  pInfo->info.state.committed = pVnode->state.applied;
26,398✔
290
  pInfo->info.state.commitTerm = pVnode->state.applyTerm;
26,398✔
291
  pInfo->info.state.commitID = ++pVnode->state.commitID;
26,398✔
292
  pInfo->pVnode = pVnode;
26,398✔
293
  pInfo->txn = metaGetTxn(pVnode->pMeta);
26,398✔
294

295
  // save info
296
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, dir, TSDB_FILENAME_LEN);
26,398✔
297

298
  vDebug("vgId:%d, save config while prepare commit", TD_VID(pVnode));
26,398✔
299
  code = vnodeSaveInfo(dir, &pInfo->info);
26,398✔
300
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
301

302
  code = tsdbPreCommit(pVnode->pTsdb);
26,400✔
303
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
304

305
  code = metaPrepareAsyncCommit(pVnode->pMeta);
26,400✔
306
  TSDB_CHECK_CODE(code, lino, _exit);
26,398!
307

308
  code = smaPrepareAsyncCommit(pVnode->pSma);
26,398✔
309
  TSDB_CHECK_CODE(code, lino, _exit);
26,392!
310

311
  (void)taosThreadMutexLock(&pVnode->mutex);
26,392✔
312
  pVnode->onCommit = pVnode->inUse;
26,396✔
313
  pVnode->inUse = NULL;
26,396✔
314
  (void)taosThreadMutexUnlock(&pVnode->mutex);
26,396✔
315

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

324
  return code;
26,396✔
325
}
326
static void vnodeReturnBufPool(SVnode *pVnode) {
26,400✔
327
  (void)taosThreadMutexLock(&pVnode->mutex);
26,400✔
328

329
  SVBufPool *pPool = pVnode->onCommit;
26,400✔
330
  int32_t    nRef = atomic_sub_fetch_32(&pPool->nRef, 1);
26,400✔
331

332
  pVnode->onCommit = NULL;
26,400✔
333
  if (nRef == 0) {
26,400✔
334
    vnodeBufPoolAddToFreeList(pPool);
25,560✔
335
  } else if (nRef > 0) {
840!
336
    vDebug("vgId:%d, buffer pool %p of id %d is added to recycle queue", TD_VID(pVnode), pPool, pPool->id);
840✔
337

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

351
  (void)taosThreadMutexUnlock(&pVnode->mutex);
26,400✔
352
}
26,400✔
353
static int32_t vnodeCommit(void *arg) {
26,400✔
354
  int32_t code = 0;
26,400✔
355

356
  SCommitInfo *pInfo = (SCommitInfo *)arg;
26,400✔
357
  SVnode      *pVnode = pInfo->pVnode;
26,400✔
358

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

367
  vnodeReturnBufPool(pVnode);
26,400✔
368

369
_exit:
26,400✔
370
  taosMemoryFree(arg);
26,400✔
371
  return code;
26,400✔
372
}
373

374
static void vnodeCommitCancel(void *arg) { taosMemoryFree(arg); }
×
375

376
int vnodeAsyncCommit(SVnode *pVnode) {
26,398✔
377
  int32_t code = 0;
26,398✔
378
  int32_t lino = 0;
26,398✔
379

380
  SCommitInfo *pInfo = (SCommitInfo *)taosMemoryCalloc(1, sizeof(*pInfo));
26,398✔
381
  if (NULL == pInfo) {
26,395!
382
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
383
  }
384

385
  // prepare to commit
386
  code = vnodePrepareCommit(pVnode, pInfo);
26,395✔
387
  TSDB_CHECK_CODE(code, lino, _exit);
26,394!
388

389
  // schedule the task
390
  code =
391
      vnodeAsync(&pVnode->commitChannel, EVA_PRIORITY_HIGH, vnodeCommit, vnodeCommitCancel, pInfo, &pVnode->commitTask);
26,394✔
392
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
393

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

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

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

418
  return code;
156✔
419
}
420

421
static int vnodeCommitImpl(SCommitInfo *pInfo) {
26,400✔
422
  int32_t code = 0;
26,400✔
423
  int32_t lino = 0;
26,400✔
424

425
  char    dir[TSDB_FILENAME_LEN] = {0};
26,400✔
426
  SVnode *pVnode = pInfo->pVnode;
26,400✔
427

428
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
26,400!
429
        pInfo->info.state.commitID, pInfo->info.state.committed, pInfo->info.state.commitTerm);
430

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

437
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, dir, TSDB_FILENAME_LEN);
26,399✔
438

439
  code = syncBeginSnapshot(pVnode->sync, pInfo->info.state.committed);
26,399✔
440
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
441

442
  code = tsdbCommitBegin(pVnode->pTsdb, pInfo);
26,400✔
443
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
444

445
  if (!TSDB_CACHE_NO(pVnode->config)) {
26,400✔
446
    code = tsdbCacheCommit(pVnode->pTsdb);
164✔
447
    TSDB_CHECK_CODE(code, lino, _exit);
164!
448
  }
449

450
  if (VND_IS_RSMA(pVnode)) {
26,400✔
451
    code = smaCommit(pVnode->pSma, pInfo);
14✔
452
    TSDB_CHECK_CODE(code, lino, _exit);
14!
453
  }
454

455
  // commit info
456
  code = vnodeCommitInfo(dir);
26,400✔
457
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
458

459
  code = tsdbCommitCommit(pVnode->pTsdb);
26,400✔
460
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
461

462
  if (VND_IS_RSMA(pVnode)) {
26,400✔
463
    code = smaFinishCommit(pVnode->pSma);
14✔
464
    TSDB_CHECK_CODE(code, lino, _exit);
14!
465
  }
466

467
  code = metaFinishCommit(pVnode->pMeta, pInfo->txn);
26,400✔
468
  TSDB_CHECK_CODE(code, lino, _exit);
26,400!
469

470
  pVnode->state.committed = pInfo->info.state.committed;
26,400✔
471

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

477
  code = syncEndSnapshot(pVnode->sync);
26,400✔
478
  TSDB_CHECK_CODE(code, lino, _exit);
26,399!
479

480
_exit:
26,399✔
481
  if (code) {
26,399!
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));
26,399!
485
  }
486
  return code;
26,400✔
487
}
488

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

493
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, tFName, TSDB_FILENAME_LEN);
12,268✔
494
  offset = strlen(tFName);
12,268✔
495
  snprintf(tFName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VND_INFO_FNAME_TMP);
12,268✔
496

497
  return taosCheckExistFile(tFName);
12,268✔
498
}
499

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

504
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, 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) {
37,754✔
514
  const SVState *pState = (SVState *)pObj;
37,754✔
515

516
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit version", pState->committed));
37,754!
517
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID));
37,760!
518
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm));
37,760!
519

520
  return 0;
37,763✔
521
}
522

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

526
  int32_t code;
527
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
13,740✔
528
  if (code) return code;
13,758!
529
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
13,758✔
530
  if (code) return code;
13,758!
531
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
13,758✔
532
  if (code) return code;
13,757!
533

534
  return 0;
13,757✔
535
}
536

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

543
  pJson = tjsonCreateObject();
37,758✔
544
  if (pJson == NULL) {
37,756!
545
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
546
  }
547

548
  code = tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config);
37,756✔
549
  TSDB_CHECK_CODE(code, lino, _exit);
37,762!
550

551
  code = tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state);
37,762✔
552
  TSDB_CHECK_CODE(code, lino, _exit);
37,764!
553

554
  pData = tjsonToString(pJson);
37,764✔
555
  if (pData == NULL) {
37,758!
556
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
557
  }
558

559
  tjsonDelete(pJson);
37,758✔
560

561
_exit:
37,763✔
562
  if (code) {
37,763!
563
    tjsonDelete(pJson);
×
564
    *ppData = NULL;
×
565
  } else {
566
    *ppData = pData;
37,763✔
567
  }
568
  return code;
37,763✔
569
}
570

571
int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
13,714✔
572
  int32_t code = 0;
13,714✔
573
  int32_t lino;
574
  SJson  *pJson = NULL;
13,714✔
575

576
  pJson = tjsonParse(pData);
13,714✔
577
  if (pJson == NULL) {
13,748!
578
    TSDB_CHECK_CODE(code = TSDB_CODE_INVALID_DATA_FMT, lino, _exit);
×
579
  }
580

581
  code = tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config);
13,748✔
582
  TSDB_CHECK_CODE(code, lino, _exit);
13,756!
583

584
  code = tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state);
13,756✔
585
  TSDB_CHECK_CODE(code, lino, _exit);
13,757!
586

587
_exit:
13,757✔
588
  tjsonDelete(pJson);
13,757✔
589
  return code;
13,758✔
590
}
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