io.atomix.protocols.raft.protocol.KeepAliveRequest Java Examples

The following examples show how to use io.atomix.protocols.raft.protocol.KeepAliveRequest. 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<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 #2
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 #3
Source File: RaftSessionConnection.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a keep alive request to the given node.
 *
 * @param request the request to send
 * @return a future to be completed with the response
 */
public CompletableFuture<KeepAliveResponse> keepAlive(KeepAliveRequest request) {
  CompletableFuture<KeepAliveResponse> future = new CompletableFuture<>();
  if (context.isCurrentContext()) {
    sendRequest(request, protocol::keepAlive, future);
  } else {
    context.execute(() -> sendRequest(request, protocol::keepAlive, future));
  }
  return future;
}
 
Example #4
Source File: RaftSessionManager.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Resets indexes for all sessions.
 */
private synchronized void resetAllIndexes() {
  Collection<RaftSessionState> sessions = Lists.newArrayList(this.sessions.values());

  // If no sessions are open, skip the keep-alive.
  if (sessions.isEmpty()) {
    return;
  }

  // Allocate session IDs, command response sequence numbers, and event index arrays.
  long[] sessionIds = new long[sessions.size()];
  long[] commandResponses = new long[sessions.size()];
  long[] eventIndexes = new long[sessions.size()];

  // For each session that needs to be kept alive, populate batch request arrays.
  int i = 0;
  for (RaftSessionState sessionState : sessions) {
    sessionIds[i] = sessionState.getSessionId().id();
    commandResponses[i] = sessionState.getCommandResponse();
    eventIndexes[i] = sessionState.getEventIndex();
    i++;
  }

  log.trace("Resetting {} sessions", sessionIds.length);

  KeepAliveRequest request = KeepAliveRequest.builder()
      .withSessionIds(sessionIds)
      .withCommandSequences(commandResponses)
      .withEventIndexes(eventIndexes)
      .build();
  connection.keepAlive(request);
}
 
Example #5
Source File: RaftSessionManager.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Resets indexes for the given session.
 *
 * @param sessionId The session for which to reset indexes.
 * @return A completable future to be completed once the session's indexes have been reset.
 */
CompletableFuture<Void> resetIndexes(SessionId sessionId) {
  RaftSessionState sessionState = sessions.get(sessionId.id());
  if (sessionState == null) {
    return Futures.exceptionalFuture(new IllegalArgumentException("Unknown session: " + sessionId));
  }

  CompletableFuture<Void> future = new CompletableFuture<>();

  KeepAliveRequest request = KeepAliveRequest.builder()
      .withSessionIds(new long[]{sessionId.id()})
      .withCommandSequences(new long[]{sessionState.getCommandResponse()})
      .withEventIndexes(new long[]{sessionState.getEventIndex()})
      .build();

  connection.keepAlive(request).whenComplete((response, error) -> {
    if (error == null) {
      if (response.status() == RaftResponse.Status.OK) {
        future.complete(null);
      } else {
        future.completeExceptionally(response.error().createException());
      }
    } else {
      future.completeExceptionally(error);
    }
  });
  return future;
}
 
Example #6
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest, CompletableFuture<KeepAliveResponse>> handler) {
  this.keepAliveHandler = handler;
}
 
Example #7
Source File: RaftClientMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return sendAndReceive(memberId, "keep-alive", request);
}
 
Example #8
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId, KeepAliveRequest request) {
  return getServer(memberId).thenCompose(listener -> listener.keepAlive(encode(request))).thenApply(this::decode);
}
 
Example #9
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest, CompletableFuture<KeepAliveResponse>> handler) {
  registerHandler("keep-alive", handler);
}
 
Example #10
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId, KeepAliveRequest request) {
  return sendAndReceive(memberId, "keep-alive", request);
}
 
Example #11
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId, KeepAliveRequest request) {
  return getServer(memberId).thenCompose(protocol -> protocol.keepAlive(encode(request))).thenApply(this::decode);
}
 
