TDengine/source/libs/sync/src/syncTimeout.c

130 lines
3.9 KiB
C
Raw Normal View History

2022-02-22 03:28:15 +00:00
/*
* 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/>.
*/
2022-11-10 02:04:05 +00:00
#define _DEFAULT_SOURCE
2022-02-22 03:28:15 +00:00
#include "syncTimeout.h"
2022-03-07 06:42:04 +00:00
#include "syncElection.h"
#include "syncRaftCfg.h"
2022-10-20 06:53:03 +00:00
#include "syncRaftLog.h"
2022-03-07 08:06:07 +00:00
#include "syncReplication.h"
#include "syncRespMgr.h"
#include "syncSnapshot.h"
2022-11-13 10:00:11 +00:00
#include "syncUtil.h"
2022-02-22 03:28:15 +00:00
static void syncNodeCleanConfigIndex(SSyncNode* ths) {
#if 0
int32_t newArrIndex = 0;
2022-11-10 02:04:05 +00:00
SyncIndex newConfigIndexArr[MAX_CONFIG_INDEX_COUNT] = {0};
SSnapshot snapshot = {0};
2022-11-10 02:04:05 +00:00
ths->pFsm->FpGetSnapshotInfo(ths->pFsm, &snapshot);
if (snapshot.lastApplyIndex != SYNC_INDEX_INVALID) {
for (int32_t i = 0; i < ths->raftCfg.configIndexCount; ++i) {
if (ths->raftCfg.configIndexArr[i] < snapshot.lastConfigIndex) {
// pass
} else {
// save
newConfigIndexArr[newArrIndex] = ths->raftCfg.configIndexArr[i];
++newArrIndex;
}
}
int32_t oldCnt = ths->raftCfg.configIndexCount;
ths->raftCfg.configIndexCount = newArrIndex;
memcpy(ths->raftCfg.configIndexArr, newConfigIndexArr, sizeof(newConfigIndexArr));
int32_t code = syncWriteCfgFile(ths);
2022-11-10 02:04:05 +00:00
if (code != 0) {
sNFatal(ths, "failed to persist cfg");
} else {
sNTrace(ths, "clean config index arr, old-cnt:%d, new-cnt:%d", oldCnt, ths->raftCfg.configIndexCount);
2022-11-10 02:04:05 +00:00
}
}
#endif
}
2022-11-10 02:04:05 +00:00
static int32_t syncNodeTimerRoutine(SSyncNode* ths) {
2022-11-30 03:20:03 +00:00
ths->tmrRoutineNum++;
2023-04-18 11:03:45 +00:00
if (ths->tmrRoutineNum % 60 == 0 && ths->totalReplicaNum > 1) {
2022-11-30 03:20:03 +00:00
sNInfo(ths, "timer routines");
} else {
sNTrace(ths, "timer routines");
}
2022-07-12 12:23:31 +00:00
// timer replicate
syncNodeReplicate(ths);
2022-10-20 06:53:03 +00:00
// clean mnode index
if (syncNodeIsMnode(ths)) {
syncNodeCleanConfigIndex(ths);
}
2022-10-20 06:53:03 +00:00
int64_t timeNow = taosGetTimestampMs();
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);
}
}
}
if (!syncNodeIsMnode(ths)) {
2022-07-12 12:23:31 +00:00
syncRespClean(ths->pSyncRespMgr);
}
2022-07-23 11:54:56 +00:00
2022-07-12 07:04:32 +00:00
return 0;
}
2022-11-13 09:21:30 +00:00
int32_t syncNodeOnTimeout(SSyncNode* ths, const SRpcMsg* pRpc) {
2022-11-12 02:08:28 +00:00
int32_t ret = 0;
SyncTimeout* pMsg = pRpc->pCont;
syncLogRecvTimer(ths, pMsg, "");
2022-03-07 06:42:04 +00:00
if (pMsg->timeoutType == SYNC_TIMEOUT_PING) {
if (atomic_load_64(&ths->pingTimerLogicClockUser) <= pMsg->logicClock) {
++(ths->pingTimerCounter);
2022-07-12 07:04:32 +00:00
syncNodeTimerRoutine(ths);
2022-03-07 06:42:04 +00:00
}
} else if (pMsg->timeoutType == SYNC_TIMEOUT_ELECTION) {
2022-10-25 10:03:22 +00:00
if (atomic_load_64(&ths->electTimerLogicClock) <= pMsg->logicClock) {
2022-03-07 06:42:04 +00:00
++(ths->electTimerCounter);
2022-03-07 06:42:04 +00:00
syncNodeElect(ths);
}
} else if (pMsg->timeoutType == SYNC_TIMEOUT_HEARTBEAT) {
if (atomic_load_64(&ths->heartbeatTimerLogicClockUser) <= pMsg->logicClock) {
++(ths->heartbeatTimerCounter);
2022-10-22 15:49:49 +00:00
sTrace("vgId:%d, sync timer, type:replicate count:%" PRIu64 ", lc-user:%" PRIu64, ths->vgId,
2022-10-22 00:39:43 +00:00
ths->heartbeatTimerCounter, ths->heartbeatTimerLogicClockUser);
2022-03-07 06:42:04 +00:00
}
2022-10-15 01:28:55 +00:00
2022-03-07 06:42:04 +00:00
} else {
sError("vgId:%d, recv unknown timer-type:%d", ths->vgId, pMsg->timeoutType);
2022-03-07 06:42:04 +00:00
}
return ret;
}