Giter VIP home page Giter VIP logo

Comments (7)

belaban avatar belaban commented on July 24, 2024 1

Released 0.4.3.Final

from jgroups-raft.

belaban avatar belaban commented on July 24, 2024

No, this is a bug. Please create an issue and I will fix it.
However, is this a realistic scenario?

from jgroups-raft.

hypnoce avatar hypnoce commented on July 24, 2024

Hi,

I will create a test case for better understanding of the issue.

In some scenarios, like developing a service that uses groups-raft on my developer machine, it's a bit annoying to have to create 3 or 5 instances to get the consensus working.
Also, some test deployment of our infrastructure does not require fault tolerancy but needs to be very lightweight.
We could abstract the ReplicatedStatemachine and use another one for single node deployment. But it would have been great to be able to use the same one.
Last, when adding dynamically nodes to Raft using a service discovery mechanism, the first node that is launched will not be functional.

What do you think ?

Thanks

from jgroups-raft.

valdar avatar valdar commented on July 24, 2024

from jgroups-raft.

hypnoce avatar hypnoce commented on July 24, 2024

Hi,

To avoid changing a lot in the current RAFT protocol, I was thinking about adding a SINLGE_MEMBER protocol that will be lower than ELECTION and RAFT and will be used to fake other members. What do you think ?

A very ugly sample that seems to work for my use case.

package org.jgroups.protocols.raft;

import org.jgroups.Event;
import org.jgroups.Header;
import org.jgroups.Message;
import org.jgroups.stack.Protocol;
import org.jgroups.util.ExtendedUUID;

public class SINGLE_MEMBER extends Protocol {
    
    private RAFT raft;
    private ELECTION election;

    @Override
    public void init() throws Exception {
        raft=findProtocol(RAFT.class);
        election=findProtocol(ELECTION.class);
        super.init();
    }

    private  <T extends Protocol> T findProtocol(Class<T> clazz) {
        for(Protocol p=up_prot; p != null; p=p.getUpProtocol()) {
            if(p.getClass().equals(clazz))
                return (T)p;
        }
        throw new IllegalStateException(clazz.getSimpleName() + " not found above " + this.getClass().getSimpleName());
    }

    @Override
    public Object down(Message msg) {
        if (this.isSingleMember()) {
            Header electionHeader = msg.getHeader(election.getId());
            Header raftHeader = msg.getHeader(raft.getId());
            if (raftHeader != null && electionHeader != null) {
                throw new IllegalStateException("Message contains a raft[" + raftHeader.getClass() + "] and election[" + electionHeader.getClass() + "] headers.");
            }
            if (raftHeader != null) {
                handleRaftHeader(raftHeader);
            } else if (electionHeader != null) {
                handleElectionHeader(electionHeader);
            }
            return null;
        }
        return super.down(msg);
    }

    private void handleElectionHeader(Header header) {
        if (header instanceof VoteRequest) {
            VoteRequest voteRequest = (VoteRequest)header;
            VoteResponse voteResponse = new VoteResponse(voteRequest.term(), true);
            Message message = new Message();
            message.putHeader(election.getId(), voteResponse);
            this.up(message);
        }
    }

    private void handleRaftHeader(Header header) {
        if (header instanceof AppendEntriesRequest) {
            AppendEntriesRequest req = (AppendEntriesRequest) header;
            AppendResult appendResult = new AppendResult(true, req.prev_log_index + 1).commitIndex(raft.commitIndex());
            AppendEntriesResponse resp = new AppendEntriesResponse(req.term(), appendResult);

            /* Generate random sender because the leader has already voted */
            ExtendedUUID randomSender = ExtendedUUID.randomUUID();
            Message message = new Message(raft.leader()).src(randomSender);
            message.putHeader(raft.getId(), resp);
            this.up(message);
        }
    }

    private boolean isSingleMember() {
        return raft.majority() == 1;
    }
}

What do you think ?

from jgroups-raft.

belaban avatar belaban commented on July 24, 2024

I'd prefer handling a single member with the generic algorithm. If you define a single member, then the majority is 1 and a single member should therefore always become leader.

from jgroups-raft.

belaban avatar belaban commented on July 24, 2024

OK, the reason a single member won't form a cluster is that a vote request is never sent to self. When a vote request is received, we determine what happens (e.g. Candidate -> Leader etc). But since we never receive our own vote request, this doesn't happen.
I changed this, so vote requests are now sent to all members including self. I also added VoteTest.testSingleMember() to test this.

from jgroups-raft.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.