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

165 lines
5.9 KiB
C
Raw Normal View History

2022-03-16 09:16:10 +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-03-18 03:29:03 +00:00
#include "syncCommit.h"
2022-03-16 12:03:22 +00:00
#include "syncIndexMgr.h"
2022-03-16 09:16:10 +00:00
#include "syncInt.h"
2022-05-14 10:12:53 +00:00
#include "syncRaftCfg.h"
2022-03-17 08:33:10 +00:00
#include "syncRaftLog.h"
2022-03-18 07:05:56 +00:00
#include "syncRaftStore.h"
2022-03-18 09:23:48 +00:00
#include "syncUtil.h"
2022-03-16 09:16:10 +00:00
// \* Leader i advances its commitIndex.
// \* This is done as a separate step from handling AppendEntries responses,
// \* in part to minimize atomic regions, and in part so that leaders of
// \* single-server clusters are able to mark entries committed.
// AdvanceCommitIndex(i) ==
// /\ state[i] = Leader
// /\ LET \* The set of servers that agree up through index.
// Agree(index) == {i} \cup {k \in Server :
// matchIndex[i][k] >= index}
// \* The maximum indexes for which a quorum agrees
// agreeIndexes == {index \in 1..Len(log[i]) :
// Agree(index) \in Quorum}
// \* New value for commitIndex'[i]
// newCommitIndex ==
// IF /\ agreeIndexes /= {}
// /\ log[i][Max(agreeIndexes)].term = currentTerm[i]
// THEN
// Max(agreeIndexes)
// ELSE
// commitIndex[i]
// IN commitIndex' = [commitIndex EXCEPT ![i] = newCommitIndex]
// /\ UNCHANGED <<messages, serverVars, candidateVars, leaderVars, log>>
//
2022-03-18 03:29:03 +00:00
void syncMaybeAdvanceCommitIndex(SSyncNode* pSyncNode) {
2022-03-16 12:03:22 +00:00
syncIndexMgrLog2("==syncNodeMaybeAdvanceCommitIndex== pNextIndex", pSyncNode->pNextIndex);
syncIndexMgrLog2("==syncNodeMaybeAdvanceCommitIndex== pMatchIndex", pSyncNode->pMatchIndex);
2022-03-17 08:33:10 +00:00
// update commit index
2022-03-18 03:47:46 +00:00
SyncIndex newCommitIndex = pSyncNode->commitIndex;
for (SyncIndex index = pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore); index > pSyncNode->commitIndex;
2022-03-18 11:09:22 +00:00
--index) {
2022-03-18 09:23:48 +00:00
bool agree = syncAgree(pSyncNode, index);
2022-03-18 11:09:22 +00:00
sTrace("syncMaybeAdvanceCommitIndex syncAgree:%d, index:%ld, pSyncNode->commitIndex:%ld", agree, index,
pSyncNode->commitIndex);
2022-03-18 09:23:48 +00:00
if (agree) {
2022-03-18 07:05:56 +00:00
// term
SSyncRaftEntry* pEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, index);
assert(pEntry != NULL);
// cannot commit, even if quorum agree. need check term!
if (pEntry->term == pSyncNode->pRaftStore->currentTerm) {
// update commit index
newCommitIndex = index;
2022-03-23 09:56:32 +00:00
sTrace("syncMaybeAdvanceCommitIndex maybe to update, newCommitIndex:%ld commit, pSyncNode->commitIndex:%ld",
newCommitIndex, pSyncNode->commitIndex);
2022-05-09 09:25:42 +00:00
syncEntryDestory(pEntry);
2022-03-18 07:05:56 +00:00
break;
2022-03-23 09:56:32 +00:00
} else {
sTrace(
"syncMaybeAdvanceCommitIndex can not commit due to term not equal, pEntry->term:%lu, "
"pSyncNode->pRaftStore->currentTerm:%lu",
pEntry->term, pSyncNode->pRaftStore->currentTerm);
2022-03-18 07:05:56 +00:00
}
2022-05-09 07:17:33 +00:00
syncEntryDestory(pEntry);
2022-03-18 03:47:46 +00:00
}
}
2022-03-17 08:33:10 +00:00
2022-03-18 03:47:46 +00:00
if (newCommitIndex > pSyncNode->commitIndex) {
SyncIndex beginIndex = pSyncNode->commitIndex + 1;
SyncIndex endIndex = newCommitIndex;
2022-03-18 07:05:56 +00:00
2022-03-18 10:42:49 +00:00
sTrace("syncMaybeAdvanceCommitIndex sync commit %ld", newCommitIndex);
2022-03-18 07:05:56 +00:00
// update commit index
2022-03-18 03:47:46 +00:00
pSyncNode->commitIndex = newCommitIndex;
2022-03-17 08:33:10 +00:00
2022-03-18 07:05:56 +00:00
// call back Wal
pSyncNode->pLogStore->updateCommitIndex(pSyncNode->pLogStore, pSyncNode->commitIndex);
// execute fsm
2022-03-18 03:47:46 +00:00
if (pSyncNode->pFsm != NULL) {
for (SyncIndex i = beginIndex; i <= endIndex; ++i) {
if (i != SYNC_INDEX_INVALID) {
SSyncRaftEntry* pEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, i);
assert(pEntry != NULL);
2022-03-17 08:33:10 +00:00
2022-03-18 03:47:46 +00:00
SRpcMsg rpcMsg;
syncEntry2OriginalRpc(pEntry, &rpcMsg);
2022-03-17 08:33:10 +00:00
2022-05-14 10:12:53 +00:00
// if (pSyncNode->pFsm->FpCommitCb != NULL && pEntry->originalRpcType != TDMT_VND_SYNC_NOOP) {
2022-05-12 07:23:41 +00:00
if (pSyncNode->pFsm->FpCommitCb != NULL && syncUtilUserCommit(pEntry->originalRpcType)) {
2022-04-18 13:50:56 +00:00
SFsmCbMeta cbMeta;
cbMeta.index = pEntry->index;
cbMeta.isWeak = pEntry->isWeak;
cbMeta.code = 0;
cbMeta.state = pSyncNode->state;
cbMeta.seqNum = pEntry->seqNum;
pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &rpcMsg, cbMeta);
2022-03-18 03:47:46 +00:00
}
2022-05-12 07:23:41 +00:00
// config change
2022-05-14 10:12:53 +00:00
if (pEntry->originalRpcType == TDMT_VND_SYNC_CONFIG_CHANGE) {
SSyncCfg newSyncCfg;
int32_t ret = syncCfgFromStr(rpcMsg.pCont, &newSyncCfg);
ASSERT(ret == 0);
2022-05-12 07:23:41 +00:00
2022-05-14 10:12:53 +00:00
syncNodeUpdateConfig(pSyncNode, &newSyncCfg);
2022-05-16 06:10:18 +00:00
if (pSyncNode->state == TAOS_SYNC_STATE_LEADER) {
syncNodeBecomeLeader(pSyncNode);
} else {
syncNodeBecomeFollower(pSyncNode);
}
2022-05-12 07:23:41 +00:00
}
2022-03-18 03:47:46 +00:00
rpcFreeCont(rpcMsg.pCont);
syncEntryDestory(pEntry);
}
2022-03-17 10:55:26 +00:00
}
2022-03-17 08:33:10 +00:00
}
}
2022-03-18 03:47:46 +00:00
}
bool syncAgreeIndex(SSyncNode* pSyncNode, SRaftId* pRaftId, SyncIndex index) {
2022-03-18 09:23:48 +00:00
// I am leader, I agree
if (syncUtilSameId(pRaftId, &(pSyncNode->myRaftId)) && pSyncNode->state == TAOS_SYNC_STATE_LEADER) {
return true;
}
2022-03-18 03:47:46 +00:00
2022-03-18 09:23:48 +00:00
// follower agree
SyncIndex matchIndex = syncIndexMgrGetIndex(pSyncNode->pMatchIndex, pRaftId);
2022-03-18 03:47:46 +00:00
if (matchIndex >= index) {
2022-03-18 09:23:48 +00:00
return true;
2022-03-18 03:47:46 +00:00
}
2022-03-18 09:23:48 +00:00
2022-03-18 09:46:40 +00:00
// not agree
2022-03-18 09:23:48 +00:00
return false;
2022-03-18 03:47:46 +00:00
}
bool syncAgree(SSyncNode* pSyncNode, SyncIndex index) {
int agreeCount = 0;
for (int i = 0; i < pSyncNode->replicaNum; ++i) {
if (syncAgreeIndex(pSyncNode, &(pSyncNode->replicasId[i]), index)) {
++agreeCount;
}
if (agreeCount >= pSyncNode->quorum) {
return true;
}
}
return false;
2022-03-16 12:03:22 +00:00
}