io.atomix.protocols.raft.RaftError Java Examples

The following examples show how to use io.atomix.protocols.raft.RaftError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<ReconfigureResponse> onReconfigure(ReconfigureRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(ReconfigureResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::reconfigure)
        .exceptionally(error -> ReconfigureResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #2
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> onJoin(JoinRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(JoinResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::join)
        .exceptionally(error -> JoinResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #3
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<CloseSessionResponse> onCloseSession(CloseSessionRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(CloseSessionResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::closeSession)
        .exceptionally(error -> CloseSessionResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #4
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> onOpenSession(OpenSessionRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(OpenSessionResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::openSession)
        .exceptionally(error -> OpenSessionResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #5
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> onKeepAlive(KeepAliveRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(KeepAliveResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::keepAlive)
        .exceptionally(error -> KeepAliveResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #6
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<CommandResponse> onCommand(CommandRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(CommandResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::command)
        .exceptionally(error -> CommandResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #7
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(QueryResponse response, Throwable error) {
  if (error == null) {
    if (response.status() == RaftResponse.Status.OK) {
      complete(response);
    } else if (response.error().type() == RaftError.Type.UNKNOWN_CLIENT
        || response.error().type() == RaftError.Type.UNKNOWN_SESSION) {
      complete(response.error().createException());
      state.setState(PrimitiveState.EXPIRED);
    } else if (response.error().type() == RaftError.Type.UNKNOWN_SERVICE
        || response.error().type() == RaftError.Type.CLOSED_SESSION) {
      complete(response.error().createException());
      state.setState(PrimitiveState.CLOSED);
    } else {
      complete(response.error().createException());
    }
  } else {
    fail(error);
  }
}
 
Example #8
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<LeaveResponse> onLeave(LeaveRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(LeaveResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::leave)
        .exceptionally(error -> LeaveResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #9
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Forwards the query to the leader.
 */
private CompletableFuture<QueryResponse> queryForward(QueryRequest request) {
  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(QueryResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  }

  log.trace("Forwarding {}", request);
  return forward(request, raft.getProtocol()::query)
      .exceptionally(error -> QueryResponse.builder()
          .withStatus(RaftResponse.Status.ERROR)
          .withError(RaftError.Type.NO_LEADER)
          .build())
      .thenApply(this::logResponse);
}
 
Example #10
Source File: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<MetadataResponse> onMetadata(MetadataRequest request) {
  raft.checkThread();
  logRequest(request);

  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(MetadataResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  } else {
    return forward(request, raft.getProtocol()::metadata)
        .exceptionally(error -> MetadataResponse.builder()
            .withStatus(RaftResponse.Status.ERROR)
            .withError(RaftError.Type.NO_LEADER)
            .build())
        .thenApply(this::logResponse);
  }
}
 
Example #11
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> onQuery(QueryRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(QueryResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #12
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<InstallResponse> onInstall(InstallRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(InstallResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #13
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> onOpenSession(OpenSessionRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(OpenSessionResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #14
Source File: OperationResponse.java    From atomix with Apache License 2.0 5 votes vote down vote up
public OperationResponse(Status status, RaftError error, long index, long eventIndex, byte[] result, long lastSequence) {
  super(status, error);
  this.index = index;
  this.eventIndex = eventIndex;
  this.result = result;
  this.lastSequence = lastSequence;
}
 
Example #15
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<ReconfigureResponse> onReconfigure(ReconfigureRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(ReconfigureResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #16
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<LeaveResponse> onLeave(LeaveRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(LeaveResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #17
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<TransferResponse> onTransfer(TransferRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(TransferResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #18
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<AppendResponse> onAppend(AppendRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(AppendResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #19
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<PollResponse> onPoll(PollRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(PollResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #20
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(VoteRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(VoteResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #21
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<CommandResponse> onCommand(CommandRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(CommandResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #22
Source File: DefaultRaftMember.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively reconfigures the cluster.
 */
private void configure(RaftMember.Type type, CompletableFuture<Void> future) {
  // Set a timer to retry the attempt to leave the cluster.
  configureTimeout = cluster.getContext().getThreadContext().schedule(cluster.getContext().getElectionTimeout(), () -> {
    configure(type, future);
  });

  // Attempt to leave the cluster by submitting a LeaveRequest directly to the server state.
  // Non-leader states should forward the request to the leader if there is one. Leader states
  // will log, replicate, and commit the reconfiguration.
  cluster.getContext().getRaftRole().onReconfigure(ReconfigureRequest.builder()
      .withIndex(cluster.getConfiguration().index())
      .withTerm(cluster.getConfiguration().term())
      .withMember(new DefaultRaftMember(id, type, updated))
      .build()).whenComplete((response, error) -> {
        if (error == null) {
          if (response.status() == RaftResponse.Status.OK) {
            cancelConfigureTimer();
            cluster.configure(new Configuration(response.index(), response.term(), response.timestamp(), response.members()));
            future.complete(null);
          } else if (response.error() == null
              || response.error().type() == RaftError.Type.UNAVAILABLE
              || response.error().type() == RaftError.Type.PROTOCOL_ERROR
              || response.error().type() == RaftError.Type.NO_LEADER) {
            cancelConfigureTimer();
            configureTimeout = cluster.getContext().getThreadContext().schedule(cluster.getContext().getElectionTimeout().multipliedBy(2), () -> configure(type, future));
          } else {
            cancelConfigureTimer();
            future.completeExceptionally(response.error().createException());
          }
        } else {
          future.completeExceptionally(error);
        }
      });
}
 
Example #23
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> onKeepAlive(KeepAliveRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(KeepAliveResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #24
Source File: LeaderRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Fails pending commands.
 */
private void failPendingCommands() {
  for (RaftSession session : raft.getSessions().getSessions()) {
    for (PendingCommand command : session.clearCommands()) {
      command.future().complete(logResponse(CommandResponse.builder()
          .withStatus(RaftResponse.Status.ERROR)
          .withError(RaftError.Type.COMMAND_FAILURE, "Request sequence number " + command.request().sequenceNumber() + " out of sequence")
          .withLastSequence(session.getRequestSequence())
          .build()));
    }
  }
}
 
Example #25
Source File: PassiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(VoteRequest request) {
  raft.checkThread();
  logRequest(request);
  updateTermAndLeader(request.term(), null);

  return CompletableFuture.completedFuture(logResponse(VoteResponse.builder()
      .withStatus(RaftResponse.Status.ERROR)
      .withError(RaftError.Type.ILLEGAL_MEMBER_STATE, "Cannot request vote from RESERVE member")
      .build()));
}
 
Example #26
Source File: PassiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<PollResponse> onPoll(PollRequest request) {
  raft.checkThread();
  logRequest(request);

  return CompletableFuture.completedFuture(logResponse(PollResponse.builder()
      .withStatus(RaftResponse.Status.ERROR)
      .withError(RaftError.Type.ILLEGAL_MEMBER_STATE, "Cannot poll RESERVE member")
      .build()));
}
 
Example #27
Source File: LeaderRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Executes a linearizable query.
 * <p>
 * Linearizable queries are first sequenced with commands and then applied to the state machine. Once
 * applied, we verify the node's leadership prior to responding successfully to the query.
 */
private CompletableFuture<QueryResponse> queryLinearizable(Indexed<QueryEntry> entry) {
  return applyQuery(entry)
      .thenComposeAsync(response -> appender.appendEntries()
          .thenApply(index -> response)
          .exceptionally(error -> QueryResponse.builder()
              .withStatus(RaftResponse.Status.ERROR)
              .withError(RaftError.Type.QUERY_FAILURE, error.getMessage())
              .build()), raft.getThreadContext());
}
 
Example #28
Source File: PassiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Completes an operation.
 */
protected <T extends OperationResponse> void completeOperation(OperationResult result, OperationResponse.Builder<?, T> builder, Throwable error, CompletableFuture<T> future) {
  if (result != null) {
    builder.withIndex(result.index());
    builder.withEventIndex(result.eventIndex());
    if (result.failed()) {
      error = result.error();
    }
  }

  if (error == null) {
    if (result == null) {
      future.complete(builder.withStatus(RaftResponse.Status.ERROR)
          .withError(RaftError.Type.PROTOCOL_ERROR)
          .build());
    } else {
      future.complete(builder.withStatus(RaftResponse.Status.OK)
          .withResult(result.result())
          .build());
    }
  } else if (error instanceof CompletionException && error.getCause() instanceof RaftException) {
    future.complete(builder.withStatus(RaftResponse.Status.ERROR)
        .withError(((RaftException) error.getCause()).getType(), error.getMessage())
        .build());
  } else if (error instanceof RaftException) {
    future.complete(builder.withStatus(RaftResponse.Status.ERROR)
        .withError(((RaftException) error).getType(), error.getMessage())
        .build());
  } else if (error instanceof PrimitiveException.ServiceException) {
    log.warn("An application error occurred: {}", error.getCause());
    future.complete(builder.withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.APPLICATION_ERROR)
        .build());
  } else {
    log.warn("An unexpected error occurred: {}", error);
    future.complete(builder.withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.PROTOCOL_ERROR, error.getMessage())
        .build());
  }
}
 
Example #29
Source File: LeaderRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source File: RaftContext.java    From atomix with Apache License 2.0 5 votes vote down vote up
private <R extends RaftResponse> CompletableFuture<R> runOnContextIfReady(
    Supplier<CompletableFuture<R>> function, Supplier<RaftResponse.Builder<?, R>> builderSupplier) {
  if (state == State.READY) {
    return runOnContext(function);
  } else {
    return CompletableFuture.completedFuture(builderSupplier.get()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.ILLEGAL_MEMBER_STATE)
        .build());
  }
}