syncTimeout.c 4.3 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 "syncTimeout.h"
M
Minghao Li 已提交
17
#include "syncElection.h"
18
#include "syncRaftCfg.h"
M
Minghao Li 已提交
19
#include "syncRaftLog.h"
M
Minghao Li 已提交
20
#include "syncReplication.h"
M
Minghao Li 已提交
21
#include "syncRespMgr.h"
M
Minghao Li 已提交
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static void syncNodeCleanConfigIndex(SSyncNode* ths) {
  int32_t   newArrIndex = 0;
  SyncIndex newConfigIndexArr[MAX_CONFIG_INDEX_COUNT];
  memset(newConfigIndexArr, 0, sizeof(newConfigIndexArr));

  SSnapshot snapshot = {0};
  if (ths->pFsm != NULL && ths->pFsm->FpGetSnapshotInfo != NULL) {
    ths->pFsm->FpGetSnapshotInfo(ths->pFsm, &snapshot);
  }

  if (snapshot.lastApplyIndex != SYNC_INDEX_INVALID) {
    for (int i = 0; i < ths->pRaftCfg->configIndexCount; ++i) {
      if (ths->pRaftCfg->configIndexArr[i] < snapshot.lastConfigIndex) {
        // pass
        ;
      } else {
        // save
        newConfigIndexArr[newArrIndex] = ths->pRaftCfg->configIndexArr[i];
        ++newArrIndex;
      }
    }

    int32_t oldCnt = ths->pRaftCfg->configIndexCount;
    ths->pRaftCfg->configIndexCount = newArrIndex;
    memcpy(ths->pRaftCfg->configIndexArr, newConfigIndexArr, sizeof(newConfigIndexArr));

    int32_t code = raftCfgPersist(ths->pRaftCfg);
    ASSERT(code == 0);

    do {
      char logBuf[128];
      snprintf(logBuf, sizeof(logBuf), "clean config index arr, old-cnt:%d, new-cnt:%d", oldCnt,
               ths->pRaftCfg->configIndexCount);
      syncNodeEventLog(ths, logBuf);
    } while (0);
  }
}

M
Minghao Li 已提交
61
int32_t syncNodeTimerRoutine(SSyncNode* ths) {
62
  syncNodeEventLog(ths, "timer routines");
M
Minghao Li 已提交
63

64 65 66
  // timer replicate
  syncNodeReplicate(ths);

M
Minghao Li 已提交
67
  // clean mnode index
68
  if (syncNodeIsMnode(ths)) {
69 70 71
    syncNodeCleanConfigIndex(ths);
  }

M
Minghao Li 已提交
72 73 74 75 76 77 78
  // end timeout wal snapshot
  int64_t timeNow = taosGetTimestampMs();
  if (timeNow - ths->snapshottingIndex > SYNC_DEL_WAL_MS &&
      atomic_load_64(&ths->snapshottingIndex) != SYNC_INDEX_INVALID) {
    SSyncLogStoreData* pData = ths->pLogStore->data;
    int32_t            code = walEndSnapshot(pData->pWal);
    if (code != 0) {
M
Minghao Li 已提交
79
      sError("vgId:%d, wal snapshot end error since:%s", ths->vgId, terrstr(terrno));
M
Minghao Li 已提交
80 81 82 83
      return -1;
    } else {
      do {
        char logBuf[256];
S
Shengliang Guan 已提交
84
        snprintf(logBuf, sizeof(logBuf), "wal snapshot end, index:%" PRId64, atomic_load_64(&ths->snapshottingIndex));
M
Minghao Li 已提交
85 86 87 88 89 90 91
        syncNodeEventLog(ths, logBuf);
      } while (0);

      atomic_store_64(&ths->snapshottingIndex, SYNC_INDEX_INVALID);
    }
  }

92
#if 0
93
  if (!syncNodeIsMnode(ths)) {
M
Minghao Li 已提交
94 95
    syncRespClean(ths->pSyncRespMgr);
  }
96 97
#endif

M
Minghao Li 已提交
98 99 100
  return 0;
}

101
int32_t syncNodeOnTimer(SSyncNode* ths, SyncTimeout* pMsg) {
M
Minghao Li 已提交
102
  int32_t ret = 0;
103
  syncLogRecvTimer(ths, pMsg, "");
M
Minghao Li 已提交
104 105 106 107

  if (pMsg->timeoutType == SYNC_TIMEOUT_PING) {
    if (atomic_load_64(&ths->pingTimerLogicClockUser) <= pMsg->logicClock) {
      ++(ths->pingTimerCounter);
M
Minghao Li 已提交
108

M
Minghao Li 已提交
109
      // syncNodePingAll(ths);
M
Minghao Li 已提交
110 111 112
      // syncNodePingPeers(ths);

      syncNodeTimerRoutine(ths);
M
Minghao Li 已提交
113 114 115 116 117
    }

  } else if (pMsg->timeoutType == SYNC_TIMEOUT_ELECTION) {
    if (atomic_load_64(&ths->electTimerLogicClockUser) <= pMsg->logicClock) {
      ++(ths->electTimerCounter);
S
Shengliang Guan 已提交
118 119
      sTrace("vgId:%d, sync timer, type:election count:%" PRIu64 ", lc-user:%" PRIu64, ths->vgId,
             ths->electTimerCounter, ths->electTimerLogicClockUser);
120

M
Minghao Li 已提交
121 122 123 124 125 126
      syncNodeElect(ths);
    }

  } else if (pMsg->timeoutType == SYNC_TIMEOUT_HEARTBEAT) {
    if (atomic_load_64(&ths->heartbeatTimerLogicClockUser) <= pMsg->logicClock) {
      ++(ths->heartbeatTimerCounter);
S
Shengliang Guan 已提交
127
      sTrace("vgId:%d, sync timer, type:replicate count:%" PRIu64 ", lc-user:%" PRIu64, ths->vgId,
S
Shengliang Guan 已提交
128
             ths->heartbeatTimerCounter, ths->heartbeatTimerLogicClockUser);
129

M
Minghao Li 已提交
130
      // syncNodeReplicate(ths, true);
M
Minghao Li 已提交
131
    }
M
Minghao Li 已提交
132

M
Minghao Li 已提交
133
  } else {
134
    sError("vgId:%d, recv unknown timer-type:%d", ths->vgId, pMsg->timeoutType);
M
Minghao Li 已提交
135 136 137
  }

  return ret;
138
}