Search in sources :

Example 1 with QueryEntry

use of io.atomix.protocols.raft.storage.log.entry.QueryEntry in project atomix by atomix.

the class LeaderRole method onQuery.

@Override
public CompletableFuture<QueryResponse> onQuery(final QueryRequest request) {
    raft.checkThread();
    logRequest(request);
    // doesn't exist if the follower hasn't had a chance to see the session's registration entry.
    if (raft.getLastApplied() < request.session()) {
        return CompletableFuture.completedFuture(logResponse(QueryResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.UNKNOWN_SESSION, "Session has not yet been created. You're seeing into the future!").build()));
    }
    // Look up the client's session.
    RaftSession session = raft.getSessions().getSession(request.session());
    if (session == null) {
        log.warn("Unknown session {}", request.session());
        return CompletableFuture.completedFuture(logResponse(QueryResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.UNKNOWN_SESSION).build()));
    }
    final Indexed<QueryEntry> entry = new Indexed<>(request.index(), new QueryEntry(raft.getTerm(), System.currentTimeMillis(), request.session(), request.sequenceNumber(), request.operation()), 0);
    final CompletableFuture<QueryResponse> future;
    switch(session.readConsistency()) {
        case SEQUENTIAL:
            future = queryLocal(entry);
            break;
        case LINEARIZABLE_LEASE:
            future = queryBoundedLinearizable(entry);
            break;
        case LINEARIZABLE:
            future = queryLinearizable(entry);
            break;
        default:
            future = Futures.exceptionalFuture(new IllegalStateException("Unknown consistency level: " + session.readConsistency()));
            break;
    }
    return future.thenApply(this::logResponse);
}
Also used : QueryEntry(io.atomix.protocols.raft.storage.log.entry.QueryEntry) RaftSession(io.atomix.protocols.raft.session.RaftSession) QueryResponse(io.atomix.protocols.raft.protocol.QueryResponse) Indexed(io.atomix.storage.journal.Indexed)

Example 2 with QueryEntry

use of io.atomix.protocols.raft.storage.log.entry.QueryEntry in project atomix by atomix.

the class PassiveRole method onQuery.

@Override
public CompletableFuture<QueryResponse> onQuery(QueryRequest request) {
    raft.checkThread();
    logRequest(request);
    // doesn't exist if the follower hasn't had a chance to see the session's registration entry.
    if (raft.getState() != RaftContext.State.READY || raft.getLastApplied() < request.session()) {
        log.trace("State out of sync, forwarding query to leader");
        return queryForward(request);
    }
    // Look up the client's session.
    RaftSession session = raft.getSessions().getSession(request.session());
    if (session == null) {
        log.trace("State out of sync, forwarding query to leader");
        return queryForward(request);
    }
    // If the session's consistency level is SEQUENTIAL, handle the request here, otherwise forward it.
    if (session.readConsistency() == ReadConsistency.SEQUENTIAL) {
        // Forward the request to the leader.
        if (raft.getLogWriter().getLastIndex() < raft.getCommitIndex()) {
            log.trace("State out of sync, forwarding query to leader");
            return queryForward(request);
        }
        final Indexed<QueryEntry> entry = new Indexed<>(request.index(), new QueryEntry(raft.getTerm(), System.currentTimeMillis(), request.session(), request.sequenceNumber(), request.operation()), 0);
        return applyQuery(entry).thenApply(this::logResponse);
    } else {
        return queryForward(request);
    }
}
Also used : QueryEntry(io.atomix.protocols.raft.storage.log.entry.QueryEntry) RaftSession(io.atomix.protocols.raft.session.RaftSession) Indexed(io.atomix.storage.journal.Indexed)

Aggregations

RaftSession (io.atomix.protocols.raft.session.RaftSession)2 QueryEntry (io.atomix.protocols.raft.storage.log.entry.QueryEntry)2 Indexed (io.atomix.storage.journal.Indexed)2 QueryResponse (io.atomix.protocols.raft.protocol.QueryResponse)1