syncTimeout.c 3.9 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
  SSnapshot snapshot = {0};
S
Shengliang Guan 已提交
28

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) {
55
  sNInfo(ths, "timer routines");
M
Minghao Li 已提交
56

57 58 59
  // timer replicate
  syncNodeReplicate(ths);

M
Minghao Li 已提交
60
  // clean mnode index
61
  if (syncNodeIsMnode(ths)) {
62 63 64
    syncNodeCleanConfigIndex(ths);
  }

M
Minghao Li 已提交
65
  int64_t timeNow = taosGetTimestampMs();
66 67 68 69 70 71 72 73 74 75 76 77 78
  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 已提交
79 80 81
    }
  }

82
#if 0
83
  if (!syncNodeIsMnode(ths)) {
M
Minghao Li 已提交
84 85
    syncRespClean(ths->pSyncRespMgr);
  }
86 87
#endif

M
Minghao Li 已提交
88 89 90
  return 0;
}

S
Shengliang Guan 已提交
91
int32_t syncNodeOnTimeout(SSyncNode* ths, const SRpcMsg* pRpc) {
S
Shengliang Guan 已提交
92 93 94
  int32_t      ret = 0;
  SyncTimeout* pMsg = pRpc->pCont;

95
  syncLogRecvTimer(ths, pMsg, "");
M
Minghao Li 已提交
96 97 98 99

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

M
Minghao Li 已提交
101
      // syncNodePingAll(ths);
M
Minghao Li 已提交
102 103 104
      // syncNodePingPeers(ths);

      syncNodeTimerRoutine(ths);
M
Minghao Li 已提交
105 106 107
    }

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

M
Minghao Li 已提交
111 112 113 114 115 116
      syncNodeElect(ths);
    }

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

M
Minghao Li 已提交
120
      // syncNodeReplicate(ths, true);
M
Minghao Li 已提交
121
    }
M
Minghao Li 已提交
122

M
Minghao Li 已提交
123
  } else {
124
    sError("vgId:%d, recv unknown timer-type:%d", ths->vgId, pMsg->timeoutType);
M
Minghao Li 已提交
125 126 127 128
  }

  return ret;
}