syncRequestVote.c 5.1 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
M
Minghao Li 已提交
17
#include "syncRequestVote.h"
18
#include "syncMessage.h"
M
Minghao Li 已提交
19
#include "syncRaftCfg.h"
M
Minghao Li 已提交
20 21 22
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
C
cadem 已提交
23
#include "syncUtil.h"
M
Minghao Li 已提交
24

M
Minghao Li 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// 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>>
M
Minghao Li 已提交
46
//
M
Minghao Li 已提交
47

S
Shengliang Guan 已提交
48 49 50
static bool syncNodeOnRequestVoteLogOK(SSyncNode* ths, SyncRequestVote* pMsg) {
  SyncTerm  myLastTerm = syncNodeGetLastTerm(ths);
  SyncIndex myLastIndex = syncNodeGetLastIndex(ths);
51

52
  if (myLastTerm == SYNC_TERM_INVALID) {
S
Shengliang Guan 已提交
53
    sNTrace(ths,
S
Shengliang Guan 已提交
54 55 56
            "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);
57 58
    return false;
  }
M
Minghao Li 已提交
59

60
  if (pMsg->lastLogTerm > myLastTerm) {
S
Shengliang Guan 已提交
61
    sNTrace(ths,
S
Shengliang Guan 已提交
62 63 64
            "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);
65

66 67
    if (pMsg->lastLogIndex < ths->commitIndex) {
      sNWarn(ths,
68 69 70 71
             "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);
    }
72 73
    return true;
  }
M
Minghao Li 已提交
74

75
  if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
S
Shengliang Guan 已提交
76
    sNTrace(ths,
S
Shengliang Guan 已提交
77 78 79
            "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);
80 81 82
    return true;
  }

S
Shengliang Guan 已提交
83
  sNTrace(ths,
S
Shengliang Guan 已提交
84 85 86
          "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);
87 88 89
  return false;
}

S
Shengliang Guan 已提交
90 91 92
int32_t syncNodeOnRequestVote(SSyncNode* ths, const SRpcMsg* pRpcMsg) {
  int32_t          ret = 0;
  SyncRequestVote* pMsg = pRpcMsg->pCont;
M
Minghao Li 已提交
93 94

  // if already drop replica, do not process
S
Shengliang Guan 已提交
95
  if (!syncNodeInRaftGroup(ths, &pMsg->srcId)) {
S
Shengliang Guan 已提交
96
    syncLogRecvRequestVote(ths, pMsg, -1, "not in my config");
M
Minghao Li 已提交
97 98 99 100 101
    return -1;
  }

  bool logOK = syncNodeOnRequestVoteLogOK(ths, pMsg);
  // maybe update term
102
  if (pMsg->term > raftStoreGetTerm(ths)) {
M
Minghao Li 已提交
103
    syncNodeStepDown(ths, pMsg->term);
M
Minghao Li 已提交
104
  }
105 106
  SyncTerm currentTerm = raftStoreGetTerm(ths);
  ASSERT(pMsg->term <= currentTerm);
M
Minghao Li 已提交
107

108
  bool grant = (pMsg->term == currentTerm) && logOK &&
S
Shengliang Guan 已提交
109
               ((!raftStoreHasVoted(ths)) || (syncUtilSameId(&ths->raftStore.voteFor, &pMsg->srcId)));
M
Minghao Li 已提交
110 111 112
  if (grant) {
    // maybe has already voted for pMsg->srcId
    // vote again, no harm
S
Shengliang Guan 已提交
113
    raftStoreVote(ths, &(pMsg->srcId));
M
Minghao Li 已提交
114

M
Minghao Li 已提交
115
    // candidate ?
116
    syncNodeStepDown(ths, currentTerm);
M
Minghao Li 已提交
117

M
Minghao Li 已提交
118 119 120 121 122
    // forbid elect for this round
    syncNodeResetElectTimer(ths);
  }

  // send msg
S
Shengliang Guan 已提交
123 124
  SRpcMsg rpcMsg = {0};
  ret = syncBuildRequestVoteReply(&rpcMsg, ths->vgId);
125
  ASSERT(ret == 0);
S
Shengliang Guan 已提交
126 127

  SyncRequestVoteReply* pReply = rpcMsg.pCont;
M
Minghao Li 已提交
128 129
  pReply->srcId = ths->myRaftId;
  pReply->destId = pMsg->srcId;
130
  pReply->term = currentTerm;
M
Minghao Li 已提交
131
  pReply->voteGranted = grant;
132
  ASSERT(!grant || pMsg->term == pReply->term);
M
Minghao Li 已提交
133 134

  // trace log
S
Shengliang Guan 已提交
135 136
  syncLogRecvRequestVote(ths, pMsg, pReply->voteGranted, "");
  syncLogSendRequestVoteReply(ths, pReply, "");
M
Minghao Li 已提交
137 138
  syncNodeSendMsgById(&pReply->destId, ths, &rpcMsg);
  return 0;
139
}