Search in sources :

Example 1 with RaftException

use of io.atomix.protocols.raft.RaftException 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)

Example 2 with RaftException

use of io.atomix.protocols.raft.RaftException in project atomix by atomix.

the class LeaderRole method onKeepAlive.

@Override
public CompletableFuture<KeepAliveResponse> onKeepAlive(KeepAliveRequest request) {
    final long term = raft.getTerm();
    final long timestamp = System.currentTimeMillis();
    raft.checkThread();
    logRequest(request);
    CompletableFuture<KeepAliveResponse> future = new CompletableFuture<>();
    appendAndCompact(new KeepAliveEntry(term, timestamp, request.sessionIds(), request.commandSequenceNumbers(), request.eventIndexes())).whenCompleteAsync((entry, error) -> {
        if (error != null) {
            future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(RaftError.Type.PROTOCOL_ERROR).build()));
            return;
        }
        appender.appendEntries(entry.index()).whenComplete((commitIndex, commitError) -> {
            raft.checkThread();
            if (isRunning()) {
                if (commitError == null) {
                    raft.getServiceManager().<long[]>apply(entry.index()).whenCompleteAsync((sessionResult, sessionError) -> {
                        if (sessionError == null) {
                            future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.OK).withLeader(raft.getCluster().getMember().nodeId()).withMembers(raft.getCluster().getMembers().stream().map(RaftMember::nodeId).filter(m -> m != null).collect(Collectors.toList())).withSessionIds(sessionResult).build()));
                        } else if (sessionError instanceof CompletionException && sessionError.getCause() instanceof RaftException) {
                            future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(((RaftException) sessionError.getCause()).getType(), sessionError.getMessage()).build()));
                        } else if (sessionError instanceof RaftException) {
                            future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(((RaftException) sessionError).getType(), sessionError.getMessage()).build()));
                        } else {
                            future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(RaftError.Type.PROTOCOL_ERROR, sessionError.getMessage()).build()));
                        }
                    }, raft.getThreadContext());
                } else {
                    future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(raft.getCluster().getMember().nodeId()).withError(RaftError.Type.PROTOCOL_ERROR).build()));
                }
            } else {
                RaftMember leader = raft.getLeader();
                future.complete(logResponse(KeepAliveResponse.builder().withStatus(RaftResponse.Status.ERROR).withLeader(leader != null ? leader.nodeId() : null).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
            }
        });
    }, raft.getThreadContext());
    return future;
}
Also used : KeepAliveResponse(io.atomix.protocols.raft.protocol.KeepAliveResponse) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) CompletableFuture(java.util.concurrent.CompletableFuture) KeepAliveEntry(io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry) RaftException(io.atomix.protocols.raft.RaftException) CompletionException(java.util.concurrent.CompletionException)

Aggregations

RaftException (io.atomix.protocols.raft.RaftException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionException (java.util.concurrent.CompletionException)2 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)1 DefaultRaftMember (io.atomix.protocols.raft.cluster.impl.DefaultRaftMember)1 RaftMemberContext (io.atomix.protocols.raft.cluster.impl.RaftMemberContext)1 KeepAliveResponse (io.atomix.protocols.raft.protocol.KeepAliveResponse)1 TransferResponse (io.atomix.protocols.raft.protocol.TransferResponse)1 KeepAliveEntry (io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry)1