Search in sources :

Example 1 with Indexed

use of io.atomix.storage.journal.Indexed in project atomix by atomix.

the class LeaderRole method onMetadata.

@Override
public CompletableFuture<MetadataResponse> onMetadata(MetadataRequest request) {
    raft.checkThread();
    logRequest(request);
    if (transferring) {
        return CompletableFuture.completedFuture(logResponse(MetadataResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.ILLEGAL_MEMBER_STATE).build()));
    }
    CompletableFuture<MetadataResponse> future = new CompletableFuture<>();
    Indexed<MetadataEntry> entry = new Indexed<>(raft.getLastApplied(), new MetadataEntry(raft.getTerm(), System.currentTimeMillis(), request.session()), 0);
    raft.getServiceManager().<MetadataResult>apply(entry).whenComplete((result, error) -> {
        if (error == null) {
            future.complete(logResponse(MetadataResponse.builder().withStatus(RaftResponse.Status.OK).withSessions(result.sessions()).build()));
        } else {
            future.complete(logResponse(MetadataResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR).build()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) MetadataResponse(io.atomix.protocols.raft.protocol.MetadataResponse) MetadataEntry(io.atomix.protocols.raft.storage.log.entry.MetadataEntry) Indexed(io.atomix.storage.journal.Indexed)

Example 2 with Indexed

use of io.atomix.storage.journal.Indexed 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 3 with Indexed

use of io.atomix.storage.journal.Indexed 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

Indexed (io.atomix.storage.journal.Indexed)3 RaftSession (io.atomix.protocols.raft.session.RaftSession)2 QueryEntry (io.atomix.protocols.raft.storage.log.entry.QueryEntry)2 MetadataResponse (io.atomix.protocols.raft.protocol.MetadataResponse)1 QueryResponse (io.atomix.protocols.raft.protocol.QueryResponse)1 MetadataEntry (io.atomix.protocols.raft.storage.log.entry.MetadataEntry)1 CompletableFuture (java.util.concurrent.CompletableFuture)1