syncRaftLog.c 23.8 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "syncRaftLog.h"
M
Minghao Li 已提交
17
#include "syncRaftCfg.h"
18
#include "syncRaftStore.h"
M
Minghao Li 已提交
19

20 21 22 23
//-------------------------------
// log[m .. n]

// public function
24
static int32_t   raftLogRestoreFromSnapshot(struct SSyncLogStore* pLogStore, SyncIndex snapshotIndex);
M
Minghao Li 已提交
25 26
static SyncIndex raftLogBeginIndex(struct SSyncLogStore* pLogStore);
static SyncIndex raftLogEndIndex(struct SSyncLogStore* pLogStore);
M
Minghao Li 已提交
27
static SyncIndex raftLogWriteIndex(struct SSyncLogStore* pLogStore);
M
Minghao Li 已提交
28 29 30 31 32 33 34
static bool      raftLogIsEmpty(struct SSyncLogStore* pLogStore);
static int32_t   raftLogEntryCount(struct SSyncLogStore* pLogStore);
static SyncIndex raftLogLastIndex(struct SSyncLogStore* pLogStore);
static SyncTerm  raftLogLastTerm(struct SSyncLogStore* pLogStore);
static int32_t   raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
static int32_t   raftLogGetEntry(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry);
static int32_t   raftLogTruncate(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);
35
static bool      raftLogExist(struct SSyncLogStore* pLogStore, SyncIndex index);
M
Minghao Li 已提交
36

37
// private function
M
Minghao Li 已提交
38 39 40
static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry);

//-------------------------------
41
// log[0 .. n]
42
static SSyncRaftEntry* logStoreGetLastEntry(SSyncLogStore* pLogStore);
M
Minghao Li 已提交
43 44 45 46 47 48 49
static SyncIndex       logStoreLastIndex(SSyncLogStore* pLogStore);
static SyncTerm        logStoreLastTerm(SSyncLogStore* pLogStore);
static SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index);
static int32_t         logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
static int32_t         logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex);
static int32_t         logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index);
static SyncIndex       logStoreGetCommitIndex(SSyncLogStore* pLogStore);
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64
//-------------------------------
SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode) {
  SSyncLogStore* pLogStore = taosMemoryMalloc(sizeof(SSyncLogStore));
  ASSERT(pLogStore != NULL);

  pLogStore->data = taosMemoryMalloc(sizeof(SSyncLogStoreData));
  ASSERT(pLogStore->data != NULL);

  SSyncLogStoreData* pData = pLogStore->data;
  pData->pSyncNode = pSyncNode;
  pData->pWal = pSyncNode->pWal;
  ASSERT(pData->pWal != NULL);

  taosThreadMutexInit(&(pData->mutex), NULL);
L
Liu Jicong 已提交
65
  pData->pWalHandle = walOpenReader(pData->pWal, NULL);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  ASSERT(pData->pWalHandle != NULL);

  pLogStore->appendEntry = logStoreAppendEntry;
  pLogStore->getEntry = logStoreGetEntry;
  pLogStore->truncate = logStoreTruncate;
  pLogStore->getLastIndex = logStoreLastIndex;
  pLogStore->getLastTerm = logStoreLastTerm;
  pLogStore->updateCommitIndex = logStoreUpdateCommitIndex;
  pLogStore->getCommitIndex = logStoreGetCommitIndex;

  pLogStore->syncLogRestoreFromSnapshot = raftLogRestoreFromSnapshot;
  pLogStore->syncLogBeginIndex = raftLogBeginIndex;
  pLogStore->syncLogEndIndex = raftLogEndIndex;
  pLogStore->syncLogIsEmpty = raftLogIsEmpty;
  pLogStore->syncLogEntryCount = raftLogEntryCount;
  pLogStore->syncLogLastIndex = raftLogLastIndex;
  pLogStore->syncLogLastTerm = raftLogLastTerm;
  pLogStore->syncLogAppendEntry = raftLogAppendEntry;
  pLogStore->syncLogGetEntry = raftLogGetEntry;
  pLogStore->syncLogTruncate = raftLogTruncate;
  pLogStore->syncLogWriteIndex = raftLogWriteIndex;
87
  pLogStore->syncLogExist = raftLogExist;
88 89 90 91 92 93 94 95 96 97

  return pLogStore;
}

