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"
|
2022-11-18 01:50:36 +00:00
|
|
|
#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>>
|
|
|
|
|
|
2023-03-07 08:34:02 +00:00
|
|
|
int32_t syncNodeReplicateReset(SSyncNode* pNode, SRaftId* pDestId) {
|
|
|
|
|
SSyncLogBuffer* pBuf = pNode->pLogBuf;
|
2024-07-25 09:18:57 +00:00
|
|
|
(void)taosThreadMutexLock(&pBuf->mutex);
|
2023-03-07 08:34:02 +00:00
|
|
|
SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(pNode, pDestId);
|
2023-04-04 12:01:03 +00:00
|
|
|
syncLogReplReset(pMgr);
|
2024-07-25 09:18:57 +00:00
|
|
|
(void)taosThreadMutexUnlock(&pBuf->mutex);
|
2024-07-25 04:09:51 +00:00
|
|
|
|
|
|
|
|
TAOS_RETURN(TSDB_CODE_SUCCESS);
|
2023-03-07 08:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-11 14:55:21 +00:00
|
|
|
int32_t syncNodeReplicate(SSyncNode* pNode) {
|
2022-11-22 02:54:32 +00:00
|
|
|
SSyncLogBuffer* pBuf = pNode->pLogBuf;
|
2024-07-25 09:18:57 +00:00
|
|
|
(void)taosThreadMutexLock(&pBuf->mutex);
|
2022-11-22 02:54:32 +00:00
|
|
|
int32_t ret = syncNodeReplicateWithoutLock(pNode);
|
2024-07-25 09:18:57 +00:00
|
|
|
(void)taosThreadMutexUnlock(&pBuf->mutex);
|
2024-07-25 04:09:51 +00:00
|
|
|
|
|
|
|
|
TAOS_RETURN(ret);
|
2022-11-22 02:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t syncNodeReplicateWithoutLock(SSyncNode* pNode) {
|
2024-03-15 01:14:54 +00:00
|
|
|
if ((pNode->state != TAOS_SYNC_STATE_LEADER && pNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) ||
|
|
|
|
|
pNode->raftCfg.cfg.totalReplicaNum == 1) {
|
2024-09-20 10:23:15 +00:00
|
|
|
TAOS_RETURN(TSDB_CODE_SUCCESS);
|
2022-11-11 14:55:21 +00:00
|
|
|
}
|
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];
|
2024-12-03 03:37:10 +00:00
|
|
|
int32_t ret = 0;
|
|
|
|
|
if ((ret = syncLogReplStart(pMgr, pNode)) != 0) {
|
|
|
|
|
sWarn("vgId:%d, failed to start log replication to dnode:%d since %s", pNode->vgId, DID(&(pNode->replicasId[i])),
|
|
|
|
|
tstrerror(ret));
|
2024-09-20 07:56:25 +00:00
|
|
|
}
|
2022-11-11 14:55:21 +00:00
|
|
|
}
|
2024-07-25 04:09:51 +00:00
|
|
|
|
|
|
|
|
TAOS_RETURN(TSDB_CODE_SUCCESS);
|
2022-11-11 14:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
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;
|
2024-09-20 07:56:25 +00:00
|
|
|
TAOS_CHECK_RETURN(syncNodeSendMsgById(destRaftId, pSyncNode, pRpcMsg));
|
2024-07-25 04:09:51 +00:00
|
|
|
|
2024-11-18 07:57:06 +00:00
|
|
|
int32_t nRef = 0;
|
|
|
|
|
if (pSyncNode != NULL) {
|
2024-11-21 10:28:37 +00:00
|
|
|
nRef = atomic_add_fetch_32(&pSyncNode->sendCount, 1);
|
2024-11-18 07:57:06 +00:00
|
|
|
if (nRef <= 0) {
|
|
|
|
|
sError("vgId:%d, send count is %d", pSyncNode->vgId, nRef);
|
|
|
|
|
}
|
2024-11-15 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSyncLogReplMgr* mgr = syncNodeGetLogReplMgr(pSyncNode, (SRaftId*)destRaftId);
|
2024-11-18 07:57:06 +00:00
|
|
|
if (mgr != NULL) {
|
2024-11-21 10:28:37 +00:00
|
|
|
nRef = atomic_add_fetch_32(&mgr->sendCount, 1);
|
2024-11-18 07:57:06 +00:00
|
|
|
if (nRef <= 0) {
|
|
|
|
|
sError("vgId:%d, send count is %d", pSyncNode->vgId, nRef);
|
|
|
|
|
}
|
2024-11-15 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 04:09:51 +00:00
|
|
|
TAOS_RETURN(TSDB_CODE_SUCCESS);
|
2022-11-11 14:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-12 12:37:15 +00:00
|
|
|
int32_t syncNodeSendHeartbeat(SSyncNode* pSyncNode, const SRaftId* destId, SRpcMsg* pMsg) {
|
2024-11-20 00:57:27 +00:00
|
|
|
SRaftId destIdTmp = *destId;
|
2024-11-18 10:25:52 +00:00
|
|
|
TAOS_CHECK_RETURN(syncNodeSendMsgById(destId, pSyncNode, pMsg));
|
|
|
|
|
|
|
|
|
|
int64_t tsMs = taosGetTimestampMs();
|
2024-11-20 00:57:27 +00:00
|
|
|
syncIndexMgrSetSentTime(pSyncNode->pMatchIndex, &destIdTmp, tsMs);
|
2024-11-18 10:25:52 +00:00
|
|
|
|
|
|
|
|
return TSDB_CODE_SUCCESS;
|
2022-10-15 01:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t syncNodeHeartbeatPeers(SSyncNode* pSyncNode) {
|
2022-11-24 04:03:05 +00:00
|
|
|
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) {
|
2022-11-24 04:03:05 +00:00
|
|
|
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;
|
2022-11-24 04:03:05 +00:00
|
|
|
pSyncMsg->timeStamp = ts;
|
2022-10-15 01:28:55 +00:00
|
|
|
|
|
|
|
|
// send msg
|
2024-08-27 04:15:01 +00:00
|
|
|
TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
|
2025-03-18 08:21:44 +00:00
|
|
|
sGTrace(&rpcMsg.info.traceId, "vgId:%d, send sync-heartbeat to dnode:%d", pSyncNode->vgId, DID(&(pSyncMsg->destId)));
|
2022-11-26 03:54:38 +00:00
|
|
|
syncLogSendHeartbeat(pSyncNode, pSyncMsg, true, 0, 0);
|
2024-09-23 10:40:46 +00:00
|
|
|
int32_t ret = syncNodeSendHeartbeat(pSyncNode, &pSyncMsg->destId, &rpcMsg);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
sError("vgId:%d, failed to send sync-heartbeat since %s", pSyncNode->vgId, tstrerror(ret));
|
2024-09-20 07:56:25 +00:00
|
|
|
}
|
2022-10-15 01:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 04:09:51 +00:00
|
|
|
TAOS_RETURN(TSDB_CODE_SUCCESS);
|
2022-10-31 04:59:42 +00:00
|
|
|
}
|