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;
}
Aggregations