void logStoreDestory(SSyncLogStore* pLogStore) {
  if (pLogStore != NULL) {
    SSyncLogStoreData* pData = pLogStore->data;

    taosThreadMutexLock(&(pData->mutex));
    if (pData->pWalHandle != NULL) {
L
Liu Jicong 已提交
98
      walCloseReader(pData->pWalHandle);
99 100 101 102 103 104 105 106 107 108 109 110
      pData->pWalHandle = NULL;
    }
    taosThreadMutexUnlock(&(pData->mutex));
    taosThreadMutexDestroy(&(pData->mutex));

    taosMemoryFree(pLogStore->data);
    taosMemoryFree(pLogStore);
  }
}

//-------------------------------
// log[m .. n]
111 112
static int32_t raftLogRestoreFromSnapshot(struct SSyncLogStore* pLogStore, SyncIndex snapshotIndex) {
  ASSERT(snapshotIndex >= 0);
M
Minghao Li 已提交
113 114 115

  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
116 117 118 119 120 121
  int32_t            code = walRestoreFromSnapshot(pWal, snapshotIndex);
  if (code != 0) {
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
122

123 124 125 126 127
    char logBuf[128];
    snprintf(logBuf, sizeof(logBuf),
             "wal restore from snapshot error, index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", snapshotIndex, err,
             err, errStr, sysErr, sysErrStr);
    syncNodeErrorLog(pData->pSyncNode, logBuf);
128

129
    return -1;
130
  }
131 132 133

  return 0;
}
M
Minghao Li 已提交
134 135 136 137

static SyncIndex raftLogBeginIndex(struct SSyncLogStore* pLogStore) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
138 139
  SyncIndex          firstVer = walGetFirstVer(pWal);
  return firstVer;
M
Minghao Li 已提交
140 141 142 143 144
}

static SyncIndex raftLogEndIndex(struct SSyncLogStore* pLogStore) { return raftLogLastIndex(pLogStore); }

static bool raftLogIsEmpty(struct SSyncLogStore* pLogStore) {
145 146 147
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  return walIsEmpty(pWal);
M
Minghao Li 已提交
148 149 150 151 152
}

static int32_t raftLogEntryCount(struct SSyncLogStore* pLogStore) {
  SyncIndex beginIndex = raftLogBeginIndex(pLogStore);
  SyncIndex endIndex = raftLogEndIndex(pLogStore);
M
Minghao Li 已提交
153
  int32_t   count = endIndex - beginIndex + 1;
M
Minghao Li 已提交
154 155 156 157
  return count > 0 ? count : 0;
}

static SyncIndex raftLogLastIndex(struct SSyncLogStore* pLogStore) {
M
Minghao Li 已提交
158
  SyncIndex          lastIndex;
M
Minghao Li 已提交
159 160 161
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  SyncIndex          lastVer = walGetLastVer(pWal);
M
Minghao Li 已提交
162

163
  return lastVer;
M
Minghao Li 已提交
164 165 166 167 168 169 170
}

static SyncIndex raftLogWriteIndex(struct SSyncLogStore* pLogStore) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  SyncIndex          lastVer = walGetLastVer(pWal);
  return lastVer + 1;
M
Minghao Li 已提交
171 172
}

173 174 175 176 177 178 179
static bool raftLogExist(struct SSyncLogStore* pLogStore, SyncIndex index) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  bool               b = walLogExist(pWal, index);
  return b;
}

