syncTimeout.c 4.1 KB
Newer Older
M
Minghao Li 已提交
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/>.
 */

16
#define _DEFAULT_SOURCE
M
Minghao Li 已提交
17
#include "syncTimeout.h"
M
Minghao Li 已提交
18
#include "syncElection.h"
19
#include "syncRaftCfg.h"
M
Minghao Li 已提交
20
#include "syncRaftLog.h"
M
Minghao Li 已提交
21
#include "syncReplication.h"
S
Shengliang Guan 已提交
22
#include "syncUtil.h"
M
Minghao Li 已提交
23

24 25
static void syncNodeCleanConfigIndex(SSyncNode* ths) {
  int32_t   newArrIndex = 0;
26
  SyncIndex newConfigIndexArr[MAX_CONFIG_INDEX_COUNT] = {0};
27 28
  SSnapshot snapshot = {0};

29
  ths->pFsm->FpGetSnapshotInfo(ths->pFsm, &snapshot);
30
  if (snapshot.lastApplyIndex != SYNC_INDEX_INVALID) {
31
    for (int32_t i = 0; i < ths->pRaftCfg->configIndexCount; ++i) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45
      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);
46 47 48 49 50
    if (code != 0) {
      sNFatal(ths, "failed to persist cfg");
    } else {
      sNTrace(ths, "clean config index arr, old-cnt:%d, new-cnt:%d", oldCnt, ths->pRaftCfg->configIndexCount);
    }
51 52 53
  }
}

54
static int32_t syncNodeTimerRoutine(SSyncNode* ths) {
M
Minghao Li 已提交
55 56 57 58 59 60 61
  ths->tmrRoutineNum++;

  if (ths->tmrRoutineNum % 60 == 0 && ths->replicaNum > 1) {
    sNInfo(ths, "timer routines");
  } else {
    sNTrace(ths, "timer routines");
  }
M
Minghao Li 已提交
62

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

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

M
Minghao Li 已提交
71
  int64_t timeNow = taosGetTimestampMs();
72 73 74 75 76 77 78 79 80 81 82 83 84
  if (atomic_load_64(&ths->snapshottingIndex) != SYNC_INDEX_INVALID) {
    // end timeout wal snapshot
    if (timeNow - ths->snapshottingTime > 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) {
        sNError(ths, "timer wal snapshot end error since:%s", terrstr());
        return -1;
      } else {
        sNTrace(ths, "wal snapshot end, index:%" PRId64, atomic_load_64(&ths->snapshottingIndex));
        atomic_store_64(&ths->snapshottingIndex, SYNC_INDEX_INVALID);
      }
M
Minghao Li 已提交
85 86 87
    }
  }

88
#if 0
89
  if (!syncNodeIsMnode(ths)) {
M
Minghao Li 已提交
90 91
    syncRespClean(ths->pSyncRespMgr);
  }
92 93
#endif

M
Minghao Li 已提交
94 95 96
  return 0;
}

S
Shengliang Guan 已提交
97
int32_t syncNodeOnTimeout(SSyncNode* ths, const SRpcMsg* pRpc) {
S
Shengliang Guan 已提交
98 99 100
  int32_t      ret = 0;
  SyncTimeout* pMsg = pRpc->pCont;

101
  syncLogRecvTimer(ths, pMsg, "");
M
Minghao Li 已提交
102 103 104 105

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

M
Minghao Li 已提交
107
      // syncNodePingAll(ths);
M
Minghao Li 已提交
108 109 110
      // syncNodePingPeers(ths);

      syncNodeTimerRoutine(ths);
M
Minghao Li 已提交
111 112 113
    }

  } else if (pMsg->timeoutType == SYNC_TIMEOUT_ELECTION) {
M
Minghao Li 已提交
114
    if (atomic_load_64(&ths->electTimerLogicClock) <= pMsg->logicClock) {
M
Minghao Li 已提交
115
      ++(ths->electTimerCounter);
116

M
Minghao Li 已提交
117 118 119 120 121 122
      syncNodeElect(ths);
    }

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

M
Minghao Li 已提交
126
      // syncNodeReplicate(ths, true);
M
Minghao Li 已提交
127
    }
M
Minghao Li 已提交
128

M
Minghao Li 已提交
129
  } else {
130
    sError("vgId:%d, recv unknown timer-type:%d", ths->vgId, pMsg->timeoutType);
M
Minghao Li 已提交
131 132 133
  }

  return ret;
134
}