raft_handle_vote_resp_message.c 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <cli@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 "syncInt.h"
#include "raft.h"
L
lichuang 已提交
18
#include "sync_raft_impl.h"
19 20 21 22 23 24 25
#include "raft_message.h"

int syncRaftHandleVoteRespMessage(SSyncRaft* pRaft, const SSyncMessage* pMsg) {
  int granted, rejected;
  int quorum;
  int voterIndex;

26
  assert(pRaft->state == TAOS_SYNC_STATE_CANDIDATE);
27

L
lichuang 已提交
28 29
  SNodeInfo* pNode = syncRaftGetNodeById(pRaft, pMsg->from);
  if (pNode == NULL) {
30 31 32 33
    syncError("[%d:%d] recv vote resp from unknown server %d", pRaft->selfGroupId, pRaft->selfId, pMsg->from);
    return 0;
  }

34
  if (pRaft->state != TAOS_SYNC_STATE_CANDIDATE) {
35 36 37 38
    syncError("[%d:%d] is not candidate, ignore vote resp", pRaft->selfGroupId, pRaft->selfId);
    return 0;
  }

39
  ESyncRaftVoteResult result = syncRaftPollVote(pRaft, pMsg->from, 
40
                                pMsg->voteResp.cType == SYNC_RAFT_CAMPAIGN_PRE_ELECTION,
41
                                !pMsg->voteResp.rejected, &rejected, &granted);
42 43 44 45

  syncInfo("[%d:%d] [quorum:%d] has received %d votes and %d vote rejections",
    pRaft->selfGroupId, pRaft->selfId, quorum, granted, rejected);

46 47
  if (result == SYNC_RAFT_VOTE_WON) {
    if (pRaft->candidateState.inPreVote) {
48
      syncRaftCampaign(pRaft, SYNC_RAFT_CAMPAIGN_ELECTION);
49
    } else {
50
      syncRaftBecomeLeader(pRaft);
51
      syncRaftBroadcastAppend(pRaft);
52 53
    }
  } else if (result == SYNC_RAFT_VOTE_LOST) {
54 55
		// pb.MsgPreVoteResp contains future term of pre-candidate
		// m.Term > r.Term; reuse r.Term    
56 57
    syncRaftBecomeFollower(pRaft, pRaft->term, SYNC_NON_NODE_ID);
  }
58

59 60
  return 0;
}