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

H
Hongze Cheng 已提交
16
#include "vnd.h"
H
Hongze Cheng 已提交
17
#include "vnodeInt.h"
H
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19 20
#define VND_INFO_FNAME_TMP "vnode_tmp.json"

H
Hongze Cheng 已提交
21 22
static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData);
static int vnodeCommitImpl(SCommitInfo *pInfo);
H
refact  
Hongze Cheng 已提交
23

H
Hongze Cheng 已提交
24 25
#define WAIT_TIME_MILI_SEC 10  // miliseconds

H
Hongze Cheng 已提交
26 27
static int32_t vnodeTryRecycleBufPool(SVnode *pVnode) {
  int32_t code = 0;
H
Hongze Cheng 已提交
28

H
Hongze Cheng 已提交
29 30 31 32
  if (pVnode->onRecycle == NULL) {
    if (pVnode->recycleHead == NULL) {
      vDebug("vgId:%d no recyclable buffer pool", TD_VID(pVnode));
      goto _exit;
H
Hongze Cheng 已提交
33
    } else {
H
Hongze Cheng 已提交
34 35 36 37 38 39 40 41 42 43 44
      vDebug("vgId:%d buffer pool %p of id %d on recycle list, try to recycle", TD_VID(pVnode), pVnode->recycleHead,
             pVnode->recycleHead->id);

      pVnode->onRecycle = pVnode->recycleHead;
      if (pVnode->recycleHead == pVnode->recycleTail) {
        pVnode->recycleHead = pVnode->recycleTail = NULL;
      } else {
        pVnode->recycleHead = pVnode->recycleHead->recycleNext;
        pVnode->recycleHead->recyclePrev = NULL;
      }
      pVnode->onRecycle->recycleNext = pVnode->onRecycle->recyclePrev = NULL;
H
Hongze Cheng 已提交
45
    }
H
Hongze Cheng 已提交
46
  }
H
Hongze Cheng 已提交
47

H
Hongze Cheng 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  // do recycle the buffer pool
  SVBufPool *pPool = pVnode->onRecycle;
  vDebug("vgId:%d buffer pool %p of id %d on recycle", TD_VID(pVnode), pPool, pPool->id);

  taosThreadMutexLock(&pPool->mutex);

  SQueryNode *pNode = pPool->qList.pNext;
  while (pNode != &pPool->qList) {
    int32_t rc = pNode->reseek(pNode->pQHandle);
    if (rc == 0) {
      SQueryNode *pTNode = pNode->pNext;
      pNode->pNext->ppNext = pNode->ppNext;
      *pNode->ppNext = pNode->pNext;
      pPool->nQuery--;
      pNode = pTNode;
    } else if (rc == TSDB_CODE_VND_QUERY_BUSY) {
H
Hongze Cheng 已提交
64
      pNode = pNode->pNext;
H
Hongze Cheng 已提交
65 66 67 68
    } else {
      taosThreadMutexUnlock(&pPool->mutex);
      code = rc;
      goto _exit;
H
Hongze Cheng 已提交
69 70 71
    }
  }

H
Hongze Cheng 已提交
72 73 74 75
  taosThreadMutexUnlock(&pPool->mutex);

  // TODO: if (pPool->nQuery == 0) add to free list

H
Hongze Cheng 已提交
76
_exit:
H
Hongze Cheng 已提交
77 78 79
  if (code) {
    vError("vgId:%d %s failed since %s", TD_VID(pVnode), __func__, tstrerror(code));
  }
H
Hongze Cheng 已提交
80 81
  return code;
}
H
Hongze Cheng 已提交
82
int vnodeBegin(SVnode *pVnode) {
H
Hongze Cheng 已提交
83
  // alloc buffer pool
H
Hongze Cheng 已提交
84
  taosThreadMutexLock(&pVnode->mutex);
H
Hongze Cheng 已提交
85

H
Hongze Cheng 已提交
86
  int32_t nTry = 0;
H
Hongze Cheng 已提交
87 88
  while (++nTry) {
    if (pVnode->freeList) {
H
Hongze Cheng 已提交
89 90
      vDebug("vgId:%d allocate free buffer pool on %d try, pPool:%p id:%d", TD_VID(pVnode), nTry, pVnode->freeList,
             pVnode->freeList->id);
H
Hongze Cheng 已提交
91

H
Hongze Cheng 已提交
92 93 94 95 96 97 98
      pVnode->inUse = pVnode->freeList;
      pVnode->inUse->nRef = 1;
      pVnode->freeList = pVnode->inUse->freeNext;
      pVnode->inUse->freeNext = NULL;
      break;
    } else {
      vInfo("vgId:%d no free buffer pool on %d try, try to recycle...", TD_VID(pVnode), nTry);
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100 101
      terrno = vnodeTryRecycleBufPool(pVnode);
      if (terrno != TSDB_CODE_SUCCESS) {
H
Hongze Cheng 已提交
102 103 104 105 106
        vError("vgId:%d %s failed since %s", TD_VID(pVnode), __func__, tstrerror(terrno));
        taosThreadMutexUnlock(&pVnode->mutex);
        return -1;
      }

H
Hongze Cheng 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      if (pVnode->freeList == NULL) {
        vDebug("vgId:%d no free buffer pool on %d try, wait %d ms...", TD_VID(pVnode), nTry, WAIT_TIME_MILI_SEC);

        struct timeval  tv;
        struct timespec ts;
        taosGetTimeOfDay(&tv);
        ts.tv_sec = tv.tv_sec;
        ts.tv_nsec = tv.tv_usec * 1000 + WAIT_TIME_MILI_SEC * 1000000;

        int32_t rc = taosThreadCondTimedWait(&pVnode->poolNotEmpty, &pVnode->mutex, &ts);
        if (rc && rc != ETIMEDOUT) {
          terrno = TAOS_SYSTEM_ERROR(rc);
          vError("vgId:%d %s failed since %s", TD_VID(pVnode), __func__, tstrerror(terrno));
          taosThreadMutexUnlock(&pVnode->mutex);
          return -1;
        }
      }
H
Hongze Cheng 已提交
124 125
    }
  }
H
Hongze Cheng 已提交
126

H
Hongze Cheng 已提交
127
  taosThreadMutexUnlock(&pVnode->mutex);
H
Hongze Cheng 已提交
128

H
Hongze Cheng 已提交
129
  pVnode->state.commitID++;
H
Hongze Cheng 已提交
130
  // begin meta
131
  if (metaBegin(pVnode->pMeta, META_BEGIN_HEAP_BUFFERPOOL) < 0) {
S
Shengliang Guan 已提交
132
    vError("vgId:%d, failed to begin meta since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
133 134 135 136
    return -1;
  }

  // begin tsdb
H
Hongze Cheng 已提交
137 138 139 140
  if (tsdbBegin(pVnode->pTsdb) < 0) {
    vError("vgId:%d, failed to begin tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
    return -1;
  }
C
Cary Xu 已提交
141

C
Cary Xu 已提交
142
  // begin sma
C
Cary Xu 已提交
143
  if (VND_IS_RSMA(pVnode) && smaBegin(pVnode->pSma) < 0) {
C
Cary Xu 已提交
144 145 146
    vError("vgId:%d, failed to begin sma since %s", TD_VID(pVnode), tstrerror(terrno));
    return -1;
  }
C
Cary Xu 已提交
147

H
Hongze Cheng 已提交
148 149 150
  return 0;
}

C
Cary Xu 已提交
151 152
int vnodeShouldCommit(SVnode *pVnode) {
  if (pVnode->inUse) {
153
    return osDataSpaceAvailable() && (pVnode->inUse->size > pVnode->inUse->node.size);
C
Cary Xu 已提交
154 155 156
  }
  return false;
}
H
Hongze Cheng 已提交
157

H
Hongze Cheng 已提交
158 159 160
int vnodeSaveInfo(const char *dir, const SVnodeInfo *pInfo) {
  char      fname[TSDB_FILENAME_LEN];
  TdFilePtr pFile;
H
Hongze Cheng 已提交
161
  char     *data;
H
Hongze Cheng 已提交
162 163 164 165 166 167

  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);

  // encode info
  data = NULL;

H
Hongze Cheng 已提交
168
  if (vnodeEncodeInfo(pInfo, &data) < 0) {
169
    vError("failed to encode json info.");
H
Hongze Cheng 已提交
170 171 172 173 174 175
    return -1;
  }

  // save info to a vnode_tmp.json
  pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
176
    vError("failed to open info file:%s for write:%s", fname, terrstr());
H
Hongze Cheng 已提交
177 178 179 180
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

H
Hongze Cheng 已提交
181
  if (taosWriteFile(pFile, data, strlen(data)) < 0) {
182
    vError("failed to write info file:%s error:%s", fname, terrstr());
H
Hongze Cheng 已提交
183 184 185 186 187
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (taosFsyncFile(pFile) < 0) {
S
Shengliang Guan 已提交
188
    vError("failed to fsync info file:%s error:%s", fname, terrstr());
H
Hongze Cheng 已提交
189 190 191 192 193 194 195 196 197
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  taosCloseFile(&pFile);

  // free info binary
  taosMemoryFree(data);

198 199
  vInfo("vgId:%d, vnode info is saved, fname:%s replica:%d selfIndex:%d", pInfo->config.vgId, fname,
        pInfo->config.syncCfg.replicaNum, pInfo->config.syncCfg.myIndex);
H
Hongze Cheng 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  return 0;

_err:
  taosCloseFile(&pFile);
  taosMemoryFree(data);
  return -1;
}

int vnodeCommitInfo(const char *dir, const SVnodeInfo *pInfo) {
  char fname[TSDB_FILENAME_LEN];
  char tfname[TSDB_FILENAME_LEN];

  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);

  if (taosRenameFile(tfname, fname) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

S
Shengliang Guan 已提交
221
  vInfo("vgId:%d, vnode info is committed", pInfo->config.vgId);
H
Hongze Cheng 已提交
222 223 224 225

  return 0;
}

H
Hongze Cheng 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
int vnodeLoadInfo(const char *dir, SVnodeInfo *pInfo) {
  char      fname[TSDB_FILENAME_LEN];
  TdFilePtr pFile = NULL;
  char     *pData = NULL;
  int64_t   size;

  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);

  // read info
  pFile = taosOpenFile(fname, TD_FILE_READ);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (taosFStatFile(pFile, &size, NULL) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
246
  pData = taosMemoryMalloc(size + 1);
H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255 256
  if (pData == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  if (taosReadFile(pFile, pData, size) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
257 258
  pData[size] = '\0';

H
Hongze Cheng 已提交
259 260 261 262 263 264 265 266 267 268
  taosCloseFile(&pFile);

  // decode info
  if (vnodeDecodeInfo(pData, pInfo) < 0) {
    taosMemoryFree(pData);
    return -1;
  }

  taosMemoryFree(pData);

H
Hongze Cheng 已提交
269
  return 0;
H
Hongze Cheng 已提交
270 271 272 273 274

_err:
  taosCloseFile(&pFile);
  taosMemoryFree(pData);
  return -1;
H
Hongze Cheng 已提交
275 276
}

277 278 279 280 281
static int32_t vnodePrepareCommit(SVnode *pVnode, SCommitInfo *pInfo) {
  int32_t code = 0;
  int32_t lino = 0;
  char    dir[TSDB_FILENAME_LEN] = {0};

H
Hongze Cheng 已提交
282 283
  tsem_wait(&pVnode->canCommit);

H
Hongze Cheng 已提交
284 285 286 287 288 289
  taosThreadMutexLock(&pVnode->mutex);
  ASSERT(pVnode->onCommit == NULL);
  pVnode->onCommit = pVnode->inUse;
  pVnode->inUse = NULL;
  taosThreadMutexUnlock(&pVnode->mutex);

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  pVnode->state.commitTerm = pVnode->state.applyTerm;

  pInfo->info.config = pVnode->config;
  pInfo->info.state.committed = pVnode->state.applied;
  pInfo->info.state.commitTerm = pVnode->state.applyTerm;
  pInfo->info.state.commitID = pVnode->state.commitID;
  pInfo->pVnode = pVnode;
  pInfo->txn = metaGetTxn(pVnode->pMeta);

  // save info
  if (pVnode->pTfs) {
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path);
  } else {
    snprintf(dir, TSDB_FILENAME_LEN, "%s", pVnode->path);
  }
305 306

  vDebug("vgId:%d, save config while prepare commit", TD_VID(pVnode));
307 308 309 310 311
  if (vnodeSaveInfo(dir, &pInfo->info) < 0) {
    code = terrno;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

312
  tsdbPrepareCommit(pVnode->pTsdb);
K
kailixu 已提交
313

314 315
  metaPrepareAsyncCommit(pVnode->pMeta);

316 317
  code = smaPrepareAsyncCommit(pVnode->pSma);
  if (code) goto _exit;
K
kailixu 已提交
318

319 320 321 322 323 324 325
_exit:
  if (code) {
    vError("vgId:%d, %s failed at line %d since %s, commit id:%" PRId64, TD_VID(pVnode), __func__, lino,
           tstrerror(code), pVnode->state.commitID);
  } else {
    vDebug("vgId:%d, %s done", TD_VID(pVnode), __func__);
  }
326

327
  return code;
H
Hongze Cheng 已提交
328 329 330 331
}
static int32_t vnodeCommitTask(void *arg) {
  int32_t code = 0;

H
Hongze Cheng 已提交
332
  SCommitInfo *pInfo = (SCommitInfo *)arg;
H
Hongze Cheng 已提交
333
  SVnode      *pVnode = pInfo->pVnode;
H
Hongze Cheng 已提交
334

H
Hongze Cheng 已提交
335 336
  // commit
  code = vnodeCommitImpl(pInfo);
H
Hongze Cheng 已提交
337 338
  if (code) goto _exit;

H
Hongze Cheng 已提交
339 340 341
  // recycle buffer pool
  taosThreadMutexLock(&pVnode->mutex);

H
Hongze Cheng 已提交
342 343 344
  SVBufPool *pPool = pVnode->onCommit;
  int32_t    nRef = atomic_sub_fetch_32(&pPool->nRef, 1);

H
Hongze Cheng 已提交
345 346 347 348 349 350 351 352 353
  pVnode->onCommit = NULL;
  if (nRef == 0) {
    // add to free list
    vDebug("vgId:%d buffer pool %p of id %d is added to free list", TD_VID(pVnode), pPool, pPool->id);

    vnodeBufPoolReset(pPool);
    pPool->freeNext = pVnode->freeList;
    pVnode->freeList = pPool;
    taosThreadCondSignal(&pVnode->poolNotEmpty);
H
Hongze Cheng 已提交
354
  } else if (nRef > 0) {
H
Hongze Cheng 已提交
355 356 357 358 359 360 361 362 363 364 365 366
    // add to recycle list
    vDebug("vgId:%d buffer pool %p of id %d is added to recycle list", TD_VID(pVnode), pPool, pPool->id);

    if (pVnode->recycleTail == NULL) {
      pPool->recyclePrev = pPool->recycleNext = NULL;
      pVnode->recycleHead = pVnode->recycleTail = pPool;
    } else {
      pPool->recyclePrev = pVnode->recycleTail;
      pPool->recycleNext = NULL;
      pVnode->recycleTail->recycleNext = pPool;
      pVnode->recycleTail = pPool;
    }
H
Hongze Cheng 已提交
367 368
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
369 370 371 372
  }

  taosThreadMutexUnlock(&pVnode->mutex);

373
_exit:
H
Hongze Cheng 已提交
374
  // end commit
H
Hongze Cheng 已提交
375
  tsem_post(&pVnode->canCommit);
H
Hongze Cheng 已提交
376
  taosMemoryFree(pInfo);
H
Hongze Cheng 已提交
377 378
  return code;
}
H
refact  
Hongze Cheng 已提交
379
int vnodeAsyncCommit(SVnode *pVnode) {
H
Hongze Cheng 已提交
380
  int32_t code = 0;
H
Hongze Cheng 已提交
381

H
Hongze Cheng 已提交
382
  SCommitInfo *pInfo = (SCommitInfo *)taosMemoryCalloc(1, sizeof(*pInfo));
H
Hongze Cheng 已提交
383 384 385 386
  if (NULL == pInfo) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
387 388 389 390 391 392 393 394

  // prepare to commit
  code = vnodePrepareCommit(pVnode, pInfo);
  if (TSDB_CODE_SUCCESS != code) {
    goto _exit;
  }

  // schedule the task
395
  code = vnodeScheduleTask(vnodeCommitTask, pInfo);
H
Hongze Cheng 已提交
396

H
Hongze Cheng 已提交
397 398
_exit:
  if (code) {
399 400 401
    if (NULL != pInfo) {
      taosMemoryFree(pInfo);
    }
402 403
    tsem_post(&pVnode->canCommit);
    vError("vgId:%d, %s failed since %s, commit id:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code),
H
Hongze Cheng 已提交
404 405
           pVnode->state.commitID);
  } else {
406 407
    vInfo("vgId:%d, vnode async commit done, commitId:%" PRId64 " term:%" PRId64 " applied:%" PRId64, TD_VID(pVnode),
          pVnode->state.commitID, pVnode->state.applyTerm, pVnode->state.applied);
H
Hongze Cheng 已提交
408 409
  }
  return code;
H
Hongze Cheng 已提交
410
}
H
refact  
Hongze Cheng 已提交
411

H
Hongze Cheng 已提交
412 413
int vnodeSyncCommit(SVnode *pVnode) {
  vnodeAsyncCommit(pVnode);
H
Hongze Cheng 已提交
414 415
  tsem_wait(&pVnode->canCommit);
  tsem_post(&pVnode->canCommit);
H
Hongze Cheng 已提交
416 417 418
  return 0;
}

H
Hongze Cheng 已提交
419 420 421 422 423 424
static int vnodeCommitImpl(SCommitInfo *pInfo) {
  int32_t code = 0;
  int32_t lino = 0;

  char    dir[TSDB_FILENAME_LEN] = {0};
  SVnode *pVnode = pInfo->pVnode;
H
Hongze Cheng 已提交
425

426
  vInfo("vgId:%d, start to commit, commitId:%" PRId64 " version:%" PRId64 " term: %" PRId64, TD_VID(pVnode),
427
        pVnode->state.commitID, pVnode->state.applied, pVnode->state.applyTerm);
H
Hongze Cheng 已提交
428

429 430 431 432 433 434
  // persist wal before starting
  if (walPersist(pVnode->pWal) < 0) {
    vError("vgId:%d, failed to persist wal since %s", TD_VID(pVnode), terrstr());
    return -1;
  }

H
Hongze Cheng 已提交
435 436 437 438 439
  if (pVnode->pTfs) {
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path);
  } else {
    snprintf(dir, TSDB_FILENAME_LEN, "%s", pVnode->path);
  }
440 441 442

  // walBeginSnapshot(pVnode->pWal, pVnode->state.applied);
  syncBeginSnapshot(pVnode->sync, pVnode->state.applied);
H
Hongze Cheng 已提交
443 444

  // commit each sub-system
H
Hongze Cheng 已提交
445
  code = tsdbCommit(pVnode->pTsdb, pInfo);
C
Cary Xu 已提交
446 447
  TSDB_CHECK_CODE(code, lino, _exit);

C
Cary Xu 已提交
448
  if (VND_IS_RSMA(pVnode)) {
H
Hongze Cheng 已提交
449
    code = smaCommit(pVnode->pSma, pInfo);
H
Hongze Cheng 已提交
450
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
451
  }
C
Cary Xu 已提交
452

H
Hongze Cheng 已提交
453
  if (tqCommit(pVnode->pTq) < 0) {
H
Hongze Cheng 已提交
454 455
    code = TSDB_CODE_FAILED;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
456 457 458
  }

  // commit info
H
Hongze Cheng 已提交
459
  if (vnodeCommitInfo(dir, &pInfo->info) < 0) {
H
Hongze Cheng 已提交
460 461
    code = terrno;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
462
  }
H
Hongze Cheng 已提交
463

C
Cary Xu 已提交
464 465 466
  code = tsdbFinishCommit(pVnode->pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);

C
Cary Xu 已提交
467 468 469 470
  if (VND_IS_RSMA(pVnode)) {
    code = smaFinishCommit(pVnode->pSma);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
471

472
  if (metaFinishCommit(pVnode->pMeta, pInfo->txn) < 0) {
473 474 475 476
    code = terrno;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
477
  pVnode->state.committed = pInfo->info.state.committed;
H
Hongze Cheng 已提交
478

C
Cary Xu 已提交
479 480
  if (smaPostCommit(pVnode->pSma) < 0) {
    vError("vgId:%d, failed to post-commit sma since %s", TD_VID(pVnode), tstrerror(terrno));
C
Cary Xu 已提交
481 482
    return -1;
  }
H
Hongze Cheng 已提交
483

484 485
  // walEndSnapshot(pVnode->pWal);
  syncEndSnapshot(pVnode->sync);
H
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487 488
_exit:
  if (code) {
S
Shengliang Guan 已提交
489
    vError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
490 491 492
  } else {
    vInfo("vgId:%d, commit end", TD_VID(pVnode));
  }
H
Hongze Cheng 已提交
493 494 495
  return 0;
}

H
Hongze Cheng 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
bool vnodeShouldRollback(SVnode *pVnode) {
  char tFName[TSDB_FILENAME_LEN] = {0};
  snprintf(tFName, TSDB_FILENAME_LEN, "%s%s%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path, TD_DIRSEP,
           VND_INFO_FNAME_TMP);

  return taosCheckExistFile(tFName);
}

void vnodeRollback(SVnode *pVnode) {
  char tFName[TSDB_FILENAME_LEN] = {0};
  snprintf(tFName, TSDB_FILENAME_LEN, "%s%s%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path, TD_DIRSEP,
           VND_INFO_FNAME_TMP);

  (void)taosRemoveFile(tFName);
}

H
Hongze Cheng 已提交
512 513 514 515
static int vnodeEncodeState(const void *pObj, SJson *pJson) {
  const SVState *pState = (SVState *)pObj;

  if (tjsonAddIntegerToObject(pJson, "commit version", pState->committed) < 0) return -1;
H
Hongze Cheng 已提交
516
  if (tjsonAddIntegerToObject(pJson, "commit ID", pState->commitID) < 0) return -1;
H
Hongze Cheng 已提交
517
  if (tjsonAddIntegerToObject(pJson, "commit term", pState->commitTerm) < 0) return -1;
H
Hongze Cheng 已提交
518 519 520 521 522 523 524

  return 0;
}

static int vnodeDecodeState(const SJson *pJson, void *pObj) {
  SVState *pState = (SVState *)pObj;

525 526
  int32_t code;
  tjsonGetNumberValue(pJson, "commit version", pState->committed, code);
H
Hongze Cheng 已提交
527
  if (code < 0) return -1;
H
Hongze Cheng 已提交
528 529
  tjsonGetNumberValue(pJson, "commit ID", pState->commitID, code);
  if (code < 0) return -1;
H
Hongze Cheng 已提交
530 531
  tjsonGetNumberValue(pJson, "commit term", pState->commitTerm, code);
  if (code < 0) return -1;
H
Hongze Cheng 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

  return 0;
}

static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData) {
  SJson *pJson;
  char  *pData;

  *ppData = NULL;

  pJson = tjsonCreateObject();
  if (pJson == NULL) {
    return -1;
  }

  if (tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config) < 0) {
    goto _err;
  }

  if (tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state) < 0) {
    goto _err;
  }

  pData = tjsonToString(pJson);
  if (pData == NULL) {
    goto _err;
  }

  tjsonDelete(pJson);

  *ppData = pData;
H
Hongze Cheng 已提交
563
  return 0;
H
Hongze Cheng 已提交
564 565 566 567

_err:
  tjsonDelete(pJson);
  return -1;
H
Hongze Cheng 已提交
568 569
}

H
Hongze Cheng 已提交
570
int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
H
Hongze Cheng 已提交
571 572
  SJson *pJson = NULL;

H
fix bug  
Hongze Cheng 已提交
573
  pJson = tjsonParse(pData);
H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587
  if (pJson == NULL) {
    return -1;
  }

  if (tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config) < 0) {
    goto _err;
  }

  if (tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state) < 0) {
    goto _err;
  }

  tjsonDelete(pJson);

H
Hongze Cheng 已提交
588
  return 0;
H
Hongze Cheng 已提交
589 590 591 592

_err:
  tjsonDelete(pJson);
  return -1;
H
Hongze Cheng 已提交
593
}