180 181 182
// if success, return last term
// if not log, return 0
// if error, return SYNC_TERM_INVALID
183 184 185 186 187 188 189 190
static SyncTerm raftLogLastTerm(struct SSyncLogStore* pLogStore) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  if (walIsEmpty(pWal)) {
    return 0;
  } else {
    SSyncRaftEntry* pLastEntry;
    int32_t         code = raftLogGetLastEntry(pLogStore, &pLastEntry);
191 192 193 194 195 196 197
    if (code == 0 && pLastEntry != NULL) {
      SyncTerm lastTerm = pLastEntry->term;
      taosMemoryFree(pLastEntry);
      return lastTerm;
    } else {
      return SYNC_TERM_INVALID;
    }
198 199
  }

200 201
  // can not be here!
  return SYNC_TERM_INVALID;
202 203
}

M
Minghao Li 已提交
204 205 206 207
static int32_t raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;

M
Minghao Li 已提交
208
  SyncIndex writeIndex = raftLogWriteIndex(pLogStore);
209
  if (pEntry->index != writeIndex) {
210 211
    sError("vgId:%d wal write index error, entry-index:%ld update to %ld", pData->pSyncNode->vgId, pEntry->index,
           writeIndex);
212 213
    pEntry->index = writeIndex;
  }
M
Minghao Li 已提交
214 215 216 217 218 219 220 221 222 223

  int          code = 0;
  SSyncLogMeta syncMeta;
  syncMeta.isWeek = pEntry->isWeak;
  syncMeta.seqNum = pEntry->seqNum;
  syncMeta.term = pEntry->term;
  code = walWriteWithSyncInfo(pWal, pEntry->index, pEntry->originalRpcType, syncMeta, pEntry->data, pEntry->dataLen);
  if (code != 0) {
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
224 225
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
226 227 228 229 230 231

    char logBuf[128];
    snprintf(logBuf, sizeof(logBuf), "wal write error, index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s",
             pEntry->index, err, err, errStr, sysErr, sysErrStr);
    syncNodeErrorLog(pData->pSyncNode, logBuf);

M
Minghao Li 已提交
232 233 234
    ASSERT(0);
  }

235
  // walFsync(pWal, true);
236

237 238 239 240 241 242
  do {
    char eventLog[128];
    snprintf(eventLog, sizeof(eventLog), "write index:%ld, type:%s,%d, type2:%s,%d", pEntry->index,
             TMSG_INFO(pEntry->msgType), pEntry->msgType, TMSG_INFO(pEntry->originalRpcType), pEntry->originalRpcType);
    syncNodeEventLog(pData->pSyncNode, eventLog);
  } while (0);
243

M
Minghao Li 已提交
244 245 246
  return code;
}

247 248 249
// entry found, return 0
// entry not found, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST
// other error, return -1
250 251 252 253 254 255 256
static int32_t raftLogGetEntry(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  int32_t            code;

  *ppEntry = NULL;

257
  // SWalReadHandle* pWalHandle = walOpenReadHandle(pWal);
L
Liu Jicong 已提交
258
  SWalReader* pWalHandle = pData->pWalHandle;
259
  if (pWalHandle == NULL) {
260
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
261 262 263
    return -1;
  }

264 265
  taosThreadMutexLock(&(pData->mutex));

L
Liu Jicong 已提交
266
  code = walReadVer(pWalHandle, index);
267 268 269
  if (code != 0) {
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
270 271
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
272 273 274 275 276 277 278 279 280 281 282

    do {
      char logBuf[128];
      snprintf(logBuf, sizeof(logBuf), "wal read error, index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", index, err,
               err, errStr, sysErr, sysErrStr);
      if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST) {
        syncNodeEventLog(pData->pSyncNode, logBuf);
      } else {
        syncNodeErrorLog(pData->pSyncNode, logBuf);
      }
    } while (0);
283

284 285 286 287 288
    /*
        int32_t saveErr = terrno;
        walCloseReadHandle(pWalHandle);
        terrno = saveErr;
    */
M
Minghao Li 已提交
289

290
    taosThreadMutexUnlock(&(pData->mutex));
291 292 293 294 295 296 297 298 299 300 301 302 303 304
    return code;
  }

  *ppEntry = syncEntryBuild(pWalHandle->pHead->head.bodyLen);
  ASSERT(*ppEntry != NULL);
  (*ppEntry)->msgType = TDMT_SYNC_CLIENT_REQUEST;
  (*ppEntry)->originalRpcType = pWalHandle->pHead->head.msgType;
  (*ppEntry)->seqNum = pWalHandle->pHead->head.syncMeta.seqNum;
  (*ppEntry)->isWeak = pWalHandle->pHead->head.syncMeta.isWeek;
  (*ppEntry)->term = pWalHandle->pHead->head.syncMeta.term;
  (*ppEntry)->index = index;
  ASSERT((*ppEntry)->dataLen == pWalHandle->pHead->head.bodyLen);
  memcpy((*ppEntry)->data, pWalHandle->pHead->head.body, pWalHandle->pHead->head.bodyLen);

