Search in sources :

Example 1 with LeaveResponse

use of io.atomix.protocols.raft.protocol.LeaveResponse in project atomix by atomix.

the class LeaderRole method onLeave.

@Override
public CompletableFuture<LeaveResponse> onLeave(final LeaveRequest request) {
    raft.checkThread();
    logRequest(request);
    // See https://groups.google.com/forum/#!topic/raft-dev/t4xj6dJTP6E
    if (configuring() || initializing()) {
        return CompletableFuture.completedFuture(logResponse(LeaveResponse.builder().withStatus(RaftResponse.Status.ERROR).build()));
    }
    // If the leaving member is not a known member of the cluster, complete the leave successfully.
    if (raft.getCluster().getMember(request.member().nodeId()) == null) {
        return CompletableFuture.completedFuture(logResponse(LeaveResponse.builder().withStatus(RaftResponse.Status.OK).withMembers(raft.getCluster().getMembers()).build()));
    }
    RaftMember member = request.member();
    Collection<RaftMember> members = raft.getCluster().getMembers();
    members.remove(member);
    CompletableFuture<LeaveResponse> future = new CompletableFuture<>();
    configure(members).whenComplete((index, error) -> {
        if (error == null) {
            future.complete(logResponse(LeaveResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(index).withTerm(raft.getCluster().getConfiguration().term()).withTime(raft.getCluster().getConfiguration().time()).withMembers(members).build()));
        } else {
            future.complete(logResponse(LeaveResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR).build()));
        }
    });
    return future;
}
Also used : RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) CompletableFuture(java.util.concurrent.CompletableFuture) LeaveResponse(io.atomix.protocols.raft.protocol.LeaveResponse)

Aggregations

RaftMember (io.atomix.protocols.raft.cluster.RaftMember)1 DefaultRaftMember (io.atomix.protocols.raft.cluster.impl.DefaultRaftMember)1 LeaveResponse (io.atomix.protocols.raft.protocol.LeaveResponse)1 CompletableFuture (java.util.concurrent.CompletableFuture)1