int sender_send(raft_server_t* raft,
                void* udata,
                int peer,
                raft_message_type_e type,
                const unsigned char* data,
                int len)
{
    sender_t* me = udata;
    msg_t* m;

    m = malloc(sizeof(msg_t));
    m->type = type;
    m->len = len;
    m->data = malloc(len);
    m->sender = raft_get_nodeid(raft);
    memcpy(m->data,data,len);
    llqueue_offer(me->outbox,m);

    if (__nsenders > peer)
    {
        llqueue_offer(__senders[peer]->inbox, m);
    }

    return 0;
}
int __append_msg(
    sender_t* me,
    void* data,
    int type,
    int len,
    int peer,
    raft_server_t* raft
)
{
    msg_t* m;
    m = malloc(sizeof(msg_t));
    m->type = type;
    m->len = len;
    m->data = malloc(len);
    m->sender = raft_get_nodeid(raft);
    memcpy(m->data,data,len);
    llqueue_offer(me->outbox,m);

    if (__nsenders > peer)
    {
        llqueue_offer(__senders[peer]->inbox, m);
    }

    return 1;
}
Exemple #3
0
/* Candidate 5.2 */
void TestRaft_follower_becoming_candidate_votes_for_self(CuTest * tc)
{
    void *r = raft_new();
    CuAssertTrue(tc, -1 == raft_get_voted_for(r));
    raft_become_candidate(r);
    CuAssertTrue(tc, raft_get_nodeid(r) == raft_get_voted_for(r));
    CuAssertTrue(tc, 1 == raft_get_nvotes_for_me(r));
}
Exemple #4
0
int raft_get_nvotes_for_me(raft_server_t* me_)
{
    raft_server_private_t* me = (raft_server_private_t*)me_;
    int i, votes;

    for (i = 0, votes = 0; i < me->num_nodes; i++)
        if (me->node != me->nodes[i] && raft_node_is_voting(me->nodes[i]))
            if (raft_node_has_vote_for_me(me->nodes[i]))
                votes += 1;

    if (me->voted_for == raft_get_nodeid(me_))
        votes += 1;

    return votes;
}
Exemple #5
0
int raft_send_requestvote(raft_server_t* me_, int node)
{
    raft_server_private_t* me = (raft_server_private_t*)me_;
    msg_requestvote_t rv;

    __log(me_, "sending requestvote to: %d", node);

    rv.term = me->current_term;
    rv.last_log_idx = raft_get_current_idx(me_);
    rv.last_log_term = raft_get_current_term(me_);
    rv.candidate_id = raft_get_nodeid(me_);
    if (me->cb.send_requestvote)
        me->cb.send_requestvote(me_, me->udata, node, &rv);
    return 0;
}