305 306 307 308 309
  /*
    int32_t saveErr = terrno;
    walCloseReadHandle(pWalHandle);
    terrno = saveErr;
  */
310

311
  taosThreadMutexUnlock(&(pData->mutex));
312 313
  return code;
}
M
Minghao Li 已提交
314 315 316 317 318 319 320 321

static int32_t raftLogTruncate(struct SSyncLogStore* pLogStore, SyncIndex fromIndex) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  int32_t            code = walRollback(pWal, fromIndex);
  if (code != 0) {
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
322 323 324 325 326
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
    sError("vgId:%d wal truncate error, from-index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s",
           pData->pSyncNode->vgId, fromIndex, err, err, errStr, sysErr, sysErrStr);

M
Minghao Li 已提交
327 328
    ASSERT(0);
  }
M
Minghao Li 已提交
329 330 331 332 333 334 335 336

  // event log
  do {
    char logBuf[128];
    snprintf(logBuf, sizeof(logBuf), "wal truncate, from-index:%ld", fromIndex);
    syncNodeEventLog(pData->pSyncNode, logBuf);
  } while (0);

M
Minghao Li 已提交
337 338 339
  return code;
}

340 341 342
// entry found, return 0
// entry not found, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST
// other error, return -1
343 344 345 346 347 348 349 350 351 352 353
static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  ASSERT(ppLastEntry != NULL);

  *ppLastEntry = NULL;
  if (walIsEmpty(pWal)) {
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  } else {
    SyncIndex lastIndex = raftLogLastIndex(pLogStore);
354 355
    ASSERT(lastIndex >= SYNC_INDEX_BEGIN);
    int32_t code = raftLogGetEntry(pLogStore, lastIndex, ppLastEntry);
356 357 358 359 360 361
    return code;
  }

  return -1;
}

M
Minghao Li 已提交
362
//-------------------------------
363
// log[0 .. n]
M
Minghao Li 已提交
364 365 366 367
int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;

M
Minghao Li 已提交
368
  SyncIndex lastIndex = logStoreLastIndex(pLogStore);
M
Minghao Li 已提交
369
  ASSERT(pEntry->index == lastIndex + 1);
M
Minghao Li 已提交
370

M
Minghao Li 已提交
371 372 373 374 375 376
  int          code = 0;
  SSyncLogMeta syncMeta;
  syncMeta.isWeek = pEntry->isWeak;
  syncMeta.seqNum = pEntry->seqNum;
  syncMeta.term = pEntry->term;
  code = walWriteWithSyncInfo(pWal, pEntry->index, pEntry->originalRpcType, syncMeta, pEntry->data, pEntry->dataLen);
M
Minghao Li 已提交
377
  if (code != 0) {
M
Minghao Li 已提交
378 379
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
380 381
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
382 383 384 385 386

    char logBuf[128];
    snprintf(logBuf, sizeof(logBuf), "wal write error, index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s",
             pEntry->index, err, err, errStr, sysErr, sysErrStr);
    syncNodeErrorLog(pData->pSyncNode, logBuf);
387

M
Minghao Li 已提交
388
    ASSERT(0);
M
Minghao Li 已提交
389
  }
