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