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-22 03:28:15 +00:00
|
|
|
#include "syncRequestVote.h"
|
2022-11-11 06:35:16 +00:00
|
|
|
#include "syncMessage.h"
|
2022-06-08 14:43:58 +00:00
|
|
|
#include "syncRaftCfg.h"
|
2022-03-14 12:43:35 +00:00
|
|
|
#include "syncRaftStore.h"
|
|
|
|
|
#include "syncUtil.h"
|
|
|
|
|
#include "syncVoteMgr.h"
|
2023-03-18 03:16:52 +00:00
|
|
|
#include "syncUtil.h"
|
2022-02-22 03:28:15 +00:00
|
|
|
|
2022-03-07 08:17:41 +00:00
|
|
|
// TLA+ Spec
|
|
|
|
|
// HandleRequestVoteRequest(i, j, m) ==
|
|
|
|
|
// LET logOk == \/ m.mlastLogTerm > LastTerm(log[i])
|
|
|
|
|
// \/ /\ m.mlastLogTerm = LastTerm(log[i])
|
|
|
|
|
// /\ m.mlastLogIndex >= Len(log[i])
|
|
|
|
|
// grant == /\ m.mterm = currentTerm[i]
|
|
|
|
|
// /\ logOk
|
|
|
|
|
// /\ votedFor[i] \in {Nil, j}
|
|
|
|
|
// IN /\ m.mterm <= currentTerm[i]
|
|
|
|
|
// /\ \/ grant /\ votedFor' = [votedFor EXCEPT ![i] = j]
|
|
|
|
|
// \/ ~grant /\ UNCHANGED votedFor
|
|
|
|
|
// /\ Reply([mtype |-> RequestVoteResponse,
|
|
|
|
|
// mterm |-> currentTerm[i],
|
|
|
|
|
// mvoteGranted |-> grant,
|
|
|
|
|
// \* mlog is used just for the `elections' history variable for
|
|
|
|
|
// \* the proof. It would not exist in a real implementation.
|
|
|
|
|
// mlog |-> log[i],
|
|
|
|
|
// msource |-> i,
|
|
|
|
|
// mdest |-> j],
|
|
|
|
|
// m)
|
|
|
|
|
// /\ UNCHANGED <<state, currentTerm, candidateVars, leaderVars, logVars>>
|
2022-03-07 08:29:21 +00:00
|
|
|
//
|
2022-06-25 03:30:34 +00:00
|
|
|
|
2023-01-09 04:01:36 +00:00
|
|
|
static bool syncNodeOnRequestVoteLogOK(SSyncNode* ths, SyncRequestVote* pMsg) {
|
|
|
|
|
SyncTerm myLastTerm = syncNodeGetLastTerm(ths);
|
|
|
|
|
SyncIndex myLastIndex = syncNodeGetLastIndex(ths);
|
2022-06-06 08:02:25 +00:00
|
|
|
|
2022-06-29 07:44:30 +00:00
|
|
|
if (myLastTerm == SYNC_TERM_INVALID) {
|
2023-01-09 04:01:36 +00:00
|
|
|
sNTrace(ths,
|
2022-11-07 05:51:37 +00:00
|
|
|
"logok:0, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
|
|
|
|
|
", recv-term:%" PRIu64 "}",
|
|
|
|
|
myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
|
2022-06-29 07:44:30 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-07-19 09:20:59 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
if (pMsg->lastLogTerm > myLastTerm) {
|
2023-01-09 04:01:36 +00:00
|
|
|
sNTrace(ths,
|
2022-11-07 05:51:37 +00:00
|
|
|
"logok:1, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
|
|
|
|
|
", recv-term:%" PRIu64 "}",
|
|
|
|
|
myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
|
2023-01-05 08:59:16 +00:00
|
|
|
|
2023-01-09 08:12:30 +00:00
|
|
|
if (pMsg->lastLogIndex < ths->commitIndex) {
|
|
|
|
|
sNWarn(ths,
|
2023-01-05 08:59:16 +00:00
|
|
|
"logok:1, commit rollback required. {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64
|
|
|
|
|
", recv-lindex:%" PRId64 ", recv-term:%" PRIu64 "}",
|
|
|
|
|
myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
|
|
|
|
|
}
|
2022-06-06 08:02:25 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2022-07-19 09:20:59 +00:00
|
|
|
|
2022-06-06 08:02:25 +00:00
|
|
|
if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
|
2023-01-09 04:01:36 +00:00
|
|
|
sNTrace(ths,
|
2022-11-07 05:51:37 +00:00
|
|
|
"logok:1, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
|
|
|
|
|
", recv-term:%" PRIu64 "}",
|
|
|
|
|
myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
|
2022-06-06 08:02:25 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 04:01:36 +00:00
|
|
|
sNTrace(ths,
|
2022-11-07 05:51:37 +00:00
|
|
|
"logok:0, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
|
|
|
|
|
", recv-term:%" PRIu64 "}",
|
|
|
|
|
myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
|
2022-06-06 08:02:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 04:58:08 +00:00
|
|
|
int32_t syncNodeOnRequestVote(SSyncNode* ths, const SRpcMsg* pRpcMsg) {
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
SyncRequestVote* pMsg = pRpcMsg->pCont;
|
2023-03-14 11:45:31 +00:00
|
|
|
bool resetElect = false;
|
2022-10-15 01:28:55 +00:00
|
|
|
|
|
|
|
|
// if already drop replica, do not process
|
2023-01-09 04:01:36 +00:00
|
|
|
if (!syncNodeInRaftGroup(ths, &pMsg->srcId)) {
|
2022-11-25 14:13:39 +00:00
|
|
|
syncLogRecvRequestVote(ths, pMsg, -1, "not in my config");
|
2022-10-15 01:28:55 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool logOK = syncNodeOnRequestVoteLogOK(ths, pMsg);
|
|
|
|
|
// maybe update term
|
2023-02-13 11:00:10 +00:00
|
|
|
if (pMsg->term > raftStoreGetTerm(ths)) {
|
2022-10-19 08:08:42 +00:00
|
|
|
syncNodeStepDown(ths, pMsg->term);
|
2022-10-15 01:28:55 +00:00
|
|
|
}
|
2023-02-13 11:00:10 +00:00
|
|
|
SyncTerm currentTerm = raftStoreGetTerm(ths);
|
|
|
|
|
ASSERT(pMsg->term <= currentTerm);
|
2022-10-15 01:28:55 +00:00
|
|
|
|
2023-02-13 11:00:10 +00:00
|
|
|
bool grant = (pMsg->term == currentTerm) && logOK &&
|
2023-01-09 04:01:36 +00:00
|
|
|
((!raftStoreHasVoted(ths)) || (syncUtilSameId(&ths->raftStore.voteFor, &pMsg->srcId)));
|
2022-10-15 01:28:55 +00:00
|
|
|
if (grant) {
|
|
|
|
|
// maybe has already voted for pMsg->srcId
|
|
|
|
|
// vote again, no harm
|
2023-01-09 04:01:36 +00:00
|
|
|
raftStoreVote(ths, &(pMsg->srcId));
|
2022-10-15 01:28:55 +00:00
|
|
|
|
2022-10-19 08:08:42 +00:00
|
|
|
// candidate ?
|
2023-02-13 11:00:10 +00:00
|
|
|
syncNodeStepDown(ths, currentTerm);
|
2022-10-19 08:08:42 +00:00
|
|
|
|
2022-10-15 01:28:55 +00:00
|
|
|
// forbid elect for this round
|
2023-03-14 11:45:31 +00:00
|
|
|
resetElect = true;
|
2022-10-15 01:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send msg
|
2022-11-12 05:17:56 +00:00
|
|
|
SRpcMsg rpcMsg = {0};
|
|
|
|
|
ret = syncBuildRequestVoteReply(&rpcMsg, ths->vgId);
|
2022-11-17 09:21:51 +00:00
|
|
|
ASSERT(ret == 0);
|
2022-11-12 05:17:56 +00:00
|
|
|
|
|
|
|
|
SyncRequestVoteReply* pReply = rpcMsg.pCont;
|
2022-10-15 01:28:55 +00:00
|
|
|
pReply->srcId = ths->myRaftId;
|
|
|
|
|
pReply->destId = pMsg->srcId;
|
2023-02-13 11:00:10 +00:00
|
|
|
pReply->term = currentTerm;
|
2022-10-15 01:28:55 +00:00
|
|
|
pReply->voteGranted = grant;
|
2023-02-13 11:00:10 +00:00
|
|
|
ASSERT(!grant || pMsg->term == pReply->term);
|
2022-10-15 01:28:55 +00:00
|
|
|
|
|
|
|
|
// trace log
|
2022-11-25 15:33:34 +00:00
|
|
|
syncLogRecvRequestVote(ths, pMsg, pReply->voteGranted, "");
|
|
|
|
|
syncLogSendRequestVoteReply(ths, pReply, "");
|
2022-10-15 01:28:55 +00:00
|
|
|
syncNodeSendMsgById(&pReply->destId, ths, &rpcMsg);
|
2023-03-14 11:45:31 +00:00
|
|
|
|
|
|
|
|
if (resetElect) syncNodeResetElectTimer(ths);
|
2022-10-15 01:28:55 +00:00
|
|
|
return 0;
|
2023-01-05 08:59:16 +00:00
|
|
|
}
|