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"
22
#include "syncRespMgr.h"
S
Shengliang Guan 已提交
23
#include "syncUtil.h"
M
Minghao Li 已提交
24

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

30
  ths->pFsm->FpGetSnapshotInfo(ths->pFsm, &snapshot);
31
  if (snapshot.lastApplyIndex != SYNC_INDEX_INVALID) {
32
    for (int32_t i = 0; i < ths->pRaftCfg->configIndexCount; ++i) {
33 34 35 36 37 38 39 40 41 42 43 44 45 46
      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);
47 48 49 50 51
    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);
    }
52 53 54
  }
}

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

  if (ths->tmrRoutineNum % 60 == 0 && ths->replicaNum > 1) {
    sNInfo(ths, "timer routines");
  } else {
    sNTrace(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
  int64_t timeNow = taosGetTimestampMs();
73 74 75 76 77 78 79 80 81 82 83 84 85
  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 已提交
86 87 88
    }
  }

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

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

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

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

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

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

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

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

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

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

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

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

  return ret;
133
}