syncTimeout.c 4.6 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
static void syncNodeCleanConfigIndex(SSyncNode* ths) {
27
#if 0
28
  int32_t   newArrIndex = 0;
29
  SyncIndex newConfigIndexArr[MAX_CONFIG_INDEX_COUNT] = {0};
30 31
  SSnapshot snapshot = {0};

32
  ths->pFsm->FpGetSnapshotInfo(ths->pFsm, &snapshot);
33
  if (snapshot.lastApplyIndex != SYNC_INDEX_INVALID) {
34 35
    for (int32_t i = 0; i < ths->raftCfg.configIndexCount; ++i) {
      if (ths->raftCfg.configIndexArr[i] < snapshot.lastConfigIndex) {
36 37 38
        // pass
      } else {
        // save
39
        newConfigIndexArr[newArrIndex] = ths->raftCfg.configIndexArr[i];
40 41 42 43
        ++newArrIndex;
      }
    }

44 45 46
    int32_t oldCnt = ths->raftCfg.configIndexCount;
    ths->raftCfg.configIndexCount = newArrIndex;
    memcpy(ths->raftCfg.configIndexArr, newConfigIndexArr, sizeof(newConfigIndexArr));
47

48
    int32_t code = syncWriteCfgFile(ths);
49 50 51
    if (code != 0) {
      sNFatal(ths, "failed to persist cfg");
    } else {
52
      sNTrace(ths, "clean config index arr, old-cnt:%d, new-cnt:%d", oldCnt, ths->raftCfg.configIndexCount);
53
    }
54
  }
55
#endif
56 57
}

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

C
cadem 已提交
61
  if (ths->tmrRoutineNum % 60 == 0 && ths->totalReplicaNum > 1) {
M
Minghao Li 已提交
62 63 64 65
    sNInfo(ths, "timer routines");
  } else {
    sNTrace(ths, "timer routines");
  }
M
Minghao Li 已提交
66

67 68 69
  // timer replicate
  syncNodeReplicate(ths);

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

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

  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);
      }
    }
  }

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

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

M
Minghao Li 已提交
110 111 112
  return 0;
}

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

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

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

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

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

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

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

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

  return ret;
145
}