Search in sources :

Example 1 with OpenSessionRequest

use of io.atomix.protocols.raft.protocol.OpenSessionRequest in project atomix by atomix.

the class RaftProxyManager method openSession.

/**
 * 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<RaftProxyState> openSession(String serviceName, PrimitiveType primitiveType, 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().withNodeId(nodeId).withServiceName(serviceName).withServiceType(primitiveType).withReadConsistency(readConsistency).withMinTimeout(minTimeout.toMillis()).withMaxTimeout(maxTimeout.toMillis()).build();
    CompletableFuture<RaftProxyState> 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.
                RaftProxyState state = new RaftProxyState(clientId, SessionId.from(response.session()), serviceName, primitiveType, response.timeout());
                sessions.put(state.getSessionId().id(), state);
                state.addStateChangeListener(s -> {
                    if (s == PrimitiveProxy.State.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;
}
Also used : OpenSessionRequest(io.atomix.protocols.raft.protocol.OpenSessionRequest) CompletableFuture(java.util.concurrent.CompletableFuture) RaftException(io.atomix.protocols.raft.RaftException) ThreadContext(io.atomix.utils.concurrent.ThreadContext)

Aggregations

RaftException (io.atomix.protocols.raft.RaftException)1 OpenSessionRequest (io.atomix.protocols.raft.protocol.OpenSessionRequest)1 ThreadContext (io.atomix.utils.concurrent.ThreadContext)1 CompletableFuture (java.util.concurrent.CompletableFuture)1