syncTimeout.c 4.7 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"
23
#include "syncSnapshot.h"
S
Shengliang Guan 已提交
24
#include "syncUtil.h"
M
Minghao Li 已提交
25

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

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

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

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

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

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

M
Minghao Li 已提交
73
  int64_t timeNow = taosGetTimestampMs();
74 75 76 77 78 79 80 81 82 83 84 85 86 87

  for (int i = 0; i < ths->peersNum; ++i) {
    SSyncSnapshotSender* pSender = syncNodeGetSnapshotSender(ths, &(ths->peersId[i]));
    if (pSender != NULL) {
      if (ths->isStart && ths->state == TAOS_SYNC_STATE_LEADER && pSender->start &&
          timeNow - pSender->lastSendTime > SYNC_SNAP_RESEND_MS) {
        snapshotReSend(pSender);
      } else {
        sTrace("vgId:%d, do not resend: nstart%d, now:%" PRId64 ", lstsend:%" PRId64 ", diff:%" PRId64, ths->vgId,
               ths->isStart, timeNow, pSender->lastSendTime, timeNow - pSender->lastSendTime);
      }
    }
  }

88 89 90 91 92 93 94 95 96 97 98 99 100
  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 已提交
101 102 103
    }
  }

104
  if (!syncNodeIsMnode(ths)) {
M
Minghao Li 已提交
105 106
    syncRespClean(ths->pSyncRespMgr);
  }
107

M
Minghao Li 已提交
108 109 110
  return 0;
}

S
Shengliang Guan 已提交
111
int32_t syncNodeOnTimeout(SSyncNode* ths, const SRpcMsg* pRpc) {
S
Shengliang Guan 已提交
112 113 114
  int32_t      ret = 0;
  SyncTimeout* pMsg = pRpc->pCont;

115
  syncLogRecvTimer(ths, pMsg, "");
M
Minghao Li 已提交
116 117 118 119

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

M
Minghao Li 已提交
121
      // syncNodePingAll(ths);
M
Minghao Li 已提交
122 123 124
      // syncNodePingPeers(ths);

      syncNodeTimerRoutine(ths);
M
Minghao Li 已提交
125 126 127
    }

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

M
Minghao Li 已提交
131 132 133 134 135 136
      syncNodeElect(ths);
    }

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

M
Minghao Li 已提交
140
      // syncNodeReplicate(ths, true);
M
Minghao Li 已提交
141
    }
M
Minghao Li 已提交
142

M
Minghao Li 已提交
143
  } else {
144
    sError("vgId:%d, recv unknown timer-type:%d", ths->vgId, pMsg->timeoutType);
M
Minghao Li 已提交
145 146 147
  }

  return ret;
148
}