Search in sources :

Example 1 with KeepAliveEntry

use of io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry 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)1 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)1 DefaultRaftMember (io.atomix.protocols.raft.cluster.impl.DefaultRaftMember)1 KeepAliveResponse (io.atomix.protocols.raft.protocol.KeepAliveResponse)1 KeepAliveEntry (io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1