M
Minghao Li 已提交
390

391
  // walFsync(pWal, true);
392

M
Minghao Li 已提交
393 394 395 396
  char eventLog[128];
  snprintf(eventLog, sizeof(eventLog), "old write index:%ld, type:%s,%d, type2:%s,%d", pEntry->index,
           TMSG_INFO(pEntry->msgType), pEntry->msgType, TMSG_INFO(pEntry->originalRpcType), pEntry->originalRpcType);
  syncNodeEventLog(pData->pSyncNode, eventLog);
397

M
Minghao Li 已提交
398
  return code;
M
Minghao Li 已提交
399
}
M
Minghao Li 已提交
400

M
Minghao Li 已提交
401 402 403 404
SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;

M
Minghao Li 已提交
405
  if (index >= SYNC_INDEX_BEGIN && index <= logStoreLastIndex(pLogStore)) {
406 407
    taosThreadMutexLock(&(pData->mutex));

408
    // SWalReadHandle* pWalHandle = walOpenReadHandle(pWal);
L
Liu Jicong 已提交
409
    SWalReader* pWalHandle = pData->pWalHandle;
M
Minghao Li 已提交
410 411
    ASSERT(pWalHandle != NULL);

L
Liu Jicong 已提交
412
    int32_t code = walReadVer(pWalHandle, index);
M
Minghao Li 已提交
413
    if (code != 0) {
M
Minghao Li 已提交
414 415
      int32_t     err = terrno;
      const char* errStr = tstrerror(err);
416 417
      int32_t     sysErr = errno;
      const char* sysErrStr = strerror(errno);
418 419 420 421 422 423 424 425 426 427 428

      do {
        char logBuf[128];
        snprintf(logBuf, sizeof(logBuf), "wal read error, index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", index,
                 err, err, errStr, sysErr, sysErrStr);
        if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST) {
          syncNodeEventLog(pData->pSyncNode, logBuf);
        } else {
          syncNodeErrorLog(pData->pSyncNode, logBuf);
        }
      } while (0);
429

M
Minghao Li 已提交
430
      ASSERT(0);
M
Minghao Li 已提交
431
    }
M
Minghao Li 已提交
432

L
Liu Jicong 已提交
433
    SSyncRaftEntry* pEntry = syncEntryBuild(pWalHandle->pHead->head.bodyLen);
M
Minghao Li 已提交
434
    ASSERT(pEntry != NULL);
M
Minghao Li 已提交
435

436
    pEntry->msgType = TDMT_SYNC_CLIENT_REQUEST;
M
Minghao Li 已提交
437 438 439 440 441
    pEntry->originalRpcType = pWalHandle->pHead->head.msgType;
    pEntry->seqNum = pWalHandle->pHead->head.syncMeta.seqNum;
    pEntry->isWeak = pWalHandle->pHead->head.syncMeta.isWeek;
    pEntry->term = pWalHandle->pHead->head.syncMeta.term;
    pEntry->index = index;
M
Minghao Li 已提交
442
    ASSERT(pEntry->dataLen == pWalHandle->pHead->head.bodyLen);
L
Liu Jicong 已提交
443
    memcpy(pEntry->data, pWalHandle->pHead->head.body, pWalHandle->pHead->head.bodyLen);
M
Minghao Li 已提交
444

445 446 447 448 449
    /*
        int32_t saveErr = terrno;
        walCloseReadHandle(pWalHandle);
        terrno = saveErr;
    */
M
Minghao Li 已提交
450

451
    taosThreadMutexUnlock(&(pData->mutex));
M
Minghao Li 已提交
452
    return pEntry;
M
Minghao Li 已提交
453

M
Minghao Li 已提交
454 455 456
  } else {
    return NULL;
  }
M
Minghao Li 已提交
457
}
M
Minghao Li 已提交
458

M
Minghao Li 已提交
459 460 461
int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
M
Minghao Li 已提交
462
  // ASSERT(walRollback(pWal, fromIndex) == 0);
