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