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

#include "syncRequestVoteReply.h"
M
Minghao Li 已提交
17
#include "syncInt.h"
M
Minghao Li 已提交
18
#include "syncRaftCfg.h"
M
Minghao Li 已提交
19 20 21
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
M
Minghao Li 已提交
22

M
Minghao Li 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// TLA+ Spec
// HandleRequestVoteResponse(i, j, m) ==
//    \* This tallies votes even when the current state is not Candidate, but
//    \* they won't be looked at, so it doesn't matter.
//    /\ m.mterm = currentTerm[i]
//    /\ votesResponded' = [votesResponded EXCEPT ![i] =
//                              votesResponded[i] \cup {j}]
//    /\ \/ /\ m.mvoteGranted
//          /\ votesGranted' = [votesGranted EXCEPT ![i] =
//                                  votesGranted[i] \cup {j}]
//          /\ voterLog' = [voterLog EXCEPT ![i] =
//                              voterLog[i] @@ (j :> m.mlog)]
//       \/ /\ ~m.mvoteGranted
//          /\ UNCHANGED <<votesGranted, voterLog>>
//    /\ Discard(m)
//    /\ UNCHANGED <<serverVars, votedFor, leaderVars, logVars>>
M
Minghao Li 已提交
39
//
M
Minghao Li 已提交
40 41
int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg) {
  int32_t ret = 0;
M
Minghao Li 已提交
42

M
Minghao Li 已提交
43 44 45 46 47 48
  // trace log
  do {
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
49 50
    snprintf(logBuf, sizeof(logBuf), "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d} ", host,
             port, pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
51 52
    syncNodeEventLog(ths, logBuf);
  } while (0);
M
Minghao Li 已提交
53 54 55

  // if already drop replica, do not process
  if (!syncNodeInRaftGroup(ths, &(pMsg->srcId)) && !ths->pRaftCfg->isStandBy) {
M
Minghao Li 已提交
56 57 58 59 60
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
    snprintf(logBuf, sizeof(logBuf),
61
             "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d}, maybe replica dropped", host, port,
M
Minghao Li 已提交
62
             pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
63 64
    syncNodeErrorLog(ths, logBuf);
    return -1;
M
Minghao Li 已提交
65 66 67 68
  }

  // drop stale response
  if (pMsg->term < ths->pRaftStore->currentTerm) {
M
Minghao Li 已提交
69 70 71 72 73
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
    snprintf(logBuf, sizeof(logBuf),
74
             "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d}, drop stale response", host, port,
M
Minghao Li 已提交
75
             pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
76 77
    syncNodeErrorLog(ths, logBuf);
    return -1;
M
Minghao Li 已提交
78 79 80 81 82 83 84 85 86
  }

  // ASSERT(!(pMsg->term > ths->pRaftStore->currentTerm));
  //  no need this code, because if I receive reply.term, then I must have sent for that term.
  //   if (pMsg->term > ths->pRaftStore->currentTerm) {
  //     syncNodeUpdateTerm(ths, pMsg->term);
  //   }

  if (pMsg->term > ths->pRaftStore->currentTerm) {
M
Minghao Li 已提交
87 88 89 90
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
91
    snprintf(logBuf, sizeof(logBuf), "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d}, error term",
M
Minghao Li 已提交
92
             host, port, pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
93 94
    syncNodeErrorLog(ths, logBuf);
    return -1;
M
Minghao Li 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  }

  ASSERT(pMsg->term == ths->pRaftStore->currentTerm);

  // This tallies votes even when the current state is not Candidate,
  // but they won't be looked at, so it doesn't matter.
  if (ths->state == TAOS_SYNC_STATE_CANDIDATE) {
    votesRespondAdd(ths->pVotesRespond, pMsg);
    if (pMsg->voteGranted) {
      // add vote
      voteGrantedVote(ths->pVotesGranted, pMsg);

      // maybe to leader
      if (voteGrantedMajority(ths->pVotesGranted)) {
        if (!ths->pVotesGranted->toLeader) {
          syncNodeCandidate2Leader(ths);

          // prevent to leader again!
          ths->pVotesGranted->toLeader = true;
        }
      }
    } else {
      ;
      // do nothing
      // UNCHANGED <<votesGranted, voterLog>>
    }
  }

M
Minghao Li 已提交
123
  return 0;
M
Minghao Li 已提交
124 125 126 127 128 129
}

#if 0
int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg) {
  int32_t ret = 0;

130
  char logBuf[128] = {0};
S
Shengliang Guan 已提交
131
  snprintf(logBuf, sizeof(logBuf), "==syncNodeOnRequestVoteReplyCb== term:%" PRIu64, ths->pRaftStore->currentTerm);
M
Minghao Li 已提交
132
  syncRequestVoteReplyLog2(logBuf, pMsg);
M
Minghao Li 已提交
133 134

  if (pMsg->term < ths->pRaftStore->currentTerm) {
M
Minghao Li 已提交
135 136
    sTrace("DropStaleResponse, receive term:%" PRIu64 ", current term:%" PRIu64 "", pMsg->term,
           ths->pRaftStore->currentTerm);
M
Minghao Li 已提交
137 138 139
    return ret;
  }

M
Minghao Li 已提交
140
  // ASSERT(!(pMsg->term > ths->pRaftStore->currentTerm));
M
Minghao Li 已提交
141 142 143 144 145 146
  //  no need this code, because if I receive reply.term, then I must have sent for that term.
  //   if (pMsg->term > ths->pRaftStore->currentTerm) {
  //     syncNodeUpdateTerm(ths, pMsg->term);
  //   }

  if (pMsg->term > ths->pRaftStore->currentTerm) {
147
    char logBuf[128] = {0};
S
Shengliang Guan 已提交
148
    snprintf(logBuf, sizeof(logBuf), "syncNodeOnRequestVoteReplyCb error term, receive:%" PRIu64 " current:%" PRIu64, pMsg->term,
M
Minghao Li 已提交
149 150 151 152 153
             ths->pRaftStore->currentTerm);
    syncNodePrint2(logBuf, ths);
    sError("%s", logBuf);
    return ret;
  }
M
Minghao Li 已提交
154

M
Minghao Li 已提交
155
  ASSERT(pMsg->term == ths->pRaftStore->currentTerm);
M
Minghao Li 已提交
156

M
Minghao Li 已提交
157 158
  // This tallies votes even when the current state is not Candidate,
  // but they won't be looked at, so it doesn't matter.
M
Minghao Li 已提交
159 160 161
  if (ths->state == TAOS_SYNC_STATE_CANDIDATE) {
    votesRespondAdd(ths->pVotesRespond, pMsg);
    if (pMsg->voteGranted) {
M
Minghao Li 已提交
162
      // add vote
M
Minghao Li 已提交
163
      voteGrantedVote(ths->pVotesGranted, pMsg);
M
Minghao Li 已提交
164 165

      // maybe to leader
M
Minghao Li 已提交
166
      if (voteGrantedMajority(ths->pVotesGranted)) {
M
Minghao Li 已提交
167
        if (!ths->pVotesGranted->toLeader) {
M
Minghao Li 已提交
168
          syncNodeCandidate2Leader(ths);
M
Minghao Li 已提交
169 170

          // prevent to leader again!
M
Minghao Li 已提交
171 172 173
          ths->pVotesGranted->toLeader = true;
        }
      }
M
Minghao Li 已提交
174 175 176 177
    } else {
      ;
      // do nothing
      // UNCHANGED <<votesGranted, voterLog>>
M
Minghao Li 已提交
178 179
    }
  }
M
Minghao Li 已提交
180

M
Minghao Li 已提交
181 182
  return ret;
}
M
Minghao Li 已提交
183
#endif
M
Minghao Li 已提交
184

185 186 187
int32_t syncNodeOnRequestVoteReplySnapshotCb(SSyncNode* ths, SyncRequestVoteReply* pMsg) {
  int32_t ret = 0;

M
Minghao Li 已提交
188 189 190 191 192 193
  // trace log
  do {
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
194 195
    snprintf(logBuf, sizeof(logBuf), "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d} ", host,
             port, pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
196 197
    syncNodeEventLog(ths, logBuf);
  } while (0);
198

M
Minghao Li 已提交
199
  // if already drop replica, do not process
M
Minghao Li 已提交
200
  if (!syncNodeInRaftGroup(ths, &(pMsg->srcId)) && !ths->pRaftCfg->isStandBy) {
M
Minghao Li 已提交
201 202 203 204 205
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
    snprintf(logBuf, sizeof(logBuf),
206
             "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d}, maybe replica dropped", host, port,
M
Minghao Li 已提交
207
             pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
208 209
    syncNodeErrorLog(ths, logBuf);
    return -1;
M
Minghao Li 已提交
210 211
  }

212
  // drop stale response
213
  if (pMsg->term < ths->pRaftStore->currentTerm) {
M
Minghao Li 已提交
214 215 216 217 218
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
    snprintf(logBuf, sizeof(logBuf),
219
             "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d}, drop stale response", host, port,
M
Minghao Li 已提交
220
             pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
221 222
    syncNodeErrorLog(ths, logBuf);
    return -1;
223 224
  }

M
Minghao Li 已提交
225
  // ASSERT(!(pMsg->term > ths->pRaftStore->currentTerm));
226 227 228 229 230 231
  //  no need this code, because if I receive reply.term, then I must have sent for that term.
  //   if (pMsg->term > ths->pRaftStore->currentTerm) {
  //     syncNodeUpdateTerm(ths, pMsg->term);
  //   }

  if (pMsg->term > ths->pRaftStore->currentTerm) {
M
Minghao Li 已提交
232 233 234 235
    char     host[64];
    uint16_t port;
    syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port);
    char logBuf[256];
236
    snprintf(logBuf, sizeof(logBuf), "recv sync-request-vote-reply from %s:%d {term:%" PRIu64 ", grant:%d}, error term",
M
Minghao Li 已提交
237
             host, port, pMsg->term, pMsg->voteGranted);
M
Minghao Li 已提交
238 239
    syncNodeErrorLog(ths, logBuf);
    return -1;
240 241
  }

242
  ASSERT(pMsg->term == ths->pRaftStore->currentTerm);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

  // This tallies votes even when the current state is not Candidate,
  // but they won't be looked at, so it doesn't matter.
  if (ths->state == TAOS_SYNC_STATE_CANDIDATE) {
    votesRespondAdd(ths->pVotesRespond, pMsg);
    if (pMsg->voteGranted) {
      // add vote
      voteGrantedVote(ths->pVotesGranted, pMsg);

      // maybe to leader
      if (voteGrantedMajority(ths->pVotesGranted)) {
        if (!ths->pVotesGranted->toLeader) {
          syncNodeCandidate2Leader(ths);

          // prevent to leader again!
          ths->pVotesGranted->toLeader = true;
        }
      }
    } else {
      ;
      // do nothing
      // UNCHANGED <<votesGranted, voterLog>>
    }
  }

M
Minghao Li 已提交
268
  return 0;
269
}