M
Minghao Li 已提交
463 464
  int32_t code = walRollback(pWal, fromIndex);
  if (code != 0) {
M
Minghao Li 已提交
465 466
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
467 468 469 470 471
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
    sError("vgId:%d wal truncate error, from-index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s",
           pData->pSyncNode->vgId, fromIndex, err, err, errStr, sysErr, sysErrStr);

M
Minghao Li 已提交
472
    ASSERT(0);
M
Minghao Li 已提交
473
  }
M
Minghao Li 已提交
474 475 476 477 478 479 480 481

  // event log
  do {
    char logBuf[128];
    snprintf(logBuf, sizeof(logBuf), "wal truncate, from-index:%ld", fromIndex);
    syncNodeEventLog(pData->pSyncNode, logBuf);
  } while (0);

M
Minghao Li 已提交
482
  return 0;
M
Minghao Li 已提交
483
}
M
Minghao Li 已提交
484

M
Minghao Li 已提交
485
SyncIndex logStoreLastIndex(SSyncLogStore* pLogStore) {
M
Minghao Li 已提交
486 487
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
M
Minghao Li 已提交
488
  SyncIndex          lastIndex = walGetLastVer(pWal);
M
Minghao Li 已提交
489 490
  return lastIndex;
}
M
Minghao Li 已提交
491

M
Minghao Li 已提交
492
SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore) {
M
Minghao Li 已提交
493
  SyncTerm        lastTerm = 0;
M
Minghao Li 已提交
494
  SSyncRaftEntry* pLastEntry = logStoreGetLastEntry(pLogStore);
M
Minghao Li 已提交
495 496
  if (pLastEntry != NULL) {
    lastTerm = pLastEntry->term;
wafwerar's avatar
wafwerar 已提交
497
    taosMemoryFree(pLastEntry);
M
Minghao Li 已提交
498
  }
M
Minghao Li 已提交
499 500
  return lastTerm;
}
M
Minghao Li 已提交
501

M
Minghao Li 已提交
502
int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index) {
503 504
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
M
Minghao Li 已提交
505
  // ASSERT(walCommit(pWal, index) == 0);
506 507 508 509
  int32_t code = walCommit(pWal, index);
  if (code != 0) {
    int32_t     err = terrno;
    const char* errStr = tstrerror(err);
510 511 512 513 514
    int32_t     sysErr = errno;
    const char* sysErrStr = strerror(errno);
    sError("vgId:%d wal update commit index error, index:%ld, err:%d %X, msg:%s, syserr:%d, sysmsg:%s",
           pData->pSyncNode->vgId, index, err, err, errStr, sysErr, sysErrStr);

515 516
    ASSERT(0);
  }
M
Minghao Li 已提交
517
  return 0;
M
Minghao Li 已提交
518
}
M
Minghao Li 已提交
519

M
Minghao Li 已提交
520 521 522 523 524 525 526 527 528
SyncIndex logStoreGetCommitIndex(SSyncLogStore* pLogStore) {
  SSyncLogStoreData* pData = pLogStore->data;
  return pData->pSyncNode->commitIndex;
}

SSyncRaftEntry* logStoreGetLastEntry(SSyncLogStore* pLogStore) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  SyncIndex          lastIndex = walGetLastVer(pWal);
M
Minghao Li 已提交
529 530 531 532 533

  SSyncRaftEntry* pEntry = NULL;
  if (lastIndex > 0) {
    pEntry = logStoreGetEntry(pLogStore, lastIndex);
  }
