syncTimeout.c 3.5 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 "syncReplication.h"
M
Minghao Li 已提交
20
#include "syncRespMgr.h"
M
Minghao Li 已提交
21

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
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 已提交
60
int32_t syncNodeTimerRoutine(SSyncNode* ths) {
61
  syncNodeEventLog(ths, "timer routines");
M
Minghao Li 已提交
62

63 64 65 66
  if (ths->vgId == 1) {
    syncNodeCleanConfigIndex(ths);
  }

67
#if 0
M
Minghao Li 已提交
68 69 70
  if (ths->vgId != 1) {
    syncRespClean(ths->pSyncRespMgr);
  }
71 72
#endif

M
Minghao Li 已提交
73 74 75
  return 0;
}

M
Minghao Li 已提交
76 77
int32_t syncNodeOnTimeoutCb(SSyncNode* ths, SyncTimeout* pMsg) {
  int32_t ret = 0;
M
Minghao Li 已提交
78
  syncTimeoutLog2("==syncNodeOnTimeoutCb==", pMsg);
M
Minghao Li 已提交
79 80 81 82

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

M
Minghao Li 已提交
84
      // syncNodePingAll(ths);
M
Minghao Li 已提交
85 86
      // syncNodePingPeers(ths);

87
      // sTrace("vgId:%d, sync timeout, type:ping count:%d", ths->vgId, ths->pingTimerCounter);
M
Minghao Li 已提交
88
      syncNodeTimerRoutine(ths);
M
Minghao Li 已提交
89 90 91 92 93
    }

  } else if (pMsg->timeoutType == SYNC_TIMEOUT_ELECTION) {
    if (atomic_load_64(&ths->electTimerLogicClockUser) <= pMsg->logicClock) {
      ++(ths->electTimerCounter);
94 95
      sTrace("vgId:%d, sync timer, type:election count:%d, electTimerLogicClockUser:%ld", ths->vgId,
             ths->electTimerCounter, ths->electTimerLogicClockUser);
M
Minghao Li 已提交
96 97 98 99 100 101
      syncNodeElect(ths);
    }

  } else if (pMsg->timeoutType == SYNC_TIMEOUT_HEARTBEAT) {
    if (atomic_load_64(&ths->heartbeatTimerLogicClockUser) <= pMsg->logicClock) {
      ++(ths->heartbeatTimerCounter);
102 103
      sTrace("vgId:%d, sync timer, type:replicate count:%d, heartbeatTimerLogicClockUser:%ld", ths->vgId,
             ths->heartbeatTimerCounter, ths->heartbeatTimerLogicClockUser);
M
Minghao Li 已提交
104
      syncNodeReplicate(ths, true);
M
Minghao Li 已提交
105 106
    }
  } else {
M
Minghao Li 已提交
107
    sError("vgId:%d, unknown timeout-type:%d", ths->vgId, pMsg->timeoutType);
M
Minghao Li 已提交
108 109 110 111
  }

  return ret;
}