Example #12
Source File: RaftClientMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId, KeepAliveRequest request) {
  return sendAndReceive(memberId, "keep-alive", request);
}
 
Example #13
Source File: LeaderRole.java    From atomix with Apache License 2.0 4 votes vote down vote up
@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().memberId())
              .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().memberId())
                      .withMembers(raft.getCluster().getMembers().stream()
                          .map(RaftMember::memberId)
                          .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().memberId())
                      .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().memberId())
                      .withError(((RaftException) sessionError).getType(), sessionError.getMessage())
                      .build()));
                } else {
                  future.complete(logResponse(KeepAliveResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withLeader(raft.getCluster().getMember().memberId())
                      .withError(RaftError.Type.PROTOCOL_ERROR, sessionError.getMessage())
                      .build()));
                }
              }, raft.getThreadContext());
            } else {
              future.complete(logResponse(KeepAliveResponse.builder()
                  .withStatus(RaftResponse.Status.ERROR)
                  .withLeader(raft.getCluster().getMember().memberId())
                  .withError(RaftError.Type.PROTOCOL_ERROR)
                  .build()));
            }
          } else {
            RaftMember leader = raft.getLeader();
            future.complete(logResponse(KeepAliveResponse.builder()
                .withStatus(RaftResponse.Status.ERROR)
                .withLeader(leader != null ? leader.memberId() : null)
                .withError(RaftError.Type.ILLEGAL_MEMBER_STATE)
                .build()));
          }
        });
      }, raft.getThreadContext());

  return future;
}
 
Example #14
Source File: RaftClientCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId, KeepAliveRequest request) {
  return sendAndReceive(context.keepAliveSubject, request, memberId);
}
 
Example #15
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest, CompletableFuture<KeepAliveResponse>> handler) {
  clusterCommunicator.subscribe(context.keepAliveSubject, serializer::decode, handler, serializer::encode);
}
 
Example #16
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId, KeepAliveRequest request) {
  return sendAndReceive(context.keepAliveSubject, request, memberId);
}
 
Example #17
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest,
    CompletableFuture<KeepAliveResponse>> handler) {
  this.keepAliveHandler = handler;
}
 
Example #18
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.keepAlive(encode(request))).thenApply(this::decode);
}
 
Example #19
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest,
    CompletableFuture<KeepAliveResponse>> handler) {
  registerHandler("keep-alive", handler);
}
 
Example #20
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return sendAndReceive(memberId, "keep-alive", request);
}
 
Example #21
Source File: LocalRaftClientProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return getServer(memberId).thenCompose(protocol ->
      protocol.keepAlive(encode(request))).thenApply(this::decode);
}
 
Example #22
Source File: RaftClientMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return sendAndReceive(memberId, "keep-alive", request);
}
 
Example #23
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest,
    CompletableFuture<KeepAliveResponse>> handler) {
  this.keepAliveHandler = handler;
}
 
Example #24
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.keepAlive(encode(request))).thenApply(this::decode);
}
 
Example #25
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerKeepAliveHandler(Function<KeepAliveRequest,
    CompletableFuture<KeepAliveResponse>> handler) {
  registerHandler("keep-alive", handler);
}
 
Example #26
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return sendAndReceive(memberId, "keep-alive", request);
}
 
Example #27
Source File: LocalRaftClientProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<KeepAliveResponse> keepAlive(MemberId memberId,
                                                      KeepAliveRequest request) {
  return getServer(memberId).thenCompose(protocol ->
      protocol.keepAlive(encode(request))).thenApply(this::decode);
}
 
Example #28
Source File: RaftRole.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Handles a keep alive request.
 *
 * @param request The request to handle.
 * @return A completable future to be completed with the request response.
 */
CompletableFuture<KeepAliveResponse> onKeepAlive(KeepAliveRequest request);