M
Minghao Li 已提交
534 535
  return pEntry;
}
M
Minghao Li 已提交
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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
cJSON* logStore2Json(SSyncLogStore* pLogStore) {
  char               u64buf[128] = {0};
  SSyncLogStoreData* pData = (SSyncLogStoreData*)pLogStore->data;
  cJSON*             pRoot = cJSON_CreateObject();

  if (pData != NULL && pData->pWal != NULL) {
    snprintf(u64buf, sizeof(u64buf), "%p", pData->pSyncNode);
    cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%p", pData->pWal);
    cJSON_AddStringToObject(pRoot, "pWal", u64buf);

    SyncIndex beginIndex = raftLogBeginIndex(pLogStore);
    snprintf(u64buf, sizeof(u64buf), "%ld", beginIndex);
    cJSON_AddStringToObject(pRoot, "beginIndex", u64buf);

    SyncIndex endIndex = raftLogEndIndex(pLogStore);
    snprintf(u64buf, sizeof(u64buf), "%ld", endIndex);
    cJSON_AddStringToObject(pRoot, "endIndex", u64buf);

    int32_t count = raftLogEntryCount(pLogStore);
    cJSON_AddNumberToObject(pRoot, "entryCount", count);

    snprintf(u64buf, sizeof(u64buf), "%ld", raftLogWriteIndex(pLogStore));
    cJSON_AddStringToObject(pRoot, "WriteIndex", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%d", raftLogIsEmpty(pLogStore));
    cJSON_AddStringToObject(pRoot, "IsEmpty", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%ld", raftLogLastIndex(pLogStore));
    cJSON_AddStringToObject(pRoot, "LastIndex", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%lu", raftLogLastTerm(pLogStore));
    cJSON_AddStringToObject(pRoot, "LastTerm", u64buf);

    cJSON* pEntries = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "pEntries", pEntries);

    if (!raftLogIsEmpty(pLogStore)) {
      for (SyncIndex i = beginIndex; i <= endIndex; ++i) {
        SSyncRaftEntry* pEntry = logStoreGetEntry(pLogStore, i);
        cJSON_AddItemToArray(pEntries, syncEntry2Json(pEntry));
        syncEntryDestory(pEntry);
      }
    }
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SSyncLogStore", pRoot);
  return pJson;
}
M
Minghao Li 已提交
586 587 588 589 590 591

char* logStore2Str(SSyncLogStore* pLogStore) {
  cJSON* pJson = logStore2Json(pLogStore);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
592 593
}

M
Minghao Li 已提交
594
cJSON* logStoreSimple2Json(SSyncLogStore* pLogStore) {
595
  char               u64buf[128] = {0};
M
Minghao Li 已提交
596 597 598 599 600 601 602 603
  SSyncLogStoreData* pData = (SSyncLogStoreData*)pLogStore->data;
  cJSON*             pRoot = cJSON_CreateObject();

  if (pData != NULL && pData->pWal != NULL) {
    snprintf(u64buf, sizeof(u64buf), "%p", pData->pSyncNode);
    cJSON_AddStringToObject(pRoot, "pSyncNode", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%p", pData->pWal);
    cJSON_AddStringToObject(pRoot, "pWal", u64buf);
M
Minghao Li 已提交
604

605 606
    SyncIndex beginIndex = raftLogBeginIndex(pLogStore);
    snprintf(u64buf, sizeof(u64buf), "%ld", beginIndex);
M
Minghao Li 已提交
607
    cJSON_AddStringToObject(pRoot, "beginIndex", u64buf);
M
Minghao Li 已提交
608 609 610 611 612 613 614

    SyncIndex endIndex = raftLogEndIndex(pLogStore);
    snprintf(u64buf, sizeof(u64buf), "%ld", endIndex);
    cJSON_AddStringToObject(pRoot, "endIndex", u64buf);

    int32_t count = raftLogEntryCount(pLogStore);
    cJSON_AddNumberToObject(pRoot, "entryCount", count);
M
Minghao Li 已提交
615 616 617 618 619 620 621 622 623 624 625

    snprintf(u64buf, sizeof(u64buf), "%ld", raftLogWriteIndex(pLogStore));
    cJSON_AddStringToObject(pRoot, "WriteIndex", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%d", raftLogIsEmpty(pLogStore));
    cJSON_AddStringToObject(pRoot, "IsEmpty", u64buf);

    snprintf(u64buf, sizeof(u64buf), "%ld", raftLogLastIndex(pLogStore));
    cJSON_AddStringToObject(pRoot, "LastIndex", u64buf);
    snprintf(u64buf, sizeof(u64buf), "%lu", raftLogLastTerm(pLogStore));
    cJSON_AddStringToObject(pRoot, "LastTerm", u64buf);
M
Minghao Li 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639
  }

  cJSON* pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SSyncLogStoreSimple", pRoot);
  return pJson;
}

char* logStoreSimple2Str(SSyncLogStore* pLogStore) {
  cJSON* pJson = logStoreSimple2Json(pLogStore);
  char*  serialized = cJSON_Print(pJson);
  cJSON_Delete(pJson);
  return serialized;
}

M
Minghao Li 已提交
640 641 642 643 644 645
SyncIndex logStoreFirstIndex(SSyncLogStore* pLogStore) {
  SSyncLogStoreData* pData = pLogStore->data;
  SWal*              pWal = pData->pWal;
  return walGetFirstVer(pWal);
}

M
Minghao Li 已提交
646
// for debug -----------------
M
Minghao Li 已提交
647
void logStorePrint(SSyncLogStore* pLogStore) {
M
Minghao Li 已提交
648 649 650
  char* serialized = logStore2Str(pLogStore);
  printf("logStorePrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
651
  taosMemoryFree(serialized);
M
Minghao Li 已提交
652 653 654 655
}

void logStorePrint2(char* s, SSyncLogStore* pLogStore) {
  char* serialized = logStore2Str(pLogStore);
M
Minghao Li 已提交
656
  printf("logStorePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
M
Minghao Li 已提交
657
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
658
  taosMemoryFree(serialized);
M
Minghao Li 已提交
659
}
M
Minghao Li 已提交
660

M
Minghao Li 已提交
661
void logStoreLog(SSyncLogStore* pLogStore) {
662 663 664 665 666
  if (gRaftDetailLog) {
    char* serialized = logStore2Str(pLogStore);
    sTraceLong("logStoreLog | len:%lu | %s", strlen(serialized), serialized);
    taosMemoryFree(serialized);
  }
M
Minghao Li 已提交
667 668 669
}

void logStoreLog2(char* s, SSyncLogStore* pLogStore) {
670 671 672 673 674
  if (gRaftDetailLog) {
    char* serialized = logStore2Str(pLogStore);
    sTraceLong("logStoreLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
675
}
M
Minghao Li 已提交
676 677 678 679 680 681

// for debug -----------------
void logStoreSimplePrint(SSyncLogStore* pLogStore) {
  char* serialized = logStoreSimple2Str(pLogStore);
  printf("logStoreSimplePrint | len:%lu | %s \n", strlen(serialized), serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
682
  taosMemoryFree(serialized);
M
Minghao Li 已提交
683 684 685 686 687 688
}

void logStoreSimplePrint2(char* s, SSyncLogStore* pLogStore) {
  char* serialized = logStoreSimple2Str(pLogStore);
  printf("logStoreSimplePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
  fflush(NULL);
wafwerar's avatar
wafwerar 已提交
689
  taosMemoryFree(serialized);
M
Minghao Li 已提交
690 691 692 693 694
}

void logStoreSimpleLog(SSyncLogStore* pLogStore) {
  char* serialized = logStoreSimple2Str(pLogStore);
  sTrace("logStoreSimpleLog | len:%lu | %s", strlen(serialized), serialized);
wafwerar's avatar
wafwerar 已提交
695
  taosMemoryFree(serialized);
M
Minghao Li 已提交
696 697 698
}

void logStoreSimpleLog2(char* s, SSyncLogStore* pLogStore) {
M
Minghao Li 已提交
699 700 701 702 703
  if (gRaftDetailLog) {
    char* serialized = logStoreSimple2Str(pLogStore);
    sTrace("logStoreSimpleLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
    taosMemoryFree(serialized);
  }
L
Liu Jicong 已提交
704
}