Exemplo n.º 1
0
int raft_recv_requestvote_response(raft_server_t* me_, int node,
        msg_requestvote_response_t* r)
{
    raft_server_private_t* me = (void*)me_;

    __log(me_, NULL, "node responded to requestvote: %d status: %s",
            node, r->vote_granted == 1 ? "granted" : "not granted");

    if (raft_is_leader(me_))
        return 0;

    assert(node < me->num_nodes);

//    if (r->term != raft_get_current_term(me_))
//        return 0;

    if (1 == r->vote_granted)
    {
        int votes;

        me->votes_for_me[node] = 1;
        votes = raft_get_nvotes_for_me(me_);
        if (raft_votes_is_majority(me->num_nodes, votes))
            raft_become_leader(me_);
    }

    return 0;
}
Exemplo n.º 2
0
static void raft_claim(raft_t r) {
	assert(r->role == CANDIDATE);
	assert(r->leader == NOBODY);

	r->votes = 1; // vote for self
	if (raft_become_leader(r)) {
		// no need to send any messages, since we are alone
		return;
	}

	raft_msg_claim_t m;

	m.msg.msgtype = RAFT_MSG_CLAIM;
	m.msg.term = r->term;
	m.msg.from = r->me;

	m.index = r->log.first + r->log.size - 1;
	if (m.index >= 0) {
		m.term = RAFT_LOG(r, m.index).term;
	} else {
		m.term = -1;
	}

	int i;
	for (i = 0; i < r->config.peernum_max; i++) {
		if (!r->peers[i].up) continue;
		if (i == r->me) continue;
		raft_peer_t *s = r->peers + i;
		s->seqno++;
		m.msg.seqno = s->seqno;

		raft_send(r, i, &m, sizeof(m));
	}
}
Exemplo n.º 3
0
static void raft_handle_vote(raft_t r, raft_msg_vote_t *m) {
	int sender = m->msg.from;
	raft_peer_t *peer = r->peers + sender;
	if (m->msg.seqno != peer->seqno) return;
	peer->seqno++;
	if (m->msg.term < r->term) return;

	if (r->role != CANDIDATE) return;

	if (m->granted) {
		r->votes++;
	}

	raft_become_leader(r);
}
Exemplo n.º 4
0
int raft_recv_requestvote_response(raft_server_t* me_,
                                   raft_node_t* node,
                                   msg_requestvote_response_t* r)
{
    raft_server_private_t* me = (raft_server_private_t*)me_;

    __log(me_, node, "node responded to requestvote status: %s",
          r->vote_granted == 1 ? "granted" : "not granted");

    if (!raft_is_candidate(me_))
    {
        return 0;
    }
    else if (raft_get_current_term(me_) < r->term)
    {
        raft_set_current_term(me_, r->term);
        raft_become_follower(me_);
        return 0;
    }
    else if (raft_get_current_term(me_) != r->term)
    {
        /* The node who voted for us would have obtained our term.
         * Therefore this is an old message we should ignore.
         * This happens if the network is pretty choppy. */
        return 0;
    }

    __log(me_, node, "node responded to requestvote: %d status: %s ct:%d rt:%d",
          node, r->vote_granted == 1 ? "granted" : "not granted",
          me->current_term,
          r->term);

    if (1 == r->vote_granted)
    {
        if (node)
            raft_node_vote_for_me(node, 1);
        int votes = raft_get_nvotes_for_me(me_);
        if (raft_votes_is_majority(me->num_nodes, votes))
            raft_become_leader(me_);
    }

    return 0;
}
Exemplo n.º 5
0
void RaftConsensus::becomeMaster()
{
  OSS::mutex_lock lock(_raftMutex);
  raft_become_leader(_raft);
}