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

taosdata / TDengine / #3530

16 Nov 2024 07:44AM UTC coverage: 60.219% (-0.7%) from 60.888%
#3530

push

travis-ci

web-flow
Update 03-ad.md

118417 of 252124 branches covered (46.97%)

Branch coverage included in aggregate %.

198982 of 274951 relevant lines covered (72.37%)

6072359.98 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

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) {
38,375✔
65
  int32_t code = 0;
38,375✔
66
  int32_t lino = 0;
38,375✔
67

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

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

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

78
      pVnode->inUse = pVnode->freeList;
38,378✔
79
      pVnode->inUse->nRef = 1;
38,378✔
80
      pVnode->freeList = pVnode->inUse->freeNext;
38,378✔
81
      pVnode->inUse->freeNext = NULL;
38,378✔
82
      break;
38,378✔
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
        if (code && code != TSDB_CODE_TIMEOUT_ERROR) {
×
107
          TSDB_CHECK_CODE(code, lino, _exit);
×
108
        }
109
      }
110
    }
111
  }
112

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

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

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

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

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

142
_exit:
38,373✔
143
  if (code) {
38,373!
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,374✔
148
}
149

150
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
1,918,300✔
151
  bool diskAvail = osDataSpaceAvailable();
1,918,300✔
152
  bool needCommit = false;
1,918,309✔
153

154
  (void)taosThreadMutexLock(&pVnode->mutex);
1,918,309✔
155
  if (pVnode->inUse && diskAvail) {
1,918,506!
156
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
1,928,720✔
157
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
10,208✔
158
                             pVnode->state.applied - pVnode->state.committed > 4096));
3,213!
159
  }
160
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
1,918,506!
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,918,506✔
167
  return needCommit;
1,918,524✔
168
}
169

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

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

179
  code = vnodeEncodeInfo(pInfo, &data);
37,465✔
180
  TSDB_CHECK_CODE(code, lino, _exit);
37,462!
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,462✔
184
  if (pFile == NULL) {
37,414!
185
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
186
  }
187

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

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

196
_exit:
37,466✔
197
  if (code) {
37,466!
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,466!
201
          pInfo->config.syncCfg.replicaNum, pInfo->config.syncCfg.myIndex, pInfo->config.syncCfg.changeVersion);
202
  }
203
  if (taosCloseFile(&pFile) != 0) {
37,469!
204
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
205
  }
206
  taosMemoryFree(data);
37,469✔
207
  return code;
37,469✔
208
}
209

210
int vnodeCommitInfo(const char *dir) {
37,469✔
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,469✔
215
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);
37,469✔
216

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

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

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

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

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

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

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

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

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

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

260
  pInfo->config.walCfg.committed = pInfo->state.committed;
13,668✔
261
_exit:
23,531✔
262
  if (code) {
23,531✔
263
    if (pFile) {
9,863!
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,531✔
268
  if (taosCloseFile(&pFile) != 0) {
23,543!
269
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
270
  }
271
  return code;
23,544✔
272
}
273

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

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

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

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

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

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

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

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

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

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

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

316
_exit:
26,106✔
317
  if (code) {
26,106!
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,106✔
322
  }
323

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

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

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

338
    if (pVnode->recycleTail == NULL) {
1,008!
339
      pPool->recyclePrev = pPool->recycleNext = NULL;
1,008✔
340
      pVnode->recycleHead = pVnode->recycleTail = pPool;
1,008✔
341
    } else {
342
      pPool->recyclePrev = pVnode->recycleTail;
×
343
      pPool->recycleNext = NULL;
×
344
      pVnode->recycleTail->recycleNext = pPool;
×
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,107✔
352
}
26,107✔
353
static int32_t vnodeCommit(void *arg) {
26,106✔
354
  int32_t code = 0;
26,106✔
355

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

359
  // commit
360
  if ((code = vnodeCommitImpl(pInfo))) {
26,106!
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,107✔
368

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

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

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

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

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

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

394
_exit:
26,107✔
395
  if (code) {
26,107!
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,107!
400
          pVnode->state.commitID, pVnode->state.applyTerm, pVnode->state.applied);
401
  }
402
  return code;
26,107✔
403
}
404

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

411
_exit:
158✔
412
  if (code) {
158!
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));
158!
416
  }
417

418
  return code;
158✔
419
}
420

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

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

428
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
26,104!
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,106!
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,107✔
438

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

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

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

450
  if (VND_IS_RSMA(pVnode)) {
26,107✔
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,107✔
457
  TSDB_CHECK_CODE(code, lino, _exit);
26,107!
458

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

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

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

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

472
  if (smaPostCommit(pVnode->pSma) < 0) {
26,107!
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,107✔
478
  TSDB_CHECK_CODE(code, lino, _exit);
26,107!
479

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

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

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

497
  return taosCheckExistFile(tFName);
12,271✔
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,461✔
514
  const SVState *pState = (SVState *)pObj;
37,461✔
515

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

520
  return 0;
37,465✔
521
}
522

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

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

534
  return 0;
13,769✔
535
}
536

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

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

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

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

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

559
  tjsonDelete(pJson);
37,464✔
560

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

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

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

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

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

587
_exit:
13,768✔
588
  tjsonDelete(pJson);
13,768✔
589
  return code;
13,764✔
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