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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

60.88
/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) {
13,993✔
65
  int32_t code = 0;
13,993✔
66
  int32_t lino = 0;
13,993✔
67

68
  (void)taosThreadMutexLock(&pVnode->mutex);
13,993✔
69

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

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

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

124
  // alloc buffer pool
125
  code = vnodeGetBufPoolToUse(pVnode);
13,994✔
126
  TSDB_CHECK_CODE(code, lino, _exit);
13,996!
127

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

132
  // begin tsdb
133
  code = tsdbBegin(pVnode->pTsdb);
13,995✔
134
  TSDB_CHECK_CODE(code, lino, _exit);
13,991!
135

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

142
_exit:
13,991✔
143
  if (code) {
13,991!
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;
13,993✔
148
}
149

150
int vnodeShouldCommit(SVnode *pVnode, bool atExit) {
838,953✔
151
  bool diskAvail = osDataSpaceAvailable();
838,953✔
152
  bool needCommit = false;
838,957✔
153

154
  (void)taosThreadMutexLock(&pVnode->mutex);
838,957✔
155
  if (pVnode->inUse && diskAvail) {
839,043!
156
    needCommit = (pVnode->inUse->size > pVnode->inUse->node.size) ||
844,598✔
157
                 (atExit && (pVnode->inUse->size > 0 || pVnode->pMeta->changed ||
5,550✔
158
                             pVnode->state.applied - pVnode->state.committed > 4096));
1,685✔
159
  }
160
  vTrace("vgId:%d, should commit:%d, disk available:%d, buffer size:%" PRId64 ", node size:%" PRId64
839,043!
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);
839,043✔
167
  return needCommit;
839,064✔
168
}
169

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

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

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

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

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

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

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

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

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

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

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

236
  // read info
237
  pFile = taosOpenFile(fname, TD_FILE_READ);
12,519✔
238
  if (pFile == NULL) {
12,542✔
239
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
5,499!
240
  }
241

242
  code = taosFStatFile(pFile, &size, NULL);
7,043✔
243
  TSDB_CHECK_CODE(code, lino, _exit);
7,035!
244

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

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

254
  pData[size] = '\0';
7,046✔
255

256
  // decode info
257
  code = vnodeDecodeInfo(pData, pInfo);
7,046✔
258
  TSDB_CHECK_CODE(code, lino, _exit);
7,043!
259

260
  pInfo->config.walCfg.committed = pInfo->state.committed;
7,043✔
261
_exit:
12,542✔
262
  if (code) {
12,542✔
263
    if (pFile) {
5,499!
264
      vError("vgId:%d %s failed at %s:%d since %s", pInfo->config.vgId, __func__, __FILE__, lino, tstrerror(code));
×
265
    }
266
  }
267
  taosMemoryFree(pData);
12,542✔
268
  if (taosCloseFile(&pFile) != 0) {
12,545!
269
    vError("vgId:%d, failed to close file", pInfo->config.vgId);
×
270
  }
271
  return code;
12,550✔
272
}
273

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

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

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

286
  pVnode->state.commitTerm = pVnode->state.applyTerm;
7,371✔
287

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

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

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

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

305
  code = metaPrepareAsyncCommit(pVnode->pMeta);
7,371✔
306
  TSDB_CHECK_CODE(code, lino, _exit);
7,370!
307

308
  code = smaPrepareAsyncCommit(pVnode->pSma);
7,370✔
309
  TSDB_CHECK_CODE(code, lino, _exit);
7,368!
310

311
  (void)taosThreadMutexLock(&pVnode->mutex);
7,368✔
312
  pVnode->onCommit = pVnode->inUse;
7,369✔
313
  pVnode->inUse = NULL;
7,369✔
314
  (void)taosThreadMutexUnlock(&pVnode->mutex);
7,369✔
315

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

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

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

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

338
    if (pVnode->recycleTail == NULL) {
73!
339
      pPool->recyclePrev = pPool->recycleNext = NULL;
73✔
340
      pVnode->recycleHead = pVnode->recycleTail = pPool;
73✔
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);
7,371✔
352
}
7,371✔
353
static int32_t vnodeCommit(void *arg) {
7,370✔
354
  int32_t code = 0;
7,370✔
355

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

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

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

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

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

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

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

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

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

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

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

418
  return code;
31✔
419
}
420

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

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

428
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
7,369!
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) {
7,371!
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);
7,371✔
438

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

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

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

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

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

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

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

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

470
  pVnode->state.committed = pInfo->info.state.committed;
7,371✔
471

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

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

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

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

497
  return taosCheckExistFile(tFName);
6,625✔
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) {
13,313✔
514
  const SVState *pState = (SVState *)pObj;
13,313✔
515

516
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit version", pState->committed));
13,313!
517
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID));
13,315!
518
  TAOS_CHECK_RETURN(tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm));
13,314✔
519

520
  return 0;
13,312✔
521
}
522

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

526
  int32_t code;
527
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
7,060✔
528
  if (code) return code;
7,068!
529
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
7,068✔
530
  if (code) return code;
7,067!
531
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
7,067✔
532
  if (code) return code;
7,070!
533

534
  return 0;
7,070✔
535
}
536

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

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

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

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

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

559
  tjsonDelete(pJson);
13,314✔
560

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

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

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

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

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

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