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

116 lines
4.1 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 09:05:13 +00:00
#define _DEFAULT_SOURCE
2022-02-26 18:24:50 +00:00
#include "syncReplication.h"
2022-03-14 06:05:40 +00:00
#include "syncIndexMgr.h"
#include "syncPipeline.h"
2022-03-14 06:05:40 +00:00
#include "syncRaftEntry.h"
2022-03-16 08:54:55 +00:00
#include "syncRaftStore.h"
2022-03-14 12:43:35 +00:00
#include "syncUtil.h"
2022-03-04 07:48:09 +00:00
2022-03-07 08:06:07 +00:00
// TLA+ Spec
// AppendEntries(i, j) ==
// /\ i /= j
// /\ state[i] = Leader
// /\ LET prevLogIndex == nextIndex[i][j] - 1
// prevLogTerm == IF prevLogIndex > 0 THEN
// log[i][prevLogIndex].term
// ELSE
// 0
// \* Send up to 1 entry, constrained by the end of the log.
// lastEntry == Min({Len(log[i]), nextIndex[i][j]})
// entries == SubSeq(log[i], nextIndex[i][j], lastEntry)
// IN Send([mtype |-> AppendEntriesRequest,
// mterm |-> currentTerm[i],
// mprevLogIndex |-> prevLogIndex,
// mprevLogTerm |-> prevLogTerm,
// mentries |-> entries,
// \* mlog is used as a history variable for the proof.
// \* It would not exist in a real implementation.
// mlog |-> log[i],
// mcommitIndex |-> Min({commitIndex[i], lastEntry}),
// msource |-> i,
// mdest |-> j])
// /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
int32_t syncNodeReplicateReset(SSyncNode* pNode, SRaftId* pDestId) {
SSyncLogBuffer* pBuf = pNode->pLogBuf;
taosThreadMutexLock(&pBuf->mutex);
SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(pNode, pDestId);
syncLogReplReset(pMgr);
taosThreadMutexUnlock(&pBuf->mutex);
return 0;
}
2022-11-11 14:55:21 +00:00
int32_t syncNodeReplicate(SSyncNode* pNode) {
SSyncLogBuffer* pBuf = pNode->pLogBuf;
taosThreadMutexLock(&pBuf->mutex);
int32_t ret = syncNodeReplicateWithoutLock(pNode);
taosThreadMutexUnlock(&pBuf->mutex);
return ret;
}
int32_t syncNodeReplicateWithoutLock(SSyncNode* pNode) {
2023-04-18 11:03:45 +00:00
if (pNode->state != TAOS_SYNC_STATE_LEADER || pNode->raftCfg.cfg.totalReplicaNum == 1) {
2022-11-11 14:55:21 +00:00
return -1;
}
2023-04-18 11:03:45 +00:00
for (int32_t i = 0; i < pNode->totalReplicaNum; i++) {
2022-11-11 14:55:21 +00:00
if (syncUtilSameId(&pNode->replicasId[i], &pNode->myRaftId)) {
continue;
}
SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
(void)syncLogReplStart(pMgr, pNode);
2022-11-11 14:55:21 +00:00
}
return 0;
}
2022-11-12 08:40:09 +00:00
int32_t syncNodeSendAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, SRpcMsg* pRpcMsg) {
2022-11-17 09:21:51 +00:00
SyncAppendEntries* pMsg = pRpcMsg->pCont;
2022-11-11 14:55:21 +00:00
pMsg->destId = *destRaftId;
2022-11-17 09:21:51 +00:00
syncNodeSendMsgById(destRaftId, pSyncNode, pRpcMsg);
2022-11-11 14:55:21 +00:00
return 0;
}
2022-11-12 12:37:15 +00:00
int32_t syncNodeSendHeartbeat(SSyncNode* pSyncNode, const SRaftId* destId, SRpcMsg* pMsg) {
return syncNodeSendMsgById(destId, pSyncNode, pMsg);
2022-10-15 01:28:55 +00:00
}
int32_t syncNodeHeartbeatPeers(SSyncNode* pSyncNode) {
int64_t ts = taosGetTimestampMs();
2022-10-15 01:28:55 +00:00
for (int32_t i = 0; i < pSyncNode->peersNum; ++i) {
2022-11-12 12:37:15 +00:00
SRpcMsg rpcMsg = {0};
if (syncBuildHeartbeat(&rpcMsg, pSyncNode->vgId) != 0) {
sError("vgId:%d, build sync-heartbeat error", pSyncNode->vgId);
2022-11-12 12:37:15 +00:00
continue;
}
SyncHeartbeat* pSyncMsg = rpcMsg.pCont;
2022-10-15 01:28:55 +00:00
pSyncMsg->srcId = pSyncNode->myRaftId;
pSyncMsg->destId = pSyncNode->peersId[i];
2023-02-13 11:00:10 +00:00
pSyncMsg->term = raftStoreGetTerm(pSyncNode);
2022-10-15 01:28:55 +00:00
pSyncMsg->commitIndex = pSyncNode->commitIndex;
2022-10-20 06:53:03 +00:00
pSyncMsg->minMatchIndex = syncMinMatchIndex(pSyncNode);
2022-10-15 01:28:55 +00:00
pSyncMsg->privateTerm = 0;
pSyncMsg->timeStamp = ts;
2022-10-15 01:28:55 +00:00
// send msg
2022-11-26 03:54:38 +00:00
syncLogSendHeartbeat(pSyncNode, pSyncMsg, true, 0, 0);
2022-11-12 12:37:15 +00:00
syncNodeSendHeartbeat(pSyncNode, &pSyncMsg->destId, &rpcMsg);
2022-10-15 01:28:55 +00:00
}
return 0;
2022-10-31 04:59:42 +00:00
}