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

The following examples show how to use io.atomix.protocols.raft.protocol.JoinRequest. 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<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 #2
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> onJoin(JoinRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(JoinResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #3
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest, CompletableFuture<JoinResponse>> handler) {
  clusterCommunicator.subscribe(context.joinSubject, serializer::decode, handler, serializer::encode);
}
 
Example #4
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest, CompletableFuture<JoinResponse>> handler) {
  this.joinHandler = handler;
}
 
Example #5
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return getServer(memberId).thenCompose(listener -> listener.join(encode(request))).thenApply(this::decode);
}
 
Example #6
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest, CompletableFuture<JoinResponse>> handler) {
  registerHandler("join", handler);
}
 
Example #7
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return sendAndReceive(memberId, "join", request);
}
 
Example #8
Source File: RaftClusterContext.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively attempts to join the cluster.
 */
private void join(Iterator<RaftMemberContext> iterator) {
  if (iterator.hasNext()) {
    cancelJoinTimer();
    joinTimeout = raft.getThreadContext().schedule(raft.getElectionTimeout().multipliedBy(2), () -> {
      join(iterator);
    });

    RaftMemberContext member = iterator.next();

    log.debug("Attempting to join via {}", member.getMember().memberId());

    JoinRequest request = JoinRequest.builder()
        .withMember(new DefaultRaftMember(getMember().memberId(), getMember().getType(), getMember().getLastUpdated()))
        .build();
    raft.getProtocol().join(member.getMember().memberId(), request).whenCompleteAsync((response, error) -> {
      // Cancel the join timer.
      cancelJoinTimer();

      if (error == null) {
        if (response.status() == RaftResponse.Status.OK) {
          log.debug("Successfully joined via {}", member.getMember().memberId());

          Configuration configuration = new Configuration(response.index(), response.term(), response.timestamp(), response.members());

          // Configure the cluster with the join response.
          // Commit the configuration as we know it was committed via the successful join response.
          configure(configuration).commit();

          // If the local member is not present in the configuration, fail the future.
          if (!members.contains(this.member)) {
            joinFuture.completeExceptionally(new IllegalStateException("not a member of the cluster"));
          } else if (joinFuture != null) {
            joinFuture.complete(null);
          }
        } else if (response.error() == null || response.error().type() == RaftError.Type.CONFIGURATION_ERROR) {
          // If the response error is null, that indicates that no error occurred but the leader was
          // in a state that was incapable of handling the join request. Attempt to join the leader
          // again after an election timeout.
          log.debug("Failed to join {}", member.getMember().memberId());
          resetJoinTimer();
        } else {
          // If the response error was non-null, attempt to join via the next server in the members list.
          log.debug("Failed to join {}", member.getMember().memberId());
          join(iterator);
        }
      } else {
        log.debug("Failed to join {}", member.getMember().memberId());
        join(iterator);
      }
    }, raft.getThreadContext());
  }
  // If join attempts remain, schedule another attempt after two election timeouts. This allows enough time
  // for servers to potentially timeout and elect a leader.
  else {
    log.debug("Failed to join cluster, retrying...");
    resetJoinTimer();
  }
}
 
Example #9
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return sendAndReceive(memberId, "join", request);
}
 
Example #10
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return sendAndReceive(context.joinSubject, request, memberId);
}
 
Example #11
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest,
    CompletableFuture<JoinResponse>> handler) {
  this.joinHandler = handler;
}
 
Example #12
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.join(encode(request))).thenApply(this::decode);
}
 
Example #13
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest,
    CompletableFuture<JoinResponse>> handler) {
  registerHandler("join", handler);
}
 
Example #14
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return sendAndReceive(memberId, "join", request);
}
 
Example #15
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest,
    CompletableFuture<JoinResponse>> handler) {
  this.joinHandler = handler;
}
 
Example #16
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<JoinResponse> join(MemberId memberId, JoinRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.join(encode(request))).thenApply(this::decode);
}
 
Example #17
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerJoinHandler(Function<JoinRequest,
    CompletableFuture<JoinResponse>> handler) {
  registerHandler("join", handler);
}
 
Example #18
Source File: RaftRole.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Handles a join request.
 *
 * @param request The request to handle.
 * @return A completable future to be completed with the request response.
 */
CompletableFuture<JoinResponse> onJoin(JoinRequest request);