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

The following examples show how to use io.atomix.protocols.raft.protocol.OpenSessionRequest. 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<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 #2
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 #3
Source File: RaftSessionConnection.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Sends an open session request to the given node.
 *
 * @param request the request to send
 * @return a future to be completed with the response
 */
public CompletableFuture<OpenSessionResponse> openSession(OpenSessionRequest request) {
  CompletableFuture<OpenSessionResponse> future = new CompletableFuture<>();
  if (context.isCurrentContext()) {
    sendRequest(request, protocol::openSession, future);
  } else {
    context.execute(() -> sendRequest(request, protocol::openSession, future));
  }
  return future;
}
 
Example #4
Source File: RaftSessionManager.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Opens a new session.
 *
 * @param serviceName           The session name.
 * @param primitiveType         The session type.
 * @param communicationStrategy The strategy with which to communicate with servers.
 * @param minTimeout            The minimum session timeout.
 * @param maxTimeout            The maximum session timeout.
 * @return A completable future to be completed once the session has been opened.
 */
public CompletableFuture<RaftSessionState> openSession(
    String serviceName,
    PrimitiveType primitiveType,
    ServiceConfig config,
    ReadConsistency readConsistency,
    CommunicationStrategy communicationStrategy,
    Duration minTimeout,
    Duration maxTimeout) {
  checkNotNull(serviceName, "serviceName cannot be null");
  checkNotNull(primitiveType, "serviceType cannot be null");
  checkNotNull(communicationStrategy, "communicationStrategy cannot be null");
  checkNotNull(maxTimeout, "timeout cannot be null");

  log.debug("Opening session; name: {}, type: {}", serviceName, primitiveType);
  OpenSessionRequest request = OpenSessionRequest.builder()
      .withMemberId(memberId)
      .withServiceName(serviceName)
      .withServiceType(primitiveType)
      .withServiceConfig(Serializer.using(primitiveType.namespace()).encode(config))
      .withReadConsistency(readConsistency)
      .withMinTimeout(minTimeout.toMillis())
      .withMaxTimeout(maxTimeout.toMillis())
      .build();

  CompletableFuture<RaftSessionState> future = new CompletableFuture<>();
  ThreadContext proxyContext = threadContextFactory.createContext();
  connection.openSession(request).whenCompleteAsync((response, error) -> {
    if (error == null) {
      if (response.status() == RaftResponse.Status.OK) {
        // Create and store the proxy state.
        RaftSessionState state = new RaftSessionState(
            clientId,
            SessionId.from(response.session()),
            serviceName,
            primitiveType,
            response.timeout());
        sessions.put(state.getSessionId().id(), state);

        state.addStateChangeListener(s -> {
          if (s == PrimitiveState.EXPIRED || s == PrimitiveState.CLOSED) {
            sessions.remove(state.getSessionId().id());
          }
        });

        // Ensure the proxy session info is reset and the session is kept alive.
        keepAliveSessions(System.currentTimeMillis(), state.getSessionTimeout());

        future.complete(state);
      } else {
        future.completeExceptionally(new RaftException.Unavailable(response.error().message()));
      }
    } else {
      future.completeExceptionally(new RaftException.Unavailable(error.getMessage()));
    }
  }, proxyContext);
  return future;
}
 
Example #5
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerOpenSessionHandler(Function<OpenSessionRequest, CompletableFuture<OpenSessionResponse>> handler) {
  this.openSessionHandler = handler;
}
 
Example #6
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> openSession(MemberId memberId, OpenSessionRequest request) {
  return getServer(memberId).thenCompose(listener -> listener.openSession(encode(request))).thenApply(this::decode);
}
 
Example #7
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerOpenSessionHandler(Function<OpenSessionRequest, CompletableFuture<OpenSessionResponse>> handler) {
  registerHandler("open-session", handler);
}
 
Example #8
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> openSession(MemberId memberId, OpenSessionRequest request) {
  return sendAndReceive(memberId, "open-session", request);
}
 
Example #9
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> openSession(MemberId memberId, OpenSessionRequest request) {
  return getServer(memberId).thenCompose(protocol -> protocol.openSession(encode(request))).thenApply(this::decode);
}
 
Example #10
Source File: RaftClientMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> openSession(MemberId memberId, OpenSessionRequest request) {
  return sendAndReceive(memberId, "open-session", request);
}
 
Example #11
Source File: LeaderRole.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> onOpenSession(OpenSessionRequest request) {
  final long term = raft.getTerm();
  final long timestamp = System.currentTimeMillis();
  final long minTimeout = request.minTimeout();

  // If the client submitted a session timeout, use the client's timeout, otherwise use the configured
  // default server session timeout.
  final long maxTimeout;
  if (request.maxTimeout() != 0) {
    maxTimeout = request.maxTimeout();
  } else {
    maxTimeout = raft.getSessionTimeout().toMillis();
  }

  raft.checkThread();
  logRequest(request);

  CompletableFuture<OpenSessionResponse> future = new CompletableFuture<>();
  appendAndCompact(new OpenSessionEntry(
      term,
      timestamp,
      request.node(),
      request.serviceName(),
      request.serviceType(),
      request.serviceConfig(),
      request.readConsistency(),
      minTimeout,
      maxTimeout))
      .whenCompleteAsync((entry, error) -> {
        if (error != null) {
          future.complete(logResponse(OpenSessionResponse.builder()
              .withStatus(RaftResponse.Status.ERROR)
              .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()).whenComplete((sessionId, sessionError) -> {
                if (sessionError == null) {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.OK)
                      .withSession(sessionId)
                      .withTimeout(maxTimeout)
                      .build()));
                } else if (sessionError instanceof CompletionException && sessionError.getCause() instanceof RaftException) {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(((RaftException) sessionError.getCause()).getType(), sessionError.getMessage())
                      .build()));
                } else if (sessionError instanceof RaftException) {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(((RaftException) sessionError).getType(), sessionError.getMessage())
                      .build()));
                } else {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(RaftError.Type.PROTOCOL_ERROR, sessionError.getMessage())
                      .build()));
                }
              });
            } else {
              future.complete(logResponse(OpenSessionResponse.builder()
                  .withStatus(RaftResponse.Status.ERROR)
                  .withError(RaftError.Type.PROTOCOL_ERROR)
                  .build()));
            }
          } else {
            future.complete(logResponse(OpenSessionResponse.builder()
                .withStatus(RaftResponse.Status.ERROR)
                .withError(RaftError.Type.ILLEGAL_MEMBER_STATE)
                .build()));
          }
        });
      }, raft.getThreadContext());

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