Search in sources :

Example 1 with TransferResponse

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

the class LeaderRole method onTransfer.

@Override
public CompletableFuture<TransferResponse> onTransfer(final TransferRequest request) {
    logRequest(request);
    RaftMemberContext member = raft.getCluster().getMemberState(request.member());
    if (member == null) {
        return CompletableFuture.completedFuture(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
    }
    transferring = true;
    CompletableFuture<TransferResponse> future = new CompletableFuture<>();
    appender.appendEntries(raft.getLogWriter().getLastIndex()).whenComplete((result, error) -> {
        if (isRunning()) {
            if (error == null) {
                log.debug("Transferring leadership to {}", request.member());
                raft.transition(RaftServer.Role.FOLLOWER);
                future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.OK).build()));
            } else if (error instanceof CompletionException && error.getCause() instanceof RaftException) {
                future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(((RaftException) error.getCause()).getType(), error.getMessage()).build()));
            } else if (error instanceof RaftException) {
                future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(((RaftException) error).getType(), error.getMessage()).build()));
            } else {
                future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR, error.getMessage()).build()));
            }
        } else {
            future.complete(logResponse(TransferResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftException(io.atomix.protocols.raft.RaftException) RaftMemberContext(io.atomix.protocols.raft.cluster.impl.RaftMemberContext) TransferResponse(io.atomix.protocols.raft.protocol.TransferResponse) CompletionException(java.util.concurrent.CompletionException)

Aggregations

RaftException (io.atomix.protocols.raft.RaftException)1 RaftMemberContext (io.atomix.protocols.raft.cluster.impl.RaftMemberContext)1 TransferResponse (io.atomix.protocols.raft.protocol.TransferResponse)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1